tests: add qmp/missing-any-arg test
[qemu/ar7.git] / ui / gtk.c
blob579990b8655f3f63d378bfcbfd818de0aa61245b
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 "qapi/error.h"
40 #include "qapi/qapi-commands-misc.h"
41 #include "qemu/cutils.h"
43 #include "ui/console.h"
44 #include "ui/gtk.h"
46 #include <glib/gi18n.h>
47 #include <locale.h>
48 #if defined(CONFIG_VTE)
49 #include <vte/vte.h>
50 #endif
51 #include <math.h>
53 #include "trace.h"
54 #include "ui/input.h"
55 #include "sysemu/sysemu.h"
56 #include "keymaps.h"
57 #include "chardev/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 #ifdef GDK_WINDOWING_X11
69 #include "x_keymap.h"
71 /* Gtk2 compat */
72 #ifndef GDK_IS_X11_DISPLAY
73 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
74 #endif
75 #endif
78 #ifdef GDK_WINDOWING_WAYLAND
79 /* Gtk2 compat */
80 #ifndef GDK_IS_WAYLAND_DISPLAY
81 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
82 #endif
83 #endif
86 #ifdef GDK_WINDOWING_WIN32
87 /* Gtk2 compat */
88 #ifndef GDK_IS_WIN32_DISPLAY
89 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
90 #endif
91 #endif
94 #ifdef GDK_WINDOWING_BROADWAY
95 /* Gtk2 compat */
96 #ifndef GDK_IS_BROADWAY_DISPLAY
97 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
98 #endif
99 #endif
102 #ifdef GDK_WINDOWING_QUARTZ
103 /* Gtk2 compat */
104 #ifndef GDK_IS_QUARTZ_DISPLAY
105 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
106 #endif
107 #endif
110 #if !defined(CONFIG_VTE)
111 # define VTE_CHECK_VERSION(a, b, c) 0
112 #endif
114 /* Some older mingw versions lack this constant or have
115 * it conditionally defined */
116 #ifdef _WIN32
117 # ifndef MAPVK_VK_TO_VSC
118 # define MAPVK_VK_TO_VSC 0
119 # endif
120 #endif
123 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
125 static const int modifier_keycode[] = {
126 Q_KEY_CODE_SHIFT,
127 Q_KEY_CODE_SHIFT_R,
128 Q_KEY_CODE_CTRL,
129 Q_KEY_CODE_CTRL_R,
130 Q_KEY_CODE_ALT,
131 Q_KEY_CODE_ALT_R,
132 Q_KEY_CODE_META_L,
133 Q_KEY_CODE_META_R,
136 static const guint16 *keycode_map;
137 static size_t keycode_maplen;
139 struct GtkDisplayState {
140 GtkWidget *window;
142 GtkWidget *menu_bar;
144 GtkAccelGroup *accel_group;
146 GtkWidget *machine_menu_item;
147 GtkWidget *machine_menu;
148 GtkWidget *pause_item;
149 GtkWidget *reset_item;
150 GtkWidget *powerdown_item;
151 GtkWidget *quit_item;
153 GtkWidget *view_menu_item;
154 GtkWidget *view_menu;
155 GtkWidget *full_screen_item;
156 GtkWidget *copy_item;
157 GtkWidget *zoom_in_item;
158 GtkWidget *zoom_out_item;
159 GtkWidget *zoom_fixed_item;
160 GtkWidget *zoom_fit_item;
161 GtkWidget *grab_item;
162 GtkWidget *grab_on_hover_item;
164 int nb_vcs;
165 VirtualConsole vc[MAX_VCS];
167 GtkWidget *show_tabs_item;
168 GtkWidget *untabify_item;
169 GtkWidget *show_menubar_item;
171 GtkWidget *vbox;
172 GtkWidget *notebook;
173 int button_mask;
174 gboolean last_set;
175 int last_x;
176 int last_y;
177 int grab_x_root;
178 int grab_y_root;
179 VirtualConsole *kbd_owner;
180 VirtualConsole *ptr_owner;
182 gboolean full_screen;
184 GdkCursor *null_cursor;
185 Notifier mouse_mode_notifier;
186 gboolean free_scale;
188 bool external_pause_update;
190 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
191 bool ignore_keys;
193 DisplayOptions *opts;
196 typedef struct VCChardev {
197 Chardev parent;
198 VirtualConsole *console;
199 bool echo;
200 } VCChardev;
202 #define TYPE_CHARDEV_VC "chardev-vc"
203 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
205 bool gtk_use_gl_area;
207 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
208 static void gd_ungrab_pointer(GtkDisplayState *s);
209 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
210 static void gd_ungrab_keyboard(GtkDisplayState *s);
212 /** Utility Functions **/
214 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
216 VirtualConsole *vc;
217 gint i;
219 for (i = 0; i < s->nb_vcs; i++) {
220 vc = &s->vc[i];
221 if (gtk_check_menu_item_get_active
222 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
223 return vc;
226 return NULL;
229 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
231 VirtualConsole *vc;
232 gint i, p;
234 for (i = 0; i < s->nb_vcs; i++) {
235 vc = &s->vc[i];
236 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
237 if (p == page) {
238 return vc;
241 return NULL;
244 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
246 gint page;
248 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
249 return gd_vc_find_by_page(s, page);
252 static bool gd_is_grab_active(GtkDisplayState *s)
254 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
257 static bool gd_grab_on_hover(GtkDisplayState *s)
259 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
262 static void gd_update_cursor(VirtualConsole *vc)
264 GtkDisplayState *s = vc->s;
265 GdkWindow *window;
267 if (vc->type != GD_VC_GFX ||
268 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
269 return;
272 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
273 return;
276 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
277 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
278 gdk_window_set_cursor(window, s->null_cursor);
279 } else {
280 gdk_window_set_cursor(window, NULL);
284 static void gd_update_caption(GtkDisplayState *s)
286 const char *status = "";
287 gchar *prefix;
288 gchar *title;
289 const char *grab = "";
290 bool is_paused = !runstate_is_running();
291 int i;
293 if (qemu_name) {
294 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
295 } else {
296 prefix = g_strdup_printf("QEMU");
299 if (s->ptr_owner != NULL &&
300 s->ptr_owner->window == NULL) {
301 grab = _(" - Press Ctrl+Alt+G to release grab");
304 if (is_paused) {
305 status = _(" [Paused]");
307 s->external_pause_update = true;
308 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
309 is_paused);
310 s->external_pause_update = false;
312 title = g_strdup_printf("%s%s%s", prefix, status, grab);
313 gtk_window_set_title(GTK_WINDOW(s->window), title);
314 g_free(title);
316 for (i = 0; i < s->nb_vcs; i++) {
317 VirtualConsole *vc = &s->vc[i];
319 if (!vc->window) {
320 continue;
322 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
323 vc == s->kbd_owner ? " +kbd" : "",
324 vc == s->ptr_owner ? " +ptr" : "");
325 gtk_window_set_title(GTK_WINDOW(vc->window), title);
326 g_free(title);
329 g_free(prefix);
332 static void gd_update_geometry_hints(VirtualConsole *vc)
334 GtkDisplayState *s = vc->s;
335 GdkWindowHints mask = 0;
336 GdkGeometry geo = {};
337 GtkWidget *geo_widget = NULL;
338 GtkWindow *geo_window;
340 if (vc->type == GD_VC_GFX) {
341 if (!vc->gfx.ds) {
342 return;
344 if (s->free_scale) {
345 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
346 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
347 mask |= GDK_HINT_MIN_SIZE;
348 } else {
349 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
350 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
351 mask |= GDK_HINT_MIN_SIZE;
353 geo_widget = vc->gfx.drawing_area;
354 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
356 #if defined(CONFIG_VTE)
357 } else if (vc->type == GD_VC_VTE) {
358 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
359 GtkBorder padding = { 0 };
361 #if VTE_CHECK_VERSION(0, 37, 0)
362 gtk_style_context_get_padding(
363 gtk_widget_get_style_context(vc->vte.terminal),
364 gtk_widget_get_state_flags(vc->vte.terminal),
365 &padding);
366 #else
368 GtkBorder *ib = NULL;
369 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
370 if (ib) {
371 padding = *ib;
372 gtk_border_free(ib);
375 #endif
377 geo.width_inc = vte_terminal_get_char_width(term);
378 geo.height_inc = vte_terminal_get_char_height(term);
379 mask |= GDK_HINT_RESIZE_INC;
380 geo.base_width = geo.width_inc;
381 geo.base_height = geo.height_inc;
382 mask |= GDK_HINT_BASE_SIZE;
383 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
384 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
385 mask |= GDK_HINT_MIN_SIZE;
387 geo.base_width += padding.left + padding.right;
388 geo.base_height += padding.top + padding.bottom;
389 geo.min_width += padding.left + padding.right;
390 geo.min_height += padding.top + padding.bottom;
391 geo_widget = vc->vte.terminal;
392 #endif
395 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
396 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
399 void gd_update_windowsize(VirtualConsole *vc)
401 GtkDisplayState *s = vc->s;
403 gd_update_geometry_hints(vc);
405 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
406 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
407 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
411 static void gd_update_full_redraw(VirtualConsole *vc)
413 GtkWidget *area = vc->gfx.drawing_area;
414 int ww, wh;
415 ww = gdk_window_get_width(gtk_widget_get_window(area));
416 wh = gdk_window_get_height(gtk_widget_get_window(area));
417 #if defined(CONFIG_GTK_GL)
418 if (vc->gfx.gls && gtk_use_gl_area) {
419 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
420 return;
422 #endif
423 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
426 static void gtk_release_modifiers(GtkDisplayState *s)
428 VirtualConsole *vc = gd_vc_find_current(s);
429 int i, qcode;
431 if (vc->type != GD_VC_GFX ||
432 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
433 return;
435 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
436 qcode = modifier_keycode[i];
437 if (!s->modifier_pressed[i]) {
438 continue;
440 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode, false);
441 s->modifier_pressed[i] = false;
445 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
446 GtkWidget *widget)
448 g_object_ref(G_OBJECT(widget));
449 gtk_container_remove(GTK_CONTAINER(from), widget);
450 gtk_container_add(GTK_CONTAINER(to), widget);
451 g_object_unref(G_OBJECT(widget));
454 /** DisplayState Callbacks **/
456 static void gd_update(DisplayChangeListener *dcl,
457 int x, int y, int w, int h)
459 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
460 GdkWindow *win;
461 int x1, x2, y1, y2;
462 int mx, my;
463 int fbw, fbh;
464 int ww, wh;
466 trace_gd_update(vc->label, x, y, w, h);
468 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
469 return;
472 if (vc->gfx.convert) {
473 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
474 NULL, vc->gfx.convert,
475 x, y, 0, 0, x, y, w, h);
478 x1 = floor(x * vc->gfx.scale_x);
479 y1 = floor(y * vc->gfx.scale_y);
481 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
482 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
484 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
485 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
487 win = gtk_widget_get_window(vc->gfx.drawing_area);
488 if (!win) {
489 return;
491 ww = gdk_window_get_width(win);
492 wh = gdk_window_get_height(win);
494 mx = my = 0;
495 if (ww > fbw) {
496 mx = (ww - fbw) / 2;
498 if (wh > fbh) {
499 my = (wh - fbh) / 2;
502 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
503 mx + x1, my + y1, (x2 - x1), (y2 - y1));
506 static void gd_refresh(DisplayChangeListener *dcl)
508 graphic_hw_update(dcl->con);
511 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
513 #if GTK_CHECK_VERSION(3, 20, 0)
514 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
515 #else
516 return gdk_device_manager_get_client_pointer(
517 gdk_display_get_device_manager(dpy));
518 #endif
521 static void gd_mouse_set(DisplayChangeListener *dcl,
522 int x, int y, int visible)
524 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
525 GdkDisplay *dpy;
526 gint x_root, y_root;
528 if (qemu_input_is_absolute()) {
529 return;
532 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
533 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
534 x, y, &x_root, &y_root);
535 gdk_device_warp(gd_get_pointer(dpy),
536 gtk_widget_get_screen(vc->gfx.drawing_area),
537 x_root, y_root);
538 vc->s->last_x = x;
539 vc->s->last_y = y;
542 static void gd_cursor_define(DisplayChangeListener *dcl,
543 QEMUCursor *c)
545 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
546 GdkPixbuf *pixbuf;
547 GdkCursor *cursor;
549 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
550 return;
553 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
554 GDK_COLORSPACE_RGB, true, 8,
555 c->width, c->height, c->width * 4,
556 NULL, NULL);
557 cursor = gdk_cursor_new_from_pixbuf
558 (gtk_widget_get_display(vc->gfx.drawing_area),
559 pixbuf, c->hot_x, c->hot_y);
560 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
561 g_object_unref(pixbuf);
562 g_object_unref(cursor);
565 static void gd_switch(DisplayChangeListener *dcl,
566 DisplaySurface *surface)
568 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
569 bool resized = true;
571 trace_gd_switch(vc->label,
572 surface ? surface_width(surface) : 0,
573 surface ? surface_height(surface) : 0);
575 if (vc->gfx.surface) {
576 cairo_surface_destroy(vc->gfx.surface);
577 vc->gfx.surface = NULL;
579 if (vc->gfx.convert) {
580 pixman_image_unref(vc->gfx.convert);
581 vc->gfx.convert = NULL;
584 if (vc->gfx.ds && surface &&
585 surface_width(vc->gfx.ds) == surface_width(surface) &&
586 surface_height(vc->gfx.ds) == surface_height(surface)) {
587 resized = false;
589 vc->gfx.ds = surface;
591 if (!surface) {
592 return;
595 if (surface->format == PIXMAN_x8r8g8b8) {
597 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
599 * No need to convert, use surface directly. Should be the
600 * common case as this is qemu_default_pixelformat(32) too.
602 vc->gfx.surface = cairo_image_surface_create_for_data
603 (surface_data(surface),
604 CAIRO_FORMAT_RGB24,
605 surface_width(surface),
606 surface_height(surface),
607 surface_stride(surface));
608 } else {
609 /* Must convert surface, use pixman to do it. */
610 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
611 surface_width(surface),
612 surface_height(surface),
613 NULL, 0);
614 vc->gfx.surface = cairo_image_surface_create_for_data
615 ((void *)pixman_image_get_data(vc->gfx.convert),
616 CAIRO_FORMAT_RGB24,
617 pixman_image_get_width(vc->gfx.convert),
618 pixman_image_get_height(vc->gfx.convert),
619 pixman_image_get_stride(vc->gfx.convert));
620 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
621 NULL, vc->gfx.convert,
622 0, 0, 0, 0, 0, 0,
623 pixman_image_get_width(vc->gfx.convert),
624 pixman_image_get_height(vc->gfx.convert));
627 if (resized) {
628 gd_update_windowsize(vc);
629 } else {
630 gd_update_full_redraw(vc);
634 static const DisplayChangeListenerOps dcl_ops = {
635 .dpy_name = "gtk",
636 .dpy_gfx_update = gd_update,
637 .dpy_gfx_switch = gd_switch,
638 .dpy_gfx_check_format = qemu_pixman_check_format,
639 .dpy_refresh = gd_refresh,
640 .dpy_mouse_set = gd_mouse_set,
641 .dpy_cursor_define = gd_cursor_define,
645 #if defined(CONFIG_OPENGL)
647 /** DisplayState Callbacks (opengl version) **/
649 #if defined(CONFIG_GTK_GL)
651 static const DisplayChangeListenerOps dcl_gl_area_ops = {
652 .dpy_name = "gtk-egl",
653 .dpy_gfx_update = gd_gl_area_update,
654 .dpy_gfx_switch = gd_gl_area_switch,
655 .dpy_gfx_check_format = console_gl_check_format,
656 .dpy_refresh = gd_gl_area_refresh,
657 .dpy_mouse_set = gd_mouse_set,
658 .dpy_cursor_define = gd_cursor_define,
660 .dpy_gl_ctx_create = gd_gl_area_create_context,
661 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
662 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
663 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
664 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
665 .dpy_gl_update = gd_gl_area_scanout_flush,
668 #endif /* CONFIG_GTK_GL */
670 static const DisplayChangeListenerOps dcl_egl_ops = {
671 .dpy_name = "gtk-egl",
672 .dpy_gfx_update = gd_egl_update,
673 .dpy_gfx_switch = gd_egl_switch,
674 .dpy_gfx_check_format = console_gl_check_format,
675 .dpy_refresh = gd_egl_refresh,
676 .dpy_mouse_set = gd_mouse_set,
677 .dpy_cursor_define = gd_cursor_define,
679 .dpy_gl_ctx_create = gd_egl_create_context,
680 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
681 .dpy_gl_ctx_make_current = gd_egl_make_current,
682 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
683 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
684 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
685 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
686 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
687 .dpy_gl_cursor_position = gd_egl_cursor_position,
688 .dpy_gl_release_dmabuf = gd_egl_release_dmabuf,
689 .dpy_gl_update = gd_egl_scanout_flush,
692 #endif /* CONFIG_OPENGL */
694 /** QEMU Events **/
696 static void gd_change_runstate(void *opaque, int running, RunState state)
698 GtkDisplayState *s = opaque;
700 gd_update_caption(s);
703 static void gd_mouse_mode_change(Notifier *notify, void *data)
705 GtkDisplayState *s;
706 int i;
708 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
709 /* release the grab at switching to absolute mode */
710 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
711 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
712 FALSE);
714 for (i = 0; i < s->nb_vcs; i++) {
715 VirtualConsole *vc = &s->vc[i];
716 gd_update_cursor(vc);
720 /** GTK Events **/
722 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
723 void *opaque)
725 GtkDisplayState *s = opaque;
726 bool allow_close = true;
728 if (s->opts->has_window_close && !s->opts->window_close) {
729 allow_close = false;
732 if (allow_close) {
733 qmp_quit(NULL);
736 return TRUE;
739 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
741 QemuUIInfo info;
743 memset(&info, 0, sizeof(info));
744 info.width = width;
745 info.height = height;
746 dpy_set_ui_info(vc->gfx.dcl.con, &info);
749 #if defined(CONFIG_GTK_GL)
751 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
752 void *opaque)
754 VirtualConsole *vc = opaque;
756 if (vc->gfx.gls) {
757 gd_gl_area_draw(vc);
759 return TRUE;
762 static void gd_resize_event(GtkGLArea *area,
763 gint width, gint height, gpointer *opaque)
765 VirtualConsole *vc = (void *)opaque;
767 gd_set_ui_info(vc, width, height);
770 #endif
772 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
774 VirtualConsole *vc = opaque;
775 GtkDisplayState *s = vc->s;
776 int mx, my;
777 int ww, wh;
778 int fbw, fbh;
780 #if defined(CONFIG_OPENGL)
781 if (vc->gfx.gls) {
782 if (gtk_use_gl_area) {
783 /* invoke render callback please */
784 return FALSE;
785 } else {
786 gd_egl_draw(vc);
787 return TRUE;
790 #endif
792 if (!gtk_widget_get_realized(widget)) {
793 return FALSE;
795 if (!vc->gfx.ds) {
796 return FALSE;
799 fbw = surface_width(vc->gfx.ds);
800 fbh = surface_height(vc->gfx.ds);
802 ww = gdk_window_get_width(gtk_widget_get_window(widget));
803 wh = gdk_window_get_height(gtk_widget_get_window(widget));
805 if (s->full_screen) {
806 vc->gfx.scale_x = (double)ww / fbw;
807 vc->gfx.scale_y = (double)wh / fbh;
808 } else if (s->free_scale) {
809 double sx, sy;
811 sx = (double)ww / fbw;
812 sy = (double)wh / fbh;
814 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
817 fbw *= vc->gfx.scale_x;
818 fbh *= vc->gfx.scale_y;
820 mx = my = 0;
821 if (ww > fbw) {
822 mx = (ww - fbw) / 2;
824 if (wh > fbh) {
825 my = (wh - fbh) / 2;
828 cairo_rectangle(cr, 0, 0, ww, wh);
830 /* Optionally cut out the inner area where the pixmap
831 will be drawn. This avoids 'flashing' since we're
832 not double-buffering. Note we're using the undocumented
833 behaviour of drawing the rectangle from right to left
834 to cut out the whole */
835 cairo_rectangle(cr, mx + fbw, my,
836 -1 * fbw, fbh);
837 cairo_fill(cr);
839 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
840 cairo_set_source_surface(cr, vc->gfx.surface,
841 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
842 cairo_paint(cr);
844 return TRUE;
847 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
848 void *opaque)
850 VirtualConsole *vc = opaque;
851 GtkDisplayState *s = vc->s;
852 int x, y;
853 int mx, my;
854 int fbh, fbw;
855 int ww, wh;
857 if (!vc->gfx.ds) {
858 return TRUE;
861 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
862 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
864 ww = gdk_window_get_width(gtk_widget_get_window(vc->gfx.drawing_area));
865 wh = gdk_window_get_height(gtk_widget_get_window(vc->gfx.drawing_area));
867 mx = my = 0;
868 if (ww > fbw) {
869 mx = (ww - fbw) / 2;
871 if (wh > fbh) {
872 my = (wh - fbh) / 2;
875 x = (motion->x - mx) / vc->gfx.scale_x;
876 y = (motion->y - my) / vc->gfx.scale_y;
878 if (qemu_input_is_absolute()) {
879 if (x < 0 || y < 0 ||
880 x >= surface_width(vc->gfx.ds) ||
881 y >= surface_height(vc->gfx.ds)) {
882 return TRUE;
884 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
885 0, surface_width(vc->gfx.ds));
886 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
887 0, surface_height(vc->gfx.ds));
888 qemu_input_event_sync();
889 } else if (s->last_set && s->ptr_owner == vc) {
890 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
891 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
892 qemu_input_event_sync();
894 s->last_x = x;
895 s->last_y = y;
896 s->last_set = TRUE;
898 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
899 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
900 int screen_width, screen_height;
902 int x = (int)motion->x_root;
903 int y = (int)motion->y_root;
905 #if GTK_CHECK_VERSION(3, 22, 0)
907 GdkDisplay *dpy = gtk_widget_get_display(widget);
908 GdkWindow *win = gtk_widget_get_window(widget);
909 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
910 GdkRectangle geometry;
911 gdk_monitor_get_geometry(monitor, &geometry);
912 screen_width = geometry.width;
913 screen_height = geometry.height;
915 #else
917 screen_width = gdk_screen_get_width(screen);
918 screen_height = gdk_screen_get_height(screen);
920 #endif
922 /* In relative mode check to see if client pointer hit
923 * one of the screen edges, and if so move it back by
924 * 200 pixels. This is important because the pointer
925 * in the server doesn't correspond 1-for-1, and so
926 * may still be only half way across the screen. Without
927 * this warp, the server pointer would thus appear to hit
928 * an invisible wall */
929 if (x == 0) {
930 x += 200;
932 if (y == 0) {
933 y += 200;
935 if (x == (screen_width - 1)) {
936 x -= 200;
938 if (y == (screen_height - 1)) {
939 y -= 200;
942 if (x != (int)motion->x_root || y != (int)motion->y_root) {
943 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
944 gdk_device_warp(dev, screen, x, y);
945 s->last_set = FALSE;
946 return FALSE;
949 return TRUE;
952 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
953 void *opaque)
955 VirtualConsole *vc = opaque;
956 GtkDisplayState *s = vc->s;
957 InputButton btn;
959 /* implicitly grab the input at the first click in the relative mode */
960 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
961 !qemu_input_is_absolute() && s->ptr_owner != vc) {
962 if (!vc->window) {
963 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
964 TRUE);
965 } else {
966 gd_grab_pointer(vc, "relative-mode-click");
968 return TRUE;
971 if (button->button == 1) {
972 btn = INPUT_BUTTON_LEFT;
973 } else if (button->button == 2) {
974 btn = INPUT_BUTTON_MIDDLE;
975 } else if (button->button == 3) {
976 btn = INPUT_BUTTON_RIGHT;
977 } else if (button->button == 8) {
978 btn = INPUT_BUTTON_SIDE;
979 } else if (button->button == 9) {
980 btn = INPUT_BUTTON_EXTRA;
981 } else {
982 return TRUE;
985 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
986 button->type == GDK_BUTTON_PRESS);
987 qemu_input_event_sync();
988 return TRUE;
991 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
992 void *opaque)
994 VirtualConsole *vc = opaque;
995 InputButton btn;
997 if (scroll->direction == GDK_SCROLL_UP) {
998 btn = INPUT_BUTTON_WHEEL_UP;
999 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1000 btn = INPUT_BUTTON_WHEEL_DOWN;
1001 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1002 gdouble delta_x, delta_y;
1003 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1004 &delta_x, &delta_y)) {
1005 return TRUE;
1007 if (delta_y > 0) {
1008 btn = INPUT_BUTTON_WHEEL_DOWN;
1009 } else {
1010 btn = INPUT_BUTTON_WHEEL_UP;
1012 } else {
1013 return TRUE;
1016 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1017 qemu_input_event_sync();
1018 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1019 qemu_input_event_sync();
1020 return TRUE;
1024 static const guint16 *gd_get_keymap(size_t *maplen)
1026 GdkDisplay *dpy = gdk_display_get_default();
1028 #ifdef GDK_WINDOWING_X11
1029 if (GDK_IS_X11_DISPLAY(dpy)) {
1030 trace_gd_keymap_windowing("x11");
1031 return qemu_xkeymap_mapping_table(
1032 gdk_x11_display_get_xdisplay(dpy), maplen);
1034 #endif
1036 #ifdef GDK_WINDOWING_WAYLAND
1037 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1038 trace_gd_keymap_windowing("wayland");
1039 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1040 return qemu_input_map_xorgevdev_to_qcode;
1042 #endif
1044 #ifdef GDK_WINDOWING_WIN32
1045 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1046 trace_gd_keymap_windowing("win32");
1047 *maplen = qemu_input_map_win32_to_qcode_len;
1048 return qemu_input_map_win32_to_qcode;
1050 #endif
1052 #ifdef GDK_WINDOWING_QUARTZ
1053 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1054 trace_gd_keymap_windowing("quartz");
1055 *maplen = qemu_input_map_osx_to_qcode_len;
1056 return qemu_input_map_osx_to_qcode;
1058 #endif
1060 #ifdef GDK_WINDOWING_BROADWAY
1061 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1062 trace_gd_keymap_windowing("broadway");
1063 g_warning("experimental: using broadway, x11 virtual keysym\n"
1064 "mapping - with very limited support. See also\n"
1065 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1066 *maplen = qemu_input_map_x11_to_qcode_len;
1067 return qemu_input_map_x11_to_qcode;
1069 #endif
1071 g_warning("Unsupported GDK Windowing platform.\n"
1072 "Disabling extended keycode tables.\n"
1073 "Please report to qemu-devel@nongnu.org\n"
1074 "including the following information:\n"
1075 "\n"
1076 " - Operating system\n"
1077 " - GDK Windowing system build\n");
1078 return NULL;
1082 static int gd_map_keycode(int scancode)
1084 if (!keycode_map) {
1085 return 0;
1087 if (scancode > keycode_maplen) {
1088 return 0;
1091 return keycode_map[scancode];
1094 static gboolean gd_text_key_down(GtkWidget *widget,
1095 GdkEventKey *key, void *opaque)
1097 VirtualConsole *vc = opaque;
1098 QemuConsole *con = vc->gfx.dcl.con;
1100 if (key->keyval == GDK_KEY_Delete) {
1101 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE, false);
1102 } else if (key->length) {
1103 kbd_put_string_console(con, key->string, key->length);
1104 } else {
1105 int qcode = gd_map_keycode(key->hardware_keycode);
1106 kbd_put_qcode_console(con, qcode, false);
1108 return TRUE;
1111 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1113 VirtualConsole *vc = opaque;
1114 GtkDisplayState *s = vc->s;
1115 int qcode;
1116 int i;
1118 if (s->ignore_keys) {
1119 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1120 return TRUE;
1123 #ifdef WIN32
1124 /* on windows, we ought to ignore the reserved key event? */
1125 if (key->hardware_keycode == 0xff)
1126 return false;
1127 #endif
1129 if (key->keyval == GDK_KEY_Pause
1130 #ifdef G_OS_WIN32
1131 /* for some reason GDK does not fill keyval for VK_PAUSE
1132 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1134 || key->hardware_keycode == VK_PAUSE
1135 #endif
1137 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1138 key->type == GDK_KEY_PRESS);
1139 return TRUE;
1142 qcode = gd_map_keycode(key->hardware_keycode);
1144 trace_gd_key_event(vc->label, key->hardware_keycode, qcode,
1145 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1147 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1148 if (qcode == modifier_keycode[i]) {
1149 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1153 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode,
1154 key->type == GDK_KEY_PRESS);
1156 return TRUE;
1159 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1161 if (event->type == GDK_MOTION_NOTIFY) {
1162 return gd_motion_event(widget, &event->motion, opaque);
1164 return FALSE;
1167 /** Window Menu Actions **/
1169 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1171 GtkDisplayState *s = opaque;
1173 if (s->external_pause_update) {
1174 return;
1176 if (runstate_is_running()) {
1177 qmp_stop(NULL);
1178 } else {
1179 qmp_cont(NULL);
1183 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1185 qmp_system_reset(NULL);
1188 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1190 qmp_system_powerdown(NULL);
1193 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1195 qmp_quit(NULL);
1198 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1200 GtkDisplayState *s = opaque;
1201 VirtualConsole *vc = gd_vc_find_by_menu(s);
1202 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1203 gint page;
1205 gtk_release_modifiers(s);
1206 if (vc) {
1207 page = gtk_notebook_page_num(nb, vc->tab_item);
1208 gtk_notebook_set_current_page(nb, page);
1209 gtk_widget_grab_focus(vc->focus);
1211 s->ignore_keys = false;
1214 static void gd_accel_switch_vc(void *opaque)
1216 VirtualConsole *vc = opaque;
1218 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1221 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1223 GtkDisplayState *s = opaque;
1224 VirtualConsole *vc = gd_vc_find_current(s);
1226 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1227 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1228 } else {
1229 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1231 gd_update_windowsize(vc);
1234 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1235 void *opaque)
1237 VirtualConsole *vc = opaque;
1238 GtkDisplayState *s = vc->s;
1240 gtk_widget_set_sensitive(vc->menu_item, true);
1241 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1242 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1243 vc->tab_item, vc->label);
1244 gtk_widget_destroy(vc->window);
1245 vc->window = NULL;
1246 return TRUE;
1249 static gboolean gd_win_grab(void *opaque)
1251 VirtualConsole *vc = opaque;
1253 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1254 if (vc->s->ptr_owner) {
1255 gd_ungrab_pointer(vc->s);
1256 } else {
1257 gd_grab_pointer(vc, "user-request-detached-tab");
1259 return TRUE;
1262 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1264 GtkDisplayState *s = opaque;
1265 VirtualConsole *vc = gd_vc_find_current(s);
1267 if (vc->type == GD_VC_GFX &&
1268 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1269 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1270 FALSE);
1272 if (!vc->window) {
1273 gtk_widget_set_sensitive(vc->menu_item, false);
1274 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1275 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1277 g_signal_connect(vc->window, "delete-event",
1278 G_CALLBACK(gd_tab_window_close), vc);
1279 gtk_widget_show_all(vc->window);
1281 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1282 GtkAccelGroup *ag = gtk_accel_group_new();
1283 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1285 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1286 vc, NULL);
1287 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1290 gd_update_geometry_hints(vc);
1291 gd_update_caption(s);
1295 static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
1297 GtkDisplayState *s = opaque;
1298 VirtualConsole *vc = gd_vc_find_current(s);
1300 if (s->full_screen) {
1301 return;
1304 if (gtk_check_menu_item_get_active(
1305 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1306 gtk_widget_show(s->menu_bar);
1307 } else {
1308 gtk_widget_hide(s->menu_bar);
1310 gd_update_windowsize(vc);
1313 static void gd_accel_show_menubar(void *opaque)
1315 GtkDisplayState *s = opaque;
1316 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
1319 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1321 GtkDisplayState *s = opaque;
1322 VirtualConsole *vc = gd_vc_find_current(s);
1324 if (!s->full_screen) {
1325 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1326 gtk_widget_hide(s->menu_bar);
1327 if (vc->type == GD_VC_GFX) {
1328 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1330 gtk_window_fullscreen(GTK_WINDOW(s->window));
1331 s->full_screen = TRUE;
1332 } else {
1333 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1334 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1335 if (gtk_check_menu_item_get_active(
1336 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1337 gtk_widget_show(s->menu_bar);
1339 s->full_screen = FALSE;
1340 if (vc->type == GD_VC_GFX) {
1341 vc->gfx.scale_x = 1.0;
1342 vc->gfx.scale_y = 1.0;
1343 gd_update_windowsize(vc);
1347 gd_update_cursor(vc);
1350 static void gd_accel_full_screen(void *opaque)
1352 GtkDisplayState *s = opaque;
1353 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1356 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1358 GtkDisplayState *s = opaque;
1359 VirtualConsole *vc = gd_vc_find_current(s);
1361 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1362 FALSE);
1364 vc->gfx.scale_x += VC_SCALE_STEP;
1365 vc->gfx.scale_y += VC_SCALE_STEP;
1367 gd_update_windowsize(vc);
1370 static void gd_accel_zoom_in(void *opaque)
1372 GtkDisplayState *s = opaque;
1373 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1376 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1378 GtkDisplayState *s = opaque;
1379 VirtualConsole *vc = gd_vc_find_current(s);
1381 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1382 FALSE);
1384 vc->gfx.scale_x -= VC_SCALE_STEP;
1385 vc->gfx.scale_y -= VC_SCALE_STEP;
1387 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1388 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1390 gd_update_windowsize(vc);
1393 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1395 GtkDisplayState *s = opaque;
1396 VirtualConsole *vc = gd_vc_find_current(s);
1398 vc->gfx.scale_x = 1.0;
1399 vc->gfx.scale_y = 1.0;
1401 gd_update_windowsize(vc);
1404 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1406 GtkDisplayState *s = opaque;
1407 VirtualConsole *vc = gd_vc_find_current(s);
1409 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1410 s->free_scale = TRUE;
1411 } else {
1412 s->free_scale = FALSE;
1413 vc->gfx.scale_x = 1.0;
1414 vc->gfx.scale_y = 1.0;
1417 gd_update_windowsize(vc);
1418 gd_update_full_redraw(vc);
1421 #if GTK_CHECK_VERSION(3, 20, 0)
1422 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1424 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1425 GdkSeat *seat = gdk_display_get_default_seat(display);
1426 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1427 GdkSeatCapabilities caps = 0;
1428 GdkCursor *cursor = NULL;
1430 if (kbd) {
1431 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1433 if (ptr) {
1434 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1435 cursor = vc->s->null_cursor;
1438 if (caps) {
1439 gdk_seat_grab(seat, window, caps, false, cursor,
1440 NULL, NULL, NULL);
1441 } else {
1442 gdk_seat_ungrab(seat);
1445 #else
1446 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1447 GdkInputSource source, GdkEventMask mask,
1448 GdkCursor *cursor)
1450 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1451 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1452 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1453 GList *tmp = devs;
1455 for (tmp = devs; tmp; tmp = tmp->next) {
1456 GdkDevice *dev = tmp->data;
1457 if (gdk_device_get_source(dev) != source) {
1458 continue;
1460 if (grab) {
1461 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1462 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1463 mask, cursor, GDK_CURRENT_TIME);
1464 } else {
1465 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1468 g_list_free(devs);
1470 #endif
1472 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1474 if (vc->s->kbd_owner) {
1475 if (vc->s->kbd_owner == vc) {
1476 return;
1477 } else {
1478 gd_ungrab_keyboard(vc->s);
1482 #if GTK_CHECK_VERSION(3, 20, 0)
1483 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1484 #else
1485 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1486 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1487 NULL);
1488 #endif
1489 vc->s->kbd_owner = vc;
1490 gd_update_caption(vc->s);
1491 trace_gd_grab(vc->label, "kbd", reason);
1494 static void gd_ungrab_keyboard(GtkDisplayState *s)
1496 VirtualConsole *vc = s->kbd_owner;
1498 if (vc == NULL) {
1499 return;
1501 s->kbd_owner = NULL;
1503 #if GTK_CHECK_VERSION(3, 20, 0)
1504 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1505 #else
1506 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1507 #endif
1508 gd_update_caption(s);
1509 trace_gd_ungrab(vc->label, "kbd");
1512 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1514 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1516 if (vc->s->ptr_owner) {
1517 if (vc->s->ptr_owner == vc) {
1518 return;
1519 } else {
1520 gd_ungrab_pointer(vc->s);
1524 #if GTK_CHECK_VERSION(3, 20, 0)
1525 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1526 gdk_device_get_position(gd_get_pointer(display),
1527 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1528 #else
1529 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1530 GDK_POINTER_MOTION_MASK |
1531 GDK_BUTTON_PRESS_MASK |
1532 GDK_BUTTON_RELEASE_MASK |
1533 GDK_BUTTON_MOTION_MASK |
1534 GDK_SCROLL_MASK,
1535 vc->s->null_cursor);
1536 gdk_device_get_position(gd_get_pointer(display),
1537 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1538 #endif
1539 vc->s->ptr_owner = vc;
1540 gd_update_caption(vc->s);
1541 trace_gd_grab(vc->label, "ptr", reason);
1544 static void gd_ungrab_pointer(GtkDisplayState *s)
1546 VirtualConsole *vc = s->ptr_owner;
1547 GdkDisplay *display;
1549 if (vc == NULL) {
1550 return;
1552 s->ptr_owner = NULL;
1554 display = gtk_widget_get_display(vc->gfx.drawing_area);
1555 #if GTK_CHECK_VERSION(3, 20, 0)
1556 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1557 gdk_device_warp(gd_get_pointer(display),
1558 gtk_widget_get_screen(vc->gfx.drawing_area),
1559 vc->s->grab_x_root, vc->s->grab_y_root);
1560 #else
1561 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1562 gdk_device_warp(gd_get_pointer(display),
1563 gtk_widget_get_screen(vc->gfx.drawing_area),
1564 vc->s->grab_x_root, vc->s->grab_y_root);
1565 #endif
1566 gd_update_caption(s);
1567 trace_gd_ungrab(vc->label, "ptr");
1570 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1572 GtkDisplayState *s = opaque;
1573 VirtualConsole *vc = gd_vc_find_current(s);
1575 if (gd_is_grab_active(s)) {
1576 gd_grab_keyboard(vc, "user-request-main-window");
1577 gd_grab_pointer(vc, "user-request-main-window");
1578 } else {
1579 gd_ungrab_keyboard(s);
1580 gd_ungrab_pointer(s);
1583 gd_update_cursor(vc);
1586 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1587 gpointer data)
1589 GtkDisplayState *s = data;
1590 VirtualConsole *vc;
1591 gboolean on_vga;
1593 if (!gtk_widget_get_realized(s->notebook)) {
1594 return;
1597 vc = gd_vc_find_by_page(s, arg2);
1598 if (!vc) {
1599 return;
1601 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1602 TRUE);
1603 on_vga = (vc->type == GD_VC_GFX &&
1604 qemu_console_is_graphic(vc->gfx.dcl.con));
1605 if (!on_vga) {
1606 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1607 FALSE);
1608 } else if (s->full_screen) {
1609 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1610 TRUE);
1612 gtk_widget_set_sensitive(s->grab_item, on_vga);
1613 #ifdef CONFIG_VTE
1614 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1615 #endif
1617 gd_update_windowsize(vc);
1618 gd_update_cursor(vc);
1621 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1622 gpointer opaque)
1624 VirtualConsole *vc = opaque;
1625 GtkDisplayState *s = vc->s;
1627 if (gd_grab_on_hover(s)) {
1628 gd_grab_keyboard(vc, "grab-on-hover");
1630 return TRUE;
1633 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1634 gpointer opaque)
1636 VirtualConsole *vc = opaque;
1637 GtkDisplayState *s = vc->s;
1639 if (gd_grab_on_hover(s)) {
1640 gd_ungrab_keyboard(s);
1642 return TRUE;
1645 static gboolean gd_focus_out_event(GtkWidget *widget,
1646 GdkEventCrossing *crossing, gpointer opaque)
1648 VirtualConsole *vc = opaque;
1649 GtkDisplayState *s = vc->s;
1651 gtk_release_modifiers(s);
1652 return TRUE;
1655 static gboolean gd_configure(GtkWidget *widget,
1656 GdkEventConfigure *cfg, gpointer opaque)
1658 VirtualConsole *vc = opaque;
1660 gd_set_ui_info(vc, cfg->width, cfg->height);
1661 return FALSE;
1664 /** Virtual Console Callbacks **/
1666 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1667 int idx, GSList *group, GtkWidget *view_menu)
1669 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1670 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1671 HOTKEY_MODIFIERS, 0,
1672 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1673 gtk_accel_label_set_accel(
1674 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1675 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1677 g_signal_connect(vc->menu_item, "activate",
1678 G_CALLBACK(gd_menu_switch_vc), s);
1679 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1681 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1682 return group;
1685 #if defined(CONFIG_VTE)
1686 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1688 GtkDisplayState *s = opaque;
1689 VirtualConsole *vc = gd_vc_find_current(s);
1691 #if VTE_CHECK_VERSION(0, 50, 0)
1692 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1693 VTE_FORMAT_TEXT);
1694 #else
1695 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1696 #endif
1699 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1701 VirtualConsole *vc = opaque;
1703 if (gtk_adjustment_get_upper(adjustment) >
1704 gtk_adjustment_get_page_size(adjustment)) {
1705 gtk_widget_show(vc->vte.scrollbar);
1706 } else {
1707 gtk_widget_hide(vc->vte.scrollbar);
1711 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1713 VCChardev *vcd = VC_CHARDEV(chr);
1714 VirtualConsole *vc = vcd->console;
1716 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1717 return len;
1720 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1722 VCChardev *vcd = VC_CHARDEV(chr);
1723 VirtualConsole *vc = vcd->console;
1725 if (vc) {
1726 vc->vte.echo = echo;
1727 } else {
1728 vcd->echo = echo;
1732 static int nb_vcs;
1733 static Chardev *vcs[MAX_VCS];
1734 static void gd_vc_open(Chardev *chr,
1735 ChardevBackend *backend,
1736 bool *be_opened,
1737 Error **errp)
1739 if (nb_vcs == MAX_VCS) {
1740 error_setg(errp, "Maximum number of consoles reached");
1741 return;
1744 vcs[nb_vcs++] = chr;
1746 /* console/chardev init sometimes completes elsewhere in a 2nd
1747 * stage, so defer OPENED events until they are fully initialized
1749 *be_opened = false;
1752 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1754 ChardevClass *cc = CHARDEV_CLASS(oc);
1756 cc->parse = qemu_chr_parse_vc;
1757 cc->open = gd_vc_open;
1758 cc->chr_write = gd_vc_chr_write;
1759 cc->chr_set_echo = gd_vc_chr_set_echo;
1762 static const TypeInfo char_gd_vc_type_info = {
1763 .name = TYPE_CHARDEV_VC,
1764 .parent = TYPE_CHARDEV,
1765 .instance_size = sizeof(VCChardev),
1766 .class_init = char_gd_vc_class_init,
1769 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1770 gpointer user_data)
1772 VirtualConsole *vc = user_data;
1774 if (vc->vte.echo) {
1775 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1776 int i;
1777 for (i = 0; i < size; i++) {
1778 uint8_t c = text[i];
1779 if (c >= 128 || isprint(c)) {
1780 /* 8-bit characters are considered printable. */
1781 vte_terminal_feed(term, &text[i], 1);
1782 } else if (c == '\r' || c == '\n') {
1783 vte_terminal_feed(term, "\r\n", 2);
1784 } else {
1785 char ctrl[2] = { '^', 0};
1786 ctrl[1] = text[i] ^ 64;
1787 vte_terminal_feed(term, ctrl, 2);
1792 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1793 return TRUE;
1796 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1797 Chardev *chr, int idx,
1798 GSList *group, GtkWidget *view_menu)
1800 char buffer[32];
1801 GtkWidget *box;
1802 GtkWidget *scrollbar;
1803 GtkAdjustment *vadjustment;
1804 VCChardev *vcd = VC_CHARDEV(chr);
1806 vc->s = s;
1807 vc->vte.echo = vcd->echo;
1808 vc->vte.chr = chr;
1809 vcd->console = vc;
1811 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1812 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1813 ? vc->vte.chr->label : buffer);
1814 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1816 vc->vte.terminal = vte_terminal_new();
1817 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1819 /* The documentation says that the default is UTF-8, but actually it is
1820 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1821 * VTE 0.54 (only UTF-8 is supported now). */
1822 #if !VTE_CHECK_VERSION(0, 54, 0)
1823 #if VTE_CHECK_VERSION(0, 38, 0)
1824 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1825 #else
1826 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1827 #endif
1828 #endif
1830 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1831 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1832 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1834 #if VTE_CHECK_VERSION(0, 28, 0)
1835 vadjustment = gtk_scrollable_get_vadjustment
1836 (GTK_SCROLLABLE(vc->vte.terminal));
1837 #else
1838 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1839 #endif
1841 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1842 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1844 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1845 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1847 vc->vte.box = box;
1848 vc->vte.scrollbar = scrollbar;
1850 g_signal_connect(vadjustment, "changed",
1851 G_CALLBACK(gd_vc_adjustment_changed), vc);
1853 vc->type = GD_VC_VTE;
1854 vc->tab_item = box;
1855 vc->focus = vc->vte.terminal;
1856 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1857 gtk_label_new(vc->label));
1859 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1861 return group;
1864 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1865 GtkWidget *view_menu)
1867 int i;
1869 for (i = 0; i < nb_vcs; i++) {
1870 VirtualConsole *vc = &s->vc[s->nb_vcs];
1871 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1872 s->nb_vcs++;
1875 #endif /* CONFIG_VTE */
1877 /** Window Creation **/
1879 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1881 g_signal_connect(vc->gfx.drawing_area, "draw",
1882 G_CALLBACK(gd_draw_event), vc);
1883 #if defined(CONFIG_GTK_GL)
1884 if (gtk_use_gl_area) {
1885 /* wire up GtkGlArea events */
1886 g_signal_connect(vc->gfx.drawing_area, "render",
1887 G_CALLBACK(gd_render_event), vc);
1888 g_signal_connect(vc->gfx.drawing_area, "resize",
1889 G_CALLBACK(gd_resize_event), vc);
1891 #endif
1892 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1893 g_signal_connect(vc->gfx.drawing_area, "event",
1894 G_CALLBACK(gd_event), vc);
1895 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1896 G_CALLBACK(gd_button_event), vc);
1897 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1898 G_CALLBACK(gd_button_event), vc);
1899 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1900 G_CALLBACK(gd_scroll_event), vc);
1901 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1902 G_CALLBACK(gd_key_event), vc);
1903 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1904 G_CALLBACK(gd_key_event), vc);
1906 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1907 G_CALLBACK(gd_enter_event), vc);
1908 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1909 G_CALLBACK(gd_leave_event), vc);
1910 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1911 G_CALLBACK(gd_focus_out_event), vc);
1912 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1913 G_CALLBACK(gd_configure), vc);
1914 } else {
1915 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1916 G_CALLBACK(gd_text_key_down), vc);
1920 static void gd_connect_signals(GtkDisplayState *s)
1922 g_signal_connect(s->show_tabs_item, "activate",
1923 G_CALLBACK(gd_menu_show_tabs), s);
1924 g_signal_connect(s->untabify_item, "activate",
1925 G_CALLBACK(gd_menu_untabify), s);
1926 g_signal_connect(s->show_menubar_item, "activate",
1927 G_CALLBACK(gd_menu_show_menubar), s);
1929 g_signal_connect(s->window, "delete-event",
1930 G_CALLBACK(gd_window_close), s);
1932 g_signal_connect(s->pause_item, "activate",
1933 G_CALLBACK(gd_menu_pause), s);
1934 g_signal_connect(s->reset_item, "activate",
1935 G_CALLBACK(gd_menu_reset), s);
1936 g_signal_connect(s->powerdown_item, "activate",
1937 G_CALLBACK(gd_menu_powerdown), s);
1938 g_signal_connect(s->quit_item, "activate",
1939 G_CALLBACK(gd_menu_quit), s);
1940 #if defined(CONFIG_VTE)
1941 g_signal_connect(s->copy_item, "activate",
1942 G_CALLBACK(gd_menu_copy), s);
1943 #endif
1944 g_signal_connect(s->full_screen_item, "activate",
1945 G_CALLBACK(gd_menu_full_screen), s);
1946 g_signal_connect(s->zoom_in_item, "activate",
1947 G_CALLBACK(gd_menu_zoom_in), s);
1948 g_signal_connect(s->zoom_out_item, "activate",
1949 G_CALLBACK(gd_menu_zoom_out), s);
1950 g_signal_connect(s->zoom_fixed_item, "activate",
1951 G_CALLBACK(gd_menu_zoom_fixed), s);
1952 g_signal_connect(s->zoom_fit_item, "activate",
1953 G_CALLBACK(gd_menu_zoom_fit), s);
1954 g_signal_connect(s->grab_item, "activate",
1955 G_CALLBACK(gd_menu_grab_input), s);
1956 g_signal_connect(s->notebook, "switch-page",
1957 G_CALLBACK(gd_change_page), s);
1960 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1962 GtkWidget *machine_menu;
1963 GtkWidget *separator;
1965 machine_menu = gtk_menu_new();
1966 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1968 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1969 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1971 separator = gtk_separator_menu_item_new();
1972 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1974 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1975 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1977 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1978 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1980 separator = gtk_separator_menu_item_new();
1981 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1983 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1984 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1985 "<QEMU>/Machine/Quit");
1986 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1987 GDK_KEY_q, HOTKEY_MODIFIERS);
1988 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1990 return machine_menu;
1993 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1994 QemuConsole *con, int idx,
1995 GSList *group, GtkWidget *view_menu)
1997 bool zoom_to_fit = false;
1999 vc->label = qemu_console_get_label(con);
2000 vc->s = s;
2001 vc->gfx.scale_x = 1.0;
2002 vc->gfx.scale_y = 1.0;
2004 #if defined(CONFIG_OPENGL)
2005 if (display_opengl) {
2006 #if defined(CONFIG_GTK_GL)
2007 if (gtk_use_gl_area) {
2008 vc->gfx.drawing_area = gtk_gl_area_new();
2009 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2010 } else
2011 #endif /* CONFIG_GTK_GL */
2013 vc->gfx.drawing_area = gtk_drawing_area_new();
2015 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2016 * It is required for opengl rendering on X11 though. A
2017 * proper replacement (native opengl support) is only
2018 * available in 3.16+. Silence the warning if possible.
2020 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2021 #pragma GCC diagnostic push
2022 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2023 #endif
2024 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2025 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2026 #pragma GCC diagnostic pop
2027 #endif
2028 vc->gfx.dcl.ops = &dcl_egl_ops;
2030 } else
2031 #endif
2033 vc->gfx.drawing_area = gtk_drawing_area_new();
2034 vc->gfx.dcl.ops = &dcl_ops;
2038 gtk_widget_add_events(vc->gfx.drawing_area,
2039 GDK_POINTER_MOTION_MASK |
2040 GDK_BUTTON_PRESS_MASK |
2041 GDK_BUTTON_RELEASE_MASK |
2042 GDK_BUTTON_MOTION_MASK |
2043 GDK_ENTER_NOTIFY_MASK |
2044 GDK_LEAVE_NOTIFY_MASK |
2045 GDK_SCROLL_MASK |
2046 GDK_KEY_PRESS_MASK);
2047 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2049 vc->type = GD_VC_GFX;
2050 vc->tab_item = vc->gfx.drawing_area;
2051 vc->focus = vc->gfx.drawing_area;
2052 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2053 vc->tab_item, gtk_label_new(vc->label));
2055 vc->gfx.dcl.con = con;
2056 register_displaychangelistener(&vc->gfx.dcl);
2058 gd_connect_vc_gfx_signals(vc);
2059 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2061 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2062 zoom_to_fit = true;
2064 if (s->opts->u.gtk.has_zoom_to_fit) {
2065 zoom_to_fit = s->opts->u.gtk.zoom_to_fit;
2067 if (zoom_to_fit) {
2068 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2069 s->free_scale = true;
2072 return group;
2075 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2077 GSList *group = NULL;
2078 GtkWidget *view_menu;
2079 GtkWidget *separator;
2080 QemuConsole *con;
2081 int vc;
2083 view_menu = gtk_menu_new();
2084 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2086 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2088 #if defined(CONFIG_VTE)
2089 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2090 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2091 #endif
2093 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2094 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2095 gtk_accel_label_set_accel(
2096 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2097 GDK_KEY_f, HOTKEY_MODIFIERS);
2098 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2100 separator = gtk_separator_menu_item_new();
2101 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2103 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2104 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2105 "<QEMU>/View/Zoom In");
2106 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2107 HOTKEY_MODIFIERS);
2108 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2109 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2110 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2112 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2113 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2114 "<QEMU>/View/Zoom Out");
2115 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2116 HOTKEY_MODIFIERS);
2117 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2119 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2120 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2121 "<QEMU>/View/Zoom Fixed");
2122 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2123 HOTKEY_MODIFIERS);
2124 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2126 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2127 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2129 separator = gtk_separator_menu_item_new();
2130 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2132 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2133 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2135 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2136 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2137 "<QEMU>/View/Grab Input");
2138 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2139 HOTKEY_MODIFIERS);
2140 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2142 separator = gtk_separator_menu_item_new();
2143 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2145 /* gfx */
2146 for (vc = 0;; vc++) {
2147 con = qemu_console_lookup_by_index(vc);
2148 if (!con) {
2149 break;
2151 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2152 vc, group, view_menu);
2153 s->nb_vcs++;
2156 #if defined(CONFIG_VTE)
2157 /* vte */
2158 gd_vcs_init(s, group, view_menu);
2159 #endif
2161 separator = gtk_separator_menu_item_new();
2162 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2164 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2165 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2167 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2168 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2170 s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
2171 _("Show Menubar"));
2172 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
2173 TRUE);
2174 gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
2175 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
2176 gtk_accel_label_set_accel(
2177 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
2178 GDK_KEY_m, HOTKEY_MODIFIERS);
2179 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
2181 return view_menu;
2184 static void gd_create_menus(GtkDisplayState *s)
2186 GtkSettings *settings;
2188 s->accel_group = gtk_accel_group_new();
2189 s->machine_menu = gd_create_menu_machine(s);
2190 s->view_menu = gd_create_menu_view(s);
2192 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2193 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2194 s->machine_menu);
2195 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2197 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2198 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2199 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2201 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2202 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2204 /* Disable the default "F10" menu shortcut. */
2205 settings = gtk_widget_get_settings(s->window);
2206 g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
2210 static gboolean gtkinit;
2212 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2214 VirtualConsole *vc;
2216 GtkDisplayState *s = g_malloc0(sizeof(*s));
2217 char *filename;
2218 GdkDisplay *window_display;
2220 if (!gtkinit) {
2221 fprintf(stderr, "gtk initialization failed\n");
2222 exit(1);
2224 assert(opts->type == DISPLAY_TYPE_GTK);
2225 s->opts = opts;
2227 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2228 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2229 s->notebook = gtk_notebook_new();
2230 s->menu_bar = gtk_menu_bar_new();
2232 s->free_scale = FALSE;
2234 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2235 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2236 * printable, but without changing any of the character classes to make
2237 * sure that we don't accidentally break implicit assumptions. */
2238 setlocale(LC_MESSAGES, "");
2239 setlocale(LC_CTYPE, "C.UTF-8");
2240 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2241 textdomain("qemu");
2243 window_display = gtk_widget_get_display(s->window);
2244 s->null_cursor = gdk_cursor_new_for_display(window_display,
2245 GDK_BLANK_CURSOR);
2247 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2248 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2249 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2251 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2252 if (filename) {
2253 GError *error = NULL;
2254 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2255 if (pixbuf) {
2256 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2257 } else {
2258 g_error_free(error);
2260 g_free(filename);
2263 gd_create_menus(s);
2265 gd_connect_signals(s);
2267 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2268 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2270 gd_update_caption(s);
2272 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2273 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2275 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2277 gtk_widget_show_all(s->window);
2279 vc = gd_vc_find_current(s);
2280 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2281 #ifdef CONFIG_VTE
2282 gtk_widget_set_sensitive(s->copy_item,
2283 vc && vc->type == GD_VC_VTE);
2284 #endif
2286 if (opts->has_full_screen &&
2287 opts->full_screen) {
2288 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2290 if (opts->u.gtk.has_grab_on_hover &&
2291 opts->u.gtk.grab_on_hover) {
2292 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2296 static void early_gtk_display_init(DisplayOptions *opts)
2298 /* The QEMU code relies on the assumption that it's always run in
2299 * the C locale. Therefore it is not prepared to deal with
2300 * operations that produce different results depending on the
2301 * locale, such as printf's formatting of decimal numbers, and
2302 * possibly others.
2304 * Since GTK+ calls setlocale() by default -importing the locale
2305 * settings from the environment- we must prevent it from doing so
2306 * using gtk_disable_setlocale().
2308 * QEMU's GTK+ UI, however, _does_ have translations for some of
2309 * the menu items. As a trade-off between a functionally correct
2310 * QEMU and a fully internationalized UI we support importing
2311 * LC_MESSAGES from the environment (see the setlocale() call
2312 * earlier in this file). This allows us to display translated
2313 * messages leaving everything else untouched.
2315 gtk_disable_setlocale();
2316 gtkinit = gtk_init_check(NULL, NULL);
2317 if (!gtkinit) {
2318 /* don't exit yet, that'll break -help */
2319 return;
2322 assert(opts->type == DISPLAY_TYPE_GTK);
2323 if (opts->has_gl && opts->gl != DISPLAYGL_MODE_OFF) {
2324 #if defined(CONFIG_OPENGL)
2325 #if defined(CONFIG_GTK_GL) && defined(GDK_WINDOWING_WAYLAND)
2326 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2327 gtk_use_gl_area = true;
2328 gtk_gl_area_init();
2329 } else
2330 #endif
2332 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
2333 gtk_egl_init(mode);
2335 #endif
2338 keycode_map = gd_get_keymap(&keycode_maplen);
2340 #if defined(CONFIG_VTE)
2341 type_register(&char_gd_vc_type_info);
2342 #endif
2345 static QemuDisplay qemu_display_gtk = {
2346 .type = DISPLAY_TYPE_GTK,
2347 .early_init = early_gtk_display_init,
2348 .init = gtk_display_init,
2351 static void register_gtk(void)
2353 qemu_display_register(&qemu_display_gtk);
2356 type_init(register_gtk);