target/riscv/pmp: Raise exception if no PMP entry is configured
[qemu/ar7.git] / ui / gtk.c
blob26665cd2e657faff3f5cfcf86577ec4f980bde5f
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"
40 #include "ui/console.h"
41 #include "ui/gtk.h"
42 #ifdef G_OS_WIN32
43 #include <gdk/gdkwin32.h>
44 #endif
45 #include "ui/win32-kbd-hook.h"
47 #include <glib/gi18n.h>
48 #include <locale.h>
49 #if defined(CONFIG_VTE)
50 #include <vte/vte.h>
51 #endif
52 #include <math.h>
54 #include "trace.h"
55 #include "qemu/cutils.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 MAX_VCS 10
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 GtkDisplayState {
123 GtkWidget *window;
125 GtkWidget *menu_bar;
127 GtkAccelGroup *accel_group;
129 GtkWidget *machine_menu_item;
130 GtkWidget *machine_menu;
131 GtkWidget *pause_item;
132 GtkWidget *reset_item;
133 GtkWidget *powerdown_item;
134 GtkWidget *quit_item;
136 GtkWidget *view_menu_item;
137 GtkWidget *view_menu;
138 GtkWidget *full_screen_item;
139 GtkWidget *copy_item;
140 GtkWidget *zoom_in_item;
141 GtkWidget *zoom_out_item;
142 GtkWidget *zoom_fixed_item;
143 GtkWidget *zoom_fit_item;
144 GtkWidget *grab_item;
145 GtkWidget *grab_on_hover_item;
147 int nb_vcs;
148 VirtualConsole vc[MAX_VCS];
150 GtkWidget *show_tabs_item;
151 GtkWidget *untabify_item;
152 GtkWidget *show_menubar_item;
154 GtkWidget *vbox;
155 GtkWidget *notebook;
156 int button_mask;
157 gboolean last_set;
158 int last_x;
159 int last_y;
160 int grab_x_root;
161 int grab_y_root;
162 VirtualConsole *kbd_owner;
163 VirtualConsole *ptr_owner;
165 gboolean full_screen;
167 GdkCursor *null_cursor;
168 Notifier mouse_mode_notifier;
169 gboolean free_scale;
171 bool external_pause_update;
173 DisplayOptions *opts;
176 struct VCChardev {
177 Chardev parent;
178 VirtualConsole *console;
179 bool echo;
181 typedef struct VCChardev VCChardev;
183 #define TYPE_CHARDEV_VC "chardev-vc"
184 DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
185 TYPE_CHARDEV_VC)
187 bool gtk_use_gl_area;
189 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
190 static void gd_ungrab_pointer(GtkDisplayState *s);
191 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
192 static void gd_ungrab_keyboard(GtkDisplayState *s);
194 /** Utility Functions **/
196 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
198 VirtualConsole *vc;
199 gint i;
201 for (i = 0; i < s->nb_vcs; i++) {
202 vc = &s->vc[i];
203 if (gtk_check_menu_item_get_active
204 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
205 return vc;
208 return NULL;
211 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
213 VirtualConsole *vc;
214 gint i, p;
216 for (i = 0; i < s->nb_vcs; i++) {
217 vc = &s->vc[i];
218 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
219 if (p == page) {
220 return vc;
223 return NULL;
226 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
228 gint page;
230 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
231 return gd_vc_find_by_page(s, page);
234 static bool gd_is_grab_active(GtkDisplayState *s)
236 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
239 static bool gd_grab_on_hover(GtkDisplayState *s)
241 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
244 static void gd_update_cursor(VirtualConsole *vc)
246 GtkDisplayState *s = vc->s;
247 GdkWindow *window;
249 if (vc->type != GD_VC_GFX ||
250 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
251 return;
254 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
255 return;
258 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
259 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
260 gdk_window_set_cursor(window, s->null_cursor);
261 } else {
262 gdk_window_set_cursor(window, NULL);
266 static void gd_update_caption(GtkDisplayState *s)
268 const char *status = "";
269 gchar *prefix;
270 gchar *title;
271 const char *grab = "";
272 bool is_paused = !runstate_is_running();
273 int i;
275 if (qemu_name) {
276 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
277 } else {
278 prefix = g_strdup_printf("QEMU");
281 if (s->ptr_owner != NULL &&
282 s->ptr_owner->window == NULL) {
283 grab = _(" - Press Ctrl+Alt+G to release grab");
286 if (is_paused) {
287 status = _(" [Paused]");
289 s->external_pause_update = true;
290 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
291 is_paused);
292 s->external_pause_update = false;
294 title = g_strdup_printf("%s%s%s", prefix, status, grab);
295 gtk_window_set_title(GTK_WINDOW(s->window), title);
296 g_free(title);
298 for (i = 0; i < s->nb_vcs; i++) {
299 VirtualConsole *vc = &s->vc[i];
301 if (!vc->window) {
302 continue;
304 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
305 vc == s->kbd_owner ? " +kbd" : "",
306 vc == s->ptr_owner ? " +ptr" : "");
307 gtk_window_set_title(GTK_WINDOW(vc->window), title);
308 g_free(title);
311 g_free(prefix);
314 static void gd_update_geometry_hints(VirtualConsole *vc)
316 GtkDisplayState *s = vc->s;
317 GdkWindowHints mask = 0;
318 GdkGeometry geo = {};
319 GtkWidget *geo_widget = NULL;
320 GtkWindow *geo_window;
322 if (vc->type == GD_VC_GFX) {
323 if (!vc->gfx.ds) {
324 return;
326 if (s->free_scale) {
327 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
328 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
329 mask |= GDK_HINT_MIN_SIZE;
330 } else {
331 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
332 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
333 mask |= GDK_HINT_MIN_SIZE;
335 geo_widget = vc->gfx.drawing_area;
336 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
338 #if defined(CONFIG_VTE)
339 } else if (vc->type == GD_VC_VTE) {
340 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
341 GtkBorder padding = { 0 };
343 #if VTE_CHECK_VERSION(0, 37, 0)
344 gtk_style_context_get_padding(
345 gtk_widget_get_style_context(vc->vte.terminal),
346 gtk_widget_get_state_flags(vc->vte.terminal),
347 &padding);
348 #else
350 GtkBorder *ib = NULL;
351 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
352 if (ib) {
353 padding = *ib;
354 gtk_border_free(ib);
357 #endif
359 geo.width_inc = vte_terminal_get_char_width(term);
360 geo.height_inc = vte_terminal_get_char_height(term);
361 mask |= GDK_HINT_RESIZE_INC;
362 geo.base_width = geo.width_inc;
363 geo.base_height = geo.height_inc;
364 mask |= GDK_HINT_BASE_SIZE;
365 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
366 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
367 mask |= GDK_HINT_MIN_SIZE;
369 geo.base_width += padding.left + padding.right;
370 geo.base_height += padding.top + padding.bottom;
371 geo.min_width += padding.left + padding.right;
372 geo.min_height += padding.top + padding.bottom;
373 geo_widget = vc->vte.terminal;
374 #endif
377 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
378 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
381 void gd_update_windowsize(VirtualConsole *vc)
383 GtkDisplayState *s = vc->s;
385 gd_update_geometry_hints(vc);
387 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
388 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
389 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
393 static void gd_update_full_redraw(VirtualConsole *vc)
395 GtkWidget *area = vc->gfx.drawing_area;
396 int ww, wh;
397 ww = gdk_window_get_width(gtk_widget_get_window(area));
398 wh = gdk_window_get_height(gtk_widget_get_window(area));
399 #if defined(CONFIG_OPENGL)
400 if (vc->gfx.gls && gtk_use_gl_area) {
401 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
402 return;
404 #endif
405 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
408 static void gtk_release_modifiers(GtkDisplayState *s)
410 VirtualConsole *vc = gd_vc_find_current(s);
412 if (vc->type != GD_VC_GFX ||
413 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
414 return;
416 qkbd_state_lift_all_keys(vc->gfx.kbd);
419 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
420 GtkWidget *widget)
422 g_object_ref(G_OBJECT(widget));
423 gtk_container_remove(GTK_CONTAINER(from), widget);
424 gtk_container_add(GTK_CONTAINER(to), widget);
425 g_object_unref(G_OBJECT(widget));
428 static void *gd_win32_get_hwnd(VirtualConsole *vc)
430 #ifdef G_OS_WIN32
431 return gdk_win32_window_get_impl_hwnd(
432 gtk_widget_get_window(vc->window ? vc->window : vc->s->window));
433 #else
434 return NULL;
435 #endif
438 /** DisplayState Callbacks **/
440 static void gd_update(DisplayChangeListener *dcl,
441 int x, int y, int w, int h)
443 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
444 GdkWindow *win;
445 int x1, x2, y1, y2;
446 int mx, my;
447 int fbw, fbh;
448 int ww, wh;
450 trace_gd_update(vc->label, x, y, w, h);
452 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
453 return;
456 if (vc->gfx.convert) {
457 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
458 NULL, vc->gfx.convert,
459 x, y, 0, 0, x, y, w, h);
462 x1 = floor(x * vc->gfx.scale_x);
463 y1 = floor(y * vc->gfx.scale_y);
465 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
466 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
468 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
469 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
471 win = gtk_widget_get_window(vc->gfx.drawing_area);
472 if (!win) {
473 return;
475 ww = gdk_window_get_width(win);
476 wh = gdk_window_get_height(win);
478 mx = my = 0;
479 if (ww > fbw) {
480 mx = (ww - fbw) / 2;
482 if (wh > fbh) {
483 my = (wh - fbh) / 2;
486 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
487 mx + x1, my + y1, (x2 - x1), (y2 - y1));
490 static void gd_refresh(DisplayChangeListener *dcl)
492 graphic_hw_update(dcl->con);
495 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
497 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
500 static void gd_mouse_set(DisplayChangeListener *dcl,
501 int x, int y, int visible)
503 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
504 GdkDisplay *dpy;
505 gint x_root, y_root;
507 if (qemu_input_is_absolute()) {
508 return;
511 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
512 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
513 x, y, &x_root, &y_root);
514 gdk_device_warp(gd_get_pointer(dpy),
515 gtk_widget_get_screen(vc->gfx.drawing_area),
516 x_root, y_root);
517 vc->s->last_x = x;
518 vc->s->last_y = y;
521 static void gd_cursor_define(DisplayChangeListener *dcl,
522 QEMUCursor *c)
524 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
525 GdkPixbuf *pixbuf;
526 GdkCursor *cursor;
528 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
529 return;
532 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
533 GDK_COLORSPACE_RGB, true, 8,
534 c->width, c->height, c->width * 4,
535 NULL, NULL);
536 cursor = gdk_cursor_new_from_pixbuf
537 (gtk_widget_get_display(vc->gfx.drawing_area),
538 pixbuf, c->hot_x, c->hot_y);
539 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
540 g_object_unref(pixbuf);
541 g_object_unref(cursor);
544 static void gd_switch(DisplayChangeListener *dcl,
545 DisplaySurface *surface)
547 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
548 bool resized = true;
550 trace_gd_switch(vc->label,
551 surface ? surface_width(surface) : 0,
552 surface ? surface_height(surface) : 0);
554 if (vc->gfx.surface) {
555 cairo_surface_destroy(vc->gfx.surface);
556 vc->gfx.surface = NULL;
558 if (vc->gfx.convert) {
559 pixman_image_unref(vc->gfx.convert);
560 vc->gfx.convert = NULL;
563 if (vc->gfx.ds && surface &&
564 surface_width(vc->gfx.ds) == surface_width(surface) &&
565 surface_height(vc->gfx.ds) == surface_height(surface)) {
566 resized = false;
568 vc->gfx.ds = surface;
570 if (!surface) {
571 return;
574 if (surface->format == PIXMAN_x8r8g8b8) {
576 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
578 * No need to convert, use surface directly. Should be the
579 * common case as this is qemu_default_pixelformat(32) too.
581 vc->gfx.surface = cairo_image_surface_create_for_data
582 (surface_data(surface),
583 CAIRO_FORMAT_RGB24,
584 surface_width(surface),
585 surface_height(surface),
586 surface_stride(surface));
587 } else {
588 /* Must convert surface, use pixman to do it. */
589 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
590 surface_width(surface),
591 surface_height(surface),
592 NULL, 0);
593 vc->gfx.surface = cairo_image_surface_create_for_data
594 ((void *)pixman_image_get_data(vc->gfx.convert),
595 CAIRO_FORMAT_RGB24,
596 pixman_image_get_width(vc->gfx.convert),
597 pixman_image_get_height(vc->gfx.convert),
598 pixman_image_get_stride(vc->gfx.convert));
599 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
600 NULL, vc->gfx.convert,
601 0, 0, 0, 0, 0, 0,
602 pixman_image_get_width(vc->gfx.convert),
603 pixman_image_get_height(vc->gfx.convert));
606 if (resized) {
607 gd_update_windowsize(vc);
608 } else {
609 gd_update_full_redraw(vc);
613 static const DisplayChangeListenerOps dcl_ops = {
614 .dpy_name = "gtk",
615 .dpy_gfx_update = gd_update,
616 .dpy_gfx_switch = gd_switch,
617 .dpy_gfx_check_format = qemu_pixman_check_format,
618 .dpy_refresh = gd_refresh,
619 .dpy_mouse_set = gd_mouse_set,
620 .dpy_cursor_define = gd_cursor_define,
624 #if defined(CONFIG_OPENGL)
626 /** DisplayState Callbacks (opengl version) **/
628 #if defined(CONFIG_OPENGL)
630 static const DisplayChangeListenerOps dcl_gl_area_ops = {
631 .dpy_name = "gtk-egl",
632 .dpy_gfx_update = gd_gl_area_update,
633 .dpy_gfx_switch = gd_gl_area_switch,
634 .dpy_gfx_check_format = console_gl_check_format,
635 .dpy_refresh = gd_gl_area_refresh,
636 .dpy_mouse_set = gd_mouse_set,
637 .dpy_cursor_define = gd_cursor_define,
639 .dpy_gl_ctx_create = gd_gl_area_create_context,
640 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
641 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
642 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
643 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
644 .dpy_gl_update = gd_gl_area_scanout_flush,
647 #endif /* CONFIG_OPENGL */
649 static const DisplayChangeListenerOps dcl_egl_ops = {
650 .dpy_name = "gtk-egl",
651 .dpy_gfx_update = gd_egl_update,
652 .dpy_gfx_switch = gd_egl_switch,
653 .dpy_gfx_check_format = console_gl_check_format,
654 .dpy_refresh = gd_egl_refresh,
655 .dpy_mouse_set = gd_mouse_set,
656 .dpy_cursor_define = gd_cursor_define,
658 .dpy_gl_ctx_create = gd_egl_create_context,
659 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
660 .dpy_gl_ctx_make_current = gd_egl_make_current,
661 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
662 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
663 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
664 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
665 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
666 .dpy_gl_cursor_position = gd_egl_cursor_position,
667 .dpy_gl_release_dmabuf = gd_egl_release_dmabuf,
668 .dpy_gl_update = gd_egl_scanout_flush,
671 #endif /* CONFIG_OPENGL */
673 /** QEMU Events **/
675 static void gd_change_runstate(void *opaque, int running, RunState state)
677 GtkDisplayState *s = opaque;
679 gd_update_caption(s);
682 static void gd_mouse_mode_change(Notifier *notify, void *data)
684 GtkDisplayState *s;
685 int i;
687 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
688 /* release the grab at switching to absolute mode */
689 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
690 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
691 FALSE);
693 for (i = 0; i < s->nb_vcs; i++) {
694 VirtualConsole *vc = &s->vc[i];
695 gd_update_cursor(vc);
699 /** GTK Events **/
701 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
702 void *opaque)
704 GtkDisplayState *s = opaque;
705 bool allow_close = true;
707 if (s->opts->has_window_close && !s->opts->window_close) {
708 allow_close = false;
711 if (allow_close) {
712 qmp_quit(NULL);
715 return TRUE;
718 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
720 QemuUIInfo info;
722 memset(&info, 0, sizeof(info));
723 info.width = width;
724 info.height = height;
725 dpy_set_ui_info(vc->gfx.dcl.con, &info);
728 #if defined(CONFIG_OPENGL)
730 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
731 void *opaque)
733 VirtualConsole *vc = opaque;
735 if (vc->gfx.gls) {
736 gd_gl_area_draw(vc);
738 return TRUE;
741 static void gd_resize_event(GtkGLArea *area,
742 gint width, gint height, gpointer *opaque)
744 VirtualConsole *vc = (void *)opaque;
746 gd_set_ui_info(vc, width, height);
749 #endif
752 * If available, return the update interval of the monitor in ms,
753 * else return 0 (the default update interval).
755 int gd_monitor_update_interval(GtkWidget *widget)
757 #ifdef GDK_VERSION_3_22
758 GdkWindow *win = gtk_widget_get_window(widget);
760 if (win) {
761 GdkDisplay *dpy = gtk_widget_get_display(widget);
762 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
763 int refresh_rate = gdk_monitor_get_refresh_rate(monitor); /* [mHz] */
765 if (refresh_rate) {
766 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
767 return MIN(1000 * 1000 / refresh_rate,
768 GUI_REFRESH_INTERVAL_DEFAULT);
771 #endif
772 return 0;
775 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
777 VirtualConsole *vc = opaque;
778 GtkDisplayState *s = vc->s;
779 int mx, my;
780 int ww, wh;
781 int fbw, fbh;
783 #if defined(CONFIG_OPENGL)
784 if (vc->gfx.gls) {
785 if (gtk_use_gl_area) {
786 /* invoke render callback please */
787 return FALSE;
788 } else {
789 gd_egl_draw(vc);
790 return TRUE;
793 #endif
795 if (!gtk_widget_get_realized(widget)) {
796 return FALSE;
798 if (!vc->gfx.ds) {
799 return FALSE;
802 vc->gfx.dcl.update_interval =
803 gd_monitor_update_interval(vc->window ? vc->window : s->window);
805 fbw = surface_width(vc->gfx.ds);
806 fbh = surface_height(vc->gfx.ds);
808 ww = gdk_window_get_width(gtk_widget_get_window(widget));
809 wh = gdk_window_get_height(gtk_widget_get_window(widget));
811 if (s->full_screen) {
812 vc->gfx.scale_x = (double)ww / fbw;
813 vc->gfx.scale_y = (double)wh / fbh;
814 } else if (s->free_scale) {
815 double sx, sy;
817 sx = (double)ww / fbw;
818 sy = (double)wh / fbh;
820 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
823 fbw *= vc->gfx.scale_x;
824 fbh *= vc->gfx.scale_y;
826 mx = my = 0;
827 if (ww > fbw) {
828 mx = (ww - fbw) / 2;
830 if (wh > fbh) {
831 my = (wh - fbh) / 2;
834 cairo_rectangle(cr, 0, 0, ww, wh);
836 /* Optionally cut out the inner area where the pixmap
837 will be drawn. This avoids 'flashing' since we're
838 not double-buffering. Note we're using the undocumented
839 behaviour of drawing the rectangle from right to left
840 to cut out the whole */
841 cairo_rectangle(cr, mx + fbw, my,
842 -1 * fbw, fbh);
843 cairo_fill(cr);
845 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
846 cairo_set_source_surface(cr, vc->gfx.surface,
847 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
848 cairo_paint(cr);
850 return TRUE;
853 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
854 void *opaque)
856 VirtualConsole *vc = opaque;
857 GtkDisplayState *s = vc->s;
858 int x, y;
859 int mx, my;
860 int fbh, fbw;
861 int ww, wh;
863 if (!vc->gfx.ds) {
864 return TRUE;
867 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
868 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
870 ww = gdk_window_get_width(gtk_widget_get_window(vc->gfx.drawing_area));
871 wh = gdk_window_get_height(gtk_widget_get_window(vc->gfx.drawing_area));
873 mx = my = 0;
874 if (ww > fbw) {
875 mx = (ww - fbw) / 2;
877 if (wh > fbh) {
878 my = (wh - fbh) / 2;
881 x = (motion->x - mx) / vc->gfx.scale_x;
882 y = (motion->y - my) / vc->gfx.scale_y;
884 if (qemu_input_is_absolute()) {
885 if (x < 0 || y < 0 ||
886 x >= surface_width(vc->gfx.ds) ||
887 y >= surface_height(vc->gfx.ds)) {
888 return TRUE;
890 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
891 0, surface_width(vc->gfx.ds));
892 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
893 0, surface_height(vc->gfx.ds));
894 qemu_input_event_sync();
895 } else if (s->last_set && s->ptr_owner == vc) {
896 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
897 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
898 qemu_input_event_sync();
900 s->last_x = x;
901 s->last_y = y;
902 s->last_set = TRUE;
904 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
905 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
906 GdkDisplay *dpy = gtk_widget_get_display(widget);
907 GdkWindow *win = gtk_widget_get_window(widget);
908 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
909 GdkRectangle geometry;
910 int screen_width, screen_height;
912 int x = (int)motion->x_root;
913 int y = (int)motion->y_root;
915 gdk_monitor_get_geometry(monitor, &geometry);
916 screen_width = geometry.width;
917 screen_height = geometry.height;
919 /* In relative mode check to see if client pointer hit
920 * one of the screen edges, and if so move it back by
921 * 200 pixels. This is important because the pointer
922 * in the server doesn't correspond 1-for-1, and so
923 * may still be only half way across the screen. Without
924 * this warp, the server pointer would thus appear to hit
925 * an invisible wall */
926 if (x == 0) {
927 x += 200;
929 if (y == 0) {
930 y += 200;
932 if (x == (screen_width - 1)) {
933 x -= 200;
935 if (y == (screen_height - 1)) {
936 y -= 200;
939 if (x != (int)motion->x_root || y != (int)motion->y_root) {
940 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
941 gdk_device_warp(dev, screen, x, y);
942 s->last_set = FALSE;
943 return FALSE;
946 return TRUE;
949 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
950 void *opaque)
952 VirtualConsole *vc = opaque;
953 GtkDisplayState *s = vc->s;
954 InputButton btn;
956 /* implicitly grab the input at the first click in the relative mode */
957 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
958 !qemu_input_is_absolute() && s->ptr_owner != vc) {
959 if (!vc->window) {
960 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
961 TRUE);
962 } else {
963 gd_grab_pointer(vc, "relative-mode-click");
965 return TRUE;
968 if (button->button == 1) {
969 btn = INPUT_BUTTON_LEFT;
970 } else if (button->button == 2) {
971 btn = INPUT_BUTTON_MIDDLE;
972 } else if (button->button == 3) {
973 btn = INPUT_BUTTON_RIGHT;
974 } else if (button->button == 8) {
975 btn = INPUT_BUTTON_SIDE;
976 } else if (button->button == 9) {
977 btn = INPUT_BUTTON_EXTRA;
978 } else {
979 return TRUE;
982 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
983 button->type == GDK_BUTTON_PRESS);
984 qemu_input_event_sync();
985 return TRUE;
988 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
989 void *opaque)
991 VirtualConsole *vc = opaque;
992 InputButton btn;
994 if (scroll->direction == GDK_SCROLL_UP) {
995 btn = INPUT_BUTTON_WHEEL_UP;
996 } else if (scroll->direction == GDK_SCROLL_DOWN) {
997 btn = INPUT_BUTTON_WHEEL_DOWN;
998 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
999 gdouble delta_x, delta_y;
1000 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1001 &delta_x, &delta_y)) {
1002 return TRUE;
1004 if (delta_y == 0) {
1005 return TRUE;
1006 } else if (delta_y > 0) {
1007 btn = INPUT_BUTTON_WHEEL_DOWN;
1008 } else {
1009 btn = INPUT_BUTTON_WHEEL_UP;
1011 } else {
1012 return TRUE;
1015 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1016 qemu_input_event_sync();
1017 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1018 qemu_input_event_sync();
1019 return TRUE;
1023 static const guint16 *gd_get_keymap(size_t *maplen)
1025 GdkDisplay *dpy = gdk_display_get_default();
1027 #ifdef GDK_WINDOWING_X11
1028 if (GDK_IS_X11_DISPLAY(dpy)) {
1029 trace_gd_keymap_windowing("x11");
1030 return qemu_xkeymap_mapping_table(
1031 gdk_x11_display_get_xdisplay(dpy), maplen);
1033 #endif
1035 #ifdef GDK_WINDOWING_WAYLAND
1036 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1037 trace_gd_keymap_windowing("wayland");
1038 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1039 return qemu_input_map_xorgevdev_to_qcode;
1041 #endif
1043 #ifdef GDK_WINDOWING_WIN32
1044 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1045 trace_gd_keymap_windowing("win32");
1046 *maplen = qemu_input_map_atset1_to_qcode_len;
1047 return qemu_input_map_atset1_to_qcode;
1049 #endif
1051 #ifdef GDK_WINDOWING_QUARTZ
1052 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1053 trace_gd_keymap_windowing("quartz");
1054 *maplen = qemu_input_map_osx_to_qcode_len;
1055 return qemu_input_map_osx_to_qcode;
1057 #endif
1059 #ifdef GDK_WINDOWING_BROADWAY
1060 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1061 trace_gd_keymap_windowing("broadway");
1062 g_warning("experimental: using broadway, x11 virtual keysym\n"
1063 "mapping - with very limited support. See also\n"
1064 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1065 *maplen = qemu_input_map_x11_to_qcode_len;
1066 return qemu_input_map_x11_to_qcode;
1068 #endif
1070 g_warning("Unsupported GDK Windowing platform.\n"
1071 "Disabling extended keycode tables.\n"
1072 "Please report to qemu-devel@nongnu.org\n"
1073 "including the following information:\n"
1074 "\n"
1075 " - Operating system\n"
1076 " - GDK Windowing system build\n");
1077 return NULL;
1081 static int gd_map_keycode(int scancode)
1083 if (!keycode_map) {
1084 return 0;
1086 if (scancode > keycode_maplen) {
1087 return 0;
1090 return keycode_map[scancode];
1093 static int gd_get_keycode(GdkEventKey *key)
1095 #ifdef G_OS_WIN32
1096 int scancode = gdk_event_get_scancode((GdkEvent *)key);
1098 /* translate Windows native scancodes to atset1 keycodes */
1099 switch (scancode & (KF_EXTENDED | 0xff)) {
1100 case 0x145: /* NUMLOCK */
1101 return scancode & 0xff;
1104 return scancode & KF_EXTENDED ?
1105 0xe000 | (scancode & 0xff) : scancode & 0xff;
1107 #else
1108 return key->hardware_keycode;
1109 #endif
1112 static gboolean gd_text_key_down(GtkWidget *widget,
1113 GdkEventKey *key, void *opaque)
1115 VirtualConsole *vc = opaque;
1116 QemuConsole *con = vc->gfx.dcl.con;
1118 if (key->keyval == GDK_KEY_Delete) {
1119 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE, false);
1120 } else if (key->length) {
1121 kbd_put_string_console(con, key->string, key->length);
1122 } else {
1123 int qcode = gd_map_keycode(gd_get_keycode(key));
1124 kbd_put_qcode_console(con, qcode, false);
1126 return TRUE;
1129 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1131 VirtualConsole *vc = opaque;
1132 int keycode, qcode;
1134 #ifdef G_OS_WIN32
1135 /* on windows, we ought to ignore the reserved key event? */
1136 if (key->hardware_keycode == 0xff)
1137 return false;
1139 if (!vc->s->kbd_owner) {
1140 if (key->hardware_keycode == VK_LWIN ||
1141 key->hardware_keycode == VK_RWIN) {
1142 return FALSE;
1145 #endif
1147 if (key->keyval == GDK_KEY_Pause
1148 #ifdef G_OS_WIN32
1149 /* for some reason GDK does not fill keyval for VK_PAUSE
1150 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1152 || key->hardware_keycode == VK_PAUSE
1153 #endif
1155 qkbd_state_key_event(vc->gfx.kbd, Q_KEY_CODE_PAUSE,
1156 key->type == GDK_KEY_PRESS);
1157 return TRUE;
1160 keycode = gd_get_keycode(key);
1161 qcode = gd_map_keycode(keycode);
1163 trace_gd_key_event(vc->label, keycode, qcode,
1164 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1166 qkbd_state_key_event(vc->gfx.kbd, qcode,
1167 key->type == GDK_KEY_PRESS);
1169 return TRUE;
1172 static gboolean gd_grab_broken_event(GtkWidget *widget,
1173 GdkEventGrabBroken *event, void *opaque)
1175 #ifdef CONFIG_WIN32
1177 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1178 * key combination leaves all three keys in a stuck condition. We use
1179 * the grab-broken-event to release all keys.
1181 if (event->keyboard) {
1182 VirtualConsole *vc = opaque;
1183 GtkDisplayState *s = vc->s;
1185 gtk_release_modifiers(s);
1187 #endif
1188 return TRUE;
1191 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1193 if (event->type == GDK_MOTION_NOTIFY) {
1194 return gd_motion_event(widget, &event->motion, opaque);
1196 return FALSE;
1199 /** Window Menu Actions **/
1201 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1203 GtkDisplayState *s = opaque;
1205 if (s->external_pause_update) {
1206 return;
1208 if (runstate_is_running()) {
1209 qmp_stop(NULL);
1210 } else {
1211 qmp_cont(NULL);
1215 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1217 qmp_system_reset(NULL);
1220 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1222 qmp_system_powerdown(NULL);
1225 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1227 qmp_quit(NULL);
1230 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1232 GtkDisplayState *s = opaque;
1233 VirtualConsole *vc = gd_vc_find_by_menu(s);
1234 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1235 gint page;
1237 gtk_release_modifiers(s);
1238 if (vc) {
1239 page = gtk_notebook_page_num(nb, vc->tab_item);
1240 gtk_notebook_set_current_page(nb, page);
1241 gtk_widget_grab_focus(vc->focus);
1245 static void gd_accel_switch_vc(void *opaque)
1247 VirtualConsole *vc = opaque;
1249 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1252 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1254 GtkDisplayState *s = opaque;
1255 VirtualConsole *vc = gd_vc_find_current(s);
1257 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1258 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1259 } else {
1260 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1262 gd_update_windowsize(vc);
1265 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1266 void *opaque)
1268 VirtualConsole *vc = opaque;
1269 GtkDisplayState *s = vc->s;
1271 gtk_widget_set_sensitive(vc->menu_item, true);
1272 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1273 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1274 vc->tab_item, vc->label);
1275 gtk_widget_destroy(vc->window);
1276 vc->window = NULL;
1277 return TRUE;
1280 static gboolean gd_win_grab(void *opaque)
1282 VirtualConsole *vc = opaque;
1284 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1285 if (vc->s->ptr_owner) {
1286 gd_ungrab_pointer(vc->s);
1287 } else {
1288 gd_grab_pointer(vc, "user-request-detached-tab");
1290 return TRUE;
1293 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1295 GtkDisplayState *s = opaque;
1296 VirtualConsole *vc = gd_vc_find_current(s);
1298 if (vc->type == GD_VC_GFX &&
1299 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1300 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1301 FALSE);
1303 if (!vc->window) {
1304 gtk_widget_set_sensitive(vc->menu_item, false);
1305 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1306 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1308 g_signal_connect(vc->window, "delete-event",
1309 G_CALLBACK(gd_tab_window_close), vc);
1310 gtk_widget_show_all(vc->window);
1312 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1313 GtkAccelGroup *ag = gtk_accel_group_new();
1314 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1316 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1317 vc, NULL);
1318 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1321 gd_update_geometry_hints(vc);
1322 gd_update_caption(s);
1326 static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
1328 GtkDisplayState *s = opaque;
1329 VirtualConsole *vc = gd_vc_find_current(s);
1331 if (s->full_screen) {
1332 return;
1335 if (gtk_check_menu_item_get_active(
1336 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1337 gtk_widget_show(s->menu_bar);
1338 } else {
1339 gtk_widget_hide(s->menu_bar);
1341 gd_update_windowsize(vc);
1344 static void gd_accel_show_menubar(void *opaque)
1346 GtkDisplayState *s = opaque;
1347 gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
1350 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1352 GtkDisplayState *s = opaque;
1353 VirtualConsole *vc = gd_vc_find_current(s);
1355 if (!s->full_screen) {
1356 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1357 gtk_widget_hide(s->menu_bar);
1358 if (vc->type == GD_VC_GFX) {
1359 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1361 gtk_window_fullscreen(GTK_WINDOW(s->window));
1362 s->full_screen = TRUE;
1363 } else {
1364 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1365 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1366 if (gtk_check_menu_item_get_active(
1367 GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
1368 gtk_widget_show(s->menu_bar);
1370 s->full_screen = FALSE;
1371 if (vc->type == GD_VC_GFX) {
1372 vc->gfx.scale_x = 1.0;
1373 vc->gfx.scale_y = 1.0;
1374 gd_update_windowsize(vc);
1378 gd_update_cursor(vc);
1381 static void gd_accel_full_screen(void *opaque)
1383 GtkDisplayState *s = opaque;
1384 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1387 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1389 GtkDisplayState *s = opaque;
1390 VirtualConsole *vc = gd_vc_find_current(s);
1392 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1393 FALSE);
1395 vc->gfx.scale_x += VC_SCALE_STEP;
1396 vc->gfx.scale_y += VC_SCALE_STEP;
1398 gd_update_windowsize(vc);
1401 static void gd_accel_zoom_in(void *opaque)
1403 GtkDisplayState *s = opaque;
1404 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1407 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1409 GtkDisplayState *s = opaque;
1410 VirtualConsole *vc = gd_vc_find_current(s);
1412 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1413 FALSE);
1415 vc->gfx.scale_x -= VC_SCALE_STEP;
1416 vc->gfx.scale_y -= VC_SCALE_STEP;
1418 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1419 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1421 gd_update_windowsize(vc);
1424 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1426 GtkDisplayState *s = opaque;
1427 VirtualConsole *vc = gd_vc_find_current(s);
1429 vc->gfx.scale_x = 1.0;
1430 vc->gfx.scale_y = 1.0;
1432 gd_update_windowsize(vc);
1435 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1437 GtkDisplayState *s = opaque;
1438 VirtualConsole *vc = gd_vc_find_current(s);
1440 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1441 s->free_scale = TRUE;
1442 } else {
1443 s->free_scale = FALSE;
1444 vc->gfx.scale_x = 1.0;
1445 vc->gfx.scale_y = 1.0;
1448 gd_update_windowsize(vc);
1449 gd_update_full_redraw(vc);
1452 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1454 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1455 GdkSeat *seat = gdk_display_get_default_seat(display);
1456 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1457 GdkSeatCapabilities caps = 0;
1458 GdkCursor *cursor = NULL;
1460 if (kbd) {
1461 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1463 if (ptr) {
1464 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1465 cursor = vc->s->null_cursor;
1468 if (caps) {
1469 gdk_seat_grab(seat, window, caps, false, cursor,
1470 NULL, NULL, NULL);
1471 } else {
1472 gdk_seat_ungrab(seat);
1476 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1478 if (vc->s->kbd_owner) {
1479 if (vc->s->kbd_owner == vc) {
1480 return;
1481 } else {
1482 gd_ungrab_keyboard(vc->s);
1486 win32_kbd_set_grab(true);
1487 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1488 vc->s->kbd_owner = vc;
1489 gd_update_caption(vc->s);
1490 trace_gd_grab(vc->label, "kbd", reason);
1493 static void gd_ungrab_keyboard(GtkDisplayState *s)
1495 VirtualConsole *vc = s->kbd_owner;
1497 if (vc == NULL) {
1498 return;
1500 s->kbd_owner = NULL;
1502 win32_kbd_set_grab(false);
1503 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1504 gd_update_caption(s);
1505 trace_gd_ungrab(vc->label, "kbd");
1508 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1510 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1512 if (vc->s->ptr_owner) {
1513 if (vc->s->ptr_owner == vc) {
1514 return;
1515 } else {
1516 gd_ungrab_pointer(vc->s);
1520 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1521 gdk_device_get_position(gd_get_pointer(display),
1522 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1523 vc->s->ptr_owner = vc;
1524 gd_update_caption(vc->s);
1525 trace_gd_grab(vc->label, "ptr", reason);
1528 static void gd_ungrab_pointer(GtkDisplayState *s)
1530 VirtualConsole *vc = s->ptr_owner;
1531 GdkDisplay *display;
1533 if (vc == NULL) {
1534 return;
1536 s->ptr_owner = NULL;
1538 display = gtk_widget_get_display(vc->gfx.drawing_area);
1539 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1540 gdk_device_warp(gd_get_pointer(display),
1541 gtk_widget_get_screen(vc->gfx.drawing_area),
1542 vc->s->grab_x_root, vc->s->grab_y_root);
1543 gd_update_caption(s);
1544 trace_gd_ungrab(vc->label, "ptr");
1547 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1549 GtkDisplayState *s = opaque;
1550 VirtualConsole *vc = gd_vc_find_current(s);
1552 if (gd_is_grab_active(s)) {
1553 gd_grab_keyboard(vc, "user-request-main-window");
1554 gd_grab_pointer(vc, "user-request-main-window");
1555 } else {
1556 gd_ungrab_keyboard(s);
1557 gd_ungrab_pointer(s);
1560 gd_update_cursor(vc);
1563 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1564 gpointer data)
1566 GtkDisplayState *s = data;
1567 VirtualConsole *vc;
1568 gboolean on_vga;
1570 if (!gtk_widget_get_realized(s->notebook)) {
1571 return;
1574 vc = gd_vc_find_by_page(s, arg2);
1575 if (!vc) {
1576 return;
1578 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1579 TRUE);
1580 on_vga = (vc->type == GD_VC_GFX &&
1581 qemu_console_is_graphic(vc->gfx.dcl.con));
1582 if (!on_vga) {
1583 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1584 FALSE);
1585 } else if (s->full_screen) {
1586 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1587 TRUE);
1589 gtk_widget_set_sensitive(s->grab_item, on_vga);
1590 #ifdef CONFIG_VTE
1591 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1592 #endif
1594 gd_update_windowsize(vc);
1595 gd_update_cursor(vc);
1598 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1599 gpointer opaque)
1601 VirtualConsole *vc = opaque;
1602 GtkDisplayState *s = vc->s;
1604 if (gd_grab_on_hover(s)) {
1605 gd_grab_keyboard(vc, "grab-on-hover");
1607 return TRUE;
1610 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1611 gpointer opaque)
1613 VirtualConsole *vc = opaque;
1614 GtkDisplayState *s = vc->s;
1616 if (gd_grab_on_hover(s)) {
1617 gd_ungrab_keyboard(s);
1619 return TRUE;
1622 static gboolean gd_focus_in_event(GtkWidget *widget,
1623 GdkEventFocus *event, gpointer opaque)
1625 VirtualConsole *vc = opaque;
1627 win32_kbd_set_window(gd_win32_get_hwnd(vc));
1628 return TRUE;
1631 static gboolean gd_focus_out_event(GtkWidget *widget,
1632 GdkEventFocus *event, gpointer opaque)
1634 VirtualConsole *vc = opaque;
1635 GtkDisplayState *s = vc->s;
1637 win32_kbd_set_window(NULL);
1638 gtk_release_modifiers(s);
1639 return TRUE;
1642 static gboolean gd_configure(GtkWidget *widget,
1643 GdkEventConfigure *cfg, gpointer opaque)
1645 VirtualConsole *vc = opaque;
1647 gd_set_ui_info(vc, cfg->width, cfg->height);
1648 return FALSE;
1651 /** Virtual Console Callbacks **/
1653 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1654 int idx, GSList *group, GtkWidget *view_menu)
1656 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1657 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1658 HOTKEY_MODIFIERS, 0,
1659 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1660 gtk_accel_label_set_accel(
1661 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1662 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1664 g_signal_connect(vc->menu_item, "activate",
1665 G_CALLBACK(gd_menu_switch_vc), s);
1666 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1668 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1671 #if defined(CONFIG_VTE)
1672 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1674 GtkDisplayState *s = opaque;
1675 VirtualConsole *vc = gd_vc_find_current(s);
1677 #if VTE_CHECK_VERSION(0, 50, 0)
1678 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1679 VTE_FORMAT_TEXT);
1680 #else
1681 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1682 #endif
1685 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1687 VirtualConsole *vc = opaque;
1689 if (gtk_adjustment_get_upper(adjustment) >
1690 gtk_adjustment_get_page_size(adjustment)) {
1691 gtk_widget_show(vc->vte.scrollbar);
1692 } else {
1693 gtk_widget_hide(vc->vte.scrollbar);
1697 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1699 VCChardev *vcd = VC_CHARDEV(chr);
1700 VirtualConsole *vc = vcd->console;
1702 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1703 return len;
1706 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1708 VCChardev *vcd = VC_CHARDEV(chr);
1709 VirtualConsole *vc = vcd->console;
1711 if (vc) {
1712 vc->vte.echo = echo;
1713 } else {
1714 vcd->echo = echo;
1718 static int nb_vcs;
1719 static Chardev *vcs[MAX_VCS];
1720 static void gd_vc_open(Chardev *chr,
1721 ChardevBackend *backend,
1722 bool *be_opened,
1723 Error **errp)
1725 if (nb_vcs == MAX_VCS) {
1726 error_setg(errp, "Maximum number of consoles reached");
1727 return;
1730 vcs[nb_vcs++] = chr;
1732 /* console/chardev init sometimes completes elsewhere in a 2nd
1733 * stage, so defer OPENED events until they are fully initialized
1735 *be_opened = false;
1738 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1740 ChardevClass *cc = CHARDEV_CLASS(oc);
1742 cc->parse = qemu_chr_parse_vc;
1743 cc->open = gd_vc_open;
1744 cc->chr_write = gd_vc_chr_write;
1745 cc->chr_set_echo = gd_vc_chr_set_echo;
1748 static const TypeInfo char_gd_vc_type_info = {
1749 .name = TYPE_CHARDEV_VC,
1750 .parent = TYPE_CHARDEV,
1751 .instance_size = sizeof(VCChardev),
1752 .class_init = char_gd_vc_class_init,
1755 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1756 gpointer user_data)
1758 VirtualConsole *vc = user_data;
1760 if (vc->vte.echo) {
1761 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1762 int i;
1763 for (i = 0; i < size; i++) {
1764 uint8_t c = text[i];
1765 if (c >= 128 || isprint(c)) {
1766 /* 8-bit characters are considered printable. */
1767 vte_terminal_feed(term, &text[i], 1);
1768 } else if (c == '\r' || c == '\n') {
1769 vte_terminal_feed(term, "\r\n", 2);
1770 } else {
1771 char ctrl[2] = { '^', 0};
1772 ctrl[1] = text[i] ^ 64;
1773 vte_terminal_feed(term, ctrl, 2);
1778 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1779 return TRUE;
1782 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1783 Chardev *chr, int idx,
1784 GSList *group, GtkWidget *view_menu)
1786 char buffer[32];
1787 GtkWidget *box;
1788 GtkWidget *scrollbar;
1789 GtkAdjustment *vadjustment;
1790 VCChardev *vcd = VC_CHARDEV(chr);
1792 vc->s = s;
1793 vc->vte.echo = vcd->echo;
1794 vc->vte.chr = chr;
1795 vcd->console = vc;
1797 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1798 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1799 ? vc->vte.chr->label : buffer);
1800 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1802 vc->vte.terminal = vte_terminal_new();
1803 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1805 /* The documentation says that the default is UTF-8, but actually it is
1806 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1807 * VTE 0.54 (only UTF-8 is supported now). */
1808 #if !VTE_CHECK_VERSION(0, 54, 0)
1809 #if VTE_CHECK_VERSION(0, 38, 0)
1810 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1811 #else
1812 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1813 #endif
1814 #endif
1816 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1817 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1818 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1820 #if VTE_CHECK_VERSION(0, 28, 0)
1821 vadjustment = gtk_scrollable_get_vadjustment
1822 (GTK_SCROLLABLE(vc->vte.terminal));
1823 #else
1824 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1825 #endif
1827 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1828 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1830 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1831 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1833 vc->vte.box = box;
1834 vc->vte.scrollbar = scrollbar;
1836 g_signal_connect(vadjustment, "changed",
1837 G_CALLBACK(gd_vc_adjustment_changed), vc);
1839 vc->type = GD_VC_VTE;
1840 vc->tab_item = box;
1841 vc->focus = vc->vte.terminal;
1842 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1843 gtk_label_new(vc->label));
1845 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1847 return group;
1850 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1851 GtkWidget *view_menu)
1853 int i;
1855 for (i = 0; i < nb_vcs; i++) {
1856 VirtualConsole *vc = &s->vc[s->nb_vcs];
1857 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1858 s->nb_vcs++;
1861 #endif /* CONFIG_VTE */
1863 /** Window Creation **/
1865 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1867 g_signal_connect(vc->gfx.drawing_area, "draw",
1868 G_CALLBACK(gd_draw_event), vc);
1869 #if defined(CONFIG_OPENGL)
1870 if (gtk_use_gl_area) {
1871 /* wire up GtkGlArea events */
1872 g_signal_connect(vc->gfx.drawing_area, "render",
1873 G_CALLBACK(gd_render_event), vc);
1874 g_signal_connect(vc->gfx.drawing_area, "resize",
1875 G_CALLBACK(gd_resize_event), vc);
1877 #endif
1878 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1879 g_signal_connect(vc->gfx.drawing_area, "event",
1880 G_CALLBACK(gd_event), vc);
1881 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1882 G_CALLBACK(gd_button_event), vc);
1883 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1884 G_CALLBACK(gd_button_event), vc);
1885 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1886 G_CALLBACK(gd_scroll_event), vc);
1887 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1888 G_CALLBACK(gd_key_event), vc);
1889 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1890 G_CALLBACK(gd_key_event), vc);
1892 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1893 G_CALLBACK(gd_enter_event), vc);
1894 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1895 G_CALLBACK(gd_leave_event), vc);
1896 g_signal_connect(vc->gfx.drawing_area, "focus-in-event",
1897 G_CALLBACK(gd_focus_in_event), vc);
1898 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1899 G_CALLBACK(gd_focus_out_event), vc);
1900 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1901 G_CALLBACK(gd_configure), vc);
1902 g_signal_connect(vc->gfx.drawing_area, "grab-broken-event",
1903 G_CALLBACK(gd_grab_broken_event), vc);
1904 } else {
1905 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1906 G_CALLBACK(gd_text_key_down), vc);
1910 static void gd_connect_signals(GtkDisplayState *s)
1912 g_signal_connect(s->show_tabs_item, "activate",
1913 G_CALLBACK(gd_menu_show_tabs), s);
1914 g_signal_connect(s->untabify_item, "activate",
1915 G_CALLBACK(gd_menu_untabify), s);
1916 g_signal_connect(s->show_menubar_item, "activate",
1917 G_CALLBACK(gd_menu_show_menubar), s);
1919 g_signal_connect(s->window, "delete-event",
1920 G_CALLBACK(gd_window_close), s);
1922 g_signal_connect(s->pause_item, "activate",
1923 G_CALLBACK(gd_menu_pause), s);
1924 g_signal_connect(s->reset_item, "activate",
1925 G_CALLBACK(gd_menu_reset), s);
1926 g_signal_connect(s->powerdown_item, "activate",
1927 G_CALLBACK(gd_menu_powerdown), s);
1928 g_signal_connect(s->quit_item, "activate",
1929 G_CALLBACK(gd_menu_quit), s);
1930 #if defined(CONFIG_VTE)
1931 g_signal_connect(s->copy_item, "activate",
1932 G_CALLBACK(gd_menu_copy), s);
1933 #endif
1934 g_signal_connect(s->full_screen_item, "activate",
1935 G_CALLBACK(gd_menu_full_screen), s);
1936 g_signal_connect(s->zoom_in_item, "activate",
1937 G_CALLBACK(gd_menu_zoom_in), s);
1938 g_signal_connect(s->zoom_out_item, "activate",
1939 G_CALLBACK(gd_menu_zoom_out), s);
1940 g_signal_connect(s->zoom_fixed_item, "activate",
1941 G_CALLBACK(gd_menu_zoom_fixed), s);
1942 g_signal_connect(s->zoom_fit_item, "activate",
1943 G_CALLBACK(gd_menu_zoom_fit), s);
1944 g_signal_connect(s->grab_item, "activate",
1945 G_CALLBACK(gd_menu_grab_input), s);
1946 g_signal_connect(s->notebook, "switch-page",
1947 G_CALLBACK(gd_change_page), s);
1950 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1952 GtkWidget *machine_menu;
1953 GtkWidget *separator;
1955 machine_menu = gtk_menu_new();
1956 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1958 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1959 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1961 separator = gtk_separator_menu_item_new();
1962 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1964 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1965 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1967 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1968 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1970 separator = gtk_separator_menu_item_new();
1971 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1973 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1974 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1975 "<QEMU>/Machine/Quit");
1976 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1977 GDK_KEY_q, HOTKEY_MODIFIERS);
1978 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1980 return machine_menu;
1983 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1984 QemuConsole *con, int idx,
1985 GSList *group, GtkWidget *view_menu)
1987 bool zoom_to_fit = false;
1989 vc->label = qemu_console_get_label(con);
1990 vc->s = s;
1991 vc->gfx.scale_x = 1.0;
1992 vc->gfx.scale_y = 1.0;
1994 #if defined(CONFIG_OPENGL)
1995 if (display_opengl) {
1996 #if defined(CONFIG_OPENGL)
1997 if (gtk_use_gl_area) {
1998 vc->gfx.drawing_area = gtk_gl_area_new();
1999 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2000 } else
2001 #endif /* CONFIG_OPENGL */
2003 vc->gfx.drawing_area = gtk_drawing_area_new();
2005 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2006 * It is required for opengl rendering on X11 though. A
2007 * proper replacement (native opengl support) is only
2008 * available in 3.16+. Silence the warning if possible.
2010 #pragma GCC diagnostic push
2011 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2012 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2013 #pragma GCC diagnostic pop
2014 vc->gfx.dcl.ops = &dcl_egl_ops;
2016 } else
2017 #endif
2019 vc->gfx.drawing_area = gtk_drawing_area_new();
2020 vc->gfx.dcl.ops = &dcl_ops;
2024 gtk_widget_add_events(vc->gfx.drawing_area,
2025 GDK_POINTER_MOTION_MASK |
2026 GDK_BUTTON_PRESS_MASK |
2027 GDK_BUTTON_RELEASE_MASK |
2028 GDK_BUTTON_MOTION_MASK |
2029 GDK_ENTER_NOTIFY_MASK |
2030 GDK_LEAVE_NOTIFY_MASK |
2031 GDK_SCROLL_MASK |
2032 GDK_SMOOTH_SCROLL_MASK |
2033 GDK_KEY_PRESS_MASK);
2034 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2036 vc->type = GD_VC_GFX;
2037 vc->tab_item = vc->gfx.drawing_area;
2038 vc->focus = vc->gfx.drawing_area;
2039 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2040 vc->tab_item, gtk_label_new(vc->label));
2042 vc->gfx.kbd = qkbd_state_init(con);
2043 vc->gfx.dcl.con = con;
2045 register_displaychangelistener(&vc->gfx.dcl);
2047 gd_connect_vc_gfx_signals(vc);
2048 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2050 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2051 zoom_to_fit = true;
2053 if (s->opts->u.gtk.has_zoom_to_fit) {
2054 zoom_to_fit = s->opts->u.gtk.zoom_to_fit;
2056 if (zoom_to_fit) {
2057 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2058 s->free_scale = true;
2061 return group;
2064 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2066 GSList *group = NULL;
2067 GtkWidget *view_menu;
2068 GtkWidget *separator;
2069 QemuConsole *con;
2070 int vc;
2072 view_menu = gtk_menu_new();
2073 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2075 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2077 #if defined(CONFIG_VTE)
2078 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2079 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2080 #endif
2082 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2083 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2084 gtk_accel_label_set_accel(
2085 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2086 GDK_KEY_f, HOTKEY_MODIFIERS);
2087 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2089 separator = gtk_separator_menu_item_new();
2090 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2092 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2093 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2094 "<QEMU>/View/Zoom In");
2095 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2096 HOTKEY_MODIFIERS);
2097 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2098 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2099 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2101 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2102 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2103 "<QEMU>/View/Zoom Out");
2104 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2105 HOTKEY_MODIFIERS);
2106 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2108 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2109 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2110 "<QEMU>/View/Zoom Fixed");
2111 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2112 HOTKEY_MODIFIERS);
2113 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2115 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2116 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2118 separator = gtk_separator_menu_item_new();
2119 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2121 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2122 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2124 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2125 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2126 "<QEMU>/View/Grab Input");
2127 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2128 HOTKEY_MODIFIERS);
2129 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2131 separator = gtk_separator_menu_item_new();
2132 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2134 /* gfx */
2135 for (vc = 0;; vc++) {
2136 con = qemu_console_lookup_by_index(vc);
2137 if (!con) {
2138 break;
2140 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2141 vc, group, view_menu);
2142 s->nb_vcs++;
2145 #if defined(CONFIG_VTE)
2146 /* vte */
2147 gd_vcs_init(s, group, view_menu);
2148 #endif
2150 separator = gtk_separator_menu_item_new();
2151 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2153 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2154 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2156 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2157 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2159 s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
2160 _("Show Menubar"));
2161 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
2162 TRUE);
2163 gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
2164 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
2165 gtk_accel_label_set_accel(
2166 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
2167 GDK_KEY_m, HOTKEY_MODIFIERS);
2168 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
2170 return view_menu;
2173 static void gd_create_menus(GtkDisplayState *s)
2175 GtkSettings *settings;
2177 s->accel_group = gtk_accel_group_new();
2178 s->machine_menu = gd_create_menu_machine(s);
2179 s->view_menu = gd_create_menu_view(s);
2181 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2182 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2183 s->machine_menu);
2184 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2186 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2187 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2188 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2190 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2191 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2193 /* Disable the default "F10" menu shortcut. */
2194 settings = gtk_widget_get_settings(s->window);
2195 g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
2199 static gboolean gtkinit;
2201 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2203 VirtualConsole *vc;
2205 GtkDisplayState *s = g_malloc0(sizeof(*s));
2206 GdkDisplay *window_display;
2207 GtkIconTheme *theme;
2208 char *dir;
2210 if (!gtkinit) {
2211 fprintf(stderr, "gtk initialization failed\n");
2212 exit(1);
2214 assert(opts->type == DISPLAY_TYPE_GTK);
2215 s->opts = opts;
2217 theme = gtk_icon_theme_get_default();
2218 dir = get_relocated_path(CONFIG_QEMU_ICONDIR);
2219 gtk_icon_theme_prepend_search_path(theme, dir);
2220 g_free(dir);
2221 g_set_prgname("qemu");
2223 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2224 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2225 s->notebook = gtk_notebook_new();
2226 s->menu_bar = gtk_menu_bar_new();
2228 s->free_scale = FALSE;
2230 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2231 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2232 * printable, but without changing any of the character classes to make
2233 * sure that we don't accidentally break implicit assumptions. */
2234 setlocale(LC_MESSAGES, "");
2235 setlocale(LC_CTYPE, "C.UTF-8");
2236 dir = get_relocated_path(CONFIG_QEMU_LOCALEDIR);
2237 bindtextdomain("qemu", dir);
2238 g_free(dir);
2239 bind_textdomain_codeset("qemu", "UTF-8");
2240 textdomain("qemu");
2242 window_display = gtk_widget_get_display(s->window);
2243 if (s->opts->has_show_cursor && s->opts->show_cursor) {
2244 s->null_cursor = NULL; /* default pointer */
2245 } else {
2246 s->null_cursor = gdk_cursor_new_for_display(window_display,
2247 GDK_BLANK_CURSOR);
2250 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2251 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2252 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2254 gtk_window_set_icon_name(GTK_WINDOW(s->window), "qemu");
2256 gd_create_menus(s);
2258 gd_connect_signals(s);
2260 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2261 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2263 gd_update_caption(s);
2265 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2266 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2268 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2270 gtk_widget_show_all(s->window);
2272 vc = gd_vc_find_current(s);
2273 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2274 #ifdef CONFIG_VTE
2275 gtk_widget_set_sensitive(s->copy_item,
2276 vc && vc->type == GD_VC_VTE);
2277 #endif
2279 if (opts->has_full_screen &&
2280 opts->full_screen) {
2281 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2283 if (opts->u.gtk.has_grab_on_hover &&
2284 opts->u.gtk.grab_on_hover) {
2285 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2289 static void early_gtk_display_init(DisplayOptions *opts)
2291 /* The QEMU code relies on the assumption that it's always run in
2292 * the C locale. Therefore it is not prepared to deal with
2293 * operations that produce different results depending on the
2294 * locale, such as printf's formatting of decimal numbers, and
2295 * possibly others.
2297 * Since GTK+ calls setlocale() by default -importing the locale
2298 * settings from the environment- we must prevent it from doing so
2299 * using gtk_disable_setlocale().
2301 * QEMU's GTK+ UI, however, _does_ have translations for some of
2302 * the menu items. As a trade-off between a functionally correct
2303 * QEMU and a fully internationalized UI we support importing
2304 * LC_MESSAGES from the environment (see the setlocale() call
2305 * earlier in this file). This allows us to display translated
2306 * messages leaving everything else untouched.
2308 gtk_disable_setlocale();
2309 gtkinit = gtk_init_check(NULL, NULL);
2310 if (!gtkinit) {
2311 /* don't exit yet, that'll break -help */
2312 return;
2315 assert(opts->type == DISPLAY_TYPE_GTK);
2316 if (opts->has_gl && opts->gl != DISPLAYGL_MODE_OFF) {
2317 #if defined(CONFIG_OPENGL)
2318 #if defined(GDK_WINDOWING_WAYLAND)
2319 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2320 gtk_use_gl_area = true;
2321 gtk_gl_area_init();
2322 } else
2323 #endif
2325 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
2326 gtk_egl_init(mode);
2328 #endif
2331 keycode_map = gd_get_keymap(&keycode_maplen);
2333 #if defined(CONFIG_VTE)
2334 type_register(&char_gd_vc_type_info);
2335 #endif
2338 static QemuDisplay qemu_display_gtk = {
2339 .type = DISPLAY_TYPE_GTK,
2340 .early_init = early_gtk_display_init,
2341 .init = gtk_display_init,
2344 static void register_gtk(void)
2346 qemu_display_register(&qemu_display_gtk);
2349 type_init(register_gtk);