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