]> git.proxmox.com Git - mirror_qemu.git/blob - ui/gtk.c
ui/gtk: use GtkGlArea on wayland only
[mirror_qemu.git] / ui / gtk.c
1 /*
2 * GTK UI
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 *
12 * Portions from gtk-vnc:
13 *
14 * GTK VNC Widget
15 *
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
18 *
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.
23 *
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.
28 *
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
32 */
33
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
36
37 #include "qemu/osdep.h"
38 #include "qemu-common.h"
39 #include "qapi/error.h"
40 #include "qapi/qapi-commands-misc.h"
41 #include "qemu/cutils.h"
42
43 #include "ui/console.h"
44 #include "ui/gtk.h"
45
46 #include <glib/gi18n.h>
47 #include <locale.h>
48 #if defined(CONFIG_VTE)
49 #include <vte/vte.h>
50 #endif
51 #include <math.h>
52
53 #include "trace.h"
54 #include "ui/input.h"
55 #include "sysemu/sysemu.h"
56 #include "keymaps.h"
57 #include "chardev/char.h"
58 #include "qom/object.h"
59
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
67
68 #ifdef GDK_WINDOWING_X11
69 #include "ui/x_keymap.h"
70
71 /* Gtk2 compat */
72 #ifndef GDK_IS_X11_DISPLAY
73 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
74 #endif
75 #endif
76
77
78 #ifdef GDK_WINDOWING_WAYLAND
79 /* Gtk2 compat */
80 #ifndef GDK_IS_WAYLAND_DISPLAY
81 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
82 #endif
83 #endif
84
85
86 #ifdef GDK_WINDOWING_WIN32
87 /* Gtk2 compat */
88 #ifndef GDK_IS_WIN32_DISPLAY
89 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
90 #endif
91 #endif
92
93
94 #ifdef GDK_WINDOWING_BROADWAY
95 /* Gtk2 compat */
96 #ifndef GDK_IS_BROADWAY_DISPLAY
97 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
98 #endif
99 #endif
100
101
102 #ifdef GDK_WINDOWING_QUARTZ
103 /* Gtk2 compat */
104 #ifndef GDK_IS_QUARTZ_DISPLAY
105 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
106 #endif
107 #endif
108
109
110 #if !defined(CONFIG_VTE)
111 # define VTE_CHECK_VERSION(a, b, c) 0
112 #endif
113
114 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
115 /*
116 * The gtk2 vte terminal widget seriously messes up the window resize
117 * for some reason. You basically can't make the qemu window smaller
118 * any more because the toplevel window geoemtry hints are overridden.
119 *
120 * Workaround that by hiding all vte widgets, except the one in the
121 * current tab.
122 *
123 * Luckily everything works smooth in gtk3.
124 */
125 # define VTE_RESIZE_HACK 1
126 #endif
127
128 #if !GTK_CHECK_VERSION(2, 20, 0)
129 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
130 #endif
131
132 #ifndef GDK_IS_X11_DISPLAY
133 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
134 #endif
135 #ifndef GDK_IS_WAYLAND_DISPLAY
136 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
137 #endif
138 #ifndef GDK_IS_WIN32_DISPLAY
139 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
140 #endif
141
142 #if !GTK_CHECK_VERSION(2, 22, 0)
143 #define GDK_KEY_0 GDK_0
144 #define GDK_KEY_1 GDK_1
145 #define GDK_KEY_2 GDK_2
146 #define GDK_KEY_f GDK_f
147 #define GDK_KEY_g GDK_g
148 #define GDK_KEY_q GDK_q
149 #define GDK_KEY_plus GDK_plus
150 #define GDK_KEY_equal GDK_equal
151 #define GDK_KEY_minus GDK_minus
152 #define GDK_KEY_Pause GDK_Pause
153 #define GDK_KEY_Delete GDK_Delete
154 #endif
155
156 /* Some older mingw versions lack this constant or have
157 * it conditionally defined */
158 #ifdef _WIN32
159 # ifndef MAPVK_VK_TO_VSC
160 # define MAPVK_VK_TO_VSC 0
161 # endif
162 #endif
163
164
165 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
166
167 static const int modifier_keycode[] = {
168 Q_KEY_CODE_SHIFT,
169 Q_KEY_CODE_SHIFT_R,
170 Q_KEY_CODE_CTRL,
171 Q_KEY_CODE_CTRL_R,
172 Q_KEY_CODE_ALT,
173 Q_KEY_CODE_ALT_R,
174 Q_KEY_CODE_META_L,
175 Q_KEY_CODE_META_R,
176 };
177
178 static const guint16 *keycode_map;
179 static size_t keycode_maplen;
180
181 struct GtkDisplayState {
182 GtkWidget *window;
183
184 GtkWidget *menu_bar;
185
186 GtkAccelGroup *accel_group;
187
188 GtkWidget *machine_menu_item;
189 GtkWidget *machine_menu;
190 GtkWidget *pause_item;
191 GtkWidget *reset_item;
192 GtkWidget *powerdown_item;
193 GtkWidget *quit_item;
194
195 GtkWidget *view_menu_item;
196 GtkWidget *view_menu;
197 GtkWidget *full_screen_item;
198 GtkWidget *copy_item;
199 GtkWidget *zoom_in_item;
200 GtkWidget *zoom_out_item;
201 GtkWidget *zoom_fixed_item;
202 GtkWidget *zoom_fit_item;
203 GtkWidget *grab_item;
204 GtkWidget *grab_on_hover_item;
205
206 int nb_vcs;
207 VirtualConsole vc[MAX_VCS];
208
209 GtkWidget *show_tabs_item;
210 GtkWidget *untabify_item;
211
212 GtkWidget *vbox;
213 GtkWidget *notebook;
214 int button_mask;
215 gboolean last_set;
216 int last_x;
217 int last_y;
218 int grab_x_root;
219 int grab_y_root;
220 VirtualConsole *kbd_owner;
221 VirtualConsole *ptr_owner;
222
223 gboolean full_screen;
224
225 GdkCursor *null_cursor;
226 Notifier mouse_mode_notifier;
227 gboolean free_scale;
228
229 bool external_pause_update;
230
231 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
232 bool ignore_keys;
233
234 DisplayOptions *opts;
235 };
236
237 typedef struct VCChardev {
238 Chardev parent;
239 VirtualConsole *console;
240 bool echo;
241 } VCChardev;
242
243 #define TYPE_CHARDEV_VC "chardev-vc"
244 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
245
246 bool gtk_use_gl_area;
247
248 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
249 static void gd_ungrab_pointer(GtkDisplayState *s);
250 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
251 static void gd_ungrab_keyboard(GtkDisplayState *s);
252
253 /** Utility Functions **/
254
255 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
256 {
257 VirtualConsole *vc;
258 gint i;
259
260 for (i = 0; i < s->nb_vcs; i++) {
261 vc = &s->vc[i];
262 if (gtk_check_menu_item_get_active
263 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
264 return vc;
265 }
266 }
267 return NULL;
268 }
269
270 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
271 {
272 VirtualConsole *vc;
273 gint i, p;
274
275 for (i = 0; i < s->nb_vcs; i++) {
276 vc = &s->vc[i];
277 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
278 if (p == page) {
279 return vc;
280 }
281 }
282 return NULL;
283 }
284
285 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
286 {
287 gint page;
288
289 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
290 return gd_vc_find_by_page(s, page);
291 }
292
293 static bool gd_is_grab_active(GtkDisplayState *s)
294 {
295 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
296 }
297
298 static bool gd_grab_on_hover(GtkDisplayState *s)
299 {
300 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
301 }
302
303 static void gd_update_cursor(VirtualConsole *vc)
304 {
305 GtkDisplayState *s = vc->s;
306 GdkWindow *window;
307
308 if (vc->type != GD_VC_GFX ||
309 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
310 return;
311 }
312
313 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
314 return;
315 }
316
317 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
318 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
319 gdk_window_set_cursor(window, s->null_cursor);
320 } else {
321 gdk_window_set_cursor(window, NULL);
322 }
323 }
324
325 static void gd_update_caption(GtkDisplayState *s)
326 {
327 const char *status = "";
328 gchar *prefix;
329 gchar *title;
330 const char *grab = "";
331 bool is_paused = !runstate_is_running();
332 int i;
333
334 if (qemu_name) {
335 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
336 } else {
337 prefix = g_strdup_printf("QEMU");
338 }
339
340 if (s->ptr_owner != NULL &&
341 s->ptr_owner->window == NULL) {
342 grab = _(" - Press Ctrl+Alt+G to release grab");
343 }
344
345 if (is_paused) {
346 status = _(" [Paused]");
347 }
348 s->external_pause_update = true;
349 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
350 is_paused);
351 s->external_pause_update = false;
352
353 title = g_strdup_printf("%s%s%s", prefix, status, grab);
354 gtk_window_set_title(GTK_WINDOW(s->window), title);
355 g_free(title);
356
357 for (i = 0; i < s->nb_vcs; i++) {
358 VirtualConsole *vc = &s->vc[i];
359
360 if (!vc->window) {
361 continue;
362 }
363 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
364 vc == s->kbd_owner ? " +kbd" : "",
365 vc == s->ptr_owner ? " +ptr" : "");
366 gtk_window_set_title(GTK_WINDOW(vc->window), title);
367 g_free(title);
368 }
369
370 g_free(prefix);
371 }
372
373 static void gd_update_geometry_hints(VirtualConsole *vc)
374 {
375 GtkDisplayState *s = vc->s;
376 GdkWindowHints mask = 0;
377 GdkGeometry geo = {};
378 GtkWidget *geo_widget = NULL;
379 GtkWindow *geo_window;
380
381 if (vc->type == GD_VC_GFX) {
382 if (!vc->gfx.ds) {
383 return;
384 }
385 if (s->free_scale) {
386 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
387 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
388 mask |= GDK_HINT_MIN_SIZE;
389 } else {
390 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
391 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
392 mask |= GDK_HINT_MIN_SIZE;
393 }
394 geo_widget = vc->gfx.drawing_area;
395 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
396
397 #if defined(CONFIG_VTE)
398 } else if (vc->type == GD_VC_VTE) {
399 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
400 GtkBorder padding = { 0 };
401
402 #if VTE_CHECK_VERSION(0, 37, 0)
403 gtk_style_context_get_padding(
404 gtk_widget_get_style_context(vc->vte.terminal),
405 gtk_widget_get_state_flags(vc->vte.terminal),
406 &padding);
407 #else
408 {
409 GtkBorder *ib = NULL;
410 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
411 if (ib) {
412 padding = *ib;
413 gtk_border_free(ib);
414 }
415 }
416 #endif
417
418 geo.width_inc = vte_terminal_get_char_width(term);
419 geo.height_inc = vte_terminal_get_char_height(term);
420 mask |= GDK_HINT_RESIZE_INC;
421 geo.base_width = geo.width_inc;
422 geo.base_height = geo.height_inc;
423 mask |= GDK_HINT_BASE_SIZE;
424 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
425 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
426 mask |= GDK_HINT_MIN_SIZE;
427
428 geo.base_width += padding.left + padding.right;
429 geo.base_height += padding.top + padding.bottom;
430 geo.min_width += padding.left + padding.right;
431 geo.min_height += padding.top + padding.bottom;
432 geo_widget = vc->vte.terminal;
433 #endif
434 }
435
436 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
437 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
438 }
439
440 void gd_update_windowsize(VirtualConsole *vc)
441 {
442 GtkDisplayState *s = vc->s;
443
444 gd_update_geometry_hints(vc);
445
446 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
447 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
448 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
449 }
450 }
451
452 static void gd_update_full_redraw(VirtualConsole *vc)
453 {
454 GtkWidget *area = vc->gfx.drawing_area;
455 int ww, wh;
456 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
457 #if defined(CONFIG_GTK_GL)
458 if (vc->gfx.gls && gtk_use_gl_area) {
459 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
460 return;
461 }
462 #endif
463 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
464 }
465
466 static void gtk_release_modifiers(GtkDisplayState *s)
467 {
468 VirtualConsole *vc = gd_vc_find_current(s);
469 int i, qcode;
470
471 if (vc->type != GD_VC_GFX ||
472 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
473 return;
474 }
475 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
476 qcode = modifier_keycode[i];
477 if (!s->modifier_pressed[i]) {
478 continue;
479 }
480 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode, false);
481 s->modifier_pressed[i] = false;
482 }
483 }
484
485 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
486 GtkWidget *widget)
487 {
488 g_object_ref(G_OBJECT(widget));
489 gtk_container_remove(GTK_CONTAINER(from), widget);
490 gtk_container_add(GTK_CONTAINER(to), widget);
491 g_object_unref(G_OBJECT(widget));
492 }
493
494 /** DisplayState Callbacks **/
495
496 static void gd_update(DisplayChangeListener *dcl,
497 int x, int y, int w, int h)
498 {
499 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
500 GdkWindow *win;
501 int x1, x2, y1, y2;
502 int mx, my;
503 int fbw, fbh;
504 int ww, wh;
505
506 trace_gd_update(vc->label, x, y, w, h);
507
508 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
509 return;
510 }
511
512 if (vc->gfx.convert) {
513 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
514 NULL, vc->gfx.convert,
515 x, y, 0, 0, x, y, w, h);
516 }
517
518 x1 = floor(x * vc->gfx.scale_x);
519 y1 = floor(y * vc->gfx.scale_y);
520
521 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
522 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
523
524 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
525 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
526
527 win = gtk_widget_get_window(vc->gfx.drawing_area);
528 if (!win) {
529 return;
530 }
531 gdk_drawable_get_size(win, &ww, &wh);
532
533 mx = my = 0;
534 if (ww > fbw) {
535 mx = (ww - fbw) / 2;
536 }
537 if (wh > fbh) {
538 my = (wh - fbh) / 2;
539 }
540
541 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
542 mx + x1, my + y1, (x2 - x1), (y2 - y1));
543 }
544
545 static void gd_refresh(DisplayChangeListener *dcl)
546 {
547 graphic_hw_update(dcl->con);
548 }
549
550 #if GTK_CHECK_VERSION(3, 0, 0)
551 static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
552 {
553 #if GTK_CHECK_VERSION(3, 20, 0)
554 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
555 #else
556 return gdk_device_manager_get_client_pointer(
557 gdk_display_get_device_manager(dpy));
558 #endif
559 }
560
561 static void gd_mouse_set(DisplayChangeListener *dcl,
562 int x, int y, int visible)
563 {
564 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
565 GdkDisplay *dpy;
566 gint x_root, y_root;
567
568 if (qemu_input_is_absolute()) {
569 return;
570 }
571
572 dpy = gtk_widget_get_display(vc->gfx.drawing_area);
573 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
574 x, y, &x_root, &y_root);
575 gdk_device_warp(gd_get_pointer(dpy),
576 gtk_widget_get_screen(vc->gfx.drawing_area),
577 x_root, y_root);
578 vc->s->last_x = x;
579 vc->s->last_y = y;
580 }
581 #else
582 static void gd_mouse_set(DisplayChangeListener *dcl,
583 int x, int y, int visible)
584 {
585 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
586 gint x_root, y_root;
587
588 if (qemu_input_is_absolute()) {
589 return;
590 }
591
592 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
593 x, y, &x_root, &y_root);
594 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
595 gtk_widget_get_screen(vc->gfx.drawing_area),
596 x_root, y_root);
597 }
598 #endif
599
600 static void gd_cursor_define(DisplayChangeListener *dcl,
601 QEMUCursor *c)
602 {
603 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
604 GdkPixbuf *pixbuf;
605 GdkCursor *cursor;
606
607 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
608 return;
609 }
610
611 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
612 GDK_COLORSPACE_RGB, true, 8,
613 c->width, c->height, c->width * 4,
614 NULL, NULL);
615 cursor = gdk_cursor_new_from_pixbuf
616 (gtk_widget_get_display(vc->gfx.drawing_area),
617 pixbuf, c->hot_x, c->hot_y);
618 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
619 g_object_unref(pixbuf);
620 #if !GTK_CHECK_VERSION(3, 0, 0)
621 gdk_cursor_unref(cursor);
622 #else
623 g_object_unref(cursor);
624 #endif
625 }
626
627 static void gd_switch(DisplayChangeListener *dcl,
628 DisplaySurface *surface)
629 {
630 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
631 bool resized = true;
632
633 trace_gd_switch(vc->label,
634 surface ? surface_width(surface) : 0,
635 surface ? surface_height(surface) : 0);
636
637 if (vc->gfx.surface) {
638 cairo_surface_destroy(vc->gfx.surface);
639 vc->gfx.surface = NULL;
640 }
641 if (vc->gfx.convert) {
642 pixman_image_unref(vc->gfx.convert);
643 vc->gfx.convert = NULL;
644 }
645
646 if (vc->gfx.ds && surface &&
647 surface_width(vc->gfx.ds) == surface_width(surface) &&
648 surface_height(vc->gfx.ds) == surface_height(surface)) {
649 resized = false;
650 }
651 vc->gfx.ds = surface;
652
653 if (!surface) {
654 return;
655 }
656
657 if (surface->format == PIXMAN_x8r8g8b8) {
658 /*
659 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
660 *
661 * No need to convert, use surface directly. Should be the
662 * common case as this is qemu_default_pixelformat(32) too.
663 */
664 vc->gfx.surface = cairo_image_surface_create_for_data
665 (surface_data(surface),
666 CAIRO_FORMAT_RGB24,
667 surface_width(surface),
668 surface_height(surface),
669 surface_stride(surface));
670 } else {
671 /* Must convert surface, use pixman to do it. */
672 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
673 surface_width(surface),
674 surface_height(surface),
675 NULL, 0);
676 vc->gfx.surface = cairo_image_surface_create_for_data
677 ((void *)pixman_image_get_data(vc->gfx.convert),
678 CAIRO_FORMAT_RGB24,
679 pixman_image_get_width(vc->gfx.convert),
680 pixman_image_get_height(vc->gfx.convert),
681 pixman_image_get_stride(vc->gfx.convert));
682 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
683 NULL, vc->gfx.convert,
684 0, 0, 0, 0, 0, 0,
685 pixman_image_get_width(vc->gfx.convert),
686 pixman_image_get_height(vc->gfx.convert));
687 }
688
689 if (resized) {
690 gd_update_windowsize(vc);
691 } else {
692 gd_update_full_redraw(vc);
693 }
694 }
695
696 static const DisplayChangeListenerOps dcl_ops = {
697 .dpy_name = "gtk",
698 .dpy_gfx_update = gd_update,
699 .dpy_gfx_switch = gd_switch,
700 .dpy_gfx_check_format = qemu_pixman_check_format,
701 .dpy_refresh = gd_refresh,
702 .dpy_mouse_set = gd_mouse_set,
703 .dpy_cursor_define = gd_cursor_define,
704 };
705
706
707 #if defined(CONFIG_OPENGL)
708
709 /** DisplayState Callbacks (opengl version) **/
710
711 #if defined(CONFIG_GTK_GL)
712
713 static const DisplayChangeListenerOps dcl_gl_area_ops = {
714 .dpy_name = "gtk-egl",
715 .dpy_gfx_update = gd_gl_area_update,
716 .dpy_gfx_switch = gd_gl_area_switch,
717 .dpy_gfx_check_format = console_gl_check_format,
718 .dpy_refresh = gd_gl_area_refresh,
719 .dpy_mouse_set = gd_mouse_set,
720 .dpy_cursor_define = gd_cursor_define,
721
722 .dpy_gl_ctx_create = gd_gl_area_create_context,
723 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
724 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
725 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
726 .dpy_gl_scanout_texture = gd_gl_area_scanout_texture,
727 .dpy_gl_update = gd_gl_area_scanout_flush,
728 };
729
730 #endif /* CONFIG_GTK_GL */
731
732 static const DisplayChangeListenerOps dcl_egl_ops = {
733 .dpy_name = "gtk-egl",
734 .dpy_gfx_update = gd_egl_update,
735 .dpy_gfx_switch = gd_egl_switch,
736 .dpy_gfx_check_format = console_gl_check_format,
737 .dpy_refresh = gd_egl_refresh,
738 .dpy_mouse_set = gd_mouse_set,
739 .dpy_cursor_define = gd_cursor_define,
740
741 .dpy_gl_ctx_create = gd_egl_create_context,
742 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
743 .dpy_gl_ctx_make_current = gd_egl_make_current,
744 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
745 .dpy_gl_scanout_disable = gd_egl_scanout_disable,
746 .dpy_gl_scanout_texture = gd_egl_scanout_texture,
747 .dpy_gl_update = gd_egl_scanout_flush,
748 };
749
750 #endif /* CONFIG_OPENGL */
751
752 /** QEMU Events **/
753
754 static void gd_change_runstate(void *opaque, int running, RunState state)
755 {
756 GtkDisplayState *s = opaque;
757
758 gd_update_caption(s);
759 }
760
761 static void gd_mouse_mode_change(Notifier *notify, void *data)
762 {
763 GtkDisplayState *s;
764 int i;
765
766 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
767 /* release the grab at switching to absolute mode */
768 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
769 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
770 FALSE);
771 }
772 for (i = 0; i < s->nb_vcs; i++) {
773 VirtualConsole *vc = &s->vc[i];
774 gd_update_cursor(vc);
775 }
776 }
777
778 /** GTK Events **/
779
780 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
781 void *opaque)
782 {
783 GtkDisplayState *s = opaque;
784 bool allow_close = true;
785 int i;
786
787 if (s->opts->has_window_close && !s->opts->window_close) {
788 allow_close = false;
789 }
790
791 if (allow_close) {
792 for (i = 0; i < s->nb_vcs; i++) {
793 if (s->vc[i].type != GD_VC_GFX) {
794 continue;
795 }
796 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
797 }
798 qmp_quit(NULL);
799 return FALSE;
800 }
801
802 return TRUE;
803 }
804
805 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
806 {
807 QemuUIInfo info;
808
809 memset(&info, 0, sizeof(info));
810 info.width = width;
811 info.height = height;
812 dpy_set_ui_info(vc->gfx.dcl.con, &info);
813 }
814
815 #if defined(CONFIG_GTK_GL)
816
817 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
818 void *opaque)
819 {
820 VirtualConsole *vc = opaque;
821
822 if (vc->gfx.gls) {
823 gd_gl_area_draw(vc);
824 }
825 return TRUE;
826 }
827
828 static void gd_resize_event(GtkGLArea *area,
829 gint width, gint height, gpointer *opaque)
830 {
831 VirtualConsole *vc = (void *)opaque;
832
833 gd_set_ui_info(vc, width, height);
834 }
835
836 #endif
837
838 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
839 {
840 VirtualConsole *vc = opaque;
841 GtkDisplayState *s = vc->s;
842 int mx, my;
843 int ww, wh;
844 int fbw, fbh;
845
846 #if defined(CONFIG_OPENGL)
847 if (vc->gfx.gls) {
848 if (gtk_use_gl_area) {
849 /* invoke render callback please */
850 return FALSE;
851 } else {
852 gd_egl_draw(vc);
853 return TRUE;
854 }
855 }
856 #endif
857
858 if (!gtk_widget_get_realized(widget)) {
859 return FALSE;
860 }
861 if (!vc->gfx.ds) {
862 return FALSE;
863 }
864
865 fbw = surface_width(vc->gfx.ds);
866 fbh = surface_height(vc->gfx.ds);
867
868 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
869
870 if (s->full_screen) {
871 vc->gfx.scale_x = (double)ww / fbw;
872 vc->gfx.scale_y = (double)wh / fbh;
873 } else if (s->free_scale) {
874 double sx, sy;
875
876 sx = (double)ww / fbw;
877 sy = (double)wh / fbh;
878
879 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
880 }
881
882 fbw *= vc->gfx.scale_x;
883 fbh *= vc->gfx.scale_y;
884
885 mx = my = 0;
886 if (ww > fbw) {
887 mx = (ww - fbw) / 2;
888 }
889 if (wh > fbh) {
890 my = (wh - fbh) / 2;
891 }
892
893 cairo_rectangle(cr, 0, 0, ww, wh);
894
895 /* Optionally cut out the inner area where the pixmap
896 will be drawn. This avoids 'flashing' since we're
897 not double-buffering. Note we're using the undocumented
898 behaviour of drawing the rectangle from right to left
899 to cut out the whole */
900 cairo_rectangle(cr, mx + fbw, my,
901 -1 * fbw, fbh);
902 cairo_fill(cr);
903
904 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
905 cairo_set_source_surface(cr, vc->gfx.surface,
906 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
907 cairo_paint(cr);
908
909 return TRUE;
910 }
911
912 #if !GTK_CHECK_VERSION(3, 0, 0)
913 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
914 void *opaque)
915 {
916 cairo_t *cr;
917 gboolean ret;
918
919 cr = gdk_cairo_create(gtk_widget_get_window(widget));
920 cairo_rectangle(cr,
921 expose->area.x,
922 expose->area.y,
923 expose->area.width,
924 expose->area.height);
925 cairo_clip(cr);
926
927 ret = gd_draw_event(widget, cr, opaque);
928
929 cairo_destroy(cr);
930
931 return ret;
932 }
933 #endif
934
935 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
936 void *opaque)
937 {
938 VirtualConsole *vc = opaque;
939 GtkDisplayState *s = vc->s;
940 int x, y;
941 int mx, my;
942 int fbh, fbw;
943 int ww, wh;
944
945 if (!vc->gfx.ds) {
946 return TRUE;
947 }
948
949 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
950 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
951
952 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
953 &ww, &wh);
954
955 mx = my = 0;
956 if (ww > fbw) {
957 mx = (ww - fbw) / 2;
958 }
959 if (wh > fbh) {
960 my = (wh - fbh) / 2;
961 }
962
963 x = (motion->x - mx) / vc->gfx.scale_x;
964 y = (motion->y - my) / vc->gfx.scale_y;
965
966 if (qemu_input_is_absolute()) {
967 if (x < 0 || y < 0 ||
968 x >= surface_width(vc->gfx.ds) ||
969 y >= surface_height(vc->gfx.ds)) {
970 return TRUE;
971 }
972 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
973 0, surface_width(vc->gfx.ds));
974 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
975 0, surface_height(vc->gfx.ds));
976 qemu_input_event_sync();
977 } else if (s->last_set && s->ptr_owner == vc) {
978 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
979 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
980 qemu_input_event_sync();
981 }
982 s->last_x = x;
983 s->last_y = y;
984 s->last_set = TRUE;
985
986 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
987 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
988 int screen_width, screen_height;
989
990 int x = (int)motion->x_root;
991 int y = (int)motion->y_root;
992
993 #if GTK_CHECK_VERSION(3, 22, 0)
994 {
995 GdkDisplay *dpy = gtk_widget_get_display(widget);
996 GdkWindow *win = gtk_widget_get_window(widget);
997 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
998 GdkRectangle geometry;
999 gdk_monitor_get_geometry(monitor, &geometry);
1000 screen_width = geometry.width;
1001 screen_height = geometry.height;
1002 }
1003 #else
1004 {
1005 screen_width = gdk_screen_get_width(screen);
1006 screen_height = gdk_screen_get_height(screen);
1007 }
1008 #endif
1009
1010 /* In relative mode check to see if client pointer hit
1011 * one of the screen edges, and if so move it back by
1012 * 200 pixels. This is important because the pointer
1013 * in the server doesn't correspond 1-for-1, and so
1014 * may still be only half way across the screen. Without
1015 * this warp, the server pointer would thus appear to hit
1016 * an invisible wall */
1017 if (x == 0) {
1018 x += 200;
1019 }
1020 if (y == 0) {
1021 y += 200;
1022 }
1023 if (x == (screen_width - 1)) {
1024 x -= 200;
1025 }
1026 if (y == (screen_height - 1)) {
1027 y -= 200;
1028 }
1029
1030 if (x != (int)motion->x_root || y != (int)motion->y_root) {
1031 #if GTK_CHECK_VERSION(3, 0, 0)
1032 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
1033 gdk_device_warp(dev, screen, x, y);
1034 #else
1035 GdkDisplay *display = gtk_widget_get_display(widget);
1036 gdk_display_warp_pointer(display, screen, x, y);
1037 #endif
1038 s->last_set = FALSE;
1039 return FALSE;
1040 }
1041 }
1042 return TRUE;
1043 }
1044
1045 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
1046 void *opaque)
1047 {
1048 VirtualConsole *vc = opaque;
1049 GtkDisplayState *s = vc->s;
1050 InputButton btn;
1051
1052 /* implicitly grab the input at the first click in the relative mode */
1053 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
1054 !qemu_input_is_absolute() && s->ptr_owner != vc) {
1055 if (!vc->window) {
1056 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1057 TRUE);
1058 } else {
1059 gd_grab_pointer(vc, "relative-mode-click");
1060 }
1061 return TRUE;
1062 }
1063
1064 if (button->button == 1) {
1065 btn = INPUT_BUTTON_LEFT;
1066 } else if (button->button == 2) {
1067 btn = INPUT_BUTTON_MIDDLE;
1068 } else if (button->button == 3) {
1069 btn = INPUT_BUTTON_RIGHT;
1070 } else if (button->button == 8) {
1071 btn = INPUT_BUTTON_SIDE;
1072 } else if (button->button == 9) {
1073 btn = INPUT_BUTTON_EXTRA;
1074 } else {
1075 return TRUE;
1076 }
1077
1078 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1079 button->type == GDK_BUTTON_PRESS);
1080 qemu_input_event_sync();
1081 return TRUE;
1082 }
1083
1084 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1085 void *opaque)
1086 {
1087 VirtualConsole *vc = opaque;
1088 InputButton btn;
1089
1090 if (scroll->direction == GDK_SCROLL_UP) {
1091 btn = INPUT_BUTTON_WHEEL_UP;
1092 } else if (scroll->direction == GDK_SCROLL_DOWN) {
1093 btn = INPUT_BUTTON_WHEEL_DOWN;
1094 #if GTK_CHECK_VERSION(3, 4, 0)
1095 } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
1096 gdouble delta_x, delta_y;
1097 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
1098 &delta_x, &delta_y)) {
1099 return TRUE;
1100 }
1101 if (delta_y > 0) {
1102 btn = INPUT_BUTTON_WHEEL_DOWN;
1103 } else {
1104 btn = INPUT_BUTTON_WHEEL_UP;
1105 }
1106 #endif
1107 } else {
1108 return TRUE;
1109 }
1110
1111 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
1112 qemu_input_event_sync();
1113 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
1114 qemu_input_event_sync();
1115 return TRUE;
1116 }
1117
1118
1119 static const guint16 *gd_get_keymap(size_t *maplen)
1120 {
1121 GdkDisplay *dpy = gdk_display_get_default();
1122
1123 #ifdef GDK_WINDOWING_X11
1124 if (GDK_IS_X11_DISPLAY(dpy)) {
1125 trace_gd_keymap_windowing("x11");
1126 return qemu_xkeymap_mapping_table(
1127 gdk_x11_display_get_xdisplay(dpy), maplen);
1128 }
1129 #endif
1130
1131 #ifdef GDK_WINDOWING_WAYLAND
1132 if (GDK_IS_WAYLAND_DISPLAY(dpy)) {
1133 trace_gd_keymap_windowing("wayland");
1134 *maplen = qemu_input_map_xorgevdev_to_qcode_len;
1135 return qemu_input_map_xorgevdev_to_qcode;
1136 }
1137 #endif
1138
1139 #ifdef GDK_WINDOWING_WIN32
1140 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1141 trace_gd_keymap_windowing("win32");
1142 *maplen = qemu_input_map_win32_to_qcode_len;
1143 return qemu_input_map_win32_to_qcode;
1144 }
1145 #endif
1146
1147 #ifdef GDK_WINDOWING_QUARTZ
1148 if (GDK_IS_QUARTZ_DISPLAY(dpy)) {
1149 trace_gd_keymap_windowing("quartz");
1150 *maplen = qemu_input_map_osx_to_qcode_len;
1151 return qemu_input_map_osx_to_qcode;
1152 }
1153 #endif
1154
1155 #ifdef GDK_WINDOWING_BROADWAY
1156 if (GDK_IS_BROADWAY_DISPLAY(dpy)) {
1157 trace_gd_keymap_windowing("broadway");
1158 g_warning("experimental: using broadway, x11 virtual keysym\n"
1159 "mapping - with very limited support. See also\n"
1160 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1161 *maplen = qemu_input_map_x11_to_qcode_len;
1162 return qemu_input_map_x11_to_qcode;
1163 }
1164 #endif
1165
1166 g_warning("Unsupported GDK Windowing platform.\n"
1167 "Disabling extended keycode tables.\n"
1168 "Please report to qemu-devel@nongnu.org\n"
1169 "including the following information:\n"
1170 "\n"
1171 " - Operating system\n"
1172 " - GDK Windowing system build\n");
1173 return NULL;
1174 }
1175
1176
1177 static int gd_map_keycode(int scancode)
1178 {
1179 if (!keycode_map) {
1180 return 0;
1181 }
1182 if (scancode > keycode_maplen) {
1183 return 0;
1184 }
1185
1186 return keycode_map[scancode];
1187 }
1188
1189 static gboolean gd_text_key_down(GtkWidget *widget,
1190 GdkEventKey *key, void *opaque)
1191 {
1192 VirtualConsole *vc = opaque;
1193 QemuConsole *con = vc->gfx.dcl.con;
1194
1195 if (key->keyval == GDK_KEY_Delete) {
1196 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1197 } else if (key->length) {
1198 kbd_put_string_console(con, key->string, key->length);
1199 } else {
1200 int qcode = gd_map_keycode(key->hardware_keycode);
1201 kbd_put_qcode_console(con, qcode);
1202 }
1203 return TRUE;
1204 }
1205
1206 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1207 {
1208 VirtualConsole *vc = opaque;
1209 GtkDisplayState *s = vc->s;
1210 int qcode;
1211 int i;
1212
1213 if (s->ignore_keys) {
1214 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1215 return TRUE;
1216 }
1217
1218 #ifdef WIN32
1219 /* on windows, we ought to ignore the reserved key event? */
1220 if (key->hardware_keycode == 0xff)
1221 return false;
1222 #endif
1223
1224 if (key->keyval == GDK_KEY_Pause
1225 #ifdef G_OS_WIN32
1226 /* for some reason GDK does not fill keyval for VK_PAUSE
1227 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1228 */
1229 || key->hardware_keycode == VK_PAUSE
1230 #endif
1231 ) {
1232 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1233 key->type == GDK_KEY_PRESS);
1234 return TRUE;
1235 }
1236
1237 qcode = gd_map_keycode(key->hardware_keycode);
1238
1239 trace_gd_key_event(vc->label, key->hardware_keycode, qcode,
1240 (key->type == GDK_KEY_PRESS) ? "down" : "up");
1241
1242 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1243 if (qcode == modifier_keycode[i]) {
1244 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1245 }
1246 }
1247
1248 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, qcode,
1249 key->type == GDK_KEY_PRESS);
1250
1251 return TRUE;
1252 }
1253
1254 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1255 {
1256 if (event->type == GDK_MOTION_NOTIFY) {
1257 return gd_motion_event(widget, &event->motion, opaque);
1258 }
1259 return FALSE;
1260 }
1261
1262 /** Window Menu Actions **/
1263
1264 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1265 {
1266 GtkDisplayState *s = opaque;
1267
1268 if (s->external_pause_update) {
1269 return;
1270 }
1271 if (runstate_is_running()) {
1272 qmp_stop(NULL);
1273 } else {
1274 qmp_cont(NULL);
1275 }
1276 }
1277
1278 static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1279 {
1280 qmp_system_reset(NULL);
1281 }
1282
1283 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1284 {
1285 qmp_system_powerdown(NULL);
1286 }
1287
1288 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1289 {
1290 qmp_quit(NULL);
1291 }
1292
1293 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1294 {
1295 GtkDisplayState *s = opaque;
1296 VirtualConsole *vc = gd_vc_find_by_menu(s);
1297 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1298 gint page;
1299
1300 gtk_release_modifiers(s);
1301 if (vc) {
1302 page = gtk_notebook_page_num(nb, vc->tab_item);
1303 gtk_notebook_set_current_page(nb, page);
1304 gtk_widget_grab_focus(vc->focus);
1305 }
1306 s->ignore_keys = false;
1307 }
1308
1309 static void gd_accel_switch_vc(void *opaque)
1310 {
1311 VirtualConsole *vc = opaque;
1312
1313 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1314 #if !GTK_CHECK_VERSION(3, 0, 0)
1315 /* GTK2 sends the accel key to the target console - ignore this until */
1316 vc->s->ignore_keys = true;
1317 #endif
1318 }
1319
1320 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1321 {
1322 GtkDisplayState *s = opaque;
1323 VirtualConsole *vc = gd_vc_find_current(s);
1324
1325 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1326 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1327 } else {
1328 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1329 }
1330 gd_update_windowsize(vc);
1331 }
1332
1333 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1334 void *opaque)
1335 {
1336 VirtualConsole *vc = opaque;
1337 GtkDisplayState *s = vc->s;
1338
1339 gtk_widget_set_sensitive(vc->menu_item, true);
1340 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1341 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1342 vc->tab_item, vc->label);
1343 gtk_widget_destroy(vc->window);
1344 vc->window = NULL;
1345 return TRUE;
1346 }
1347
1348 static gboolean gd_win_grab(void *opaque)
1349 {
1350 VirtualConsole *vc = opaque;
1351
1352 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1353 if (vc->s->ptr_owner) {
1354 gd_ungrab_pointer(vc->s);
1355 } else {
1356 gd_grab_pointer(vc, "user-request-detached-tab");
1357 }
1358 return TRUE;
1359 }
1360
1361 static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1362 {
1363 GtkDisplayState *s = opaque;
1364 VirtualConsole *vc = gd_vc_find_current(s);
1365
1366 if (vc->type == GD_VC_GFX &&
1367 qemu_console_is_graphic(vc->gfx.dcl.con)) {
1368 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1369 FALSE);
1370 }
1371 if (!vc->window) {
1372 gtk_widget_set_sensitive(vc->menu_item, false);
1373 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1374 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1375
1376 g_signal_connect(vc->window, "delete-event",
1377 G_CALLBACK(gd_tab_window_close), vc);
1378 gtk_widget_show_all(vc->window);
1379
1380 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1381 GtkAccelGroup *ag = gtk_accel_group_new();
1382 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1383
1384 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1385 vc, NULL);
1386 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1387 }
1388
1389 gd_update_geometry_hints(vc);
1390 gd_update_caption(s);
1391 }
1392 }
1393
1394 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1395 {
1396 GtkDisplayState *s = opaque;
1397 VirtualConsole *vc = gd_vc_find_current(s);
1398
1399 if (!s->full_screen) {
1400 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1401 gtk_widget_hide(s->menu_bar);
1402 if (vc->type == GD_VC_GFX) {
1403 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1404 }
1405 gtk_window_fullscreen(GTK_WINDOW(s->window));
1406 s->full_screen = TRUE;
1407 } else {
1408 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1409 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1410 gtk_widget_show(s->menu_bar);
1411 s->full_screen = FALSE;
1412 if (vc->type == GD_VC_GFX) {
1413 vc->gfx.scale_x = 1.0;
1414 vc->gfx.scale_y = 1.0;
1415 gd_update_windowsize(vc);
1416 }
1417 }
1418
1419 gd_update_cursor(vc);
1420 }
1421
1422 static void gd_accel_full_screen(void *opaque)
1423 {
1424 GtkDisplayState *s = opaque;
1425 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1426 }
1427
1428 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1429 {
1430 GtkDisplayState *s = opaque;
1431 VirtualConsole *vc = gd_vc_find_current(s);
1432
1433 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1434 FALSE);
1435
1436 vc->gfx.scale_x += VC_SCALE_STEP;
1437 vc->gfx.scale_y += VC_SCALE_STEP;
1438
1439 gd_update_windowsize(vc);
1440 }
1441
1442 static void gd_accel_zoom_in(void *opaque)
1443 {
1444 GtkDisplayState *s = opaque;
1445 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item));
1446 }
1447
1448 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1449 {
1450 GtkDisplayState *s = opaque;
1451 VirtualConsole *vc = gd_vc_find_current(s);
1452
1453 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1454 FALSE);
1455
1456 vc->gfx.scale_x -= VC_SCALE_STEP;
1457 vc->gfx.scale_y -= VC_SCALE_STEP;
1458
1459 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1460 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1461
1462 gd_update_windowsize(vc);
1463 }
1464
1465 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1466 {
1467 GtkDisplayState *s = opaque;
1468 VirtualConsole *vc = gd_vc_find_current(s);
1469
1470 vc->gfx.scale_x = 1.0;
1471 vc->gfx.scale_y = 1.0;
1472
1473 gd_update_windowsize(vc);
1474 }
1475
1476 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1477 {
1478 GtkDisplayState *s = opaque;
1479 VirtualConsole *vc = gd_vc_find_current(s);
1480
1481 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1482 s->free_scale = TRUE;
1483 } else {
1484 s->free_scale = FALSE;
1485 vc->gfx.scale_x = 1.0;
1486 vc->gfx.scale_y = 1.0;
1487 }
1488
1489 gd_update_windowsize(vc);
1490 gd_update_full_redraw(vc);
1491 }
1492
1493 #if GTK_CHECK_VERSION(3, 20, 0)
1494 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1495 {
1496 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1497 GdkSeat *seat = gdk_display_get_default_seat(display);
1498 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1499 GdkSeatCapabilities caps = 0;
1500 GdkCursor *cursor = NULL;
1501
1502 if (kbd) {
1503 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1504 }
1505 if (ptr) {
1506 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1507 cursor = vc->s->null_cursor;
1508 }
1509
1510 if (caps) {
1511 gdk_seat_grab(seat, window, caps, false, cursor,
1512 NULL, NULL, NULL);
1513 } else {
1514 gdk_seat_ungrab(seat);
1515 }
1516 }
1517 #elif GTK_CHECK_VERSION(3, 0, 0)
1518 static void gd_grab_devices(VirtualConsole *vc, bool grab,
1519 GdkInputSource source, GdkEventMask mask,
1520 GdkCursor *cursor)
1521 {
1522 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1523 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1524 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1525 GList *tmp = devs;
1526
1527 for (tmp = devs; tmp; tmp = tmp->next) {
1528 GdkDevice *dev = tmp->data;
1529 if (gdk_device_get_source(dev) != source) {
1530 continue;
1531 }
1532 if (grab) {
1533 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1534 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1535 mask, cursor, GDK_CURRENT_TIME);
1536 } else {
1537 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1538 }
1539 }
1540 g_list_free(devs);
1541 }
1542 #endif
1543
1544 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1545 {
1546 if (vc->s->kbd_owner) {
1547 if (vc->s->kbd_owner == vc) {
1548 return;
1549 } else {
1550 gd_ungrab_keyboard(vc->s);
1551 }
1552 }
1553
1554 #if GTK_CHECK_VERSION(3, 20, 0)
1555 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1556 #elif GTK_CHECK_VERSION(3, 0, 0)
1557 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1558 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1559 NULL);
1560 #else
1561 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1562 FALSE,
1563 GDK_CURRENT_TIME);
1564 #endif
1565 vc->s->kbd_owner = vc;
1566 gd_update_caption(vc->s);
1567 trace_gd_grab(vc->label, "kbd", reason);
1568 }
1569
1570 static void gd_ungrab_keyboard(GtkDisplayState *s)
1571 {
1572 VirtualConsole *vc = s->kbd_owner;
1573
1574 if (vc == NULL) {
1575 return;
1576 }
1577 s->kbd_owner = NULL;
1578
1579 #if GTK_CHECK_VERSION(3, 20, 0)
1580 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1581 #elif GTK_CHECK_VERSION(3, 0, 0)
1582 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1583 #else
1584 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1585 #endif
1586 gd_update_caption(s);
1587 trace_gd_ungrab(vc->label, "kbd");
1588 }
1589
1590 static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1591 {
1592 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1593
1594 if (vc->s->ptr_owner) {
1595 if (vc->s->ptr_owner == vc) {
1596 return;
1597 } else {
1598 gd_ungrab_pointer(vc->s);
1599 }
1600 }
1601
1602 #if GTK_CHECK_VERSION(3, 20, 0)
1603 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1604 gdk_device_get_position(gd_get_pointer(display),
1605 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1606 #elif GTK_CHECK_VERSION(3, 0, 0)
1607 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1608 GDK_POINTER_MOTION_MASK |
1609 GDK_BUTTON_PRESS_MASK |
1610 GDK_BUTTON_RELEASE_MASK |
1611 GDK_BUTTON_MOTION_MASK |
1612 GDK_SCROLL_MASK,
1613 vc->s->null_cursor);
1614 gdk_device_get_position(gd_get_pointer(display),
1615 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1616 #else
1617 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1618 FALSE, /* All events to come to our window directly */
1619 GDK_POINTER_MOTION_MASK |
1620 GDK_BUTTON_PRESS_MASK |
1621 GDK_BUTTON_RELEASE_MASK |
1622 GDK_BUTTON_MOTION_MASK |
1623 GDK_SCROLL_MASK,
1624 NULL, /* Allow cursor to move over entire desktop */
1625 vc->s->null_cursor,
1626 GDK_CURRENT_TIME);
1627 gdk_display_get_pointer(display, NULL,
1628 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1629 #endif
1630 vc->s->ptr_owner = vc;
1631 gd_update_caption(vc->s);
1632 trace_gd_grab(vc->label, "ptr", reason);
1633 }
1634
1635 static void gd_ungrab_pointer(GtkDisplayState *s)
1636 {
1637 VirtualConsole *vc = s->ptr_owner;
1638 GdkDisplay *display;
1639
1640 if (vc == NULL) {
1641 return;
1642 }
1643 s->ptr_owner = NULL;
1644
1645 display = gtk_widget_get_display(vc->gfx.drawing_area);
1646 #if GTK_CHECK_VERSION(3, 20, 0)
1647 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1648 gdk_device_warp(gd_get_pointer(display),
1649 gtk_widget_get_screen(vc->gfx.drawing_area),
1650 vc->s->grab_x_root, vc->s->grab_y_root);
1651 #elif GTK_CHECK_VERSION(3, 0, 0)
1652 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1653 gdk_device_warp(gd_get_pointer(display),
1654 gtk_widget_get_screen(vc->gfx.drawing_area),
1655 vc->s->grab_x_root, vc->s->grab_y_root);
1656 #else
1657 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1658 gdk_display_warp_pointer(display,
1659 gtk_widget_get_screen(vc->gfx.drawing_area),
1660 vc->s->grab_x_root, vc->s->grab_y_root);
1661 #endif
1662 gd_update_caption(s);
1663 trace_gd_ungrab(vc->label, "ptr");
1664 }
1665
1666 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1667 {
1668 GtkDisplayState *s = opaque;
1669 VirtualConsole *vc = gd_vc_find_current(s);
1670
1671 if (gd_is_grab_active(s)) {
1672 gd_grab_keyboard(vc, "user-request-main-window");
1673 gd_grab_pointer(vc, "user-request-main-window");
1674 } else {
1675 gd_ungrab_keyboard(s);
1676 gd_ungrab_pointer(s);
1677 }
1678
1679 gd_update_cursor(vc);
1680 }
1681
1682 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1683 gpointer data)
1684 {
1685 GtkDisplayState *s = data;
1686 VirtualConsole *vc;
1687 gboolean on_vga;
1688
1689 if (!gtk_widget_get_realized(s->notebook)) {
1690 return;
1691 }
1692
1693 #ifdef VTE_RESIZE_HACK
1694 vc = gd_vc_find_current(s);
1695 if (vc && vc->type == GD_VC_VTE) {
1696 gtk_widget_hide(vc->vte.terminal);
1697 }
1698 #endif
1699 vc = gd_vc_find_by_page(s, arg2);
1700 if (!vc) {
1701 return;
1702 }
1703 #ifdef VTE_RESIZE_HACK
1704 if (vc->type == GD_VC_VTE) {
1705 gtk_widget_show(vc->vte.terminal);
1706 }
1707 #endif
1708 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1709 TRUE);
1710 on_vga = (vc->type == GD_VC_GFX &&
1711 qemu_console_is_graphic(vc->gfx.dcl.con));
1712 if (!on_vga) {
1713 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1714 FALSE);
1715 } else if (s->full_screen) {
1716 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1717 TRUE);
1718 }
1719 gtk_widget_set_sensitive(s->grab_item, on_vga);
1720 #ifdef CONFIG_VTE
1721 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1722 #endif
1723
1724 gd_update_windowsize(vc);
1725 gd_update_cursor(vc);
1726 }
1727
1728 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1729 gpointer opaque)
1730 {
1731 VirtualConsole *vc = opaque;
1732 GtkDisplayState *s = vc->s;
1733
1734 if (gd_grab_on_hover(s)) {
1735 gd_grab_keyboard(vc, "grab-on-hover");
1736 }
1737 return TRUE;
1738 }
1739
1740 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1741 gpointer opaque)
1742 {
1743 VirtualConsole *vc = opaque;
1744 GtkDisplayState *s = vc->s;
1745
1746 if (gd_grab_on_hover(s)) {
1747 gd_ungrab_keyboard(s);
1748 }
1749 return TRUE;
1750 }
1751
1752 static gboolean gd_focus_out_event(GtkWidget *widget,
1753 GdkEventCrossing *crossing, gpointer opaque)
1754 {
1755 VirtualConsole *vc = opaque;
1756 GtkDisplayState *s = vc->s;
1757
1758 gtk_release_modifiers(s);
1759 return TRUE;
1760 }
1761
1762 static gboolean gd_configure(GtkWidget *widget,
1763 GdkEventConfigure *cfg, gpointer opaque)
1764 {
1765 VirtualConsole *vc = opaque;
1766
1767 gd_set_ui_info(vc, cfg->width, cfg->height);
1768 return FALSE;
1769 }
1770
1771 /** Virtual Console Callbacks **/
1772
1773 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1774 int idx, GSList *group, GtkWidget *view_menu)
1775 {
1776 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1777 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1778 HOTKEY_MODIFIERS, 0,
1779 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1780 #if GTK_CHECK_VERSION(3, 8, 0)
1781 gtk_accel_label_set_accel(
1782 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1783 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1784 #endif
1785
1786 g_signal_connect(vc->menu_item, "activate",
1787 G_CALLBACK(gd_menu_switch_vc), s);
1788 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1789
1790 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1791 return group;
1792 }
1793
1794 #if defined(CONFIG_VTE)
1795 static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1796 {
1797 GtkDisplayState *s = opaque;
1798 VirtualConsole *vc = gd_vc_find_current(s);
1799
1800 #if VTE_CHECK_VERSION(0, 50, 0)
1801 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc->vte.terminal),
1802 VTE_FORMAT_TEXT);
1803 #else
1804 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1805 #endif
1806 }
1807
1808 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1809 {
1810 VirtualConsole *vc = opaque;
1811
1812 if (gtk_adjustment_get_upper(adjustment) >
1813 gtk_adjustment_get_page_size(adjustment)) {
1814 gtk_widget_show(vc->vte.scrollbar);
1815 } else {
1816 gtk_widget_hide(vc->vte.scrollbar);
1817 }
1818 }
1819
1820 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1821 {
1822 VCChardev *vcd = VC_CHARDEV(chr);
1823 VirtualConsole *vc = vcd->console;
1824
1825 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1826 return len;
1827 }
1828
1829 static void gd_vc_chr_set_echo(Chardev *chr, bool echo)
1830 {
1831 VCChardev *vcd = VC_CHARDEV(chr);
1832 VirtualConsole *vc = vcd->console;
1833
1834 if (vc) {
1835 vc->vte.echo = echo;
1836 } else {
1837 vcd->echo = echo;
1838 }
1839 }
1840
1841 static int nb_vcs;
1842 static Chardev *vcs[MAX_VCS];
1843 static void gd_vc_open(Chardev *chr,
1844 ChardevBackend *backend,
1845 bool *be_opened,
1846 Error **errp)
1847 {
1848 if (nb_vcs == MAX_VCS) {
1849 error_setg(errp, "Maximum number of consoles reached");
1850 return;
1851 }
1852
1853 vcs[nb_vcs++] = chr;
1854
1855 /* console/chardev init sometimes completes elsewhere in a 2nd
1856 * stage, so defer OPENED events until they are fully initialized
1857 */
1858 *be_opened = false;
1859 }
1860
1861 static void char_gd_vc_class_init(ObjectClass *oc, void *data)
1862 {
1863 ChardevClass *cc = CHARDEV_CLASS(oc);
1864
1865 cc->parse = qemu_chr_parse_vc;
1866 cc->open = gd_vc_open;
1867 cc->chr_write = gd_vc_chr_write;
1868 cc->chr_set_echo = gd_vc_chr_set_echo;
1869 }
1870
1871 static const TypeInfo char_gd_vc_type_info = {
1872 .name = TYPE_CHARDEV_VC,
1873 .parent = TYPE_CHARDEV,
1874 .instance_size = sizeof(VCChardev),
1875 .class_init = char_gd_vc_class_init,
1876 };
1877
1878 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1879 gpointer user_data)
1880 {
1881 VirtualConsole *vc = user_data;
1882
1883 if (vc->vte.echo) {
1884 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1885 int i;
1886 for (i = 0; i < size; i++) {
1887 uint8_t c = text[i];
1888 if (c >= 128 || isprint(c)) {
1889 /* 8-bit characters are considered printable. */
1890 vte_terminal_feed(term, &text[i], 1);
1891 } else if (c == '\r' || c == '\n') {
1892 vte_terminal_feed(term, "\r\n", 2);
1893 } else {
1894 char ctrl[2] = { '^', 0};
1895 ctrl[1] = text[i] ^ 64;
1896 vte_terminal_feed(term, ctrl, 2);
1897 }
1898 }
1899 }
1900
1901 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
1902 return TRUE;
1903 }
1904
1905 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1906 Chardev *chr, int idx,
1907 GSList *group, GtkWidget *view_menu)
1908 {
1909 char buffer[32];
1910 GtkWidget *box;
1911 GtkWidget *scrollbar;
1912 GtkAdjustment *vadjustment;
1913 VCChardev *vcd = VC_CHARDEV(chr);
1914
1915 vc->s = s;
1916 vc->vte.echo = vcd->echo;
1917 vc->vte.chr = chr;
1918 vcd->console = vc;
1919
1920 snprintf(buffer, sizeof(buffer), "vc%d", idx);
1921 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1922 ? vc->vte.chr->label : buffer);
1923 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1924
1925 vc->vte.terminal = vte_terminal_new();
1926 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1927
1928 /* The documentation says that the default is UTF-8, but actually it is
1929 * 7-bit ASCII at least in VTE 0.38.
1930 */
1931 #if VTE_CHECK_VERSION(0, 38, 0)
1932 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1933 #else
1934 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1935 #endif
1936
1937 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1938 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1939 VC_TERM_X_MIN, VC_TERM_Y_MIN);
1940
1941 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1942 vadjustment = gtk_scrollable_get_vadjustment
1943 (GTK_SCROLLABLE(vc->vte.terminal));
1944 #else
1945 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1946 #endif
1947
1948 #if GTK_CHECK_VERSION(3, 0, 0)
1949 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1950 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1951 #else
1952 box = gtk_hbox_new(false, 2);
1953 scrollbar = gtk_vscrollbar_new(vadjustment);
1954 #endif
1955
1956 gtk_box_pack_end(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1957 gtk_box_pack_end(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1958
1959 vc->vte.box = box;
1960 vc->vte.scrollbar = scrollbar;
1961
1962 g_signal_connect(vadjustment, "changed",
1963 G_CALLBACK(gd_vc_adjustment_changed), vc);
1964
1965 vc->type = GD_VC_VTE;
1966 vc->tab_item = box;
1967 vc->focus = vc->vte.terminal;
1968 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1969 gtk_label_new(vc->label));
1970
1971 qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
1972
1973 return group;
1974 }
1975
1976 static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1977 GtkWidget *view_menu)
1978 {
1979 int i;
1980
1981 for (i = 0; i < nb_vcs; i++) {
1982 VirtualConsole *vc = &s->vc[s->nb_vcs];
1983 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1984 s->nb_vcs++;
1985 }
1986 }
1987 #endif /* CONFIG_VTE */
1988
1989 /** Window Creation **/
1990
1991 static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1992 {
1993 #if GTK_CHECK_VERSION(3, 0, 0)
1994 g_signal_connect(vc->gfx.drawing_area, "draw",
1995 G_CALLBACK(gd_draw_event), vc);
1996 #if defined(CONFIG_GTK_GL)
1997 if (gtk_use_gl_area) {
1998 /* wire up GtkGlArea events */
1999 g_signal_connect(vc->gfx.drawing_area, "render",
2000 G_CALLBACK(gd_render_event), vc);
2001 g_signal_connect(vc->gfx.drawing_area, "resize",
2002 G_CALLBACK(gd_resize_event), vc);
2003 }
2004 #endif
2005 #else
2006 g_signal_connect(vc->gfx.drawing_area, "expose-event",
2007 G_CALLBACK(gd_expose_event), vc);
2008 #endif
2009 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
2010 g_signal_connect(vc->gfx.drawing_area, "event",
2011 G_CALLBACK(gd_event), vc);
2012 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
2013 G_CALLBACK(gd_button_event), vc);
2014 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
2015 G_CALLBACK(gd_button_event), vc);
2016 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
2017 G_CALLBACK(gd_scroll_event), vc);
2018 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2019 G_CALLBACK(gd_key_event), vc);
2020 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
2021 G_CALLBACK(gd_key_event), vc);
2022
2023 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
2024 G_CALLBACK(gd_enter_event), vc);
2025 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
2026 G_CALLBACK(gd_leave_event), vc);
2027 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
2028 G_CALLBACK(gd_focus_out_event), vc);
2029 g_signal_connect(vc->gfx.drawing_area, "configure-event",
2030 G_CALLBACK(gd_configure), vc);
2031 } else {
2032 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
2033 G_CALLBACK(gd_text_key_down), vc);
2034 }
2035 }
2036
2037 static void gd_connect_signals(GtkDisplayState *s)
2038 {
2039 g_signal_connect(s->show_tabs_item, "activate",
2040 G_CALLBACK(gd_menu_show_tabs), s);
2041 g_signal_connect(s->untabify_item, "activate",
2042 G_CALLBACK(gd_menu_untabify), s);
2043
2044 g_signal_connect(s->window, "delete-event",
2045 G_CALLBACK(gd_window_close), s);
2046
2047 g_signal_connect(s->pause_item, "activate",
2048 G_CALLBACK(gd_menu_pause), s);
2049 g_signal_connect(s->reset_item, "activate",
2050 G_CALLBACK(gd_menu_reset), s);
2051 g_signal_connect(s->powerdown_item, "activate",
2052 G_CALLBACK(gd_menu_powerdown), s);
2053 g_signal_connect(s->quit_item, "activate",
2054 G_CALLBACK(gd_menu_quit), s);
2055 #if defined(CONFIG_VTE)
2056 g_signal_connect(s->copy_item, "activate",
2057 G_CALLBACK(gd_menu_copy), s);
2058 #endif
2059 g_signal_connect(s->full_screen_item, "activate",
2060 G_CALLBACK(gd_menu_full_screen), s);
2061 g_signal_connect(s->zoom_in_item, "activate",
2062 G_CALLBACK(gd_menu_zoom_in), s);
2063 g_signal_connect(s->zoom_out_item, "activate",
2064 G_CALLBACK(gd_menu_zoom_out), s);
2065 g_signal_connect(s->zoom_fixed_item, "activate",
2066 G_CALLBACK(gd_menu_zoom_fixed), s);
2067 g_signal_connect(s->zoom_fit_item, "activate",
2068 G_CALLBACK(gd_menu_zoom_fit), s);
2069 g_signal_connect(s->grab_item, "activate",
2070 G_CALLBACK(gd_menu_grab_input), s);
2071 g_signal_connect(s->notebook, "switch-page",
2072 G_CALLBACK(gd_change_page), s);
2073 }
2074
2075 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
2076 {
2077 GtkWidget *machine_menu;
2078 GtkWidget *separator;
2079
2080 machine_menu = gtk_menu_new();
2081 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
2082
2083 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2084 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
2085
2086 separator = gtk_separator_menu_item_new();
2087 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2088
2089 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
2090 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
2091
2092 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2093 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
2094
2095 separator = gtk_separator_menu_item_new();
2096 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
2097
2098 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
2099 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
2100 "<QEMU>/Machine/Quit");
2101 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2102 GDK_KEY_q, HOTKEY_MODIFIERS);
2103 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
2104
2105 return machine_menu;
2106 }
2107
2108 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
2109 QemuConsole *con, int idx,
2110 GSList *group, GtkWidget *view_menu)
2111 {
2112 vc->label = qemu_console_get_label(con);
2113 vc->s = s;
2114 vc->gfx.scale_x = 1.0;
2115 vc->gfx.scale_y = 1.0;
2116
2117 #if defined(CONFIG_OPENGL)
2118 if (display_opengl) {
2119 #if defined(CONFIG_GTK_GL)
2120 if (gtk_use_gl_area) {
2121 vc->gfx.drawing_area = gtk_gl_area_new();
2122 vc->gfx.dcl.ops = &dcl_gl_area_ops;
2123 } else
2124 #endif /* CONFIG_GTK_GL */
2125 {
2126 vc->gfx.drawing_area = gtk_drawing_area_new();
2127 /*
2128 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2129 * It is required for opengl rendering on X11 though. A
2130 * proper replacement (native opengl support) is only
2131 * available in 3.16+. Silence the warning if possible.
2132 */
2133 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2134 #pragma GCC diagnostic push
2135 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2136 #endif
2137 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
2138 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2139 #pragma GCC diagnostic pop
2140 #endif
2141 vc->gfx.dcl.ops = &dcl_egl_ops;
2142 }
2143 } else
2144 #endif
2145 {
2146 vc->gfx.drawing_area = gtk_drawing_area_new();
2147 vc->gfx.dcl.ops = &dcl_ops;
2148 }
2149
2150
2151 gtk_widget_add_events(vc->gfx.drawing_area,
2152 GDK_POINTER_MOTION_MASK |
2153 GDK_BUTTON_PRESS_MASK |
2154 GDK_BUTTON_RELEASE_MASK |
2155 GDK_BUTTON_MOTION_MASK |
2156 GDK_ENTER_NOTIFY_MASK |
2157 GDK_LEAVE_NOTIFY_MASK |
2158 GDK_SCROLL_MASK |
2159 GDK_KEY_PRESS_MASK);
2160 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2161
2162 vc->type = GD_VC_GFX;
2163 vc->tab_item = vc->gfx.drawing_area;
2164 vc->focus = vc->gfx.drawing_area;
2165 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2166 vc->tab_item, gtk_label_new(vc->label));
2167
2168 vc->gfx.dcl.con = con;
2169 register_displaychangelistener(&vc->gfx.dcl);
2170
2171 gd_connect_vc_gfx_signals(vc);
2172 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2173
2174 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2175 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2176 s->free_scale = true;
2177 }
2178
2179 return group;
2180 }
2181
2182 static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
2183 {
2184 GSList *group = NULL;
2185 GtkWidget *view_menu;
2186 GtkWidget *separator;
2187 QemuConsole *con;
2188 int vc;
2189
2190 view_menu = gtk_menu_new();
2191 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
2192
2193 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2194
2195 #if defined(CONFIG_VTE)
2196 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2197 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2198 #endif
2199
2200 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2201 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2202 #if GTK_CHECK_VERSION(3, 8, 0)
2203 gtk_accel_label_set_accel(
2204 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2205 GDK_KEY_f, HOTKEY_MODIFIERS);
2206 #endif
2207 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2208
2209 separator = gtk_separator_menu_item_new();
2210 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2211
2212 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2213 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2214 "<QEMU>/View/Zoom In");
2215 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2216 HOTKEY_MODIFIERS);
2217 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0,
2218 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL));
2219 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2220
2221 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2222 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2223 "<QEMU>/View/Zoom Out");
2224 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2225 HOTKEY_MODIFIERS);
2226 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2227
2228 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2229 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2230 "<QEMU>/View/Zoom Fixed");
2231 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2232 HOTKEY_MODIFIERS);
2233 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2234
2235 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2236 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2237
2238 separator = gtk_separator_menu_item_new();
2239 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2240
2241 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2242 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
2243
2244 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2245 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2246 "<QEMU>/View/Grab Input");
2247 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2248 HOTKEY_MODIFIERS);
2249 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2250
2251 separator = gtk_separator_menu_item_new();
2252 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2253
2254 /* gfx */
2255 for (vc = 0;; vc++) {
2256 con = qemu_console_lookup_by_index(vc);
2257 if (!con) {
2258 break;
2259 }
2260 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2261 vc, group, view_menu);
2262 s->nb_vcs++;
2263 }
2264
2265 #if defined(CONFIG_VTE)
2266 /* vte */
2267 gd_vcs_init(s, group, view_menu);
2268 #endif
2269
2270 separator = gtk_separator_menu_item_new();
2271 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
2272
2273 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2274 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
2275
2276 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2277 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2278
2279 return view_menu;
2280 }
2281
2282 static void gd_create_menus(GtkDisplayState *s)
2283 {
2284 s->accel_group = gtk_accel_group_new();
2285 s->machine_menu = gd_create_menu_machine(s);
2286 s->view_menu = gd_create_menu_view(s);
2287
2288 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2289 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2290 s->machine_menu);
2291 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
2292
2293 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
2294 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2295 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
2296
2297 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2298 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
2299 }
2300
2301
2302 static gboolean gtkinit;
2303
2304 static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)
2305 {
2306 VirtualConsole *vc;
2307
2308 GtkDisplayState *s = g_malloc0(sizeof(*s));
2309 char *filename;
2310 GdkDisplay *window_display;
2311
2312 if (!gtkinit) {
2313 fprintf(stderr, "gtk initialization failed\n");
2314 exit(1);
2315 }
2316 assert(opts->type == DISPLAY_TYPE_GTK);
2317 s->opts = opts;
2318
2319 #if !GTK_CHECK_VERSION(3, 0, 0)
2320 g_printerr("Running QEMU with GTK 2.x is deprecated, and will be removed\n"
2321 "in a future release. Please switch to GTK 3.x instead\n");
2322 #endif
2323
2324 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2325 #if GTK_CHECK_VERSION(3, 2, 0)
2326 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2327 #else
2328 s->vbox = gtk_vbox_new(FALSE, 0);
2329 #endif
2330 s->notebook = gtk_notebook_new();
2331 s->menu_bar = gtk_menu_bar_new();
2332
2333 s->free_scale = FALSE;
2334
2335 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2336 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2337 * printable, but without changing any of the character classes to make
2338 * sure that we don't accidentally break implicit assumptions. */
2339 setlocale(LC_MESSAGES, "");
2340 setlocale(LC_CTYPE, "C.UTF-8");
2341 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2342 textdomain("qemu");
2343
2344 window_display = gtk_widget_get_display(s->window);
2345 s->null_cursor = gdk_cursor_new_for_display(window_display,
2346 GDK_BLANK_CURSOR);
2347
2348 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2349 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2350 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2351
2352 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
2353 if (filename) {
2354 GError *error = NULL;
2355 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2356 if (pixbuf) {
2357 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2358 } else {
2359 g_error_free(error);
2360 }
2361 g_free(filename);
2362 }
2363
2364 gd_create_menus(s);
2365
2366 gd_connect_signals(s);
2367
2368 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2369 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2370
2371 gd_update_caption(s);
2372
2373 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2374 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2375
2376 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2377
2378 gtk_widget_show_all(s->window);
2379
2380 #ifdef VTE_RESIZE_HACK
2381 {
2382 VirtualConsole *cur = gd_vc_find_current(s);
2383 if (cur) {
2384 int i;
2385
2386 for (i = 0; i < s->nb_vcs; i++) {
2387 VirtualConsole *vc = &s->vc[i];
2388 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2389 gtk_widget_hide(vc->vte.terminal);
2390 }
2391 }
2392 gd_update_windowsize(cur);
2393 }
2394 }
2395 #endif
2396
2397 vc = gd_vc_find_current(s);
2398 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
2399 #ifdef CONFIG_VTE
2400 gtk_widget_set_sensitive(s->copy_item,
2401 vc && vc->type == GD_VC_VTE);
2402 #endif
2403
2404 if (opts->has_full_screen &&
2405 opts->full_screen) {
2406 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2407 }
2408 if (opts->u.gtk.has_grab_on_hover &&
2409 opts->u.gtk.grab_on_hover) {
2410 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2411 }
2412 }
2413
2414 static void early_gtk_display_init(DisplayOptions *opts)
2415 {
2416 /* The QEMU code relies on the assumption that it's always run in
2417 * the C locale. Therefore it is not prepared to deal with
2418 * operations that produce different results depending on the
2419 * locale, such as printf's formatting of decimal numbers, and
2420 * possibly others.
2421 *
2422 * Since GTK+ calls setlocale() by default -importing the locale
2423 * settings from the environment- we must prevent it from doing so
2424 * using gtk_disable_setlocale().
2425 *
2426 * QEMU's GTK+ UI, however, _does_ have translations for some of
2427 * the menu items. As a trade-off between a functionally correct
2428 * QEMU and a fully internationalized UI we support importing
2429 * LC_MESSAGES from the environment (see the setlocale() call
2430 * earlier in this file). This allows us to display translated
2431 * messages leaving everything else untouched.
2432 */
2433 gtk_disable_setlocale();
2434 gtkinit = gtk_init_check(NULL, NULL);
2435 if (!gtkinit) {
2436 /* don't exit yet, that'll break -help */
2437 return;
2438 }
2439
2440 assert(opts->type == DISPLAY_TYPE_GTK);
2441 if (opts->has_gl && opts->gl) {
2442 #if defined(CONFIG_OPENGL)
2443 #if defined(CONFIG_GTK_GL) && defined(GDK_WINDOWING_WAYLAND)
2444 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2445 gtk_use_gl_area = true;
2446 gtk_gl_area_init();
2447 }
2448 #endif
2449 {
2450 gtk_egl_init();
2451 }
2452 #endif
2453 }
2454
2455 keycode_map = gd_get_keymap(&keycode_maplen);
2456
2457 #if defined(CONFIG_VTE)
2458 type_register(&char_gd_vc_type_info);
2459 #endif
2460 }
2461
2462 static QemuDisplay qemu_display_gtk = {
2463 .type = DISPLAY_TYPE_GTK,
2464 .early_init = early_gtk_display_init,
2465 .init = gtk_display_init,
2466 };
2467
2468 static void register_gtk(void)
2469 {
2470 qemu_display_register(&qemu_display_gtk);
2471 }
2472
2473 type_init(register_gtk);