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