nbd/server: Allow MULTI_CONN for shared writable exports
[qemu.git] / ui / gtk.c
blobc57c36749e0eb2f3ddb567412e553f5ee3477e36
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 bool
618 gd_gl_area_is_compatible_dcl(DisplayGLCtx *dgc,
619 DisplayChangeListener *dcl)
621 return dcl->ops == &dcl_gl_area_ops;
624 static const DisplayGLCtxOps gl_area_ctx_ops = {
625 .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
626 .dpy_gl_ctx_create = gd_gl_area_create_context,
627 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
628 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
631 #ifdef CONFIG_X11
632 static const DisplayChangeListenerOps dcl_egl_ops = {
633 .dpy_name = "gtk-egl",
634 .dpy_gfx_update = gd_egl_update,
635 .dpy_gfx_switch = gd_egl_switch,
636 .dpy_gfx_check_format = console_gl_check_format,
637 .dpy_refresh = gd_egl_refresh,
638 .dpy_mouse_set = gd_mouse_set,
639 .dpy_cursor_define = gd_cursor_define,
641 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
642 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
643 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
644 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
645 .dpy_gl_cursor_position = gd_egl_cursor_position,
646 .dpy_gl_update = gd_egl_flush,
647 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
648 .dpy_has_dmabuf = gd_has_dmabuf,
651 static bool
652 gd_egl_is_compatible_dcl(DisplayGLCtx *dgc,
653 DisplayChangeListener *dcl)
655 return dcl->ops == &dcl_egl_ops;
658 static const DisplayGLCtxOps egl_ctx_ops = {
659 .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
660 .dpy_gl_ctx_create = gd_egl_create_context,
661 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
662 .dpy_gl_ctx_make_current = gd_egl_make_current,
664 #endif
666 #endif /* CONFIG_OPENGL */
668 /** QEMU Events **/
670 static void gd_change_runstate(void *opaque, bool running, RunState state)
672 GtkDisplayState *s = opaque;
674 gd_update_caption(s);
677 static void gd_mouse_mode_change(Notifier *notify, void *data)
679 GtkDisplayState *s;
680 int i;
682 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
683 /* release the grab at switching to absolute mode */
684 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
685 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
686 FALSE);
688 for (i = 0; i < s->nb_vcs; i++) {
689 VirtualConsole *vc = &s->vc[i];
690 gd_update_cursor(vc);
694 /** GTK Events **/
696 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
697 void *opaque)
699 GtkDisplayState *s = opaque;
700 bool allow_close = true;
702 if (s->opts->has_window_close && !s->opts->window_close) {
703 allow_close = false;
706 if (allow_close) {
707 qmp_quit(NULL);
710 return TRUE;
713 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
715 QemuUIInfo info;
717 memset(&info, 0, sizeof(info));
718 info.width = width;
719 info.height = height;
720 dpy_set_ui_info(vc->gfx.dcl.con, &info, true);
723 #if defined(CONFIG_OPENGL)
725 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
726 void *opaque)
728 VirtualConsole *vc = opaque;
730 if (vc->gfx.gls) {
731 gd_gl_area_draw(vc);
733 return TRUE;
736 static void gd_resize_event(GtkGLArea *area,
737 gint width, gint height, gpointer *opaque)
739 VirtualConsole *vc = (void *)opaque;
741 gd_set_ui_info(vc, width, height);
744 #endif
747 * If available, return the update interval of the monitor in ms,
748 * else return 0 (the default update interval).
750 int gd_monitor_update_interval(GtkWidget *widget)
752 #ifdef GDK_VERSION_3_22
753 GdkWindow *win = gtk_widget_get_window(widget);
755 if (win) {
756 GdkDisplay *dpy = gtk_widget_get_display(widget);
757 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
758 int refresh_rate = gdk_monitor_get_refresh_rate(monitor); /* [mHz] */
760 if (refresh_rate) {
761 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
762 return MIN(1000 * 1000 / refresh_rate,
763 GUI_REFRESH_INTERVAL_DEFAULT);
766 #endif
767 return 0;
770 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
772 VirtualConsole *vc = opaque;
773 GtkDisplayState *s = vc->s;
774 int mx, my;
775 int ww, wh;
776 int fbw, fbh;
778 #if defined(CONFIG_OPENGL)
779 if (vc->gfx.gls) {
780 if (gtk_use_gl_area) {
781 /* invoke render callback please */
782 return FALSE;
783 } else {
784 #ifdef CONFIG_X11
785 gd_egl_draw(vc);
786 return TRUE;
787 #else
788 abort();
789 #endif
792 #endif
794 if (!gtk_widget_get_realized(widget)) {
795 return FALSE;
797 if (!vc->gfx.ds) {
798 return FALSE;
800 if (!vc->gfx.surface) {
801 return FALSE;
804 vc->gfx.dcl.update_interval =
805 gd_monitor_update_interval(vc->window ? vc->window : s->window);
807 fbw = surface_width(vc->gfx.ds);
808 fbh = surface_height(vc->gfx.ds);
810 ww = gdk_window_get_width(gtk_widget_get_window(widget));
811 wh = gdk_window_get_height(gtk_widget_get_window(widget));
813 if (s->full_screen) {
814 vc->gfx.scale_x = (double)ww / fbw;
815 vc->gfx.scale_y = (double)wh / fbh;
816 } else if (s->free_scale) {
817 double sx, sy;
819 sx = (double)ww / fbw;
820 sy = (double)wh / fbh;
822 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
825 fbw *= vc->gfx.scale_x;
826 fbh *= vc->gfx.scale_y;
828 mx = my = 0;
829 if (ww > fbw) {
830 mx = (ww - fbw) / 2;
832 if (wh > fbh) {
833 my = (wh - fbh) / 2;
836 cairo_rectangle(cr, 0, 0, ww, wh);
838 /* Optionally cut out the inner area where the pixmap
839 will be drawn. This avoids 'flashing' since we're
840 not double-buffering. Note we're using the undocumented
841 behaviour of drawing the rectangle from right to left
842 to cut out the whole */
843 cairo_rectangle(cr, mx + fbw, my,
844 -1 * fbw, fbh);
845 cairo_fill(cr);
847 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
848 cairo_set_source_surface(cr, vc->gfx.surface,
849 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
850 cairo_paint(cr);
852 return TRUE;
855 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
856 void *opaque)
858 VirtualConsole *vc = opaque;
859 GtkDisplayState *s = vc->s;
860 GdkWindow *window;
861 int x, y;
862 int mx, my;
863 int fbh, fbw;
864 int ww, wh, ws;
866 if (!vc->gfx.ds) {
867 return TRUE;
870 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
871 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
873 window = gtk_widget_get_window(vc->gfx.drawing_area);
874 ww = gdk_window_get_width(window);
875 wh = gdk_window_get_height(window);
876 ws = gdk_window_get_scale_factor(window);
878 mx = my = 0;
879 if (ww > fbw) {
880 mx = (ww - fbw) / 2;
882 if (wh > fbh) {
883 my = (wh - fbh) / 2;
886 x = (motion->x - mx) / vc->gfx.scale_x * ws;
887 y = (motion->y - my) / vc->gfx.scale_y * ws;
889 if (qemu_input_is_absolute()) {
890 if (x < 0 || y < 0 ||
891 x >= surface_width(vc->gfx.ds) ||
892 y >= surface_height(vc->gfx.ds)) {
893 return TRUE;
895 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
896 0, surface_width(vc->gfx.ds));
897 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
898 0, surface_height(vc->gfx.ds));
899 qemu_input_event_sync();
900 } else if (s->last_set && s->ptr_owner == vc) {
901 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
902 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
903 qemu_input_event_sync();
905 s->last_x = x;
906 s->last_y = y;
907 s->last_set = TRUE;
909 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
910 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
911 GdkDisplay *dpy = gtk_widget_get_display(widget);
912 GdkWindow *win = gtk_widget_get_window(widget);
913 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
914 GdkRectangle geometry;
916 int x = (int)motion->x_root;
917 int y = (int)motion->y_root;
919 gdk_monitor_get_geometry(monitor, &geometry);
921 /* In relative mode check to see if client pointer hit
922 * one of the monitor edges, and if so move it back to the
923 * center of the monitor. This is important because the pointer
924 * in the server doesn't correspond 1-for-1, and so
925 * may still be only half way across the screen. Without
926 * this warp, the server pointer would thus appear to hit
927 * an invisible wall */
928 if (x <= geometry.x || x - geometry.x >= geometry.width - 1 ||
929 y <= geometry.y || y - geometry.y >= geometry.height - 1) {
930 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
931 x = geometry.x + geometry.width / 2;
932 y = geometry.y + geometry.height / 2;
934 gdk_device_warp(dev, screen, x, y);
935 s->last_set = FALSE;
936 return FALSE;
939 return TRUE;
942 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
943 void *opaque)
945 VirtualConsole *vc = opaque;
946 GtkDisplayState *s = vc->s;
947 InputButton btn;
949 /* implicitly grab the input at the first click in the relative mode */
950 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
951 !qemu_input_is_absolute() && s->ptr_owner != vc) {
952 if (!vc->window) {
953 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
954 TRUE);
955 } else {
956 gd_grab_pointer(vc, "relative-mode-click");
958 return TRUE;
961 if (button->button == 1) {
962 btn = INPUT_BUTTON_LEFT;
963 } else if (button->button == 2) {
964 btn = INPUT_BUTTON_MIDDLE;
965 } else if (button->button == 3) {
966 btn = INPUT_BUTTON_RIGHT;
967 } else if (button->button == 8) {
968 btn = INPUT_BUTTON_SIDE;
969 } else if (button->button == 9) {
970 btn = INPUT_BUTTON_EXTRA;
971 } else {
972 return TRUE;
975 if (button->type == GDK_2BUTTON_PRESS || button->type == GDK_3BUTTON_PRESS) {
976 return TRUE;
979 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
980 button->type == GDK_BUTTON_PRESS);
981 qemu_input_event_sync();
982 return TRUE;
985 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
986 void *opaque)
988 VirtualConsole *vc = opaque;
989 InputButton btn_vertical;
990 InputButton btn_horizontal;
991 bool has_vertical = false;
992 bool has_horizontal = false;
994 if (scroll->direction == GDK_SCROLL_UP) {
995 btn_vertical = INPUT_BUTTON_WHEEL_UP;
996 has_vertical = true;
997 } else if (scroll->direction == GDK_SCROLL_DOWN) {
998 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
999 has_vertical = true;
1000 } else if (scroll->direction == GDK_SCROLL_LEFT) {
1001 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1002 has_horizontal = true;
1003 } else if (scroll->direction == GDK_SCROLL_RIGHT) {
1004 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1005 has_horizontal = true;
1006 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1007 gdouble delta_x, delta_y;
1008 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1009 &delta_x, &delta_y)) {
1010 return TRUE;
1013 if (delta_y > 0) {
1014 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
1015 has_vertical = true;
1016 } else if (delta_y < 0) {
1017 btn_vertical = INPUT_BUTTON_WHEEL_UP;
1018 has_vertical = true;
1019 } else if (delta_x > 0) {
1020 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1021 has_horizontal = true;
1022 } else if (delta_x < 0) {
1023 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1024 has_horizontal = true;
1025 } else {
1026 return TRUE;
1028 } else {
1029 return TRUE;
1032 if (has_vertical) {
1033 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, true);
1034 qemu_input_event_sync();
1035 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, false);
1036 qemu_input_event_sync();
1039 if (has_horizontal) {
1040 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, true);
1041 qemu_input_event_sync();
1042 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, false);
1043 qemu_input_event_sync();
1046 return TRUE;
1050 static const guint16 *gd_get_keymap(size_t *maplen)
1052 GdkDisplay *dpy = gdk_display_get_default();
1054 #ifdef GDK_WINDOWING_X11
1055 if (GDK_IS_X11_DISPLAY(dpy)) {
1056 trace_gd_keymap_windowing("x11");
1057 return qemu_xkeymap_mapping_table(
1058 gdk_x11_display_get_xdisplay(dpy), maplen);
1060 #endif
1062 #ifdef GDK_WINDOWING_WAYLAND
1063 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1064 trace_gd_keymap_windowing("wayland");
1065 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1066 return qemu_input_map_xorgevdev_to_qcode;
1068 #endif
1070 #ifdef GDK_WINDOWING_WIN32
1071 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1072 trace_gd_keymap_windowing("win32");
1073 *maplen = qemu_input_map_atset1_to_qcode_len;
1074 return qemu_input_map_atset1_to_qcode;
1076 #endif
1078 #ifdef GDK_WINDOWING_QUARTZ
1079 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1080 trace_gd_keymap_windowing("quartz");
1081 *maplen = qemu_input_map_osx_to_qcode_len;
1082 return qemu_input_map_osx_to_qcode;
1084 #endif
1086 #ifdef GDK_WINDOWING_BROADWAY
1087 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1088 trace_gd_keymap_windowing("broadway");
1089 g_warning("experimental: using broadway, x11 virtual keysym\n"
1090 "mapping - with very limited support. See also\n"
1091 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1092 *maplen = qemu_input_map_x11_to_qcode_len;
1093 return qemu_input_map_x11_to_qcode;
1095 #endif
1097 g_warning("Unsupported GDK Windowing platform.\n"
1098 "Disabling extended keycode tables.\n"
1099 "Please report to qemu-devel@nongnu.org\n"
1100 "including the following information:\n"
1101 "\n"
1102 " - Operating system\n"
1103 " - GDK Windowing system build\n");
1104 return NULL;
1108 static int gd_map_keycode(int scancode)
1110 if (!keycode_map) {
1111 return 0;
1113 if (scancode > keycode_maplen) {
1114 return 0;
1117 return keycode_map[scancode];
1120 static int gd_get_keycode(GdkEventKey *key)
1122 #ifdef G_OS_WIN32
1123 int scancode = gdk_event_get_scancode((GdkEvent *)key);
1125 /* translate Windows native scancodes to atset1 keycodes */
1126 switch (scancode & (KF_EXTENDED | 0xff)) {
1127 case 0x145: /* NUMLOCK */
1128 return scancode & 0xff;
1131 return scancode & KF_EXTENDED ?
1132 0xe000 | (scancode & 0xff) : scancode & 0xff;
1134 #else
1135 return key->hardware_keycode;
1136 #endif
1139 static gboolean gd_text_key_down(GtkWidget *widget,
1140 GdkEventKey *key, void *opaque)
1142 VirtualConsole *vc = opaque;
1143 QemuConsole *con = vc->gfx.dcl.con;
1145 if (key->keyval == GDK_KEY_Delete) {
1146 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE, false);
1147 } else if (key->length) {
1148 kbd_put_string_console(con, key->string, key->length);
1149 } else {
1150 int qcode = gd_map_keycode(gd_get_keycode(key));
1151 kbd_put_qcode_console(con, qcode, false);
1153 return TRUE;
1156 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1158 VirtualConsole *vc = opaque;
1159 int keycode, qcode;
1161 #ifdef G_OS_WIN32
1162 /* on windows, we ought to ignore the reserved key event? */
1163 if (key->hardware_keycode == 0xff)
1164 return false;
1166 if (!vc->s->kbd_owner) {
1167 if (key->hardware_keycode == VK_LWIN ||
1168 key->hardware_keycode == VK_RWIN) {
1169 return FALSE;
1172 #endif
1174 if (key->keyval == GDK_KEY_Pause
1175 #ifdef G_OS_WIN32
1176 /* for some reason GDK does not fill keyval for VK_PAUSE
1177 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1179 || key->hardware_keycode == VK_PAUSE
1180 #endif
1182 qkbd_state_key_event(vc->gfx.kbd, Q_KEY_CODE_PAUSE,
1183 key->type == GDK_KEY_PRESS);
1184 return TRUE;
1187 keycode = gd_get_keycode(key);
1188 qcode = gd_map_keycode(keycode);
1190 trace_gd_key_event(vc->label, keycode, qcode,
1191 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1193 qkbd_state_key_event(vc->gfx.kbd, qcode,
1194 key->type == GDK_KEY_PRESS);
1196 return TRUE;
1199 static gboolean gd_grab_broken_event(GtkWidget *widget,
1200 GdkEventGrabBroken *event, void *opaque)
1202 #ifdef CONFIG_WIN32
1204 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1205 * key combination leaves all three keys in a stuck condition. We use
1206 * the grab-broken-event to release all keys.
1208 if (event->keyboard) {
1209 VirtualConsole *vc = opaque;
1210 GtkDisplayState *s = vc->s;
1212 gtk_release_modifiers(s);
1214 #endif
1215 return TRUE;
1218 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1220 if (event->type == GDK_MOTION_NOTIFY) {
1221 return gd_motion_event(widget, &event->motion, opaque);
1223 return FALSE;
1226 /** Window Menu Actions **/
1228 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1230 GtkDisplayState *s = opaque;
1232 if (s->external_pause_update) {
1233 return;
1235 if (runstate_is_running()) {
1236 qmp_stop(NULL);
1237 } else {
1238 qmp_cont(NULL);
1242 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1244 qmp_system_reset(NULL);
1247 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1249 qmp_system_powerdown(NULL);
1252 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1254 qmp_quit(NULL);
1257 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1259 GtkDisplayState *s = opaque;
1260 VirtualConsole *vc = gd_vc_find_by_menu(s);
1261 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1262 gint page;
1264 gtk_release_modifiers(s);
1265 if (vc) {
1266 page = gtk_notebook_page_num(nb, vc->tab_item);
1267 gtk_notebook_set_current_page(nb, page);
1268 gtk_widget_grab_focus(vc->focus);
1272 static void gd_accel_switch_vc(void *opaque)
1274 VirtualConsole *vc = opaque;
1276 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1279 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1281 GtkDisplayState *s = opaque;
1282 VirtualConsole *vc = gd_vc_find_current(s);
1284 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1285 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1286 } else {
1287 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1289 gd_update_windowsize(vc);
1292 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1293 void *opaque)
1295 VirtualConsole *vc = opaque;
1296 GtkDisplayState *s = vc->s;
1298 gtk_widget_set_sensitive(vc->menu_item, true);
1299 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1300 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1301 vc->tab_item, vc->label);
1302 gtk_widget_destroy(vc->window);
1303 vc->window = NULL;
1304 #if defined(CONFIG_OPENGL)
1305 if (vc->gfx.esurface) {
1306 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1307 vc->gfx.esurface = NULL;
1309 if (vc->gfx.ectx) {
1310 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1311 vc->gfx.ectx = NULL;
1313 #endif
1314 return TRUE;
1317 static gboolean gd_win_grab(void *opaque)
1319 VirtualConsole *vc = opaque;
1321 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1322 if (vc->s->ptr_owner) {
1323 gd_ungrab_pointer(vc->s);
1324 } else {
1325 gd_grab_pointer(vc, "user-request-detached-tab");
1327 return TRUE;
1330 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1332 GtkDisplayState *s = opaque;
1333 VirtualConsole *vc = gd_vc_find_current(s);
1335 if (vc->type == GD_VC_GFX &&
1336 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1337 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1338 FALSE);
1340 if (!vc->window) {
1341 gtk_widget_set_sensitive(vc->menu_item, false);
1342 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1343 #if defined(CONFIG_OPENGL)
1344 if (vc->gfx.esurface) {
1345 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1346 vc->gfx.esurface = NULL;
1348 if (vc->gfx.esurface) {
1349 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1350 vc->gfx.ectx = NULL;
1352 #endif
1353 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1355 g_signal_connect(vc->window, "delete-event",
1356 G_CALLBACK(gd_tab_window_close), vc);
1357 gtk_widget_show_all(vc->window);
1359 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1360 GtkAccelGroup *ag = gtk_accel_group_new();
1361 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1363 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1364 vc, NULL);
1365 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1368 gd_update_geometry_hints(vc);
1369 gd_update_caption(s);
1373 static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
1375 GtkDisplayState *s = opaque;
1376 VirtualConsole *vc = gd_vc_find_current(s);
1378 if (s->full_screen) {
1379 return;
1382 if (gtk_check_menu_item_get_active(
1383 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1384 gtk_widget_show(s->menu_bar);
1385 } else {
1386 gtk_widget_hide(s->menu_bar);
1388 gd_update_windowsize(vc);
1391 static void gd_accel_show_menubar(void *opaque)
1393 GtkDisplayState *s = opaque;
1394 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
1397 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1399 GtkDisplayState *s = opaque;
1400 VirtualConsole *vc = gd_vc_find_current(s);
1402 if (!s->full_screen) {
1403 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1404 gtk_widget_hide(s->menu_bar);
1405 if (vc->type == GD_VC_GFX) {
1406 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1408 gtk_window_fullscreen(GTK_WINDOW(s->window));
1409 s->full_screen = TRUE;
1410 } else {
1411 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1412 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1413 if (gtk_check_menu_item_get_active(
1414 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1415 gtk_widget_show(s->menu_bar);
1417 s->full_screen = FALSE;
1418 if (vc->type == GD_VC_GFX) {
1419 vc->gfx.scale_x = 1.0;
1420 vc->gfx.scale_y = 1.0;
1421 gd_update_windowsize(vc);
1425 gd_update_cursor(vc);
1428 static void gd_accel_full_screen(void *opaque)
1430 GtkDisplayState *s = opaque;
1431 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1434 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1436 GtkDisplayState *s = opaque;
1437 VirtualConsole *vc = gd_vc_find_current(s);
1439 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1440 FALSE);
1442 vc->gfx.scale_x += VC_SCALE_STEP;
1443 vc->gfx.scale_y += VC_SCALE_STEP;
1445 gd_update_windowsize(vc);
1448 static void gd_accel_zoom_in(void *opaque)
1450 GtkDisplayState *s = opaque;
1451 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1454 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1456 GtkDisplayState *s = opaque;
1457 VirtualConsole *vc = gd_vc_find_current(s);
1459 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1460 FALSE);
1462 vc->gfx.scale_x -= VC_SCALE_STEP;
1463 vc->gfx.scale_y -= VC_SCALE_STEP;
1465 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1466 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1468 gd_update_windowsize(vc);
1471 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1473 GtkDisplayState *s = opaque;
1474 VirtualConsole *vc = gd_vc_find_current(s);
1476 vc->gfx.scale_x = 1.0;
1477 vc->gfx.scale_y = 1.0;
1479 gd_update_windowsize(vc);
1482 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1484 GtkDisplayState *s = opaque;
1485 VirtualConsole *vc = gd_vc_find_current(s);
1487 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1488 s->free_scale = TRUE;
1489 } else {
1490 s->free_scale = FALSE;
1491 vc->gfx.scale_x = 1.0;
1492 vc->gfx.scale_y = 1.0;
1495 gd_update_windowsize(vc);
1496 gd_update_full_redraw(vc);
1499 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1501 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1502 GdkSeat *seat = gdk_display_get_default_seat(display);
1503 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1504 GdkSeatCapabilities caps = 0;
1505 GdkCursor *cursor = NULL;
1507 if (kbd) {
1508 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1510 if (ptr) {
1511 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1512 cursor = vc->s->null_cursor;
1515 if (caps) {
1516 gdk_seat_grab(seat, window, caps, false, cursor,
1517 NULL, NULL, NULL);
1518 } else {
1519 gdk_seat_ungrab(seat);
1523 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1525 if (vc->s->kbd_owner) {
1526 if (vc->s->kbd_owner == vc) {
1527 return;
1528 } else {
1529 gd_ungrab_keyboard(vc->s);
1533 win32_kbd_set_grab(true);
1534 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1535 vc->s->kbd_owner = vc;
1536 gd_update_caption(vc->s);
1537 trace_gd_grab(vc->label, "kbd", reason);
1540 static void gd_ungrab_keyboard(GtkDisplayState *s)
1542 VirtualConsole *vc = s->kbd_owner;
1544 if (vc == NULL) {
1545 return;
1547 s->kbd_owner = NULL;
1549 win32_kbd_set_grab(false);
1550 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1551 gd_update_caption(s);
1552 trace_gd_ungrab(vc->label, "kbd");
1555 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1557 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1559 if (vc->s->ptr_owner) {
1560 if (vc->s->ptr_owner == vc) {
1561 return;
1562 } else {
1563 gd_ungrab_pointer(vc->s);
1567 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1568 gdk_device_get_position(gd_get_pointer(display),
1569 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1570 vc->s->ptr_owner = vc;
1571 gd_update_caption(vc->s);
1572 trace_gd_grab(vc->label, "ptr", reason);
1575 static void gd_ungrab_pointer(GtkDisplayState *s)
1577 VirtualConsole *vc = s->ptr_owner;
1578 GdkDisplay *display;
1580 if (vc == NULL) {
1581 return;
1583 s->ptr_owner = NULL;
1585 display = gtk_widget_get_display(vc->gfx.drawing_area);
1586 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1587 gdk_device_warp(gd_get_pointer(display),
1588 gtk_widget_get_screen(vc->gfx.drawing_area),
1589 vc->s->grab_x_root, vc->s->grab_y_root);
1590 gd_update_caption(s);
1591 trace_gd_ungrab(vc->label, "ptr");
1594 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1596 GtkDisplayState *s = opaque;
1597 VirtualConsole *vc = gd_vc_find_current(s);
1599 if (gd_is_grab_active(s)) {
1600 gd_grab_keyboard(vc, "user-request-main-window");
1601 gd_grab_pointer(vc, "user-request-main-window");
1602 } else {
1603 gd_ungrab_keyboard(s);
1604 gd_ungrab_pointer(s);
1607 gd_update_cursor(vc);
1610 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1611 gpointer data)
1613 GtkDisplayState *s = data;
1614 VirtualConsole *vc;
1615 gboolean on_vga;
1617 if (!gtk_widget_get_realized(s->notebook)) {
1618 return;
1621 vc = gd_vc_find_by_page(s, arg2);
1622 if (!vc) {
1623 return;
1625 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1626 TRUE);
1627 on_vga = (vc->type == GD_VC_GFX &&
1628 qemu_console_is_graphic(vc->gfx.dcl.con));
1629 if (!on_vga) {
1630 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1631 FALSE);
1632 } else if (s->full_screen) {
1633 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1634 TRUE);
1636 gtk_widget_set_sensitive(s->grab_item, on_vga);
1637 #ifdef CONFIG_VTE
1638 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1639 #endif
1641 gd_update_windowsize(vc);
1642 gd_update_cursor(vc);
1645 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1646 gpointer opaque)
1648 VirtualConsole *vc = opaque;
1649 GtkDisplayState *s = vc->s;
1651 if (gd_grab_on_hover(s)) {
1652 gd_grab_keyboard(vc, "grab-on-hover");
1654 return TRUE;
1657 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1658 gpointer opaque)
1660 VirtualConsole *vc = opaque;
1661 GtkDisplayState *s = vc->s;
1663 if (gd_grab_on_hover(s)) {
1664 gd_ungrab_keyboard(s);
1666 return TRUE;
1669 static gboolean gd_focus_in_event(GtkWidget *widget,
1670 GdkEventFocus *event, gpointer opaque)
1672 VirtualConsole *vc = opaque;
1674 win32_kbd_set_window(gd_win32_get_hwnd(vc));
1675 return TRUE;
1678 static gboolean gd_focus_out_event(GtkWidget *widget,
1679 GdkEventFocus *event, gpointer opaque)
1681 VirtualConsole *vc = opaque;
1682 GtkDisplayState *s = vc->s;
1684 win32_kbd_set_window(NULL);
1685 gtk_release_modifiers(s);
1686 return TRUE;
1689 static gboolean gd_configure(GtkWidget *widget,
1690 GdkEventConfigure *cfg, gpointer opaque)
1692 VirtualConsole *vc = opaque;
1694 gd_set_ui_info(vc, cfg->width, cfg->height);
1695 return FALSE;
1698 /** Virtual Console Callbacks **/
1700 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1701 int idx, GSList *group, GtkWidget *view_menu)
1703 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1704 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1705 HOTKEY_MODIFIERS, 0,
1706 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1707 gtk_accel_label_set_accel(
1708 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1709 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1711 g_signal_connect(vc->menu_item, "activate",
1712 G_CALLBACK(gd_menu_switch_vc), s);
1713 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1715 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1718 #if defined(CONFIG_VTE)
1719 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1721 GtkDisplayState *s = opaque;
1722 VirtualConsole *vc = gd_vc_find_current(s);
1724 #if VTE_CHECK_VERSION(0, 50, 0)
1725 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1726 VTE_FORMAT_TEXT);
1727 #else
1728 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1729 #endif
1732 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1734 VirtualConsole *vc = opaque;
1736 if (gtk_adjustment_get_upper(adjustment) >
1737 gtk_adjustment_get_page_size(adjustment)) {
1738 gtk_widget_show(vc->vte.scrollbar);
1739 } else {
1740 gtk_widget_hide(vc->vte.scrollbar);
1744 static void gd_vc_send_chars(VirtualConsole *vc)
1746 uint32_t len, avail;
1748 len = qemu_chr_be_can_write(vc->vte.chr);
1749 avail = fifo8_num_used(&vc->vte.out_fifo);
1750 while (len > 0 && avail > 0) {
1751 const uint8_t *buf;
1752 uint32_t size;
1754 buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
1755 qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size);
1756 len = qemu_chr_be_can_write(vc->vte.chr);
1757 avail -= size;
1761 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1763 VCChardev *vcd = VC_CHARDEV(chr);
1764 VirtualConsole *vc = vcd->console;
1766 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1767 return len;
1770 static void gd_vc_chr_accept_input(Chardev *chr)
1772 VCChardev *vcd = VC_CHARDEV(chr);
1773 VirtualConsole *vc = vcd->console;
1775 gd_vc_send_chars(vc);
1778 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1780 VCChardev *vcd = VC_CHARDEV(chr);
1781 VirtualConsole *vc = vcd->console;
1783 if (vc) {
1784 vc->vte.echo = echo;
1785 } else {
1786 vcd->echo = echo;
1790 static int nb_vcs;
1791 static Chardev *vcs[MAX_VCS];
1792 static void gd_vc_open(Chardev *chr,
1793 ChardevBackend *backend,
1794 bool *be_opened,
1795 Error **errp)
1797 if (nb_vcs == MAX_VCS) {
1798 error_setg(errp, "Maximum number of consoles reached");
1799 return;
1802 vcs[nb_vcs++] = chr;
1804 /* console/chardev init sometimes completes elsewhere in a 2nd
1805 * stage, so defer OPENED events until they are fully initialized
1807 *be_opened = false;
1810 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1812 ChardevClass *cc = CHARDEV_CLASS(oc);
1814 cc->parse = qemu_chr_parse_vc;
1815 cc->open = gd_vc_open;
1816 cc->chr_write = gd_vc_chr_write;
1817 cc->chr_accept_input = gd_vc_chr_accept_input;
1818 cc->chr_set_echo = gd_vc_chr_set_echo;
1821 static const TypeInfo char_gd_vc_type_info = {
1822 .name = TYPE_CHARDEV_VC,
1823 .parent = TYPE_CHARDEV,
1824 .instance_size = sizeof(VCChardev),
1825 .class_init = char_gd_vc_class_init,
1828 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1829 gpointer user_data)
1831 VirtualConsole *vc = user_data;
1832 uint32_t free;
1834 if (vc->vte.echo) {
1835 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1836 int i;
1837 for (i = 0; i < size; i++) {
1838 uint8_t c = text[i];
1839 if (c >= 128 || isprint(c)) {
1840 /* 8-bit characters are considered printable. */
1841 vte_terminal_feed(term, &text[i], 1);
1842 } else if (c == '\r' || c == '\n') {
1843 vte_terminal_feed(term, "\r\n", 2);
1844 } else {
1845 char ctrl[2] = { '^', 0};
1846 ctrl[1] = text[i] ^ 64;
1847 vte_terminal_feed(term, ctrl, 2);
1852 free = fifo8_num_free(&vc->vte.out_fifo);
1853 fifo8_push_all(&vc->vte.out_fifo, (uint8_t *)text, MIN(free, size));
1854 gd_vc_send_chars(vc);
1856 return TRUE;
1859 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1860 Chardev *chr, int idx,
1861 GSList *group, GtkWidget *view_menu)
1863 char buffer[32];
1864 GtkWidget *box;
1865 GtkWidget *scrollbar;
1866 GtkAdjustment *vadjustment;
1867 VCChardev *vcd = VC_CHARDEV(chr);
1869 vc->s = s;
1870 vc->vte.echo = vcd->echo;
1871 vc->vte.chr = chr;
1872 fifo8_create(&vc->vte.out_fifo, 4096);
1873 vcd->console = vc;
1875 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1876 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1877 ? vc->vte.chr->label : buffer);
1878 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1880 vc->vte.terminal = vte_terminal_new();
1881 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1883 /* The documentation says that the default is UTF-8, but actually it is
1884 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1885 * VTE 0.54 (only UTF-8 is supported now). */
1886 #if !VTE_CHECK_VERSION(0, 54, 0)
1887 #if VTE_CHECK_VERSION(0, 38, 0)
1888 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1889 #else
1890 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1891 #endif
1892 #endif
1894 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1895 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1896 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1898 #if VTE_CHECK_VERSION(0, 28, 0)
1899 vadjustment = gtk_scrollable_get_vadjustment
1900 (GTK_SCROLLABLE(vc->vte.terminal));
1901 #else
1902 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1903 #endif
1905 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1906 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1908 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1909 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1911 vc->vte.box = box;
1912 vc->vte.scrollbar = scrollbar;
1914 g_signal_connect(vadjustment, "changed",
1915 G_CALLBACK(gd_vc_adjustment_changed), vc);
1917 vc->type = GD_VC_VTE;
1918 vc->tab_item = box;
1919 vc->focus = vc->vte.terminal;
1920 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1921 gtk_label_new(vc->label));
1923 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1925 return group;
1928 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1929 GtkWidget *view_menu)
1931 int i;
1933 for (i = 0; i < nb_vcs; i++) {
1934 VirtualConsole *vc = &s->vc[s->nb_vcs];
1935 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1936 s->nb_vcs++;
1939 #endif /* CONFIG_VTE */
1941 /** Window Creation **/
1943 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1945 g_signal_connect(vc->gfx.drawing_area, "draw",
1946 G_CALLBACK(gd_draw_event), vc);
1947 #if defined(CONFIG_OPENGL)
1948 if (gtk_use_gl_area) {
1949 /* wire up GtkGlArea events */
1950 g_signal_connect(vc->gfx.drawing_area, "render",
1951 G_CALLBACK(gd_render_event), vc);
1952 g_signal_connect(vc->gfx.drawing_area, "resize",
1953 G_CALLBACK(gd_resize_event), vc);
1955 #endif
1956 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1957 g_signal_connect(vc->gfx.drawing_area, "event",
1958 G_CALLBACK(gd_event), vc);
1959 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1960 G_CALLBACK(gd_button_event), vc);
1961 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1962 G_CALLBACK(gd_button_event), vc);
1963 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1964 G_CALLBACK(gd_scroll_event), vc);
1965 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1966 G_CALLBACK(gd_key_event), vc);
1967 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1968 G_CALLBACK(gd_key_event), vc);
1970 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1971 G_CALLBACK(gd_enter_event), vc);
1972 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1973 G_CALLBACK(gd_leave_event), vc);
1974 g_signal_connect(vc->gfx.drawing_area, "focus-in-event",
1975 G_CALLBACK(gd_focus_in_event), vc);
1976 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1977 G_CALLBACK(gd_focus_out_event), vc);
1978 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1979 G_CALLBACK(gd_configure), vc);
1980 g_signal_connect(vc->gfx.drawing_area, "grab-broken-event",
1981 G_CALLBACK(gd_grab_broken_event), vc);
1982 } else {
1983 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1984 G_CALLBACK(gd_text_key_down), vc);
1988 static void gd_connect_signals(GtkDisplayState *s)
1990 g_signal_connect(s->show_tabs_item, "activate",
1991 G_CALLBACK(gd_menu_show_tabs), s);
1992 g_signal_connect(s->untabify_item, "activate",
1993 G_CALLBACK(gd_menu_untabify), s);
1994 g_signal_connect(s->show_menubar_item, "activate",
1995 G_CALLBACK(gd_menu_show_menubar), s);
1997 g_signal_connect(s->window, "delete-event",
1998 G_CALLBACK(gd_window_close), s);
2000 g_signal_connect(s->pause_item, "activate",
2001 G_CALLBACK(gd_menu_pause), s);
2002 g_signal_connect(s->reset_item, "activate",
2003 G_CALLBACK(gd_menu_reset), s);
2004 g_signal_connect(s->powerdown_item, "activate",
2005 G_CALLBACK(gd_menu_powerdown), s);
2006 g_signal_connect(s->quit_item, "activate",
2007 G_CALLBACK(gd_menu_quit), s);
2008 #if defined(CONFIG_VTE)
2009 g_signal_connect(s->copy_item, "activate",
2010 G_CALLBACK(gd_menu_copy), s);
2011 #endif
2012 g_signal_connect(s->full_screen_item, "activate",
2013 G_CALLBACK(gd_menu_full_screen), s);
2014 g_signal_connect(s->zoom_in_item, "activate",
2015 G_CALLBACK(gd_menu_zoom_in), s);
2016 g_signal_connect(s->zoom_out_item, "activate",
2017 G_CALLBACK(gd_menu_zoom_out), s);
2018 g_signal_connect(s->zoom_fixed_item, "activate",
2019 G_CALLBACK(gd_menu_zoom_fixed), s);
2020 g_signal_connect(s->zoom_fit_item, "activate",
2021 G_CALLBACK(gd_menu_zoom_fit), s);
2022 g_signal_connect(s->grab_item, "activate",
2023 G_CALLBACK(gd_menu_grab_input), s);
2024 g_signal_connect(s->notebook, "switch-page",
2025 G_CALLBACK(gd_change_page), s);
2028 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2030 GtkWidget *machine_menu;
2031 GtkWidget *separator;
2033 machine_menu = gtk_menu_new();
2034 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2036 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2037 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2039 separator = gtk_separator_menu_item_new();
2040 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2042 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2043 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2045 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2046 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2048 separator = gtk_separator_menu_item_new();
2049 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2051 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2052 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2053 "<QEMU>/Machine/Quit");
2054 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2055 GDK_KEY_q, HOTKEY_MODIFIERS);
2056 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2058 return machine_menu;
2061 #if defined(CONFIG_OPENGL)
2062 static void gl_area_realize(GtkGLArea *area, VirtualConsole *vc)
2064 gtk_gl_area_make_current(area);
2065 qemu_egl_display = eglGetCurrentDisplay();
2066 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2067 if (!vc->gfx.has_dmabuf) {
2068 error_report("GtkGLArea console lacks DMABUF support.");
2071 #endif
2073 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2074 QemuConsole *con, int idx,
2075 GSList *group, GtkWidget *view_menu)
2077 bool zoom_to_fit = false;
2079 vc->label = qemu_console_get_label(con);
2080 vc->s = s;
2081 vc->gfx.scale_x = 1.0;
2082 vc->gfx.scale_y = 1.0;
2084 #if defined(CONFIG_OPENGL)
2085 if (display_opengl) {
2086 if (gtk_use_gl_area) {
2087 vc->gfx.drawing_area = gtk_gl_area_new();
2088 g_signal_connect(vc->gfx.drawing_area, "realize",
2089 G_CALLBACK(gl_area_realize), vc);
2090 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2091 vc->gfx.dgc.ops = &gl_area_ctx_ops;
2092 } else {
2093 #ifdef CONFIG_X11
2094 vc->gfx.drawing_area = gtk_drawing_area_new();
2096 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2097 * It is required for opengl rendering on X11 though. A
2098 * proper replacement (native opengl support) is only
2099 * available in 3.16+. Silence the warning if possible.
2101 #pragma GCC diagnostic push
2102 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2103 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2104 #pragma GCC diagnostic pop
2105 vc->gfx.dcl.ops = &dcl_egl_ops;
2106 vc->gfx.dgc.ops = &egl_ctx_ops;
2107 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2108 #else
2109 abort();
2110 #endif
2112 } else
2113 #endif
2115 vc->gfx.drawing_area = gtk_drawing_area_new();
2116 vc->gfx.dcl.ops = &dcl_ops;
2120 gtk_widget_add_events(vc->gfx.drawing_area,
2121 GDK_POINTER_MOTION_MASK |
2122 GDK_BUTTON_PRESS_MASK |
2123 GDK_BUTTON_RELEASE_MASK |
2124 GDK_BUTTON_MOTION_MASK |
2125 GDK_ENTER_NOTIFY_MASK |
2126 GDK_LEAVE_NOTIFY_MASK |
2127 GDK_SCROLL_MASK |
2128 GDK_SMOOTH_SCROLL_MASK |
2129 GDK_KEY_PRESS_MASK);
2130 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2132 vc->type = GD_VC_GFX;
2133 vc->tab_item = vc->gfx.drawing_area;
2134 vc->focus = vc->gfx.drawing_area;
2135 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2136 vc->tab_item, gtk_label_new(vc->label));
2138 vc->gfx.kbd = qkbd_state_init(con);
2139 vc->gfx.dcl.con = con;
2141 if (display_opengl) {
2142 qemu_console_set_display_gl_ctx(con, &vc->gfx.dgc);
2144 register_displaychangelistener(&vc->gfx.dcl);
2146 gd_connect_vc_gfx_signals(vc);
2147 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2149 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2150 zoom_to_fit = true;
2152 if (s->opts->u.gtk.has_zoom_to_fit) {
2153 zoom_to_fit = s->opts->u.gtk.zoom_to_fit;
2155 if (zoom_to_fit) {
2156 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2157 s->free_scale = true;
2160 return group;
2163 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2165 GSList *group = NULL;
2166 GtkWidget *view_menu;
2167 GtkWidget *separator;
2168 QemuConsole *con;
2169 int vc;
2171 view_menu = gtk_menu_new();
2172 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2174 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2176 #if defined(CONFIG_VTE)
2177 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2178 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2179 #endif
2181 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2182 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2183 gtk_accel_label_set_accel(
2184 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2185 GDK_KEY_f, HOTKEY_MODIFIERS);
2186 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2188 separator = gtk_separator_menu_item_new();
2189 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2191 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2192 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2193 "<QEMU>/View/Zoom In");
2194 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2195 HOTKEY_MODIFIERS);
2196 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2197 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2198 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2200 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2201 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2202 "<QEMU>/View/Zoom Out");
2203 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2204 HOTKEY_MODIFIERS);
2205 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2207 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2208 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2209 "<QEMU>/View/Zoom Fixed");
2210 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2211 HOTKEY_MODIFIERS);
2212 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2214 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2215 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2217 separator = gtk_separator_menu_item_new();
2218 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2220 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2221 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2223 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2224 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2225 "<QEMU>/View/Grab Input");
2226 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2227 HOTKEY_MODIFIERS);
2228 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2230 separator = gtk_separator_menu_item_new();
2231 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2233 /* gfx */
2234 for (vc = 0;; vc++) {
2235 con = qemu_console_lookup_by_index(vc);
2236 if (!con) {
2237 break;
2239 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2240 vc, group, view_menu);
2241 s->nb_vcs++;
2244 #if defined(CONFIG_VTE)
2245 /* vte */
2246 gd_vcs_init(s, group, view_menu);
2247 #endif
2249 separator = gtk_separator_menu_item_new();
2250 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2252 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2253 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2255 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2256 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2258 s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
2259 _("Show Menubar"));
2260 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
2261 TRUE);
2262 gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
2263 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
2264 gtk_accel_label_set_accel(
2265 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
2266 GDK_KEY_m, HOTKEY_MODIFIERS);
2267 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
2269 return view_menu;
2272 static void gd_create_menus(GtkDisplayState *s)
2274 GtkSettings *settings;
2276 s->accel_group = gtk_accel_group_new();
2277 s->machine_menu = gd_create_menu_machine(s);
2278 s->view_menu = gd_create_menu_view(s);
2280 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2281 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2282 s->machine_menu);
2283 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2285 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2286 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2287 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2289 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2290 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2292 /* Disable the default "F10" menu shortcut. */
2293 settings = gtk_widget_get_settings(s->window);
2294 g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
2298 static gboolean gtkinit;
2300 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2302 VirtualConsole *vc;
2304 GtkDisplayState *s = g_malloc0(sizeof(*s));
2305 GdkDisplay *window_display;
2306 GtkIconTheme *theme;
2307 char *dir;
2309 if (!gtkinit) {
2310 fprintf(stderr, "gtk initialization failed\n");
2311 exit(1);
2313 assert(opts->type == DISPLAY_TYPE_GTK);
2314 s->opts = opts;
2316 theme = gtk_icon_theme_get_default();
2317 dir = get_relocated_path(CONFIG_QEMU_ICONDIR);
2318 gtk_icon_theme_prepend_search_path(theme, dir);
2319 g_free(dir);
2320 g_set_prgname("qemu");
2322 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2323 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2324 s->notebook = gtk_notebook_new();
2325 s->menu_bar = gtk_menu_bar_new();
2327 s->free_scale = FALSE;
2329 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2330 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2331 * printable, but without changing any of the character classes to make
2332 * sure that we don't accidentally break implicit assumptions. */
2333 setlocale(LC_MESSAGES, "");
2334 setlocale(LC_CTYPE, "C.UTF-8");
2335 dir = get_relocated_path(CONFIG_QEMU_LOCALEDIR);
2336 bindtextdomain("qemu", dir);
2337 g_free(dir);
2338 bind_textdomain_codeset("qemu", "UTF-8");
2339 textdomain("qemu");
2341 window_display = gtk_widget_get_display(s->window);
2342 if (s->opts->has_show_cursor && s->opts->show_cursor) {
2343 s->null_cursor = NULL; /* default pointer */
2344 } else {
2345 s->null_cursor = gdk_cursor_new_for_display(window_display,
2346 GDK_BLANK_CURSOR);
2349 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2350 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2351 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2353 gtk_window_set_icon_name(GTK_WINDOW(s->window), "qemu");
2355 gd_create_menus(s);
2357 gd_connect_signals(s);
2359 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2360 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2362 gd_update_caption(s);
2364 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2365 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2367 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2369 gtk_widget_show_all(s->window);
2371 vc = gd_vc_find_current(s);
2372 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2373 #ifdef CONFIG_VTE
2374 gtk_widget_set_sensitive(s->copy_item,
2375 vc && vc->type == GD_VC_VTE);
2376 #endif
2378 if (opts->has_full_screen &&
2379 opts->full_screen) {
2380 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2382 if (opts->u.gtk.has_grab_on_hover &&
2383 opts->u.gtk.grab_on_hover) {
2384 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2386 gd_clipboard_init(s);
2389 static void early_gtk_display_init(DisplayOptions *opts)
2391 /* The QEMU code relies on the assumption that it's always run in
2392 * the C locale. Therefore it is not prepared to deal with
2393 * operations that produce different results depending on the
2394 * locale, such as printf's formatting of decimal numbers, and
2395 * possibly others.
2397 * Since GTK+ calls setlocale() by default -importing the locale
2398 * settings from the environment- we must prevent it from doing so
2399 * using gtk_disable_setlocale().
2401 * QEMU's GTK+ UI, however, _does_ have translations for some of
2402 * the menu items. As a trade-off between a functionally correct
2403 * QEMU and a fully internationalized UI we support importing
2404 * LC_MESSAGES from the environment (see the setlocale() call
2405 * earlier in this file). This allows us to display translated
2406 * messages leaving everything else untouched.
2408 gtk_disable_setlocale();
2409 gtkinit = gtk_init_check(NULL, NULL);
2410 if (!gtkinit) {
2411 /* don't exit yet, that'll break -help */
2412 return;
2415 assert(opts->type == DISPLAY_TYPE_GTK);
2416 if (opts->has_gl && opts->gl != DISPLAYGL_MODE_OFF) {
2417 #if defined(CONFIG_OPENGL)
2418 #if defined(GDK_WINDOWING_WAYLAND)
2419 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2420 gtk_use_gl_area = true;
2421 gtk_gl_area_init();
2422 } else
2423 #endif
2425 #ifdef CONFIG_X11
2426 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
2427 gtk_egl_init(mode);
2428 #endif
2430 #endif
2433 keycode_map = gd_get_keymap(&keycode_maplen);
2435 #if defined(CONFIG_VTE)
2436 type_register(&char_gd_vc_type_info);
2437 #endif
2440 static QemuDisplay qemu_display_gtk = {
2441 .type = DISPLAY_TYPE_GTK,
2442 .early_init = early_gtk_display_init,
2443 .init = gtk_display_init,
2446 static void register_gtk(void)
2448 qemu_display_register(&qemu_display_gtk);
2451 type_init(register_gtk);
2453 #ifdef CONFIG_OPENGL
2454 module_dep("ui-opengl");
2455 #endif