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