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