openpic: avoid buffer overrun on incoming migration
[qemu.git] / ui / gtk.c
blob9f5061a1be73b1787716aef4b7f9b138c28f8022
1 /*
2 * GTK UI
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
12 * Portions from gtk-vnc:
14 * GTK VNC Widget
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
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.
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.
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
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
37 #ifdef _WIN32
38 # define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
39 #endif
41 #include "qemu-common.h"
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
54 #include <gdk/gdkkeysyms.h>
55 #include <glib/gi18n.h>
56 #include <locale.h>
57 #if defined(CONFIG_VTE)
58 #include <vte/vte.h>
59 #endif
60 #include <math.h>
62 #include "trace.h"
63 #include "ui/console.h"
64 #include "ui/input.h"
65 #include "sysemu/sysemu.h"
66 #include "qmp-commands.h"
67 #include "x_keymap.h"
68 #include "keymaps.h"
69 #include "sysemu/char.h"
71 #define MAX_VCS 10
73 #if !defined(CONFIG_VTE)
74 # define VTE_CHECK_VERSION(a, b, c) 0
75 #endif
77 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
78 #if GTK_CHECK_VERSION(3, 0, 0)
79 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
81 *ww = gdk_window_get_width(w);
82 *wh = gdk_window_get_height(w);
84 #endif
86 #if !GTK_CHECK_VERSION(2, 20, 0)
87 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
88 #endif
90 #ifndef GDK_KEY_0
91 #define GDK_KEY_0 GDK_0
92 #define GDK_KEY_1 GDK_1
93 #define GDK_KEY_2 GDK_2
94 #define GDK_KEY_f GDK_f
95 #define GDK_KEY_g GDK_g
96 #define GDK_KEY_q GDK_q
97 #define GDK_KEY_plus GDK_plus
98 #define GDK_KEY_minus GDK_minus
99 #endif
101 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
103 static const int modifier_keycode[] = {
104 /* shift, control, alt keys, meta keys, both left & right */
105 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
108 typedef struct VirtualConsole
110 GtkWidget *menu_item;
111 GtkWidget *terminal;
112 #if defined(CONFIG_VTE)
113 GtkWidget *scrolled_window;
114 CharDriverState *chr;
115 #endif
116 } VirtualConsole;
118 typedef struct GtkDisplayState
120 GtkWidget *window;
122 GtkWidget *menu_bar;
124 GtkAccelGroup *accel_group;
126 GtkWidget *machine_menu_item;
127 GtkWidget *machine_menu;
128 GtkWidget *pause_item;
129 GtkWidget *reset_item;
130 GtkWidget *powerdown_item;
131 GtkWidget *quit_item;
133 GtkWidget *view_menu_item;
134 GtkWidget *view_menu;
135 GtkWidget *full_screen_item;
136 GtkWidget *zoom_in_item;
137 GtkWidget *zoom_out_item;
138 GtkWidget *zoom_fixed_item;
139 GtkWidget *zoom_fit_item;
140 GtkWidget *grab_item;
141 GtkWidget *grab_on_hover_item;
142 GtkWidget *vga_item;
144 int nb_vcs;
145 VirtualConsole vc[MAX_VCS];
147 GtkWidget *show_tabs_item;
149 GtkWidget *vbox;
150 GtkWidget *notebook;
151 GtkWidget *drawing_area;
152 cairo_surface_t *surface;
153 pixman_image_t *convert;
154 DisplayChangeListener dcl;
155 DisplaySurface *ds;
156 int button_mask;
157 gboolean last_set;
158 int last_x;
159 int last_y;
160 int grab_x_root;
161 int grab_y_root;
163 double scale_x;
164 double scale_y;
165 gboolean full_screen;
167 GdkCursor *null_cursor;
168 Notifier mouse_mode_notifier;
169 gboolean free_scale;
171 bool external_pause_update;
173 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
174 } GtkDisplayState;
176 static GtkDisplayState *global_state;
178 /** Utility Functions **/
180 static bool gd_is_grab_active(GtkDisplayState *s)
182 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
185 static bool gd_grab_on_hover(GtkDisplayState *s)
187 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
190 static bool gd_on_vga(GtkDisplayState *s)
192 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
195 static void gd_update_cursor(GtkDisplayState *s, gboolean override)
197 GdkWindow *window;
198 bool on_vga;
200 window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));
202 on_vga = gd_on_vga(s);
204 if ((override || on_vga) &&
205 (s->full_screen || qemu_input_is_absolute() || gd_is_grab_active(s))) {
206 gdk_window_set_cursor(window, s->null_cursor);
207 } else {
208 gdk_window_set_cursor(window, NULL);
212 static void gd_update_caption(GtkDisplayState *s)
214 const char *status = "";
215 gchar *title;
216 const char *grab = "";
217 bool is_paused = !runstate_is_running();
219 if (gd_is_grab_active(s)) {
220 grab = _(" - Press Ctrl+Alt+G to release grab");
223 if (is_paused) {
224 status = _(" [Paused]");
226 s->external_pause_update = true;
227 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
228 is_paused);
229 s->external_pause_update = false;
231 if (qemu_name) {
232 title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
233 } else {
234 title = g_strdup_printf("QEMU%s%s", status, grab);
237 gtk_window_set_title(GTK_WINDOW(s->window), title);
239 g_free(title);
242 static void gd_update_windowsize(GtkDisplayState *s)
244 if (!s->full_screen) {
245 GtkRequisition req;
246 double sx, sy;
248 if (s->free_scale) {
249 sx = s->scale_x;
250 sy = s->scale_y;
252 s->scale_y = 1.0;
253 s->scale_x = 1.0;
254 } else {
255 sx = 1.0;
256 sy = 1.0;
259 gtk_widget_set_size_request(s->drawing_area,
260 surface_width(s->ds) * s->scale_x,
261 surface_height(s->ds) * s->scale_y);
262 #if GTK_CHECK_VERSION(3, 0, 0)
263 gtk_widget_get_preferred_size(s->vbox, NULL, &req);
264 #else
265 gtk_widget_size_request(s->vbox, &req);
266 #endif
268 gtk_window_resize(GTK_WINDOW(s->window),
269 req.width * sx, req.height * sy);
273 static void gd_update_full_redraw(GtkDisplayState *s)
275 int ww, wh;
276 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
277 gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
280 static void gtk_release_modifiers(GtkDisplayState *s)
282 int i, keycode;
284 if (!gd_on_vga(s)) {
285 return;
287 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
288 keycode = modifier_keycode[i];
289 if (!s->modifier_pressed[i]) {
290 continue;
292 qemu_input_event_send_key_number(s->dcl.con, keycode, false);
293 s->modifier_pressed[i] = false;
297 /** DisplayState Callbacks **/
299 static void gd_update(DisplayChangeListener *dcl,
300 int x, int y, int w, int h)
302 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
303 int x1, x2, y1, y2;
304 int mx, my;
305 int fbw, fbh;
306 int ww, wh;
308 trace_gd_update(x, y, w, h);
310 if (s->convert) {
311 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
312 x, y, 0, 0, x, y, w, h);
315 x1 = floor(x * s->scale_x);
316 y1 = floor(y * s->scale_y);
318 x2 = ceil(x * s->scale_x + w * s->scale_x);
319 y2 = ceil(y * s->scale_y + h * s->scale_y);
321 fbw = surface_width(s->ds) * s->scale_x;
322 fbh = surface_height(s->ds) * s->scale_y;
324 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
326 mx = my = 0;
327 if (ww > fbw) {
328 mx = (ww - fbw) / 2;
330 if (wh > fbh) {
331 my = (wh - fbh) / 2;
334 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
337 static void gd_refresh(DisplayChangeListener *dcl)
339 graphic_hw_update(dcl->con);
342 #if GTK_CHECK_VERSION(3, 0, 0)
343 static void gd_mouse_set(DisplayChangeListener *dcl,
344 int x, int y, int visible)
346 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
347 GdkDisplay *dpy;
348 GdkDeviceManager *mgr;
349 gint x_root, y_root;
351 if (qemu_input_is_absolute()) {
352 return;
355 dpy = gtk_widget_get_display(s->drawing_area);
356 mgr = gdk_display_get_device_manager(dpy);
357 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
358 x, y, &x_root, &y_root);
359 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
360 gtk_widget_get_screen(s->drawing_area),
361 x_root, y_root);
363 #else
364 static void gd_mouse_set(DisplayChangeListener *dcl,
365 int x, int y, int visible)
367 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
368 gint x_root, y_root;
370 if (qemu_input_is_absolute()) {
371 return;
374 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
375 x, y, &x_root, &y_root);
376 gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area),
377 gtk_widget_get_screen(s->drawing_area),
378 x_root, y_root);
380 #endif
382 static void gd_cursor_define(DisplayChangeListener *dcl,
383 QEMUCursor *c)
385 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
386 GdkPixbuf *pixbuf;
387 GdkCursor *cursor;
389 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
390 GDK_COLORSPACE_RGB, true, 8,
391 c->width, c->height, c->width * 4,
392 NULL, NULL);
393 cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area),
394 pixbuf, c->hot_x, c->hot_y);
395 gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor);
396 g_object_unref(pixbuf);
397 #if !GTK_CHECK_VERSION(3, 0, 0)
398 gdk_cursor_unref(cursor);
399 #else
400 g_object_unref(cursor);
401 #endif
404 static void gd_switch(DisplayChangeListener *dcl,
405 DisplaySurface *surface)
407 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
408 bool resized = true;
410 trace_gd_switch(surface_width(surface), surface_height(surface));
412 if (s->surface) {
413 cairo_surface_destroy(s->surface);
416 if (s->ds &&
417 surface_width(s->ds) == surface_width(surface) &&
418 surface_height(s->ds) == surface_height(surface)) {
419 resized = false;
421 s->ds = surface;
423 if (s->convert) {
424 pixman_image_unref(s->convert);
425 s->convert = NULL;
428 if (surface->format == PIXMAN_x8r8g8b8) {
430 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
432 * No need to convert, use surface directly. Should be the
433 * common case as this is qemu_default_pixelformat(32) too.
435 s->surface = cairo_image_surface_create_for_data
436 (surface_data(surface),
437 CAIRO_FORMAT_RGB24,
438 surface_width(surface),
439 surface_height(surface),
440 surface_stride(surface));
441 } else {
442 /* Must convert surface, use pixman to do it. */
443 s->convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
444 surface_width(surface),
445 surface_height(surface),
446 NULL, 0);
447 s->surface = cairo_image_surface_create_for_data
448 ((void *)pixman_image_get_data(s->convert),
449 CAIRO_FORMAT_RGB24,
450 pixman_image_get_width(s->convert),
451 pixman_image_get_height(s->convert),
452 pixman_image_get_stride(s->convert));
453 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
454 0, 0, 0, 0, 0, 0,
455 pixman_image_get_width(s->convert),
456 pixman_image_get_height(s->convert));
459 if (resized) {
460 gd_update_windowsize(s);
461 } else {
462 gd_update_full_redraw(s);
466 /** QEMU Events **/
468 static void gd_change_runstate(void *opaque, int running, RunState state)
470 GtkDisplayState *s = opaque;
472 gd_update_caption(s);
475 static void gd_mouse_mode_change(Notifier *notify, void *data)
477 GtkDisplayState *s;
479 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
480 /* release the grab at switching to absolute mode */
481 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
482 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
483 FALSE);
485 gd_update_cursor(s, FALSE);
488 /** GTK Events **/
490 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
491 void *opaque)
493 GtkDisplayState *s = opaque;
495 if (!no_quit) {
496 unregister_displaychangelistener(&s->dcl);
497 qmp_quit(NULL);
498 return FALSE;
501 return TRUE;
504 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
506 GtkDisplayState *s = opaque;
507 int mx, my;
508 int ww, wh;
509 int fbw, fbh;
511 if (!gtk_widget_get_realized(widget)) {
512 return FALSE;
515 fbw = surface_width(s->ds);
516 fbh = surface_height(s->ds);
518 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
520 if (s->full_screen) {
521 s->scale_x = (double)ww / fbw;
522 s->scale_y = (double)wh / fbh;
523 } else if (s->free_scale) {
524 double sx, sy;
526 sx = (double)ww / fbw;
527 sy = (double)wh / fbh;
529 s->scale_x = s->scale_y = MIN(sx, sy);
532 fbw *= s->scale_x;
533 fbh *= s->scale_y;
535 mx = my = 0;
536 if (ww > fbw) {
537 mx = (ww - fbw) / 2;
539 if (wh > fbh) {
540 my = (wh - fbh) / 2;
543 cairo_rectangle(cr, 0, 0, ww, wh);
545 /* Optionally cut out the inner area where the pixmap
546 will be drawn. This avoids 'flashing' since we're
547 not double-buffering. Note we're using the undocumented
548 behaviour of drawing the rectangle from right to left
549 to cut out the whole */
550 cairo_rectangle(cr, mx + fbw, my,
551 -1 * fbw, fbh);
552 cairo_fill(cr);
554 cairo_scale(cr, s->scale_x, s->scale_y);
555 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
556 cairo_paint(cr);
558 return TRUE;
561 #if !GTK_CHECK_VERSION(3, 0, 0)
562 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
563 void *opaque)
565 cairo_t *cr;
566 gboolean ret;
568 cr = gdk_cairo_create(gtk_widget_get_window(widget));
569 cairo_rectangle(cr,
570 expose->area.x,
571 expose->area.y,
572 expose->area.width,
573 expose->area.height);
574 cairo_clip(cr);
576 ret = gd_draw_event(widget, cr, opaque);
578 cairo_destroy(cr);
580 return ret;
582 #endif
584 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
585 void *opaque)
587 GtkDisplayState *s = opaque;
588 int x, y;
589 int mx, my;
590 int fbh, fbw;
591 int ww, wh;
593 fbw = surface_width(s->ds) * s->scale_x;
594 fbh = surface_height(s->ds) * s->scale_y;
596 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
598 mx = my = 0;
599 if (ww > fbw) {
600 mx = (ww - fbw) / 2;
602 if (wh > fbh) {
603 my = (wh - fbh) / 2;
606 x = (motion->x - mx) / s->scale_x;
607 y = (motion->y - my) / s->scale_y;
609 if (qemu_input_is_absolute()) {
610 if (x < 0 || y < 0 ||
611 x >= surface_width(s->ds) ||
612 y >= surface_height(s->ds)) {
613 return TRUE;
615 qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_X, x,
616 surface_width(s->ds));
617 qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_Y, y,
618 surface_height(s->ds));
619 qemu_input_event_sync();
620 } else if (s->last_set && gd_is_grab_active(s)) {
621 qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_X, x - s->last_x);
622 qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_Y, y - s->last_y);
623 qemu_input_event_sync();
625 s->last_x = x;
626 s->last_y = y;
627 s->last_set = TRUE;
629 if (!qemu_input_is_absolute() && gd_is_grab_active(s)) {
630 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
631 int x = (int)motion->x_root;
632 int y = (int)motion->y_root;
634 /* In relative mode check to see if client pointer hit
635 * one of the screen edges, and if so move it back by
636 * 200 pixels. This is important because the pointer
637 * in the server doesn't correspond 1-for-1, and so
638 * may still be only half way across the screen. Without
639 * this warp, the server pointer would thus appear to hit
640 * an invisible wall */
641 if (x == 0) {
642 x += 200;
644 if (y == 0) {
645 y += 200;
647 if (x == (gdk_screen_get_width(screen) - 1)) {
648 x -= 200;
650 if (y == (gdk_screen_get_height(screen) - 1)) {
651 y -= 200;
654 if (x != (int)motion->x_root || y != (int)motion->y_root) {
655 #if GTK_CHECK_VERSION(3, 0, 0)
656 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
657 gdk_device_warp(dev, screen, x, y);
658 #else
659 GdkDisplay *display = gtk_widget_get_display(widget);
660 gdk_display_warp_pointer(display, screen, x, y);
661 #endif
662 s->last_set = FALSE;
663 return FALSE;
666 return TRUE;
669 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
670 void *opaque)
672 GtkDisplayState *s = opaque;
673 InputButton btn;
675 /* implicitly grab the input at the first click in the relative mode */
676 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
677 !qemu_input_is_absolute() && !gd_is_grab_active(s)) {
678 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
679 TRUE);
680 return TRUE;
683 if (button->button == 1) {
684 btn = INPUT_BUTTON_LEFT;
685 } else if (button->button == 2) {
686 btn = INPUT_BUTTON_MIDDLE;
687 } else if (button->button == 3) {
688 btn = INPUT_BUTTON_RIGHT;
689 } else {
690 return TRUE;
693 qemu_input_queue_btn(s->dcl.con, btn, button->type == GDK_BUTTON_PRESS);
694 qemu_input_event_sync();
695 return TRUE;
698 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
699 void *opaque)
701 GtkDisplayState *s = opaque;
702 InputButton btn;
704 if (scroll->direction == GDK_SCROLL_UP) {
705 btn = INPUT_BUTTON_WHEEL_UP;
706 } else if (scroll->direction == GDK_SCROLL_DOWN) {
707 btn = INPUT_BUTTON_WHEEL_DOWN;
708 } else {
709 return TRUE;
712 qemu_input_queue_btn(s->dcl.con, btn, true);
713 qemu_input_event_sync();
714 qemu_input_queue_btn(s->dcl.con, btn, false);
715 qemu_input_event_sync();
716 return TRUE;
719 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
721 GtkDisplayState *s = opaque;
722 int gdk_keycode = key->hardware_keycode;
723 int i;
725 #ifdef _WIN32
726 UINT qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
727 switch (qemu_keycode) {
728 case 103: /* alt gr */
729 qemu_keycode = 56 | SCANCODE_GREY;
730 break;
732 #else
733 int qemu_keycode;
735 if (gdk_keycode < 9) {
736 qemu_keycode = 0;
737 } else if (gdk_keycode < 97) {
738 qemu_keycode = gdk_keycode - 8;
739 } else if (gdk_keycode < 158) {
740 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
741 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
742 qemu_keycode = 0x70;
743 } else if (gdk_keycode == 211) { /* backslash */
744 qemu_keycode = 0x73;
745 } else {
746 qemu_keycode = 0;
748 #endif
750 trace_gd_key_event(gdk_keycode, qemu_keycode,
751 (key->type == GDK_KEY_PRESS) ? "down" : "up");
753 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
754 if (qemu_keycode == modifier_keycode[i]) {
755 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
759 qemu_input_event_send_key_number(s->dcl.con, qemu_keycode,
760 key->type == GDK_KEY_PRESS);
762 return TRUE;
765 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
767 if (event->type == GDK_MOTION_NOTIFY) {
768 return gd_motion_event(widget, &event->motion, opaque);
770 return FALSE;
773 /** Window Menu Actions **/
775 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
777 GtkDisplayState *s = opaque;
779 if (s->external_pause_update) {
780 return;
782 if (runstate_is_running()) {
783 qmp_stop(NULL);
784 } else {
785 qmp_cont(NULL);
789 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
791 qmp_system_reset(NULL);
794 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
796 qmp_system_powerdown(NULL);
799 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
801 qmp_quit(NULL);
804 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
806 GtkDisplayState *s = opaque;
808 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
809 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
810 } else {
811 int i;
813 gtk_release_modifiers(s);
814 for (i = 0; i < s->nb_vcs; i++) {
815 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
816 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
817 break;
823 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
825 GtkDisplayState *s = opaque;
827 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
828 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
829 } else {
830 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
834 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
836 GtkDisplayState *s = opaque;
838 if (!s->full_screen) {
839 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
840 gtk_widget_set_size_request(s->menu_bar, 0, 0);
841 gtk_widget_set_size_request(s->drawing_area, -1, -1);
842 gtk_window_fullscreen(GTK_WINDOW(s->window));
843 if (gd_on_vga(s)) {
844 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
846 s->full_screen = TRUE;
847 } else {
848 gtk_window_unfullscreen(GTK_WINDOW(s->window));
849 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
850 gtk_widget_set_size_request(s->menu_bar, -1, -1);
851 gtk_widget_set_size_request(s->drawing_area,
852 surface_width(s->ds),
853 surface_height(s->ds));
854 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
855 s->full_screen = FALSE;
856 s->scale_x = 1.0;
857 s->scale_y = 1.0;
860 gd_update_cursor(s, FALSE);
863 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
865 GtkDisplayState *s = opaque;
867 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
868 FALSE);
870 s->scale_x += .25;
871 s->scale_y += .25;
873 gd_update_windowsize(s);
876 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
878 GtkDisplayState *s = opaque;
880 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
881 FALSE);
883 s->scale_x -= .25;
884 s->scale_y -= .25;
886 s->scale_x = MAX(s->scale_x, .25);
887 s->scale_y = MAX(s->scale_y, .25);
889 gd_update_windowsize(s);
892 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
894 GtkDisplayState *s = opaque;
896 s->scale_x = 1.0;
897 s->scale_y = 1.0;
899 gd_update_windowsize(s);
902 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
904 GtkDisplayState *s = opaque;
906 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
907 s->free_scale = TRUE;
908 } else {
909 s->free_scale = FALSE;
910 s->scale_x = 1.0;
911 s->scale_y = 1.0;
912 gd_update_windowsize(s);
915 gd_update_full_redraw(s);
918 static void gd_grab_keyboard(GtkDisplayState *s)
920 #if GTK_CHECK_VERSION(3, 0, 0)
921 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
922 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
923 GList *devices = gdk_device_manager_list_devices(mgr,
924 GDK_DEVICE_TYPE_MASTER);
925 GList *tmp = devices;
926 while (tmp) {
927 GdkDevice *dev = tmp->data;
928 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
929 gdk_device_grab(dev,
930 gtk_widget_get_window(s->drawing_area),
931 GDK_OWNERSHIP_NONE,
932 FALSE,
933 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
934 NULL,
935 GDK_CURRENT_TIME);
937 tmp = tmp->next;
939 g_list_free(devices);
940 #else
941 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
942 FALSE,
943 GDK_CURRENT_TIME);
944 #endif
947 static void gd_ungrab_keyboard(GtkDisplayState *s)
949 #if GTK_CHECK_VERSION(3, 0, 0)
950 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
951 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
952 GList *devices = gdk_device_manager_list_devices(mgr,
953 GDK_DEVICE_TYPE_MASTER);
954 GList *tmp = devices;
955 while (tmp) {
956 GdkDevice *dev = tmp->data;
957 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
958 gdk_device_ungrab(dev,
959 GDK_CURRENT_TIME);
961 tmp = tmp->next;
963 g_list_free(devices);
964 #else
965 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
966 #endif
969 static void gd_grab_pointer(GtkDisplayState *s)
971 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
972 #if GTK_CHECK_VERSION(3, 0, 0)
973 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
974 GList *devices = gdk_device_manager_list_devices(mgr,
975 GDK_DEVICE_TYPE_MASTER);
976 GList *tmp = devices;
977 while (tmp) {
978 GdkDevice *dev = tmp->data;
979 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
980 gdk_device_grab(dev,
981 gtk_widget_get_window(s->drawing_area),
982 GDK_OWNERSHIP_NONE,
983 FALSE, /* All events to come to our
984 window directly */
985 GDK_POINTER_MOTION_MASK |
986 GDK_BUTTON_PRESS_MASK |
987 GDK_BUTTON_RELEASE_MASK |
988 GDK_BUTTON_MOTION_MASK |
989 GDK_SCROLL_MASK,
990 s->null_cursor,
991 GDK_CURRENT_TIME);
993 tmp = tmp->next;
995 g_list_free(devices);
996 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
997 NULL, &s->grab_x_root, &s->grab_y_root);
998 #else
999 gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
1000 FALSE, /* All events to come to our window directly */
1001 GDK_POINTER_MOTION_MASK |
1002 GDK_BUTTON_PRESS_MASK |
1003 GDK_BUTTON_RELEASE_MASK |
1004 GDK_BUTTON_MOTION_MASK |
1005 GDK_SCROLL_MASK,
1006 NULL, /* Allow cursor to move over entire desktop */
1007 s->null_cursor,
1008 GDK_CURRENT_TIME);
1009 gdk_display_get_pointer(display, NULL,
1010 &s->grab_x_root, &s->grab_y_root, NULL);
1011 #endif
1014 static void gd_ungrab_pointer(GtkDisplayState *s)
1016 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
1017 #if GTK_CHECK_VERSION(3, 0, 0)
1018 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1019 GList *devices = gdk_device_manager_list_devices(mgr,
1020 GDK_DEVICE_TYPE_MASTER);
1021 GList *tmp = devices;
1022 while (tmp) {
1023 GdkDevice *dev = tmp->data;
1024 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
1025 gdk_device_ungrab(dev,
1026 GDK_CURRENT_TIME);
1028 tmp = tmp->next;
1030 g_list_free(devices);
1031 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1032 gtk_widget_get_screen(s->drawing_area),
1033 s->grab_x_root, s->grab_y_root);
1034 #else
1035 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1036 gdk_display_warp_pointer(display,
1037 gtk_widget_get_screen(s->drawing_area),
1038 s->grab_x_root, s->grab_y_root);
1039 #endif
1042 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1044 GtkDisplayState *s = opaque;
1046 if (gd_is_grab_active(s)) {
1047 gd_grab_keyboard(s);
1048 gd_grab_pointer(s);
1049 } else {
1050 gd_ungrab_keyboard(s);
1051 gd_ungrab_pointer(s);
1054 gd_update_caption(s);
1055 gd_update_cursor(s, FALSE);
1058 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1059 gpointer data)
1061 GtkDisplayState *s = data;
1062 guint last_page;
1063 gboolean on_vga;
1065 if (!gtk_widget_get_realized(s->notebook)) {
1066 return;
1069 last_page = gtk_notebook_get_current_page(nb);
1071 if (last_page) {
1072 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
1075 on_vga = arg2 == 0;
1077 if (!on_vga) {
1078 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1079 FALSE);
1080 } else if (s->full_screen) {
1081 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1082 TRUE);
1085 if (arg2 == 0) {
1086 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
1087 } else {
1088 #if defined(CONFIG_VTE)
1089 VirtualConsole *vc = &s->vc[arg2 - 1];
1090 VteTerminal *term = VTE_TERMINAL(vc->terminal);
1091 int width, height;
1093 width = 80 * vte_terminal_get_char_width(term);
1094 height = 25 * vte_terminal_get_char_height(term);
1096 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1097 gtk_widget_set_size_request(vc->terminal, width, height);
1098 #else
1099 g_assert_not_reached();
1100 #endif
1103 gtk_widget_set_sensitive(s->grab_item, on_vga);
1105 gd_update_cursor(s, TRUE);
1108 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1110 GtkDisplayState *s = data;
1112 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1113 gd_grab_keyboard(s);
1116 return TRUE;
1119 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1121 GtkDisplayState *s = data;
1123 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1124 gd_ungrab_keyboard(s);
1127 return TRUE;
1130 static gboolean gd_focus_out_event(GtkWidget *widget,
1131 GdkEventCrossing *crossing, gpointer data)
1133 GtkDisplayState *s = data;
1135 gtk_release_modifiers(s);
1137 return TRUE;
1140 /** Virtual Console Callbacks **/
1142 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1144 #if defined(CONFIG_VTE)
1145 VirtualConsole *vc = chr->opaque;
1147 vte_terminal_feed(VTE_TERMINAL(vc->terminal), (const char *)buf, len);
1148 #endif
1149 return len;
1152 static int nb_vcs;
1153 static CharDriverState *vcs[MAX_VCS];
1155 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1157 CharDriverState *chr;
1159 chr = g_malloc0(sizeof(*chr));
1160 chr->chr_write = gd_vc_chr_write;
1161 /* defer OPENED events until our vc is fully initialized */
1162 chr->explicit_be_open = true;
1164 vcs[nb_vcs++] = chr;
1166 return chr;
1169 void early_gtk_display_init(void)
1171 register_vc_handler(gd_vc_handler);
1174 #if defined(CONFIG_VTE)
1175 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1176 gpointer user_data)
1178 VirtualConsole *vc = user_data;
1180 qemu_chr_be_write(vc->chr, (uint8_t *)text, (unsigned int)size);
1181 return TRUE;
1183 #endif
1185 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group,
1186 GtkWidget *view_menu)
1188 #if defined(CONFIG_VTE)
1189 const char *label;
1190 char buffer[32];
1191 char path[32];
1192 GtkWidget *scrolled_window;
1193 GtkAdjustment *vadjustment;
1195 snprintf(buffer, sizeof(buffer), "vc%d", index);
1196 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1198 vc->chr = vcs[index];
1200 if (vc->chr->label) {
1201 label = vc->chr->label;
1202 } else {
1203 label = buffer;
1206 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1207 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1208 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1209 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, HOTKEY_MODIFIERS);
1211 vc->terminal = vte_terminal_new();
1212 g_signal_connect(vc->terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1214 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1216 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1217 vadjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vc->terminal));
1218 #else
1219 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1220 #endif
1222 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1223 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1225 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1227 vc->chr->opaque = vc;
1228 vc->scrolled_window = scrolled_window;
1230 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1231 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1233 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1234 g_signal_connect(vc->menu_item, "activate",
1235 G_CALLBACK(gd_menu_switch_vc), s);
1237 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1239 qemu_chr_be_generic_open(vc->chr);
1240 if (vc->chr->init) {
1241 vc->chr->init(vc->chr);
1244 #endif /* CONFIG_VTE */
1245 return group;
1248 /** Window Creation **/
1250 static void gd_connect_signals(GtkDisplayState *s)
1252 g_signal_connect(s->show_tabs_item, "activate",
1253 G_CALLBACK(gd_menu_show_tabs), s);
1255 g_signal_connect(s->window, "delete-event",
1256 G_CALLBACK(gd_window_close), s);
1258 #if GTK_CHECK_VERSION(3, 0, 0)
1259 g_signal_connect(s->drawing_area, "draw",
1260 G_CALLBACK(gd_draw_event), s);
1261 #else
1262 g_signal_connect(s->drawing_area, "expose-event",
1263 G_CALLBACK(gd_expose_event), s);
1264 #endif
1265 g_signal_connect(s->drawing_area, "event",
1266 G_CALLBACK(gd_event), s);
1267 g_signal_connect(s->drawing_area, "button-press-event",
1268 G_CALLBACK(gd_button_event), s);
1269 g_signal_connect(s->drawing_area, "button-release-event",
1270 G_CALLBACK(gd_button_event), s);
1271 g_signal_connect(s->drawing_area, "scroll-event",
1272 G_CALLBACK(gd_scroll_event), s);
1273 g_signal_connect(s->drawing_area, "key-press-event",
1274 G_CALLBACK(gd_key_event), s);
1275 g_signal_connect(s->drawing_area, "key-release-event",
1276 G_CALLBACK(gd_key_event), s);
1278 g_signal_connect(s->pause_item, "activate",
1279 G_CALLBACK(gd_menu_pause), s);
1280 g_signal_connect(s->reset_item, "activate",
1281 G_CALLBACK(gd_menu_reset), s);
1282 g_signal_connect(s->powerdown_item, "activate",
1283 G_CALLBACK(gd_menu_powerdown), s);
1284 g_signal_connect(s->quit_item, "activate",
1285 G_CALLBACK(gd_menu_quit), s);
1286 g_signal_connect(s->full_screen_item, "activate",
1287 G_CALLBACK(gd_menu_full_screen), s);
1288 g_signal_connect(s->zoom_in_item, "activate",
1289 G_CALLBACK(gd_menu_zoom_in), s);
1290 g_signal_connect(s->zoom_out_item, "activate",
1291 G_CALLBACK(gd_menu_zoom_out), s);
1292 g_signal_connect(s->zoom_fixed_item, "activate",
1293 G_CALLBACK(gd_menu_zoom_fixed), s);
1294 g_signal_connect(s->zoom_fit_item, "activate",
1295 G_CALLBACK(gd_menu_zoom_fit), s);
1296 g_signal_connect(s->vga_item, "activate",
1297 G_CALLBACK(gd_menu_switch_vc), s);
1298 g_signal_connect(s->grab_item, "activate",
1299 G_CALLBACK(gd_menu_grab_input), s);
1300 g_signal_connect(s->notebook, "switch-page",
1301 G_CALLBACK(gd_change_page), s);
1302 g_signal_connect(s->drawing_area, "enter-notify-event",
1303 G_CALLBACK(gd_enter_event), s);
1304 g_signal_connect(s->drawing_area, "leave-notify-event",
1305 G_CALLBACK(gd_leave_event), s);
1306 g_signal_connect(s->drawing_area, "focus-out-event",
1307 G_CALLBACK(gd_focus_out_event), s);
1310 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1312 GtkWidget *machine_menu;
1313 GtkWidget *separator;
1315 machine_menu = gtk_menu_new();
1316 gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1318 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1319 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1321 separator = gtk_separator_menu_item_new();
1322 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1324 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1325 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1327 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1328 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1330 separator = gtk_separator_menu_item_new();
1331 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1333 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1334 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1335 "<QEMU>/Machine/Quit");
1336 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1337 GDK_KEY_q, HOTKEY_MODIFIERS);
1338 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1340 return machine_menu;
1343 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1345 GSList *group = NULL;
1346 GtkWidget *view_menu;
1347 GtkWidget *separator;
1348 int i;
1350 view_menu = gtk_menu_new();
1351 gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1353 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1354 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1355 "<QEMU>/View/Full Screen");
1356 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
1357 HOTKEY_MODIFIERS);
1358 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1360 separator = gtk_separator_menu_item_new();
1361 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1363 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1364 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1365 "<QEMU>/View/Zoom In");
1366 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1367 HOTKEY_MODIFIERS);
1368 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1370 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1371 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1372 "<QEMU>/View/Zoom Out");
1373 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1374 HOTKEY_MODIFIERS);
1375 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1377 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1378 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1379 "<QEMU>/View/Zoom Fixed");
1380 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1381 HOTKEY_MODIFIERS);
1382 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1384 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1385 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1387 separator = gtk_separator_menu_item_new();
1388 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1390 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1391 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1393 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1394 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1395 "<QEMU>/View/Grab Input");
1396 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1397 HOTKEY_MODIFIERS);
1398 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1400 separator = gtk_separator_menu_item_new();
1401 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1403 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1404 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1405 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1406 "<QEMU>/View/VGA");
1407 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, HOTKEY_MODIFIERS);
1408 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item);
1410 for (i = 0; i < nb_vcs; i++) {
1411 VirtualConsole *vc = &s->vc[i];
1413 group = gd_vc_init(s, vc, i, group, view_menu);
1414 s->nb_vcs++;
1417 separator = gtk_separator_menu_item_new();
1418 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1420 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1421 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1423 return view_menu;
1426 static void gd_create_menus(GtkDisplayState *s)
1428 GtkAccelGroup *accel_group;
1430 accel_group = gtk_accel_group_new();
1431 s->machine_menu = gd_create_menu_machine(s, accel_group);
1432 s->view_menu = gd_create_menu_view(s, accel_group);
1434 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1435 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1436 s->machine_menu);
1437 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1439 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1440 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1441 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1443 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1444 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1445 s->accel_group = accel_group;
1448 static const DisplayChangeListenerOps dcl_ops = {
1449 .dpy_name = "gtk",
1450 .dpy_gfx_update = gd_update,
1451 .dpy_gfx_switch = gd_switch,
1452 .dpy_refresh = gd_refresh,
1453 .dpy_mouse_set = gd_mouse_set,
1454 .dpy_cursor_define = gd_cursor_define,
1457 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
1459 GtkDisplayState *s = g_malloc0(sizeof(*s));
1460 char *filename;
1462 gtk_init(NULL, NULL);
1464 s->dcl.ops = &dcl_ops;
1465 s->dcl.con = qemu_console_lookup_by_index(0);
1467 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1468 #if GTK_CHECK_VERSION(3, 2, 0)
1469 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1470 #else
1471 s->vbox = gtk_vbox_new(FALSE, 0);
1472 #endif
1473 s->notebook = gtk_notebook_new();
1474 s->drawing_area = gtk_drawing_area_new();
1475 s->menu_bar = gtk_menu_bar_new();
1477 s->scale_x = 1.0;
1478 s->scale_y = 1.0;
1479 s->free_scale = FALSE;
1481 setlocale(LC_ALL, "");
1482 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1483 textdomain("qemu");
1485 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1487 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1488 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1489 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1491 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1493 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1494 if (filename) {
1495 GError *error = NULL;
1496 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1497 if (pixbuf) {
1498 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1499 } else {
1500 g_error_free(error);
1502 g_free(filename);
1505 gd_create_menus(s);
1507 gd_connect_signals(s);
1509 gtk_widget_add_events(s->drawing_area,
1510 GDK_POINTER_MOTION_MASK |
1511 GDK_BUTTON_PRESS_MASK |
1512 GDK_BUTTON_RELEASE_MASK |
1513 GDK_BUTTON_MOTION_MASK |
1514 GDK_ENTER_NOTIFY_MASK |
1515 GDK_LEAVE_NOTIFY_MASK |
1516 GDK_SCROLL_MASK |
1517 GDK_KEY_PRESS_MASK);
1518 gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1519 gtk_widget_set_can_focus(s->drawing_area, TRUE);
1521 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1522 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1524 gd_update_caption(s);
1526 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1527 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1529 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1531 gtk_widget_show_all(s->window);
1533 if (full_screen) {
1534 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1536 if (grab_on_hover) {
1537 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1540 register_displaychangelistener(&s->dcl);
1542 global_state = s;