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