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