]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk.c
gtk: Install fullscreen accelerator on toplevel window
[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
1023static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1024{
1025 GtkDisplayState *s = opaque;
fa7a1e52 1026 VirtualConsole *vc = gd_vc_find_current(s);
a4ccabcf
AL
1027
1028 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1029 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1030 } else {
1031 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1032 }
fa7a1e52 1033 gd_update_windowsize(vc);
a4ccabcf
AL
1034}
1035
cdeb7090
GH
1036static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1037 void *opaque)
1038{
1039 VirtualConsole *vc = opaque;
1040 GtkDisplayState *s = vc->s;
1041
1042 gtk_widget_set_sensitive(vc->menu_item, true);
316cb068 1043 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
cdeb7090
GH
1044 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1045 vc->tab_item, vc->label);
1046 gtk_widget_destroy(vc->window);
1047 vc->window = NULL;
1048 return TRUE;
1049}
1050
0c77a37f
GH
1051static gboolean gd_win_grab(void *opaque)
1052{
1053 VirtualConsole *vc = opaque;
1054
1055 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1056 if (vc->s->ptr_owner) {
1057 gd_ungrab_pointer(vc->s);
1058 } else {
1059 gd_grab_pointer(vc);
1060 }
1061 gd_update_caption(vc->s);
1062 return TRUE;
1063}
1064
cdeb7090
GH
1065static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1066{
1067 GtkDisplayState *s = opaque;
1068 VirtualConsole *vc = gd_vc_find_current(s);
cdeb7090
GH
1069
1070 if (vc->type == GD_VC_GFX) {
aa0a55d4
GH
1071 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1072 FALSE);
cdeb7090
GH
1073 }
1074 if (!vc->window) {
1075 gtk_widget_set_sensitive(vc->menu_item, false);
1076 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
316cb068 1077 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
cdeb7090 1078
cdeb7090
GH
1079 g_signal_connect(vc->window, "delete-event",
1080 G_CALLBACK(gd_tab_window_close), vc);
1081 gtk_widget_show_all(vc->window);
4eeaa3a8 1082
0c77a37f
GH
1083 GtkAccelGroup *ag = gtk_accel_group_new();
1084 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1085
1086 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab), vc, NULL);
1087 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1088
82fc1809 1089 gd_update_geometry_hints(vc);
4eeaa3a8 1090 gd_update_caption(s);
cdeb7090
GH
1091 }
1092}
1093
c6158483
AL
1094static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1095{
1096 GtkDisplayState *s = opaque;
e3500d1f 1097 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1098
10409282 1099 if (!s->full_screen) {
c6158483
AL
1100 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1101 gtk_widget_set_size_request(s->menu_bar, 0, 0);
e3500d1f
GH
1102 if (vc->type == GD_VC_GFX) {
1103 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1104 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1105 TRUE);
c6158483 1106 }
e3500d1f 1107 gtk_window_fullscreen(GTK_WINDOW(s->window));
c6158483
AL
1108 s->full_screen = TRUE;
1109 } else {
1110 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1111 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1112 gtk_widget_set_size_request(s->menu_bar, -1, -1);
c6158483 1113 s->full_screen = FALSE;
e3500d1f
GH
1114 if (vc->type == GD_VC_GFX) {
1115 vc->gfx.scale_x = 1.0;
1116 vc->gfx.scale_y = 1.0;
82fc1809 1117 gd_update_windowsize(vc);
e3500d1f
GH
1118 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1119 FALSE);
1120 }
c6158483
AL
1121 }
1122
e3500d1f 1123 gd_update_cursor(vc);
c6158483
AL
1124}
1125
95414914
CR
1126static void gd_accel_full_screen(void *opaque)
1127{
1128 GtkDisplayState *s = opaque;
1129 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1130}
1131
c6158483
AL
1132static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1133{
1134 GtkDisplayState *s = opaque;
e3500d1f 1135 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1136
1137 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1138 FALSE);
1139
82fc1809
GH
1140 vc->gfx.scale_x += VC_SCALE_STEP;
1141 vc->gfx.scale_y += VC_SCALE_STEP;
c6158483 1142
e3500d1f 1143 gd_update_windowsize(vc);
c6158483
AL
1144}
1145
1146static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1147{
1148 GtkDisplayState *s = opaque;
e3500d1f 1149 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1150
1151 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1152 FALSE);
1153
82fc1809
GH
1154 vc->gfx.scale_x -= VC_SCALE_STEP;
1155 vc->gfx.scale_y -= VC_SCALE_STEP;
c6158483 1156
82fc1809
GH
1157 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1158 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
c6158483 1159
e3500d1f 1160 gd_update_windowsize(vc);
c6158483
AL
1161}
1162
1163static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1164{
1165 GtkDisplayState *s = opaque;
e3500d1f 1166 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1167
e3500d1f
GH
1168 vc->gfx.scale_x = 1.0;
1169 vc->gfx.scale_y = 1.0;
c6158483 1170
e3500d1f 1171 gd_update_windowsize(vc);
c6158483
AL
1172}
1173
1174static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1175{
1176 GtkDisplayState *s = opaque;
e3500d1f 1177 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1178
1179 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1180 s->free_scale = TRUE;
1181 } else {
1182 s->free_scale = FALSE;
e3500d1f
GH
1183 vc->gfx.scale_x = 1.0;
1184 vc->gfx.scale_y = 1.0;
c6158483
AL
1185 }
1186
82fc1809 1187 gd_update_windowsize(vc);
e3500d1f 1188 gd_update_full_redraw(vc);
c6158483
AL
1189}
1190
655199da 1191#if GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1192static void gd_grab_devices(VirtualConsole *vc, bool grab,
1193 GdkInputSource source, GdkEventMask mask,
1194 GdkCursor *cursor)
1195{
e3500d1f 1196 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
655199da 1197 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1198 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1199 GList *tmp = devs;
1200
1201 for (tmp = devs; tmp; tmp = tmp->next) {
655199da 1202 GdkDevice *dev = tmp->data;
f50def91
GH
1203 if (gdk_device_get_source(dev) != source) {
1204 continue;
1205 }
1206 if (grab) {
1207 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1208 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1209 mask, cursor, GDK_CURRENT_TIME);
1210 } else {
1211 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
655199da 1212 }
655199da 1213 }
f50def91
GH
1214 g_list_free(devs);
1215}
1216#endif
1217
1218static void gd_grab_keyboard(VirtualConsole *vc)
1219{
1220#if GTK_CHECK_VERSION(3, 0, 0)
1221 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1222 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1223 NULL);
655199da 1224#else
e3500d1f 1225 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
5104a1f6
AL
1226 FALSE,
1227 GDK_CURRENT_TIME);
655199da 1228#endif
4c638e2e 1229 vc->s->kbd_owner = vc;
1c856da5 1230 trace_gd_grab(vc->label, "kbd", true);
5104a1f6
AL
1231}
1232
4c638e2e 1233static void gd_ungrab_keyboard(GtkDisplayState *s)
5104a1f6 1234{
4c638e2e
GH
1235 VirtualConsole *vc = s->kbd_owner;
1236
1237 if (vc == NULL) {
1238 return;
1239 }
1240 s->kbd_owner = NULL;
1241
655199da 1242#if GTK_CHECK_VERSION(3, 0, 0)
f50def91 1243 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
655199da 1244#else
5104a1f6 1245 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
655199da 1246#endif
1c856da5 1247 trace_gd_grab(vc->label, "kbd", false);
5104a1f6
AL
1248}
1249
e3500d1f 1250static void gd_grab_pointer(VirtualConsole *vc)
2a05485d 1251{
e3500d1f 1252 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
ecce1929 1253#if GTK_CHECK_VERSION(3, 0, 0)
2a05485d 1254 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1255 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1256 GDK_POINTER_MOTION_MASK |
1257 GDK_BUTTON_PRESS_MASK |
1258 GDK_BUTTON_RELEASE_MASK |
1259 GDK_BUTTON_MOTION_MASK |
1260 GDK_SCROLL_MASK,
1261 vc->s->null_cursor);
ecce1929 1262 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
e3500d1f 1263 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
2a05485d 1264#else
e3500d1f 1265 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
2a05485d
DB
1266 FALSE, /* All events to come to our window directly */
1267 GDK_POINTER_MOTION_MASK |
1268 GDK_BUTTON_PRESS_MASK |
1269 GDK_BUTTON_RELEASE_MASK |
1270 GDK_BUTTON_MOTION_MASK |
1271 GDK_SCROLL_MASK,
1272 NULL, /* Allow cursor to move over entire desktop */
e3500d1f 1273 vc->s->null_cursor,
2a05485d 1274 GDK_CURRENT_TIME);
ecce1929 1275 gdk_display_get_pointer(display, NULL,
e3500d1f 1276 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
2a05485d 1277#endif
4c638e2e 1278 vc->s->ptr_owner = vc;
1c856da5 1279 trace_gd_grab(vc->label, "ptr", true);
2a05485d
DB
1280}
1281
4c638e2e 1282static void gd_ungrab_pointer(GtkDisplayState *s)
2a05485d 1283{
4c638e2e
GH
1284 VirtualConsole *vc = s->ptr_owner;
1285
1286 if (vc == NULL) {
1287 return;
1288 }
1289 s->ptr_owner = NULL;
1290
e3500d1f 1291 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
ecce1929 1292#if GTK_CHECK_VERSION(3, 0, 0)
2a05485d 1293 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91 1294 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
ecce1929 1295 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
e3500d1f
GH
1296 gtk_widget_get_screen(vc->gfx.drawing_area),
1297 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d
DB
1298#else
1299 gdk_pointer_ungrab(GDK_CURRENT_TIME);
ecce1929 1300 gdk_display_warp_pointer(display,
e3500d1f
GH
1301 gtk_widget_get_screen(vc->gfx.drawing_area),
1302 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d 1303#endif
1c856da5 1304 trace_gd_grab(vc->label, "ptr", false);
2a05485d
DB
1305}
1306
5104a1f6
AL
1307static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1308{
1309 GtkDisplayState *s = opaque;
e3500d1f 1310 VirtualConsole *vc = gd_vc_find_current(s);
5104a1f6
AL
1311
1312 if (gd_is_grab_active(s)) {
746b8670
GH
1313 if (!gd_grab_on_hover(s)) {
1314 gd_grab_keyboard(vc);
1315 }
e3500d1f 1316 gd_grab_pointer(vc);
5104a1f6 1317 } else {
4c638e2e
GH
1318 gd_ungrab_keyboard(s);
1319 gd_ungrab_pointer(s);
5104a1f6
AL
1320 }
1321
1322 gd_update_caption(s);
e3500d1f 1323 gd_update_cursor(vc);
5104a1f6
AL
1324}
1325
a4ccabcf
AL
1326static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1327 gpointer data)
1328{
1329 GtkDisplayState *s = data;
e3500d1f 1330 VirtualConsole *vc;
5104a1f6 1331 gboolean on_vga;
a4ccabcf
AL
1332
1333 if (!gtk_widget_get_realized(s->notebook)) {
1334 return;
1335 }
1336
6fa27697
GH
1337#ifdef VTE_RESIZE_HACK
1338 vc = gd_vc_find_current(s);
1339 if (vc && vc->type == GD_VC_VTE) {
1340 gtk_widget_hide(vc->vte.terminal);
1341 }
1342#endif
e3500d1f
GH
1343 vc = gd_vc_find_by_page(s, arg2);
1344 if (!vc) {
1345 return;
1346 }
6fa27697
GH
1347#ifdef VTE_RESIZE_HACK
1348 if (vc->type == GD_VC_VTE) {
1349 gtk_widget_show(vc->vte.terminal);
1350 }
1351#endif
e3500d1f
GH
1352 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1353 TRUE);
e3500d1f 1354 on_vga = (vc->type == GD_VC_GFX);
5104a1f6
AL
1355 if (!on_vga) {
1356 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1357 FALSE);
c6158483
AL
1358 } else if (s->full_screen) {
1359 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1360 TRUE);
5104a1f6 1361 }
5104a1f6
AL
1362 gtk_widget_set_sensitive(s->grab_item, on_vga);
1363
82fc1809 1364 gd_update_windowsize(vc);
e3500d1f 1365 gd_update_cursor(vc);
a4ccabcf
AL
1366}
1367
e3500d1f
GH
1368static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1369 gpointer opaque)
5104a1f6 1370{
e3500d1f
GH
1371 VirtualConsole *vc = opaque;
1372 GtkDisplayState *s = vc->s;
5104a1f6 1373
2884cf5b
GH
1374 if (gd_grab_on_hover(s)) {
1375 gd_ungrab_keyboard(s);
e3500d1f 1376 gd_grab_keyboard(vc);
2884cf5b 1377 gd_update_caption(s);
5104a1f6 1378 }
5104a1f6
AL
1379 return TRUE;
1380}
1381
e3500d1f
GH
1382static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1383 gpointer opaque)
5104a1f6 1384{
e3500d1f
GH
1385 VirtualConsole *vc = opaque;
1386 GtkDisplayState *s = vc->s;
5104a1f6 1387
2884cf5b 1388 if (gd_grab_on_hover(s)) {
4c638e2e 1389 gd_ungrab_keyboard(s);
2884cf5b 1390 gd_update_caption(s);
5104a1f6 1391 }
5104a1f6
AL
1392 return TRUE;
1393}
1394
6db253ca 1395static gboolean gd_focus_out_event(GtkWidget *widget,
e3500d1f 1396 GdkEventCrossing *crossing, gpointer opaque)
6db253ca 1397{
e3500d1f
GH
1398 VirtualConsole *vc = opaque;
1399 GtkDisplayState *s = vc->s;
6db253ca
JK
1400
1401 gtk_release_modifiers(s);
6db253ca
JK
1402 return TRUE;
1403}
1404
d861def3
AL
1405/** Virtual Console Callbacks **/
1406
ed1132e4 1407static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
cdeb7090 1408 int idx, GSList *group, GtkWidget *view_menu)
ed1132e4
GH
1409{
1410 char path[32];
1411
1412 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", idx);
1413
cdeb7090 1414 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
ed1132e4
GH
1415 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1416 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1417 gtk_accel_map_add_entry(path, GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1418
1419 g_signal_connect(vc->menu_item, "activate",
1420 G_CALLBACK(gd_menu_switch_vc), s);
1421 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1422
1423 return group;
1424}
1425
ee5f31e4 1426#if defined(CONFIG_VTE)
0fb20d1c
CR
1427static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1428{
1429 VirtualConsole *vc = opaque;
1430
1431 if (gtk_adjustment_get_upper(adjustment) >
1432 gtk_adjustment_get_page_size(adjustment)) {
271a25c0 1433 gtk_widget_show(vc->vte.scrollbar);
0fb20d1c 1434 } else {
271a25c0 1435 gtk_widget_hide(vc->vte.scrollbar);
0fb20d1c
CR
1436 }
1437}
1438
d861def3
AL
1439static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1440{
1441 VirtualConsole *vc = chr->opaque;
1442
271a25c0 1443 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
d4370741 1444 return len;
d861def3
AL
1445}
1446
1447static int nb_vcs;
1448static CharDriverState *vcs[MAX_VCS];
1449
702ec69c 1450static CharDriverState *gd_vc_handler(ChardevVC *unused)
d861def3
AL
1451{
1452 CharDriverState *chr;
1453
1454 chr = g_malloc0(sizeof(*chr));
1455 chr->chr_write = gd_vc_chr_write;
bd5c51ee
MR
1456 /* defer OPENED events until our vc is fully initialized */
1457 chr->explicit_be_open = true;
d861def3
AL
1458
1459 vcs[nb_vcs++] = chr;
1460
1461 return chr;
1462}
1463
d4370741
CR
1464static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1465 gpointer user_data)
d861def3 1466{
d4370741 1467 VirtualConsole *vc = user_data;
d861def3 1468
271a25c0 1469 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
d861def3
AL
1470 return TRUE;
1471}
1472
ed1132e4
GH
1473static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1474 CharDriverState *chr, int idx,
e3500d1f 1475 GSList *group, GtkWidget *view_menu)
d861def3 1476{
d861def3 1477 char buffer[32];
0fb20d1c
CR
1478 GtkWidget *box;
1479 GtkWidget *scrollbar;
1480 GtkAdjustment *vadjustment;
d861def3 1481
e3500d1f 1482 vc->s = s;
ed1132e4 1483 vc->vte.chr = chr;
d861def3 1484
ed1132e4 1485 snprintf(buffer, sizeof(buffer), "vc%d", idx);
cdeb7090
GH
1486 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1487 ? vc->vte.chr->label : buffer);
1488 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
d861def3 1489
271a25c0
GH
1490 vc->vte.terminal = vte_terminal_new();
1491 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
d861def3 1492
271a25c0 1493 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
82fc1809
GH
1494 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1495 VC_TERM_X_MIN, VC_TERM_Y_MIN);
d861def3 1496
0fb20d1c 1497#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
271a25c0
GH
1498 vadjustment = gtk_scrollable_get_vadjustment
1499 (GTK_SCROLLABLE(vc->vte.terminal));
0fb20d1c 1500#else
271a25c0 1501 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
0fb20d1c
CR
1502#endif
1503
1504#if GTK_CHECK_VERSION(3, 0, 0)
1505 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1506 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1507#else
1508 box = gtk_hbox_new(false, 2);
1509 scrollbar = gtk_vscrollbar_new(vadjustment);
1510#endif
1511
271a25c0 1512 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
0fb20d1c
CR
1513 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1514
271a25c0
GH
1515 vc->vte.chr->opaque = vc;
1516 vc->vte.box = box;
1517 vc->vte.scrollbar = scrollbar;
0fb20d1c
CR
1518
1519 g_signal_connect(vadjustment, "changed",
1520 G_CALLBACK(gd_vc_adjustment_changed), vc);
d861def3 1521
e3500d1f 1522 vc->type = GD_VC_VTE;
271a25c0
GH
1523 vc->tab_item = box;
1524 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
cdeb7090 1525 gtk_label_new(vc->label));
d861def3 1526
271a25c0
GH
1527 qemu_chr_be_generic_open(vc->vte.chr);
1528 if (vc->vte.chr->init) {
1529 vc->vte.chr->init(vc->vte.chr);
d861def3
AL
1530 }
1531
d861def3 1532 return group;
a4ccabcf
AL
1533}
1534
ed1132e4 1535static void gd_vcs_init(GtkDisplayState *s, GSList *group,
ee5f31e4
GH
1536 GtkWidget *view_menu)
1537{
1538 int i;
1539
1540 for (i = 0; i < nb_vcs; i++) {
ed1132e4
GH
1541 VirtualConsole *vc = &s->vc[s->nb_vcs];
1542 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
ee5f31e4
GH
1543 s->nb_vcs++;
1544 }
1545}
1546#endif /* CONFIG_VTE */
1547
a4ccabcf
AL
1548/** Window Creation **/
1549
e3500d1f
GH
1550static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1551{
1552#if GTK_CHECK_VERSION(3, 0, 0)
1553 g_signal_connect(vc->gfx.drawing_area, "draw",
1554 G_CALLBACK(gd_draw_event), vc);
1555#else
1556 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1557 G_CALLBACK(gd_expose_event), vc);
1558#endif
1559 g_signal_connect(vc->gfx.drawing_area, "event",
1560 G_CALLBACK(gd_event), vc);
1561 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1562 G_CALLBACK(gd_button_event), vc);
1563 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1564 G_CALLBACK(gd_button_event), vc);
1565 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1566 G_CALLBACK(gd_scroll_event), vc);
1567 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1568 G_CALLBACK(gd_key_event), vc);
1569 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1570 G_CALLBACK(gd_key_event), vc);
1571
1572 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1573 G_CALLBACK(gd_enter_event), vc);
1574 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1575 G_CALLBACK(gd_leave_event), vc);
1576 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1577 G_CALLBACK(gd_focus_out_event), vc);
1578}
1579
a4ccabcf
AL
1580static void gd_connect_signals(GtkDisplayState *s)
1581{
1582 g_signal_connect(s->show_tabs_item, "activate",
1583 G_CALLBACK(gd_menu_show_tabs), s);
cdeb7090
GH
1584 g_signal_connect(s->untabify_item, "activate",
1585 G_CALLBACK(gd_menu_untabify), s);
a4ccabcf
AL
1586
1587 g_signal_connect(s->window, "delete-event",
1588 G_CALLBACK(gd_window_close), s);
1589
30e8f22b
JK
1590 g_signal_connect(s->pause_item, "activate",
1591 G_CALLBACK(gd_menu_pause), s);
1592 g_signal_connect(s->reset_item, "activate",
1593 G_CALLBACK(gd_menu_reset), s);
1594 g_signal_connect(s->powerdown_item, "activate",
1595 G_CALLBACK(gd_menu_powerdown), s);
a4ccabcf
AL
1596 g_signal_connect(s->quit_item, "activate",
1597 G_CALLBACK(gd_menu_quit), s);
c6158483
AL
1598 g_signal_connect(s->full_screen_item, "activate",
1599 G_CALLBACK(gd_menu_full_screen), s);
1600 g_signal_connect(s->zoom_in_item, "activate",
1601 G_CALLBACK(gd_menu_zoom_in), s);
1602 g_signal_connect(s->zoom_out_item, "activate",
1603 G_CALLBACK(gd_menu_zoom_out), s);
1604 g_signal_connect(s->zoom_fixed_item, "activate",
1605 G_CALLBACK(gd_menu_zoom_fixed), s);
1606 g_signal_connect(s->zoom_fit_item, "activate",
1607 G_CALLBACK(gd_menu_zoom_fit), s);
5104a1f6
AL
1608 g_signal_connect(s->grab_item, "activate",
1609 G_CALLBACK(gd_menu_grab_input), s);
a4ccabcf
AL
1610 g_signal_connect(s->notebook, "switch-page",
1611 G_CALLBACK(gd_change_page), s);
1612}
1613
400519d2 1614static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
a4ccabcf 1615{
bf9b255f 1616 GtkWidget *machine_menu;
a4ccabcf
AL
1617 GtkWidget *separator;
1618
bf9b255f 1619 machine_menu = gtk_menu_new();
400519d2 1620 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
30e8f22b
JK
1621
1622 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
bf9b255f 1623 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
30e8f22b
JK
1624
1625 separator = gtk_separator_menu_item_new();
bf9b255f 1626 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
30e8f22b 1627
9068f20d 1628 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
bf9b255f 1629 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
30e8f22b 1630
9068f20d 1631 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
bf9b255f 1632 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
30e8f22b
JK
1633
1634 separator = gtk_separator_menu_item_new();
bf9b255f 1635 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
a4ccabcf 1636
3d914488 1637 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
a4ccabcf 1638 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
30e8f22b 1639 "<QEMU>/Machine/Quit");
3d914488 1640 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
db1da1f2 1641 GDK_KEY_q, HOTKEY_MODIFIERS);
bf9b255f 1642 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
a4ccabcf 1643
bf9b255f
AL
1644 return machine_menu;
1645}
1646
e3500d1f
GH
1647static const DisplayChangeListenerOps dcl_ops = {
1648 .dpy_name = "gtk",
1649 .dpy_gfx_update = gd_update,
1650 .dpy_gfx_switch = gd_switch,
1651 .dpy_refresh = gd_refresh,
1652 .dpy_mouse_set = gd_mouse_set,
1653 .dpy_cursor_define = gd_cursor_define,
1654};
1655
1656static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
ed1132e4 1657 QemuConsole *con, int idx,
e3500d1f
GH
1658 GSList *group, GtkWidget *view_menu)
1659{
6a24ced5
GH
1660 Error *local_err = NULL;
1661 Object *obj;
1662
1663 obj = object_property_get_link(OBJECT(con), "device", &local_err);
1664 if (obj) {
cdeb7090
GH
1665 vc->label = g_strdup_printf("%s", object_get_typename(obj));
1666 } else {
1667 vc->label = g_strdup_printf("VGA");
6a24ced5
GH
1668 }
1669
e3500d1f
GH
1670 vc->s = s;
1671 vc->gfx.scale_x = 1.0;
1672 vc->gfx.scale_y = 1.0;
1673
1674 vc->gfx.drawing_area = gtk_drawing_area_new();
1675 gtk_widget_add_events(vc->gfx.drawing_area,
1676 GDK_POINTER_MOTION_MASK |
1677 GDK_BUTTON_PRESS_MASK |
1678 GDK_BUTTON_RELEASE_MASK |
1679 GDK_BUTTON_MOTION_MASK |
1680 GDK_ENTER_NOTIFY_MASK |
1681 GDK_LEAVE_NOTIFY_MASK |
1682 GDK_SCROLL_MASK |
1683 GDK_KEY_PRESS_MASK);
e3500d1f
GH
1684 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1685
1686 vc->type = GD_VC_GFX;
1687 vc->tab_item = vc->gfx.drawing_area;
1688 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
cdeb7090 1689 vc->tab_item, gtk_label_new(vc->label));
e3500d1f
GH
1690 gd_connect_vc_gfx_signals(vc);
1691
cdeb7090 1692 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
e3500d1f
GH
1693
1694 vc->gfx.dcl.ops = &dcl_ops;
1695 vc->gfx.dcl.con = con;
1696 register_displaychangelistener(&vc->gfx.dcl);
1697
1698 return group;
1699}
1700
400519d2 1701static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
bf9b255f
AL
1702{
1703 GSList *group = NULL;
1704 GtkWidget *view_menu;
1705 GtkWidget *separator;
ed1132e4
GH
1706 QemuConsole *con;
1707 int vc;
bf9b255f
AL
1708
1709 view_menu = gtk_menu_new();
400519d2 1710 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
a4ccabcf 1711
3d914488 1712 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
95414914
CR
1713
1714 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
1715 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
1716#if GTK_CHECK_VERSION(3, 8, 0)
1717 gtk_accel_label_set_accel(
1718 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
1719 GDK_KEY_f, HOTKEY_MODIFIERS);
1720#endif
bf9b255f 1721 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
c6158483
AL
1722
1723 separator = gtk_separator_menu_item_new();
bf9b255f 1724 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 1725
3d914488 1726 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
c6158483
AL
1727 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1728 "<QEMU>/View/Zoom In");
b1e749c0
JK
1729 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1730 HOTKEY_MODIFIERS);
bf9b255f 1731 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
c6158483 1732
3d914488 1733 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
c6158483
AL
1734 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1735 "<QEMU>/View/Zoom Out");
b1e749c0
JK
1736 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1737 HOTKEY_MODIFIERS);
bf9b255f 1738 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
c6158483 1739
3d914488 1740 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
c6158483
AL
1741 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1742 "<QEMU>/View/Zoom Fixed");
b1e749c0
JK
1743 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1744 HOTKEY_MODIFIERS);
bf9b255f 1745 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
c6158483 1746
834574ea 1747 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
bf9b255f 1748 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
c6158483
AL
1749
1750 separator = gtk_separator_menu_item_new();
bf9b255f 1751 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 1752
834574ea 1753 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
bf9b255f 1754 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
5104a1f6 1755
834574ea 1756 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
5104a1f6
AL
1757 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1758 "<QEMU>/View/Grab Input");
b1e749c0
JK
1759 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1760 HOTKEY_MODIFIERS);
bf9b255f 1761 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
5104a1f6 1762
a4ccabcf 1763 separator = gtk_separator_menu_item_new();
bf9b255f 1764 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 1765
e3500d1f 1766 /* gfx */
ed1132e4
GH
1767 for (vc = 0;; vc++) {
1768 con = qemu_console_lookup_by_index(vc);
1769 if (!con || !qemu_console_is_graphic(con)) {
1770 break;
1771 }
1772 group = gd_vc_gfx_init(s, &s->vc[vc], con,
1773 vc, group, view_menu);
1774 s->nb_vcs++;
1775 }
a4ccabcf 1776
ee5f31e4 1777#if defined(CONFIG_VTE)
e3500d1f 1778 /* vte */
ed1132e4 1779 gd_vcs_init(s, group, view_menu);
ee5f31e4 1780#endif
d861def3 1781
a4ccabcf 1782 separator = gtk_separator_menu_item_new();
bf9b255f 1783 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 1784
834574ea 1785 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
bf9b255f 1786 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
a4ccabcf 1787
cdeb7090
GH
1788 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1789 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
1790
bf9b255f
AL
1791 return view_menu;
1792}
1793
1794static void gd_create_menus(GtkDisplayState *s)
1795{
400519d2
CR
1796 s->accel_group = gtk_accel_group_new();
1797 s->machine_menu = gd_create_menu_machine(s);
1798 s->view_menu = gd_create_menu_view(s);
bf9b255f
AL
1799
1800 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
30e8f22b
JK
1801 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1802 s->machine_menu);
1803 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
a4ccabcf 1804
bf9b255f 1805 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
a4ccabcf
AL
1806 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1807 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
bf9b255f 1808
400519d2
CR
1809 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
1810 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
a4ccabcf
AL
1811}
1812
3158a348
BR
1813static void gd_set_keycode_type(GtkDisplayState *s)
1814{
0a337ed0 1815#ifdef GDK_WINDOWING_X11
3158a348 1816 GdkDisplay *display = gtk_widget_get_display(s->window);
0a337ed0
GH
1817 if (GDK_IS_X11_DISPLAY(display)) {
1818 Display *x11_display = gdk_x11_display_get_xdisplay(display);
1819 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
1820 XkbUseCoreKbd);
1821 char *keycodes = NULL;
1822
1823 if (desc && desc->names) {
1824 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
1825 }
1826 if (keycodes == NULL) {
1827 fprintf(stderr, "could not lookup keycode name\n");
1828 } else if (strstart(keycodes, "evdev", NULL)) {
1829 s->has_evdev = true;
1830 } else if (!strstart(keycodes, "xfree86", NULL)) {
1831 fprintf(stderr, "unknown keycodes `%s', please report to "
1832 "qemu-devel@nongnu.org\n", keycodes);
1833 }
84961407
CF
1834
1835 if (desc) {
1836 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
1837 }
1838 if (keycodes) {
1839 XFree(keycodes);
1840 }
3158a348
BR
1841 }
1842#endif
1843}
1844
881249c7 1845void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
a4ccabcf
AL
1846{
1847 GtkDisplayState *s = g_malloc0(sizeof(*s));
d819cdcc 1848 char *filename;
a4ccabcf
AL
1849
1850 gtk_init(NULL, NULL);
1851
a4ccabcf 1852 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
51572ab0
DB
1853#if GTK_CHECK_VERSION(3, 2, 0)
1854 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1855#else
a4ccabcf 1856 s->vbox = gtk_vbox_new(FALSE, 0);
51572ab0 1857#endif
a4ccabcf 1858 s->notebook = gtk_notebook_new();
a4ccabcf
AL
1859 s->menu_bar = gtk_menu_bar_new();
1860
c6158483 1861 s->free_scale = FALSE;
a4ccabcf 1862
834574ea
AL
1863 setlocale(LC_ALL, "");
1864 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1865 textdomain("qemu");
1866
a4ccabcf
AL
1867 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1868
1869 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1870 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1871 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1872
f7da9c17 1873 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
d819cdcc
SW
1874 if (filename) {
1875 GError *error = NULL;
1876 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1877 if (pixbuf) {
1878 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1879 } else {
1880 g_error_free(error);
1881 }
1882 g_free(filename);
1883 }
1884
a4ccabcf
AL
1885 gd_create_menus(s);
1886
1887 gd_connect_signals(s);
1888
a4ccabcf
AL
1889 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1890 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1891
a4ccabcf
AL
1892 gd_update_caption(s);
1893
1894 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1895 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1896
1897 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1898
1899 gtk_widget_show_all(s->window);
1900
6fa27697
GH
1901#ifdef VTE_RESIZE_HACK
1902 {
1903 VirtualConsole *cur = gd_vc_find_current(s);
1904 int i;
1905
1906 for (i = 0; i < s->nb_vcs; i++) {
1907 VirtualConsole *vc = &s->vc[i];
1908 if (vc && vc->type == GD_VC_VTE && vc != cur) {
1909 gtk_widget_hide(vc->vte.terminal);
1910 }
1911 }
1912 gd_update_windowsize(cur);
1913 }
1914#endif
1915
787ba4f0
PW
1916 if (full_screen) {
1917 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1918 }
881249c7
JK
1919 if (grab_on_hover) {
1920 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1921 }
787ba4f0 1922
3158a348 1923 gd_set_keycode_type(s);
a4ccabcf 1924}
ee5f31e4
GH
1925
1926void early_gtk_display_init(void)
1927{
1928#if defined(CONFIG_VTE)
1929 register_vc_handler(gd_vc_handler);
1930#endif
1931}