xref: /openbmc/qemu/ui/gtk.c (revision 4a66d3bf)
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 #ifdef _WIN32
38 # define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
39 #endif
40 
41 #include "qemu-common.h"
42 
43 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
44 /* Work around an -Wstrict-prototypes warning in GTK headers */
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
47 #endif
48 #include <gtk/gtk.h>
49 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
50 #pragma GCC diagnostic pop
51 #endif
52 
53 
54 #include <gdk/gdkkeysyms.h>
55 #include <glib/gi18n.h>
56 #include <locale.h>
57 #if defined(CONFIG_VTE)
58 #include <vte/vte.h>
59 #endif
60 #include <math.h>
61 
62 #include "trace.h"
63 #include "ui/console.h"
64 #include "ui/input.h"
65 #include "sysemu/sysemu.h"
66 #include "qmp-commands.h"
67 #include "x_keymap.h"
68 #include "keymaps.h"
69 #include "sysemu/char.h"
70 
71 #define MAX_VCS 10
72 
73 #if !defined(CONFIG_VTE)
74 # define VTE_CHECK_VERSION(a, b, c) 0
75 #endif
76 
77 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
78 #if GTK_CHECK_VERSION(3, 0, 0)
79 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
80 {
81     *ww = gdk_window_get_width(w);
82     *wh = gdk_window_get_height(w);
83 }
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_KEY_0
91 #define GDK_KEY_0 GDK_0
92 #define GDK_KEY_1 GDK_1
93 #define GDK_KEY_2 GDK_2
94 #define GDK_KEY_f GDK_f
95 #define GDK_KEY_g GDK_g
96 #define GDK_KEY_plus GDK_plus
97 #define GDK_KEY_minus GDK_minus
98 #endif
99 
100 #define HOTKEY_MODIFIERS        (GDK_CONTROL_MASK | GDK_MOD1_MASK)
101 #define IGNORE_MODIFIER_MASK \
102     (GDK_MODIFIER_MASK & ~(GDK_LOCK_MASK | GDK_MOD2_MASK))
103 
104 static const int modifier_keycode[] = {
105     /* shift, control, alt keys, meta keys, both left & right */
106     0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
107 };
108 
109 typedef struct VirtualConsole
110 {
111     GtkWidget *menu_item;
112     GtkWidget *terminal;
113 #if defined(CONFIG_VTE)
114     GtkWidget *scrolled_window;
115     CharDriverState *chr;
116 #endif
117     int fd;
118 } VirtualConsole;
119 
120 typedef struct GtkDisplayState
121 {
122     GtkWidget *window;
123 
124     GtkWidget *menu_bar;
125 
126     GtkAccelGroup *accel_group;
127 
128     GtkWidget *machine_menu_item;
129     GtkWidget *machine_menu;
130     GtkWidget *pause_item;
131     GtkWidget *reset_item;
132     GtkWidget *powerdown_item;
133     GtkWidget *quit_item;
134 
135     GtkWidget *view_menu_item;
136     GtkWidget *view_menu;
137     GtkWidget *full_screen_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     GtkWidget *vga_item;
145 
146     int nb_vcs;
147     VirtualConsole vc[MAX_VCS];
148 
149     GtkWidget *show_tabs_item;
150 
151     GtkWidget *vbox;
152     GtkWidget *notebook;
153     GtkWidget *drawing_area;
154     cairo_surface_t *surface;
155     pixman_image_t *convert;
156     DisplayChangeListener dcl;
157     DisplaySurface *ds;
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 
165     double scale_x;
166     double scale_y;
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 } GtkDisplayState;
177 
178 static GtkDisplayState *global_state;
179 
180 /** Utility Functions **/
181 
182 static bool gd_is_grab_active(GtkDisplayState *s)
183 {
184     return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
185 }
186 
187 static bool gd_grab_on_hover(GtkDisplayState *s)
188 {
189     return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
190 }
191 
192 static bool gd_on_vga(GtkDisplayState *s)
193 {
194     return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
195 }
196 
197 static void gd_update_cursor(GtkDisplayState *s, gboolean override)
198 {
199     GdkWindow *window;
200     bool on_vga;
201 
202     window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));
203 
204     on_vga = gd_on_vga(s);
205 
206     if ((override || on_vga) &&
207         (s->full_screen || qemu_input_is_absolute() || gd_is_grab_active(s))) {
208         gdk_window_set_cursor(window, s->null_cursor);
209     } else {
210         gdk_window_set_cursor(window, NULL);
211     }
212 }
213 
214 static void gd_update_caption(GtkDisplayState *s)
215 {
216     const char *status = "";
217     gchar *title;
218     const char *grab = "";
219     bool is_paused = !runstate_is_running();
220 
221     if (gd_is_grab_active(s)) {
222         grab = _(" - Press Ctrl+Alt+G to release grab");
223     }
224 
225     if (is_paused) {
226         status = _(" [Paused]");
227     }
228     s->external_pause_update = true;
229     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
230                                    is_paused);
231     s->external_pause_update = false;
232 
233     if (qemu_name) {
234         title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
235     } else {
236         title = g_strdup_printf("QEMU%s%s", status, grab);
237     }
238 
239     gtk_window_set_title(GTK_WINDOW(s->window), title);
240 
241     g_free(title);
242 }
243 
244 static void gd_update_windowsize(GtkDisplayState *s)
245 {
246     if (!s->full_screen) {
247         GtkRequisition req;
248         double sx, sy;
249 
250         if (s->free_scale) {
251             sx = s->scale_x;
252             sy = s->scale_y;
253 
254             s->scale_y = 1.0;
255             s->scale_x = 1.0;
256         } else {
257             sx = 1.0;
258             sy = 1.0;
259         }
260 
261         gtk_widget_set_size_request(s->drawing_area,
262                                     surface_width(s->ds) * s->scale_x,
263                                     surface_height(s->ds) * s->scale_y);
264 #if GTK_CHECK_VERSION(3, 0, 0)
265         gtk_widget_get_preferred_size(s->vbox, NULL, &req);
266 #else
267         gtk_widget_size_request(s->vbox, &req);
268 #endif
269 
270         gtk_window_resize(GTK_WINDOW(s->window),
271                           req.width * sx, req.height * sy);
272     }
273 }
274 
275 static void gd_update_full_redraw(GtkDisplayState *s)
276 {
277     int ww, wh;
278     gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
279     gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
280 }
281 
282 static void gtk_release_modifiers(GtkDisplayState *s)
283 {
284     int i, keycode;
285 
286     if (!gd_on_vga(s)) {
287         return;
288     }
289     for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
290         keycode = modifier_keycode[i];
291         if (!s->modifier_pressed[i]) {
292             continue;
293         }
294         qemu_input_event_send_key_number(s->dcl.con, keycode, false);
295         s->modifier_pressed[i] = false;
296     }
297 }
298 
299 /** DisplayState Callbacks **/
300 
301 static void gd_update(DisplayChangeListener *dcl,
302                       int x, int y, int w, int h)
303 {
304     GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
305     int x1, x2, y1, y2;
306     int mx, my;
307     int fbw, fbh;
308     int ww, wh;
309 
310     trace_gd_update(x, y, w, h);
311 
312     if (s->convert) {
313         pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
314                                x, y, 0, 0, x, y, w, h);
315     }
316 
317     x1 = floor(x * s->scale_x);
318     y1 = floor(y * s->scale_y);
319 
320     x2 = ceil(x * s->scale_x + w * s->scale_x);
321     y2 = ceil(y * s->scale_y + h * s->scale_y);
322 
323     fbw = surface_width(s->ds) * s->scale_x;
324     fbh = surface_height(s->ds) * s->scale_y;
325 
326     gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
327 
328     mx = my = 0;
329     if (ww > fbw) {
330         mx = (ww - fbw) / 2;
331     }
332     if (wh > fbh) {
333         my = (wh - fbh) / 2;
334     }
335 
336     gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
337 }
338 
339 static void gd_refresh(DisplayChangeListener *dcl)
340 {
341     graphic_hw_update(dcl->con);
342 }
343 
344 #if GTK_CHECK_VERSION(3, 0, 0)
345 static void gd_mouse_set(DisplayChangeListener *dcl,
346                          int x, int y, int visible)
347 {
348     GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
349     GdkDisplay *dpy;
350     GdkDeviceManager *mgr;
351     gint x_root, y_root;
352 
353     if (qemu_input_is_absolute()) {
354         return;
355     }
356 
357     dpy = gtk_widget_get_display(s->drawing_area);
358     mgr = gdk_display_get_device_manager(dpy);
359     gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
360                                x, y, &x_root, &y_root);
361     gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
362                     gtk_widget_get_screen(s->drawing_area),
363                     x_root, y_root);
364 }
365 #else
366 static void gd_mouse_set(DisplayChangeListener *dcl,
367                          int x, int y, int visible)
368 {
369     GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
370     gint x_root, y_root;
371 
372     if (qemu_input_is_absolute()) {
373         return;
374     }
375 
376     gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
377                                x, y, &x_root, &y_root);
378     gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area),
379                              gtk_widget_get_screen(s->drawing_area),
380                              x_root, y_root);
381 }
382 #endif
383 
384 static void gd_cursor_define(DisplayChangeListener *dcl,
385                              QEMUCursor *c)
386 {
387     GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
388     GdkPixbuf *pixbuf;
389     GdkCursor *cursor;
390 
391     pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
392                                       GDK_COLORSPACE_RGB, true, 8,
393                                       c->width, c->height, c->width * 4,
394                                       NULL, NULL);
395     cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area),
396                                         pixbuf, c->hot_x, c->hot_y);
397     gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor);
398     g_object_unref(pixbuf);
399 #if !GTK_CHECK_VERSION(3, 0, 0)
400     gdk_cursor_unref(cursor);
401 #else
402     g_object_unref(cursor);
403 #endif
404 }
405 
406 static void gd_switch(DisplayChangeListener *dcl,
407                       DisplaySurface *surface)
408 {
409     GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
410     bool resized = true;
411 
412     trace_gd_switch(surface_width(surface), surface_height(surface));
413 
414     if (s->surface) {
415         cairo_surface_destroy(s->surface);
416     }
417 
418     if (s->ds &&
419         surface_width(s->ds) == surface_width(surface) &&
420         surface_height(s->ds) == surface_height(surface)) {
421         resized = false;
422     }
423     s->ds = surface;
424 
425     if (s->convert) {
426         pixman_image_unref(s->convert);
427         s->convert = NULL;
428     }
429 
430     if (surface->format == PIXMAN_x8r8g8b8) {
431         /*
432          * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
433          *
434          * No need to convert, use surface directly.  Should be the
435          * common case as this is qemu_default_pixelformat(32) too.
436          */
437         s->surface = cairo_image_surface_create_for_data
438             (surface_data(surface),
439              CAIRO_FORMAT_RGB24,
440              surface_width(surface),
441              surface_height(surface),
442              surface_stride(surface));
443     } else {
444         /* Must convert surface, use pixman to do it. */
445         s->convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
446                                               surface_width(surface),
447                                               surface_height(surface),
448                                               NULL, 0);
449         s->surface = cairo_image_surface_create_for_data
450             ((void *)pixman_image_get_data(s->convert),
451              CAIRO_FORMAT_RGB24,
452              pixman_image_get_width(s->convert),
453              pixman_image_get_height(s->convert),
454              pixman_image_get_stride(s->convert));
455         pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
456                                0, 0, 0, 0, 0, 0,
457                                pixman_image_get_width(s->convert),
458                                pixman_image_get_height(s->convert));
459     }
460 
461     if (resized) {
462         gd_update_windowsize(s);
463     } else {
464         gd_update_full_redraw(s);
465     }
466 }
467 
468 /** QEMU Events **/
469 
470 static void gd_change_runstate(void *opaque, int running, RunState state)
471 {
472     GtkDisplayState *s = opaque;
473 
474     gd_update_caption(s);
475 }
476 
477 static void gd_mouse_mode_change(Notifier *notify, void *data)
478 {
479     GtkDisplayState *s;
480 
481     s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
482     /* release the grab at switching to absolute mode */
483     if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
484         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
485                                        FALSE);
486     }
487     gd_update_cursor(s, FALSE);
488 }
489 
490 /** GTK Events **/
491 
492 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
493 {
494     GtkDisplayState *s = opaque;
495     gboolean handled = FALSE;
496 
497     if (!gd_is_grab_active(s) ||
498         (key->state & IGNORE_MODIFIER_MASK) == HOTKEY_MODIFIERS) {
499         handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
500     }
501     if (handled) {
502         gtk_release_modifiers(s);
503     } else {
504         handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
505     }
506 
507     return handled;
508 }
509 
510 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
511                                 void *opaque)
512 {
513     GtkDisplayState *s = opaque;
514 
515     if (!no_quit) {
516         unregister_displaychangelistener(&s->dcl);
517         qmp_quit(NULL);
518         return FALSE;
519     }
520 
521     return TRUE;
522 }
523 
524 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
525 {
526     GtkDisplayState *s = opaque;
527     int mx, my;
528     int ww, wh;
529     int fbw, fbh;
530 
531     if (!gtk_widget_get_realized(widget)) {
532         return FALSE;
533     }
534 
535     fbw = surface_width(s->ds);
536     fbh = surface_height(s->ds);
537 
538     gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
539 
540     if (s->full_screen) {
541         s->scale_x = (double)ww / fbw;
542         s->scale_y = (double)wh / fbh;
543     } else if (s->free_scale) {
544         double sx, sy;
545 
546         sx = (double)ww / fbw;
547         sy = (double)wh / fbh;
548 
549         s->scale_x = s->scale_y = MIN(sx, sy);
550     }
551 
552     fbw *= s->scale_x;
553     fbh *= s->scale_y;
554 
555     mx = my = 0;
556     if (ww > fbw) {
557         mx = (ww - fbw) / 2;
558     }
559     if (wh > fbh) {
560         my = (wh - fbh) / 2;
561     }
562 
563     cairo_rectangle(cr, 0, 0, ww, wh);
564 
565     /* Optionally cut out the inner area where the pixmap
566        will be drawn. This avoids 'flashing' since we're
567        not double-buffering. Note we're using the undocumented
568        behaviour of drawing the rectangle from right to left
569        to cut out the whole */
570     cairo_rectangle(cr, mx + fbw, my,
571                     -1 * fbw, fbh);
572     cairo_fill(cr);
573 
574     cairo_scale(cr, s->scale_x, s->scale_y);
575     cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
576     cairo_paint(cr);
577 
578     return TRUE;
579 }
580 
581 #if !GTK_CHECK_VERSION(3, 0, 0)
582 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
583                                 void *opaque)
584 {
585     cairo_t *cr;
586     gboolean ret;
587 
588     cr = gdk_cairo_create(gtk_widget_get_window(widget));
589     cairo_rectangle(cr,
590                     expose->area.x,
591                     expose->area.y,
592                     expose->area.width,
593                     expose->area.height);
594     cairo_clip(cr);
595 
596     ret = gd_draw_event(widget, cr, opaque);
597 
598     cairo_destroy(cr);
599 
600     return ret;
601 }
602 #endif
603 
604 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
605                                 void *opaque)
606 {
607     GtkDisplayState *s = opaque;
608     int x, y;
609     int mx, my;
610     int fbh, fbw;
611     int ww, wh;
612 
613     fbw = surface_width(s->ds) * s->scale_x;
614     fbh = surface_height(s->ds) * s->scale_y;
615 
616     gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
617 
618     mx = my = 0;
619     if (ww > fbw) {
620         mx = (ww - fbw) / 2;
621     }
622     if (wh > fbh) {
623         my = (wh - fbh) / 2;
624     }
625 
626     x = (motion->x - mx) / s->scale_x;
627     y = (motion->y - my) / s->scale_y;
628 
629     if (qemu_input_is_absolute()) {
630         if (x < 0 || y < 0 ||
631             x >= surface_width(s->ds) ||
632             y >= surface_height(s->ds)) {
633             return TRUE;
634         }
635         qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_X, x,
636                              surface_width(s->ds));
637         qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_Y, y,
638                              surface_height(s->ds));
639         qemu_input_event_sync();
640     } else if (s->last_set && gd_is_grab_active(s)) {
641         qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_X, x - s->last_x);
642         qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_Y, y - s->last_y);
643         qemu_input_event_sync();
644     }
645     s->last_x = x;
646     s->last_y = y;
647     s->last_set = TRUE;
648 
649     if (!qemu_input_is_absolute() && gd_is_grab_active(s)) {
650         GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
651         int x = (int)motion->x_root;
652         int y = (int)motion->y_root;
653 
654         /* In relative mode check to see if client pointer hit
655          * one of the screen edges, and if so move it back by
656          * 200 pixels. This is important because the pointer
657          * in the server doesn't correspond 1-for-1, and so
658          * may still be only half way across the screen. Without
659          * this warp, the server pointer would thus appear to hit
660          * an invisible wall */
661         if (x == 0) {
662             x += 200;
663         }
664         if (y == 0) {
665             y += 200;
666         }
667         if (x == (gdk_screen_get_width(screen) - 1)) {
668             x -= 200;
669         }
670         if (y == (gdk_screen_get_height(screen) - 1)) {
671             y -= 200;
672         }
673 
674         if (x != (int)motion->x_root || y != (int)motion->y_root) {
675 #if GTK_CHECK_VERSION(3, 0, 0)
676             GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
677             gdk_device_warp(dev, screen, x, y);
678 #else
679             GdkDisplay *display = gtk_widget_get_display(widget);
680             gdk_display_warp_pointer(display, screen, x, y);
681 #endif
682             s->last_set = FALSE;
683             return FALSE;
684         }
685     }
686     return TRUE;
687 }
688 
689 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
690                                 void *opaque)
691 {
692     GtkDisplayState *s = opaque;
693     InputButton btn;
694 
695     /* implicitly grab the input at the first click in the relative mode */
696     if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
697         !qemu_input_is_absolute() && !gd_is_grab_active(s)) {
698         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
699                                        TRUE);
700         return TRUE;
701     }
702 
703     if (button->button == 1) {
704         btn = INPUT_BUTTON_LEFT;
705     } else if (button->button == 2) {
706         btn = INPUT_BUTTON_MIDDLE;
707     } else if (button->button == 3) {
708         btn = INPUT_BUTTON_RIGHT;
709     } else {
710         return TRUE;
711     }
712 
713     qemu_input_queue_btn(s->dcl.con, btn, button->type == GDK_BUTTON_PRESS);
714     qemu_input_event_sync();
715     return TRUE;
716 }
717 
718 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
719                                 void *opaque)
720 {
721     GtkDisplayState *s = opaque;
722     InputButton btn;
723 
724     if (scroll->direction == GDK_SCROLL_UP) {
725         btn = INPUT_BUTTON_WHEEL_UP;
726     } else if (scroll->direction == GDK_SCROLL_DOWN) {
727         btn = INPUT_BUTTON_WHEEL_DOWN;
728     } else {
729         return TRUE;
730     }
731 
732     qemu_input_queue_btn(s->dcl.con, btn, true);
733     qemu_input_event_sync();
734     qemu_input_queue_btn(s->dcl.con, btn, false);
735     qemu_input_event_sync();
736     return TRUE;
737 }
738 
739 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
740 {
741     GtkDisplayState *s = opaque;
742     int gdk_keycode = key->hardware_keycode;
743     int i;
744 
745 #ifdef _WIN32
746     UINT qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
747     switch (qemu_keycode) {
748     case 103:   /* alt gr */
749         qemu_keycode = 56 | SCANCODE_GREY;
750         break;
751     }
752 #else
753     int qemu_keycode;
754 
755     if (gdk_keycode < 9) {
756         qemu_keycode = 0;
757     } else if (gdk_keycode < 97) {
758         qemu_keycode = gdk_keycode - 8;
759     } else if (gdk_keycode < 158) {
760         qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
761     } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
762         qemu_keycode = 0x70;
763     } else if (gdk_keycode == 211) { /* backslash */
764         qemu_keycode = 0x73;
765     } else {
766         qemu_keycode = 0;
767     }
768 #endif
769 
770     trace_gd_key_event(gdk_keycode, qemu_keycode,
771                        (key->type == GDK_KEY_PRESS) ? "down" : "up");
772 
773     for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
774         if (qemu_keycode == modifier_keycode[i]) {
775             s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
776         }
777     }
778 
779     qemu_input_event_send_key_number(s->dcl.con, qemu_keycode,
780                                      key->type == GDK_KEY_PRESS);
781 
782     return TRUE;
783 }
784 
785 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
786 {
787     if (event->type == GDK_MOTION_NOTIFY) {
788         return gd_motion_event(widget, &event->motion, opaque);
789     }
790     return FALSE;
791 }
792 
793 /** Window Menu Actions **/
794 
795 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
796 {
797     GtkDisplayState *s = opaque;
798 
799     if (s->external_pause_update) {
800         return;
801     }
802     if (runstate_is_running()) {
803         qmp_stop(NULL);
804     } else {
805         qmp_cont(NULL);
806     }
807 }
808 
809 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
810 {
811     qmp_system_reset(NULL);
812 }
813 
814 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
815 {
816     qmp_system_powerdown(NULL);
817 }
818 
819 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
820 {
821     qmp_quit(NULL);
822 }
823 
824 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
825 {
826     GtkDisplayState *s = opaque;
827 
828     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
829         gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
830     } else {
831         int i;
832 
833         gtk_release_modifiers(s);
834         for (i = 0; i < s->nb_vcs; i++) {
835             if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
836                 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
837                 break;
838             }
839         }
840     }
841 }
842 
843 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
844 {
845     GtkDisplayState *s = opaque;
846 
847     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
848         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
849     } else {
850         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
851     }
852 }
853 
854 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
855 {
856     GtkDisplayState *s = opaque;
857 
858     if (!s->full_screen) {
859         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
860         gtk_widget_set_size_request(s->menu_bar, 0, 0);
861         gtk_widget_set_size_request(s->drawing_area, -1, -1);
862         gtk_window_fullscreen(GTK_WINDOW(s->window));
863         if (gd_on_vga(s)) {
864             gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
865         }
866         s->full_screen = TRUE;
867     } else {
868         gtk_window_unfullscreen(GTK_WINDOW(s->window));
869         gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
870         gtk_widget_set_size_request(s->menu_bar, -1, -1);
871         gtk_widget_set_size_request(s->drawing_area,
872                                     surface_width(s->ds),
873                                     surface_height(s->ds));
874         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
875         s->full_screen = FALSE;
876         s->scale_x = 1.0;
877         s->scale_y = 1.0;
878     }
879 
880     gd_update_cursor(s, FALSE);
881 }
882 
883 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
884 {
885     GtkDisplayState *s = opaque;
886 
887     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
888                                    FALSE);
889 
890     s->scale_x += .25;
891     s->scale_y += .25;
892 
893     gd_update_windowsize(s);
894 }
895 
896 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
897 {
898     GtkDisplayState *s = opaque;
899 
900     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
901                                    FALSE);
902 
903     s->scale_x -= .25;
904     s->scale_y -= .25;
905 
906     s->scale_x = MAX(s->scale_x, .25);
907     s->scale_y = MAX(s->scale_y, .25);
908 
909     gd_update_windowsize(s);
910 }
911 
912 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
913 {
914     GtkDisplayState *s = opaque;
915 
916     s->scale_x = 1.0;
917     s->scale_y = 1.0;
918 
919     gd_update_windowsize(s);
920 }
921 
922 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
923 {
924     GtkDisplayState *s = opaque;
925 
926     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
927         s->free_scale = TRUE;
928     } else {
929         s->free_scale = FALSE;
930         s->scale_x = 1.0;
931         s->scale_y = 1.0;
932         gd_update_windowsize(s);
933     }
934 
935     gd_update_full_redraw(s);
936 }
937 
938 static void gd_grab_keyboard(GtkDisplayState *s)
939 {
940 #if GTK_CHECK_VERSION(3, 0, 0)
941     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
942     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
943     GList *devices = gdk_device_manager_list_devices(mgr,
944                                                      GDK_DEVICE_TYPE_MASTER);
945     GList *tmp = devices;
946     while (tmp) {
947         GdkDevice *dev = tmp->data;
948         if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
949             gdk_device_grab(dev,
950                             gtk_widget_get_window(s->drawing_area),
951                             GDK_OWNERSHIP_NONE,
952                             FALSE,
953                             GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
954                             NULL,
955                             GDK_CURRENT_TIME);
956         }
957         tmp = tmp->next;
958     }
959     g_list_free(devices);
960 #else
961     gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
962                       FALSE,
963                       GDK_CURRENT_TIME);
964 #endif
965 }
966 
967 static void gd_ungrab_keyboard(GtkDisplayState *s)
968 {
969 #if GTK_CHECK_VERSION(3, 0, 0)
970     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
971     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
972     GList *devices = gdk_device_manager_list_devices(mgr,
973                                                      GDK_DEVICE_TYPE_MASTER);
974     GList *tmp = devices;
975     while (tmp) {
976         GdkDevice *dev = tmp->data;
977         if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
978             gdk_device_ungrab(dev,
979                               GDK_CURRENT_TIME);
980         }
981         tmp = tmp->next;
982     }
983     g_list_free(devices);
984 #else
985     gdk_keyboard_ungrab(GDK_CURRENT_TIME);
986 #endif
987 }
988 
989 static void gd_grab_pointer(GtkDisplayState *s)
990 {
991     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
992 #if GTK_CHECK_VERSION(3, 0, 0)
993     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
994     GList *devices = gdk_device_manager_list_devices(mgr,
995                                                      GDK_DEVICE_TYPE_MASTER);
996     GList *tmp = devices;
997     while (tmp) {
998         GdkDevice *dev = tmp->data;
999         if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
1000             gdk_device_grab(dev,
1001                             gtk_widget_get_window(s->drawing_area),
1002                             GDK_OWNERSHIP_NONE,
1003                             FALSE, /* All events to come to our
1004                                       window directly */
1005                             GDK_POINTER_MOTION_MASK |
1006                             GDK_BUTTON_PRESS_MASK |
1007                             GDK_BUTTON_RELEASE_MASK |
1008                             GDK_BUTTON_MOTION_MASK |
1009                             GDK_SCROLL_MASK,
1010                             s->null_cursor,
1011                             GDK_CURRENT_TIME);
1012         }
1013         tmp = tmp->next;
1014     }
1015     g_list_free(devices);
1016     gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1017                             NULL, &s->grab_x_root, &s->grab_y_root);
1018 #else
1019     gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
1020                      FALSE, /* All events to come to our window directly */
1021                      GDK_POINTER_MOTION_MASK |
1022                      GDK_BUTTON_PRESS_MASK |
1023                      GDK_BUTTON_RELEASE_MASK |
1024                      GDK_BUTTON_MOTION_MASK |
1025                      GDK_SCROLL_MASK,
1026                      NULL, /* Allow cursor to move over entire desktop */
1027                      s->null_cursor,
1028                      GDK_CURRENT_TIME);
1029     gdk_display_get_pointer(display, NULL,
1030                             &s->grab_x_root, &s->grab_y_root, NULL);
1031 #endif
1032 }
1033 
1034 static void gd_ungrab_pointer(GtkDisplayState *s)
1035 {
1036     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
1037 #if GTK_CHECK_VERSION(3, 0, 0)
1038     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1039     GList *devices = gdk_device_manager_list_devices(mgr,
1040                                                      GDK_DEVICE_TYPE_MASTER);
1041     GList *tmp = devices;
1042     while (tmp) {
1043         GdkDevice *dev = tmp->data;
1044         if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
1045             gdk_device_ungrab(dev,
1046                               GDK_CURRENT_TIME);
1047         }
1048         tmp = tmp->next;
1049     }
1050     g_list_free(devices);
1051     gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1052                     gtk_widget_get_screen(s->drawing_area),
1053                     s->grab_x_root, s->grab_y_root);
1054 #else
1055     gdk_pointer_ungrab(GDK_CURRENT_TIME);
1056     gdk_display_warp_pointer(display,
1057                              gtk_widget_get_screen(s->drawing_area),
1058                              s->grab_x_root, s->grab_y_root);
1059 #endif
1060 }
1061 
1062 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1063 {
1064     GtkDisplayState *s = opaque;
1065 
1066     if (gd_is_grab_active(s)) {
1067         gd_grab_keyboard(s);
1068         gd_grab_pointer(s);
1069     } else {
1070         gd_ungrab_keyboard(s);
1071         gd_ungrab_pointer(s);
1072     }
1073 
1074     gd_update_caption(s);
1075     gd_update_cursor(s, FALSE);
1076 }
1077 
1078 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1079                            gpointer data)
1080 {
1081     GtkDisplayState *s = data;
1082     guint last_page;
1083     gboolean on_vga;
1084 
1085     if (!gtk_widget_get_realized(s->notebook)) {
1086         return;
1087     }
1088 
1089     last_page = gtk_notebook_get_current_page(nb);
1090 
1091     if (last_page) {
1092         gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
1093     }
1094 
1095     on_vga = arg2 == 0;
1096 
1097     if (!on_vga) {
1098         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1099                                        FALSE);
1100     } else if (s->full_screen) {
1101         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1102                                        TRUE);
1103     }
1104 
1105     if (arg2 == 0) {
1106         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
1107     } else {
1108 #if defined(CONFIG_VTE)
1109         VirtualConsole *vc = &s->vc[arg2 - 1];
1110         VteTerminal *term = VTE_TERMINAL(vc->terminal);
1111         int width, height;
1112 
1113         width = 80 * vte_terminal_get_char_width(term);
1114         height = 25 * vte_terminal_get_char_height(term);
1115 
1116         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1117         gtk_widget_set_size_request(vc->terminal, width, height);
1118 #else
1119         g_assert_not_reached();
1120 #endif
1121     }
1122 
1123     gtk_widget_set_sensitive(s->grab_item, on_vga);
1124 
1125     gd_update_cursor(s, TRUE);
1126 }
1127 
1128 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1129 {
1130     GtkDisplayState *s = data;
1131 
1132     if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1133         gd_grab_keyboard(s);
1134     }
1135 
1136     return TRUE;
1137 }
1138 
1139 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1140 {
1141     GtkDisplayState *s = data;
1142 
1143     if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1144         gd_ungrab_keyboard(s);
1145     }
1146 
1147     return TRUE;
1148 }
1149 
1150 static gboolean gd_focus_out_event(GtkWidget *widget,
1151                                    GdkEventCrossing *crossing, gpointer data)
1152 {
1153     GtkDisplayState *s = data;
1154 
1155     gtk_release_modifiers(s);
1156 
1157     return TRUE;
1158 }
1159 
1160 /** Virtual Console Callbacks **/
1161 
1162 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1163 {
1164     VirtualConsole *vc = chr->opaque;
1165 
1166     return vc ? write(vc->fd, buf, len) : len;
1167 }
1168 
1169 static int nb_vcs;
1170 static CharDriverState *vcs[MAX_VCS];
1171 
1172 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1173 {
1174     CharDriverState *chr;
1175 
1176     chr = g_malloc0(sizeof(*chr));
1177     chr->chr_write = gd_vc_chr_write;
1178     /* defer OPENED events until our vc is fully initialized */
1179     chr->explicit_be_open = true;
1180 
1181     vcs[nb_vcs++] = chr;
1182 
1183     return chr;
1184 }
1185 
1186 void early_gtk_display_init(void)
1187 {
1188     register_vc_handler(gd_vc_handler);
1189 }
1190 
1191 #if defined(CONFIG_VTE)
1192 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
1193 {
1194     VirtualConsole *vc = opaque;
1195     uint8_t buffer[1024];
1196     ssize_t len;
1197 
1198     len = read(vc->fd, buffer, sizeof(buffer));
1199     if (len <= 0) {
1200         return FALSE;
1201     }
1202 
1203     qemu_chr_be_write(vc->chr, buffer, len);
1204 
1205     return TRUE;
1206 }
1207 #endif
1208 
1209 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group,
1210                           GtkWidget *view_menu)
1211 {
1212 #if defined(CONFIG_VTE)
1213     const char *label;
1214     char buffer[32];
1215     char path[32];
1216 #if VTE_CHECK_VERSION(0, 26, 0)
1217     VtePty *pty;
1218 #endif
1219     GIOChannel *chan;
1220     GtkWidget *scrolled_window;
1221     GtkAdjustment *vadjustment;
1222     int master_fd, slave_fd;
1223 
1224     snprintf(buffer, sizeof(buffer), "vc%d", index);
1225     snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1226 
1227     vc->chr = vcs[index];
1228 
1229     if (vc->chr->label) {
1230         label = vc->chr->label;
1231     } else {
1232         label = buffer;
1233     }
1234 
1235     vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1236     group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1237     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1238     gtk_accel_map_add_entry(path, GDK_KEY_2 + index, HOTKEY_MODIFIERS);
1239 
1240     vc->terminal = vte_terminal_new();
1241 
1242     master_fd = qemu_openpty_raw(&slave_fd, NULL);
1243     g_assert(master_fd != -1);
1244 
1245 #if VTE_CHECK_VERSION(0, 26, 0)
1246     pty = vte_pty_new_foreign(master_fd, NULL);
1247     vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1248 #else
1249     vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
1250 #endif
1251 
1252     vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1253 
1254     vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1255 
1256     scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1257     gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1258 
1259     vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1260 
1261     vc->fd = slave_fd;
1262     vc->chr->opaque = vc;
1263     vc->scrolled_window = scrolled_window;
1264 
1265     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1266                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1267 
1268     gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1269     g_signal_connect(vc->menu_item, "activate",
1270                      G_CALLBACK(gd_menu_switch_vc), s);
1271 
1272     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1273 
1274     qemu_chr_be_generic_open(vc->chr);
1275     if (vc->chr->init) {
1276         vc->chr->init(vc->chr);
1277     }
1278 
1279     chan = g_io_channel_unix_new(vc->fd);
1280     g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1281 
1282 #endif /* CONFIG_VTE */
1283     return group;
1284 }
1285 
1286 /** Window Creation **/
1287 
1288 static void gd_connect_signals(GtkDisplayState *s)
1289 {
1290     g_signal_connect(s->show_tabs_item, "activate",
1291                      G_CALLBACK(gd_menu_show_tabs), s);
1292 
1293     g_signal_connect(s->window, "key-press-event",
1294                      G_CALLBACK(gd_window_key_event), s);
1295     g_signal_connect(s->window, "delete-event",
1296                      G_CALLBACK(gd_window_close), s);
1297 
1298 #if GTK_CHECK_VERSION(3, 0, 0)
1299     g_signal_connect(s->drawing_area, "draw",
1300                      G_CALLBACK(gd_draw_event), s);
1301 #else
1302     g_signal_connect(s->drawing_area, "expose-event",
1303                      G_CALLBACK(gd_expose_event), s);
1304 #endif
1305     g_signal_connect(s->drawing_area, "event",
1306                      G_CALLBACK(gd_event), s);
1307     g_signal_connect(s->drawing_area, "button-press-event",
1308                      G_CALLBACK(gd_button_event), s);
1309     g_signal_connect(s->drawing_area, "button-release-event",
1310                      G_CALLBACK(gd_button_event), s);
1311     g_signal_connect(s->drawing_area, "scroll-event",
1312                      G_CALLBACK(gd_scroll_event), s);
1313     g_signal_connect(s->drawing_area, "key-press-event",
1314                      G_CALLBACK(gd_key_event), s);
1315     g_signal_connect(s->drawing_area, "key-release-event",
1316                      G_CALLBACK(gd_key_event), s);
1317 
1318     g_signal_connect(s->pause_item, "activate",
1319                      G_CALLBACK(gd_menu_pause), s);
1320     g_signal_connect(s->reset_item, "activate",
1321                      G_CALLBACK(gd_menu_reset), s);
1322     g_signal_connect(s->powerdown_item, "activate",
1323                      G_CALLBACK(gd_menu_powerdown), s);
1324     g_signal_connect(s->quit_item, "activate",
1325                      G_CALLBACK(gd_menu_quit), s);
1326     g_signal_connect(s->full_screen_item, "activate",
1327                      G_CALLBACK(gd_menu_full_screen), s);
1328     g_signal_connect(s->zoom_in_item, "activate",
1329                      G_CALLBACK(gd_menu_zoom_in), s);
1330     g_signal_connect(s->zoom_out_item, "activate",
1331                      G_CALLBACK(gd_menu_zoom_out), s);
1332     g_signal_connect(s->zoom_fixed_item, "activate",
1333                      G_CALLBACK(gd_menu_zoom_fixed), s);
1334     g_signal_connect(s->zoom_fit_item, "activate",
1335                      G_CALLBACK(gd_menu_zoom_fit), s);
1336     g_signal_connect(s->vga_item, "activate",
1337                      G_CALLBACK(gd_menu_switch_vc), s);
1338     g_signal_connect(s->grab_item, "activate",
1339                      G_CALLBACK(gd_menu_grab_input), s);
1340     g_signal_connect(s->notebook, "switch-page",
1341                      G_CALLBACK(gd_change_page), s);
1342     g_signal_connect(s->drawing_area, "enter-notify-event",
1343                      G_CALLBACK(gd_enter_event), s);
1344     g_signal_connect(s->drawing_area, "leave-notify-event",
1345                      G_CALLBACK(gd_leave_event), s);
1346     g_signal_connect(s->drawing_area, "focus-out-event",
1347                      G_CALLBACK(gd_focus_out_event), s);
1348 }
1349 
1350 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1351 {
1352     GtkWidget *machine_menu;
1353     GtkWidget *separator;
1354     GtkStockItem item;
1355 
1356     machine_menu = gtk_menu_new();
1357     gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1358 
1359     s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1360     gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1361 
1362     separator = gtk_separator_menu_item_new();
1363     gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1364 
1365     s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1366     gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1367 
1368     s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1369     gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1370 
1371     separator = gtk_separator_menu_item_new();
1372     gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1373 
1374     s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1375     gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1376     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1377                                  "<QEMU>/Machine/Quit");
1378     gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1379     gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1380 
1381     return machine_menu;
1382 }
1383 
1384 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1385 {
1386     GSList *group = NULL;
1387     GtkWidget *view_menu;
1388     GtkWidget *separator;
1389     int i;
1390 
1391     view_menu = gtk_menu_new();
1392     gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1393 
1394     s->full_screen_item =
1395         gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1396     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1397                                  "<QEMU>/View/Full Screen");
1398     gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
1399                             HOTKEY_MODIFIERS);
1400     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1401 
1402     separator = gtk_separator_menu_item_new();
1403     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1404 
1405     s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1406     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1407                                  "<QEMU>/View/Zoom In");
1408     gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1409                             HOTKEY_MODIFIERS);
1410     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1411 
1412     s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1413     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1414                                  "<QEMU>/View/Zoom Out");
1415     gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1416                             HOTKEY_MODIFIERS);
1417     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1418 
1419     s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1420     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1421                                  "<QEMU>/View/Zoom Fixed");
1422     gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1423                             HOTKEY_MODIFIERS);
1424     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1425 
1426     s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1427     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1428 
1429     separator = gtk_separator_menu_item_new();
1430     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1431 
1432     s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1433     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1434 
1435     s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1436     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1437                                  "<QEMU>/View/Grab Input");
1438     gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1439                             HOTKEY_MODIFIERS);
1440     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1441 
1442     separator = gtk_separator_menu_item_new();
1443     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1444 
1445     s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1446     group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1447     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1448                                  "<QEMU>/View/VGA");
1449     gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, HOTKEY_MODIFIERS);
1450     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item);
1451 
1452     for (i = 0; i < nb_vcs; i++) {
1453         VirtualConsole *vc = &s->vc[i];
1454 
1455         group = gd_vc_init(s, vc, i, group, view_menu);
1456         s->nb_vcs++;
1457     }
1458 
1459     separator = gtk_separator_menu_item_new();
1460     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1461 
1462     s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1463     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1464 
1465     return view_menu;
1466 }
1467 
1468 static void gd_create_menus(GtkDisplayState *s)
1469 {
1470     GtkAccelGroup *accel_group;
1471 
1472     accel_group = gtk_accel_group_new();
1473     s->machine_menu = gd_create_menu_machine(s, accel_group);
1474     s->view_menu = gd_create_menu_view(s, accel_group);
1475 
1476     s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1477     gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1478                               s->machine_menu);
1479     gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1480 
1481     s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1482     gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1483     gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1484 
1485     g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1486     gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1487     s->accel_group = accel_group;
1488 }
1489 
1490 static const DisplayChangeListenerOps dcl_ops = {
1491     .dpy_name          = "gtk",
1492     .dpy_gfx_update    = gd_update,
1493     .dpy_gfx_switch    = gd_switch,
1494     .dpy_refresh       = gd_refresh,
1495     .dpy_mouse_set     = gd_mouse_set,
1496     .dpy_cursor_define = gd_cursor_define,
1497 };
1498 
1499 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
1500 {
1501     GtkDisplayState *s = g_malloc0(sizeof(*s));
1502     char *filename;
1503 
1504     gtk_init(NULL, NULL);
1505 
1506     s->dcl.ops = &dcl_ops;
1507     s->dcl.con = qemu_console_lookup_by_index(0);
1508 
1509     s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1510 #if GTK_CHECK_VERSION(3, 2, 0)
1511     s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1512 #else
1513     s->vbox = gtk_vbox_new(FALSE, 0);
1514 #endif
1515     s->notebook = gtk_notebook_new();
1516     s->drawing_area = gtk_drawing_area_new();
1517     s->menu_bar = gtk_menu_bar_new();
1518 
1519     s->scale_x = 1.0;
1520     s->scale_y = 1.0;
1521     s->free_scale = FALSE;
1522 
1523     setlocale(LC_ALL, "");
1524     bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1525     textdomain("qemu");
1526 
1527     s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1528 
1529     s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1530     qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1531     qemu_add_vm_change_state_handler(gd_change_runstate, s);
1532 
1533     gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1534 
1535     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1536     if (filename) {
1537         GError *error = NULL;
1538         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1539         if (pixbuf) {
1540             gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1541         } else {
1542             g_error_free(error);
1543         }
1544         g_free(filename);
1545     }
1546 
1547     gd_create_menus(s);
1548 
1549     gd_connect_signals(s);
1550 
1551     gtk_widget_add_events(s->drawing_area,
1552                           GDK_POINTER_MOTION_MASK |
1553                           GDK_BUTTON_PRESS_MASK |
1554                           GDK_BUTTON_RELEASE_MASK |
1555                           GDK_BUTTON_MOTION_MASK |
1556                           GDK_ENTER_NOTIFY_MASK |
1557                           GDK_LEAVE_NOTIFY_MASK |
1558                           GDK_SCROLL_MASK |
1559                           GDK_KEY_PRESS_MASK);
1560     gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1561     gtk_widget_set_can_focus(s->drawing_area, TRUE);
1562 
1563     gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1564     gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1565 
1566     gd_update_caption(s);
1567 
1568     gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1569     gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1570 
1571     gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1572 
1573     gtk_widget_show_all(s->window);
1574 
1575     if (full_screen) {
1576         gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1577     }
1578     if (grab_on_hover) {
1579         gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1580     }
1581 
1582     register_displaychangelistener(&s->dcl);
1583 
1584     global_state = s;
1585 }
1586