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