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