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