virtio-net: properly check the vhost status during status set
[qemu/ar7.git] / ui / gtk.c
blobe12f22838e37c2ff768a0fd55d9fd4ba4a4b0956
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 <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/un.h>
57 #include <sys/wait.h>
58 #include <pty.h>
59 #include <math.h>
61 #include "ui/console.h"
62 #include "sysemu/sysemu.h"
63 #include "qmp-commands.h"
64 #include "x_keymap.h"
65 #include "keymaps.h"
66 #include "sysemu/char.h"
68 //#define DEBUG_GTK
70 #ifdef DEBUG_GTK
71 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
72 #else
73 #define DPRINTF(fmt, ...) do { } while (0)
74 #endif
76 #define MAX_VCS 10
79 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
80 #if GTK_CHECK_VERSION(3, 0, 0)
81 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
83 *ww = gdk_window_get_width(w);
84 *wh = gdk_window_get_height(w);
86 #endif
88 #if !GTK_CHECK_VERSION(2, 20, 0)
89 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
90 #endif
92 #ifndef GDK_KEY_0
93 #define GDK_KEY_0 GDK_0
94 #define GDK_KEY_1 GDK_1
95 #define GDK_KEY_2 GDK_2
96 #define GDK_KEY_f GDK_f
97 #define GDK_KEY_g GDK_g
98 #define GDK_KEY_plus GDK_plus
99 #define GDK_KEY_minus GDK_minus
100 #endif
102 static const int modifier_keycode[] = {
103 /* shift, control, alt keys, meta keys, both left & right */
104 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
107 typedef struct VirtualConsole
109 GtkWidget *menu_item;
110 GtkWidget *terminal;
111 GtkWidget *scrolled_window;
112 CharDriverState *chr;
113 int fd;
114 } VirtualConsole;
116 typedef struct GtkDisplayState
118 GtkWidget *window;
120 GtkWidget *menu_bar;
122 GtkAccelGroup *accel_group;
124 GtkWidget *machine_menu_item;
125 GtkWidget *machine_menu;
126 GtkWidget *pause_item;
127 GtkWidget *reset_item;
128 GtkWidget *powerdown_item;
129 GtkWidget *quit_item;
131 GtkWidget *view_menu_item;
132 GtkWidget *view_menu;
133 GtkWidget *full_screen_item;
134 GtkWidget *zoom_in_item;
135 GtkWidget *zoom_out_item;
136 GtkWidget *zoom_fixed_item;
137 GtkWidget *zoom_fit_item;
138 GtkWidget *grab_item;
139 GtkWidget *grab_on_hover_item;
140 GtkWidget *vga_item;
142 int nb_vcs;
143 VirtualConsole vc[MAX_VCS];
145 GtkWidget *show_tabs_item;
147 GtkWidget *vbox;
148 GtkWidget *notebook;
149 GtkWidget *drawing_area;
150 cairo_surface_t *surface;
151 DisplayChangeListener dcl;
152 DisplaySurface *ds;
153 int button_mask;
154 int last_x;
155 int last_y;
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 x1 = floor(x * s->scale_x);
308 y1 = floor(y * s->scale_y);
310 x2 = ceil(x * s->scale_x + w * s->scale_x);
311 y2 = ceil(y * s->scale_y + h * s->scale_y);
313 fbw = surface_width(s->ds) * s->scale_x;
314 fbh = surface_height(s->ds) * s->scale_y;
316 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
318 mx = my = 0;
319 if (ww > fbw) {
320 mx = (ww - fbw) / 2;
322 if (wh > fbh) {
323 my = (wh - fbh) / 2;
326 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
329 static void gd_refresh(DisplayChangeListener *dcl)
331 graphic_hw_update(dcl->con);
334 static void gd_mouse_set(DisplayChangeListener *dcl,
335 int x, int y, int visible)
337 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
338 gint x_root, y_root;
340 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
341 x, y, &x_root, &y_root);
342 gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area),
343 gtk_widget_get_screen(s->drawing_area),
344 x_root, y_root);
347 static void gd_cursor_define(DisplayChangeListener *dcl,
348 QEMUCursor *c)
350 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
351 GdkPixbuf *pixbuf;
352 GdkCursor *cursor;
354 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
355 GDK_COLORSPACE_RGB, true, 8,
356 c->width, c->height, c->width * 4,
357 NULL, NULL);
358 cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area),
359 pixbuf, c->hot_x, c->hot_y);
360 gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor);
361 g_object_unref(pixbuf);
362 g_object_unref(cursor);
365 static void gd_switch(DisplayChangeListener *dcl,
366 DisplaySurface *surface)
368 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
369 cairo_format_t kind;
370 bool resized = true;
371 int stride;
373 DPRINTF("resize(width=%d, height=%d)\n",
374 surface_width(surface), surface_height(surface));
376 if (s->surface) {
377 cairo_surface_destroy(s->surface);
380 if (s->ds &&
381 surface_width(s->ds) == surface_width(surface) &&
382 surface_height(s->ds) == surface_height(surface)) {
383 resized = false;
385 s->ds = surface;
386 switch (surface_bits_per_pixel(surface)) {
387 case 8:
388 kind = CAIRO_FORMAT_A8;
389 break;
390 case 16:
391 kind = CAIRO_FORMAT_RGB16_565;
392 break;
393 case 32:
394 kind = CAIRO_FORMAT_RGB24;
395 break;
396 default:
397 g_assert_not_reached();
398 break;
401 stride = cairo_format_stride_for_width(kind, surface_width(surface));
402 g_assert(surface_stride(surface) == stride);
404 s->surface = cairo_image_surface_create_for_data(surface_data(surface),
405 kind,
406 surface_width(surface),
407 surface_height(surface),
408 surface_stride(surface));
410 if (resized) {
411 gd_update_windowsize(s);
412 } else {
413 gd_update_full_redraw(s);
417 /** QEMU Events **/
419 static void gd_change_runstate(void *opaque, int running, RunState state)
421 GtkDisplayState *s = opaque;
423 gd_update_caption(s);
426 static void gd_mouse_mode_change(Notifier *notify, void *data)
428 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
429 FALSE);
432 /** GTK Events **/
434 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
436 GtkDisplayState *s = opaque;
437 GtkAccelGroupEntry *entries;
438 guint n_entries = 0;
439 gboolean propagate_accel = TRUE;
440 gboolean handled = FALSE;
442 entries = gtk_accel_group_query(s->accel_group, key->keyval,
443 key->state, &n_entries);
444 if (n_entries) {
445 const char *quark = g_quark_to_string(entries[0].accel_path_quark);
447 if (gd_is_grab_active(s) && strstart(quark, "<QEMU>/File/", NULL)) {
448 propagate_accel = FALSE;
452 if (!handled && propagate_accel) {
453 handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
455 if (handled) {
456 gtk_release_modifiers(s);
457 } else {
458 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
461 return handled;
464 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
465 void *opaque)
467 GtkDisplayState *s = opaque;
469 if (!no_quit) {
470 unregister_displaychangelistener(&s->dcl);
471 qmp_quit(NULL);
472 return FALSE;
475 return TRUE;
478 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
480 GtkDisplayState *s = opaque;
481 int mx, my;
482 int ww, wh;
483 int fbw, fbh;
485 if (!gtk_widget_get_realized(widget)) {
486 return FALSE;
489 fbw = surface_width(s->ds);
490 fbh = surface_height(s->ds);
492 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
494 if (s->full_screen) {
495 s->scale_x = (double)ww / fbw;
496 s->scale_y = (double)wh / fbh;
497 } else if (s->free_scale) {
498 double sx, sy;
500 sx = (double)ww / fbw;
501 sy = (double)wh / fbh;
503 s->scale_x = s->scale_y = MIN(sx, sy);
506 fbw *= s->scale_x;
507 fbh *= s->scale_y;
509 mx = my = 0;
510 if (ww > fbw) {
511 mx = (ww - fbw) / 2;
513 if (wh > fbh) {
514 my = (wh - fbh) / 2;
517 cairo_rectangle(cr, 0, 0, ww, wh);
519 /* Optionally cut out the inner area where the pixmap
520 will be drawn. This avoids 'flashing' since we're
521 not double-buffering. Note we're using the undocumented
522 behaviour of drawing the rectangle from right to left
523 to cut out the whole */
524 cairo_rectangle(cr, mx + fbw, my,
525 -1 * fbw, fbh);
526 cairo_fill(cr);
528 cairo_scale(cr, s->scale_x, s->scale_y);
529 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
530 cairo_paint(cr);
532 return TRUE;
535 #if !GTK_CHECK_VERSION(3, 0, 0)
536 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
537 void *opaque)
539 cairo_t *cr;
540 gboolean ret;
542 cr = gdk_cairo_create(gtk_widget_get_window(widget));
543 cairo_rectangle(cr,
544 expose->area.x,
545 expose->area.y,
546 expose->area.width,
547 expose->area.height);
548 cairo_clip(cr);
550 ret = gd_draw_event(widget, cr, opaque);
552 cairo_destroy(cr);
554 return ret;
556 #endif
558 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
559 void *opaque)
561 GtkDisplayState *s = opaque;
562 int dx, dy;
563 int x, y;
564 int mx, my;
565 int fbh, fbw;
566 int ww, wh;
568 fbw = surface_width(s->ds) * s->scale_x;
569 fbh = surface_height(s->ds) * s->scale_y;
571 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
573 mx = my = 0;
574 if (ww > fbw) {
575 mx = (ww - fbw) / 2;
577 if (wh > fbh) {
578 my = (wh - fbh) / 2;
581 x = (motion->x - mx) / s->scale_x;
582 y = (motion->y - my) / s->scale_y;
584 if (x < 0 || y < 0 ||
585 x >= surface_width(s->ds) ||
586 y >= surface_height(s->ds)) {
587 return TRUE;
590 if (kbd_mouse_is_absolute()) {
591 dx = x * 0x7FFF / (surface_width(s->ds) - 1);
592 dy = y * 0x7FFF / (surface_height(s->ds) - 1);
593 } else if (s->last_x == -1 || s->last_y == -1) {
594 dx = 0;
595 dy = 0;
596 } else {
597 dx = x - s->last_x;
598 dy = y - s->last_y;
601 s->last_x = x;
602 s->last_y = y;
604 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
605 kbd_mouse_event(dx, dy, 0, s->button_mask);
608 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) {
609 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
610 int x = (int)motion->x_root;
611 int y = (int)motion->y_root;
613 /* In relative mode check to see if client pointer hit
614 * one of the screen edges, and if so move it back by
615 * 200 pixels. This is important because the pointer
616 * in the server doesn't correspond 1-for-1, and so
617 * may still be only half way across the screen. Without
618 * this warp, the server pointer would thus appear to hit
619 * an invisible wall */
620 if (x == 0) {
621 x += 200;
623 if (y == 0) {
624 y += 200;
626 if (x == (gdk_screen_get_width(screen) - 1)) {
627 x -= 200;
629 if (y == (gdk_screen_get_height(screen) - 1)) {
630 y -= 200;
633 if (x != (int)motion->x_root || y != (int)motion->y_root) {
634 #if GTK_CHECK_VERSION(3, 0, 0)
635 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
636 gdk_device_warp(dev, screen, x, y);
637 #else
638 GdkDisplay *display = gtk_widget_get_display(widget);
639 gdk_display_warp_pointer(display, screen, x, y);
640 #endif
641 s->last_x = -1;
642 s->last_y = -1;
643 return FALSE;
646 return TRUE;
649 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
650 void *opaque)
652 GtkDisplayState *s = opaque;
653 int dx, dy;
654 int n;
656 if (button->button == 1) {
657 n = 0x01;
658 } else if (button->button == 2) {
659 n = 0x04;
660 } else if (button->button == 3) {
661 n = 0x02;
662 } else {
663 n = 0x00;
666 if (button->type == GDK_BUTTON_PRESS) {
667 s->button_mask |= n;
668 } else if (button->type == GDK_BUTTON_RELEASE) {
669 s->button_mask &= ~n;
672 if (kbd_mouse_is_absolute()) {
673 dx = s->last_x * 0x7FFF / (surface_width(s->ds) - 1);
674 dy = s->last_y * 0x7FFF / (surface_height(s->ds) - 1);
675 } else {
676 dx = 0;
677 dy = 0;
680 kbd_mouse_event(dx, dy, 0, s->button_mask);
682 return TRUE;
685 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
687 GtkDisplayState *s = opaque;
688 int gdk_keycode;
689 int qemu_keycode;
690 int i;
692 gdk_keycode = key->hardware_keycode;
694 if (gdk_keycode < 9) {
695 qemu_keycode = 0;
696 } else if (gdk_keycode < 97) {
697 qemu_keycode = gdk_keycode - 8;
698 } else if (gdk_keycode < 158) {
699 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
700 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
701 qemu_keycode = 0x70;
702 } else if (gdk_keycode == 211) { /* backslash */
703 qemu_keycode = 0x73;
704 } else {
705 qemu_keycode = 0;
708 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
709 gdk_keycode, qemu_keycode,
710 (key->type == GDK_KEY_PRESS) ? "down" : "up");
712 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
713 if (qemu_keycode == modifier_keycode[i]) {
714 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
718 if (qemu_keycode & SCANCODE_GREY) {
719 kbd_put_keycode(SCANCODE_EMUL0);
722 if (key->type == GDK_KEY_PRESS) {
723 kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK);
724 } else if (key->type == GDK_KEY_RELEASE) {
725 kbd_put_keycode(qemu_keycode | SCANCODE_UP);
726 } else {
727 g_assert_not_reached();
730 return TRUE;
733 /** Window Menu Actions **/
735 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
737 GtkDisplayState *s = opaque;
739 if (s->external_pause_update) {
740 return;
742 if (runstate_is_running()) {
743 qmp_stop(NULL);
744 } else {
745 qmp_cont(NULL);
749 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
751 qmp_system_reset(NULL);
754 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
756 qmp_system_powerdown(NULL);
759 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
761 qmp_quit(NULL);
764 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
766 GtkDisplayState *s = opaque;
768 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
769 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
770 } else {
771 int i;
773 gtk_release_modifiers(s);
774 for (i = 0; i < s->nb_vcs; i++) {
775 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
776 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
777 break;
783 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
785 GtkDisplayState *s = opaque;
787 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
788 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
789 } else {
790 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
794 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
796 GtkDisplayState *s = opaque;
798 if (!s->full_screen) {
799 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
800 gtk_widget_set_size_request(s->menu_bar, 0, 0);
801 gtk_widget_set_size_request(s->drawing_area, -1, -1);
802 gtk_window_fullscreen(GTK_WINDOW(s->window));
803 if (gd_on_vga(s)) {
804 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
806 s->full_screen = TRUE;
807 } else {
808 gtk_window_unfullscreen(GTK_WINDOW(s->window));
809 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
810 gtk_widget_set_size_request(s->menu_bar, -1, -1);
811 gtk_widget_set_size_request(s->drawing_area,
812 surface_width(s->ds),
813 surface_height(s->ds));
814 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
815 s->full_screen = FALSE;
816 s->scale_x = 1.0;
817 s->scale_y = 1.0;
820 gd_update_cursor(s, FALSE);
823 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
825 GtkDisplayState *s = opaque;
827 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
828 FALSE);
830 s->scale_x += .25;
831 s->scale_y += .25;
833 gd_update_windowsize(s);
836 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
838 GtkDisplayState *s = opaque;
840 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
841 FALSE);
843 s->scale_x -= .25;
844 s->scale_y -= .25;
846 s->scale_x = MAX(s->scale_x, .25);
847 s->scale_y = MAX(s->scale_y, .25);
849 gd_update_windowsize(s);
852 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
854 GtkDisplayState *s = opaque;
856 s->scale_x = 1.0;
857 s->scale_y = 1.0;
859 gd_update_windowsize(s);
862 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
864 GtkDisplayState *s = opaque;
866 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
867 s->free_scale = TRUE;
868 } else {
869 s->free_scale = FALSE;
872 gd_update_windowsize(s);
873 gd_update_full_redraw(s);
876 static void gd_grab_keyboard(GtkDisplayState *s)
878 #if GTK_CHECK_VERSION(3, 0, 0)
879 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
880 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
881 GList *devices = gdk_device_manager_list_devices(mgr,
882 GDK_DEVICE_TYPE_MASTER);
883 GList *tmp = devices;
884 while (tmp) {
885 GdkDevice *dev = tmp->data;
886 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
887 gdk_device_grab(dev,
888 gtk_widget_get_window(s->drawing_area),
889 GDK_OWNERSHIP_NONE,
890 FALSE,
891 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
892 NULL,
893 GDK_CURRENT_TIME);
895 tmp = tmp->next;
897 g_list_free(devices);
898 #else
899 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
900 FALSE,
901 GDK_CURRENT_TIME);
902 #endif
905 static void gd_ungrab_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_ungrab(dev,
917 GDK_CURRENT_TIME);
919 tmp = tmp->next;
921 g_list_free(devices);
922 #else
923 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
924 #endif
927 static void gd_grab_pointer(GtkDisplayState *s)
929 #if GTK_CHECK_VERSION(3, 0, 0)
930 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
931 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
932 GList *devices = gdk_device_manager_list_devices(mgr,
933 GDK_DEVICE_TYPE_MASTER);
934 GList *tmp = devices;
935 while (tmp) {
936 GdkDevice *dev = tmp->data;
937 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
938 gdk_device_grab(dev,
939 gtk_widget_get_window(s->drawing_area),
940 GDK_OWNERSHIP_NONE,
941 FALSE, /* All events to come to our
942 window directly */
943 GDK_POINTER_MOTION_MASK |
944 GDK_BUTTON_PRESS_MASK |
945 GDK_BUTTON_RELEASE_MASK |
946 GDK_BUTTON_MOTION_MASK |
947 GDK_SCROLL_MASK,
948 s->null_cursor,
949 GDK_CURRENT_TIME);
951 tmp = tmp->next;
953 g_list_free(devices);
954 #else
955 gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
956 FALSE, /* All events to come to our window directly */
957 GDK_POINTER_MOTION_MASK |
958 GDK_BUTTON_PRESS_MASK |
959 GDK_BUTTON_RELEASE_MASK |
960 GDK_BUTTON_MOTION_MASK |
961 GDK_SCROLL_MASK,
962 NULL, /* Allow cursor to move over entire desktop */
963 s->null_cursor,
964 GDK_CURRENT_TIME);
965 #endif
968 static void gd_ungrab_pointer(GtkDisplayState *s)
970 #if GTK_CHECK_VERSION(3, 0, 0)
971 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
972 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
973 GList *devices = gdk_device_manager_list_devices(mgr,
974 GDK_DEVICE_TYPE_MASTER);
975 GList *tmp = devices;
976 while (tmp) {
977 GdkDevice *dev = tmp->data;
978 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
979 gdk_device_ungrab(dev,
980 GDK_CURRENT_TIME);
982 tmp = tmp->next;
984 g_list_free(devices);
985 #else
986 gdk_pointer_ungrab(GDK_CURRENT_TIME);
987 #endif
990 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
992 GtkDisplayState *s = opaque;
994 if (gd_is_grab_active(s)) {
995 gd_grab_keyboard(s);
996 gd_grab_pointer(s);
997 } else {
998 gd_ungrab_keyboard(s);
999 gd_ungrab_pointer(s);
1002 gd_update_caption(s);
1003 gd_update_cursor(s, FALSE);
1006 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1007 gpointer data)
1009 GtkDisplayState *s = data;
1010 guint last_page;
1011 gboolean on_vga;
1013 if (!gtk_widget_get_realized(s->notebook)) {
1014 return;
1017 last_page = gtk_notebook_get_current_page(nb);
1019 if (last_page) {
1020 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
1023 on_vga = arg2 == 0;
1025 if (!on_vga) {
1026 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1027 FALSE);
1028 } else if (s->full_screen) {
1029 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1030 TRUE);
1033 if (arg2 == 0) {
1034 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
1035 } else {
1036 VirtualConsole *vc = &s->vc[arg2 - 1];
1037 VteTerminal *term = VTE_TERMINAL(vc->terminal);
1038 int width, height;
1040 width = 80 * vte_terminal_get_char_width(term);
1041 height = 25 * vte_terminal_get_char_height(term);
1043 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1044 gtk_widget_set_size_request(vc->terminal, width, height);
1047 gtk_widget_set_sensitive(s->grab_item, on_vga);
1049 gd_update_cursor(s, TRUE);
1052 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1054 GtkDisplayState *s = data;
1056 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1057 gd_grab_keyboard(s);
1060 return TRUE;
1063 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
1065 GtkDisplayState *s = data;
1067 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1068 gd_ungrab_keyboard(s);
1071 return TRUE;
1074 static gboolean gd_focus_out_event(GtkWidget *widget,
1075 GdkEventCrossing *crossing, gpointer data)
1077 GtkDisplayState *s = data;
1079 gtk_release_modifiers(s);
1081 return TRUE;
1084 /** Virtual Console Callbacks **/
1086 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1088 VirtualConsole *vc = chr->opaque;
1090 return write(vc->fd, buf, len);
1093 static int nb_vcs;
1094 static CharDriverState *vcs[MAX_VCS];
1096 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1098 CharDriverState *chr;
1100 chr = g_malloc0(sizeof(*chr));
1101 chr->chr_write = gd_vc_chr_write;
1103 vcs[nb_vcs++] = chr;
1105 return chr;
1108 void early_gtk_display_init(void)
1110 register_vc_handler(gd_vc_handler);
1113 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
1115 VirtualConsole *vc = opaque;
1116 uint8_t buffer[1024];
1117 ssize_t len;
1119 len = read(vc->fd, buffer, sizeof(buffer));
1120 if (len <= 0) {
1121 return FALSE;
1124 qemu_chr_be_write(vc->chr, buffer, len);
1126 return TRUE;
1129 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group,
1130 GtkWidget *view_menu)
1132 const char *label;
1133 char buffer[32];
1134 char path[32];
1135 #if VTE_CHECK_VERSION(0, 26, 0)
1136 VtePty *pty;
1137 #endif
1138 GIOChannel *chan;
1139 GtkWidget *scrolled_window;
1140 GtkAdjustment *vadjustment;
1141 int master_fd, slave_fd, ret;
1142 struct termios tty;
1144 snprintf(buffer, sizeof(buffer), "vc%d", index);
1145 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1147 vc->chr = vcs[index];
1149 if (vc->chr->label) {
1150 label = vc->chr->label;
1151 } else {
1152 label = buffer;
1155 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1156 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1157 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1158 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1160 vc->terminal = vte_terminal_new();
1162 ret = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
1163 g_assert(ret != -1);
1165 /* Set raw attributes on the pty. */
1166 tcgetattr(slave_fd, &tty);
1167 cfmakeraw(&tty);
1168 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1170 #if VTE_CHECK_VERSION(0, 26, 0)
1171 pty = vte_pty_new_foreign(master_fd, NULL);
1172 vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1173 #else
1174 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
1175 #endif
1177 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1179 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1181 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1182 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1184 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1186 vc->fd = slave_fd;
1187 vc->chr->opaque = vc;
1188 vc->scrolled_window = scrolled_window;
1190 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1191 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1193 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1194 g_signal_connect(vc->menu_item, "activate",
1195 G_CALLBACK(gd_menu_switch_vc), s);
1197 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1199 qemu_chr_be_generic_open(vc->chr);
1200 if (vc->chr->init) {
1201 vc->chr->init(vc->chr);
1204 chan = g_io_channel_unix_new(vc->fd);
1205 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1207 return group;
1210 /** Window Creation **/
1212 static void gd_connect_signals(GtkDisplayState *s)
1214 g_signal_connect(s->show_tabs_item, "activate",
1215 G_CALLBACK(gd_menu_show_tabs), s);
1217 g_signal_connect(s->window, "key-press-event",
1218 G_CALLBACK(gd_window_key_event), s);
1219 g_signal_connect(s->window, "delete-event",
1220 G_CALLBACK(gd_window_close), s);
1222 #if GTK_CHECK_VERSION(3, 0, 0)
1223 g_signal_connect(s->drawing_area, "draw",
1224 G_CALLBACK(gd_draw_event), s);
1225 #else
1226 g_signal_connect(s->drawing_area, "expose-event",
1227 G_CALLBACK(gd_expose_event), s);
1228 #endif
1229 g_signal_connect(s->drawing_area, "motion-notify-event",
1230 G_CALLBACK(gd_motion_event), s);
1231 g_signal_connect(s->drawing_area, "button-press-event",
1232 G_CALLBACK(gd_button_event), s);
1233 g_signal_connect(s->drawing_area, "button-release-event",
1234 G_CALLBACK(gd_button_event), s);
1235 g_signal_connect(s->drawing_area, "key-press-event",
1236 G_CALLBACK(gd_key_event), s);
1237 g_signal_connect(s->drawing_area, "key-release-event",
1238 G_CALLBACK(gd_key_event), s);
1240 g_signal_connect(s->pause_item, "activate",
1241 G_CALLBACK(gd_menu_pause), s);
1242 g_signal_connect(s->reset_item, "activate",
1243 G_CALLBACK(gd_menu_reset), s);
1244 g_signal_connect(s->powerdown_item, "activate",
1245 G_CALLBACK(gd_menu_powerdown), s);
1246 g_signal_connect(s->quit_item, "activate",
1247 G_CALLBACK(gd_menu_quit), s);
1248 g_signal_connect(s->full_screen_item, "activate",
1249 G_CALLBACK(gd_menu_full_screen), s);
1250 g_signal_connect(s->zoom_in_item, "activate",
1251 G_CALLBACK(gd_menu_zoom_in), s);
1252 g_signal_connect(s->zoom_out_item, "activate",
1253 G_CALLBACK(gd_menu_zoom_out), s);
1254 g_signal_connect(s->zoom_fixed_item, "activate",
1255 G_CALLBACK(gd_menu_zoom_fixed), s);
1256 g_signal_connect(s->zoom_fit_item, "activate",
1257 G_CALLBACK(gd_menu_zoom_fit), s);
1258 g_signal_connect(s->vga_item, "activate",
1259 G_CALLBACK(gd_menu_switch_vc), s);
1260 g_signal_connect(s->grab_item, "activate",
1261 G_CALLBACK(gd_menu_grab_input), s);
1262 g_signal_connect(s->notebook, "switch-page",
1263 G_CALLBACK(gd_change_page), s);
1264 g_signal_connect(s->drawing_area, "enter-notify-event",
1265 G_CALLBACK(gd_enter_event), s);
1266 g_signal_connect(s->drawing_area, "leave-notify-event",
1267 G_CALLBACK(gd_leave_event), s);
1268 g_signal_connect(s->drawing_area, "focus-out-event",
1269 G_CALLBACK(gd_focus_out_event), s);
1272 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1274 GtkWidget *machine_menu;
1275 GtkWidget *separator;
1276 GtkStockItem item;
1278 machine_menu = gtk_menu_new();
1279 gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1281 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1282 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1284 separator = gtk_separator_menu_item_new();
1285 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1287 s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1288 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1290 s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1291 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1293 separator = gtk_separator_menu_item_new();
1294 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1296 s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1297 gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1298 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1299 "<QEMU>/Machine/Quit");
1300 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1301 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1303 return machine_menu;
1306 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1308 GSList *group = NULL;
1309 GtkWidget *view_menu;
1310 GtkWidget *separator;
1311 int i;
1313 view_menu = gtk_menu_new();
1314 gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1316 s->full_screen_item =
1317 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1318 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1319 "<QEMU>/View/Full Screen");
1320 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1321 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1323 separator = gtk_separator_menu_item_new();
1324 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1326 s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1327 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1328 "<QEMU>/View/Zoom In");
1329 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1330 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1332 s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1333 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1334 "<QEMU>/View/Zoom Out");
1335 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1336 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1338 s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1339 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1340 "<QEMU>/View/Zoom Fixed");
1341 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1342 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1344 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1345 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1347 separator = gtk_separator_menu_item_new();
1348 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1350 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1351 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1353 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1354 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1355 "<QEMU>/View/Grab Input");
1356 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1357 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1359 separator = gtk_separator_menu_item_new();
1360 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1362 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1363 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1364 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1365 "<QEMU>/View/VGA");
1366 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1367 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item);
1369 for (i = 0; i < nb_vcs; i++) {
1370 VirtualConsole *vc = &s->vc[i];
1372 group = gd_vc_init(s, vc, i, group, view_menu);
1373 s->nb_vcs++;
1376 separator = gtk_separator_menu_item_new();
1377 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1379 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1380 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1382 return view_menu;
1385 static void gd_create_menus(GtkDisplayState *s)
1387 GtkAccelGroup *accel_group;
1389 accel_group = gtk_accel_group_new();
1390 s->machine_menu = gd_create_menu_machine(s, accel_group);
1391 s->view_menu = gd_create_menu_view(s, accel_group);
1393 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1394 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1395 s->machine_menu);
1396 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1398 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1399 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1400 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1402 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1403 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1404 s->accel_group = accel_group;
1407 static const DisplayChangeListenerOps dcl_ops = {
1408 .dpy_name = "gtk",
1409 .dpy_gfx_update = gd_update,
1410 .dpy_gfx_switch = gd_switch,
1411 .dpy_refresh = gd_refresh,
1412 .dpy_mouse_set = gd_mouse_set,
1413 .dpy_cursor_define = gd_cursor_define,
1416 void gtk_display_init(DisplayState *ds)
1418 GtkDisplayState *s = g_malloc0(sizeof(*s));
1419 char *filename;
1421 gtk_init(NULL, NULL);
1423 s->dcl.ops = &dcl_ops;
1424 s->dcl.con = qemu_console_lookup_by_index(0);
1426 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1427 #if GTK_CHECK_VERSION(3, 2, 0)
1428 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1429 #else
1430 s->vbox = gtk_vbox_new(FALSE, 0);
1431 #endif
1432 s->notebook = gtk_notebook_new();
1433 s->drawing_area = gtk_drawing_area_new();
1434 s->menu_bar = gtk_menu_bar_new();
1436 s->scale_x = 1.0;
1437 s->scale_y = 1.0;
1438 s->free_scale = FALSE;
1440 setlocale(LC_ALL, "");
1441 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1442 textdomain("qemu");
1444 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1446 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1447 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1448 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1450 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1452 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
1453 if (filename) {
1454 GError *error = NULL;
1455 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1456 if (pixbuf) {
1457 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1458 } else {
1459 g_error_free(error);
1461 g_free(filename);
1464 gd_create_menus(s);
1466 gd_connect_signals(s);
1468 gtk_widget_add_events(s->drawing_area,
1469 GDK_POINTER_MOTION_MASK |
1470 GDK_BUTTON_PRESS_MASK |
1471 GDK_BUTTON_RELEASE_MASK |
1472 GDK_BUTTON_MOTION_MASK |
1473 GDK_ENTER_NOTIFY_MASK |
1474 GDK_LEAVE_NOTIFY_MASK |
1475 GDK_SCROLL_MASK |
1476 GDK_KEY_PRESS_MASK);
1477 gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1478 gtk_widget_set_can_focus(s->drawing_area, TRUE);
1480 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1481 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1483 gd_update_caption(s);
1485 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1486 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1488 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1490 gtk_widget_show_all(s->window);
1492 register_displaychangelistener(&s->dcl);
1494 global_state = s;