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