Merge remote-tracking branch 'remotes/xtensa/tags/20180402-xtensa' into staging
[qemu.git] / ui / gtk.c
blobef5bc42094a30f70d0b001aa2868e7ac8bc2ba67
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 bool gtk_use_gl_area;
248 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
249 static void gd_ungrab_pointer(GtkDisplayState *s);
250 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
251 static void gd_ungrab_keyboard(GtkDisplayState *s);
253 /** Utility Functions **/
255 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
257 VirtualConsole *vc;
258 gint i;
260 for (i = 0; i < s->nb_vcs; i++) {
261 vc = &s->vc[i];
262 if (gtk_check_menu_item_get_active
263 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
264 return vc;
267 return NULL;
270 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
272 VirtualConsole *vc;
273 gint i, p;
275 for (i = 0; i < s->nb_vcs; i++) {
276 vc = &s->vc[i];
277 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
278 if (p == page) {
279 return vc;
282 return NULL;
285 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
287 gint page;
289 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
290 return gd_vc_find_by_page(s, page);
293 static bool gd_is_grab_active(GtkDisplayState *s)
295 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
298 static bool gd_grab_on_hover(GtkDisplayState *s)
300 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
303 static void gd_update_cursor(VirtualConsole *vc)
305 GtkDisplayState *s = vc->s;
306 GdkWindow *window;
308 if (vc->type != GD_VC_GFX ||
309 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
310 return;
313 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
314 return;
317 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
318 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
319 gdk_window_set_cursor(window, s->null_cursor);
320 } else {
321 gdk_window_set_cursor(window, NULL);
325 static void gd_update_caption(GtkDisplayState *s)
327 const char *status = "";
328 gchar *prefix;
329 gchar *title;
330 const char *grab = "";
331 bool is_paused = !runstate_is_running();
332 int i;
334 if (qemu_name) {
335 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
336 } else {
337 prefix = g_strdup_printf("QEMU");
340 if (s->ptr_owner != NULL &&
341 s->ptr_owner->window == NULL) {
342 grab = _(" - Press Ctrl+Alt+G to release grab");
345 if (is_paused) {
346 status = _(" [Paused]");
348 s->external_pause_update = true;
349 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
350 is_paused);
351 s->external_pause_update = false;
353 title = g_strdup_printf("%s%s%s", prefix, status, grab);
354 gtk_window_set_title(GTK_WINDOW(s->window), title);
355 g_free(title);
357 for (i = 0; i < s->nb_vcs; i++) {
358 VirtualConsole *vc = &s->vc[i];
360 if (!vc->window) {
361 continue;
363 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
364 vc == s->kbd_owner ? " +kbd" : "",
365 vc == s->ptr_owner ? " +ptr" : "");
366 gtk_window_set_title(GTK_WINDOW(vc->window), title);
367 g_free(title);
370 g_free(prefix);
373 static void gd_update_geometry_hints(VirtualConsole *vc)
375 GtkDisplayState *s = vc->s;
376 GdkWindowHints mask = 0;
377 GdkGeometry geo = {};
378 GtkWidget *geo_widget = NULL;
379 GtkWindow *geo_window;
381 if (vc->type == GD_VC_GFX) {
382 if (!vc->gfx.ds) {
383 return;
385 if (s->free_scale) {
386 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
387 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
388 mask |= GDK_HINT_MIN_SIZE;
389 } else {
390 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
391 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
392 mask |= GDK_HINT_MIN_SIZE;
394 geo_widget = vc->gfx.drawing_area;
395 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
397 #if defined(CONFIG_VTE)
398 } else if (vc->type == GD_VC_VTE) {
399 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
400 GtkBorder padding = { 0 };
402 #if VTE_CHECK_VERSION(0, 37, 0)
403 gtk_style_context_get_padding(
404 gtk_widget_get_style_context(vc->vte.terminal),
405 gtk_widget_get_state_flags(vc->vte.terminal),
406 &padding);
407 #else
409 GtkBorder *ib = NULL;
410 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
411 if (ib) {
412 padding = *ib;
413 gtk_border_free(ib);
416 #endif
418 geo.width_inc = vte_terminal_get_char_width(term);
419 geo.height_inc = vte_terminal_get_char_height(term);
420 mask |= GDK_HINT_RESIZE_INC;
421 geo.base_width = geo.width_inc;
422 geo.base_height = geo.height_inc;
423 mask |= GDK_HINT_BASE_SIZE;
424 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
425 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
426 mask |= GDK_HINT_MIN_SIZE;
428 geo.base_width += padding.left + padding.right;
429 geo.base_height += padding.top + padding.bottom;
430 geo.min_width += padding.left + padding.right;
431 geo.min_height += padding.top + padding.bottom;
432 geo_widget = vc->vte.terminal;
433 #endif
436 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
437 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
440 void gd_update_windowsize(VirtualConsole *vc)
442 GtkDisplayState *s = vc->s;
444 gd_update_geometry_hints(vc);
446 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
447 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
448 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
452 static void gd_update_full_redraw(VirtualConsole *vc)
454 GtkWidget *area = vc->gfx.drawing_area;
455 int ww, wh;
456 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
457 #if defined(CONFIG_GTK_GL)
458 if (vc->gfx.gls && gtk_use_gl_area) {
459 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
460 return;
462 #endif
463 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
466 static void gtk_release_modifiers(GtkDisplayState *s)
468 VirtualConsole *vc = gd_vc_find_current(s);
469 int i, qcode;
471 if (vc->type != GD_VC_GFX ||
472 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
473 return;
475 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
476 qcode = modifier_keycode[i];
477 if (!s->modifier_pressed[i]) {
478 continue;
480 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode, false);
481 s->modifier_pressed[i] = false;
485 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
486 GtkWidget *widget)
488 g_object_ref(G_OBJECT(widget));
489 gtk_container_remove(GTK_CONTAINER(from), widget);
490 gtk_container_add(GTK_CONTAINER(to), widget);
491 g_object_unref(G_OBJECT(widget));
494 /** DisplayState Callbacks **/
496 static void gd_update(DisplayChangeListener *dcl,
497 int x, int y, int w, int h)
499 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
500 GdkWindow *win;
501 int x1, x2, y1, y2;
502 int mx, my;
503 int fbw, fbh;
504 int ww, wh;
506 trace_gd_update(vc->label, x, y, w, h);
508 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
509 return;
512 if (vc->gfx.convert) {
513 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
514 NULL, vc->gfx.convert,
515 x, y, 0, 0, x, y, w, h);
518 x1 = floor(x * vc->gfx.scale_x);
519 y1 = floor(y * vc->gfx.scale_y);
521 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
522 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
524 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
525 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
527 win = gtk_widget_get_window(vc->gfx.drawing_area);
528 if (!win) {
529 return;
531 gdk_drawable_get_size(win, &ww, &wh);
533 mx = my = 0;
534 if (ww > fbw) {
535 mx = (ww - fbw) / 2;
537 if (wh > fbh) {
538 my = (wh - fbh) / 2;
541 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
542 mx + x1, my + y1, (x2 - x1), (y2 - y1));
545 static void gd_refresh(DisplayChangeListener *dcl)
547 graphic_hw_update(dcl->con);
550 #if GTK_CHECK_VERSION(3, 0, 0)
551 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
553 #if GTK_CHECK_VERSION(3, 20, 0)
554 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
555 #else
556 return gdk_device_manager_get_client_pointer(
557 gdk_display_get_device_manager(dpy));
558 #endif
561 static void gd_mouse_set(DisplayChangeListener *dcl,
562 int x, int y, int visible)
564 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
565 GdkDisplay *dpy;
566 gint x_root, y_root;
568 if (qemu_input_is_absolute()) {
569 return;
572 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
573 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
574 x, y, &x_root, &y_root);
575 gdk_device_warp(gd_get_pointer(dpy),
576 gtk_widget_get_screen(vc->gfx.drawing_area),
577 x_root, y_root);
578 vc->s->last_x = x;
579 vc->s->last_y = y;
581 #else
582 static void gd_mouse_set(DisplayChangeListener *dcl,
583 int x, int y, int visible)
585 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
586 gint x_root, y_root;
588 if (qemu_input_is_absolute()) {
589 return;
592 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
593 x, y, &x_root, &y_root);
594 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
595 gtk_widget_get_screen(vc->gfx.drawing_area),
596 x_root, y_root);
598 #endif
600 static void gd_cursor_define(DisplayChangeListener *dcl,
601 QEMUCursor *c)
603 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
604 GdkPixbuf *pixbuf;
605 GdkCursor *cursor;
607 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
608 return;
611 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
612 GDK_COLORSPACE_RGB, true, 8,
613 c->width, c->height, c->width * 4,
614 NULL, NULL);
615 cursor = gdk_cursor_new_from_pixbuf
616 (gtk_widget_get_display(vc->gfx.drawing_area),
617 pixbuf, c->hot_x, c->hot_y);
618 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
619 g_object_unref(pixbuf);
620 #if !GTK_CHECK_VERSION(3, 0, 0)
621 gdk_cursor_unref(cursor);
622 #else
623 g_object_unref(cursor);
624 #endif
627 static void gd_switch(DisplayChangeListener *dcl,
628 DisplaySurface *surface)
630 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
631 bool resized = true;
633 trace_gd_switch(vc->label,
634 surface ? surface_width(surface) : 0,
635 surface ? surface_height(surface) : 0);
637 if (vc->gfx.surface) {
638 cairo_surface_destroy(vc->gfx.surface);
639 vc->gfx.surface = NULL;
641 if (vc->gfx.convert) {
642 pixman_image_unref(vc->gfx.convert);
643 vc->gfx.convert = NULL;
646 if (vc->gfx.ds && surface &&
647 surface_width(vc->gfx.ds) == surface_width(surface) &&
648 surface_height(vc->gfx.ds) == surface_height(surface)) {
649 resized = false;
651 vc->gfx.ds = surface;
653 if (!surface) {
654 return;
657 if (surface->format == PIXMAN_x8r8g8b8) {
659 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
661 * No need to convert, use surface directly. Should be the
662 * common case as this is qemu_default_pixelformat(32) too.
664 vc->gfx.surface = cairo_image_surface_create_for_data
665 (surface_data(surface),
666 CAIRO_FORMAT_RGB24,
667 surface_width(surface),
668 surface_height(surface),
669 surface_stride(surface));
670 } else {
671 /* Must convert surface, use pixman to do it. */
672 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
673 surface_width(surface),
674 surface_height(surface),
675 NULL, 0);
676 vc->gfx.surface = cairo_image_surface_create_for_data
677 ((void *)pixman_image_get_data(vc->gfx.convert),
678 CAIRO_FORMAT_RGB24,
679 pixman_image_get_width(vc->gfx.convert),
680 pixman_image_get_height(vc->gfx.convert),
681 pixman_image_get_stride(vc->gfx.convert));
682 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
683 NULL, vc->gfx.convert,
684 0, 0, 0, 0, 0, 0,
685 pixman_image_get_width(vc->gfx.convert),
686 pixman_image_get_height(vc->gfx.convert));
689 if (resized) {
690 gd_update_windowsize(vc);
691 } else {
692 gd_update_full_redraw(vc);
696 static const DisplayChangeListenerOps dcl_ops = {
697 .dpy_name = "gtk",
698 .dpy_gfx_update = gd_update,
699 .dpy_gfx_switch = gd_switch,
700 .dpy_gfx_check_format = qemu_pixman_check_format,
701 .dpy_refresh = gd_refresh,
702 .dpy_mouse_set = gd_mouse_set,
703 .dpy_cursor_define = gd_cursor_define,
707 #if defined(CONFIG_OPENGL)
709 /** DisplayState Callbacks (opengl version) **/
711 #if defined(CONFIG_GTK_GL)
713 static const DisplayChangeListenerOps dcl_gl_area_ops = {
714 .dpy_name = "gtk-egl",
715 .dpy_gfx_update = gd_gl_area_update,
716 .dpy_gfx_switch = gd_gl_area_switch,
717 .dpy_gfx_check_format = console_gl_check_format,
718 .dpy_refresh = gd_gl_area_refresh,
719 .dpy_mouse_set = gd_mouse_set,
720 .dpy_cursor_define = gd_cursor_define,
722 .dpy_gl_ctx_create = gd_gl_area_create_context,
723 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
724 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
725 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
726 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
727 .dpy_gl_update = gd_gl_area_scanout_flush,
730 #endif /* CONFIG_GTK_GL */
732 static const DisplayChangeListenerOps dcl_egl_ops = {
733 .dpy_name = "gtk-egl",
734 .dpy_gfx_update = gd_egl_update,
735 .dpy_gfx_switch = gd_egl_switch,
736 .dpy_gfx_check_format = console_gl_check_format,
737 .dpy_refresh = gd_egl_refresh,
738 .dpy_mouse_set = gd_mouse_set,
739 .dpy_cursor_define = gd_cursor_define,
741 .dpy_gl_ctx_create = gd_egl_create_context,
742 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
743 .dpy_gl_ctx_make_current = gd_egl_make_current,
744 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
745 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
746 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
747 .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf,
748 .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf,
749 .dpy_gl_cursor_position = gd_egl_cursor_position,
750 .dpy_gl_release_dmabuf = gd_egl_release_dmabuf,
751 .dpy_gl_update = gd_egl_scanout_flush,
754 #endif /* CONFIG_OPENGL */
756 /** QEMU Events **/
758 static void gd_change_runstate(void *opaque, int running, RunState state)
760 GtkDisplayState *s = opaque;
762 gd_update_caption(s);
765 static void gd_mouse_mode_change(Notifier *notify, void *data)
767 GtkDisplayState *s;
768 int i;
770 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
771 /* release the grab at switching to absolute mode */
772 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
773 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
774 FALSE);
776 for (i = 0; i < s->nb_vcs; i++) {
777 VirtualConsole *vc = &s->vc[i];
778 gd_update_cursor(vc);
782 /** GTK Events **/
784 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
785 void *opaque)
787 GtkDisplayState *s = opaque;
788 bool allow_close = true;
789 int i;
791 if (s->opts->has_window_close && !s->opts->window_close) {
792 allow_close = false;
795 if (allow_close) {
796 for (i = 0; i < s->nb_vcs; i++) {
797 if (s->vc[i].type != GD_VC_GFX) {
798 continue;
800 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
802 qmp_quit(NULL);
803 return FALSE;
806 return TRUE;
809 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
811 QemuUIInfo info;
813 memset(&info, 0, sizeof(info));
814 info.width = width;
815 info.height = height;
816 dpy_set_ui_info(vc->gfx.dcl.con, &info);
819 #if defined(CONFIG_GTK_GL)
821 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
822 void *opaque)
824 VirtualConsole *vc = opaque;
826 if (vc->gfx.gls) {
827 gd_gl_area_draw(vc);
829 return TRUE;
832 static void gd_resize_event(GtkGLArea *area,
833 gint width, gint height, gpointer *opaque)
835 VirtualConsole *vc = (void *)opaque;
837 gd_set_ui_info(vc, width, height);
840 #endif
842 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
844 VirtualConsole *vc = opaque;
845 GtkDisplayState *s = vc->s;
846 int mx, my;
847 int ww, wh;
848 int fbw, fbh;
850 #if defined(CONFIG_OPENGL)
851 if (vc->gfx.gls) {
852 if (gtk_use_gl_area) {
853 /* invoke render callback please */
854 return FALSE;
855 } else {
856 gd_egl_draw(vc);
857 return TRUE;
860 #endif
862 if (!gtk_widget_get_realized(widget)) {
863 return FALSE;
865 if (!vc->gfx.ds) {
866 return FALSE;
869 fbw = surface_width(vc->gfx.ds);
870 fbh = surface_height(vc->gfx.ds);
872 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
874 if (s->full_screen) {
875 vc->gfx.scale_x = (double)ww / fbw;
876 vc->gfx.scale_y = (double)wh / fbh;
877 } else if (s->free_scale) {
878 double sx, sy;
880 sx = (double)ww / fbw;
881 sy = (double)wh / fbh;
883 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
886 fbw *= vc->gfx.scale_x;
887 fbh *= vc->gfx.scale_y;
889 mx = my = 0;
890 if (ww > fbw) {
891 mx = (ww - fbw) / 2;
893 if (wh > fbh) {
894 my = (wh - fbh) / 2;
897 cairo_rectangle(cr, 0, 0, ww, wh);
899 /* Optionally cut out the inner area where the pixmap
900 will be drawn. This avoids 'flashing' since we're
901 not double-buffering. Note we're using the undocumented
902 behaviour of drawing the rectangle from right to left
903 to cut out the whole */
904 cairo_rectangle(cr, mx + fbw, my,
905 -1 * fbw, fbh);
906 cairo_fill(cr);
908 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
909 cairo_set_source_surface(cr, vc->gfx.surface,
910 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
911 cairo_paint(cr);
913 return TRUE;
916 #if !GTK_CHECK_VERSION(3, 0, 0)
917 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
918 void *opaque)
920 cairo_t *cr;
921 gboolean ret;
923 cr = gdk_cairo_create(gtk_widget_get_window(widget));
924 cairo_rectangle(cr,
925 expose->area.x,
926 expose->area.y,
927 expose->area.width,
928 expose->area.height);
929 cairo_clip(cr);
931 ret = gd_draw_event(widget, cr, opaque);
933 cairo_destroy(cr);
935 return ret;
937 #endif
939 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
940 void *opaque)
942 VirtualConsole *vc = opaque;
943 GtkDisplayState *s = vc->s;
944 int x, y;
945 int mx, my;
946 int fbh, fbw;
947 int ww, wh;
949 if (!vc->gfx.ds) {
950 return TRUE;
953 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
954 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
956 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
957 &ww, &wh);
959 mx = my = 0;
960 if (ww > fbw) {
961 mx = (ww - fbw) / 2;
963 if (wh > fbh) {
964 my = (wh - fbh) / 2;
967 x = (motion->x - mx) / vc->gfx.scale_x;
968 y = (motion->y - my) / vc->gfx.scale_y;
970 if (qemu_input_is_absolute()) {
971 if (x < 0 || y < 0 ||
972 x >= surface_width(vc->gfx.ds) ||
973 y >= surface_height(vc->gfx.ds)) {
974 return TRUE;
976 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
977 0, surface_width(vc->gfx.ds));
978 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
979 0, surface_height(vc->gfx.ds));
980 qemu_input_event_sync();
981 } else if (s->last_set && s->ptr_owner == vc) {
982 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
983 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
984 qemu_input_event_sync();
986 s->last_x = x;
987 s->last_y = y;
988 s->last_set = TRUE;
990 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
991 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
992 int screen_width, screen_height;
994 int x = (int)motion->x_root;
995 int y = (int)motion->y_root;
997 #if GTK_CHECK_VERSION(3, 22, 0)
999 GdkDisplay *dpy = gtk_widget_get_display(widget);
1000 GdkWindow *win = gtk_widget_get_window(widget);
1001 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
1002 GdkRectangle geometry;
1003 gdk_monitor_get_geometry(monitor, &geometry);
1004 screen_width = geometry.width;
1005 screen_height = geometry.height;
1007 #else
1009 screen_width = gdk_screen_get_width(screen);
1010 screen_height = gdk_screen_get_height(screen);
1012 #endif
1014 /* In relative mode check to see if client pointer hit
1015 * one of the screen edges, and if so move it back by
1016 * 200 pixels. This is important because the pointer
1017 * in the server doesn't correspond 1-for-1, and so
1018 * may still be only half way across the screen. Without
1019 * this warp, the server pointer would thus appear to hit
1020 * an invisible wall */
1021 if (x == 0) {
1022 x += 200;
1024 if (y == 0) {
1025 y += 200;
1027 if (x == (screen_width - 1)) {
1028 x -= 200;
1030 if (y == (screen_height - 1)) {
1031 y -= 200;
1034 if (x != (int)motion->x_root || y != (int)motion->y_root) {
1035 #if GTK_CHECK_VERSION(3, 0, 0)
1036 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
1037 gdk_device_warp(dev, screen, x, y);
1038 #else
1039 GdkDisplay *display = gtk_widget_get_display(widget);
1040 gdk_display_warp_pointer(display, screen, x, y);
1041 #endif
1042 s->last_set = FALSE;
1043 return FALSE;
1046 return TRUE;
1049 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
1050 void *opaque)
1052 VirtualConsole *vc = opaque;
1053 GtkDisplayState *s = vc->s;
1054 InputButton btn;
1056 /* implicitly grab the input at the first click in the relative mode */
1057 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
1058 !qemu_input_is_absolute() && s->ptr_owner != vc) {
1059 if (!vc->window) {
1060 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1061 TRUE);
1062 } else {
1063 gd_grab_pointer(vc, "relative-mode-click");
1065 return TRUE;
1068 if (button->button == 1) {
1069 btn = INPUT_BUTTON_LEFT;
1070 } else if (button->button == 2) {
1071 btn = INPUT_BUTTON_MIDDLE;
1072 } else if (button->button == 3) {
1073 btn = INPUT_BUTTON_RIGHT;
1074 } else if (button->button == 8) {
1075 btn = INPUT_BUTTON_SIDE;
1076 } else if (button->button == 9) {
1077 btn = INPUT_BUTTON_EXTRA;
1078 } else {
1079 return TRUE;
1082 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1083 button->type == GDK_BUTTON_PRESS);
1084 qemu_input_event_sync();
1085 return TRUE;
1088 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1089 void *opaque)
1091 VirtualConsole *vc = opaque;
1092 InputButton btn;
1094 if (scroll->direction == GDK_SCROLL_UP) {
1095 btn = INPUT_BUTTON_WHEEL_UP;
1096 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1097 btn = INPUT_BUTTON_WHEEL_DOWN;
1098 #if GTK_CHECK_VERSION(3, 4, 0)
1099 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1100 gdouble delta_x, delta_y;
1101 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1102 &delta_x, &delta_y)) {
1103 return TRUE;
1105 if (delta_y > 0) {
1106 btn = INPUT_BUTTON_WHEEL_DOWN;
1107 } else {
1108 btn = INPUT_BUTTON_WHEEL_UP;
1110 #endif
1111 } else {
1112 return TRUE;
1115 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1116 qemu_input_event_sync();
1117 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1118 qemu_input_event_sync();
1119 return TRUE;
1123 static const guint16 *gd_get_keymap(size_t *maplen)
1125 GdkDisplay *dpy = gdk_display_get_default();
1127 #ifdef GDK_WINDOWING_X11
1128 if (GDK_IS_X11_DISPLAY(dpy)) {
1129 trace_gd_keymap_windowing("x11");
1130 return qemu_xkeymap_mapping_table(
1131 gdk_x11_display_get_xdisplay(dpy), maplen);
1133 #endif
1135 #ifdef GDK_WINDOWING_WAYLAND
1136 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1137 trace_gd_keymap_windowing("wayland");
1138 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1139 return qemu_input_map_xorgevdev_to_qcode;
1141 #endif
1143 #ifdef GDK_WINDOWING_WIN32
1144 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1145 trace_gd_keymap_windowing("win32");
1146 *maplen = qemu_input_map_win32_to_qcode_len;
1147 return qemu_input_map_win32_to_qcode;
1149 #endif
1151 #ifdef GDK_WINDOWING_QUARTZ
1152 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1153 trace_gd_keymap_windowing("quartz");
1154 *maplen = qemu_input_map_osx_to_qcode_len;
1155 return qemu_input_map_osx_to_qcode;
1157 #endif
1159 #ifdef GDK_WINDOWING_BROADWAY
1160 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1161 trace_gd_keymap_windowing("broadway");
1162 g_warning("experimental: using broadway, x11 virtual keysym\n"
1163 "mapping - with very limited support. See also\n"
1164 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1165 *maplen = qemu_input_map_x11_to_qcode_len;
1166 return qemu_input_map_x11_to_qcode;
1168 #endif
1170 g_warning("Unsupported GDK Windowing platform.\n"
1171 "Disabling extended keycode tables.\n"
1172 "Please report to qemu-devel@nongnu.org\n"
1173 "including the following information:\n"
1174 "\n"
1175 " - Operating system\n"
1176 " - GDK Windowing system build\n");
1177 return NULL;
1181 static int gd_map_keycode(int scancode)
1183 if (!keycode_map) {
1184 return 0;
1186 if (scancode > keycode_maplen) {
1187 return 0;
1190 return keycode_map[scancode];
1193 static gboolean gd_text_key_down(GtkWidget *widget,
1194 GdkEventKey *key, void *opaque)
1196 VirtualConsole *vc = opaque;
1197 QemuConsole *con = vc->gfx.dcl.con;
1199 if (key->keyval == GDK_KEY_Delete) {
1200 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1201 } else if (key->length) {
1202 kbd_put_string_console(con, key->string, key->length);
1203 } else {
1204 int qcode = gd_map_keycode(key->hardware_keycode);
1205 kbd_put_qcode_console(con, qcode);
1207 return TRUE;
1210 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1212 VirtualConsole *vc = opaque;
1213 GtkDisplayState *s = vc->s;
1214 int qcode;
1215 int i;
1217 if (s->ignore_keys) {
1218 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1219 return TRUE;
1222 #ifdef WIN32
1223 /* on windows, we ought to ignore the reserved key event? */
1224 if (key->hardware_keycode == 0xff)
1225 return false;
1226 #endif
1228 if (key->keyval == GDK_KEY_Pause
1229 #ifdef G_OS_WIN32
1230 /* for some reason GDK does not fill keyval for VK_PAUSE
1231 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1233 || key->hardware_keycode == VK_PAUSE
1234 #endif
1236 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1237 key->type == GDK_KEY_PRESS);
1238 return TRUE;
1241 qcode = gd_map_keycode(key->hardware_keycode);
1243 trace_gd_key_event(vc->label, key->hardware_keycode, qcode,
1244 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1246 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1247 if (qcode == modifier_keycode[i]) {
1248 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1252 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode,
1253 key->type == GDK_KEY_PRESS);
1255 return TRUE;
1258 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1260 if (event->type == GDK_MOTION_NOTIFY) {
1261 return gd_motion_event(widget, &event->motion, opaque);
1263 return FALSE;
1266 /** Window Menu Actions **/
1268 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1270 GtkDisplayState *s = opaque;
1272 if (s->external_pause_update) {
1273 return;
1275 if (runstate_is_running()) {
1276 qmp_stop(NULL);
1277 } else {
1278 qmp_cont(NULL);
1282 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1284 qmp_system_reset(NULL);
1287 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1289 qmp_system_powerdown(NULL);
1292 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1294 qmp_quit(NULL);
1297 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1299 GtkDisplayState *s = opaque;
1300 VirtualConsole *vc = gd_vc_find_by_menu(s);
1301 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1302 gint page;
1304 gtk_release_modifiers(s);
1305 if (vc) {
1306 page = gtk_notebook_page_num(nb, vc->tab_item);
1307 gtk_notebook_set_current_page(nb, page);
1308 gtk_widget_grab_focus(vc->focus);
1310 s->ignore_keys = false;
1313 static void gd_accel_switch_vc(void *opaque)
1315 VirtualConsole *vc = opaque;
1317 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1318 #if !GTK_CHECK_VERSION(3, 0, 0)
1319 /* GTK2 sends the accel key to the target console - ignore this until */
1320 vc->s->ignore_keys = true;
1321 #endif
1324 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1326 GtkDisplayState *s = opaque;
1327 VirtualConsole *vc = gd_vc_find_current(s);
1329 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1330 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1331 } else {
1332 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1334 gd_update_windowsize(vc);
1337 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1338 void *opaque)
1340 VirtualConsole *vc = opaque;
1341 GtkDisplayState *s = vc->s;
1343 gtk_widget_set_sensitive(vc->menu_item, true);
1344 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1345 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1346 vc->tab_item, vc->label);
1347 gtk_widget_destroy(vc->window);
1348 vc->window = NULL;
1349 return TRUE;
1352 static gboolean gd_win_grab(void *opaque)
1354 VirtualConsole *vc = opaque;
1356 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1357 if (vc->s->ptr_owner) {
1358 gd_ungrab_pointer(vc->s);
1359 } else {
1360 gd_grab_pointer(vc, "user-request-detached-tab");
1362 return TRUE;
1365 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1367 GtkDisplayState *s = opaque;
1368 VirtualConsole *vc = gd_vc_find_current(s);
1370 if (vc->type == GD_VC_GFX &&
1371 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1372 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1373 FALSE);
1375 if (!vc->window) {
1376 gtk_widget_set_sensitive(vc->menu_item, false);
1377 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1378 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1380 g_signal_connect(vc->window, "delete-event",
1381 G_CALLBACK(gd_tab_window_close), vc);
1382 gtk_widget_show_all(vc->window);
1384 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1385 GtkAccelGroup *ag = gtk_accel_group_new();
1386 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1388 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1389 vc, NULL);
1390 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1393 gd_update_geometry_hints(vc);
1394 gd_update_caption(s);
1398 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1400 GtkDisplayState *s = opaque;
1401 VirtualConsole *vc = gd_vc_find_current(s);
1403 if (!s->full_screen) {
1404 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1405 gtk_widget_hide(s->menu_bar);
1406 if (vc->type == GD_VC_GFX) {
1407 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1409 gtk_window_fullscreen(GTK_WINDOW(s->window));
1410 s->full_screen = TRUE;
1411 } else {
1412 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1413 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1414 gtk_widget_show(s->menu_bar);
1415 s->full_screen = FALSE;
1416 if (vc->type == GD_VC_GFX) {
1417 vc->gfx.scale_x = 1.0;
1418 vc->gfx.scale_y = 1.0;
1419 gd_update_windowsize(vc);
1423 gd_update_cursor(vc);
1426 static void gd_accel_full_screen(void *opaque)
1428 GtkDisplayState *s = opaque;
1429 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1432 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1434 GtkDisplayState *s = opaque;
1435 VirtualConsole *vc = gd_vc_find_current(s);
1437 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1438 FALSE);
1440 vc->gfx.scale_x += VC_SCALE_STEP;
1441 vc->gfx.scale_y += VC_SCALE_STEP;
1443 gd_update_windowsize(vc);
1446 static void gd_accel_zoom_in(void *opaque)
1448 GtkDisplayState *s = opaque;
1449 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1452 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1454 GtkDisplayState *s = opaque;
1455 VirtualConsole *vc = gd_vc_find_current(s);
1457 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1458 FALSE);
1460 vc->gfx.scale_x -= VC_SCALE_STEP;
1461 vc->gfx.scale_y -= VC_SCALE_STEP;
1463 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1464 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1466 gd_update_windowsize(vc);
1469 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1471 GtkDisplayState *s = opaque;
1472 VirtualConsole *vc = gd_vc_find_current(s);
1474 vc->gfx.scale_x = 1.0;
1475 vc->gfx.scale_y = 1.0;
1477 gd_update_windowsize(vc);
1480 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1482 GtkDisplayState *s = opaque;
1483 VirtualConsole *vc = gd_vc_find_current(s);
1485 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1486 s->free_scale = TRUE;
1487 } else {
1488 s->free_scale = FALSE;
1489 vc->gfx.scale_x = 1.0;
1490 vc->gfx.scale_y = 1.0;
1493 gd_update_windowsize(vc);
1494 gd_update_full_redraw(vc);
1497 #if GTK_CHECK_VERSION(3, 20, 0)
1498 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1500 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1501 GdkSeat *seat = gdk_display_get_default_seat(display);
1502 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1503 GdkSeatCapabilities caps = 0;
1504 GdkCursor *cursor = NULL;
1506 if (kbd) {
1507 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1509 if (ptr) {
1510 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1511 cursor = vc->s->null_cursor;
1514 if (caps) {
1515 gdk_seat_grab(seat, window, caps, false, cursor,
1516 NULL, NULL, NULL);
1517 } else {
1518 gdk_seat_ungrab(seat);
1521 #elif GTK_CHECK_VERSION(3, 0, 0)
1522 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1523 GdkInputSource source, GdkEventMask mask,
1524 GdkCursor *cursor)
1526 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1527 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1528 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1529 GList *tmp = devs;
1531 for (tmp = devs; tmp; tmp = tmp->next) {
1532 GdkDevice *dev = tmp->data;
1533 if (gdk_device_get_source(dev) != source) {
1534 continue;
1536 if (grab) {
1537 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1538 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1539 mask, cursor, GDK_CURRENT_TIME);
1540 } else {
1541 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1544 g_list_free(devs);
1546 #endif
1548 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1550 if (vc->s->kbd_owner) {
1551 if (vc->s->kbd_owner == vc) {
1552 return;
1553 } else {
1554 gd_ungrab_keyboard(vc->s);
1558 #if GTK_CHECK_VERSION(3, 20, 0)
1559 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1560 #elif GTK_CHECK_VERSION(3, 0, 0)
1561 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1562 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1563 NULL);
1564 #else
1565 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1566 FALSE,
1567 GDK_CURRENT_TIME);
1568 #endif
1569 vc->s->kbd_owner = vc;
1570 gd_update_caption(vc->s);
1571 trace_gd_grab(vc->label, "kbd", reason);
1574 static void gd_ungrab_keyboard(GtkDisplayState *s)
1576 VirtualConsole *vc = s->kbd_owner;
1578 if (vc == NULL) {
1579 return;
1581 s->kbd_owner = NULL;
1583 #if GTK_CHECK_VERSION(3, 20, 0)
1584 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1585 #elif GTK_CHECK_VERSION(3, 0, 0)
1586 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1587 #else
1588 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1589 #endif
1590 gd_update_caption(s);
1591 trace_gd_ungrab(vc->label, "kbd");
1594 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1596 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1598 if (vc->s->ptr_owner) {
1599 if (vc->s->ptr_owner == vc) {
1600 return;
1601 } else {
1602 gd_ungrab_pointer(vc->s);
1606 #if GTK_CHECK_VERSION(3, 20, 0)
1607 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1608 gdk_device_get_position(gd_get_pointer(display),
1609 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1610 #elif GTK_CHECK_VERSION(3, 0, 0)
1611 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1612 GDK_POINTER_MOTION_MASK |
1613 GDK_BUTTON_PRESS_MASK |
1614 GDK_BUTTON_RELEASE_MASK |
1615 GDK_BUTTON_MOTION_MASK |
1616 GDK_SCROLL_MASK,
1617 vc->s->null_cursor);
1618 gdk_device_get_position(gd_get_pointer(display),
1619 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1620 #else
1621 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1622 FALSE, /* All events to come to our window directly */
1623 GDK_POINTER_MOTION_MASK |
1624 GDK_BUTTON_PRESS_MASK |
1625 GDK_BUTTON_RELEASE_MASK |
1626 GDK_BUTTON_MOTION_MASK |
1627 GDK_SCROLL_MASK,
1628 NULL, /* Allow cursor to move over entire desktop */
1629 vc->s->null_cursor,
1630 GDK_CURRENT_TIME);
1631 gdk_display_get_pointer(display, NULL,
1632 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1633 #endif
1634 vc->s->ptr_owner = vc;
1635 gd_update_caption(vc->s);
1636 trace_gd_grab(vc->label, "ptr", reason);
1639 static void gd_ungrab_pointer(GtkDisplayState *s)
1641 VirtualConsole *vc = s->ptr_owner;
1642 GdkDisplay *display;
1644 if (vc == NULL) {
1645 return;
1647 s->ptr_owner = NULL;
1649 display = gtk_widget_get_display(vc->gfx.drawing_area);
1650 #if GTK_CHECK_VERSION(3, 20, 0)
1651 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
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 #elif GTK_CHECK_VERSION(3, 0, 0)
1656 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1657 gdk_device_warp(gd_get_pointer(display),
1658 gtk_widget_get_screen(vc->gfx.drawing_area),
1659 vc->s->grab_x_root, vc->s->grab_y_root);
1660 #else
1661 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1662 gdk_display_warp_pointer(display,
1663 gtk_widget_get_screen(vc->gfx.drawing_area),
1664 vc->s->grab_x_root, vc->s->grab_y_root);
1665 #endif
1666 gd_update_caption(s);
1667 trace_gd_ungrab(vc->label, "ptr");
1670 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1672 GtkDisplayState *s = opaque;
1673 VirtualConsole *vc = gd_vc_find_current(s);
1675 if (gd_is_grab_active(s)) {
1676 gd_grab_keyboard(vc, "user-request-main-window");
1677 gd_grab_pointer(vc, "user-request-main-window");
1678 } else {
1679 gd_ungrab_keyboard(s);
1680 gd_ungrab_pointer(s);
1683 gd_update_cursor(vc);
1686 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1687 gpointer data)
1689 GtkDisplayState *s = data;
1690 VirtualConsole *vc;
1691 gboolean on_vga;
1693 if (!gtk_widget_get_realized(s->notebook)) {
1694 return;
1697 #ifdef VTE_RESIZE_HACK
1698 vc = gd_vc_find_current(s);
1699 if (vc && vc->type == GD_VC_VTE) {
1700 gtk_widget_hide(vc->vte.terminal);
1702 #endif
1703 vc = gd_vc_find_by_page(s, arg2);
1704 if (!vc) {
1705 return;
1707 #ifdef VTE_RESIZE_HACK
1708 if (vc->type == GD_VC_VTE) {
1709 gtk_widget_show(vc->vte.terminal);
1711 #endif
1712 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1713 TRUE);
1714 on_vga = (vc->type == GD_VC_GFX &&
1715 qemu_console_is_graphic(vc->gfx.dcl.con));
1716 if (!on_vga) {
1717 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1718 FALSE);
1719 } else if (s->full_screen) {
1720 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1721 TRUE);
1723 gtk_widget_set_sensitive(s->grab_item, on_vga);
1724 #ifdef CONFIG_VTE
1725 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1726 #endif
1728 gd_update_windowsize(vc);
1729 gd_update_cursor(vc);
1732 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1733 gpointer opaque)
1735 VirtualConsole *vc = opaque;
1736 GtkDisplayState *s = vc->s;
1738 if (gd_grab_on_hover(s)) {
1739 gd_grab_keyboard(vc, "grab-on-hover");
1741 return TRUE;
1744 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1745 gpointer opaque)
1747 VirtualConsole *vc = opaque;
1748 GtkDisplayState *s = vc->s;
1750 if (gd_grab_on_hover(s)) {
1751 gd_ungrab_keyboard(s);
1753 return TRUE;
1756 static gboolean gd_focus_out_event(GtkWidget *widget,
1757 GdkEventCrossing *crossing, gpointer opaque)
1759 VirtualConsole *vc = opaque;
1760 GtkDisplayState *s = vc->s;
1762 gtk_release_modifiers(s);
1763 return TRUE;
1766 static gboolean gd_configure(GtkWidget *widget,
1767 GdkEventConfigure *cfg, gpointer opaque)
1769 VirtualConsole *vc = opaque;
1771 gd_set_ui_info(vc, cfg->width, cfg->height);
1772 return FALSE;
1775 /** Virtual Console Callbacks **/
1777 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1778 int idx, GSList *group, GtkWidget *view_menu)
1780 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1781 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1782 HOTKEY_MODIFIERS, 0,
1783 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1784 #if GTK_CHECK_VERSION(3, 8, 0)
1785 gtk_accel_label_set_accel(
1786 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1787 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1788 #endif
1790 g_signal_connect(vc->menu_item, "activate",
1791 G_CALLBACK(gd_menu_switch_vc), s);
1792 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1794 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1795 return group;
1798 #if defined(CONFIG_VTE)
1799 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1801 GtkDisplayState *s = opaque;
1802 VirtualConsole *vc = gd_vc_find_current(s);
1804 #if VTE_CHECK_VERSION(0, 50, 0)
1805 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1806 VTE_FORMAT_TEXT);
1807 #else
1808 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1809 #endif
1812 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1814 VirtualConsole *vc = opaque;
1816 if (gtk_adjustment_get_upper(adjustment) >
1817 gtk_adjustment_get_page_size(adjustment)) {
1818 gtk_widget_show(vc->vte.scrollbar);
1819 } else {
1820 gtk_widget_hide(vc->vte.scrollbar);
1824 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1826 VCChardev *vcd = VC_CHARDEV(chr);
1827 VirtualConsole *vc = vcd->console;
1829 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1830 return len;
1833 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1835 VCChardev *vcd = VC_CHARDEV(chr);
1836 VirtualConsole *vc = vcd->console;
1838 if (vc) {
1839 vc->vte.echo = echo;
1840 } else {
1841 vcd->echo = echo;
1845 static int nb_vcs;
1846 static Chardev *vcs[MAX_VCS];
1847 static void gd_vc_open(Chardev *chr,
1848 ChardevBackend *backend,
1849 bool *be_opened,
1850 Error **errp)
1852 if (nb_vcs == MAX_VCS) {
1853 error_setg(errp, "Maximum number of consoles reached");
1854 return;
1857 vcs[nb_vcs++] = chr;
1859 /* console/chardev init sometimes completes elsewhere in a 2nd
1860 * stage, so defer OPENED events until they are fully initialized
1862 *be_opened = false;
1865 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1867 ChardevClass *cc = CHARDEV_CLASS(oc);
1869 cc->parse = qemu_chr_parse_vc;
1870 cc->open = gd_vc_open;
1871 cc->chr_write = gd_vc_chr_write;
1872 cc->chr_set_echo = gd_vc_chr_set_echo;
1875 static const TypeInfo char_gd_vc_type_info = {
1876 .name = TYPE_CHARDEV_VC,
1877 .parent = TYPE_CHARDEV,
1878 .instance_size = sizeof(VCChardev),
1879 .class_init = char_gd_vc_class_init,
1882 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1883 gpointer user_data)
1885 VirtualConsole *vc = user_data;
1887 if (vc->vte.echo) {
1888 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1889 int i;
1890 for (i = 0; i < size; i++) {
1891 uint8_t c = text[i];
1892 if (c >= 128 || isprint(c)) {
1893 /* 8-bit characters are considered printable. */
1894 vte_terminal_feed(term, &text[i], 1);
1895 } else if (c == '\r' || c == '\n') {
1896 vte_terminal_feed(term, "\r\n", 2);
1897 } else {
1898 char ctrl[2] = { '^', 0};
1899 ctrl[1] = text[i] ^ 64;
1900 vte_terminal_feed(term, ctrl, 2);
1905 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1906 return TRUE;
1909 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1910 Chardev *chr, int idx,
1911 GSList *group, GtkWidget *view_menu)
1913 char buffer[32];
1914 GtkWidget *box;
1915 GtkWidget *scrollbar;
1916 GtkAdjustment *vadjustment;
1917 VCChardev *vcd = VC_CHARDEV(chr);
1919 vc->s = s;
1920 vc->vte.echo = vcd->echo;
1921 vc->vte.chr = chr;
1922 vcd->console = vc;
1924 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1925 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1926 ? vc->vte.chr->label : buffer);
1927 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1929 vc->vte.terminal = vte_terminal_new();
1930 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1932 /* The documentation says that the default is UTF-8, but actually it is
1933 * 7-bit ASCII at least in VTE 0.38.
1935 #if VTE_CHECK_VERSION(0, 38, 0)
1936 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1937 #else
1938 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1939 #endif
1941 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1942 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1943 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1945 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1946 vadjustment = gtk_scrollable_get_vadjustment
1947 (GTK_SCROLLABLE(vc->vte.terminal));
1948 #else
1949 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1950 #endif
1952 #if GTK_CHECK_VERSION(3, 0, 0)
1953 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1954 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1955 #else
1956 box = gtk_hbox_new(false, 2);
1957 scrollbar = gtk_vscrollbar_new(vadjustment);
1958 #endif
1960 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1961 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1963 vc->vte.box = box;
1964 vc->vte.scrollbar = scrollbar;
1966 g_signal_connect(vadjustment, "changed",
1967 G_CALLBACK(gd_vc_adjustment_changed), vc);
1969 vc->type = GD_VC_VTE;
1970 vc->tab_item = box;
1971 vc->focus = vc->vte.terminal;
1972 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1973 gtk_label_new(vc->label));
1975 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1977 return group;
1980 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1981 GtkWidget *view_menu)
1983 int i;
1985 for (i = 0; i < nb_vcs; i++) {
1986 VirtualConsole *vc = &s->vc[s->nb_vcs];
1987 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1988 s->nb_vcs++;
1991 #endif /* CONFIG_VTE */
1993 /** Window Creation **/
1995 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1997 #if GTK_CHECK_VERSION(3, 0, 0)
1998 g_signal_connect(vc->gfx.drawing_area, "draw",
1999 G_CALLBACK(gd_draw_event), vc);
2000 #if defined(CONFIG_GTK_GL)
2001 if (gtk_use_gl_area) {
2002 /* wire up GtkGlArea events */
2003 g_signal_connect(vc->gfx.drawing_area, "render",
2004 G_CALLBACK(gd_render_event), vc);
2005 g_signal_connect(vc->gfx.drawing_area, "resize",
2006 G_CALLBACK(gd_resize_event), vc);
2008 #endif
2009 #else
2010 g_signal_connect(vc->gfx.drawing_area, "expose-event",
2011 G_CALLBACK(gd_expose_event), vc);
2012 #endif
2013 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
2014 g_signal_connect(vc->gfx.drawing_area, "event",
2015 G_CALLBACK(gd_event), vc);
2016 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
2017 G_CALLBACK(gd_button_event), vc);
2018 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
2019 G_CALLBACK(gd_button_event), vc);
2020 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
2021 G_CALLBACK(gd_scroll_event), vc);
2022 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2023 G_CALLBACK(gd_key_event), vc);
2024 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
2025 G_CALLBACK(gd_key_event), vc);
2027 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
2028 G_CALLBACK(gd_enter_event), vc);
2029 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
2030 G_CALLBACK(gd_leave_event), vc);
2031 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
2032 G_CALLBACK(gd_focus_out_event), vc);
2033 g_signal_connect(vc->gfx.drawing_area, "configure-event",
2034 G_CALLBACK(gd_configure), vc);
2035 } else {
2036 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2037 G_CALLBACK(gd_text_key_down), vc);
2041 static void gd_connect_signals(GtkDisplayState *s)
2043 g_signal_connect(s->show_tabs_item, "activate",
2044 G_CALLBACK(gd_menu_show_tabs), s);
2045 g_signal_connect(s->untabify_item, "activate",
2046 G_CALLBACK(gd_menu_untabify), s);
2048 g_signal_connect(s->window, "delete-event",
2049 G_CALLBACK(gd_window_close), s);
2051 g_signal_connect(s->pause_item, "activate",
2052 G_CALLBACK(gd_menu_pause), s);
2053 g_signal_connect(s->reset_item, "activate",
2054 G_CALLBACK(gd_menu_reset), s);
2055 g_signal_connect(s->powerdown_item, "activate",
2056 G_CALLBACK(gd_menu_powerdown), s);
2057 g_signal_connect(s->quit_item, "activate",
2058 G_CALLBACK(gd_menu_quit), s);
2059 #if defined(CONFIG_VTE)
2060 g_signal_connect(s->copy_item, "activate",
2061 G_CALLBACK(gd_menu_copy), s);
2062 #endif
2063 g_signal_connect(s->full_screen_item, "activate",
2064 G_CALLBACK(gd_menu_full_screen), s);
2065 g_signal_connect(s->zoom_in_item, "activate",
2066 G_CALLBACK(gd_menu_zoom_in), s);
2067 g_signal_connect(s->zoom_out_item, "activate",
2068 G_CALLBACK(gd_menu_zoom_out), s);
2069 g_signal_connect(s->zoom_fixed_item, "activate",
2070 G_CALLBACK(gd_menu_zoom_fixed), s);
2071 g_signal_connect(s->zoom_fit_item, "activate",
2072 G_CALLBACK(gd_menu_zoom_fit), s);
2073 g_signal_connect(s->grab_item, "activate",
2074 G_CALLBACK(gd_menu_grab_input), s);
2075 g_signal_connect(s->notebook, "switch-page",
2076 G_CALLBACK(gd_change_page), s);
2079 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2081 GtkWidget *machine_menu;
2082 GtkWidget *separator;
2084 machine_menu = gtk_menu_new();
2085 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2087 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2088 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2090 separator = gtk_separator_menu_item_new();
2091 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2093 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2094 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2096 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2097 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2099 separator = gtk_separator_menu_item_new();
2100 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2102 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2103 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2104 "<QEMU>/Machine/Quit");
2105 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2106 GDK_KEY_q, HOTKEY_MODIFIERS);
2107 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2109 return machine_menu;
2112 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2113 QemuConsole *con, int idx,
2114 GSList *group, GtkWidget *view_menu)
2116 vc->label = qemu_console_get_label(con);
2117 vc->s = s;
2118 vc->gfx.scale_x = 1.0;
2119 vc->gfx.scale_y = 1.0;
2121 #if defined(CONFIG_OPENGL)
2122 if (display_opengl) {
2123 #if defined(CONFIG_GTK_GL)
2124 if (gtk_use_gl_area) {
2125 vc->gfx.drawing_area = gtk_gl_area_new();
2126 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2127 } else
2128 #endif /* CONFIG_GTK_GL */
2130 vc->gfx.drawing_area = gtk_drawing_area_new();
2132 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2133 * It is required for opengl rendering on X11 though. A
2134 * proper replacement (native opengl support) is only
2135 * available in 3.16+. Silence the warning if possible.
2137 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2138 #pragma GCC diagnostic push
2139 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2140 #endif
2141 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2142 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2143 #pragma GCC diagnostic pop
2144 #endif
2145 vc->gfx.dcl.ops = &dcl_egl_ops;
2147 } else
2148 #endif
2150 vc->gfx.drawing_area = gtk_drawing_area_new();
2151 vc->gfx.dcl.ops = &dcl_ops;
2155 gtk_widget_add_events(vc->gfx.drawing_area,
2156 GDK_POINTER_MOTION_MASK |
2157 GDK_BUTTON_PRESS_MASK |
2158 GDK_BUTTON_RELEASE_MASK |
2159 GDK_BUTTON_MOTION_MASK |
2160 GDK_ENTER_NOTIFY_MASK |
2161 GDK_LEAVE_NOTIFY_MASK |
2162 GDK_SCROLL_MASK |
2163 GDK_KEY_PRESS_MASK);
2164 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2166 vc->type = GD_VC_GFX;
2167 vc->tab_item = vc->gfx.drawing_area;
2168 vc->focus = vc->gfx.drawing_area;
2169 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2170 vc->tab_item, gtk_label_new(vc->label));
2172 vc->gfx.dcl.con = con;
2173 register_displaychangelistener(&vc->gfx.dcl);
2175 gd_connect_vc_gfx_signals(vc);
2176 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2178 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2179 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2180 s->free_scale = true;
2183 return group;
2186 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2188 GSList *group = NULL;
2189 GtkWidget *view_menu;
2190 GtkWidget *separator;
2191 QemuConsole *con;
2192 int vc;
2194 view_menu = gtk_menu_new();
2195 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2197 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2199 #if defined(CONFIG_VTE)
2200 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2201 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2202 #endif
2204 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2205 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2206 #if GTK_CHECK_VERSION(3, 8, 0)
2207 gtk_accel_label_set_accel(
2208 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2209 GDK_KEY_f, HOTKEY_MODIFIERS);
2210 #endif
2211 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2213 separator = gtk_separator_menu_item_new();
2214 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2216 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2217 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2218 "<QEMU>/View/Zoom In");
2219 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2220 HOTKEY_MODIFIERS);
2221 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2222 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2223 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2225 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2226 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2227 "<QEMU>/View/Zoom Out");
2228 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2229 HOTKEY_MODIFIERS);
2230 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2232 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2233 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2234 "<QEMU>/View/Zoom Fixed");
2235 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2236 HOTKEY_MODIFIERS);
2237 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2239 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2240 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2242 separator = gtk_separator_menu_item_new();
2243 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2245 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2246 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2248 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2249 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2250 "<QEMU>/View/Grab Input");
2251 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2252 HOTKEY_MODIFIERS);
2253 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2255 separator = gtk_separator_menu_item_new();
2256 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2258 /* gfx */
2259 for (vc = 0;; vc++) {
2260 con = qemu_console_lookup_by_index(vc);
2261 if (!con) {
2262 break;
2264 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2265 vc, group, view_menu);
2266 s->nb_vcs++;
2269 #if defined(CONFIG_VTE)
2270 /* vte */
2271 gd_vcs_init(s, group, view_menu);
2272 #endif
2274 separator = gtk_separator_menu_item_new();
2275 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2277 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2278 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2280 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2281 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2283 return view_menu;
2286 static void gd_create_menus(GtkDisplayState *s)
2288 s->accel_group = gtk_accel_group_new();
2289 s->machine_menu = gd_create_menu_machine(s);
2290 s->view_menu = gd_create_menu_view(s);
2292 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2293 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2294 s->machine_menu);
2295 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2297 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2298 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2299 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2301 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2302 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2306 static gboolean gtkinit;
2308 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2310 VirtualConsole *vc;
2312 GtkDisplayState *s = g_malloc0(sizeof(*s));
2313 char *filename;
2314 GdkDisplay *window_display;
2316 if (!gtkinit) {
2317 fprintf(stderr, "gtk initialization failed\n");
2318 exit(1);
2320 assert(opts->type == DISPLAY_TYPE_GTK);
2321 s->opts = opts;
2323 #if !GTK_CHECK_VERSION(3, 0, 0)
2324 g_printerr("Running QEMU with GTK 2.x is deprecated, and will be removed\n"
2325 "in a future release. Please switch to GTK 3.x instead\n");
2326 #endif
2328 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2329 #if GTK_CHECK_VERSION(3, 2, 0)
2330 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2331 #else
2332 s->vbox = gtk_vbox_new(FALSE, 0);
2333 #endif
2334 s->notebook = gtk_notebook_new();
2335 s->menu_bar = gtk_menu_bar_new();
2337 s->free_scale = FALSE;
2339 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2340 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2341 * printable, but without changing any of the character classes to make
2342 * sure that we don't accidentally break implicit assumptions. */
2343 setlocale(LC_MESSAGES, "");
2344 setlocale(LC_CTYPE, "C.UTF-8");
2345 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2346 textdomain("qemu");
2348 window_display = gtk_widget_get_display(s->window);
2349 s->null_cursor = gdk_cursor_new_for_display(window_display,
2350 GDK_BLANK_CURSOR);
2352 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2353 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2354 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2356 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2357 if (filename) {
2358 GError *error = NULL;
2359 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2360 if (pixbuf) {
2361 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2362 } else {
2363 g_error_free(error);
2365 g_free(filename);
2368 gd_create_menus(s);
2370 gd_connect_signals(s);
2372 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2373 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2375 gd_update_caption(s);
2377 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2378 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2380 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2382 gtk_widget_show_all(s->window);
2384 #ifdef VTE_RESIZE_HACK
2386 VirtualConsole *cur = gd_vc_find_current(s);
2387 if (cur) {
2388 int i;
2390 for (i = 0; i < s->nb_vcs; i++) {
2391 VirtualConsole *vc = &s->vc[i];
2392 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2393 gtk_widget_hide(vc->vte.terminal);
2396 gd_update_windowsize(cur);
2399 #endif
2401 vc = gd_vc_find_current(s);
2402 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2403 #ifdef CONFIG_VTE
2404 gtk_widget_set_sensitive(s->copy_item,
2405 vc && vc->type == GD_VC_VTE);
2406 #endif
2408 if (opts->has_full_screen &&
2409 opts->full_screen) {
2410 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2412 if (opts->u.gtk.has_grab_on_hover &&
2413 opts->u.gtk.grab_on_hover) {
2414 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2418 static void early_gtk_display_init(DisplayOptions *opts)
2420 /* The QEMU code relies on the assumption that it's always run in
2421 * the C locale. Therefore it is not prepared to deal with
2422 * operations that produce different results depending on the
2423 * locale, such as printf's formatting of decimal numbers, and
2424 * possibly others.
2426 * Since GTK+ calls setlocale() by default -importing the locale
2427 * settings from the environment- we must prevent it from doing so
2428 * using gtk_disable_setlocale().
2430 * QEMU's GTK+ UI, however, _does_ have translations for some of
2431 * the menu items. As a trade-off between a functionally correct
2432 * QEMU and a fully internationalized UI we support importing
2433 * LC_MESSAGES from the environment (see the setlocale() call
2434 * earlier in this file). This allows us to display translated
2435 * messages leaving everything else untouched.
2437 gtk_disable_setlocale();
2438 gtkinit = gtk_init_check(NULL, NULL);
2439 if (!gtkinit) {
2440 /* don't exit yet, that'll break -help */
2441 return;
2444 assert(opts->type == DISPLAY_TYPE_GTK);
2445 if (opts->has_gl && opts->gl) {
2446 #if defined(CONFIG_OPENGL)
2447 #if defined(CONFIG_GTK_GL) && defined(GDK_WINDOWING_WAYLAND)
2448 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2449 gtk_use_gl_area = true;
2450 gtk_gl_area_init();
2452 #endif
2454 gtk_egl_init();
2456 #endif
2459 keycode_map = gd_get_keymap(&keycode_maplen);
2461 #if defined(CONFIG_VTE)
2462 type_register(&char_gd_vc_type_info);
2463 #endif
2466 static QemuDisplay qemu_display_gtk = {
2467 .type = DISPLAY_TYPE_GTK,
2468 .early_init = early_gtk_display_init,
2469 .init = gtk_display_init,
2472 static void register_gtk(void)
2474 qemu_display_register(&qemu_display_gtk);
2477 type_init(register_gtk);