]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk.c
gtk: check for existing grabs in gd_grab_{pointer,keyboard}
[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
2884cf5b
GH
168static void gd_grab_pointer(VirtualConsole *vc);
169static void gd_ungrab_pointer(GtkDisplayState *s);
aa4f4058
GH
170static void gd_grab_keyboard(VirtualConsole *vc);
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 {
2884cf5b 858 gd_grab_pointer(vc);
2884cf5b
GH
859 gd_update_caption(s);
860 }
800b0e81
TI
861 return TRUE;
862 }
863
a4ccabcf 864 if (button->button == 1) {
192f81bf 865 btn = INPUT_BUTTON_LEFT;
a4ccabcf 866 } else if (button->button == 2) {
192f81bf 867 btn = INPUT_BUTTON_MIDDLE;
a4ccabcf 868 } else if (button->button == 3) {
192f81bf 869 btn = INPUT_BUTTON_RIGHT;
a4ccabcf 870 } else {
192f81bf 871 return TRUE;
a4ccabcf
AL
872 }
873
e3500d1f
GH
874 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
875 button->type == GDK_BUTTON_PRESS);
192f81bf 876 qemu_input_event_sync();
a4ccabcf
AL
877 return TRUE;
878}
879
d58b9122
JK
880static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
881 void *opaque)
882{
e3500d1f 883 VirtualConsole *vc = opaque;
d58b9122
JK
884 InputButton btn;
885
886 if (scroll->direction == GDK_SCROLL_UP) {
887 btn = INPUT_BUTTON_WHEEL_UP;
888 } else if (scroll->direction == GDK_SCROLL_DOWN) {
889 btn = INPUT_BUTTON_WHEEL_DOWN;
890 } else {
891 return TRUE;
892 }
893
e3500d1f 894 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
d58b9122 895 qemu_input_event_sync();
e3500d1f 896 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
d58b9122
JK
897 qemu_input_event_sync();
898 return TRUE;
899}
900
0a337ed0 901static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
a4ccabcf 902{
932f2d7e 903 int qemu_keycode;
a4ccabcf 904
0a337ed0
GH
905#ifdef GDK_WINDOWING_WIN32
906 if (GDK_IS_WIN32_DISPLAY(dpy)) {
907 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
908 switch (qemu_keycode) {
909 case 103: /* alt gr */
910 qemu_keycode = 56 | SCANCODE_GREY;
911 break;
912 }
913 return qemu_keycode;
2777ccc5 914 }
0a337ed0 915#endif
a4ccabcf
AL
916
917 if (gdk_keycode < 9) {
918 qemu_keycode = 0;
919 } else if (gdk_keycode < 97) {
920 qemu_keycode = gdk_keycode - 8;
0a337ed0
GH
921#ifdef GDK_WINDOWING_X11
922 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
3158a348
BR
923 if (s->has_evdev) {
924 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
925 } else {
926 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
927 }
0a337ed0 928#endif
a4ccabcf
AL
929 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
930 qemu_keycode = 0x70;
931 } else if (gdk_keycode == 211) { /* backslash */
932 qemu_keycode = 0x73;
933 } else {
934 qemu_keycode = 0;
935 }
936
932f2d7e
GH
937 return qemu_keycode;
938}
939
f8c223f6
GH
940static gboolean gd_text_key_down(GtkWidget *widget,
941 GdkEventKey *key, void *opaque)
942{
943 VirtualConsole *vc = opaque;
944 QemuConsole *con = vc->gfx.dcl.con;
945
946 if (key->length) {
947 kbd_put_string_console(con, key->string, key->length);
948 } else {
949 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
950 key->hardware_keycode);
951 int qcode = qemu_input_key_number_to_qcode(num);
952 kbd_put_qcode_console(con, qcode);
953 }
954 return TRUE;
955}
956
932f2d7e
GH
957static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
958{
959 VirtualConsole *vc = opaque;
960 GtkDisplayState *s = vc->s;
961 int gdk_keycode = key->hardware_keycode;
962 int qemu_keycode;
963 int i;
964
1a01716a
JK
965 if (s->ignore_keys) {
966 s->ignore_keys = (key->type == GDK_KEY_PRESS);
967 return TRUE;
968 }
969
5c960521
MD
970 if (key->keyval == GDK_KEY_Pause) {
971 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
972 key->type == GDK_KEY_PRESS);
973 return TRUE;
974 }
975
0a337ed0
GH
976 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
977 gdk_keycode);
932f2d7e 978
74444bc1 979 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
ef0dd982 980 (key->type == GDK_KEY_PRESS) ? "down" : "up");
a4ccabcf 981
6db253ca
JK
982 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
983 if (qemu_keycode == modifier_keycode[i]) {
984 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
985 }
986 }
987
e3500d1f 988 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
af98ba92 989 key->type == GDK_KEY_PRESS);
a4ccabcf
AL
990
991 return TRUE;
992}
993
0d0e044d
TI
994static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
995{
996 if (event->type == GDK_MOTION_NOTIFY) {
997 return gd_motion_event(widget, &event->motion, opaque);
998 }
999 return FALSE;
1000}
1001
a4ccabcf
AL
1002/** Window Menu Actions **/
1003
30e8f22b
JK
1004static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1005{
1006 GtkDisplayState *s = opaque;
1007
1008 if (s->external_pause_update) {
1009 return;
1010 }
1011 if (runstate_is_running()) {
1012 qmp_stop(NULL);
1013 } else {
1014 qmp_cont(NULL);
1015 }
1016}
1017
1018static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1019{
1020 qmp_system_reset(NULL);
1021}
1022
1023static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1024{
1025 qmp_system_powerdown(NULL);
1026}
1027
a4ccabcf
AL
1028static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1029{
1030 qmp_quit(NULL);
1031}
1032
1033static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1034{
1035 GtkDisplayState *s = opaque;
e3500d1f 1036 VirtualConsole *vc = gd_vc_find_by_menu(s);
e72b59fa 1037 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
832189c9 1038 gint page;
a4ccabcf 1039
e3500d1f
GH
1040 gtk_release_modifiers(s);
1041 if (vc) {
e72b59fa
JS
1042 page = gtk_notebook_page_num(nb, vc->tab_item);
1043 gtk_notebook_set_current_page(nb, page);
9d677e1c 1044 gtk_widget_grab_focus(vc->focus);
a4ccabcf 1045 }
1a01716a 1046 s->ignore_keys = false;
a4ccabcf
AL
1047}
1048
277836c8
CR
1049static void gd_accel_switch_vc(void *opaque)
1050{
1051 VirtualConsole *vc = opaque;
1a01716a 1052
277836c8 1053 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1a01716a
JK
1054#if !GTK_CHECK_VERSION(3, 0, 0)
1055 /* GTK2 sends the accel key to the target console - ignore this until */
1056 vc->s->ignore_keys = true;
1057#endif
277836c8
CR
1058}
1059
a4ccabcf
AL
1060static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1061{
1062 GtkDisplayState *s = opaque;
fa7a1e52 1063 VirtualConsole *vc = gd_vc_find_current(s);
a4ccabcf
AL
1064
1065 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1066 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1067 } else {
1068 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1069 }
fa7a1e52 1070 gd_update_windowsize(vc);
a4ccabcf
AL
1071}
1072
cdeb7090
GH
1073static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1074 void *opaque)
1075{
1076 VirtualConsole *vc = opaque;
1077 GtkDisplayState *s = vc->s;
1078
1079 gtk_widget_set_sensitive(vc->menu_item, true);
316cb068 1080 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
cdeb7090
GH
1081 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1082 vc->tab_item, vc->label);
1083 gtk_widget_destroy(vc->window);
1084 vc->window = NULL;
1085 return TRUE;
1086}
1087
0c77a37f
GH
1088static gboolean gd_win_grab(void *opaque)
1089{
1090 VirtualConsole *vc = opaque;
1091
1092 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1093 if (vc->s->ptr_owner) {
1094 gd_ungrab_pointer(vc->s);
1095 } else {
1096 gd_grab_pointer(vc);
1097 }
1098 gd_update_caption(vc->s);
1099 return TRUE;
1100}
1101
cdeb7090
GH
1102static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1103{
1104 GtkDisplayState *s = opaque;
1105 VirtualConsole *vc = gd_vc_find_current(s);
cdeb7090 1106
f8c223f6
GH
1107 if (vc->type == GD_VC_GFX &&
1108 qemu_console_is_graphic(vc->gfx.dcl.con)) {
aa0a55d4
GH
1109 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1110 FALSE);
cdeb7090
GH
1111 }
1112 if (!vc->window) {
1113 gtk_widget_set_sensitive(vc->menu_item, false);
1114 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
316cb068 1115 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
cdeb7090 1116
cdeb7090
GH
1117 g_signal_connect(vc->window, "delete-event",
1118 G_CALLBACK(gd_tab_window_close), vc);
1119 gtk_widget_show_all(vc->window);
4eeaa3a8 1120
f8c223f6
GH
1121 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1122 GtkAccelGroup *ag = gtk_accel_group_new();
1123 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
0c77a37f 1124
f8c223f6
GH
1125 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1126 vc, NULL);
1127 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1128 }
0c77a37f 1129
82fc1809 1130 gd_update_geometry_hints(vc);
4eeaa3a8 1131 gd_update_caption(s);
cdeb7090
GH
1132 }
1133}
1134
c6158483
AL
1135static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1136{
1137 GtkDisplayState *s = opaque;
e3500d1f 1138 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1139
10409282 1140 if (!s->full_screen) {
c6158483 1141 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
b0f31820 1142 gtk_widget_hide(s->menu_bar);
e3500d1f
GH
1143 if (vc->type == GD_VC_GFX) {
1144 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
f8c223f6
GH
1145 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1146 gtk_check_menu_item_set_active
1147 (GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
1148 }
c6158483 1149 }
e3500d1f 1150 gtk_window_fullscreen(GTK_WINDOW(s->window));
c6158483
AL
1151 s->full_screen = TRUE;
1152 } else {
1153 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1154 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
b0f31820 1155 gtk_widget_show(s->menu_bar);
c6158483 1156 s->full_screen = FALSE;
e3500d1f
GH
1157 if (vc->type == GD_VC_GFX) {
1158 vc->gfx.scale_x = 1.0;
1159 vc->gfx.scale_y = 1.0;
82fc1809 1160 gd_update_windowsize(vc);
e3500d1f
GH
1161 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1162 FALSE);
1163 }
c6158483
AL
1164 }
1165
e3500d1f 1166 gd_update_cursor(vc);
c6158483
AL
1167}
1168
95414914
CR
1169static void gd_accel_full_screen(void *opaque)
1170{
1171 GtkDisplayState *s = opaque;
1172 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1173}
1174
c6158483
AL
1175static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1176{
1177 GtkDisplayState *s = opaque;
e3500d1f 1178 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1179
1180 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1181 FALSE);
1182
82fc1809
GH
1183 vc->gfx.scale_x += VC_SCALE_STEP;
1184 vc->gfx.scale_y += VC_SCALE_STEP;
c6158483 1185
e3500d1f 1186 gd_update_windowsize(vc);
c6158483
AL
1187}
1188
1189static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1190{
1191 GtkDisplayState *s = opaque;
e3500d1f 1192 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1193
1194 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1195 FALSE);
1196
82fc1809
GH
1197 vc->gfx.scale_x -= VC_SCALE_STEP;
1198 vc->gfx.scale_y -= VC_SCALE_STEP;
c6158483 1199
82fc1809
GH
1200 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1201 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
c6158483 1202
e3500d1f 1203 gd_update_windowsize(vc);
c6158483
AL
1204}
1205
1206static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1207{
1208 GtkDisplayState *s = opaque;
e3500d1f 1209 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1210
e3500d1f
GH
1211 vc->gfx.scale_x = 1.0;
1212 vc->gfx.scale_y = 1.0;
c6158483 1213
e3500d1f 1214 gd_update_windowsize(vc);
c6158483
AL
1215}
1216
1217static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1218{
1219 GtkDisplayState *s = opaque;
e3500d1f 1220 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1221
1222 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1223 s->free_scale = TRUE;
1224 } else {
1225 s->free_scale = FALSE;
e3500d1f
GH
1226 vc->gfx.scale_x = 1.0;
1227 vc->gfx.scale_y = 1.0;
c6158483
AL
1228 }
1229
82fc1809 1230 gd_update_windowsize(vc);
e3500d1f 1231 gd_update_full_redraw(vc);
c6158483
AL
1232}
1233
655199da 1234#if GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1235static void gd_grab_devices(VirtualConsole *vc, bool grab,
1236 GdkInputSource source, GdkEventMask mask,
1237 GdkCursor *cursor)
1238{
e3500d1f 1239 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
655199da 1240 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1241 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1242 GList *tmp = devs;
1243
1244 for (tmp = devs; tmp; tmp = tmp->next) {
655199da 1245 GdkDevice *dev = tmp->data;
f50def91
GH
1246 if (gdk_device_get_source(dev) != source) {
1247 continue;
1248 }
1249 if (grab) {
1250 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1251 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1252 mask, cursor, GDK_CURRENT_TIME);
1253 } else {
1254 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
655199da 1255 }
655199da 1256 }
f50def91
GH
1257 g_list_free(devs);
1258}
1259#endif
1260
1261static void gd_grab_keyboard(VirtualConsole *vc)
1262{
aa4f4058
GH
1263 if (vc->s->kbd_owner) {
1264 if (vc->s->kbd_owner == vc) {
1265 return;
1266 } else {
1267 gd_ungrab_keyboard(vc->s);
1268 }
1269 }
1270
f50def91
GH
1271#if GTK_CHECK_VERSION(3, 0, 0)
1272 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1273 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1274 NULL);
655199da 1275#else
e3500d1f 1276 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
5104a1f6
AL
1277 FALSE,
1278 GDK_CURRENT_TIME);
655199da 1279#endif
4c638e2e 1280 vc->s->kbd_owner = vc;
1c856da5 1281 trace_gd_grab(vc->label, "kbd", true);
5104a1f6
AL
1282}
1283
4c638e2e 1284static void gd_ungrab_keyboard(GtkDisplayState *s)
5104a1f6 1285{
4c638e2e
GH
1286 VirtualConsole *vc = s->kbd_owner;
1287
1288 if (vc == NULL) {
1289 return;
1290 }
1291 s->kbd_owner = NULL;
1292
655199da 1293#if GTK_CHECK_VERSION(3, 0, 0)
f50def91 1294 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
655199da 1295#else
5104a1f6 1296 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
655199da 1297#endif
1c856da5 1298 trace_gd_grab(vc->label, "kbd", false);
5104a1f6
AL
1299}
1300
e3500d1f 1301static void gd_grab_pointer(VirtualConsole *vc)
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;
1c856da5 1339 trace_gd_grab(vc->label, "ptr", true);
2a05485d
DB
1340}
1341
4c638e2e 1342static void gd_ungrab_pointer(GtkDisplayState *s)
2a05485d 1343{
4c638e2e
GH
1344 VirtualConsole *vc = s->ptr_owner;
1345
1346 if (vc == NULL) {
1347 return;
1348 }
1349 s->ptr_owner = NULL;
1350
e3500d1f 1351 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
ecce1929 1352#if GTK_CHECK_VERSION(3, 0, 0)
2a05485d 1353 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91 1354 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
ecce1929 1355 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
e3500d1f
GH
1356 gtk_widget_get_screen(vc->gfx.drawing_area),
1357 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d
DB
1358#else
1359 gdk_pointer_ungrab(GDK_CURRENT_TIME);
ecce1929 1360 gdk_display_warp_pointer(display,
e3500d1f
GH
1361 gtk_widget_get_screen(vc->gfx.drawing_area),
1362 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d 1363#endif
1c856da5 1364 trace_gd_grab(vc->label, "ptr", false);
2a05485d
DB
1365}
1366
5104a1f6
AL
1367static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1368{
1369 GtkDisplayState *s = opaque;
e3500d1f 1370 VirtualConsole *vc = gd_vc_find_current(s);
5104a1f6
AL
1371
1372 if (gd_is_grab_active(s)) {
aa4f4058 1373 gd_grab_keyboard(vc);
e3500d1f 1374 gd_grab_pointer(vc);
5104a1f6 1375 } else {
4c638e2e
GH
1376 gd_ungrab_keyboard(s);
1377 gd_ungrab_pointer(s);
5104a1f6
AL
1378 }
1379
1380 gd_update_caption(s);
e3500d1f 1381 gd_update_cursor(vc);
5104a1f6
AL
1382}
1383
a4ccabcf
AL
1384static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1385 gpointer data)
1386{
1387 GtkDisplayState *s = data;
e3500d1f 1388 VirtualConsole *vc;
5104a1f6 1389 gboolean on_vga;
a4ccabcf
AL
1390
1391 if (!gtk_widget_get_realized(s->notebook)) {
1392 return;
1393 }
1394
6fa27697
GH
1395#ifdef VTE_RESIZE_HACK
1396 vc = gd_vc_find_current(s);
1397 if (vc && vc->type == GD_VC_VTE) {
1398 gtk_widget_hide(vc->vte.terminal);
1399 }
1400#endif
e3500d1f
GH
1401 vc = gd_vc_find_by_page(s, arg2);
1402 if (!vc) {
1403 return;
1404 }
6fa27697
GH
1405#ifdef VTE_RESIZE_HACK
1406 if (vc->type == GD_VC_VTE) {
1407 gtk_widget_show(vc->vte.terminal);
1408 }
1409#endif
e3500d1f
GH
1410 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1411 TRUE);
f8c223f6
GH
1412 on_vga = (vc->type == GD_VC_GFX &&
1413 qemu_console_is_graphic(vc->gfx.dcl.con));
5104a1f6
AL
1414 if (!on_vga) {
1415 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1416 FALSE);
c6158483
AL
1417 } else if (s->full_screen) {
1418 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1419 TRUE);
5104a1f6 1420 }
5104a1f6
AL
1421 gtk_widget_set_sensitive(s->grab_item, on_vga);
1422
82fc1809 1423 gd_update_windowsize(vc);
e3500d1f 1424 gd_update_cursor(vc);
a4ccabcf
AL
1425}
1426
e3500d1f
GH
1427static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1428 gpointer opaque)
5104a1f6 1429{
e3500d1f
GH
1430 VirtualConsole *vc = opaque;
1431 GtkDisplayState *s = vc->s;
5104a1f6 1432
2884cf5b 1433 if (gd_grab_on_hover(s)) {
e3500d1f 1434 gd_grab_keyboard(vc);
2884cf5b 1435 gd_update_caption(s);
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);
2884cf5b 1448 gd_update_caption(s);
5104a1f6 1449 }
5104a1f6
AL
1450 return TRUE;
1451}
1452
6db253ca 1453static gboolean gd_focus_out_event(GtkWidget *widget,
e3500d1f 1454 GdkEventCrossing *crossing, gpointer opaque)
6db253ca 1455{
e3500d1f
GH
1456 VirtualConsole *vc = opaque;
1457 GtkDisplayState *s = vc->s;
6db253ca
JK
1458
1459 gtk_release_modifiers(s);
6db253ca
JK
1460 return TRUE;
1461}
1462
1301e515
GH
1463static gboolean gd_configure(GtkWidget *widget,
1464 GdkEventConfigure *cfg, gpointer opaque)
1465{
1466 VirtualConsole *vc = opaque;
1467 QemuUIInfo info;
1468
1469 memset(&info, 0, sizeof(info));
1470 info.width = cfg->width;
1471 info.height = cfg->height;
1472 dpy_set_ui_info(vc->gfx.dcl.con, &info);
1473 return FALSE;
1474}
1475
d861def3
AL
1476/** Virtual Console Callbacks **/
1477
ed1132e4 1478static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
cdeb7090 1479 int idx, GSList *group, GtkWidget *view_menu)
ed1132e4 1480{
cdeb7090 1481 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
277836c8
CR
1482 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1483 HOTKEY_MODIFIERS, 0,
1484 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1485#if GTK_CHECK_VERSION(3, 8, 0)
1486 gtk_accel_label_set_accel(
1487 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1488 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1489#endif
ed1132e4
GH
1490
1491 g_signal_connect(vc->menu_item, "activate",
1492 G_CALLBACK(gd_menu_switch_vc), s);
1493 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1494
277836c8 1495 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
ed1132e4
GH
1496 return group;
1497}
1498
ee5f31e4 1499#if defined(CONFIG_VTE)
0fb20d1c
CR
1500static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1501{
1502 VirtualConsole *vc = opaque;
1503
1504 if (gtk_adjustment_get_upper(adjustment) >
1505 gtk_adjustment_get_page_size(adjustment)) {
271a25c0 1506 gtk_widget_show(vc->vte.scrollbar);
0fb20d1c 1507 } else {
271a25c0 1508 gtk_widget_hide(vc->vte.scrollbar);
0fb20d1c
CR
1509 }
1510}
1511
d861def3
AL
1512static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1513{
1514 VirtualConsole *vc = chr->opaque;
1515
271a25c0 1516 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
d4370741 1517 return len;
d861def3
AL
1518}
1519
1520static int nb_vcs;
1521static CharDriverState *vcs[MAX_VCS];
1522
702ec69c 1523static CharDriverState *gd_vc_handler(ChardevVC *unused)
d861def3
AL
1524{
1525 CharDriverState *chr;
1526
1527 chr = g_malloc0(sizeof(*chr));
1528 chr->chr_write = gd_vc_chr_write;
bd5c51ee
MR
1529 /* defer OPENED events until our vc is fully initialized */
1530 chr->explicit_be_open = true;
d861def3
AL
1531
1532 vcs[nb_vcs++] = chr;
1533
1534 return chr;
1535}
1536
d4370741
CR
1537static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1538 gpointer user_data)
d861def3 1539{
d4370741 1540 VirtualConsole *vc = user_data;
d861def3 1541
271a25c0 1542 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
d861def3
AL
1543 return TRUE;
1544}
1545
ed1132e4
GH
1546static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1547 CharDriverState *chr, int idx,
e3500d1f 1548 GSList *group, GtkWidget *view_menu)
d861def3 1549{
d861def3 1550 char buffer[32];
0fb20d1c
CR
1551 GtkWidget *box;
1552 GtkWidget *scrollbar;
1553 GtkAdjustment *vadjustment;
d861def3 1554
e3500d1f 1555 vc->s = s;
ed1132e4 1556 vc->vte.chr = chr;
d861def3 1557
ed1132e4 1558 snprintf(buffer, sizeof(buffer), "vc%d", idx);
cdeb7090
GH
1559 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1560 ? vc->vte.chr->label : buffer);
1561 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
d861def3 1562
271a25c0
GH
1563 vc->vte.terminal = vte_terminal_new();
1564 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
d861def3 1565
271a25c0 1566 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
82fc1809
GH
1567 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1568 VC_TERM_X_MIN, VC_TERM_Y_MIN);
d861def3 1569
0fb20d1c 1570#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
271a25c0
GH
1571 vadjustment = gtk_scrollable_get_vadjustment
1572 (GTK_SCROLLABLE(vc->vte.terminal));
0fb20d1c 1573#else
271a25c0 1574 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
0fb20d1c
CR
1575#endif
1576
1577#if GTK_CHECK_VERSION(3, 0, 0)
1578 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1579 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1580#else
1581 box = gtk_hbox_new(false, 2);
1582 scrollbar = gtk_vscrollbar_new(vadjustment);
1583#endif
1584
271a25c0 1585 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
0fb20d1c
CR
1586 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1587
271a25c0
GH
1588 vc->vte.chr->opaque = vc;
1589 vc->vte.box = box;
1590 vc->vte.scrollbar = scrollbar;
0fb20d1c
CR
1591
1592 g_signal_connect(vadjustment, "changed",
1593 G_CALLBACK(gd_vc_adjustment_changed), vc);
d861def3 1594
e3500d1f 1595 vc->type = GD_VC_VTE;
271a25c0 1596 vc->tab_item = box;
9d677e1c 1597 vc->focus = vc->vte.terminal;
271a25c0 1598 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
cdeb7090 1599 gtk_label_new(vc->label));
d861def3 1600
271a25c0
GH
1601 qemu_chr_be_generic_open(vc->vte.chr);
1602 if (vc->vte.chr->init) {
1603 vc->vte.chr->init(vc->vte.chr);
d861def3
AL
1604 }
1605
d861def3 1606 return group;
a4ccabcf
AL
1607}
1608
ed1132e4 1609static void gd_vcs_init(GtkDisplayState *s, GSList *group,
ee5f31e4
GH
1610 GtkWidget *view_menu)
1611{
1612 int i;
1613
1614 for (i = 0; i < nb_vcs; i++) {
ed1132e4
GH
1615 VirtualConsole *vc = &s->vc[s->nb_vcs];
1616 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
ee5f31e4
GH
1617 s->nb_vcs++;
1618 }
1619}
1620#endif /* CONFIG_VTE */
1621
a4ccabcf
AL
1622/** Window Creation **/
1623
e3500d1f
GH
1624static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1625{
1626#if GTK_CHECK_VERSION(3, 0, 0)
1627 g_signal_connect(vc->gfx.drawing_area, "draw",
1628 G_CALLBACK(gd_draw_event), vc);
1629#else
1630 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1631 G_CALLBACK(gd_expose_event), vc);
1632#endif
f8c223f6
GH
1633 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1634 g_signal_connect(vc->gfx.drawing_area, "event",
1635 G_CALLBACK(gd_event), vc);
1636 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1637 G_CALLBACK(gd_button_event), vc);
1638 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1639 G_CALLBACK(gd_button_event), vc);
1640 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1641 G_CALLBACK(gd_scroll_event), vc);
1642 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1643 G_CALLBACK(gd_key_event), vc);
1644 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1645 G_CALLBACK(gd_key_event), vc);
1646
1647 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1648 G_CALLBACK(gd_enter_event), vc);
1649 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1650 G_CALLBACK(gd_leave_event), vc);
1651 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1652 G_CALLBACK(gd_focus_out_event), vc);
1301e515
GH
1653 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1654 G_CALLBACK(gd_configure), vc);
f8c223f6
GH
1655 } else {
1656 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1657 G_CALLBACK(gd_text_key_down), vc);
1658 }
e3500d1f
GH
1659}
1660
a4ccabcf
AL
1661static void gd_connect_signals(GtkDisplayState *s)
1662{
1663 g_signal_connect(s->show_tabs_item, "activate",
1664 G_CALLBACK(gd_menu_show_tabs), s);
cdeb7090
GH
1665 g_signal_connect(s->untabify_item, "activate",
1666 G_CALLBACK(gd_menu_untabify), s);
a4ccabcf
AL
1667
1668 g_signal_connect(s->window, "delete-event",
1669 G_CALLBACK(gd_window_close), s);
1670
30e8f22b
JK
1671 g_signal_connect(s->pause_item, "activate",
1672 G_CALLBACK(gd_menu_pause), s);
1673 g_signal_connect(s->reset_item, "activate",
1674 G_CALLBACK(gd_menu_reset), s);
1675 g_signal_connect(s->powerdown_item, "activate",
1676 G_CALLBACK(gd_menu_powerdown), s);
a4ccabcf
AL
1677 g_signal_connect(s->quit_item, "activate",
1678 G_CALLBACK(gd_menu_quit), s);
c6158483
AL
1679 g_signal_connect(s->full_screen_item, "activate",
1680 G_CALLBACK(gd_menu_full_screen), s);
1681 g_signal_connect(s->zoom_in_item, "activate",
1682 G_CALLBACK(gd_menu_zoom_in), s);
1683 g_signal_connect(s->zoom_out_item, "activate",
1684 G_CALLBACK(gd_menu_zoom_out), s);
1685 g_signal_connect(s->zoom_fixed_item, "activate",
1686 G_CALLBACK(gd_menu_zoom_fixed), s);
1687 g_signal_connect(s->zoom_fit_item, "activate",
1688 G_CALLBACK(gd_menu_zoom_fit), s);
5104a1f6
AL
1689 g_signal_connect(s->grab_item, "activate",
1690 G_CALLBACK(gd_menu_grab_input), s);
a4ccabcf
AL
1691 g_signal_connect(s->notebook, "switch-page",
1692 G_CALLBACK(gd_change_page), s);
1693}
1694
400519d2 1695static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
a4ccabcf 1696{
bf9b255f 1697 GtkWidget *machine_menu;
a4ccabcf
AL
1698 GtkWidget *separator;
1699
bf9b255f 1700 machine_menu = gtk_menu_new();
400519d2 1701 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
30e8f22b
JK
1702
1703 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
bf9b255f 1704 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
30e8f22b
JK
1705
1706 separator = gtk_separator_menu_item_new();
bf9b255f 1707 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
30e8f22b 1708
9068f20d 1709 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
bf9b255f 1710 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
30e8f22b 1711
9068f20d 1712 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
bf9b255f 1713 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
30e8f22b
JK
1714
1715 separator = gtk_separator_menu_item_new();
bf9b255f 1716 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
a4ccabcf 1717
3d914488 1718 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
a4ccabcf 1719 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
30e8f22b 1720 "<QEMU>/Machine/Quit");
3d914488 1721 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
db1da1f2 1722 GDK_KEY_q, HOTKEY_MODIFIERS);
bf9b255f 1723 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
a4ccabcf 1724
bf9b255f
AL
1725 return machine_menu;
1726}
1727
e3500d1f 1728static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
ed1132e4 1729 QemuConsole *con, int idx,
e3500d1f
GH
1730 GSList *group, GtkWidget *view_menu)
1731{
779ce88f 1732 vc->label = qemu_console_get_label(con);
e3500d1f
GH
1733 vc->s = s;
1734 vc->gfx.scale_x = 1.0;
1735 vc->gfx.scale_y = 1.0;
1736
1737 vc->gfx.drawing_area = gtk_drawing_area_new();
1738 gtk_widget_add_events(vc->gfx.drawing_area,
1739 GDK_POINTER_MOTION_MASK |
1740 GDK_BUTTON_PRESS_MASK |
1741 GDK_BUTTON_RELEASE_MASK |
1742 GDK_BUTTON_MOTION_MASK |
1743 GDK_ENTER_NOTIFY_MASK |
1744 GDK_LEAVE_NOTIFY_MASK |
1745 GDK_SCROLL_MASK |
1746 GDK_KEY_PRESS_MASK);
e3500d1f
GH
1747 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1748
1749 vc->type = GD_VC_GFX;
1750 vc->tab_item = vc->gfx.drawing_area;
9d677e1c 1751 vc->focus = vc->gfx.drawing_area;
e3500d1f 1752 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
cdeb7090 1753 vc->tab_item, gtk_label_new(vc->label));
e3500d1f 1754
97edf3bd
GH
1755#if defined(CONFIG_OPENGL)
1756 if (display_opengl) {
1757 /*
1758 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1759 * It is required for opengl rendering on X11 though. A
1760 * proper replacement (native opengl support) is only
1761 * available in 3.16+. Silence the warning if possible.
1762 */
1763#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1764#pragma GCC diagnostic push
1765#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1766#endif
1767 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1768#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1769#pragma GCC diagnostic pop
1770#endif
1771 vc->gfx.dcl.ops = &dcl_egl_ops;
1772 } else
1773#endif
1774 {
1775 vc->gfx.dcl.ops = &dcl_ops;
1776 }
1777
e3500d1f
GH
1778 vc->gfx.dcl.con = con;
1779 register_displaychangelistener(&vc->gfx.dcl);
1780
f8c223f6
GH
1781 gd_connect_vc_gfx_signals(vc);
1782 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1783
1301e515
GH
1784 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
1785 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
1786 }
1787
e3500d1f
GH
1788 return group;
1789}
1790
400519d2 1791static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
bf9b255f
AL
1792{
1793 GSList *group = NULL;
1794 GtkWidget *view_menu;
1795 GtkWidget *separator;
ed1132e4
GH
1796 QemuConsole *con;
1797 int vc;
bf9b255f
AL
1798
1799 view_menu = gtk_menu_new();
400519d2 1800 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
a4ccabcf 1801
3d914488 1802 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
95414914
CR
1803
1804 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
1805 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
1806#if GTK_CHECK_VERSION(3, 8, 0)
1807 gtk_accel_label_set_accel(
1808 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
1809 GDK_KEY_f, HOTKEY_MODIFIERS);
1810#endif
bf9b255f 1811 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
c6158483
AL
1812
1813 separator = gtk_separator_menu_item_new();
bf9b255f 1814 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 1815
3d914488 1816 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
c6158483
AL
1817 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1818 "<QEMU>/View/Zoom In");
b1e749c0
JK
1819 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1820 HOTKEY_MODIFIERS);
bf9b255f 1821 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
c6158483 1822
3d914488 1823 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
c6158483
AL
1824 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1825 "<QEMU>/View/Zoom Out");
b1e749c0
JK
1826 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1827 HOTKEY_MODIFIERS);
bf9b255f 1828 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
c6158483 1829
3d914488 1830 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
c6158483
AL
1831 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1832 "<QEMU>/View/Zoom Fixed");
b1e749c0
JK
1833 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1834 HOTKEY_MODIFIERS);
bf9b255f 1835 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
c6158483 1836
834574ea 1837 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
bf9b255f 1838 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
c6158483
AL
1839
1840 separator = gtk_separator_menu_item_new();
bf9b255f 1841 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 1842
834574ea 1843 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
bf9b255f 1844 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
5104a1f6 1845
834574ea 1846 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
5104a1f6
AL
1847 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1848 "<QEMU>/View/Grab Input");
b1e749c0
JK
1849 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1850 HOTKEY_MODIFIERS);
bf9b255f 1851 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
5104a1f6 1852
a4ccabcf 1853 separator = gtk_separator_menu_item_new();
bf9b255f 1854 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 1855
e3500d1f 1856 /* gfx */
ed1132e4
GH
1857 for (vc = 0;; vc++) {
1858 con = qemu_console_lookup_by_index(vc);
f8c223f6 1859 if (!con) {
ed1132e4
GH
1860 break;
1861 }
1862 group = gd_vc_gfx_init(s, &s->vc[vc], con,
1863 vc, group, view_menu);
1864 s->nb_vcs++;
1865 }
a4ccabcf 1866
ee5f31e4 1867#if defined(CONFIG_VTE)
e3500d1f 1868 /* vte */
ed1132e4 1869 gd_vcs_init(s, group, view_menu);
ee5f31e4 1870#endif
d861def3 1871
a4ccabcf 1872 separator = gtk_separator_menu_item_new();
bf9b255f 1873 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 1874
834574ea 1875 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
bf9b255f 1876 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
a4ccabcf 1877
cdeb7090
GH
1878 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1879 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
1880
bf9b255f
AL
1881 return view_menu;
1882}
1883
1884static void gd_create_menus(GtkDisplayState *s)
1885{
400519d2
CR
1886 s->accel_group = gtk_accel_group_new();
1887 s->machine_menu = gd_create_menu_machine(s);
1888 s->view_menu = gd_create_menu_view(s);
bf9b255f
AL
1889
1890 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
30e8f22b
JK
1891 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1892 s->machine_menu);
1893 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
a4ccabcf 1894
bf9b255f 1895 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
a4ccabcf
AL
1896 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1897 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
bf9b255f 1898
400519d2
CR
1899 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
1900 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
a4ccabcf
AL
1901}
1902
3158a348
BR
1903static void gd_set_keycode_type(GtkDisplayState *s)
1904{
0a337ed0 1905#ifdef GDK_WINDOWING_X11
3158a348 1906 GdkDisplay *display = gtk_widget_get_display(s->window);
0a337ed0
GH
1907 if (GDK_IS_X11_DISPLAY(display)) {
1908 Display *x11_display = gdk_x11_display_get_xdisplay(display);
1909 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
1910 XkbUseCoreKbd);
1911 char *keycodes = NULL;
1912
1913 if (desc && desc->names) {
1914 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
1915 }
1916 if (keycodes == NULL) {
1917 fprintf(stderr, "could not lookup keycode name\n");
1918 } else if (strstart(keycodes, "evdev", NULL)) {
1919 s->has_evdev = true;
1920 } else if (!strstart(keycodes, "xfree86", NULL)) {
1921 fprintf(stderr, "unknown keycodes `%s', please report to "
1922 "qemu-devel@nongnu.org\n", keycodes);
1923 }
84961407
CF
1924
1925 if (desc) {
1926 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
1927 }
1928 if (keycodes) {
1929 XFree(keycodes);
1930 }
3158a348
BR
1931 }
1932#endif
1933}
1934
060ab763
GH
1935static gboolean gtkinit;
1936
881249c7 1937void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
a4ccabcf
AL
1938{
1939 GtkDisplayState *s = g_malloc0(sizeof(*s));
d819cdcc 1940 char *filename;
63c67b6d 1941 GdkDisplay *window_display;
a4ccabcf 1942
060ab763
GH
1943 if (!gtkinit) {
1944 fprintf(stderr, "gtk initialization failed\n");
1945 exit(1);
1946 }
1947
a4ccabcf 1948 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
51572ab0
DB
1949#if GTK_CHECK_VERSION(3, 2, 0)
1950 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1951#else
a4ccabcf 1952 s->vbox = gtk_vbox_new(FALSE, 0);
51572ab0 1953#endif
a4ccabcf 1954 s->notebook = gtk_notebook_new();
a4ccabcf
AL
1955 s->menu_bar = gtk_menu_bar_new();
1956
c6158483 1957 s->free_scale = FALSE;
a4ccabcf 1958
834574ea
AL
1959 setlocale(LC_ALL, "");
1960 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1961 textdomain("qemu");
1962
63c67b6d
HR
1963 window_display = gtk_widget_get_display(s->window);
1964 s->null_cursor = gdk_cursor_new_for_display(window_display,
1965 GDK_BLANK_CURSOR);
a4ccabcf
AL
1966
1967 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1968 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1969 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1970
f7da9c17 1971 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
d819cdcc
SW
1972 if (filename) {
1973 GError *error = NULL;
1974 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1975 if (pixbuf) {
1976 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1977 } else {
1978 g_error_free(error);
1979 }
1980 g_free(filename);
1981 }
1982
a4ccabcf
AL
1983 gd_create_menus(s);
1984
1985 gd_connect_signals(s);
1986
a4ccabcf
AL
1987 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1988 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1989
a4ccabcf
AL
1990 gd_update_caption(s);
1991
1992 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1993 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1994
1995 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1996
1997 gtk_widget_show_all(s->window);
1998
6fa27697
GH
1999#ifdef VTE_RESIZE_HACK
2000 {
2001 VirtualConsole *cur = gd_vc_find_current(s);
b310a2a6
FZ
2002 if (cur) {
2003 int i;
2004
2005 for (i = 0; i < s->nb_vcs; i++) {
2006 VirtualConsole *vc = &s->vc[i];
2007 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2008 gtk_widget_hide(vc->vte.terminal);
2009 }
6fa27697 2010 }
b310a2a6 2011 gd_update_windowsize(cur);
6fa27697 2012 }
6fa27697
GH
2013 }
2014#endif
2015
787ba4f0
PW
2016 if (full_screen) {
2017 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2018 }
881249c7
JK
2019 if (grab_on_hover) {
2020 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2021 }
787ba4f0 2022
3158a348 2023 gd_set_keycode_type(s);
a4ccabcf 2024}
ee5f31e4 2025
97edf3bd 2026void early_gtk_display_init(int opengl)
ee5f31e4 2027{
060ab763
GH
2028 gtkinit = gtk_init_check(NULL, NULL);
2029 if (!gtkinit) {
2030 /* don't exit yet, that'll break -help */
2031 return;
2032 }
97edf3bd
GH
2033
2034 switch (opengl) {
2035 case -1: /* default */
2036 case 0: /* off */
2037 break;
2038 case 1: /* on */
2039#if defined(CONFIG_OPENGL)
2040 gtk_egl_init();
2041#endif
2042 break;
2043 default:
2044 g_assert_not_reached();
2045 break;
2046 }
2047
ee5f31e4
GH
2048#if defined(CONFIG_VTE)
2049 register_vc_handler(gd_vc_handler);
2050#endif
2051}