Merge remote-tracking branch 'remotes/armbru/tags/pull-error-2015-02-18' into staging
[qemu-kvm.git] / ui / gtk.c
blob6a81076ffc2dbbba94811333bae96978f69b954e
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 #ifdef _WIN32
38 # define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
39 #endif
41 #include "qemu-common.h"
43 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
44 /* Work around an -Wstrict-prototypes warning in GTK headers */
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
47 #endif
48 #include <gtk/gtk.h>
49 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
50 #pragma GCC diagnostic pop
51 #endif
54 #include <gdk/gdkkeysyms.h>
55 #include <glib/gi18n.h>
56 #include <locale.h>
57 #if defined(CONFIG_VTE)
58 #include <vte/vte.h>
59 #endif
60 #include <math.h>
62 #include "trace.h"
63 #include "ui/console.h"
64 #include "ui/input.h"
65 #include "sysemu/sysemu.h"
66 #include "qmp-commands.h"
67 #include "x_keymap.h"
68 #include "keymaps.h"
69 #include "sysemu/char.h"
70 #include "qom/object.h"
71 #ifdef GDK_WINDOWING_X11
72 #include <gdk/gdkx.h>
73 #include <X11/XKBlib.h>
74 #endif
76 #define MAX_VCS 10
77 #define VC_WINDOW_X_MIN 320
78 #define VC_WINDOW_Y_MIN 240
79 #define VC_TERM_X_MIN 80
80 #define VC_TERM_Y_MIN 25
81 #define VC_SCALE_MIN 0.25
82 #define VC_SCALE_STEP 0.25
84 #if !defined(CONFIG_VTE)
85 # define VTE_CHECK_VERSION(a, b, c) 0
86 #endif
88 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
90 * The gtk2 vte terminal widget seriously messes up the window resize
91 * for some reason. You basically can't make the qemu window smaller
92 * any more because the toplevel window geoemtry hints are overridden.
94 * Workaround that by hiding all vte widgets, except the one in the
95 * current tab.
97 * Luckily everything works smooth in gtk3.
99 # define VTE_RESIZE_HACK 1
100 #endif
102 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
103 #if GTK_CHECK_VERSION(3, 0, 0)
104 static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
106 *ww = gdk_window_get_width(w);
107 *wh = gdk_window_get_height(w);
109 #endif
111 #if !GTK_CHECK_VERSION(2, 20, 0)
112 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
113 #endif
115 #ifndef GDK_IS_X11_DISPLAY
116 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
117 #endif
118 #ifndef GDK_IS_WIN32_DISPLAY
119 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
120 #endif
122 #ifndef GDK_KEY_0
123 #define GDK_KEY_0 GDK_0
124 #define GDK_KEY_1 GDK_1
125 #define GDK_KEY_2 GDK_2
126 #define GDK_KEY_f GDK_f
127 #define GDK_KEY_g GDK_g
128 #define GDK_KEY_q GDK_q
129 #define GDK_KEY_plus GDK_plus
130 #define GDK_KEY_minus GDK_minus
131 #define GDK_KEY_Pause GDK_Pause
132 #endif
134 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
136 static const int modifier_keycode[] = {
137 /* shift, control, alt keys, meta keys, both left & right */
138 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
141 typedef struct GtkDisplayState GtkDisplayState;
143 typedef struct VirtualGfxConsole {
144 GtkWidget *drawing_area;
145 DisplayChangeListener dcl;
146 DisplaySurface *ds;
147 pixman_image_t *convert;
148 cairo_surface_t *surface;
149 double scale_x;
150 double scale_y;
151 } VirtualGfxConsole;
153 #if defined(CONFIG_VTE)
154 typedef struct VirtualVteConsole {
155 GtkWidget *box;
156 GtkWidget *scrollbar;
157 GtkWidget *terminal;
158 CharDriverState *chr;
159 } VirtualVteConsole;
160 #endif
162 typedef enum VirtualConsoleType {
163 GD_VC_GFX,
164 GD_VC_VTE,
165 } VirtualConsoleType;
167 typedef struct VirtualConsole {
168 GtkDisplayState *s;
169 char *label;
170 GtkWidget *window;
171 GtkWidget *menu_item;
172 GtkWidget *tab_item;
173 VirtualConsoleType type;
174 union {
175 VirtualGfxConsole gfx;
176 #if defined(CONFIG_VTE)
177 VirtualVteConsole vte;
178 #endif
180 } VirtualConsole;
182 struct GtkDisplayState {
183 GtkWidget *window;
185 GtkWidget *menu_bar;
187 GtkAccelGroup *accel_group;
189 GtkWidget *machine_menu_item;
190 GtkWidget *machine_menu;
191 GtkWidget *pause_item;
192 GtkWidget *reset_item;
193 GtkWidget *powerdown_item;
194 GtkWidget *quit_item;
196 GtkWidget *view_menu_item;
197 GtkWidget *view_menu;
198 GtkWidget *full_screen_item;
199 GtkWidget *zoom_in_item;
200 GtkWidget *zoom_out_item;
201 GtkWidget *zoom_fixed_item;
202 GtkWidget *zoom_fit_item;
203 GtkWidget *grab_item;
204 GtkWidget *grab_on_hover_item;
206 int nb_vcs;
207 VirtualConsole vc[MAX_VCS];
209 GtkWidget *show_tabs_item;
210 GtkWidget *untabify_item;
212 GtkWidget *vbox;
213 GtkWidget *notebook;
214 int button_mask;
215 gboolean last_set;
216 int last_x;
217 int last_y;
218 int grab_x_root;
219 int grab_y_root;
220 VirtualConsole *kbd_owner;
221 VirtualConsole *ptr_owner;
223 gboolean full_screen;
225 GdkCursor *null_cursor;
226 Notifier mouse_mode_notifier;
227 gboolean free_scale;
229 bool external_pause_update;
231 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
232 bool has_evdev;
235 static void gd_grab_pointer(VirtualConsole *vc);
236 static void gd_ungrab_pointer(GtkDisplayState *s);
238 /** Utility Functions **/
240 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
242 VirtualConsole *vc;
243 gint i;
245 for (i = 0; i < s->nb_vcs; i++) {
246 vc = &s->vc[i];
247 if (gtk_check_menu_item_get_active
248 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
249 return vc;
252 return NULL;
255 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
257 VirtualConsole *vc;
258 gint i, p;
260 for (i = 0; i < s->nb_vcs; i++) {
261 vc = &s->vc[i];
262 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
263 if (p == page) {
264 return vc;
267 return NULL;
270 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
272 gint page;
274 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
275 return gd_vc_find_by_page(s, page);
278 static bool gd_is_grab_active(GtkDisplayState *s)
280 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
283 static bool gd_grab_on_hover(GtkDisplayState *s)
285 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
288 static void gd_update_cursor(VirtualConsole *vc)
290 GtkDisplayState *s = vc->s;
291 GdkWindow *window;
293 if (vc->type != GD_VC_GFX) {
294 return;
297 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
298 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
299 gdk_window_set_cursor(window, s->null_cursor);
300 } else {
301 gdk_window_set_cursor(window, NULL);
305 static void gd_update_caption(GtkDisplayState *s)
307 const char *status = "";
308 gchar *prefix;
309 gchar *title;
310 const char *grab = "";
311 bool is_paused = !runstate_is_running();
312 int i;
314 if (qemu_name) {
315 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
316 } else {
317 prefix = g_strdup_printf("QEMU");
320 if (s->ptr_owner != NULL &&
321 s->ptr_owner->window == NULL) {
322 grab = _(" - Press Ctrl+Alt+G to release grab");
325 if (is_paused) {
326 status = _(" [Paused]");
328 s->external_pause_update = true;
329 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
330 is_paused);
331 s->external_pause_update = false;
333 title = g_strdup_printf("%s%s%s", prefix, status, grab);
334 gtk_window_set_title(GTK_WINDOW(s->window), title);
335 g_free(title);
337 for (i = 0; i < s->nb_vcs; i++) {
338 VirtualConsole *vc = &s->vc[i];
340 if (!vc->window) {
341 continue;
343 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
344 vc == s->kbd_owner ? " +kbd" : "",
345 vc == s->ptr_owner ? " +ptr" : "");
346 gtk_window_set_title(GTK_WINDOW(vc->window), title);
347 g_free(title);
350 g_free(prefix);
353 static void gd_update_geometry_hints(VirtualConsole *vc)
355 GtkDisplayState *s = vc->s;
356 GdkWindowHints mask = 0;
357 GdkGeometry geo = {};
358 GtkWidget *geo_widget = NULL;
359 GtkWindow *geo_window;
361 if (vc->type == GD_VC_GFX) {
362 if (s->free_scale) {
363 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
364 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
365 mask |= GDK_HINT_MIN_SIZE;
366 } else {
367 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
368 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
369 mask |= GDK_HINT_MIN_SIZE;
371 geo_widget = vc->gfx.drawing_area;
372 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
374 #if defined(CONFIG_VTE)
375 } else if (vc->type == GD_VC_VTE) {
376 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
377 GtkBorder *ib;
379 geo.width_inc = vte_terminal_get_char_width(term);
380 geo.height_inc = vte_terminal_get_char_height(term);
381 mask |= GDK_HINT_RESIZE_INC;
382 geo.base_width = geo.width_inc;
383 geo.base_height = geo.height_inc;
384 mask |= GDK_HINT_BASE_SIZE;
385 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
386 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
387 mask |= GDK_HINT_MIN_SIZE;
388 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
389 geo.base_width += ib->left + ib->right;
390 geo.base_height += ib->top + ib->bottom;
391 geo.min_width += ib->left + ib->right;
392 geo.min_height += ib->top + ib->bottom;
393 geo_widget = vc->vte.terminal;
394 #endif
397 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
398 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
401 static void gd_update_windowsize(VirtualConsole *vc)
403 GtkDisplayState *s = vc->s;
405 gd_update_geometry_hints(vc);
407 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
408 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
409 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
413 static void gd_update_full_redraw(VirtualConsole *vc)
415 GtkWidget *area = vc->gfx.drawing_area;
416 int ww, wh;
417 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
418 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
421 static void gtk_release_modifiers(GtkDisplayState *s)
423 VirtualConsole *vc = gd_vc_find_current(s);
424 int i, keycode;
426 if (vc->type != GD_VC_GFX) {
427 return;
429 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
430 keycode = modifier_keycode[i];
431 if (!s->modifier_pressed[i]) {
432 continue;
434 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
435 s->modifier_pressed[i] = false;
439 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
440 GtkWidget *widget)
442 g_object_ref(G_OBJECT(widget));
443 gtk_container_remove(GTK_CONTAINER(from), widget);
444 gtk_container_add(GTK_CONTAINER(to), widget);
445 g_object_unref(G_OBJECT(widget));
448 /** DisplayState Callbacks **/
450 static void gd_update(DisplayChangeListener *dcl,
451 int x, int y, int w, int h)
453 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
454 int x1, x2, y1, y2;
455 int mx, my;
456 int fbw, fbh;
457 int ww, wh;
459 trace_gd_update(vc->label, x, y, w, h);
461 if (vc->gfx.convert) {
462 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
463 NULL, vc->gfx.convert,
464 x, y, 0, 0, x, y, w, h);
467 x1 = floor(x * vc->gfx.scale_x);
468 y1 = floor(y * vc->gfx.scale_y);
470 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
471 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
473 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
474 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
476 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
477 &ww, &wh);
479 mx = my = 0;
480 if (ww > fbw) {
481 mx = (ww - fbw) / 2;
483 if (wh > fbh) {
484 my = (wh - fbh) / 2;
487 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
488 mx + x1, my + y1, (x2 - x1), (y2 - y1));
491 static void gd_refresh(DisplayChangeListener *dcl)
493 graphic_hw_update(dcl->con);
496 #if GTK_CHECK_VERSION(3, 0, 0)
497 static void gd_mouse_set(DisplayChangeListener *dcl,
498 int x, int y, int visible)
500 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
501 GdkDisplay *dpy;
502 GdkDeviceManager *mgr;
503 gint x_root, y_root;
505 if (qemu_input_is_absolute()) {
506 return;
509 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
510 mgr = gdk_display_get_device_manager(dpy);
511 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
512 x, y, &x_root, &y_root);
513 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
514 gtk_widget_get_screen(vc->gfx.drawing_area),
515 x_root, y_root);
517 #else
518 static void gd_mouse_set(DisplayChangeListener *dcl,
519 int x, int y, int visible)
521 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
522 gint x_root, y_root;
524 if (qemu_input_is_absolute()) {
525 return;
528 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
529 x, y, &x_root, &y_root);
530 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
531 gtk_widget_get_screen(vc->gfx.drawing_area),
532 x_root, y_root);
534 #endif
536 static void gd_cursor_define(DisplayChangeListener *dcl,
537 QEMUCursor *c)
539 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
540 GdkPixbuf *pixbuf;
541 GdkCursor *cursor;
543 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
544 GDK_COLORSPACE_RGB, true, 8,
545 c->width, c->height, c->width * 4,
546 NULL, NULL);
547 cursor = gdk_cursor_new_from_pixbuf
548 (gtk_widget_get_display(vc->gfx.drawing_area),
549 pixbuf, c->hot_x, c->hot_y);
550 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
551 g_object_unref(pixbuf);
552 #if !GTK_CHECK_VERSION(3, 0, 0)
553 gdk_cursor_unref(cursor);
554 #else
555 g_object_unref(cursor);
556 #endif
559 static void gd_switch(DisplayChangeListener *dcl,
560 DisplaySurface *surface)
562 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
563 bool resized = true;
565 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
567 if (vc->gfx.surface) {
568 cairo_surface_destroy(vc->gfx.surface);
571 if (vc->gfx.ds &&
572 surface_width(vc->gfx.ds) == surface_width(surface) &&
573 surface_height(vc->gfx.ds) == surface_height(surface)) {
574 resized = false;
576 vc->gfx.ds = surface;
578 if (vc->gfx.convert) {
579 pixman_image_unref(vc->gfx.convert);
580 vc->gfx.convert = NULL;
583 if (surface->format == PIXMAN_x8r8g8b8) {
585 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
587 * No need to convert, use surface directly. Should be the
588 * common case as this is qemu_default_pixelformat(32) too.
590 vc->gfx.surface = cairo_image_surface_create_for_data
591 (surface_data(surface),
592 CAIRO_FORMAT_RGB24,
593 surface_width(surface),
594 surface_height(surface),
595 surface_stride(surface));
596 } else {
597 /* Must convert surface, use pixman to do it. */
598 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
599 surface_width(surface),
600 surface_height(surface),
601 NULL, 0);
602 vc->gfx.surface = cairo_image_surface_create_for_data
603 ((void *)pixman_image_get_data(vc->gfx.convert),
604 CAIRO_FORMAT_RGB24,
605 pixman_image_get_width(vc->gfx.convert),
606 pixman_image_get_height(vc->gfx.convert),
607 pixman_image_get_stride(vc->gfx.convert));
608 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
609 NULL, vc->gfx.convert,
610 0, 0, 0, 0, 0, 0,
611 pixman_image_get_width(vc->gfx.convert),
612 pixman_image_get_height(vc->gfx.convert));
615 if (resized) {
616 gd_update_windowsize(vc);
617 } else {
618 gd_update_full_redraw(vc);
622 /** QEMU Events **/
624 static void gd_change_runstate(void *opaque, int running, RunState state)
626 GtkDisplayState *s = opaque;
628 gd_update_caption(s);
631 static void gd_mouse_mode_change(Notifier *notify, void *data)
633 GtkDisplayState *s;
634 int i;
636 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
637 /* release the grab at switching to absolute mode */
638 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
639 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
640 FALSE);
642 for (i = 0; i < s->nb_vcs; i++) {
643 VirtualConsole *vc = &s->vc[i];
644 gd_update_cursor(vc);
648 /** GTK Events **/
650 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
651 void *opaque)
653 GtkDisplayState *s = opaque;
654 int i;
656 if (!no_quit) {
657 for (i = 0; i < s->nb_vcs; i++) {
658 if (s->vc[i].type != GD_VC_GFX) {
659 continue;
661 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
663 qmp_quit(NULL);
664 return FALSE;
667 return TRUE;
670 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
672 VirtualConsole *vc = opaque;
673 GtkDisplayState *s = vc->s;
674 int mx, my;
675 int ww, wh;
676 int fbw, fbh;
678 if (!gtk_widget_get_realized(widget)) {
679 return FALSE;
682 fbw = surface_width(vc->gfx.ds);
683 fbh = surface_height(vc->gfx.ds);
685 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
687 if (s->full_screen) {
688 vc->gfx.scale_x = (double)ww / fbw;
689 vc->gfx.scale_y = (double)wh / fbh;
690 } else if (s->free_scale) {
691 double sx, sy;
693 sx = (double)ww / fbw;
694 sy = (double)wh / fbh;
696 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
699 fbw *= vc->gfx.scale_x;
700 fbh *= vc->gfx.scale_y;
702 mx = my = 0;
703 if (ww > fbw) {
704 mx = (ww - fbw) / 2;
706 if (wh > fbh) {
707 my = (wh - fbh) / 2;
710 cairo_rectangle(cr, 0, 0, ww, wh);
712 /* Optionally cut out the inner area where the pixmap
713 will be drawn. This avoids 'flashing' since we're
714 not double-buffering. Note we're using the undocumented
715 behaviour of drawing the rectangle from right to left
716 to cut out the whole */
717 cairo_rectangle(cr, mx + fbw, my,
718 -1 * fbw, fbh);
719 cairo_fill(cr);
721 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
722 cairo_set_source_surface(cr, vc->gfx.surface,
723 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
724 cairo_paint(cr);
726 return TRUE;
729 #if !GTK_CHECK_VERSION(3, 0, 0)
730 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
731 void *opaque)
733 cairo_t *cr;
734 gboolean ret;
736 cr = gdk_cairo_create(gtk_widget_get_window(widget));
737 cairo_rectangle(cr,
738 expose->area.x,
739 expose->area.y,
740 expose->area.width,
741 expose->area.height);
742 cairo_clip(cr);
744 ret = gd_draw_event(widget, cr, opaque);
746 cairo_destroy(cr);
748 return ret;
750 #endif
752 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
753 void *opaque)
755 VirtualConsole *vc = opaque;
756 GtkDisplayState *s = vc->s;
757 int x, y;
758 int mx, my;
759 int fbh, fbw;
760 int ww, wh;
762 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
763 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
765 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
766 &ww, &wh);
768 mx = my = 0;
769 if (ww > fbw) {
770 mx = (ww - fbw) / 2;
772 if (wh > fbh) {
773 my = (wh - fbh) / 2;
776 x = (motion->x - mx) / vc->gfx.scale_x;
777 y = (motion->y - my) / vc->gfx.scale_y;
779 if (qemu_input_is_absolute()) {
780 if (x < 0 || y < 0 ||
781 x >= surface_width(vc->gfx.ds) ||
782 y >= surface_height(vc->gfx.ds)) {
783 return TRUE;
785 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
786 surface_width(vc->gfx.ds));
787 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
788 surface_height(vc->gfx.ds));
789 qemu_input_event_sync();
790 } else if (s->last_set && s->ptr_owner == vc) {
791 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
792 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
793 qemu_input_event_sync();
795 s->last_x = x;
796 s->last_y = y;
797 s->last_set = TRUE;
799 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
800 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
801 int x = (int)motion->x_root;
802 int y = (int)motion->y_root;
804 /* In relative mode check to see if client pointer hit
805 * one of the screen edges, and if so move it back by
806 * 200 pixels. This is important because the pointer
807 * in the server doesn't correspond 1-for-1, and so
808 * may still be only half way across the screen. Without
809 * this warp, the server pointer would thus appear to hit
810 * an invisible wall */
811 if (x == 0) {
812 x += 200;
814 if (y == 0) {
815 y += 200;
817 if (x == (gdk_screen_get_width(screen) - 1)) {
818 x -= 200;
820 if (y == (gdk_screen_get_height(screen) - 1)) {
821 y -= 200;
824 if (x != (int)motion->x_root || y != (int)motion->y_root) {
825 #if GTK_CHECK_VERSION(3, 0, 0)
826 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
827 gdk_device_warp(dev, screen, x, y);
828 #else
829 GdkDisplay *display = gtk_widget_get_display(widget);
830 gdk_display_warp_pointer(display, screen, x, y);
831 #endif
832 s->last_set = FALSE;
833 return FALSE;
836 return TRUE;
839 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
840 void *opaque)
842 VirtualConsole *vc = opaque;
843 GtkDisplayState *s = vc->s;
844 InputButton btn;
846 /* implicitly grab the input at the first click in the relative mode */
847 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
848 !qemu_input_is_absolute() && s->ptr_owner != vc) {
849 gd_ungrab_pointer(s);
850 if (!vc->window) {
851 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
852 TRUE);
853 } else {
854 gd_grab_pointer(vc);
855 gd_update_caption(s);
857 return TRUE;
860 if (button->button == 1) {
861 btn = INPUT_BUTTON_LEFT;
862 } else if (button->button == 2) {
863 btn = INPUT_BUTTON_MIDDLE;
864 } else if (button->button == 3) {
865 btn = INPUT_BUTTON_RIGHT;
866 } else {
867 return TRUE;
870 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
871 button->type == GDK_BUTTON_PRESS);
872 qemu_input_event_sync();
873 return TRUE;
876 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
877 void *opaque)
879 VirtualConsole *vc = opaque;
880 InputButton btn;
882 if (scroll->direction == GDK_SCROLL_UP) {
883 btn = INPUT_BUTTON_WHEEL_UP;
884 } else if (scroll->direction == GDK_SCROLL_DOWN) {
885 btn = INPUT_BUTTON_WHEEL_DOWN;
886 } else {
887 return TRUE;
890 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
891 qemu_input_event_sync();
892 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
893 qemu_input_event_sync();
894 return TRUE;
897 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
899 int qemu_keycode;
901 #ifdef GDK_WINDOWING_WIN32
902 if (GDK_IS_WIN32_DISPLAY(dpy)) {
903 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
904 switch (qemu_keycode) {
905 case 103: /* alt gr */
906 qemu_keycode = 56 | SCANCODE_GREY;
907 break;
909 return qemu_keycode;
911 #endif
913 if (gdk_keycode < 9) {
914 qemu_keycode = 0;
915 } else if (gdk_keycode < 97) {
916 qemu_keycode = gdk_keycode - 8;
917 #ifdef GDK_WINDOWING_X11
918 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
919 if (s->has_evdev) {
920 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
921 } else {
922 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
924 #endif
925 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
926 qemu_keycode = 0x70;
927 } else if (gdk_keycode == 211) { /* backslash */
928 qemu_keycode = 0x73;
929 } else {
930 qemu_keycode = 0;
933 return qemu_keycode;
936 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
938 VirtualConsole *vc = opaque;
939 GtkDisplayState *s = vc->s;
940 int gdk_keycode = key->hardware_keycode;
941 int qemu_keycode;
942 int i;
944 if (key->keyval == GDK_KEY_Pause) {
945 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
946 key->type == GDK_KEY_PRESS);
947 return TRUE;
950 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
951 gdk_keycode);
953 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
954 (key->type == GDK_KEY_PRESS) ? "down" : "up");
956 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
957 if (qemu_keycode == modifier_keycode[i]) {
958 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
962 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
963 key->type == GDK_KEY_PRESS);
965 return TRUE;
968 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
970 if (event->type == GDK_MOTION_NOTIFY) {
971 return gd_motion_event(widget, &event->motion, opaque);
973 return FALSE;
976 /** Window Menu Actions **/
978 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
980 GtkDisplayState *s = opaque;
982 if (s->external_pause_update) {
983 return;
985 if (runstate_is_running()) {
986 qmp_stop(NULL);
987 } else {
988 qmp_cont(NULL);
992 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
994 qmp_system_reset(NULL);
997 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
999 qmp_system_powerdown(NULL);
1002 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1004 qmp_quit(NULL);
1007 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1009 GtkDisplayState *s = opaque;
1010 VirtualConsole *vc = gd_vc_find_by_menu(s);
1011 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1012 GtkWidget *child;
1013 gint page;
1015 gtk_release_modifiers(s);
1016 if (vc) {
1017 page = gtk_notebook_page_num(nb, vc->tab_item);
1018 gtk_notebook_set_current_page(nb, page);
1019 child = gtk_notebook_get_nth_page(nb, page);
1020 gtk_widget_grab_focus(child);
1024 static void gd_accel_switch_vc(void *opaque)
1026 VirtualConsole *vc = opaque;
1027 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1030 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1032 GtkDisplayState *s = opaque;
1033 VirtualConsole *vc = gd_vc_find_current(s);
1035 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1036 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1037 } else {
1038 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1040 gd_update_windowsize(vc);
1043 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1044 void *opaque)
1046 VirtualConsole *vc = opaque;
1047 GtkDisplayState *s = vc->s;
1049 gtk_widget_set_sensitive(vc->menu_item, true);
1050 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1051 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1052 vc->tab_item, vc->label);
1053 gtk_widget_destroy(vc->window);
1054 vc->window = NULL;
1055 return TRUE;
1058 static gboolean gd_win_grab(void *opaque)
1060 VirtualConsole *vc = opaque;
1062 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1063 if (vc->s->ptr_owner) {
1064 gd_ungrab_pointer(vc->s);
1065 } else {
1066 gd_grab_pointer(vc);
1068 gd_update_caption(vc->s);
1069 return TRUE;
1072 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1074 GtkDisplayState *s = opaque;
1075 VirtualConsole *vc = gd_vc_find_current(s);
1077 if (vc->type == GD_VC_GFX) {
1078 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1079 FALSE);
1081 if (!vc->window) {
1082 gtk_widget_set_sensitive(vc->menu_item, false);
1083 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1084 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1086 g_signal_connect(vc->window, "delete-event",
1087 G_CALLBACK(gd_tab_window_close), vc);
1088 gtk_widget_show_all(vc->window);
1090 GtkAccelGroup *ag = gtk_accel_group_new();
1091 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1093 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab), vc, NULL);
1094 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1096 gd_update_geometry_hints(vc);
1097 gd_update_caption(s);
1101 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1103 GtkDisplayState *s = opaque;
1104 VirtualConsole *vc = gd_vc_find_current(s);
1106 if (!s->full_screen) {
1107 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1108 gtk_widget_hide(s->menu_bar);
1109 if (vc->type == GD_VC_GFX) {
1110 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1111 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1112 TRUE);
1114 gtk_window_fullscreen(GTK_WINDOW(s->window));
1115 s->full_screen = TRUE;
1116 } else {
1117 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1118 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1119 gtk_widget_show(s->menu_bar);
1120 s->full_screen = FALSE;
1121 if (vc->type == GD_VC_GFX) {
1122 vc->gfx.scale_x = 1.0;
1123 vc->gfx.scale_y = 1.0;
1124 gd_update_windowsize(vc);
1125 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1126 FALSE);
1130 gd_update_cursor(vc);
1133 static void gd_accel_full_screen(void *opaque)
1135 GtkDisplayState *s = opaque;
1136 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1139 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1141 GtkDisplayState *s = opaque;
1142 VirtualConsole *vc = gd_vc_find_current(s);
1144 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1145 FALSE);
1147 vc->gfx.scale_x += VC_SCALE_STEP;
1148 vc->gfx.scale_y += VC_SCALE_STEP;
1150 gd_update_windowsize(vc);
1153 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1155 GtkDisplayState *s = opaque;
1156 VirtualConsole *vc = gd_vc_find_current(s);
1158 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1159 FALSE);
1161 vc->gfx.scale_x -= VC_SCALE_STEP;
1162 vc->gfx.scale_y -= VC_SCALE_STEP;
1164 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1165 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1167 gd_update_windowsize(vc);
1170 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1172 GtkDisplayState *s = opaque;
1173 VirtualConsole *vc = gd_vc_find_current(s);
1175 vc->gfx.scale_x = 1.0;
1176 vc->gfx.scale_y = 1.0;
1178 gd_update_windowsize(vc);
1181 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1183 GtkDisplayState *s = opaque;
1184 VirtualConsole *vc = gd_vc_find_current(s);
1186 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1187 s->free_scale = TRUE;
1188 } else {
1189 s->free_scale = FALSE;
1190 vc->gfx.scale_x = 1.0;
1191 vc->gfx.scale_y = 1.0;
1194 gd_update_windowsize(vc);
1195 gd_update_full_redraw(vc);
1198 #if GTK_CHECK_VERSION(3, 0, 0)
1199 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1200 GdkInputSource source, GdkEventMask mask,
1201 GdkCursor *cursor)
1203 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1204 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1205 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1206 GList *tmp = devs;
1208 for (tmp = devs; tmp; tmp = tmp->next) {
1209 GdkDevice *dev = tmp->data;
1210 if (gdk_device_get_source(dev) != source) {
1211 continue;
1213 if (grab) {
1214 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1215 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1216 mask, cursor, GDK_CURRENT_TIME);
1217 } else {
1218 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1221 g_list_free(devs);
1223 #endif
1225 static void gd_grab_keyboard(VirtualConsole *vc)
1227 #if GTK_CHECK_VERSION(3, 0, 0)
1228 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1229 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1230 NULL);
1231 #else
1232 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1233 FALSE,
1234 GDK_CURRENT_TIME);
1235 #endif
1236 vc->s->kbd_owner = vc;
1237 trace_gd_grab(vc->label, "kbd", true);
1240 static void gd_ungrab_keyboard(GtkDisplayState *s)
1242 VirtualConsole *vc = s->kbd_owner;
1244 if (vc == NULL) {
1245 return;
1247 s->kbd_owner = NULL;
1249 #if GTK_CHECK_VERSION(3, 0, 0)
1250 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1251 #else
1252 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1253 #endif
1254 trace_gd_grab(vc->label, "kbd", false);
1257 static void gd_grab_pointer(VirtualConsole *vc)
1259 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1260 #if GTK_CHECK_VERSION(3, 0, 0)
1261 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1262 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1263 GDK_POINTER_MOTION_MASK |
1264 GDK_BUTTON_PRESS_MASK |
1265 GDK_BUTTON_RELEASE_MASK |
1266 GDK_BUTTON_MOTION_MASK |
1267 GDK_SCROLL_MASK,
1268 vc->s->null_cursor);
1269 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1270 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1271 #else
1272 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1273 FALSE, /* All events to come to our window directly */
1274 GDK_POINTER_MOTION_MASK |
1275 GDK_BUTTON_PRESS_MASK |
1276 GDK_BUTTON_RELEASE_MASK |
1277 GDK_BUTTON_MOTION_MASK |
1278 GDK_SCROLL_MASK,
1279 NULL, /* Allow cursor to move over entire desktop */
1280 vc->s->null_cursor,
1281 GDK_CURRENT_TIME);
1282 gdk_display_get_pointer(display, NULL,
1283 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1284 #endif
1285 vc->s->ptr_owner = vc;
1286 trace_gd_grab(vc->label, "ptr", true);
1289 static void gd_ungrab_pointer(GtkDisplayState *s)
1291 VirtualConsole *vc = s->ptr_owner;
1293 if (vc == NULL) {
1294 return;
1296 s->ptr_owner = NULL;
1298 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1299 #if GTK_CHECK_VERSION(3, 0, 0)
1300 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1301 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1302 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1303 gtk_widget_get_screen(vc->gfx.drawing_area),
1304 vc->s->grab_x_root, vc->s->grab_y_root);
1305 #else
1306 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1307 gdk_display_warp_pointer(display,
1308 gtk_widget_get_screen(vc->gfx.drawing_area),
1309 vc->s->grab_x_root, vc->s->grab_y_root);
1310 #endif
1311 trace_gd_grab(vc->label, "ptr", false);
1314 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1316 GtkDisplayState *s = opaque;
1317 VirtualConsole *vc = gd_vc_find_current(s);
1319 if (gd_is_grab_active(s)) {
1320 if (!gd_grab_on_hover(s)) {
1321 gd_grab_keyboard(vc);
1323 gd_grab_pointer(vc);
1324 } else {
1325 gd_ungrab_keyboard(s);
1326 gd_ungrab_pointer(s);
1329 gd_update_caption(s);
1330 gd_update_cursor(vc);
1333 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1334 gpointer data)
1336 GtkDisplayState *s = data;
1337 VirtualConsole *vc;
1338 gboolean on_vga;
1340 if (!gtk_widget_get_realized(s->notebook)) {
1341 return;
1344 #ifdef VTE_RESIZE_HACK
1345 vc = gd_vc_find_current(s);
1346 if (vc && vc->type == GD_VC_VTE) {
1347 gtk_widget_hide(vc->vte.terminal);
1349 #endif
1350 vc = gd_vc_find_by_page(s, arg2);
1351 if (!vc) {
1352 return;
1354 #ifdef VTE_RESIZE_HACK
1355 if (vc->type == GD_VC_VTE) {
1356 gtk_widget_show(vc->vte.terminal);
1358 #endif
1359 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1360 TRUE);
1361 on_vga = (vc->type == GD_VC_GFX);
1362 if (!on_vga) {
1363 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1364 FALSE);
1365 } else if (s->full_screen) {
1366 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1367 TRUE);
1369 gtk_widget_set_sensitive(s->grab_item, on_vga);
1371 gd_update_windowsize(vc);
1372 gd_update_cursor(vc);
1375 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1376 gpointer opaque)
1378 VirtualConsole *vc = opaque;
1379 GtkDisplayState *s = vc->s;
1381 if (gd_grab_on_hover(s)) {
1382 gd_ungrab_keyboard(s);
1383 gd_grab_keyboard(vc);
1384 gd_update_caption(s);
1386 return TRUE;
1389 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1390 gpointer opaque)
1392 VirtualConsole *vc = opaque;
1393 GtkDisplayState *s = vc->s;
1395 if (gd_grab_on_hover(s)) {
1396 gd_ungrab_keyboard(s);
1397 gd_update_caption(s);
1399 return TRUE;
1402 static gboolean gd_focus_out_event(GtkWidget *widget,
1403 GdkEventCrossing *crossing, gpointer opaque)
1405 VirtualConsole *vc = opaque;
1406 GtkDisplayState *s = vc->s;
1408 gtk_release_modifiers(s);
1409 return TRUE;
1412 /** Virtual Console Callbacks **/
1414 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1415 int idx, GSList *group, GtkWidget *view_menu)
1417 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1418 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1419 HOTKEY_MODIFIERS, 0,
1420 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1421 #if GTK_CHECK_VERSION(3, 8, 0)
1422 gtk_accel_label_set_accel(
1423 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1424 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1425 #endif
1427 g_signal_connect(vc->menu_item, "activate",
1428 G_CALLBACK(gd_menu_switch_vc), s);
1429 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1431 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1432 return group;
1435 #if defined(CONFIG_VTE)
1436 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1438 VirtualConsole *vc = opaque;
1440 if (gtk_adjustment_get_upper(adjustment) >
1441 gtk_adjustment_get_page_size(adjustment)) {
1442 gtk_widget_show(vc->vte.scrollbar);
1443 } else {
1444 gtk_widget_hide(vc->vte.scrollbar);
1448 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1450 VirtualConsole *vc = chr->opaque;
1452 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1453 return len;
1456 static int nb_vcs;
1457 static CharDriverState *vcs[MAX_VCS];
1459 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1461 CharDriverState *chr;
1463 chr = g_malloc0(sizeof(*chr));
1464 chr->chr_write = gd_vc_chr_write;
1465 /* defer OPENED events until our vc is fully initialized */
1466 chr->explicit_be_open = true;
1468 vcs[nb_vcs++] = chr;
1470 return chr;
1473 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1474 gpointer user_data)
1476 VirtualConsole *vc = user_data;
1478 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1479 return TRUE;
1482 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1483 CharDriverState *chr, int idx,
1484 GSList *group, GtkWidget *view_menu)
1486 char buffer[32];
1487 GtkWidget *box;
1488 GtkWidget *scrollbar;
1489 GtkAdjustment *vadjustment;
1491 vc->s = s;
1492 vc->vte.chr = chr;
1494 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1495 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1496 ? vc->vte.chr->label : buffer);
1497 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1499 vc->vte.terminal = vte_terminal_new();
1500 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1502 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1503 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1504 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1506 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1507 vadjustment = gtk_scrollable_get_vadjustment
1508 (GTK_SCROLLABLE(vc->vte.terminal));
1509 #else
1510 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1511 #endif
1513 #if GTK_CHECK_VERSION(3, 0, 0)
1514 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1515 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1516 #else
1517 box = gtk_hbox_new(false, 2);
1518 scrollbar = gtk_vscrollbar_new(vadjustment);
1519 #endif
1521 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1522 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1524 vc->vte.chr->opaque = vc;
1525 vc->vte.box = box;
1526 vc->vte.scrollbar = scrollbar;
1528 g_signal_connect(vadjustment, "changed",
1529 G_CALLBACK(gd_vc_adjustment_changed), vc);
1531 vc->type = GD_VC_VTE;
1532 vc->tab_item = box;
1533 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1534 gtk_label_new(vc->label));
1536 qemu_chr_be_generic_open(vc->vte.chr);
1537 if (vc->vte.chr->init) {
1538 vc->vte.chr->init(vc->vte.chr);
1541 return group;
1544 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1545 GtkWidget *view_menu)
1547 int i;
1549 for (i = 0; i < nb_vcs; i++) {
1550 VirtualConsole *vc = &s->vc[s->nb_vcs];
1551 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1552 s->nb_vcs++;
1555 #endif /* CONFIG_VTE */
1557 /** Window Creation **/
1559 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1561 #if GTK_CHECK_VERSION(3, 0, 0)
1562 g_signal_connect(vc->gfx.drawing_area, "draw",
1563 G_CALLBACK(gd_draw_event), vc);
1564 #else
1565 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1566 G_CALLBACK(gd_expose_event), vc);
1567 #endif
1568 g_signal_connect(vc->gfx.drawing_area, "event",
1569 G_CALLBACK(gd_event), vc);
1570 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1571 G_CALLBACK(gd_button_event), vc);
1572 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1573 G_CALLBACK(gd_button_event), vc);
1574 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1575 G_CALLBACK(gd_scroll_event), vc);
1576 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1577 G_CALLBACK(gd_key_event), vc);
1578 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1579 G_CALLBACK(gd_key_event), vc);
1581 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1582 G_CALLBACK(gd_enter_event), vc);
1583 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1584 G_CALLBACK(gd_leave_event), vc);
1585 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1586 G_CALLBACK(gd_focus_out_event), vc);
1589 static void gd_connect_signals(GtkDisplayState *s)
1591 g_signal_connect(s->show_tabs_item, "activate",
1592 G_CALLBACK(gd_menu_show_tabs), s);
1593 g_signal_connect(s->untabify_item, "activate",
1594 G_CALLBACK(gd_menu_untabify), s);
1596 g_signal_connect(s->window, "delete-event",
1597 G_CALLBACK(gd_window_close), s);
1599 g_signal_connect(s->pause_item, "activate",
1600 G_CALLBACK(gd_menu_pause), s);
1601 g_signal_connect(s->reset_item, "activate",
1602 G_CALLBACK(gd_menu_reset), s);
1603 g_signal_connect(s->powerdown_item, "activate",
1604 G_CALLBACK(gd_menu_powerdown), s);
1605 g_signal_connect(s->quit_item, "activate",
1606 G_CALLBACK(gd_menu_quit), s);
1607 g_signal_connect(s->full_screen_item, "activate",
1608 G_CALLBACK(gd_menu_full_screen), s);
1609 g_signal_connect(s->zoom_in_item, "activate",
1610 G_CALLBACK(gd_menu_zoom_in), s);
1611 g_signal_connect(s->zoom_out_item, "activate",
1612 G_CALLBACK(gd_menu_zoom_out), s);
1613 g_signal_connect(s->zoom_fixed_item, "activate",
1614 G_CALLBACK(gd_menu_zoom_fixed), s);
1615 g_signal_connect(s->zoom_fit_item, "activate",
1616 G_CALLBACK(gd_menu_zoom_fit), s);
1617 g_signal_connect(s->grab_item, "activate",
1618 G_CALLBACK(gd_menu_grab_input), s);
1619 g_signal_connect(s->notebook, "switch-page",
1620 G_CALLBACK(gd_change_page), s);
1623 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1625 GtkWidget *machine_menu;
1626 GtkWidget *separator;
1628 machine_menu = gtk_menu_new();
1629 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1631 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1632 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1634 separator = gtk_separator_menu_item_new();
1635 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1637 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1638 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1640 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1641 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1643 separator = gtk_separator_menu_item_new();
1644 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1646 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1647 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1648 "<QEMU>/Machine/Quit");
1649 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1650 GDK_KEY_q, HOTKEY_MODIFIERS);
1651 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1653 return machine_menu;
1656 static const DisplayChangeListenerOps dcl_ops = {
1657 .dpy_name = "gtk",
1658 .dpy_gfx_update = gd_update,
1659 .dpy_gfx_switch = gd_switch,
1660 .dpy_gfx_check_format = qemu_pixman_check_format,
1661 .dpy_refresh = gd_refresh,
1662 .dpy_mouse_set = gd_mouse_set,
1663 .dpy_cursor_define = gd_cursor_define,
1666 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1667 QemuConsole *con, int idx,
1668 GSList *group, GtkWidget *view_menu)
1670 Object *obj;
1672 obj = object_property_get_link(OBJECT(con), "device", NULL);
1673 if (obj) {
1674 vc->label = g_strdup_printf("%s", object_get_typename(obj));
1675 } else {
1676 vc->label = g_strdup_printf("VGA");
1679 vc->s = s;
1680 vc->gfx.scale_x = 1.0;
1681 vc->gfx.scale_y = 1.0;
1683 vc->gfx.drawing_area = gtk_drawing_area_new();
1684 gtk_widget_add_events(vc->gfx.drawing_area,
1685 GDK_POINTER_MOTION_MASK |
1686 GDK_BUTTON_PRESS_MASK |
1687 GDK_BUTTON_RELEASE_MASK |
1688 GDK_BUTTON_MOTION_MASK |
1689 GDK_ENTER_NOTIFY_MASK |
1690 GDK_LEAVE_NOTIFY_MASK |
1691 GDK_SCROLL_MASK |
1692 GDK_KEY_PRESS_MASK);
1693 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1695 vc->type = GD_VC_GFX;
1696 vc->tab_item = vc->gfx.drawing_area;
1697 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
1698 vc->tab_item, gtk_label_new(vc->label));
1699 gd_connect_vc_gfx_signals(vc);
1701 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1703 vc->gfx.dcl.ops = &dcl_ops;
1704 vc->gfx.dcl.con = con;
1705 register_displaychangelistener(&vc->gfx.dcl);
1707 return group;
1710 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
1712 GSList *group = NULL;
1713 GtkWidget *view_menu;
1714 GtkWidget *separator;
1715 QemuConsole *con;
1716 int vc;
1718 view_menu = gtk_menu_new();
1719 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
1721 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1723 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
1724 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
1725 #if GTK_CHECK_VERSION(3, 8, 0)
1726 gtk_accel_label_set_accel(
1727 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
1728 GDK_KEY_f, HOTKEY_MODIFIERS);
1729 #endif
1730 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1732 separator = gtk_separator_menu_item_new();
1733 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1735 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1736 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1737 "<QEMU>/View/Zoom In");
1738 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1739 HOTKEY_MODIFIERS);
1740 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1742 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1743 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1744 "<QEMU>/View/Zoom Out");
1745 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1746 HOTKEY_MODIFIERS);
1747 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1749 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1750 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1751 "<QEMU>/View/Zoom Fixed");
1752 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1753 HOTKEY_MODIFIERS);
1754 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1756 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1757 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1759 separator = gtk_separator_menu_item_new();
1760 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1762 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1763 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1765 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1766 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1767 "<QEMU>/View/Grab Input");
1768 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1769 HOTKEY_MODIFIERS);
1770 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1772 separator = gtk_separator_menu_item_new();
1773 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1775 /* gfx */
1776 for (vc = 0;; vc++) {
1777 con = qemu_console_lookup_by_index(vc);
1778 if (!con || !qemu_console_is_graphic(con)) {
1779 break;
1781 group = gd_vc_gfx_init(s, &s->vc[vc], con,
1782 vc, group, view_menu);
1783 s->nb_vcs++;
1786 #if defined(CONFIG_VTE)
1787 /* vte */
1788 gd_vcs_init(s, group, view_menu);
1789 #endif
1791 separator = gtk_separator_menu_item_new();
1792 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1794 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1795 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1797 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1798 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
1800 return view_menu;
1803 static void gd_create_menus(GtkDisplayState *s)
1805 s->accel_group = gtk_accel_group_new();
1806 s->machine_menu = gd_create_menu_machine(s);
1807 s->view_menu = gd_create_menu_view(s);
1809 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1810 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1811 s->machine_menu);
1812 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1814 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1815 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1816 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1818 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
1819 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
1822 static void gd_set_keycode_type(GtkDisplayState *s)
1824 #ifdef GDK_WINDOWING_X11
1825 GdkDisplay *display = gtk_widget_get_display(s->window);
1826 if (GDK_IS_X11_DISPLAY(display)) {
1827 Display *x11_display = gdk_x11_display_get_xdisplay(display);
1828 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
1829 XkbUseCoreKbd);
1830 char *keycodes = NULL;
1832 if (desc && desc->names) {
1833 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
1835 if (keycodes == NULL) {
1836 fprintf(stderr, "could not lookup keycode name\n");
1837 } else if (strstart(keycodes, "evdev", NULL)) {
1838 s->has_evdev = true;
1839 } else if (!strstart(keycodes, "xfree86", NULL)) {
1840 fprintf(stderr, "unknown keycodes `%s', please report to "
1841 "qemu-devel@nongnu.org\n", keycodes);
1844 if (desc) {
1845 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
1847 if (keycodes) {
1848 XFree(keycodes);
1851 #endif
1854 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
1856 GtkDisplayState *s = g_malloc0(sizeof(*s));
1857 char *filename;
1859 gtk_init(NULL, NULL);
1861 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1862 #if GTK_CHECK_VERSION(3, 2, 0)
1863 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1864 #else
1865 s->vbox = gtk_vbox_new(FALSE, 0);
1866 #endif
1867 s->notebook = gtk_notebook_new();
1868 s->menu_bar = gtk_menu_bar_new();
1870 s->free_scale = FALSE;
1872 setlocale(LC_ALL, "");
1873 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1874 textdomain("qemu");
1876 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1878 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1879 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1880 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1882 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1883 if (filename) {
1884 GError *error = NULL;
1885 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1886 if (pixbuf) {
1887 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1888 } else {
1889 g_error_free(error);
1891 g_free(filename);
1894 gd_create_menus(s);
1896 gd_connect_signals(s);
1898 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1899 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1901 gd_update_caption(s);
1903 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1904 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1906 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1908 gtk_widget_show_all(s->window);
1910 #ifdef VTE_RESIZE_HACK
1912 VirtualConsole *cur = gd_vc_find_current(s);
1913 if (cur) {
1914 int i;
1916 for (i = 0; i < s->nb_vcs; i++) {
1917 VirtualConsole *vc = &s->vc[i];
1918 if (vc && vc->type == GD_VC_VTE && vc != cur) {
1919 gtk_widget_hide(vc->vte.terminal);
1922 gd_update_windowsize(cur);
1925 #endif
1927 if (full_screen) {
1928 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1930 if (grab_on_hover) {
1931 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1934 gd_set_keycode_type(s);
1937 void early_gtk_display_init(void)
1939 #if defined(CONFIG_VTE)
1940 register_vc_handler(gd_vc_handler);
1941 #endif