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