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