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