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