Drop duplicate #include
[qemu/armbru.git] / ui / gtk.c
blob7f752d8b7d6ad95b5475c60a8f226e5ad22cdf80
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 "ui/input.h"
57 #include "sysemu/runstate.h"
58 #include "sysemu/sysemu.h"
59 #include "keymaps.h"
60 #include "chardev/char.h"
61 #include "qom/object.h"
63 #define VC_WINDOW_X_MIN 320
64 #define VC_WINDOW_Y_MIN 240
65 #define VC_TERM_X_MIN 80
66 #define VC_TERM_Y_MIN 25
67 #define VC_SCALE_MIN 0.25
68 #define VC_SCALE_STEP 0.25
70 #ifdef GDK_WINDOWING_X11
71 #include "x_keymap.h"
73 /* Gtk2 compat */
74 #ifndef GDK_IS_X11_DISPLAY
75 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
76 #endif
77 #endif
80 #ifdef GDK_WINDOWING_WAYLAND
81 /* Gtk2 compat */
82 #ifndef GDK_IS_WAYLAND_DISPLAY
83 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
84 #endif
85 #endif
88 #ifdef GDK_WINDOWING_WIN32
89 /* Gtk2 compat */
90 #ifndef GDK_IS_WIN32_DISPLAY
91 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
92 #endif
93 #endif
96 #ifdef GDK_WINDOWING_BROADWAY
97 /* Gtk2 compat */
98 #ifndef GDK_IS_BROADWAY_DISPLAY
99 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
100 #endif
101 #endif
104 #ifdef GDK_WINDOWING_QUARTZ
105 /* Gtk2 compat */
106 #ifndef GDK_IS_QUARTZ_DISPLAY
107 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
108 #endif
109 #endif
112 #if !defined(CONFIG_VTE)
113 # define VTE_CHECK_VERSION(a, b, c) 0
114 #endif
116 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
118 static const guint16 *keycode_map;
119 static size_t keycode_maplen;
121 struct VCChardev {
122 Chardev parent;
123 VirtualConsole *console;
124 bool echo;
126 typedef struct VCChardev VCChardev;
128 #define TYPE_CHARDEV_VC "chardev-vc"
129 DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
130 TYPE_CHARDEV_VC)
132 bool gtk_use_gl_area;
134 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
135 static void gd_ungrab_pointer(GtkDisplayState *s);
136 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
137 static void gd_ungrab_keyboard(GtkDisplayState *s);
139 /** Utility Functions **/
141 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
143 VirtualConsole *vc;
144 gint i;
146 for (i = 0; i < s->nb_vcs; i++) {
147 vc = &s->vc[i];
148 if (gtk_check_menu_item_get_active
149 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
150 return vc;
153 return NULL;
156 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
158 VirtualConsole *vc;
159 gint i, p;
161 for (i = 0; i < s->nb_vcs; i++) {
162 vc = &s->vc[i];
163 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
164 if (p == page) {
165 return vc;
168 return NULL;
171 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
173 gint page;
175 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
176 return gd_vc_find_by_page(s, page);
179 static bool gd_is_grab_active(GtkDisplayState *s)
181 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
184 static bool gd_grab_on_hover(GtkDisplayState *s)
186 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
189 static void gd_update_cursor(VirtualConsole *vc)
191 GtkDisplayState *s = vc->s;
192 GdkWindow *window;
194 if (vc->type != GD_VC_GFX ||
195 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
196 return;
199 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
200 return;
203 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
204 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
205 gdk_window_set_cursor(window, s->null_cursor);
206 } else {
207 gdk_window_set_cursor(window, NULL);
211 static void gd_update_caption(GtkDisplayState *s)
213 const char *status = "";
214 gchar *prefix;
215 gchar *title;
216 const char *grab = "";
217 bool is_paused = !runstate_is_running();
218 int i;
220 if (qemu_name) {
221 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
222 } else {
223 prefix = g_strdup_printf("QEMU");
226 if (s->ptr_owner != NULL &&
227 s->ptr_owner->window == NULL) {
228 grab = _(" - Press Ctrl+Alt+G to release grab");
231 if (is_paused) {
232 status = _(" [Paused]");
234 s->external_pause_update = true;
235 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
236 is_paused);
237 s->external_pause_update = false;
239 title = g_strdup_printf("%s%s%s", prefix, status, grab);
240 gtk_window_set_title(GTK_WINDOW(s->window), title);
241 g_free(title);
243 for (i = 0; i < s->nb_vcs; i++) {
244 VirtualConsole *vc = &s->vc[i];
246 if (!vc->window) {
247 continue;
249 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
250 vc == s->kbd_owner ? " +kbd" : "",
251 vc == s->ptr_owner ? " +ptr" : "");
252 gtk_window_set_title(GTK_WINDOW(vc->window), title);
253 g_free(title);
256 g_free(prefix);
259 static void gd_update_geometry_hints(VirtualConsole *vc)
261 GtkDisplayState *s = vc->s;
262 GdkWindowHints mask = 0;
263 GdkGeometry geo = {};
264 GtkWidget *geo_widget = NULL;
265 GtkWindow *geo_window;
267 if (vc->type == GD_VC_GFX) {
268 if (!vc->gfx.ds) {
269 return;
271 if (s->free_scale) {
272 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
273 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
274 mask |= GDK_HINT_MIN_SIZE;
275 } else {
276 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
277 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
278 mask |= GDK_HINT_MIN_SIZE;
280 geo_widget = vc->gfx.drawing_area;
281 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
283 #if defined(CONFIG_VTE)
284 } else if (vc->type == GD_VC_VTE) {
285 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
286 GtkBorder padding = { 0 };
288 #if VTE_CHECK_VERSION(0, 37, 0)
289 gtk_style_context_get_padding(
290 gtk_widget_get_style_context(vc->vte.terminal),
291 gtk_widget_get_state_flags(vc->vte.terminal),
292 &padding);
293 #else
295 GtkBorder *ib = NULL;
296 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
297 if (ib) {
298 padding = *ib;
299 gtk_border_free(ib);
302 #endif
304 geo.width_inc = vte_terminal_get_char_width(term);
305 geo.height_inc = vte_terminal_get_char_height(term);
306 mask |= GDK_HINT_RESIZE_INC;
307 geo.base_width = geo.width_inc;
308 geo.base_height = geo.height_inc;
309 mask |= GDK_HINT_BASE_SIZE;
310 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
311 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
312 mask |= GDK_HINT_MIN_SIZE;
314 geo.base_width += padding.left + padding.right;
315 geo.base_height += padding.top + padding.bottom;
316 geo.min_width += padding.left + padding.right;
317 geo.min_height += padding.top + padding.bottom;
318 geo_widget = vc->vte.terminal;
319 #endif
322 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
323 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
326 void gd_update_windowsize(VirtualConsole *vc)
328 GtkDisplayState *s = vc->s;
330 gd_update_geometry_hints(vc);
332 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
333 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
334 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
338 static void gd_update_full_redraw(VirtualConsole *vc)
340 GtkWidget *area = vc->gfx.drawing_area;
341 int ww, wh;
342 ww = gdk_window_get_width(gtk_widget_get_window(area));
343 wh = gdk_window_get_height(gtk_widget_get_window(area));
344 #if defined(CONFIG_OPENGL)
345 if (vc->gfx.gls && gtk_use_gl_area) {
346 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
347 return;
349 #endif
350 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
353 static void gtk_release_modifiers(GtkDisplayState *s)
355 VirtualConsole *vc = gd_vc_find_current(s);
357 if (vc->type != GD_VC_GFX ||
358 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
359 return;
361 qkbd_state_lift_all_keys(vc->gfx.kbd);
364 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
365 GtkWidget *widget)
367 g_object_ref(G_OBJECT(widget));
368 gtk_container_remove(GTK_CONTAINER(from), widget);
369 gtk_container_add(GTK_CONTAINER(to), widget);
370 g_object_unref(G_OBJECT(widget));
373 static void *gd_win32_get_hwnd(VirtualConsole *vc)
375 #ifdef G_OS_WIN32
376 return gdk_win32_window_get_impl_hwnd(
377 gtk_widget_get_window(vc->window ? vc->window : vc->s->window));
378 #else
379 return NULL;
380 #endif
383 /** DisplayState Callbacks **/
385 static void gd_update(DisplayChangeListener *dcl,
386 int x, int y, int w, int h)
388 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
389 GdkWindow *win;
390 int x1, x2, y1, y2;
391 int mx, my;
392 int fbw, fbh;
393 int ww, wh;
395 trace_gd_update(vc->label, x, y, w, h);
397 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
398 return;
401 if (vc->gfx.convert) {
402 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
403 NULL, vc->gfx.convert,
404 x, y, 0, 0, x, y, w, h);
407 x1 = floor(x * vc->gfx.scale_x);
408 y1 = floor(y * vc->gfx.scale_y);
410 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
411 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
413 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
414 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
416 win = gtk_widget_get_window(vc->gfx.drawing_area);
417 if (!win) {
418 return;
420 ww = gdk_window_get_width(win);
421 wh = gdk_window_get_height(win);
423 mx = my = 0;
424 if (ww > fbw) {
425 mx = (ww - fbw) / 2;
427 if (wh > fbh) {
428 my = (wh - fbh) / 2;
431 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
432 mx + x1, my + y1, (x2 - x1), (y2 - y1));
435 static void gd_refresh(DisplayChangeListener *dcl)
437 graphic_hw_update(dcl->con);
440 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
442 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
445 static void gd_mouse_set(DisplayChangeListener *dcl,
446 int x, int y, int visible)
448 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
449 GdkDisplay *dpy;
450 gint x_root, y_root;
452 if (qemu_input_is_absolute()) {
453 return;
456 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
457 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
458 x, y, &x_root, &y_root);
459 gdk_device_warp(gd_get_pointer(dpy),
460 gtk_widget_get_screen(vc->gfx.drawing_area),
461 x_root, y_root);
462 vc->s->last_x = x;
463 vc->s->last_y = y;
466 static void gd_cursor_define(DisplayChangeListener *dcl,
467 QEMUCursor *c)
469 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
470 GdkPixbuf *pixbuf;
471 GdkCursor *cursor;
473 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
474 return;
477 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
478 GDK_COLORSPACE_RGB, true, 8,
479 c->width, c->height, c->width * 4,
480 NULL, NULL);
481 cursor = gdk_cursor_new_from_pixbuf
482 (gtk_widget_get_display(vc->gfx.drawing_area),
483 pixbuf, c->hot_x, c->hot_y);
484 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
485 g_object_unref(pixbuf);
486 g_object_unref(cursor);
489 static void gd_switch(DisplayChangeListener *dcl,
490 DisplaySurface *surface)
492 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
493 bool resized = true;
495 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
497 if (vc->gfx.surface) {
498 cairo_surface_destroy(vc->gfx.surface);
499 vc->gfx.surface = NULL;
501 if (vc->gfx.convert) {
502 pixman_image_unref(vc->gfx.convert);
503 vc->gfx.convert = NULL;
506 if (vc->gfx.ds &&
507 surface_width(vc->gfx.ds) == surface_width(surface) &&
508 surface_height(vc->gfx.ds) == surface_height(surface)) {
509 resized = false;
511 vc->gfx.ds = surface;
513 if (surface->format == PIXMAN_x8r8g8b8) {
515 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
517 * No need to convert, use surface directly. Should be the
518 * common case as this is qemu_default_pixelformat(32) too.
520 vc->gfx.surface = cairo_image_surface_create_for_data
521 (surface_data(surface),
522 CAIRO_FORMAT_RGB24,
523 surface_width(surface),
524 surface_height(surface),
525 surface_stride(surface));
526 } else {
527 /* Must convert surface, use pixman to do it. */
528 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
529 surface_width(surface),
530 surface_height(surface),
531 NULL, 0);
532 vc->gfx.surface = cairo_image_surface_create_for_data
533 ((void *)pixman_image_get_data(vc->gfx.convert),
534 CAIRO_FORMAT_RGB24,
535 pixman_image_get_width(vc->gfx.convert),
536 pixman_image_get_height(vc->gfx.convert),
537 pixman_image_get_stride(vc->gfx.convert));
538 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
539 NULL, vc->gfx.convert,
540 0, 0, 0, 0, 0, 0,
541 pixman_image_get_width(vc->gfx.convert),
542 pixman_image_get_height(vc->gfx.convert));
545 if (resized) {
546 gd_update_windowsize(vc);
547 } else {
548 gd_update_full_redraw(vc);
552 static const DisplayChangeListenerOps dcl_ops = {
553 .dpy_name = "gtk",
554 .dpy_gfx_update = gd_update,
555 .dpy_gfx_switch = gd_switch,
556 .dpy_gfx_check_format = qemu_pixman_check_format,
557 .dpy_refresh = gd_refresh,
558 .dpy_mouse_set = gd_mouse_set,
559 .dpy_cursor_define = gd_cursor_define,
563 #if defined(CONFIG_OPENGL)
565 static bool gd_has_dmabuf(DisplayChangeListener *dcl)
567 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
569 if (gtk_use_gl_area && !gtk_widget_get_realized(vc->gfx.drawing_area)) {
570 /* FIXME: Assume it will work, actual check done after realize */
571 /* fixing this would require delaying listener registration */
572 return true;
575 return vc->gfx.has_dmabuf;
578 static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
579 QemuDmaBuf *dmabuf)
581 #ifdef CONFIG_GBM
582 egl_dmabuf_release_texture(dmabuf);
583 #endif
586 void gd_hw_gl_flushed(void *vcon)
588 VirtualConsole *vc = vcon;
589 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
591 qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
592 close(dmabuf->fence_fd);
593 dmabuf->fence_fd = -1;
594 graphic_hw_gl_block(vc->gfx.dcl.con, false);
597 /** DisplayState Callbacks (opengl version) **/
599 static const DisplayChangeListenerOps dcl_gl_area_ops = {
600 .dpy_name = "gtk-egl",
601 .dpy_gfx_update = gd_gl_area_update,
602 .dpy_gfx_switch = gd_gl_area_switch,
603 .dpy_gfx_check_format = console_gl_check_format,
604 .dpy_refresh = gd_gl_area_refresh,
605 .dpy_mouse_set = gd_mouse_set,
606 .dpy_cursor_define = gd_cursor_define,
608 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
609 .dpy_gl_scanout_disable = gd_gl_area_scanout_disable,
610 .dpy_gl_update = gd_gl_area_scanout_flush,
611 .dpy_gl_scanout_dmabuf = gd_gl_area_scanout_dmabuf,
612 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
613 .dpy_has_dmabuf = gd_has_dmabuf,
616 static bool
617 gd_gl_area_is_compatible_dcl(DisplayGLCtx *dgc,
618 DisplayChangeListener *dcl)
620 return dcl->ops == &dcl_gl_area_ops;
623 static const DisplayGLCtxOps gl_area_ctx_ops = {
624 .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
625 .dpy_gl_ctx_create = gd_gl_area_create_context,
626 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
627 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
630 #ifdef CONFIG_X11
631 static const DisplayChangeListenerOps dcl_egl_ops = {
632 .dpy_name = "gtk-egl",
633 .dpy_gfx_update = gd_egl_update,
634 .dpy_gfx_switch = gd_egl_switch,
635 .dpy_gfx_check_format = console_gl_check_format,
636 .dpy_refresh = gd_egl_refresh,
637 .dpy_mouse_set = gd_mouse_set,
638 .dpy_cursor_define = gd_cursor_define,
640 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
641 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
642 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
643 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
644 .dpy_gl_cursor_position = gd_egl_cursor_position,
645 .dpy_gl_update = gd_egl_flush,
646 .dpy_gl_release_dmabuf = gd_gl_release_dmabuf,
647 .dpy_has_dmabuf = gd_has_dmabuf,
650 static bool
651 gd_egl_is_compatible_dcl(DisplayGLCtx *dgc,
652 DisplayChangeListener *dcl)
654 return dcl->ops == &dcl_egl_ops;
657 static const DisplayGLCtxOps egl_ctx_ops = {
658 .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
659 .dpy_gl_ctx_create = gd_egl_create_context,
660 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
661 .dpy_gl_ctx_make_current = gd_egl_make_current,
663 #endif
665 #endif /* CONFIG_OPENGL */
667 /** QEMU Events **/
669 static void gd_change_runstate(void *opaque, bool running, RunState state)
671 GtkDisplayState *s = opaque;
673 gd_update_caption(s);
676 static void gd_mouse_mode_change(Notifier *notify, void *data)
678 GtkDisplayState *s;
679 int i;
681 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
682 /* release the grab at switching to absolute mode */
683 if (qemu_input_is_absolute() && s->ptr_owner) {
684 if (!s->ptr_owner->window) {
685 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
686 FALSE);
687 } else {
688 gd_ungrab_pointer(s);
691 for (i = 0; i < s->nb_vcs; i++) {
692 VirtualConsole *vc = &s->vc[i];
693 gd_update_cursor(vc);
697 /** GTK Events **/
699 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
700 void *opaque)
702 GtkDisplayState *s = opaque;
703 bool allow_close = true;
705 if (s->opts->has_window_close && !s->opts->window_close) {
706 allow_close = false;
709 if (allow_close) {
710 qmp_quit(NULL);
713 return TRUE;
716 static void gd_set_ui_refresh_rate(VirtualConsole *vc, int refresh_rate)
718 QemuUIInfo info;
720 info = *dpy_get_ui_info(vc->gfx.dcl.con);
721 info.refresh_rate = refresh_rate;
722 dpy_set_ui_info(vc->gfx.dcl.con, &info, true);
725 static void gd_set_ui_size(VirtualConsole *vc, gint width, gint height)
727 QemuUIInfo info;
729 info = *dpy_get_ui_info(vc->gfx.dcl.con);
730 info.width = width;
731 info.height = height;
732 dpy_set_ui_info(vc->gfx.dcl.con, &info, true);
735 #if defined(CONFIG_OPENGL)
737 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
738 void *opaque)
740 VirtualConsole *vc = opaque;
742 if (vc->gfx.gls) {
743 gd_gl_area_draw(vc);
745 return TRUE;
748 static void gd_resize_event(GtkGLArea *area,
749 gint width, gint height, gpointer *opaque)
751 VirtualConsole *vc = (void *)opaque;
753 gd_set_ui_size(vc, width, height);
756 #endif
758 void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget)
760 #ifdef GDK_VERSION_3_22
761 GdkWindow *win = gtk_widget_get_window(widget);
762 int refresh_rate;
764 if (win) {
765 GdkDisplay *dpy = gtk_widget_get_display(widget);
766 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
767 refresh_rate = gdk_monitor_get_refresh_rate(monitor); /* [mHz] */
768 } else {
769 refresh_rate = 0;
772 gd_set_ui_refresh_rate(vc, refresh_rate);
774 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
775 vc->gfx.dcl.update_interval = refresh_rate ?
776 MIN(1000 * 1000 / refresh_rate, GUI_REFRESH_INTERVAL_DEFAULT) :
777 GUI_REFRESH_INTERVAL_DEFAULT;
778 #endif
781 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
783 VirtualConsole *vc = opaque;
784 GtkDisplayState *s = vc->s;
785 int mx, my;
786 int ww, wh;
787 int fbw, fbh;
789 #if defined(CONFIG_OPENGL)
790 if (vc->gfx.gls) {
791 if (gtk_use_gl_area) {
792 /* invoke render callback please */
793 return FALSE;
794 } else {
795 #ifdef CONFIG_X11
796 gd_egl_draw(vc);
797 return TRUE;
798 #else
799 abort();
800 #endif
803 #endif
805 if (!gtk_widget_get_realized(widget)) {
806 return FALSE;
808 if (!vc->gfx.ds) {
809 return FALSE;
811 if (!vc->gfx.surface) {
812 return FALSE;
815 gd_update_monitor_refresh_rate(vc, vc->window ? vc->window : s->window);
817 fbw = surface_width(vc->gfx.ds);
818 fbh = surface_height(vc->gfx.ds);
820 ww = gdk_window_get_width(gtk_widget_get_window(widget));
821 wh = gdk_window_get_height(gtk_widget_get_window(widget));
823 if (s->full_screen) {
824 vc->gfx.scale_x = (double)ww / fbw;
825 vc->gfx.scale_y = (double)wh / fbh;
826 } else if (s->free_scale) {
827 double sx, sy;
829 sx = (double)ww / fbw;
830 sy = (double)wh / fbh;
832 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
835 fbw *= vc->gfx.scale_x;
836 fbh *= vc->gfx.scale_y;
838 mx = my = 0;
839 if (ww > fbw) {
840 mx = (ww - fbw) / 2;
842 if (wh > fbh) {
843 my = (wh - fbh) / 2;
846 cairo_rectangle(cr, 0, 0, ww, wh);
848 /* Optionally cut out the inner area where the pixmap
849 will be drawn. This avoids 'flashing' since we're
850 not double-buffering. Note we're using the undocumented
851 behaviour of drawing the rectangle from right to left
852 to cut out the whole */
853 cairo_rectangle(cr, mx + fbw, my,
854 -1 * fbw, fbh);
855 cairo_fill(cr);
857 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
858 cairo_set_source_surface(cr, vc->gfx.surface,
859 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
860 cairo_paint(cr);
862 return TRUE;
865 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
866 void *opaque)
868 VirtualConsole *vc = opaque;
869 GtkDisplayState *s = vc->s;
870 GdkWindow *window;
871 int x, y;
872 int mx, my;
873 int fbh, fbw;
874 int ww, wh, ws;
876 if (!vc->gfx.ds) {
877 return TRUE;
880 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
881 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
883 window = gtk_widget_get_window(vc->gfx.drawing_area);
884 ww = gdk_window_get_width(window);
885 wh = gdk_window_get_height(window);
886 ws = gdk_window_get_scale_factor(window);
888 mx = my = 0;
889 if (ww > fbw) {
890 mx = (ww - fbw) / 2;
892 if (wh > fbh) {
893 my = (wh - fbh) / 2;
896 x = (motion->x - mx) / vc->gfx.scale_x * ws;
897 y = (motion->y - my) / vc->gfx.scale_y * ws;
899 if (qemu_input_is_absolute()) {
900 if (x < 0 || y < 0 ||
901 x >= surface_width(vc->gfx.ds) ||
902 y >= surface_height(vc->gfx.ds)) {
903 return TRUE;
905 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
906 0, surface_width(vc->gfx.ds));
907 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
908 0, surface_height(vc->gfx.ds));
909 qemu_input_event_sync();
910 } else if (s->last_set && s->ptr_owner == vc) {
911 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
912 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
913 qemu_input_event_sync();
915 s->last_x = x;
916 s->last_y = y;
917 s->last_set = TRUE;
919 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
920 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
921 GdkDisplay *dpy = gtk_widget_get_display(widget);
922 GdkWindow *win = gtk_widget_get_window(widget);
923 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
924 GdkRectangle geometry;
926 int x = (int)motion->x_root;
927 int y = (int)motion->y_root;
929 gdk_monitor_get_geometry(monitor, &geometry);
931 /* In relative mode check to see if client pointer hit
932 * one of the monitor edges, and if so move it back to the
933 * center of the monitor. This is important because the pointer
934 * in the server doesn't correspond 1-for-1, and so
935 * may still be only half way across the screen. Without
936 * this warp, the server pointer would thus appear to hit
937 * an invisible wall */
938 if (x <= geometry.x || x - geometry.x >= geometry.width - 1 ||
939 y <= geometry.y || y - geometry.y >= geometry.height - 1) {
940 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
941 x = geometry.x + geometry.width / 2;
942 y = geometry.y + geometry.height / 2;
944 gdk_device_warp(dev, screen, x, y);
945 s->last_set = FALSE;
946 return FALSE;
949 return TRUE;
952 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
953 void *opaque)
955 VirtualConsole *vc = opaque;
956 GtkDisplayState *s = vc->s;
957 InputButton btn;
959 /* implicitly grab the input at the first click in the relative mode */
960 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
961 !qemu_input_is_absolute() && s->ptr_owner != vc) {
962 if (!vc->window) {
963 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
964 TRUE);
965 } else {
966 gd_grab_pointer(vc, "relative-mode-click");
968 return TRUE;
971 if (button->button == 1) {
972 btn = INPUT_BUTTON_LEFT;
973 } else if (button->button == 2) {
974 btn = INPUT_BUTTON_MIDDLE;
975 } else if (button->button == 3) {
976 btn = INPUT_BUTTON_RIGHT;
977 } else if (button->button == 8) {
978 btn = INPUT_BUTTON_SIDE;
979 } else if (button->button == 9) {
980 btn = INPUT_BUTTON_EXTRA;
981 } else {
982 return TRUE;
985 if (button->type == GDK_2BUTTON_PRESS || button->type == GDK_3BUTTON_PRESS) {
986 return TRUE;
989 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
990 button->type == GDK_BUTTON_PRESS);
991 qemu_input_event_sync();
992 return TRUE;
995 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
996 void *opaque)
998 VirtualConsole *vc = opaque;
999 InputButton btn_vertical;
1000 InputButton btn_horizontal;
1001 bool has_vertical = false;
1002 bool has_horizontal = false;
1004 if (scroll->direction == GDK_SCROLL_UP) {
1005 btn_vertical = INPUT_BUTTON_WHEEL_UP;
1006 has_vertical = true;
1007 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1008 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
1009 has_vertical = true;
1010 } else if (scroll->direction == GDK_SCROLL_LEFT) {
1011 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1012 has_horizontal = true;
1013 } else if (scroll->direction == GDK_SCROLL_RIGHT) {
1014 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1015 has_horizontal = true;
1016 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1017 gdouble delta_x, delta_y;
1018 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1019 &delta_x, &delta_y)) {
1020 return TRUE;
1023 if (delta_y > 0) {
1024 btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
1025 has_vertical = true;
1026 } else if (delta_y < 0) {
1027 btn_vertical = INPUT_BUTTON_WHEEL_UP;
1028 has_vertical = true;
1029 } else if (delta_x > 0) {
1030 btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
1031 has_horizontal = true;
1032 } else if (delta_x < 0) {
1033 btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
1034 has_horizontal = true;
1035 } else {
1036 return TRUE;
1038 } else {
1039 return TRUE;
1042 if (has_vertical) {
1043 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, true);
1044 qemu_input_event_sync();
1045 qemu_input_queue_btn(vc->gfx.dcl.con, btn_vertical, false);
1046 qemu_input_event_sync();
1049 if (has_horizontal) {
1050 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, true);
1051 qemu_input_event_sync();
1052 qemu_input_queue_btn(vc->gfx.dcl.con, btn_horizontal, false);
1053 qemu_input_event_sync();
1056 return TRUE;
1060 static const guint16 *gd_get_keymap(size_t *maplen)
1062 GdkDisplay *dpy = gdk_display_get_default();
1064 #ifdef GDK_WINDOWING_X11
1065 if (GDK_IS_X11_DISPLAY(dpy)) {
1066 trace_gd_keymap_windowing("x11");
1067 return qemu_xkeymap_mapping_table(
1068 gdk_x11_display_get_xdisplay(dpy), maplen);
1070 #endif
1072 #ifdef GDK_WINDOWING_WAYLAND
1073 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1074 trace_gd_keymap_windowing("wayland");
1075 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1076 return qemu_input_map_xorgevdev_to_qcode;
1078 #endif
1080 #ifdef GDK_WINDOWING_WIN32
1081 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1082 trace_gd_keymap_windowing("win32");
1083 *maplen = qemu_input_map_atset1_to_qcode_len;
1084 return qemu_input_map_atset1_to_qcode;
1086 #endif
1088 #ifdef GDK_WINDOWING_QUARTZ
1089 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1090 trace_gd_keymap_windowing("quartz");
1091 *maplen = qemu_input_map_osx_to_qcode_len;
1092 return qemu_input_map_osx_to_qcode;
1094 #endif
1096 #ifdef GDK_WINDOWING_BROADWAY
1097 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1098 trace_gd_keymap_windowing("broadway");
1099 g_warning("experimental: using broadway, x11 virtual keysym\n"
1100 "mapping - with very limited support. See also\n"
1101 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1102 *maplen = qemu_input_map_x11_to_qcode_len;
1103 return qemu_input_map_x11_to_qcode;
1105 #endif
1107 g_warning("Unsupported GDK Windowing platform.\n"
1108 "Disabling extended keycode tables.\n"
1109 "Please report to qemu-devel@nongnu.org\n"
1110 "including the following information:\n"
1111 "\n"
1112 " - Operating system\n"
1113 " - GDK Windowing system build\n");
1114 return NULL;
1118 static int gd_map_keycode(int scancode)
1120 if (!keycode_map) {
1121 return 0;
1123 if (scancode > keycode_maplen) {
1124 return 0;
1127 return keycode_map[scancode];
1130 static int gd_get_keycode(GdkEventKey *key)
1132 #ifdef G_OS_WIN32
1133 int scancode = gdk_event_get_scancode((GdkEvent *)key);
1135 /* translate Windows native scancodes to atset1 keycodes */
1136 switch (scancode & (KF_EXTENDED | 0xff)) {
1137 case 0x145: /* NUMLOCK */
1138 return scancode & 0xff;
1141 return scancode & KF_EXTENDED ?
1142 0xe000 | (scancode & 0xff) : scancode & 0xff;
1144 #else
1145 return key->hardware_keycode;
1146 #endif
1149 static gboolean gd_text_key_down(GtkWidget *widget,
1150 GdkEventKey *key, void *opaque)
1152 VirtualConsole *vc = opaque;
1153 QemuConsole *con = vc->gfx.dcl.con;
1155 if (key->keyval == GDK_KEY_Delete) {
1156 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE, false);
1157 } else if (key->length) {
1158 kbd_put_string_console(con, key->string, key->length);
1159 } else {
1160 int qcode = gd_map_keycode(gd_get_keycode(key));
1161 kbd_put_qcode_console(con, qcode, false);
1163 return TRUE;
1166 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1168 VirtualConsole *vc = opaque;
1169 int keycode, qcode;
1171 #ifdef G_OS_WIN32
1172 /* on windows, we ought to ignore the reserved key event? */
1173 if (key->hardware_keycode == 0xff)
1174 return false;
1176 if (!vc->s->kbd_owner) {
1177 if (key->hardware_keycode == VK_LWIN ||
1178 key->hardware_keycode == VK_RWIN) {
1179 return FALSE;
1182 #endif
1184 if (key->keyval == GDK_KEY_Pause
1185 #ifdef G_OS_WIN32
1186 /* for some reason GDK does not fill keyval for VK_PAUSE
1187 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1189 || key->hardware_keycode == VK_PAUSE
1190 #endif
1192 qkbd_state_key_event(vc->gfx.kbd, Q_KEY_CODE_PAUSE,
1193 key->type == GDK_KEY_PRESS);
1194 return TRUE;
1197 keycode = gd_get_keycode(key);
1198 qcode = gd_map_keycode(keycode);
1200 trace_gd_key_event(vc->label, keycode, qcode,
1201 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1203 qkbd_state_key_event(vc->gfx.kbd, qcode,
1204 key->type == GDK_KEY_PRESS);
1206 return TRUE;
1209 static gboolean gd_grab_broken_event(GtkWidget *widget,
1210 GdkEventGrabBroken *event, void *opaque)
1212 #ifdef CONFIG_WIN32
1214 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1215 * key combination leaves all three keys in a stuck condition. We use
1216 * the grab-broken-event to release all keys.
1218 if (event->keyboard) {
1219 VirtualConsole *vc = opaque;
1220 GtkDisplayState *s = vc->s;
1222 gtk_release_modifiers(s);
1224 #endif
1225 return TRUE;
1228 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1230 if (event->type == GDK_MOTION_NOTIFY) {
1231 return gd_motion_event(widget, &event->motion, opaque);
1233 return FALSE;
1236 /** Window Menu Actions **/
1238 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1240 GtkDisplayState *s = opaque;
1242 if (s->external_pause_update) {
1243 return;
1245 if (runstate_is_running()) {
1246 qmp_stop(NULL);
1247 } else {
1248 qmp_cont(NULL);
1252 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1254 qmp_system_reset(NULL);
1257 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1259 qmp_system_powerdown(NULL);
1262 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1264 qmp_quit(NULL);
1267 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1269 GtkDisplayState *s = opaque;
1270 VirtualConsole *vc = gd_vc_find_by_menu(s);
1271 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1272 gint page;
1274 gtk_release_modifiers(s);
1275 if (vc) {
1276 page = gtk_notebook_page_num(nb, vc->tab_item);
1277 gtk_notebook_set_current_page(nb, page);
1278 gtk_widget_grab_focus(vc->focus);
1282 static void gd_accel_switch_vc(void *opaque)
1284 VirtualConsole *vc = opaque;
1286 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1289 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1291 GtkDisplayState *s = opaque;
1292 VirtualConsole *vc = gd_vc_find_current(s);
1294 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1295 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1296 } else {
1297 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1299 gd_update_windowsize(vc);
1302 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1303 void *opaque)
1305 VirtualConsole *vc = opaque;
1306 GtkDisplayState *s = vc->s;
1308 gtk_widget_set_sensitive(vc->menu_item, true);
1309 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1310 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1311 vc->tab_item, vc->label);
1312 gtk_widget_destroy(vc->window);
1313 vc->window = NULL;
1314 #if defined(CONFIG_OPENGL)
1315 if (vc->gfx.esurface) {
1316 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1317 vc->gfx.esurface = NULL;
1319 if (vc->gfx.ectx) {
1320 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1321 vc->gfx.ectx = NULL;
1323 #endif
1324 return TRUE;
1327 static gboolean gd_win_grab(void *opaque)
1329 VirtualConsole *vc = opaque;
1331 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1332 if (vc->s->ptr_owner) {
1333 gd_ungrab_pointer(vc->s);
1334 } else {
1335 gd_grab_pointer(vc, "user-request-detached-tab");
1337 return TRUE;
1340 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1342 GtkDisplayState *s = opaque;
1343 VirtualConsole *vc = gd_vc_find_current(s);
1345 if (vc->type == GD_VC_GFX &&
1346 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1347 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1348 FALSE);
1350 if (!vc->window) {
1351 gtk_widget_set_sensitive(vc->menu_item, false);
1352 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1353 #if defined(CONFIG_OPENGL)
1354 if (vc->gfx.esurface) {
1355 eglDestroySurface(qemu_egl_display, vc->gfx.esurface);
1356 vc->gfx.esurface = NULL;
1358 if (vc->gfx.esurface) {
1359 eglDestroyContext(qemu_egl_display, vc->gfx.ectx);
1360 vc->gfx.ectx = NULL;
1362 #endif
1363 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1365 g_signal_connect(vc->window, "delete-event",
1366 G_CALLBACK(gd_tab_window_close), vc);
1367 gtk_widget_show_all(vc->window);
1369 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1370 GtkAccelGroup *ag = gtk_accel_group_new();
1371 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1373 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1374 vc, NULL);
1375 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1378 gd_update_geometry_hints(vc);
1379 gd_update_caption(s);
1383 static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
1385 GtkDisplayState *s = opaque;
1386 VirtualConsole *vc = gd_vc_find_current(s);
1388 if (s->full_screen) {
1389 return;
1392 if (gtk_check_menu_item_get_active(
1393 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1394 gtk_widget_show(s->menu_bar);
1395 } else {
1396 gtk_widget_hide(s->menu_bar);
1398 gd_update_windowsize(vc);
1401 static void gd_accel_show_menubar(void *opaque)
1403 GtkDisplayState *s = opaque;
1404 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
1407 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1409 GtkDisplayState *s = opaque;
1410 VirtualConsole *vc = gd_vc_find_current(s);
1412 if (!s->full_screen) {
1413 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1414 gtk_widget_hide(s->menu_bar);
1415 if (vc->type == GD_VC_GFX) {
1416 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1418 gtk_window_fullscreen(GTK_WINDOW(s->window));
1419 s->full_screen = TRUE;
1420 } else {
1421 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1422 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1423 if (gtk_check_menu_item_get_active(
1424 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1425 gtk_widget_show(s->menu_bar);
1427 s->full_screen = FALSE;
1428 if (vc->type == GD_VC_GFX) {
1429 vc->gfx.scale_x = 1.0;
1430 vc->gfx.scale_y = 1.0;
1431 gd_update_windowsize(vc);
1435 gd_update_cursor(vc);
1438 static void gd_accel_full_screen(void *opaque)
1440 GtkDisplayState *s = opaque;
1441 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1444 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1446 GtkDisplayState *s = opaque;
1447 VirtualConsole *vc = gd_vc_find_current(s);
1449 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1450 FALSE);
1452 vc->gfx.scale_x += VC_SCALE_STEP;
1453 vc->gfx.scale_y += VC_SCALE_STEP;
1455 gd_update_windowsize(vc);
1458 static void gd_accel_zoom_in(void *opaque)
1460 GtkDisplayState *s = opaque;
1461 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1464 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1466 GtkDisplayState *s = opaque;
1467 VirtualConsole *vc = gd_vc_find_current(s);
1469 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1470 FALSE);
1472 vc->gfx.scale_x -= VC_SCALE_STEP;
1473 vc->gfx.scale_y -= VC_SCALE_STEP;
1475 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1476 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1478 gd_update_windowsize(vc);
1481 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1483 GtkDisplayState *s = opaque;
1484 VirtualConsole *vc = gd_vc_find_current(s);
1486 vc->gfx.scale_x = 1.0;
1487 vc->gfx.scale_y = 1.0;
1489 gd_update_windowsize(vc);
1492 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1494 GtkDisplayState *s = opaque;
1495 VirtualConsole *vc = gd_vc_find_current(s);
1497 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1498 s->free_scale = TRUE;
1499 } else {
1500 s->free_scale = FALSE;
1501 vc->gfx.scale_x = 1.0;
1502 vc->gfx.scale_y = 1.0;
1505 gd_update_windowsize(vc);
1506 gd_update_full_redraw(vc);
1509 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1511 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1512 GdkSeat *seat = gdk_display_get_default_seat(display);
1513 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1514 GdkSeatCapabilities caps = 0;
1515 GdkCursor *cursor = NULL;
1517 if (kbd) {
1518 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1520 if (ptr) {
1521 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1522 cursor = vc->s->null_cursor;
1525 if (caps) {
1526 gdk_seat_grab(seat, window, caps, false, cursor,
1527 NULL, NULL, NULL);
1528 } else {
1529 gdk_seat_ungrab(seat);
1533 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1535 if (vc->s->kbd_owner) {
1536 if (vc->s->kbd_owner == vc) {
1537 return;
1538 } else {
1539 gd_ungrab_keyboard(vc->s);
1543 win32_kbd_set_grab(true);
1544 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1545 vc->s->kbd_owner = vc;
1546 gd_update_caption(vc->s);
1547 trace_gd_grab(vc->label, "kbd", reason);
1550 static void gd_ungrab_keyboard(GtkDisplayState *s)
1552 VirtualConsole *vc = s->kbd_owner;
1554 if (vc == NULL) {
1555 return;
1557 s->kbd_owner = NULL;
1559 win32_kbd_set_grab(false);
1560 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1561 gd_update_caption(s);
1562 trace_gd_ungrab(vc->label, "kbd");
1565 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1567 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1569 if (vc->s->ptr_owner) {
1570 if (vc->s->ptr_owner == vc) {
1571 return;
1572 } else {
1573 gd_ungrab_pointer(vc->s);
1577 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1578 gdk_device_get_position(gd_get_pointer(display),
1579 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1580 vc->s->ptr_owner = vc;
1581 gd_update_caption(vc->s);
1582 trace_gd_grab(vc->label, "ptr", reason);
1585 static void gd_ungrab_pointer(GtkDisplayState *s)
1587 VirtualConsole *vc = s->ptr_owner;
1588 GdkDisplay *display;
1590 if (vc == NULL) {
1591 return;
1593 s->ptr_owner = NULL;
1595 display = gtk_widget_get_display(vc->gfx.drawing_area);
1596 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1597 gdk_device_warp(gd_get_pointer(display),
1598 gtk_widget_get_screen(vc->gfx.drawing_area),
1599 vc->s->grab_x_root, vc->s->grab_y_root);
1600 gd_update_caption(s);
1601 trace_gd_ungrab(vc->label, "ptr");
1604 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1606 GtkDisplayState *s = opaque;
1607 VirtualConsole *vc = gd_vc_find_current(s);
1609 if (gd_is_grab_active(s)) {
1610 gd_grab_keyboard(vc, "user-request-main-window");
1611 gd_grab_pointer(vc, "user-request-main-window");
1612 } else {
1613 gd_ungrab_keyboard(s);
1614 gd_ungrab_pointer(s);
1617 gd_update_cursor(vc);
1620 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1621 gpointer data)
1623 GtkDisplayState *s = data;
1624 VirtualConsole *vc;
1625 gboolean on_vga;
1627 if (!gtk_widget_get_realized(s->notebook)) {
1628 return;
1631 vc = gd_vc_find_by_page(s, arg2);
1632 if (!vc) {
1633 return;
1635 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1636 TRUE);
1637 on_vga = (vc->type == GD_VC_GFX &&
1638 qemu_console_is_graphic(vc->gfx.dcl.con));
1639 if (!on_vga) {
1640 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1641 FALSE);
1642 } else if (s->full_screen) {
1643 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1644 TRUE);
1646 gtk_widget_set_sensitive(s->grab_item, on_vga);
1647 #ifdef CONFIG_VTE
1648 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1649 #endif
1651 gd_update_windowsize(vc);
1652 gd_update_cursor(vc);
1655 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1656 gpointer opaque)
1658 VirtualConsole *vc = opaque;
1659 GtkDisplayState *s = vc->s;
1661 if (gd_grab_on_hover(s)) {
1662 gd_grab_keyboard(vc, "grab-on-hover");
1664 return TRUE;
1667 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1668 gpointer opaque)
1670 VirtualConsole *vc = opaque;
1671 GtkDisplayState *s = vc->s;
1673 if (gd_grab_on_hover(s)) {
1674 gd_ungrab_keyboard(s);
1676 return TRUE;
1679 static gboolean gd_focus_in_event(GtkWidget *widget,
1680 GdkEventFocus *event, gpointer opaque)
1682 VirtualConsole *vc = opaque;
1684 win32_kbd_set_window(gd_win32_get_hwnd(vc));
1685 return TRUE;
1688 static gboolean gd_focus_out_event(GtkWidget *widget,
1689 GdkEventFocus *event, gpointer opaque)
1691 VirtualConsole *vc = opaque;
1692 GtkDisplayState *s = vc->s;
1694 win32_kbd_set_window(NULL);
1695 gtk_release_modifiers(s);
1696 return TRUE;
1699 static gboolean gd_configure(GtkWidget *widget,
1700 GdkEventConfigure *cfg, gpointer opaque)
1702 VirtualConsole *vc = opaque;
1704 gd_set_ui_size(vc, cfg->width, cfg->height);
1705 return FALSE;
1708 /** Virtual Console Callbacks **/
1710 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1711 int idx, GSList *group, GtkWidget *view_menu)
1713 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1714 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1715 HOTKEY_MODIFIERS, 0,
1716 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1717 gtk_accel_label_set_accel(
1718 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1719 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1721 g_signal_connect(vc->menu_item, "activate",
1722 G_CALLBACK(gd_menu_switch_vc), s);
1723 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1725 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1728 #if defined(CONFIG_VTE)
1729 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1731 GtkDisplayState *s = opaque;
1732 VirtualConsole *vc = gd_vc_find_current(s);
1734 #if VTE_CHECK_VERSION(0, 50, 0)
1735 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1736 VTE_FORMAT_TEXT);
1737 #else
1738 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1739 #endif
1742 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1744 VirtualConsole *vc = opaque;
1746 if (gtk_adjustment_get_upper(adjustment) >
1747 gtk_adjustment_get_page_size(adjustment)) {
1748 gtk_widget_show(vc->vte.scrollbar);
1749 } else {
1750 gtk_widget_hide(vc->vte.scrollbar);
1754 static void gd_vc_send_chars(VirtualConsole *vc)
1756 uint32_t len, avail;
1758 len = qemu_chr_be_can_write(vc->vte.chr);
1759 avail = fifo8_num_used(&vc->vte.out_fifo);
1760 while (len > 0 && avail > 0) {
1761 const uint8_t *buf;
1762 uint32_t size;
1764 buf = fifo8_pop_buf(&vc->vte.out_fifo, MIN(len, avail), &size);
1765 qemu_chr_be_write(vc->vte.chr, buf, size);
1766 len = qemu_chr_be_can_write(vc->vte.chr);
1767 avail -= size;
1771 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1773 VCChardev *vcd = VC_CHARDEV(chr);
1774 VirtualConsole *vc = vcd->console;
1776 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1777 return len;
1780 static void gd_vc_chr_accept_input(Chardev *chr)
1782 VCChardev *vcd = VC_CHARDEV(chr);
1783 VirtualConsole *vc = vcd->console;
1785 gd_vc_send_chars(vc);
1788 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1790 VCChardev *vcd = VC_CHARDEV(chr);
1791 VirtualConsole *vc = vcd->console;
1793 if (vc) {
1794 vc->vte.echo = echo;
1795 } else {
1796 vcd->echo = echo;
1800 static int nb_vcs;
1801 static Chardev *vcs[MAX_VCS];
1802 static void gd_vc_open(Chardev *chr,
1803 ChardevBackend *backend,
1804 bool *be_opened,
1805 Error **errp)
1807 if (nb_vcs == MAX_VCS) {
1808 error_setg(errp, "Maximum number of consoles reached");
1809 return;
1812 vcs[nb_vcs++] = chr;
1814 /* console/chardev init sometimes completes elsewhere in a 2nd
1815 * stage, so defer OPENED events until they are fully initialized
1817 *be_opened = false;
1820 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1822 ChardevClass *cc = CHARDEV_CLASS(oc);
1824 cc->parse = qemu_chr_parse_vc;
1825 cc->open = gd_vc_open;
1826 cc->chr_write = gd_vc_chr_write;
1827 cc->chr_accept_input = gd_vc_chr_accept_input;
1828 cc->chr_set_echo = gd_vc_chr_set_echo;
1831 static const TypeInfo char_gd_vc_type_info = {
1832 .name = TYPE_CHARDEV_VC,
1833 .parent = TYPE_CHARDEV,
1834 .instance_size = sizeof(VCChardev),
1835 .class_init = char_gd_vc_class_init,
1838 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1839 gpointer user_data)
1841 VirtualConsole *vc = user_data;
1842 uint32_t free;
1844 if (vc->vte.echo) {
1845 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1846 int i;
1847 for (i = 0; i < size; i++) {
1848 uint8_t c = text[i];
1849 if (c >= 128 || isprint(c)) {
1850 /* 8-bit characters are considered printable. */
1851 vte_terminal_feed(term, &text[i], 1);
1852 } else if (c == '\r' || c == '\n') {
1853 vte_terminal_feed(term, "\r\n", 2);
1854 } else {
1855 char ctrl[2] = { '^', 0};
1856 ctrl[1] = text[i] ^ 64;
1857 vte_terminal_feed(term, ctrl, 2);
1862 free = fifo8_num_free(&vc->vte.out_fifo);
1863 fifo8_push_all(&vc->vte.out_fifo, (uint8_t *)text, MIN(free, size));
1864 gd_vc_send_chars(vc);
1866 return TRUE;
1869 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1870 Chardev *chr, int idx,
1871 GSList *group, GtkWidget *view_menu)
1873 char buffer[32];
1874 GtkWidget *box;
1875 GtkWidget *scrollbar;
1876 GtkAdjustment *vadjustment;
1877 VCChardev *vcd = VC_CHARDEV(chr);
1879 vc->s = s;
1880 vc->vte.echo = vcd->echo;
1881 vc->vte.chr = chr;
1882 fifo8_create(&vc->vte.out_fifo, 4096);
1883 vcd->console = vc;
1885 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1886 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1887 ? vc->vte.chr->label : buffer);
1888 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1890 vc->vte.terminal = vte_terminal_new();
1891 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1893 /* The documentation says that the default is UTF-8, but actually it is
1894 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1895 * VTE 0.54 (only UTF-8 is supported now). */
1896 #if !VTE_CHECK_VERSION(0, 54, 0)
1897 #if VTE_CHECK_VERSION(0, 38, 0)
1898 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1899 #else
1900 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1901 #endif
1902 #endif
1904 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1905 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1906 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1908 #if VTE_CHECK_VERSION(0, 28, 0)
1909 vadjustment = gtk_scrollable_get_vadjustment
1910 (GTK_SCROLLABLE(vc->vte.terminal));
1911 #else
1912 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1913 #endif
1915 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1916 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1918 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1919 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1921 vc->vte.box = box;
1922 vc->vte.scrollbar = scrollbar;
1924 g_signal_connect(vadjustment, "changed",
1925 G_CALLBACK(gd_vc_adjustment_changed), vc);
1927 vc->type = GD_VC_VTE;
1928 vc->tab_item = box;
1929 vc->focus = vc->vte.terminal;
1930 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1931 gtk_label_new(vc->label));
1933 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1935 return group;
1938 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1939 GtkWidget *view_menu)
1941 int i;
1943 for (i = 0; i < nb_vcs; i++) {
1944 VirtualConsole *vc = &s->vc[s->nb_vcs];
1945 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1946 s->nb_vcs++;
1949 #endif /* CONFIG_VTE */
1951 /** Window Creation **/
1953 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1955 g_signal_connect(vc->gfx.drawing_area, "draw",
1956 G_CALLBACK(gd_draw_event), vc);
1957 #if defined(CONFIG_OPENGL)
1958 if (gtk_use_gl_area) {
1959 /* wire up GtkGlArea events */
1960 g_signal_connect(vc->gfx.drawing_area, "render",
1961 G_CALLBACK(gd_render_event), vc);
1962 g_signal_connect(vc->gfx.drawing_area, "resize",
1963 G_CALLBACK(gd_resize_event), vc);
1965 #endif
1966 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1967 g_signal_connect(vc->gfx.drawing_area, "event",
1968 G_CALLBACK(gd_event), vc);
1969 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1970 G_CALLBACK(gd_button_event), vc);
1971 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1972 G_CALLBACK(gd_button_event), vc);
1973 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1974 G_CALLBACK(gd_scroll_event), vc);
1975 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1976 G_CALLBACK(gd_key_event), vc);
1977 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1978 G_CALLBACK(gd_key_event), vc);
1980 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1981 G_CALLBACK(gd_enter_event), vc);
1982 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1983 G_CALLBACK(gd_leave_event), vc);
1984 g_signal_connect(vc->gfx.drawing_area, "focus-in-event",
1985 G_CALLBACK(gd_focus_in_event), vc);
1986 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1987 G_CALLBACK(gd_focus_out_event), vc);
1988 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1989 G_CALLBACK(gd_configure), vc);
1990 g_signal_connect(vc->gfx.drawing_area, "grab-broken-event",
1991 G_CALLBACK(gd_grab_broken_event), vc);
1992 } else {
1993 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1994 G_CALLBACK(gd_text_key_down), vc);
1998 static void gd_connect_signals(GtkDisplayState *s)
2000 g_signal_connect(s->show_tabs_item, "activate",
2001 G_CALLBACK(gd_menu_show_tabs), s);
2002 g_signal_connect(s->untabify_item, "activate",
2003 G_CALLBACK(gd_menu_untabify), s);
2004 g_signal_connect(s->show_menubar_item, "activate",
2005 G_CALLBACK(gd_menu_show_menubar), s);
2007 g_signal_connect(s->window, "delete-event",
2008 G_CALLBACK(gd_window_close), s);
2010 g_signal_connect(s->pause_item, "activate",
2011 G_CALLBACK(gd_menu_pause), s);
2012 g_signal_connect(s->reset_item, "activate",
2013 G_CALLBACK(gd_menu_reset), s);
2014 g_signal_connect(s->powerdown_item, "activate",
2015 G_CALLBACK(gd_menu_powerdown), s);
2016 g_signal_connect(s->quit_item, "activate",
2017 G_CALLBACK(gd_menu_quit), s);
2018 #if defined(CONFIG_VTE)
2019 g_signal_connect(s->copy_item, "activate",
2020 G_CALLBACK(gd_menu_copy), s);
2021 #endif
2022 g_signal_connect(s->full_screen_item, "activate",
2023 G_CALLBACK(gd_menu_full_screen), s);
2024 g_signal_connect(s->zoom_in_item, "activate",
2025 G_CALLBACK(gd_menu_zoom_in), s);
2026 g_signal_connect(s->zoom_out_item, "activate",
2027 G_CALLBACK(gd_menu_zoom_out), s);
2028 g_signal_connect(s->zoom_fixed_item, "activate",
2029 G_CALLBACK(gd_menu_zoom_fixed), s);
2030 g_signal_connect(s->zoom_fit_item, "activate",
2031 G_CALLBACK(gd_menu_zoom_fit), s);
2032 g_signal_connect(s->grab_item, "activate",
2033 G_CALLBACK(gd_menu_grab_input), s);
2034 g_signal_connect(s->notebook, "switch-page",
2035 G_CALLBACK(gd_change_page), s);
2038 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2040 GtkWidget *machine_menu;
2041 GtkWidget *separator;
2043 machine_menu = gtk_menu_new();
2044 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2046 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2047 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2049 separator = gtk_separator_menu_item_new();
2050 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2052 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2053 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2055 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2056 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2058 separator = gtk_separator_menu_item_new();
2059 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2061 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2062 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2063 "<QEMU>/Machine/Quit");
2064 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2065 GDK_KEY_q, HOTKEY_MODIFIERS);
2066 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2068 return machine_menu;
2071 #if defined(CONFIG_OPENGL)
2072 static void gl_area_realize(GtkGLArea *area, VirtualConsole *vc)
2074 gtk_gl_area_make_current(area);
2075 qemu_egl_display = eglGetCurrentDisplay();
2076 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2077 if (!vc->gfx.has_dmabuf) {
2078 error_report("GtkGLArea console lacks DMABUF support.");
2081 #endif
2083 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2084 QemuConsole *con, int idx,
2085 GSList *group, GtkWidget *view_menu)
2087 bool zoom_to_fit = false;
2089 vc->label = qemu_console_get_label(con);
2090 vc->s = s;
2091 vc->gfx.scale_x = 1.0;
2092 vc->gfx.scale_y = 1.0;
2094 #if defined(CONFIG_OPENGL)
2095 if (display_opengl) {
2096 if (gtk_use_gl_area) {
2097 vc->gfx.drawing_area = gtk_gl_area_new();
2098 g_signal_connect(vc->gfx.drawing_area, "realize",
2099 G_CALLBACK(gl_area_realize), vc);
2100 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2101 vc->gfx.dgc.ops = &gl_area_ctx_ops;
2102 } else {
2103 #ifdef CONFIG_X11
2104 vc->gfx.drawing_area = gtk_drawing_area_new();
2106 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2107 * It is required for opengl rendering on X11 though. A
2108 * proper replacement (native opengl support) is only
2109 * available in 3.16+. Silence the warning if possible.
2111 #pragma GCC diagnostic push
2112 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2113 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2114 #pragma GCC diagnostic pop
2115 vc->gfx.dcl.ops = &dcl_egl_ops;
2116 vc->gfx.dgc.ops = &egl_ctx_ops;
2117 vc->gfx.has_dmabuf = qemu_egl_has_dmabuf();
2118 #else
2119 abort();
2120 #endif
2122 } else
2123 #endif
2125 vc->gfx.drawing_area = gtk_drawing_area_new();
2126 vc->gfx.dcl.ops = &dcl_ops;
2130 gtk_widget_add_events(vc->gfx.drawing_area,
2131 GDK_POINTER_MOTION_MASK |
2132 GDK_BUTTON_PRESS_MASK |
2133 GDK_BUTTON_RELEASE_MASK |
2134 GDK_BUTTON_MOTION_MASK |
2135 GDK_ENTER_NOTIFY_MASK |
2136 GDK_LEAVE_NOTIFY_MASK |
2137 GDK_SCROLL_MASK |
2138 GDK_SMOOTH_SCROLL_MASK |
2139 GDK_KEY_PRESS_MASK);
2140 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2142 vc->type = GD_VC_GFX;
2143 vc->tab_item = vc->gfx.drawing_area;
2144 vc->focus = vc->gfx.drawing_area;
2145 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2146 vc->tab_item, gtk_label_new(vc->label));
2148 vc->gfx.kbd = qkbd_state_init(con);
2149 vc->gfx.dcl.con = con;
2151 if (display_opengl) {
2152 qemu_console_set_display_gl_ctx(con, &vc->gfx.dgc);
2154 register_displaychangelistener(&vc->gfx.dcl);
2156 gd_connect_vc_gfx_signals(vc);
2157 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2159 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2160 zoom_to_fit = true;
2162 if (s->opts->u.gtk.has_zoom_to_fit) {
2163 zoom_to_fit = s->opts->u.gtk.zoom_to_fit;
2165 if (zoom_to_fit) {
2166 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2167 s->free_scale = true;
2170 return group;
2173 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, DisplayOptions *opts)
2175 GSList *group = NULL;
2176 GtkWidget *view_menu;
2177 GtkWidget *separator;
2178 QemuConsole *con;
2179 int vc;
2181 view_menu = gtk_menu_new();
2182 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2184 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2186 #if defined(CONFIG_VTE)
2187 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2188 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2189 #endif
2191 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2192 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2193 gtk_accel_label_set_accel(
2194 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2195 GDK_KEY_f, HOTKEY_MODIFIERS);
2196 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2198 separator = gtk_separator_menu_item_new();
2199 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2201 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2202 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2203 "<QEMU>/View/Zoom In");
2204 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2205 HOTKEY_MODIFIERS);
2206 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2207 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2208 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2210 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2211 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2212 "<QEMU>/View/Zoom Out");
2213 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2214 HOTKEY_MODIFIERS);
2215 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2217 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2218 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2219 "<QEMU>/View/Zoom Fixed");
2220 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2221 HOTKEY_MODIFIERS);
2222 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2224 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2225 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2227 separator = gtk_separator_menu_item_new();
2228 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2230 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2231 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2233 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2234 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2235 "<QEMU>/View/Grab Input");
2236 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2237 HOTKEY_MODIFIERS);
2238 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2240 separator = gtk_separator_menu_item_new();
2241 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2243 /* gfx */
2244 for (vc = 0;; vc++) {
2245 con = qemu_console_lookup_by_index(vc);
2246 if (!con) {
2247 break;
2249 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2250 vc, group, view_menu);
2251 s->nb_vcs++;
2254 #if defined(CONFIG_VTE)
2255 /* vte */
2256 gd_vcs_init(s, group, view_menu);
2257 #endif
2259 separator = gtk_separator_menu_item_new();
2260 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2262 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2263 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2265 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2266 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2268 s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
2269 _("Show Menubar"));
2270 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
2271 !opts->u.gtk.has_show_menubar ||
2272 opts->u.gtk.show_menubar);
2273 gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
2274 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
2275 gtk_accel_label_set_accel(
2276 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
2277 GDK_KEY_m, HOTKEY_MODIFIERS);
2278 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
2280 return view_menu;
2283 static void gd_create_menus(GtkDisplayState *s, DisplayOptions *opts)
2285 GtkSettings *settings;
2287 s->accel_group = gtk_accel_group_new();
2288 s->machine_menu = gd_create_menu_machine(s);
2289 s->view_menu = gd_create_menu_view(s, opts);
2291 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2292 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2293 s->machine_menu);
2294 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2296 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2297 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2298 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2300 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2301 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2303 /* Disable the default "F10" menu shortcut. */
2304 settings = gtk_widget_get_settings(s->window);
2305 g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
2309 static gboolean gtkinit;
2311 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2313 VirtualConsole *vc;
2315 GtkDisplayState *s = g_malloc0(sizeof(*s));
2316 GdkDisplay *window_display;
2317 GtkIconTheme *theme;
2318 char *dir;
2320 if (!gtkinit) {
2321 fprintf(stderr, "gtk initialization failed\n");
2322 exit(1);
2324 assert(opts->type == DISPLAY_TYPE_GTK);
2325 s->opts = opts;
2327 theme = gtk_icon_theme_get_default();
2328 dir = get_relocated_path(CONFIG_QEMU_ICONDIR);
2329 gtk_icon_theme_prepend_search_path(theme, dir);
2330 g_free(dir);
2331 g_set_prgname("qemu");
2333 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2334 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2335 s->notebook = gtk_notebook_new();
2336 s->menu_bar = gtk_menu_bar_new();
2338 s->free_scale = FALSE;
2340 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2341 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2342 * printable, but without changing any of the character classes to make
2343 * sure that we don't accidentally break implicit assumptions. */
2344 setlocale(LC_MESSAGES, "");
2345 setlocale(LC_CTYPE, "C.UTF-8");
2346 dir = get_relocated_path(CONFIG_QEMU_LOCALEDIR);
2347 bindtextdomain("qemu", dir);
2348 g_free(dir);
2349 bind_textdomain_codeset("qemu", "UTF-8");
2350 textdomain("qemu");
2352 window_display = gtk_widget_get_display(s->window);
2353 if (s->opts->has_show_cursor && s->opts->show_cursor) {
2354 s->null_cursor = NULL; /* default pointer */
2355 } else {
2356 s->null_cursor = gdk_cursor_new_for_display(window_display,
2357 GDK_BLANK_CURSOR);
2360 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2361 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2362 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2364 gtk_window_set_icon_name(GTK_WINDOW(s->window), "qemu");
2366 gd_create_menus(s, opts);
2368 gd_connect_signals(s);
2370 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2371 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2373 gd_update_caption(s);
2375 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2376 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2378 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2380 gtk_widget_show_all(s->window);
2381 if (opts->u.gtk.has_show_menubar &&
2382 !opts->u.gtk.show_menubar) {
2383 gtk_widget_hide(s->menu_bar);
2386 vc = gd_vc_find_current(s);
2387 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2388 #ifdef CONFIG_VTE
2389 gtk_widget_set_sensitive(s->copy_item,
2390 vc && vc->type == GD_VC_VTE);
2391 #endif
2393 if (opts->has_full_screen &&
2394 opts->full_screen) {
2395 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2397 if (opts->u.gtk.has_grab_on_hover &&
2398 opts->u.gtk.grab_on_hover) {
2399 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2401 if (opts->u.gtk.has_show_tabs &&
2402 opts->u.gtk.show_tabs) {
2403 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_tabs_item));
2405 #ifdef CONFIG_GTK_CLIPBOARD
2406 gd_clipboard_init(s);
2407 #endif /* CONFIG_GTK_CLIPBOARD */
2410 static void early_gtk_display_init(DisplayOptions *opts)
2412 /* The QEMU code relies on the assumption that it's always run in
2413 * the C locale. Therefore it is not prepared to deal with
2414 * operations that produce different results depending on the
2415 * locale, such as printf's formatting of decimal numbers, and
2416 * possibly others.
2418 * Since GTK+ calls setlocale() by default -importing the locale
2419 * settings from the environment- we must prevent it from doing so
2420 * using gtk_disable_setlocale().
2422 * QEMU's GTK+ UI, however, _does_ have translations for some of
2423 * the menu items. As a trade-off between a functionally correct
2424 * QEMU and a fully internationalized UI we support importing
2425 * LC_MESSAGES from the environment (see the setlocale() call
2426 * earlier in this file). This allows us to display translated
2427 * messages leaving everything else untouched.
2429 gtk_disable_setlocale();
2430 gtkinit = gtk_init_check(NULL, NULL);
2431 if (!gtkinit) {
2432 /* don't exit yet, that'll break -help */
2433 return;
2436 assert(opts->type == DISPLAY_TYPE_GTK);
2437 if (opts->has_gl && opts->gl != DISPLAYGL_MODE_OFF) {
2438 #if defined(CONFIG_OPENGL)
2439 #if defined(GDK_WINDOWING_WAYLAND)
2440 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2441 gtk_use_gl_area = true;
2442 gtk_gl_area_init();
2443 } else
2444 #endif
2446 #ifdef CONFIG_X11
2447 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
2448 gtk_egl_init(mode);
2449 #endif
2451 #endif
2454 keycode_map = gd_get_keymap(&keycode_maplen);
2456 #if defined(CONFIG_VTE)
2457 type_register(&char_gd_vc_type_info);
2458 #endif
2461 static QemuDisplay qemu_display_gtk = {
2462 .type = DISPLAY_TYPE_GTK,
2463 .early_init = early_gtk_display_init,
2464 .init = gtk_display_init,
2467 static void register_gtk(void)
2469 qemu_display_register(&qemu_display_gtk);
2472 type_init(register_gtk);
2474 #ifdef CONFIG_OPENGL
2475 module_dep("ui-opengl");
2476 #endif