target-arm: A64: Add support for floating point conditional compare
[qemu.git] / ui / gtk.c
blob6316f5ba00dfb1cb1806d91c938a9a5ccedae7a9
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 "trace.h"
57 #include "ui/console.h"
58 #include "sysemu/sysemu.h"
59 #include "qmp-commands.h"
60 #include "x_keymap.h"
61 #include "keymaps.h"
62 #include "sysemu/char.h"
64 #define MAX_VCS 10
67 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
68 #if GTK_CHECK_VERSION(3, 0, 0)
69 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
71 *ww = gdk_window_get_width(w);
72 *wh = gdk_window_get_height(w);
74 #endif
76 #if !GTK_CHECK_VERSION(2, 20, 0)
77 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
78 #endif
80 #ifndef GDK_KEY_0
81 #define GDK_KEY_0 GDK_0
82 #define GDK_KEY_1 GDK_1
83 #define GDK_KEY_2 GDK_2
84 #define GDK_KEY_f GDK_f
85 #define GDK_KEY_g GDK_g
86 #define GDK_KEY_plus GDK_plus
87 #define GDK_KEY_minus GDK_minus
88 #endif
90 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
91 #define IGNORE_MODIFIER_MASK \
92 (GDK_MODIFIER_MASK & ~(GDK_LOCK_MASK | GDK_MOD2_MASK))
94 static const int modifier_keycode[] = {
95 /* shift, control, alt keys, meta keys, both left & right */
96 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
99 typedef struct VirtualConsole
101 GtkWidget *menu_item;
102 GtkWidget *terminal;
103 GtkWidget *scrolled_window;
104 CharDriverState *chr;
105 int fd;
106 } VirtualConsole;
108 typedef struct GtkDisplayState
110 GtkWidget *window;
112 GtkWidget *menu_bar;
114 GtkAccelGroup *accel_group;
116 GtkWidget *machine_menu_item;
117 GtkWidget *machine_menu;
118 GtkWidget *pause_item;
119 GtkWidget *reset_item;
120 GtkWidget *powerdown_item;
121 GtkWidget *quit_item;
123 GtkWidget *view_menu_item;
124 GtkWidget *view_menu;
125 GtkWidget *full_screen_item;
126 GtkWidget *zoom_in_item;
127 GtkWidget *zoom_out_item;
128 GtkWidget *zoom_fixed_item;
129 GtkWidget *zoom_fit_item;
130 GtkWidget *grab_item;
131 GtkWidget *grab_on_hover_item;
132 GtkWidget *vga_item;
134 int nb_vcs;
135 VirtualConsole vc[MAX_VCS];
137 GtkWidget *show_tabs_item;
139 GtkWidget *vbox;
140 GtkWidget *notebook;
141 GtkWidget *drawing_area;
142 cairo_surface_t *surface;
143 pixman_image_t *convert;
144 DisplayChangeListener dcl;
145 DisplaySurface *ds;
146 int button_mask;
147 int last_x;
148 int last_y;
150 double scale_x;
151 double scale_y;
152 gboolean full_screen;
154 GdkCursor *null_cursor;
155 Notifier mouse_mode_notifier;
156 gboolean free_scale;
158 bool external_pause_update;
160 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
161 } GtkDisplayState;
163 static GtkDisplayState *global_state;
165 /** Utility Functions **/
167 static bool gd_is_grab_active(GtkDisplayState *s)
169 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
172 static bool gd_grab_on_hover(GtkDisplayState *s)
174 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
177 static bool gd_on_vga(GtkDisplayState *s)
179 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
182 static void gd_update_cursor(GtkDisplayState *s, gboolean override)
184 GdkWindow *window;
185 bool on_vga;
187 window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));
189 on_vga = gd_on_vga(s);
191 if ((override || on_vga) &&
192 (s->full_screen || kbd_mouse_is_absolute() || gd_is_grab_active(s))) {
193 gdk_window_set_cursor(window, s->null_cursor);
194 } else {
195 gdk_window_set_cursor(window, NULL);
199 static void gd_update_caption(GtkDisplayState *s)
201 const char *status = "";
202 gchar *title;
203 const char *grab = "";
204 bool is_paused = !runstate_is_running();
206 if (gd_is_grab_active(s)) {
207 grab = _(" - Press Ctrl+Alt+G to release grab");
210 if (is_paused) {
211 status = _(" [Paused]");
213 s->external_pause_update = true;
214 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
215 is_paused);
216 s->external_pause_update = false;
218 if (qemu_name) {
219 title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
220 } else {
221 title = g_strdup_printf("QEMU%s%s", status, grab);
224 gtk_window_set_title(GTK_WINDOW(s->window), title);
226 g_free(title);
229 static void gd_update_windowsize(GtkDisplayState *s)
231 if (!s->full_screen) {
232 GtkRequisition req;
233 double sx, sy;
235 if (s->free_scale) {
236 sx = s->scale_x;
237 sy = s->scale_y;
239 s->scale_y = 1.0;
240 s->scale_x = 1.0;
241 } else {
242 sx = 1.0;
243 sy = 1.0;
246 gtk_widget_set_size_request(s->drawing_area,
247 surface_width(s->ds) * s->scale_x,
248 surface_height(s->ds) * s->scale_y);
249 #if GTK_CHECK_VERSION(3, 0, 0)
250 gtk_widget_get_preferred_size(s->vbox, NULL, &req);
251 #else
252 gtk_widget_size_request(s->vbox, &req);
253 #endif
255 gtk_window_resize(GTK_WINDOW(s->window),
256 req.width * sx, req.height * sy);
260 static void gd_update_full_redraw(GtkDisplayState *s)
262 int ww, wh;
263 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
264 gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
267 static void gtk_release_modifiers(GtkDisplayState *s)
269 int i, keycode;
271 if (!gd_on_vga(s)) {
272 return;
274 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
275 keycode = modifier_keycode[i];
276 if (!s->modifier_pressed[i]) {
277 continue;
279 if (keycode & SCANCODE_GREY) {
280 kbd_put_keycode(SCANCODE_EMUL0);
282 kbd_put_keycode(keycode | SCANCODE_UP);
283 s->modifier_pressed[i] = false;
287 /** DisplayState Callbacks **/
289 static void gd_update(DisplayChangeListener *dcl,
290 int x, int y, int w, int h)
292 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
293 int x1, x2, y1, y2;
294 int mx, my;
295 int fbw, fbh;
296 int ww, wh;
298 trace_gd_update(x, y, w, h);
300 if (s->convert) {
301 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
302 x, y, 0, 0, x, y, w, h);
305 x1 = floor(x * s->scale_x);
306 y1 = floor(y * s->scale_y);
308 x2 = ceil(x * s->scale_x + w * s->scale_x);
309 y2 = ceil(y * s->scale_y + h * s->scale_y);
311 fbw = surface_width(s->ds) * s->scale_x;
312 fbh = surface_height(s->ds) * s->scale_y;
314 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
316 mx = my = 0;
317 if (ww > fbw) {
318 mx = (ww - fbw) / 2;
320 if (wh > fbh) {
321 my = (wh - fbh) / 2;
324 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
327 static void gd_refresh(DisplayChangeListener *dcl)
329 graphic_hw_update(dcl->con);
332 #if GTK_CHECK_VERSION(3, 0, 0)
333 static void gd_mouse_set(DisplayChangeListener *dcl,
334 int x, int y, int visible)
336 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
337 GdkDisplay *dpy;
338 GdkDeviceManager *mgr;
339 gint x_root, y_root;
341 dpy = gtk_widget_get_display(s->drawing_area);
342 mgr = gdk_display_get_device_manager(dpy);
343 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
344 x, y, &x_root, &y_root);
345 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
346 gtk_widget_get_screen(s->drawing_area),
347 x, y);
349 #else
350 static void gd_mouse_set(DisplayChangeListener *dcl,
351 int x, int y, int visible)
353 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
354 gint x_root, y_root;
356 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
357 x, y, &x_root, &y_root);
358 gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area),
359 gtk_widget_get_screen(s->drawing_area),
360 x_root, y_root);
362 #endif
364 static void gd_cursor_define(DisplayChangeListener *dcl,
365 QEMUCursor *c)
367 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
368 GdkPixbuf *pixbuf;
369 GdkCursor *cursor;
371 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
372 GDK_COLORSPACE_RGB, true, 8,
373 c->width, c->height, c->width * 4,
374 NULL, NULL);
375 cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area),
376 pixbuf, c->hot_x, c->hot_y);
377 gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor);
378 g_object_unref(pixbuf);
379 #if !GTK_CHECK_VERSION(3, 0, 0)
380 gdk_cursor_unref(cursor);
381 #else
382 g_object_unref(cursor);
383 #endif
386 static void gd_switch(DisplayChangeListener *dcl,
387 DisplaySurface *surface)
389 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
390 bool resized = true;
392 trace_gd_switch(surface_width(surface), surface_height(surface));
394 if (s->surface) {
395 cairo_surface_destroy(s->surface);
398 if (s->ds &&
399 surface_width(s->ds) == surface_width(surface) &&
400 surface_height(s->ds) == surface_height(surface)) {
401 resized = false;
403 s->ds = surface;
405 if (s->convert) {
406 pixman_image_unref(s->convert);
407 s->convert = NULL;
410 if (surface->format == PIXMAN_x8r8g8b8) {
412 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
414 * No need to convert, use surface directly. Should be the
415 * common case as this is qemu_default_pixelformat(32) too.
417 s->surface = cairo_image_surface_create_for_data
418 (surface_data(surface),
419 CAIRO_FORMAT_RGB24,
420 surface_width(surface),
421 surface_height(surface),
422 surface_stride(surface));
423 } else {
424 /* Must convert surface, use pixman to do it. */
425 s->convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
426 surface_width(surface),
427 surface_height(surface),
428 NULL, 0);
429 s->surface = cairo_image_surface_create_for_data
430 ((void *)pixman_image_get_data(s->convert),
431 CAIRO_FORMAT_RGB24,
432 pixman_image_get_width(s->convert),
433 pixman_image_get_height(s->convert),
434 pixman_image_get_stride(s->convert));
435 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
436 0, 0, 0, 0, 0, 0,
437 pixman_image_get_width(s->convert),
438 pixman_image_get_height(s->convert));
441 if (resized) {
442 gd_update_windowsize(s);
443 } else {
444 gd_update_full_redraw(s);
448 /** QEMU Events **/
450 static void gd_change_runstate(void *opaque, int running, RunState state)
452 GtkDisplayState *s = opaque;
454 gd_update_caption(s);
457 static void gd_mouse_mode_change(Notifier *notify, void *data)
459 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
460 FALSE);
463 /** GTK Events **/
465 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
467 GtkDisplayState *s = opaque;
468 gboolean handled = FALSE;
470 if (!gd_is_grab_active(s) ||
471 (key->state & IGNORE_MODIFIER_MASK) == HOTKEY_MODIFIERS) {
472 handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
474 if (handled) {
475 gtk_release_modifiers(s);
476 } else {
477 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
480 return handled;
483 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
484 void *opaque)
486 GtkDisplayState *s = opaque;
488 if (!no_quit) {
489 unregister_displaychangelistener(&s->dcl);
490 qmp_quit(NULL);
491 return FALSE;
494 return TRUE;
497 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
499 GtkDisplayState *s = opaque;
500 int mx, my;
501 int ww, wh;
502 int fbw, fbh;
504 if (!gtk_widget_get_realized(widget)) {
505 return FALSE;
508 fbw = surface_width(s->ds);
509 fbh = surface_height(s->ds);
511 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
513 if (s->full_screen) {
514 s->scale_x = (double)ww / fbw;
515 s->scale_y = (double)wh / fbh;
516 } else if (s->free_scale) {
517 double sx, sy;
519 sx = (double)ww / fbw;
520 sy = (double)wh / fbh;
522 s->scale_x = s->scale_y = MIN(sx, sy);
525 fbw *= s->scale_x;
526 fbh *= s->scale_y;
528 mx = my = 0;
529 if (ww > fbw) {
530 mx = (ww - fbw) / 2;
532 if (wh > fbh) {
533 my = (wh - fbh) / 2;
536 cairo_rectangle(cr, 0, 0, ww, wh);
538 /* Optionally cut out the inner area where the pixmap
539 will be drawn. This avoids 'flashing' since we're
540 not double-buffering. Note we're using the undocumented
541 behaviour of drawing the rectangle from right to left
542 to cut out the whole */
543 cairo_rectangle(cr, mx + fbw, my,
544 -1 * fbw, fbh);
545 cairo_fill(cr);
547 cairo_scale(cr, s->scale_x, s->scale_y);
548 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
549 cairo_paint(cr);
551 return TRUE;
554 #if !GTK_CHECK_VERSION(3, 0, 0)
555 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
556 void *opaque)
558 cairo_t *cr;
559 gboolean ret;
561 cr = gdk_cairo_create(gtk_widget_get_window(widget));
562 cairo_rectangle(cr,
563 expose->area.x,
564 expose->area.y,
565 expose->area.width,
566 expose->area.height);
567 cairo_clip(cr);
569 ret = gd_draw_event(widget, cr, opaque);
571 cairo_destroy(cr);
573 return ret;
575 #endif
577 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
578 void *opaque)
580 GtkDisplayState *s = opaque;
581 int dx, dy;
582 int x, y;
583 int mx, my;
584 int fbh, fbw;
585 int ww, wh;
587 fbw = surface_width(s->ds) * s->scale_x;
588 fbh = surface_height(s->ds) * s->scale_y;
590 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
592 mx = my = 0;
593 if (ww > fbw) {
594 mx = (ww - fbw) / 2;
596 if (wh > fbh) {
597 my = (wh - fbh) / 2;
600 x = (motion->x - mx) / s->scale_x;
601 y = (motion->y - my) / s->scale_y;
603 if (x < 0 || y < 0 ||
604 x >= surface_width(s->ds) ||
605 y >= surface_height(s->ds)) {
606 return TRUE;
609 if (kbd_mouse_is_absolute()) {
610 dx = x * 0x7FFF / (surface_width(s->ds) - 1);
611 dy = y * 0x7FFF / (surface_height(s->ds) - 1);
612 } else if (s->last_x == -1 || s->last_y == -1) {
613 dx = 0;
614 dy = 0;
615 } else {
616 dx = x - s->last_x;
617 dy = y - s->last_y;
620 s->last_x = x;
621 s->last_y = y;
623 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
624 kbd_mouse_event(dx, dy, 0, s->button_mask);
627 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) {
628 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
629 int x = (int)motion->x_root;
630 int y = (int)motion->y_root;
632 /* In relative mode check to see if client pointer hit
633 * one of the screen edges, and if so move it back by
634 * 200 pixels. This is important because the pointer
635 * in the server doesn't correspond 1-for-1, and so
636 * may still be only half way across the screen. Without
637 * this warp, the server pointer would thus appear to hit
638 * an invisible wall */
639 if (x == 0) {
640 x += 200;
642 if (y == 0) {
643 y += 200;
645 if (x == (gdk_screen_get_width(screen) - 1)) {
646 x -= 200;
648 if (y == (gdk_screen_get_height(screen) - 1)) {
649 y -= 200;
652 if (x != (int)motion->x_root || y != (int)motion->y_root) {
653 #if GTK_CHECK_VERSION(3, 0, 0)
654 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
655 gdk_device_warp(dev, screen, x, y);
656 #else
657 GdkDisplay *display = gtk_widget_get_display(widget);
658 gdk_display_warp_pointer(display, screen, x, y);
659 #endif
660 s->last_x = -1;
661 s->last_y = -1;
662 return FALSE;
665 return TRUE;
668 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
669 void *opaque)
671 GtkDisplayState *s = opaque;
672 int dx, dy;
673 int n;
675 if (button->button == 1) {
676 n = 0x01;
677 } else if (button->button == 2) {
678 n = 0x04;
679 } else if (button->button == 3) {
680 n = 0x02;
681 } else {
682 n = 0x00;
685 if (button->type == GDK_BUTTON_PRESS) {
686 s->button_mask |= n;
687 } else if (button->type == GDK_BUTTON_RELEASE) {
688 s->button_mask &= ~n;
691 if (kbd_mouse_is_absolute()) {
692 dx = s->last_x * 0x7FFF / (surface_width(s->ds) - 1);
693 dy = s->last_y * 0x7FFF / (surface_height(s->ds) - 1);
694 } else {
695 dx = 0;
696 dy = 0;
699 kbd_mouse_event(dx, dy, 0, s->button_mask);
701 return TRUE;
704 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
706 GtkDisplayState *s = opaque;
707 int gdk_keycode;
708 int qemu_keycode;
709 int i;
711 gdk_keycode = key->hardware_keycode;
713 if (gdk_keycode < 9) {
714 qemu_keycode = 0;
715 } else if (gdk_keycode < 97) {
716 qemu_keycode = gdk_keycode - 8;
717 } else if (gdk_keycode < 158) {
718 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
719 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
720 qemu_keycode = 0x70;
721 } else if (gdk_keycode == 211) { /* backslash */
722 qemu_keycode = 0x73;
723 } else {
724 qemu_keycode = 0;
727 trace_gd_key_event(gdk_keycode, qemu_keycode,
728 (key->type == GDK_KEY_PRESS) ? "down" : "up");
730 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
731 if (qemu_keycode == modifier_keycode[i]) {
732 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
736 if (qemu_keycode & SCANCODE_GREY) {
737 kbd_put_keycode(SCANCODE_EMUL0);
740 if (key->type == GDK_KEY_PRESS) {
741 kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK);
742 } else if (key->type == GDK_KEY_RELEASE) {
743 kbd_put_keycode(qemu_keycode | SCANCODE_UP);
744 } else {
745 g_assert_not_reached();
748 return TRUE;
751 /** Window Menu Actions **/
753 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
755 GtkDisplayState *s = opaque;
757 if (s->external_pause_update) {
758 return;
760 if (runstate_is_running()) {
761 qmp_stop(NULL);
762 } else {
763 qmp_cont(NULL);
767 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
769 qmp_system_reset(NULL);
772 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
774 qmp_system_powerdown(NULL);
777 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
779 qmp_quit(NULL);
782 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
784 GtkDisplayState *s = opaque;
786 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
787 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
788 } else {
789 int i;
791 gtk_release_modifiers(s);
792 for (i = 0; i < s->nb_vcs; i++) {
793 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
794 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
795 break;
801 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
803 GtkDisplayState *s = opaque;
805 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
806 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
807 } else {
808 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
812 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
814 GtkDisplayState *s = opaque;
816 if (!s->full_screen) {
817 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
818 gtk_widget_set_size_request(s->menu_bar, 0, 0);
819 gtk_widget_set_size_request(s->drawing_area, -1, -1);
820 gtk_window_fullscreen(GTK_WINDOW(s->window));
821 if (gd_on_vga(s)) {
822 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
824 s->full_screen = TRUE;
825 } else {
826 gtk_window_unfullscreen(GTK_WINDOW(s->window));
827 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
828 gtk_widget_set_size_request(s->menu_bar, -1, -1);
829 gtk_widget_set_size_request(s->drawing_area,
830 surface_width(s->ds),
831 surface_height(s->ds));
832 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
833 s->full_screen = FALSE;
834 s->scale_x = 1.0;
835 s->scale_y = 1.0;
838 gd_update_cursor(s, FALSE);
841 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
843 GtkDisplayState *s = opaque;
845 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
846 FALSE);
848 s->scale_x += .25;
849 s->scale_y += .25;
851 gd_update_windowsize(s);
854 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
856 GtkDisplayState *s = opaque;
858 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
859 FALSE);
861 s->scale_x -= .25;
862 s->scale_y -= .25;
864 s->scale_x = MAX(s->scale_x, .25);
865 s->scale_y = MAX(s->scale_y, .25);
867 gd_update_windowsize(s);
870 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
872 GtkDisplayState *s = opaque;
874 s->scale_x = 1.0;
875 s->scale_y = 1.0;
877 gd_update_windowsize(s);
880 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
882 GtkDisplayState *s = opaque;
884 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
885 s->free_scale = TRUE;
886 } else {
887 s->free_scale = FALSE;
888 s->scale_x = 1.0;
889 s->scale_y = 1.0;
890 gd_update_windowsize(s);
893 gd_update_full_redraw(s);
896 static void gd_grab_keyboard(GtkDisplayState *s)
898 #if GTK_CHECK_VERSION(3, 0, 0)
899 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
900 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
901 GList *devices = gdk_device_manager_list_devices(mgr,
902 GDK_DEVICE_TYPE_MASTER);
903 GList *tmp = devices;
904 while (tmp) {
905 GdkDevice *dev = tmp->data;
906 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
907 gdk_device_grab(dev,
908 gtk_widget_get_window(s->drawing_area),
909 GDK_OWNERSHIP_NONE,
910 FALSE,
911 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
912 NULL,
913 GDK_CURRENT_TIME);
915 tmp = tmp->next;
917 g_list_free(devices);
918 #else
919 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
920 FALSE,
921 GDK_CURRENT_TIME);
922 #endif
925 static void gd_ungrab_keyboard(GtkDisplayState *s)
927 #if GTK_CHECK_VERSION(3, 0, 0)
928 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
929 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
930 GList *devices = gdk_device_manager_list_devices(mgr,
931 GDK_DEVICE_TYPE_MASTER);
932 GList *tmp = devices;
933 while (tmp) {
934 GdkDevice *dev = tmp->data;
935 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
936 gdk_device_ungrab(dev,
937 GDK_CURRENT_TIME);
939 tmp = tmp->next;
941 g_list_free(devices);
942 #else
943 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
944 #endif
947 static void gd_grab_pointer(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_MOUSE) {
958 gdk_device_grab(dev,
959 gtk_widget_get_window(s->drawing_area),
960 GDK_OWNERSHIP_NONE,
961 FALSE, /* All events to come to our
962 window directly */
963 GDK_POINTER_MOTION_MASK |
964 GDK_BUTTON_PRESS_MASK |
965 GDK_BUTTON_RELEASE_MASK |
966 GDK_BUTTON_MOTION_MASK |
967 GDK_SCROLL_MASK,
968 s->null_cursor,
969 GDK_CURRENT_TIME);
971 tmp = tmp->next;
973 g_list_free(devices);
974 #else
975 gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
976 FALSE, /* All events to come to our window directly */
977 GDK_POINTER_MOTION_MASK |
978 GDK_BUTTON_PRESS_MASK |
979 GDK_BUTTON_RELEASE_MASK |
980 GDK_BUTTON_MOTION_MASK |
981 GDK_SCROLL_MASK,
982 NULL, /* Allow cursor to move over entire desktop */
983 s->null_cursor,
984 GDK_CURRENT_TIME);
985 #endif
988 static void gd_ungrab_pointer(GtkDisplayState *s)
990 #if GTK_CHECK_VERSION(3, 0, 0)
991 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
992 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
993 GList *devices = gdk_device_manager_list_devices(mgr,
994 GDK_DEVICE_TYPE_MASTER);
995 GList *tmp = devices;
996 while (tmp) {
997 GdkDevice *dev = tmp->data;
998 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
999 gdk_device_ungrab(dev,
1000 GDK_CURRENT_TIME);
1002 tmp = tmp->next;
1004 g_list_free(devices);
1005 #else
1006 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1007 #endif
1010 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1012 GtkDisplayState *s = opaque;
1014 if (gd_is_grab_active(s)) {
1015 gd_grab_keyboard(s);
1016 gd_grab_pointer(s);
1017 } else {
1018 gd_ungrab_keyboard(s);
1019 gd_ungrab_pointer(s);
1022 gd_update_caption(s);
1023 gd_update_cursor(s, FALSE);
1026 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1027 gpointer data)
1029 GtkDisplayState *s = data;
1030 guint last_page;
1031 gboolean on_vga;
1033 if (!gtk_widget_get_realized(s->notebook)) {
1034 return;
1037 last_page = gtk_notebook_get_current_page(nb);
1039 if (last_page) {
1040 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
1043 on_vga = arg2 == 0;
1045 if (!on_vga) {
1046 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1047 FALSE);
1048 } else if (s->full_screen) {
1049 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1050 TRUE);
1053 if (arg2 == 0) {
1054 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
1055 } else {
1056 VirtualConsole *vc = &s->vc[arg2 - 1];
1057 VteTerminal *term = VTE_TERMINAL(vc->terminal);
1058 int width, height;
1060 width = 80 * vte_terminal_get_char_width(term);
1061 height = 25 * vte_terminal_get_char_height(term);
1063 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1064 gtk_widget_set_size_request(vc->terminal, width, height);
1067 gtk_widget_set_sensitive(s->grab_item, on_vga);
1069 gd_update_cursor(s, TRUE);
1072 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1074 GtkDisplayState *s = data;
1076 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1077 gd_grab_keyboard(s);
1080 return TRUE;
1083 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1085 GtkDisplayState *s = data;
1087 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1088 gd_ungrab_keyboard(s);
1091 return TRUE;
1094 static gboolean gd_focus_out_event(GtkWidget *widget,
1095 GdkEventCrossing *crossing, gpointer data)
1097 GtkDisplayState *s = data;
1099 gtk_release_modifiers(s);
1101 return TRUE;
1104 /** Virtual Console Callbacks **/
1106 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1108 VirtualConsole *vc = chr->opaque;
1110 return write(vc->fd, buf, len);
1113 static int nb_vcs;
1114 static CharDriverState *vcs[MAX_VCS];
1116 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1118 CharDriverState *chr;
1120 chr = g_malloc0(sizeof(*chr));
1121 chr->chr_write = gd_vc_chr_write;
1122 /* defer OPENED events until our vc is fully initialized */
1123 chr->explicit_be_open = true;
1125 vcs[nb_vcs++] = chr;
1127 return chr;
1130 void early_gtk_display_init(void)
1132 register_vc_handler(gd_vc_handler);
1135 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
1137 VirtualConsole *vc = opaque;
1138 uint8_t buffer[1024];
1139 ssize_t len;
1141 len = read(vc->fd, buffer, sizeof(buffer));
1142 if (len <= 0) {
1143 return FALSE;
1146 qemu_chr_be_write(vc->chr, buffer, len);
1148 return TRUE;
1151 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group,
1152 GtkWidget *view_menu)
1154 const char *label;
1155 char buffer[32];
1156 char path[32];
1157 #if VTE_CHECK_VERSION(0, 26, 0)
1158 VtePty *pty;
1159 #endif
1160 GIOChannel *chan;
1161 GtkWidget *scrolled_window;
1162 GtkAdjustment *vadjustment;
1163 int master_fd, slave_fd;
1165 snprintf(buffer, sizeof(buffer), "vc%d", index);
1166 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1168 vc->chr = vcs[index];
1170 if (vc->chr->label) {
1171 label = vc->chr->label;
1172 } else {
1173 label = buffer;
1176 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1177 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1178 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1179 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, HOTKEY_MODIFIERS);
1181 vc->terminal = vte_terminal_new();
1183 master_fd = qemu_openpty_raw(&slave_fd, NULL);
1184 g_assert(master_fd != -1);
1186 #if VTE_CHECK_VERSION(0, 26, 0)
1187 pty = vte_pty_new_foreign(master_fd, NULL);
1188 vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1189 #else
1190 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
1191 #endif
1193 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1195 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1197 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1198 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1200 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1202 vc->fd = slave_fd;
1203 vc->chr->opaque = vc;
1204 vc->scrolled_window = scrolled_window;
1206 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1207 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1209 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1210 g_signal_connect(vc->menu_item, "activate",
1211 G_CALLBACK(gd_menu_switch_vc), s);
1213 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1215 qemu_chr_be_generic_open(vc->chr);
1216 if (vc->chr->init) {
1217 vc->chr->init(vc->chr);
1220 chan = g_io_channel_unix_new(vc->fd);
1221 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1223 return group;
1226 /** Window Creation **/
1228 static void gd_connect_signals(GtkDisplayState *s)
1230 g_signal_connect(s->show_tabs_item, "activate",
1231 G_CALLBACK(gd_menu_show_tabs), s);
1233 g_signal_connect(s->window, "key-press-event",
1234 G_CALLBACK(gd_window_key_event), s);
1235 g_signal_connect(s->window, "delete-event",
1236 G_CALLBACK(gd_window_close), s);
1238 #if GTK_CHECK_VERSION(3, 0, 0)
1239 g_signal_connect(s->drawing_area, "draw",
1240 G_CALLBACK(gd_draw_event), s);
1241 #else
1242 g_signal_connect(s->drawing_area, "expose-event",
1243 G_CALLBACK(gd_expose_event), s);
1244 #endif
1245 g_signal_connect(s->drawing_area, "motion-notify-event",
1246 G_CALLBACK(gd_motion_event), s);
1247 g_signal_connect(s->drawing_area, "button-press-event",
1248 G_CALLBACK(gd_button_event), s);
1249 g_signal_connect(s->drawing_area, "button-release-event",
1250 G_CALLBACK(gd_button_event), s);
1251 g_signal_connect(s->drawing_area, "key-press-event",
1252 G_CALLBACK(gd_key_event), s);
1253 g_signal_connect(s->drawing_area, "key-release-event",
1254 G_CALLBACK(gd_key_event), s);
1256 g_signal_connect(s->pause_item, "activate",
1257 G_CALLBACK(gd_menu_pause), s);
1258 g_signal_connect(s->reset_item, "activate",
1259 G_CALLBACK(gd_menu_reset), s);
1260 g_signal_connect(s->powerdown_item, "activate",
1261 G_CALLBACK(gd_menu_powerdown), s);
1262 g_signal_connect(s->quit_item, "activate",
1263 G_CALLBACK(gd_menu_quit), s);
1264 g_signal_connect(s->full_screen_item, "activate",
1265 G_CALLBACK(gd_menu_full_screen), s);
1266 g_signal_connect(s->zoom_in_item, "activate",
1267 G_CALLBACK(gd_menu_zoom_in), s);
1268 g_signal_connect(s->zoom_out_item, "activate",
1269 G_CALLBACK(gd_menu_zoom_out), s);
1270 g_signal_connect(s->zoom_fixed_item, "activate",
1271 G_CALLBACK(gd_menu_zoom_fixed), s);
1272 g_signal_connect(s->zoom_fit_item, "activate",
1273 G_CALLBACK(gd_menu_zoom_fit), s);
1274 g_signal_connect(s->vga_item, "activate",
1275 G_CALLBACK(gd_menu_switch_vc), s);
1276 g_signal_connect(s->grab_item, "activate",
1277 G_CALLBACK(gd_menu_grab_input), s);
1278 g_signal_connect(s->notebook, "switch-page",
1279 G_CALLBACK(gd_change_page), s);
1280 g_signal_connect(s->drawing_area, "enter-notify-event",
1281 G_CALLBACK(gd_enter_event), s);
1282 g_signal_connect(s->drawing_area, "leave-notify-event",
1283 G_CALLBACK(gd_leave_event), s);
1284 g_signal_connect(s->drawing_area, "focus-out-event",
1285 G_CALLBACK(gd_focus_out_event), s);
1288 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1290 GtkWidget *machine_menu;
1291 GtkWidget *separator;
1292 GtkStockItem item;
1294 machine_menu = gtk_menu_new();
1295 gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1297 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1298 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1300 separator = gtk_separator_menu_item_new();
1301 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1303 s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1304 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1306 s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1307 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1309 separator = gtk_separator_menu_item_new();
1310 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1312 s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1313 gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1314 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1315 "<QEMU>/Machine/Quit");
1316 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1317 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1319 return machine_menu;
1322 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1324 GSList *group = NULL;
1325 GtkWidget *view_menu;
1326 GtkWidget *separator;
1327 int i;
1329 view_menu = gtk_menu_new();
1330 gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1332 s->full_screen_item =
1333 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1334 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1335 "<QEMU>/View/Full Screen");
1336 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
1337 HOTKEY_MODIFIERS);
1338 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1340 separator = gtk_separator_menu_item_new();
1341 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1343 s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1344 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1345 "<QEMU>/View/Zoom In");
1346 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1347 HOTKEY_MODIFIERS);
1348 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1350 s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1351 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1352 "<QEMU>/View/Zoom Out");
1353 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1354 HOTKEY_MODIFIERS);
1355 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1357 s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1358 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1359 "<QEMU>/View/Zoom Fixed");
1360 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1361 HOTKEY_MODIFIERS);
1362 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1364 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1365 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1367 separator = gtk_separator_menu_item_new();
1368 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1370 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1371 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1373 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1374 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1375 "<QEMU>/View/Grab Input");
1376 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1377 HOTKEY_MODIFIERS);
1378 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1380 separator = gtk_separator_menu_item_new();
1381 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1383 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1384 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1385 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1386 "<QEMU>/View/VGA");
1387 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, HOTKEY_MODIFIERS);
1388 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item);
1390 for (i = 0; i < nb_vcs; i++) {
1391 VirtualConsole *vc = &s->vc[i];
1393 group = gd_vc_init(s, vc, i, group, view_menu);
1394 s->nb_vcs++;
1397 separator = gtk_separator_menu_item_new();
1398 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1400 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1401 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1403 return view_menu;
1406 static void gd_create_menus(GtkDisplayState *s)
1408 GtkAccelGroup *accel_group;
1410 accel_group = gtk_accel_group_new();
1411 s->machine_menu = gd_create_menu_machine(s, accel_group);
1412 s->view_menu = gd_create_menu_view(s, accel_group);
1414 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1415 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1416 s->machine_menu);
1417 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1419 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1420 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1421 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1423 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1424 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1425 s->accel_group = accel_group;
1428 static const DisplayChangeListenerOps dcl_ops = {
1429 .dpy_name = "gtk",
1430 .dpy_gfx_update = gd_update,
1431 .dpy_gfx_switch = gd_switch,
1432 .dpy_refresh = gd_refresh,
1433 .dpy_mouse_set = gd_mouse_set,
1434 .dpy_cursor_define = gd_cursor_define,
1437 void gtk_display_init(DisplayState *ds, bool full_screen)
1439 GtkDisplayState *s = g_malloc0(sizeof(*s));
1440 char *filename;
1442 gtk_init(NULL, NULL);
1444 s->dcl.ops = &dcl_ops;
1445 s->dcl.con = qemu_console_lookup_by_index(0);
1447 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1448 #if GTK_CHECK_VERSION(3, 2, 0)
1449 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1450 #else
1451 s->vbox = gtk_vbox_new(FALSE, 0);
1452 #endif
1453 s->notebook = gtk_notebook_new();
1454 s->drawing_area = gtk_drawing_area_new();
1455 s->menu_bar = gtk_menu_bar_new();
1457 s->scale_x = 1.0;
1458 s->scale_y = 1.0;
1459 s->free_scale = FALSE;
1461 setlocale(LC_ALL, "");
1462 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1463 textdomain("qemu");
1465 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1467 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1468 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1469 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1471 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1473 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1474 if (filename) {
1475 GError *error = NULL;
1476 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1477 if (pixbuf) {
1478 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1479 } else {
1480 g_error_free(error);
1482 g_free(filename);
1485 gd_create_menus(s);
1487 gd_connect_signals(s);
1489 gtk_widget_add_events(s->drawing_area,
1490 GDK_POINTER_MOTION_MASK |
1491 GDK_BUTTON_PRESS_MASK |
1492 GDK_BUTTON_RELEASE_MASK |
1493 GDK_BUTTON_MOTION_MASK |
1494 GDK_ENTER_NOTIFY_MASK |
1495 GDK_LEAVE_NOTIFY_MASK |
1496 GDK_SCROLL_MASK |
1497 GDK_KEY_PRESS_MASK);
1498 gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1499 gtk_widget_set_can_focus(s->drawing_area, TRUE);
1501 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1502 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1504 gd_update_caption(s);
1506 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1507 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1509 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1511 gtk_widget_show_all(s->window);
1513 if (full_screen) {
1514 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1517 register_displaychangelistener(&s->dcl);
1519 global_state = s;