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