qcow2: Check snapshot L1 table in qcow2_snapshot_goto()
[qemu/ar7.git] / ui / gtk.c
blob563cff32b89613f032a4df77c41d37ee1a2a853c
1 /*
2 * GTK UI
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * Portions from gtk-vnc:
14 * GTK VNC Widget
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.0 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
37 #include "qemu/osdep.h"
38 #include "qemu-common.h"
39 #include "qapi/error.h"
40 #include "qapi/qapi-commands-misc.h"
41 #include "qemu/cutils.h"
43 #include "ui/console.h"
44 #include "ui/gtk.h"
46 #include <glib/gi18n.h>
47 #include <locale.h>
48 #if defined(CONFIG_VTE)
49 #include <vte/vte.h>
50 #endif
51 #include <math.h>
53 #include "trace.h"
54 #include "ui/input.h"
55 #include "sysemu/sysemu.h"
56 #include "keymaps.h"
57 #include "chardev/char.h"
58 #include "qom/object.h"
60 #define MAX_VCS 10
61 #define VC_WINDOW_X_MIN 320
62 #define VC_WINDOW_Y_MIN 240
63 #define VC_TERM_X_MIN 80
64 #define VC_TERM_Y_MIN 25
65 #define VC_SCALE_MIN 0.25
66 #define VC_SCALE_STEP 0.25
68 #ifdef GDK_WINDOWING_X11
69 #include "ui/x_keymap.h"
71 /* Gtk2 compat */
72 #ifndef GDK_IS_X11_DISPLAY
73 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
74 #endif
75 #endif
78 #ifdef GDK_WINDOWING_WAYLAND
79 /* Gtk2 compat */
80 #ifndef GDK_IS_WAYLAND_DISPLAY
81 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
82 #endif
83 #endif
86 #ifdef GDK_WINDOWING_WIN32
87 /* Gtk2 compat */
88 #ifndef GDK_IS_WIN32_DISPLAY
89 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
90 #endif
91 #endif
94 #ifdef GDK_WINDOWING_BROADWAY
95 /* Gtk2 compat */
96 #ifndef GDK_IS_BROADWAY_DISPLAY
97 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
98 #endif
99 #endif
102 #ifdef GDK_WINDOWING_QUARTZ
103 /* Gtk2 compat */
104 #ifndef GDK_IS_QUARTZ_DISPLAY
105 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
106 #endif
107 #endif
110 #if !defined(CONFIG_VTE)
111 # define VTE_CHECK_VERSION(a, b, c) 0
112 #endif
114 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
116 * The gtk2 vte terminal widget seriously messes up the window resize
117 * for some reason. You basically can't make the qemu window smaller
118 * any more because the toplevel window geoemtry hints are overridden.
120 * Workaround that by hiding all vte widgets, except the one in the
121 * current tab.
123 * Luckily everything works smooth in gtk3.
125 # define VTE_RESIZE_HACK 1
126 #endif
128 #if !GTK_CHECK_VERSION(2, 20, 0)
129 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
130 #endif
132 #ifndef GDK_IS_X11_DISPLAY
133 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
134 #endif
135 #ifndef GDK_IS_WAYLAND_DISPLAY
136 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
137 #endif
138 #ifndef GDK_IS_WIN32_DISPLAY
139 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
140 #endif
142 #if !GTK_CHECK_VERSION(2, 22, 0)
143 #define GDK_KEY_0 GDK_0
144 #define GDK_KEY_1 GDK_1
145 #define GDK_KEY_2 GDK_2
146 #define GDK_KEY_f GDK_f
147 #define GDK_KEY_g GDK_g
148 #define GDK_KEY_q GDK_q
149 #define GDK_KEY_plus GDK_plus
150 #define GDK_KEY_equal GDK_equal
151 #define GDK_KEY_minus GDK_minus
152 #define GDK_KEY_Pause GDK_Pause
153 #define GDK_KEY_Delete GDK_Delete
154 #endif
156 /* Some older mingw versions lack this constant or have
157 * it conditionally defined */
158 #ifdef _WIN32
159 # ifndef MAPVK_VK_TO_VSC
160 # define MAPVK_VK_TO_VSC 0
161 # endif
162 #endif
165 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
167 static const int modifier_keycode[] = {
168 Q_KEY_CODE_SHIFT,
169 Q_KEY_CODE_SHIFT_R,
170 Q_KEY_CODE_CTRL,
171 Q_KEY_CODE_CTRL_R,
172 Q_KEY_CODE_ALT,
173 Q_KEY_CODE_ALT_R,
174 Q_KEY_CODE_META_L,
175 Q_KEY_CODE_META_R,
178 static const guint16 *keycode_map;
179 static size_t keycode_maplen;
181 struct GtkDisplayState {
182 GtkWidget *window;
184 GtkWidget *menu_bar;
186 GtkAccelGroup *accel_group;
188 GtkWidget *machine_menu_item;
189 GtkWidget *machine_menu;
190 GtkWidget *pause_item;
191 GtkWidget *reset_item;
192 GtkWidget *powerdown_item;
193 GtkWidget *quit_item;
195 GtkWidget *view_menu_item;
196 GtkWidget *view_menu;
197 GtkWidget *full_screen_item;
198 GtkWidget *copy_item;
199 GtkWidget *zoom_in_item;
200 GtkWidget *zoom_out_item;
201 GtkWidget *zoom_fixed_item;
202 GtkWidget *zoom_fit_item;
203 GtkWidget *grab_item;
204 GtkWidget *grab_on_hover_item;
206 int nb_vcs;
207 VirtualConsole vc[MAX_VCS];
209 GtkWidget *show_tabs_item;
210 GtkWidget *untabify_item;
212 GtkWidget *vbox;
213 GtkWidget *notebook;
214 int button_mask;
215 gboolean last_set;
216 int last_x;
217 int last_y;
218 int grab_x_root;
219 int grab_y_root;
220 VirtualConsole *kbd_owner;
221 VirtualConsole *ptr_owner;
223 gboolean full_screen;
225 GdkCursor *null_cursor;
226 Notifier mouse_mode_notifier;
227 gboolean free_scale;
229 bool external_pause_update;
231 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
232 bool ignore_keys;
234 DisplayOptions *opts;
237 typedef struct VCChardev {
238 Chardev parent;
239 VirtualConsole *console;
240 bool echo;
241 } VCChardev;
243 #define TYPE_CHARDEV_VC "chardev-vc"
244 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
246 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
247 static void gd_ungrab_pointer(GtkDisplayState *s);
248 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
249 static void gd_ungrab_keyboard(GtkDisplayState *s);
251 /** Utility Functions **/
253 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
255 VirtualConsole *vc;
256 gint i;
258 for (i = 0; i < s->nb_vcs; i++) {
259 vc = &s->vc[i];
260 if (gtk_check_menu_item_get_active
261 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
262 return vc;
265 return NULL;
268 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
270 VirtualConsole *vc;
271 gint i, p;
273 for (i = 0; i < s->nb_vcs; i++) {
274 vc = &s->vc[i];
275 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
276 if (p == page) {
277 return vc;
280 return NULL;
283 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
285 gint page;
287 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
288 return gd_vc_find_by_page(s, page);
291 static bool gd_is_grab_active(GtkDisplayState *s)
293 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
296 static bool gd_grab_on_hover(GtkDisplayState *s)
298 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
301 static void gd_update_cursor(VirtualConsole *vc)
303 GtkDisplayState *s = vc->s;
304 GdkWindow *window;
306 if (vc->type != GD_VC_GFX ||
307 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
308 return;
311 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
312 return;
315 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
316 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
317 gdk_window_set_cursor(window, s->null_cursor);
318 } else {
319 gdk_window_set_cursor(window, NULL);
323 static void gd_update_caption(GtkDisplayState *s)
325 const char *status = "";
326 gchar *prefix;
327 gchar *title;
328 const char *grab = "";
329 bool is_paused = !runstate_is_running();
330 int i;
332 if (qemu_name) {
333 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
334 } else {
335 prefix = g_strdup_printf("QEMU");
338 if (s->ptr_owner != NULL &&
339 s->ptr_owner->window == NULL) {
340 grab = _(" - Press Ctrl+Alt+G to release grab");
343 if (is_paused) {
344 status = _(" [Paused]");
346 s->external_pause_update = true;
347 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
348 is_paused);
349 s->external_pause_update = false;
351 title = g_strdup_printf("%s%s%s", prefix, status, grab);
352 gtk_window_set_title(GTK_WINDOW(s->window), title);
353 g_free(title);
355 for (i = 0; i < s->nb_vcs; i++) {
356 VirtualConsole *vc = &s->vc[i];
358 if (!vc->window) {
359 continue;
361 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
362 vc == s->kbd_owner ? " +kbd" : "",
363 vc == s->ptr_owner ? " +ptr" : "");
364 gtk_window_set_title(GTK_WINDOW(vc->window), title);
365 g_free(title);
368 g_free(prefix);
371 static void gd_update_geometry_hints(VirtualConsole *vc)
373 GtkDisplayState *s = vc->s;
374 GdkWindowHints mask = 0;
375 GdkGeometry geo = {};
376 GtkWidget *geo_widget = NULL;
377 GtkWindow *geo_window;
379 if (vc->type == GD_VC_GFX) {
380 if (!vc->gfx.ds) {
381 return;
383 if (s->free_scale) {
384 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
385 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
386 mask |= GDK_HINT_MIN_SIZE;
387 } else {
388 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
389 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
390 mask |= GDK_HINT_MIN_SIZE;
392 geo_widget = vc->gfx.drawing_area;
393 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
395 #if defined(CONFIG_VTE)
396 } else if (vc->type == GD_VC_VTE) {
397 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
398 GtkBorder padding = { 0 };
400 #if VTE_CHECK_VERSION(0, 37, 0)
401 gtk_style_context_get_padding(
402 gtk_widget_get_style_context(vc->vte.terminal),
403 gtk_widget_get_state_flags(vc->vte.terminal),
404 &padding);
405 #else
407 GtkBorder *ib = NULL;
408 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
409 if (ib) {
410 padding = *ib;
411 gtk_border_free(ib);
414 #endif
416 geo.width_inc = vte_terminal_get_char_width(term);
417 geo.height_inc = vte_terminal_get_char_height(term);
418 mask |= GDK_HINT_RESIZE_INC;
419 geo.base_width = geo.width_inc;
420 geo.base_height = geo.height_inc;
421 mask |= GDK_HINT_BASE_SIZE;
422 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
423 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
424 mask |= GDK_HINT_MIN_SIZE;
426 geo.base_width += padding.left + padding.right;
427 geo.base_height += padding.top + padding.bottom;
428 geo.min_width += padding.left + padding.right;
429 geo.min_height += padding.top + padding.bottom;
430 geo_widget = vc->vte.terminal;
431 #endif
434 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
435 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
438 void gd_update_windowsize(VirtualConsole *vc)
440 GtkDisplayState *s = vc->s;
442 gd_update_geometry_hints(vc);
444 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
445 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
446 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
450 static void gd_update_full_redraw(VirtualConsole *vc)
452 GtkWidget *area = vc->gfx.drawing_area;
453 int ww, wh;
454 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
455 #if defined(CONFIG_GTK_GL)
456 if (vc->gfx.gls) {
457 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
458 return;
460 #endif
461 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
464 static void gtk_release_modifiers(GtkDisplayState *s)
466 VirtualConsole *vc = gd_vc_find_current(s);
467 int i, qcode;
469 if (vc->type != GD_VC_GFX ||
470 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
471 return;
473 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
474 qcode = modifier_keycode[i];
475 if (!s->modifier_pressed[i]) {
476 continue;
478 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode, false);
479 s->modifier_pressed[i] = false;
483 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
484 GtkWidget *widget)
486 g_object_ref(G_OBJECT(widget));
487 gtk_container_remove(GTK_CONTAINER(from), widget);
488 gtk_container_add(GTK_CONTAINER(to), widget);
489 g_object_unref(G_OBJECT(widget));
492 /** DisplayState Callbacks **/
494 static void gd_update(DisplayChangeListener *dcl,
495 int x, int y, int w, int h)
497 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
498 GdkWindow *win;
499 int x1, x2, y1, y2;
500 int mx, my;
501 int fbw, fbh;
502 int ww, wh;
504 trace_gd_update(vc->label, x, y, w, h);
506 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
507 return;
510 if (vc->gfx.convert) {
511 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
512 NULL, vc->gfx.convert,
513 x, y, 0, 0, x, y, w, h);
516 x1 = floor(x * vc->gfx.scale_x);
517 y1 = floor(y * vc->gfx.scale_y);
519 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
520 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
522 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
523 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
525 win = gtk_widget_get_window(vc->gfx.drawing_area);
526 if (!win) {
527 return;
529 gdk_drawable_get_size(win, &ww, &wh);
531 mx = my = 0;
532 if (ww > fbw) {
533 mx = (ww - fbw) / 2;
535 if (wh > fbh) {
536 my = (wh - fbh) / 2;
539 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
540 mx + x1, my + y1, (x2 - x1), (y2 - y1));
543 static void gd_refresh(DisplayChangeListener *dcl)
545 graphic_hw_update(dcl->con);
548 #if GTK_CHECK_VERSION(3, 0, 0)
549 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
551 #if GTK_CHECK_VERSION(3, 20, 0)
552 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
553 #else
554 return gdk_device_manager_get_client_pointer(
555 gdk_display_get_device_manager(dpy));
556 #endif
559 static void gd_mouse_set(DisplayChangeListener *dcl,
560 int x, int y, int visible)
562 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
563 GdkDisplay *dpy;
564 gint x_root, y_root;
566 if (qemu_input_is_absolute()) {
567 return;
570 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
571 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
572 x, y, &x_root, &y_root);
573 gdk_device_warp(gd_get_pointer(dpy),
574 gtk_widget_get_screen(vc->gfx.drawing_area),
575 x_root, y_root);
576 vc->s->last_x = x;
577 vc->s->last_y = y;
579 #else
580 static void gd_mouse_set(DisplayChangeListener *dcl,
581 int x, int y, int visible)
583 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
584 gint x_root, y_root;
586 if (qemu_input_is_absolute()) {
587 return;
590 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
591 x, y, &x_root, &y_root);
592 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
593 gtk_widget_get_screen(vc->gfx.drawing_area),
594 x_root, y_root);
596 #endif
598 static void gd_cursor_define(DisplayChangeListener *dcl,
599 QEMUCursor *c)
601 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
602 GdkPixbuf *pixbuf;
603 GdkCursor *cursor;
605 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
606 return;
609 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
610 GDK_COLORSPACE_RGB, true, 8,
611 c->width, c->height, c->width * 4,
612 NULL, NULL);
613 cursor = gdk_cursor_new_from_pixbuf
614 (gtk_widget_get_display(vc->gfx.drawing_area),
615 pixbuf, c->hot_x, c->hot_y);
616 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
617 g_object_unref(pixbuf);
618 #if !GTK_CHECK_VERSION(3, 0, 0)
619 gdk_cursor_unref(cursor);
620 #else
621 g_object_unref(cursor);
622 #endif
625 static void gd_switch(DisplayChangeListener *dcl,
626 DisplaySurface *surface)
628 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
629 bool resized = true;
631 trace_gd_switch(vc->label,
632 surface ? surface_width(surface) : 0,
633 surface ? surface_height(surface) : 0);
635 if (vc->gfx.surface) {
636 cairo_surface_destroy(vc->gfx.surface);
637 vc->gfx.surface = NULL;
639 if (vc->gfx.convert) {
640 pixman_image_unref(vc->gfx.convert);
641 vc->gfx.convert = NULL;
644 if (vc->gfx.ds && surface &&
645 surface_width(vc->gfx.ds) == surface_width(surface) &&
646 surface_height(vc->gfx.ds) == surface_height(surface)) {
647 resized = false;
649 vc->gfx.ds = surface;
651 if (!surface) {
652 return;
655 if (surface->format == PIXMAN_x8r8g8b8) {
657 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
659 * No need to convert, use surface directly. Should be the
660 * common case as this is qemu_default_pixelformat(32) too.
662 vc->gfx.surface = cairo_image_surface_create_for_data
663 (surface_data(surface),
664 CAIRO_FORMAT_RGB24,
665 surface_width(surface),
666 surface_height(surface),
667 surface_stride(surface));
668 } else {
669 /* Must convert surface, use pixman to do it. */
670 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
671 surface_width(surface),
672 surface_height(surface),
673 NULL, 0);
674 vc->gfx.surface = cairo_image_surface_create_for_data
675 ((void *)pixman_image_get_data(vc->gfx.convert),
676 CAIRO_FORMAT_RGB24,
677 pixman_image_get_width(vc->gfx.convert),
678 pixman_image_get_height(vc->gfx.convert),
679 pixman_image_get_stride(vc->gfx.convert));
680 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
681 NULL, vc->gfx.convert,
682 0, 0, 0, 0, 0, 0,
683 pixman_image_get_width(vc->gfx.convert),
684 pixman_image_get_height(vc->gfx.convert));
687 if (resized) {
688 gd_update_windowsize(vc);
689 } else {
690 gd_update_full_redraw(vc);
694 static const DisplayChangeListenerOps dcl_ops = {
695 .dpy_name = "gtk",
696 .dpy_gfx_update = gd_update,
697 .dpy_gfx_switch = gd_switch,
698 .dpy_gfx_check_format = qemu_pixman_check_format,
699 .dpy_refresh = gd_refresh,
700 .dpy_mouse_set = gd_mouse_set,
701 .dpy_cursor_define = gd_cursor_define,
705 #if defined(CONFIG_OPENGL)
707 /** DisplayState Callbacks (opengl version) **/
709 #if defined(CONFIG_GTK_GL)
711 static const DisplayChangeListenerOps dcl_gl_area_ops = {
712 .dpy_name = "gtk-egl",
713 .dpy_gfx_update = gd_gl_area_update,
714 .dpy_gfx_switch = gd_gl_area_switch,
715 .dpy_gfx_check_format = console_gl_check_format,
716 .dpy_refresh = gd_gl_area_refresh,
717 .dpy_mouse_set = gd_mouse_set,
718 .dpy_cursor_define = gd_cursor_define,
720 .dpy_gl_ctx_create = gd_gl_area_create_context,
721 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
722 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
723 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
724 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
725 .dpy_gl_update = gd_gl_area_scanout_flush,
728 #else
730 static const DisplayChangeListenerOps dcl_egl_ops = {
731 .dpy_name = "gtk-egl",
732 .dpy_gfx_update = gd_egl_update,
733 .dpy_gfx_switch = gd_egl_switch,
734 .dpy_gfx_check_format = console_gl_check_format,
735 .dpy_refresh = gd_egl_refresh,
736 .dpy_mouse_set = gd_mouse_set,
737 .dpy_cursor_define = gd_cursor_define,
739 .dpy_gl_ctx_create = gd_egl_create_context,
740 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
741 .dpy_gl_ctx_make_current = gd_egl_make_current,
742 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
743 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
744 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
745 .dpy_gl_update = gd_egl_scanout_flush,
748 #endif /* CONFIG_GTK_GL */
749 #endif /* CONFIG_OPENGL */
751 /** QEMU Events **/
753 static void gd_change_runstate(void *opaque, int running, RunState state)
755 GtkDisplayState *s = opaque;
757 gd_update_caption(s);
760 static void gd_mouse_mode_change(Notifier *notify, void *data)
762 GtkDisplayState *s;
763 int i;
765 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
766 /* release the grab at switching to absolute mode */
767 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
768 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
769 FALSE);
771 for (i = 0; i < s->nb_vcs; i++) {
772 VirtualConsole *vc = &s->vc[i];
773 gd_update_cursor(vc);
777 /** GTK Events **/
779 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
780 void *opaque)
782 GtkDisplayState *s = opaque;
783 bool allow_close = true;
784 int i;
786 if (s->opts->has_window_close && !s->opts->window_close) {
787 allow_close = false;
790 if (allow_close) {
791 for (i = 0; i < s->nb_vcs; i++) {
792 if (s->vc[i].type != GD_VC_GFX) {
793 continue;
795 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
797 qmp_quit(NULL);
798 return FALSE;
801 return TRUE;
804 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
806 QemuUIInfo info;
808 memset(&info, 0, sizeof(info));
809 info.width = width;
810 info.height = height;
811 dpy_set_ui_info(vc->gfx.dcl.con, &info);
814 #if defined(CONFIG_GTK_GL)
816 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
817 void *opaque)
819 VirtualConsole *vc = opaque;
821 if (vc->gfx.gls) {
822 gd_gl_area_draw(vc);
824 return TRUE;
827 static void gd_resize_event(GtkGLArea *area,
828 gint width, gint height, gpointer *opaque)
830 VirtualConsole *vc = (void *)opaque;
832 gd_set_ui_info(vc, width, height);
835 #endif
837 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
839 VirtualConsole *vc = opaque;
840 GtkDisplayState *s = vc->s;
841 int mx, my;
842 int ww, wh;
843 int fbw, fbh;
845 #if defined(CONFIG_OPENGL)
846 if (vc->gfx.gls) {
847 #if defined(CONFIG_GTK_GL)
848 /* invoke render callback please */
849 return FALSE;
850 #else
851 gd_egl_draw(vc);
852 return TRUE;
853 #endif
855 #endif
857 if (!gtk_widget_get_realized(widget)) {
858 return FALSE;
860 if (!vc->gfx.ds) {
861 return FALSE;
864 fbw = surface_width(vc->gfx.ds);
865 fbh = surface_height(vc->gfx.ds);
867 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
869 if (s->full_screen) {
870 vc->gfx.scale_x = (double)ww / fbw;
871 vc->gfx.scale_y = (double)wh / fbh;
872 } else if (s->free_scale) {
873 double sx, sy;
875 sx = (double)ww / fbw;
876 sy = (double)wh / fbh;
878 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
881 fbw *= vc->gfx.scale_x;
882 fbh *= vc->gfx.scale_y;
884 mx = my = 0;
885 if (ww > fbw) {
886 mx = (ww - fbw) / 2;
888 if (wh > fbh) {
889 my = (wh - fbh) / 2;
892 cairo_rectangle(cr, 0, 0, ww, wh);
894 /* Optionally cut out the inner area where the pixmap
895 will be drawn. This avoids 'flashing' since we're
896 not double-buffering. Note we're using the undocumented
897 behaviour of drawing the rectangle from right to left
898 to cut out the whole */
899 cairo_rectangle(cr, mx + fbw, my,
900 -1 * fbw, fbh);
901 cairo_fill(cr);
903 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
904 cairo_set_source_surface(cr, vc->gfx.surface,
905 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
906 cairo_paint(cr);
908 return TRUE;
911 #if !GTK_CHECK_VERSION(3, 0, 0)
912 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
913 void *opaque)
915 cairo_t *cr;
916 gboolean ret;
918 cr = gdk_cairo_create(gtk_widget_get_window(widget));
919 cairo_rectangle(cr,
920 expose->area.x,
921 expose->area.y,
922 expose->area.width,
923 expose->area.height);
924 cairo_clip(cr);
926 ret = gd_draw_event(widget, cr, opaque);
928 cairo_destroy(cr);
930 return ret;
932 #endif
934 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
935 void *opaque)
937 VirtualConsole *vc = opaque;
938 GtkDisplayState *s = vc->s;
939 int x, y;
940 int mx, my;
941 int fbh, fbw;
942 int ww, wh;
944 if (!vc->gfx.ds) {
945 return TRUE;
948 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
949 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
951 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
952 &ww, &wh);
954 mx = my = 0;
955 if (ww > fbw) {
956 mx = (ww - fbw) / 2;
958 if (wh > fbh) {
959 my = (wh - fbh) / 2;
962 x = (motion->x - mx) / vc->gfx.scale_x;
963 y = (motion->y - my) / vc->gfx.scale_y;
965 if (qemu_input_is_absolute()) {
966 if (x < 0 || y < 0 ||
967 x >= surface_width(vc->gfx.ds) ||
968 y >= surface_height(vc->gfx.ds)) {
969 return TRUE;
971 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
972 0, surface_width(vc->gfx.ds));
973 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
974 0, surface_height(vc->gfx.ds));
975 qemu_input_event_sync();
976 } else if (s->last_set && s->ptr_owner == vc) {
977 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
978 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
979 qemu_input_event_sync();
981 s->last_x = x;
982 s->last_y = y;
983 s->last_set = TRUE;
985 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
986 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
987 int screen_width, screen_height;
989 int x = (int)motion->x_root;
990 int y = (int)motion->y_root;
992 #if GTK_CHECK_VERSION(3, 22, 0)
994 GdkDisplay *dpy = gtk_widget_get_display(widget);
995 GdkWindow *win = gtk_widget_get_window(widget);
996 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
997 GdkRectangle geometry;
998 gdk_monitor_get_geometry(monitor, &geometry);
999 screen_width = geometry.width;
1000 screen_height = geometry.height;
1002 #else
1004 screen_width = gdk_screen_get_width(screen);
1005 screen_height = gdk_screen_get_height(screen);
1007 #endif
1009 /* In relative mode check to see if client pointer hit
1010 * one of the screen edges, and if so move it back by
1011 * 200 pixels. This is important because the pointer
1012 * in the server doesn't correspond 1-for-1, and so
1013 * may still be only half way across the screen. Without
1014 * this warp, the server pointer would thus appear to hit
1015 * an invisible wall */
1016 if (x == 0) {
1017 x += 200;
1019 if (y == 0) {
1020 y += 200;
1022 if (x == (screen_width - 1)) {
1023 x -= 200;
1025 if (y == (screen_height - 1)) {
1026 y -= 200;
1029 if (x != (int)motion->x_root || y != (int)motion->y_root) {
1030 #if GTK_CHECK_VERSION(3, 0, 0)
1031 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
1032 gdk_device_warp(dev, screen, x, y);
1033 #else
1034 GdkDisplay *display = gtk_widget_get_display(widget);
1035 gdk_display_warp_pointer(display, screen, x, y);
1036 #endif
1037 s->last_set = FALSE;
1038 return FALSE;
1041 return TRUE;
1044 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
1045 void *opaque)
1047 VirtualConsole *vc = opaque;
1048 GtkDisplayState *s = vc->s;
1049 InputButton btn;
1051 /* implicitly grab the input at the first click in the relative mode */
1052 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
1053 !qemu_input_is_absolute() && s->ptr_owner != vc) {
1054 if (!vc->window) {
1055 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1056 TRUE);
1057 } else {
1058 gd_grab_pointer(vc, "relative-mode-click");
1060 return TRUE;
1063 if (button->button == 1) {
1064 btn = INPUT_BUTTON_LEFT;
1065 } else if (button->button == 2) {
1066 btn = INPUT_BUTTON_MIDDLE;
1067 } else if (button->button == 3) {
1068 btn = INPUT_BUTTON_RIGHT;
1069 } else if (button->button == 8) {
1070 btn = INPUT_BUTTON_SIDE;
1071 } else if (button->button == 9) {
1072 btn = INPUT_BUTTON_EXTRA;
1073 } else {
1074 return TRUE;
1077 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1078 button->type == GDK_BUTTON_PRESS);
1079 qemu_input_event_sync();
1080 return TRUE;
1083 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1084 void *opaque)
1086 VirtualConsole *vc = opaque;
1087 InputButton btn;
1089 if (scroll->direction == GDK_SCROLL_UP) {
1090 btn = INPUT_BUTTON_WHEEL_UP;
1091 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1092 btn = INPUT_BUTTON_WHEEL_DOWN;
1093 #if GTK_CHECK_VERSION(3, 4, 0)
1094 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1095 gdouble delta_x, delta_y;
1096 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1097 &delta_x, &delta_y)) {
1098 return TRUE;
1100 if (delta_y > 0) {
1101 btn = INPUT_BUTTON_WHEEL_DOWN;
1102 } else {
1103 btn = INPUT_BUTTON_WHEEL_UP;
1105 #endif
1106 } else {
1107 return TRUE;
1110 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1111 qemu_input_event_sync();
1112 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1113 qemu_input_event_sync();
1114 return TRUE;
1118 static const guint16 *gd_get_keymap(size_t *maplen)
1120 GdkDisplay *dpy = gdk_display_get_default();
1122 #ifdef GDK_WINDOWING_X11
1123 if (GDK_IS_X11_DISPLAY(dpy)) {
1124 trace_gd_keymap_windowing("x11");
1125 return qemu_xkeymap_mapping_table(
1126 gdk_x11_display_get_xdisplay(dpy), maplen);
1128 #endif
1130 #ifdef GDK_WINDOWING_WAYLAND
1131 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1132 trace_gd_keymap_windowing("wayland");
1133 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1134 return qemu_input_map_xorgevdev_to_qcode;
1136 #endif
1138 #ifdef GDK_WINDOWING_WIN32
1139 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1140 trace_gd_keymap_windowing("win32");
1141 *maplen = qemu_input_map_win32_to_qcode_len;
1142 return qemu_input_map_win32_to_qcode;
1144 #endif
1146 #ifdef GDK_WINDOWING_QUARTZ
1147 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1148 trace_gd_keymap_windowing("quartz");
1149 *maplen = qemu_input_map_osx_to_qcode_len;
1150 return qemu_input_map_osx_to_qcode;
1152 #endif
1154 #ifdef GDK_WINDOWING_BROADWAY
1155 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1156 trace_gd_keymap_windowing("broadway");
1157 g_warning("experimental: using broadway, x11 virtual keysym\n"
1158 "mapping - with very limited support. See also\n"
1159 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1160 *maplen = qemu_input_map_x11_to_qcode_len;
1161 return qemu_input_map_x11_to_qcode;
1163 #endif
1165 g_warning("Unsupported GDK Windowing platform.\n"
1166 "Disabling extended keycode tables.\n"
1167 "Please report to qemu-devel@nongnu.org\n"
1168 "including the following information:\n"
1169 "\n"
1170 " - Operating system\n"
1171 " - GDK Windowing system build\n");
1172 return NULL;
1176 static int gd_map_keycode(int scancode)
1178 if (!keycode_map) {
1179 return 0;
1181 if (scancode > keycode_maplen) {
1182 return 0;
1185 return keycode_map[scancode];
1188 static gboolean gd_text_key_down(GtkWidget *widget,
1189 GdkEventKey *key, void *opaque)
1191 VirtualConsole *vc = opaque;
1192 QemuConsole *con = vc->gfx.dcl.con;
1194 if (key->keyval == GDK_KEY_Delete) {
1195 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1196 } else if (key->length) {
1197 kbd_put_string_console(con, key->string, key->length);
1198 } else {
1199 int qcode = gd_map_keycode(key->hardware_keycode);
1200 kbd_put_qcode_console(con, qcode);
1202 return TRUE;
1205 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1207 VirtualConsole *vc = opaque;
1208 GtkDisplayState *s = vc->s;
1209 int qcode;
1210 int i;
1212 if (s->ignore_keys) {
1213 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1214 return TRUE;
1217 #ifdef WIN32
1218 /* on windows, we ought to ignore the reserved key event? */
1219 if (key->hardware_keycode == 0xff)
1220 return false;
1221 #endif
1223 if (key->keyval == GDK_KEY_Pause
1224 #ifdef G_OS_WIN32
1225 /* for some reason GDK does not fill keyval for VK_PAUSE
1226 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1228 || key->hardware_keycode == VK_PAUSE
1229 #endif
1231 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1232 key->type == GDK_KEY_PRESS);
1233 return TRUE;
1236 qcode = gd_map_keycode(key->hardware_keycode);
1238 trace_gd_key_event(vc->label, key->hardware_keycode, qcode,
1239 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1241 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1242 if (qcode == modifier_keycode[i]) {
1243 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1247 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode,
1248 key->type == GDK_KEY_PRESS);
1250 return TRUE;
1253 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1255 if (event->type == GDK_MOTION_NOTIFY) {
1256 return gd_motion_event(widget, &event->motion, opaque);
1258 return FALSE;
1261 /** Window Menu Actions **/
1263 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1265 GtkDisplayState *s = opaque;
1267 if (s->external_pause_update) {
1268 return;
1270 if (runstate_is_running()) {
1271 qmp_stop(NULL);
1272 } else {
1273 qmp_cont(NULL);
1277 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1279 qmp_system_reset(NULL);
1282 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1284 qmp_system_powerdown(NULL);
1287 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1289 qmp_quit(NULL);
1292 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1294 GtkDisplayState *s = opaque;
1295 VirtualConsole *vc = gd_vc_find_by_menu(s);
1296 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1297 gint page;
1299 gtk_release_modifiers(s);
1300 if (vc) {
1301 page = gtk_notebook_page_num(nb, vc->tab_item);
1302 gtk_notebook_set_current_page(nb, page);
1303 gtk_widget_grab_focus(vc->focus);
1305 s->ignore_keys = false;
1308 static void gd_accel_switch_vc(void *opaque)
1310 VirtualConsole *vc = opaque;
1312 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1313 #if !GTK_CHECK_VERSION(3, 0, 0)
1314 /* GTK2 sends the accel key to the target console - ignore this until */
1315 vc->s->ignore_keys = true;
1316 #endif
1319 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1321 GtkDisplayState *s = opaque;
1322 VirtualConsole *vc = gd_vc_find_current(s);
1324 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1325 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1326 } else {
1327 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1329 gd_update_windowsize(vc);
1332 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1333 void *opaque)
1335 VirtualConsole *vc = opaque;
1336 GtkDisplayState *s = vc->s;
1338 gtk_widget_set_sensitive(vc->menu_item, true);
1339 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1340 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1341 vc->tab_item, vc->label);
1342 gtk_widget_destroy(vc->window);
1343 vc->window = NULL;
1344 return TRUE;
1347 static gboolean gd_win_grab(void *opaque)
1349 VirtualConsole *vc = opaque;
1351 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1352 if (vc->s->ptr_owner) {
1353 gd_ungrab_pointer(vc->s);
1354 } else {
1355 gd_grab_pointer(vc, "user-request-detached-tab");
1357 return TRUE;
1360 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1362 GtkDisplayState *s = opaque;
1363 VirtualConsole *vc = gd_vc_find_current(s);
1365 if (vc->type == GD_VC_GFX &&
1366 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1367 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1368 FALSE);
1370 if (!vc->window) {
1371 gtk_widget_set_sensitive(vc->menu_item, false);
1372 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1373 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1375 g_signal_connect(vc->window, "delete-event",
1376 G_CALLBACK(gd_tab_window_close), vc);
1377 gtk_widget_show_all(vc->window);
1379 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1380 GtkAccelGroup *ag = gtk_accel_group_new();
1381 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1383 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1384 vc, NULL);
1385 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1388 gd_update_geometry_hints(vc);
1389 gd_update_caption(s);
1393 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1395 GtkDisplayState *s = opaque;
1396 VirtualConsole *vc = gd_vc_find_current(s);
1398 if (!s->full_screen) {
1399 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1400 gtk_widget_hide(s->menu_bar);
1401 if (vc->type == GD_VC_GFX) {
1402 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1404 gtk_window_fullscreen(GTK_WINDOW(s->window));
1405 s->full_screen = TRUE;
1406 } else {
1407 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1408 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1409 gtk_widget_show(s->menu_bar);
1410 s->full_screen = FALSE;
1411 if (vc->type == GD_VC_GFX) {
1412 vc->gfx.scale_x = 1.0;
1413 vc->gfx.scale_y = 1.0;
1414 gd_update_windowsize(vc);
1418 gd_update_cursor(vc);
1421 static void gd_accel_full_screen(void *opaque)
1423 GtkDisplayState *s = opaque;
1424 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1427 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1429 GtkDisplayState *s = opaque;
1430 VirtualConsole *vc = gd_vc_find_current(s);
1432 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1433 FALSE);
1435 vc->gfx.scale_x += VC_SCALE_STEP;
1436 vc->gfx.scale_y += VC_SCALE_STEP;
1438 gd_update_windowsize(vc);
1441 static void gd_accel_zoom_in(void *opaque)
1443 GtkDisplayState *s = opaque;
1444 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1447 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1449 GtkDisplayState *s = opaque;
1450 VirtualConsole *vc = gd_vc_find_current(s);
1452 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1453 FALSE);
1455 vc->gfx.scale_x -= VC_SCALE_STEP;
1456 vc->gfx.scale_y -= VC_SCALE_STEP;
1458 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1459 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1461 gd_update_windowsize(vc);
1464 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1466 GtkDisplayState *s = opaque;
1467 VirtualConsole *vc = gd_vc_find_current(s);
1469 vc->gfx.scale_x = 1.0;
1470 vc->gfx.scale_y = 1.0;
1472 gd_update_windowsize(vc);
1475 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1477 GtkDisplayState *s = opaque;
1478 VirtualConsole *vc = gd_vc_find_current(s);
1480 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1481 s->free_scale = TRUE;
1482 } else {
1483 s->free_scale = FALSE;
1484 vc->gfx.scale_x = 1.0;
1485 vc->gfx.scale_y = 1.0;
1488 gd_update_windowsize(vc);
1489 gd_update_full_redraw(vc);
1492 #if GTK_CHECK_VERSION(3, 20, 0)
1493 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1495 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1496 GdkSeat *seat = gdk_display_get_default_seat(display);
1497 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1498 GdkSeatCapabilities caps = 0;
1499 GdkCursor *cursor = NULL;
1501 if (kbd) {
1502 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1504 if (ptr) {
1505 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1506 cursor = vc->s->null_cursor;
1509 if (caps) {
1510 gdk_seat_grab(seat, window, caps, false, cursor,
1511 NULL, NULL, NULL);
1512 } else {
1513 gdk_seat_ungrab(seat);
1516 #elif GTK_CHECK_VERSION(3, 0, 0)
1517 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1518 GdkInputSource source, GdkEventMask mask,
1519 GdkCursor *cursor)
1521 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1522 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1523 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1524 GList *tmp = devs;
1526 for (tmp = devs; tmp; tmp = tmp->next) {
1527 GdkDevice *dev = tmp->data;
1528 if (gdk_device_get_source(dev) != source) {
1529 continue;
1531 if (grab) {
1532 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1533 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1534 mask, cursor, GDK_CURRENT_TIME);
1535 } else {
1536 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1539 g_list_free(devs);
1541 #endif
1543 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1545 if (vc->s->kbd_owner) {
1546 if (vc->s->kbd_owner == vc) {
1547 return;
1548 } else {
1549 gd_ungrab_keyboard(vc->s);
1553 #if GTK_CHECK_VERSION(3, 20, 0)
1554 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1555 #elif GTK_CHECK_VERSION(3, 0, 0)
1556 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1557 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1558 NULL);
1559 #else
1560 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1561 FALSE,
1562 GDK_CURRENT_TIME);
1563 #endif
1564 vc->s->kbd_owner = vc;
1565 gd_update_caption(vc->s);
1566 trace_gd_grab(vc->label, "kbd", reason);
1569 static void gd_ungrab_keyboard(GtkDisplayState *s)
1571 VirtualConsole *vc = s->kbd_owner;
1573 if (vc == NULL) {
1574 return;
1576 s->kbd_owner = NULL;
1578 #if GTK_CHECK_VERSION(3, 20, 0)
1579 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1580 #elif GTK_CHECK_VERSION(3, 0, 0)
1581 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1582 #else
1583 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1584 #endif
1585 gd_update_caption(s);
1586 trace_gd_ungrab(vc->label, "kbd");
1589 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1591 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1593 if (vc->s->ptr_owner) {
1594 if (vc->s->ptr_owner == vc) {
1595 return;
1596 } else {
1597 gd_ungrab_pointer(vc->s);
1601 #if GTK_CHECK_VERSION(3, 20, 0)
1602 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1603 gdk_device_get_position(gd_get_pointer(display),
1604 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1605 #elif GTK_CHECK_VERSION(3, 0, 0)
1606 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1607 GDK_POINTER_MOTION_MASK |
1608 GDK_BUTTON_PRESS_MASK |
1609 GDK_BUTTON_RELEASE_MASK |
1610 GDK_BUTTON_MOTION_MASK |
1611 GDK_SCROLL_MASK,
1612 vc->s->null_cursor);
1613 gdk_device_get_position(gd_get_pointer(display),
1614 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1615 #else
1616 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1617 FALSE, /* All events to come to our window directly */
1618 GDK_POINTER_MOTION_MASK |
1619 GDK_BUTTON_PRESS_MASK |
1620 GDK_BUTTON_RELEASE_MASK |
1621 GDK_BUTTON_MOTION_MASK |
1622 GDK_SCROLL_MASK,
1623 NULL, /* Allow cursor to move over entire desktop */
1624 vc->s->null_cursor,
1625 GDK_CURRENT_TIME);
1626 gdk_display_get_pointer(display, NULL,
1627 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1628 #endif
1629 vc->s->ptr_owner = vc;
1630 gd_update_caption(vc->s);
1631 trace_gd_grab(vc->label, "ptr", reason);
1634 static void gd_ungrab_pointer(GtkDisplayState *s)
1636 VirtualConsole *vc = s->ptr_owner;
1637 GdkDisplay *display;
1639 if (vc == NULL) {
1640 return;
1642 s->ptr_owner = NULL;
1644 display = gtk_widget_get_display(vc->gfx.drawing_area);
1645 #if GTK_CHECK_VERSION(3, 20, 0)
1646 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1647 gdk_device_warp(gd_get_pointer(display),
1648 gtk_widget_get_screen(vc->gfx.drawing_area),
1649 vc->s->grab_x_root, vc->s->grab_y_root);
1650 #elif GTK_CHECK_VERSION(3, 0, 0)
1651 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1652 gdk_device_warp(gd_get_pointer(display),
1653 gtk_widget_get_screen(vc->gfx.drawing_area),
1654 vc->s->grab_x_root, vc->s->grab_y_root);
1655 #else
1656 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1657 gdk_display_warp_pointer(display,
1658 gtk_widget_get_screen(vc->gfx.drawing_area),
1659 vc->s->grab_x_root, vc->s->grab_y_root);
1660 #endif
1661 gd_update_caption(s);
1662 trace_gd_ungrab(vc->label, "ptr");
1665 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1667 GtkDisplayState *s = opaque;
1668 VirtualConsole *vc = gd_vc_find_current(s);
1670 if (gd_is_grab_active(s)) {
1671 gd_grab_keyboard(vc, "user-request-main-window");
1672 gd_grab_pointer(vc, "user-request-main-window");
1673 } else {
1674 gd_ungrab_keyboard(s);
1675 gd_ungrab_pointer(s);
1678 gd_update_cursor(vc);
1681 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1682 gpointer data)
1684 GtkDisplayState *s = data;
1685 VirtualConsole *vc;
1686 gboolean on_vga;
1688 if (!gtk_widget_get_realized(s->notebook)) {
1689 return;
1692 #ifdef VTE_RESIZE_HACK
1693 vc = gd_vc_find_current(s);
1694 if (vc && vc->type == GD_VC_VTE) {
1695 gtk_widget_hide(vc->vte.terminal);
1697 #endif
1698 vc = gd_vc_find_by_page(s, arg2);
1699 if (!vc) {
1700 return;
1702 #ifdef VTE_RESIZE_HACK
1703 if (vc->type == GD_VC_VTE) {
1704 gtk_widget_show(vc->vte.terminal);
1706 #endif
1707 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1708 TRUE);
1709 on_vga = (vc->type == GD_VC_GFX &&
1710 qemu_console_is_graphic(vc->gfx.dcl.con));
1711 if (!on_vga) {
1712 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1713 FALSE);
1714 } else if (s->full_screen) {
1715 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1716 TRUE);
1718 gtk_widget_set_sensitive(s->grab_item, on_vga);
1719 #ifdef CONFIG_VTE
1720 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1721 #endif
1723 gd_update_windowsize(vc);
1724 gd_update_cursor(vc);
1727 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1728 gpointer opaque)
1730 VirtualConsole *vc = opaque;
1731 GtkDisplayState *s = vc->s;
1733 if (gd_grab_on_hover(s)) {
1734 gd_grab_keyboard(vc, "grab-on-hover");
1736 return TRUE;
1739 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1740 gpointer opaque)
1742 VirtualConsole *vc = opaque;
1743 GtkDisplayState *s = vc->s;
1745 if (gd_grab_on_hover(s)) {
1746 gd_ungrab_keyboard(s);
1748 return TRUE;
1751 static gboolean gd_focus_out_event(GtkWidget *widget,
1752 GdkEventCrossing *crossing, gpointer opaque)
1754 VirtualConsole *vc = opaque;
1755 GtkDisplayState *s = vc->s;
1757 gtk_release_modifiers(s);
1758 return TRUE;
1761 static gboolean gd_configure(GtkWidget *widget,
1762 GdkEventConfigure *cfg, gpointer opaque)
1764 VirtualConsole *vc = opaque;
1766 gd_set_ui_info(vc, cfg->width, cfg->height);
1767 return FALSE;
1770 /** Virtual Console Callbacks **/
1772 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1773 int idx, GSList *group, GtkWidget *view_menu)
1775 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1776 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1777 HOTKEY_MODIFIERS, 0,
1778 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1779 #if GTK_CHECK_VERSION(3, 8, 0)
1780 gtk_accel_label_set_accel(
1781 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1782 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1783 #endif
1785 g_signal_connect(vc->menu_item, "activate",
1786 G_CALLBACK(gd_menu_switch_vc), s);
1787 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1789 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1790 return group;
1793 #if defined(CONFIG_VTE)
1794 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1796 GtkDisplayState *s = opaque;
1797 VirtualConsole *vc = gd_vc_find_current(s);
1799 #if VTE_CHECK_VERSION(0, 50, 0)
1800 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1801 VTE_FORMAT_TEXT);
1802 #else
1803 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1804 #endif
1807 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1809 VirtualConsole *vc = opaque;
1811 if (gtk_adjustment_get_upper(adjustment) >
1812 gtk_adjustment_get_page_size(adjustment)) {
1813 gtk_widget_show(vc->vte.scrollbar);
1814 } else {
1815 gtk_widget_hide(vc->vte.scrollbar);
1819 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1821 VCChardev *vcd = VC_CHARDEV(chr);
1822 VirtualConsole *vc = vcd->console;
1824 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1825 return len;
1828 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1830 VCChardev *vcd = VC_CHARDEV(chr);
1831 VirtualConsole *vc = vcd->console;
1833 if (vc) {
1834 vc->vte.echo = echo;
1835 } else {
1836 vcd->echo = echo;
1840 static int nb_vcs;
1841 static Chardev *vcs[MAX_VCS];
1842 static void gd_vc_open(Chardev *chr,
1843 ChardevBackend *backend,
1844 bool *be_opened,
1845 Error **errp)
1847 if (nb_vcs == MAX_VCS) {
1848 error_setg(errp, "Maximum number of consoles reached");
1849 return;
1852 vcs[nb_vcs++] = chr;
1854 /* console/chardev init sometimes completes elsewhere in a 2nd
1855 * stage, so defer OPENED events until they are fully initialized
1857 *be_opened = false;
1860 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1862 ChardevClass *cc = CHARDEV_CLASS(oc);
1864 cc->parse = qemu_chr_parse_vc;
1865 cc->open = gd_vc_open;
1866 cc->chr_write = gd_vc_chr_write;
1867 cc->chr_set_echo = gd_vc_chr_set_echo;
1870 static const TypeInfo char_gd_vc_type_info = {
1871 .name = TYPE_CHARDEV_VC,
1872 .parent = TYPE_CHARDEV,
1873 .instance_size = sizeof(VCChardev),
1874 .class_init = char_gd_vc_class_init,
1877 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1878 gpointer user_data)
1880 VirtualConsole *vc = user_data;
1882 if (vc->vte.echo) {
1883 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1884 int i;
1885 for (i = 0; i < size; i++) {
1886 uint8_t c = text[i];
1887 if (c >= 128 || isprint(c)) {
1888 /* 8-bit characters are considered printable. */
1889 vte_terminal_feed(term, &text[i], 1);
1890 } else if (c == '\r' || c == '\n') {
1891 vte_terminal_feed(term, "\r\n", 2);
1892 } else {
1893 char ctrl[2] = { '^', 0};
1894 ctrl[1] = text[i] ^ 64;
1895 vte_terminal_feed(term, ctrl, 2);
1900 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1901 return TRUE;
1904 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1905 Chardev *chr, int idx,
1906 GSList *group, GtkWidget *view_menu)
1908 char buffer[32];
1909 GtkWidget *box;
1910 GtkWidget *scrollbar;
1911 GtkAdjustment *vadjustment;
1912 VCChardev *vcd = VC_CHARDEV(chr);
1914 vc->s = s;
1915 vc->vte.echo = vcd->echo;
1916 vc->vte.chr = chr;
1917 vcd->console = vc;
1919 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1920 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1921 ? vc->vte.chr->label : buffer);
1922 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1924 vc->vte.terminal = vte_terminal_new();
1925 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1927 /* The documentation says that the default is UTF-8, but actually it is
1928 * 7-bit ASCII at least in VTE 0.38.
1930 #if VTE_CHECK_VERSION(0, 38, 0)
1931 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1932 #else
1933 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1934 #endif
1936 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1937 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1938 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1940 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1941 vadjustment = gtk_scrollable_get_vadjustment
1942 (GTK_SCROLLABLE(vc->vte.terminal));
1943 #else
1944 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1945 #endif
1947 #if GTK_CHECK_VERSION(3, 0, 0)
1948 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1949 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1950 #else
1951 box = gtk_hbox_new(false, 2);
1952 scrollbar = gtk_vscrollbar_new(vadjustment);
1953 #endif
1955 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1956 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1958 vc->vte.box = box;
1959 vc->vte.scrollbar = scrollbar;
1961 g_signal_connect(vadjustment, "changed",
1962 G_CALLBACK(gd_vc_adjustment_changed), vc);
1964 vc->type = GD_VC_VTE;
1965 vc->tab_item = box;
1966 vc->focus = vc->vte.terminal;
1967 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1968 gtk_label_new(vc->label));
1970 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1972 return group;
1975 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1976 GtkWidget *view_menu)
1978 int i;
1980 for (i = 0; i < nb_vcs; i++) {
1981 VirtualConsole *vc = &s->vc[s->nb_vcs];
1982 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1983 s->nb_vcs++;
1986 #endif /* CONFIG_VTE */
1988 /** Window Creation **/
1990 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1992 #if GTK_CHECK_VERSION(3, 0, 0)
1993 g_signal_connect(vc->gfx.drawing_area, "draw",
1994 G_CALLBACK(gd_draw_event), vc);
1995 #if defined(CONFIG_GTK_GL)
1996 if (display_opengl) {
1997 /* wire up GtkGlArea events */
1998 g_signal_connect(vc->gfx.drawing_area, "render",
1999 G_CALLBACK(gd_render_event), vc);
2000 g_signal_connect(vc->gfx.drawing_area, "resize",
2001 G_CALLBACK(gd_resize_event), vc);
2003 #endif
2004 #else
2005 g_signal_connect(vc->gfx.drawing_area, "expose-event",
2006 G_CALLBACK(gd_expose_event), vc);
2007 #endif
2008 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
2009 g_signal_connect(vc->gfx.drawing_area, "event",
2010 G_CALLBACK(gd_event), vc);
2011 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
2012 G_CALLBACK(gd_button_event), vc);
2013 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
2014 G_CALLBACK(gd_button_event), vc);
2015 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
2016 G_CALLBACK(gd_scroll_event), vc);
2017 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2018 G_CALLBACK(gd_key_event), vc);
2019 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
2020 G_CALLBACK(gd_key_event), vc);
2022 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
2023 G_CALLBACK(gd_enter_event), vc);
2024 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
2025 G_CALLBACK(gd_leave_event), vc);
2026 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
2027 G_CALLBACK(gd_focus_out_event), vc);
2028 g_signal_connect(vc->gfx.drawing_area, "configure-event",
2029 G_CALLBACK(gd_configure), vc);
2030 } else {
2031 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2032 G_CALLBACK(gd_text_key_down), vc);
2036 static void gd_connect_signals(GtkDisplayState *s)
2038 g_signal_connect(s->show_tabs_item, "activate",
2039 G_CALLBACK(gd_menu_show_tabs), s);
2040 g_signal_connect(s->untabify_item, "activate",
2041 G_CALLBACK(gd_menu_untabify), s);
2043 g_signal_connect(s->window, "delete-event",
2044 G_CALLBACK(gd_window_close), s);
2046 g_signal_connect(s->pause_item, "activate",
2047 G_CALLBACK(gd_menu_pause), s);
2048 g_signal_connect(s->reset_item, "activate",
2049 G_CALLBACK(gd_menu_reset), s);
2050 g_signal_connect(s->powerdown_item, "activate",
2051 G_CALLBACK(gd_menu_powerdown), s);
2052 g_signal_connect(s->quit_item, "activate",
2053 G_CALLBACK(gd_menu_quit), s);
2054 #if defined(CONFIG_VTE)
2055 g_signal_connect(s->copy_item, "activate",
2056 G_CALLBACK(gd_menu_copy), s);
2057 #endif
2058 g_signal_connect(s->full_screen_item, "activate",
2059 G_CALLBACK(gd_menu_full_screen), s);
2060 g_signal_connect(s->zoom_in_item, "activate",
2061 G_CALLBACK(gd_menu_zoom_in), s);
2062 g_signal_connect(s->zoom_out_item, "activate",
2063 G_CALLBACK(gd_menu_zoom_out), s);
2064 g_signal_connect(s->zoom_fixed_item, "activate",
2065 G_CALLBACK(gd_menu_zoom_fixed), s);
2066 g_signal_connect(s->zoom_fit_item, "activate",
2067 G_CALLBACK(gd_menu_zoom_fit), s);
2068 g_signal_connect(s->grab_item, "activate",
2069 G_CALLBACK(gd_menu_grab_input), s);
2070 g_signal_connect(s->notebook, "switch-page",
2071 G_CALLBACK(gd_change_page), s);
2074 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2076 GtkWidget *machine_menu;
2077 GtkWidget *separator;
2079 machine_menu = gtk_menu_new();
2080 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2082 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2083 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2085 separator = gtk_separator_menu_item_new();
2086 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2088 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2089 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2091 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2092 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2094 separator = gtk_separator_menu_item_new();
2095 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2097 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2098 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2099 "<QEMU>/Machine/Quit");
2100 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2101 GDK_KEY_q, HOTKEY_MODIFIERS);
2102 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2104 return machine_menu;
2107 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2108 QemuConsole *con, int idx,
2109 GSList *group, GtkWidget *view_menu)
2111 vc->label = qemu_console_get_label(con);
2112 vc->s = s;
2113 vc->gfx.scale_x = 1.0;
2114 vc->gfx.scale_y = 1.0;
2116 #if defined(CONFIG_OPENGL)
2117 if (display_opengl) {
2118 #if defined(CONFIG_GTK_GL)
2119 vc->gfx.drawing_area = gtk_gl_area_new();
2120 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2121 #else
2122 vc->gfx.drawing_area = gtk_drawing_area_new();
2124 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2125 * It is required for opengl rendering on X11 though. A
2126 * proper replacement (native opengl support) is only
2127 * available in 3.16+. Silence the warning if possible.
2129 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2130 #pragma GCC diagnostic push
2131 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2132 #endif
2133 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2134 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2135 #pragma GCC diagnostic pop
2136 #endif
2137 vc->gfx.dcl.ops = &dcl_egl_ops;
2138 #endif /* CONFIG_GTK_GL */
2139 } else
2140 #endif
2142 vc->gfx.drawing_area = gtk_drawing_area_new();
2143 vc->gfx.dcl.ops = &dcl_ops;
2147 gtk_widget_add_events(vc->gfx.drawing_area,
2148 GDK_POINTER_MOTION_MASK |
2149 GDK_BUTTON_PRESS_MASK |
2150 GDK_BUTTON_RELEASE_MASK |
2151 GDK_BUTTON_MOTION_MASK |
2152 GDK_ENTER_NOTIFY_MASK |
2153 GDK_LEAVE_NOTIFY_MASK |
2154 GDK_SCROLL_MASK |
2155 GDK_KEY_PRESS_MASK);
2156 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2158 vc->type = GD_VC_GFX;
2159 vc->tab_item = vc->gfx.drawing_area;
2160 vc->focus = vc->gfx.drawing_area;
2161 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2162 vc->tab_item, gtk_label_new(vc->label));
2164 vc->gfx.dcl.con = con;
2165 register_displaychangelistener(&vc->gfx.dcl);
2167 gd_connect_vc_gfx_signals(vc);
2168 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2170 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2171 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2172 s->free_scale = true;
2175 return group;
2178 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2180 GSList *group = NULL;
2181 GtkWidget *view_menu;
2182 GtkWidget *separator;
2183 QemuConsole *con;
2184 int vc;
2186 view_menu = gtk_menu_new();
2187 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2189 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2191 #if defined(CONFIG_VTE)
2192 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2193 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2194 #endif
2196 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2197 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2198 #if GTK_CHECK_VERSION(3, 8, 0)
2199 gtk_accel_label_set_accel(
2200 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2201 GDK_KEY_f, HOTKEY_MODIFIERS);
2202 #endif
2203 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2205 separator = gtk_separator_menu_item_new();
2206 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2208 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2209 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2210 "<QEMU>/View/Zoom In");
2211 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2212 HOTKEY_MODIFIERS);
2213 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2214 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2215 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2217 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2218 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2219 "<QEMU>/View/Zoom Out");
2220 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2221 HOTKEY_MODIFIERS);
2222 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2224 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2225 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2226 "<QEMU>/View/Zoom Fixed");
2227 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2228 HOTKEY_MODIFIERS);
2229 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2231 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2232 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2234 separator = gtk_separator_menu_item_new();
2235 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2237 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2238 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2240 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2241 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2242 "<QEMU>/View/Grab Input");
2243 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2244 HOTKEY_MODIFIERS);
2245 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2247 separator = gtk_separator_menu_item_new();
2248 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2250 /* gfx */
2251 for (vc = 0;; vc++) {
2252 con = qemu_console_lookup_by_index(vc);
2253 if (!con) {
2254 break;
2256 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2257 vc, group, view_menu);
2258 s->nb_vcs++;
2261 #if defined(CONFIG_VTE)
2262 /* vte */
2263 gd_vcs_init(s, group, view_menu);
2264 #endif
2266 separator = gtk_separator_menu_item_new();
2267 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2269 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2270 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2272 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2273 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2275 return view_menu;
2278 static void gd_create_menus(GtkDisplayState *s)
2280 s->accel_group = gtk_accel_group_new();
2281 s->machine_menu = gd_create_menu_machine(s);
2282 s->view_menu = gd_create_menu_view(s);
2284 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2285 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2286 s->machine_menu);
2287 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2289 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2290 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2291 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2293 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2294 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2298 static gboolean gtkinit;
2300 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2302 VirtualConsole *vc;
2304 GtkDisplayState *s = g_malloc0(sizeof(*s));
2305 char *filename;
2306 GdkDisplay *window_display;
2308 if (!gtkinit) {
2309 fprintf(stderr, "gtk initialization failed\n");
2310 exit(1);
2312 assert(opts->type == DISPLAY_TYPE_GTK);
2313 s->opts = opts;
2315 #if !GTK_CHECK_VERSION(3, 0, 0)
2316 g_printerr("Running QEMU with GTK 2.x is deprecated, and will be removed\n"
2317 "in a future release. Please switch to GTK 3.x instead\n");
2318 #endif
2320 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2321 #if GTK_CHECK_VERSION(3, 2, 0)
2322 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2323 #else
2324 s->vbox = gtk_vbox_new(FALSE, 0);
2325 #endif
2326 s->notebook = gtk_notebook_new();
2327 s->menu_bar = gtk_menu_bar_new();
2329 s->free_scale = FALSE;
2331 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2332 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2333 * printable, but without changing any of the character classes to make
2334 * sure that we don't accidentally break implicit assumptions. */
2335 setlocale(LC_MESSAGES, "");
2336 setlocale(LC_CTYPE, "C.UTF-8");
2337 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2338 textdomain("qemu");
2340 window_display = gtk_widget_get_display(s->window);
2341 s->null_cursor = gdk_cursor_new_for_display(window_display,
2342 GDK_BLANK_CURSOR);
2344 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2345 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2346 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2348 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2349 if (filename) {
2350 GError *error = NULL;
2351 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2352 if (pixbuf) {
2353 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2354 } else {
2355 g_error_free(error);
2357 g_free(filename);
2360 gd_create_menus(s);
2362 gd_connect_signals(s);
2364 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2365 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2367 gd_update_caption(s);
2369 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2370 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2372 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2374 gtk_widget_show_all(s->window);
2376 #ifdef VTE_RESIZE_HACK
2378 VirtualConsole *cur = gd_vc_find_current(s);
2379 if (cur) {
2380 int i;
2382 for (i = 0; i < s->nb_vcs; i++) {
2383 VirtualConsole *vc = &s->vc[i];
2384 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2385 gtk_widget_hide(vc->vte.terminal);
2388 gd_update_windowsize(cur);
2391 #endif
2393 vc = gd_vc_find_current(s);
2394 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2395 #ifdef CONFIG_VTE
2396 gtk_widget_set_sensitive(s->copy_item,
2397 vc && vc->type == GD_VC_VTE);
2398 #endif
2400 if (opts->has_full_screen &&
2401 opts->full_screen) {
2402 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2404 if (opts->u.gtk.has_grab_on_hover &&
2405 opts->u.gtk.grab_on_hover) {
2406 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
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) {
2438 #if defined(CONFIG_OPENGL)
2439 #if defined(CONFIG_GTK_GL)
2440 gtk_gl_area_init();
2441 #else
2442 gtk_egl_init();
2443 #endif
2444 #endif
2447 keycode_map = gd_get_keymap(&keycode_maplen);
2449 #if defined(CONFIG_VTE)
2450 type_register(&char_gd_vc_type_info);
2451 #endif
2454 static QemuDisplay qemu_display_gtk = {
2455 .type = DISPLAY_TYPE_GTK,
2456 .early_init = early_gtk_display_init,
2457 .init = gtk_display_init,
2460 static void register_gtk(void)
2462 qemu_display_register(&qemu_display_gtk);
2465 type_init(register_gtk);