]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk.c
bt: use qemu_chr_alloc()
[mirror_qemu.git] / ui / gtk.c
CommitLineData
a4ccabcf
AL
1/*
2 * GTK UI
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 * Portions from gtk-vnc:
13 *
14 * GTK VNC Widget
15 *
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
18 *
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.0 of the License, or (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 */
33
834574ea
AL
34#define GETTEXT_PACKAGE "qemu"
35#define LOCALEDIR "po"
36
e16f4c87 37#include "qemu/osdep.h"
c95e3080 38#include "qemu-common.h"
f348b6d1 39#include "qemu/cutils.h"
c95e3080 40
dc7ff344
GH
41#include "ui/console.h"
42#include "ui/gtk.h"
c95e3080 43
834574ea 44#include <glib/gi18n.h>
3f58eade 45#include <locale.h>
bbbf9bfb 46#if defined(CONFIG_VTE)
a4ccabcf 47#include <vte/vte.h>
bbbf9bfb 48#endif
a4ccabcf
AL
49#include <math.h>
50
ef0dd982 51#include "trace.h"
af98ba92 52#include "ui/input.h"
a4ccabcf
AL
53#include "sysemu/sysemu.h"
54#include "qmp-commands.h"
55#include "x_keymap.h"
56#include "keymaps.h"
dccfcd0e 57#include "sysemu/char.h"
6a24ced5 58#include "qom/object.h"
a4ccabcf 59
d861def3 60#define MAX_VCS 10
82fc1809
GH
61#define VC_WINDOW_X_MIN 320
62#define VC_WINDOW_Y_MIN 240
63#define VC_TERM_X_MIN 80
64#define VC_TERM_Y_MIN 25
65#define VC_SCALE_MIN 0.25
66#define VC_SCALE_STEP 0.25
d861def3 67
bbbf9bfb
SW
68#if !defined(CONFIG_VTE)
69# define VTE_CHECK_VERSION(a, b, c) 0
70#endif
cba68834 71
6fa27697
GH
72#if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
73/*
74 * The gtk2 vte terminal widget seriously messes up the window resize
75 * for some reason. You basically can't make the qemu window smaller
76 * any more because the toplevel window geoemtry hints are overridden.
77 *
78 * Workaround that by hiding all vte widgets, except the one in the
79 * current tab.
80 *
81 * Luckily everything works smooth in gtk3.
82 */
83# define VTE_RESIZE_HACK 1
84#endif
85
ef6413a2
DB
86#if !GTK_CHECK_VERSION(2, 20, 0)
87#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
88#endif
89
0a337ed0
GH
90#ifndef GDK_IS_X11_DISPLAY
91#define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
92#endif
a8ffb372
DB
93#ifndef GDK_IS_WAYLAND_DISPLAY
94#define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
95#endif
0a337ed0
GH
96#ifndef GDK_IS_WIN32_DISPLAY
97#define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
98#endif
99
204af15b 100#if !GTK_CHECK_VERSION(2, 22, 0)
bc0477c7
DB
101#define GDK_KEY_0 GDK_0
102#define GDK_KEY_1 GDK_1
103#define GDK_KEY_2 GDK_2
104#define GDK_KEY_f GDK_f
105#define GDK_KEY_g GDK_g
db1da1f2 106#define GDK_KEY_q GDK_q
bc0477c7
DB
107#define GDK_KEY_plus GDK_plus
108#define GDK_KEY_minus GDK_minus
dc520171 109#define GDK_KEY_Pause GDK_Pause
204af15b 110#define GDK_KEY_Delete GDK_Delete
bc0477c7 111#endif
ef6413a2 112
c8f3f17c
DB
113/* Some older mingw versions lack this constant or have
114 * it conditionally defined */
115#ifdef _WIN32
116# ifndef MAPVK_VK_TO_VSC
117# define MAPVK_VK_TO_VSC 0
118# endif
119#endif
120
121
b1e749c0 122#define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
b1e749c0 123
6db253ca
JK
124static const int modifier_keycode[] = {
125 /* shift, control, alt keys, meta keys, both left & right */
126 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
127};
128
e3500d1f 129struct GtkDisplayState {
a4ccabcf
AL
130 GtkWidget *window;
131
132 GtkWidget *menu_bar;
133
73d4dc71
AL
134 GtkAccelGroup *accel_group;
135
30e8f22b
JK
136 GtkWidget *machine_menu_item;
137 GtkWidget *machine_menu;
138 GtkWidget *pause_item;
139 GtkWidget *reset_item;
140 GtkWidget *powerdown_item;
a4ccabcf
AL
141 GtkWidget *quit_item;
142
143 GtkWidget *view_menu_item;
144 GtkWidget *view_menu;
c6158483 145 GtkWidget *full_screen_item;
44b31e0b 146 GtkWidget *copy_item;
c6158483
AL
147 GtkWidget *zoom_in_item;
148 GtkWidget *zoom_out_item;
149 GtkWidget *zoom_fixed_item;
150 GtkWidget *zoom_fit_item;
5104a1f6
AL
151 GtkWidget *grab_item;
152 GtkWidget *grab_on_hover_item;
a4ccabcf 153
d861def3
AL
154 int nb_vcs;
155 VirtualConsole vc[MAX_VCS];
156
a4ccabcf 157 GtkWidget *show_tabs_item;
cdeb7090 158 GtkWidget *untabify_item;
a4ccabcf
AL
159
160 GtkWidget *vbox;
161 GtkWidget *notebook;
a4ccabcf 162 int button_mask;
e61031cd 163 gboolean last_set;
a4ccabcf
AL
164 int last_x;
165 int last_y;
ecce1929
TI
166 int grab_x_root;
167 int grab_y_root;
4c638e2e
GH
168 VirtualConsole *kbd_owner;
169 VirtualConsole *ptr_owner;
a4ccabcf 170
c6158483 171 gboolean full_screen;
a4ccabcf
AL
172
173 GdkCursor *null_cursor;
174 Notifier mouse_mode_notifier;
c6158483 175 gboolean free_scale;
30e8f22b
JK
176
177 bool external_pause_update;
6db253ca
JK
178
179 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
3158a348 180 bool has_evdev;
1a01716a 181 bool ignore_keys;
e3500d1f 182};
a4ccabcf 183
41ac54b2
MAL
184typedef struct VCDriverState {
185 CharDriverState parent;
186 VirtualConsole *console;
187 bool echo;
188} VCDriverState;
189
d531deef 190static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
2884cf5b 191static void gd_ungrab_pointer(GtkDisplayState *s);
d531deef 192static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
aa4f4058 193static void gd_ungrab_keyboard(GtkDisplayState *s);
2884cf5b 194
a4ccabcf
AL
195/** Utility Functions **/
196
271a25c0
GH
197static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
198{
199 VirtualConsole *vc;
200 gint i;
201
202 for (i = 0; i < s->nb_vcs; i++) {
203 vc = &s->vc[i];
204 if (gtk_check_menu_item_get_active
205 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
206 return vc;
207 }
208 }
209 return NULL;
210}
211
212static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
213{
214 VirtualConsole *vc;
215 gint i, p;
216
217 for (i = 0; i < s->nb_vcs; i++) {
218 vc = &s->vc[i];
219 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
220 if (p == page) {
221 return vc;
222 }
223 }
224 return NULL;
225}
226
e3500d1f
GH
227static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
228{
229 gint page;
230
231 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
232 return gd_vc_find_by_page(s, page);
233}
234
5104a1f6
AL
235static bool gd_is_grab_active(GtkDisplayState *s)
236{
237 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
238}
239
240static bool gd_grab_on_hover(GtkDisplayState *s)
241{
242 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
243}
244
e3500d1f 245static void gd_update_cursor(VirtualConsole *vc)
a4ccabcf 246{
e3500d1f 247 GtkDisplayState *s = vc->s;
a4ccabcf 248 GdkWindow *window;
a4ccabcf 249
f8c223f6
GH
250 if (vc->type != GD_VC_GFX ||
251 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
e3500d1f
GH
252 return;
253 }
a4ccabcf 254
4cdfc935
HP
255 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
256 return;
257 }
258
e3500d1f 259 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
2884cf5b 260 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
a4ccabcf
AL
261 gdk_window_set_cursor(window, s->null_cursor);
262 } else {
263 gdk_window_set_cursor(window, NULL);
264 }
265}
266
267static void gd_update_caption(GtkDisplayState *s)
268{
269 const char *status = "";
4eeaa3a8 270 gchar *prefix;
a4ccabcf 271 gchar *title;
5104a1f6 272 const char *grab = "";
30e8f22b 273 bool is_paused = !runstate_is_running();
4eeaa3a8
GH
274 int i;
275
276 if (qemu_name) {
277 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
278 } else {
279 prefix = g_strdup_printf("QEMU");
280 }
5104a1f6 281
4eeaa3a8
GH
282 if (s->ptr_owner != NULL &&
283 s->ptr_owner->window == NULL) {
d8da9ee8 284 grab = _(" - Press Ctrl+Alt+G to release grab");
5104a1f6 285 }
a4ccabcf 286
30e8f22b 287 if (is_paused) {
d8da9ee8 288 status = _(" [Paused]");
a4ccabcf 289 }
30e8f22b
JK
290 s->external_pause_update = true;
291 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
292 is_paused);
293 s->external_pause_update = false;
a4ccabcf 294
4eeaa3a8 295 title = g_strdup_printf("%s%s%s", prefix, status, grab);
a4ccabcf 296 gtk_window_set_title(GTK_WINDOW(s->window), title);
a4ccabcf 297 g_free(title);
4eeaa3a8
GH
298
299 for (i = 0; i < s->nb_vcs; i++) {
300 VirtualConsole *vc = &s->vc[i];
301
302 if (!vc->window) {
303 continue;
304 }
305 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
306 vc == s->kbd_owner ? " +kbd" : "",
307 vc == s->ptr_owner ? " +ptr" : "");
308 gtk_window_set_title(GTK_WINDOW(vc->window), title);
309 g_free(title);
310 }
311
312 g_free(prefix);
a4ccabcf
AL
313}
314
82fc1809 315static void gd_update_geometry_hints(VirtualConsole *vc)
9d9801cf 316{
e3500d1f 317 GtkDisplayState *s = vc->s;
82fc1809
GH
318 GdkWindowHints mask = 0;
319 GdkGeometry geo = {};
320 GtkWidget *geo_widget = NULL;
321 GtkWindow *geo_window;
e3500d1f 322
82fc1809 323 if (vc->type == GD_VC_GFX) {
f98f43ea
GH
324 if (!vc->gfx.ds) {
325 return;
326 }
82fc1809
GH
327 if (s->free_scale) {
328 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
329 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
330 mask |= GDK_HINT_MIN_SIZE;
331 } else {
332 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
333 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
334 mask |= GDK_HINT_MIN_SIZE;
335 }
336 geo_widget = vc->gfx.drawing_area;
337 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
9d9801cf 338
82fc1809
GH
339#if defined(CONFIG_VTE)
340 } else if (vc->type == GD_VC_VTE) {
341 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
6978dc4a 342 GtkBorder padding = { 0 };
82fc1809 343
84e2dc4b 344#if VTE_CHECK_VERSION(0, 37, 0)
84e2dc4b
CR
345 gtk_style_context_get_padding(
346 gtk_widget_get_style_context(vc->vte.terminal),
347 gtk_widget_get_state_flags(vc->vte.terminal),
348 &padding);
84e2dc4b 349#else
6978dc4a
AG
350 {
351 GtkBorder *ib = NULL;
352 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
353 if (ib) {
354 padding = *ib;
355 gtk_border_free(ib);
356 }
357 }
84e2dc4b
CR
358#endif
359
82fc1809
GH
360 geo.width_inc = vte_terminal_get_char_width(term);
361 geo.height_inc = vte_terminal_get_char_height(term);
362 mask |= GDK_HINT_RESIZE_INC;
363 geo.base_width = geo.width_inc;
364 geo.base_height = geo.height_inc;
365 mask |= GDK_HINT_BASE_SIZE;
366 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
367 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
368 mask |= GDK_HINT_MIN_SIZE;
84e2dc4b 369
6978dc4a
AG
370 geo.base_width += padding.left + padding.right;
371 geo.base_height += padding.top + padding.bottom;
372 geo.min_width += padding.left + padding.right;
373 geo.min_height += padding.top + padding.bottom;
82fc1809
GH
374 geo_widget = vc->vte.terminal;
375#endif
9d9801cf 376 }
82fc1809
GH
377
378 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
379 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
380}
381
97edf3bd 382void gd_update_windowsize(VirtualConsole *vc)
82fc1809
GH
383{
384 GtkDisplayState *s = vc->s;
385
386 gd_update_geometry_hints(vc);
387
388 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
389 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
390 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
aa0a55d4 391 }
9d9801cf
GH
392}
393
e3500d1f 394static void gd_update_full_redraw(VirtualConsole *vc)
9d9801cf 395{
e3500d1f 396 GtkWidget *area = vc->gfx.drawing_area;
9d9801cf 397 int ww, wh;
e3500d1f 398 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
925a0400
GH
399#if defined(CONFIG_GTK_GL)
400 if (vc->gfx.gls) {
401 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
402 return;
403 }
404#endif
e3500d1f 405 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
9d9801cf
GH
406}
407
6db253ca
JK
408static void gtk_release_modifiers(GtkDisplayState *s)
409{
e3500d1f 410 VirtualConsole *vc = gd_vc_find_current(s);
6db253ca
JK
411 int i, keycode;
412
f8c223f6
GH
413 if (vc->type != GD_VC_GFX ||
414 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
6db253ca
JK
415 return;
416 }
417 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
418 keycode = modifier_keycode[i];
419 if (!s->modifier_pressed[i]) {
420 continue;
421 }
e3500d1f 422 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
6db253ca
JK
423 s->modifier_pressed[i] = false;
424 }
425}
426
316cb068
GH
427static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
428 GtkWidget *widget)
429{
430 g_object_ref(G_OBJECT(widget));
431 gtk_container_remove(GTK_CONTAINER(from), widget);
432 gtk_container_add(GTK_CONTAINER(to), widget);
433 g_object_unref(G_OBJECT(widget));
434}
435
a4ccabcf
AL
436/** DisplayState Callbacks **/
437
7c20b4a3 438static void gd_update(DisplayChangeListener *dcl,
bc2ed970 439 int x, int y, int w, int h)
a4ccabcf 440{
e3500d1f 441 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
f8c223f6 442 GdkWindow *win;
a4ccabcf 443 int x1, x2, y1, y2;
c6158483
AL
444 int mx, my;
445 int fbw, fbh;
446 int ww, wh;
a4ccabcf 447
74444bc1 448 trace_gd_update(vc->label, x, y, w, h);
a4ccabcf 449
4cdfc935
HP
450 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
451 return;
452 }
453
e3500d1f
GH
454 if (vc->gfx.convert) {
455 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
456 NULL, vc->gfx.convert,
f0875536
GH
457 x, y, 0, 0, x, y, w, h);
458 }
459
e3500d1f
GH
460 x1 = floor(x * vc->gfx.scale_x);
461 y1 = floor(y * vc->gfx.scale_y);
a4ccabcf 462
e3500d1f
GH
463 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
464 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
a4ccabcf 465
e3500d1f
GH
466 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
467 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
c6158483 468
f8c223f6
GH
469 win = gtk_widget_get_window(vc->gfx.drawing_area);
470 if (!win) {
471 return;
472 }
473 gdk_drawable_get_size(win, &ww, &wh);
c6158483
AL
474
475 mx = my = 0;
476 if (ww > fbw) {
477 mx = (ww - fbw) / 2;
478 }
479 if (wh > fbh) {
480 my = (wh - fbh) / 2;
481 }
482
e3500d1f
GH
483 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
484 mx + x1, my + y1, (x2 - x1), (y2 - y1));
a4ccabcf
AL
485}
486
bc2ed970 487static void gd_refresh(DisplayChangeListener *dcl)
a4ccabcf 488{
284d1c6b 489 graphic_hw_update(dcl->con);
a4ccabcf
AL
490}
491
b087143b 492#if GTK_CHECK_VERSION(3, 0, 0)
bb732ee7
CR
493static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
494{
495#if GTK_CHECK_VERSION(3, 20, 0)
496 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
497#else
498 return gdk_device_manager_get_client_pointer(
499 gdk_display_get_device_manager(dpy));
500#endif
501}
502
b087143b
IM
503static void gd_mouse_set(DisplayChangeListener *dcl,
504 int x, int y, int visible)
505{
e3500d1f 506 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
b087143b 507 GdkDisplay *dpy;
b087143b
IM
508 gint x_root, y_root;
509
2bda6602
CR
510 if (qemu_input_is_absolute()) {
511 return;
512 }
513
e3500d1f 514 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
e3500d1f 515 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
b087143b 516 x, y, &x_root, &y_root);
bb732ee7 517 gdk_device_warp(gd_get_pointer(dpy),
e3500d1f 518 gtk_widget_get_screen(vc->gfx.drawing_area),
298526fe 519 x_root, y_root);
1271f7f7
GH
520 vc->s->last_x = x;
521 vc->s->last_y = y;
b087143b
IM
522}
523#else
9697f5d2
GH
524static void gd_mouse_set(DisplayChangeListener *dcl,
525 int x, int y, int visible)
526{
e3500d1f 527 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9697f5d2
GH
528 gint x_root, y_root;
529
2bda6602
CR
530 if (qemu_input_is_absolute()) {
531 return;
532 }
533
e3500d1f 534 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
9697f5d2 535 x, y, &x_root, &y_root);
e3500d1f
GH
536 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
537 gtk_widget_get_screen(vc->gfx.drawing_area),
9697f5d2
GH
538 x_root, y_root);
539}
b087143b 540#endif
9697f5d2
GH
541
542static void gd_cursor_define(DisplayChangeListener *dcl,
543 QEMUCursor *c)
544{
e3500d1f 545 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9697f5d2
GH
546 GdkPixbuf *pixbuf;
547 GdkCursor *cursor;
548
4cdfc935
HP
549 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
550 return;
551 }
552
9697f5d2
GH
553 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
554 GDK_COLORSPACE_RGB, true, 8,
555 c->width, c->height, c->width * 4,
556 NULL, NULL);
e3500d1f
GH
557 cursor = gdk_cursor_new_from_pixbuf
558 (gtk_widget_get_display(vc->gfx.drawing_area),
559 pixbuf, c->hot_x, c->hot_y);
560 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
9697f5d2 561 g_object_unref(pixbuf);
030b4b7d 562#if !GTK_CHECK_VERSION(3, 0, 0)
17139240 563 gdk_cursor_unref(cursor);
030b4b7d
SW
564#else
565 g_object_unref(cursor);
566#endif
9697f5d2
GH
567}
568
c12aeb86 569static void gd_switch(DisplayChangeListener *dcl,
c12aeb86 570 DisplaySurface *surface)
a4ccabcf 571{
e3500d1f 572 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9d9801cf 573 bool resized = true;
a4ccabcf 574
f98f43ea
GH
575 trace_gd_switch(vc->label,
576 surface ? surface_width(surface) : 0,
577 surface ? surface_height(surface) : 0);
a4ccabcf 578
e3500d1f
GH
579 if (vc->gfx.surface) {
580 cairo_surface_destroy(vc->gfx.surface);
f98f43ea
GH
581 vc->gfx.surface = NULL;
582 }
583 if (vc->gfx.convert) {
584 pixman_image_unref(vc->gfx.convert);
585 vc->gfx.convert = NULL;
a4ccabcf
AL
586 }
587
f98f43ea 588 if (vc->gfx.ds && surface &&
e3500d1f
GH
589 surface_width(vc->gfx.ds) == surface_width(surface) &&
590 surface_height(vc->gfx.ds) == surface_height(surface)) {
9d9801cf
GH
591 resized = false;
592 }
e3500d1f 593 vc->gfx.ds = surface;
a4ccabcf 594
f98f43ea
GH
595 if (!surface) {
596 return;
f0875536 597 }
a4ccabcf 598
f0875536
GH
599 if (surface->format == PIXMAN_x8r8g8b8) {
600 /*
601 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
602 *
603 * No need to convert, use surface directly. Should be the
604 * common case as this is qemu_default_pixelformat(32) too.
605 */
e3500d1f 606 vc->gfx.surface = cairo_image_surface_create_for_data
f0875536
GH
607 (surface_data(surface),
608 CAIRO_FORMAT_RGB24,
609 surface_width(surface),
610 surface_height(surface),
611 surface_stride(surface));
612 } else {
613 /* Must convert surface, use pixman to do it. */
e3500d1f
GH
614 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
615 surface_width(surface),
616 surface_height(surface),
617 NULL, 0);
618 vc->gfx.surface = cairo_image_surface_create_for_data
619 ((void *)pixman_image_get_data(vc->gfx.convert),
f0875536 620 CAIRO_FORMAT_RGB24,
e3500d1f
GH
621 pixman_image_get_width(vc->gfx.convert),
622 pixman_image_get_height(vc->gfx.convert),
623 pixman_image_get_stride(vc->gfx.convert));
624 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
625 NULL, vc->gfx.convert,
f0875536 626 0, 0, 0, 0, 0, 0,
e3500d1f
GH
627 pixman_image_get_width(vc->gfx.convert),
628 pixman_image_get_height(vc->gfx.convert));
f0875536 629 }
c6158483 630
9d9801cf 631 if (resized) {
e3500d1f 632 gd_update_windowsize(vc);
9d9801cf 633 } else {
e3500d1f 634 gd_update_full_redraw(vc);
c6158483 635 }
a4ccabcf
AL
636}
637
97edf3bd
GH
638static const DisplayChangeListenerOps dcl_ops = {
639 .dpy_name = "gtk",
640 .dpy_gfx_update = gd_update,
641 .dpy_gfx_switch = gd_switch,
642 .dpy_gfx_check_format = qemu_pixman_check_format,
643 .dpy_refresh = gd_refresh,
644 .dpy_mouse_set = gd_mouse_set,
645 .dpy_cursor_define = gd_cursor_define,
646};
647
648
649#if defined(CONFIG_OPENGL)
650
651/** DisplayState Callbacks (opengl version) **/
652
925a0400
GH
653#if defined(CONFIG_GTK_GL)
654
655static const DisplayChangeListenerOps dcl_gl_area_ops = {
656 .dpy_name = "gtk-egl",
657 .dpy_gfx_update = gd_gl_area_update,
658 .dpy_gfx_switch = gd_gl_area_switch,
659 .dpy_gfx_check_format = console_gl_check_format,
660 .dpy_refresh = gd_gl_area_refresh,
661 .dpy_mouse_set = gd_mouse_set,
662 .dpy_cursor_define = gd_cursor_define,
663
664 .dpy_gl_ctx_create = gd_gl_area_create_context,
665 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
666 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
667 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
668 .dpy_gl_scanout = gd_gl_area_scanout,
669 .dpy_gl_update = gd_gl_area_scanout_flush,
670};
671
672#else
673
97edf3bd
GH
674static const DisplayChangeListenerOps dcl_egl_ops = {
675 .dpy_name = "gtk-egl",
676 .dpy_gfx_update = gd_egl_update,
677 .dpy_gfx_switch = gd_egl_switch,
678 .dpy_gfx_check_format = console_gl_check_format,
679 .dpy_refresh = gd_egl_refresh,
680 .dpy_mouse_set = gd_mouse_set,
681 .dpy_cursor_define = gd_cursor_define,
4782aeb7
GH
682
683 .dpy_gl_ctx_create = gd_egl_create_context,
684 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
685 .dpy_gl_ctx_make_current = gd_egl_make_current,
686 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
687 .dpy_gl_scanout = gd_egl_scanout,
688 .dpy_gl_update = gd_egl_scanout_flush,
97edf3bd
GH
689};
690
925a0400
GH
691#endif /* CONFIG_GTK_GL */
692#endif /* CONFIG_OPENGL */
97edf3bd 693
a4ccabcf
AL
694/** QEMU Events **/
695
696static void gd_change_runstate(void *opaque, int running, RunState state)
697{
698 GtkDisplayState *s = opaque;
699
700 gd_update_caption(s);
701}
702
703static void gd_mouse_mode_change(Notifier *notify, void *data)
704{
800b0e81 705 GtkDisplayState *s;
99623c90 706 int i;
800b0e81
TI
707
708 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
709 /* release the grab at switching to absolute mode */
710 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
711 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
712 FALSE);
713 }
99623c90
GH
714 for (i = 0; i < s->nb_vcs; i++) {
715 VirtualConsole *vc = &s->vc[i];
716 gd_update_cursor(vc);
717 }
a4ccabcf
AL
718}
719
720/** GTK Events **/
721
722static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
723 void *opaque)
724{
725 GtkDisplayState *s = opaque;
e3500d1f 726 int i;
a4ccabcf
AL
727
728 if (!no_quit) {
e3500d1f
GH
729 for (i = 0; i < s->nb_vcs; i++) {
730 if (s->vc[i].type != GD_VC_GFX) {
731 continue;
732 }
733 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
734 }
a4ccabcf
AL
735 qmp_quit(NULL);
736 return FALSE;
737 }
738
739 return TRUE;
740}
741
925a0400
GH
742static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
743{
744 QemuUIInfo info;
745
746 memset(&info, 0, sizeof(info));
747 info.width = width;
748 info.height = height;
749 dpy_set_ui_info(vc->gfx.dcl.con, &info);
750}
751
752#if defined(CONFIG_GTK_GL)
753
754static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
755 void *opaque)
756{
757 VirtualConsole *vc = opaque;
758
759 if (vc->gfx.gls) {
760 gd_gl_area_draw(vc);
761 }
762 return TRUE;
763}
764
765static void gd_resize_event(GtkGLArea *area,
766 gint width, gint height, gpointer *opaque)
767{
768 VirtualConsole *vc = (void *)opaque;
769
770 gd_set_ui_info(vc, width, height);
771}
772
773#endif
774
a4ccabcf
AL
775static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
776{
e3500d1f
GH
777 VirtualConsole *vc = opaque;
778 GtkDisplayState *s = vc->s;
c6158483 779 int mx, my;
a4ccabcf
AL
780 int ww, wh;
781 int fbw, fbh;
782
97edf3bd
GH
783#if defined(CONFIG_OPENGL)
784 if (vc->gfx.gls) {
925a0400
GH
785#if defined(CONFIG_GTK_GL)
786 /* invoke render callback please */
787 return FALSE;
788#else
97edf3bd
GH
789 gd_egl_draw(vc);
790 return TRUE;
925a0400 791#endif
97edf3bd
GH
792 }
793#endif
794
c6158483
AL
795 if (!gtk_widget_get_realized(widget)) {
796 return FALSE;
797 }
f98f43ea
GH
798 if (!vc->gfx.ds) {
799 return FALSE;
800 }
c6158483 801
e3500d1f
GH
802 fbw = surface_width(vc->gfx.ds);
803 fbh = surface_height(vc->gfx.ds);
a4ccabcf
AL
804
805 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
806
c6158483 807 if (s->full_screen) {
e3500d1f
GH
808 vc->gfx.scale_x = (double)ww / fbw;
809 vc->gfx.scale_y = (double)wh / fbh;
c6158483
AL
810 } else if (s->free_scale) {
811 double sx, sy;
812
813 sx = (double)ww / fbw;
814 sy = (double)wh / fbh;
815
e3500d1f 816 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
a4ccabcf
AL
817 }
818
e3500d1f
GH
819 fbw *= vc->gfx.scale_x;
820 fbh *= vc->gfx.scale_y;
5104a1f6 821
c6158483
AL
822 mx = my = 0;
823 if (ww > fbw) {
824 mx = (ww - fbw) / 2;
825 }
826 if (wh > fbh) {
827 my = (wh - fbh) / 2;
828 }
829
830 cairo_rectangle(cr, 0, 0, ww, wh);
831
832 /* Optionally cut out the inner area where the pixmap
833 will be drawn. This avoids 'flashing' since we're
834 not double-buffering. Note we're using the undocumented
835 behaviour of drawing the rectangle from right to left
836 to cut out the whole */
837 cairo_rectangle(cr, mx + fbw, my,
838 -1 * fbw, fbh);
839 cairo_fill(cr);
840
e3500d1f
GH
841 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
842 cairo_set_source_surface(cr, vc->gfx.surface,
843 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
a4ccabcf
AL
844 cairo_paint(cr);
845
846 return TRUE;
847}
848
fe43bca8 849#if !GTK_CHECK_VERSION(3, 0, 0)
a4ccabcf
AL
850static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
851 void *opaque)
852{
853 cairo_t *cr;
854 gboolean ret;
855
856 cr = gdk_cairo_create(gtk_widget_get_window(widget));
857 cairo_rectangle(cr,
858 expose->area.x,
859 expose->area.y,
860 expose->area.width,
861 expose->area.height);
862 cairo_clip(cr);
863
864 ret = gd_draw_event(widget, cr, opaque);
865
866 cairo_destroy(cr);
867
868 return ret;
869}
fe43bca8 870#endif
a4ccabcf
AL
871
872static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
873 void *opaque)
874{
e3500d1f
GH
875 VirtualConsole *vc = opaque;
876 GtkDisplayState *s = vc->s;
a4ccabcf 877 int x, y;
c6158483
AL
878 int mx, my;
879 int fbh, fbw;
880 int ww, wh;
881
f98f43ea
GH
882 if (!vc->gfx.ds) {
883 return TRUE;
884 }
885
e3500d1f
GH
886 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
887 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
a4ccabcf 888
e3500d1f
GH
889 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
890 &ww, &wh);
c6158483
AL
891
892 mx = my = 0;
893 if (ww > fbw) {
894 mx = (ww - fbw) / 2;
895 }
896 if (wh > fbh) {
897 my = (wh - fbh) / 2;
898 }
899
e3500d1f
GH
900 x = (motion->x - mx) / vc->gfx.scale_x;
901 y = (motion->y - my) / vc->gfx.scale_y;
c6158483 902
192f81bf 903 if (qemu_input_is_absolute()) {
e61031cd 904 if (x < 0 || y < 0 ||
e3500d1f
GH
905 x >= surface_width(vc->gfx.ds) ||
906 y >= surface_height(vc->gfx.ds)) {
e61031cd
TI
907 return TRUE;
908 }
e3500d1f
GH
909 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
910 surface_width(vc->gfx.ds));
911 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
912 surface_height(vc->gfx.ds));
192f81bf 913 qemu_input_event_sync();
2884cf5b 914 } else if (s->last_set && s->ptr_owner == vc) {
e3500d1f
GH
915 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
916 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
192f81bf 917 qemu_input_event_sync();
a4ccabcf 918 }
a4ccabcf
AL
919 s->last_x = x;
920 s->last_y = y;
e61031cd 921 s->last_set = TRUE;
a4ccabcf 922
2884cf5b 923 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
e3500d1f 924 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
76d8f93b
AG
925 int screen_width, screen_height;
926
5104a1f6
AL
927 int x = (int)motion->x_root;
928 int y = (int)motion->y_root;
929
76d8f93b
AG
930#if GTK_CHECK_VERSION(3, 22, 0)
931 {
932 GdkDisplay *dpy = gtk_widget_get_display(widget);
933 GdkWindow *win = gtk_widget_get_window(widget);
934 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
935 GdkRectangle geometry;
936 gdk_monitor_get_geometry(monitor, &geometry);
937 screen_width = geometry.width;
938 screen_height = geometry.height;
939 }
940#else
941 {
942 screen_width = gdk_screen_get_width(screen);
943 screen_height = gdk_screen_get_height(screen);
944 }
945#endif
946
5104a1f6
AL
947 /* In relative mode check to see if client pointer hit
948 * one of the screen edges, and if so move it back by
949 * 200 pixels. This is important because the pointer
950 * in the server doesn't correspond 1-for-1, and so
951 * may still be only half way across the screen. Without
952 * this warp, the server pointer would thus appear to hit
953 * an invisible wall */
954 if (x == 0) {
955 x += 200;
956 }
957 if (y == 0) {
958 y += 200;
959 }
76d8f93b 960 if (x == (screen_width - 1)) {
5104a1f6
AL
961 x -= 200;
962 }
76d8f93b 963 if (y == (screen_height - 1)) {
5104a1f6
AL
964 y -= 200;
965 }
966
967 if (x != (int)motion->x_root || y != (int)motion->y_root) {
8906de76
DB
968#if GTK_CHECK_VERSION(3, 0, 0)
969 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
970 gdk_device_warp(dev, screen, x, y);
971#else
972 GdkDisplay *display = gtk_widget_get_display(widget);
5104a1f6 973 gdk_display_warp_pointer(display, screen, x, y);
8906de76 974#endif
e61031cd 975 s->last_set = FALSE;
5104a1f6
AL
976 return FALSE;
977 }
978 }
a4ccabcf
AL
979 return TRUE;
980}
981
982static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
983 void *opaque)
984{
e3500d1f
GH
985 VirtualConsole *vc = opaque;
986 GtkDisplayState *s = vc->s;
192f81bf 987 InputButton btn;
a4ccabcf 988
800b0e81
TI
989 /* implicitly grab the input at the first click in the relative mode */
990 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
2884cf5b 991 !qemu_input_is_absolute() && s->ptr_owner != vc) {
2884cf5b
GH
992 if (!vc->window) {
993 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
994 TRUE);
995 } else {
d531deef 996 gd_grab_pointer(vc, "relative-mode-click");
2884cf5b 997 }
800b0e81
TI
998 return TRUE;
999 }
1000
a4ccabcf 1001 if (button->button == 1) {
192f81bf 1002 btn = INPUT_BUTTON_LEFT;
a4ccabcf 1003 } else if (button->button == 2) {
192f81bf 1004 btn = INPUT_BUTTON_MIDDLE;
a4ccabcf 1005 } else if (button->button == 3) {
192f81bf 1006 btn = INPUT_BUTTON_RIGHT;
a4ccabcf 1007 } else {
192f81bf 1008 return TRUE;
a4ccabcf
AL
1009 }
1010
e3500d1f
GH
1011 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1012 button->type == GDK_BUTTON_PRESS);
192f81bf 1013 qemu_input_event_sync();
a4ccabcf
AL
1014 return TRUE;
1015}
1016
d58b9122
JK
1017static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1018 void *opaque)
1019{
e3500d1f 1020 VirtualConsole *vc = opaque;
d58b9122
JK
1021 InputButton btn;
1022
1023 if (scroll->direction == GDK_SCROLL_UP) {
f22d0af0 1024 btn = INPUT_BUTTON_WHEEL_UP;
d58b9122 1025 } else if (scroll->direction == GDK_SCROLL_DOWN) {
f22d0af0 1026 btn = INPUT_BUTTON_WHEEL_DOWN;
d58b9122
JK
1027 } else {
1028 return TRUE;
1029 }
1030
e3500d1f 1031 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
d58b9122 1032 qemu_input_event_sync();
e3500d1f 1033 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
d58b9122
JK
1034 qemu_input_event_sync();
1035 return TRUE;
1036}
1037
0a337ed0 1038static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
a4ccabcf 1039{
932f2d7e 1040 int qemu_keycode;
a4ccabcf 1041
0a337ed0
GH
1042#ifdef GDK_WINDOWING_WIN32
1043 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1044 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
1045 switch (qemu_keycode) {
1046 case 103: /* alt gr */
1047 qemu_keycode = 56 | SCANCODE_GREY;
1048 break;
1049 }
1050 return qemu_keycode;
2777ccc5 1051 }
0a337ed0 1052#endif
a4ccabcf
AL
1053
1054 if (gdk_keycode < 9) {
1055 qemu_keycode = 0;
1056 } else if (gdk_keycode < 97) {
1057 qemu_keycode = gdk_keycode - 8;
0a337ed0
GH
1058#ifdef GDK_WINDOWING_X11
1059 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
3158a348
BR
1060 if (s->has_evdev) {
1061 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1062 } else {
1063 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
1064 }
a8ffb372
DB
1065#endif
1066#ifdef GDK_WINDOWING_WAYLAND
1067 } else if (GDK_IS_WAYLAND_DISPLAY(dpy) && gdk_keycode < 158) {
1068 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
0a337ed0 1069#endif
a4ccabcf
AL
1070 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
1071 qemu_keycode = 0x70;
1072 } else if (gdk_keycode == 211) { /* backslash */
1073 qemu_keycode = 0x73;
1074 } else {
1075 qemu_keycode = 0;
1076 }
1077
932f2d7e
GH
1078 return qemu_keycode;
1079}
1080
f8c223f6
GH
1081static gboolean gd_text_key_down(GtkWidget *widget,
1082 GdkEventKey *key, void *opaque)
1083{
1084 VirtualConsole *vc = opaque;
1085 QemuConsole *con = vc->gfx.dcl.con;
1086
85617885
TH
1087 if (key->keyval == GDK_KEY_Delete) {
1088 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1089 } else if (key->length) {
f8c223f6
GH
1090 kbd_put_string_console(con, key->string, key->length);
1091 } else {
1092 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
1093 key->hardware_keycode);
1094 int qcode = qemu_input_key_number_to_qcode(num);
1095 kbd_put_qcode_console(con, qcode);
1096 }
1097 return TRUE;
1098}
1099
932f2d7e
GH
1100static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1101{
1102 VirtualConsole *vc = opaque;
1103 GtkDisplayState *s = vc->s;
1104 int gdk_keycode = key->hardware_keycode;
1105 int qemu_keycode;
1106 int i;
1107
1a01716a
JK
1108 if (s->ignore_keys) {
1109 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1110 return TRUE;
1111 }
1112
5c960521
MD
1113 if (key->keyval == GDK_KEY_Pause) {
1114 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1115 key->type == GDK_KEY_PRESS);
1116 return TRUE;
1117 }
1118
0a337ed0
GH
1119 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
1120 gdk_keycode);
932f2d7e 1121
74444bc1 1122 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
ef0dd982 1123 (key->type == GDK_KEY_PRESS) ? "down" : "up");
a4ccabcf 1124
6db253ca
JK
1125 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1126 if (qemu_keycode == modifier_keycode[i]) {
1127 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1128 }
1129 }
1130
e3500d1f 1131 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
af98ba92 1132 key->type == GDK_KEY_PRESS);
a4ccabcf
AL
1133
1134 return TRUE;
1135}
1136
0d0e044d
TI
1137static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1138{
1139 if (event->type == GDK_MOTION_NOTIFY) {
1140 return gd_motion_event(widget, &event->motion, opaque);
1141 }
1142 return FALSE;
1143}
1144
a4ccabcf
AL
1145/** Window Menu Actions **/
1146
30e8f22b
JK
1147static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1148{
1149 GtkDisplayState *s = opaque;
1150
1151 if (s->external_pause_update) {
1152 return;
1153 }
1154 if (runstate_is_running()) {
1155 qmp_stop(NULL);
1156 } else {
1157 qmp_cont(NULL);
1158 }
1159}
1160
1161static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1162{
1163 qmp_system_reset(NULL);
1164}
1165
1166static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1167{
1168 qmp_system_powerdown(NULL);
1169}
1170
a4ccabcf
AL
1171static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1172{
1173 qmp_quit(NULL);
1174}
1175
1176static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1177{
1178 GtkDisplayState *s = opaque;
e3500d1f 1179 VirtualConsole *vc = gd_vc_find_by_menu(s);
e72b59fa 1180 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
832189c9 1181 gint page;
a4ccabcf 1182
e3500d1f
GH
1183 gtk_release_modifiers(s);
1184 if (vc) {
e72b59fa
JS
1185 page = gtk_notebook_page_num(nb, vc->tab_item);
1186 gtk_notebook_set_current_page(nb, page);
9d677e1c 1187 gtk_widget_grab_focus(vc->focus);
a4ccabcf 1188 }
1a01716a 1189 s->ignore_keys = false;
a4ccabcf
AL
1190}
1191
277836c8
CR
1192static void gd_accel_switch_vc(void *opaque)
1193{
1194 VirtualConsole *vc = opaque;
1a01716a 1195
277836c8 1196 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1a01716a
JK
1197#if !GTK_CHECK_VERSION(3, 0, 0)
1198 /* GTK2 sends the accel key to the target console - ignore this until */
1199 vc->s->ignore_keys = true;
1200#endif
277836c8
CR
1201}
1202
a4ccabcf
AL
1203static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1204{
1205 GtkDisplayState *s = opaque;
fa7a1e52 1206 VirtualConsole *vc = gd_vc_find_current(s);
a4ccabcf
AL
1207
1208 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1209 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1210 } else {
1211 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1212 }
fa7a1e52 1213 gd_update_windowsize(vc);
a4ccabcf
AL
1214}
1215
cdeb7090
GH
1216static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1217 void *opaque)
1218{
1219 VirtualConsole *vc = opaque;
1220 GtkDisplayState *s = vc->s;
1221
1222 gtk_widget_set_sensitive(vc->menu_item, true);
316cb068 1223 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
cdeb7090
GH
1224 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1225 vc->tab_item, vc->label);
1226 gtk_widget_destroy(vc->window);
1227 vc->window = NULL;
1228 return TRUE;
1229}
1230
0c77a37f
GH
1231static gboolean gd_win_grab(void *opaque)
1232{
1233 VirtualConsole *vc = opaque;
1234
1235 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1236 if (vc->s->ptr_owner) {
1237 gd_ungrab_pointer(vc->s);
1238 } else {
d531deef 1239 gd_grab_pointer(vc, "user-request-detached-tab");
0c77a37f 1240 }
0c77a37f
GH
1241 return TRUE;
1242}
1243
cdeb7090
GH
1244static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1245{
1246 GtkDisplayState *s = opaque;
1247 VirtualConsole *vc = gd_vc_find_current(s);
cdeb7090 1248
f8c223f6
GH
1249 if (vc->type == GD_VC_GFX &&
1250 qemu_console_is_graphic(vc->gfx.dcl.con)) {
aa0a55d4
GH
1251 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1252 FALSE);
cdeb7090
GH
1253 }
1254 if (!vc->window) {
1255 gtk_widget_set_sensitive(vc->menu_item, false);
1256 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
316cb068 1257 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
cdeb7090 1258
cdeb7090
GH
1259 g_signal_connect(vc->window, "delete-event",
1260 G_CALLBACK(gd_tab_window_close), vc);
1261 gtk_widget_show_all(vc->window);
4eeaa3a8 1262
f8c223f6
GH
1263 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1264 GtkAccelGroup *ag = gtk_accel_group_new();
1265 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
0c77a37f 1266
f8c223f6
GH
1267 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1268 vc, NULL);
1269 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1270 }
0c77a37f 1271
82fc1809 1272 gd_update_geometry_hints(vc);
4eeaa3a8 1273 gd_update_caption(s);
cdeb7090
GH
1274 }
1275}
1276
c6158483
AL
1277static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1278{
1279 GtkDisplayState *s = opaque;
e3500d1f 1280 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1281
10409282 1282 if (!s->full_screen) {
c6158483 1283 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
b0f31820 1284 gtk_widget_hide(s->menu_bar);
e3500d1f
GH
1285 if (vc->type == GD_VC_GFX) {
1286 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
c6158483 1287 }
e3500d1f 1288 gtk_window_fullscreen(GTK_WINDOW(s->window));
c6158483
AL
1289 s->full_screen = TRUE;
1290 } else {
1291 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1292 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
b0f31820 1293 gtk_widget_show(s->menu_bar);
c6158483 1294 s->full_screen = FALSE;
e3500d1f
GH
1295 if (vc->type == GD_VC_GFX) {
1296 vc->gfx.scale_x = 1.0;
1297 vc->gfx.scale_y = 1.0;
82fc1809 1298 gd_update_windowsize(vc);
e3500d1f 1299 }
c6158483
AL
1300 }
1301
e3500d1f 1302 gd_update_cursor(vc);
c6158483
AL
1303}
1304
95414914
CR
1305static void gd_accel_full_screen(void *opaque)
1306{
1307 GtkDisplayState *s = opaque;
1308 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1309}
1310
c6158483
AL
1311static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1312{
1313 GtkDisplayState *s = opaque;
e3500d1f 1314 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1315
1316 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1317 FALSE);
1318
82fc1809
GH
1319 vc->gfx.scale_x += VC_SCALE_STEP;
1320 vc->gfx.scale_y += VC_SCALE_STEP;
c6158483 1321
e3500d1f 1322 gd_update_windowsize(vc);
c6158483
AL
1323}
1324
1325static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1326{
1327 GtkDisplayState *s = opaque;
e3500d1f 1328 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1329
1330 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1331 FALSE);
1332
82fc1809
GH
1333 vc->gfx.scale_x -= VC_SCALE_STEP;
1334 vc->gfx.scale_y -= VC_SCALE_STEP;
c6158483 1335
82fc1809
GH
1336 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1337 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
c6158483 1338
e3500d1f 1339 gd_update_windowsize(vc);
c6158483
AL
1340}
1341
1342static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1343{
1344 GtkDisplayState *s = opaque;
e3500d1f 1345 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1346
e3500d1f
GH
1347 vc->gfx.scale_x = 1.0;
1348 vc->gfx.scale_y = 1.0;
c6158483 1349
e3500d1f 1350 gd_update_windowsize(vc);
c6158483
AL
1351}
1352
1353static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1354{
1355 GtkDisplayState *s = opaque;
e3500d1f 1356 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1357
1358 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1359 s->free_scale = TRUE;
1360 } else {
1361 s->free_scale = FALSE;
e3500d1f
GH
1362 vc->gfx.scale_x = 1.0;
1363 vc->gfx.scale_y = 1.0;
c6158483
AL
1364 }
1365
82fc1809 1366 gd_update_windowsize(vc);
e3500d1f 1367 gd_update_full_redraw(vc);
c6158483
AL
1368}
1369
a69fc693
GH
1370#if GTK_CHECK_VERSION(3, 20, 0)
1371static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1372{
1373 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1374 GdkSeat *seat = gdk_display_get_default_seat(display);
1375 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1376 GdkSeatCapabilities caps = 0;
1377 GdkCursor *cursor = NULL;
1378
1379 if (kbd) {
1380 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1381 }
1382 if (ptr) {
1383 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1384 cursor = vc->s->null_cursor;
1385 }
1386
1387 if (caps) {
1388 gdk_seat_grab(seat, window, caps, false, cursor,
1389 NULL, NULL, NULL);
1390 } else {
1391 gdk_seat_ungrab(seat);
1392 }
1393}
1394#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1395static void gd_grab_devices(VirtualConsole *vc, bool grab,
1396 GdkInputSource source, GdkEventMask mask,
1397 GdkCursor *cursor)
1398{
e3500d1f 1399 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
655199da 1400 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1401 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1402 GList *tmp = devs;
1403
1404 for (tmp = devs; tmp; tmp = tmp->next) {
655199da 1405 GdkDevice *dev = tmp->data;
f50def91
GH
1406 if (gdk_device_get_source(dev) != source) {
1407 continue;
1408 }
1409 if (grab) {
1410 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1411 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1412 mask, cursor, GDK_CURRENT_TIME);
1413 } else {
1414 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
655199da 1415 }
655199da 1416 }
f50def91
GH
1417 g_list_free(devs);
1418}
1419#endif
1420
d531deef 1421static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
f50def91 1422{
aa4f4058
GH
1423 if (vc->s->kbd_owner) {
1424 if (vc->s->kbd_owner == vc) {
1425 return;
1426 } else {
1427 gd_ungrab_keyboard(vc->s);
1428 }
1429 }
1430
a69fc693
GH
1431#if GTK_CHECK_VERSION(3, 20, 0)
1432 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1433#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1434 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1435 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1436 NULL);
655199da 1437#else
e3500d1f 1438 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
5104a1f6
AL
1439 FALSE,
1440 GDK_CURRENT_TIME);
655199da 1441#endif
4c638e2e 1442 vc->s->kbd_owner = vc;
695cc59d 1443 gd_update_caption(vc->s);
d531deef 1444 trace_gd_grab(vc->label, "kbd", reason);
5104a1f6
AL
1445}
1446
4c638e2e 1447static void gd_ungrab_keyboard(GtkDisplayState *s)
5104a1f6 1448{
4c638e2e
GH
1449 VirtualConsole *vc = s->kbd_owner;
1450
1451 if (vc == NULL) {
1452 return;
1453 }
1454 s->kbd_owner = NULL;
1455
a69fc693
GH
1456#if GTK_CHECK_VERSION(3, 20, 0)
1457 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1458#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91 1459 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
655199da 1460#else
5104a1f6 1461 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
655199da 1462#endif
695cc59d 1463 gd_update_caption(s);
d531deef 1464 trace_gd_ungrab(vc->label, "kbd");
5104a1f6
AL
1465}
1466
d531deef 1467static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
2a05485d 1468{
e3500d1f 1469 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
aa4f4058
GH
1470
1471 if (vc->s->ptr_owner) {
1472 if (vc->s->ptr_owner == vc) {
1473 return;
1474 } else {
1475 gd_ungrab_pointer(vc->s);
1476 }
1477 }
1478
a69fc693
GH
1479#if GTK_CHECK_VERSION(3, 20, 0)
1480 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1481 gdk_device_get_position(gd_get_pointer(display),
1482 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1483#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1484 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1485 GDK_POINTER_MOTION_MASK |
1486 GDK_BUTTON_PRESS_MASK |
1487 GDK_BUTTON_RELEASE_MASK |
1488 GDK_BUTTON_MOTION_MASK |
1489 GDK_SCROLL_MASK,
1490 vc->s->null_cursor);
bb732ee7 1491 gdk_device_get_position(gd_get_pointer(display),
e3500d1f 1492 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
2a05485d 1493#else
e3500d1f 1494 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
2a05485d
DB
1495 FALSE, /* All events to come to our window directly */
1496 GDK_POINTER_MOTION_MASK |
1497 GDK_BUTTON_PRESS_MASK |
1498 GDK_BUTTON_RELEASE_MASK |
1499 GDK_BUTTON_MOTION_MASK |
1500 GDK_SCROLL_MASK,
1501 NULL, /* Allow cursor to move over entire desktop */
e3500d1f 1502 vc->s->null_cursor,
2a05485d 1503 GDK_CURRENT_TIME);
ecce1929 1504 gdk_display_get_pointer(display, NULL,
e3500d1f 1505 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
2a05485d 1506#endif
4c638e2e 1507 vc->s->ptr_owner = vc;
695cc59d 1508 gd_update_caption(vc->s);
d531deef 1509 trace_gd_grab(vc->label, "ptr", reason);
2a05485d
DB
1510}
1511
4c638e2e 1512static void gd_ungrab_pointer(GtkDisplayState *s)
2a05485d 1513{
4c638e2e 1514 VirtualConsole *vc = s->ptr_owner;
41cc5239 1515 GdkDisplay *display;
4c638e2e
GH
1516
1517 if (vc == NULL) {
1518 return;
1519 }
1520 s->ptr_owner = NULL;
1521
41cc5239 1522 display = gtk_widget_get_display(vc->gfx.drawing_area);
a69fc693
GH
1523#if GTK_CHECK_VERSION(3, 20, 0)
1524 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1525 gdk_device_warp(gd_get_pointer(display),
1526 gtk_widget_get_screen(vc->gfx.drawing_area),
1527 vc->s->grab_x_root, vc->s->grab_y_root);
1528#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91 1529 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
bb732ee7 1530 gdk_device_warp(gd_get_pointer(display),
e3500d1f
GH
1531 gtk_widget_get_screen(vc->gfx.drawing_area),
1532 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d
DB
1533#else
1534 gdk_pointer_ungrab(GDK_CURRENT_TIME);
ecce1929 1535 gdk_display_warp_pointer(display,
e3500d1f
GH
1536 gtk_widget_get_screen(vc->gfx.drawing_area),
1537 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d 1538#endif
695cc59d 1539 gd_update_caption(s);
d531deef 1540 trace_gd_ungrab(vc->label, "ptr");
2a05485d
DB
1541}
1542
5104a1f6
AL
1543static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1544{
1545 GtkDisplayState *s = opaque;
e3500d1f 1546 VirtualConsole *vc = gd_vc_find_current(s);
5104a1f6
AL
1547
1548 if (gd_is_grab_active(s)) {
d531deef
GH
1549 gd_grab_keyboard(vc, "user-request-main-window");
1550 gd_grab_pointer(vc, "user-request-main-window");
5104a1f6 1551 } else {
4c638e2e
GH
1552 gd_ungrab_keyboard(s);
1553 gd_ungrab_pointer(s);
5104a1f6
AL
1554 }
1555
e3500d1f 1556 gd_update_cursor(vc);
5104a1f6
AL
1557}
1558
a4ccabcf
AL
1559static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1560 gpointer data)
1561{
1562 GtkDisplayState *s = data;
e3500d1f 1563 VirtualConsole *vc;
5104a1f6 1564 gboolean on_vga;
a4ccabcf
AL
1565
1566 if (!gtk_widget_get_realized(s->notebook)) {
1567 return;
1568 }
1569
6fa27697
GH
1570#ifdef VTE_RESIZE_HACK
1571 vc = gd_vc_find_current(s);
1572 if (vc && vc->type == GD_VC_VTE) {
1573 gtk_widget_hide(vc->vte.terminal);
1574 }
1575#endif
e3500d1f
GH
1576 vc = gd_vc_find_by_page(s, arg2);
1577 if (!vc) {
1578 return;
1579 }
6fa27697
GH
1580#ifdef VTE_RESIZE_HACK
1581 if (vc->type == GD_VC_VTE) {
1582 gtk_widget_show(vc->vte.terminal);
1583 }
1584#endif
e3500d1f
GH
1585 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1586 TRUE);
f8c223f6
GH
1587 on_vga = (vc->type == GD_VC_GFX &&
1588 qemu_console_is_graphic(vc->gfx.dcl.con));
5104a1f6
AL
1589 if (!on_vga) {
1590 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1591 FALSE);
c6158483
AL
1592 } else if (s->full_screen) {
1593 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1594 TRUE);
5104a1f6 1595 }
5104a1f6 1596 gtk_widget_set_sensitive(s->grab_item, on_vga);
a0815632
SH
1597#ifdef CONFIG_VTE
1598 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1599#endif
5104a1f6 1600
82fc1809 1601 gd_update_windowsize(vc);
e3500d1f 1602 gd_update_cursor(vc);
a4ccabcf
AL
1603}
1604
e3500d1f
GH
1605static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1606 gpointer opaque)
5104a1f6 1607{
e3500d1f
GH
1608 VirtualConsole *vc = opaque;
1609 GtkDisplayState *s = vc->s;
5104a1f6 1610
2884cf5b 1611 if (gd_grab_on_hover(s)) {
d531deef 1612 gd_grab_keyboard(vc, "grab-on-hover");
5104a1f6 1613 }
5104a1f6
AL
1614 return TRUE;
1615}
1616
e3500d1f
GH
1617static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1618 gpointer opaque)
5104a1f6 1619{
e3500d1f
GH
1620 VirtualConsole *vc = opaque;
1621 GtkDisplayState *s = vc->s;
5104a1f6 1622
2884cf5b 1623 if (gd_grab_on_hover(s)) {
4c638e2e 1624 gd_ungrab_keyboard(s);
5104a1f6 1625 }
5104a1f6
AL
1626 return TRUE;
1627}
1628
6db253ca 1629static gboolean gd_focus_out_event(GtkWidget *widget,
e3500d1f 1630 GdkEventCrossing *crossing, gpointer opaque)
6db253ca 1631{
e3500d1f
GH
1632 VirtualConsole *vc = opaque;
1633 GtkDisplayState *s = vc->s;
6db253ca
JK
1634
1635 gtk_release_modifiers(s);
6db253ca
JK
1636 return TRUE;
1637}
1638
1301e515
GH
1639static gboolean gd_configure(GtkWidget *widget,
1640 GdkEventConfigure *cfg, gpointer opaque)
1641{
1642 VirtualConsole *vc = opaque;
1301e515 1643
925a0400 1644 gd_set_ui_info(vc, cfg->width, cfg->height);
1301e515
GH
1645 return FALSE;
1646}
1647
d861def3
AL
1648/** Virtual Console Callbacks **/
1649
ed1132e4 1650static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
cdeb7090 1651 int idx, GSList *group, GtkWidget *view_menu)
ed1132e4 1652{
cdeb7090 1653 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
277836c8
CR
1654 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1655 HOTKEY_MODIFIERS, 0,
1656 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1657#if GTK_CHECK_VERSION(3, 8, 0)
1658 gtk_accel_label_set_accel(
1659 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1660 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1661#endif
ed1132e4
GH
1662
1663 g_signal_connect(vc->menu_item, "activate",
1664 G_CALLBACK(gd_menu_switch_vc), s);
1665 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1666
277836c8 1667 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
ed1132e4
GH
1668 return group;
1669}
1670
ee5f31e4 1671#if defined(CONFIG_VTE)
44b31e0b
MT
1672static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1673{
1674 GtkDisplayState *s = opaque;
1675 VirtualConsole *vc = gd_vc_find_current(s);
1676
1677 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1678}
1679
0fb20d1c
CR
1680static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1681{
1682 VirtualConsole *vc = opaque;
1683
1684 if (gtk_adjustment_get_upper(adjustment) >
1685 gtk_adjustment_get_page_size(adjustment)) {
271a25c0 1686 gtk_widget_show(vc->vte.scrollbar);
0fb20d1c 1687 } else {
271a25c0 1688 gtk_widget_hide(vc->vte.scrollbar);
0fb20d1c
CR
1689 }
1690}
1691
d861def3
AL
1692static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1693{
41ac54b2
MAL
1694 VCDriverState *vcd = (VCDriverState *)chr;
1695 VirtualConsole *vc = vcd->console;
d861def3 1696
271a25c0 1697 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
d4370741 1698 return len;
d861def3
AL
1699}
1700
fba958c6
PB
1701static void gd_vc_chr_set_echo(CharDriverState *chr, bool echo)
1702{
41ac54b2
MAL
1703 VCDriverState *vcd = (VCDriverState *)chr;
1704 VirtualConsole *vc = vcd->console;
fba958c6 1705
41ac54b2
MAL
1706 if (vc) {
1707 vc->vte.echo = echo;
1708 } else {
1709 vcd->echo = echo;
1710 }
fba958c6
PB
1711}
1712
d861def3
AL
1713static int nb_vcs;
1714static CharDriverState *vcs[MAX_VCS];
1715
919e11f3 1716static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
d861def3 1717{
b68e956a 1718 static const CharDriver gd_vc_driver = {
41ac54b2 1719 .instance_size = sizeof(VCDriverState),
b68e956a
MAL
1720 .kind = CHARDEV_BACKEND_KIND_VC,
1721 .chr_write = gd_vc_chr_write,
1722 .chr_set_echo = gd_vc_chr_set_echo,
1723 };
1724
919e11f3 1725 ChardevCommon *common = qapi_ChardevVC_base(vc);
d861def3
AL
1726 CharDriverState *chr;
1727
c952b715
MAL
1728 if (nb_vcs == MAX_VCS) {
1729 error_setg(errp, "Maximum number of consoles reached");
1730 return NULL;
1731 }
1732
b68e956a 1733 chr = qemu_chr_alloc(&gd_vc_driver, common, errp);
919e11f3
DB
1734 if (!chr) {
1735 return NULL;
1736 }
1737
d861def3
AL
1738 vcs[nb_vcs++] = chr;
1739
1740 return chr;
1741}
1742
d4370741
CR
1743static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1744 gpointer user_data)
d861def3 1745{
d4370741 1746 VirtualConsole *vc = user_data;
d861def3 1747
fba958c6
PB
1748 if (vc->vte.echo) {
1749 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1750 int i;
1751 for (i = 0; i < size; i++) {
1752 uint8_t c = text[i];
1753 if (c >= 128 || isprint(c)) {
1754 /* 8-bit characters are considered printable. */
1755 vte_terminal_feed(term, &text[i], 1);
1756 } else if (c == '\r' || c == '\n') {
1757 vte_terminal_feed(term, "\r\n", 2);
1758 } else {
1759 char ctrl[2] = { '^', 0};
1760 ctrl[1] = text[i] ^ 64;
1761 vte_terminal_feed(term, ctrl, 2);
1762 }
1763 }
1764 }
1765
271a25c0 1766 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
d861def3
AL
1767 return TRUE;
1768}
1769
ed1132e4
GH
1770static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1771 CharDriverState *chr, int idx,
e3500d1f 1772 GSList *group, GtkWidget *view_menu)
d861def3 1773{
d861def3 1774 char buffer[32];
0fb20d1c
CR
1775 GtkWidget *box;
1776 GtkWidget *scrollbar;
1777 GtkAdjustment *vadjustment;
41ac54b2 1778 VCDriverState *vcd = (VCDriverState *)chr;
d861def3 1779
e3500d1f 1780 vc->s = s;
41ac54b2 1781 vc->vte.echo = vcd->echo;
ed1132e4 1782 vc->vte.chr = chr;
41ac54b2 1783 vcd->console = vc;
d861def3 1784
ed1132e4 1785 snprintf(buffer, sizeof(buffer), "vc%d", idx);
cdeb7090
GH
1786 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1787 ? vc->vte.chr->label : buffer);
1788 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
d861def3 1789
271a25c0
GH
1790 vc->vte.terminal = vte_terminal_new();
1791 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
d861def3 1792
fba958c6
PB
1793 /* The documentation says that the default is UTF-8, but actually it is
1794 * 7-bit ASCII at least in VTE 0.38.
1795 */
4d594233 1796#if VTE_CHECK_VERSION(0, 38, 0)
fba958c6
PB
1797 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1798#else
1799 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1800#endif
1801
271a25c0 1802 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
82fc1809
GH
1803 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1804 VC_TERM_X_MIN, VC_TERM_Y_MIN);
d861def3 1805
0fb20d1c 1806#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
271a25c0
GH
1807 vadjustment = gtk_scrollable_get_vadjustment
1808 (GTK_SCROLLABLE(vc->vte.terminal));
0fb20d1c 1809#else
271a25c0 1810 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
0fb20d1c
CR
1811#endif
1812
1813#if GTK_CHECK_VERSION(3, 0, 0)
1814 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1815 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1816#else
1817 box = gtk_hbox_new(false, 2);
1818 scrollbar = gtk_vscrollbar_new(vadjustment);
1819#endif
1820
271a25c0 1821 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
0fb20d1c
CR
1822 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1823
271a25c0
GH
1824 vc->vte.box = box;
1825 vc->vte.scrollbar = scrollbar;
0fb20d1c
CR
1826
1827 g_signal_connect(vadjustment, "changed",
1828 G_CALLBACK(gd_vc_adjustment_changed), vc);
d861def3 1829
e3500d1f 1830 vc->type = GD_VC_VTE;
271a25c0 1831 vc->tab_item = box;
9d677e1c 1832 vc->focus = vc->vte.terminal;
271a25c0 1833 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
cdeb7090 1834 gtk_label_new(vc->label));
d861def3 1835
271a25c0 1836 qemu_chr_be_generic_open(vc->vte.chr);
d861def3 1837
d861def3 1838 return group;
a4ccabcf
AL
1839}
1840
ed1132e4 1841static void gd_vcs_init(GtkDisplayState *s, GSList *group,
ee5f31e4
GH
1842 GtkWidget *view_menu)
1843{
1844 int i;
1845
1846 for (i = 0; i < nb_vcs; i++) {
ed1132e4
GH
1847 VirtualConsole *vc = &s->vc[s->nb_vcs];
1848 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
ee5f31e4
GH
1849 s->nb_vcs++;
1850 }
1851}
1852#endif /* CONFIG_VTE */
1853
a4ccabcf
AL
1854/** Window Creation **/
1855
e3500d1f
GH
1856static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1857{
1858#if GTK_CHECK_VERSION(3, 0, 0)
1859 g_signal_connect(vc->gfx.drawing_area, "draw",
1860 G_CALLBACK(gd_draw_event), vc);
925a0400
GH
1861#if defined(CONFIG_GTK_GL)
1862 if (display_opengl) {
1863 /* wire up GtkGlArea events */
1864 g_signal_connect(vc->gfx.drawing_area, "render",
1865 G_CALLBACK(gd_render_event), vc);
1866 g_signal_connect(vc->gfx.drawing_area, "resize",
1867 G_CALLBACK(gd_resize_event), vc);
1868 }
1869#endif
e3500d1f
GH
1870#else
1871 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1872 G_CALLBACK(gd_expose_event), vc);
1873#endif
f8c223f6
GH
1874 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1875 g_signal_connect(vc->gfx.drawing_area, "event",
1876 G_CALLBACK(gd_event), vc);
1877 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1878 G_CALLBACK(gd_button_event), vc);
1879 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1880 G_CALLBACK(gd_button_event), vc);
1881 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1882 G_CALLBACK(gd_scroll_event), vc);
1883 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1884 G_CALLBACK(gd_key_event), vc);
1885 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1886 G_CALLBACK(gd_key_event), vc);
1887
1888 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1889 G_CALLBACK(gd_enter_event), vc);
1890 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1891 G_CALLBACK(gd_leave_event), vc);
1892 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1893 G_CALLBACK(gd_focus_out_event), vc);
1301e515
GH
1894 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1895 G_CALLBACK(gd_configure), vc);
f8c223f6
GH
1896 } else {
1897 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1898 G_CALLBACK(gd_text_key_down), vc);
1899 }
e3500d1f
GH
1900}
1901
a4ccabcf
AL
1902static void gd_connect_signals(GtkDisplayState *s)
1903{
1904 g_signal_connect(s->show_tabs_item, "activate",
1905 G_CALLBACK(gd_menu_show_tabs), s);
cdeb7090
GH
1906 g_signal_connect(s->untabify_item, "activate",
1907 G_CALLBACK(gd_menu_untabify), s);
a4ccabcf
AL
1908
1909 g_signal_connect(s->window, "delete-event",
1910 G_CALLBACK(gd_window_close), s);
1911
30e8f22b
JK
1912 g_signal_connect(s->pause_item, "activate",
1913 G_CALLBACK(gd_menu_pause), s);
1914 g_signal_connect(s->reset_item, "activate",
1915 G_CALLBACK(gd_menu_reset), s);
1916 g_signal_connect(s->powerdown_item, "activate",
1917 G_CALLBACK(gd_menu_powerdown), s);
a4ccabcf
AL
1918 g_signal_connect(s->quit_item, "activate",
1919 G_CALLBACK(gd_menu_quit), s);
44b31e0b
MT
1920#if defined(CONFIG_VTE)
1921 g_signal_connect(s->copy_item, "activate",
1922 G_CALLBACK(gd_menu_copy), s);
1923#endif
c6158483
AL
1924 g_signal_connect(s->full_screen_item, "activate",
1925 G_CALLBACK(gd_menu_full_screen), s);
1926 g_signal_connect(s->zoom_in_item, "activate",
1927 G_CALLBACK(gd_menu_zoom_in), s);
1928 g_signal_connect(s->zoom_out_item, "activate",
1929 G_CALLBACK(gd_menu_zoom_out), s);
1930 g_signal_connect(s->zoom_fixed_item, "activate",
1931 G_CALLBACK(gd_menu_zoom_fixed), s);
1932 g_signal_connect(s->zoom_fit_item, "activate",
1933 G_CALLBACK(gd_menu_zoom_fit), s);
5104a1f6
AL
1934 g_signal_connect(s->grab_item, "activate",
1935 G_CALLBACK(gd_menu_grab_input), s);
a4ccabcf
AL
1936 g_signal_connect(s->notebook, "switch-page",
1937 G_CALLBACK(gd_change_page), s);
1938}
1939
400519d2 1940static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
a4ccabcf 1941{
bf9b255f 1942 GtkWidget *machine_menu;
a4ccabcf
AL
1943 GtkWidget *separator;
1944
bf9b255f 1945 machine_menu = gtk_menu_new();
400519d2 1946 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
30e8f22b
JK
1947
1948 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
bf9b255f 1949 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
30e8f22b
JK
1950
1951 separator = gtk_separator_menu_item_new();
bf9b255f 1952 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
30e8f22b 1953
9068f20d 1954 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
bf9b255f 1955 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
30e8f22b 1956
9068f20d 1957 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
bf9b255f 1958 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
30e8f22b
JK
1959
1960 separator = gtk_separator_menu_item_new();
bf9b255f 1961 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
a4ccabcf 1962
3d914488 1963 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
a4ccabcf 1964 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
30e8f22b 1965 "<QEMU>/Machine/Quit");
3d914488 1966 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
db1da1f2 1967 GDK_KEY_q, HOTKEY_MODIFIERS);
bf9b255f 1968 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
a4ccabcf 1969
bf9b255f
AL
1970 return machine_menu;
1971}
1972
e3500d1f 1973static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
ed1132e4 1974 QemuConsole *con, int idx,
e3500d1f
GH
1975 GSList *group, GtkWidget *view_menu)
1976{
779ce88f 1977 vc->label = qemu_console_get_label(con);
e3500d1f
GH
1978 vc->s = s;
1979 vc->gfx.scale_x = 1.0;
1980 vc->gfx.scale_y = 1.0;
1981
97edf3bd
GH
1982#if defined(CONFIG_OPENGL)
1983 if (display_opengl) {
925a0400
GH
1984#if defined(CONFIG_GTK_GL)
1985 vc->gfx.drawing_area = gtk_gl_area_new();
1986 vc->gfx.dcl.ops = &dcl_gl_area_ops;
1987#else
1988 vc->gfx.drawing_area = gtk_drawing_area_new();
97edf3bd
GH
1989 /*
1990 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1991 * It is required for opengl rendering on X11 though. A
1992 * proper replacement (native opengl support) is only
1993 * available in 3.16+. Silence the warning if possible.
1994 */
1995#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1996#pragma GCC diagnostic push
1997#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1998#endif
1999 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2000#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2001#pragma GCC diagnostic pop
2002#endif
2003 vc->gfx.dcl.ops = &dcl_egl_ops;
925a0400 2004#endif /* CONFIG_GTK_GL */
97edf3bd
GH
2005 } else
2006#endif
2007 {
925a0400 2008 vc->gfx.drawing_area = gtk_drawing_area_new();
97edf3bd
GH
2009 vc->gfx.dcl.ops = &dcl_ops;
2010 }
2011
925a0400
GH
2012
2013 gtk_widget_add_events(vc->gfx.drawing_area,
2014 GDK_POINTER_MOTION_MASK |
2015 GDK_BUTTON_PRESS_MASK |
2016 GDK_BUTTON_RELEASE_MASK |
2017 GDK_BUTTON_MOTION_MASK |
2018 GDK_ENTER_NOTIFY_MASK |
2019 GDK_LEAVE_NOTIFY_MASK |
2020 GDK_SCROLL_MASK |
2021 GDK_KEY_PRESS_MASK);
2022 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2023
2024 vc->type = GD_VC_GFX;
2025 vc->tab_item = vc->gfx.drawing_area;
2026 vc->focus = vc->gfx.drawing_area;
2027 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2028 vc->tab_item, gtk_label_new(vc->label));
2029
e3500d1f
GH
2030 vc->gfx.dcl.con = con;
2031 register_displaychangelistener(&vc->gfx.dcl);
2032
f8c223f6
GH
2033 gd_connect_vc_gfx_signals(vc);
2034 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2035
1301e515
GH
2036 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2037 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
1d73cd78 2038 s->free_scale = true;
1301e515
GH
2039 }
2040
e3500d1f
GH
2041 return group;
2042}
2043
400519d2 2044static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
bf9b255f
AL
2045{
2046 GSList *group = NULL;
2047 GtkWidget *view_menu;
2048 GtkWidget *separator;
ed1132e4
GH
2049 QemuConsole *con;
2050 int vc;
bf9b255f
AL
2051
2052 view_menu = gtk_menu_new();
400519d2 2053 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
a4ccabcf 2054
3d914488 2055 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
95414914 2056
44b31e0b
MT
2057#if defined(CONFIG_VTE)
2058 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2059 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2060#endif
2061
95414914
CR
2062 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2063 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2064#if GTK_CHECK_VERSION(3, 8, 0)
2065 gtk_accel_label_set_accel(
2066 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2067 GDK_KEY_f, HOTKEY_MODIFIERS);
2068#endif
bf9b255f 2069 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
c6158483
AL
2070
2071 separator = gtk_separator_menu_item_new();
bf9b255f 2072 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 2073
3d914488 2074 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
c6158483
AL
2075 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2076 "<QEMU>/View/Zoom In");
b1e749c0
JK
2077 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2078 HOTKEY_MODIFIERS);
bf9b255f 2079 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
c6158483 2080
3d914488 2081 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
c6158483
AL
2082 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2083 "<QEMU>/View/Zoom Out");
b1e749c0
JK
2084 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2085 HOTKEY_MODIFIERS);
bf9b255f 2086 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
c6158483 2087
3d914488 2088 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
c6158483
AL
2089 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2090 "<QEMU>/View/Zoom Fixed");
b1e749c0
JK
2091 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2092 HOTKEY_MODIFIERS);
bf9b255f 2093 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
c6158483 2094
834574ea 2095 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
bf9b255f 2096 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
c6158483
AL
2097
2098 separator = gtk_separator_menu_item_new();
bf9b255f 2099 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 2100
834574ea 2101 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
bf9b255f 2102 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
5104a1f6 2103
834574ea 2104 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
5104a1f6
AL
2105 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2106 "<QEMU>/View/Grab Input");
b1e749c0
JK
2107 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2108 HOTKEY_MODIFIERS);
bf9b255f 2109 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
5104a1f6 2110
a4ccabcf 2111 separator = gtk_separator_menu_item_new();
bf9b255f 2112 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 2113
e3500d1f 2114 /* gfx */
ed1132e4
GH
2115 for (vc = 0;; vc++) {
2116 con = qemu_console_lookup_by_index(vc);
f8c223f6 2117 if (!con) {
ed1132e4
GH
2118 break;
2119 }
2120 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2121 vc, group, view_menu);
2122 s->nb_vcs++;
2123 }
a4ccabcf 2124
ee5f31e4 2125#if defined(CONFIG_VTE)
e3500d1f 2126 /* vte */
ed1132e4 2127 gd_vcs_init(s, group, view_menu);
ee5f31e4 2128#endif
d861def3 2129
a4ccabcf 2130 separator = gtk_separator_menu_item_new();
bf9b255f 2131 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 2132
834574ea 2133 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
bf9b255f 2134 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
a4ccabcf 2135
cdeb7090
GH
2136 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2137 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2138
bf9b255f
AL
2139 return view_menu;
2140}
2141
2142static void gd_create_menus(GtkDisplayState *s)
2143{
400519d2
CR
2144 s->accel_group = gtk_accel_group_new();
2145 s->machine_menu = gd_create_menu_machine(s);
2146 s->view_menu = gd_create_menu_view(s);
bf9b255f
AL
2147
2148 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
30e8f22b
JK
2149 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2150 s->machine_menu);
2151 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
a4ccabcf 2152
bf9b255f 2153 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
a4ccabcf
AL
2154 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2155 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
bf9b255f 2156
400519d2
CR
2157 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2158 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
a4ccabcf
AL
2159}
2160
3158a348
BR
2161static void gd_set_keycode_type(GtkDisplayState *s)
2162{
0a337ed0 2163#ifdef GDK_WINDOWING_X11
3158a348 2164 GdkDisplay *display = gtk_widget_get_display(s->window);
0a337ed0
GH
2165 if (GDK_IS_X11_DISPLAY(display)) {
2166 Display *x11_display = gdk_x11_display_get_xdisplay(display);
2167 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
2168 XkbUseCoreKbd);
2169 char *keycodes = NULL;
2170
2171 if (desc && desc->names) {
2172 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
2173 }
2174 if (keycodes == NULL) {
2175 fprintf(stderr, "could not lookup keycode name\n");
2176 } else if (strstart(keycodes, "evdev", NULL)) {
2177 s->has_evdev = true;
2178 } else if (!strstart(keycodes, "xfree86", NULL)) {
2179 fprintf(stderr, "unknown keycodes `%s', please report to "
2180 "qemu-devel@nongnu.org\n", keycodes);
2181 }
84961407
CF
2182
2183 if (desc) {
2184 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
2185 }
2186 if (keycodes) {
2187 XFree(keycodes);
2188 }
3158a348
BR
2189 }
2190#endif
2191}
2192
060ab763
GH
2193static gboolean gtkinit;
2194
881249c7 2195void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
a4ccabcf 2196{
3d4da9d6
HP
2197 VirtualConsole *vc;
2198
a4ccabcf 2199 GtkDisplayState *s = g_malloc0(sizeof(*s));
d819cdcc 2200 char *filename;
63c67b6d 2201 GdkDisplay *window_display;
a4ccabcf 2202
060ab763
GH
2203 if (!gtkinit) {
2204 fprintf(stderr, "gtk initialization failed\n");
2205 exit(1);
2206 }
2207
a4ccabcf 2208 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
51572ab0
DB
2209#if GTK_CHECK_VERSION(3, 2, 0)
2210 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2211#else
a4ccabcf 2212 s->vbox = gtk_vbox_new(FALSE, 0);
51572ab0 2213#endif
a4ccabcf 2214 s->notebook = gtk_notebook_new();
a4ccabcf
AL
2215 s->menu_bar = gtk_menu_bar_new();
2216
c6158483 2217 s->free_scale = FALSE;
a4ccabcf 2218
2cb5d2a4
AG
2219 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2220 setlocale(LC_MESSAGES, "");
834574ea
AL
2221 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2222 textdomain("qemu");
2223
63c67b6d
HR
2224 window_display = gtk_widget_get_display(s->window);
2225 s->null_cursor = gdk_cursor_new_for_display(window_display,
2226 GDK_BLANK_CURSOR);
a4ccabcf
AL
2227
2228 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2229 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2230 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2231
f7da9c17 2232 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
d819cdcc
SW
2233 if (filename) {
2234 GError *error = NULL;
2235 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2236 if (pixbuf) {
2237 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2238 } else {
2239 g_error_free(error);
2240 }
2241 g_free(filename);
2242 }
2243
a4ccabcf
AL
2244 gd_create_menus(s);
2245
2246 gd_connect_signals(s);
2247
a4ccabcf
AL
2248 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2249 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2250
a4ccabcf
AL
2251 gd_update_caption(s);
2252
2253 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2254 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2255
2256 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2257
2258 gtk_widget_show_all(s->window);
2259
6fa27697
GH
2260#ifdef VTE_RESIZE_HACK
2261 {
2262 VirtualConsole *cur = gd_vc_find_current(s);
b310a2a6
FZ
2263 if (cur) {
2264 int i;
2265
2266 for (i = 0; i < s->nb_vcs; i++) {
2267 VirtualConsole *vc = &s->vc[i];
2268 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2269 gtk_widget_hide(vc->vte.terminal);
2270 }
6fa27697 2271 }
b310a2a6 2272 gd_update_windowsize(cur);
6fa27697 2273 }
6fa27697
GH
2274 }
2275#endif
2276
3d4da9d6
HP
2277 vc = gd_vc_find_current(s);
2278 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
a0815632
SH
2279#ifdef CONFIG_VTE
2280 gtk_widget_set_sensitive(s->copy_item,
3d4da9d6 2281 vc && vc->type == GD_VC_VTE);
a0815632
SH
2282#endif
2283
787ba4f0
PW
2284 if (full_screen) {
2285 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2286 }
881249c7
JK
2287 if (grab_on_hover) {
2288 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2289 }
787ba4f0 2290
3158a348 2291 gd_set_keycode_type(s);
a4ccabcf 2292}
ee5f31e4 2293
97edf3bd 2294void early_gtk_display_init(int opengl)
ee5f31e4 2295{
2cb5d2a4
AG
2296 /* The QEMU code relies on the assumption that it's always run in
2297 * the C locale. Therefore it is not prepared to deal with
2298 * operations that produce different results depending on the
2299 * locale, such as printf's formatting of decimal numbers, and
2300 * possibly others.
2301 *
2302 * Since GTK+ calls setlocale() by default -importing the locale
2303 * settings from the environment- we must prevent it from doing so
2304 * using gtk_disable_setlocale().
2305 *
2306 * QEMU's GTK+ UI, however, _does_ have translations for some of
2307 * the menu items. As a trade-off between a functionally correct
2308 * QEMU and a fully internationalized UI we support importing
2309 * LC_MESSAGES from the environment (see the setlocale() call
2310 * earlier in this file). This allows us to display translated
2311 * messages leaving everything else untouched.
2312 */
2313 gtk_disable_setlocale();
060ab763
GH
2314 gtkinit = gtk_init_check(NULL, NULL);
2315 if (!gtkinit) {
2316 /* don't exit yet, that'll break -help */
2317 return;
2318 }
97edf3bd
GH
2319
2320 switch (opengl) {
2321 case -1: /* default */
2322 case 0: /* off */
2323 break;
2324 case 1: /* on */
2325#if defined(CONFIG_OPENGL)
925a0400
GH
2326#if defined(CONFIG_GTK_GL)
2327 gtk_gl_area_init();
2328#else
97edf3bd 2329 gtk_egl_init();
925a0400 2330#endif
97edf3bd
GH
2331#endif
2332 break;
2333 default:
2334 g_assert_not_reached();
2335 break;
2336 }
2337
ee5f31e4
GH
2338#if defined(CONFIG_VTE)
2339 register_vc_handler(gd_vc_handler);
2340#endif
2341}