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