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