ui: gtk: fix crash when terminal inner-border is NULL
[qemu.git] / ui / gtk.c
blob9876d899aa98b2770d33af14c16ca7f613db4c21
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_WIN32_DISPLAY
94 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
95 #endif
97 #ifndef GDK_KEY_0
98 #define GDK_KEY_0 GDK_0
99 #define GDK_KEY_1 GDK_1
100 #define GDK_KEY_2 GDK_2
101 #define GDK_KEY_f GDK_f
102 #define GDK_KEY_g GDK_g
103 #define GDK_KEY_q GDK_q
104 #define GDK_KEY_plus GDK_plus
105 #define GDK_KEY_minus GDK_minus
106 #define GDK_KEY_Pause GDK_Pause
107 #endif
109 /* Some older mingw versions lack this constant or have
110 * it conditionally defined */
111 #ifdef _WIN32
112 # ifndef MAPVK_VK_TO_VSC
113 # define MAPVK_VK_TO_VSC 0
114 # endif
115 #endif
118 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
120 static const int modifier_keycode[] = {
121 /* shift, control, alt keys, meta keys, both left & right */
122 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
125 struct GtkDisplayState {
126 GtkWidget *window;
128 GtkWidget *menu_bar;
130 GtkAccelGroup *accel_group;
132 GtkWidget *machine_menu_item;
133 GtkWidget *machine_menu;
134 GtkWidget *pause_item;
135 GtkWidget *reset_item;
136 GtkWidget *powerdown_item;
137 GtkWidget *quit_item;
139 GtkWidget *view_menu_item;
140 GtkWidget *view_menu;
141 GtkWidget *full_screen_item;
142 GtkWidget *zoom_in_item;
143 GtkWidget *zoom_out_item;
144 GtkWidget *zoom_fixed_item;
145 GtkWidget *zoom_fit_item;
146 GtkWidget *grab_item;
147 GtkWidget *grab_on_hover_item;
149 int nb_vcs;
150 VirtualConsole vc[MAX_VCS];
152 GtkWidget *show_tabs_item;
153 GtkWidget *untabify_item;
155 GtkWidget *vbox;
156 GtkWidget *notebook;
157 int button_mask;
158 gboolean last_set;
159 int last_x;
160 int last_y;
161 int grab_x_root;
162 int grab_y_root;
163 VirtualConsole *kbd_owner;
164 VirtualConsole *ptr_owner;
166 gboolean full_screen;
168 GdkCursor *null_cursor;
169 Notifier mouse_mode_notifier;
170 gboolean free_scale;
172 bool external_pause_update;
174 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
175 bool has_evdev;
176 bool ignore_keys;
179 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
180 static void gd_ungrab_pointer(GtkDisplayState *s);
181 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
182 static void gd_ungrab_keyboard(GtkDisplayState *s);
184 /** Utility Functions **/
186 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
188 VirtualConsole *vc;
189 gint i;
191 for (i = 0; i < s->nb_vcs; i++) {
192 vc = &s->vc[i];
193 if (gtk_check_menu_item_get_active
194 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
195 return vc;
198 return NULL;
201 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
203 VirtualConsole *vc;
204 gint i, p;
206 for (i = 0; i < s->nb_vcs; i++) {
207 vc = &s->vc[i];
208 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
209 if (p == page) {
210 return vc;
213 return NULL;
216 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
218 gint page;
220 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
221 return gd_vc_find_by_page(s, page);
224 static bool gd_is_grab_active(GtkDisplayState *s)
226 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
229 static bool gd_grab_on_hover(GtkDisplayState *s)
231 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
234 static void gd_update_cursor(VirtualConsole *vc)
236 GtkDisplayState *s = vc->s;
237 GdkWindow *window;
239 if (vc->type != GD_VC_GFX ||
240 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
241 return;
244 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
245 return;
248 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
249 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
250 gdk_window_set_cursor(window, s->null_cursor);
251 } else {
252 gdk_window_set_cursor(window, NULL);
256 static void gd_update_caption(GtkDisplayState *s)
258 const char *status = "";
259 gchar *prefix;
260 gchar *title;
261 const char *grab = "";
262 bool is_paused = !runstate_is_running();
263 int i;
265 if (qemu_name) {
266 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
267 } else {
268 prefix = g_strdup_printf("QEMU");
271 if (s->ptr_owner != NULL &&
272 s->ptr_owner->window == NULL) {
273 grab = _(" - Press Ctrl+Alt+G to release grab");
276 if (is_paused) {
277 status = _(" [Paused]");
279 s->external_pause_update = true;
280 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
281 is_paused);
282 s->external_pause_update = false;
284 title = g_strdup_printf("%s%s%s", prefix, status, grab);
285 gtk_window_set_title(GTK_WINDOW(s->window), title);
286 g_free(title);
288 for (i = 0; i < s->nb_vcs; i++) {
289 VirtualConsole *vc = &s->vc[i];
291 if (!vc->window) {
292 continue;
294 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
295 vc == s->kbd_owner ? " +kbd" : "",
296 vc == s->ptr_owner ? " +ptr" : "");
297 gtk_window_set_title(GTK_WINDOW(vc->window), title);
298 g_free(title);
301 g_free(prefix);
304 static void gd_update_geometry_hints(VirtualConsole *vc)
306 GtkDisplayState *s = vc->s;
307 GdkWindowHints mask = 0;
308 GdkGeometry geo = {};
309 GtkWidget *geo_widget = NULL;
310 GtkWindow *geo_window;
312 if (vc->type == GD_VC_GFX) {
313 if (!vc->gfx.ds) {
314 return;
316 if (s->free_scale) {
317 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
318 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
319 mask |= GDK_HINT_MIN_SIZE;
320 } else {
321 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
322 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
323 mask |= GDK_HINT_MIN_SIZE;
325 geo_widget = vc->gfx.drawing_area;
326 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
328 #if defined(CONFIG_VTE)
329 } else if (vc->type == GD_VC_VTE) {
330 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
331 GtkBorder *ib;
333 geo.width_inc = vte_terminal_get_char_width(term);
334 geo.height_inc = vte_terminal_get_char_height(term);
335 mask |= GDK_HINT_RESIZE_INC;
336 geo.base_width = geo.width_inc;
337 geo.base_height = geo.height_inc;
338 mask |= GDK_HINT_BASE_SIZE;
339 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
340 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
341 mask |= GDK_HINT_MIN_SIZE;
342 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
343 if (ib) {
344 geo.base_width += ib->left + ib->right;
345 geo.base_height += ib->top + ib->bottom;
346 geo.min_width += ib->left + ib->right;
347 geo.min_height += ib->top + ib->bottom;
349 geo_widget = vc->vte.terminal;
350 #endif
353 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
354 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
357 void gd_update_windowsize(VirtualConsole *vc)
359 GtkDisplayState *s = vc->s;
361 gd_update_geometry_hints(vc);
363 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
364 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
365 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
369 static void gd_update_full_redraw(VirtualConsole *vc)
371 GtkWidget *area = vc->gfx.drawing_area;
372 int ww, wh;
373 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
374 #if defined(CONFIG_GTK_GL)
375 if (vc->gfx.gls) {
376 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
377 return;
379 #endif
380 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
383 static void gtk_release_modifiers(GtkDisplayState *s)
385 VirtualConsole *vc = gd_vc_find_current(s);
386 int i, keycode;
388 if (vc->type != GD_VC_GFX ||
389 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
390 return;
392 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
393 keycode = modifier_keycode[i];
394 if (!s->modifier_pressed[i]) {
395 continue;
397 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
398 s->modifier_pressed[i] = false;
402 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
403 GtkWidget *widget)
405 g_object_ref(G_OBJECT(widget));
406 gtk_container_remove(GTK_CONTAINER(from), widget);
407 gtk_container_add(GTK_CONTAINER(to), widget);
408 g_object_unref(G_OBJECT(widget));
411 /** DisplayState Callbacks **/
413 static void gd_update(DisplayChangeListener *dcl,
414 int x, int y, int w, int h)
416 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
417 GdkWindow *win;
418 int x1, x2, y1, y2;
419 int mx, my;
420 int fbw, fbh;
421 int ww, wh;
423 trace_gd_update(vc->label, x, y, w, h);
425 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
426 return;
429 if (vc->gfx.convert) {
430 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
431 NULL, vc->gfx.convert,
432 x, y, 0, 0, x, y, w, h);
435 x1 = floor(x * vc->gfx.scale_x);
436 y1 = floor(y * vc->gfx.scale_y);
438 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
439 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
441 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
442 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
444 win = gtk_widget_get_window(vc->gfx.drawing_area);
445 if (!win) {
446 return;
448 gdk_drawable_get_size(win, &ww, &wh);
450 mx = my = 0;
451 if (ww > fbw) {
452 mx = (ww - fbw) / 2;
454 if (wh > fbh) {
455 my = (wh - fbh) / 2;
458 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
459 mx + x1, my + y1, (x2 - x1), (y2 - y1));
462 static void gd_refresh(DisplayChangeListener *dcl)
464 graphic_hw_update(dcl->con);
467 #if GTK_CHECK_VERSION(3, 0, 0)
468 static void gd_mouse_set(DisplayChangeListener *dcl,
469 int x, int y, int visible)
471 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
472 GdkDisplay *dpy;
473 GdkDeviceManager *mgr;
474 gint x_root, y_root;
476 if (qemu_input_is_absolute()) {
477 return;
480 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
481 mgr = gdk_display_get_device_manager(dpy);
482 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
483 x, y, &x_root, &y_root);
484 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
485 gtk_widget_get_screen(vc->gfx.drawing_area),
486 x_root, y_root);
487 vc->s->last_x = x;
488 vc->s->last_y = y;
490 #else
491 static void gd_mouse_set(DisplayChangeListener *dcl,
492 int x, int y, int visible)
494 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
495 gint x_root, y_root;
497 if (qemu_input_is_absolute()) {
498 return;
501 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
502 x, y, &x_root, &y_root);
503 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
504 gtk_widget_get_screen(vc->gfx.drawing_area),
505 x_root, y_root);
507 #endif
509 static void gd_cursor_define(DisplayChangeListener *dcl,
510 QEMUCursor *c)
512 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
513 GdkPixbuf *pixbuf;
514 GdkCursor *cursor;
516 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
517 return;
520 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
521 GDK_COLORSPACE_RGB, true, 8,
522 c->width, c->height, c->width * 4,
523 NULL, NULL);
524 cursor = gdk_cursor_new_from_pixbuf
525 (gtk_widget_get_display(vc->gfx.drawing_area),
526 pixbuf, c->hot_x, c->hot_y);
527 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
528 g_object_unref(pixbuf);
529 #if !GTK_CHECK_VERSION(3, 0, 0)
530 gdk_cursor_unref(cursor);
531 #else
532 g_object_unref(cursor);
533 #endif
536 static void gd_switch(DisplayChangeListener *dcl,
537 DisplaySurface *surface)
539 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
540 bool resized = true;
542 trace_gd_switch(vc->label,
543 surface ? surface_width(surface) : 0,
544 surface ? surface_height(surface) : 0);
546 if (vc->gfx.surface) {
547 cairo_surface_destroy(vc->gfx.surface);
548 vc->gfx.surface = NULL;
550 if (vc->gfx.convert) {
551 pixman_image_unref(vc->gfx.convert);
552 vc->gfx.convert = NULL;
555 if (vc->gfx.ds && surface &&
556 surface_width(vc->gfx.ds) == surface_width(surface) &&
557 surface_height(vc->gfx.ds) == surface_height(surface)) {
558 resized = false;
560 vc->gfx.ds = surface;
562 if (!surface) {
563 return;
566 if (surface->format == PIXMAN_x8r8g8b8) {
568 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
570 * No need to convert, use surface directly. Should be the
571 * common case as this is qemu_default_pixelformat(32) too.
573 vc->gfx.surface = cairo_image_surface_create_for_data
574 (surface_data(surface),
575 CAIRO_FORMAT_RGB24,
576 surface_width(surface),
577 surface_height(surface),
578 surface_stride(surface));
579 } else {
580 /* Must convert surface, use pixman to do it. */
581 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
582 surface_width(surface),
583 surface_height(surface),
584 NULL, 0);
585 vc->gfx.surface = cairo_image_surface_create_for_data
586 ((void *)pixman_image_get_data(vc->gfx.convert),
587 CAIRO_FORMAT_RGB24,
588 pixman_image_get_width(vc->gfx.convert),
589 pixman_image_get_height(vc->gfx.convert),
590 pixman_image_get_stride(vc->gfx.convert));
591 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
592 NULL, vc->gfx.convert,
593 0, 0, 0, 0, 0, 0,
594 pixman_image_get_width(vc->gfx.convert),
595 pixman_image_get_height(vc->gfx.convert));
598 if (resized) {
599 gd_update_windowsize(vc);
600 } else {
601 gd_update_full_redraw(vc);
605 static const DisplayChangeListenerOps dcl_ops = {
606 .dpy_name = "gtk",
607 .dpy_gfx_update = gd_update,
608 .dpy_gfx_switch = gd_switch,
609 .dpy_gfx_check_format = qemu_pixman_check_format,
610 .dpy_refresh = gd_refresh,
611 .dpy_mouse_set = gd_mouse_set,
612 .dpy_cursor_define = gd_cursor_define,
616 #if defined(CONFIG_OPENGL)
618 /** DisplayState Callbacks (opengl version) **/
620 #if defined(CONFIG_GTK_GL)
622 static const DisplayChangeListenerOps dcl_gl_area_ops = {
623 .dpy_name = "gtk-egl",
624 .dpy_gfx_update = gd_gl_area_update,
625 .dpy_gfx_switch = gd_gl_area_switch,
626 .dpy_gfx_check_format = console_gl_check_format,
627 .dpy_refresh = gd_gl_area_refresh,
628 .dpy_mouse_set = gd_mouse_set,
629 .dpy_cursor_define = gd_cursor_define,
631 .dpy_gl_ctx_create = gd_gl_area_create_context,
632 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
633 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
634 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
635 .dpy_gl_scanout = gd_gl_area_scanout,
636 .dpy_gl_update = gd_gl_area_scanout_flush,
639 #else
641 static const DisplayChangeListenerOps dcl_egl_ops = {
642 .dpy_name = "gtk-egl",
643 .dpy_gfx_update = gd_egl_update,
644 .dpy_gfx_switch = gd_egl_switch,
645 .dpy_gfx_check_format = console_gl_check_format,
646 .dpy_refresh = gd_egl_refresh,
647 .dpy_mouse_set = gd_mouse_set,
648 .dpy_cursor_define = gd_cursor_define,
650 .dpy_gl_ctx_create = gd_egl_create_context,
651 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
652 .dpy_gl_ctx_make_current = gd_egl_make_current,
653 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
654 .dpy_gl_scanout = gd_egl_scanout,
655 .dpy_gl_update = gd_egl_scanout_flush,
658 #endif /* CONFIG_GTK_GL */
659 #endif /* CONFIG_OPENGL */
661 /** QEMU Events **/
663 static void gd_change_runstate(void *opaque, int running, RunState state)
665 GtkDisplayState *s = opaque;
667 gd_update_caption(s);
670 static void gd_mouse_mode_change(Notifier *notify, void *data)
672 GtkDisplayState *s;
673 int i;
675 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
676 /* release the grab at switching to absolute mode */
677 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
678 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
679 FALSE);
681 for (i = 0; i < s->nb_vcs; i++) {
682 VirtualConsole *vc = &s->vc[i];
683 gd_update_cursor(vc);
687 /** GTK Events **/
689 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
690 void *opaque)
692 GtkDisplayState *s = opaque;
693 int i;
695 if (!no_quit) {
696 for (i = 0; i < s->nb_vcs; i++) {
697 if (s->vc[i].type != GD_VC_GFX) {
698 continue;
700 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
702 qmp_quit(NULL);
703 return FALSE;
706 return TRUE;
709 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
711 QemuUIInfo info;
713 memset(&info, 0, sizeof(info));
714 info.width = width;
715 info.height = height;
716 dpy_set_ui_info(vc->gfx.dcl.con, &info);
719 #if defined(CONFIG_GTK_GL)
721 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
722 void *opaque)
724 VirtualConsole *vc = opaque;
726 if (vc->gfx.gls) {
727 gd_gl_area_draw(vc);
729 return TRUE;
732 static void gd_resize_event(GtkGLArea *area,
733 gint width, gint height, gpointer *opaque)
735 VirtualConsole *vc = (void *)opaque;
737 gd_set_ui_info(vc, width, height);
740 #endif
742 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
744 VirtualConsole *vc = opaque;
745 GtkDisplayState *s = vc->s;
746 int mx, my;
747 int ww, wh;
748 int fbw, fbh;
750 #if defined(CONFIG_OPENGL)
751 if (vc->gfx.gls) {
752 #if defined(CONFIG_GTK_GL)
753 /* invoke render callback please */
754 return FALSE;
755 #else
756 gd_egl_draw(vc);
757 return TRUE;
758 #endif
760 #endif
762 if (!gtk_widget_get_realized(widget)) {
763 return FALSE;
765 if (!vc->gfx.ds) {
766 return FALSE;
769 fbw = surface_width(vc->gfx.ds);
770 fbh = surface_height(vc->gfx.ds);
772 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
774 if (s->full_screen) {
775 vc->gfx.scale_x = (double)ww / fbw;
776 vc->gfx.scale_y = (double)wh / fbh;
777 } else if (s->free_scale) {
778 double sx, sy;
780 sx = (double)ww / fbw;
781 sy = (double)wh / fbh;
783 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
786 fbw *= vc->gfx.scale_x;
787 fbh *= vc->gfx.scale_y;
789 mx = my = 0;
790 if (ww > fbw) {
791 mx = (ww - fbw) / 2;
793 if (wh > fbh) {
794 my = (wh - fbh) / 2;
797 cairo_rectangle(cr, 0, 0, ww, wh);
799 /* Optionally cut out the inner area where the pixmap
800 will be drawn. This avoids 'flashing' since we're
801 not double-buffering. Note we're using the undocumented
802 behaviour of drawing the rectangle from right to left
803 to cut out the whole */
804 cairo_rectangle(cr, mx + fbw, my,
805 -1 * fbw, fbh);
806 cairo_fill(cr);
808 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
809 cairo_set_source_surface(cr, vc->gfx.surface,
810 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
811 cairo_paint(cr);
813 return TRUE;
816 #if !GTK_CHECK_VERSION(3, 0, 0)
817 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
818 void *opaque)
820 cairo_t *cr;
821 gboolean ret;
823 cr = gdk_cairo_create(gtk_widget_get_window(widget));
824 cairo_rectangle(cr,
825 expose->area.x,
826 expose->area.y,
827 expose->area.width,
828 expose->area.height);
829 cairo_clip(cr);
831 ret = gd_draw_event(widget, cr, opaque);
833 cairo_destroy(cr);
835 return ret;
837 #endif
839 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
840 void *opaque)
842 VirtualConsole *vc = opaque;
843 GtkDisplayState *s = vc->s;
844 int x, y;
845 int mx, my;
846 int fbh, fbw;
847 int ww, wh;
849 if (!vc->gfx.ds) {
850 return TRUE;
853 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
854 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
856 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
857 &ww, &wh);
859 mx = my = 0;
860 if (ww > fbw) {
861 mx = (ww - fbw) / 2;
863 if (wh > fbh) {
864 my = (wh - fbh) / 2;
867 x = (motion->x - mx) / vc->gfx.scale_x;
868 y = (motion->y - my) / vc->gfx.scale_y;
870 if (qemu_input_is_absolute()) {
871 if (x < 0 || y < 0 ||
872 x >= surface_width(vc->gfx.ds) ||
873 y >= surface_height(vc->gfx.ds)) {
874 return TRUE;
876 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
877 surface_width(vc->gfx.ds));
878 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
879 surface_height(vc->gfx.ds));
880 qemu_input_event_sync();
881 } else if (s->last_set && s->ptr_owner == vc) {
882 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
883 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
884 qemu_input_event_sync();
886 s->last_x = x;
887 s->last_y = y;
888 s->last_set = TRUE;
890 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
891 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
892 int x = (int)motion->x_root;
893 int y = (int)motion->y_root;
895 /* In relative mode check to see if client pointer hit
896 * one of the screen edges, and if so move it back by
897 * 200 pixels. This is important because the pointer
898 * in the server doesn't correspond 1-for-1, and so
899 * may still be only half way across the screen. Without
900 * this warp, the server pointer would thus appear to hit
901 * an invisible wall */
902 if (x == 0) {
903 x += 200;
905 if (y == 0) {
906 y += 200;
908 if (x == (gdk_screen_get_width(screen) - 1)) {
909 x -= 200;
911 if (y == (gdk_screen_get_height(screen) - 1)) {
912 y -= 200;
915 if (x != (int)motion->x_root || y != (int)motion->y_root) {
916 #if GTK_CHECK_VERSION(3, 0, 0)
917 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
918 gdk_device_warp(dev, screen, x, y);
919 #else
920 GdkDisplay *display = gtk_widget_get_display(widget);
921 gdk_display_warp_pointer(display, screen, x, y);
922 #endif
923 s->last_set = FALSE;
924 return FALSE;
927 return TRUE;
930 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
931 void *opaque)
933 VirtualConsole *vc = opaque;
934 GtkDisplayState *s = vc->s;
935 InputButton btn;
937 /* implicitly grab the input at the first click in the relative mode */
938 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
939 !qemu_input_is_absolute() && s->ptr_owner != vc) {
940 if (!vc->window) {
941 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
942 TRUE);
943 } else {
944 gd_grab_pointer(vc, "relative-mode-click");
946 return TRUE;
949 if (button->button == 1) {
950 btn = INPUT_BUTTON_LEFT;
951 } else if (button->button == 2) {
952 btn = INPUT_BUTTON_MIDDLE;
953 } else if (button->button == 3) {
954 btn = INPUT_BUTTON_RIGHT;
955 } else {
956 return TRUE;
959 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
960 button->type == GDK_BUTTON_PRESS);
961 qemu_input_event_sync();
962 return TRUE;
965 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
966 void *opaque)
968 VirtualConsole *vc = opaque;
969 InputButton btn;
971 if (scroll->direction == GDK_SCROLL_UP) {
972 btn = INPUT_BUTTON_WHEEL_UP;
973 } else if (scroll->direction == GDK_SCROLL_DOWN) {
974 btn = INPUT_BUTTON_WHEEL_DOWN;
975 } else {
976 return TRUE;
979 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
980 qemu_input_event_sync();
981 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
982 qemu_input_event_sync();
983 return TRUE;
986 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
988 int qemu_keycode;
990 #ifdef GDK_WINDOWING_WIN32
991 if (GDK_IS_WIN32_DISPLAY(dpy)) {
992 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
993 switch (qemu_keycode) {
994 case 103: /* alt gr */
995 qemu_keycode = 56 | SCANCODE_GREY;
996 break;
998 return qemu_keycode;
1000 #endif
1002 if (gdk_keycode < 9) {
1003 qemu_keycode = 0;
1004 } else if (gdk_keycode < 97) {
1005 qemu_keycode = gdk_keycode - 8;
1006 #ifdef GDK_WINDOWING_X11
1007 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
1008 if (s->has_evdev) {
1009 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1010 } else {
1011 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
1013 #endif
1014 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
1015 qemu_keycode = 0x70;
1016 } else if (gdk_keycode == 211) { /* backslash */
1017 qemu_keycode = 0x73;
1018 } else {
1019 qemu_keycode = 0;
1022 return qemu_keycode;
1025 static gboolean gd_text_key_down(GtkWidget *widget,
1026 GdkEventKey *key, void *opaque)
1028 VirtualConsole *vc = opaque;
1029 QemuConsole *con = vc->gfx.dcl.con;
1031 if (key->length) {
1032 kbd_put_string_console(con, key->string, key->length);
1033 } else {
1034 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
1035 key->hardware_keycode);
1036 int qcode = qemu_input_key_number_to_qcode(num);
1037 kbd_put_qcode_console(con, qcode);
1039 return TRUE;
1042 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1044 VirtualConsole *vc = opaque;
1045 GtkDisplayState *s = vc->s;
1046 int gdk_keycode = key->hardware_keycode;
1047 int qemu_keycode;
1048 int i;
1050 if (s->ignore_keys) {
1051 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1052 return TRUE;
1055 if (key->keyval == GDK_KEY_Pause) {
1056 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1057 key->type == GDK_KEY_PRESS);
1058 return TRUE;
1061 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
1062 gdk_keycode);
1064 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
1065 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1067 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1068 if (qemu_keycode == modifier_keycode[i]) {
1069 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1073 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
1074 key->type == GDK_KEY_PRESS);
1076 return TRUE;
1079 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1081 if (event->type == GDK_MOTION_NOTIFY) {
1082 return gd_motion_event(widget, &event->motion, opaque);
1084 return FALSE;
1087 /** Window Menu Actions **/
1089 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1091 GtkDisplayState *s = opaque;
1093 if (s->external_pause_update) {
1094 return;
1096 if (runstate_is_running()) {
1097 qmp_stop(NULL);
1098 } else {
1099 qmp_cont(NULL);
1103 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1105 qmp_system_reset(NULL);
1108 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1110 qmp_system_powerdown(NULL);
1113 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1115 qmp_quit(NULL);
1118 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1120 GtkDisplayState *s = opaque;
1121 VirtualConsole *vc = gd_vc_find_by_menu(s);
1122 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1123 gint page;
1125 gtk_release_modifiers(s);
1126 if (vc) {
1127 page = gtk_notebook_page_num(nb, vc->tab_item);
1128 gtk_notebook_set_current_page(nb, page);
1129 gtk_widget_grab_focus(vc->focus);
1131 s->ignore_keys = false;
1134 static void gd_accel_switch_vc(void *opaque)
1136 VirtualConsole *vc = opaque;
1138 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1139 #if !GTK_CHECK_VERSION(3, 0, 0)
1140 /* GTK2 sends the accel key to the target console - ignore this until */
1141 vc->s->ignore_keys = true;
1142 #endif
1145 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1147 GtkDisplayState *s = opaque;
1148 VirtualConsole *vc = gd_vc_find_current(s);
1150 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1151 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1152 } else {
1153 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1155 gd_update_windowsize(vc);
1158 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1159 void *opaque)
1161 VirtualConsole *vc = opaque;
1162 GtkDisplayState *s = vc->s;
1164 gtk_widget_set_sensitive(vc->menu_item, true);
1165 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1166 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1167 vc->tab_item, vc->label);
1168 gtk_widget_destroy(vc->window);
1169 vc->window = NULL;
1170 return TRUE;
1173 static gboolean gd_win_grab(void *opaque)
1175 VirtualConsole *vc = opaque;
1177 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1178 if (vc->s->ptr_owner) {
1179 gd_ungrab_pointer(vc->s);
1180 } else {
1181 gd_grab_pointer(vc, "user-request-detached-tab");
1183 return TRUE;
1186 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1188 GtkDisplayState *s = opaque;
1189 VirtualConsole *vc = gd_vc_find_current(s);
1191 if (vc->type == GD_VC_GFX &&
1192 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1193 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1194 FALSE);
1196 if (!vc->window) {
1197 gtk_widget_set_sensitive(vc->menu_item, false);
1198 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1199 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1201 g_signal_connect(vc->window, "delete-event",
1202 G_CALLBACK(gd_tab_window_close), vc);
1203 gtk_widget_show_all(vc->window);
1205 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1206 GtkAccelGroup *ag = gtk_accel_group_new();
1207 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1209 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1210 vc, NULL);
1211 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1214 gd_update_geometry_hints(vc);
1215 gd_update_caption(s);
1219 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1221 GtkDisplayState *s = opaque;
1222 VirtualConsole *vc = gd_vc_find_current(s);
1224 if (!s->full_screen) {
1225 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1226 gtk_widget_hide(s->menu_bar);
1227 if (vc->type == GD_VC_GFX) {
1228 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1230 gtk_window_fullscreen(GTK_WINDOW(s->window));
1231 s->full_screen = TRUE;
1232 } else {
1233 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1234 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1235 gtk_widget_show(s->menu_bar);
1236 s->full_screen = FALSE;
1237 if (vc->type == GD_VC_GFX) {
1238 vc->gfx.scale_x = 1.0;
1239 vc->gfx.scale_y = 1.0;
1240 gd_update_windowsize(vc);
1244 gd_update_cursor(vc);
1247 static void gd_accel_full_screen(void *opaque)
1249 GtkDisplayState *s = opaque;
1250 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1253 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1255 GtkDisplayState *s = opaque;
1256 VirtualConsole *vc = gd_vc_find_current(s);
1258 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1259 FALSE);
1261 vc->gfx.scale_x += VC_SCALE_STEP;
1262 vc->gfx.scale_y += VC_SCALE_STEP;
1264 gd_update_windowsize(vc);
1267 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1269 GtkDisplayState *s = opaque;
1270 VirtualConsole *vc = gd_vc_find_current(s);
1272 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1273 FALSE);
1275 vc->gfx.scale_x -= VC_SCALE_STEP;
1276 vc->gfx.scale_y -= VC_SCALE_STEP;
1278 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1279 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1281 gd_update_windowsize(vc);
1284 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1286 GtkDisplayState *s = opaque;
1287 VirtualConsole *vc = gd_vc_find_current(s);
1289 vc->gfx.scale_x = 1.0;
1290 vc->gfx.scale_y = 1.0;
1292 gd_update_windowsize(vc);
1295 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1297 GtkDisplayState *s = opaque;
1298 VirtualConsole *vc = gd_vc_find_current(s);
1300 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1301 s->free_scale = TRUE;
1302 } else {
1303 s->free_scale = FALSE;
1304 vc->gfx.scale_x = 1.0;
1305 vc->gfx.scale_y = 1.0;
1308 gd_update_windowsize(vc);
1309 gd_update_full_redraw(vc);
1312 #if GTK_CHECK_VERSION(3, 0, 0)
1313 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1314 GdkInputSource source, GdkEventMask mask,
1315 GdkCursor *cursor)
1317 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1318 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1319 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1320 GList *tmp = devs;
1322 for (tmp = devs; tmp; tmp = tmp->next) {
1323 GdkDevice *dev = tmp->data;
1324 if (gdk_device_get_source(dev) != source) {
1325 continue;
1327 if (grab) {
1328 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1329 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1330 mask, cursor, GDK_CURRENT_TIME);
1331 } else {
1332 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1335 g_list_free(devs);
1337 #endif
1339 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1341 if (vc->s->kbd_owner) {
1342 if (vc->s->kbd_owner == vc) {
1343 return;
1344 } else {
1345 gd_ungrab_keyboard(vc->s);
1349 #if GTK_CHECK_VERSION(3, 0, 0)
1350 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1351 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1352 NULL);
1353 #else
1354 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1355 FALSE,
1356 GDK_CURRENT_TIME);
1357 #endif
1358 vc->s->kbd_owner = vc;
1359 gd_update_caption(vc->s);
1360 trace_gd_grab(vc->label, "kbd", reason);
1363 static void gd_ungrab_keyboard(GtkDisplayState *s)
1365 VirtualConsole *vc = s->kbd_owner;
1367 if (vc == NULL) {
1368 return;
1370 s->kbd_owner = NULL;
1372 #if GTK_CHECK_VERSION(3, 0, 0)
1373 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1374 #else
1375 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1376 #endif
1377 gd_update_caption(s);
1378 trace_gd_ungrab(vc->label, "kbd");
1381 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1383 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1385 if (vc->s->ptr_owner) {
1386 if (vc->s->ptr_owner == vc) {
1387 return;
1388 } else {
1389 gd_ungrab_pointer(vc->s);
1393 #if GTK_CHECK_VERSION(3, 0, 0)
1394 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1395 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1396 GDK_POINTER_MOTION_MASK |
1397 GDK_BUTTON_PRESS_MASK |
1398 GDK_BUTTON_RELEASE_MASK |
1399 GDK_BUTTON_MOTION_MASK |
1400 GDK_SCROLL_MASK,
1401 vc->s->null_cursor);
1402 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1403 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1404 #else
1405 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1406 FALSE, /* All events to come to our window directly */
1407 GDK_POINTER_MOTION_MASK |
1408 GDK_BUTTON_PRESS_MASK |
1409 GDK_BUTTON_RELEASE_MASK |
1410 GDK_BUTTON_MOTION_MASK |
1411 GDK_SCROLL_MASK,
1412 NULL, /* Allow cursor to move over entire desktop */
1413 vc->s->null_cursor,
1414 GDK_CURRENT_TIME);
1415 gdk_display_get_pointer(display, NULL,
1416 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1417 #endif
1418 vc->s->ptr_owner = vc;
1419 gd_update_caption(vc->s);
1420 trace_gd_grab(vc->label, "ptr", reason);
1423 static void gd_ungrab_pointer(GtkDisplayState *s)
1425 VirtualConsole *vc = s->ptr_owner;
1427 if (vc == NULL) {
1428 return;
1430 s->ptr_owner = NULL;
1432 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1433 #if GTK_CHECK_VERSION(3, 0, 0)
1434 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1435 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1436 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1437 gtk_widget_get_screen(vc->gfx.drawing_area),
1438 vc->s->grab_x_root, vc->s->grab_y_root);
1439 #else
1440 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1441 gdk_display_warp_pointer(display,
1442 gtk_widget_get_screen(vc->gfx.drawing_area),
1443 vc->s->grab_x_root, vc->s->grab_y_root);
1444 #endif
1445 gd_update_caption(s);
1446 trace_gd_ungrab(vc->label, "ptr");
1449 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1451 GtkDisplayState *s = opaque;
1452 VirtualConsole *vc = gd_vc_find_current(s);
1454 if (gd_is_grab_active(s)) {
1455 gd_grab_keyboard(vc, "user-request-main-window");
1456 gd_grab_pointer(vc, "user-request-main-window");
1457 } else {
1458 gd_ungrab_keyboard(s);
1459 gd_ungrab_pointer(s);
1462 gd_update_cursor(vc);
1465 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1466 gpointer data)
1468 GtkDisplayState *s = data;
1469 VirtualConsole *vc;
1470 gboolean on_vga;
1472 if (!gtk_widget_get_realized(s->notebook)) {
1473 return;
1476 #ifdef VTE_RESIZE_HACK
1477 vc = gd_vc_find_current(s);
1478 if (vc && vc->type == GD_VC_VTE) {
1479 gtk_widget_hide(vc->vte.terminal);
1481 #endif
1482 vc = gd_vc_find_by_page(s, arg2);
1483 if (!vc) {
1484 return;
1486 #ifdef VTE_RESIZE_HACK
1487 if (vc->type == GD_VC_VTE) {
1488 gtk_widget_show(vc->vte.terminal);
1490 #endif
1491 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1492 TRUE);
1493 on_vga = (vc->type == GD_VC_GFX &&
1494 qemu_console_is_graphic(vc->gfx.dcl.con));
1495 if (!on_vga) {
1496 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1497 FALSE);
1498 } else if (s->full_screen) {
1499 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1500 TRUE);
1502 gtk_widget_set_sensitive(s->grab_item, on_vga);
1504 gd_update_windowsize(vc);
1505 gd_update_cursor(vc);
1508 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1509 gpointer opaque)
1511 VirtualConsole *vc = opaque;
1512 GtkDisplayState *s = vc->s;
1514 if (gd_grab_on_hover(s)) {
1515 gd_grab_keyboard(vc, "grab-on-hover");
1517 return TRUE;
1520 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1521 gpointer opaque)
1523 VirtualConsole *vc = opaque;
1524 GtkDisplayState *s = vc->s;
1526 if (gd_grab_on_hover(s)) {
1527 gd_ungrab_keyboard(s);
1529 return TRUE;
1532 static gboolean gd_focus_out_event(GtkWidget *widget,
1533 GdkEventCrossing *crossing, gpointer opaque)
1535 VirtualConsole *vc = opaque;
1536 GtkDisplayState *s = vc->s;
1538 gtk_release_modifiers(s);
1539 return TRUE;
1542 static gboolean gd_configure(GtkWidget *widget,
1543 GdkEventConfigure *cfg, gpointer opaque)
1545 VirtualConsole *vc = opaque;
1547 gd_set_ui_info(vc, cfg->width, cfg->height);
1548 return FALSE;
1551 /** Virtual Console Callbacks **/
1553 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1554 int idx, GSList *group, GtkWidget *view_menu)
1556 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1557 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1558 HOTKEY_MODIFIERS, 0,
1559 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1560 #if GTK_CHECK_VERSION(3, 8, 0)
1561 gtk_accel_label_set_accel(
1562 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1563 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1564 #endif
1566 g_signal_connect(vc->menu_item, "activate",
1567 G_CALLBACK(gd_menu_switch_vc), s);
1568 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1570 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1571 return group;
1574 #if defined(CONFIG_VTE)
1575 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1577 VirtualConsole *vc = opaque;
1579 if (gtk_adjustment_get_upper(adjustment) >
1580 gtk_adjustment_get_page_size(adjustment)) {
1581 gtk_widget_show(vc->vte.scrollbar);
1582 } else {
1583 gtk_widget_hide(vc->vte.scrollbar);
1587 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1589 VirtualConsole *vc = chr->opaque;
1591 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1592 return len;
1595 static void gd_vc_chr_set_echo(CharDriverState *chr, bool echo)
1597 VirtualConsole *vc = chr->opaque;
1599 vc->vte.echo = echo;
1602 static int nb_vcs;
1603 static CharDriverState *vcs[MAX_VCS];
1605 static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
1607 ChardevCommon *common = qapi_ChardevVC_base(vc);
1608 CharDriverState *chr;
1610 chr = qemu_chr_alloc(common, errp);
1611 if (!chr) {
1612 return NULL;
1615 chr->chr_write = gd_vc_chr_write;
1616 chr->chr_set_echo = gd_vc_chr_set_echo;
1618 /* Temporary, until gd_vc_vte_init runs. */
1619 chr->opaque = g_new0(VirtualConsole, 1);
1621 /* defer OPENED events until our vc is fully initialized */
1622 chr->explicit_be_open = true;
1624 vcs[nb_vcs++] = chr;
1626 return chr;
1629 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1630 gpointer user_data)
1632 VirtualConsole *vc = user_data;
1634 if (vc->vte.echo) {
1635 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1636 int i;
1637 for (i = 0; i < size; i++) {
1638 uint8_t c = text[i];
1639 if (c >= 128 || isprint(c)) {
1640 /* 8-bit characters are considered printable. */
1641 vte_terminal_feed(term, &text[i], 1);
1642 } else if (c == '\r' || c == '\n') {
1643 vte_terminal_feed(term, "\r\n", 2);
1644 } else {
1645 char ctrl[2] = { '^', 0};
1646 ctrl[1] = text[i] ^ 64;
1647 vte_terminal_feed(term, ctrl, 2);
1652 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1653 return TRUE;
1656 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1657 CharDriverState *chr, int idx,
1658 GSList *group, GtkWidget *view_menu)
1660 char buffer[32];
1661 GtkWidget *box;
1662 GtkWidget *scrollbar;
1663 GtkAdjustment *vadjustment;
1664 VirtualConsole *tmp_vc = chr->opaque;
1666 vc->s = s;
1667 vc->vte.echo = tmp_vc->vte.echo;
1669 vc->vte.chr = chr;
1670 chr->opaque = vc;
1671 g_free(tmp_vc);
1673 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1674 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1675 ? vc->vte.chr->label : buffer);
1676 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1678 vc->vte.terminal = vte_terminal_new();
1679 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1681 /* The documentation says that the default is UTF-8, but actually it is
1682 * 7-bit ASCII at least in VTE 0.38.
1684 #if VTE_CHECK_VERSION(0, 40, 0)
1685 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1686 #else
1687 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1688 #endif
1690 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1691 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1692 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1694 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1695 vadjustment = gtk_scrollable_get_vadjustment
1696 (GTK_SCROLLABLE(vc->vte.terminal));
1697 #else
1698 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1699 #endif
1701 #if GTK_CHECK_VERSION(3, 0, 0)
1702 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1703 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1704 #else
1705 box = gtk_hbox_new(false, 2);
1706 scrollbar = gtk_vscrollbar_new(vadjustment);
1707 #endif
1709 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1710 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1712 vc->vte.box = box;
1713 vc->vte.scrollbar = scrollbar;
1715 g_signal_connect(vadjustment, "changed",
1716 G_CALLBACK(gd_vc_adjustment_changed), vc);
1718 vc->type = GD_VC_VTE;
1719 vc->tab_item = box;
1720 vc->focus = vc->vte.terminal;
1721 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1722 gtk_label_new(vc->label));
1724 qemu_chr_be_generic_open(vc->vte.chr);
1725 if (vc->vte.chr->init) {
1726 vc->vte.chr->init(vc->vte.chr);
1729 return group;
1732 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1733 GtkWidget *view_menu)
1735 int i;
1737 for (i = 0; i < nb_vcs; i++) {
1738 VirtualConsole *vc = &s->vc[s->nb_vcs];
1739 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1740 s->nb_vcs++;
1743 #endif /* CONFIG_VTE */
1745 /** Window Creation **/
1747 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1749 #if GTK_CHECK_VERSION(3, 0, 0)
1750 g_signal_connect(vc->gfx.drawing_area, "draw",
1751 G_CALLBACK(gd_draw_event), vc);
1752 #if defined(CONFIG_GTK_GL)
1753 if (display_opengl) {
1754 /* wire up GtkGlArea events */
1755 g_signal_connect(vc->gfx.drawing_area, "render",
1756 G_CALLBACK(gd_render_event), vc);
1757 g_signal_connect(vc->gfx.drawing_area, "resize",
1758 G_CALLBACK(gd_resize_event), vc);
1760 #endif
1761 #else
1762 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1763 G_CALLBACK(gd_expose_event), vc);
1764 #endif
1765 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1766 g_signal_connect(vc->gfx.drawing_area, "event",
1767 G_CALLBACK(gd_event), vc);
1768 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1769 G_CALLBACK(gd_button_event), vc);
1770 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1771 G_CALLBACK(gd_button_event), vc);
1772 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1773 G_CALLBACK(gd_scroll_event), vc);
1774 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1775 G_CALLBACK(gd_key_event), vc);
1776 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1777 G_CALLBACK(gd_key_event), vc);
1779 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1780 G_CALLBACK(gd_enter_event), vc);
1781 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1782 G_CALLBACK(gd_leave_event), vc);
1783 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1784 G_CALLBACK(gd_focus_out_event), vc);
1785 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1786 G_CALLBACK(gd_configure), vc);
1787 } else {
1788 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1789 G_CALLBACK(gd_text_key_down), vc);
1793 static void gd_connect_signals(GtkDisplayState *s)
1795 g_signal_connect(s->show_tabs_item, "activate",
1796 G_CALLBACK(gd_menu_show_tabs), s);
1797 g_signal_connect(s->untabify_item, "activate",
1798 G_CALLBACK(gd_menu_untabify), s);
1800 g_signal_connect(s->window, "delete-event",
1801 G_CALLBACK(gd_window_close), s);
1803 g_signal_connect(s->pause_item, "activate",
1804 G_CALLBACK(gd_menu_pause), s);
1805 g_signal_connect(s->reset_item, "activate",
1806 G_CALLBACK(gd_menu_reset), s);
1807 g_signal_connect(s->powerdown_item, "activate",
1808 G_CALLBACK(gd_menu_powerdown), s);
1809 g_signal_connect(s->quit_item, "activate",
1810 G_CALLBACK(gd_menu_quit), s);
1811 g_signal_connect(s->full_screen_item, "activate",
1812 G_CALLBACK(gd_menu_full_screen), s);
1813 g_signal_connect(s->zoom_in_item, "activate",
1814 G_CALLBACK(gd_menu_zoom_in), s);
1815 g_signal_connect(s->zoom_out_item, "activate",
1816 G_CALLBACK(gd_menu_zoom_out), s);
1817 g_signal_connect(s->zoom_fixed_item, "activate",
1818 G_CALLBACK(gd_menu_zoom_fixed), s);
1819 g_signal_connect(s->zoom_fit_item, "activate",
1820 G_CALLBACK(gd_menu_zoom_fit), s);
1821 g_signal_connect(s->grab_item, "activate",
1822 G_CALLBACK(gd_menu_grab_input), s);
1823 g_signal_connect(s->notebook, "switch-page",
1824 G_CALLBACK(gd_change_page), s);
1827 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1829 GtkWidget *machine_menu;
1830 GtkWidget *separator;
1832 machine_menu = gtk_menu_new();
1833 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1835 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1836 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1838 separator = gtk_separator_menu_item_new();
1839 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1841 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1842 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1844 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1845 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1847 separator = gtk_separator_menu_item_new();
1848 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1850 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1851 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1852 "<QEMU>/Machine/Quit");
1853 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1854 GDK_KEY_q, HOTKEY_MODIFIERS);
1855 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1857 return machine_menu;
1860 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1861 QemuConsole *con, int idx,
1862 GSList *group, GtkWidget *view_menu)
1864 vc->label = qemu_console_get_label(con);
1865 vc->s = s;
1866 vc->gfx.scale_x = 1.0;
1867 vc->gfx.scale_y = 1.0;
1869 #if defined(CONFIG_OPENGL)
1870 if (display_opengl) {
1871 #if defined(CONFIG_GTK_GL)
1872 vc->gfx.drawing_area = gtk_gl_area_new();
1873 vc->gfx.dcl.ops = &dcl_gl_area_ops;
1874 #else
1875 vc->gfx.drawing_area = gtk_drawing_area_new();
1877 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1878 * It is required for opengl rendering on X11 though. A
1879 * proper replacement (native opengl support) is only
1880 * available in 3.16+. Silence the warning if possible.
1882 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1883 #pragma GCC diagnostic push
1884 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1885 #endif
1886 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1887 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1888 #pragma GCC diagnostic pop
1889 #endif
1890 vc->gfx.dcl.ops = &dcl_egl_ops;
1891 #endif /* CONFIG_GTK_GL */
1892 } else
1893 #endif
1895 vc->gfx.drawing_area = gtk_drawing_area_new();
1896 vc->gfx.dcl.ops = &dcl_ops;
1900 gtk_widget_add_events(vc->gfx.drawing_area,
1901 GDK_POINTER_MOTION_MASK |
1902 GDK_BUTTON_PRESS_MASK |
1903 GDK_BUTTON_RELEASE_MASK |
1904 GDK_BUTTON_MOTION_MASK |
1905 GDK_ENTER_NOTIFY_MASK |
1906 GDK_LEAVE_NOTIFY_MASK |
1907 GDK_SCROLL_MASK |
1908 GDK_KEY_PRESS_MASK);
1909 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1911 vc->type = GD_VC_GFX;
1912 vc->tab_item = vc->gfx.drawing_area;
1913 vc->focus = vc->gfx.drawing_area;
1914 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
1915 vc->tab_item, gtk_label_new(vc->label));
1917 vc->gfx.dcl.con = con;
1918 register_displaychangelistener(&vc->gfx.dcl);
1920 gd_connect_vc_gfx_signals(vc);
1921 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1923 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
1924 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
1925 s->free_scale = true;
1928 return group;
1931 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
1933 GSList *group = NULL;
1934 GtkWidget *view_menu;
1935 GtkWidget *separator;
1936 QemuConsole *con;
1937 int vc;
1939 view_menu = gtk_menu_new();
1940 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
1942 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1944 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
1945 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
1946 #if GTK_CHECK_VERSION(3, 8, 0)
1947 gtk_accel_label_set_accel(
1948 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
1949 GDK_KEY_f, HOTKEY_MODIFIERS);
1950 #endif
1951 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1953 separator = gtk_separator_menu_item_new();
1954 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1956 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1957 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1958 "<QEMU>/View/Zoom In");
1959 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1960 HOTKEY_MODIFIERS);
1961 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1963 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1964 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1965 "<QEMU>/View/Zoom Out");
1966 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1967 HOTKEY_MODIFIERS);
1968 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1970 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1971 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1972 "<QEMU>/View/Zoom Fixed");
1973 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1974 HOTKEY_MODIFIERS);
1975 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1977 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1978 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1980 separator = gtk_separator_menu_item_new();
1981 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1983 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1984 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1986 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1987 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1988 "<QEMU>/View/Grab Input");
1989 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1990 HOTKEY_MODIFIERS);
1991 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1993 separator = gtk_separator_menu_item_new();
1994 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1996 /* gfx */
1997 for (vc = 0;; vc++) {
1998 con = qemu_console_lookup_by_index(vc);
1999 if (!con) {
2000 break;
2002 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2003 vc, group, view_menu);
2004 s->nb_vcs++;
2007 #if defined(CONFIG_VTE)
2008 /* vte */
2009 gd_vcs_init(s, group, view_menu);
2010 #endif
2012 separator = gtk_separator_menu_item_new();
2013 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2015 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2016 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2018 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2019 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2021 return view_menu;
2024 static void gd_create_menus(GtkDisplayState *s)
2026 s->accel_group = gtk_accel_group_new();
2027 s->machine_menu = gd_create_menu_machine(s);
2028 s->view_menu = gd_create_menu_view(s);
2030 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2031 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2032 s->machine_menu);
2033 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2035 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2036 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2037 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2039 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2040 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2043 static void gd_set_keycode_type(GtkDisplayState *s)
2045 #ifdef GDK_WINDOWING_X11
2046 GdkDisplay *display = gtk_widget_get_display(s->window);
2047 if (GDK_IS_X11_DISPLAY(display)) {
2048 Display *x11_display = gdk_x11_display_get_xdisplay(display);
2049 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
2050 XkbUseCoreKbd);
2051 char *keycodes = NULL;
2053 if (desc && desc->names) {
2054 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
2056 if (keycodes == NULL) {
2057 fprintf(stderr, "could not lookup keycode name\n");
2058 } else if (strstart(keycodes, "evdev", NULL)) {
2059 s->has_evdev = true;
2060 } else if (!strstart(keycodes, "xfree86", NULL)) {
2061 fprintf(stderr, "unknown keycodes `%s', please report to "
2062 "qemu-devel@nongnu.org\n", keycodes);
2065 if (desc) {
2066 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
2068 if (keycodes) {
2069 XFree(keycodes);
2072 #endif
2075 static gboolean gtkinit;
2077 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
2079 GtkDisplayState *s = g_malloc0(sizeof(*s));
2080 char *filename;
2081 GdkDisplay *window_display;
2083 if (!gtkinit) {
2084 fprintf(stderr, "gtk initialization failed\n");
2085 exit(1);
2088 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2089 #if GTK_CHECK_VERSION(3, 2, 0)
2090 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2091 #else
2092 s->vbox = gtk_vbox_new(FALSE, 0);
2093 #endif
2094 s->notebook = gtk_notebook_new();
2095 s->menu_bar = gtk_menu_bar_new();
2097 s->free_scale = FALSE;
2099 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2100 setlocale(LC_MESSAGES, "");
2101 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2102 textdomain("qemu");
2104 window_display = gtk_widget_get_display(s->window);
2105 s->null_cursor = gdk_cursor_new_for_display(window_display,
2106 GDK_BLANK_CURSOR);
2108 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2109 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2110 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2112 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2113 if (filename) {
2114 GError *error = NULL;
2115 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2116 if (pixbuf) {
2117 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2118 } else {
2119 g_error_free(error);
2121 g_free(filename);
2124 gd_create_menus(s);
2126 gd_connect_signals(s);
2128 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2129 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2131 gd_update_caption(s);
2133 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2134 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2136 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2138 gtk_widget_show_all(s->window);
2140 #ifdef VTE_RESIZE_HACK
2142 VirtualConsole *cur = gd_vc_find_current(s);
2143 if (cur) {
2144 int i;
2146 for (i = 0; i < s->nb_vcs; i++) {
2147 VirtualConsole *vc = &s->vc[i];
2148 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2149 gtk_widget_hide(vc->vte.terminal);
2152 gd_update_windowsize(cur);
2155 #endif
2157 if (full_screen) {
2158 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2160 if (grab_on_hover) {
2161 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2164 gd_set_keycode_type(s);
2167 void early_gtk_display_init(int opengl)
2169 /* The QEMU code relies on the assumption that it's always run in
2170 * the C locale. Therefore it is not prepared to deal with
2171 * operations that produce different results depending on the
2172 * locale, such as printf's formatting of decimal numbers, and
2173 * possibly others.
2175 * Since GTK+ calls setlocale() by default -importing the locale
2176 * settings from the environment- we must prevent it from doing so
2177 * using gtk_disable_setlocale().
2179 * QEMU's GTK+ UI, however, _does_ have translations for some of
2180 * the menu items. As a trade-off between a functionally correct
2181 * QEMU and a fully internationalized UI we support importing
2182 * LC_MESSAGES from the environment (see the setlocale() call
2183 * earlier in this file). This allows us to display translated
2184 * messages leaving everything else untouched.
2186 gtk_disable_setlocale();
2187 gtkinit = gtk_init_check(NULL, NULL);
2188 if (!gtkinit) {
2189 /* don't exit yet, that'll break -help */
2190 return;
2193 switch (opengl) {
2194 case -1: /* default */
2195 case 0: /* off */
2196 break;
2197 case 1: /* on */
2198 #if defined(CONFIG_OPENGL)
2199 #if defined(CONFIG_GTK_GL)
2200 gtk_gl_area_init();
2201 #else
2202 gtk_egl_init();
2203 #endif
2204 #endif
2205 break;
2206 default:
2207 g_assert_not_reached();
2208 break;
2211 #if defined(CONFIG_VTE)
2212 register_vc_handler(gd_vc_handler);
2213 #endif