vfio/container: Rename vfio_init_container to vfio_set_iommu
[qemu/ar7.git] / ui / gtk.c
blob810d7fc7960735e03bde904a62147743b935ff28
1 /*
2 * GTK UI
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * Portions from gtk-vnc (originally licensed under the LGPL v2+):
24 * GTK VNC Widget
26 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
27 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
30 #define GETTEXT_PACKAGE "qemu"
31 #define LOCALEDIR "po"
33 #include "qemu/osdep.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-commands-control.h"
36 #include "qapi/qapi-commands-machine.h"
37 #include "qapi/qapi-commands-misc.h"
38 #include "qemu/cutils.h"
39 #include "qemu/error-report.h"
40 #include "qemu/main-loop.h"
42 #include "ui/console.h"
43 #include "ui/gtk.h"
44 #ifdef G_OS_WIN32
45 #include <gdk/gdkwin32.h>
46 #endif
47 #include "ui/win32-kbd-hook.h"
49 #include <glib/gi18n.h>
50 #include <locale.h>
51 #if defined(CONFIG_VTE)
52 #include <vte/vte.h>
53 #endif
54 #include <math.h>
56 #include "trace.h"
57 #include "ui/input.h"
58 #include "sysemu/runstate.h"
59 #include "sysemu/sysemu.h"
60 #include "keymaps.h"
61 #include "chardev/char.h"
62 #include "qom/object.h"
64 #define VC_WINDOW_X_MIN 320
65 #define VC_WINDOW_Y_MIN 240
66 #define VC_TERM_X_MIN 80
67 #define VC_TERM_Y_MIN 25
68 #define VC_SCALE_MIN 0.25
69 #define VC_SCALE_STEP 0.25
71 #ifdef GDK_WINDOWING_X11
72 #include "x_keymap.h"
74 /* Gtk2 compat */
75 #ifndef GDK_IS_X11_DISPLAY
76 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
77 #endif
78 #endif
81 #ifdef GDK_WINDOWING_WAYLAND
82 /* Gtk2 compat */
83 #ifndef GDK_IS_WAYLAND_DISPLAY
84 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
85 #endif
86 #endif
89 #ifdef GDK_WINDOWING_WIN32
90 /* Gtk2 compat */
91 #ifndef GDK_IS_WIN32_DISPLAY
92 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
93 #endif
94 #endif
97 #ifdef GDK_WINDOWING_BROADWAY
98 /* Gtk2 compat */
99 #ifndef GDK_IS_BROADWAY_DISPLAY
100 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
101 #endif
102 #endif
105 #ifdef GDK_WINDOWING_QUARTZ
106 /* Gtk2 compat */
107 #ifndef GDK_IS_QUARTZ_DISPLAY
108 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
109 #endif
110 #endif
113 #if !defined(CONFIG_VTE)
114 # define VTE_CHECK_VERSION(a, b, c) 0
115 #endif
117 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
119 static const guint16 *keycode_map;
120 static size_t keycode_maplen;
122 struct VCChardev {
123 Chardev parent;
124 VirtualConsole *console;
125 bool echo;
127 typedef struct VCChardev VCChardev;
129 #define TYPE_CHARDEV_VC "chardev-vc"
130 DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
131 TYPE_CHARDEV_VC)
133 static struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX];
135 bool gtk_use_gl_area;
137 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
138 static void gd_ungrab_pointer(GtkDisplayState *s);
139 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
140 static void gd_ungrab_keyboard(GtkDisplayState *s);
142 /** Utility Functions **/
144 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
146 VirtualConsole *vc;
147 gint i;
149 for (i = 0; i < s->nb_vcs; i++) {
150 vc = &s->vc[i];
151 if (gtk_check_menu_item_get_active
152 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
153 return vc;
156 return NULL;
159 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
161 VirtualConsole *vc;
162 gint i, p;
164 for (i = 0; i < s->nb_vcs; i++) {
165 vc = &s->vc[i];
166 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
167 if (p == page) {
168 return vc;
171 return NULL;
174 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
176 gint page;
178 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
179 return gd_vc_find_by_page(s, page);
182 static bool gd_is_grab_active(GtkDisplayState *s)
184 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
187 static bool gd_grab_on_hover(GtkDisplayState *s)
189 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
192 static void gd_update_cursor(VirtualConsole *vc)
194 GtkDisplayState *s = vc->s;
195 GdkWindow *window;
197 if (vc->type != GD_VC_GFX ||
198 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
199 return;
202 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
203 return;
206 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
207 if (s->full_screen || qemu_input_is_absolute(vc->gfx.dcl.con) || s->ptr_owner == vc) {
208 gdk_window_set_cursor(window, s->null_cursor);
209 } else {
210 gdk_window_set_cursor(window, NULL);
214 static void gd_update_caption(GtkDisplayState *s)
216 const char *status = "";
217 gchar *prefix;
218 gchar *title;
219 const char *grab = "";
220 bool is_paused = !runstate_is_running();
221 int i;
223 if (qemu_name) {
224 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
225 } else {
226 prefix = g_strdup_printf("QEMU");
229 if (s->ptr_owner != NULL &&
230 s->ptr_owner->window == NULL) {
231 grab = _(" - Press Ctrl+Alt+G to release grab");
234 if (is_paused) {
235 status = _(" [Paused]");
237 s->external_pause_update = true;
238 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
239 is_paused);
240 s->external_pause_update = false;
242 title = g_strdup_printf("%s%s%s", prefix, status, grab);
243 gtk_window_set_title(GTK_WINDOW(s->window), title);
244 g_free(title);
246 for (i = 0; i < s->nb_vcs; i++) {
247 VirtualConsole *vc = &s->vc[i];
249 if (!vc->window) {
250 continue;
252 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
253 vc == s->kbd_owner ? " +kbd" : "",
254 vc == s->ptr_owner ? " +ptr" : "");
255 gtk_window_set_title(GTK_WINDOW(vc->window), title);
256 g_free(title);
259 g_free(prefix);
262 static void gd_update_geometry_hints(VirtualConsole *vc)
264 GtkDisplayState *s = vc->s;
265 GdkWindowHints mask = 0;
266 GdkGeometry geo = {};
267 GtkWidget *geo_widget = NULL;
268 GtkWindow *geo_window;
270 if (vc->type == GD_VC_GFX) {
271 if (!vc->gfx.ds) {
272 return;
274 if (s->free_scale) {
275 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
276 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
277 mask |= GDK_HINT_MIN_SIZE;
278 } else {
279 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
280 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
281 mask |= GDK_HINT_MIN_SIZE;
283 geo_widget = vc->gfx.drawing_area;
284 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
286 #if defined(CONFIG_VTE)
287 } else if (vc->type == GD_VC_VTE) {
288 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
289 GtkBorder padding = { 0 };
291 #if VTE_CHECK_VERSION(0, 37, 0)
292 gtk_style_context_get_padding(
293 gtk_widget_get_style_context(vc->vte.terminal),
294 gtk_widget_get_state_flags(vc->vte.terminal),
295 &padding);
296 #else
298 GtkBorder *ib = NULL;
299 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
300 if (ib) {
301 padding = *ib;
302 gtk_border_free(ib);
305 #endif
307 geo.width_inc = vte_terminal_get_char_width(term);
308 geo.height_inc = vte_terminal_get_char_height(term);
309 mask |= GDK_HINT_RESIZE_INC;
310 geo.base_width = geo.width_inc;
311 geo.base_height = geo.height_inc;
312 mask |= GDK_HINT_BASE_SIZE;
313 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
314 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
315 mask |= GDK_HINT_MIN_SIZE;
317 geo.base_width += padding.left + padding.right;
318 geo.base_height += padding.top + padding.bottom;
319 geo.min_width += padding.left + padding.right;
320 geo.min_height += padding.top + padding.bottom;
321 geo_widget = vc->vte.terminal;
322 #endif
325 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
326 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
329 void gd_update_windowsize(VirtualConsole *vc)
331 GtkDisplayState *s = vc->s;
333 gd_update_geometry_hints(vc);
335 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
336 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
337 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
341 static void gd_update_full_redraw(VirtualConsole *vc)
343 GtkWidget *area = vc->gfx.drawing_area;
344 int ww, wh;
345 ww = gdk_window_get_width(gtk_widget_get_window(area));
346 wh = gdk_window_get_height(gtk_widget_get_window(area));
347 #if defined(CONFIG_OPENGL)
348 if (vc->gfx.gls && gtk_use_gl_area) {
349 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
350 return;
352 #endif
353 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
356 static void gtk_release_modifiers(GtkDisplayState *s)
358 VirtualConsole *vc = gd_vc_find_current(s);
360 if (vc->type != GD_VC_GFX ||
361 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
362 return;
364 qkbd_state_lift_all_keys(vc->gfx.kbd);
367 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
368 GtkWidget *widget)
370 g_object_ref(G_OBJECT(widget));
371 gtk_container_remove(GTK_CONTAINER(from), widget);
372 gtk_container_add(GTK_CONTAINER(to), widget);
373 g_object_unref(G_OBJECT(widget));
376 static void *gd_win32_get_hwnd(VirtualConsole *vc)
378 #ifdef G_OS_WIN32
379 return gdk_win32_window_get_impl_hwnd(
380 gtk_widget_get_window(vc->window ? vc->window : vc->s->window));
381 #else
382 return NULL;
383 #endif
386 /** DisplayState Callbacks **/
388 static void gd_update(DisplayChangeListener *dcl,
389 int x, int y, int w, int h)
391 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
392 GdkWindow *win;
393 int x1, x2, y1, y2;
394 int mx, my;
395 int fbw, fbh;
396 int ww, wh;
398 trace_gd_update(vc->label, x, y, w, h);
400 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
401 return;
404 if (vc->gfx.convert) {
405 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
406 NULL, vc->gfx.convert,
407 x, y, 0, 0, x, y, w, h);
410 x1 = floor(x * vc->gfx.scale_x);
411 y1 = floor(y * vc->gfx.scale_y);
413 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
414 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
416 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
417 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
419 win = gtk_widget_get_window(vc->gfx.drawing_area);
420 if (!win) {
421 return;
423 ww = gdk_window_get_width(win);
424 wh = gdk_window_get_height(win);
426 mx = my = 0;
427 if (ww > fbw) {
428 mx = (ww - fbw) / 2;
430 if (wh > fbh) {
431 my = (wh - fbh) / 2;
434 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
435 mx + x1, my + y1, (x2 - x1), (y2 - y1));
438 static void gd_refresh(DisplayChangeListener *dcl)
440 graphic_hw_update(dcl->con);
443 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
445 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
448 static void gd_mouse_set(DisplayChangeListener *dcl,
449 int x, int y, int visible)
451 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
452 GdkDisplay *dpy;
453 gint x_root, y_root;
455 if (!gtk_widget_get_realized(vc->gfx.drawing_area) ||
456 qemu_input_is_absolute(dcl->con)) {
457 return;
460 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
461 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
462 x, y, &x_root, &y_root);
463 gdk_device_warp(gd_get_pointer(dpy),
464 gtk_widget_get_screen(vc->gfx.drawing_area),
465 x_root, y_root);
466 vc->s->last_x = x;
467 vc->s->last_y = y;
470 static void gd_cursor_define(DisplayChangeListener *dcl,
471 QEMUCursor *c)
473 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
474 GdkPixbuf *pixbuf;
475 GdkCursor *cursor;
477 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
478 return;
481 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
482 GDK_COLORSPACE_RGB, true, 8,
483 c->width, c->height, c->width * 4,
484 NULL, NULL);
485 cursor = gdk_cursor_new_from_pixbuf
486 (gtk_widget_get_display(vc->gfx.drawing_area),
487 pixbuf, c->hot_x, c->hot_y);
488 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
489 g_object_unref(pixbuf);
490 g_object_unref(cursor);
493 static void gd_switch(DisplayChangeListener *dcl,
494 DisplaySurface *surface)
496 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
497 bool resized = true;
499 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
501 if (vc->gfx.surface) {
502 cairo_surface_destroy(vc->gfx.surface);
503 vc->gfx.surface = NULL;
505 if (vc->gfx.convert) {
506 pixman_image_unref(vc->gfx.convert);
507 vc->gfx.convert = NULL;
510 if (vc->gfx.ds &&
511 surface_width(vc->gfx.ds) == surface_width(surface) &&
512 surface_height(vc->gfx.ds) == surface_height(surface)) {
513 resized = false;
515 vc->gfx.ds = surface;
517 if (surface_format(surface) == PIXMAN_x8r8g8b8) {
519 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
521 * No need to convert, use surface directly. Should be the
522 * common case as this is qemu_default_pixelformat(32) too.
524 vc->gfx.surface = cairo_image_surface_create_for_data
525 (surface_data(surface),
526 CAIRO_FORMAT_RGB24,
527 surface_width(surface),
528 surface_height(surface),
529 surface_stride(surface));
530 } else {
531 /* Must convert surface, use pixman to do it. */
532 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
533 surface_width(surface),
534 surface_height(surface),
535 NULL, 0);
536 vc->gfx.surface = cairo_image_surface_create_for_data
537 ((void *)pixman_image_get_data(vc->gfx.convert),
538 CAIRO_FORMAT_RGB24,
539 pixman_image_get_width(vc->gfx.convert),
540 pixman_image_get_height(vc->gfx.convert),
541 pixman_image_get_stride(vc->gfx.convert));
542 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
543 NULL, vc->gfx.convert,
544 0, 0, 0, 0, 0, 0,
545 pixman_image_get_width(vc->gfx.convert),
546 pixman_image_get_height(vc->gfx.convert));
549 if (resized) {
550 gd_update_windowsize(vc);
551 } else {
552 gd_update_full_redraw(vc);
556 static const DisplayChangeListenerOps dcl_ops = {
557 .dpy_name = "gtk",
558 .dpy_gfx_update = gd_update,
559 .dpy_gfx_switch = gd_switch,
560 .dpy_gfx_check_format = qemu_pixman_check_format,
561 .dpy_refresh = gd_refresh,
562 .dpy_mouse_set = gd_mouse_set,
563 .dpy_cursor_define = gd_cursor_define,
567 #if defined(CONFIG_OPENGL)
569 static bool gd_has_dmabuf(DisplayChangeListener *dcl)
571 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
573 if (gtk_use_gl_area && !gtk_widget_get_realized(vc->gfx.drawing_area)) {
574 /* FIXME: Assume it will work, actual check done after realize */
575 /* fixing this would require delaying listener registration */
576 return true;
579 return vc->gfx.has_dmabuf;
582 static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
583 QemuDmaBuf *dmabuf)
585 #ifdef CONFIG_GBM
586 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
588 egl_dmabuf_release_texture(dmabuf);
589 if (vc->gfx.guest_fb.dmabuf == dmabuf) {
590 vc->gfx.guest_fb.dmabuf = NULL;
592 #endif
595 void gd_hw_gl_flushed(void *vcon)
597 VirtualConsole *vc = vcon;
598 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
600 qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
601 close(dmabuf->fence_fd);
602 dmabuf->fence_fd = -1;
603 graphic_hw_gl_block(vc->gfx.dcl.con, false);
606 /** DisplayState Callbacks (opengl version) **/
608 static const DisplayChangeListenerOps dcl_gl_area_ops = {
609 .dpy_name = "gtk-egl",
610 .dpy_gfx_update = gd_gl_area_update,
611 .dpy_gfx_switch = gd_gl_area_switch,
612 .dpy_gfx_check_format = console_gl_check_format,
613 .dpy_refresh = gd_gl_area_refresh,
614 .dpy_mouse_set = gd_mouse_set,
615 .dpy_cursor_define = gd_cursor_define,
617 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
618 .dpy_gl_scanout_disable = gd_gl_area_scanout_disable,
619 .dpy_gl_update = gd_gl_area_scanout_flush,
620 .dpy_gl_scanout_dmabuf = gd_gl_area_scanout_dmabuf,
621 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
622 .dpy_has_dmabuf = gd_has_dmabuf,
625 static bool
626 gd_gl_area_is_compatible_dcl(DisplayGLCtx *dgc,
627 DisplayChangeListener *dcl)
629 return dcl->ops == &dcl_gl_area_ops;
632 static const DisplayGLCtxOps gl_area_ctx_ops = {
633 .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
634 .dpy_gl_ctx_create = gd_gl_area_create_context,
635 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
636 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
639 #ifdef CONFIG_X11
640 static const DisplayChangeListenerOps dcl_egl_ops = {
641 .dpy_name = "gtk-egl",
642 .dpy_gfx_update = gd_egl_update,
643 .dpy_gfx_switch = gd_egl_switch,
644 .dpy_gfx_check_format = console_gl_check_format,
645 .dpy_refresh = gd_egl_refresh,
646 .dpy_mouse_set = gd_mouse_set,
647 .dpy_cursor_define = gd_cursor_define,
649 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
650 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
651 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
652 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
653 .dpy_gl_cursor_position = gd_egl_cursor_position,
654 .dpy_gl_update = gd_egl_flush,
655 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
656 .dpy_has_dmabuf = gd_has_dmabuf,
659 static bool
660 gd_egl_is_compatible_dcl(DisplayGLCtx *dgc,
661 DisplayChangeListener *dcl)
663 return dcl->ops == &dcl_egl_ops;
666 static const DisplayGLCtxOps egl_ctx_ops = {
667 .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
668 .dpy_gl_ctx_create = gd_egl_create_context,
669 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
670 .dpy_gl_ctx_make_current = gd_egl_make_current,
672 #endif
674 #endif /* CONFIG_OPENGL */
676 /** QEMU Events **/
678 static void gd_change_runstate(void *opaque, bool running, RunState state)
680 GtkDisplayState *s = opaque;
682 gd_update_caption(s);
685 static void gd_mouse_mode_change(Notifier *notify, void *data)
687 GtkDisplayState *s;
688 int i;
690 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
691 /* release the grab at switching to absolute mode */
692 if (s->ptr_owner && qemu_input_is_absolute(s->ptr_owner->gfx.dcl.con)) {
693 if (!s->ptr_owner->window) {
694 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
695 FALSE);
696 } else {
697 gd_ungrab_pointer(s);
700 for (i = 0; i < s->nb_vcs; i++) {
701 VirtualConsole *vc = &s->vc[i];
702 gd_update_cursor(vc);
706 /** GTK Events **/
708 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
709 void *opaque)
711 GtkDisplayState *s = opaque;
712 bool allow_close = true;
714 if (s->opts->has_window_close && !s->opts->window_close) {
715 allow_close = false;
718 if (allow_close) {
719 qmp_quit(NULL);
722 return TRUE;
725 static void gd_set_ui_refresh_rate(VirtualConsole *vc, int refresh_rate)
727 QemuUIInfo info;
729 if (!dpy_ui_info_supported(vc->gfx.dcl.con)) {
730 return;
733 info = *dpy_get_ui_info(vc->gfx.dcl.con);
734 info.refresh_rate = refresh_rate;
735 dpy_set_ui_info(vc->gfx.dcl.con, &info, true);
738 static void gd_set_ui_size(VirtualConsole *vc, gint width, gint height)
740 QemuUIInfo info;
742 if (!dpy_ui_info_supported(vc->gfx.dcl.con)) {
743 return;
746 info = *dpy_get_ui_info(vc->gfx.dcl.con);
747 info.width = width;
748 info.height = height;
749 dpy_set_ui_info(vc->gfx.dcl.con, &info, true);
752 #if defined(CONFIG_OPENGL)
754 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
755 void *opaque)
757 VirtualConsole *vc = opaque;
759 if (vc->gfx.gls) {
760 gd_gl_area_draw(vc);
762 return TRUE;
765 static void gd_resize_event(GtkGLArea *area,
766 gint width, gint height, gpointer *opaque)
768 VirtualConsole *vc = (void *)opaque;
770 gd_set_ui_size(vc, width, height);
773 #endif
775 void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget)
777 #ifdef GDK_VERSION_3_22
778 GdkWindow *win = gtk_widget_get_window(widget);
779 int refresh_rate;
781 if (win) {
782 GdkDisplay *dpy = gtk_widget_get_display(widget);
783 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
784 refresh_rate = gdk_monitor_get_refresh_rate(monitor); /* [mHz] */
785 } else {
786 refresh_rate = 0;
789 gd_set_ui_refresh_rate(vc, refresh_rate);
791 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
792 vc->gfx.dcl.update_interval = refresh_rate ?
793 MIN(1000 * 1000 / refresh_rate, GUI_REFRESH_INTERVAL_DEFAULT) :
794 GUI_REFRESH_INTERVAL_DEFAULT;
795 #endif
798 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
800 VirtualConsole *vc = opaque;
801 GtkDisplayState *s = vc->s;
802 int mx, my;
803 int ww, wh;
804 int fbw, fbh;
806 #if defined(CONFIG_OPENGL)
807 if (vc->gfx.gls) {
808 if (gtk_use_gl_area) {
809 /* invoke render callback please */
810 return FALSE;
811 } else {
812 #ifdef CONFIG_X11
813 gd_egl_draw(vc);
814 return TRUE;
815 #else
816 abort();
817 #endif
820 #endif
822 if (!gtk_widget_get_realized(widget)) {
823 return FALSE;
825 if (!vc->gfx.ds) {
826 return FALSE;
828 if (!vc->gfx.surface) {
829 return FALSE;
832 gd_update_monitor_refresh_rate(vc, vc->window ? vc->window : s->window);
834 fbw = surface_width(vc->gfx.ds);
835 fbh = surface_height(vc->gfx.ds);
837 ww = gdk_window_get_width(gtk_widget_get_window(widget));
838 wh = gdk_window_get_height(gtk_widget_get_window(widget));
840 if (s->full_screen) {
841 vc->gfx.scale_x = (double)ww / fbw;
842 vc->gfx.scale_y = (double)wh / fbh;
843 } else if (s->free_scale) {
844 double sx, sy;
846 sx = (double)ww / fbw;
847 sy = (double)wh / fbh;
849 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
852 fbw *= vc->gfx.scale_x;
853 fbh *= vc->gfx.scale_y;
855 mx = my = 0;
856 if (ww > fbw) {
857 mx = (ww - fbw) / 2;
859 if (wh > fbh) {
860 my = (wh - fbh) / 2;
863 cairo_rectangle(cr, 0, 0, ww, wh);
865 /* Optionally cut out the inner area where the pixmap
866 will be drawn. This avoids 'flashing' since we're
867 not double-buffering. Note we're using the undocumented
868 behaviour of drawing the rectangle from right to left
869 to cut out the whole */
870 cairo_rectangle(cr, mx + fbw, my,
871 -1 * fbw, fbh);
872 cairo_fill(cr);
874 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
875 cairo_set_source_surface(cr, vc->gfx.surface,
876 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
877 cairo_paint(cr);
879 return TRUE;
882 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
883 void *opaque)
885 VirtualConsole *vc = opaque;
886 GtkDisplayState *s = vc->s;
887 int x, y;
888 int mx, my;
889 int fbh, fbw;
890 int ww, wh, ws;
892 if (!vc->gfx.ds) {
893 return TRUE;
896 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
897 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
899 ww = gtk_widget_get_allocated_width(widget);
900 wh = gtk_widget_get_allocated_height(widget);
901 ws = gtk_widget_get_scale_factor(widget);
903 mx = my = 0;
904 if (ww > fbw) {
905 mx = (ww - fbw) / 2;
907 if (wh > fbh) {
908 my = (wh - fbh) / 2;
911 x = (motion->x - mx) / vc->gfx.scale_x * ws;
912 y = (motion->y - my) / vc->gfx.scale_y * ws;
914 if (qemu_input_is_absolute(vc->gfx.dcl.con)) {
915 if (x < 0 || y < 0 ||
916 x >= surface_width(vc->gfx.ds) ||
917 y >= surface_height(vc->gfx.ds)) {
918 return TRUE;
920 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
921 0, surface_width(vc->gfx.ds));
922 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
923 0, surface_height(vc->gfx.ds));
924 qemu_input_event_sync();
925 } else if (s->last_set && s->ptr_owner == vc) {
926 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
927 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
928 qemu_input_event_sync();
930 s->last_x = x;
931 s->last_y = y;
932 s->last_set = TRUE;
934 if (!qemu_input_is_absolute(vc->gfx.dcl.con) && s->ptr_owner == vc) {
935 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
936 GdkDisplay *dpy = gtk_widget_get_display(widget);
937 GdkWindow *win = gtk_widget_get_window(widget);
938 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
939 GdkRectangle geometry;
941 int xr = (int)motion->x_root;
942 int yr = (int)motion->y_root;
944 gdk_monitor_get_geometry(monitor, &geometry);
946 /* In relative mode check to see if client pointer hit
947 * one of the monitor edges, and if so move it back to the
948 * center of the monitor. This is important because the pointer
949 * in the server doesn't correspond 1-for-1, and so
950 * may still be only half way across the screen. Without
951 * this warp, the server pointer would thus appear to hit
952 * an invisible wall */
953 if (xr <= geometry.x || xr - geometry.x >= geometry.width - 1 ||
954 yr <= geometry.y || yr - geometry.y >= geometry.height - 1) {
955 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
956 xr = geometry.x + geometry.width / 2;
957 yr = geometry.y + geometry.height / 2;
959 gdk_device_warp(dev, screen, xr, yr);
960 s->last_set = FALSE;
961 return FALSE;
964 return TRUE;
967 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
968 void *opaque)
970 VirtualConsole *vc = opaque;
971 GtkDisplayState *s = vc->s;
972 InputButton btn;
974 /* implicitly grab the input at the first click in the relative mode */
975 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
976 !qemu_input_is_absolute(vc->gfx.dcl.con) && s->ptr_owner != vc) {
977 if (!vc->window) {
978 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
979 TRUE);
980 } else {
981 gd_grab_pointer(vc, "relative-mode-click");
983 return TRUE;
986 if (button->button == 1) {
987 btn = INPUT_BUTTON_LEFT;
988 } else if (button->button == 2) {
989 btn = INPUT_BUTTON_MIDDLE;
990 } else if (button->button == 3) {
991 btn = INPUT_BUTTON_RIGHT;
992 } else if (button->button == 8) {
993 btn = INPUT_BUTTON_SIDE;
994 } else if (button->button == 9) {
995 btn = INPUT_BUTTON_EXTRA;
996 } else {
997 return TRUE;
1000 if (button->type == GDK_2BUTTON_PRESS || button->type == GDK_3BUTTON_PRESS) {
1001 return TRUE;
1004 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1005 button->type == GDK_BUTTON_PRESS);
1006 qemu_input_event_sync();
1007 return TRUE;
1010 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1011 void *opaque)
1013 VirtualConsole *vc = opaque;
1014 InputButton btn_vertical;
1015 InputButton btn_horizontal;
1016 bool has_vertical = false;
1017 bool has_horizontal = false;
1019 if (scroll->direction == GDK_SCROLL_UP) {
1020 btn_vertical = INPUT_BUTTON_WHEEL_UP;
1021 has_vertical = true;
1022 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1023 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
1024 has_vertical = true;
1025 } else if (scroll->direction == GDK_SCROLL_LEFT) {
1026 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1027 has_horizontal = true;
1028 } else if (scroll->direction == GDK_SCROLL_RIGHT) {
1029 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1030 has_horizontal = true;
1031 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1032 gdouble delta_x, delta_y;
1033 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1034 &delta_x, &delta_y)) {
1035 return TRUE;
1038 if (delta_y > 0) {
1039 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
1040 has_vertical = true;
1041 } else if (delta_y < 0) {
1042 btn_vertical = INPUT_BUTTON_WHEEL_UP;
1043 has_vertical = true;
1044 } else if (delta_x > 0) {
1045 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1046 has_horizontal = true;
1047 } else if (delta_x < 0) {
1048 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1049 has_horizontal = true;
1050 } else {
1051 return TRUE;
1053 } else {
1054 return TRUE;
1057 if (has_vertical) {
1058 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, true);
1059 qemu_input_event_sync();
1060 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, false);
1061 qemu_input_event_sync();
1064 if (has_horizontal) {
1065 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, true);
1066 qemu_input_event_sync();
1067 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, false);
1068 qemu_input_event_sync();
1071 return TRUE;
1075 static gboolean gd_touch_event(GtkWidget *widget, GdkEventTouch *touch,
1076 void *opaque)
1078 VirtualConsole *vc = opaque;
1079 uint64_t num_slot = GPOINTER_TO_UINT(touch->sequence);
1080 int type = -1;
1082 switch (touch->type) {
1083 case GDK_TOUCH_BEGIN:
1084 type = INPUT_MULTI_TOUCH_TYPE_BEGIN;
1085 break;
1086 case GDK_TOUCH_UPDATE:
1087 type = INPUT_MULTI_TOUCH_TYPE_UPDATE;
1088 break;
1089 case GDK_TOUCH_END:
1090 case GDK_TOUCH_CANCEL:
1091 type = INPUT_MULTI_TOUCH_TYPE_END;
1092 break;
1093 default:
1094 warn_report("gtk: unexpected touch event type\n");
1095 return FALSE;
1098 console_handle_touch_event(vc->gfx.dcl.con, touch_slots,
1099 num_slot, surface_width(vc->gfx.ds),
1100 surface_height(vc->gfx.ds), touch->x,
1101 touch->y, type, &error_warn);
1102 return TRUE;
1105 static const guint16 *gd_get_keymap(size_t *maplen)
1107 GdkDisplay *dpy = gdk_display_get_default();
1109 #ifdef GDK_WINDOWING_X11
1110 if (GDK_IS_X11_DISPLAY(dpy)) {
1111 trace_gd_keymap_windowing("x11");
1112 return qemu_xkeymap_mapping_table(
1113 gdk_x11_display_get_xdisplay(dpy), maplen);
1115 #endif
1117 #ifdef GDK_WINDOWING_WAYLAND
1118 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1119 trace_gd_keymap_windowing("wayland");
1120 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1121 return qemu_input_map_xorgevdev_to_qcode;
1123 #endif
1125 #ifdef GDK_WINDOWING_WIN32
1126 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1127 trace_gd_keymap_windowing("win32");
1128 *maplen = qemu_input_map_atset1_to_qcode_len;
1129 return qemu_input_map_atset1_to_qcode;
1131 #endif
1133 #ifdef GDK_WINDOWING_QUARTZ
1134 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1135 trace_gd_keymap_windowing("quartz");
1136 *maplen = qemu_input_map_osx_to_qcode_len;
1137 return qemu_input_map_osx_to_qcode;
1139 #endif
1141 #ifdef GDK_WINDOWING_BROADWAY
1142 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1143 trace_gd_keymap_windowing("broadway");
1144 g_warning("experimental: using broadway, x11 virtual keysym\n"
1145 "mapping - with very limited support. See also\n"
1146 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1147 *maplen = qemu_input_map_x11_to_qcode_len;
1148 return qemu_input_map_x11_to_qcode;
1150 #endif
1152 g_warning("Unsupported GDK Windowing platform.\n"
1153 "Disabling extended keycode tables.\n"
1154 "Please report to qemu-devel@nongnu.org\n"
1155 "including the following information:\n"
1156 "\n"
1157 " - Operating system\n"
1158 " - GDK Windowing system build\n");
1159 return NULL;
1163 static int gd_map_keycode(int scancode)
1165 if (!keycode_map) {
1166 return 0;
1168 if (scancode > keycode_maplen) {
1169 return 0;
1172 return keycode_map[scancode];
1175 static int gd_get_keycode(GdkEventKey *key)
1177 #ifdef G_OS_WIN32
1178 int scancode = gdk_event_get_scancode((GdkEvent *)key);
1180 /* translate Windows native scancodes to atset1 keycodes */
1181 switch (scancode & (KF_EXTENDED | 0xff)) {
1182 case 0x145: /* NUMLOCK */
1183 return scancode & 0xff;
1186 return scancode & KF_EXTENDED ?
1187 0xe000 | (scancode & 0xff) : scancode & 0xff;
1189 #else
1190 return key->hardware_keycode;
1191 #endif
1194 static gboolean gd_text_key_down(GtkWidget *widget,
1195 GdkEventKey *key, void *opaque)
1197 VirtualConsole *vc = opaque;
1198 QemuTextConsole *con = QEMU_TEXT_CONSOLE(vc->gfx.dcl.con);
1200 if (key->keyval == GDK_KEY_Delete) {
1201 qemu_text_console_put_qcode(con, Q_KEY_CODE_DELETE, false);
1202 } else if (key->length) {
1203 qemu_text_console_put_string(con, key->string, key->length);
1204 } else {
1205 int qcode = gd_map_keycode(gd_get_keycode(key));
1206 qemu_text_console_put_qcode(con, qcode, false);
1208 return TRUE;
1211 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1213 VirtualConsole *vc = opaque;
1214 int keycode, qcode;
1216 #ifdef G_OS_WIN32
1217 /* on windows, we ought to ignore the reserved key event? */
1218 if (key->hardware_keycode == 0xff)
1219 return false;
1221 if (!vc->s->kbd_owner) {
1222 if (key->hardware_keycode == VK_LWIN ||
1223 key->hardware_keycode == VK_RWIN) {
1224 return FALSE;
1227 #endif
1229 if (key->keyval == GDK_KEY_Pause
1230 #ifdef G_OS_WIN32
1231 /* for some reason GDK does not fill keyval for VK_PAUSE
1232 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1234 || key->hardware_keycode == VK_PAUSE
1235 #endif
1237 qkbd_state_key_event(vc->gfx.kbd, Q_KEY_CODE_PAUSE,
1238 key->type == GDK_KEY_PRESS);
1239 return TRUE;
1242 keycode = gd_get_keycode(key);
1243 qcode = gd_map_keycode(keycode);
1245 trace_gd_key_event(vc->label, keycode, qcode,
1246 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1248 qkbd_state_key_event(vc->gfx.kbd, qcode,
1249 key->type == GDK_KEY_PRESS);
1251 return TRUE;
1254 static gboolean gd_grab_broken_event(GtkWidget *widget,
1255 GdkEventGrabBroken *event, void *opaque)
1257 #ifdef CONFIG_WIN32
1259 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1260 * key combination leaves all three keys in a stuck condition. We use
1261 * the grab-broken-event to release all keys.
1263 if (event->keyboard) {
1264 VirtualConsole *vc = opaque;
1265 GtkDisplayState *s = vc->s;
1267 gtk_release_modifiers(s);
1269 #endif
1270 return TRUE;
1273 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1275 if (event->type == GDK_MOTION_NOTIFY) {
1276 return gd_motion_event(widget, &event->motion, opaque);
1278 return FALSE;
1281 /** Window Menu Actions **/
1283 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1285 GtkDisplayState *s = opaque;
1287 if (s->external_pause_update) {
1288 return;
1290 if (runstate_is_running()) {
1291 qmp_stop(NULL);
1292 } else {
1293 qmp_cont(NULL);
1297 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1299 qmp_system_reset(NULL);
1302 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1304 qmp_system_powerdown(NULL);
1307 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1309 qmp_quit(NULL);
1312 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1314 GtkDisplayState *s = opaque;
1315 VirtualConsole *vc = gd_vc_find_by_menu(s);
1316 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1317 gint page;
1319 gtk_release_modifiers(s);
1320 if (vc) {
1321 page = gtk_notebook_page_num(nb, vc->tab_item);
1322 gtk_notebook_set_current_page(nb, page);
1323 gtk_widget_grab_focus(vc->focus);
1327 static void gd_accel_switch_vc(void *opaque)
1329 VirtualConsole *vc = opaque;
1331 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1334 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1336 GtkDisplayState *s = opaque;
1337 VirtualConsole *vc = gd_vc_find_current(s);
1339 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1340 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1341 } else {
1342 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1344 gd_update_windowsize(vc);
1347 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1348 void *opaque)
1350 VirtualConsole *vc = opaque;
1351 GtkDisplayState *s = vc->s;
1353 gtk_widget_set_sensitive(vc->menu_item, true);
1354 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1355 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1356 vc->tab_item, vc->label);
1357 gtk_widget_destroy(vc->window);
1358 vc->window = NULL;
1359 #if defined(CONFIG_OPENGL)
1360 if (vc->gfx.esurface) {
1361 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1362 vc->gfx.esurface = NULL;
1364 if (vc->gfx.ectx) {
1365 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1366 vc->gfx.ectx = NULL;
1368 #endif
1369 return TRUE;
1372 static gboolean gd_win_grab(void *opaque)
1374 VirtualConsole *vc = opaque;
1376 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1377 if (vc->s->ptr_owner) {
1378 gd_ungrab_pointer(vc->s);
1379 } else {
1380 gd_grab_pointer(vc, "user-request-detached-tab");
1382 return TRUE;
1385 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1387 GtkDisplayState *s = opaque;
1388 VirtualConsole *vc = gd_vc_find_current(s);
1390 if (vc->type == GD_VC_GFX &&
1391 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1392 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1393 FALSE);
1395 if (!vc->window) {
1396 gtk_widget_set_sensitive(vc->menu_item, false);
1397 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1398 #if defined(CONFIG_OPENGL)
1399 if (vc->gfx.esurface) {
1400 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1401 vc->gfx.esurface = NULL;
1403 if (vc->gfx.ectx) {
1404 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1405 vc->gfx.ectx = NULL;
1407 #endif
1408 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1410 g_signal_connect(vc->window, "delete-event",
1411 G_CALLBACK(gd_tab_window_close), vc);
1412 gtk_widget_show_all(vc->window);
1414 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1415 GtkAccelGroup *ag = gtk_accel_group_new();
1416 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1418 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1419 vc, NULL);
1420 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1423 gd_update_geometry_hints(vc);
1424 gd_update_caption(s);
1428 static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
1430 GtkDisplayState *s = opaque;
1431 VirtualConsole *vc = gd_vc_find_current(s);
1433 if (s->full_screen) {
1434 return;
1437 if (gtk_check_menu_item_get_active(
1438 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1439 gtk_widget_show(s->menu_bar);
1440 } else {
1441 gtk_widget_hide(s->menu_bar);
1443 gd_update_windowsize(vc);
1446 static void gd_accel_show_menubar(void *opaque)
1448 GtkDisplayState *s = opaque;
1449 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
1452 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1454 GtkDisplayState *s = opaque;
1455 VirtualConsole *vc = gd_vc_find_current(s);
1457 if (!s->full_screen) {
1458 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1459 gtk_widget_hide(s->menu_bar);
1460 if (vc->type == GD_VC_GFX) {
1461 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1463 gtk_window_fullscreen(GTK_WINDOW(s->window));
1464 s->full_screen = TRUE;
1465 } else {
1466 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1467 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1468 if (gtk_check_menu_item_get_active(
1469 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1470 gtk_widget_show(s->menu_bar);
1472 s->full_screen = FALSE;
1473 if (vc->type == GD_VC_GFX) {
1474 vc->gfx.scale_x = 1.0;
1475 vc->gfx.scale_y = 1.0;
1476 gd_update_windowsize(vc);
1480 gd_update_cursor(vc);
1483 static void gd_accel_full_screen(void *opaque)
1485 GtkDisplayState *s = opaque;
1486 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1489 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1491 GtkDisplayState *s = opaque;
1492 VirtualConsole *vc = gd_vc_find_current(s);
1494 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1495 FALSE);
1497 vc->gfx.scale_x += VC_SCALE_STEP;
1498 vc->gfx.scale_y += VC_SCALE_STEP;
1500 gd_update_windowsize(vc);
1503 static void gd_accel_zoom_in(void *opaque)
1505 GtkDisplayState *s = opaque;
1506 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1509 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1511 GtkDisplayState *s = opaque;
1512 VirtualConsole *vc = gd_vc_find_current(s);
1514 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1515 FALSE);
1517 vc->gfx.scale_x -= VC_SCALE_STEP;
1518 vc->gfx.scale_y -= VC_SCALE_STEP;
1520 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1521 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1523 gd_update_windowsize(vc);
1526 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1528 GtkDisplayState *s = opaque;
1529 VirtualConsole *vc = gd_vc_find_current(s);
1531 vc->gfx.scale_x = 1.0;
1532 vc->gfx.scale_y = 1.0;
1534 gd_update_windowsize(vc);
1537 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1539 GtkDisplayState *s = opaque;
1540 VirtualConsole *vc = gd_vc_find_current(s);
1542 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1543 s->free_scale = TRUE;
1544 } else {
1545 s->free_scale = FALSE;
1546 vc->gfx.scale_x = 1.0;
1547 vc->gfx.scale_y = 1.0;
1550 gd_update_windowsize(vc);
1551 gd_update_full_redraw(vc);
1554 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1556 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1557 GdkSeat *seat = gdk_display_get_default_seat(display);
1558 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1559 GdkSeatCapabilities caps = 0;
1560 GdkCursor *cursor = NULL;
1562 if (kbd) {
1563 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1565 if (ptr) {
1566 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1567 cursor = vc->s->null_cursor;
1570 if (caps) {
1571 gdk_seat_grab(seat, window, caps, false, cursor,
1572 NULL, NULL, NULL);
1573 } else {
1574 gdk_seat_ungrab(seat);
1578 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1580 if (vc->s->kbd_owner) {
1581 if (vc->s->kbd_owner == vc) {
1582 return;
1583 } else {
1584 gd_ungrab_keyboard(vc->s);
1588 win32_kbd_set_grab(true);
1589 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1590 vc->s->kbd_owner = vc;
1591 gd_update_caption(vc->s);
1592 trace_gd_grab(vc->label, "kbd", reason);
1595 static void gd_ungrab_keyboard(GtkDisplayState *s)
1597 VirtualConsole *vc = s->kbd_owner;
1599 if (vc == NULL) {
1600 return;
1602 s->kbd_owner = NULL;
1604 win32_kbd_set_grab(false);
1605 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1606 gd_update_caption(s);
1607 trace_gd_ungrab(vc->label, "kbd");
1610 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1612 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1614 if (vc->s->ptr_owner) {
1615 if (vc->s->ptr_owner == vc) {
1616 return;
1617 } else {
1618 gd_ungrab_pointer(vc->s);
1622 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1623 gdk_device_get_position(gd_get_pointer(display),
1624 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1625 vc->s->ptr_owner = vc;
1626 gd_update_caption(vc->s);
1627 trace_gd_grab(vc->label, "ptr", reason);
1630 static void gd_ungrab_pointer(GtkDisplayState *s)
1632 VirtualConsole *vc = s->ptr_owner;
1633 GdkDisplay *display;
1635 if (vc == NULL) {
1636 return;
1638 s->ptr_owner = NULL;
1640 display = gtk_widget_get_display(vc->gfx.drawing_area);
1641 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1642 gdk_device_warp(gd_get_pointer(display),
1643 gtk_widget_get_screen(vc->gfx.drawing_area),
1644 vc->s->grab_x_root, vc->s->grab_y_root);
1645 gd_update_caption(s);
1646 trace_gd_ungrab(vc->label, "ptr");
1649 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1651 GtkDisplayState *s = opaque;
1652 VirtualConsole *vc = gd_vc_find_current(s);
1654 if (gd_is_grab_active(s)) {
1655 gd_grab_keyboard(vc, "user-request-main-window");
1656 gd_grab_pointer(vc, "user-request-main-window");
1657 } else {
1658 gd_ungrab_keyboard(s);
1659 gd_ungrab_pointer(s);
1662 gd_update_cursor(vc);
1665 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1666 gpointer data)
1668 GtkDisplayState *s = data;
1669 VirtualConsole *vc;
1670 gboolean on_vga;
1672 if (!gtk_widget_get_realized(s->notebook)) {
1673 return;
1676 vc = gd_vc_find_by_page(s, arg2);
1677 if (!vc) {
1678 return;
1680 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1681 TRUE);
1682 on_vga = (vc->type == GD_VC_GFX &&
1683 qemu_console_is_graphic(vc->gfx.dcl.con));
1684 if (!on_vga) {
1685 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1686 FALSE);
1687 } else if (s->full_screen) {
1688 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1689 TRUE);
1691 gtk_widget_set_sensitive(s->grab_item, on_vga);
1692 #ifdef CONFIG_VTE
1693 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1694 #endif
1696 gd_update_windowsize(vc);
1697 gd_update_cursor(vc);
1700 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1701 gpointer opaque)
1703 VirtualConsole *vc = opaque;
1704 GtkDisplayState *s = vc->s;
1706 if (gd_grab_on_hover(s)) {
1707 gd_grab_keyboard(vc, "grab-on-hover");
1709 return TRUE;
1712 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1713 gpointer opaque)
1715 VirtualConsole *vc = opaque;
1716 GtkDisplayState *s = vc->s;
1718 if (gd_grab_on_hover(s)) {
1719 gd_ungrab_keyboard(s);
1721 return TRUE;
1724 static gboolean gd_focus_in_event(GtkWidget *widget,
1725 GdkEventFocus *event, gpointer opaque)
1727 VirtualConsole *vc = opaque;
1729 win32_kbd_set_window(gd_win32_get_hwnd(vc));
1730 return TRUE;
1733 static gboolean gd_focus_out_event(GtkWidget *widget,
1734 GdkEventFocus *event, gpointer opaque)
1736 VirtualConsole *vc = opaque;
1737 GtkDisplayState *s = vc->s;
1739 win32_kbd_set_window(NULL);
1740 gtk_release_modifiers(s);
1741 return TRUE;
1744 static gboolean gd_configure(GtkWidget *widget,
1745 GdkEventConfigure *cfg, gpointer opaque)
1747 VirtualConsole *vc = opaque;
1749 gd_set_ui_size(vc, cfg->width, cfg->height);
1750 return FALSE;
1753 /** Virtual Console Callbacks **/
1755 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1756 int idx, GSList *group, GtkWidget *view_menu)
1758 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1759 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1760 HOTKEY_MODIFIERS, 0,
1761 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1762 gtk_accel_label_set_accel(
1763 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1764 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1766 g_signal_connect(vc->menu_item, "activate",
1767 G_CALLBACK(gd_menu_switch_vc), s);
1768 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1770 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1773 #if defined(CONFIG_VTE)
1774 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1776 GtkDisplayState *s = opaque;
1777 VirtualConsole *vc = gd_vc_find_current(s);
1779 #if VTE_CHECK_VERSION(0, 50, 0)
1780 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1781 VTE_FORMAT_TEXT);
1782 #else
1783 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1784 #endif
1787 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1789 VirtualConsole *vc = opaque;
1791 if (gtk_adjustment_get_upper(adjustment) >
1792 gtk_adjustment_get_page_size(adjustment)) {
1793 gtk_widget_show(vc->vte.scrollbar);
1794 } else {
1795 gtk_widget_hide(vc->vte.scrollbar);
1799 static void gd_vc_send_chars(VirtualConsole *vc)
1801 uint32_t len, avail;
1803 len = qemu_chr_be_can_write(vc->vte.chr);
1804 avail = fifo8_num_used(&vc->vte.out_fifo);
1805 while (len > 0 && avail > 0) {
1806 const uint8_t *buf;
1807 uint32_t size;
1809 buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
1810 qemu_chr_be_write(vc->vte.chr, buf, size);
1811 len = qemu_chr_be_can_write(vc->vte.chr);
1812 avail -= size;
1816 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1818 VCChardev *vcd = VC_CHARDEV(chr);
1819 VirtualConsole *vc = vcd->console;
1821 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1822 return len;
1825 static void gd_vc_chr_accept_input(Chardev *chr)
1827 VCChardev *vcd = VC_CHARDEV(chr);
1828 VirtualConsole *vc = vcd->console;
1830 if (vc) {
1831 gd_vc_send_chars(vc);
1835 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1837 VCChardev *vcd = VC_CHARDEV(chr);
1838 VirtualConsole *vc = vcd->console;
1840 if (vc) {
1841 vc->vte.echo = echo;
1842 } else {
1843 vcd->echo = echo;
1847 static int nb_vcs;
1848 static Chardev *vcs[MAX_VCS];
1849 static void gd_vc_open(Chardev *chr,
1850 ChardevBackend *backend,
1851 bool *be_opened,
1852 Error **errp)
1854 if (nb_vcs == MAX_VCS) {
1855 error_setg(errp, "Maximum number of consoles reached");
1856 return;
1859 vcs[nb_vcs++] = chr;
1861 /* console/chardev init sometimes completes elsewhere in a 2nd
1862 * stage, so defer OPENED events until they are fully initialized
1864 *be_opened = false;
1867 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1869 ChardevClass *cc = CHARDEV_CLASS(oc);
1871 cc->open = gd_vc_open;
1872 cc->chr_write = gd_vc_chr_write;
1873 cc->chr_accept_input = gd_vc_chr_accept_input;
1874 cc->chr_set_echo = gd_vc_chr_set_echo;
1877 static const TypeInfo char_gd_vc_type_info = {
1878 .name = TYPE_CHARDEV_VC,
1879 .parent = TYPE_CHARDEV,
1880 .instance_size = sizeof(VCChardev),
1881 .class_init = char_gd_vc_class_init,
1884 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1885 gpointer user_data)
1887 VirtualConsole *vc = user_data;
1888 uint32_t free;
1890 if (vc->vte.echo) {
1891 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1892 int i;
1893 for (i = 0; i < size; i++) {
1894 uint8_t c = text[i];
1895 if (c >= 128 || isprint(c)) {
1896 /* 8-bit characters are considered printable. */
1897 vte_terminal_feed(term, &text[i], 1);
1898 } else if (c == '\r' || c == '\n') {
1899 vte_terminal_feed(term, "\r\n", 2);
1900 } else {
1901 char ctrl[2] = { '^', 0};
1902 ctrl[1] = text[i] ^ 64;
1903 vte_terminal_feed(term, ctrl, 2);
1908 free = fifo8_num_free(&vc->vte.out_fifo);
1909 fifo8_push_all(&vc->vte.out_fifo, (uint8_t *)text, MIN(free, size));
1910 gd_vc_send_chars(vc);
1912 return TRUE;
1915 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1916 Chardev *chr, int idx,
1917 GSList *group, GtkWidget *view_menu)
1919 char buffer[32];
1920 GtkWidget *box;
1921 GtkWidget *scrollbar;
1922 GtkAdjustment *vadjustment;
1923 VCChardev *vcd = VC_CHARDEV(chr);
1925 vc->s = s;
1926 vc->vte.echo = vcd->echo;
1927 vc->vte.chr = chr;
1928 fifo8_create(&vc->vte.out_fifo, 4096);
1929 vcd->console = vc;
1931 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1932 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1933 ? vc->vte.chr->label : buffer);
1934 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1936 vc->vte.terminal = vte_terminal_new();
1937 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1939 /* The documentation says that the default is UTF-8, but actually it is
1940 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1941 * VTE 0.54 (only UTF-8 is supported now). */
1942 #if !VTE_CHECK_VERSION(0, 54, 0)
1943 #if VTE_CHECK_VERSION(0, 38, 0)
1944 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1945 #else
1946 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1947 #endif
1948 #endif
1950 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1951 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1952 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1954 #if VTE_CHECK_VERSION(0, 28, 0)
1955 vadjustment = gtk_scrollable_get_vadjustment
1956 (GTK_SCROLLABLE(vc->vte.terminal));
1957 #else
1958 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1959 #endif
1961 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1962 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1964 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1965 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1967 vc->vte.box = box;
1968 vc->vte.scrollbar = scrollbar;
1970 g_signal_connect(vadjustment, "changed",
1971 G_CALLBACK(gd_vc_adjustment_changed), vc);
1973 vc->type = GD_VC_VTE;
1974 vc->tab_item = box;
1975 vc->focus = vc->vte.terminal;
1976 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1977 gtk_label_new(vc->label));
1979 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1981 return group;
1984 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1985 GtkWidget *view_menu)
1987 int i;
1989 for (i = 0; i < nb_vcs; i++) {
1990 VirtualConsole *vc = &s->vc[s->nb_vcs];
1991 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1992 s->nb_vcs++;
1995 #endif /* CONFIG_VTE */
1997 /** Window Creation **/
1999 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
2001 g_signal_connect(vc->gfx.drawing_area, "draw",
2002 G_CALLBACK(gd_draw_event), vc);
2003 #if defined(CONFIG_OPENGL)
2004 if (gtk_use_gl_area) {
2005 /* wire up GtkGlArea events */
2006 g_signal_connect(vc->gfx.drawing_area, "render",
2007 G_CALLBACK(gd_render_event), vc);
2008 g_signal_connect(vc->gfx.drawing_area, "resize",
2009 G_CALLBACK(gd_resize_event), vc);
2011 #endif
2012 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
2013 g_signal_connect(vc->gfx.drawing_area, "event",
2014 G_CALLBACK(gd_event), vc);
2015 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
2016 G_CALLBACK(gd_button_event), vc);
2017 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
2018 G_CALLBACK(gd_button_event), vc);
2019 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
2020 G_CALLBACK(gd_scroll_event), vc);
2021 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2022 G_CALLBACK(gd_key_event), vc);
2023 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
2024 G_CALLBACK(gd_key_event), vc);
2025 g_signal_connect(vc->gfx.drawing_area, "touch-event",
2026 G_CALLBACK(gd_touch_event), vc);
2028 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
2029 G_CALLBACK(gd_enter_event), vc);
2030 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
2031 G_CALLBACK(gd_leave_event), vc);
2032 g_signal_connect(vc->gfx.drawing_area, "focus-in-event",
2033 G_CALLBACK(gd_focus_in_event), vc);
2034 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
2035 G_CALLBACK(gd_focus_out_event), vc);
2036 g_signal_connect(vc->gfx.drawing_area, "configure-event",
2037 G_CALLBACK(gd_configure), vc);
2038 g_signal_connect(vc->gfx.drawing_area, "grab-broken-event",
2039 G_CALLBACK(gd_grab_broken_event), vc);
2040 } else {
2041 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2042 G_CALLBACK(gd_text_key_down), vc);
2046 static void gd_connect_signals(GtkDisplayState *s)
2048 g_signal_connect(s->show_tabs_item, "activate",
2049 G_CALLBACK(gd_menu_show_tabs), s);
2050 g_signal_connect(s->untabify_item, "activate",
2051 G_CALLBACK(gd_menu_untabify), s);
2052 g_signal_connect(s->show_menubar_item, "activate",
2053 G_CALLBACK(gd_menu_show_menubar), s);
2055 g_signal_connect(s->window, "delete-event",
2056 G_CALLBACK(gd_window_close), s);
2058 g_signal_connect(s->pause_item, "activate",
2059 G_CALLBACK(gd_menu_pause), s);
2060 g_signal_connect(s->reset_item, "activate",
2061 G_CALLBACK(gd_menu_reset), s);
2062 g_signal_connect(s->powerdown_item, "activate",
2063 G_CALLBACK(gd_menu_powerdown), s);
2064 g_signal_connect(s->quit_item, "activate",
2065 G_CALLBACK(gd_menu_quit), s);
2066 #if defined(CONFIG_VTE)
2067 g_signal_connect(s->copy_item, "activate",
2068 G_CALLBACK(gd_menu_copy), s);
2069 #endif
2070 g_signal_connect(s->full_screen_item, "activate",
2071 G_CALLBACK(gd_menu_full_screen), s);
2072 g_signal_connect(s->zoom_in_item, "activate",
2073 G_CALLBACK(gd_menu_zoom_in), s);
2074 g_signal_connect(s->zoom_out_item, "activate",
2075 G_CALLBACK(gd_menu_zoom_out), s);
2076 g_signal_connect(s->zoom_fixed_item, "activate",
2077 G_CALLBACK(gd_menu_zoom_fixed), s);
2078 g_signal_connect(s->zoom_fit_item, "activate",
2079 G_CALLBACK(gd_menu_zoom_fit), s);
2080 g_signal_connect(s->grab_item, "activate",
2081 G_CALLBACK(gd_menu_grab_input), s);
2082 g_signal_connect(s->notebook, "switch-page",
2083 G_CALLBACK(gd_change_page), s);
2086 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2088 GtkWidget *machine_menu;
2089 GtkWidget *separator;
2091 machine_menu = gtk_menu_new();
2092 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2094 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2095 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2097 separator = gtk_separator_menu_item_new();
2098 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2100 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2101 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2103 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2104 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2106 separator = gtk_separator_menu_item_new();
2107 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2109 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2110 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2111 "<QEMU>/Machine/Quit");
2112 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2113 GDK_KEY_q, HOTKEY_MODIFIERS);
2114 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2116 return machine_menu;
2119 #if defined(CONFIG_OPENGL)
2120 static void gl_area_realize(GtkGLArea *area, VirtualConsole *vc)
2122 gtk_gl_area_make_current(area);
2123 qemu_egl_display = eglGetCurrentDisplay();
2124 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2125 if (!vc->gfx.has_dmabuf) {
2126 error_report("GtkGLArea console lacks DMABUF support.");
2129 #endif
2131 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2132 QemuConsole *con, int idx,
2133 GSList *group, GtkWidget *view_menu)
2135 bool zoom_to_fit = false;
2136 int i;
2138 vc->label = qemu_console_get_label(con);
2139 vc->s = s;
2140 vc->gfx.scale_x = 1.0;
2141 vc->gfx.scale_y = 1.0;
2143 #if defined(CONFIG_OPENGL)
2144 if (display_opengl) {
2145 if (gtk_use_gl_area) {
2146 vc->gfx.drawing_area = gtk_gl_area_new();
2147 g_signal_connect(vc->gfx.drawing_area, "realize",
2148 G_CALLBACK(gl_area_realize), vc);
2149 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2150 vc->gfx.dgc.ops = &gl_area_ctx_ops;
2151 } else {
2152 #ifdef CONFIG_X11
2153 vc->gfx.drawing_area = gtk_drawing_area_new();
2155 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2156 * It is required for opengl rendering on X11 though. A
2157 * proper replacement (native opengl support) is only
2158 * available in 3.16+. Silence the warning if possible.
2160 #pragma GCC diagnostic push
2161 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2162 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2163 #pragma GCC diagnostic pop
2164 vc->gfx.dcl.ops = &dcl_egl_ops;
2165 vc->gfx.dgc.ops = &egl_ctx_ops;
2166 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2167 #else
2168 abort();
2169 #endif
2171 } else
2172 #endif
2174 vc->gfx.drawing_area = gtk_drawing_area_new();
2175 vc->gfx.dcl.ops = &dcl_ops;
2179 gtk_widget_add_events(vc->gfx.drawing_area,
2180 GDK_POINTER_MOTION_MASK |
2181 GDK_BUTTON_PRESS_MASK |
2182 GDK_BUTTON_RELEASE_MASK |
2183 GDK_BUTTON_MOTION_MASK |
2184 GDK_TOUCH_MASK |
2185 GDK_ENTER_NOTIFY_MASK |
2186 GDK_LEAVE_NOTIFY_MASK |
2187 GDK_SCROLL_MASK |
2188 GDK_SMOOTH_SCROLL_MASK |
2189 GDK_KEY_PRESS_MASK);
2190 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2192 vc->type = GD_VC_GFX;
2193 vc->tab_item = vc->gfx.drawing_area;
2194 vc->focus = vc->gfx.drawing_area;
2195 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2196 vc->tab_item, gtk_label_new(vc->label));
2198 vc->gfx.kbd = qkbd_state_init(con);
2199 vc->gfx.dcl.con = con;
2201 if (display_opengl) {
2202 qemu_console_set_display_gl_ctx(con, &vc->gfx.dgc);
2204 register_displaychangelistener(&vc->gfx.dcl);
2206 gd_connect_vc_gfx_signals(vc);
2207 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2209 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2210 zoom_to_fit = true;
2212 if (s->opts->u.gtk.has_zoom_to_fit) {
2213 zoom_to_fit = s->opts->u.gtk.zoom_to_fit;
2215 if (zoom_to_fit) {
2216 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2217 s->free_scale = true;
2220 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; i++) {
2221 struct touch_slot *slot = &touch_slots[i];
2222 slot->tracking_id = -1;
2225 return group;
2228 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, DisplayOptions *opts)
2230 GSList *group = NULL;
2231 GtkWidget *view_menu;
2232 GtkWidget *separator;
2233 QemuConsole *con;
2234 int vc;
2236 view_menu = gtk_menu_new();
2237 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2239 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2241 #if defined(CONFIG_VTE)
2242 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2243 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2244 #endif
2246 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2247 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2248 gtk_accel_label_set_accel(
2249 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2250 GDK_KEY_f, HOTKEY_MODIFIERS);
2251 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2253 separator = gtk_separator_menu_item_new();
2254 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2256 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2257 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2258 "<QEMU>/View/Zoom In");
2259 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2260 HOTKEY_MODIFIERS);
2261 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2262 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2263 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2265 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2266 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2267 "<QEMU>/View/Zoom Out");
2268 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2269 HOTKEY_MODIFIERS);
2270 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2272 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2273 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2274 "<QEMU>/View/Zoom Fixed");
2275 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2276 HOTKEY_MODIFIERS);
2277 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2279 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2280 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2282 separator = gtk_separator_menu_item_new();
2283 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2285 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2286 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2288 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2289 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2290 "<QEMU>/View/Grab Input");
2291 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2292 HOTKEY_MODIFIERS);
2293 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2295 separator = gtk_separator_menu_item_new();
2296 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2298 /* gfx */
2299 for (vc = 0;; vc++) {
2300 con = qemu_console_lookup_by_index(vc);
2301 if (!con) {
2302 break;
2304 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2305 vc, group, view_menu);
2306 s->nb_vcs++;
2309 #if defined(CONFIG_VTE)
2310 /* vte */
2311 gd_vcs_init(s, group, view_menu);
2312 #endif
2314 separator = gtk_separator_menu_item_new();
2315 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2317 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2318 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2320 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2321 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2323 s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
2324 _("Show Menubar"));
2325 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
2326 !opts->u.gtk.has_show_menubar ||
2327 opts->u.gtk.show_menubar);
2328 gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
2329 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
2330 gtk_accel_label_set_accel(
2331 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
2332 GDK_KEY_m, HOTKEY_MODIFIERS);
2333 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
2335 return view_menu;
2338 static void gd_create_menus(GtkDisplayState *s, DisplayOptions *opts)
2340 GtkSettings *settings;
2342 s->accel_group = gtk_accel_group_new();
2343 s->machine_menu = gd_create_menu_machine(s);
2344 s->view_menu = gd_create_menu_view(s, opts);
2346 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2347 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2348 s->machine_menu);
2349 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2351 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2352 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2353 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2355 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2356 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2358 /* Disable the default "F10" menu shortcut. */
2359 settings = gtk_widget_get_settings(s->window);
2360 g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
2364 static gboolean gtkinit;
2366 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2368 VirtualConsole *vc;
2370 GtkDisplayState *s;
2371 GdkDisplay *window_display;
2372 GtkIconTheme *theme;
2373 char *dir;
2374 int idx;
2376 if (!gtkinit) {
2377 fprintf(stderr, "gtk initialization failed\n");
2378 exit(1);
2380 assert(opts->type == DISPLAY_TYPE_GTK);
2381 s = g_malloc0(sizeof(*s));
2382 s->opts = opts;
2384 theme = gtk_icon_theme_get_default();
2385 dir = get_relocated_path(CONFIG_QEMU_ICONDIR);
2386 gtk_icon_theme_prepend_search_path(theme, dir);
2387 g_free(dir);
2388 g_set_prgname("qemu");
2390 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2391 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2392 s->notebook = gtk_notebook_new();
2393 s->menu_bar = gtk_menu_bar_new();
2395 s->free_scale = FALSE;
2397 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2398 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2399 * printable, but without changing any of the character classes to make
2400 * sure that we don't accidentally break implicit assumptions. */
2401 setlocale(LC_MESSAGES, "");
2402 setlocale(LC_CTYPE, "C.UTF-8");
2403 dir = get_relocated_path(CONFIG_QEMU_LOCALEDIR);
2404 bindtextdomain("qemu", dir);
2405 g_free(dir);
2406 bind_textdomain_codeset("qemu", "UTF-8");
2407 textdomain("qemu");
2409 window_display = gtk_widget_get_display(s->window);
2410 if (s->opts->has_show_cursor && s->opts->show_cursor) {
2411 s->null_cursor = NULL; /* default pointer */
2412 } else {
2413 s->null_cursor = gdk_cursor_new_for_display(window_display,
2414 GDK_BLANK_CURSOR);
2417 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2418 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2419 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2421 gtk_window_set_icon_name(GTK_WINDOW(s->window), "qemu");
2423 gd_create_menus(s, opts);
2425 gd_connect_signals(s);
2427 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2428 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2430 gd_update_caption(s);
2432 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2433 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2435 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2437 gtk_widget_show_all(s->window);
2439 for (idx = 0;; idx++) {
2440 QemuConsole *con = qemu_console_lookup_by_index(idx);
2441 if (!con) {
2442 break;
2444 gtk_widget_realize(s->vc[idx].gfx.drawing_area);
2447 if (opts->u.gtk.has_show_menubar &&
2448 !opts->u.gtk.show_menubar) {
2449 gtk_widget_hide(s->menu_bar);
2452 vc = gd_vc_find_current(s);
2453 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2454 #ifdef CONFIG_VTE
2455 gtk_widget_set_sensitive(s->copy_item,
2456 vc && vc->type == GD_VC_VTE);
2457 #endif
2459 if (opts->has_full_screen &&
2460 opts->full_screen) {
2461 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2463 if (opts->u.gtk.has_grab_on_hover &&
2464 opts->u.gtk.grab_on_hover) {
2465 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2467 if (opts->u.gtk.has_show_tabs &&
2468 opts->u.gtk.show_tabs) {
2469 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_tabs_item));
2471 #ifdef CONFIG_GTK_CLIPBOARD
2472 gd_clipboard_init(s);
2473 #endif /* CONFIG_GTK_CLIPBOARD */
2476 static void early_gtk_display_init(DisplayOptions *opts)
2478 /* The QEMU code relies on the assumption that it's always run in
2479 * the C locale. Therefore it is not prepared to deal with
2480 * operations that produce different results depending on the
2481 * locale, such as printf's formatting of decimal numbers, and
2482 * possibly others.
2484 * Since GTK+ calls setlocale() by default -importing the locale
2485 * settings from the environment- we must prevent it from doing so
2486 * using gtk_disable_setlocale().
2488 * QEMU's GTK+ UI, however, _does_ have translations for some of
2489 * the menu items. As a trade-off between a functionally correct
2490 * QEMU and a fully internationalized UI we support importing
2491 * LC_MESSAGES from the environment (see the setlocale() call
2492 * earlier in this file). This allows us to display translated
2493 * messages leaving everything else untouched.
2495 gtk_disable_setlocale();
2496 gtkinit = gtk_init_check(NULL, NULL);
2497 if (!gtkinit) {
2498 /* don't exit yet, that'll break -help */
2499 return;
2502 assert(opts->type == DISPLAY_TYPE_GTK);
2503 if (opts->has_gl && opts->gl != DISPLAYGL_MODE_OFF) {
2504 #if defined(CONFIG_OPENGL)
2505 #if defined(GDK_WINDOWING_WAYLAND)
2506 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2507 gtk_use_gl_area = true;
2508 gtk_gl_area_init();
2509 } else
2510 #endif
2511 #if defined(GDK_WINDOWING_WIN32)
2512 if (GDK_IS_WIN32_DISPLAY(gdk_display_get_default())) {
2513 gtk_use_gl_area = true;
2514 gtk_gl_area_init();
2515 } else
2516 #endif
2518 #ifdef CONFIG_X11
2519 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
2520 gtk_egl_init(mode);
2521 #endif
2523 #endif
2526 keycode_map = gd_get_keymap(&keycode_maplen);
2528 #if defined(CONFIG_VTE)
2529 type_register(&char_gd_vc_type_info);
2530 #endif
2533 static QemuDisplay qemu_display_gtk = {
2534 .type = DISPLAY_TYPE_GTK,
2535 .early_init = early_gtk_display_init,
2536 .init = gtk_display_init,
2537 .vc = "vc",
2540 static void register_gtk(void)
2542 qemu_display_register(&qemu_display_gtk);
2545 type_init(register_gtk);
2547 #ifdef CONFIG_OPENGL
2548 module_dep("ui-opengl");
2549 #endif