i386: add ACPI table files from seabios
[qemu.git] / ui / gtk.c
blobb5f4f0bd407d26439f12c9dd6f1b6a2b4658a13b
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 #include "qemu-common.h"
39 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
40 /* Work around an -Wstrict-prototypes warning in GTK headers */
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
43 #endif
44 #include <gtk/gtk.h>
45 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
46 #pragma GCC diagnostic pop
47 #endif
50 #include <gdk/gdkkeysyms.h>
51 #include <glib/gi18n.h>
52 #include <locale.h>
53 #include <vte/vte.h>
54 #include <math.h>
56 #include "ui/console.h"
57 #include "sysemu/sysemu.h"
58 #include "qmp-commands.h"
59 #include "x_keymap.h"
60 #include "keymaps.h"
61 #include "sysemu/char.h"
63 //#define DEBUG_GTK
65 #ifdef DEBUG_GTK
66 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
67 #else
68 #define DPRINTF(fmt, ...) do { } while (0)
69 #endif
71 #define MAX_VCS 10
74 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
75 #if GTK_CHECK_VERSION(3, 0, 0)
76 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
78 *ww = gdk_window_get_width(w);
79 *wh = gdk_window_get_height(w);
81 #endif
83 #if !GTK_CHECK_VERSION(2, 20, 0)
84 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
85 #endif
87 #ifndef GDK_KEY_0
88 #define GDK_KEY_0 GDK_0
89 #define GDK_KEY_1 GDK_1
90 #define GDK_KEY_2 GDK_2
91 #define GDK_KEY_f GDK_f
92 #define GDK_KEY_g GDK_g
93 #define GDK_KEY_plus GDK_plus
94 #define GDK_KEY_minus GDK_minus
95 #endif
97 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
98 #define IGNORE_MODIFIER_MASK \
99 (GDK_MODIFIER_MASK & ~(GDK_LOCK_MASK | GDK_MOD2_MASK))
101 static const int modifier_keycode[] = {
102 /* shift, control, alt keys, meta keys, both left & right */
103 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
106 typedef struct VirtualConsole
108 GtkWidget *menu_item;
109 GtkWidget *terminal;
110 GtkWidget *scrolled_window;
111 CharDriverState *chr;
112 int fd;
113 } VirtualConsole;
115 typedef struct GtkDisplayState
117 GtkWidget *window;
119 GtkWidget *menu_bar;
121 GtkAccelGroup *accel_group;
123 GtkWidget *machine_menu_item;
124 GtkWidget *machine_menu;
125 GtkWidget *pause_item;
126 GtkWidget *reset_item;
127 GtkWidget *powerdown_item;
128 GtkWidget *quit_item;
130 GtkWidget *view_menu_item;
131 GtkWidget *view_menu;
132 GtkWidget *full_screen_item;
133 GtkWidget *zoom_in_item;
134 GtkWidget *zoom_out_item;
135 GtkWidget *zoom_fixed_item;
136 GtkWidget *zoom_fit_item;
137 GtkWidget *grab_item;
138 GtkWidget *grab_on_hover_item;
139 GtkWidget *vga_item;
141 int nb_vcs;
142 VirtualConsole vc[MAX_VCS];
144 GtkWidget *show_tabs_item;
146 GtkWidget *vbox;
147 GtkWidget *notebook;
148 GtkWidget *drawing_area;
149 cairo_surface_t *surface;
150 pixman_image_t *convert;
151 DisplayChangeListener dcl;
152 DisplaySurface *ds;
153 int button_mask;
154 int last_x;
155 int last_y;
157 double scale_x;
158 double scale_y;
159 gboolean full_screen;
161 GdkCursor *null_cursor;
162 Notifier mouse_mode_notifier;
163 gboolean free_scale;
165 bool external_pause_update;
167 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
168 } GtkDisplayState;
170 static GtkDisplayState *global_state;
172 /** Utility Functions **/
174 static bool gd_is_grab_active(GtkDisplayState *s)
176 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
179 static bool gd_grab_on_hover(GtkDisplayState *s)
181 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
184 static bool gd_on_vga(GtkDisplayState *s)
186 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
189 static void gd_update_cursor(GtkDisplayState *s, gboolean override)
191 GdkWindow *window;
192 bool on_vga;
194 window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));
196 on_vga = gd_on_vga(s);
198 if ((override || on_vga) &&
199 (s->full_screen || kbd_mouse_is_absolute() || gd_is_grab_active(s))) {
200 gdk_window_set_cursor(window, s->null_cursor);
201 } else {
202 gdk_window_set_cursor(window, NULL);
206 static void gd_update_caption(GtkDisplayState *s)
208 const char *status = "";
209 gchar *title;
210 const char *grab = "";
211 bool is_paused = !runstate_is_running();
213 if (gd_is_grab_active(s)) {
214 grab = _(" - Press Ctrl+Alt+G to release grab");
217 if (is_paused) {
218 status = _(" [Paused]");
220 s->external_pause_update = true;
221 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
222 is_paused);
223 s->external_pause_update = false;
225 if (qemu_name) {
226 title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
227 } else {
228 title = g_strdup_printf("QEMU%s%s", status, grab);
231 gtk_window_set_title(GTK_WINDOW(s->window), title);
233 g_free(title);
236 static void gd_update_windowsize(GtkDisplayState *s)
238 if (!s->full_screen) {
239 GtkRequisition req;
240 double sx, sy;
242 if (s->free_scale) {
243 sx = s->scale_x;
244 sy = s->scale_y;
246 s->scale_y = 1.0;
247 s->scale_x = 1.0;
248 } else {
249 sx = 1.0;
250 sy = 1.0;
253 gtk_widget_set_size_request(s->drawing_area,
254 surface_width(s->ds) * s->scale_x,
255 surface_height(s->ds) * s->scale_y);
256 #if GTK_CHECK_VERSION(3, 0, 0)
257 gtk_widget_get_preferred_size(s->vbox, NULL, &req);
258 #else
259 gtk_widget_size_request(s->vbox, &req);
260 #endif
262 gtk_window_resize(GTK_WINDOW(s->window),
263 req.width * sx, req.height * sy);
267 static void gd_update_full_redraw(GtkDisplayState *s)
269 int ww, wh;
270 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
271 gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
274 static void gtk_release_modifiers(GtkDisplayState *s)
276 int i, keycode;
278 if (!gd_on_vga(s)) {
279 return;
281 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
282 keycode = modifier_keycode[i];
283 if (!s->modifier_pressed[i]) {
284 continue;
286 if (keycode & SCANCODE_GREY) {
287 kbd_put_keycode(SCANCODE_EMUL0);
289 kbd_put_keycode(keycode | SCANCODE_UP);
290 s->modifier_pressed[i] = false;
294 /** DisplayState Callbacks **/
296 static void gd_update(DisplayChangeListener *dcl,
297 int x, int y, int w, int h)
299 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
300 int x1, x2, y1, y2;
301 int mx, my;
302 int fbw, fbh;
303 int ww, wh;
305 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x, y, w, h);
307 if (s->convert) {
308 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
309 x, y, 0, 0, x, y, w, h);
312 x1 = floor(x * s->scale_x);
313 y1 = floor(y * s->scale_y);
315 x2 = ceil(x * s->scale_x + w * s->scale_x);
316 y2 = ceil(y * s->scale_y + h * s->scale_y);
318 fbw = surface_width(s->ds) * s->scale_x;
319 fbh = surface_height(s->ds) * s->scale_y;
321 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
323 mx = my = 0;
324 if (ww > fbw) {
325 mx = (ww - fbw) / 2;
327 if (wh > fbh) {
328 my = (wh - fbh) / 2;
331 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
334 static void gd_refresh(DisplayChangeListener *dcl)
336 graphic_hw_update(dcl->con);
339 #if GTK_CHECK_VERSION(3, 0, 0)
340 static void gd_mouse_set(DisplayChangeListener *dcl,
341 int x, int y, int visible)
343 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
344 GdkDisplay *dpy;
345 GdkDeviceManager *mgr;
346 gint x_root, y_root;
348 dpy = gtk_widget_get_display(s->drawing_area);
349 mgr = gdk_display_get_device_manager(dpy);
350 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
351 x, y, &x_root, &y_root);
352 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
353 gtk_widget_get_screen(s->drawing_area),
354 x, y);
356 #else
357 static void gd_mouse_set(DisplayChangeListener *dcl,
358 int x, int y, int visible)
360 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
361 gint x_root, y_root;
363 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
364 x, y, &x_root, &y_root);
365 gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area),
366 gtk_widget_get_screen(s->drawing_area),
367 x_root, y_root);
369 #endif
371 static void gd_cursor_define(DisplayChangeListener *dcl,
372 QEMUCursor *c)
374 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
375 GdkPixbuf *pixbuf;
376 GdkCursor *cursor;
378 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
379 GDK_COLORSPACE_RGB, true, 8,
380 c->width, c->height, c->width * 4,
381 NULL, NULL);
382 cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area),
383 pixbuf, c->hot_x, c->hot_y);
384 gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor);
385 g_object_unref(pixbuf);
386 #if !GTK_CHECK_VERSION(3, 0, 0)
387 gdk_cursor_unref(cursor);
388 #else
389 g_object_unref(cursor);
390 #endif
393 static void gd_switch(DisplayChangeListener *dcl,
394 DisplaySurface *surface)
396 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
397 bool resized = true;
399 DPRINTF("resize(width=%d, height=%d)\n",
400 surface_width(surface), surface_height(surface));
402 if (s->surface) {
403 cairo_surface_destroy(s->surface);
406 if (s->ds &&
407 surface_width(s->ds) == surface_width(surface) &&
408 surface_height(s->ds) == surface_height(surface)) {
409 resized = false;
411 s->ds = surface;
413 if (s->convert) {
414 pixman_image_unref(s->convert);
415 s->convert = NULL;
418 if (surface->format == PIXMAN_x8r8g8b8) {
420 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
422 * No need to convert, use surface directly. Should be the
423 * common case as this is qemu_default_pixelformat(32) too.
425 s->surface = cairo_image_surface_create_for_data
426 (surface_data(surface),
427 CAIRO_FORMAT_RGB24,
428 surface_width(surface),
429 surface_height(surface),
430 surface_stride(surface));
431 } else {
432 /* Must convert surface, use pixman to do it. */
433 s->convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
434 surface_width(surface),
435 surface_height(surface),
436 NULL, 0);
437 s->surface = cairo_image_surface_create_for_data
438 ((void *)pixman_image_get_data(s->convert),
439 CAIRO_FORMAT_RGB24,
440 pixman_image_get_width(s->convert),
441 pixman_image_get_height(s->convert),
442 pixman_image_get_stride(s->convert));
443 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
444 0, 0, 0, 0, 0, 0,
445 pixman_image_get_width(s->convert),
446 pixman_image_get_height(s->convert));
449 if (resized) {
450 gd_update_windowsize(s);
451 } else {
452 gd_update_full_redraw(s);
456 /** QEMU Events **/
458 static void gd_change_runstate(void *opaque, int running, RunState state)
460 GtkDisplayState *s = opaque;
462 gd_update_caption(s);
465 static void gd_mouse_mode_change(Notifier *notify, void *data)
467 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
468 FALSE);
471 /** GTK Events **/
473 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
475 GtkDisplayState *s = opaque;
476 gboolean handled = FALSE;
478 if (!gd_is_grab_active(s) ||
479 (key->state & IGNORE_MODIFIER_MASK) == HOTKEY_MODIFIERS) {
480 handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
482 if (handled) {
483 gtk_release_modifiers(s);
484 } else {
485 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
488 return handled;
491 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
492 void *opaque)
494 GtkDisplayState *s = opaque;
496 if (!no_quit) {
497 unregister_displaychangelistener(&s->dcl);
498 qmp_quit(NULL);
499 return FALSE;
502 return TRUE;
505 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
507 GtkDisplayState *s = opaque;
508 int mx, my;
509 int ww, wh;
510 int fbw, fbh;
512 if (!gtk_widget_get_realized(widget)) {
513 return FALSE;
516 fbw = surface_width(s->ds);
517 fbh = surface_height(s->ds);
519 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
521 if (s->full_screen) {
522 s->scale_x = (double)ww / fbw;
523 s->scale_y = (double)wh / fbh;
524 } else if (s->free_scale) {
525 double sx, sy;
527 sx = (double)ww / fbw;
528 sy = (double)wh / fbh;
530 s->scale_x = s->scale_y = MIN(sx, sy);
533 fbw *= s->scale_x;
534 fbh *= s->scale_y;
536 mx = my = 0;
537 if (ww > fbw) {
538 mx = (ww - fbw) / 2;
540 if (wh > fbh) {
541 my = (wh - fbh) / 2;
544 cairo_rectangle(cr, 0, 0, ww, wh);
546 /* Optionally cut out the inner area where the pixmap
547 will be drawn. This avoids 'flashing' since we're
548 not double-buffering. Note we're using the undocumented
549 behaviour of drawing the rectangle from right to left
550 to cut out the whole */
551 cairo_rectangle(cr, mx + fbw, my,
552 -1 * fbw, fbh);
553 cairo_fill(cr);
555 cairo_scale(cr, s->scale_x, s->scale_y);
556 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
557 cairo_paint(cr);
559 return TRUE;
562 #if !GTK_CHECK_VERSION(3, 0, 0)
563 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
564 void *opaque)
566 cairo_t *cr;
567 gboolean ret;
569 cr = gdk_cairo_create(gtk_widget_get_window(widget));
570 cairo_rectangle(cr,
571 expose->area.x,
572 expose->area.y,
573 expose->area.width,
574 expose->area.height);
575 cairo_clip(cr);
577 ret = gd_draw_event(widget, cr, opaque);
579 cairo_destroy(cr);
581 return ret;
583 #endif
585 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
586 void *opaque)
588 GtkDisplayState *s = opaque;
589 int dx, dy;
590 int x, y;
591 int mx, my;
592 int fbh, fbw;
593 int ww, wh;
595 fbw = surface_width(s->ds) * s->scale_x;
596 fbh = surface_height(s->ds) * s->scale_y;
598 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
600 mx = my = 0;
601 if (ww > fbw) {
602 mx = (ww - fbw) / 2;
604 if (wh > fbh) {
605 my = (wh - fbh) / 2;
608 x = (motion->x - mx) / s->scale_x;
609 y = (motion->y - my) / s->scale_y;
611 if (x < 0 || y < 0 ||
612 x >= surface_width(s->ds) ||
613 y >= surface_height(s->ds)) {
614 return TRUE;
617 if (kbd_mouse_is_absolute()) {
618 dx = x * 0x7FFF / (surface_width(s->ds) - 1);
619 dy = y * 0x7FFF / (surface_height(s->ds) - 1);
620 } else if (s->last_x == -1 || s->last_y == -1) {
621 dx = 0;
622 dy = 0;
623 } else {
624 dx = x - s->last_x;
625 dy = y - s->last_y;
628 s->last_x = x;
629 s->last_y = y;
631 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
632 kbd_mouse_event(dx, dy, 0, s->button_mask);
635 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) {
636 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
637 int x = (int)motion->x_root;
638 int y = (int)motion->y_root;
640 /* In relative mode check to see if client pointer hit
641 * one of the screen edges, and if so move it back by
642 * 200 pixels. This is important because the pointer
643 * in the server doesn't correspond 1-for-1, and so
644 * may still be only half way across the screen. Without
645 * this warp, the server pointer would thus appear to hit
646 * an invisible wall */
647 if (x == 0) {
648 x += 200;
650 if (y == 0) {
651 y += 200;
653 if (x == (gdk_screen_get_width(screen) - 1)) {
654 x -= 200;
656 if (y == (gdk_screen_get_height(screen) - 1)) {
657 y -= 200;
660 if (x != (int)motion->x_root || y != (int)motion->y_root) {
661 #if GTK_CHECK_VERSION(3, 0, 0)
662 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
663 gdk_device_warp(dev, screen, x, y);
664 #else
665 GdkDisplay *display = gtk_widget_get_display(widget);
666 gdk_display_warp_pointer(display, screen, x, y);
667 #endif
668 s->last_x = -1;
669 s->last_y = -1;
670 return FALSE;
673 return TRUE;
676 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
677 void *opaque)
679 GtkDisplayState *s = opaque;
680 int dx, dy;
681 int n;
683 if (button->button == 1) {
684 n = 0x01;
685 } else if (button->button == 2) {
686 n = 0x04;
687 } else if (button->button == 3) {
688 n = 0x02;
689 } else {
690 n = 0x00;
693 if (button->type == GDK_BUTTON_PRESS) {
694 s->button_mask |= n;
695 } else if (button->type == GDK_BUTTON_RELEASE) {
696 s->button_mask &= ~n;
699 if (kbd_mouse_is_absolute()) {
700 dx = s->last_x * 0x7FFF / (surface_width(s->ds) - 1);
701 dy = s->last_y * 0x7FFF / (surface_height(s->ds) - 1);
702 } else {
703 dx = 0;
704 dy = 0;
707 kbd_mouse_event(dx, dy, 0, s->button_mask);
709 return TRUE;
712 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
714 GtkDisplayState *s = opaque;
715 int gdk_keycode;
716 int qemu_keycode;
717 int i;
719 gdk_keycode = key->hardware_keycode;
721 if (gdk_keycode < 9) {
722 qemu_keycode = 0;
723 } else if (gdk_keycode < 97) {
724 qemu_keycode = gdk_keycode - 8;
725 } else if (gdk_keycode < 158) {
726 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
727 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
728 qemu_keycode = 0x70;
729 } else if (gdk_keycode == 211) { /* backslash */
730 qemu_keycode = 0x73;
731 } else {
732 qemu_keycode = 0;
735 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
736 gdk_keycode, qemu_keycode,
737 (key->type == GDK_KEY_PRESS) ? "down" : "up");
739 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
740 if (qemu_keycode == modifier_keycode[i]) {
741 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
745 if (qemu_keycode & SCANCODE_GREY) {
746 kbd_put_keycode(SCANCODE_EMUL0);
749 if (key->type == GDK_KEY_PRESS) {
750 kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK);
751 } else if (key->type == GDK_KEY_RELEASE) {
752 kbd_put_keycode(qemu_keycode | SCANCODE_UP);
753 } else {
754 g_assert_not_reached();
757 return TRUE;
760 /** Window Menu Actions **/
762 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
764 GtkDisplayState *s = opaque;
766 if (s->external_pause_update) {
767 return;
769 if (runstate_is_running()) {
770 qmp_stop(NULL);
771 } else {
772 qmp_cont(NULL);
776 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
778 qmp_system_reset(NULL);
781 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
783 qmp_system_powerdown(NULL);
786 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
788 qmp_quit(NULL);
791 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
793 GtkDisplayState *s = opaque;
795 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
796 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
797 } else {
798 int i;
800 gtk_release_modifiers(s);
801 for (i = 0; i < s->nb_vcs; i++) {
802 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
803 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
804 break;
810 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
812 GtkDisplayState *s = opaque;
814 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
815 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
816 } else {
817 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
821 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
823 GtkDisplayState *s = opaque;
825 if (!s->full_screen) {
826 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
827 gtk_widget_set_size_request(s->menu_bar, 0, 0);
828 gtk_widget_set_size_request(s->drawing_area, -1, -1);
829 gtk_window_fullscreen(GTK_WINDOW(s->window));
830 if (gd_on_vga(s)) {
831 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
833 s->full_screen = TRUE;
834 } else {
835 gtk_window_unfullscreen(GTK_WINDOW(s->window));
836 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
837 gtk_widget_set_size_request(s->menu_bar, -1, -1);
838 gtk_widget_set_size_request(s->drawing_area,
839 surface_width(s->ds),
840 surface_height(s->ds));
841 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
842 s->full_screen = FALSE;
843 s->scale_x = 1.0;
844 s->scale_y = 1.0;
847 gd_update_cursor(s, FALSE);
850 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
852 GtkDisplayState *s = opaque;
854 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
855 FALSE);
857 s->scale_x += .25;
858 s->scale_y += .25;
860 gd_update_windowsize(s);
863 static void gd_menu_zoom_out(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 s->scale_x = MAX(s->scale_x, .25);
874 s->scale_y = MAX(s->scale_y, .25);
876 gd_update_windowsize(s);
879 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
881 GtkDisplayState *s = opaque;
883 s->scale_x = 1.0;
884 s->scale_y = 1.0;
886 gd_update_windowsize(s);
889 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
891 GtkDisplayState *s = opaque;
893 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
894 s->free_scale = TRUE;
895 } else {
896 s->free_scale = FALSE;
897 s->scale_x = 1.0;
898 s->scale_y = 1.0;
899 gd_update_windowsize(s);
902 gd_update_full_redraw(s);
905 static void gd_grab_keyboard(GtkDisplayState *s)
907 #if GTK_CHECK_VERSION(3, 0, 0)
908 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
909 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
910 GList *devices = gdk_device_manager_list_devices(mgr,
911 GDK_DEVICE_TYPE_MASTER);
912 GList *tmp = devices;
913 while (tmp) {
914 GdkDevice *dev = tmp->data;
915 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
916 gdk_device_grab(dev,
917 gtk_widget_get_window(s->drawing_area),
918 GDK_OWNERSHIP_NONE,
919 FALSE,
920 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
921 NULL,
922 GDK_CURRENT_TIME);
924 tmp = tmp->next;
926 g_list_free(devices);
927 #else
928 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
929 FALSE,
930 GDK_CURRENT_TIME);
931 #endif
934 static void gd_ungrab_keyboard(GtkDisplayState *s)
936 #if GTK_CHECK_VERSION(3, 0, 0)
937 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
938 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
939 GList *devices = gdk_device_manager_list_devices(mgr,
940 GDK_DEVICE_TYPE_MASTER);
941 GList *tmp = devices;
942 while (tmp) {
943 GdkDevice *dev = tmp->data;
944 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
945 gdk_device_ungrab(dev,
946 GDK_CURRENT_TIME);
948 tmp = tmp->next;
950 g_list_free(devices);
951 #else
952 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
953 #endif
956 static void gd_grab_pointer(GtkDisplayState *s)
958 #if GTK_CHECK_VERSION(3, 0, 0)
959 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
960 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
961 GList *devices = gdk_device_manager_list_devices(mgr,
962 GDK_DEVICE_TYPE_MASTER);
963 GList *tmp = devices;
964 while (tmp) {
965 GdkDevice *dev = tmp->data;
966 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
967 gdk_device_grab(dev,
968 gtk_widget_get_window(s->drawing_area),
969 GDK_OWNERSHIP_NONE,
970 FALSE, /* All events to come to our
971 window directly */
972 GDK_POINTER_MOTION_MASK |
973 GDK_BUTTON_PRESS_MASK |
974 GDK_BUTTON_RELEASE_MASK |
975 GDK_BUTTON_MOTION_MASK |
976 GDK_SCROLL_MASK,
977 s->null_cursor,
978 GDK_CURRENT_TIME);
980 tmp = tmp->next;
982 g_list_free(devices);
983 #else
984 gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
985 FALSE, /* All events to come to our window directly */
986 GDK_POINTER_MOTION_MASK |
987 GDK_BUTTON_PRESS_MASK |
988 GDK_BUTTON_RELEASE_MASK |
989 GDK_BUTTON_MOTION_MASK |
990 GDK_SCROLL_MASK,
991 NULL, /* Allow cursor to move over entire desktop */
992 s->null_cursor,
993 GDK_CURRENT_TIME);
994 #endif
997 static void gd_ungrab_pointer(GtkDisplayState *s)
999 #if GTK_CHECK_VERSION(3, 0, 0)
1000 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
1001 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1002 GList *devices = gdk_device_manager_list_devices(mgr,
1003 GDK_DEVICE_TYPE_MASTER);
1004 GList *tmp = devices;
1005 while (tmp) {
1006 GdkDevice *dev = tmp->data;
1007 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
1008 gdk_device_ungrab(dev,
1009 GDK_CURRENT_TIME);
1011 tmp = tmp->next;
1013 g_list_free(devices);
1014 #else
1015 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1016 #endif
1019 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1021 GtkDisplayState *s = opaque;
1023 if (gd_is_grab_active(s)) {
1024 gd_grab_keyboard(s);
1025 gd_grab_pointer(s);
1026 } else {
1027 gd_ungrab_keyboard(s);
1028 gd_ungrab_pointer(s);
1031 gd_update_caption(s);
1032 gd_update_cursor(s, FALSE);
1035 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1036 gpointer data)
1038 GtkDisplayState *s = data;
1039 guint last_page;
1040 gboolean on_vga;
1042 if (!gtk_widget_get_realized(s->notebook)) {
1043 return;
1046 last_page = gtk_notebook_get_current_page(nb);
1048 if (last_page) {
1049 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
1052 on_vga = arg2 == 0;
1054 if (!on_vga) {
1055 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1056 FALSE);
1057 } else if (s->full_screen) {
1058 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1059 TRUE);
1062 if (arg2 == 0) {
1063 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
1064 } else {
1065 VirtualConsole *vc = &s->vc[arg2 - 1];
1066 VteTerminal *term = VTE_TERMINAL(vc->terminal);
1067 int width, height;
1069 width = 80 * vte_terminal_get_char_width(term);
1070 height = 25 * vte_terminal_get_char_height(term);
1072 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1073 gtk_widget_set_size_request(vc->terminal, width, height);
1076 gtk_widget_set_sensitive(s->grab_item, on_vga);
1078 gd_update_cursor(s, TRUE);
1081 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1083 GtkDisplayState *s = data;
1085 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1086 gd_grab_keyboard(s);
1089 return TRUE;
1092 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1094 GtkDisplayState *s = data;
1096 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1097 gd_ungrab_keyboard(s);
1100 return TRUE;
1103 static gboolean gd_focus_out_event(GtkWidget *widget,
1104 GdkEventCrossing *crossing, gpointer data)
1106 GtkDisplayState *s = data;
1108 gtk_release_modifiers(s);
1110 return TRUE;
1113 /** Virtual Console Callbacks **/
1115 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1117 VirtualConsole *vc = chr->opaque;
1119 return write(vc->fd, buf, len);
1122 static int nb_vcs;
1123 static CharDriverState *vcs[MAX_VCS];
1125 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1127 CharDriverState *chr;
1129 chr = g_malloc0(sizeof(*chr));
1130 chr->chr_write = gd_vc_chr_write;
1131 /* defer OPENED events until our vc is fully initialized */
1132 chr->explicit_be_open = true;
1134 vcs[nb_vcs++] = chr;
1136 return chr;
1139 void early_gtk_display_init(void)
1141 register_vc_handler(gd_vc_handler);
1144 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
1146 VirtualConsole *vc = opaque;
1147 uint8_t buffer[1024];
1148 ssize_t len;
1150 len = read(vc->fd, buffer, sizeof(buffer));
1151 if (len <= 0) {
1152 return FALSE;
1155 qemu_chr_be_write(vc->chr, buffer, len);
1157 return TRUE;
1160 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group,
1161 GtkWidget *view_menu)
1163 const char *label;
1164 char buffer[32];
1165 char path[32];
1166 #if VTE_CHECK_VERSION(0, 26, 0)
1167 VtePty *pty;
1168 #endif
1169 GIOChannel *chan;
1170 GtkWidget *scrolled_window;
1171 GtkAdjustment *vadjustment;
1172 int master_fd, slave_fd;
1174 snprintf(buffer, sizeof(buffer), "vc%d", index);
1175 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1177 vc->chr = vcs[index];
1179 if (vc->chr->label) {
1180 label = vc->chr->label;
1181 } else {
1182 label = buffer;
1185 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1186 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1187 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1188 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, HOTKEY_MODIFIERS);
1190 vc->terminal = vte_terminal_new();
1192 master_fd = qemu_openpty_raw(&slave_fd, NULL);
1193 g_assert(master_fd != -1);
1195 #if VTE_CHECK_VERSION(0, 26, 0)
1196 pty = vte_pty_new_foreign(master_fd, NULL);
1197 vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1198 #else
1199 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
1200 #endif
1202 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1204 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1206 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1207 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1209 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1211 vc->fd = slave_fd;
1212 vc->chr->opaque = vc;
1213 vc->scrolled_window = scrolled_window;
1215 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1216 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1218 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1219 g_signal_connect(vc->menu_item, "activate",
1220 G_CALLBACK(gd_menu_switch_vc), s);
1222 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1224 qemu_chr_be_generic_open(vc->chr);
1225 if (vc->chr->init) {
1226 vc->chr->init(vc->chr);
1229 chan = g_io_channel_unix_new(vc->fd);
1230 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1232 return group;
1235 /** Window Creation **/
1237 static void gd_connect_signals(GtkDisplayState *s)
1239 g_signal_connect(s->show_tabs_item, "activate",
1240 G_CALLBACK(gd_menu_show_tabs), s);
1242 g_signal_connect(s->window, "key-press-event",
1243 G_CALLBACK(gd_window_key_event), s);
1244 g_signal_connect(s->window, "delete-event",
1245 G_CALLBACK(gd_window_close), s);
1247 #if GTK_CHECK_VERSION(3, 0, 0)
1248 g_signal_connect(s->drawing_area, "draw",
1249 G_CALLBACK(gd_draw_event), s);
1250 #else
1251 g_signal_connect(s->drawing_area, "expose-event",
1252 G_CALLBACK(gd_expose_event), s);
1253 #endif
1254 g_signal_connect(s->drawing_area, "motion-notify-event",
1255 G_CALLBACK(gd_motion_event), s);
1256 g_signal_connect(s->drawing_area, "button-press-event",
1257 G_CALLBACK(gd_button_event), s);
1258 g_signal_connect(s->drawing_area, "button-release-event",
1259 G_CALLBACK(gd_button_event), s);
1260 g_signal_connect(s->drawing_area, "key-press-event",
1261 G_CALLBACK(gd_key_event), s);
1262 g_signal_connect(s->drawing_area, "key-release-event",
1263 G_CALLBACK(gd_key_event), s);
1265 g_signal_connect(s->pause_item, "activate",
1266 G_CALLBACK(gd_menu_pause), s);
1267 g_signal_connect(s->reset_item, "activate",
1268 G_CALLBACK(gd_menu_reset), s);
1269 g_signal_connect(s->powerdown_item, "activate",
1270 G_CALLBACK(gd_menu_powerdown), s);
1271 g_signal_connect(s->quit_item, "activate",
1272 G_CALLBACK(gd_menu_quit), s);
1273 g_signal_connect(s->full_screen_item, "activate",
1274 G_CALLBACK(gd_menu_full_screen), s);
1275 g_signal_connect(s->zoom_in_item, "activate",
1276 G_CALLBACK(gd_menu_zoom_in), s);
1277 g_signal_connect(s->zoom_out_item, "activate",
1278 G_CALLBACK(gd_menu_zoom_out), s);
1279 g_signal_connect(s->zoom_fixed_item, "activate",
1280 G_CALLBACK(gd_menu_zoom_fixed), s);
1281 g_signal_connect(s->zoom_fit_item, "activate",
1282 G_CALLBACK(gd_menu_zoom_fit), s);
1283 g_signal_connect(s->vga_item, "activate",
1284 G_CALLBACK(gd_menu_switch_vc), s);
1285 g_signal_connect(s->grab_item, "activate",
1286 G_CALLBACK(gd_menu_grab_input), s);
1287 g_signal_connect(s->notebook, "switch-page",
1288 G_CALLBACK(gd_change_page), s);
1289 g_signal_connect(s->drawing_area, "enter-notify-event",
1290 G_CALLBACK(gd_enter_event), s);
1291 g_signal_connect(s->drawing_area, "leave-notify-event",
1292 G_CALLBACK(gd_leave_event), s);
1293 g_signal_connect(s->drawing_area, "focus-out-event",
1294 G_CALLBACK(gd_focus_out_event), s);
1297 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1299 GtkWidget *machine_menu;
1300 GtkWidget *separator;
1301 GtkStockItem item;
1303 machine_menu = gtk_menu_new();
1304 gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1306 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1307 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1309 separator = gtk_separator_menu_item_new();
1310 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1312 s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1313 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1315 s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1316 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1318 separator = gtk_separator_menu_item_new();
1319 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1321 s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1322 gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1323 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1324 "<QEMU>/Machine/Quit");
1325 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1326 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1328 return machine_menu;
1331 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1333 GSList *group = NULL;
1334 GtkWidget *view_menu;
1335 GtkWidget *separator;
1336 int i;
1338 view_menu = gtk_menu_new();
1339 gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1341 s->full_screen_item =
1342 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1343 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1344 "<QEMU>/View/Full Screen");
1345 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
1346 HOTKEY_MODIFIERS);
1347 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1349 separator = gtk_separator_menu_item_new();
1350 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1352 s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1353 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1354 "<QEMU>/View/Zoom In");
1355 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1356 HOTKEY_MODIFIERS);
1357 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1359 s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1360 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1361 "<QEMU>/View/Zoom Out");
1362 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1363 HOTKEY_MODIFIERS);
1364 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1366 s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1367 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1368 "<QEMU>/View/Zoom Fixed");
1369 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1370 HOTKEY_MODIFIERS);
1371 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1373 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1374 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1376 separator = gtk_separator_menu_item_new();
1377 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1379 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1380 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1382 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1383 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1384 "<QEMU>/View/Grab Input");
1385 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1386 HOTKEY_MODIFIERS);
1387 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1389 separator = gtk_separator_menu_item_new();
1390 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1392 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1393 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1394 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1395 "<QEMU>/View/VGA");
1396 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, HOTKEY_MODIFIERS);
1397 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item);
1399 for (i = 0; i < nb_vcs; i++) {
1400 VirtualConsole *vc = &s->vc[i];
1402 group = gd_vc_init(s, vc, i, group, view_menu);
1403 s->nb_vcs++;
1406 separator = gtk_separator_menu_item_new();
1407 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1409 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1410 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1412 return view_menu;
1415 static void gd_create_menus(GtkDisplayState *s)
1417 GtkAccelGroup *accel_group;
1419 accel_group = gtk_accel_group_new();
1420 s->machine_menu = gd_create_menu_machine(s, accel_group);
1421 s->view_menu = gd_create_menu_view(s, accel_group);
1423 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1424 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1425 s->machine_menu);
1426 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1428 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1429 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1430 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1432 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1433 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1434 s->accel_group = accel_group;
1437 static const DisplayChangeListenerOps dcl_ops = {
1438 .dpy_name = "gtk",
1439 .dpy_gfx_update = gd_update,
1440 .dpy_gfx_switch = gd_switch,
1441 .dpy_refresh = gd_refresh,
1442 .dpy_mouse_set = gd_mouse_set,
1443 .dpy_cursor_define = gd_cursor_define,
1446 void gtk_display_init(DisplayState *ds, bool full_screen)
1448 GtkDisplayState *s = g_malloc0(sizeof(*s));
1449 char *filename;
1451 gtk_init(NULL, NULL);
1453 s->dcl.ops = &dcl_ops;
1454 s->dcl.con = qemu_console_lookup_by_index(0);
1456 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1457 #if GTK_CHECK_VERSION(3, 2, 0)
1458 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1459 #else
1460 s->vbox = gtk_vbox_new(FALSE, 0);
1461 #endif
1462 s->notebook = gtk_notebook_new();
1463 s->drawing_area = gtk_drawing_area_new();
1464 s->menu_bar = gtk_menu_bar_new();
1466 s->scale_x = 1.0;
1467 s->scale_y = 1.0;
1468 s->free_scale = FALSE;
1470 setlocale(LC_ALL, "");
1471 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1472 textdomain("qemu");
1474 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1476 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1477 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1478 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1480 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1482 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1483 if (filename) {
1484 GError *error = NULL;
1485 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1486 if (pixbuf) {
1487 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1488 } else {
1489 g_error_free(error);
1491 g_free(filename);
1494 gd_create_menus(s);
1496 gd_connect_signals(s);
1498 gtk_widget_add_events(s->drawing_area,
1499 GDK_POINTER_MOTION_MASK |
1500 GDK_BUTTON_PRESS_MASK |
1501 GDK_BUTTON_RELEASE_MASK |
1502 GDK_BUTTON_MOTION_MASK |
1503 GDK_ENTER_NOTIFY_MASK |
1504 GDK_LEAVE_NOTIFY_MASK |
1505 GDK_SCROLL_MASK |
1506 GDK_KEY_PRESS_MASK);
1507 gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1508 gtk_widget_set_can_focus(s->drawing_area, TRUE);
1510 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1511 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1513 gd_update_caption(s);
1515 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1516 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1518 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1520 gtk_widget_show_all(s->window);
1522 if (full_screen) {
1523 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1526 register_displaychangelistener(&s->dcl);
1528 global_state = s;