ppc: Remove some stub POWER6 models
[qemu.git] / ui / gtk.c
blobe81642876a9b44e3241885336900052df9a5f135
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 #if !GTK_CHECK_VERSION(2, 22, 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 #define GDK_KEY_Delete GDK_Delete
108 #endif
110 /* Some older mingw versions lack this constant or have
111 * it conditionally defined */
112 #ifdef _WIN32
113 # ifndef MAPVK_VK_TO_VSC
114 # define MAPVK_VK_TO_VSC 0
115 # endif
116 #endif
119 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
121 static const int modifier_keycode[] = {
122 /* shift, control, alt keys, meta keys, both left & right */
123 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
126 struct GtkDisplayState {
127 GtkWidget *window;
129 GtkWidget *menu_bar;
131 GtkAccelGroup *accel_group;
133 GtkWidget *machine_menu_item;
134 GtkWidget *machine_menu;
135 GtkWidget *pause_item;
136 GtkWidget *reset_item;
137 GtkWidget *powerdown_item;
138 GtkWidget *quit_item;
140 GtkWidget *view_menu_item;
141 GtkWidget *view_menu;
142 GtkWidget *full_screen_item;
143 GtkWidget *copy_item;
144 GtkWidget *zoom_in_item;
145 GtkWidget *zoom_out_item;
146 GtkWidget *zoom_fixed_item;
147 GtkWidget *zoom_fit_item;
148 GtkWidget *grab_item;
149 GtkWidget *grab_on_hover_item;
151 int nb_vcs;
152 VirtualConsole vc[MAX_VCS];
154 GtkWidget *show_tabs_item;
155 GtkWidget *untabify_item;
157 GtkWidget *vbox;
158 GtkWidget *notebook;
159 int button_mask;
160 gboolean last_set;
161 int last_x;
162 int last_y;
163 int grab_x_root;
164 int grab_y_root;
165 VirtualConsole *kbd_owner;
166 VirtualConsole *ptr_owner;
168 gboolean full_screen;
170 GdkCursor *null_cursor;
171 Notifier mouse_mode_notifier;
172 gboolean free_scale;
174 bool external_pause_update;
176 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
177 bool has_evdev;
178 bool ignore_keys;
181 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
182 static void gd_ungrab_pointer(GtkDisplayState *s);
183 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
184 static void gd_ungrab_keyboard(GtkDisplayState *s);
186 /** Utility Functions **/
188 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
190 VirtualConsole *vc;
191 gint i;
193 for (i = 0; i < s->nb_vcs; i++) {
194 vc = &s->vc[i];
195 if (gtk_check_menu_item_get_active
196 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
197 return vc;
200 return NULL;
203 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
205 VirtualConsole *vc;
206 gint i, p;
208 for (i = 0; i < s->nb_vcs; i++) {
209 vc = &s->vc[i];
210 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
211 if (p == page) {
212 return vc;
215 return NULL;
218 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
220 gint page;
222 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
223 return gd_vc_find_by_page(s, page);
226 static bool gd_is_grab_active(GtkDisplayState *s)
228 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
231 static bool gd_grab_on_hover(GtkDisplayState *s)
233 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
236 static void gd_update_cursor(VirtualConsole *vc)
238 GtkDisplayState *s = vc->s;
239 GdkWindow *window;
241 if (vc->type != GD_VC_GFX ||
242 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
243 return;
246 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
247 return;
250 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
251 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
252 gdk_window_set_cursor(window, s->null_cursor);
253 } else {
254 gdk_window_set_cursor(window, NULL);
258 static void gd_update_caption(GtkDisplayState *s)
260 const char *status = "";
261 gchar *prefix;
262 gchar *title;
263 const char *grab = "";
264 bool is_paused = !runstate_is_running();
265 int i;
267 if (qemu_name) {
268 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
269 } else {
270 prefix = g_strdup_printf("QEMU");
273 if (s->ptr_owner != NULL &&
274 s->ptr_owner->window == NULL) {
275 grab = _(" - Press Ctrl+Alt+G to release grab");
278 if (is_paused) {
279 status = _(" [Paused]");
281 s->external_pause_update = true;
282 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
283 is_paused);
284 s->external_pause_update = false;
286 title = g_strdup_printf("%s%s%s", prefix, status, grab);
287 gtk_window_set_title(GTK_WINDOW(s->window), title);
288 g_free(title);
290 for (i = 0; i < s->nb_vcs; i++) {
291 VirtualConsole *vc = &s->vc[i];
293 if (!vc->window) {
294 continue;
296 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
297 vc == s->kbd_owner ? " +kbd" : "",
298 vc == s->ptr_owner ? " +ptr" : "");
299 gtk_window_set_title(GTK_WINDOW(vc->window), title);
300 g_free(title);
303 g_free(prefix);
306 static void gd_update_geometry_hints(VirtualConsole *vc)
308 GtkDisplayState *s = vc->s;
309 GdkWindowHints mask = 0;
310 GdkGeometry geo = {};
311 GtkWidget *geo_widget = NULL;
312 GtkWindow *geo_window;
314 if (vc->type == GD_VC_GFX) {
315 if (!vc->gfx.ds) {
316 return;
318 if (s->free_scale) {
319 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
320 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
321 mask |= GDK_HINT_MIN_SIZE;
322 } else {
323 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
324 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
325 mask |= GDK_HINT_MIN_SIZE;
327 geo_widget = vc->gfx.drawing_area;
328 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
330 #if defined(CONFIG_VTE)
331 } else if (vc->type == GD_VC_VTE) {
332 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
333 GtkBorder padding = { 0 };
335 #if VTE_CHECK_VERSION(0, 37, 0)
336 gtk_style_context_get_padding(
337 gtk_widget_get_style_context(vc->vte.terminal),
338 gtk_widget_get_state_flags(vc->vte.terminal),
339 &padding);
340 #else
342 GtkBorder *ib = NULL;
343 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
344 if (ib) {
345 padding = *ib;
346 gtk_border_free(ib);
349 #endif
351 geo.width_inc = vte_terminal_get_char_width(term);
352 geo.height_inc = vte_terminal_get_char_height(term);
353 mask |= GDK_HINT_RESIZE_INC;
354 geo.base_width = geo.width_inc;
355 geo.base_height = geo.height_inc;
356 mask |= GDK_HINT_BASE_SIZE;
357 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
358 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
359 mask |= GDK_HINT_MIN_SIZE;
361 geo.base_width += padding.left + padding.right;
362 geo.base_height += padding.top + padding.bottom;
363 geo.min_width += padding.left + padding.right;
364 geo.min_height += padding.top + padding.bottom;
365 geo_widget = vc->vte.terminal;
366 #endif
369 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
370 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
373 void gd_update_windowsize(VirtualConsole *vc)
375 GtkDisplayState *s = vc->s;
377 gd_update_geometry_hints(vc);
379 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
380 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
381 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
385 static void gd_update_full_redraw(VirtualConsole *vc)
387 GtkWidget *area = vc->gfx.drawing_area;
388 int ww, wh;
389 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
390 #if defined(CONFIG_GTK_GL)
391 if (vc->gfx.gls) {
392 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
393 return;
395 #endif
396 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
399 static void gtk_release_modifiers(GtkDisplayState *s)
401 VirtualConsole *vc = gd_vc_find_current(s);
402 int i, keycode;
404 if (vc->type != GD_VC_GFX ||
405 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
406 return;
408 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
409 keycode = modifier_keycode[i];
410 if (!s->modifier_pressed[i]) {
411 continue;
413 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
414 s->modifier_pressed[i] = false;
418 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
419 GtkWidget *widget)
421 g_object_ref(G_OBJECT(widget));
422 gtk_container_remove(GTK_CONTAINER(from), widget);
423 gtk_container_add(GTK_CONTAINER(to), widget);
424 g_object_unref(G_OBJECT(widget));
427 /** DisplayState Callbacks **/
429 static void gd_update(DisplayChangeListener *dcl,
430 int x, int y, int w, int h)
432 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
433 GdkWindow *win;
434 int x1, x2, y1, y2;
435 int mx, my;
436 int fbw, fbh;
437 int ww, wh;
439 trace_gd_update(vc->label, x, y, w, h);
441 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
442 return;
445 if (vc->gfx.convert) {
446 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
447 NULL, vc->gfx.convert,
448 x, y, 0, 0, x, y, w, h);
451 x1 = floor(x * vc->gfx.scale_x);
452 y1 = floor(y * vc->gfx.scale_y);
454 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
455 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
457 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
458 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
460 win = gtk_widget_get_window(vc->gfx.drawing_area);
461 if (!win) {
462 return;
464 gdk_drawable_get_size(win, &ww, &wh);
466 mx = my = 0;
467 if (ww > fbw) {
468 mx = (ww - fbw) / 2;
470 if (wh > fbh) {
471 my = (wh - fbh) / 2;
474 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
475 mx + x1, my + y1, (x2 - x1), (y2 - y1));
478 static void gd_refresh(DisplayChangeListener *dcl)
480 graphic_hw_update(dcl->con);
483 #if GTK_CHECK_VERSION(3, 0, 0)
484 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
486 #if GTK_CHECK_VERSION(3, 20, 0)
487 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
488 #else
489 return gdk_device_manager_get_client_pointer(
490 gdk_display_get_device_manager(dpy));
491 #endif
494 static void gd_mouse_set(DisplayChangeListener *dcl,
495 int x, int y, int visible)
497 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
498 GdkDisplay *dpy;
499 gint x_root, y_root;
501 if (qemu_input_is_absolute()) {
502 return;
505 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
506 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
507 x, y, &x_root, &y_root);
508 gdk_device_warp(gd_get_pointer(dpy),
509 gtk_widget_get_screen(vc->gfx.drawing_area),
510 x_root, y_root);
511 vc->s->last_x = x;
512 vc->s->last_y = y;
514 #else
515 static void gd_mouse_set(DisplayChangeListener *dcl,
516 int x, int y, int visible)
518 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
519 gint x_root, y_root;
521 if (qemu_input_is_absolute()) {
522 return;
525 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
526 x, y, &x_root, &y_root);
527 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
528 gtk_widget_get_screen(vc->gfx.drawing_area),
529 x_root, y_root);
531 #endif
533 static void gd_cursor_define(DisplayChangeListener *dcl,
534 QEMUCursor *c)
536 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
537 GdkPixbuf *pixbuf;
538 GdkCursor *cursor;
540 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
541 return;
544 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
545 GDK_COLORSPACE_RGB, true, 8,
546 c->width, c->height, c->width * 4,
547 NULL, NULL);
548 cursor = gdk_cursor_new_from_pixbuf
549 (gtk_widget_get_display(vc->gfx.drawing_area),
550 pixbuf, c->hot_x, c->hot_y);
551 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
552 g_object_unref(pixbuf);
553 #if !GTK_CHECK_VERSION(3, 0, 0)
554 gdk_cursor_unref(cursor);
555 #else
556 g_object_unref(cursor);
557 #endif
560 static void gd_switch(DisplayChangeListener *dcl,
561 DisplaySurface *surface)
563 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
564 bool resized = true;
566 trace_gd_switch(vc->label,
567 surface ? surface_width(surface) : 0,
568 surface ? surface_height(surface) : 0);
570 if (vc->gfx.surface) {
571 cairo_surface_destroy(vc->gfx.surface);
572 vc->gfx.surface = NULL;
574 if (vc->gfx.convert) {
575 pixman_image_unref(vc->gfx.convert);
576 vc->gfx.convert = NULL;
579 if (vc->gfx.ds && surface &&
580 surface_width(vc->gfx.ds) == surface_width(surface) &&
581 surface_height(vc->gfx.ds) == surface_height(surface)) {
582 resized = false;
584 vc->gfx.ds = surface;
586 if (!surface) {
587 return;
590 if (surface->format == PIXMAN_x8r8g8b8) {
592 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
594 * No need to convert, use surface directly. Should be the
595 * common case as this is qemu_default_pixelformat(32) too.
597 vc->gfx.surface = cairo_image_surface_create_for_data
598 (surface_data(surface),
599 CAIRO_FORMAT_RGB24,
600 surface_width(surface),
601 surface_height(surface),
602 surface_stride(surface));
603 } else {
604 /* Must convert surface, use pixman to do it. */
605 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
606 surface_width(surface),
607 surface_height(surface),
608 NULL, 0);
609 vc->gfx.surface = cairo_image_surface_create_for_data
610 ((void *)pixman_image_get_data(vc->gfx.convert),
611 CAIRO_FORMAT_RGB24,
612 pixman_image_get_width(vc->gfx.convert),
613 pixman_image_get_height(vc->gfx.convert),
614 pixman_image_get_stride(vc->gfx.convert));
615 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
616 NULL, vc->gfx.convert,
617 0, 0, 0, 0, 0, 0,
618 pixman_image_get_width(vc->gfx.convert),
619 pixman_image_get_height(vc->gfx.convert));
622 if (resized) {
623 gd_update_windowsize(vc);
624 } else {
625 gd_update_full_redraw(vc);
629 static const DisplayChangeListenerOps dcl_ops = {
630 .dpy_name = "gtk",
631 .dpy_gfx_update = gd_update,
632 .dpy_gfx_switch = gd_switch,
633 .dpy_gfx_check_format = qemu_pixman_check_format,
634 .dpy_refresh = gd_refresh,
635 .dpy_mouse_set = gd_mouse_set,
636 .dpy_cursor_define = gd_cursor_define,
640 #if defined(CONFIG_OPENGL)
642 /** DisplayState Callbacks (opengl version) **/
644 #if defined(CONFIG_GTK_GL)
646 static const DisplayChangeListenerOps dcl_gl_area_ops = {
647 .dpy_name = "gtk-egl",
648 .dpy_gfx_update = gd_gl_area_update,
649 .dpy_gfx_switch = gd_gl_area_switch,
650 .dpy_gfx_check_format = console_gl_check_format,
651 .dpy_refresh = gd_gl_area_refresh,
652 .dpy_mouse_set = gd_mouse_set,
653 .dpy_cursor_define = gd_cursor_define,
655 .dpy_gl_ctx_create = gd_gl_area_create_context,
656 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
657 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
658 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
659 .dpy_gl_scanout = gd_gl_area_scanout,
660 .dpy_gl_update = gd_gl_area_scanout_flush,
663 #else
665 static const DisplayChangeListenerOps dcl_egl_ops = {
666 .dpy_name = "gtk-egl",
667 .dpy_gfx_update = gd_egl_update,
668 .dpy_gfx_switch = gd_egl_switch,
669 .dpy_gfx_check_format = console_gl_check_format,
670 .dpy_refresh = gd_egl_refresh,
671 .dpy_mouse_set = gd_mouse_set,
672 .dpy_cursor_define = gd_cursor_define,
674 .dpy_gl_ctx_create = gd_egl_create_context,
675 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
676 .dpy_gl_ctx_make_current = gd_egl_make_current,
677 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
678 .dpy_gl_scanout = gd_egl_scanout,
679 .dpy_gl_update = gd_egl_scanout_flush,
682 #endif /* CONFIG_GTK_GL */
683 #endif /* CONFIG_OPENGL */
685 /** QEMU Events **/
687 static void gd_change_runstate(void *opaque, int running, RunState state)
689 GtkDisplayState *s = opaque;
691 gd_update_caption(s);
694 static void gd_mouse_mode_change(Notifier *notify, void *data)
696 GtkDisplayState *s;
697 int i;
699 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
700 /* release the grab at switching to absolute mode */
701 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
702 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
703 FALSE);
705 for (i = 0; i < s->nb_vcs; i++) {
706 VirtualConsole *vc = &s->vc[i];
707 gd_update_cursor(vc);
711 /** GTK Events **/
713 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
714 void *opaque)
716 GtkDisplayState *s = opaque;
717 int i;
719 if (!no_quit) {
720 for (i = 0; i < s->nb_vcs; i++) {
721 if (s->vc[i].type != GD_VC_GFX) {
722 continue;
724 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
726 qmp_quit(NULL);
727 return FALSE;
730 return TRUE;
733 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
735 QemuUIInfo info;
737 memset(&info, 0, sizeof(info));
738 info.width = width;
739 info.height = height;
740 dpy_set_ui_info(vc->gfx.dcl.con, &info);
743 #if defined(CONFIG_GTK_GL)
745 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
746 void *opaque)
748 VirtualConsole *vc = opaque;
750 if (vc->gfx.gls) {
751 gd_gl_area_draw(vc);
753 return TRUE;
756 static void gd_resize_event(GtkGLArea *area,
757 gint width, gint height, gpointer *opaque)
759 VirtualConsole *vc = (void *)opaque;
761 gd_set_ui_info(vc, width, height);
764 #endif
766 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
768 VirtualConsole *vc = opaque;
769 GtkDisplayState *s = vc->s;
770 int mx, my;
771 int ww, wh;
772 int fbw, fbh;
774 #if defined(CONFIG_OPENGL)
775 if (vc->gfx.gls) {
776 #if defined(CONFIG_GTK_GL)
777 /* invoke render callback please */
778 return FALSE;
779 #else
780 gd_egl_draw(vc);
781 return TRUE;
782 #endif
784 #endif
786 if (!gtk_widget_get_realized(widget)) {
787 return FALSE;
789 if (!vc->gfx.ds) {
790 return FALSE;
793 fbw = surface_width(vc->gfx.ds);
794 fbh = surface_height(vc->gfx.ds);
796 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
798 if (s->full_screen) {
799 vc->gfx.scale_x = (double)ww / fbw;
800 vc->gfx.scale_y = (double)wh / fbh;
801 } else if (s->free_scale) {
802 double sx, sy;
804 sx = (double)ww / fbw;
805 sy = (double)wh / fbh;
807 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
810 fbw *= vc->gfx.scale_x;
811 fbh *= vc->gfx.scale_y;
813 mx = my = 0;
814 if (ww > fbw) {
815 mx = (ww - fbw) / 2;
817 if (wh > fbh) {
818 my = (wh - fbh) / 2;
821 cairo_rectangle(cr, 0, 0, ww, wh);
823 /* Optionally cut out the inner area where the pixmap
824 will be drawn. This avoids 'flashing' since we're
825 not double-buffering. Note we're using the undocumented
826 behaviour of drawing the rectangle from right to left
827 to cut out the whole */
828 cairo_rectangle(cr, mx + fbw, my,
829 -1 * fbw, fbh);
830 cairo_fill(cr);
832 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
833 cairo_set_source_surface(cr, vc->gfx.surface,
834 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
835 cairo_paint(cr);
837 return TRUE;
840 #if !GTK_CHECK_VERSION(3, 0, 0)
841 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
842 void *opaque)
844 cairo_t *cr;
845 gboolean ret;
847 cr = gdk_cairo_create(gtk_widget_get_window(widget));
848 cairo_rectangle(cr,
849 expose->area.x,
850 expose->area.y,
851 expose->area.width,
852 expose->area.height);
853 cairo_clip(cr);
855 ret = gd_draw_event(widget, cr, opaque);
857 cairo_destroy(cr);
859 return ret;
861 #endif
863 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
864 void *opaque)
866 VirtualConsole *vc = opaque;
867 GtkDisplayState *s = vc->s;
868 int x, y;
869 int mx, my;
870 int fbh, fbw;
871 int ww, wh;
873 if (!vc->gfx.ds) {
874 return TRUE;
877 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
878 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
880 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
881 &ww, &wh);
883 mx = my = 0;
884 if (ww > fbw) {
885 mx = (ww - fbw) / 2;
887 if (wh > fbh) {
888 my = (wh - fbh) / 2;
891 x = (motion->x - mx) / vc->gfx.scale_x;
892 y = (motion->y - my) / vc->gfx.scale_y;
894 if (qemu_input_is_absolute()) {
895 if (x < 0 || y < 0 ||
896 x >= surface_width(vc->gfx.ds) ||
897 y >= surface_height(vc->gfx.ds)) {
898 return TRUE;
900 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
901 surface_width(vc->gfx.ds));
902 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
903 surface_height(vc->gfx.ds));
904 qemu_input_event_sync();
905 } else if (s->last_set && s->ptr_owner == vc) {
906 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
907 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
908 qemu_input_event_sync();
910 s->last_x = x;
911 s->last_y = y;
912 s->last_set = TRUE;
914 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
915 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
916 int screen_width, screen_height;
918 int x = (int)motion->x_root;
919 int y = (int)motion->y_root;
921 #if GTK_CHECK_VERSION(3, 22, 0)
923 GdkDisplay *dpy = gtk_widget_get_display(widget);
924 GdkWindow *win = gtk_widget_get_window(widget);
925 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
926 GdkRectangle geometry;
927 gdk_monitor_get_geometry(monitor, &geometry);
928 screen_width = geometry.width;
929 screen_height = geometry.height;
931 #else
933 screen_width = gdk_screen_get_width(screen);
934 screen_height = gdk_screen_get_height(screen);
936 #endif
938 /* In relative mode check to see if client pointer hit
939 * one of the screen edges, and if so move it back by
940 * 200 pixels. This is important because the pointer
941 * in the server doesn't correspond 1-for-1, and so
942 * may still be only half way across the screen. Without
943 * this warp, the server pointer would thus appear to hit
944 * an invisible wall */
945 if (x == 0) {
946 x += 200;
948 if (y == 0) {
949 y += 200;
951 if (x == (screen_width - 1)) {
952 x -= 200;
954 if (y == (screen_height - 1)) {
955 y -= 200;
958 if (x != (int)motion->x_root || y != (int)motion->y_root) {
959 #if GTK_CHECK_VERSION(3, 0, 0)
960 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
961 gdk_device_warp(dev, screen, x, y);
962 #else
963 GdkDisplay *display = gtk_widget_get_display(widget);
964 gdk_display_warp_pointer(display, screen, x, y);
965 #endif
966 s->last_set = FALSE;
967 return FALSE;
970 return TRUE;
973 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
974 void *opaque)
976 VirtualConsole *vc = opaque;
977 GtkDisplayState *s = vc->s;
978 InputButton btn;
980 /* implicitly grab the input at the first click in the relative mode */
981 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
982 !qemu_input_is_absolute() && s->ptr_owner != vc) {
983 if (!vc->window) {
984 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
985 TRUE);
986 } else {
987 gd_grab_pointer(vc, "relative-mode-click");
989 return TRUE;
992 if (button->button == 1) {
993 btn = INPUT_BUTTON_LEFT;
994 } else if (button->button == 2) {
995 btn = INPUT_BUTTON_MIDDLE;
996 } else if (button->button == 3) {
997 btn = INPUT_BUTTON_RIGHT;
998 } else {
999 return TRUE;
1002 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1003 button->type == GDK_BUTTON_PRESS);
1004 qemu_input_event_sync();
1005 return TRUE;
1008 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1009 void *opaque)
1011 VirtualConsole *vc = opaque;
1012 InputButton btn;
1014 if (scroll->direction == GDK_SCROLL_UP) {
1015 btn = INPUT_BUTTON_WHEEL_UP;
1016 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1017 btn = INPUT_BUTTON_WHEEL_DOWN;
1018 } else {
1019 return TRUE;
1022 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1023 qemu_input_event_sync();
1024 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1025 qemu_input_event_sync();
1026 return TRUE;
1029 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
1031 int qemu_keycode;
1033 #ifdef GDK_WINDOWING_WIN32
1034 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1035 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
1036 switch (qemu_keycode) {
1037 case 103: /* alt gr */
1038 qemu_keycode = 56 | SCANCODE_GREY;
1039 break;
1041 return qemu_keycode;
1043 #endif
1045 if (gdk_keycode < 9) {
1046 qemu_keycode = 0;
1047 } else if (gdk_keycode < 97) {
1048 qemu_keycode = gdk_keycode - 8;
1049 #ifdef GDK_WINDOWING_X11
1050 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
1051 if (s->has_evdev) {
1052 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1053 } else {
1054 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
1056 #endif
1057 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
1058 qemu_keycode = 0x70;
1059 } else if (gdk_keycode == 211) { /* backslash */
1060 qemu_keycode = 0x73;
1061 } else {
1062 qemu_keycode = 0;
1065 return qemu_keycode;
1068 static gboolean gd_text_key_down(GtkWidget *widget,
1069 GdkEventKey *key, void *opaque)
1071 VirtualConsole *vc = opaque;
1072 QemuConsole *con = vc->gfx.dcl.con;
1074 if (key->keyval == GDK_KEY_Delete) {
1075 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1076 } else if (key->length) {
1077 kbd_put_string_console(con, key->string, key->length);
1078 } else {
1079 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
1080 key->hardware_keycode);
1081 int qcode = qemu_input_key_number_to_qcode(num);
1082 kbd_put_qcode_console(con, qcode);
1084 return TRUE;
1087 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1089 VirtualConsole *vc = opaque;
1090 GtkDisplayState *s = vc->s;
1091 int gdk_keycode = key->hardware_keycode;
1092 int qemu_keycode;
1093 int i;
1095 if (s->ignore_keys) {
1096 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1097 return TRUE;
1100 if (key->keyval == GDK_KEY_Pause) {
1101 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1102 key->type == GDK_KEY_PRESS);
1103 return TRUE;
1106 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
1107 gdk_keycode);
1109 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
1110 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1112 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1113 if (qemu_keycode == modifier_keycode[i]) {
1114 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1118 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
1119 key->type == GDK_KEY_PRESS);
1121 return TRUE;
1124 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1126 if (event->type == GDK_MOTION_NOTIFY) {
1127 return gd_motion_event(widget, &event->motion, opaque);
1129 return FALSE;
1132 /** Window Menu Actions **/
1134 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1136 GtkDisplayState *s = opaque;
1138 if (s->external_pause_update) {
1139 return;
1141 if (runstate_is_running()) {
1142 qmp_stop(NULL);
1143 } else {
1144 qmp_cont(NULL);
1148 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1150 qmp_system_reset(NULL);
1153 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1155 qmp_system_powerdown(NULL);
1158 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1160 qmp_quit(NULL);
1163 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1165 GtkDisplayState *s = opaque;
1166 VirtualConsole *vc = gd_vc_find_by_menu(s);
1167 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1168 gint page;
1170 gtk_release_modifiers(s);
1171 if (vc) {
1172 page = gtk_notebook_page_num(nb, vc->tab_item);
1173 gtk_notebook_set_current_page(nb, page);
1174 gtk_widget_grab_focus(vc->focus);
1176 s->ignore_keys = false;
1179 static void gd_accel_switch_vc(void *opaque)
1181 VirtualConsole *vc = opaque;
1183 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1184 #if !GTK_CHECK_VERSION(3, 0, 0)
1185 /* GTK2 sends the accel key to the target console - ignore this until */
1186 vc->s->ignore_keys = true;
1187 #endif
1190 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1192 GtkDisplayState *s = opaque;
1193 VirtualConsole *vc = gd_vc_find_current(s);
1195 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1196 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1197 } else {
1198 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1200 gd_update_windowsize(vc);
1203 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1204 void *opaque)
1206 VirtualConsole *vc = opaque;
1207 GtkDisplayState *s = vc->s;
1209 gtk_widget_set_sensitive(vc->menu_item, true);
1210 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1211 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1212 vc->tab_item, vc->label);
1213 gtk_widget_destroy(vc->window);
1214 vc->window = NULL;
1215 return TRUE;
1218 static gboolean gd_win_grab(void *opaque)
1220 VirtualConsole *vc = opaque;
1222 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1223 if (vc->s->ptr_owner) {
1224 gd_ungrab_pointer(vc->s);
1225 } else {
1226 gd_grab_pointer(vc, "user-request-detached-tab");
1228 return TRUE;
1231 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1233 GtkDisplayState *s = opaque;
1234 VirtualConsole *vc = gd_vc_find_current(s);
1236 if (vc->type == GD_VC_GFX &&
1237 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1238 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1239 FALSE);
1241 if (!vc->window) {
1242 gtk_widget_set_sensitive(vc->menu_item, false);
1243 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1244 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1246 g_signal_connect(vc->window, "delete-event",
1247 G_CALLBACK(gd_tab_window_close), vc);
1248 gtk_widget_show_all(vc->window);
1250 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1251 GtkAccelGroup *ag = gtk_accel_group_new();
1252 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1254 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1255 vc, NULL);
1256 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1259 gd_update_geometry_hints(vc);
1260 gd_update_caption(s);
1264 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1266 GtkDisplayState *s = opaque;
1267 VirtualConsole *vc = gd_vc_find_current(s);
1269 if (!s->full_screen) {
1270 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1271 gtk_widget_hide(s->menu_bar);
1272 if (vc->type == GD_VC_GFX) {
1273 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1275 gtk_window_fullscreen(GTK_WINDOW(s->window));
1276 s->full_screen = TRUE;
1277 } else {
1278 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1279 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1280 gtk_widget_show(s->menu_bar);
1281 s->full_screen = FALSE;
1282 if (vc->type == GD_VC_GFX) {
1283 vc->gfx.scale_x = 1.0;
1284 vc->gfx.scale_y = 1.0;
1285 gd_update_windowsize(vc);
1289 gd_update_cursor(vc);
1292 static void gd_accel_full_screen(void *opaque)
1294 GtkDisplayState *s = opaque;
1295 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1298 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1300 GtkDisplayState *s = opaque;
1301 VirtualConsole *vc = gd_vc_find_current(s);
1303 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1304 FALSE);
1306 vc->gfx.scale_x += VC_SCALE_STEP;
1307 vc->gfx.scale_y += VC_SCALE_STEP;
1309 gd_update_windowsize(vc);
1312 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1314 GtkDisplayState *s = opaque;
1315 VirtualConsole *vc = gd_vc_find_current(s);
1317 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1318 FALSE);
1320 vc->gfx.scale_x -= VC_SCALE_STEP;
1321 vc->gfx.scale_y -= VC_SCALE_STEP;
1323 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1324 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1326 gd_update_windowsize(vc);
1329 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1331 GtkDisplayState *s = opaque;
1332 VirtualConsole *vc = gd_vc_find_current(s);
1334 vc->gfx.scale_x = 1.0;
1335 vc->gfx.scale_y = 1.0;
1337 gd_update_windowsize(vc);
1340 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1342 GtkDisplayState *s = opaque;
1343 VirtualConsole *vc = gd_vc_find_current(s);
1345 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1346 s->free_scale = TRUE;
1347 } else {
1348 s->free_scale = FALSE;
1349 vc->gfx.scale_x = 1.0;
1350 vc->gfx.scale_y = 1.0;
1353 gd_update_windowsize(vc);
1354 gd_update_full_redraw(vc);
1357 #if GTK_CHECK_VERSION(3, 20, 0)
1358 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1360 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1361 GdkSeat *seat = gdk_display_get_default_seat(display);
1362 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1363 GdkSeatCapabilities caps = 0;
1364 GdkCursor *cursor = NULL;
1366 if (kbd) {
1367 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1369 if (ptr) {
1370 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1371 cursor = vc->s->null_cursor;
1374 if (caps) {
1375 gdk_seat_grab(seat, window, caps, false, cursor,
1376 NULL, NULL, NULL);
1377 } else {
1378 gdk_seat_ungrab(seat);
1381 #elif GTK_CHECK_VERSION(3, 0, 0)
1382 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1383 GdkInputSource source, GdkEventMask mask,
1384 GdkCursor *cursor)
1386 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1387 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1388 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1389 GList *tmp = devs;
1391 for (tmp = devs; tmp; tmp = tmp->next) {
1392 GdkDevice *dev = tmp->data;
1393 if (gdk_device_get_source(dev) != source) {
1394 continue;
1396 if (grab) {
1397 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1398 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1399 mask, cursor, GDK_CURRENT_TIME);
1400 } else {
1401 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1404 g_list_free(devs);
1406 #endif
1408 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1410 if (vc->s->kbd_owner) {
1411 if (vc->s->kbd_owner == vc) {
1412 return;
1413 } else {
1414 gd_ungrab_keyboard(vc->s);
1418 #if GTK_CHECK_VERSION(3, 20, 0)
1419 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1420 #elif GTK_CHECK_VERSION(3, 0, 0)
1421 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1422 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1423 NULL);
1424 #else
1425 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1426 FALSE,
1427 GDK_CURRENT_TIME);
1428 #endif
1429 vc->s->kbd_owner = vc;
1430 gd_update_caption(vc->s);
1431 trace_gd_grab(vc->label, "kbd", reason);
1434 static void gd_ungrab_keyboard(GtkDisplayState *s)
1436 VirtualConsole *vc = s->kbd_owner;
1438 if (vc == NULL) {
1439 return;
1441 s->kbd_owner = NULL;
1443 #if GTK_CHECK_VERSION(3, 20, 0)
1444 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1445 #elif GTK_CHECK_VERSION(3, 0, 0)
1446 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1447 #else
1448 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1449 #endif
1450 gd_update_caption(s);
1451 trace_gd_ungrab(vc->label, "kbd");
1454 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1456 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1458 if (vc->s->ptr_owner) {
1459 if (vc->s->ptr_owner == vc) {
1460 return;
1461 } else {
1462 gd_ungrab_pointer(vc->s);
1466 #if GTK_CHECK_VERSION(3, 20, 0)
1467 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1468 gdk_device_get_position(gd_get_pointer(display),
1469 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1470 #elif GTK_CHECK_VERSION(3, 0, 0)
1471 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1472 GDK_POINTER_MOTION_MASK |
1473 GDK_BUTTON_PRESS_MASK |
1474 GDK_BUTTON_RELEASE_MASK |
1475 GDK_BUTTON_MOTION_MASK |
1476 GDK_SCROLL_MASK,
1477 vc->s->null_cursor);
1478 gdk_device_get_position(gd_get_pointer(display),
1479 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1480 #else
1481 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1482 FALSE, /* All events to come to our window directly */
1483 GDK_POINTER_MOTION_MASK |
1484 GDK_BUTTON_PRESS_MASK |
1485 GDK_BUTTON_RELEASE_MASK |
1486 GDK_BUTTON_MOTION_MASK |
1487 GDK_SCROLL_MASK,
1488 NULL, /* Allow cursor to move over entire desktop */
1489 vc->s->null_cursor,
1490 GDK_CURRENT_TIME);
1491 gdk_display_get_pointer(display, NULL,
1492 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1493 #endif
1494 vc->s->ptr_owner = vc;
1495 gd_update_caption(vc->s);
1496 trace_gd_grab(vc->label, "ptr", reason);
1499 static void gd_ungrab_pointer(GtkDisplayState *s)
1501 VirtualConsole *vc = s->ptr_owner;
1502 GdkDisplay *display;
1504 if (vc == NULL) {
1505 return;
1507 s->ptr_owner = NULL;
1509 display = gtk_widget_get_display(vc->gfx.drawing_area);
1510 #if GTK_CHECK_VERSION(3, 20, 0)
1511 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1512 gdk_device_warp(gd_get_pointer(display),
1513 gtk_widget_get_screen(vc->gfx.drawing_area),
1514 vc->s->grab_x_root, vc->s->grab_y_root);
1515 #elif GTK_CHECK_VERSION(3, 0, 0)
1516 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1517 gdk_device_warp(gd_get_pointer(display),
1518 gtk_widget_get_screen(vc->gfx.drawing_area),
1519 vc->s->grab_x_root, vc->s->grab_y_root);
1520 #else
1521 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1522 gdk_display_warp_pointer(display,
1523 gtk_widget_get_screen(vc->gfx.drawing_area),
1524 vc->s->grab_x_root, vc->s->grab_y_root);
1525 #endif
1526 gd_update_caption(s);
1527 trace_gd_ungrab(vc->label, "ptr");
1530 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1532 GtkDisplayState *s = opaque;
1533 VirtualConsole *vc = gd_vc_find_current(s);
1535 if (gd_is_grab_active(s)) {
1536 gd_grab_keyboard(vc, "user-request-main-window");
1537 gd_grab_pointer(vc, "user-request-main-window");
1538 } else {
1539 gd_ungrab_keyboard(s);
1540 gd_ungrab_pointer(s);
1543 gd_update_cursor(vc);
1546 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1547 gpointer data)
1549 GtkDisplayState *s = data;
1550 VirtualConsole *vc;
1551 gboolean on_vga;
1553 if (!gtk_widget_get_realized(s->notebook)) {
1554 return;
1557 #ifdef VTE_RESIZE_HACK
1558 vc = gd_vc_find_current(s);
1559 if (vc && vc->type == GD_VC_VTE) {
1560 gtk_widget_hide(vc->vte.terminal);
1562 #endif
1563 vc = gd_vc_find_by_page(s, arg2);
1564 if (!vc) {
1565 return;
1567 #ifdef VTE_RESIZE_HACK
1568 if (vc->type == GD_VC_VTE) {
1569 gtk_widget_show(vc->vte.terminal);
1571 #endif
1572 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1573 TRUE);
1574 on_vga = (vc->type == GD_VC_GFX &&
1575 qemu_console_is_graphic(vc->gfx.dcl.con));
1576 if (!on_vga) {
1577 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1578 FALSE);
1579 } else if (s->full_screen) {
1580 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1581 TRUE);
1583 gtk_widget_set_sensitive(s->grab_item, on_vga);
1585 gd_update_windowsize(vc);
1586 gd_update_cursor(vc);
1589 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1590 gpointer opaque)
1592 VirtualConsole *vc = opaque;
1593 GtkDisplayState *s = vc->s;
1595 if (gd_grab_on_hover(s)) {
1596 gd_grab_keyboard(vc, "grab-on-hover");
1598 return TRUE;
1601 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1602 gpointer opaque)
1604 VirtualConsole *vc = opaque;
1605 GtkDisplayState *s = vc->s;
1607 if (gd_grab_on_hover(s)) {
1608 gd_ungrab_keyboard(s);
1610 return TRUE;
1613 static gboolean gd_focus_out_event(GtkWidget *widget,
1614 GdkEventCrossing *crossing, gpointer opaque)
1616 VirtualConsole *vc = opaque;
1617 GtkDisplayState *s = vc->s;
1619 gtk_release_modifiers(s);
1620 return TRUE;
1623 static gboolean gd_configure(GtkWidget *widget,
1624 GdkEventConfigure *cfg, gpointer opaque)
1626 VirtualConsole *vc = opaque;
1628 gd_set_ui_info(vc, cfg->width, cfg->height);
1629 return FALSE;
1632 /** Virtual Console Callbacks **/
1634 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1635 int idx, GSList *group, GtkWidget *view_menu)
1637 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1638 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1639 HOTKEY_MODIFIERS, 0,
1640 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1641 #if GTK_CHECK_VERSION(3, 8, 0)
1642 gtk_accel_label_set_accel(
1643 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1644 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1645 #endif
1647 g_signal_connect(vc->menu_item, "activate",
1648 G_CALLBACK(gd_menu_switch_vc), s);
1649 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1651 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1652 return group;
1655 #if defined(CONFIG_VTE)
1656 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1658 GtkDisplayState *s = opaque;
1659 VirtualConsole *vc = gd_vc_find_current(s);
1661 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1664 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1666 VirtualConsole *vc = opaque;
1668 if (gtk_adjustment_get_upper(adjustment) >
1669 gtk_adjustment_get_page_size(adjustment)) {
1670 gtk_widget_show(vc->vte.scrollbar);
1671 } else {
1672 gtk_widget_hide(vc->vte.scrollbar);
1676 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1678 VirtualConsole *vc = chr->opaque;
1680 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1681 return len;
1684 static void gd_vc_chr_set_echo(CharDriverState *chr, bool echo)
1686 VirtualConsole *vc = chr->opaque;
1688 vc->vte.echo = echo;
1691 static int nb_vcs;
1692 static CharDriverState *vcs[MAX_VCS];
1694 static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
1696 ChardevCommon *common = qapi_ChardevVC_base(vc);
1697 CharDriverState *chr;
1699 chr = qemu_chr_alloc(common, errp);
1700 if (!chr) {
1701 return NULL;
1704 chr->chr_write = gd_vc_chr_write;
1705 chr->chr_set_echo = gd_vc_chr_set_echo;
1707 /* Temporary, until gd_vc_vte_init runs. */
1708 chr->opaque = g_new0(VirtualConsole, 1);
1710 vcs[nb_vcs++] = chr;
1712 return chr;
1715 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1716 gpointer user_data)
1718 VirtualConsole *vc = user_data;
1720 if (vc->vte.echo) {
1721 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1722 int i;
1723 for (i = 0; i < size; i++) {
1724 uint8_t c = text[i];
1725 if (c >= 128 || isprint(c)) {
1726 /* 8-bit characters are considered printable. */
1727 vte_terminal_feed(term, &text[i], 1);
1728 } else if (c == '\r' || c == '\n') {
1729 vte_terminal_feed(term, "\r\n", 2);
1730 } else {
1731 char ctrl[2] = { '^', 0};
1732 ctrl[1] = text[i] ^ 64;
1733 vte_terminal_feed(term, ctrl, 2);
1738 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1739 return TRUE;
1742 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1743 CharDriverState *chr, int idx,
1744 GSList *group, GtkWidget *view_menu)
1746 char buffer[32];
1747 GtkWidget *box;
1748 GtkWidget *scrollbar;
1749 GtkAdjustment *vadjustment;
1750 VirtualConsole *tmp_vc = chr->opaque;
1752 vc->s = s;
1753 vc->vte.echo = tmp_vc->vte.echo;
1755 vc->vte.chr = chr;
1756 chr->opaque = vc;
1757 g_free(tmp_vc);
1759 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1760 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1761 ? vc->vte.chr->label : buffer);
1762 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1764 vc->vte.terminal = vte_terminal_new();
1765 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1767 /* The documentation says that the default is UTF-8, but actually it is
1768 * 7-bit ASCII at least in VTE 0.38.
1770 #if VTE_CHECK_VERSION(0, 38, 0)
1771 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1772 #else
1773 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1774 #endif
1776 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1777 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1778 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1780 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1781 vadjustment = gtk_scrollable_get_vadjustment
1782 (GTK_SCROLLABLE(vc->vte.terminal));
1783 #else
1784 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1785 #endif
1787 #if GTK_CHECK_VERSION(3, 0, 0)
1788 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1789 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1790 #else
1791 box = gtk_hbox_new(false, 2);
1792 scrollbar = gtk_vscrollbar_new(vadjustment);
1793 #endif
1795 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1796 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1798 vc->vte.box = box;
1799 vc->vte.scrollbar = scrollbar;
1801 g_signal_connect(vadjustment, "changed",
1802 G_CALLBACK(gd_vc_adjustment_changed), vc);
1804 vc->type = GD_VC_VTE;
1805 vc->tab_item = box;
1806 vc->focus = vc->vte.terminal;
1807 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1808 gtk_label_new(vc->label));
1810 qemu_chr_be_generic_open(vc->vte.chr);
1812 return group;
1815 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1816 GtkWidget *view_menu)
1818 int i;
1820 for (i = 0; i < nb_vcs; i++) {
1821 VirtualConsole *vc = &s->vc[s->nb_vcs];
1822 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1823 s->nb_vcs++;
1826 #endif /* CONFIG_VTE */
1828 /** Window Creation **/
1830 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1832 #if GTK_CHECK_VERSION(3, 0, 0)
1833 g_signal_connect(vc->gfx.drawing_area, "draw",
1834 G_CALLBACK(gd_draw_event), vc);
1835 #if defined(CONFIG_GTK_GL)
1836 if (display_opengl) {
1837 /* wire up GtkGlArea events */
1838 g_signal_connect(vc->gfx.drawing_area, "render",
1839 G_CALLBACK(gd_render_event), vc);
1840 g_signal_connect(vc->gfx.drawing_area, "resize",
1841 G_CALLBACK(gd_resize_event), vc);
1843 #endif
1844 #else
1845 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1846 G_CALLBACK(gd_expose_event), vc);
1847 #endif
1848 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1849 g_signal_connect(vc->gfx.drawing_area, "event",
1850 G_CALLBACK(gd_event), vc);
1851 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1852 G_CALLBACK(gd_button_event), vc);
1853 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1854 G_CALLBACK(gd_button_event), vc);
1855 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1856 G_CALLBACK(gd_scroll_event), vc);
1857 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1858 G_CALLBACK(gd_key_event), vc);
1859 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1860 G_CALLBACK(gd_key_event), vc);
1862 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1863 G_CALLBACK(gd_enter_event), vc);
1864 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1865 G_CALLBACK(gd_leave_event), vc);
1866 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1867 G_CALLBACK(gd_focus_out_event), vc);
1868 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1869 G_CALLBACK(gd_configure), vc);
1870 } else {
1871 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1872 G_CALLBACK(gd_text_key_down), vc);
1876 static void gd_connect_signals(GtkDisplayState *s)
1878 g_signal_connect(s->show_tabs_item, "activate",
1879 G_CALLBACK(gd_menu_show_tabs), s);
1880 g_signal_connect(s->untabify_item, "activate",
1881 G_CALLBACK(gd_menu_untabify), s);
1883 g_signal_connect(s->window, "delete-event",
1884 G_CALLBACK(gd_window_close), s);
1886 g_signal_connect(s->pause_item, "activate",
1887 G_CALLBACK(gd_menu_pause), s);
1888 g_signal_connect(s->reset_item, "activate",
1889 G_CALLBACK(gd_menu_reset), s);
1890 g_signal_connect(s->powerdown_item, "activate",
1891 G_CALLBACK(gd_menu_powerdown), s);
1892 g_signal_connect(s->quit_item, "activate",
1893 G_CALLBACK(gd_menu_quit), s);
1894 #if defined(CONFIG_VTE)
1895 g_signal_connect(s->copy_item, "activate",
1896 G_CALLBACK(gd_menu_copy), s);
1897 #endif
1898 g_signal_connect(s->full_screen_item, "activate",
1899 G_CALLBACK(gd_menu_full_screen), s);
1900 g_signal_connect(s->zoom_in_item, "activate",
1901 G_CALLBACK(gd_menu_zoom_in), s);
1902 g_signal_connect(s->zoom_out_item, "activate",
1903 G_CALLBACK(gd_menu_zoom_out), s);
1904 g_signal_connect(s->zoom_fixed_item, "activate",
1905 G_CALLBACK(gd_menu_zoom_fixed), s);
1906 g_signal_connect(s->zoom_fit_item, "activate",
1907 G_CALLBACK(gd_menu_zoom_fit), s);
1908 g_signal_connect(s->grab_item, "activate",
1909 G_CALLBACK(gd_menu_grab_input), s);
1910 g_signal_connect(s->notebook, "switch-page",
1911 G_CALLBACK(gd_change_page), s);
1914 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
1916 GtkWidget *machine_menu;
1917 GtkWidget *separator;
1919 machine_menu = gtk_menu_new();
1920 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1922 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1923 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1925 separator = gtk_separator_menu_item_new();
1926 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1928 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1929 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1931 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1932 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1934 separator = gtk_separator_menu_item_new();
1935 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1937 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1938 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1939 "<QEMU>/Machine/Quit");
1940 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1941 GDK_KEY_q, HOTKEY_MODIFIERS);
1942 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1944 return machine_menu;
1947 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1948 QemuConsole *con, int idx,
1949 GSList *group, GtkWidget *view_menu)
1951 vc->label = qemu_console_get_label(con);
1952 vc->s = s;
1953 vc->gfx.scale_x = 1.0;
1954 vc->gfx.scale_y = 1.0;
1956 #if defined(CONFIG_OPENGL)
1957 if (display_opengl) {
1958 #if defined(CONFIG_GTK_GL)
1959 vc->gfx.drawing_area = gtk_gl_area_new();
1960 vc->gfx.dcl.ops = &dcl_gl_area_ops;
1961 #else
1962 vc->gfx.drawing_area = gtk_drawing_area_new();
1964 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1965 * It is required for opengl rendering on X11 though. A
1966 * proper replacement (native opengl support) is only
1967 * available in 3.16+. Silence the warning if possible.
1969 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1970 #pragma GCC diagnostic push
1971 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1972 #endif
1973 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1974 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1975 #pragma GCC diagnostic pop
1976 #endif
1977 vc->gfx.dcl.ops = &dcl_egl_ops;
1978 #endif /* CONFIG_GTK_GL */
1979 } else
1980 #endif
1982 vc->gfx.drawing_area = gtk_drawing_area_new();
1983 vc->gfx.dcl.ops = &dcl_ops;
1987 gtk_widget_add_events(vc->gfx.drawing_area,
1988 GDK_POINTER_MOTION_MASK |
1989 GDK_BUTTON_PRESS_MASK |
1990 GDK_BUTTON_RELEASE_MASK |
1991 GDK_BUTTON_MOTION_MASK |
1992 GDK_ENTER_NOTIFY_MASK |
1993 GDK_LEAVE_NOTIFY_MASK |
1994 GDK_SCROLL_MASK |
1995 GDK_KEY_PRESS_MASK);
1996 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1998 vc->type = GD_VC_GFX;
1999 vc->tab_item = vc->gfx.drawing_area;
2000 vc->focus = vc->gfx.drawing_area;
2001 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2002 vc->tab_item, gtk_label_new(vc->label));
2004 vc->gfx.dcl.con = con;
2005 register_displaychangelistener(&vc->gfx.dcl);
2007 gd_connect_vc_gfx_signals(vc);
2008 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2010 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2011 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2012 s->free_scale = true;
2015 return group;
2018 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2020 GSList *group = NULL;
2021 GtkWidget *view_menu;
2022 GtkWidget *separator;
2023 QemuConsole *con;
2024 int vc;
2026 view_menu = gtk_menu_new();
2027 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2029 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2031 #if defined(CONFIG_VTE)
2032 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2033 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2034 #endif
2036 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2037 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2038 #if GTK_CHECK_VERSION(3, 8, 0)
2039 gtk_accel_label_set_accel(
2040 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2041 GDK_KEY_f, HOTKEY_MODIFIERS);
2042 #endif
2043 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2045 separator = gtk_separator_menu_item_new();
2046 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2048 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2049 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2050 "<QEMU>/View/Zoom In");
2051 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2052 HOTKEY_MODIFIERS);
2053 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2055 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2056 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2057 "<QEMU>/View/Zoom Out");
2058 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2059 HOTKEY_MODIFIERS);
2060 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2062 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2063 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2064 "<QEMU>/View/Zoom Fixed");
2065 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2066 HOTKEY_MODIFIERS);
2067 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2069 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2070 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2072 separator = gtk_separator_menu_item_new();
2073 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2075 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2076 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2078 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2079 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2080 "<QEMU>/View/Grab Input");
2081 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2082 HOTKEY_MODIFIERS);
2083 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2085 separator = gtk_separator_menu_item_new();
2086 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2088 /* gfx */
2089 for (vc = 0;; vc++) {
2090 con = qemu_console_lookup_by_index(vc);
2091 if (!con) {
2092 break;
2094 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2095 vc, group, view_menu);
2096 s->nb_vcs++;
2099 #if defined(CONFIG_VTE)
2100 /* vte */
2101 gd_vcs_init(s, group, view_menu);
2102 #endif
2104 separator = gtk_separator_menu_item_new();
2105 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2107 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2108 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2110 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2111 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2113 return view_menu;
2116 static void gd_create_menus(GtkDisplayState *s)
2118 s->accel_group = gtk_accel_group_new();
2119 s->machine_menu = gd_create_menu_machine(s);
2120 s->view_menu = gd_create_menu_view(s);
2122 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2123 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2124 s->machine_menu);
2125 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2127 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2128 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2129 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2131 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2132 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2135 static void gd_set_keycode_type(GtkDisplayState *s)
2137 #ifdef GDK_WINDOWING_X11
2138 GdkDisplay *display = gtk_widget_get_display(s->window);
2139 if (GDK_IS_X11_DISPLAY(display)) {
2140 Display *x11_display = gdk_x11_display_get_xdisplay(display);
2141 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
2142 XkbUseCoreKbd);
2143 char *keycodes = NULL;
2145 if (desc && desc->names) {
2146 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
2148 if (keycodes == NULL) {
2149 fprintf(stderr, "could not lookup keycode name\n");
2150 } else if (strstart(keycodes, "evdev", NULL)) {
2151 s->has_evdev = true;
2152 } else if (!strstart(keycodes, "xfree86", NULL)) {
2153 fprintf(stderr, "unknown keycodes `%s', please report to "
2154 "qemu-devel@nongnu.org\n", keycodes);
2157 if (desc) {
2158 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
2160 if (keycodes) {
2161 XFree(keycodes);
2164 #endif
2167 static gboolean gtkinit;
2169 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
2171 GtkDisplayState *s = g_malloc0(sizeof(*s));
2172 char *filename;
2173 GdkDisplay *window_display;
2175 if (!gtkinit) {
2176 fprintf(stderr, "gtk initialization failed\n");
2177 exit(1);
2180 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2181 #if GTK_CHECK_VERSION(3, 2, 0)
2182 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2183 #else
2184 s->vbox = gtk_vbox_new(FALSE, 0);
2185 #endif
2186 s->notebook = gtk_notebook_new();
2187 s->menu_bar = gtk_menu_bar_new();
2189 s->free_scale = FALSE;
2191 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2192 setlocale(LC_MESSAGES, "");
2193 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2194 textdomain("qemu");
2196 window_display = gtk_widget_get_display(s->window);
2197 s->null_cursor = gdk_cursor_new_for_display(window_display,
2198 GDK_BLANK_CURSOR);
2200 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2201 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2202 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2204 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2205 if (filename) {
2206 GError *error = NULL;
2207 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2208 if (pixbuf) {
2209 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2210 } else {
2211 g_error_free(error);
2213 g_free(filename);
2216 gd_create_menus(s);
2218 gd_connect_signals(s);
2220 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2221 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2223 gd_update_caption(s);
2225 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2226 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2228 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2230 gtk_widget_show_all(s->window);
2232 #ifdef VTE_RESIZE_HACK
2234 VirtualConsole *cur = gd_vc_find_current(s);
2235 if (cur) {
2236 int i;
2238 for (i = 0; i < s->nb_vcs; i++) {
2239 VirtualConsole *vc = &s->vc[i];
2240 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2241 gtk_widget_hide(vc->vte.terminal);
2244 gd_update_windowsize(cur);
2247 #endif
2249 if (full_screen) {
2250 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2252 if (grab_on_hover) {
2253 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2256 gd_set_keycode_type(s);
2259 void early_gtk_display_init(int opengl)
2261 /* The QEMU code relies on the assumption that it's always run in
2262 * the C locale. Therefore it is not prepared to deal with
2263 * operations that produce different results depending on the
2264 * locale, such as printf's formatting of decimal numbers, and
2265 * possibly others.
2267 * Since GTK+ calls setlocale() by default -importing the locale
2268 * settings from the environment- we must prevent it from doing so
2269 * using gtk_disable_setlocale().
2271 * QEMU's GTK+ UI, however, _does_ have translations for some of
2272 * the menu items. As a trade-off between a functionally correct
2273 * QEMU and a fully internationalized UI we support importing
2274 * LC_MESSAGES from the environment (see the setlocale() call
2275 * earlier in this file). This allows us to display translated
2276 * messages leaving everything else untouched.
2278 gtk_disable_setlocale();
2279 gtkinit = gtk_init_check(NULL, NULL);
2280 if (!gtkinit) {
2281 /* don't exit yet, that'll break -help */
2282 return;
2285 switch (opengl) {
2286 case -1: /* default */
2287 case 0: /* off */
2288 break;
2289 case 1: /* on */
2290 #if defined(CONFIG_OPENGL)
2291 #if defined(CONFIG_GTK_GL)
2292 gtk_gl_area_init();
2293 #else
2294 gtk_egl_init();
2295 #endif
2296 #endif
2297 break;
2298 default:
2299 g_assert_not_reached();
2300 break;
2303 #if defined(CONFIG_VTE)
2304 register_vc_handler(gd_vc_handler);
2305 #endif