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