Replace expose-event handler with draw handler in GTK3
[qemu/pbrook.git] / ui / gtk.c
blobfa4c3b0d1242cb2e29891de129bab0a40d081572
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 ignored "-Wstrict-prototypes"
42 #endif
43 #include <gtk/gtk.h>
44 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
45 #pragma GCC diagnostic error "-Wstrict-prototypes"
46 #endif
49 #include <gdk/gdkkeysyms.h>
50 #include <glib/gi18n.h>
51 #include <locale.h>
52 #include <vte/vte.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #include <sys/wait.h>
57 #include <pty.h>
58 #include <math.h>
60 #include "ui/console.h"
61 #include "sysemu/sysemu.h"
62 #include "qmp-commands.h"
63 #include "x_keymap.h"
64 #include "keymaps.h"
65 #include "char/char.h"
67 //#define DEBUG_GTK
69 #ifdef DEBUG_GTK
70 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
71 #else
72 #define DPRINTF(fmt, ...) do { } while (0)
73 #endif
75 #define MAX_VCS 10
78 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
79 #if GTK_CHECK_VERSION(3, 0, 0)
80 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
82 *ww = gdk_window_get_width(w);
83 *wh = gdk_window_get_height(w);
85 #endif
87 typedef struct VirtualConsole
89 GtkWidget *menu_item;
90 GtkWidget *terminal;
91 GtkWidget *scrolled_window;
92 CharDriverState *chr;
93 int fd;
94 } VirtualConsole;
96 typedef struct GtkDisplayState
98 GtkWidget *window;
100 GtkWidget *menu_bar;
102 GtkAccelGroup *accel_group;
104 GtkWidget *machine_menu_item;
105 GtkWidget *machine_menu;
106 GtkWidget *pause_item;
107 GtkWidget *reset_item;
108 GtkWidget *powerdown_item;
109 GtkWidget *quit_item;
111 GtkWidget *view_menu_item;
112 GtkWidget *view_menu;
113 GtkWidget *full_screen_item;
114 GtkWidget *zoom_in_item;
115 GtkWidget *zoom_out_item;
116 GtkWidget *zoom_fixed_item;
117 GtkWidget *zoom_fit_item;
118 GtkWidget *grab_item;
119 GtkWidget *grab_on_hover_item;
120 GtkWidget *vga_item;
122 int nb_vcs;
123 VirtualConsole vc[MAX_VCS];
125 GtkWidget *show_tabs_item;
127 GtkWidget *vbox;
128 GtkWidget *notebook;
129 GtkWidget *drawing_area;
130 cairo_surface_t *surface;
131 DisplayChangeListener dcl;
132 DisplayState *ds;
133 int button_mask;
134 int last_x;
135 int last_y;
137 double scale_x;
138 double scale_y;
139 gboolean full_screen;
141 GdkCursor *null_cursor;
142 Notifier mouse_mode_notifier;
143 gboolean free_scale;
145 bool external_pause_update;
146 } GtkDisplayState;
148 static GtkDisplayState *global_state;
150 /** Utility Functions **/
152 static bool gd_is_grab_active(GtkDisplayState *s)
154 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
157 static bool gd_grab_on_hover(GtkDisplayState *s)
159 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
162 static bool gd_on_vga(GtkDisplayState *s)
164 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
167 static void gd_update_cursor(GtkDisplayState *s, gboolean override)
169 GdkWindow *window;
170 bool on_vga;
172 window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));
174 on_vga = gd_on_vga(s);
176 if ((override || on_vga) &&
177 (s->full_screen || kbd_mouse_is_absolute() || gd_is_grab_active(s))) {
178 gdk_window_set_cursor(window, s->null_cursor);
179 } else {
180 gdk_window_set_cursor(window, NULL);
184 static void gd_update_caption(GtkDisplayState *s)
186 const char *status = "";
187 gchar *title;
188 const char *grab = "";
189 bool is_paused = !runstate_is_running();
191 if (gd_is_grab_active(s)) {
192 grab = " - Press Ctrl+Alt+G to release grab";
195 if (is_paused) {
196 status = " [Paused]";
198 s->external_pause_update = true;
199 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
200 is_paused);
201 s->external_pause_update = false;
203 if (qemu_name) {
204 title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
205 } else {
206 title = g_strdup_printf("QEMU%s%s", status, grab);
209 gtk_window_set_title(GTK_WINDOW(s->window), title);
211 g_free(title);
214 /** DisplayState Callbacks **/
216 static void gd_update(DisplayState *ds, int x, int y, int w, int h)
218 GtkDisplayState *s = ds->opaque;
219 int x1, x2, y1, y2;
220 int mx, my;
221 int fbw, fbh;
222 int ww, wh;
224 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x, y, w, h);
226 x1 = floor(x * s->scale_x);
227 y1 = floor(y * s->scale_y);
229 x2 = ceil(x * s->scale_x + w * s->scale_x);
230 y2 = ceil(y * s->scale_y + h * s->scale_y);
232 fbw = ds_get_width(s->ds) * s->scale_x;
233 fbh = ds_get_height(s->ds) * s->scale_y;
235 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
237 mx = my = 0;
238 if (ww > fbw) {
239 mx = (ww - fbw) / 2;
241 if (wh > fbh) {
242 my = (wh - fbh) / 2;
245 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
248 static void gd_refresh(DisplayState *ds)
250 vga_hw_update();
253 static void gd_resize(DisplayState *ds)
255 GtkDisplayState *s = ds->opaque;
256 cairo_format_t kind;
257 int stride;
259 DPRINTF("resize(width=%d, height=%d)\n",
260 ds_get_width(ds), ds_get_height(ds));
262 if (s->surface) {
263 cairo_surface_destroy(s->surface);
266 switch (ds->surface->pf.bits_per_pixel) {
267 case 8:
268 kind = CAIRO_FORMAT_A8;
269 break;
270 case 16:
271 kind = CAIRO_FORMAT_RGB16_565;
272 break;
273 case 32:
274 kind = CAIRO_FORMAT_RGB24;
275 break;
276 default:
277 g_assert_not_reached();
278 break;
281 stride = cairo_format_stride_for_width(kind, ds_get_width(ds));
282 g_assert(ds_get_linesize(ds) == stride);
284 s->surface = cairo_image_surface_create_for_data(ds_get_data(ds),
285 kind,
286 ds_get_width(ds),
287 ds_get_height(ds),
288 ds_get_linesize(ds));
290 if (!s->full_screen) {
291 GtkRequisition req;
292 double sx, sy;
294 if (s->free_scale) {
295 sx = s->scale_x;
296 sy = s->scale_y;
298 s->scale_y = 1.0;
299 s->scale_x = 1.0;
300 } else {
301 sx = 1.0;
302 sy = 1.0;
305 gtk_widget_set_size_request(s->drawing_area,
306 ds_get_width(ds) * s->scale_x,
307 ds_get_height(ds) * s->scale_y);
308 #if GTK_CHECK_VERSION(3, 0, 0)
309 gtk_widget_get_preferred_size(s->vbox, NULL, &req);
310 #else
311 gtk_widget_size_request(s->vbox, &req);
312 #endif
314 gtk_window_resize(GTK_WINDOW(s->window),
315 req.width * sx, req.height * sy);
319 /** QEMU Events **/
321 static void gd_change_runstate(void *opaque, int running, RunState state)
323 GtkDisplayState *s = opaque;
325 gd_update_caption(s);
328 static void gd_mouse_mode_change(Notifier *notify, void *data)
330 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
331 FALSE);
334 /** GTK Events **/
336 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
338 GtkDisplayState *s = opaque;
339 GtkAccelGroupEntry *entries;
340 guint n_entries = 0;
341 gboolean propagate_accel = TRUE;
342 gboolean handled = FALSE;
344 entries = gtk_accel_group_query(s->accel_group, key->keyval,
345 key->state, &n_entries);
346 if (n_entries) {
347 const char *quark = g_quark_to_string(entries[0].accel_path_quark);
349 if (gd_is_grab_active(s) && strstart(quark, "<QEMU>/File/", NULL)) {
350 propagate_accel = FALSE;
354 if (!handled && propagate_accel) {
355 handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
358 if (!handled) {
359 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
362 return handled;
365 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
366 void *opaque)
368 GtkDisplayState *s = opaque;
370 if (!no_quit) {
371 unregister_displaychangelistener(s->ds, &s->dcl);
372 qmp_quit(NULL);
373 return FALSE;
376 return TRUE;
379 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
381 GtkDisplayState *s = opaque;
382 int mx, my;
383 int ww, wh;
384 int fbw, fbh;
386 if (!gtk_widget_get_realized(widget)) {
387 return FALSE;
390 fbw = ds_get_width(s->ds);
391 fbh = ds_get_height(s->ds);
393 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
395 if (s->full_screen) {
396 s->scale_x = (double)ww / fbw;
397 s->scale_y = (double)wh / fbh;
398 } else if (s->free_scale) {
399 double sx, sy;
401 sx = (double)ww / fbw;
402 sy = (double)wh / fbh;
404 s->scale_x = s->scale_y = MIN(sx, sy);
407 fbw *= s->scale_x;
408 fbh *= s->scale_y;
410 mx = my = 0;
411 if (ww > fbw) {
412 mx = (ww - fbw) / 2;
414 if (wh > fbh) {
415 my = (wh - fbh) / 2;
418 cairo_rectangle(cr, 0, 0, ww, wh);
420 /* Optionally cut out the inner area where the pixmap
421 will be drawn. This avoids 'flashing' since we're
422 not double-buffering. Note we're using the undocumented
423 behaviour of drawing the rectangle from right to left
424 to cut out the whole */
425 cairo_rectangle(cr, mx + fbw, my,
426 -1 * fbw, fbh);
427 cairo_fill(cr);
429 cairo_scale(cr, s->scale_x, s->scale_y);
430 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
431 cairo_paint(cr);
433 return TRUE;
436 #if !GTK_CHECK_VERSION(3, 0, 0)
437 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
438 void *opaque)
440 cairo_t *cr;
441 gboolean ret;
443 cr = gdk_cairo_create(gtk_widget_get_window(widget));
444 cairo_rectangle(cr,
445 expose->area.x,
446 expose->area.y,
447 expose->area.width,
448 expose->area.height);
449 cairo_clip(cr);
451 ret = gd_draw_event(widget, cr, opaque);
453 cairo_destroy(cr);
455 return ret;
457 #endif
459 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
460 void *opaque)
462 GtkDisplayState *s = opaque;
463 int dx, dy;
464 int x, y;
465 int mx, my;
466 int fbh, fbw;
467 int ww, wh;
469 fbw = ds_get_width(s->ds) * s->scale_x;
470 fbh = ds_get_height(s->ds) * s->scale_y;
472 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
474 mx = my = 0;
475 if (ww > fbw) {
476 mx = (ww - fbw) / 2;
478 if (wh > fbh) {
479 my = (wh - fbh) / 2;
482 x = (motion->x - mx) / s->scale_x;
483 y = (motion->y - my) / s->scale_y;
485 if (x < 0 || y < 0 ||
486 x >= ds_get_width(s->ds) ||
487 y >= ds_get_height(s->ds)) {
488 return TRUE;
491 if (kbd_mouse_is_absolute()) {
492 dx = x * 0x7FFF / (ds_get_width(s->ds) - 1);
493 dy = y * 0x7FFF / (ds_get_height(s->ds) - 1);
494 } else if (s->last_x == -1 || s->last_y == -1) {
495 dx = 0;
496 dy = 0;
497 } else {
498 dx = x - s->last_x;
499 dy = y - s->last_y;
502 s->last_x = x;
503 s->last_y = y;
505 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
506 kbd_mouse_event(dx, dy, 0, s->button_mask);
509 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) {
510 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
511 int x = (int)motion->x_root;
512 int y = (int)motion->y_root;
514 /* In relative mode check to see if client pointer hit
515 * one of the screen edges, and if so move it back by
516 * 200 pixels. This is important because the pointer
517 * in the server doesn't correspond 1-for-1, and so
518 * may still be only half way across the screen. Without
519 * this warp, the server pointer would thus appear to hit
520 * an invisible wall */
521 if (x == 0) {
522 x += 200;
524 if (y == 0) {
525 y += 200;
527 if (x == (gdk_screen_get_width(screen) - 1)) {
528 x -= 200;
530 if (y == (gdk_screen_get_height(screen) - 1)) {
531 y -= 200;
534 if (x != (int)motion->x_root || y != (int)motion->y_root) {
535 #if GTK_CHECK_VERSION(3, 0, 0)
536 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
537 gdk_device_warp(dev, screen, x, y);
538 #else
539 GdkDisplay *display = gtk_widget_get_display(widget);
540 gdk_display_warp_pointer(display, screen, x, y);
541 #endif
542 s->last_x = -1;
543 s->last_y = -1;
544 return FALSE;
547 return TRUE;
550 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
551 void *opaque)
553 GtkDisplayState *s = opaque;
554 int dx, dy;
555 int n;
557 if (button->button == 1) {
558 n = 0x01;
559 } else if (button->button == 2) {
560 n = 0x04;
561 } else if (button->button == 3) {
562 n = 0x02;
563 } else {
564 n = 0x00;
567 if (button->type == GDK_BUTTON_PRESS) {
568 s->button_mask |= n;
569 } else if (button->type == GDK_BUTTON_RELEASE) {
570 s->button_mask &= ~n;
573 if (kbd_mouse_is_absolute()) {
574 dx = s->last_x * 0x7FFF / (ds_get_width(s->ds) - 1);
575 dy = s->last_y * 0x7FFF / (ds_get_height(s->ds) - 1);
576 } else {
577 dx = 0;
578 dy = 0;
581 kbd_mouse_event(dx, dy, 0, s->button_mask);
583 return TRUE;
586 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
588 int gdk_keycode;
589 int qemu_keycode;
591 gdk_keycode = key->hardware_keycode;
593 if (gdk_keycode < 9) {
594 qemu_keycode = 0;
595 } else if (gdk_keycode < 97) {
596 qemu_keycode = gdk_keycode - 8;
597 } else if (gdk_keycode < 158) {
598 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
599 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
600 qemu_keycode = 0x70;
601 } else if (gdk_keycode == 211) { /* backslash */
602 qemu_keycode = 0x73;
603 } else {
604 qemu_keycode = 0;
607 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
608 gdk_keycode, qemu_keycode,
609 (key->type == GDK_KEY_PRESS) ? "down" : "up");
611 if (qemu_keycode & SCANCODE_GREY) {
612 kbd_put_keycode(SCANCODE_EMUL0);
615 if (key->type == GDK_KEY_PRESS) {
616 kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK);
617 } else if (key->type == GDK_KEY_RELEASE) {
618 kbd_put_keycode(qemu_keycode | SCANCODE_UP);
619 } else {
620 g_assert_not_reached();
623 return TRUE;
626 /** Window Menu Actions **/
628 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
630 GtkDisplayState *s = opaque;
632 if (s->external_pause_update) {
633 return;
635 if (runstate_is_running()) {
636 qmp_stop(NULL);
637 } else {
638 qmp_cont(NULL);
642 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
644 qmp_system_reset(NULL);
647 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
649 qmp_system_powerdown(NULL);
652 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
654 qmp_quit(NULL);
657 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
659 GtkDisplayState *s = opaque;
661 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
662 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
663 } else {
664 int i;
666 for (i = 0; i < s->nb_vcs; i++) {
667 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
668 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
669 break;
675 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
677 GtkDisplayState *s = opaque;
679 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
680 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
681 } else {
682 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
686 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
688 GtkDisplayState *s = opaque;
690 if (!s->full_screen) {
691 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
692 gtk_widget_set_size_request(s->menu_bar, 0, 0);
693 gtk_widget_set_size_request(s->drawing_area, -1, -1);
694 gtk_window_fullscreen(GTK_WINDOW(s->window));
695 if (gd_on_vga(s)) {
696 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
698 s->full_screen = TRUE;
699 } else {
700 gtk_window_unfullscreen(GTK_WINDOW(s->window));
701 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
702 gtk_widget_set_size_request(s->menu_bar, -1, -1);
703 gtk_widget_set_size_request(s->drawing_area,
704 ds_get_width(s->ds), ds_get_height(s->ds));
705 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
706 s->full_screen = FALSE;
707 s->scale_x = 1.0;
708 s->scale_y = 1.0;
711 gd_update_cursor(s, FALSE);
714 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
716 GtkDisplayState *s = opaque;
718 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
719 FALSE);
721 s->scale_x += .25;
722 s->scale_y += .25;
724 gd_resize(s->ds);
727 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
729 GtkDisplayState *s = opaque;
731 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
732 FALSE);
734 s->scale_x -= .25;
735 s->scale_y -= .25;
737 s->scale_x = MAX(s->scale_x, .25);
738 s->scale_y = MAX(s->scale_y, .25);
740 gd_resize(s->ds);
743 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
745 GtkDisplayState *s = opaque;
747 s->scale_x = 1.0;
748 s->scale_y = 1.0;
750 gd_resize(s->ds);
753 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
755 GtkDisplayState *s = opaque;
756 int ww, wh;
758 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
759 s->free_scale = TRUE;
760 } else {
761 s->free_scale = FALSE;
764 gd_resize(s->ds);
766 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
767 gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
770 static void gd_grab_keyboard(GtkDisplayState *s)
772 #if GTK_CHECK_VERSION(3, 0, 0)
773 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
774 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
775 GList *devices = gdk_device_manager_list_devices(mgr,
776 GDK_DEVICE_TYPE_MASTER);
777 GList *tmp = devices;
778 while (tmp) {
779 GdkDevice *dev = tmp->data;
780 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
781 gdk_device_grab(dev,
782 gtk_widget_get_window(s->drawing_area),
783 GDK_OWNERSHIP_NONE,
784 FALSE,
785 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
786 NULL,
787 GDK_CURRENT_TIME);
789 tmp = tmp->next;
791 g_list_free(devices);
792 #else
793 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
794 FALSE,
795 GDK_CURRENT_TIME);
796 #endif
799 static void gd_ungrab_keyboard(GtkDisplayState *s)
801 #if GTK_CHECK_VERSION(3, 0, 0)
802 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
803 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
804 GList *devices = gdk_device_manager_list_devices(mgr,
805 GDK_DEVICE_TYPE_MASTER);
806 GList *tmp = devices;
807 while (tmp) {
808 GdkDevice *dev = tmp->data;
809 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
810 gdk_device_ungrab(dev,
811 GDK_CURRENT_TIME);
813 tmp = tmp->next;
815 g_list_free(devices);
816 #else
817 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
818 #endif
821 static void gd_grab_pointer(GtkDisplayState *s)
823 #if GTK_CHECK_VERSION(3, 0, 0)
824 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
825 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
826 GList *devices = gdk_device_manager_list_devices(mgr,
827 GDK_DEVICE_TYPE_MASTER);
828 GList *tmp = devices;
829 while (tmp) {
830 GdkDevice *dev = tmp->data;
831 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
832 gdk_device_grab(dev,
833 gtk_widget_get_window(s->drawing_area),
834 GDK_OWNERSHIP_NONE,
835 FALSE, /* All events to come to our
836 window directly */
837 GDK_POINTER_MOTION_MASK |
838 GDK_BUTTON_PRESS_MASK |
839 GDK_BUTTON_RELEASE_MASK |
840 GDK_BUTTON_MOTION_MASK |
841 GDK_SCROLL_MASK,
842 s->null_cursor,
843 GDK_CURRENT_TIME);
845 tmp = tmp->next;
847 g_list_free(devices);
848 #else
849 gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
850 FALSE, /* All events to come to our window directly */
851 GDK_POINTER_MOTION_MASK |
852 GDK_BUTTON_PRESS_MASK |
853 GDK_BUTTON_RELEASE_MASK |
854 GDK_BUTTON_MOTION_MASK |
855 GDK_SCROLL_MASK,
856 NULL, /* Allow cursor to move over entire desktop */
857 s->null_cursor,
858 GDK_CURRENT_TIME);
859 #endif
862 static void gd_ungrab_pointer(GtkDisplayState *s)
864 #if GTK_CHECK_VERSION(3, 0, 0)
865 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
866 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
867 GList *devices = gdk_device_manager_list_devices(mgr,
868 GDK_DEVICE_TYPE_MASTER);
869 GList *tmp = devices;
870 while (tmp) {
871 GdkDevice *dev = tmp->data;
872 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
873 gdk_device_ungrab(dev,
874 GDK_CURRENT_TIME);
876 tmp = tmp->next;
878 g_list_free(devices);
879 #else
880 gdk_pointer_ungrab(GDK_CURRENT_TIME);
881 #endif
884 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
886 GtkDisplayState *s = opaque;
888 if (gd_is_grab_active(s)) {
889 gd_grab_keyboard(s);
890 gd_grab_pointer(s);
891 } else {
892 gd_ungrab_keyboard(s);
893 gd_ungrab_pointer(s);
896 gd_update_caption(s);
897 gd_update_cursor(s, FALSE);
900 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
901 gpointer data)
903 GtkDisplayState *s = data;
904 guint last_page;
905 gboolean on_vga;
907 if (!gtk_widget_get_realized(s->notebook)) {
908 return;
911 last_page = gtk_notebook_get_current_page(nb);
913 if (last_page) {
914 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
917 on_vga = arg2 == 0;
919 if (!on_vga) {
920 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
921 FALSE);
922 } else if (s->full_screen) {
923 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
924 TRUE);
927 if (arg2 == 0) {
928 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
929 } else {
930 VirtualConsole *vc = &s->vc[arg2 - 1];
931 VteTerminal *term = VTE_TERMINAL(vc->terminal);
932 int width, height;
934 width = 80 * vte_terminal_get_char_width(term);
935 height = 25 * vte_terminal_get_char_height(term);
937 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
938 gtk_widget_set_size_request(vc->terminal, width, height);
941 gtk_widget_set_sensitive(s->grab_item, on_vga);
943 gd_update_cursor(s, TRUE);
946 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
948 GtkDisplayState *s = data;
950 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
951 gd_grab_keyboard(s);
954 return TRUE;
957 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
959 GtkDisplayState *s = data;
961 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
962 gd_ungrab_keyboard(s);
965 return TRUE;
968 /** Virtual Console Callbacks **/
970 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
972 VirtualConsole *vc = chr->opaque;
974 return write(vc->fd, buf, len);
977 static int nb_vcs;
978 static CharDriverState *vcs[MAX_VCS];
980 static CharDriverState *gd_vc_handler(QemuOpts *opts)
982 CharDriverState *chr;
984 chr = g_malloc0(sizeof(*chr));
985 chr->chr_write = gd_vc_chr_write;
987 vcs[nb_vcs++] = chr;
989 return chr;
992 void early_gtk_display_init(void)
994 register_vc_handler(gd_vc_handler);
997 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
999 VirtualConsole *vc = opaque;
1000 uint8_t buffer[1024];
1001 ssize_t len;
1003 len = read(vc->fd, buffer, sizeof(buffer));
1004 if (len <= 0) {
1005 return FALSE;
1008 qemu_chr_be_write(vc->chr, buffer, len);
1010 return TRUE;
1013 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group)
1015 const char *label;
1016 char buffer[32];
1017 char path[32];
1018 #if VTE_CHECK_VERSION(0, 26, 0)
1019 VtePty *pty;
1020 #endif
1021 GIOChannel *chan;
1022 GtkWidget *scrolled_window;
1023 GtkAdjustment *vadjustment;
1024 int master_fd, slave_fd, ret;
1025 struct termios tty;
1027 snprintf(buffer, sizeof(buffer), "vc%d", index);
1028 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
1030 vc->chr = vcs[index];
1032 if (vc->chr->label) {
1033 label = vc->chr->label;
1034 } else {
1035 label = buffer;
1038 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
1039 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1040 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1041 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1043 vc->terminal = vte_terminal_new();
1045 ret = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
1046 g_assert(ret != -1);
1048 /* Set raw attributes on the pty. */
1049 tcgetattr(slave_fd, &tty);
1050 cfmakeraw(&tty);
1051 tcsetattr(slave_fd, TCSAFLUSH, &tty);
1053 #if VTE_CHECK_VERSION(0, 26, 0)
1054 pty = vte_pty_new_foreign(master_fd, NULL);
1055 vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1056 #else
1057 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
1058 #endif
1060 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
1062 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
1064 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1065 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1067 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1069 vc->fd = slave_fd;
1070 vc->chr->opaque = vc;
1071 vc->scrolled_window = scrolled_window;
1073 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1074 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1076 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1077 g_signal_connect(vc->menu_item, "activate",
1078 G_CALLBACK(gd_menu_switch_vc), s);
1080 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), vc->menu_item);
1082 qemu_chr_generic_open(vc->chr);
1083 if (vc->chr->init) {
1084 vc->chr->init(vc->chr);
1087 chan = g_io_channel_unix_new(vc->fd);
1088 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1090 return group;
1093 /** Window Creation **/
1095 static void gd_connect_signals(GtkDisplayState *s)
1097 g_signal_connect(s->show_tabs_item, "activate",
1098 G_CALLBACK(gd_menu_show_tabs), s);
1100 g_signal_connect(s->window, "key-press-event",
1101 G_CALLBACK(gd_window_key_event), s);
1102 g_signal_connect(s->window, "delete-event",
1103 G_CALLBACK(gd_window_close), s);
1105 #if GTK_CHECK_VERSION(3, 0, 0)
1106 g_signal_connect(s->drawing_area, "draw",
1107 G_CALLBACK(gd_draw_event), s);
1108 #else
1109 g_signal_connect(s->drawing_area, "expose-event",
1110 G_CALLBACK(gd_expose_event), s);
1111 #endif
1112 g_signal_connect(s->drawing_area, "motion-notify-event",
1113 G_CALLBACK(gd_motion_event), s);
1114 g_signal_connect(s->drawing_area, "button-press-event",
1115 G_CALLBACK(gd_button_event), s);
1116 g_signal_connect(s->drawing_area, "button-release-event",
1117 G_CALLBACK(gd_button_event), s);
1118 g_signal_connect(s->drawing_area, "key-press-event",
1119 G_CALLBACK(gd_key_event), s);
1120 g_signal_connect(s->drawing_area, "key-release-event",
1121 G_CALLBACK(gd_key_event), s);
1123 g_signal_connect(s->pause_item, "activate",
1124 G_CALLBACK(gd_menu_pause), s);
1125 g_signal_connect(s->reset_item, "activate",
1126 G_CALLBACK(gd_menu_reset), s);
1127 g_signal_connect(s->powerdown_item, "activate",
1128 G_CALLBACK(gd_menu_powerdown), s);
1129 g_signal_connect(s->quit_item, "activate",
1130 G_CALLBACK(gd_menu_quit), s);
1131 g_signal_connect(s->full_screen_item, "activate",
1132 G_CALLBACK(gd_menu_full_screen), s);
1133 g_signal_connect(s->zoom_in_item, "activate",
1134 G_CALLBACK(gd_menu_zoom_in), s);
1135 g_signal_connect(s->zoom_out_item, "activate",
1136 G_CALLBACK(gd_menu_zoom_out), s);
1137 g_signal_connect(s->zoom_fixed_item, "activate",
1138 G_CALLBACK(gd_menu_zoom_fixed), s);
1139 g_signal_connect(s->zoom_fit_item, "activate",
1140 G_CALLBACK(gd_menu_zoom_fit), s);
1141 g_signal_connect(s->vga_item, "activate",
1142 G_CALLBACK(gd_menu_switch_vc), s);
1143 g_signal_connect(s->grab_item, "activate",
1144 G_CALLBACK(gd_menu_grab_input), s);
1145 g_signal_connect(s->notebook, "switch-page",
1146 G_CALLBACK(gd_change_page), s);
1147 g_signal_connect(s->drawing_area, "enter-notify-event",
1148 G_CALLBACK(gd_enter_event), s);
1149 g_signal_connect(s->drawing_area, "leave-notify-event",
1150 G_CALLBACK(gd_leave_event), s);
1153 static void gd_create_menus(GtkDisplayState *s)
1155 GtkStockItem item;
1156 GtkAccelGroup *accel_group;
1157 GSList *group = NULL;
1158 GtkWidget *separator;
1159 int i;
1161 accel_group = gtk_accel_group_new();
1162 s->machine_menu = gtk_menu_new();
1163 gtk_menu_set_accel_group(GTK_MENU(s->machine_menu), accel_group);
1164 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1166 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1167 gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->pause_item);
1169 separator = gtk_separator_menu_item_new();
1170 gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), separator);
1172 s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1173 gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->reset_item);
1175 s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1176 gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->powerdown_item);
1178 separator = gtk_separator_menu_item_new();
1179 gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), separator);
1181 s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1182 gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1183 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1184 "<QEMU>/Machine/Quit");
1185 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1186 gtk_menu_shell_append(GTK_MENU_SHELL(s->machine_menu), s->quit_item);
1188 s->view_menu = gtk_menu_new();
1189 gtk_menu_set_accel_group(GTK_MENU(s->view_menu), accel_group);
1190 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1192 s->full_screen_item =
1193 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1194 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1195 "<QEMU>/View/Full Screen");
1196 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1197 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->full_screen_item);
1199 separator = gtk_separator_menu_item_new();
1200 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1202 s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1203 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1204 "<QEMU>/View/Zoom In");
1205 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1206 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_in_item);
1208 s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1209 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1210 "<QEMU>/View/Zoom Out");
1211 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1212 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_out_item);
1214 s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1215 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1216 "<QEMU>/View/Zoom Fixed");
1217 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1218 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_fixed_item);
1220 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1221 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->zoom_fit_item);
1223 separator = gtk_separator_menu_item_new();
1224 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1226 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1227 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->grab_on_hover_item);
1229 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1230 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1231 "<QEMU>/View/Grab Input");
1232 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1233 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->grab_item);
1235 separator = gtk_separator_menu_item_new();
1236 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1238 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1239 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1240 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1241 "<QEMU>/View/VGA");
1242 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1243 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->vga_item);
1245 for (i = 0; i < nb_vcs; i++) {
1246 VirtualConsole *vc = &s->vc[i];
1248 group = gd_vc_init(s, vc, i, group);
1249 s->nb_vcs++;
1252 separator = gtk_separator_menu_item_new();
1253 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), separator);
1255 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1256 gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), s->show_tabs_item);
1258 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1259 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1260 s->accel_group = accel_group;
1262 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1263 s->machine_menu);
1264 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1266 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1267 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1270 void gtk_display_init(DisplayState *ds)
1272 GtkDisplayState *s = g_malloc0(sizeof(*s));
1274 gtk_init(NULL, NULL);
1276 ds->opaque = s;
1277 s->ds = ds;
1278 s->dcl.dpy_gfx_update = gd_update;
1279 s->dcl.dpy_gfx_resize = gd_resize;
1280 s->dcl.dpy_refresh = gd_refresh;
1282 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1283 #if GTK_CHECK_VERSION(3, 2, 0)
1284 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1285 #else
1286 s->vbox = gtk_vbox_new(FALSE, 0);
1287 #endif
1288 s->notebook = gtk_notebook_new();
1289 s->drawing_area = gtk_drawing_area_new();
1290 s->menu_bar = gtk_menu_bar_new();
1292 s->scale_x = 1.0;
1293 s->scale_y = 1.0;
1294 s->free_scale = FALSE;
1296 setlocale(LC_ALL, "");
1297 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1298 textdomain("qemu");
1300 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1302 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1303 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1304 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1306 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1308 gd_create_menus(s);
1310 gd_connect_signals(s);
1312 gtk_widget_add_events(s->drawing_area,
1313 GDK_POINTER_MOTION_MASK |
1314 GDK_BUTTON_PRESS_MASK |
1315 GDK_BUTTON_RELEASE_MASK |
1316 GDK_BUTTON_MOTION_MASK |
1317 GDK_ENTER_NOTIFY_MASK |
1318 GDK_LEAVE_NOTIFY_MASK |
1319 GDK_SCROLL_MASK |
1320 GDK_KEY_PRESS_MASK);
1321 gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1322 gtk_widget_set_can_focus(s->drawing_area, TRUE);
1324 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1325 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1327 gd_update_caption(s);
1329 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1330 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1332 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1334 gtk_widget_show_all(s->window);
1336 register_displaychangelistener(ds, &s->dcl);
1338 global_state = s;