block/iscsi: use 16 byte CDBs only when necessary
[qemu/ar7.git] / ui / gtk.c
blobb02fcd695909d145d6f77ad0789087482f4dc7ee
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 #endif
133 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
135 static const int modifier_keycode[] = {
136 /* shift, control, alt keys, meta keys, both left & right */
137 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
140 typedef struct GtkDisplayState GtkDisplayState;
142 typedef struct VirtualGfxConsole {
143 GtkWidget *drawing_area;
144 DisplayChangeListener dcl;
145 DisplaySurface *ds;
146 pixman_image_t *convert;
147 cairo_surface_t *surface;
148 double scale_x;
149 double scale_y;
150 } VirtualGfxConsole;
152 #if defined(CONFIG_VTE)
153 typedef struct VirtualVteConsole {
154 GtkWidget *box;
155 GtkWidget *scrollbar;
156 GtkWidget *terminal;
157 CharDriverState *chr;
158 } VirtualVteConsole;
159 #endif
161 typedef enum VirtualConsoleType {
162 GD_VC_GFX,
163 GD_VC_VTE,
164 } VirtualConsoleType;
166 typedef struct VirtualConsole {
167 GtkDisplayState *s;
168 char *label;
169 GtkWidget *window;
170 GtkWidget *menu_item;
171 GtkWidget *tab_item;
172 VirtualConsoleType type;
173 union {
174 VirtualGfxConsole gfx;
175 #if defined(CONFIG_VTE)
176 VirtualVteConsole vte;
177 #endif
179 } VirtualConsole;
181 struct GtkDisplayState {
182 GtkWidget *window;
184 GtkWidget *menu_bar;
186 GtkAccelGroup *accel_group;
188 GtkWidget *machine_menu_item;
189 GtkWidget *machine_menu;
190 GtkWidget *pause_item;
191 GtkWidget *reset_item;
192 GtkWidget *powerdown_item;
193 GtkWidget *quit_item;
195 GtkWidget *view_menu_item;
196 GtkWidget *view_menu;
197 GtkWidget *full_screen_item;
198 GtkWidget *zoom_in_item;
199 GtkWidget *zoom_out_item;
200 GtkWidget *zoom_fixed_item;
201 GtkWidget *zoom_fit_item;
202 GtkWidget *grab_item;
203 GtkWidget *grab_on_hover_item;
205 int nb_vcs;
206 VirtualConsole vc[MAX_VCS];
208 GtkWidget *show_tabs_item;
209 GtkWidget *untabify_item;
211 GtkWidget *vbox;
212 GtkWidget *notebook;
213 int button_mask;
214 gboolean last_set;
215 int last_x;
216 int last_y;
217 int grab_x_root;
218 int grab_y_root;
219 VirtualConsole *kbd_owner;
220 VirtualConsole *ptr_owner;
222 gboolean full_screen;
224 GdkCursor *null_cursor;
225 Notifier mouse_mode_notifier;
226 gboolean free_scale;
228 bool external_pause_update;
230 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
231 bool has_evdev;
234 static void gd_grab_pointer(VirtualConsole *vc);
235 static void gd_ungrab_pointer(GtkDisplayState *s);
237 /** Utility Functions **/
239 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
241 VirtualConsole *vc;
242 gint i;
244 for (i = 0; i < s->nb_vcs; i++) {
245 vc = &s->vc[i];
246 if (gtk_check_menu_item_get_active
247 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
248 return vc;
251 return NULL;
254 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
256 VirtualConsole *vc;
257 gint i, p;
259 for (i = 0; i < s->nb_vcs; i++) {
260 vc = &s->vc[i];
261 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
262 if (p == page) {
263 return vc;
266 return NULL;
269 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
271 gint page;
273 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
274 return gd_vc_find_by_page(s, page);
277 static bool gd_is_grab_active(GtkDisplayState *s)
279 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
282 static bool gd_grab_on_hover(GtkDisplayState *s)
284 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
287 static void gd_update_cursor(VirtualConsole *vc)
289 GtkDisplayState *s = vc->s;
290 GdkWindow *window;
292 if (vc->type != GD_VC_GFX) {
293 return;
296 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
297 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
298 gdk_window_set_cursor(window, s->null_cursor);
299 } else {
300 gdk_window_set_cursor(window, NULL);
304 static void gd_update_caption(GtkDisplayState *s)
306 const char *status = "";
307 gchar *prefix;
308 gchar *title;
309 const char *grab = "";
310 bool is_paused = !runstate_is_running();
311 int i;
313 if (qemu_name) {
314 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
315 } else {
316 prefix = g_strdup_printf("QEMU");
319 if (s->ptr_owner != NULL &&
320 s->ptr_owner->window == NULL) {
321 grab = _(" - Press Ctrl+Alt+G to release grab");
324 if (is_paused) {
325 status = _(" [Paused]");
327 s->external_pause_update = true;
328 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
329 is_paused);
330 s->external_pause_update = false;
332 title = g_strdup_printf("%s%s%s", prefix, status, grab);
333 gtk_window_set_title(GTK_WINDOW(s->window), title);
334 g_free(title);
336 for (i = 0; i < s->nb_vcs; i++) {
337 VirtualConsole *vc = &s->vc[i];
339 if (!vc->window) {
340 continue;
342 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
343 vc == s->kbd_owner ? " +kbd" : "",
344 vc == s->ptr_owner ? " +ptr" : "");
345 gtk_window_set_title(GTK_WINDOW(vc->window), title);
346 g_free(title);
349 g_free(prefix);
352 static void gd_update_geometry_hints(VirtualConsole *vc)
354 GtkDisplayState *s = vc->s;
355 GdkWindowHints mask = 0;
356 GdkGeometry geo = {};
357 GtkWidget *geo_widget = NULL;
358 GtkWindow *geo_window;
360 if (vc->type == GD_VC_GFX) {
361 if (s->free_scale) {
362 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
363 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
364 mask |= GDK_HINT_MIN_SIZE;
365 } else {
366 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
367 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
368 mask |= GDK_HINT_MIN_SIZE;
370 geo_widget = vc->gfx.drawing_area;
371 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
373 #if defined(CONFIG_VTE)
374 } else if (vc->type == GD_VC_VTE) {
375 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
376 GtkBorder *ib;
378 geo.width_inc = vte_terminal_get_char_width(term);
379 geo.height_inc = vte_terminal_get_char_height(term);
380 mask |= GDK_HINT_RESIZE_INC;
381 geo.base_width = geo.width_inc;
382 geo.base_height = geo.height_inc;
383 mask |= GDK_HINT_BASE_SIZE;
384 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
385 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
386 mask |= GDK_HINT_MIN_SIZE;
387 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
388 geo.base_width += ib->left + ib->right;
389 geo.base_height += ib->top + ib->bottom;
390 geo.min_width += ib->left + ib->right;
391 geo.min_height += ib->top + ib->bottom;
392 geo_widget = vc->vte.terminal;
393 #endif
396 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
397 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
400 static void gd_update_windowsize(VirtualConsole *vc)
402 GtkDisplayState *s = vc->s;
404 gd_update_geometry_hints(vc);
406 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
407 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
408 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
412 static void gd_update_full_redraw(VirtualConsole *vc)
414 GtkWidget *area = vc->gfx.drawing_area;
415 int ww, wh;
416 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
417 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
420 static void gtk_release_modifiers(GtkDisplayState *s)
422 VirtualConsole *vc = gd_vc_find_current(s);
423 int i, keycode;
425 if (vc->type != GD_VC_GFX) {
426 return;
428 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
429 keycode = modifier_keycode[i];
430 if (!s->modifier_pressed[i]) {
431 continue;
433 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
434 s->modifier_pressed[i] = false;
438 /** DisplayState Callbacks **/
440 static void gd_update(DisplayChangeListener *dcl,
441 int x, int y, int w, int h)
443 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
444 int x1, x2, y1, y2;
445 int mx, my;
446 int fbw, fbh;
447 int ww, wh;
449 trace_gd_update(vc->label, x, y, w, h);
451 if (vc->gfx.convert) {
452 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
453 NULL, vc->gfx.convert,
454 x, y, 0, 0, x, y, w, h);
457 x1 = floor(x * vc->gfx.scale_x);
458 y1 = floor(y * vc->gfx.scale_y);
460 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
461 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
463 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
464 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
466 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
467 &ww, &wh);
469 mx = my = 0;
470 if (ww > fbw) {
471 mx = (ww - fbw) / 2;
473 if (wh > fbh) {
474 my = (wh - fbh) / 2;
477 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
478 mx + x1, my + y1, (x2 - x1), (y2 - y1));
481 static void gd_refresh(DisplayChangeListener *dcl)
483 graphic_hw_update(dcl->con);
486 #if GTK_CHECK_VERSION(3, 0, 0)
487 static void gd_mouse_set(DisplayChangeListener *dcl,
488 int x, int y, int visible)
490 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
491 GdkDisplay *dpy;
492 GdkDeviceManager *mgr;
493 gint x_root, y_root;
495 if (qemu_input_is_absolute()) {
496 return;
499 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
500 mgr = gdk_display_get_device_manager(dpy);
501 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
502 x, y, &x_root, &y_root);
503 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
504 gtk_widget_get_screen(vc->gfx.drawing_area),
505 x_root, y_root);
507 #else
508 static void gd_mouse_set(DisplayChangeListener *dcl,
509 int x, int y, int visible)
511 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
512 gint x_root, y_root;
514 if (qemu_input_is_absolute()) {
515 return;
518 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
519 x, y, &x_root, &y_root);
520 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
521 gtk_widget_get_screen(vc->gfx.drawing_area),
522 x_root, y_root);
524 #endif
526 static void gd_cursor_define(DisplayChangeListener *dcl,
527 QEMUCursor *c)
529 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
530 GdkPixbuf *pixbuf;
531 GdkCursor *cursor;
533 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
534 GDK_COLORSPACE_RGB, true, 8,
535 c->width, c->height, c->width * 4,
536 NULL, NULL);
537 cursor = gdk_cursor_new_from_pixbuf
538 (gtk_widget_get_display(vc->gfx.drawing_area),
539 pixbuf, c->hot_x, c->hot_y);
540 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
541 g_object_unref(pixbuf);
542 #if !GTK_CHECK_VERSION(3, 0, 0)
543 gdk_cursor_unref(cursor);
544 #else
545 g_object_unref(cursor);
546 #endif
549 static void gd_switch(DisplayChangeListener *dcl,
550 DisplaySurface *surface)
552 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
553 bool resized = true;
555 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
557 if (vc->gfx.surface) {
558 cairo_surface_destroy(vc->gfx.surface);
561 if (vc->gfx.ds &&
562 surface_width(vc->gfx.ds) == surface_width(surface) &&
563 surface_height(vc->gfx.ds) == surface_height(surface)) {
564 resized = false;
566 vc->gfx.ds = surface;
568 if (vc->gfx.convert) {
569 pixman_image_unref(vc->gfx.convert);
570 vc->gfx.convert = NULL;
573 if (surface->format == PIXMAN_x8r8g8b8) {
575 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
577 * No need to convert, use surface directly. Should be the
578 * common case as this is qemu_default_pixelformat(32) too.
580 vc->gfx.surface = cairo_image_surface_create_for_data
581 (surface_data(surface),
582 CAIRO_FORMAT_RGB24,
583 surface_width(surface),
584 surface_height(surface),
585 surface_stride(surface));
586 } else {
587 /* Must convert surface, use pixman to do it. */
588 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
589 surface_width(surface),
590 surface_height(surface),
591 NULL, 0);
592 vc->gfx.surface = cairo_image_surface_create_for_data
593 ((void *)pixman_image_get_data(vc->gfx.convert),
594 CAIRO_FORMAT_RGB24,
595 pixman_image_get_width(vc->gfx.convert),
596 pixman_image_get_height(vc->gfx.convert),
597 pixman_image_get_stride(vc->gfx.convert));
598 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
599 NULL, vc->gfx.convert,
600 0, 0, 0, 0, 0, 0,
601 pixman_image_get_width(vc->gfx.convert),
602 pixman_image_get_height(vc->gfx.convert));
605 if (resized) {
606 gd_update_windowsize(vc);
607 } else {
608 gd_update_full_redraw(vc);
612 /** QEMU Events **/
614 static void gd_change_runstate(void *opaque, int running, RunState state)
616 GtkDisplayState *s = opaque;
618 gd_update_caption(s);
621 static void gd_mouse_mode_change(Notifier *notify, void *data)
623 GtkDisplayState *s;
624 int i;
626 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
627 /* release the grab at switching to absolute mode */
628 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
629 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
630 FALSE);
632 for (i = 0; i < s->nb_vcs; i++) {
633 VirtualConsole *vc = &s->vc[i];
634 gd_update_cursor(vc);
638 /** GTK Events **/
640 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
641 void *opaque)
643 GtkDisplayState *s = opaque;
644 int i;
646 if (!no_quit) {
647 for (i = 0; i < s->nb_vcs; i++) {
648 if (s->vc[i].type != GD_VC_GFX) {
649 continue;
651 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
653 qmp_quit(NULL);
654 return FALSE;
657 return TRUE;
660 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
662 VirtualConsole *vc = opaque;
663 GtkDisplayState *s = vc->s;
664 int mx, my;
665 int ww, wh;
666 int fbw, fbh;
668 if (!gtk_widget_get_realized(widget)) {
669 return FALSE;
672 fbw = surface_width(vc->gfx.ds);
673 fbh = surface_height(vc->gfx.ds);
675 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
677 if (s->full_screen) {
678 vc->gfx.scale_x = (double)ww / fbw;
679 vc->gfx.scale_y = (double)wh / fbh;
680 } else if (s->free_scale) {
681 double sx, sy;
683 sx = (double)ww / fbw;
684 sy = (double)wh / fbh;
686 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
689 fbw *= vc->gfx.scale_x;
690 fbh *= vc->gfx.scale_y;
692 mx = my = 0;
693 if (ww > fbw) {
694 mx = (ww - fbw) / 2;
696 if (wh > fbh) {
697 my = (wh - fbh) / 2;
700 cairo_rectangle(cr, 0, 0, ww, wh);
702 /* Optionally cut out the inner area where the pixmap
703 will be drawn. This avoids 'flashing' since we're
704 not double-buffering. Note we're using the undocumented
705 behaviour of drawing the rectangle from right to left
706 to cut out the whole */
707 cairo_rectangle(cr, mx + fbw, my,
708 -1 * fbw, fbh);
709 cairo_fill(cr);
711 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
712 cairo_set_source_surface(cr, vc->gfx.surface,
713 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
714 cairo_paint(cr);
716 return TRUE;
719 #if !GTK_CHECK_VERSION(3, 0, 0)
720 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
721 void *opaque)
723 cairo_t *cr;
724 gboolean ret;
726 cr = gdk_cairo_create(gtk_widget_get_window(widget));
727 cairo_rectangle(cr,
728 expose->area.x,
729 expose->area.y,
730 expose->area.width,
731 expose->area.height);
732 cairo_clip(cr);
734 ret = gd_draw_event(widget, cr, opaque);
736 cairo_destroy(cr);
738 return ret;
740 #endif
742 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
743 void *opaque)
745 VirtualConsole *vc = opaque;
746 GtkDisplayState *s = vc->s;
747 int x, y;
748 int mx, my;
749 int fbh, fbw;
750 int ww, wh;
752 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
753 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
755 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
756 &ww, &wh);
758 mx = my = 0;
759 if (ww > fbw) {
760 mx = (ww - fbw) / 2;
762 if (wh > fbh) {
763 my = (wh - fbh) / 2;
766 x = (motion->x - mx) / vc->gfx.scale_x;
767 y = (motion->y - my) / vc->gfx.scale_y;
769 if (qemu_input_is_absolute()) {
770 if (x < 0 || y < 0 ||
771 x >= surface_width(vc->gfx.ds) ||
772 y >= surface_height(vc->gfx.ds)) {
773 return TRUE;
775 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
776 surface_width(vc->gfx.ds));
777 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
778 surface_height(vc->gfx.ds));
779 qemu_input_event_sync();
780 } else if (s->last_set && s->ptr_owner == vc) {
781 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
782 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
783 qemu_input_event_sync();
785 s->last_x = x;
786 s->last_y = y;
787 s->last_set = TRUE;
789 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
790 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
791 int x = (int)motion->x_root;
792 int y = (int)motion->y_root;
794 /* In relative mode check to see if client pointer hit
795 * one of the screen edges, and if so move it back by
796 * 200 pixels. This is important because the pointer
797 * in the server doesn't correspond 1-for-1, and so
798 * may still be only half way across the screen. Without
799 * this warp, the server pointer would thus appear to hit
800 * an invisible wall */
801 if (x == 0) {
802 x += 200;
804 if (y == 0) {
805 y += 200;
807 if (x == (gdk_screen_get_width(screen) - 1)) {
808 x -= 200;
810 if (y == (gdk_screen_get_height(screen) - 1)) {
811 y -= 200;
814 if (x != (int)motion->x_root || y != (int)motion->y_root) {
815 #if GTK_CHECK_VERSION(3, 0, 0)
816 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
817 gdk_device_warp(dev, screen, x, y);
818 #else
819 GdkDisplay *display = gtk_widget_get_display(widget);
820 gdk_display_warp_pointer(display, screen, x, y);
821 #endif
822 s->last_set = FALSE;
823 return FALSE;
826 return TRUE;
829 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
830 void *opaque)
832 VirtualConsole *vc = opaque;
833 GtkDisplayState *s = vc->s;
834 InputButton btn;
836 /* implicitly grab the input at the first click in the relative mode */
837 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
838 !qemu_input_is_absolute() && s->ptr_owner != vc) {
839 gd_ungrab_pointer(s);
840 if (!vc->window) {
841 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
842 TRUE);
843 } else {
844 gd_grab_pointer(vc);
845 gd_update_caption(s);
847 return TRUE;
850 if (button->button == 1) {
851 btn = INPUT_BUTTON_LEFT;
852 } else if (button->button == 2) {
853 btn = INPUT_BUTTON_MIDDLE;
854 } else if (button->button == 3) {
855 btn = INPUT_BUTTON_RIGHT;
856 } else {
857 return TRUE;
860 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
861 button->type == GDK_BUTTON_PRESS);
862 qemu_input_event_sync();
863 return TRUE;
866 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
867 void *opaque)
869 VirtualConsole *vc = opaque;
870 InputButton btn;
872 if (scroll->direction == GDK_SCROLL_UP) {
873 btn = INPUT_BUTTON_WHEEL_UP;
874 } else if (scroll->direction == GDK_SCROLL_DOWN) {
875 btn = INPUT_BUTTON_WHEEL_DOWN;
876 } else {
877 return TRUE;
880 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
881 qemu_input_event_sync();
882 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
883 qemu_input_event_sync();
884 return TRUE;
887 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
889 int qemu_keycode;
891 #ifdef GDK_WINDOWING_WIN32
892 if (GDK_IS_WIN32_DISPLAY(dpy)) {
893 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
894 switch (qemu_keycode) {
895 case 103: /* alt gr */
896 qemu_keycode = 56 | SCANCODE_GREY;
897 break;
899 return qemu_keycode;
901 #endif
903 if (gdk_keycode < 9) {
904 qemu_keycode = 0;
905 } else if (gdk_keycode < 97) {
906 qemu_keycode = gdk_keycode - 8;
907 #ifdef GDK_WINDOWING_X11
908 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
909 if (s->has_evdev) {
910 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
911 } else {
912 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
914 #endif
915 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
916 qemu_keycode = 0x70;
917 } else if (gdk_keycode == 211) { /* backslash */
918 qemu_keycode = 0x73;
919 } else {
920 qemu_keycode = 0;
923 return qemu_keycode;
926 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
928 VirtualConsole *vc = opaque;
929 GtkDisplayState *s = vc->s;
930 int gdk_keycode = key->hardware_keycode;
931 int qemu_keycode;
932 int i;
934 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
935 gdk_keycode);
937 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
938 (key->type == GDK_KEY_PRESS) ? "down" : "up");
940 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
941 if (qemu_keycode == modifier_keycode[i]) {
942 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
946 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
947 key->type == GDK_KEY_PRESS);
949 return TRUE;
952 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
954 if (event->type == GDK_MOTION_NOTIFY) {
955 return gd_motion_event(widget, &event->motion, opaque);
957 return FALSE;
960 /** Window Menu Actions **/
962 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
964 GtkDisplayState *s = opaque;
966 if (s->external_pause_update) {
967 return;
969 if (runstate_is_running()) {
970 qmp_stop(NULL);
971 } else {
972 qmp_cont(NULL);
976 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
978 qmp_system_reset(NULL);
981 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
983 qmp_system_powerdown(NULL);
986 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
988 qmp_quit(NULL);
991 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
993 GtkDisplayState *s = opaque;
994 VirtualConsole *vc = gd_vc_find_by_menu(s);
995 gint page;
997 gtk_release_modifiers(s);
998 if (vc) {
999 page = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook),
1000 vc->tab_item);
1001 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), page);
1005 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1007 GtkDisplayState *s = opaque;
1008 VirtualConsole *vc = gd_vc_find_current(s);
1010 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1011 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1012 } else {
1013 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1015 gd_update_windowsize(vc);
1018 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1019 void *opaque)
1021 VirtualConsole *vc = opaque;
1022 GtkDisplayState *s = vc->s;
1024 gtk_widget_set_sensitive(vc->menu_item, true);
1025 gtk_widget_reparent(vc->tab_item, s->notebook);
1026 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1027 vc->tab_item, vc->label);
1028 gtk_widget_destroy(vc->window);
1029 vc->window = NULL;
1030 return TRUE;
1033 static gboolean gd_win_grab(void *opaque)
1035 VirtualConsole *vc = opaque;
1037 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1038 if (vc->s->ptr_owner) {
1039 gd_ungrab_pointer(vc->s);
1040 } else {
1041 gd_grab_pointer(vc);
1043 gd_update_caption(vc->s);
1044 return TRUE;
1047 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1049 GtkDisplayState *s = opaque;
1050 VirtualConsole *vc = gd_vc_find_current(s);
1052 if (vc->type == GD_VC_GFX) {
1053 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1054 FALSE);
1056 if (!vc->window) {
1057 gtk_widget_set_sensitive(vc->menu_item, false);
1058 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1059 gtk_widget_reparent(vc->tab_item, vc->window);
1061 g_signal_connect(vc->window, "delete-event",
1062 G_CALLBACK(gd_tab_window_close), vc);
1063 gtk_widget_show_all(vc->window);
1065 GtkAccelGroup *ag = gtk_accel_group_new();
1066 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1068 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab), vc, NULL);
1069 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1071 gd_update_geometry_hints(vc);
1072 gd_update_caption(s);
1076 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1078 GtkDisplayState *s = opaque;
1079 VirtualConsole *vc = gd_vc_find_current(s);
1081 if (!s->full_screen) {
1082 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1083 gtk_widget_set_size_request(s->menu_bar, 0, 0);
1084 if (vc->type == GD_VC_GFX) {
1085 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1086 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1087 TRUE);
1089 gtk_window_fullscreen(GTK_WINDOW(s->window));
1090 s->full_screen = TRUE;
1091 } else {
1092 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1093 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1094 gtk_widget_set_size_request(s->menu_bar, -1, -1);
1095 s->full_screen = FALSE;
1096 if (vc->type == GD_VC_GFX) {
1097 vc->gfx.scale_x = 1.0;
1098 vc->gfx.scale_y = 1.0;
1099 gd_update_windowsize(vc);
1100 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1101 FALSE);
1105 gd_update_cursor(vc);
1108 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1110 GtkDisplayState *s = opaque;
1111 VirtualConsole *vc = gd_vc_find_current(s);
1113 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1114 FALSE);
1116 vc->gfx.scale_x += VC_SCALE_STEP;
1117 vc->gfx.scale_y += VC_SCALE_STEP;
1119 gd_update_windowsize(vc);
1122 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1124 GtkDisplayState *s = opaque;
1125 VirtualConsole *vc = gd_vc_find_current(s);
1127 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1128 FALSE);
1130 vc->gfx.scale_x -= VC_SCALE_STEP;
1131 vc->gfx.scale_y -= VC_SCALE_STEP;
1133 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1134 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1136 gd_update_windowsize(vc);
1139 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1141 GtkDisplayState *s = opaque;
1142 VirtualConsole *vc = gd_vc_find_current(s);
1144 vc->gfx.scale_x = 1.0;
1145 vc->gfx.scale_y = 1.0;
1147 gd_update_windowsize(vc);
1150 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1152 GtkDisplayState *s = opaque;
1153 VirtualConsole *vc = gd_vc_find_current(s);
1155 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1156 s->free_scale = TRUE;
1157 } else {
1158 s->free_scale = FALSE;
1159 vc->gfx.scale_x = 1.0;
1160 vc->gfx.scale_y = 1.0;
1163 gd_update_windowsize(vc);
1164 gd_update_full_redraw(vc);
1167 #if GTK_CHECK_VERSION(3, 0, 0)
1168 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1169 GdkInputSource source, GdkEventMask mask,
1170 GdkCursor *cursor)
1172 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1173 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1174 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1175 GList *tmp = devs;
1177 for (tmp = devs; tmp; tmp = tmp->next) {
1178 GdkDevice *dev = tmp->data;
1179 if (gdk_device_get_source(dev) != source) {
1180 continue;
1182 if (grab) {
1183 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1184 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1185 mask, cursor, GDK_CURRENT_TIME);
1186 } else {
1187 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1190 g_list_free(devs);
1192 #endif
1194 static void gd_grab_keyboard(VirtualConsole *vc)
1196 #if GTK_CHECK_VERSION(3, 0, 0)
1197 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1198 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1199 NULL);
1200 #else
1201 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1202 FALSE,
1203 GDK_CURRENT_TIME);
1204 #endif
1205 vc->s->kbd_owner = vc;
1206 trace_gd_grab(vc->label, "kbd", true);
1209 static void gd_ungrab_keyboard(GtkDisplayState *s)
1211 VirtualConsole *vc = s->kbd_owner;
1213 if (vc == NULL) {
1214 return;
1216 s->kbd_owner = NULL;
1218 #if GTK_CHECK_VERSION(3, 0, 0)
1219 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1220 #else
1221 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1222 #endif
1223 trace_gd_grab(vc->label, "kbd", false);
1226 static void gd_grab_pointer(VirtualConsole *vc)
1228 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1229 #if GTK_CHECK_VERSION(3, 0, 0)
1230 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1231 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1232 GDK_POINTER_MOTION_MASK |
1233 GDK_BUTTON_PRESS_MASK |
1234 GDK_BUTTON_RELEASE_MASK |
1235 GDK_BUTTON_MOTION_MASK |
1236 GDK_SCROLL_MASK,
1237 vc->s->null_cursor);
1238 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1239 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1240 #else
1241 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1242 FALSE, /* All events to come to our window directly */
1243 GDK_POINTER_MOTION_MASK |
1244 GDK_BUTTON_PRESS_MASK |
1245 GDK_BUTTON_RELEASE_MASK |
1246 GDK_BUTTON_MOTION_MASK |
1247 GDK_SCROLL_MASK,
1248 NULL, /* Allow cursor to move over entire desktop */
1249 vc->s->null_cursor,
1250 GDK_CURRENT_TIME);
1251 gdk_display_get_pointer(display, NULL,
1252 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1253 #endif
1254 vc->s->ptr_owner = vc;
1255 trace_gd_grab(vc->label, "ptr", true);
1258 static void gd_ungrab_pointer(GtkDisplayState *s)
1260 VirtualConsole *vc = s->ptr_owner;
1262 if (vc == NULL) {
1263 return;
1265 s->ptr_owner = NULL;
1267 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1268 #if GTK_CHECK_VERSION(3, 0, 0)
1269 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1270 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1271 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1272 gtk_widget_get_screen(vc->gfx.drawing_area),
1273 vc->s->grab_x_root, vc->s->grab_y_root);
1274 #else
1275 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1276 gdk_display_warp_pointer(display,
1277 gtk_widget_get_screen(vc->gfx.drawing_area),
1278 vc->s->grab_x_root, vc->s->grab_y_root);
1279 #endif
1280 trace_gd_grab(vc->label, "ptr", false);
1283 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1285 GtkDisplayState *s = opaque;
1286 VirtualConsole *vc = gd_vc_find_current(s);
1288 if (gd_is_grab_active(s)) {
1289 if (!gd_grab_on_hover(s)) {
1290 gd_grab_keyboard(vc);
1292 gd_grab_pointer(vc);
1293 } else {
1294 gd_ungrab_keyboard(s);
1295 gd_ungrab_pointer(s);
1298 gd_update_caption(s);
1299 gd_update_cursor(vc);
1302 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1303 gpointer data)
1305 GtkDisplayState *s = data;
1306 VirtualConsole *vc;
1307 gboolean on_vga;
1309 if (!gtk_widget_get_realized(s->notebook)) {
1310 return;
1313 #ifdef VTE_RESIZE_HACK
1314 vc = gd_vc_find_current(s);
1315 if (vc && vc->type == GD_VC_VTE) {
1316 gtk_widget_hide(vc->vte.terminal);
1318 #endif
1319 vc = gd_vc_find_by_page(s, arg2);
1320 if (!vc) {
1321 return;
1323 #ifdef VTE_RESIZE_HACK
1324 if (vc->type == GD_VC_VTE) {
1325 gtk_widget_show(vc->vte.terminal);
1327 #endif
1328 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1329 TRUE);
1330 on_vga = (vc->type == GD_VC_GFX);
1331 if (!on_vga) {
1332 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1333 FALSE);
1334 } else if (s->full_screen) {
1335 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1336 TRUE);
1338 gtk_widget_set_sensitive(s->grab_item, on_vga);
1340 gd_update_windowsize(vc);
1341 gd_update_cursor(vc);
1344 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1345 gpointer opaque)
1347 VirtualConsole *vc = opaque;
1348 GtkDisplayState *s = vc->s;
1350 if (gd_grab_on_hover(s)) {
1351 gd_ungrab_keyboard(s);
1352 gd_grab_keyboard(vc);
1353 gd_update_caption(s);
1355 return TRUE;
1358 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1359 gpointer opaque)
1361 VirtualConsole *vc = opaque;
1362 GtkDisplayState *s = vc->s;
1364 if (gd_grab_on_hover(s)) {
1365 gd_ungrab_keyboard(s);
1366 gd_update_caption(s);
1368 return TRUE;
1371 static gboolean gd_focus_out_event(GtkWidget *widget,
1372 GdkEventCrossing *crossing, gpointer opaque)
1374 VirtualConsole *vc = opaque;
1375 GtkDisplayState *s = vc->s;
1377 gtk_release_modifiers(s);
1378 return TRUE;
1381 /** Virtual Console Callbacks **/
1383 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1384 int idx, GSList *group, GtkWidget *view_menu)
1386 char path[32];
1388 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", idx);
1390 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1391 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1392 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1393 gtk_accel_map_add_entry(path, GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1395 g_signal_connect(vc->menu_item, "activate",
1396 G_CALLBACK(gd_menu_switch_vc), s);
1397 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1399 return group;
1402 #if defined(CONFIG_VTE)
1403 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1405 VirtualConsole *vc = opaque;
1407 if (gtk_adjustment_get_upper(adjustment) >
1408 gtk_adjustment_get_page_size(adjustment)) {
1409 gtk_widget_show(vc->vte.scrollbar);
1410 } else {
1411 gtk_widget_hide(vc->vte.scrollbar);
1415 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1417 VirtualConsole *vc = chr->opaque;
1419 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1420 return len;
1423 static int nb_vcs;
1424 static CharDriverState *vcs[MAX_VCS];
1426 static CharDriverState *gd_vc_handler(ChardevVC *unused)
1428 CharDriverState *chr;
1430 chr = g_malloc0(sizeof(*chr));
1431 chr->chr_write = gd_vc_chr_write;
1432 /* defer OPENED events until our vc is fully initialized */
1433 chr->explicit_be_open = true;
1435 vcs[nb_vcs++] = chr;
1437 return chr;
1440 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1441 gpointer user_data)
1443 VirtualConsole *vc = user_data;
1445 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1446 return TRUE;
1449 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1450 CharDriverState *chr, int idx,
1451 GSList *group, GtkWidget *view_menu)
1453 char buffer[32];
1454 GtkWidget *box;
1455 GtkWidget *scrollbar;
1456 GtkAdjustment *vadjustment;
1458 vc->s = s;
1459 vc->vte.chr = chr;
1461 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1462 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1463 ? vc->vte.chr->label : buffer);
1464 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1466 vc->vte.terminal = vte_terminal_new();
1467 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1469 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1470 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1471 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1473 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1474 vadjustment = gtk_scrollable_get_vadjustment
1475 (GTK_SCROLLABLE(vc->vte.terminal));
1476 #else
1477 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1478 #endif
1480 #if GTK_CHECK_VERSION(3, 0, 0)
1481 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1482 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1483 #else
1484 box = gtk_hbox_new(false, 2);
1485 scrollbar = gtk_vscrollbar_new(vadjustment);
1486 #endif
1488 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1489 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1491 vc->vte.chr->opaque = vc;
1492 vc->vte.box = box;
1493 vc->vte.scrollbar = scrollbar;
1495 g_signal_connect(vadjustment, "changed",
1496 G_CALLBACK(gd_vc_adjustment_changed), vc);
1498 vc->type = GD_VC_VTE;
1499 vc->tab_item = box;
1500 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1501 gtk_label_new(vc->label));
1503 qemu_chr_be_generic_open(vc->vte.chr);
1504 if (vc->vte.chr->init) {
1505 vc->vte.chr->init(vc->vte.chr);
1508 return group;
1511 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1512 GtkWidget *view_menu)
1514 int i;
1516 for (i = 0; i < nb_vcs; i++) {
1517 VirtualConsole *vc = &s->vc[s->nb_vcs];
1518 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1519 s->nb_vcs++;
1522 #endif /* CONFIG_VTE */
1524 /** Window Creation **/
1526 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1528 #if GTK_CHECK_VERSION(3, 0, 0)
1529 g_signal_connect(vc->gfx.drawing_area, "draw",
1530 G_CALLBACK(gd_draw_event), vc);
1531 #else
1532 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1533 G_CALLBACK(gd_expose_event), vc);
1534 #endif
1535 g_signal_connect(vc->gfx.drawing_area, "event",
1536 G_CALLBACK(gd_event), vc);
1537 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1538 G_CALLBACK(gd_button_event), vc);
1539 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1540 G_CALLBACK(gd_button_event), vc);
1541 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1542 G_CALLBACK(gd_scroll_event), vc);
1543 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1544 G_CALLBACK(gd_key_event), vc);
1545 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1546 G_CALLBACK(gd_key_event), vc);
1548 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1549 G_CALLBACK(gd_enter_event), vc);
1550 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1551 G_CALLBACK(gd_leave_event), vc);
1552 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1553 G_CALLBACK(gd_focus_out_event), vc);
1556 static void gd_connect_signals(GtkDisplayState *s)
1558 g_signal_connect(s->show_tabs_item, "activate",
1559 G_CALLBACK(gd_menu_show_tabs), s);
1560 g_signal_connect(s->untabify_item, "activate",
1561 G_CALLBACK(gd_menu_untabify), s);
1563 g_signal_connect(s->window, "delete-event",
1564 G_CALLBACK(gd_window_close), s);
1566 g_signal_connect(s->pause_item, "activate",
1567 G_CALLBACK(gd_menu_pause), s);
1568 g_signal_connect(s->reset_item, "activate",
1569 G_CALLBACK(gd_menu_reset), s);
1570 g_signal_connect(s->powerdown_item, "activate",
1571 G_CALLBACK(gd_menu_powerdown), s);
1572 g_signal_connect(s->quit_item, "activate",
1573 G_CALLBACK(gd_menu_quit), s);
1574 g_signal_connect(s->full_screen_item, "activate",
1575 G_CALLBACK(gd_menu_full_screen), s);
1576 g_signal_connect(s->zoom_in_item, "activate",
1577 G_CALLBACK(gd_menu_zoom_in), s);
1578 g_signal_connect(s->zoom_out_item, "activate",
1579 G_CALLBACK(gd_menu_zoom_out), s);
1580 g_signal_connect(s->zoom_fixed_item, "activate",
1581 G_CALLBACK(gd_menu_zoom_fixed), s);
1582 g_signal_connect(s->zoom_fit_item, "activate",
1583 G_CALLBACK(gd_menu_zoom_fit), s);
1584 g_signal_connect(s->grab_item, "activate",
1585 G_CALLBACK(gd_menu_grab_input), s);
1586 g_signal_connect(s->notebook, "switch-page",
1587 G_CALLBACK(gd_change_page), s);
1590 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1592 GtkWidget *machine_menu;
1593 GtkWidget *separator;
1595 machine_menu = gtk_menu_new();
1596 gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1598 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1599 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1601 separator = gtk_separator_menu_item_new();
1602 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1604 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1605 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1607 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1608 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1610 separator = gtk_separator_menu_item_new();
1611 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1613 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1614 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1615 "<QEMU>/Machine/Quit");
1616 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1617 GDK_KEY_q, HOTKEY_MODIFIERS);
1618 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1620 return machine_menu;
1623 static const DisplayChangeListenerOps dcl_ops = {
1624 .dpy_name = "gtk",
1625 .dpy_gfx_update = gd_update,
1626 .dpy_gfx_switch = gd_switch,
1627 .dpy_refresh = gd_refresh,
1628 .dpy_mouse_set = gd_mouse_set,
1629 .dpy_cursor_define = gd_cursor_define,
1632 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1633 QemuConsole *con, int idx,
1634 GSList *group, GtkWidget *view_menu)
1636 Error *local_err = NULL;
1637 Object *obj;
1639 obj = object_property_get_link(OBJECT(con), "device", &local_err);
1640 if (obj) {
1641 vc->label = g_strdup_printf("%s", object_get_typename(obj));
1642 } else {
1643 vc->label = g_strdup_printf("VGA");
1646 vc->s = s;
1647 vc->gfx.scale_x = 1.0;
1648 vc->gfx.scale_y = 1.0;
1650 vc->gfx.drawing_area = gtk_drawing_area_new();
1651 gtk_widget_add_events(vc->gfx.drawing_area,
1652 GDK_POINTER_MOTION_MASK |
1653 GDK_BUTTON_PRESS_MASK |
1654 GDK_BUTTON_RELEASE_MASK |
1655 GDK_BUTTON_MOTION_MASK |
1656 GDK_ENTER_NOTIFY_MASK |
1657 GDK_LEAVE_NOTIFY_MASK |
1658 GDK_SCROLL_MASK |
1659 GDK_KEY_PRESS_MASK);
1660 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1661 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1663 vc->type = GD_VC_GFX;
1664 vc->tab_item = vc->gfx.drawing_area;
1665 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
1666 vc->tab_item, gtk_label_new(vc->label));
1667 gd_connect_vc_gfx_signals(vc);
1669 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1671 vc->gfx.dcl.ops = &dcl_ops;
1672 vc->gfx.dcl.con = con;
1673 register_displaychangelistener(&vc->gfx.dcl);
1675 return group;
1678 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1680 GSList *group = NULL;
1681 GtkWidget *view_menu;
1682 GtkWidget *separator;
1683 QemuConsole *con;
1684 int vc;
1686 view_menu = gtk_menu_new();
1687 gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1689 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1690 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1691 "<QEMU>/View/Full Screen");
1692 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
1693 HOTKEY_MODIFIERS);
1694 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1696 separator = gtk_separator_menu_item_new();
1697 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1699 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1700 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1701 "<QEMU>/View/Zoom In");
1702 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1703 HOTKEY_MODIFIERS);
1704 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1706 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1707 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1708 "<QEMU>/View/Zoom Out");
1709 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1710 HOTKEY_MODIFIERS);
1711 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1713 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1714 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1715 "<QEMU>/View/Zoom Fixed");
1716 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1717 HOTKEY_MODIFIERS);
1718 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1720 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1721 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1723 separator = gtk_separator_menu_item_new();
1724 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1726 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1727 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1729 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1730 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1731 "<QEMU>/View/Grab Input");
1732 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1733 HOTKEY_MODIFIERS);
1734 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1736 separator = gtk_separator_menu_item_new();
1737 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1739 /* gfx */
1740 for (vc = 0;; vc++) {
1741 con = qemu_console_lookup_by_index(vc);
1742 if (!con || !qemu_console_is_graphic(con)) {
1743 break;
1745 group = gd_vc_gfx_init(s, &s->vc[vc], con,
1746 vc, group, view_menu);
1747 s->nb_vcs++;
1750 #if defined(CONFIG_VTE)
1751 /* vte */
1752 gd_vcs_init(s, group, view_menu);
1753 #endif
1755 separator = gtk_separator_menu_item_new();
1756 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1758 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1759 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1761 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1762 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
1764 return view_menu;
1767 static void gd_create_menus(GtkDisplayState *s)
1769 GtkAccelGroup *accel_group;
1771 accel_group = gtk_accel_group_new();
1772 s->machine_menu = gd_create_menu_machine(s, accel_group);
1773 s->view_menu = gd_create_menu_view(s, accel_group);
1775 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1776 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1777 s->machine_menu);
1778 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1780 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1781 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1782 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1784 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1785 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1786 s->accel_group = accel_group;
1789 static void gd_set_keycode_type(GtkDisplayState *s)
1791 #ifdef GDK_WINDOWING_X11
1792 GdkDisplay *display = gtk_widget_get_display(s->window);
1793 if (GDK_IS_X11_DISPLAY(display)) {
1794 Display *x11_display = gdk_x11_display_get_xdisplay(display);
1795 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
1796 XkbUseCoreKbd);
1797 char *keycodes = NULL;
1799 if (desc && desc->names) {
1800 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
1802 if (keycodes == NULL) {
1803 fprintf(stderr, "could not lookup keycode name\n");
1804 } else if (strstart(keycodes, "evdev", NULL)) {
1805 s->has_evdev = true;
1806 } else if (!strstart(keycodes, "xfree86", NULL)) {
1807 fprintf(stderr, "unknown keycodes `%s', please report to "
1808 "qemu-devel@nongnu.org\n", keycodes);
1811 #endif
1814 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
1816 GtkDisplayState *s = g_malloc0(sizeof(*s));
1817 char *filename;
1819 gtk_init(NULL, NULL);
1821 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1822 #if GTK_CHECK_VERSION(3, 2, 0)
1823 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1824 #else
1825 s->vbox = gtk_vbox_new(FALSE, 0);
1826 #endif
1827 s->notebook = gtk_notebook_new();
1828 s->menu_bar = gtk_menu_bar_new();
1830 s->free_scale = FALSE;
1832 setlocale(LC_ALL, "");
1833 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1834 textdomain("qemu");
1836 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1838 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1839 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1840 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1842 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1843 if (filename) {
1844 GError *error = NULL;
1845 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1846 if (pixbuf) {
1847 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1848 } else {
1849 g_error_free(error);
1851 g_free(filename);
1854 gd_create_menus(s);
1856 gd_connect_signals(s);
1858 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1859 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1861 gd_update_caption(s);
1863 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1864 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1866 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1868 gtk_widget_show_all(s->window);
1870 #ifdef VTE_RESIZE_HACK
1872 VirtualConsole *cur = gd_vc_find_current(s);
1873 int i;
1875 for (i = 0; i < s->nb_vcs; i++) {
1876 VirtualConsole *vc = &s->vc[i];
1877 if (vc && vc->type == GD_VC_VTE && vc != cur) {
1878 gtk_widget_hide(vc->vte.terminal);
1881 gd_update_windowsize(cur);
1883 #endif
1885 if (full_screen) {
1886 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1888 if (grab_on_hover) {
1889 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1892 gd_set_keycode_type(s);
1895 void early_gtk_display_init(void)
1897 #if defined(CONFIG_VTE)
1898 register_vc_handler(gd_vc_handler);
1899 #endif