char: remove class kind field
[qemu/ar7.git] / ui / gtk.c
blob4984bf3bd999c6486233af1f54fbba9d30a8e9cb
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 "qemu/cutils.h"
41 #include "ui/console.h"
42 #include "ui/gtk.h"
44 #include <glib/gi18n.h>
45 #include <locale.h>
46 #if defined(CONFIG_VTE)
47 #include <vte/vte.h>
48 #endif
49 #include <math.h>
51 #include "trace.h"
52 #include "ui/input.h"
53 #include "sysemu/sysemu.h"
54 #include "qmp-commands.h"
55 #include "x_keymap.h"
56 #include "keymaps.h"
57 #include "sysemu/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 #if !defined(CONFIG_VTE)
69 # define VTE_CHECK_VERSION(a, b, c) 0
70 #endif
72 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
74 * The gtk2 vte terminal widget seriously messes up the window resize
75 * for some reason. You basically can't make the qemu window smaller
76 * any more because the toplevel window geoemtry hints are overridden.
78 * Workaround that by hiding all vte widgets, except the one in the
79 * current tab.
81 * Luckily everything works smooth in gtk3.
83 # define VTE_RESIZE_HACK 1
84 #endif
86 #if !GTK_CHECK_VERSION(2, 20, 0)
87 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
88 #endif
90 #ifndef GDK_IS_X11_DISPLAY
91 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
92 #endif
93 #ifndef GDK_IS_WAYLAND_DISPLAY
94 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
95 #endif
96 #ifndef GDK_IS_WIN32_DISPLAY
97 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
98 #endif
100 #if !GTK_CHECK_VERSION(2, 22, 0)
101 #define GDK_KEY_0 GDK_0
102 #define GDK_KEY_1 GDK_1
103 #define GDK_KEY_2 GDK_2
104 #define GDK_KEY_f GDK_f
105 #define GDK_KEY_g GDK_g
106 #define GDK_KEY_q GDK_q
107 #define GDK_KEY_plus GDK_plus
108 #define GDK_KEY_minus GDK_minus
109 #define GDK_KEY_Pause GDK_Pause
110 #define GDK_KEY_Delete GDK_Delete
111 #endif
113 /* Some older mingw versions lack this constant or have
114 * it conditionally defined */
115 #ifdef _WIN32
116 # ifndef MAPVK_VK_TO_VSC
117 # define MAPVK_VK_TO_VSC 0
118 # endif
119 #endif
122 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
124 static const int modifier_keycode[] = {
125 /* shift, control, alt keys, meta keys, both left & right */
126 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
129 struct GtkDisplayState {
130 GtkWidget *window;
132 GtkWidget *menu_bar;
134 GtkAccelGroup *accel_group;
136 GtkWidget *machine_menu_item;
137 GtkWidget *machine_menu;
138 GtkWidget *pause_item;
139 GtkWidget *reset_item;
140 GtkWidget *powerdown_item;
141 GtkWidget *quit_item;
143 GtkWidget *view_menu_item;
144 GtkWidget *view_menu;
145 GtkWidget *full_screen_item;
146 GtkWidget *copy_item;
147 GtkWidget *zoom_in_item;
148 GtkWidget *zoom_out_item;
149 GtkWidget *zoom_fixed_item;
150 GtkWidget *zoom_fit_item;
151 GtkWidget *grab_item;
152 GtkWidget *grab_on_hover_item;
154 int nb_vcs;
155 VirtualConsole vc[MAX_VCS];
157 GtkWidget *show_tabs_item;
158 GtkWidget *untabify_item;
160 GtkWidget *vbox;
161 GtkWidget *notebook;
162 int button_mask;
163 gboolean last_set;
164 int last_x;
165 int last_y;
166 int grab_x_root;
167 int grab_y_root;
168 VirtualConsole *kbd_owner;
169 VirtualConsole *ptr_owner;
171 gboolean full_screen;
173 GdkCursor *null_cursor;
174 Notifier mouse_mode_notifier;
175 gboolean free_scale;
177 bool external_pause_update;
179 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
180 bool has_evdev;
181 bool ignore_keys;
184 typedef struct VCChardev {
185 Chardev parent;
186 VirtualConsole *console;
187 bool echo;
188 } VCChardev;
190 #define TYPE_CHARDEV_VC "chardev-vc"
191 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
193 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
194 static void gd_ungrab_pointer(GtkDisplayState *s);
195 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
196 static void gd_ungrab_keyboard(GtkDisplayState *s);
198 /** Utility Functions **/
200 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
202 VirtualConsole *vc;
203 gint i;
205 for (i = 0; i < s->nb_vcs; i++) {
206 vc = &s->vc[i];
207 if (gtk_check_menu_item_get_active
208 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
209 return vc;
212 return NULL;
215 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
217 VirtualConsole *vc;
218 gint i, p;
220 for (i = 0; i < s->nb_vcs; i++) {
221 vc = &s->vc[i];
222 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
223 if (p == page) {
224 return vc;
227 return NULL;
230 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
232 gint page;
234 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
235 return gd_vc_find_by_page(s, page);
238 static bool gd_is_grab_active(GtkDisplayState *s)
240 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
243 static bool gd_grab_on_hover(GtkDisplayState *s)
245 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
248 static void gd_update_cursor(VirtualConsole *vc)
250 GtkDisplayState *s = vc->s;
251 GdkWindow *window;
253 if (vc->type != GD_VC_GFX ||
254 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
255 return;
258 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
259 return;
262 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
263 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
264 gdk_window_set_cursor(window, s->null_cursor);
265 } else {
266 gdk_window_set_cursor(window, NULL);
270 static void gd_update_caption(GtkDisplayState *s)
272 const char *status = "";
273 gchar *prefix;
274 gchar *title;
275 const char *grab = "";
276 bool is_paused = !runstate_is_running();
277 int i;
279 if (qemu_name) {
280 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
281 } else {
282 prefix = g_strdup_printf("QEMU");
285 if (s->ptr_owner != NULL &&
286 s->ptr_owner->window == NULL) {
287 grab = _(" - Press Ctrl+Alt+G to release grab");
290 if (is_paused) {
291 status = _(" [Paused]");
293 s->external_pause_update = true;
294 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
295 is_paused);
296 s->external_pause_update = false;
298 title = g_strdup_printf("%s%s%s", prefix, status, grab);
299 gtk_window_set_title(GTK_WINDOW(s->window), title);
300 g_free(title);
302 for (i = 0; i < s->nb_vcs; i++) {
303 VirtualConsole *vc = &s->vc[i];
305 if (!vc->window) {
306 continue;
308 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
309 vc == s->kbd_owner ? " +kbd" : "",
310 vc == s->ptr_owner ? " +ptr" : "");
311 gtk_window_set_title(GTK_WINDOW(vc->window), title);
312 g_free(title);
315 g_free(prefix);
318 static void gd_update_geometry_hints(VirtualConsole *vc)
320 GtkDisplayState *s = vc->s;
321 GdkWindowHints mask = 0;
322 GdkGeometry geo = {};
323 GtkWidget *geo_widget = NULL;
324 GtkWindow *geo_window;
326 if (vc->type == GD_VC_GFX) {
327 if (!vc->gfx.ds) {
328 return;
330 if (s->free_scale) {
331 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
332 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
333 mask |= GDK_HINT_MIN_SIZE;
334 } else {
335 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
336 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
337 mask |= GDK_HINT_MIN_SIZE;
339 geo_widget = vc->gfx.drawing_area;
340 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
342 #if defined(CONFIG_VTE)
343 } else if (vc->type == GD_VC_VTE) {
344 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
345 GtkBorder padding = { 0 };
347 #if VTE_CHECK_VERSION(0, 37, 0)
348 gtk_style_context_get_padding(
349 gtk_widget_get_style_context(vc->vte.terminal),
350 gtk_widget_get_state_flags(vc->vte.terminal),
351 &padding);
352 #else
354 GtkBorder *ib = NULL;
355 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
356 if (ib) {
357 padding = *ib;
358 gtk_border_free(ib);
361 #endif
363 geo.width_inc = vte_terminal_get_char_width(term);
364 geo.height_inc = vte_terminal_get_char_height(term);
365 mask |= GDK_HINT_RESIZE_INC;
366 geo.base_width = geo.width_inc;
367 geo.base_height = geo.height_inc;
368 mask |= GDK_HINT_BASE_SIZE;
369 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
370 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
371 mask |= GDK_HINT_MIN_SIZE;
373 geo.base_width += padding.left + padding.right;
374 geo.base_height += padding.top + padding.bottom;
375 geo.min_width += padding.left + padding.right;
376 geo.min_height += padding.top + padding.bottom;
377 geo_widget = vc->vte.terminal;
378 #endif
381 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
382 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
385 void gd_update_windowsize(VirtualConsole *vc)
387 GtkDisplayState *s = vc->s;
389 gd_update_geometry_hints(vc);
391 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
392 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
393 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
397 static void gd_update_full_redraw(VirtualConsole *vc)
399 GtkWidget *area = vc->gfx.drawing_area;
400 int ww, wh;
401 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
402 #if defined(CONFIG_GTK_GL)
403 if (vc->gfx.gls) {
404 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
405 return;
407 #endif
408 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
411 static void gtk_release_modifiers(GtkDisplayState *s)
413 VirtualConsole *vc = gd_vc_find_current(s);
414 int i, keycode;
416 if (vc->type != GD_VC_GFX ||
417 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
418 return;
420 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
421 keycode = modifier_keycode[i];
422 if (!s->modifier_pressed[i]) {
423 continue;
425 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
426 s->modifier_pressed[i] = false;
430 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
431 GtkWidget *widget)
433 g_object_ref(G_OBJECT(widget));
434 gtk_container_remove(GTK_CONTAINER(from), widget);
435 gtk_container_add(GTK_CONTAINER(to), widget);
436 g_object_unref(G_OBJECT(widget));
439 /** DisplayState Callbacks **/
441 static void gd_update(DisplayChangeListener *dcl,
442 int x, int y, int w, int h)
444 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
445 GdkWindow *win;
446 int x1, x2, y1, y2;
447 int mx, my;
448 int fbw, fbh;
449 int ww, wh;
451 trace_gd_update(vc->label, x, y, w, h);
453 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
454 return;
457 if (vc->gfx.convert) {
458 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
459 NULL, vc->gfx.convert,
460 x, y, 0, 0, x, y, w, h);
463 x1 = floor(x * vc->gfx.scale_x);
464 y1 = floor(y * vc->gfx.scale_y);
466 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
467 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
469 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
470 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
472 win = gtk_widget_get_window(vc->gfx.drawing_area);
473 if (!win) {
474 return;
476 gdk_drawable_get_size(win, &ww, &wh);
478 mx = my = 0;
479 if (ww > fbw) {
480 mx = (ww - fbw) / 2;
482 if (wh > fbh) {
483 my = (wh - fbh) / 2;
486 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
487 mx + x1, my + y1, (x2 - x1), (y2 - y1));
490 static void gd_refresh(DisplayChangeListener *dcl)
492 graphic_hw_update(dcl->con);
495 #if GTK_CHECK_VERSION(3, 0, 0)
496 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
498 #if GTK_CHECK_VERSION(3, 20, 0)
499 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
500 #else
501 return gdk_device_manager_get_client_pointer(
502 gdk_display_get_device_manager(dpy));
503 #endif
506 static void gd_mouse_set(DisplayChangeListener *dcl,
507 int x, int y, int visible)
509 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
510 GdkDisplay *dpy;
511 gint x_root, y_root;
513 if (qemu_input_is_absolute()) {
514 return;
517 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
518 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
519 x, y, &x_root, &y_root);
520 gdk_device_warp(gd_get_pointer(dpy),
521 gtk_widget_get_screen(vc->gfx.drawing_area),
522 x_root, y_root);
523 vc->s->last_x = x;
524 vc->s->last_y = y;
526 #else
527 static void gd_mouse_set(DisplayChangeListener *dcl,
528 int x, int y, int visible)
530 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
531 gint x_root, y_root;
533 if (qemu_input_is_absolute()) {
534 return;
537 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
538 x, y, &x_root, &y_root);
539 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
540 gtk_widget_get_screen(vc->gfx.drawing_area),
541 x_root, y_root);
543 #endif
545 static void gd_cursor_define(DisplayChangeListener *dcl,
546 QEMUCursor *c)
548 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
549 GdkPixbuf *pixbuf;
550 GdkCursor *cursor;
552 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
553 return;
556 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
557 GDK_COLORSPACE_RGB, true, 8,
558 c->width, c->height, c->width * 4,
559 NULL, NULL);
560 cursor = gdk_cursor_new_from_pixbuf
561 (gtk_widget_get_display(vc->gfx.drawing_area),
562 pixbuf, c->hot_x, c->hot_y);
563 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
564 g_object_unref(pixbuf);
565 #if !GTK_CHECK_VERSION(3, 0, 0)
566 gdk_cursor_unref(cursor);
567 #else
568 g_object_unref(cursor);
569 #endif
572 static void gd_switch(DisplayChangeListener *dcl,
573 DisplaySurface *surface)
575 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
576 bool resized = true;
578 trace_gd_switch(vc->label,
579 surface ? surface_width(surface) : 0,
580 surface ? surface_height(surface) : 0);
582 if (vc->gfx.surface) {
583 cairo_surface_destroy(vc->gfx.surface);
584 vc->gfx.surface = NULL;
586 if (vc->gfx.convert) {
587 pixman_image_unref(vc->gfx.convert);
588 vc->gfx.convert = NULL;
591 if (vc->gfx.ds && surface &&
592 surface_width(vc->gfx.ds) == surface_width(surface) &&
593 surface_height(vc->gfx.ds) == surface_height(surface)) {
594 resized = false;
596 vc->gfx.ds = surface;
598 if (!surface) {
599 return;
602 if (surface->format == PIXMAN_x8r8g8b8) {
604 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
606 * No need to convert, use surface directly. Should be the
607 * common case as this is qemu_default_pixelformat(32) too.
609 vc->gfx.surface = cairo_image_surface_create_for_data
610 (surface_data(surface),
611 CAIRO_FORMAT_RGB24,
612 surface_width(surface),
613 surface_height(surface),
614 surface_stride(surface));
615 } else {
616 /* Must convert surface, use pixman to do it. */
617 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
618 surface_width(surface),
619 surface_height(surface),
620 NULL, 0);
621 vc->gfx.surface = cairo_image_surface_create_for_data
622 ((void *)pixman_image_get_data(vc->gfx.convert),
623 CAIRO_FORMAT_RGB24,
624 pixman_image_get_width(vc->gfx.convert),
625 pixman_image_get_height(vc->gfx.convert),
626 pixman_image_get_stride(vc->gfx.convert));
627 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
628 NULL, vc->gfx.convert,
629 0, 0, 0, 0, 0, 0,
630 pixman_image_get_width(vc->gfx.convert),
631 pixman_image_get_height(vc->gfx.convert));
634 if (resized) {
635 gd_update_windowsize(vc);
636 } else {
637 gd_update_full_redraw(vc);
641 static const DisplayChangeListenerOps dcl_ops = {
642 .dpy_name = "gtk",
643 .dpy_gfx_update = gd_update,
644 .dpy_gfx_switch = gd_switch,
645 .dpy_gfx_check_format = qemu_pixman_check_format,
646 .dpy_refresh = gd_refresh,
647 .dpy_mouse_set = gd_mouse_set,
648 .dpy_cursor_define = gd_cursor_define,
652 #if defined(CONFIG_OPENGL)
654 /** DisplayState Callbacks (opengl version) **/
656 #if defined(CONFIG_GTK_GL)
658 static const DisplayChangeListenerOps dcl_gl_area_ops = {
659 .dpy_name = "gtk-egl",
660 .dpy_gfx_update = gd_gl_area_update,
661 .dpy_gfx_switch = gd_gl_area_switch,
662 .dpy_gfx_check_format = console_gl_check_format,
663 .dpy_refresh = gd_gl_area_refresh,
664 .dpy_mouse_set = gd_mouse_set,
665 .dpy_cursor_define = gd_cursor_define,
667 .dpy_gl_ctx_create = gd_gl_area_create_context,
668 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
669 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
670 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
671 .dpy_gl_scanout = gd_gl_area_scanout,
672 .dpy_gl_update = gd_gl_area_scanout_flush,
675 #else
677 static const DisplayChangeListenerOps dcl_egl_ops = {
678 .dpy_name = "gtk-egl",
679 .dpy_gfx_update = gd_egl_update,
680 .dpy_gfx_switch = gd_egl_switch,
681 .dpy_gfx_check_format = console_gl_check_format,
682 .dpy_refresh = gd_egl_refresh,
683 .dpy_mouse_set = gd_mouse_set,
684 .dpy_cursor_define = gd_cursor_define,
686 .dpy_gl_ctx_create = gd_egl_create_context,
687 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
688 .dpy_gl_ctx_make_current = gd_egl_make_current,
689 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
690 .dpy_gl_scanout = gd_egl_scanout,
691 .dpy_gl_update = gd_egl_scanout_flush,
694 #endif /* CONFIG_GTK_GL */
695 #endif /* CONFIG_OPENGL */
697 /** QEMU Events **/
699 static void gd_change_runstate(void *opaque, int running, RunState state)
701 GtkDisplayState *s = opaque;
703 gd_update_caption(s);
706 static void gd_mouse_mode_change(Notifier *notify, void *data)
708 GtkDisplayState *s;
709 int i;
711 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
712 /* release the grab at switching to absolute mode */
713 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
714 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
715 FALSE);
717 for (i = 0; i < s->nb_vcs; i++) {
718 VirtualConsole *vc = &s->vc[i];
719 gd_update_cursor(vc);
723 /** GTK Events **/
725 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
726 void *opaque)
728 GtkDisplayState *s = opaque;
729 int i;
731 if (!no_quit) {
732 for (i = 0; i < s->nb_vcs; i++) {
733 if (s->vc[i].type != GD_VC_GFX) {
734 continue;
736 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
738 qmp_quit(NULL);
739 return FALSE;
742 return TRUE;
745 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
747 QemuUIInfo info;
749 memset(&info, 0, sizeof(info));
750 info.width = width;
751 info.height = height;
752 dpy_set_ui_info(vc->gfx.dcl.con, &info);
755 #if defined(CONFIG_GTK_GL)
757 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
758 void *opaque)
760 VirtualConsole *vc = opaque;
762 if (vc->gfx.gls) {
763 gd_gl_area_draw(vc);
765 return TRUE;
768 static void gd_resize_event(GtkGLArea *area,
769 gint width, gint height, gpointer *opaque)
771 VirtualConsole *vc = (void *)opaque;
773 gd_set_ui_info(vc, width, height);
776 #endif
778 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
780 VirtualConsole *vc = opaque;
781 GtkDisplayState *s = vc->s;
782 int mx, my;
783 int ww, wh;
784 int fbw, fbh;
786 #if defined(CONFIG_OPENGL)
787 if (vc->gfx.gls) {
788 #if defined(CONFIG_GTK_GL)
789 /* invoke render callback please */
790 return FALSE;
791 #else
792 gd_egl_draw(vc);
793 return TRUE;
794 #endif
796 #endif
798 if (!gtk_widget_get_realized(widget)) {
799 return FALSE;
801 if (!vc->gfx.ds) {
802 return FALSE;
805 fbw = surface_width(vc->gfx.ds);
806 fbh = surface_height(vc->gfx.ds);
808 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
810 if (s->full_screen) {
811 vc->gfx.scale_x = (double)ww / fbw;
812 vc->gfx.scale_y = (double)wh / fbh;
813 } else if (s->free_scale) {
814 double sx, sy;
816 sx = (double)ww / fbw;
817 sy = (double)wh / fbh;
819 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
822 fbw *= vc->gfx.scale_x;
823 fbh *= vc->gfx.scale_y;
825 mx = my = 0;
826 if (ww > fbw) {
827 mx = (ww - fbw) / 2;
829 if (wh > fbh) {
830 my = (wh - fbh) / 2;
833 cairo_rectangle(cr, 0, 0, ww, wh);
835 /* Optionally cut out the inner area where the pixmap
836 will be drawn. This avoids 'flashing' since we're
837 not double-buffering. Note we're using the undocumented
838 behaviour of drawing the rectangle from right to left
839 to cut out the whole */
840 cairo_rectangle(cr, mx + fbw, my,
841 -1 * fbw, fbh);
842 cairo_fill(cr);
844 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
845 cairo_set_source_surface(cr, vc->gfx.surface,
846 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
847 cairo_paint(cr);
849 return TRUE;
852 #if !GTK_CHECK_VERSION(3, 0, 0)
853 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
854 void *opaque)
856 cairo_t *cr;
857 gboolean ret;
859 cr = gdk_cairo_create(gtk_widget_get_window(widget));
860 cairo_rectangle(cr,
861 expose->area.x,
862 expose->area.y,
863 expose->area.width,
864 expose->area.height);
865 cairo_clip(cr);
867 ret = gd_draw_event(widget, cr, opaque);
869 cairo_destroy(cr);
871 return ret;
873 #endif
875 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
876 void *opaque)
878 VirtualConsole *vc = opaque;
879 GtkDisplayState *s = vc->s;
880 int x, y;
881 int mx, my;
882 int fbh, fbw;
883 int ww, wh;
885 if (!vc->gfx.ds) {
886 return TRUE;
889 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
890 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
892 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
893 &ww, &wh);
895 mx = my = 0;
896 if (ww > fbw) {
897 mx = (ww - fbw) / 2;
899 if (wh > fbh) {
900 my = (wh - fbh) / 2;
903 x = (motion->x - mx) / vc->gfx.scale_x;
904 y = (motion->y - my) / vc->gfx.scale_y;
906 if (qemu_input_is_absolute()) {
907 if (x < 0 || y < 0 ||
908 x >= surface_width(vc->gfx.ds) ||
909 y >= surface_height(vc->gfx.ds)) {
910 return TRUE;
912 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
913 surface_width(vc->gfx.ds));
914 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
915 surface_height(vc->gfx.ds));
916 qemu_input_event_sync();
917 } else if (s->last_set && s->ptr_owner == vc) {
918 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
919 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
920 qemu_input_event_sync();
922 s->last_x = x;
923 s->last_y = y;
924 s->last_set = TRUE;
926 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
927 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
928 int screen_width, screen_height;
930 int x = (int)motion->x_root;
931 int y = (int)motion->y_root;
933 #if GTK_CHECK_VERSION(3, 22, 0)
935 GdkDisplay *dpy = gtk_widget_get_display(widget);
936 GdkWindow *win = gtk_widget_get_window(widget);
937 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
938 GdkRectangle geometry;
939 gdk_monitor_get_geometry(monitor, &geometry);
940 screen_width = geometry.width;
941 screen_height = geometry.height;
943 #else
945 screen_width = gdk_screen_get_width(screen);
946 screen_height = gdk_screen_get_height(screen);
948 #endif
950 /* In relative mode check to see if client pointer hit
951 * one of the screen edges, and if so move it back by
952 * 200 pixels. This is important because the pointer
953 * in the server doesn't correspond 1-for-1, and so
954 * may still be only half way across the screen. Without
955 * this warp, the server pointer would thus appear to hit
956 * an invisible wall */
957 if (x == 0) {
958 x += 200;
960 if (y == 0) {
961 y += 200;
963 if (x == (screen_width - 1)) {
964 x -= 200;
966 if (y == (screen_height - 1)) {
967 y -= 200;
970 if (x != (int)motion->x_root || y != (int)motion->y_root) {
971 #if GTK_CHECK_VERSION(3, 0, 0)
972 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
973 gdk_device_warp(dev, screen, x, y);
974 #else
975 GdkDisplay *display = gtk_widget_get_display(widget);
976 gdk_display_warp_pointer(display, screen, x, y);
977 #endif
978 s->last_set = FALSE;
979 return FALSE;
982 return TRUE;
985 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
986 void *opaque)
988 VirtualConsole *vc = opaque;
989 GtkDisplayState *s = vc->s;
990 InputButton btn;
992 /* implicitly grab the input at the first click in the relative mode */
993 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
994 !qemu_input_is_absolute() && s->ptr_owner != vc) {
995 if (!vc->window) {
996 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
997 TRUE);
998 } else {
999 gd_grab_pointer(vc, "relative-mode-click");
1001 return TRUE;
1004 if (button->button == 1) {
1005 btn = INPUT_BUTTON_LEFT;
1006 } else if (button->button == 2) {
1007 btn = INPUT_BUTTON_MIDDLE;
1008 } else if (button->button == 3) {
1009 btn = INPUT_BUTTON_RIGHT;
1010 } else {
1011 return TRUE;
1014 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1015 button->type == GDK_BUTTON_PRESS);
1016 qemu_input_event_sync();
1017 return TRUE;
1020 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1021 void *opaque)
1023 VirtualConsole *vc = opaque;
1024 InputButton btn;
1026 if (scroll->direction == GDK_SCROLL_UP) {
1027 btn = INPUT_BUTTON_WHEEL_UP;
1028 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1029 btn = INPUT_BUTTON_WHEEL_DOWN;
1030 } else {
1031 return TRUE;
1034 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1035 qemu_input_event_sync();
1036 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1037 qemu_input_event_sync();
1038 return TRUE;
1041 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
1043 int qemu_keycode;
1045 #ifdef GDK_WINDOWING_WIN32
1046 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1047 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
1048 switch (qemu_keycode) {
1049 case 103: /* alt gr */
1050 qemu_keycode = 56 | SCANCODE_GREY;
1051 break;
1053 return qemu_keycode;
1055 #endif
1057 if (gdk_keycode < 9) {
1058 qemu_keycode = 0;
1059 } else if (gdk_keycode < 97) {
1060 qemu_keycode = gdk_keycode - 8;
1061 #ifdef GDK_WINDOWING_X11
1062 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
1063 if (s->has_evdev) {
1064 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1065 } else {
1066 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
1068 #endif
1069 #ifdef GDK_WINDOWING_WAYLAND
1070 } else if (GDK_IS_WAYLAND_DISPLAY(dpy) && gdk_keycode < 158) {
1071 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1072 #endif
1073 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
1074 qemu_keycode = 0x70;
1075 } else if (gdk_keycode == 211) { /* backslash */
1076 qemu_keycode = 0x73;
1077 } else {
1078 qemu_keycode = 0;
1081 return qemu_keycode;
1084 static gboolean gd_text_key_down(GtkWidget *widget,
1085 GdkEventKey *key, void *opaque)
1087 VirtualConsole *vc = opaque;
1088 QemuConsole *con = vc->gfx.dcl.con;
1090 if (key->keyval == GDK_KEY_Delete) {
1091 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1092 } else if (key->length) {
1093 kbd_put_string_console(con, key->string, key->length);
1094 } else {
1095 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
1096 key->hardware_keycode);
1097 int qcode = qemu_input_key_number_to_qcode(num);
1098 kbd_put_qcode_console(con, qcode);
1100 return TRUE;
1103 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1105 VirtualConsole *vc = opaque;
1106 GtkDisplayState *s = vc->s;
1107 int gdk_keycode = key->hardware_keycode;
1108 int qemu_keycode;
1109 int i;
1111 if (s->ignore_keys) {
1112 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1113 return TRUE;
1116 if (key->keyval == GDK_KEY_Pause) {
1117 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1118 key->type == GDK_KEY_PRESS);
1119 return TRUE;
1122 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
1123 gdk_keycode);
1125 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
1126 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1128 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1129 if (qemu_keycode == modifier_keycode[i]) {
1130 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1134 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
1135 key->type == GDK_KEY_PRESS);
1137 return TRUE;
1140 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1142 if (event->type == GDK_MOTION_NOTIFY) {
1143 return gd_motion_event(widget, &event->motion, opaque);
1145 return FALSE;
1148 /** Window Menu Actions **/
1150 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1152 GtkDisplayState *s = opaque;
1154 if (s->external_pause_update) {
1155 return;
1157 if (runstate_is_running()) {
1158 qmp_stop(NULL);
1159 } else {
1160 qmp_cont(NULL);
1164 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1166 qmp_system_reset(NULL);
1169 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1171 qmp_system_powerdown(NULL);
1174 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1176 qmp_quit(NULL);
1179 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1181 GtkDisplayState *s = opaque;
1182 VirtualConsole *vc = gd_vc_find_by_menu(s);
1183 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1184 gint page;
1186 gtk_release_modifiers(s);
1187 if (vc) {
1188 page = gtk_notebook_page_num(nb, vc->tab_item);
1189 gtk_notebook_set_current_page(nb, page);
1190 gtk_widget_grab_focus(vc->focus);
1192 s->ignore_keys = false;
1195 static void gd_accel_switch_vc(void *opaque)
1197 VirtualConsole *vc = opaque;
1199 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1200 #if !GTK_CHECK_VERSION(3, 0, 0)
1201 /* GTK2 sends the accel key to the target console - ignore this until */
1202 vc->s->ignore_keys = true;
1203 #endif
1206 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1208 GtkDisplayState *s = opaque;
1209 VirtualConsole *vc = gd_vc_find_current(s);
1211 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1212 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1213 } else {
1214 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1216 gd_update_windowsize(vc);
1219 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1220 void *opaque)
1222 VirtualConsole *vc = opaque;
1223 GtkDisplayState *s = vc->s;
1225 gtk_widget_set_sensitive(vc->menu_item, true);
1226 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1227 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1228 vc->tab_item, vc->label);
1229 gtk_widget_destroy(vc->window);
1230 vc->window = NULL;
1231 return TRUE;
1234 static gboolean gd_win_grab(void *opaque)
1236 VirtualConsole *vc = opaque;
1238 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1239 if (vc->s->ptr_owner) {
1240 gd_ungrab_pointer(vc->s);
1241 } else {
1242 gd_grab_pointer(vc, "user-request-detached-tab");
1244 return TRUE;
1247 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1249 GtkDisplayState *s = opaque;
1250 VirtualConsole *vc = gd_vc_find_current(s);
1252 if (vc->type == GD_VC_GFX &&
1253 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1254 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1255 FALSE);
1257 if (!vc->window) {
1258 gtk_widget_set_sensitive(vc->menu_item, false);
1259 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1260 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1262 g_signal_connect(vc->window, "delete-event",
1263 G_CALLBACK(gd_tab_window_close), vc);
1264 gtk_widget_show_all(vc->window);
1266 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1267 GtkAccelGroup *ag = gtk_accel_group_new();
1268 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1270 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1271 vc, NULL);
1272 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1275 gd_update_geometry_hints(vc);
1276 gd_update_caption(s);
1280 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1282 GtkDisplayState *s = opaque;
1283 VirtualConsole *vc = gd_vc_find_current(s);
1285 if (!s->full_screen) {
1286 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1287 gtk_widget_hide(s->menu_bar);
1288 if (vc->type == GD_VC_GFX) {
1289 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1291 gtk_window_fullscreen(GTK_WINDOW(s->window));
1292 s->full_screen = TRUE;
1293 } else {
1294 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1295 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1296 gtk_widget_show(s->menu_bar);
1297 s->full_screen = FALSE;
1298 if (vc->type == GD_VC_GFX) {
1299 vc->gfx.scale_x = 1.0;
1300 vc->gfx.scale_y = 1.0;
1301 gd_update_windowsize(vc);
1305 gd_update_cursor(vc);
1308 static void gd_accel_full_screen(void *opaque)
1310 GtkDisplayState *s = opaque;
1311 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1314 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1316 GtkDisplayState *s = opaque;
1317 VirtualConsole *vc = gd_vc_find_current(s);
1319 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1320 FALSE);
1322 vc->gfx.scale_x += VC_SCALE_STEP;
1323 vc->gfx.scale_y += VC_SCALE_STEP;
1325 gd_update_windowsize(vc);
1328 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1330 GtkDisplayState *s = opaque;
1331 VirtualConsole *vc = gd_vc_find_current(s);
1333 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1334 FALSE);
1336 vc->gfx.scale_x -= VC_SCALE_STEP;
1337 vc->gfx.scale_y -= VC_SCALE_STEP;
1339 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1340 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1342 gd_update_windowsize(vc);
1345 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1347 GtkDisplayState *s = opaque;
1348 VirtualConsole *vc = gd_vc_find_current(s);
1350 vc->gfx.scale_x = 1.0;
1351 vc->gfx.scale_y = 1.0;
1353 gd_update_windowsize(vc);
1356 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1358 GtkDisplayState *s = opaque;
1359 VirtualConsole *vc = gd_vc_find_current(s);
1361 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1362 s->free_scale = TRUE;
1363 } else {
1364 s->free_scale = FALSE;
1365 vc->gfx.scale_x = 1.0;
1366 vc->gfx.scale_y = 1.0;
1369 gd_update_windowsize(vc);
1370 gd_update_full_redraw(vc);
1373 #if GTK_CHECK_VERSION(3, 20, 0)
1374 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1376 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1377 GdkSeat *seat = gdk_display_get_default_seat(display);
1378 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1379 GdkSeatCapabilities caps = 0;
1380 GdkCursor *cursor = NULL;
1382 if (kbd) {
1383 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1385 if (ptr) {
1386 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1387 cursor = vc->s->null_cursor;
1390 if (caps) {
1391 gdk_seat_grab(seat, window, caps, false, cursor,
1392 NULL, NULL, NULL);
1393 } else {
1394 gdk_seat_ungrab(seat);
1397 #elif GTK_CHECK_VERSION(3, 0, 0)
1398 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1399 GdkInputSource source, GdkEventMask mask,
1400 GdkCursor *cursor)
1402 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1403 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1404 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1405 GList *tmp = devs;
1407 for (tmp = devs; tmp; tmp = tmp->next) {
1408 GdkDevice *dev = tmp->data;
1409 if (gdk_device_get_source(dev) != source) {
1410 continue;
1412 if (grab) {
1413 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1414 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1415 mask, cursor, GDK_CURRENT_TIME);
1416 } else {
1417 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1420 g_list_free(devs);
1422 #endif
1424 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1426 if (vc->s->kbd_owner) {
1427 if (vc->s->kbd_owner == vc) {
1428 return;
1429 } else {
1430 gd_ungrab_keyboard(vc->s);
1434 #if GTK_CHECK_VERSION(3, 20, 0)
1435 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1436 #elif GTK_CHECK_VERSION(3, 0, 0)
1437 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1438 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1439 NULL);
1440 #else
1441 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1442 FALSE,
1443 GDK_CURRENT_TIME);
1444 #endif
1445 vc->s->kbd_owner = vc;
1446 gd_update_caption(vc->s);
1447 trace_gd_grab(vc->label, "kbd", reason);
1450 static void gd_ungrab_keyboard(GtkDisplayState *s)
1452 VirtualConsole *vc = s->kbd_owner;
1454 if (vc == NULL) {
1455 return;
1457 s->kbd_owner = NULL;
1459 #if GTK_CHECK_VERSION(3, 20, 0)
1460 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1461 #elif GTK_CHECK_VERSION(3, 0, 0)
1462 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1463 #else
1464 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1465 #endif
1466 gd_update_caption(s);
1467 trace_gd_ungrab(vc->label, "kbd");
1470 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1472 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1474 if (vc->s->ptr_owner) {
1475 if (vc->s->ptr_owner == vc) {
1476 return;
1477 } else {
1478 gd_ungrab_pointer(vc->s);
1482 #if GTK_CHECK_VERSION(3, 20, 0)
1483 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1484 gdk_device_get_position(gd_get_pointer(display),
1485 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1486 #elif GTK_CHECK_VERSION(3, 0, 0)
1487 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1488 GDK_POINTER_MOTION_MASK |
1489 GDK_BUTTON_PRESS_MASK |
1490 GDK_BUTTON_RELEASE_MASK |
1491 GDK_BUTTON_MOTION_MASK |
1492 GDK_SCROLL_MASK,
1493 vc->s->null_cursor);
1494 gdk_device_get_position(gd_get_pointer(display),
1495 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1496 #else
1497 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1498 FALSE, /* All events to come to our window directly */
1499 GDK_POINTER_MOTION_MASK |
1500 GDK_BUTTON_PRESS_MASK |
1501 GDK_BUTTON_RELEASE_MASK |
1502 GDK_BUTTON_MOTION_MASK |
1503 GDK_SCROLL_MASK,
1504 NULL, /* Allow cursor to move over entire desktop */
1505 vc->s->null_cursor,
1506 GDK_CURRENT_TIME);
1507 gdk_display_get_pointer(display, NULL,
1508 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1509 #endif
1510 vc->s->ptr_owner = vc;
1511 gd_update_caption(vc->s);
1512 trace_gd_grab(vc->label, "ptr", reason);
1515 static void gd_ungrab_pointer(GtkDisplayState *s)
1517 VirtualConsole *vc = s->ptr_owner;
1518 GdkDisplay *display;
1520 if (vc == NULL) {
1521 return;
1523 s->ptr_owner = NULL;
1525 display = gtk_widget_get_display(vc->gfx.drawing_area);
1526 #if GTK_CHECK_VERSION(3, 20, 0)
1527 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1528 gdk_device_warp(gd_get_pointer(display),
1529 gtk_widget_get_screen(vc->gfx.drawing_area),
1530 vc->s->grab_x_root, vc->s->grab_y_root);
1531 #elif GTK_CHECK_VERSION(3, 0, 0)
1532 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1533 gdk_device_warp(gd_get_pointer(display),
1534 gtk_widget_get_screen(vc->gfx.drawing_area),
1535 vc->s->grab_x_root, vc->s->grab_y_root);
1536 #else
1537 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1538 gdk_display_warp_pointer(display,
1539 gtk_widget_get_screen(vc->gfx.drawing_area),
1540 vc->s->grab_x_root, vc->s->grab_y_root);
1541 #endif
1542 gd_update_caption(s);
1543 trace_gd_ungrab(vc->label, "ptr");
1546 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1548 GtkDisplayState *s = opaque;
1549 VirtualConsole *vc = gd_vc_find_current(s);
1551 if (gd_is_grab_active(s)) {
1552 gd_grab_keyboard(vc, "user-request-main-window");
1553 gd_grab_pointer(vc, "user-request-main-window");
1554 } else {
1555 gd_ungrab_keyboard(s);
1556 gd_ungrab_pointer(s);
1559 gd_update_cursor(vc);
1562 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1563 gpointer data)
1565 GtkDisplayState *s = data;
1566 VirtualConsole *vc;
1567 gboolean on_vga;
1569 if (!gtk_widget_get_realized(s->notebook)) {
1570 return;
1573 #ifdef VTE_RESIZE_HACK
1574 vc = gd_vc_find_current(s);
1575 if (vc && vc->type == GD_VC_VTE) {
1576 gtk_widget_hide(vc->vte.terminal);
1578 #endif
1579 vc = gd_vc_find_by_page(s, arg2);
1580 if (!vc) {
1581 return;
1583 #ifdef VTE_RESIZE_HACK
1584 if (vc->type == GD_VC_VTE) {
1585 gtk_widget_show(vc->vte.terminal);
1587 #endif
1588 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1589 TRUE);
1590 on_vga = (vc->type == GD_VC_GFX &&
1591 qemu_console_is_graphic(vc->gfx.dcl.con));
1592 if (!on_vga) {
1593 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1594 FALSE);
1595 } else if (s->full_screen) {
1596 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1597 TRUE);
1599 gtk_widget_set_sensitive(s->grab_item, on_vga);
1600 #ifdef CONFIG_VTE
1601 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1602 #endif
1604 gd_update_windowsize(vc);
1605 gd_update_cursor(vc);
1608 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1609 gpointer opaque)
1611 VirtualConsole *vc = opaque;
1612 GtkDisplayState *s = vc->s;
1614 if (gd_grab_on_hover(s)) {
1615 gd_grab_keyboard(vc, "grab-on-hover");
1617 return TRUE;
1620 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1621 gpointer opaque)
1623 VirtualConsole *vc = opaque;
1624 GtkDisplayState *s = vc->s;
1626 if (gd_grab_on_hover(s)) {
1627 gd_ungrab_keyboard(s);
1629 return TRUE;
1632 static gboolean gd_focus_out_event(GtkWidget *widget,
1633 GdkEventCrossing *crossing, gpointer opaque)
1635 VirtualConsole *vc = opaque;
1636 GtkDisplayState *s = vc->s;
1638 gtk_release_modifiers(s);
1639 return TRUE;
1642 static gboolean gd_configure(GtkWidget *widget,
1643 GdkEventConfigure *cfg, gpointer opaque)
1645 VirtualConsole *vc = opaque;
1647 gd_set_ui_info(vc, cfg->width, cfg->height);
1648 return FALSE;
1651 /** Virtual Console Callbacks **/
1653 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1654 int idx, GSList *group, GtkWidget *view_menu)
1656 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1657 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1658 HOTKEY_MODIFIERS, 0,
1659 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1660 #if GTK_CHECK_VERSION(3, 8, 0)
1661 gtk_accel_label_set_accel(
1662 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1663 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1664 #endif
1666 g_signal_connect(vc->menu_item, "activate",
1667 G_CALLBACK(gd_menu_switch_vc), s);
1668 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1670 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1671 return group;
1674 #if defined(CONFIG_VTE)
1675 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1677 GtkDisplayState *s = opaque;
1678 VirtualConsole *vc = gd_vc_find_current(s);
1680 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1683 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1685 VirtualConsole *vc = opaque;
1687 if (gtk_adjustment_get_upper(adjustment) >
1688 gtk_adjustment_get_page_size(adjustment)) {
1689 gtk_widget_show(vc->vte.scrollbar);
1690 } else {
1691 gtk_widget_hide(vc->vte.scrollbar);
1695 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1697 VCChardev *vcd = VC_CHARDEV(chr);
1698 VirtualConsole *vc = vcd->console;
1700 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1701 return len;
1704 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1706 VCChardev *vcd = VC_CHARDEV(chr);
1707 VirtualConsole *vc = vcd->console;
1709 if (vc) {
1710 vc->vte.echo = echo;
1711 } else {
1712 vcd->echo = echo;
1716 static int nb_vcs;
1717 static Chardev *vcs[MAX_VCS];
1718 static void gd_vc_open(Chardev *chr,
1719 ChardevBackend *backend,
1720 bool *be_opened,
1721 Error **errp)
1723 if (nb_vcs == MAX_VCS) {
1724 error_setg(errp, "Maximum number of consoles reached");
1725 return;
1728 vcs[nb_vcs++] = chr;
1730 /* console/chardev init sometimes completes elsewhere in a 2nd
1731 * stage, so defer OPENED events until they are fully initialized
1733 *be_opened = false;
1736 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1738 ChardevClass *cc = CHARDEV_CLASS(oc);
1740 cc->parse = qemu_chr_parse_vc;
1741 cc->open = gd_vc_open;
1742 cc->chr_write = gd_vc_chr_write;
1743 cc->chr_set_echo = gd_vc_chr_set_echo;
1746 static const TypeInfo char_gd_vc_type_info = {
1747 .name = TYPE_CHARDEV_VC,
1748 .parent = TYPE_CHARDEV,
1749 .instance_size = sizeof(VCChardev),
1750 .class_init = char_gd_vc_class_init,
1753 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1754 gpointer user_data)
1756 VirtualConsole *vc = user_data;
1758 if (vc->vte.echo) {
1759 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1760 int i;
1761 for (i = 0; i < size; i++) {
1762 uint8_t c = text[i];
1763 if (c >= 128 || isprint(c)) {
1764 /* 8-bit characters are considered printable. */
1765 vte_terminal_feed(term, &text[i], 1);
1766 } else if (c == '\r' || c == '\n') {
1767 vte_terminal_feed(term, "\r\n", 2);
1768 } else {
1769 char ctrl[2] = { '^', 0};
1770 ctrl[1] = text[i] ^ 64;
1771 vte_terminal_feed(term, ctrl, 2);
1776 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1777 return TRUE;
1780 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1781 Chardev *chr, int idx,
1782 GSList *group, GtkWidget *view_menu)
1784 char buffer[32];
1785 GtkWidget *box;
1786 GtkWidget *scrollbar;
1787 GtkAdjustment *vadjustment;
1788 VCChardev *vcd = VC_CHARDEV(chr);
1790 vc->s = s;
1791 vc->vte.echo = vcd->echo;
1792 vc->vte.chr = chr;
1793 vcd->console = vc;
1795 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1796 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1797 ? vc->vte.chr->label : buffer);
1798 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1800 vc->vte.terminal = vte_terminal_new();
1801 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1803 /* The documentation says that the default is UTF-8, but actually it is
1804 * 7-bit ASCII at least in VTE 0.38.
1806 #if VTE_CHECK_VERSION(0, 38, 0)
1807 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1808 #else
1809 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1810 #endif
1812 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1813 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1814 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1816 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1817 vadjustment = gtk_scrollable_get_vadjustment
1818 (GTK_SCROLLABLE(vc->vte.terminal));
1819 #else
1820 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1821 #endif
1823 #if GTK_CHECK_VERSION(3, 0, 0)
1824 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1825 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1826 #else
1827 box = gtk_hbox_new(false, 2);
1828 scrollbar = gtk_vscrollbar_new(vadjustment);
1829 #endif
1831 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1832 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1834 vc->vte.box = box;
1835 vc->vte.scrollbar = scrollbar;
1837 g_signal_connect(vadjustment, "changed",
1838 G_CALLBACK(gd_vc_adjustment_changed), vc);
1840 vc->type = GD_VC_VTE;
1841 vc->tab_item = box;
1842 vc->focus = vc->vte.terminal;
1843 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1844 gtk_label_new(vc->label));
1846 qemu_chr_be_generic_open(vc->vte.chr);
1848 return group;
1851 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1852 GtkWidget *view_menu)
1854 int i;
1856 for (i = 0; i < nb_vcs; i++) {
1857 VirtualConsole *vc = &s->vc[s->nb_vcs];
1858 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1859 s->nb_vcs++;
1862 #endif /* CONFIG_VTE */
1864 /** Window Creation **/
1866 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1868 #if GTK_CHECK_VERSION(3, 0, 0)
1869 g_signal_connect(vc->gfx.drawing_area, "draw",
1870 G_CALLBACK(gd_draw_event), vc);
1871 #if defined(CONFIG_GTK_GL)
1872 if (display_opengl) {
1873 /* wire up GtkGlArea events */
1874 g_signal_connect(vc->gfx.drawing_area, "render",
1875 G_CALLBACK(gd_render_event), vc);
1876 g_signal_connect(vc->gfx.drawing_area, "resize",
1877 G_CALLBACK(gd_resize_event), vc);
1879 #endif
1880 #else
1881 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1882 G_CALLBACK(gd_expose_event), vc);
1883 #endif
1884 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1885 g_signal_connect(vc->gfx.drawing_area, "event",
1886 G_CALLBACK(gd_event), vc);
1887 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1888 G_CALLBACK(gd_button_event), vc);
1889 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1890 G_CALLBACK(gd_button_event), vc);
1891 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1892 G_CALLBACK(gd_scroll_event), vc);
1893 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1894 G_CALLBACK(gd_key_event), vc);
1895 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1896 G_CALLBACK(gd_key_event), vc);
1898 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1899 G_CALLBACK(gd_enter_event), vc);
1900 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1901 G_CALLBACK(gd_leave_event), vc);
1902 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1903 G_CALLBACK(gd_focus_out_event), vc);
1904 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1905 G_CALLBACK(gd_configure), vc);
1906 } else {
1907 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1908 G_CALLBACK(gd_text_key_down), vc);
1912 static void gd_connect_signals(GtkDisplayState *s)
1914 g_signal_connect(s->show_tabs_item, "activate",
1915 G_CALLBACK(gd_menu_show_tabs), s);
1916 g_signal_connect(s->untabify_item, "activate",
1917 G_CALLBACK(gd_menu_untabify), s);
1919 g_signal_connect(s->window, "delete-event",
1920 G_CALLBACK(gd_window_close), s);
1922 g_signal_connect(s->pause_item, "activate",
1923 G_CALLBACK(gd_menu_pause), s);
1924 g_signal_connect(s->reset_item, "activate",
1925 G_CALLBACK(gd_menu_reset), s);
1926 g_signal_connect(s->powerdown_item, "activate",
1927 G_CALLBACK(gd_menu_powerdown), s);
1928 g_signal_connect(s->quit_item, "activate",
1929 G_CALLBACK(gd_menu_quit), s);
1930 #if defined(CONFIG_VTE)
1931 g_signal_connect(s->copy_item, "activate",
1932 G_CALLBACK(gd_menu_copy), s);
1933 #endif
1934 g_signal_connect(s->full_screen_item, "activate",
1935 G_CALLBACK(gd_menu_full_screen), s);
1936 g_signal_connect(s->zoom_in_item, "activate",
1937 G_CALLBACK(gd_menu_zoom_in), s);
1938 g_signal_connect(s->zoom_out_item, "activate",
1939 G_CALLBACK(gd_menu_zoom_out), s);
1940 g_signal_connect(s->zoom_fixed_item, "activate",
1941 G_CALLBACK(gd_menu_zoom_fixed), s);
1942 g_signal_connect(s->zoom_fit_item, "activate",
1943 G_CALLBACK(gd_menu_zoom_fit), s);
1944 g_signal_connect(s->grab_item, "activate",
1945 G_CALLBACK(gd_menu_grab_input), s);
1946 g_signal_connect(s->notebook, "switch-page",
1947 G_CALLBACK(gd_change_page), s);
1950 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1952 GtkWidget *machine_menu;
1953 GtkWidget *separator;
1955 machine_menu = gtk_menu_new();
1956 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1958 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1959 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1961 separator = gtk_separator_menu_item_new();
1962 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1964 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1965 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1967 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1968 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1970 separator = gtk_separator_menu_item_new();
1971 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1973 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1974 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1975 "<QEMU>/Machine/Quit");
1976 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1977 GDK_KEY_q, HOTKEY_MODIFIERS);
1978 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1980 return machine_menu;
1983 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1984 QemuConsole *con, int idx,
1985 GSList *group, GtkWidget *view_menu)
1987 vc->label = qemu_console_get_label(con);
1988 vc->s = s;
1989 vc->gfx.scale_x = 1.0;
1990 vc->gfx.scale_y = 1.0;
1992 #if defined(CONFIG_OPENGL)
1993 if (display_opengl) {
1994 #if defined(CONFIG_GTK_GL)
1995 vc->gfx.drawing_area = gtk_gl_area_new();
1996 vc->gfx.dcl.ops = &dcl_gl_area_ops;
1997 #else
1998 vc->gfx.drawing_area = gtk_drawing_area_new();
2000 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2001 * It is required for opengl rendering on X11 though. A
2002 * proper replacement (native opengl support) is only
2003 * available in 3.16+. Silence the warning if possible.
2005 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2006 #pragma GCC diagnostic push
2007 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2008 #endif
2009 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2010 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2011 #pragma GCC diagnostic pop
2012 #endif
2013 vc->gfx.dcl.ops = &dcl_egl_ops;
2014 #endif /* CONFIG_GTK_GL */
2015 } else
2016 #endif
2018 vc->gfx.drawing_area = gtk_drawing_area_new();
2019 vc->gfx.dcl.ops = &dcl_ops;
2023 gtk_widget_add_events(vc->gfx.drawing_area,
2024 GDK_POINTER_MOTION_MASK |
2025 GDK_BUTTON_PRESS_MASK |
2026 GDK_BUTTON_RELEASE_MASK |
2027 GDK_BUTTON_MOTION_MASK |
2028 GDK_ENTER_NOTIFY_MASK |
2029 GDK_LEAVE_NOTIFY_MASK |
2030 GDK_SCROLL_MASK |
2031 GDK_KEY_PRESS_MASK);
2032 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2034 vc->type = GD_VC_GFX;
2035 vc->tab_item = vc->gfx.drawing_area;
2036 vc->focus = vc->gfx.drawing_area;
2037 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2038 vc->tab_item, gtk_label_new(vc->label));
2040 vc->gfx.dcl.con = con;
2041 register_displaychangelistener(&vc->gfx.dcl);
2043 gd_connect_vc_gfx_signals(vc);
2044 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2046 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2047 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2048 s->free_scale = true;
2051 return group;
2054 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2056 GSList *group = NULL;
2057 GtkWidget *view_menu;
2058 GtkWidget *separator;
2059 QemuConsole *con;
2060 int vc;
2062 view_menu = gtk_menu_new();
2063 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2065 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2067 #if defined(CONFIG_VTE)
2068 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2069 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2070 #endif
2072 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2073 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2074 #if GTK_CHECK_VERSION(3, 8, 0)
2075 gtk_accel_label_set_accel(
2076 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2077 GDK_KEY_f, HOTKEY_MODIFIERS);
2078 #endif
2079 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2081 separator = gtk_separator_menu_item_new();
2082 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2084 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2085 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2086 "<QEMU>/View/Zoom In");
2087 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2088 HOTKEY_MODIFIERS);
2089 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2091 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2092 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2093 "<QEMU>/View/Zoom Out");
2094 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2095 HOTKEY_MODIFIERS);
2096 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2098 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2099 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2100 "<QEMU>/View/Zoom Fixed");
2101 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2102 HOTKEY_MODIFIERS);
2103 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2105 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2106 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2108 separator = gtk_separator_menu_item_new();
2109 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2111 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2112 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2114 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2115 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2116 "<QEMU>/View/Grab Input");
2117 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2118 HOTKEY_MODIFIERS);
2119 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2121 separator = gtk_separator_menu_item_new();
2122 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2124 /* gfx */
2125 for (vc = 0;; vc++) {
2126 con = qemu_console_lookup_by_index(vc);
2127 if (!con) {
2128 break;
2130 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2131 vc, group, view_menu);
2132 s->nb_vcs++;
2135 #if defined(CONFIG_VTE)
2136 /* vte */
2137 gd_vcs_init(s, group, view_menu);
2138 #endif
2140 separator = gtk_separator_menu_item_new();
2141 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2143 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2144 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2146 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2147 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2149 return view_menu;
2152 static void gd_create_menus(GtkDisplayState *s)
2154 s->accel_group = gtk_accel_group_new();
2155 s->machine_menu = gd_create_menu_machine(s);
2156 s->view_menu = gd_create_menu_view(s);
2158 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2159 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2160 s->machine_menu);
2161 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2163 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2164 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2165 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2167 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2168 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2171 static void gd_set_keycode_type(GtkDisplayState *s)
2173 #ifdef GDK_WINDOWING_X11
2174 GdkDisplay *display = gtk_widget_get_display(s->window);
2175 if (GDK_IS_X11_DISPLAY(display)) {
2176 Display *x11_display = gdk_x11_display_get_xdisplay(display);
2177 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
2178 XkbUseCoreKbd);
2179 char *keycodes = NULL;
2181 if (desc && desc->names) {
2182 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
2184 if (keycodes == NULL) {
2185 fprintf(stderr, "could not lookup keycode name\n");
2186 } else if (strstart(keycodes, "evdev", NULL)) {
2187 s->has_evdev = true;
2188 } else if (!strstart(keycodes, "xfree86", NULL)) {
2189 fprintf(stderr, "unknown keycodes `%s', please report to "
2190 "qemu-devel@nongnu.org\n", keycodes);
2193 if (desc) {
2194 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
2196 if (keycodes) {
2197 XFree(keycodes);
2200 #endif
2203 static gboolean gtkinit;
2205 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
2207 VirtualConsole *vc;
2209 GtkDisplayState *s = g_malloc0(sizeof(*s));
2210 char *filename;
2211 GdkDisplay *window_display;
2213 if (!gtkinit) {
2214 fprintf(stderr, "gtk initialization failed\n");
2215 exit(1);
2218 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2219 #if GTK_CHECK_VERSION(3, 2, 0)
2220 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2221 #else
2222 s->vbox = gtk_vbox_new(FALSE, 0);
2223 #endif
2224 s->notebook = gtk_notebook_new();
2225 s->menu_bar = gtk_menu_bar_new();
2227 s->free_scale = FALSE;
2229 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2230 setlocale(LC_MESSAGES, "");
2231 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2232 textdomain("qemu");
2234 window_display = gtk_widget_get_display(s->window);
2235 s->null_cursor = gdk_cursor_new_for_display(window_display,
2236 GDK_BLANK_CURSOR);
2238 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2239 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2240 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2242 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2243 if (filename) {
2244 GError *error = NULL;
2245 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2246 if (pixbuf) {
2247 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2248 } else {
2249 g_error_free(error);
2251 g_free(filename);
2254 gd_create_menus(s);
2256 gd_connect_signals(s);
2258 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2259 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2261 gd_update_caption(s);
2263 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2264 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2266 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2268 gtk_widget_show_all(s->window);
2270 #ifdef VTE_RESIZE_HACK
2272 VirtualConsole *cur = gd_vc_find_current(s);
2273 if (cur) {
2274 int i;
2276 for (i = 0; i < s->nb_vcs; i++) {
2277 VirtualConsole *vc = &s->vc[i];
2278 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2279 gtk_widget_hide(vc->vte.terminal);
2282 gd_update_windowsize(cur);
2285 #endif
2287 vc = gd_vc_find_current(s);
2288 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2289 #ifdef CONFIG_VTE
2290 gtk_widget_set_sensitive(s->copy_item,
2291 vc && vc->type == GD_VC_VTE);
2292 #endif
2294 if (full_screen) {
2295 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2297 if (grab_on_hover) {
2298 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2301 gd_set_keycode_type(s);
2304 void early_gtk_display_init(int opengl)
2306 /* The QEMU code relies on the assumption that it's always run in
2307 * the C locale. Therefore it is not prepared to deal with
2308 * operations that produce different results depending on the
2309 * locale, such as printf's formatting of decimal numbers, and
2310 * possibly others.
2312 * Since GTK+ calls setlocale() by default -importing the locale
2313 * settings from the environment- we must prevent it from doing so
2314 * using gtk_disable_setlocale().
2316 * QEMU's GTK+ UI, however, _does_ have translations for some of
2317 * the menu items. As a trade-off between a functionally correct
2318 * QEMU and a fully internationalized UI we support importing
2319 * LC_MESSAGES from the environment (see the setlocale() call
2320 * earlier in this file). This allows us to display translated
2321 * messages leaving everything else untouched.
2323 gtk_disable_setlocale();
2324 gtkinit = gtk_init_check(NULL, NULL);
2325 if (!gtkinit) {
2326 /* don't exit yet, that'll break -help */
2327 return;
2330 switch (opengl) {
2331 case -1: /* default */
2332 case 0: /* off */
2333 break;
2334 case 1: /* on */
2335 #if defined(CONFIG_OPENGL)
2336 #if defined(CONFIG_GTK_GL)
2337 gtk_gl_area_init();
2338 #else
2339 gtk_egl_init();
2340 #endif
2341 #endif
2342 break;
2343 default:
2344 g_assert_not_reached();
2345 break;
2348 #if defined(CONFIG_VTE)
2349 type_register(&char_gd_vc_type_info);
2350 #endif