virtio: force VIRTIO_F_IOMMU_PLATFORM
[qemu.git] / ui / gtk.c
blob86368e38b7ce7298ba1eafc4b38fa281969c3e89
1 /*
2 * GTK UI
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * Portions from gtk-vnc:
14 * GTK VNC Widget
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.0 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
37 #include "qemu/osdep.h"
38 #include "qemu-common.h"
39 #include "qemu/cutils.h"
41 #include "ui/console.h"
42 #include "ui/gtk.h"
44 #include <glib/gi18n.h>
45 #include <locale.h>
46 #if defined(CONFIG_VTE)
47 #include <vte/vte.h>
48 #endif
49 #include <math.h>
51 #include "trace.h"
52 #include "ui/input.h"
53 #include "sysemu/sysemu.h"
54 #include "qmp-commands.h"
55 #include "x_keymap.h"
56 #include "keymaps.h"
57 #include "sysemu/char.h"
58 #include "qom/object.h"
60 #define MAX_VCS 10
61 #define VC_WINDOW_X_MIN 320
62 #define VC_WINDOW_Y_MIN 240
63 #define VC_TERM_X_MIN 80
64 #define VC_TERM_Y_MIN 25
65 #define VC_SCALE_MIN 0.25
66 #define VC_SCALE_STEP 0.25
68 #if !defined(CONFIG_VTE)
69 # define VTE_CHECK_VERSION(a, b, c) 0
70 #endif
72 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
74 * The gtk2 vte terminal widget seriously messes up the window resize
75 * for some reason. You basically can't make the qemu window smaller
76 * any more because the toplevel window geoemtry hints are overridden.
78 * Workaround that by hiding all vte widgets, except the one in the
79 * current tab.
81 * Luckily everything works smooth in gtk3.
83 # define VTE_RESIZE_HACK 1
84 #endif
86 #if !GTK_CHECK_VERSION(2, 20, 0)
87 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
88 #endif
90 #ifndef GDK_IS_X11_DISPLAY
91 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
92 #endif
93 #ifndef GDK_IS_WAYLAND_DISPLAY
94 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
95 #endif
96 #ifndef GDK_IS_WIN32_DISPLAY
97 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
98 #endif
100 #if !GTK_CHECK_VERSION(2, 22, 0)
101 #define GDK_KEY_0 GDK_0
102 #define GDK_KEY_1 GDK_1
103 #define GDK_KEY_2 GDK_2
104 #define GDK_KEY_f GDK_f
105 #define GDK_KEY_g GDK_g
106 #define GDK_KEY_q GDK_q
107 #define GDK_KEY_plus GDK_plus
108 #define GDK_KEY_minus GDK_minus
109 #define GDK_KEY_Pause GDK_Pause
110 #define GDK_KEY_Delete GDK_Delete
111 #endif
113 /* Some older mingw versions lack this constant or have
114 * it conditionally defined */
115 #ifdef _WIN32
116 # ifndef MAPVK_VK_TO_VSC
117 # define MAPVK_VK_TO_VSC 0
118 # endif
119 #endif
122 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
124 static const int modifier_keycode[] = {
125 /* shift, control, alt keys, meta keys, both left & right */
126 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
129 struct GtkDisplayState {
130 GtkWidget *window;
132 GtkWidget *menu_bar;
134 GtkAccelGroup *accel_group;
136 GtkWidget *machine_menu_item;
137 GtkWidget *machine_menu;
138 GtkWidget *pause_item;
139 GtkWidget *reset_item;
140 GtkWidget *powerdown_item;
141 GtkWidget *quit_item;
143 GtkWidget *view_menu_item;
144 GtkWidget *view_menu;
145 GtkWidget *full_screen_item;
146 GtkWidget *copy_item;
147 GtkWidget *zoom_in_item;
148 GtkWidget *zoom_out_item;
149 GtkWidget *zoom_fixed_item;
150 GtkWidget *zoom_fit_item;
151 GtkWidget *grab_item;
152 GtkWidget *grab_on_hover_item;
154 int nb_vcs;
155 VirtualConsole vc[MAX_VCS];
157 GtkWidget *show_tabs_item;
158 GtkWidget *untabify_item;
160 GtkWidget *vbox;
161 GtkWidget *notebook;
162 int button_mask;
163 gboolean last_set;
164 int last_x;
165 int last_y;
166 int grab_x_root;
167 int grab_y_root;
168 VirtualConsole *kbd_owner;
169 VirtualConsole *ptr_owner;
171 gboolean full_screen;
173 GdkCursor *null_cursor;
174 Notifier mouse_mode_notifier;
175 gboolean free_scale;
177 bool external_pause_update;
179 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
180 bool has_evdev;
181 bool ignore_keys;
184 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
185 static void gd_ungrab_pointer(GtkDisplayState *s);
186 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
187 static void gd_ungrab_keyboard(GtkDisplayState *s);
189 /** Utility Functions **/
191 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
193 VirtualConsole *vc;
194 gint i;
196 for (i = 0; i < s->nb_vcs; i++) {
197 vc = &s->vc[i];
198 if (gtk_check_menu_item_get_active
199 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
200 return vc;
203 return NULL;
206 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
208 VirtualConsole *vc;
209 gint i, p;
211 for (i = 0; i < s->nb_vcs; i++) {
212 vc = &s->vc[i];
213 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
214 if (p == page) {
215 return vc;
218 return NULL;
221 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
223 gint page;
225 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
226 return gd_vc_find_by_page(s, page);
229 static bool gd_is_grab_active(GtkDisplayState *s)
231 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
234 static bool gd_grab_on_hover(GtkDisplayState *s)
236 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
239 static void gd_update_cursor(VirtualConsole *vc)
241 GtkDisplayState *s = vc->s;
242 GdkWindow *window;
244 if (vc->type != GD_VC_GFX ||
245 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
246 return;
249 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
250 return;
253 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
254 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
255 gdk_window_set_cursor(window, s->null_cursor);
256 } else {
257 gdk_window_set_cursor(window, NULL);
261 static void gd_update_caption(GtkDisplayState *s)
263 const char *status = "";
264 gchar *prefix;
265 gchar *title;
266 const char *grab = "";
267 bool is_paused = !runstate_is_running();
268 int i;
270 if (qemu_name) {
271 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
272 } else {
273 prefix = g_strdup_printf("QEMU");
276 if (s->ptr_owner != NULL &&
277 s->ptr_owner->window == NULL) {
278 grab = _(" - Press Ctrl+Alt+G to release grab");
281 if (is_paused) {
282 status = _(" [Paused]");
284 s->external_pause_update = true;
285 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
286 is_paused);
287 s->external_pause_update = false;
289 title = g_strdup_printf("%s%s%s", prefix, status, grab);
290 gtk_window_set_title(GTK_WINDOW(s->window), title);
291 g_free(title);
293 for (i = 0; i < s->nb_vcs; i++) {
294 VirtualConsole *vc = &s->vc[i];
296 if (!vc->window) {
297 continue;
299 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
300 vc == s->kbd_owner ? " +kbd" : "",
301 vc == s->ptr_owner ? " +ptr" : "");
302 gtk_window_set_title(GTK_WINDOW(vc->window), title);
303 g_free(title);
306 g_free(prefix);
309 static void gd_update_geometry_hints(VirtualConsole *vc)
311 GtkDisplayState *s = vc->s;
312 GdkWindowHints mask = 0;
313 GdkGeometry geo = {};
314 GtkWidget *geo_widget = NULL;
315 GtkWindow *geo_window;
317 if (vc->type == GD_VC_GFX) {
318 if (!vc->gfx.ds) {
319 return;
321 if (s->free_scale) {
322 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
323 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
324 mask |= GDK_HINT_MIN_SIZE;
325 } else {
326 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
327 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
328 mask |= GDK_HINT_MIN_SIZE;
330 geo_widget = vc->gfx.drawing_area;
331 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
333 #if defined(CONFIG_VTE)
334 } else if (vc->type == GD_VC_VTE) {
335 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
336 GtkBorder padding = { 0 };
338 #if VTE_CHECK_VERSION(0, 37, 0)
339 gtk_style_context_get_padding(
340 gtk_widget_get_style_context(vc->vte.terminal),
341 gtk_widget_get_state_flags(vc->vte.terminal),
342 &padding);
343 #else
345 GtkBorder *ib = NULL;
346 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
347 if (ib) {
348 padding = *ib;
349 gtk_border_free(ib);
352 #endif
354 geo.width_inc = vte_terminal_get_char_width(term);
355 geo.height_inc = vte_terminal_get_char_height(term);
356 mask |= GDK_HINT_RESIZE_INC;
357 geo.base_width = geo.width_inc;
358 geo.base_height = geo.height_inc;
359 mask |= GDK_HINT_BASE_SIZE;
360 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
361 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
362 mask |= GDK_HINT_MIN_SIZE;
364 geo.base_width += padding.left + padding.right;
365 geo.base_height += padding.top + padding.bottom;
366 geo.min_width += padding.left + padding.right;
367 geo.min_height += padding.top + padding.bottom;
368 geo_widget = vc->vte.terminal;
369 #endif
372 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
373 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
376 void gd_update_windowsize(VirtualConsole *vc)
378 GtkDisplayState *s = vc->s;
380 gd_update_geometry_hints(vc);
382 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
383 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
384 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
388 static void gd_update_full_redraw(VirtualConsole *vc)
390 GtkWidget *area = vc->gfx.drawing_area;
391 int ww, wh;
392 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
393 #if defined(CONFIG_GTK_GL)
394 if (vc->gfx.gls) {
395 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
396 return;
398 #endif
399 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
402 static void gtk_release_modifiers(GtkDisplayState *s)
404 VirtualConsole *vc = gd_vc_find_current(s);
405 int i, keycode;
407 if (vc->type != GD_VC_GFX ||
408 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
409 return;
411 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
412 keycode = modifier_keycode[i];
413 if (!s->modifier_pressed[i]) {
414 continue;
416 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
417 s->modifier_pressed[i] = false;
421 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
422 GtkWidget *widget)
424 g_object_ref(G_OBJECT(widget));
425 gtk_container_remove(GTK_CONTAINER(from), widget);
426 gtk_container_add(GTK_CONTAINER(to), widget);
427 g_object_unref(G_OBJECT(widget));
430 /** DisplayState Callbacks **/
432 static void gd_update(DisplayChangeListener *dcl,
433 int x, int y, int w, int h)
435 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
436 GdkWindow *win;
437 int x1, x2, y1, y2;
438 int mx, my;
439 int fbw, fbh;
440 int ww, wh;
442 trace_gd_update(vc->label, x, y, w, h);
444 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
445 return;
448 if (vc->gfx.convert) {
449 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
450 NULL, vc->gfx.convert,
451 x, y, 0, 0, x, y, w, h);
454 x1 = floor(x * vc->gfx.scale_x);
455 y1 = floor(y * vc->gfx.scale_y);
457 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
458 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
460 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
461 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
463 win = gtk_widget_get_window(vc->gfx.drawing_area);
464 if (!win) {
465 return;
467 gdk_drawable_get_size(win, &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 GdkDevice *gd_get_pointer(GdkDisplay *dpy)
489 #if GTK_CHECK_VERSION(3, 20, 0)
490 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
491 #else
492 return gdk_device_manager_get_client_pointer(
493 gdk_display_get_device_manager(dpy));
494 #endif
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 gint x_root, y_root;
504 if (qemu_input_is_absolute()) {
505 return;
508 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
509 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
510 x, y, &x_root, &y_root);
511 gdk_device_warp(gd_get_pointer(dpy),
512 gtk_widget_get_screen(vc->gfx.drawing_area),
513 x_root, y_root);
514 vc->s->last_x = x;
515 vc->s->last_y = y;
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 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
544 return;
547 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
548 GDK_COLORSPACE_RGB, true, 8,
549 c->width, c->height, c->width * 4,
550 NULL, NULL);
551 cursor = gdk_cursor_new_from_pixbuf
552 (gtk_widget_get_display(vc->gfx.drawing_area),
553 pixbuf, c->hot_x, c->hot_y);
554 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
555 g_object_unref(pixbuf);
556 #if !GTK_CHECK_VERSION(3, 0, 0)
557 gdk_cursor_unref(cursor);
558 #else
559 g_object_unref(cursor);
560 #endif
563 static void gd_switch(DisplayChangeListener *dcl,
564 DisplaySurface *surface)
566 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
567 bool resized = true;
569 trace_gd_switch(vc->label,
570 surface ? surface_width(surface) : 0,
571 surface ? surface_height(surface) : 0);
573 if (vc->gfx.surface) {
574 cairo_surface_destroy(vc->gfx.surface);
575 vc->gfx.surface = NULL;
577 if (vc->gfx.convert) {
578 pixman_image_unref(vc->gfx.convert);
579 vc->gfx.convert = NULL;
582 if (vc->gfx.ds && surface &&
583 surface_width(vc->gfx.ds) == surface_width(surface) &&
584 surface_height(vc->gfx.ds) == surface_height(surface)) {
585 resized = false;
587 vc->gfx.ds = surface;
589 if (!surface) {
590 return;
593 if (surface->format == PIXMAN_x8r8g8b8) {
595 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
597 * No need to convert, use surface directly. Should be the
598 * common case as this is qemu_default_pixelformat(32) too.
600 vc->gfx.surface = cairo_image_surface_create_for_data
601 (surface_data(surface),
602 CAIRO_FORMAT_RGB24,
603 surface_width(surface),
604 surface_height(surface),
605 surface_stride(surface));
606 } else {
607 /* Must convert surface, use pixman to do it. */
608 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
609 surface_width(surface),
610 surface_height(surface),
611 NULL, 0);
612 vc->gfx.surface = cairo_image_surface_create_for_data
613 ((void *)pixman_image_get_data(vc->gfx.convert),
614 CAIRO_FORMAT_RGB24,
615 pixman_image_get_width(vc->gfx.convert),
616 pixman_image_get_height(vc->gfx.convert),
617 pixman_image_get_stride(vc->gfx.convert));
618 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
619 NULL, vc->gfx.convert,
620 0, 0, 0, 0, 0, 0,
621 pixman_image_get_width(vc->gfx.convert),
622 pixman_image_get_height(vc->gfx.convert));
625 if (resized) {
626 gd_update_windowsize(vc);
627 } else {
628 gd_update_full_redraw(vc);
632 static const DisplayChangeListenerOps dcl_ops = {
633 .dpy_name = "gtk",
634 .dpy_gfx_update = gd_update,
635 .dpy_gfx_switch = gd_switch,
636 .dpy_gfx_check_format = qemu_pixman_check_format,
637 .dpy_refresh = gd_refresh,
638 .dpy_mouse_set = gd_mouse_set,
639 .dpy_cursor_define = gd_cursor_define,
643 #if defined(CONFIG_OPENGL)
645 /** DisplayState Callbacks (opengl version) **/
647 #if defined(CONFIG_GTK_GL)
649 static const DisplayChangeListenerOps dcl_gl_area_ops = {
650 .dpy_name = "gtk-egl",
651 .dpy_gfx_update = gd_gl_area_update,
652 .dpy_gfx_switch = gd_gl_area_switch,
653 .dpy_gfx_check_format = console_gl_check_format,
654 .dpy_refresh = gd_gl_area_refresh,
655 .dpy_mouse_set = gd_mouse_set,
656 .dpy_cursor_define = gd_cursor_define,
658 .dpy_gl_ctx_create = gd_gl_area_create_context,
659 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
660 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
661 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
662 .dpy_gl_scanout = gd_gl_area_scanout,
663 .dpy_gl_update = gd_gl_area_scanout_flush,
666 #else
668 static const DisplayChangeListenerOps dcl_egl_ops = {
669 .dpy_name = "gtk-egl",
670 .dpy_gfx_update = gd_egl_update,
671 .dpy_gfx_switch = gd_egl_switch,
672 .dpy_gfx_check_format = console_gl_check_format,
673 .dpy_refresh = gd_egl_refresh,
674 .dpy_mouse_set = gd_mouse_set,
675 .dpy_cursor_define = gd_cursor_define,
677 .dpy_gl_ctx_create = gd_egl_create_context,
678 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
679 .dpy_gl_ctx_make_current = gd_egl_make_current,
680 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
681 .dpy_gl_scanout = gd_egl_scanout,
682 .dpy_gl_update = gd_egl_scanout_flush,
685 #endif /* CONFIG_GTK_GL */
686 #endif /* CONFIG_OPENGL */
688 /** QEMU Events **/
690 static void gd_change_runstate(void *opaque, int running, RunState state)
692 GtkDisplayState *s = opaque;
694 gd_update_caption(s);
697 static void gd_mouse_mode_change(Notifier *notify, void *data)
699 GtkDisplayState *s;
700 int i;
702 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
703 /* release the grab at switching to absolute mode */
704 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
705 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
706 FALSE);
708 for (i = 0; i < s->nb_vcs; i++) {
709 VirtualConsole *vc = &s->vc[i];
710 gd_update_cursor(vc);
714 /** GTK Events **/
716 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
717 void *opaque)
719 GtkDisplayState *s = opaque;
720 int i;
722 if (!no_quit) {
723 for (i = 0; i < s->nb_vcs; i++) {
724 if (s->vc[i].type != GD_VC_GFX) {
725 continue;
727 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
729 qmp_quit(NULL);
730 return FALSE;
733 return TRUE;
736 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
738 QemuUIInfo info;
740 memset(&info, 0, sizeof(info));
741 info.width = width;
742 info.height = height;
743 dpy_set_ui_info(vc->gfx.dcl.con, &info);
746 #if defined(CONFIG_GTK_GL)
748 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
749 void *opaque)
751 VirtualConsole *vc = opaque;
753 if (vc->gfx.gls) {
754 gd_gl_area_draw(vc);
756 return TRUE;
759 static void gd_resize_event(GtkGLArea *area,
760 gint width, gint height, gpointer *opaque)
762 VirtualConsole *vc = (void *)opaque;
764 gd_set_ui_info(vc, width, height);
767 #endif
769 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
771 VirtualConsole *vc = opaque;
772 GtkDisplayState *s = vc->s;
773 int mx, my;
774 int ww, wh;
775 int fbw, fbh;
777 #if defined(CONFIG_OPENGL)
778 if (vc->gfx.gls) {
779 #if defined(CONFIG_GTK_GL)
780 /* invoke render callback please */
781 return FALSE;
782 #else
783 gd_egl_draw(vc);
784 return TRUE;
785 #endif
787 #endif
789 if (!gtk_widget_get_realized(widget)) {
790 return FALSE;
792 if (!vc->gfx.ds) {
793 return FALSE;
796 fbw = surface_width(vc->gfx.ds);
797 fbh = surface_height(vc->gfx.ds);
799 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
801 if (s->full_screen) {
802 vc->gfx.scale_x = (double)ww / fbw;
803 vc->gfx.scale_y = (double)wh / fbh;
804 } else if (s->free_scale) {
805 double sx, sy;
807 sx = (double)ww / fbw;
808 sy = (double)wh / fbh;
810 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
813 fbw *= vc->gfx.scale_x;
814 fbh *= vc->gfx.scale_y;
816 mx = my = 0;
817 if (ww > fbw) {
818 mx = (ww - fbw) / 2;
820 if (wh > fbh) {
821 my = (wh - fbh) / 2;
824 cairo_rectangle(cr, 0, 0, ww, wh);
826 /* Optionally cut out the inner area where the pixmap
827 will be drawn. This avoids 'flashing' since we're
828 not double-buffering. Note we're using the undocumented
829 behaviour of drawing the rectangle from right to left
830 to cut out the whole */
831 cairo_rectangle(cr, mx + fbw, my,
832 -1 * fbw, fbh);
833 cairo_fill(cr);
835 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
836 cairo_set_source_surface(cr, vc->gfx.surface,
837 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
838 cairo_paint(cr);
840 return TRUE;
843 #if !GTK_CHECK_VERSION(3, 0, 0)
844 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
845 void *opaque)
847 cairo_t *cr;
848 gboolean ret;
850 cr = gdk_cairo_create(gtk_widget_get_window(widget));
851 cairo_rectangle(cr,
852 expose->area.x,
853 expose->area.y,
854 expose->area.width,
855 expose->area.height);
856 cairo_clip(cr);
858 ret = gd_draw_event(widget, cr, opaque);
860 cairo_destroy(cr);
862 return ret;
864 #endif
866 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
867 void *opaque)
869 VirtualConsole *vc = opaque;
870 GtkDisplayState *s = vc->s;
871 int x, y;
872 int mx, my;
873 int fbh, fbw;
874 int ww, wh;
876 if (!vc->gfx.ds) {
877 return TRUE;
880 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
881 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
883 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
884 &ww, &wh);
886 mx = my = 0;
887 if (ww > fbw) {
888 mx = (ww - fbw) / 2;
890 if (wh > fbh) {
891 my = (wh - fbh) / 2;
894 x = (motion->x - mx) / vc->gfx.scale_x;
895 y = (motion->y - my) / vc->gfx.scale_y;
897 if (qemu_input_is_absolute()) {
898 if (x < 0 || y < 0 ||
899 x >= surface_width(vc->gfx.ds) ||
900 y >= surface_height(vc->gfx.ds)) {
901 return TRUE;
903 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
904 surface_width(vc->gfx.ds));
905 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
906 surface_height(vc->gfx.ds));
907 qemu_input_event_sync();
908 } else if (s->last_set && s->ptr_owner == vc) {
909 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
910 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
911 qemu_input_event_sync();
913 s->last_x = x;
914 s->last_y = y;
915 s->last_set = TRUE;
917 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
918 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
919 int screen_width, screen_height;
921 int x = (int)motion->x_root;
922 int y = (int)motion->y_root;
924 #if GTK_CHECK_VERSION(3, 22, 0)
926 GdkDisplay *dpy = gtk_widget_get_display(widget);
927 GdkWindow *win = gtk_widget_get_window(widget);
928 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
929 GdkRectangle geometry;
930 gdk_monitor_get_geometry(monitor, &geometry);
931 screen_width = geometry.width;
932 screen_height = geometry.height;
934 #else
936 screen_width = gdk_screen_get_width(screen);
937 screen_height = gdk_screen_get_height(screen);
939 #endif
941 /* In relative mode check to see if client pointer hit
942 * one of the screen edges, and if so move it back by
943 * 200 pixels. This is important because the pointer
944 * in the server doesn't correspond 1-for-1, and so
945 * may still be only half way across the screen. Without
946 * this warp, the server pointer would thus appear to hit
947 * an invisible wall */
948 if (x == 0) {
949 x += 200;
951 if (y == 0) {
952 y += 200;
954 if (x == (screen_width - 1)) {
955 x -= 200;
957 if (y == (screen_height - 1)) {
958 y -= 200;
961 if (x != (int)motion->x_root || y != (int)motion->y_root) {
962 #if GTK_CHECK_VERSION(3, 0, 0)
963 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
964 gdk_device_warp(dev, screen, x, y);
965 #else
966 GdkDisplay *display = gtk_widget_get_display(widget);
967 gdk_display_warp_pointer(display, screen, x, y);
968 #endif
969 s->last_set = FALSE;
970 return FALSE;
973 return TRUE;
976 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
977 void *opaque)
979 VirtualConsole *vc = opaque;
980 GtkDisplayState *s = vc->s;
981 InputButton btn;
983 /* implicitly grab the input at the first click in the relative mode */
984 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
985 !qemu_input_is_absolute() && s->ptr_owner != vc) {
986 if (!vc->window) {
987 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
988 TRUE);
989 } else {
990 gd_grab_pointer(vc, "relative-mode-click");
992 return TRUE;
995 if (button->button == 1) {
996 btn = INPUT_BUTTON_LEFT;
997 } else if (button->button == 2) {
998 btn = INPUT_BUTTON_MIDDLE;
999 } else if (button->button == 3) {
1000 btn = INPUT_BUTTON_RIGHT;
1001 } else {
1002 return TRUE;
1005 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1006 button->type == GDK_BUTTON_PRESS);
1007 qemu_input_event_sync();
1008 return TRUE;
1011 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1012 void *opaque)
1014 VirtualConsole *vc = opaque;
1015 InputButton btn;
1017 if (scroll->direction == GDK_SCROLL_UP) {
1018 btn = INPUT_BUTTON_WHEEL_UP;
1019 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1020 btn = INPUT_BUTTON_WHEEL_DOWN;
1021 } else {
1022 return TRUE;
1025 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1026 qemu_input_event_sync();
1027 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1028 qemu_input_event_sync();
1029 return TRUE;
1032 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
1034 int qemu_keycode;
1036 #ifdef GDK_WINDOWING_WIN32
1037 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1038 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
1039 switch (qemu_keycode) {
1040 case 103: /* alt gr */
1041 qemu_keycode = 56 | SCANCODE_GREY;
1042 break;
1044 return qemu_keycode;
1046 #endif
1048 if (gdk_keycode < 9) {
1049 qemu_keycode = 0;
1050 } else if (gdk_keycode < 97) {
1051 qemu_keycode = gdk_keycode - 8;
1052 #ifdef GDK_WINDOWING_X11
1053 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
1054 if (s->has_evdev) {
1055 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1056 } else {
1057 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
1059 #endif
1060 #ifdef GDK_WINDOWING_WAYLAND
1061 } else if (GDK_IS_WAYLAND_DISPLAY(dpy) && gdk_keycode < 158) {
1062 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1063 #endif
1064 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
1065 qemu_keycode = 0x70;
1066 } else if (gdk_keycode == 211) { /* backslash */
1067 qemu_keycode = 0x73;
1068 } else {
1069 qemu_keycode = 0;
1072 return qemu_keycode;
1075 static gboolean gd_text_key_down(GtkWidget *widget,
1076 GdkEventKey *key, void *opaque)
1078 VirtualConsole *vc = opaque;
1079 QemuConsole *con = vc->gfx.dcl.con;
1081 if (key->keyval == GDK_KEY_Delete) {
1082 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1083 } else if (key->length) {
1084 kbd_put_string_console(con, key->string, key->length);
1085 } else {
1086 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
1087 key->hardware_keycode);
1088 int qcode = qemu_input_key_number_to_qcode(num);
1089 kbd_put_qcode_console(con, qcode);
1091 return TRUE;
1094 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1096 VirtualConsole *vc = opaque;
1097 GtkDisplayState *s = vc->s;
1098 int gdk_keycode = key->hardware_keycode;
1099 int qemu_keycode;
1100 int i;
1102 if (s->ignore_keys) {
1103 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1104 return TRUE;
1107 if (key->keyval == GDK_KEY_Pause) {
1108 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1109 key->type == GDK_KEY_PRESS);
1110 return TRUE;
1113 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
1114 gdk_keycode);
1116 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
1117 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1119 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1120 if (qemu_keycode == modifier_keycode[i]) {
1121 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1125 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
1126 key->type == GDK_KEY_PRESS);
1128 return TRUE;
1131 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1133 if (event->type == GDK_MOTION_NOTIFY) {
1134 return gd_motion_event(widget, &event->motion, opaque);
1136 return FALSE;
1139 /** Window Menu Actions **/
1141 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1143 GtkDisplayState *s = opaque;
1145 if (s->external_pause_update) {
1146 return;
1148 if (runstate_is_running()) {
1149 qmp_stop(NULL);
1150 } else {
1151 qmp_cont(NULL);
1155 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1157 qmp_system_reset(NULL);
1160 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1162 qmp_system_powerdown(NULL);
1165 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1167 qmp_quit(NULL);
1170 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1172 GtkDisplayState *s = opaque;
1173 VirtualConsole *vc = gd_vc_find_by_menu(s);
1174 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1175 gint page;
1177 gtk_release_modifiers(s);
1178 if (vc) {
1179 page = gtk_notebook_page_num(nb, vc->tab_item);
1180 gtk_notebook_set_current_page(nb, page);
1181 gtk_widget_grab_focus(vc->focus);
1183 s->ignore_keys = false;
1186 static void gd_accel_switch_vc(void *opaque)
1188 VirtualConsole *vc = opaque;
1190 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1191 #if !GTK_CHECK_VERSION(3, 0, 0)
1192 /* GTK2 sends the accel key to the target console - ignore this until */
1193 vc->s->ignore_keys = true;
1194 #endif
1197 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1199 GtkDisplayState *s = opaque;
1200 VirtualConsole *vc = gd_vc_find_current(s);
1202 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1203 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1204 } else {
1205 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1207 gd_update_windowsize(vc);
1210 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1211 void *opaque)
1213 VirtualConsole *vc = opaque;
1214 GtkDisplayState *s = vc->s;
1216 gtk_widget_set_sensitive(vc->menu_item, true);
1217 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1218 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1219 vc->tab_item, vc->label);
1220 gtk_widget_destroy(vc->window);
1221 vc->window = NULL;
1222 return TRUE;
1225 static gboolean gd_win_grab(void *opaque)
1227 VirtualConsole *vc = opaque;
1229 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1230 if (vc->s->ptr_owner) {
1231 gd_ungrab_pointer(vc->s);
1232 } else {
1233 gd_grab_pointer(vc, "user-request-detached-tab");
1235 return TRUE;
1238 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1240 GtkDisplayState *s = opaque;
1241 VirtualConsole *vc = gd_vc_find_current(s);
1243 if (vc->type == GD_VC_GFX &&
1244 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1245 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1246 FALSE);
1248 if (!vc->window) {
1249 gtk_widget_set_sensitive(vc->menu_item, false);
1250 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1251 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1253 g_signal_connect(vc->window, "delete-event",
1254 G_CALLBACK(gd_tab_window_close), vc);
1255 gtk_widget_show_all(vc->window);
1257 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1258 GtkAccelGroup *ag = gtk_accel_group_new();
1259 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1261 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1262 vc, NULL);
1263 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1266 gd_update_geometry_hints(vc);
1267 gd_update_caption(s);
1271 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1273 GtkDisplayState *s = opaque;
1274 VirtualConsole *vc = gd_vc_find_current(s);
1276 if (!s->full_screen) {
1277 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1278 gtk_widget_hide(s->menu_bar);
1279 if (vc->type == GD_VC_GFX) {
1280 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1282 gtk_window_fullscreen(GTK_WINDOW(s->window));
1283 s->full_screen = TRUE;
1284 } else {
1285 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1286 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1287 gtk_widget_show(s->menu_bar);
1288 s->full_screen = FALSE;
1289 if (vc->type == GD_VC_GFX) {
1290 vc->gfx.scale_x = 1.0;
1291 vc->gfx.scale_y = 1.0;
1292 gd_update_windowsize(vc);
1296 gd_update_cursor(vc);
1299 static void gd_accel_full_screen(void *opaque)
1301 GtkDisplayState *s = opaque;
1302 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1305 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1307 GtkDisplayState *s = opaque;
1308 VirtualConsole *vc = gd_vc_find_current(s);
1310 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1311 FALSE);
1313 vc->gfx.scale_x += VC_SCALE_STEP;
1314 vc->gfx.scale_y += VC_SCALE_STEP;
1316 gd_update_windowsize(vc);
1319 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1321 GtkDisplayState *s = opaque;
1322 VirtualConsole *vc = gd_vc_find_current(s);
1324 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1325 FALSE);
1327 vc->gfx.scale_x -= VC_SCALE_STEP;
1328 vc->gfx.scale_y -= VC_SCALE_STEP;
1330 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1331 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1333 gd_update_windowsize(vc);
1336 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1338 GtkDisplayState *s = opaque;
1339 VirtualConsole *vc = gd_vc_find_current(s);
1341 vc->gfx.scale_x = 1.0;
1342 vc->gfx.scale_y = 1.0;
1344 gd_update_windowsize(vc);
1347 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1349 GtkDisplayState *s = opaque;
1350 VirtualConsole *vc = gd_vc_find_current(s);
1352 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1353 s->free_scale = TRUE;
1354 } else {
1355 s->free_scale = FALSE;
1356 vc->gfx.scale_x = 1.0;
1357 vc->gfx.scale_y = 1.0;
1360 gd_update_windowsize(vc);
1361 gd_update_full_redraw(vc);
1364 #if GTK_CHECK_VERSION(3, 20, 0)
1365 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1367 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1368 GdkSeat *seat = gdk_display_get_default_seat(display);
1369 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1370 GdkSeatCapabilities caps = 0;
1371 GdkCursor *cursor = NULL;
1373 if (kbd) {
1374 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1376 if (ptr) {
1377 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1378 cursor = vc->s->null_cursor;
1381 if (caps) {
1382 gdk_seat_grab(seat, window, caps, false, cursor,
1383 NULL, NULL, NULL);
1384 } else {
1385 gdk_seat_ungrab(seat);
1388 #elif GTK_CHECK_VERSION(3, 0, 0)
1389 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1390 GdkInputSource source, GdkEventMask mask,
1391 GdkCursor *cursor)
1393 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1394 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1395 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1396 GList *tmp = devs;
1398 for (tmp = devs; tmp; tmp = tmp->next) {
1399 GdkDevice *dev = tmp->data;
1400 if (gdk_device_get_source(dev) != source) {
1401 continue;
1403 if (grab) {
1404 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1405 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1406 mask, cursor, GDK_CURRENT_TIME);
1407 } else {
1408 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1411 g_list_free(devs);
1413 #endif
1415 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1417 if (vc->s->kbd_owner) {
1418 if (vc->s->kbd_owner == vc) {
1419 return;
1420 } else {
1421 gd_ungrab_keyboard(vc->s);
1425 #if GTK_CHECK_VERSION(3, 20, 0)
1426 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1427 #elif GTK_CHECK_VERSION(3, 0, 0)
1428 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1429 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1430 NULL);
1431 #else
1432 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1433 FALSE,
1434 GDK_CURRENT_TIME);
1435 #endif
1436 vc->s->kbd_owner = vc;
1437 gd_update_caption(vc->s);
1438 trace_gd_grab(vc->label, "kbd", reason);
1441 static void gd_ungrab_keyboard(GtkDisplayState *s)
1443 VirtualConsole *vc = s->kbd_owner;
1445 if (vc == NULL) {
1446 return;
1448 s->kbd_owner = NULL;
1450 #if GTK_CHECK_VERSION(3, 20, 0)
1451 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1452 #elif GTK_CHECK_VERSION(3, 0, 0)
1453 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1454 #else
1455 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1456 #endif
1457 gd_update_caption(s);
1458 trace_gd_ungrab(vc->label, "kbd");
1461 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1463 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1465 if (vc->s->ptr_owner) {
1466 if (vc->s->ptr_owner == vc) {
1467 return;
1468 } else {
1469 gd_ungrab_pointer(vc->s);
1473 #if GTK_CHECK_VERSION(3, 20, 0)
1474 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1475 gdk_device_get_position(gd_get_pointer(display),
1476 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1477 #elif GTK_CHECK_VERSION(3, 0, 0)
1478 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1479 GDK_POINTER_MOTION_MASK |
1480 GDK_BUTTON_PRESS_MASK |
1481 GDK_BUTTON_RELEASE_MASK |
1482 GDK_BUTTON_MOTION_MASK |
1483 GDK_SCROLL_MASK,
1484 vc->s->null_cursor);
1485 gdk_device_get_position(gd_get_pointer(display),
1486 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1487 #else
1488 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1489 FALSE, /* All events to come to our window directly */
1490 GDK_POINTER_MOTION_MASK |
1491 GDK_BUTTON_PRESS_MASK |
1492 GDK_BUTTON_RELEASE_MASK |
1493 GDK_BUTTON_MOTION_MASK |
1494 GDK_SCROLL_MASK,
1495 NULL, /* Allow cursor to move over entire desktop */
1496 vc->s->null_cursor,
1497 GDK_CURRENT_TIME);
1498 gdk_display_get_pointer(display, NULL,
1499 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1500 #endif
1501 vc->s->ptr_owner = vc;
1502 gd_update_caption(vc->s);
1503 trace_gd_grab(vc->label, "ptr", reason);
1506 static void gd_ungrab_pointer(GtkDisplayState *s)
1508 VirtualConsole *vc = s->ptr_owner;
1509 GdkDisplay *display;
1511 if (vc == NULL) {
1512 return;
1514 s->ptr_owner = NULL;
1516 display = gtk_widget_get_display(vc->gfx.drawing_area);
1517 #if GTK_CHECK_VERSION(3, 20, 0)
1518 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1519 gdk_device_warp(gd_get_pointer(display),
1520 gtk_widget_get_screen(vc->gfx.drawing_area),
1521 vc->s->grab_x_root, vc->s->grab_y_root);
1522 #elif GTK_CHECK_VERSION(3, 0, 0)
1523 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1524 gdk_device_warp(gd_get_pointer(display),
1525 gtk_widget_get_screen(vc->gfx.drawing_area),
1526 vc->s->grab_x_root, vc->s->grab_y_root);
1527 #else
1528 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1529 gdk_display_warp_pointer(display,
1530 gtk_widget_get_screen(vc->gfx.drawing_area),
1531 vc->s->grab_x_root, vc->s->grab_y_root);
1532 #endif
1533 gd_update_caption(s);
1534 trace_gd_ungrab(vc->label, "ptr");
1537 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1539 GtkDisplayState *s = opaque;
1540 VirtualConsole *vc = gd_vc_find_current(s);
1542 if (gd_is_grab_active(s)) {
1543 gd_grab_keyboard(vc, "user-request-main-window");
1544 gd_grab_pointer(vc, "user-request-main-window");
1545 } else {
1546 gd_ungrab_keyboard(s);
1547 gd_ungrab_pointer(s);
1550 gd_update_cursor(vc);
1553 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1554 gpointer data)
1556 GtkDisplayState *s = data;
1557 VirtualConsole *vc;
1558 gboolean on_vga;
1560 if (!gtk_widget_get_realized(s->notebook)) {
1561 return;
1564 #ifdef VTE_RESIZE_HACK
1565 vc = gd_vc_find_current(s);
1566 if (vc && vc->type == GD_VC_VTE) {
1567 gtk_widget_hide(vc->vte.terminal);
1569 #endif
1570 vc = gd_vc_find_by_page(s, arg2);
1571 if (!vc) {
1572 return;
1574 #ifdef VTE_RESIZE_HACK
1575 if (vc->type == GD_VC_VTE) {
1576 gtk_widget_show(vc->vte.terminal);
1578 #endif
1579 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1580 TRUE);
1581 on_vga = (vc->type == GD_VC_GFX &&
1582 qemu_console_is_graphic(vc->gfx.dcl.con));
1583 if (!on_vga) {
1584 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1585 FALSE);
1586 } else if (s->full_screen) {
1587 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1588 TRUE);
1590 gtk_widget_set_sensitive(s->grab_item, on_vga);
1591 #ifdef CONFIG_VTE
1592 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1593 #endif
1595 gd_update_windowsize(vc);
1596 gd_update_cursor(vc);
1599 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1600 gpointer opaque)
1602 VirtualConsole *vc = opaque;
1603 GtkDisplayState *s = vc->s;
1605 if (gd_grab_on_hover(s)) {
1606 gd_grab_keyboard(vc, "grab-on-hover");
1608 return TRUE;
1611 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1612 gpointer opaque)
1614 VirtualConsole *vc = opaque;
1615 GtkDisplayState *s = vc->s;
1617 if (gd_grab_on_hover(s)) {
1618 gd_ungrab_keyboard(s);
1620 return TRUE;
1623 static gboolean gd_focus_out_event(GtkWidget *widget,
1624 GdkEventCrossing *crossing, gpointer opaque)
1626 VirtualConsole *vc = opaque;
1627 GtkDisplayState *s = vc->s;
1629 gtk_release_modifiers(s);
1630 return TRUE;
1633 static gboolean gd_configure(GtkWidget *widget,
1634 GdkEventConfigure *cfg, gpointer opaque)
1636 VirtualConsole *vc = opaque;
1638 gd_set_ui_info(vc, cfg->width, cfg->height);
1639 return FALSE;
1642 /** Virtual Console Callbacks **/
1644 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1645 int idx, GSList *group, GtkWidget *view_menu)
1647 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1648 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1649 HOTKEY_MODIFIERS, 0,
1650 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1651 #if GTK_CHECK_VERSION(3, 8, 0)
1652 gtk_accel_label_set_accel(
1653 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1654 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1655 #endif
1657 g_signal_connect(vc->menu_item, "activate",
1658 G_CALLBACK(gd_menu_switch_vc), s);
1659 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1661 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1662 return group;
1665 #if defined(CONFIG_VTE)
1666 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1668 GtkDisplayState *s = opaque;
1669 VirtualConsole *vc = gd_vc_find_current(s);
1671 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1674 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1676 VirtualConsole *vc = opaque;
1678 if (gtk_adjustment_get_upper(adjustment) >
1679 gtk_adjustment_get_page_size(adjustment)) {
1680 gtk_widget_show(vc->vte.scrollbar);
1681 } else {
1682 gtk_widget_hide(vc->vte.scrollbar);
1686 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1688 VirtualConsole *vc = chr->opaque;
1690 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1691 return len;
1694 static void gd_vc_chr_set_echo(CharDriverState *chr, bool echo)
1696 VirtualConsole *vc = chr->opaque;
1698 vc->vte.echo = echo;
1701 static int nb_vcs;
1702 static CharDriverState *vcs[MAX_VCS];
1704 static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
1706 ChardevCommon *common = qapi_ChardevVC_base(vc);
1707 CharDriverState *chr;
1709 if (nb_vcs == MAX_VCS) {
1710 error_setg(errp, "Maximum number of consoles reached");
1711 return NULL;
1714 chr = qemu_chr_alloc(common, errp);
1715 if (!chr) {
1716 return NULL;
1719 chr->chr_write = gd_vc_chr_write;
1720 chr->chr_set_echo = gd_vc_chr_set_echo;
1722 /* Temporary, until gd_vc_vte_init runs. */
1723 chr->opaque = g_new0(VirtualConsole, 1);
1725 vcs[nb_vcs++] = chr;
1727 return chr;
1730 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1731 gpointer user_data)
1733 VirtualConsole *vc = user_data;
1735 if (vc->vte.echo) {
1736 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1737 int i;
1738 for (i = 0; i < size; i++) {
1739 uint8_t c = text[i];
1740 if (c >= 128 || isprint(c)) {
1741 /* 8-bit characters are considered printable. */
1742 vte_terminal_feed(term, &text[i], 1);
1743 } else if (c == '\r' || c == '\n') {
1744 vte_terminal_feed(term, "\r\n", 2);
1745 } else {
1746 char ctrl[2] = { '^', 0};
1747 ctrl[1] = text[i] ^ 64;
1748 vte_terminal_feed(term, ctrl, 2);
1753 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1754 return TRUE;
1757 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1758 CharDriverState *chr, int idx,
1759 GSList *group, GtkWidget *view_menu)
1761 char buffer[32];
1762 GtkWidget *box;
1763 GtkWidget *scrollbar;
1764 GtkAdjustment *vadjustment;
1765 VirtualConsole *tmp_vc = chr->opaque;
1767 vc->s = s;
1768 vc->vte.echo = tmp_vc->vte.echo;
1770 vc->vte.chr = chr;
1771 chr->opaque = vc;
1772 g_free(tmp_vc);
1774 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1775 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1776 ? vc->vte.chr->label : buffer);
1777 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1779 vc->vte.terminal = vte_terminal_new();
1780 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1782 /* The documentation says that the default is UTF-8, but actually it is
1783 * 7-bit ASCII at least in VTE 0.38.
1785 #if VTE_CHECK_VERSION(0, 38, 0)
1786 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1787 #else
1788 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1789 #endif
1791 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1792 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1793 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1795 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1796 vadjustment = gtk_scrollable_get_vadjustment
1797 (GTK_SCROLLABLE(vc->vte.terminal));
1798 #else
1799 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1800 #endif
1802 #if GTK_CHECK_VERSION(3, 0, 0)
1803 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1804 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1805 #else
1806 box = gtk_hbox_new(false, 2);
1807 scrollbar = gtk_vscrollbar_new(vadjustment);
1808 #endif
1810 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1811 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1813 vc->vte.box = box;
1814 vc->vte.scrollbar = scrollbar;
1816 g_signal_connect(vadjustment, "changed",
1817 G_CALLBACK(gd_vc_adjustment_changed), vc);
1819 vc->type = GD_VC_VTE;
1820 vc->tab_item = box;
1821 vc->focus = vc->vte.terminal;
1822 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1823 gtk_label_new(vc->label));
1825 qemu_chr_be_generic_open(vc->vte.chr);
1827 return group;
1830 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1831 GtkWidget *view_menu)
1833 int i;
1835 for (i = 0; i < nb_vcs; i++) {
1836 VirtualConsole *vc = &s->vc[s->nb_vcs];
1837 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1838 s->nb_vcs++;
1841 #endif /* CONFIG_VTE */
1843 /** Window Creation **/
1845 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1847 #if GTK_CHECK_VERSION(3, 0, 0)
1848 g_signal_connect(vc->gfx.drawing_area, "draw",
1849 G_CALLBACK(gd_draw_event), vc);
1850 #if defined(CONFIG_GTK_GL)
1851 if (display_opengl) {
1852 /* wire up GtkGlArea events */
1853 g_signal_connect(vc->gfx.drawing_area, "render",
1854 G_CALLBACK(gd_render_event), vc);
1855 g_signal_connect(vc->gfx.drawing_area, "resize",
1856 G_CALLBACK(gd_resize_event), vc);
1858 #endif
1859 #else
1860 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1861 G_CALLBACK(gd_expose_event), vc);
1862 #endif
1863 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1864 g_signal_connect(vc->gfx.drawing_area, "event",
1865 G_CALLBACK(gd_event), vc);
1866 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1867 G_CALLBACK(gd_button_event), vc);
1868 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1869 G_CALLBACK(gd_button_event), vc);
1870 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1871 G_CALLBACK(gd_scroll_event), vc);
1872 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1873 G_CALLBACK(gd_key_event), vc);
1874 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1875 G_CALLBACK(gd_key_event), vc);
1877 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1878 G_CALLBACK(gd_enter_event), vc);
1879 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1880 G_CALLBACK(gd_leave_event), vc);
1881 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1882 G_CALLBACK(gd_focus_out_event), vc);
1883 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1884 G_CALLBACK(gd_configure), vc);
1885 } else {
1886 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1887 G_CALLBACK(gd_text_key_down), vc);
1891 static void gd_connect_signals(GtkDisplayState *s)
1893 g_signal_connect(s->show_tabs_item, "activate",
1894 G_CALLBACK(gd_menu_show_tabs), s);
1895 g_signal_connect(s->untabify_item, "activate",
1896 G_CALLBACK(gd_menu_untabify), s);
1898 g_signal_connect(s->window, "delete-event",
1899 G_CALLBACK(gd_window_close), s);
1901 g_signal_connect(s->pause_item, "activate",
1902 G_CALLBACK(gd_menu_pause), s);
1903 g_signal_connect(s->reset_item, "activate",
1904 G_CALLBACK(gd_menu_reset), s);
1905 g_signal_connect(s->powerdown_item, "activate",
1906 G_CALLBACK(gd_menu_powerdown), s);
1907 g_signal_connect(s->quit_item, "activate",
1908 G_CALLBACK(gd_menu_quit), s);
1909 #if defined(CONFIG_VTE)
1910 g_signal_connect(s->copy_item, "activate",
1911 G_CALLBACK(gd_menu_copy), s);
1912 #endif
1913 g_signal_connect(s->full_screen_item, "activate",
1914 G_CALLBACK(gd_menu_full_screen), s);
1915 g_signal_connect(s->zoom_in_item, "activate",
1916 G_CALLBACK(gd_menu_zoom_in), s);
1917 g_signal_connect(s->zoom_out_item, "activate",
1918 G_CALLBACK(gd_menu_zoom_out), s);
1919 g_signal_connect(s->zoom_fixed_item, "activate",
1920 G_CALLBACK(gd_menu_zoom_fixed), s);
1921 g_signal_connect(s->zoom_fit_item, "activate",
1922 G_CALLBACK(gd_menu_zoom_fit), s);
1923 g_signal_connect(s->grab_item, "activate",
1924 G_CALLBACK(gd_menu_grab_input), s);
1925 g_signal_connect(s->notebook, "switch-page",
1926 G_CALLBACK(gd_change_page), s);
1929 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1931 GtkWidget *machine_menu;
1932 GtkWidget *separator;
1934 machine_menu = gtk_menu_new();
1935 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1937 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1938 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1940 separator = gtk_separator_menu_item_new();
1941 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1943 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1944 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1946 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1947 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1949 separator = gtk_separator_menu_item_new();
1950 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1952 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1953 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1954 "<QEMU>/Machine/Quit");
1955 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1956 GDK_KEY_q, HOTKEY_MODIFIERS);
1957 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1959 return machine_menu;
1962 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1963 QemuConsole *con, int idx,
1964 GSList *group, GtkWidget *view_menu)
1966 vc->label = qemu_console_get_label(con);
1967 vc->s = s;
1968 vc->gfx.scale_x = 1.0;
1969 vc->gfx.scale_y = 1.0;
1971 #if defined(CONFIG_OPENGL)
1972 if (display_opengl) {
1973 #if defined(CONFIG_GTK_GL)
1974 vc->gfx.drawing_area = gtk_gl_area_new();
1975 vc->gfx.dcl.ops = &dcl_gl_area_ops;
1976 #else
1977 vc->gfx.drawing_area = gtk_drawing_area_new();
1979 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1980 * It is required for opengl rendering on X11 though. A
1981 * proper replacement (native opengl support) is only
1982 * available in 3.16+. Silence the warning if possible.
1984 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1985 #pragma GCC diagnostic push
1986 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1987 #endif
1988 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1989 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1990 #pragma GCC diagnostic pop
1991 #endif
1992 vc->gfx.dcl.ops = &dcl_egl_ops;
1993 #endif /* CONFIG_GTK_GL */
1994 } else
1995 #endif
1997 vc->gfx.drawing_area = gtk_drawing_area_new();
1998 vc->gfx.dcl.ops = &dcl_ops;
2002 gtk_widget_add_events(vc->gfx.drawing_area,
2003 GDK_POINTER_MOTION_MASK |
2004 GDK_BUTTON_PRESS_MASK |
2005 GDK_BUTTON_RELEASE_MASK |
2006 GDK_BUTTON_MOTION_MASK |
2007 GDK_ENTER_NOTIFY_MASK |
2008 GDK_LEAVE_NOTIFY_MASK |
2009 GDK_SCROLL_MASK |
2010 GDK_KEY_PRESS_MASK);
2011 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2013 vc->type = GD_VC_GFX;
2014 vc->tab_item = vc->gfx.drawing_area;
2015 vc->focus = vc->gfx.drawing_area;
2016 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2017 vc->tab_item, gtk_label_new(vc->label));
2019 vc->gfx.dcl.con = con;
2020 register_displaychangelistener(&vc->gfx.dcl);
2022 gd_connect_vc_gfx_signals(vc);
2023 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2025 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2026 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2027 s->free_scale = true;
2030 return group;
2033 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2035 GSList *group = NULL;
2036 GtkWidget *view_menu;
2037 GtkWidget *separator;
2038 QemuConsole *con;
2039 int vc;
2041 view_menu = gtk_menu_new();
2042 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2044 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2046 #if defined(CONFIG_VTE)
2047 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2048 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2049 #endif
2051 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2052 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2053 #if GTK_CHECK_VERSION(3, 8, 0)
2054 gtk_accel_label_set_accel(
2055 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2056 GDK_KEY_f, HOTKEY_MODIFIERS);
2057 #endif
2058 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2060 separator = gtk_separator_menu_item_new();
2061 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2063 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2064 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2065 "<QEMU>/View/Zoom In");
2066 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2067 HOTKEY_MODIFIERS);
2068 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2070 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2071 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2072 "<QEMU>/View/Zoom Out");
2073 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2074 HOTKEY_MODIFIERS);
2075 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2077 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2078 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2079 "<QEMU>/View/Zoom Fixed");
2080 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2081 HOTKEY_MODIFIERS);
2082 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2084 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2085 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2087 separator = gtk_separator_menu_item_new();
2088 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2090 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2091 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2093 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2094 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2095 "<QEMU>/View/Grab Input");
2096 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2097 HOTKEY_MODIFIERS);
2098 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2100 separator = gtk_separator_menu_item_new();
2101 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2103 /* gfx */
2104 for (vc = 0;; vc++) {
2105 con = qemu_console_lookup_by_index(vc);
2106 if (!con) {
2107 break;
2109 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2110 vc, group, view_menu);
2111 s->nb_vcs++;
2114 #if defined(CONFIG_VTE)
2115 /* vte */
2116 gd_vcs_init(s, group, view_menu);
2117 #endif
2119 separator = gtk_separator_menu_item_new();
2120 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2122 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2123 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2125 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2126 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2128 return view_menu;
2131 static void gd_create_menus(GtkDisplayState *s)
2133 s->accel_group = gtk_accel_group_new();
2134 s->machine_menu = gd_create_menu_machine(s);
2135 s->view_menu = gd_create_menu_view(s);
2137 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2138 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2139 s->machine_menu);
2140 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2142 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2143 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2144 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2146 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2147 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2150 static void gd_set_keycode_type(GtkDisplayState *s)
2152 #ifdef GDK_WINDOWING_X11
2153 GdkDisplay *display = gtk_widget_get_display(s->window);
2154 if (GDK_IS_X11_DISPLAY(display)) {
2155 Display *x11_display = gdk_x11_display_get_xdisplay(display);
2156 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
2157 XkbUseCoreKbd);
2158 char *keycodes = NULL;
2160 if (desc && desc->names) {
2161 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
2163 if (keycodes == NULL) {
2164 fprintf(stderr, "could not lookup keycode name\n");
2165 } else if (strstart(keycodes, "evdev", NULL)) {
2166 s->has_evdev = true;
2167 } else if (!strstart(keycodes, "xfree86", NULL)) {
2168 fprintf(stderr, "unknown keycodes `%s', please report to "
2169 "qemu-devel@nongnu.org\n", keycodes);
2172 if (desc) {
2173 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
2175 if (keycodes) {
2176 XFree(keycodes);
2179 #endif
2182 static gboolean gtkinit;
2184 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
2186 VirtualConsole *vc;
2188 GtkDisplayState *s = g_malloc0(sizeof(*s));
2189 char *filename;
2190 GdkDisplay *window_display;
2192 if (!gtkinit) {
2193 fprintf(stderr, "gtk initialization failed\n");
2194 exit(1);
2197 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2198 #if GTK_CHECK_VERSION(3, 2, 0)
2199 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2200 #else
2201 s->vbox = gtk_vbox_new(FALSE, 0);
2202 #endif
2203 s->notebook = gtk_notebook_new();
2204 s->menu_bar = gtk_menu_bar_new();
2206 s->free_scale = FALSE;
2208 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2209 setlocale(LC_MESSAGES, "");
2210 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2211 textdomain("qemu");
2213 window_display = gtk_widget_get_display(s->window);
2214 s->null_cursor = gdk_cursor_new_for_display(window_display,
2215 GDK_BLANK_CURSOR);
2217 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2218 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2219 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2221 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2222 if (filename) {
2223 GError *error = NULL;
2224 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2225 if (pixbuf) {
2226 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2227 } else {
2228 g_error_free(error);
2230 g_free(filename);
2233 gd_create_menus(s);
2235 gd_connect_signals(s);
2237 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2238 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2240 gd_update_caption(s);
2242 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2243 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2245 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2247 gtk_widget_show_all(s->window);
2249 #ifdef VTE_RESIZE_HACK
2251 VirtualConsole *cur = gd_vc_find_current(s);
2252 if (cur) {
2253 int i;
2255 for (i = 0; i < s->nb_vcs; i++) {
2256 VirtualConsole *vc = &s->vc[i];
2257 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2258 gtk_widget_hide(vc->vte.terminal);
2261 gd_update_windowsize(cur);
2264 #endif
2266 vc = gd_vc_find_current(s);
2267 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2268 #ifdef CONFIG_VTE
2269 gtk_widget_set_sensitive(s->copy_item,
2270 vc && vc->type == GD_VC_VTE);
2271 #endif
2273 if (full_screen) {
2274 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2276 if (grab_on_hover) {
2277 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2280 gd_set_keycode_type(s);
2283 void early_gtk_display_init(int opengl)
2285 /* The QEMU code relies on the assumption that it's always run in
2286 * the C locale. Therefore it is not prepared to deal with
2287 * operations that produce different results depending on the
2288 * locale, such as printf's formatting of decimal numbers, and
2289 * possibly others.
2291 * Since GTK+ calls setlocale() by default -importing the locale
2292 * settings from the environment- we must prevent it from doing so
2293 * using gtk_disable_setlocale().
2295 * QEMU's GTK+ UI, however, _does_ have translations for some of
2296 * the menu items. As a trade-off between a functionally correct
2297 * QEMU and a fully internationalized UI we support importing
2298 * LC_MESSAGES from the environment (see the setlocale() call
2299 * earlier in this file). This allows us to display translated
2300 * messages leaving everything else untouched.
2302 gtk_disable_setlocale();
2303 gtkinit = gtk_init_check(NULL, NULL);
2304 if (!gtkinit) {
2305 /* don't exit yet, that'll break -help */
2306 return;
2309 switch (opengl) {
2310 case -1: /* default */
2311 case 0: /* off */
2312 break;
2313 case 1: /* on */
2314 #if defined(CONFIG_OPENGL)
2315 #if defined(CONFIG_GTK_GL)
2316 gtk_gl_area_init();
2317 #else
2318 gtk_egl_init();
2319 #endif
2320 #endif
2321 break;
2322 default:
2323 g_assert_not_reached();
2324 break;
2327 #if defined(CONFIG_VTE)
2328 register_vc_handler(gd_vc_handler);
2329 #endif