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