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