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