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