1a6bee616a251c4e3455e498f22fc2dd3c941a28
[sdk/emulator/qemu.git] / ui / gtk.c
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 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     vga_hw_update();
331 }
332
333 static void gd_switch(DisplayChangeListener *dcl,
334                       DisplaySurface *surface)
335 {
336     GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
337     cairo_format_t kind;
338     bool resized = true;
339     int stride;
340
341     DPRINTF("resize(width=%d, height=%d)\n",
342             surface_width(surface), surface_height(surface));
343
344     if (s->surface) {
345         cairo_surface_destroy(s->surface);
346     }
347
348     if (s->ds &&
349         surface_width(s->ds) == surface_width(surface) &&
350         surface_height(s->ds) == surface_height(surface)) {
351         resized = false;
352     }
353     s->ds = surface;
354     switch (surface_bits_per_pixel(surface)) {
355     case 8:
356         kind = CAIRO_FORMAT_A8;
357         break;
358     case 16:
359         kind = CAIRO_FORMAT_RGB16_565;
360         break;
361     case 32:
362         kind = CAIRO_FORMAT_RGB24;
363         break;
364     default:
365         g_assert_not_reached();
366         break;
367     }
368
369     stride = cairo_format_stride_for_width(kind, surface_width(surface));
370     g_assert(surface_stride(surface) == stride);
371
372     s->surface = cairo_image_surface_create_for_data(surface_data(surface),
373                                                      kind,
374                                                      surface_width(surface),
375                                                      surface_height(surface),
376                                                      surface_stride(surface));
377
378     if (resized) {
379         gd_update_windowsize(s);
380     } else {
381         gd_update_full_redraw(s);
382     }
383 }
384
385 /** QEMU Events **/
386
387 static void gd_change_runstate(void *opaque, int running, RunState state)
388 {
389     GtkDisplayState *s = opaque;
390
391     gd_update_caption(s);
392 }
393
394 static void gd_mouse_mode_change(Notifier *notify, void *data)
395 {
396     gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
397                      FALSE);
398 }
399
400 /** GTK Events **/
401
402 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
403 {
404     GtkDisplayState *s = opaque;
405     GtkAccelGroupEntry *entries;
406     guint n_entries = 0;
407     gboolean propagate_accel = TRUE;
408     gboolean handled = FALSE;
409
410     entries = gtk_accel_group_query(s->accel_group, key->keyval,
411                                     key->state, &n_entries);
412     if (n_entries) {
413         const char *quark = g_quark_to_string(entries[0].accel_path_quark);
414
415         if (gd_is_grab_active(s) && strstart(quark, "<QEMU>/File/", NULL)) {
416             propagate_accel = FALSE;
417         }
418     }
419
420     if (!handled && propagate_accel) {
421         handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
422     }
423     if (handled) {
424         gtk_release_modifiers(s);
425     } else {
426         handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
427     }
428
429     return handled;
430 }
431
432 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
433                                 void *opaque)
434 {
435     GtkDisplayState *s = opaque;
436
437     if (!no_quit) {
438         unregister_displaychangelistener(&s->dcl);
439         qmp_quit(NULL);
440         return FALSE;
441     }
442
443     return TRUE;
444 }
445
446 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
447 {
448     GtkDisplayState *s = opaque;
449     int mx, my;
450     int ww, wh;
451     int fbw, fbh;
452
453     if (!gtk_widget_get_realized(widget)) {
454         return FALSE;
455     }
456
457     fbw = surface_width(s->ds);
458     fbh = surface_height(s->ds);
459
460     gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
461
462     if (s->full_screen) {
463         s->scale_x = (double)ww / fbw;
464         s->scale_y = (double)wh / fbh;
465     } else if (s->free_scale) {
466         double sx, sy;
467
468         sx = (double)ww / fbw;
469         sy = (double)wh / fbh;
470
471         s->scale_x = s->scale_y = MIN(sx, sy);
472     }
473
474     fbw *= s->scale_x;
475     fbh *= s->scale_y;
476
477     mx = my = 0;
478     if (ww > fbw) {
479         mx = (ww - fbw) / 2;
480     }
481     if (wh > fbh) {
482         my = (wh - fbh) / 2;
483     }
484
485     cairo_rectangle(cr, 0, 0, ww, wh);
486
487     /* Optionally cut out the inner area where the pixmap
488        will be drawn. This avoids 'flashing' since we're
489        not double-buffering. Note we're using the undocumented
490        behaviour of drawing the rectangle from right to left
491        to cut out the whole */
492     cairo_rectangle(cr, mx + fbw, my,
493                     -1 * fbw, fbh);
494     cairo_fill(cr);
495
496     cairo_scale(cr, s->scale_x, s->scale_y);
497     cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
498     cairo_paint(cr);
499
500     return TRUE;
501 }
502
503 #if !GTK_CHECK_VERSION(3, 0, 0)
504 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
505                                 void *opaque)
506 {
507     cairo_t *cr;
508     gboolean ret;
509
510     cr = gdk_cairo_create(gtk_widget_get_window(widget));
511     cairo_rectangle(cr,
512                     expose->area.x,
513                     expose->area.y,
514                     expose->area.width,
515                     expose->area.height);
516     cairo_clip(cr);
517
518     ret = gd_draw_event(widget, cr, opaque);
519
520     cairo_destroy(cr);
521
522     return ret;
523 }
524 #endif
525
526 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
527                                 void *opaque)
528 {
529     GtkDisplayState *s = opaque;
530     int dx, dy;
531     int x, y;
532     int mx, my;
533     int fbh, fbw;
534     int ww, wh;
535
536     fbw = surface_width(s->ds) * s->scale_x;
537     fbh = surface_height(s->ds) * s->scale_y;
538
539     gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
540
541     mx = my = 0;
542     if (ww > fbw) {
543         mx = (ww - fbw) / 2;
544     }
545     if (wh > fbh) {
546         my = (wh - fbh) / 2;
547     }
548
549     x = (motion->x - mx) / s->scale_x;
550     y = (motion->y - my) / s->scale_y;
551
552     if (x < 0 || y < 0 ||
553         x >= surface_width(s->ds) ||
554         y >= surface_height(s->ds)) {
555         return TRUE;
556     }
557
558     if (kbd_mouse_is_absolute()) {
559         dx = x * 0x7FFF / (surface_width(s->ds) - 1);
560         dy = y * 0x7FFF / (surface_height(s->ds) - 1);
561     } else if (s->last_x == -1 || s->last_y == -1) {
562         dx = 0;
563         dy = 0;
564     } else {
565         dx = x - s->last_x;
566         dy = y - s->last_y;
567     }
568
569     s->last_x = x;
570     s->last_y = y;
571
572     if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
573         kbd_mouse_event(dx, dy, 0, s->button_mask);
574     }
575
576     if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) {
577         GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
578         int x = (int)motion->x_root;
579         int y = (int)motion->y_root;
580
581         /* In relative mode check to see if client pointer hit
582          * one of the screen edges, and if so move it back by
583          * 200 pixels. This is important because the pointer
584          * in the server doesn't correspond 1-for-1, and so
585          * may still be only half way across the screen. Without
586          * this warp, the server pointer would thus appear to hit
587          * an invisible wall */
588         if (x == 0) {
589             x += 200;
590         }
591         if (y == 0) {
592             y += 200;
593         }
594         if (x == (gdk_screen_get_width(screen) - 1)) {
595             x -= 200;
596         }
597         if (y == (gdk_screen_get_height(screen) - 1)) {
598             y -= 200;
599         }
600
601         if (x != (int)motion->x_root || y != (int)motion->y_root) {
602 #if GTK_CHECK_VERSION(3, 0, 0)
603             GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
604             gdk_device_warp(dev, screen, x, y);
605 #else
606             GdkDisplay *display = gtk_widget_get_display(widget);
607             gdk_display_warp_pointer(display, screen, x, y);
608 #endif
609             s->last_x = -1;
610             s->last_y = -1;
611             return FALSE;
612         }
613     }
614     return TRUE;
615 }
616
617 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
618                                 void *opaque)
619 {
620     GtkDisplayState *s = opaque;
621     int dx, dy;
622     int n;
623
624     if (button->button == 1) {
625         n = 0x01;
626     } else if (button->button == 2) {
627         n = 0x04;
628     } else if (button->button == 3) {
629         n = 0x02;
630     } else {
631         n = 0x00;
632     }
633
634     if (button->type == GDK_BUTTON_PRESS) {
635         s->button_mask |= n;
636     } else if (button->type == GDK_BUTTON_RELEASE) {
637         s->button_mask &= ~n;
638     }
639
640     if (kbd_mouse_is_absolute()) {
641         dx = s->last_x * 0x7FFF / (surface_width(s->ds) - 1);
642         dy = s->last_y * 0x7FFF / (surface_height(s->ds) - 1);
643     } else {
644         dx = 0;
645         dy = 0;
646     }
647
648     kbd_mouse_event(dx, dy, 0, s->button_mask);
649         
650     return TRUE;
651 }
652
653 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
654 {
655     GtkDisplayState *s = opaque;
656     int gdk_keycode;
657     int qemu_keycode;
658     int i;
659
660     gdk_keycode = key->hardware_keycode;
661
662     if (gdk_keycode < 9) {
663         qemu_keycode = 0;
664     } else if (gdk_keycode < 97) {
665         qemu_keycode = gdk_keycode - 8;
666     } else if (gdk_keycode < 158) {
667         qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
668     } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
669         qemu_keycode = 0x70;
670     } else if (gdk_keycode == 211) { /* backslash */
671         qemu_keycode = 0x73;
672     } else {
673         qemu_keycode = 0;
674     }
675
676     DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
677             gdk_keycode, qemu_keycode,
678             (key->type == GDK_KEY_PRESS) ? "down" : "up");
679
680     for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
681         if (qemu_keycode == modifier_keycode[i]) {
682             s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
683         }
684     }
685
686     if (qemu_keycode & SCANCODE_GREY) {
687         kbd_put_keycode(SCANCODE_EMUL0);
688     }
689
690     if (key->type == GDK_KEY_PRESS) {
691         kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK);
692     } else if (key->type == GDK_KEY_RELEASE) {
693         kbd_put_keycode(qemu_keycode | SCANCODE_UP);
694     } else {
695         g_assert_not_reached();
696     }
697
698     return TRUE;
699 }
700
701 /** Window Menu Actions **/
702
703 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
704 {
705     GtkDisplayState *s = opaque;
706
707     if (s->external_pause_update) {
708         return;
709     }
710     if (runstate_is_running()) {
711         qmp_stop(NULL);
712     } else {
713         qmp_cont(NULL);
714     }
715 }
716
717 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
718 {
719     qmp_system_reset(NULL);
720 }
721
722 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
723 {
724     qmp_system_powerdown(NULL);
725 }
726
727 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
728 {
729     qmp_quit(NULL);
730 }
731
732 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
733 {
734     GtkDisplayState *s = opaque;
735
736     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
737         gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
738     } else {
739         int i;
740
741         gtk_release_modifiers(s);
742         for (i = 0; i < s->nb_vcs; i++) {
743             if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
744                 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
745                 break;
746             }
747         }
748     }
749 }
750
751 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
752 {
753     GtkDisplayState *s = opaque;
754
755     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
756         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
757     } else {
758         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
759     }
760 }
761
762 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
763 {
764     GtkDisplayState *s = opaque;
765
766     if (!s->full_screen) {
767         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
768         gtk_widget_set_size_request(s->menu_bar, 0, 0);
769         gtk_widget_set_size_request(s->drawing_area, -1, -1);
770         gtk_window_fullscreen(GTK_WINDOW(s->window));
771         if (gd_on_vga(s)) {
772             gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
773         }
774         s->full_screen = TRUE;
775     } else {
776         gtk_window_unfullscreen(GTK_WINDOW(s->window));
777         gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
778         gtk_widget_set_size_request(s->menu_bar, -1, -1);
779         gtk_widget_set_size_request(s->drawing_area,
780                                     surface_width(s->ds),
781                                     surface_height(s->ds));
782         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
783         s->full_screen = FALSE;
784         s->scale_x = 1.0;
785         s->scale_y = 1.0;
786     }
787
788     gd_update_cursor(s, FALSE);
789 }
790
791 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
792 {
793     GtkDisplayState *s = opaque;
794
795     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
796                                    FALSE);
797
798     s->scale_x += .25;
799     s->scale_y += .25;
800
801     gd_update_windowsize(s);
802 }
803
804 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
805 {
806     GtkDisplayState *s = opaque;
807
808     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
809                                    FALSE);
810
811     s->scale_x -= .25;
812     s->scale_y -= .25;
813
814     s->scale_x = MAX(s->scale_x, .25);
815     s->scale_y = MAX(s->scale_y, .25);
816
817     gd_update_windowsize(s);
818 }
819
820 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
821 {
822     GtkDisplayState *s = opaque;
823
824     s->scale_x = 1.0;
825     s->scale_y = 1.0;
826
827     gd_update_windowsize(s);
828 }
829
830 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
831 {
832     GtkDisplayState *s = opaque;
833
834     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
835         s->free_scale = TRUE;
836     } else {
837         s->free_scale = FALSE;
838     }
839
840     gd_update_windowsize(s);
841     gd_update_full_redraw(s);
842 }
843
844 static void gd_grab_keyboard(GtkDisplayState *s)
845 {
846 #if GTK_CHECK_VERSION(3, 0, 0)
847     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
848     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
849     GList *devices = gdk_device_manager_list_devices(mgr,
850                                                      GDK_DEVICE_TYPE_MASTER);
851     GList *tmp = devices;
852     while (tmp) {
853         GdkDevice *dev = tmp->data;
854         if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
855             gdk_device_grab(dev,
856                             gtk_widget_get_window(s->drawing_area),
857                             GDK_OWNERSHIP_NONE,
858                             FALSE,
859                             GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
860                             NULL,
861                             GDK_CURRENT_TIME);
862         }
863         tmp = tmp->next;
864     }
865     g_list_free(devices);
866 #else
867     gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
868                       FALSE,
869                       GDK_CURRENT_TIME);
870 #endif
871 }
872
873 static void gd_ungrab_keyboard(GtkDisplayState *s)
874 {
875 #if GTK_CHECK_VERSION(3, 0, 0)
876     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
877     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
878     GList *devices = gdk_device_manager_list_devices(mgr,
879                                                      GDK_DEVICE_TYPE_MASTER);
880     GList *tmp = devices;
881     while (tmp) {
882         GdkDevice *dev = tmp->data;
883         if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
884             gdk_device_ungrab(dev,
885                               GDK_CURRENT_TIME);
886         }
887         tmp = tmp->next;
888     }
889     g_list_free(devices);
890 #else
891     gdk_keyboard_ungrab(GDK_CURRENT_TIME);
892 #endif
893 }
894
895 static void gd_grab_pointer(GtkDisplayState *s)
896 {
897 #if GTK_CHECK_VERSION(3, 0, 0)
898     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
899     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
900     GList *devices = gdk_device_manager_list_devices(mgr,
901                                                      GDK_DEVICE_TYPE_MASTER);
902     GList *tmp = devices;
903     while (tmp) {
904         GdkDevice *dev = tmp->data;
905         if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
906             gdk_device_grab(dev,
907                             gtk_widget_get_window(s->drawing_area),
908                             GDK_OWNERSHIP_NONE,
909                             FALSE, /* All events to come to our
910                                       window directly */
911                             GDK_POINTER_MOTION_MASK |
912                             GDK_BUTTON_PRESS_MASK |
913                             GDK_BUTTON_RELEASE_MASK |
914                             GDK_BUTTON_MOTION_MASK |
915                             GDK_SCROLL_MASK,
916                             s->null_cursor,
917                             GDK_CURRENT_TIME);
918         }
919         tmp = tmp->next;
920     }
921     g_list_free(devices);
922 #else
923     gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
924                      FALSE, /* All events to come to our window directly */
925                      GDK_POINTER_MOTION_MASK |
926                      GDK_BUTTON_PRESS_MASK |
927                      GDK_BUTTON_RELEASE_MASK |
928                      GDK_BUTTON_MOTION_MASK |
929                      GDK_SCROLL_MASK,
930                      NULL, /* Allow cursor to move over entire desktop */
931                      s->null_cursor,
932                      GDK_CURRENT_TIME);
933 #endif
934 }
935
936 static void gd_ungrab_pointer(GtkDisplayState *s)
937 {
938 #if GTK_CHECK_VERSION(3, 0, 0)
939     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
940     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
941     GList *devices = gdk_device_manager_list_devices(mgr,
942                                                      GDK_DEVICE_TYPE_MASTER);
943     GList *tmp = devices;
944     while (tmp) {
945         GdkDevice *dev = tmp->data;
946         if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
947             gdk_device_ungrab(dev,
948                               GDK_CURRENT_TIME);
949         }
950         tmp = tmp->next;
951     }
952     g_list_free(devices);
953 #else
954     gdk_pointer_ungrab(GDK_CURRENT_TIME);
955 #endif
956 }
957
958 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
959 {
960     GtkDisplayState *s = opaque;
961
962     if (gd_is_grab_active(s)) {
963         gd_grab_keyboard(s);
964         gd_grab_pointer(s);
965     } else {
966         gd_ungrab_keyboard(s);
967         gd_ungrab_pointer(s);
968     }
969
970     gd_update_caption(s);
971     gd_update_cursor(s, FALSE);
972 }
973
974 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
975                            gpointer data)
976 {
977     GtkDisplayState *s = data;
978     guint last_page;
979     gboolean on_vga;
980
981     if (!gtk_widget_get_realized(s->notebook)) {
982         return;
983     }
984
985     last_page = gtk_notebook_get_current_page(nb);
986
987     if (last_page) {
988         gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
989     }
990
991     on_vga = arg2 == 0;
992
993     if (!on_vga) {
994         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
995                                        FALSE);
996     } else if (s->full_screen) {
997         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
998                                        TRUE);
999     }
1000
1001     if (arg2 == 0) {
1002         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
1003     } else {
1004         VirtualConsole *vc = &s->vc[arg2 - 1];
1005         VteTerminal *term = VTE_TERMINAL(vc->terminal);
1006         int width, height;
1007
1008         width = 80 * vte_terminal_get_char_width(term);
1009         height = 25 * vte_terminal_get_char_height(term);
1010
1011         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1012         gtk_widget_set_size_request(vc->terminal, width, height);
1013     }
1014
1015     gtk_widget_set_sensitive(s->grab_item, on_vga);
1016
1017     gd_update_cursor(s, TRUE);
1018 }
1019
1020 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1021 {
1022     GtkDisplayState *s = data;
1023
1024     if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1025         gd_grab_keyboard(s);
1026     }
1027
1028     return TRUE;
1029 }
1030
1031 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1032 {
1033     GtkDisplayState *s = data;
1034
1035     if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1036         gd_ungrab_keyboard(s);
1037     }
1038
1039     return TRUE;
1040 }
1041
1042 static gboolean gd_focus_out_event(GtkWidget *widget,
1043                                    GdkEventCrossing *crossing, gpointer data)
1044 {
1045     GtkDisplayState *s = data;
1046
1047     gtk_release_modifiers(s);
1048
1049     return TRUE;
1050 }
1051
1052 /** Virtual Console Callbacks **/
1053
1054 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1055 {
1056     VirtualConsole *vc = chr->opaque;
1057
1058     return write(vc->fd, buf, len);
1059 }
1060
1061 static int nb_vcs;
1062 static CharDriverState *vcs[MAX_VCS];
1063
1064 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1065 {
1066     CharDriverState *chr;
1067
1068     chr = g_malloc0(sizeof(*chr));
1069     chr->chr_write = gd_vc_chr_write;
1070
1071     vcs[nb_vcs++] = chr;
1072
1073     return chr;
1074 }
1075
1076 void early_gtk_display_init(void)
1077 {
1078     register_vc_handler(gd_vc_handler);
1079 }
1080
1081 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
1082 {
1083     VirtualConsole *vc = opaque;
1084     uint8_t buffer[1024];
1085     ssize_t len;
1086
1087     len = read(vc->fd, buffer, sizeof(buffer));
1088     if (len <= 0) {
1089         return FALSE;
1090     }
1091
1092     qemu_chr_be_write(vc->chr, buffer, len);
1093
1094     return TRUE;
1095 }
1096
1097 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group)
1098 {
1099     const char *label;
1100     char buffer[32];
1101     char path[32];
1102 #if VTE_CHECK_VERSION(0, 26, 0)
1103     VtePty *pty;
1104 #endif
1105     GIOChannel *chan;
1106     GtkWidget *scrolled_window;
1107     GtkAdjustment *vadjustment;
1108     int master_fd, slave_fd, ret;
1109     struct termios tty;
1110
1111     snprintf(buffer, sizeof(buffer), "vc%d", index);
1112     snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1113
1114     vc->chr = vcs[index];
1115
1116     if (vc->chr->label) {
1117         label = vc->chr->label;
1118     } else {
1119         label = buffer;
1120     }
1121
1122     vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1123     group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1124     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1125     gtk_accel_map_add_entry(path, GDK_KEY_2 + index, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1126
1127     vc->terminal = vte_terminal_new();
1128
1129     ret = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
1130     g_assert(ret != -1);
1131
1132     /* Set raw attributes on the pty. */
1133     tcgetattr(slave_fd, &tty);
1134     cfmakeraw(&tty);
1135     tcsetattr(slave_fd, TCSAFLUSH, &tty);
1136
1137 #if VTE_CHECK_VERSION(0, 26, 0)
1138     pty = vte_pty_new_foreign(master_fd, NULL);
1139     vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1140 #else
1141     vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
1142 #endif
1143
1144     vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1145
1146     vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1147
1148     scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1149     gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1150
1151     vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1152
1153     vc->fd = slave_fd;
1154     vc->chr->opaque = vc;
1155     vc->scrolled_window = scrolled_window;
1156
1157     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1158                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1159
1160     gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1161     g_signal_connect(vc->menu_item, "activate",
1162                      G_CALLBACK(gd_menu_switch_vc), s);
1163
1164     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), vc->menu_item);
1165
1166     qemu_chr_be_generic_open(vc->chr);
1167     if (vc->chr->init) {
1168         vc->chr->init(vc->chr);
1169     }
1170
1171     chan = g_io_channel_unix_new(vc->fd);
1172     g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1173
1174     return group;
1175 }
1176
1177 /** Window Creation **/
1178
1179 static void gd_connect_signals(GtkDisplayState *s)
1180 {
1181     g_signal_connect(s->show_tabs_item, "activate",
1182                      G_CALLBACK(gd_menu_show_tabs), s);
1183
1184     g_signal_connect(s->window, "key-press-event",
1185                      G_CALLBACK(gd_window_key_event), s);
1186     g_signal_connect(s->window, "delete-event",
1187                      G_CALLBACK(gd_window_close), s);
1188
1189 #if GTK_CHECK_VERSION(3, 0, 0)
1190     g_signal_connect(s->drawing_area, "draw",
1191                      G_CALLBACK(gd_draw_event), s);
1192 #else
1193     g_signal_connect(s->drawing_area, "expose-event",
1194                      G_CALLBACK(gd_expose_event), s);
1195 #endif
1196     g_signal_connect(s->drawing_area, "motion-notify-event",
1197                      G_CALLBACK(gd_motion_event), s);
1198     g_signal_connect(s->drawing_area, "button-press-event",
1199                      G_CALLBACK(gd_button_event), s);
1200     g_signal_connect(s->drawing_area, "button-release-event",
1201                      G_CALLBACK(gd_button_event), s);
1202     g_signal_connect(s->drawing_area, "key-press-event",
1203                      G_CALLBACK(gd_key_event), s);
1204     g_signal_connect(s->drawing_area, "key-release-event",
1205                      G_CALLBACK(gd_key_event), s);
1206
1207     g_signal_connect(s->pause_item, "activate",
1208                      G_CALLBACK(gd_menu_pause), s);
1209     g_signal_connect(s->reset_item, "activate",
1210                      G_CALLBACK(gd_menu_reset), s);
1211     g_signal_connect(s->powerdown_item, "activate",
1212                      G_CALLBACK(gd_menu_powerdown), s);
1213     g_signal_connect(s->quit_item, "activate",
1214                      G_CALLBACK(gd_menu_quit), s);
1215     g_signal_connect(s->full_screen_item, "activate",
1216                      G_CALLBACK(gd_menu_full_screen), s);
1217     g_signal_connect(s->zoom_in_item, "activate",
1218                      G_CALLBACK(gd_menu_zoom_in), s);
1219     g_signal_connect(s->zoom_out_item, "activate",
1220                      G_CALLBACK(gd_menu_zoom_out), s);
1221     g_signal_connect(s->zoom_fixed_item, "activate",
1222                      G_CALLBACK(gd_menu_zoom_fixed), s);
1223     g_signal_connect(s->zoom_fit_item, "activate",
1224                      G_CALLBACK(gd_menu_zoom_fit), s);
1225     g_signal_connect(s->vga_item, "activate",
1226                      G_CALLBACK(gd_menu_switch_vc), s);
1227     g_signal_connect(s->grab_item, "activate",
1228                      G_CALLBACK(gd_menu_grab_input), s);
1229     g_signal_connect(s->notebook, "switch-page",
1230                      G_CALLBACK(gd_change_page), s);
1231     g_signal_connect(s->drawing_area, "enter-notify-event",
1232                      G_CALLBACK(gd_enter_event), s);
1233     g_signal_connect(s->drawing_area, "leave-notify-event",
1234                      G_CALLBACK(gd_leave_event), s);
1235     g_signal_connect(s->drawing_area, "focus-out-event",
1236                      G_CALLBACK(gd_focus_out_event), s);
1237 }
1238
1239 static void gd_create_menus(GtkDisplayState *s)
1240 {
1241     GtkStockItem item;
1242     GtkAccelGroup *accel_group;
1243     GSList *group = NULL;
1244     GtkWidget *separator;
1245     int i;
1246
1247     accel_group = gtk_accel_group_new();
1248     s->machine_menu = gtk_menu_new();
1249     gtk_menu_set_accel_group(GTK_MENU(s->machine_menu), accel_group);
1250     s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1251
1252     s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1253     gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->pause_item);
1254
1255     separator = gtk_separator_menu_item_new();
1256     gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), separator);
1257
1258     s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1259     gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->reset_item);
1260
1261     s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1262     gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->powerdown_item);
1263
1264     separator = gtk_separator_menu_item_new();
1265     gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), separator);
1266
1267     s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1268     gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1269     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1270                                  "<QEMU>/Machine/Quit");
1271     gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1272     gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->quit_item);
1273
1274     s->view_menu = gtk_menu_new();
1275     gtk_menu_set_accel_group(GTK_MENU(s->view_menu), accel_group);
1276     s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1277
1278     s->full_screen_item =
1279         gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1280     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1281                                  "<QEMU>/View/Full Screen");
1282     gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1283     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->full_screen_item);
1284
1285     separator = gtk_separator_menu_item_new();
1286     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1287
1288     s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1289     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1290                                  "<QEMU>/View/Zoom In");
1291     gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1292     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_in_item);
1293
1294     s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1295     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1296                                  "<QEMU>/View/Zoom Out");
1297     gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1298     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_out_item);
1299
1300     s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1301     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1302                                  "<QEMU>/View/Zoom Fixed");
1303     gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1304     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_fixed_item);
1305
1306     s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1307     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_fit_item);
1308
1309     separator = gtk_separator_menu_item_new();
1310     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1311
1312     s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1313     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->grab_on_hover_item);
1314
1315     s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1316     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1317                                  "<QEMU>/View/Grab Input");
1318     gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1319     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->grab_item);
1320
1321     separator = gtk_separator_menu_item_new();
1322     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1323
1324     s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1325     group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1326     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1327                                  "<QEMU>/View/VGA");
1328     gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1329     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->vga_item);
1330
1331     for (i = 0; i < nb_vcs; i++) {
1332         VirtualConsole *vc = &s->vc[i];
1333
1334         group = gd_vc_init(s, vc, i, group);
1335         s->nb_vcs++;
1336     }
1337
1338     separator = gtk_separator_menu_item_new();
1339     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1340
1341     s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1342     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->show_tabs_item);
1343
1344     g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1345     gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1346     s->accel_group = accel_group;
1347
1348     gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1349                               s->machine_menu);
1350     gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1351
1352     gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1353     gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1354 }
1355
1356 static const DisplayChangeListenerOps dcl_ops = {
1357     .dpy_name          = "gtk",
1358     .dpy_gfx_update    = gd_update,
1359     .dpy_gfx_switch    = gd_switch,
1360     .dpy_refresh       = gd_refresh,
1361 };
1362
1363 void gtk_display_init(DisplayState *ds)
1364 {
1365     GtkDisplayState *s = g_malloc0(sizeof(*s));
1366     char *filename;
1367
1368     gtk_init(NULL, NULL);
1369
1370     s->dcl.ops = &dcl_ops;
1371
1372     s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1373 #if GTK_CHECK_VERSION(3, 2, 0)
1374     s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1375 #else
1376     s->vbox = gtk_vbox_new(FALSE, 0);
1377 #endif
1378     s->notebook = gtk_notebook_new();
1379     s->drawing_area = gtk_drawing_area_new();
1380     s->menu_bar = gtk_menu_bar_new();
1381
1382     s->scale_x = 1.0;
1383     s->scale_y = 1.0;
1384     s->free_scale = FALSE;
1385
1386     setlocale(LC_ALL, "");
1387     bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1388     textdomain("qemu");
1389
1390     s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1391
1392     s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1393     qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1394     qemu_add_vm_change_state_handler(gd_change_runstate, s);
1395
1396     gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1397
1398     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
1399     if (filename) {
1400         GError *error = NULL;
1401         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1402         if (pixbuf) {
1403             gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1404         } else {
1405             g_error_free(error);
1406         }
1407         g_free(filename);
1408     }
1409
1410     gd_create_menus(s);
1411
1412     gd_connect_signals(s);
1413
1414     gtk_widget_add_events(s->drawing_area,
1415                           GDK_POINTER_MOTION_MASK |
1416                           GDK_BUTTON_PRESS_MASK |
1417                           GDK_BUTTON_RELEASE_MASK |
1418                           GDK_BUTTON_MOTION_MASK |
1419                           GDK_ENTER_NOTIFY_MASK |
1420                           GDK_LEAVE_NOTIFY_MASK |
1421                           GDK_SCROLL_MASK |
1422                           GDK_KEY_PRESS_MASK);
1423     gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1424     gtk_widget_set_can_focus(s->drawing_area, TRUE);
1425
1426     gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1427     gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1428
1429     gd_update_caption(s);
1430
1431     gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1432     gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1433
1434     gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1435
1436     gtk_widget_show_all(s->window);
1437
1438     register_displaychangelistener(ds, &s->dcl);
1439
1440     global_state = s;
1441 }