Conditionalize use of gdk_keyboard_grab / gdk_keyboard_ungrab
[qemu/pbrook.git] / ui / gtk.c
blob3826bea97c8c5e8e82b9136f40d05deebe7c36b8
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 gtk_widget_size_request(s->vbox, &req);
310 gtk_window_resize(GTK_WINDOW(s->window),
311 req.width * sx, req.height * sy);
315 /** QEMU Events **/
317 static void gd_change_runstate(void *opaque, int running, RunState state)
319 GtkDisplayState *s = opaque;
321 gd_update_caption(s);
324 static void gd_mouse_mode_change(Notifier *notify, void *data)
326 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
327 FALSE);
330 /** GTK Events **/
332 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
334 GtkDisplayState *s = opaque;
335 GtkAccelGroupEntry *entries;
336 guint n_entries = 0;
337 gboolean propagate_accel = TRUE;
338 gboolean handled = FALSE;
340 entries = gtk_accel_group_query(s->accel_group, key->keyval,
341 key->state, &n_entries);
342 if (n_entries) {
343 const char *quark = g_quark_to_string(entries[0].accel_path_quark);
345 if (gd_is_grab_active(s) && strstart(quark, "<QEMU>/File/", NULL)) {
346 propagate_accel = FALSE;
350 if (!handled && propagate_accel) {
351 handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
354 if (!handled) {
355 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
358 return handled;
361 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
362 void *opaque)
364 GtkDisplayState *s = opaque;
366 if (!no_quit) {
367 unregister_displaychangelistener(s->ds, &s->dcl);
368 qmp_quit(NULL);
369 return FALSE;
372 return TRUE;
375 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
377 GtkDisplayState *s = opaque;
378 int mx, my;
379 int ww, wh;
380 int fbw, fbh;
382 if (!gtk_widget_get_realized(widget)) {
383 return FALSE;
386 fbw = ds_get_width(s->ds);
387 fbh = ds_get_height(s->ds);
389 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
391 if (s->full_screen) {
392 s->scale_x = (double)ww / fbw;
393 s->scale_y = (double)wh / fbh;
394 } else if (s->free_scale) {
395 double sx, sy;
397 sx = (double)ww / fbw;
398 sy = (double)wh / fbh;
400 s->scale_x = s->scale_y = MIN(sx, sy);
403 fbw *= s->scale_x;
404 fbh *= s->scale_y;
406 mx = my = 0;
407 if (ww > fbw) {
408 mx = (ww - fbw) / 2;
410 if (wh > fbh) {
411 my = (wh - fbh) / 2;
414 cairo_rectangle(cr, 0, 0, ww, wh);
416 /* Optionally cut out the inner area where the pixmap
417 will be drawn. This avoids 'flashing' since we're
418 not double-buffering. Note we're using the undocumented
419 behaviour of drawing the rectangle from right to left
420 to cut out the whole */
421 cairo_rectangle(cr, mx + fbw, my,
422 -1 * fbw, fbh);
423 cairo_fill(cr);
425 cairo_scale(cr, s->scale_x, s->scale_y);
426 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
427 cairo_paint(cr);
429 return TRUE;
432 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
433 void *opaque)
435 cairo_t *cr;
436 gboolean ret;
438 cr = gdk_cairo_create(gtk_widget_get_window(widget));
439 cairo_rectangle(cr,
440 expose->area.x,
441 expose->area.y,
442 expose->area.width,
443 expose->area.height);
444 cairo_clip(cr);
446 ret = gd_draw_event(widget, cr, opaque);
448 cairo_destroy(cr);
450 return ret;
453 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
454 void *opaque)
456 GtkDisplayState *s = opaque;
457 int dx, dy;
458 int x, y;
459 int mx, my;
460 int fbh, fbw;
461 int ww, wh;
463 fbw = ds_get_width(s->ds) * s->scale_x;
464 fbh = ds_get_height(s->ds) * s->scale_y;
466 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
468 mx = my = 0;
469 if (ww > fbw) {
470 mx = (ww - fbw) / 2;
472 if (wh > fbh) {
473 my = (wh - fbh) / 2;
476 x = (motion->x - mx) / s->scale_x;
477 y = (motion->y - my) / s->scale_y;
479 if (x < 0 || y < 0 ||
480 x >= ds_get_width(s->ds) ||
481 y >= ds_get_height(s->ds)) {
482 return TRUE;
485 if (kbd_mouse_is_absolute()) {
486 dx = x * 0x7FFF / (ds_get_width(s->ds) - 1);
487 dy = y * 0x7FFF / (ds_get_height(s->ds) - 1);
488 } else if (s->last_x == -1 || s->last_y == -1) {
489 dx = 0;
490 dy = 0;
491 } else {
492 dx = x - s->last_x;
493 dy = y - s->last_y;
496 s->last_x = x;
497 s->last_y = y;
499 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
500 kbd_mouse_event(dx, dy, 0, s->button_mask);
503 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) {
504 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
505 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
506 int x = (int)motion->x_root;
507 int y = (int)motion->y_root;
509 /* In relative mode check to see if client pointer hit
510 * one of the screen edges, and if so move it back by
511 * 200 pixels. This is important because the pointer
512 * in the server doesn't correspond 1-for-1, and so
513 * may still be only half way across the screen. Without
514 * this warp, the server pointer would thus appear to hit
515 * an invisible wall */
516 if (x == 0) {
517 x += 200;
519 if (y == 0) {
520 y += 200;
522 if (x == (gdk_screen_get_width(screen) - 1)) {
523 x -= 200;
525 if (y == (gdk_screen_get_height(screen) - 1)) {
526 y -= 200;
529 if (x != (int)motion->x_root || y != (int)motion->y_root) {
530 gdk_display_warp_pointer(display, screen, x, y);
531 s->last_x = -1;
532 s->last_y = -1;
533 return FALSE;
536 return TRUE;
539 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
540 void *opaque)
542 GtkDisplayState *s = opaque;
543 int dx, dy;
544 int n;
546 if (button->button == 1) {
547 n = 0x01;
548 } else if (button->button == 2) {
549 n = 0x04;
550 } else if (button->button == 3) {
551 n = 0x02;
552 } else {
553 n = 0x00;
556 if (button->type == GDK_BUTTON_PRESS) {
557 s->button_mask |= n;
558 } else if (button->type == GDK_BUTTON_RELEASE) {
559 s->button_mask &= ~n;
562 if (kbd_mouse_is_absolute()) {
563 dx = s->last_x * 0x7FFF / (ds_get_width(s->ds) - 1);
564 dy = s->last_y * 0x7FFF / (ds_get_height(s->ds) - 1);
565 } else {
566 dx = 0;
567 dy = 0;
570 kbd_mouse_event(dx, dy, 0, s->button_mask);
572 return TRUE;
575 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
577 int gdk_keycode;
578 int qemu_keycode;
580 gdk_keycode = key->hardware_keycode;
582 if (gdk_keycode < 9) {
583 qemu_keycode = 0;
584 } else if (gdk_keycode < 97) {
585 qemu_keycode = gdk_keycode - 8;
586 } else if (gdk_keycode < 158) {
587 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
588 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
589 qemu_keycode = 0x70;
590 } else if (gdk_keycode == 211) { /* backslash */
591 qemu_keycode = 0x73;
592 } else {
593 qemu_keycode = 0;
596 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
597 gdk_keycode, qemu_keycode,
598 (key->type == GDK_KEY_PRESS) ? "down" : "up");
600 if (qemu_keycode & SCANCODE_GREY) {
601 kbd_put_keycode(SCANCODE_EMUL0);
604 if (key->type == GDK_KEY_PRESS) {
605 kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK);
606 } else if (key->type == GDK_KEY_RELEASE) {
607 kbd_put_keycode(qemu_keycode | SCANCODE_UP);
608 } else {
609 g_assert_not_reached();
612 return TRUE;
615 /** Window Menu Actions **/
617 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
619 GtkDisplayState *s = opaque;
621 if (s->external_pause_update) {
622 return;
624 if (runstate_is_running()) {
625 qmp_stop(NULL);
626 } else {
627 qmp_cont(NULL);
631 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
633 qmp_system_reset(NULL);
636 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
638 qmp_system_powerdown(NULL);
641 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
643 qmp_quit(NULL);
646 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
648 GtkDisplayState *s = opaque;
650 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
651 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
652 } else {
653 int i;
655 for (i = 0; i < s->nb_vcs; i++) {
656 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
657 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
658 break;
664 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
666 GtkDisplayState *s = opaque;
668 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
669 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
670 } else {
671 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
675 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
677 GtkDisplayState *s = opaque;
679 if (!s->full_screen) {
680 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
681 gtk_widget_set_size_request(s->menu_bar, 0, 0);
682 gtk_widget_set_size_request(s->drawing_area, -1, -1);
683 gtk_window_fullscreen(GTK_WINDOW(s->window));
684 if (gd_on_vga(s)) {
685 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
687 s->full_screen = TRUE;
688 } else {
689 gtk_window_unfullscreen(GTK_WINDOW(s->window));
690 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
691 gtk_widget_set_size_request(s->menu_bar, -1, -1);
692 gtk_widget_set_size_request(s->drawing_area,
693 ds_get_width(s->ds), ds_get_height(s->ds));
694 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
695 s->full_screen = FALSE;
696 s->scale_x = 1.0;
697 s->scale_y = 1.0;
700 gd_update_cursor(s, FALSE);
703 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
705 GtkDisplayState *s = opaque;
707 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
708 FALSE);
710 s->scale_x += .25;
711 s->scale_y += .25;
713 gd_resize(s->ds);
716 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
718 GtkDisplayState *s = opaque;
720 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
721 FALSE);
723 s->scale_x -= .25;
724 s->scale_y -= .25;
726 s->scale_x = MAX(s->scale_x, .25);
727 s->scale_y = MAX(s->scale_y, .25);
729 gd_resize(s->ds);
732 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
734 GtkDisplayState *s = opaque;
736 s->scale_x = 1.0;
737 s->scale_y = 1.0;
739 gd_resize(s->ds);
742 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
744 GtkDisplayState *s = opaque;
745 int ww, wh;
747 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
748 s->free_scale = TRUE;
749 } else {
750 s->free_scale = FALSE;
753 gd_resize(s->ds);
755 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
756 gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
759 static void gd_grab_keyboard(GtkDisplayState *s)
761 #if GTK_CHECK_VERSION(3, 0, 0)
762 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
763 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
764 GList *devices = gdk_device_manager_list_devices(mgr,
765 GDK_DEVICE_TYPE_MASTER);
766 GList *tmp = devices;
767 while (tmp) {
768 GdkDevice *dev = tmp->data;
769 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
770 gdk_device_grab(dev,
771 gtk_widget_get_window(s->drawing_area),
772 GDK_OWNERSHIP_NONE,
773 FALSE,
774 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
775 NULL,
776 GDK_CURRENT_TIME);
778 tmp = tmp->next;
780 g_list_free(devices);
781 #else
782 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
783 FALSE,
784 GDK_CURRENT_TIME);
785 #endif
788 static void gd_ungrab_keyboard(GtkDisplayState *s)
790 #if GTK_CHECK_VERSION(3, 0, 0)
791 GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
792 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
793 GList *devices = gdk_device_manager_list_devices(mgr,
794 GDK_DEVICE_TYPE_MASTER);
795 GList *tmp = devices;
796 while (tmp) {
797 GdkDevice *dev = tmp->data;
798 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
799 gdk_device_ungrab(dev,
800 GDK_CURRENT_TIME);
802 tmp = tmp->next;
804 g_list_free(devices);
805 #else
806 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
807 #endif
810 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
812 GtkDisplayState *s = opaque;
814 if (gd_is_grab_active(s)) {
815 gd_grab_keyboard(s);
816 gdk_pointer_grab(gtk_widget_get_window(GTK_WIDGET(s->drawing_area)),
817 FALSE, /* All events to come to our window directly */
818 GDK_POINTER_MOTION_MASK |
819 GDK_BUTTON_PRESS_MASK |
820 GDK_BUTTON_RELEASE_MASK |
821 GDK_BUTTON_MOTION_MASK |
822 GDK_SCROLL_MASK,
823 NULL, /* Allow cursor to move over entire desktop */
824 s->null_cursor,
825 GDK_CURRENT_TIME);
826 } else {
827 gd_ungrab_keyboard(s);
828 gdk_pointer_ungrab(GDK_CURRENT_TIME);
831 gd_update_caption(s);
832 gd_update_cursor(s, FALSE);
835 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
836 gpointer data)
838 GtkDisplayState *s = data;
839 guint last_page;
840 gboolean on_vga;
842 if (!gtk_widget_get_realized(s->notebook)) {
843 return;
846 last_page = gtk_notebook_get_current_page(nb);
848 if (last_page) {
849 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
852 on_vga = arg2 == 0;
854 if (!on_vga) {
855 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
856 FALSE);
857 } else if (s->full_screen) {
858 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
859 TRUE);
862 if (arg2 == 0) {
863 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
864 } else {
865 VirtualConsole *vc = &s->vc[arg2 - 1];
866 VteTerminal *term = VTE_TERMINAL(vc->terminal);
867 int width, height;
869 width = 80 * vte_terminal_get_char_width(term);
870 height = 25 * vte_terminal_get_char_height(term);
872 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
873 gtk_widget_set_size_request(vc->terminal, width, height);
876 gtk_widget_set_sensitive(s->grab_item, on_vga);
878 gd_update_cursor(s, TRUE);
881 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
883 GtkDisplayState *s = data;
885 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
886 gd_grab_keyboard(s);
889 return TRUE;
892 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
894 GtkDisplayState *s = data;
896 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
897 gd_ungrab_keyboard(s);
900 return TRUE;
903 /** Virtual Console Callbacks **/
905 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
907 VirtualConsole *vc = chr->opaque;
909 return write(vc->fd, buf, len);
912 static int nb_vcs;
913 static CharDriverState *vcs[MAX_VCS];
915 static CharDriverState *gd_vc_handler(QemuOpts *opts)
917 CharDriverState *chr;
919 chr = g_malloc0(sizeof(*chr));
920 chr->chr_write = gd_vc_chr_write;
922 vcs[nb_vcs++] = chr;
924 return chr;
927 void early_gtk_display_init(void)
929 register_vc_handler(gd_vc_handler);
932 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
934 VirtualConsole *vc = opaque;
935 uint8_t buffer[1024];
936 ssize_t len;
938 len = read(vc->fd, buffer, sizeof(buffer));
939 if (len <= 0) {
940 return FALSE;
943 qemu_chr_be_write(vc->chr, buffer, len);
945 return TRUE;
948 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group)
950 const char *label;
951 char buffer[32];
952 char path[32];
953 #if VTE_CHECK_VERSION(0, 26, 0)
954 VtePty *pty;
955 #endif
956 GIOChannel *chan;
957 GtkWidget *scrolled_window;
958 GtkAdjustment *vadjustment;
959 int master_fd, slave_fd, ret;
960 struct termios tty;
962 snprintf(buffer, sizeof(buffer), "vc%d", index);
963 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
965 vc->chr = vcs[index];
967 if (vc->chr->label) {
968 label = vc->chr->label;
969 } else {
970 label = buffer;
973 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
974 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
975 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
976 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, GDK_CONTROL_MASK | GDK_MOD1_MASK);
978 vc->terminal = vte_terminal_new();
980 ret = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
981 g_assert(ret != -1);
983 /* Set raw attributes on the pty. */
984 tcgetattr(slave_fd, &tty);
985 cfmakeraw(&tty);
986 tcsetattr(slave_fd, TCSAFLUSH, &tty);
988 #if VTE_CHECK_VERSION(0, 26, 0)
989 pty = vte_pty_new_foreign(master_fd, NULL);
990 vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
991 #else
992 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
993 #endif
995 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
997 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
999 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
1000 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
1002 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
1004 vc->fd = slave_fd;
1005 vc->chr->opaque = vc;
1006 vc->scrolled_window = scrolled_window;
1008 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
1009 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1011 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
1012 g_signal_connect(vc->menu_item, "activate",
1013 G_CALLBACK(gd_menu_switch_vc), s);
1015 gtk_menu_append(GTK_MENU(s->view_menu), vc->menu_item);
1017 qemu_chr_generic_open(vc->chr);
1018 if (vc->chr->init) {
1019 vc->chr->init(vc->chr);
1022 chan = g_io_channel_unix_new(vc->fd);
1023 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
1025 return group;
1028 /** Window Creation **/
1030 static void gd_connect_signals(GtkDisplayState *s)
1032 g_signal_connect(s->show_tabs_item, "activate",
1033 G_CALLBACK(gd_menu_show_tabs), s);
1035 g_signal_connect(s->window, "key-press-event",
1036 G_CALLBACK(gd_window_key_event), s);
1037 g_signal_connect(s->window, "delete-event",
1038 G_CALLBACK(gd_window_close), s);
1040 g_signal_connect(s->drawing_area, "expose-event",
1041 G_CALLBACK(gd_expose_event), s);
1042 g_signal_connect(s->drawing_area, "motion-notify-event",
1043 G_CALLBACK(gd_motion_event), s);
1044 g_signal_connect(s->drawing_area, "button-press-event",
1045 G_CALLBACK(gd_button_event), s);
1046 g_signal_connect(s->drawing_area, "button-release-event",
1047 G_CALLBACK(gd_button_event), s);
1048 g_signal_connect(s->drawing_area, "key-press-event",
1049 G_CALLBACK(gd_key_event), s);
1050 g_signal_connect(s->drawing_area, "key-release-event",
1051 G_CALLBACK(gd_key_event), s);
1053 g_signal_connect(s->pause_item, "activate",
1054 G_CALLBACK(gd_menu_pause), s);
1055 g_signal_connect(s->reset_item, "activate",
1056 G_CALLBACK(gd_menu_reset), s);
1057 g_signal_connect(s->powerdown_item, "activate",
1058 G_CALLBACK(gd_menu_powerdown), s);
1059 g_signal_connect(s->quit_item, "activate",
1060 G_CALLBACK(gd_menu_quit), s);
1061 g_signal_connect(s->full_screen_item, "activate",
1062 G_CALLBACK(gd_menu_full_screen), s);
1063 g_signal_connect(s->zoom_in_item, "activate",
1064 G_CALLBACK(gd_menu_zoom_in), s);
1065 g_signal_connect(s->zoom_out_item, "activate",
1066 G_CALLBACK(gd_menu_zoom_out), s);
1067 g_signal_connect(s->zoom_fixed_item, "activate",
1068 G_CALLBACK(gd_menu_zoom_fixed), s);
1069 g_signal_connect(s->zoom_fit_item, "activate",
1070 G_CALLBACK(gd_menu_zoom_fit), s);
1071 g_signal_connect(s->vga_item, "activate",
1072 G_CALLBACK(gd_menu_switch_vc), s);
1073 g_signal_connect(s->grab_item, "activate",
1074 G_CALLBACK(gd_menu_grab_input), s);
1075 g_signal_connect(s->notebook, "switch-page",
1076 G_CALLBACK(gd_change_page), s);
1077 g_signal_connect(s->drawing_area, "enter-notify-event",
1078 G_CALLBACK(gd_enter_event), s);
1079 g_signal_connect(s->drawing_area, "leave-notify-event",
1080 G_CALLBACK(gd_leave_event), s);
1083 static void gd_create_menus(GtkDisplayState *s)
1085 GtkStockItem item;
1086 GtkAccelGroup *accel_group;
1087 GSList *group = NULL;
1088 GtkWidget *separator;
1089 int i;
1091 accel_group = gtk_accel_group_new();
1092 s->machine_menu = gtk_menu_new();
1093 gtk_menu_set_accel_group(GTK_MENU(s->machine_menu), accel_group);
1094 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1096 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1097 gtk_menu_append(GTK_MENU(s->machine_menu), s->pause_item);
1099 separator = gtk_separator_menu_item_new();
1100 gtk_menu_append(GTK_MENU(s->machine_menu), separator);
1102 s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1103 gtk_menu_append(GTK_MENU(s->machine_menu), s->reset_item);
1105 s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1106 gtk_menu_append(GTK_MENU(s->machine_menu), s->powerdown_item);
1108 separator = gtk_separator_menu_item_new();
1109 gtk_menu_append(GTK_MENU(s->machine_menu), separator);
1111 s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
1112 gtk_stock_lookup(GTK_STOCK_QUIT, &item);
1113 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1114 "<QEMU>/Machine/Quit");
1115 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
1116 gtk_menu_append(GTK_MENU(s->machine_menu), s->quit_item);
1118 s->view_menu = gtk_menu_new();
1119 gtk_menu_set_accel_group(GTK_MENU(s->view_menu), accel_group);
1120 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1122 s->full_screen_item =
1123 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1124 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1125 "<QEMU>/View/Full Screen");
1126 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1127 gtk_menu_append(GTK_MENU(s->view_menu), s->full_screen_item);
1129 separator = gtk_separator_menu_item_new();
1130 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1132 s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
1133 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1134 "<QEMU>/View/Zoom In");
1135 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1136 gtk_menu_append(GTK_MENU(s->view_menu), s->zoom_in_item);
1138 s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
1139 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1140 "<QEMU>/View/Zoom Out");
1141 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1142 gtk_menu_append(GTK_MENU(s->view_menu), s->zoom_out_item);
1144 s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
1145 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1146 "<QEMU>/View/Zoom Fixed");
1147 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1148 gtk_menu_append(GTK_MENU(s->view_menu), s->zoom_fixed_item);
1150 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1151 gtk_menu_append(GTK_MENU(s->view_menu), s->zoom_fit_item);
1153 separator = gtk_separator_menu_item_new();
1154 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1156 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1157 gtk_menu_append(GTK_MENU(s->view_menu), s->grab_on_hover_item);
1159 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1160 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1161 "<QEMU>/View/Grab Input");
1162 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1163 gtk_menu_append(GTK_MENU(s->view_menu), s->grab_item);
1165 separator = gtk_separator_menu_item_new();
1166 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1168 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
1169 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
1170 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
1171 "<QEMU>/View/VGA");
1172 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, GDK_CONTROL_MASK | GDK_MOD1_MASK);
1173 gtk_menu_append(GTK_MENU(s->view_menu), s->vga_item);
1175 for (i = 0; i < nb_vcs; i++) {
1176 VirtualConsole *vc = &s->vc[i];
1178 group = gd_vc_init(s, vc, i, group);
1179 s->nb_vcs++;
1182 separator = gtk_separator_menu_item_new();
1183 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1185 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1186 gtk_menu_append(GTK_MENU(s->view_menu), s->show_tabs_item);
1188 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1189 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1190 s->accel_group = accel_group;
1192 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1193 s->machine_menu);
1194 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1196 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1197 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1200 void gtk_display_init(DisplayState *ds)
1202 GtkDisplayState *s = g_malloc0(sizeof(*s));
1204 gtk_init(NULL, NULL);
1206 ds->opaque = s;
1207 s->ds = ds;
1208 s->dcl.dpy_gfx_update = gd_update;
1209 s->dcl.dpy_gfx_resize = gd_resize;
1210 s->dcl.dpy_refresh = gd_refresh;
1212 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1213 s->vbox = gtk_vbox_new(FALSE, 0);
1214 s->notebook = gtk_notebook_new();
1215 s->drawing_area = gtk_drawing_area_new();
1216 s->menu_bar = gtk_menu_bar_new();
1218 s->scale_x = 1.0;
1219 s->scale_y = 1.0;
1220 s->free_scale = FALSE;
1222 setlocale(LC_ALL, "");
1223 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1224 textdomain("qemu");
1226 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1228 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1229 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1230 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1232 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1234 gd_create_menus(s);
1236 gd_connect_signals(s);
1238 gtk_widget_add_events(s->drawing_area,
1239 GDK_POINTER_MOTION_MASK |
1240 GDK_BUTTON_PRESS_MASK |
1241 GDK_BUTTON_RELEASE_MASK |
1242 GDK_BUTTON_MOTION_MASK |
1243 GDK_ENTER_NOTIFY_MASK |
1244 GDK_LEAVE_NOTIFY_MASK |
1245 GDK_SCROLL_MASK |
1246 GDK_KEY_PRESS_MASK);
1247 gtk_widget_set_double_buffered(s->drawing_area, FALSE);
1248 gtk_widget_set_can_focus(s->drawing_area, TRUE);
1250 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1251 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1253 gd_update_caption(s);
1255 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1256 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1258 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1260 gtk_widget_show_all(s->window);
1262 register_displaychangelistener(ds, &s->dcl);
1264 global_state = s;