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