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