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