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