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