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