accel: Restrict sysemu stubs to system emulation
[qemu/rayw.git] / ui / gtk.c
bloba8567b9ddc8f57de4cdad71e1b084d5c28e94046
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/main-loop.h"
41 #include "ui/console.h"
42 #include "ui/gtk.h"
43 #ifdef G_OS_WIN32
44 #include <gdk/gdkwin32.h>
45 #endif
46 #include "ui/win32-kbd-hook.h"
48 #include <glib/gi18n.h>
49 #include <locale.h>
50 #if defined(CONFIG_VTE)
51 #include <vte/vte.h>
52 #endif
53 #include <math.h>
55 #include "trace.h"
56 #include "qemu/cutils.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 bool gtk_use_gl_area;
135 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
136 static void gd_ungrab_pointer(GtkDisplayState *s);
137 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
138 static void gd_ungrab_keyboard(GtkDisplayState *s);
140 /** Utility Functions **/
142 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
144 VirtualConsole *vc;
145 gint i;
147 for (i = 0; i < s->nb_vcs; i++) {
148 vc = &s->vc[i];
149 if (gtk_check_menu_item_get_active
150 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
151 return vc;
154 return NULL;
157 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
159 VirtualConsole *vc;
160 gint i, p;
162 for (i = 0; i < s->nb_vcs; i++) {
163 vc = &s->vc[i];
164 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
165 if (p == page) {
166 return vc;
169 return NULL;
172 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
174 gint page;
176 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
177 return gd_vc_find_by_page(s, page);
180 static bool gd_is_grab_active(GtkDisplayState *s)
182 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
185 static bool gd_grab_on_hover(GtkDisplayState *s)
187 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
190 static void gd_update_cursor(VirtualConsole *vc)
192 GtkDisplayState *s = vc->s;
193 GdkWindow *window;
195 if (vc->type != GD_VC_GFX ||
196 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
197 return;
200 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
201 return;
204 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
205 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
206 gdk_window_set_cursor(window, s->null_cursor);
207 } else {
208 gdk_window_set_cursor(window, NULL);
212 static void gd_update_caption(GtkDisplayState *s)
214 const char *status = "";
215 gchar *prefix;
216 gchar *title;
217 const char *grab = "";
218 bool is_paused = !runstate_is_running();
219 int i;
221 if (qemu_name) {
222 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
223 } else {
224 prefix = g_strdup_printf("QEMU");
227 if (s->ptr_owner != NULL &&
228 s->ptr_owner->window == NULL) {
229 grab = _(" - Press Ctrl+Alt+G to release grab");
232 if (is_paused) {
233 status = _(" [Paused]");
235 s->external_pause_update = true;
236 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
237 is_paused);
238 s->external_pause_update = false;
240 title = g_strdup_printf("%s%s%s", prefix, status, grab);
241 gtk_window_set_title(GTK_WINDOW(s->window), title);
242 g_free(title);
244 for (i = 0; i < s->nb_vcs; i++) {
245 VirtualConsole *vc = &s->vc[i];
247 if (!vc->window) {
248 continue;
250 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
251 vc == s->kbd_owner ? " +kbd" : "",
252 vc == s->ptr_owner ? " +ptr" : "");
253 gtk_window_set_title(GTK_WINDOW(vc->window), title);
254 g_free(title);
257 g_free(prefix);
260 static void gd_update_geometry_hints(VirtualConsole *vc)
262 GtkDisplayState *s = vc->s;
263 GdkWindowHints mask = 0;
264 GdkGeometry geo = {};
265 GtkWidget *geo_widget = NULL;
266 GtkWindow *geo_window;
268 if (vc->type == GD_VC_GFX) {
269 if (!vc->gfx.ds) {
270 return;
272 if (s->free_scale) {
273 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
274 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
275 mask |= GDK_HINT_MIN_SIZE;
276 } else {
277 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
278 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
279 mask |= GDK_HINT_MIN_SIZE;
281 geo_widget = vc->gfx.drawing_area;
282 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
284 #if defined(CONFIG_VTE)
285 } else if (vc->type == GD_VC_VTE) {
286 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
287 GtkBorder padding = { 0 };
289 #if VTE_CHECK_VERSION(0, 37, 0)
290 gtk_style_context_get_padding(
291 gtk_widget_get_style_context(vc->vte.terminal),
292 gtk_widget_get_state_flags(vc->vte.terminal),
293 &padding);
294 #else
296 GtkBorder *ib = NULL;
297 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
298 if (ib) {
299 padding = *ib;
300 gtk_border_free(ib);
303 #endif
305 geo.width_inc = vte_terminal_get_char_width(term);
306 geo.height_inc = vte_terminal_get_char_height(term);
307 mask |= GDK_HINT_RESIZE_INC;
308 geo.base_width = geo.width_inc;
309 geo.base_height = geo.height_inc;
310 mask |= GDK_HINT_BASE_SIZE;
311 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
312 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
313 mask |= GDK_HINT_MIN_SIZE;
315 geo.base_width += padding.left + padding.right;
316 geo.base_height += padding.top + padding.bottom;
317 geo.min_width += padding.left + padding.right;
318 geo.min_height += padding.top + padding.bottom;
319 geo_widget = vc->vte.terminal;
320 #endif
323 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
324 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
327 void gd_update_windowsize(VirtualConsole *vc)
329 GtkDisplayState *s = vc->s;
331 gd_update_geometry_hints(vc);
333 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
334 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
335 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
339 static void gd_update_full_redraw(VirtualConsole *vc)
341 GtkWidget *area = vc->gfx.drawing_area;
342 int ww, wh;
343 ww = gdk_window_get_width(gtk_widget_get_window(area));
344 wh = gdk_window_get_height(gtk_widget_get_window(area));
345 #if defined(CONFIG_OPENGL)
346 if (vc->gfx.gls && gtk_use_gl_area) {
347 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
348 return;
350 #endif
351 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
354 static void gtk_release_modifiers(GtkDisplayState *s)
356 VirtualConsole *vc = gd_vc_find_current(s);
358 if (vc->type != GD_VC_GFX ||
359 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
360 return;
362 qkbd_state_lift_all_keys(vc->gfx.kbd);
365 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
366 GtkWidget *widget)
368 g_object_ref(G_OBJECT(widget));
369 gtk_container_remove(GTK_CONTAINER(from), widget);
370 gtk_container_add(GTK_CONTAINER(to), widget);
371 g_object_unref(G_OBJECT(widget));
374 static void *gd_win32_get_hwnd(VirtualConsole *vc)
376 #ifdef G_OS_WIN32
377 return gdk_win32_window_get_impl_hwnd(
378 gtk_widget_get_window(vc->window ? vc->window : vc->s->window));
379 #else
380 return NULL;
381 #endif
384 /** DisplayState Callbacks **/
386 static void gd_update(DisplayChangeListener *dcl,
387 int x, int y, int w, int h)
389 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
390 GdkWindow *win;
391 int x1, x2, y1, y2;
392 int mx, my;
393 int fbw, fbh;
394 int ww, wh;
396 trace_gd_update(vc->label, x, y, w, h);
398 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
399 return;
402 if (vc->gfx.convert) {
403 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
404 NULL, vc->gfx.convert,
405 x, y, 0, 0, x, y, w, h);
408 x1 = floor(x * vc->gfx.scale_x);
409 y1 = floor(y * vc->gfx.scale_y);
411 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
412 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
414 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
415 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
417 win = gtk_widget_get_window(vc->gfx.drawing_area);
418 if (!win) {
419 return;
421 ww = gdk_window_get_width(win);
422 wh = gdk_window_get_height(win);
424 mx = my = 0;
425 if (ww > fbw) {
426 mx = (ww - fbw) / 2;
428 if (wh > fbh) {
429 my = (wh - fbh) / 2;
432 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
433 mx + x1, my + y1, (x2 - x1), (y2 - y1));
436 static void gd_refresh(DisplayChangeListener *dcl)
438 graphic_hw_update(dcl->con);
441 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
443 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
446 static void gd_mouse_set(DisplayChangeListener *dcl,
447 int x, int y, int visible)
449 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
450 GdkDisplay *dpy;
451 gint x_root, y_root;
453 if (qemu_input_is_absolute()) {
454 return;
457 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
458 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
459 x, y, &x_root, &y_root);
460 gdk_device_warp(gd_get_pointer(dpy),
461 gtk_widget_get_screen(vc->gfx.drawing_area),
462 x_root, y_root);
463 vc->s->last_x = x;
464 vc->s->last_y = y;
467 static void gd_cursor_define(DisplayChangeListener *dcl,
468 QEMUCursor *c)
470 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
471 GdkPixbuf *pixbuf;
472 GdkCursor *cursor;
474 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
475 return;
478 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
479 GDK_COLORSPACE_RGB, true, 8,
480 c->width, c->height, c->width * 4,
481 NULL, NULL);
482 cursor = gdk_cursor_new_from_pixbuf
483 (gtk_widget_get_display(vc->gfx.drawing_area),
484 pixbuf, c->hot_x, c->hot_y);
485 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
486 g_object_unref(pixbuf);
487 g_object_unref(cursor);
490 static void gd_switch(DisplayChangeListener *dcl,
491 DisplaySurface *surface)
493 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
494 bool resized = true;
496 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
498 if (vc->gfx.surface) {
499 cairo_surface_destroy(vc->gfx.surface);
500 vc->gfx.surface = NULL;
502 if (vc->gfx.convert) {
503 pixman_image_unref(vc->gfx.convert);
504 vc->gfx.convert = NULL;
507 if (vc->gfx.ds &&
508 surface_width(vc->gfx.ds) == surface_width(surface) &&
509 surface_height(vc->gfx.ds) == surface_height(surface)) {
510 resized = false;
512 vc->gfx.ds = surface;
514 if (surface->format == PIXMAN_x8r8g8b8) {
516 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
518 * No need to convert, use surface directly. Should be the
519 * common case as this is qemu_default_pixelformat(32) too.
521 vc->gfx.surface = cairo_image_surface_create_for_data
522 (surface_data(surface),
523 CAIRO_FORMAT_RGB24,
524 surface_width(surface),
525 surface_height(surface),
526 surface_stride(surface));
527 } else {
528 /* Must convert surface, use pixman to do it. */
529 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
530 surface_width(surface),
531 surface_height(surface),
532 NULL, 0);
533 vc->gfx.surface = cairo_image_surface_create_for_data
534 ((void *)pixman_image_get_data(vc->gfx.convert),
535 CAIRO_FORMAT_RGB24,
536 pixman_image_get_width(vc->gfx.convert),
537 pixman_image_get_height(vc->gfx.convert),
538 pixman_image_get_stride(vc->gfx.convert));
539 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
540 NULL, vc->gfx.convert,
541 0, 0, 0, 0, 0, 0,
542 pixman_image_get_width(vc->gfx.convert),
543 pixman_image_get_height(vc->gfx.convert));
546 if (resized) {
547 gd_update_windowsize(vc);
548 } else {
549 gd_update_full_redraw(vc);
553 static const DisplayChangeListenerOps dcl_ops = {
554 .dpy_name = "gtk",
555 .dpy_gfx_update = gd_update,
556 .dpy_gfx_switch = gd_switch,
557 .dpy_gfx_check_format = qemu_pixman_check_format,
558 .dpy_refresh = gd_refresh,
559 .dpy_mouse_set = gd_mouse_set,
560 .dpy_cursor_define = gd_cursor_define,
564 #if defined(CONFIG_OPENGL)
566 static bool gd_has_dmabuf(DisplayChangeListener *dcl)
568 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
570 if (gtk_use_gl_area && !gtk_widget_get_realized(vc->gfx.drawing_area)) {
571 /* FIXME: Assume it will work, actual check done after realize */
572 /* fixing this would require delaying listener registration */
573 return true;
576 return vc->gfx.has_dmabuf;
579 static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
580 QemuDmaBuf *dmabuf)
582 #ifdef CONFIG_GBM
583 egl_dmabuf_release_texture(dmabuf);
584 #endif
587 void gd_hw_gl_flushed(void *vcon)
589 VirtualConsole *vc = vcon;
590 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
592 qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
593 close(dmabuf->fence_fd);
594 dmabuf->fence_fd = -1;
595 graphic_hw_gl_block(vc->gfx.dcl.con, false);
598 /** DisplayState Callbacks (opengl version) **/
600 static const DisplayChangeListenerOps dcl_gl_area_ops = {
601 .dpy_name = "gtk-egl",
602 .dpy_gfx_update = gd_gl_area_update,
603 .dpy_gfx_switch = gd_gl_area_switch,
604 .dpy_gfx_check_format = console_gl_check_format,
605 .dpy_refresh = gd_gl_area_refresh,
606 .dpy_mouse_set = gd_mouse_set,
607 .dpy_cursor_define = gd_cursor_define,
609 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
610 .dpy_gl_scanout_disable = gd_gl_area_scanout_disable,
611 .dpy_gl_update = gd_gl_area_scanout_flush,
612 .dpy_gl_scanout_dmabuf = gd_gl_area_scanout_dmabuf,
613 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
614 .dpy_has_dmabuf = gd_has_dmabuf,
617 static const DisplayGLCtxOps gl_area_ctx_ops = {
618 .compatible_dcl = &dcl_gl_area_ops,
619 .dpy_gl_ctx_create = gd_gl_area_create_context,
620 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
621 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
624 #ifdef CONFIG_X11
625 static const DisplayChangeListenerOps dcl_egl_ops = {
626 .dpy_name = "gtk-egl",
627 .dpy_gfx_update = gd_egl_update,
628 .dpy_gfx_switch = gd_egl_switch,
629 .dpy_gfx_check_format = console_gl_check_format,
630 .dpy_refresh = gd_egl_refresh,
631 .dpy_mouse_set = gd_mouse_set,
632 .dpy_cursor_define = gd_cursor_define,
634 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
635 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
636 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
637 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
638 .dpy_gl_cursor_position = gd_egl_cursor_position,
639 .dpy_gl_update = gd_egl_flush,
640 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
641 .dpy_has_dmabuf = gd_has_dmabuf,
644 static const DisplayGLCtxOps egl_ctx_ops = {
645 .compatible_dcl = &dcl_egl_ops,
646 .dpy_gl_ctx_create = gd_egl_create_context,
647 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
648 .dpy_gl_ctx_make_current = gd_egl_make_current,
650 #endif
652 #endif /* CONFIG_OPENGL */
654 /** QEMU Events **/
656 static void gd_change_runstate(void *opaque, bool running, RunState state)
658 GtkDisplayState *s = opaque;
660 gd_update_caption(s);
663 static void gd_mouse_mode_change(Notifier *notify, void *data)
665 GtkDisplayState *s;
666 int i;
668 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
669 /* release the grab at switching to absolute mode */
670 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
671 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
672 FALSE);
674 for (i = 0; i < s->nb_vcs; i++) {
675 VirtualConsole *vc = &s->vc[i];
676 gd_update_cursor(vc);
680 /** GTK Events **/
682 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
683 void *opaque)
685 GtkDisplayState *s = opaque;
686 bool allow_close = true;
688 if (s->opts->has_window_close && !s->opts->window_close) {
689 allow_close = false;
692 if (allow_close) {
693 qmp_quit(NULL);
696 return TRUE;
699 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
701 QemuUIInfo info;
703 memset(&info, 0, sizeof(info));
704 info.width = width;
705 info.height = height;
706 dpy_set_ui_info(vc->gfx.dcl.con, &info, true);
709 #if defined(CONFIG_OPENGL)
711 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
712 void *opaque)
714 VirtualConsole *vc = opaque;
716 if (vc->gfx.gls) {
717 gd_gl_area_draw(vc);
719 return TRUE;
722 static void gd_resize_event(GtkGLArea *area,
723 gint width, gint height, gpointer *opaque)
725 VirtualConsole *vc = (void *)opaque;
727 gd_set_ui_info(vc, width, height);
730 #endif
733 * If available, return the update interval of the monitor in ms,
734 * else return 0 (the default update interval).
736 int gd_monitor_update_interval(GtkWidget *widget)
738 #ifdef GDK_VERSION_3_22
739 GdkWindow *win = gtk_widget_get_window(widget);
741 if (win) {
742 GdkDisplay *dpy = gtk_widget_get_display(widget);
743 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
744 int refresh_rate = gdk_monitor_get_refresh_rate(monitor); /* [mHz] */
746 if (refresh_rate) {
747 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
748 return MIN(1000 * 1000 / refresh_rate,
749 GUI_REFRESH_INTERVAL_DEFAULT);
752 #endif
753 return 0;
756 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
758 VirtualConsole *vc = opaque;
759 GtkDisplayState *s = vc->s;
760 int mx, my;
761 int ww, wh;
762 int fbw, fbh;
764 #if defined(CONFIG_OPENGL)
765 if (vc->gfx.gls) {
766 if (gtk_use_gl_area) {
767 /* invoke render callback please */
768 return FALSE;
769 } else {
770 #ifdef CONFIG_X11
771 gd_egl_draw(vc);
772 return TRUE;
773 #else
774 abort();
775 #endif
778 #endif
780 if (!gtk_widget_get_realized(widget)) {
781 return FALSE;
783 if (!vc->gfx.ds) {
784 return FALSE;
786 if (!vc->gfx.surface) {
787 return FALSE;
790 vc->gfx.dcl.update_interval =
791 gd_monitor_update_interval(vc->window ? vc->window : s->window);
793 fbw = surface_width(vc->gfx.ds);
794 fbh = surface_height(vc->gfx.ds);
796 ww = gdk_window_get_width(gtk_widget_get_window(widget));
797 wh = gdk_window_get_height(gtk_widget_get_window(widget));
799 if (s->full_screen) {
800 vc->gfx.scale_x = (double)ww / fbw;
801 vc->gfx.scale_y = (double)wh / fbh;
802 } else if (s->free_scale) {
803 double sx, sy;
805 sx = (double)ww / fbw;
806 sy = (double)wh / fbh;
808 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
811 fbw *= vc->gfx.scale_x;
812 fbh *= vc->gfx.scale_y;
814 mx = my = 0;
815 if (ww > fbw) {
816 mx = (ww - fbw) / 2;
818 if (wh > fbh) {
819 my = (wh - fbh) / 2;
822 cairo_rectangle(cr, 0, 0, ww, wh);
824 /* Optionally cut out the inner area where the pixmap
825 will be drawn. This avoids 'flashing' since we're
826 not double-buffering. Note we're using the undocumented
827 behaviour of drawing the rectangle from right to left
828 to cut out the whole */
829 cairo_rectangle(cr, mx + fbw, my,
830 -1 * fbw, fbh);
831 cairo_fill(cr);
833 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
834 cairo_set_source_surface(cr, vc->gfx.surface,
835 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
836 cairo_paint(cr);
838 return TRUE;
841 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
842 void *opaque)
844 VirtualConsole *vc = opaque;
845 GtkDisplayState *s = vc->s;
846 GdkWindow *window;
847 int x, y;
848 int mx, my;
849 int fbh, fbw;
850 int ww, wh, ws;
852 if (!vc->gfx.ds) {
853 return TRUE;
856 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
857 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
859 window = gtk_widget_get_window(vc->gfx.drawing_area);
860 ww = gdk_window_get_width(window);
861 wh = gdk_window_get_height(window);
862 ws = gdk_window_get_scale_factor(window);
864 mx = my = 0;
865 if (ww > fbw) {
866 mx = (ww - fbw) / 2;
868 if (wh > fbh) {
869 my = (wh - fbh) / 2;
872 x = (motion->x - mx) / vc->gfx.scale_x * ws;
873 y = (motion->y - my) / vc->gfx.scale_y * ws;
875 if (qemu_input_is_absolute()) {
876 if (x < 0 || y < 0 ||
877 x >= surface_width(vc->gfx.ds) ||
878 y >= surface_height(vc->gfx.ds)) {
879 return TRUE;
881 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
882 0, surface_width(vc->gfx.ds));
883 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
884 0, surface_height(vc->gfx.ds));
885 qemu_input_event_sync();
886 } else if (s->last_set && s->ptr_owner == vc) {
887 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
888 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
889 qemu_input_event_sync();
891 s->last_x = x;
892 s->last_y = y;
893 s->last_set = TRUE;
895 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
896 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
897 GdkDisplay *dpy = gtk_widget_get_display(widget);
898 GdkWindow *win = gtk_widget_get_window(widget);
899 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
900 GdkRectangle geometry;
902 int x = (int)motion->x_root;
903 int y = (int)motion->y_root;
905 gdk_monitor_get_geometry(monitor, &geometry);
907 /* In relative mode check to see if client pointer hit
908 * one of the monitor edges, and if so move it back to the
909 * center of the monitor. This is important because the pointer
910 * in the server doesn't correspond 1-for-1, and so
911 * may still be only half way across the screen. Without
912 * this warp, the server pointer would thus appear to hit
913 * an invisible wall */
914 if (x <= geometry.x || x - geometry.x >= geometry.width - 1 ||
915 y <= geometry.y || y - geometry.y >= geometry.height - 1) {
916 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
917 x = geometry.x + geometry.width / 2;
918 y = geometry.y + geometry.height / 2;
920 gdk_device_warp(dev, screen, x, y);
921 s->last_set = FALSE;
922 return FALSE;
925 return TRUE;
928 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
929 void *opaque)
931 VirtualConsole *vc = opaque;
932 GtkDisplayState *s = vc->s;
933 InputButton btn;
935 /* implicitly grab the input at the first click in the relative mode */
936 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
937 !qemu_input_is_absolute() && s->ptr_owner != vc) {
938 if (!vc->window) {
939 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
940 TRUE);
941 } else {
942 gd_grab_pointer(vc, "relative-mode-click");
944 return TRUE;
947 if (button->button == 1) {
948 btn = INPUT_BUTTON_LEFT;
949 } else if (button->button == 2) {
950 btn = INPUT_BUTTON_MIDDLE;
951 } else if (button->button == 3) {
952 btn = INPUT_BUTTON_RIGHT;
953 } else if (button->button == 8) {
954 btn = INPUT_BUTTON_SIDE;
955 } else if (button->button == 9) {
956 btn = INPUT_BUTTON_EXTRA;
957 } else {
958 return TRUE;
961 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
962 button->type == GDK_BUTTON_PRESS);
963 qemu_input_event_sync();
964 return TRUE;
967 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
968 void *opaque)
970 VirtualConsole *vc = opaque;
971 InputButton btn_vertical;
972 InputButton btn_horizontal;
973 bool has_vertical = false;
974 bool has_horizontal = false;
976 if (scroll->direction == GDK_SCROLL_UP) {
977 btn_vertical = INPUT_BUTTON_WHEEL_UP;
978 has_vertical = true;
979 } else if (scroll->direction == GDK_SCROLL_DOWN) {
980 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
981 has_vertical = true;
982 } else if (scroll->direction == GDK_SCROLL_LEFT) {
983 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
984 has_horizontal = true;
985 } else if (scroll->direction == GDK_SCROLL_RIGHT) {
986 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
987 has_horizontal = true;
988 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
989 gdouble delta_x, delta_y;
990 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
991 &delta_x, &delta_y)) {
992 return TRUE;
995 if (delta_y > 0) {
996 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
997 has_vertical = true;
998 } else if (delta_y < 0) {
999 btn_vertical = INPUT_BUTTON_WHEEL_UP;
1000 has_vertical = true;
1001 } else if (delta_x > 0) {
1002 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1003 has_horizontal = true;
1004 } else if (delta_x < 0) {
1005 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1006 has_horizontal = true;
1007 } else {
1008 return TRUE;
1010 } else {
1011 return TRUE;
1014 if (has_vertical) {
1015 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, true);
1016 qemu_input_event_sync();
1017 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, false);
1018 qemu_input_event_sync();
1021 if (has_horizontal) {
1022 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, true);
1023 qemu_input_event_sync();
1024 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, false);
1025 qemu_input_event_sync();
1028 return TRUE;
1032 static const guint16 *gd_get_keymap(size_t *maplen)
1034 GdkDisplay *dpy = gdk_display_get_default();
1036 #ifdef GDK_WINDOWING_X11
1037 if (GDK_IS_X11_DISPLAY(dpy)) {
1038 trace_gd_keymap_windowing("x11");
1039 return qemu_xkeymap_mapping_table(
1040 gdk_x11_display_get_xdisplay(dpy), maplen);
1042 #endif
1044 #ifdef GDK_WINDOWING_WAYLAND
1045 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1046 trace_gd_keymap_windowing("wayland");
1047 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1048 return qemu_input_map_xorgevdev_to_qcode;
1050 #endif
1052 #ifdef GDK_WINDOWING_WIN32
1053 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1054 trace_gd_keymap_windowing("win32");
1055 *maplen = qemu_input_map_atset1_to_qcode_len;
1056 return qemu_input_map_atset1_to_qcode;
1058 #endif
1060 #ifdef GDK_WINDOWING_QUARTZ
1061 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1062 trace_gd_keymap_windowing("quartz");
1063 *maplen = qemu_input_map_osx_to_qcode_len;
1064 return qemu_input_map_osx_to_qcode;
1066 #endif
1068 #ifdef GDK_WINDOWING_BROADWAY
1069 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1070 trace_gd_keymap_windowing("broadway");
1071 g_warning("experimental: using broadway, x11 virtual keysym\n"
1072 "mapping - with very limited support. See also\n"
1073 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1074 *maplen = qemu_input_map_x11_to_qcode_len;
1075 return qemu_input_map_x11_to_qcode;
1077 #endif
1079 g_warning("Unsupported GDK Windowing platform.\n"
1080 "Disabling extended keycode tables.\n"
1081 "Please report to qemu-devel@nongnu.org\n"
1082 "including the following information:\n"
1083 "\n"
1084 " - Operating system\n"
1085 " - GDK Windowing system build\n");
1086 return NULL;
1090 static int gd_map_keycode(int scancode)
1092 if (!keycode_map) {
1093 return 0;
1095 if (scancode > keycode_maplen) {
1096 return 0;
1099 return keycode_map[scancode];
1102 static int gd_get_keycode(GdkEventKey *key)
1104 #ifdef G_OS_WIN32
1105 int scancode = gdk_event_get_scancode((GdkEvent *)key);
1107 /* translate Windows native scancodes to atset1 keycodes */
1108 switch (scancode & (KF_EXTENDED | 0xff)) {
1109 case 0x145: /* NUMLOCK */
1110 return scancode & 0xff;
1113 return scancode & KF_EXTENDED ?
1114 0xe000 | (scancode & 0xff) : scancode & 0xff;
1116 #else
1117 return key->hardware_keycode;
1118 #endif
1121 static gboolean gd_text_key_down(GtkWidget *widget,
1122 GdkEventKey *key, void *opaque)
1124 VirtualConsole *vc = opaque;
1125 QemuConsole *con = vc->gfx.dcl.con;
1127 if (key->keyval == GDK_KEY_Delete) {
1128 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE, false);
1129 } else if (key->length) {
1130 kbd_put_string_console(con, key->string, key->length);
1131 } else {
1132 int qcode = gd_map_keycode(gd_get_keycode(key));
1133 kbd_put_qcode_console(con, qcode, false);
1135 return TRUE;
1138 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1140 VirtualConsole *vc = opaque;
1141 int keycode, qcode;
1143 #ifdef G_OS_WIN32
1144 /* on windows, we ought to ignore the reserved key event? */
1145 if (key->hardware_keycode == 0xff)
1146 return false;
1148 if (!vc->s->kbd_owner) {
1149 if (key->hardware_keycode == VK_LWIN ||
1150 key->hardware_keycode == VK_RWIN) {
1151 return FALSE;
1154 #endif
1156 if (key->keyval == GDK_KEY_Pause
1157 #ifdef G_OS_WIN32
1158 /* for some reason GDK does not fill keyval for VK_PAUSE
1159 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1161 || key->hardware_keycode == VK_PAUSE
1162 #endif
1164 qkbd_state_key_event(vc->gfx.kbd, Q_KEY_CODE_PAUSE,
1165 key->type == GDK_KEY_PRESS);
1166 return TRUE;
1169 keycode = gd_get_keycode(key);
1170 qcode = gd_map_keycode(keycode);
1172 trace_gd_key_event(vc->label, keycode, qcode,
1173 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1175 qkbd_state_key_event(vc->gfx.kbd, qcode,
1176 key->type == GDK_KEY_PRESS);
1178 return TRUE;
1181 static gboolean gd_grab_broken_event(GtkWidget *widget,
1182 GdkEventGrabBroken *event, void *opaque)
1184 #ifdef CONFIG_WIN32
1186 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1187 * key combination leaves all three keys in a stuck condition. We use
1188 * the grab-broken-event to release all keys.
1190 if (event->keyboard) {
1191 VirtualConsole *vc = opaque;
1192 GtkDisplayState *s = vc->s;
1194 gtk_release_modifiers(s);
1196 #endif
1197 return TRUE;
1200 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1202 if (event->type == GDK_MOTION_NOTIFY) {
1203 return gd_motion_event(widget, &event->motion, opaque);
1205 return FALSE;
1208 /** Window Menu Actions **/
1210 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1212 GtkDisplayState *s = opaque;
1214 if (s->external_pause_update) {
1215 return;
1217 if (runstate_is_running()) {
1218 qmp_stop(NULL);
1219 } else {
1220 qmp_cont(NULL);
1224 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1226 qmp_system_reset(NULL);
1229 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1231 qmp_system_powerdown(NULL);
1234 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1236 qmp_quit(NULL);
1239 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1241 GtkDisplayState *s = opaque;
1242 VirtualConsole *vc = gd_vc_find_by_menu(s);
1243 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1244 gint page;
1246 gtk_release_modifiers(s);
1247 if (vc) {
1248 page = gtk_notebook_page_num(nb, vc->tab_item);
1249 gtk_notebook_set_current_page(nb, page);
1250 gtk_widget_grab_focus(vc->focus);
1254 static void gd_accel_switch_vc(void *opaque)
1256 VirtualConsole *vc = opaque;
1258 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1261 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1263 GtkDisplayState *s = opaque;
1264 VirtualConsole *vc = gd_vc_find_current(s);
1266 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1267 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1268 } else {
1269 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1271 gd_update_windowsize(vc);
1274 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1275 void *opaque)
1277 VirtualConsole *vc = opaque;
1278 GtkDisplayState *s = vc->s;
1280 gtk_widget_set_sensitive(vc->menu_item, true);
1281 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1282 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1283 vc->tab_item, vc->label);
1284 gtk_widget_destroy(vc->window);
1285 vc->window = NULL;
1286 #if defined(CONFIG_OPENGL)
1287 if (vc->gfx.esurface) {
1288 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1289 vc->gfx.esurface = NULL;
1291 if (vc->gfx.ectx) {
1292 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1293 vc->gfx.ectx = NULL;
1295 #endif
1296 return TRUE;
1299 static gboolean gd_win_grab(void *opaque)
1301 VirtualConsole *vc = opaque;
1303 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1304 if (vc->s->ptr_owner) {
1305 gd_ungrab_pointer(vc->s);
1306 } else {
1307 gd_grab_pointer(vc, "user-request-detached-tab");
1309 return TRUE;
1312 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1314 GtkDisplayState *s = opaque;
1315 VirtualConsole *vc = gd_vc_find_current(s);
1317 if (vc->type == GD_VC_GFX &&
1318 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1319 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1320 FALSE);
1322 if (!vc->window) {
1323 gtk_widget_set_sensitive(vc->menu_item, false);
1324 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1325 #if defined(CONFIG_OPENGL)
1326 if (vc->gfx.esurface) {
1327 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1328 vc->gfx.esurface = NULL;
1330 if (vc->gfx.esurface) {
1331 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1332 vc->gfx.ectx = NULL;
1334 #endif
1335 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1337 g_signal_connect(vc->window, "delete-event",
1338 G_CALLBACK(gd_tab_window_close), vc);
1339 gtk_widget_show_all(vc->window);
1341 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1342 GtkAccelGroup *ag = gtk_accel_group_new();
1343 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1345 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1346 vc, NULL);
1347 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1350 gd_update_geometry_hints(vc);
1351 gd_update_caption(s);
1355 static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
1357 GtkDisplayState *s = opaque;
1358 VirtualConsole *vc = gd_vc_find_current(s);
1360 if (s->full_screen) {
1361 return;
1364 if (gtk_check_menu_item_get_active(
1365 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1366 gtk_widget_show(s->menu_bar);
1367 } else {
1368 gtk_widget_hide(s->menu_bar);
1370 gd_update_windowsize(vc);
1373 static void gd_accel_show_menubar(void *opaque)
1375 GtkDisplayState *s = opaque;
1376 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
1379 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1381 GtkDisplayState *s = opaque;
1382 VirtualConsole *vc = gd_vc_find_current(s);
1384 if (!s->full_screen) {
1385 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1386 gtk_widget_hide(s->menu_bar);
1387 if (vc->type == GD_VC_GFX) {
1388 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1390 gtk_window_fullscreen(GTK_WINDOW(s->window));
1391 s->full_screen = TRUE;
1392 } else {
1393 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1394 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1395 if (gtk_check_menu_item_get_active(
1396 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1397 gtk_widget_show(s->menu_bar);
1399 s->full_screen = FALSE;
1400 if (vc->type == GD_VC_GFX) {
1401 vc->gfx.scale_x = 1.0;
1402 vc->gfx.scale_y = 1.0;
1403 gd_update_windowsize(vc);
1407 gd_update_cursor(vc);
1410 static void gd_accel_full_screen(void *opaque)
1412 GtkDisplayState *s = opaque;
1413 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1416 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1418 GtkDisplayState *s = opaque;
1419 VirtualConsole *vc = gd_vc_find_current(s);
1421 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1422 FALSE);
1424 vc->gfx.scale_x += VC_SCALE_STEP;
1425 vc->gfx.scale_y += VC_SCALE_STEP;
1427 gd_update_windowsize(vc);
1430 static void gd_accel_zoom_in(void *opaque)
1432 GtkDisplayState *s = opaque;
1433 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1436 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1438 GtkDisplayState *s = opaque;
1439 VirtualConsole *vc = gd_vc_find_current(s);
1441 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1442 FALSE);
1444 vc->gfx.scale_x -= VC_SCALE_STEP;
1445 vc->gfx.scale_y -= VC_SCALE_STEP;
1447 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1448 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1450 gd_update_windowsize(vc);
1453 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1455 GtkDisplayState *s = opaque;
1456 VirtualConsole *vc = gd_vc_find_current(s);
1458 vc->gfx.scale_x = 1.0;
1459 vc->gfx.scale_y = 1.0;
1461 gd_update_windowsize(vc);
1464 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1466 GtkDisplayState *s = opaque;
1467 VirtualConsole *vc = gd_vc_find_current(s);
1469 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1470 s->free_scale = TRUE;
1471 } else {
1472 s->free_scale = FALSE;
1473 vc->gfx.scale_x = 1.0;
1474 vc->gfx.scale_y = 1.0;
1477 gd_update_windowsize(vc);
1478 gd_update_full_redraw(vc);
1481 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1483 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1484 GdkSeat *seat = gdk_display_get_default_seat(display);
1485 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1486 GdkSeatCapabilities caps = 0;
1487 GdkCursor *cursor = NULL;
1489 if (kbd) {
1490 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1492 if (ptr) {
1493 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1494 cursor = vc->s->null_cursor;
1497 if (caps) {
1498 gdk_seat_grab(seat, window, caps, false, cursor,
1499 NULL, NULL, NULL);
1500 } else {
1501 gdk_seat_ungrab(seat);
1505 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1507 if (vc->s->kbd_owner) {
1508 if (vc->s->kbd_owner == vc) {
1509 return;
1510 } else {
1511 gd_ungrab_keyboard(vc->s);
1515 win32_kbd_set_grab(true);
1516 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1517 vc->s->kbd_owner = vc;
1518 gd_update_caption(vc->s);
1519 trace_gd_grab(vc->label, "kbd", reason);
1522 static void gd_ungrab_keyboard(GtkDisplayState *s)
1524 VirtualConsole *vc = s->kbd_owner;
1526 if (vc == NULL) {
1527 return;
1529 s->kbd_owner = NULL;
1531 win32_kbd_set_grab(false);
1532 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1533 gd_update_caption(s);
1534 trace_gd_ungrab(vc->label, "kbd");
1537 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1539 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1541 if (vc->s->ptr_owner) {
1542 if (vc->s->ptr_owner == vc) {
1543 return;
1544 } else {
1545 gd_ungrab_pointer(vc->s);
1549 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1550 gdk_device_get_position(gd_get_pointer(display),
1551 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1552 vc->s->ptr_owner = vc;
1553 gd_update_caption(vc->s);
1554 trace_gd_grab(vc->label, "ptr", reason);
1557 static void gd_ungrab_pointer(GtkDisplayState *s)
1559 VirtualConsole *vc = s->ptr_owner;
1560 GdkDisplay *display;
1562 if (vc == NULL) {
1563 return;
1565 s->ptr_owner = NULL;
1567 display = gtk_widget_get_display(vc->gfx.drawing_area);
1568 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1569 gdk_device_warp(gd_get_pointer(display),
1570 gtk_widget_get_screen(vc->gfx.drawing_area),
1571 vc->s->grab_x_root, vc->s->grab_y_root);
1572 gd_update_caption(s);
1573 trace_gd_ungrab(vc->label, "ptr");
1576 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1578 GtkDisplayState *s = opaque;
1579 VirtualConsole *vc = gd_vc_find_current(s);
1581 if (gd_is_grab_active(s)) {
1582 gd_grab_keyboard(vc, "user-request-main-window");
1583 gd_grab_pointer(vc, "user-request-main-window");
1584 } else {
1585 gd_ungrab_keyboard(s);
1586 gd_ungrab_pointer(s);
1589 gd_update_cursor(vc);
1592 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1593 gpointer data)
1595 GtkDisplayState *s = data;
1596 VirtualConsole *vc;
1597 gboolean on_vga;
1599 if (!gtk_widget_get_realized(s->notebook)) {
1600 return;
1603 vc = gd_vc_find_by_page(s, arg2);
1604 if (!vc) {
1605 return;
1607 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1608 TRUE);
1609 on_vga = (vc->type == GD_VC_GFX &&
1610 qemu_console_is_graphic(vc->gfx.dcl.con));
1611 if (!on_vga) {
1612 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1613 FALSE);
1614 } else if (s->full_screen) {
1615 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1616 TRUE);
1618 gtk_widget_set_sensitive(s->grab_item, on_vga);
1619 #ifdef CONFIG_VTE
1620 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1621 #endif
1623 gd_update_windowsize(vc);
1624 gd_update_cursor(vc);
1627 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1628 gpointer opaque)
1630 VirtualConsole *vc = opaque;
1631 GtkDisplayState *s = vc->s;
1633 if (gd_grab_on_hover(s)) {
1634 gd_grab_keyboard(vc, "grab-on-hover");
1636 return TRUE;
1639 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1640 gpointer opaque)
1642 VirtualConsole *vc = opaque;
1643 GtkDisplayState *s = vc->s;
1645 if (gd_grab_on_hover(s)) {
1646 gd_ungrab_keyboard(s);
1648 return TRUE;
1651 static gboolean gd_focus_in_event(GtkWidget *widget,
1652 GdkEventFocus *event, gpointer opaque)
1654 VirtualConsole *vc = opaque;
1656 win32_kbd_set_window(gd_win32_get_hwnd(vc));
1657 return TRUE;
1660 static gboolean gd_focus_out_event(GtkWidget *widget,
1661 GdkEventFocus *event, gpointer opaque)
1663 VirtualConsole *vc = opaque;
1664 GtkDisplayState *s = vc->s;
1666 win32_kbd_set_window(NULL);
1667 gtk_release_modifiers(s);
1668 return TRUE;
1671 static gboolean gd_configure(GtkWidget *widget,
1672 GdkEventConfigure *cfg, gpointer opaque)
1674 VirtualConsole *vc = opaque;
1676 gd_set_ui_info(vc, cfg->width, cfg->height);
1677 return FALSE;
1680 /** Virtual Console Callbacks **/
1682 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1683 int idx, GSList *group, GtkWidget *view_menu)
1685 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1686 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1687 HOTKEY_MODIFIERS, 0,
1688 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1689 gtk_accel_label_set_accel(
1690 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1691 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1693 g_signal_connect(vc->menu_item, "activate",
1694 G_CALLBACK(gd_menu_switch_vc), s);
1695 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1697 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1700 #if defined(CONFIG_VTE)
1701 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1703 GtkDisplayState *s = opaque;
1704 VirtualConsole *vc = gd_vc_find_current(s);
1706 #if VTE_CHECK_VERSION(0, 50, 0)
1707 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1708 VTE_FORMAT_TEXT);
1709 #else
1710 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1711 #endif
1714 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1716 VirtualConsole *vc = opaque;
1718 if (gtk_adjustment_get_upper(adjustment) >
1719 gtk_adjustment_get_page_size(adjustment)) {
1720 gtk_widget_show(vc->vte.scrollbar);
1721 } else {
1722 gtk_widget_hide(vc->vte.scrollbar);
1726 static void gd_vc_send_chars(VirtualConsole *vc)
1728 uint32_t len, avail;
1730 len = qemu_chr_be_can_write(vc->vte.chr);
1731 avail = fifo8_num_used(&vc->vte.out_fifo);
1732 while (len > 0 && avail > 0) {
1733 const uint8_t *buf;
1734 uint32_t size;
1736 buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
1737 qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size);
1738 len = qemu_chr_be_can_write(vc->vte.chr);
1739 avail -= size;
1743 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1745 VCChardev *vcd = VC_CHARDEV(chr);
1746 VirtualConsole *vc = vcd->console;
1748 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1749 return len;
1752 static void gd_vc_chr_accept_input(Chardev *chr)
1754 VCChardev *vcd = VC_CHARDEV(chr);
1755 VirtualConsole *vc = vcd->console;
1757 gd_vc_send_chars(vc);
1760 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1762 VCChardev *vcd = VC_CHARDEV(chr);
1763 VirtualConsole *vc = vcd->console;
1765 if (vc) {
1766 vc->vte.echo = echo;
1767 } else {
1768 vcd->echo = echo;
1772 static int nb_vcs;
1773 static Chardev *vcs[MAX_VCS];
1774 static void gd_vc_open(Chardev *chr,
1775 ChardevBackend *backend,
1776 bool *be_opened,
1777 Error **errp)
1779 if (nb_vcs == MAX_VCS) {
1780 error_setg(errp, "Maximum number of consoles reached");
1781 return;
1784 vcs[nb_vcs++] = chr;
1786 /* console/chardev init sometimes completes elsewhere in a 2nd
1787 * stage, so defer OPENED events until they are fully initialized
1789 *be_opened = false;
1792 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1794 ChardevClass *cc = CHARDEV_CLASS(oc);
1796 cc->parse = qemu_chr_parse_vc;
1797 cc->open = gd_vc_open;
1798 cc->chr_write = gd_vc_chr_write;
1799 cc->chr_accept_input = gd_vc_chr_accept_input;
1800 cc->chr_set_echo = gd_vc_chr_set_echo;
1803 static const TypeInfo char_gd_vc_type_info = {
1804 .name = TYPE_CHARDEV_VC,
1805 .parent = TYPE_CHARDEV,
1806 .instance_size = sizeof(VCChardev),
1807 .class_init = char_gd_vc_class_init,
1810 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1811 gpointer user_data)
1813 VirtualConsole *vc = user_data;
1814 uint32_t free;
1816 if (vc->vte.echo) {
1817 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1818 int i;
1819 for (i = 0; i < size; i++) {
1820 uint8_t c = text[i];
1821 if (c >= 128 || isprint(c)) {
1822 /* 8-bit characters are considered printable. */
1823 vte_terminal_feed(term, &text[i], 1);
1824 } else if (c == '\r' || c == '\n') {
1825 vte_terminal_feed(term, "\r\n", 2);
1826 } else {
1827 char ctrl[2] = { '^', 0};
1828 ctrl[1] = text[i] ^ 64;
1829 vte_terminal_feed(term, ctrl, 2);
1834 free = fifo8_num_free(&vc->vte.out_fifo);
1835 fifo8_push_all(&vc->vte.out_fifo, (uint8_t *)text, MIN(free, size));
1836 gd_vc_send_chars(vc);
1838 return TRUE;
1841 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1842 Chardev *chr, int idx,
1843 GSList *group, GtkWidget *view_menu)
1845 char buffer[32];
1846 GtkWidget *box;
1847 GtkWidget *scrollbar;
1848 GtkAdjustment *vadjustment;
1849 VCChardev *vcd = VC_CHARDEV(chr);
1851 vc->s = s;
1852 vc->vte.echo = vcd->echo;
1853 vc->vte.chr = chr;
1854 fifo8_create(&vc->vte.out_fifo, 4096);
1855 vcd->console = vc;
1857 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1858 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1859 ? vc->vte.chr->label : buffer);
1860 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1862 vc->vte.terminal = vte_terminal_new();
1863 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1865 /* The documentation says that the default is UTF-8, but actually it is
1866 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1867 * VTE 0.54 (only UTF-8 is supported now). */
1868 #if !VTE_CHECK_VERSION(0, 54, 0)
1869 #if VTE_CHECK_VERSION(0, 38, 0)
1870 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1871 #else
1872 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1873 #endif
1874 #endif
1876 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1877 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1878 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1880 #if VTE_CHECK_VERSION(0, 28, 0)
1881 vadjustment = gtk_scrollable_get_vadjustment
1882 (GTK_SCROLLABLE(vc->vte.terminal));
1883 #else
1884 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1885 #endif
1887 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1888 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1890 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1891 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1893 vc->vte.box = box;
1894 vc->vte.scrollbar = scrollbar;
1896 g_signal_connect(vadjustment, "changed",
1897 G_CALLBACK(gd_vc_adjustment_changed), vc);
1899 vc->type = GD_VC_VTE;
1900 vc->tab_item = box;
1901 vc->focus = vc->vte.terminal;
1902 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1903 gtk_label_new(vc->label));
1905 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1907 return group;
1910 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1911 GtkWidget *view_menu)
1913 int i;
1915 for (i = 0; i < nb_vcs; i++) {
1916 VirtualConsole *vc = &s->vc[s->nb_vcs];
1917 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1918 s->nb_vcs++;
1921 #endif /* CONFIG_VTE */
1923 /** Window Creation **/
1925 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1927 g_signal_connect(vc->gfx.drawing_area, "draw",
1928 G_CALLBACK(gd_draw_event), vc);
1929 #if defined(CONFIG_OPENGL)
1930 if (gtk_use_gl_area) {
1931 /* wire up GtkGlArea events */
1932 g_signal_connect(vc->gfx.drawing_area, "render",
1933 G_CALLBACK(gd_render_event), vc);
1934 g_signal_connect(vc->gfx.drawing_area, "resize",
1935 G_CALLBACK(gd_resize_event), vc);
1937 #endif
1938 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1939 g_signal_connect(vc->gfx.drawing_area, "event",
1940 G_CALLBACK(gd_event), vc);
1941 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1942 G_CALLBACK(gd_button_event), vc);
1943 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1944 G_CALLBACK(gd_button_event), vc);
1945 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1946 G_CALLBACK(gd_scroll_event), vc);
1947 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1948 G_CALLBACK(gd_key_event), vc);
1949 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1950 G_CALLBACK(gd_key_event), vc);
1952 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1953 G_CALLBACK(gd_enter_event), vc);
1954 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1955 G_CALLBACK(gd_leave_event), vc);
1956 g_signal_connect(vc->gfx.drawing_area, "focus-in-event",
1957 G_CALLBACK(gd_focus_in_event), vc);
1958 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1959 G_CALLBACK(gd_focus_out_event), vc);
1960 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1961 G_CALLBACK(gd_configure), vc);
1962 g_signal_connect(vc->gfx.drawing_area, "grab-broken-event",
1963 G_CALLBACK(gd_grab_broken_event), vc);
1964 } else {
1965 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1966 G_CALLBACK(gd_text_key_down), vc);
1970 static void gd_connect_signals(GtkDisplayState *s)
1972 g_signal_connect(s->show_tabs_item, "activate",
1973 G_CALLBACK(gd_menu_show_tabs), s);
1974 g_signal_connect(s->untabify_item, "activate",
1975 G_CALLBACK(gd_menu_untabify), s);
1976 g_signal_connect(s->show_menubar_item, "activate",
1977 G_CALLBACK(gd_menu_show_menubar), s);
1979 g_signal_connect(s->window, "delete-event",
1980 G_CALLBACK(gd_window_close), s);
1982 g_signal_connect(s->pause_item, "activate",
1983 G_CALLBACK(gd_menu_pause), s);
1984 g_signal_connect(s->reset_item, "activate",
1985 G_CALLBACK(gd_menu_reset), s);
1986 g_signal_connect(s->powerdown_item, "activate",
1987 G_CALLBACK(gd_menu_powerdown), s);
1988 g_signal_connect(s->quit_item, "activate",
1989 G_CALLBACK(gd_menu_quit), s);
1990 #if defined(CONFIG_VTE)
1991 g_signal_connect(s->copy_item, "activate",
1992 G_CALLBACK(gd_menu_copy), s);
1993 #endif
1994 g_signal_connect(s->full_screen_item, "activate",
1995 G_CALLBACK(gd_menu_full_screen), s);
1996 g_signal_connect(s->zoom_in_item, "activate",
1997 G_CALLBACK(gd_menu_zoom_in), s);
1998 g_signal_connect(s->zoom_out_item, "activate",
1999 G_CALLBACK(gd_menu_zoom_out), s);
2000 g_signal_connect(s->zoom_fixed_item, "activate",
2001 G_CALLBACK(gd_menu_zoom_fixed), s);
2002 g_signal_connect(s->zoom_fit_item, "activate",
2003 G_CALLBACK(gd_menu_zoom_fit), s);
2004 g_signal_connect(s->grab_item, "activate",
2005 G_CALLBACK(gd_menu_grab_input), s);
2006 g_signal_connect(s->notebook, "switch-page",
2007 G_CALLBACK(gd_change_page), s);
2010 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2012 GtkWidget *machine_menu;
2013 GtkWidget *separator;
2015 machine_menu = gtk_menu_new();
2016 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2018 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2019 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2021 separator = gtk_separator_menu_item_new();
2022 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2024 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2025 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2027 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2028 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2030 separator = gtk_separator_menu_item_new();
2031 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2033 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2034 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2035 "<QEMU>/Machine/Quit");
2036 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2037 GDK_KEY_q, HOTKEY_MODIFIERS);
2038 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2040 return machine_menu;
2043 #if defined(CONFIG_OPENGL)
2044 static void gl_area_realize(GtkGLArea *area, VirtualConsole *vc)
2046 gtk_gl_area_make_current(area);
2047 qemu_egl_display = eglGetCurrentDisplay();
2048 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2049 if (!vc->gfx.has_dmabuf) {
2050 error_report("GtkGLArea console lacks DMABUF support.");
2053 #endif
2055 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2056 QemuConsole *con, int idx,
2057 GSList *group, GtkWidget *view_menu)
2059 bool zoom_to_fit = false;
2061 vc->label = qemu_console_get_label(con);
2062 vc->s = s;
2063 vc->gfx.scale_x = 1.0;
2064 vc->gfx.scale_y = 1.0;
2066 #if defined(CONFIG_OPENGL)
2067 if (display_opengl) {
2068 if (gtk_use_gl_area) {
2069 vc->gfx.drawing_area = gtk_gl_area_new();
2070 g_signal_connect(vc->gfx.drawing_area, "realize",
2071 G_CALLBACK(gl_area_realize), vc);
2072 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2073 vc->gfx.dgc.ops = &gl_area_ctx_ops;
2074 } else {
2075 #ifdef CONFIG_X11
2076 vc->gfx.drawing_area = gtk_drawing_area_new();
2078 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2079 * It is required for opengl rendering on X11 though. A
2080 * proper replacement (native opengl support) is only
2081 * available in 3.16+. Silence the warning if possible.
2083 #pragma GCC diagnostic push
2084 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2085 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2086 #pragma GCC diagnostic pop
2087 vc->gfx.dcl.ops = &dcl_egl_ops;
2088 vc->gfx.dgc.ops = &egl_ctx_ops;
2089 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2090 #else
2091 abort();
2092 #endif
2094 } else
2095 #endif
2097 vc->gfx.drawing_area = gtk_drawing_area_new();
2098 vc->gfx.dcl.ops = &dcl_ops;
2102 gtk_widget_add_events(vc->gfx.drawing_area,
2103 GDK_POINTER_MOTION_MASK |
2104 GDK_BUTTON_PRESS_MASK |
2105 GDK_BUTTON_RELEASE_MASK |
2106 GDK_BUTTON_MOTION_MASK |
2107 GDK_ENTER_NOTIFY_MASK |
2108 GDK_LEAVE_NOTIFY_MASK |
2109 GDK_SCROLL_MASK |
2110 GDK_SMOOTH_SCROLL_MASK |
2111 GDK_KEY_PRESS_MASK);
2112 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2114 vc->type = GD_VC_GFX;
2115 vc->tab_item = vc->gfx.drawing_area;
2116 vc->focus = vc->gfx.drawing_area;
2117 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2118 vc->tab_item, gtk_label_new(vc->label));
2120 vc->gfx.kbd = qkbd_state_init(con);
2121 vc->gfx.dcl.con = con;
2123 if (display_opengl) {
2124 qemu_console_set_display_gl_ctx(con, &vc->gfx.dgc);
2126 register_displaychangelistener(&vc->gfx.dcl);
2128 gd_connect_vc_gfx_signals(vc);
2129 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2131 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2132 zoom_to_fit = true;
2134 if (s->opts->u.gtk.has_zoom_to_fit) {
2135 zoom_to_fit = s->opts->u.gtk.zoom_to_fit;
2137 if (zoom_to_fit) {
2138 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2139 s->free_scale = true;
2142 return group;
2145 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2147 GSList *group = NULL;
2148 GtkWidget *view_menu;
2149 GtkWidget *separator;
2150 QemuConsole *con;
2151 int vc;
2153 view_menu = gtk_menu_new();
2154 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2156 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2158 #if defined(CONFIG_VTE)
2159 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2160 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2161 #endif
2163 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2164 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2165 gtk_accel_label_set_accel(
2166 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2167 GDK_KEY_f, HOTKEY_MODIFIERS);
2168 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2170 separator = gtk_separator_menu_item_new();
2171 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2173 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2174 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2175 "<QEMU>/View/Zoom In");
2176 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2177 HOTKEY_MODIFIERS);
2178 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2179 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2180 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2182 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2183 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2184 "<QEMU>/View/Zoom Out");
2185 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2186 HOTKEY_MODIFIERS);
2187 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2189 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2190 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2191 "<QEMU>/View/Zoom Fixed");
2192 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2193 HOTKEY_MODIFIERS);
2194 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2196 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2197 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2199 separator = gtk_separator_menu_item_new();
2200 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2202 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2203 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2205 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2206 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2207 "<QEMU>/View/Grab Input");
2208 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2209 HOTKEY_MODIFIERS);
2210 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2212 separator = gtk_separator_menu_item_new();
2213 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2215 /* gfx */
2216 for (vc = 0;; vc++) {
2217 con = qemu_console_lookup_by_index(vc);
2218 if (!con) {
2219 break;
2221 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2222 vc, group, view_menu);
2223 s->nb_vcs++;
2226 #if defined(CONFIG_VTE)
2227 /* vte */
2228 gd_vcs_init(s, group, view_menu);
2229 #endif
2231 separator = gtk_separator_menu_item_new();
2232 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2234 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2235 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2237 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2238 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2240 s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
2241 _("Show Menubar"));
2242 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
2243 TRUE);
2244 gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
2245 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
2246 gtk_accel_label_set_accel(
2247 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
2248 GDK_KEY_m, HOTKEY_MODIFIERS);
2249 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
2251 return view_menu;
2254 static void gd_create_menus(GtkDisplayState *s)
2256 GtkSettings *settings;
2258 s->accel_group = gtk_accel_group_new();
2259 s->machine_menu = gd_create_menu_machine(s);
2260 s->view_menu = gd_create_menu_view(s);
2262 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2263 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2264 s->machine_menu);
2265 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2267 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2268 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2269 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2271 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2272 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2274 /* Disable the default "F10" menu shortcut. */
2275 settings = gtk_widget_get_settings(s->window);
2276 g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
2280 static gboolean gtkinit;
2282 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2284 VirtualConsole *vc;
2286 GtkDisplayState *s = g_malloc0(sizeof(*s));
2287 GdkDisplay *window_display;
2288 GtkIconTheme *theme;
2289 char *dir;
2291 if (!gtkinit) {
2292 fprintf(stderr, "gtk initialization failed\n");
2293 exit(1);
2295 assert(opts->type == DISPLAY_TYPE_GTK);
2296 s->opts = opts;
2298 theme = gtk_icon_theme_get_default();
2299 dir = get_relocated_path(CONFIG_QEMU_ICONDIR);
2300 gtk_icon_theme_prepend_search_path(theme, dir);
2301 g_free(dir);
2302 g_set_prgname("qemu");
2304 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2305 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2306 s->notebook = gtk_notebook_new();
2307 s->menu_bar = gtk_menu_bar_new();
2309 s->free_scale = FALSE;
2311 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2312 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2313 * printable, but without changing any of the character classes to make
2314 * sure that we don't accidentally break implicit assumptions. */
2315 setlocale(LC_MESSAGES, "");
2316 setlocale(LC_CTYPE, "C.UTF-8");
2317 dir = get_relocated_path(CONFIG_QEMU_LOCALEDIR);
2318 bindtextdomain("qemu", dir);
2319 g_free(dir);
2320 bind_textdomain_codeset("qemu", "UTF-8");
2321 textdomain("qemu");
2323 window_display = gtk_widget_get_display(s->window);
2324 if (s->opts->has_show_cursor && s->opts->show_cursor) {
2325 s->null_cursor = NULL; /* default pointer */
2326 } else {
2327 s->null_cursor = gdk_cursor_new_for_display(window_display,
2328 GDK_BLANK_CURSOR);
2331 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2332 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2333 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2335 gtk_window_set_icon_name(GTK_WINDOW(s->window), "qemu");
2337 gd_create_menus(s);
2339 gd_connect_signals(s);
2341 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2342 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2344 gd_update_caption(s);
2346 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2347 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2349 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2351 gtk_widget_show_all(s->window);
2353 vc = gd_vc_find_current(s);
2354 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2355 #ifdef CONFIG_VTE
2356 gtk_widget_set_sensitive(s->copy_item,
2357 vc && vc->type == GD_VC_VTE);
2358 #endif
2360 if (opts->has_full_screen &&
2361 opts->full_screen) {
2362 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2364 if (opts->u.gtk.has_grab_on_hover &&
2365 opts->u.gtk.grab_on_hover) {
2366 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2368 gd_clipboard_init(s);
2371 static void early_gtk_display_init(DisplayOptions *opts)
2373 /* The QEMU code relies on the assumption that it's always run in
2374 * the C locale. Therefore it is not prepared to deal with
2375 * operations that produce different results depending on the
2376 * locale, such as printf's formatting of decimal numbers, and
2377 * possibly others.
2379 * Since GTK+ calls setlocale() by default -importing the locale
2380 * settings from the environment- we must prevent it from doing so
2381 * using gtk_disable_setlocale().
2383 * QEMU's GTK+ UI, however, _does_ have translations for some of
2384 * the menu items. As a trade-off between a functionally correct
2385 * QEMU and a fully internationalized UI we support importing
2386 * LC_MESSAGES from the environment (see the setlocale() call
2387 * earlier in this file). This allows us to display translated
2388 * messages leaving everything else untouched.
2390 gtk_disable_setlocale();
2391 gtkinit = gtk_init_check(NULL, NULL);
2392 if (!gtkinit) {
2393 /* don't exit yet, that'll break -help */
2394 return;
2397 assert(opts->type == DISPLAY_TYPE_GTK);
2398 if (opts->has_gl && opts->gl != DISPLAYGL_MODE_OFF) {
2399 #if defined(CONFIG_OPENGL)
2400 #if defined(GDK_WINDOWING_WAYLAND)
2401 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2402 gtk_use_gl_area = true;
2403 gtk_gl_area_init();
2404 } else
2405 #endif
2407 #ifdef CONFIG_X11
2408 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
2409 gtk_egl_init(mode);
2410 #endif
2412 #endif
2415 keycode_map = gd_get_keymap(&keycode_maplen);
2417 #if defined(CONFIG_VTE)
2418 type_register(&char_gd_vc_type_info);
2419 #endif
2422 static QemuDisplay qemu_display_gtk = {
2423 .type = DISPLAY_TYPE_GTK,
2424 .early_init = early_gtk_display_init,
2425 .init = gtk_display_init,
2428 static void register_gtk(void)
2430 qemu_display_register(&qemu_display_gtk);
2433 type_init(register_gtk);
2435 #ifdef CONFIG_OPENGL
2436 module_dep("ui-opengl");
2437 #endif