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