]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk.c
char: use a feature bit for replay
[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
e16f4c87 37#include "qemu/osdep.h"
c95e3080 38#include "qemu-common.h"
f348b6d1 39#include "qemu/cutils.h"
c95e3080 40
dc7ff344
GH
41#include "ui/console.h"
42#include "ui/gtk.h"
c95e3080 43
834574ea 44#include <glib/gi18n.h>
3f58eade 45#include <locale.h>
bbbf9bfb 46#if defined(CONFIG_VTE)
a4ccabcf 47#include <vte/vte.h>
bbbf9bfb 48#endif
a4ccabcf
AL
49#include <math.h>
50
ef0dd982 51#include "trace.h"
af98ba92 52#include "ui/input.h"
a4ccabcf
AL
53#include "sysemu/sysemu.h"
54#include "qmp-commands.h"
55#include "x_keymap.h"
56#include "keymaps.h"
dccfcd0e 57#include "sysemu/char.h"
6a24ced5 58#include "qom/object.h"
a4ccabcf 59
d861def3 60#define MAX_VCS 10
82fc1809
GH
61#define VC_WINDOW_X_MIN 320
62#define VC_WINDOW_Y_MIN 240
63#define VC_TERM_X_MIN 80
64#define VC_TERM_Y_MIN 25
65#define VC_SCALE_MIN 0.25
66#define VC_SCALE_STEP 0.25
d861def3 67
bbbf9bfb
SW
68#if !defined(CONFIG_VTE)
69# define VTE_CHECK_VERSION(a, b, c) 0
70#endif
cba68834 71
6fa27697
GH
72#if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
73/*
74 * The gtk2 vte terminal widget seriously messes up the window resize
75 * for some reason. You basically can't make the qemu window smaller
76 * any more because the toplevel window geoemtry hints are overridden.
77 *
78 * Workaround that by hiding all vte widgets, except the one in the
79 * current tab.
80 *
81 * Luckily everything works smooth in gtk3.
82 */
83# define VTE_RESIZE_HACK 1
84#endif
85
ef6413a2
DB
86#if !GTK_CHECK_VERSION(2, 20, 0)
87#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
88#endif
89
0a337ed0
GH
90#ifndef GDK_IS_X11_DISPLAY
91#define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
92#endif
a8ffb372
DB
93#ifndef GDK_IS_WAYLAND_DISPLAY
94#define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
95#endif
0a337ed0
GH
96#ifndef GDK_IS_WIN32_DISPLAY
97#define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
98#endif
99
204af15b 100#if !GTK_CHECK_VERSION(2, 22, 0)
bc0477c7
DB
101#define GDK_KEY_0 GDK_0
102#define GDK_KEY_1 GDK_1
103#define GDK_KEY_2 GDK_2
104#define GDK_KEY_f GDK_f
105#define GDK_KEY_g GDK_g
db1da1f2 106#define GDK_KEY_q GDK_q
bc0477c7
DB
107#define GDK_KEY_plus GDK_plus
108#define GDK_KEY_minus GDK_minus
dc520171 109#define GDK_KEY_Pause GDK_Pause
204af15b 110#define GDK_KEY_Delete GDK_Delete
bc0477c7 111#endif
ef6413a2 112
c8f3f17c
DB
113/* Some older mingw versions lack this constant or have
114 * it conditionally defined */
115#ifdef _WIN32
116# ifndef MAPVK_VK_TO_VSC
117# define MAPVK_VK_TO_VSC 0
118# endif
119#endif
120
121
b1e749c0 122#define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
b1e749c0 123
6db253ca
JK
124static const int modifier_keycode[] = {
125 /* shift, control, alt keys, meta keys, both left & right */
126 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
127};
128
e3500d1f 129struct GtkDisplayState {
a4ccabcf
AL
130 GtkWidget *window;
131
132 GtkWidget *menu_bar;
133
73d4dc71
AL
134 GtkAccelGroup *accel_group;
135
30e8f22b
JK
136 GtkWidget *machine_menu_item;
137 GtkWidget *machine_menu;
138 GtkWidget *pause_item;
139 GtkWidget *reset_item;
140 GtkWidget *powerdown_item;
a4ccabcf
AL
141 GtkWidget *quit_item;
142
143 GtkWidget *view_menu_item;
144 GtkWidget *view_menu;
c6158483 145 GtkWidget *full_screen_item;
44b31e0b 146 GtkWidget *copy_item;
c6158483
AL
147 GtkWidget *zoom_in_item;
148 GtkWidget *zoom_out_item;
149 GtkWidget *zoom_fixed_item;
150 GtkWidget *zoom_fit_item;
5104a1f6
AL
151 GtkWidget *grab_item;
152 GtkWidget *grab_on_hover_item;
a4ccabcf 153
d861def3
AL
154 int nb_vcs;
155 VirtualConsole vc[MAX_VCS];
156
a4ccabcf 157 GtkWidget *show_tabs_item;
cdeb7090 158 GtkWidget *untabify_item;
a4ccabcf
AL
159
160 GtkWidget *vbox;
161 GtkWidget *notebook;
a4ccabcf 162 int button_mask;
e61031cd 163 gboolean last_set;
a4ccabcf
AL
164 int last_x;
165 int last_y;
ecce1929
TI
166 int grab_x_root;
167 int grab_y_root;
4c638e2e
GH
168 VirtualConsole *kbd_owner;
169 VirtualConsole *ptr_owner;
a4ccabcf 170
c6158483 171 gboolean full_screen;
a4ccabcf
AL
172
173 GdkCursor *null_cursor;
174 Notifier mouse_mode_notifier;
c6158483 175 gboolean free_scale;
30e8f22b
JK
176
177 bool external_pause_update;
6db253ca
JK
178
179 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
3158a348 180 bool has_evdev;
1a01716a 181 bool ignore_keys;
e3500d1f 182};
a4ccabcf 183
d531deef 184static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
2884cf5b 185static void gd_ungrab_pointer(GtkDisplayState *s);
d531deef 186static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
aa4f4058 187static void gd_ungrab_keyboard(GtkDisplayState *s);
2884cf5b 188
a4ccabcf
AL
189/** Utility Functions **/
190
271a25c0
GH
191static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
192{
193 VirtualConsole *vc;
194 gint i;
195
196 for (i = 0; i < s->nb_vcs; i++) {
197 vc = &s->vc[i];
198 if (gtk_check_menu_item_get_active
199 (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
200 return vc;
201 }
202 }
203 return NULL;
204}
205
206static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
207{
208 VirtualConsole *vc;
209 gint i, p;
210
211 for (i = 0; i < s->nb_vcs; i++) {
212 vc = &s->vc[i];
213 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
214 if (p == page) {
215 return vc;
216 }
217 }
218 return NULL;
219}
220
e3500d1f
GH
221static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
222{
223 gint page;
224
225 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
226 return gd_vc_find_by_page(s, page);
227}
228
5104a1f6
AL
229static bool gd_is_grab_active(GtkDisplayState *s)
230{
231 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
232}
233
234static bool gd_grab_on_hover(GtkDisplayState *s)
235{
236 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
237}
238
e3500d1f 239static void gd_update_cursor(VirtualConsole *vc)
a4ccabcf 240{
e3500d1f 241 GtkDisplayState *s = vc->s;
a4ccabcf 242 GdkWindow *window;
a4ccabcf 243
f8c223f6
GH
244 if (vc->type != GD_VC_GFX ||
245 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
e3500d1f
GH
246 return;
247 }
a4ccabcf 248
4cdfc935
HP
249 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
250 return;
251 }
252
e3500d1f 253 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
2884cf5b 254 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
a4ccabcf
AL
255 gdk_window_set_cursor(window, s->null_cursor);
256 } else {
257 gdk_window_set_cursor(window, NULL);
258 }
259}
260
261static void gd_update_caption(GtkDisplayState *s)
262{
263 const char *status = "";
4eeaa3a8 264 gchar *prefix;
a4ccabcf 265 gchar *title;
5104a1f6 266 const char *grab = "";
30e8f22b 267 bool is_paused = !runstate_is_running();
4eeaa3a8
GH
268 int i;
269
270 if (qemu_name) {
271 prefix = g_strdup_printf("QEMU (%s)", qemu_name);
272 } else {
273 prefix = g_strdup_printf("QEMU");
274 }
5104a1f6 275
4eeaa3a8
GH
276 if (s->ptr_owner != NULL &&
277 s->ptr_owner->window == NULL) {
d8da9ee8 278 grab = _(" - Press Ctrl+Alt+G to release grab");
5104a1f6 279 }
a4ccabcf 280
30e8f22b 281 if (is_paused) {
d8da9ee8 282 status = _(" [Paused]");
a4ccabcf 283 }
30e8f22b
JK
284 s->external_pause_update = true;
285 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
286 is_paused);
287 s->external_pause_update = false;
a4ccabcf 288
4eeaa3a8 289 title = g_strdup_printf("%s%s%s", prefix, status, grab);
a4ccabcf 290 gtk_window_set_title(GTK_WINDOW(s->window), title);
a4ccabcf 291 g_free(title);
4eeaa3a8
GH
292
293 for (i = 0; i < s->nb_vcs; i++) {
294 VirtualConsole *vc = &s->vc[i];
295
296 if (!vc->window) {
297 continue;
298 }
299 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
300 vc == s->kbd_owner ? " +kbd" : "",
301 vc == s->ptr_owner ? " +ptr" : "");
302 gtk_window_set_title(GTK_WINDOW(vc->window), title);
303 g_free(title);
304 }
305
306 g_free(prefix);
a4ccabcf
AL
307}
308
82fc1809 309static void gd_update_geometry_hints(VirtualConsole *vc)
9d9801cf 310{
e3500d1f 311 GtkDisplayState *s = vc->s;
82fc1809
GH
312 GdkWindowHints mask = 0;
313 GdkGeometry geo = {};
314 GtkWidget *geo_widget = NULL;
315 GtkWindow *geo_window;
e3500d1f 316
82fc1809 317 if (vc->type == GD_VC_GFX) {
f98f43ea
GH
318 if (!vc->gfx.ds) {
319 return;
320 }
82fc1809
GH
321 if (s->free_scale) {
322 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
323 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
324 mask |= GDK_HINT_MIN_SIZE;
325 } else {
326 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
327 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
328 mask |= GDK_HINT_MIN_SIZE;
329 }
330 geo_widget = vc->gfx.drawing_area;
331 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
9d9801cf 332
82fc1809
GH
333#if defined(CONFIG_VTE)
334 } else if (vc->type == GD_VC_VTE) {
335 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
6978dc4a 336 GtkBorder padding = { 0 };
82fc1809 337
84e2dc4b 338#if VTE_CHECK_VERSION(0, 37, 0)
84e2dc4b
CR
339 gtk_style_context_get_padding(
340 gtk_widget_get_style_context(vc->vte.terminal),
341 gtk_widget_get_state_flags(vc->vte.terminal),
342 &padding);
84e2dc4b 343#else
6978dc4a
AG
344 {
345 GtkBorder *ib = NULL;
346 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
347 if (ib) {
348 padding = *ib;
349 gtk_border_free(ib);
350 }
351 }
84e2dc4b
CR
352#endif
353
82fc1809
GH
354 geo.width_inc = vte_terminal_get_char_width(term);
355 geo.height_inc = vte_terminal_get_char_height(term);
356 mask |= GDK_HINT_RESIZE_INC;
357 geo.base_width = geo.width_inc;
358 geo.base_height = geo.height_inc;
359 mask |= GDK_HINT_BASE_SIZE;
360 geo.min_width = geo.width_inc * VC_TERM_X_MIN;
361 geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
362 mask |= GDK_HINT_MIN_SIZE;
84e2dc4b 363
6978dc4a
AG
364 geo.base_width += padding.left + padding.right;
365 geo.base_height += padding.top + padding.bottom;
366 geo.min_width += padding.left + padding.right;
367 geo.min_height += padding.top + padding.bottom;
82fc1809
GH
368 geo_widget = vc->vte.terminal;
369#endif
9d9801cf 370 }
82fc1809
GH
371
372 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
373 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
374}
375
97edf3bd 376void gd_update_windowsize(VirtualConsole *vc)
82fc1809
GH
377{
378 GtkDisplayState *s = vc->s;
379
380 gd_update_geometry_hints(vc);
381
382 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
383 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
384 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
aa0a55d4 385 }
9d9801cf
GH
386}
387
e3500d1f 388static void gd_update_full_redraw(VirtualConsole *vc)
9d9801cf 389{
e3500d1f 390 GtkWidget *area = vc->gfx.drawing_area;
9d9801cf 391 int ww, wh;
e3500d1f 392 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
925a0400
GH
393#if defined(CONFIG_GTK_GL)
394 if (vc->gfx.gls) {
395 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
396 return;
397 }
398#endif
e3500d1f 399 gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
9d9801cf
GH
400}
401
6db253ca
JK
402static void gtk_release_modifiers(GtkDisplayState *s)
403{
e3500d1f 404 VirtualConsole *vc = gd_vc_find_current(s);
6db253ca
JK
405 int i, keycode;
406
f8c223f6
GH
407 if (vc->type != GD_VC_GFX ||
408 !qemu_console_is_graphic(vc->gfx.dcl.con)) {
6db253ca
JK
409 return;
410 }
411 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
412 keycode = modifier_keycode[i];
413 if (!s->modifier_pressed[i]) {
414 continue;
415 }
e3500d1f 416 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
6db253ca
JK
417 s->modifier_pressed[i] = false;
418 }
419}
420
316cb068
GH
421static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
422 GtkWidget *widget)
423{
424 g_object_ref(G_OBJECT(widget));
425 gtk_container_remove(GTK_CONTAINER(from), widget);
426 gtk_container_add(GTK_CONTAINER(to), widget);
427 g_object_unref(G_OBJECT(widget));
428}
429
a4ccabcf
AL
430/** DisplayState Callbacks **/
431
7c20b4a3 432static void gd_update(DisplayChangeListener *dcl,
bc2ed970 433 int x, int y, int w, int h)
a4ccabcf 434{
e3500d1f 435 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
f8c223f6 436 GdkWindow *win;
a4ccabcf 437 int x1, x2, y1, y2;
c6158483
AL
438 int mx, my;
439 int fbw, fbh;
440 int ww, wh;
a4ccabcf 441
74444bc1 442 trace_gd_update(vc->label, x, y, w, h);
a4ccabcf 443
4cdfc935
HP
444 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
445 return;
446 }
447
e3500d1f
GH
448 if (vc->gfx.convert) {
449 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
450 NULL, vc->gfx.convert,
f0875536
GH
451 x, y, 0, 0, x, y, w, h);
452 }
453
e3500d1f
GH
454 x1 = floor(x * vc->gfx.scale_x);
455 y1 = floor(y * vc->gfx.scale_y);
a4ccabcf 456
e3500d1f
GH
457 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
458 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
a4ccabcf 459
e3500d1f
GH
460 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
461 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
c6158483 462
f8c223f6
GH
463 win = gtk_widget_get_window(vc->gfx.drawing_area);
464 if (!win) {
465 return;
466 }
467 gdk_drawable_get_size(win, &ww, &wh);
c6158483
AL
468
469 mx = my = 0;
470 if (ww > fbw) {
471 mx = (ww - fbw) / 2;
472 }
473 if (wh > fbh) {
474 my = (wh - fbh) / 2;
475 }
476
e3500d1f
GH
477 gtk_widget_queue_draw_area(vc->gfx.drawing_area,
478 mx + x1, my + y1, (x2 - x1), (y2 - y1));
a4ccabcf
AL
479}
480
bc2ed970 481static void gd_refresh(DisplayChangeListener *dcl)
a4ccabcf 482{
284d1c6b 483 graphic_hw_update(dcl->con);
a4ccabcf
AL
484}
485
b087143b 486#if GTK_CHECK_VERSION(3, 0, 0)
bb732ee7
CR
487static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
488{
489#if GTK_CHECK_VERSION(3, 20, 0)
490 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
491#else
492 return gdk_device_manager_get_client_pointer(
493 gdk_display_get_device_manager(dpy));
494#endif
495}
496
b087143b
IM
497static void gd_mouse_set(DisplayChangeListener *dcl,
498 int x, int y, int visible)
499{
e3500d1f 500 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
b087143b 501 GdkDisplay *dpy;
b087143b
IM
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);
e3500d1f 509 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
b087143b 510 x, y, &x_root, &y_root);
bb732ee7 511 gdk_device_warp(gd_get_pointer(dpy),
e3500d1f 512 gtk_widget_get_screen(vc->gfx.drawing_area),
298526fe 513 x_root, y_root);
1271f7f7
GH
514 vc->s->last_x = x;
515 vc->s->last_y = y;
b087143b
IM
516}
517#else
9697f5d2
GH
518static void gd_mouse_set(DisplayChangeListener *dcl,
519 int x, int y, int visible)
520{
e3500d1f 521 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9697f5d2
GH
522 gint x_root, y_root;
523
2bda6602
CR
524 if (qemu_input_is_absolute()) {
525 return;
526 }
527
e3500d1f 528 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
9697f5d2 529 x, y, &x_root, &y_root);
e3500d1f
GH
530 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
531 gtk_widget_get_screen(vc->gfx.drawing_area),
9697f5d2
GH
532 x_root, y_root);
533}
b087143b 534#endif
9697f5d2
GH
535
536static void gd_cursor_define(DisplayChangeListener *dcl,
537 QEMUCursor *c)
538{
e3500d1f 539 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9697f5d2
GH
540 GdkPixbuf *pixbuf;
541 GdkCursor *cursor;
542
4cdfc935
HP
543 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
544 return;
545 }
546
9697f5d2
GH
547 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
548 GDK_COLORSPACE_RGB, true, 8,
549 c->width, c->height, c->width * 4,
550 NULL, NULL);
e3500d1f
GH
551 cursor = gdk_cursor_new_from_pixbuf
552 (gtk_widget_get_display(vc->gfx.drawing_area),
553 pixbuf, c->hot_x, c->hot_y);
554 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
9697f5d2 555 g_object_unref(pixbuf);
030b4b7d 556#if !GTK_CHECK_VERSION(3, 0, 0)
17139240 557 gdk_cursor_unref(cursor);
030b4b7d
SW
558#else
559 g_object_unref(cursor);
560#endif
9697f5d2
GH
561}
562
c12aeb86 563static void gd_switch(DisplayChangeListener *dcl,
c12aeb86 564 DisplaySurface *surface)
a4ccabcf 565{
e3500d1f 566 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9d9801cf 567 bool resized = true;
a4ccabcf 568
f98f43ea
GH
569 trace_gd_switch(vc->label,
570 surface ? surface_width(surface) : 0,
571 surface ? surface_height(surface) : 0);
a4ccabcf 572
e3500d1f
GH
573 if (vc->gfx.surface) {
574 cairo_surface_destroy(vc->gfx.surface);
f98f43ea
GH
575 vc->gfx.surface = NULL;
576 }
577 if (vc->gfx.convert) {
578 pixman_image_unref(vc->gfx.convert);
579 vc->gfx.convert = NULL;
a4ccabcf
AL
580 }
581
f98f43ea 582 if (vc->gfx.ds && surface &&
e3500d1f
GH
583 surface_width(vc->gfx.ds) == surface_width(surface) &&
584 surface_height(vc->gfx.ds) == surface_height(surface)) {
9d9801cf
GH
585 resized = false;
586 }
e3500d1f 587 vc->gfx.ds = surface;
a4ccabcf 588
f98f43ea
GH
589 if (!surface) {
590 return;
f0875536 591 }
a4ccabcf 592
f0875536
GH
593 if (surface->format == PIXMAN_x8r8g8b8) {
594 /*
595 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
596 *
597 * No need to convert, use surface directly. Should be the
598 * common case as this is qemu_default_pixelformat(32) too.
599 */
e3500d1f 600 vc->gfx.surface = cairo_image_surface_create_for_data
f0875536
GH
601 (surface_data(surface),
602 CAIRO_FORMAT_RGB24,
603 surface_width(surface),
604 surface_height(surface),
605 surface_stride(surface));
606 } else {
607 /* Must convert surface, use pixman to do it. */
e3500d1f
GH
608 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
609 surface_width(surface),
610 surface_height(surface),
611 NULL, 0);
612 vc->gfx.surface = cairo_image_surface_create_for_data
613 ((void *)pixman_image_get_data(vc->gfx.convert),
f0875536 614 CAIRO_FORMAT_RGB24,
e3500d1f
GH
615 pixman_image_get_width(vc->gfx.convert),
616 pixman_image_get_height(vc->gfx.convert),
617 pixman_image_get_stride(vc->gfx.convert));
618 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
619 NULL, vc->gfx.convert,
f0875536 620 0, 0, 0, 0, 0, 0,
e3500d1f
GH
621 pixman_image_get_width(vc->gfx.convert),
622 pixman_image_get_height(vc->gfx.convert));
f0875536 623 }
c6158483 624
9d9801cf 625 if (resized) {
e3500d1f 626 gd_update_windowsize(vc);
9d9801cf 627 } else {
e3500d1f 628 gd_update_full_redraw(vc);
c6158483 629 }
a4ccabcf
AL
630}
631
97edf3bd
GH
632static const DisplayChangeListenerOps dcl_ops = {
633 .dpy_name = "gtk",
634 .dpy_gfx_update = gd_update,
635 .dpy_gfx_switch = gd_switch,
636 .dpy_gfx_check_format = qemu_pixman_check_format,
637 .dpy_refresh = gd_refresh,
638 .dpy_mouse_set = gd_mouse_set,
639 .dpy_cursor_define = gd_cursor_define,
640};
641
642
643#if defined(CONFIG_OPENGL)
644
645/** DisplayState Callbacks (opengl version) **/
646
925a0400
GH
647#if defined(CONFIG_GTK_GL)
648
649static const DisplayChangeListenerOps dcl_gl_area_ops = {
650 .dpy_name = "gtk-egl",
651 .dpy_gfx_update = gd_gl_area_update,
652 .dpy_gfx_switch = gd_gl_area_switch,
653 .dpy_gfx_check_format = console_gl_check_format,
654 .dpy_refresh = gd_gl_area_refresh,
655 .dpy_mouse_set = gd_mouse_set,
656 .dpy_cursor_define = gd_cursor_define,
657
658 .dpy_gl_ctx_create = gd_gl_area_create_context,
659 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context,
660 .dpy_gl_ctx_make_current = gd_gl_area_make_current,
661 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context,
662 .dpy_gl_scanout = gd_gl_area_scanout,
663 .dpy_gl_update = gd_gl_area_scanout_flush,
664};
665
666#else
667
97edf3bd
GH
668static const DisplayChangeListenerOps dcl_egl_ops = {
669 .dpy_name = "gtk-egl",
670 .dpy_gfx_update = gd_egl_update,
671 .dpy_gfx_switch = gd_egl_switch,
672 .dpy_gfx_check_format = console_gl_check_format,
673 .dpy_refresh = gd_egl_refresh,
674 .dpy_mouse_set = gd_mouse_set,
675 .dpy_cursor_define = gd_cursor_define,
4782aeb7
GH
676
677 .dpy_gl_ctx_create = gd_egl_create_context,
678 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
679 .dpy_gl_ctx_make_current = gd_egl_make_current,
680 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
681 .dpy_gl_scanout = gd_egl_scanout,
682 .dpy_gl_update = gd_egl_scanout_flush,
97edf3bd
GH
683};
684
925a0400
GH
685#endif /* CONFIG_GTK_GL */
686#endif /* CONFIG_OPENGL */
97edf3bd 687
a4ccabcf
AL
688/** QEMU Events **/
689
690static void gd_change_runstate(void *opaque, int running, RunState state)
691{
692 GtkDisplayState *s = opaque;
693
694 gd_update_caption(s);
695}
696
697static void gd_mouse_mode_change(Notifier *notify, void *data)
698{
800b0e81 699 GtkDisplayState *s;
99623c90 700 int i;
800b0e81
TI
701
702 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
703 /* release the grab at switching to absolute mode */
704 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
705 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
706 FALSE);
707 }
99623c90
GH
708 for (i = 0; i < s->nb_vcs; i++) {
709 VirtualConsole *vc = &s->vc[i];
710 gd_update_cursor(vc);
711 }
a4ccabcf
AL
712}
713
714/** GTK Events **/
715
716static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
717 void *opaque)
718{
719 GtkDisplayState *s = opaque;
e3500d1f 720 int i;
a4ccabcf
AL
721
722 if (!no_quit) {
e3500d1f
GH
723 for (i = 0; i < s->nb_vcs; i++) {
724 if (s->vc[i].type != GD_VC_GFX) {
725 continue;
726 }
727 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
728 }
a4ccabcf
AL
729 qmp_quit(NULL);
730 return FALSE;
731 }
732
733 return TRUE;
734}
735
925a0400
GH
736static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
737{
738 QemuUIInfo info;
739
740 memset(&info, 0, sizeof(info));
741 info.width = width;
742 info.height = height;
743 dpy_set_ui_info(vc->gfx.dcl.con, &info);
744}
745
746#if defined(CONFIG_GTK_GL)
747
748static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
749 void *opaque)
750{
751 VirtualConsole *vc = opaque;
752
753 if (vc->gfx.gls) {
754 gd_gl_area_draw(vc);
755 }
756 return TRUE;
757}
758
759static void gd_resize_event(GtkGLArea *area,
760 gint width, gint height, gpointer *opaque)
761{
762 VirtualConsole *vc = (void *)opaque;
763
764 gd_set_ui_info(vc, width, height);
765}
766
767#endif
768
a4ccabcf
AL
769static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
770{
e3500d1f
GH
771 VirtualConsole *vc = opaque;
772 GtkDisplayState *s = vc->s;
c6158483 773 int mx, my;
a4ccabcf
AL
774 int ww, wh;
775 int fbw, fbh;
776
97edf3bd
GH
777#if defined(CONFIG_OPENGL)
778 if (vc->gfx.gls) {
925a0400
GH
779#if defined(CONFIG_GTK_GL)
780 /* invoke render callback please */
781 return FALSE;
782#else
97edf3bd
GH
783 gd_egl_draw(vc);
784 return TRUE;
925a0400 785#endif
97edf3bd
GH
786 }
787#endif
788
c6158483
AL
789 if (!gtk_widget_get_realized(widget)) {
790 return FALSE;
791 }
f98f43ea
GH
792 if (!vc->gfx.ds) {
793 return FALSE;
794 }
c6158483 795
e3500d1f
GH
796 fbw = surface_width(vc->gfx.ds);
797 fbh = surface_height(vc->gfx.ds);
a4ccabcf
AL
798
799 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
800
c6158483 801 if (s->full_screen) {
e3500d1f
GH
802 vc->gfx.scale_x = (double)ww / fbw;
803 vc->gfx.scale_y = (double)wh / fbh;
c6158483
AL
804 } else if (s->free_scale) {
805 double sx, sy;
806
807 sx = (double)ww / fbw;
808 sy = (double)wh / fbh;
809
e3500d1f 810 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
a4ccabcf
AL
811 }
812
e3500d1f
GH
813 fbw *= vc->gfx.scale_x;
814 fbh *= vc->gfx.scale_y;
5104a1f6 815
c6158483
AL
816 mx = my = 0;
817 if (ww > fbw) {
818 mx = (ww - fbw) / 2;
819 }
820 if (wh > fbh) {
821 my = (wh - fbh) / 2;
822 }
823
824 cairo_rectangle(cr, 0, 0, ww, wh);
825
826 /* Optionally cut out the inner area where the pixmap
827 will be drawn. This avoids 'flashing' since we're
828 not double-buffering. Note we're using the undocumented
829 behaviour of drawing the rectangle from right to left
830 to cut out the whole */
831 cairo_rectangle(cr, mx + fbw, my,
832 -1 * fbw, fbh);
833 cairo_fill(cr);
834
e3500d1f
GH
835 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
836 cairo_set_source_surface(cr, vc->gfx.surface,
837 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
a4ccabcf
AL
838 cairo_paint(cr);
839
840 return TRUE;
841}
842
fe43bca8 843#if !GTK_CHECK_VERSION(3, 0, 0)
a4ccabcf
AL
844static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
845 void *opaque)
846{
847 cairo_t *cr;
848 gboolean ret;
849
850 cr = gdk_cairo_create(gtk_widget_get_window(widget));
851 cairo_rectangle(cr,
852 expose->area.x,
853 expose->area.y,
854 expose->area.width,
855 expose->area.height);
856 cairo_clip(cr);
857
858 ret = gd_draw_event(widget, cr, opaque);
859
860 cairo_destroy(cr);
861
862 return ret;
863}
fe43bca8 864#endif
a4ccabcf
AL
865
866static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
867 void *opaque)
868{
e3500d1f
GH
869 VirtualConsole *vc = opaque;
870 GtkDisplayState *s = vc->s;
a4ccabcf 871 int x, y;
c6158483
AL
872 int mx, my;
873 int fbh, fbw;
874 int ww, wh;
875
f98f43ea
GH
876 if (!vc->gfx.ds) {
877 return TRUE;
878 }
879
e3500d1f
GH
880 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
881 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
a4ccabcf 882
e3500d1f
GH
883 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
884 &ww, &wh);
c6158483
AL
885
886 mx = my = 0;
887 if (ww > fbw) {
888 mx = (ww - fbw) / 2;
889 }
890 if (wh > fbh) {
891 my = (wh - fbh) / 2;
892 }
893
e3500d1f
GH
894 x = (motion->x - mx) / vc->gfx.scale_x;
895 y = (motion->y - my) / vc->gfx.scale_y;
c6158483 896
192f81bf 897 if (qemu_input_is_absolute()) {
e61031cd 898 if (x < 0 || y < 0 ||
e3500d1f
GH
899 x >= surface_width(vc->gfx.ds) ||
900 y >= surface_height(vc->gfx.ds)) {
e61031cd
TI
901 return TRUE;
902 }
e3500d1f
GH
903 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
904 surface_width(vc->gfx.ds));
905 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
906 surface_height(vc->gfx.ds));
192f81bf 907 qemu_input_event_sync();
2884cf5b 908 } else if (s->last_set && s->ptr_owner == vc) {
e3500d1f
GH
909 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
910 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
192f81bf 911 qemu_input_event_sync();
a4ccabcf 912 }
a4ccabcf
AL
913 s->last_x = x;
914 s->last_y = y;
e61031cd 915 s->last_set = TRUE;
a4ccabcf 916
2884cf5b 917 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
e3500d1f 918 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
76d8f93b
AG
919 int screen_width, screen_height;
920
5104a1f6
AL
921 int x = (int)motion->x_root;
922 int y = (int)motion->y_root;
923
76d8f93b
AG
924#if GTK_CHECK_VERSION(3, 22, 0)
925 {
926 GdkDisplay *dpy = gtk_widget_get_display(widget);
927 GdkWindow *win = gtk_widget_get_window(widget);
928 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
929 GdkRectangle geometry;
930 gdk_monitor_get_geometry(monitor, &geometry);
931 screen_width = geometry.width;
932 screen_height = geometry.height;
933 }
934#else
935 {
936 screen_width = gdk_screen_get_width(screen);
937 screen_height = gdk_screen_get_height(screen);
938 }
939#endif
940
5104a1f6
AL
941 /* In relative mode check to see if client pointer hit
942 * one of the screen edges, and if so move it back by
943 * 200 pixels. This is important because the pointer
944 * in the server doesn't correspond 1-for-1, and so
945 * may still be only half way across the screen. Without
946 * this warp, the server pointer would thus appear to hit
947 * an invisible wall */
948 if (x == 0) {
949 x += 200;
950 }
951 if (y == 0) {
952 y += 200;
953 }
76d8f93b 954 if (x == (screen_width - 1)) {
5104a1f6
AL
955 x -= 200;
956 }
76d8f93b 957 if (y == (screen_height - 1)) {
5104a1f6
AL
958 y -= 200;
959 }
960
961 if (x != (int)motion->x_root || y != (int)motion->y_root) {
8906de76
DB
962#if GTK_CHECK_VERSION(3, 0, 0)
963 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
964 gdk_device_warp(dev, screen, x, y);
965#else
966 GdkDisplay *display = gtk_widget_get_display(widget);
5104a1f6 967 gdk_display_warp_pointer(display, screen, x, y);
8906de76 968#endif
e61031cd 969 s->last_set = FALSE;
5104a1f6
AL
970 return FALSE;
971 }
972 }
a4ccabcf
AL
973 return TRUE;
974}
975
976static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
977 void *opaque)
978{
e3500d1f
GH
979 VirtualConsole *vc = opaque;
980 GtkDisplayState *s = vc->s;
192f81bf 981 InputButton btn;
a4ccabcf 982
800b0e81
TI
983 /* implicitly grab the input at the first click in the relative mode */
984 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
2884cf5b 985 !qemu_input_is_absolute() && s->ptr_owner != vc) {
2884cf5b
GH
986 if (!vc->window) {
987 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
988 TRUE);
989 } else {
d531deef 990 gd_grab_pointer(vc, "relative-mode-click");
2884cf5b 991 }
800b0e81
TI
992 return TRUE;
993 }
994
a4ccabcf 995 if (button->button == 1) {
192f81bf 996 btn = INPUT_BUTTON_LEFT;
a4ccabcf 997 } else if (button->button == 2) {
192f81bf 998 btn = INPUT_BUTTON_MIDDLE;
a4ccabcf 999 } else if (button->button == 3) {
192f81bf 1000 btn = INPUT_BUTTON_RIGHT;
a4ccabcf 1001 } else {
192f81bf 1002 return TRUE;
a4ccabcf
AL
1003 }
1004
e3500d1f
GH
1005 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
1006 button->type == GDK_BUTTON_PRESS);
192f81bf 1007 qemu_input_event_sync();
a4ccabcf
AL
1008 return TRUE;
1009}
1010
d58b9122
JK
1011static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
1012 void *opaque)
1013{
e3500d1f 1014 VirtualConsole *vc = opaque;
d58b9122
JK
1015 InputButton btn;
1016
1017 if (scroll->direction == GDK_SCROLL_UP) {
f22d0af0 1018 btn = INPUT_BUTTON_WHEEL_UP;
d58b9122 1019 } else if (scroll->direction == GDK_SCROLL_DOWN) {
f22d0af0 1020 btn = INPUT_BUTTON_WHEEL_DOWN;
d58b9122
JK
1021 } else {
1022 return TRUE;
1023 }
1024
e3500d1f 1025 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
d58b9122 1026 qemu_input_event_sync();
e3500d1f 1027 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
d58b9122
JK
1028 qemu_input_event_sync();
1029 return TRUE;
1030}
1031
0a337ed0 1032static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
a4ccabcf 1033{
932f2d7e 1034 int qemu_keycode;
a4ccabcf 1035
0a337ed0
GH
1036#ifdef GDK_WINDOWING_WIN32
1037 if (GDK_IS_WIN32_DISPLAY(dpy)) {
1038 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
1039 switch (qemu_keycode) {
1040 case 103: /* alt gr */
1041 qemu_keycode = 56 | SCANCODE_GREY;
1042 break;
1043 }
1044 return qemu_keycode;
2777ccc5 1045 }
0a337ed0 1046#endif
a4ccabcf
AL
1047
1048 if (gdk_keycode < 9) {
1049 qemu_keycode = 0;
1050 } else if (gdk_keycode < 97) {
1051 qemu_keycode = gdk_keycode - 8;
0a337ed0
GH
1052#ifdef GDK_WINDOWING_X11
1053 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
3158a348
BR
1054 if (s->has_evdev) {
1055 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
1056 } else {
1057 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
1058 }
a8ffb372
DB
1059#endif
1060#ifdef GDK_WINDOWING_WAYLAND
1061 } else if (GDK_IS_WAYLAND_DISPLAY(dpy) && gdk_keycode < 158) {
1062 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
0a337ed0 1063#endif
a4ccabcf
AL
1064 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
1065 qemu_keycode = 0x70;
1066 } else if (gdk_keycode == 211) { /* backslash */
1067 qemu_keycode = 0x73;
1068 } else {
1069 qemu_keycode = 0;
1070 }
1071
932f2d7e
GH
1072 return qemu_keycode;
1073}
1074
f8c223f6
GH
1075static gboolean gd_text_key_down(GtkWidget *widget,
1076 GdkEventKey *key, void *opaque)
1077{
1078 VirtualConsole *vc = opaque;
1079 QemuConsole *con = vc->gfx.dcl.con;
1080
85617885
TH
1081 if (key->keyval == GDK_KEY_Delete) {
1082 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE);
1083 } else if (key->length) {
f8c223f6
GH
1084 kbd_put_string_console(con, key->string, key->length);
1085 } else {
1086 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
1087 key->hardware_keycode);
1088 int qcode = qemu_input_key_number_to_qcode(num);
1089 kbd_put_qcode_console(con, qcode);
1090 }
1091 return TRUE;
1092}
1093
932f2d7e
GH
1094static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
1095{
1096 VirtualConsole *vc = opaque;
1097 GtkDisplayState *s = vc->s;
1098 int gdk_keycode = key->hardware_keycode;
1099 int qemu_keycode;
1100 int i;
1101
1a01716a
JK
1102 if (s->ignore_keys) {
1103 s->ignore_keys = (key->type == GDK_KEY_PRESS);
1104 return TRUE;
1105 }
1106
5c960521
MD
1107 if (key->keyval == GDK_KEY_Pause) {
1108 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
1109 key->type == GDK_KEY_PRESS);
1110 return TRUE;
1111 }
1112
0a337ed0
GH
1113 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
1114 gdk_keycode);
932f2d7e 1115
74444bc1 1116 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
ef0dd982 1117 (key->type == GDK_KEY_PRESS) ? "down" : "up");
a4ccabcf 1118
6db253ca
JK
1119 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
1120 if (qemu_keycode == modifier_keycode[i]) {
1121 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
1122 }
1123 }
1124
e3500d1f 1125 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
af98ba92 1126 key->type == GDK_KEY_PRESS);
a4ccabcf
AL
1127
1128 return TRUE;
1129}
1130
0d0e044d
TI
1131static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
1132{
1133 if (event->type == GDK_MOTION_NOTIFY) {
1134 return gd_motion_event(widget, &event->motion, opaque);
1135 }
1136 return FALSE;
1137}
1138
a4ccabcf
AL
1139/** Window Menu Actions **/
1140
30e8f22b
JK
1141static void gd_menu_pause(GtkMenuItem *item, void *opaque)
1142{
1143 GtkDisplayState *s = opaque;
1144
1145 if (s->external_pause_update) {
1146 return;
1147 }
1148 if (runstate_is_running()) {
1149 qmp_stop(NULL);
1150 } else {
1151 qmp_cont(NULL);
1152 }
1153}
1154
1155static void gd_menu_reset(GtkMenuItem *item, void *opaque)
1156{
1157 qmp_system_reset(NULL);
1158}
1159
1160static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
1161{
1162 qmp_system_powerdown(NULL);
1163}
1164
a4ccabcf
AL
1165static void gd_menu_quit(GtkMenuItem *item, void *opaque)
1166{
1167 qmp_quit(NULL);
1168}
1169
1170static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
1171{
1172 GtkDisplayState *s = opaque;
e3500d1f 1173 VirtualConsole *vc = gd_vc_find_by_menu(s);
e72b59fa 1174 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
832189c9 1175 gint page;
a4ccabcf 1176
e3500d1f
GH
1177 gtk_release_modifiers(s);
1178 if (vc) {
e72b59fa
JS
1179 page = gtk_notebook_page_num(nb, vc->tab_item);
1180 gtk_notebook_set_current_page(nb, page);
9d677e1c 1181 gtk_widget_grab_focus(vc->focus);
a4ccabcf 1182 }
1a01716a 1183 s->ignore_keys = false;
a4ccabcf
AL
1184}
1185
277836c8
CR
1186static void gd_accel_switch_vc(void *opaque)
1187{
1188 VirtualConsole *vc = opaque;
1a01716a 1189
277836c8 1190 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1a01716a
JK
1191#if !GTK_CHECK_VERSION(3, 0, 0)
1192 /* GTK2 sends the accel key to the target console - ignore this until */
1193 vc->s->ignore_keys = true;
1194#endif
277836c8
CR
1195}
1196
a4ccabcf
AL
1197static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1198{
1199 GtkDisplayState *s = opaque;
fa7a1e52 1200 VirtualConsole *vc = gd_vc_find_current(s);
a4ccabcf
AL
1201
1202 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1203 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1204 } else {
1205 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1206 }
fa7a1e52 1207 gd_update_windowsize(vc);
a4ccabcf
AL
1208}
1209
cdeb7090
GH
1210static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1211 void *opaque)
1212{
1213 VirtualConsole *vc = opaque;
1214 GtkDisplayState *s = vc->s;
1215
1216 gtk_widget_set_sensitive(vc->menu_item, true);
316cb068 1217 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
cdeb7090
GH
1218 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1219 vc->tab_item, vc->label);
1220 gtk_widget_destroy(vc->window);
1221 vc->window = NULL;
1222 return TRUE;
1223}
1224
0c77a37f
GH
1225static gboolean gd_win_grab(void *opaque)
1226{
1227 VirtualConsole *vc = opaque;
1228
1229 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1230 if (vc->s->ptr_owner) {
1231 gd_ungrab_pointer(vc->s);
1232 } else {
d531deef 1233 gd_grab_pointer(vc, "user-request-detached-tab");
0c77a37f 1234 }
0c77a37f
GH
1235 return TRUE;
1236}
1237
cdeb7090
GH
1238static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1239{
1240 GtkDisplayState *s = opaque;
1241 VirtualConsole *vc = gd_vc_find_current(s);
cdeb7090 1242
f8c223f6
GH
1243 if (vc->type == GD_VC_GFX &&
1244 qemu_console_is_graphic(vc->gfx.dcl.con)) {
aa0a55d4
GH
1245 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1246 FALSE);
cdeb7090
GH
1247 }
1248 if (!vc->window) {
1249 gtk_widget_set_sensitive(vc->menu_item, false);
1250 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
316cb068 1251 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
cdeb7090 1252
cdeb7090
GH
1253 g_signal_connect(vc->window, "delete-event",
1254 G_CALLBACK(gd_tab_window_close), vc);
1255 gtk_widget_show_all(vc->window);
4eeaa3a8 1256
f8c223f6
GH
1257 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1258 GtkAccelGroup *ag = gtk_accel_group_new();
1259 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
0c77a37f 1260
f8c223f6
GH
1261 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1262 vc, NULL);
1263 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1264 }
0c77a37f 1265
82fc1809 1266 gd_update_geometry_hints(vc);
4eeaa3a8 1267 gd_update_caption(s);
cdeb7090
GH
1268 }
1269}
1270
c6158483
AL
1271static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1272{
1273 GtkDisplayState *s = opaque;
e3500d1f 1274 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1275
10409282 1276 if (!s->full_screen) {
c6158483 1277 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
b0f31820 1278 gtk_widget_hide(s->menu_bar);
e3500d1f
GH
1279 if (vc->type == GD_VC_GFX) {
1280 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
c6158483 1281 }
e3500d1f 1282 gtk_window_fullscreen(GTK_WINDOW(s->window));
c6158483
AL
1283 s->full_screen = TRUE;
1284 } else {
1285 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1286 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
b0f31820 1287 gtk_widget_show(s->menu_bar);
c6158483 1288 s->full_screen = FALSE;
e3500d1f
GH
1289 if (vc->type == GD_VC_GFX) {
1290 vc->gfx.scale_x = 1.0;
1291 vc->gfx.scale_y = 1.0;
82fc1809 1292 gd_update_windowsize(vc);
e3500d1f 1293 }
c6158483
AL
1294 }
1295
e3500d1f 1296 gd_update_cursor(vc);
c6158483
AL
1297}
1298
95414914
CR
1299static void gd_accel_full_screen(void *opaque)
1300{
1301 GtkDisplayState *s = opaque;
1302 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1303}
1304
c6158483
AL
1305static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1306{
1307 GtkDisplayState *s = opaque;
e3500d1f 1308 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1309
1310 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1311 FALSE);
1312
82fc1809
GH
1313 vc->gfx.scale_x += VC_SCALE_STEP;
1314 vc->gfx.scale_y += VC_SCALE_STEP;
c6158483 1315
e3500d1f 1316 gd_update_windowsize(vc);
c6158483
AL
1317}
1318
1319static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1320{
1321 GtkDisplayState *s = opaque;
e3500d1f 1322 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1323
1324 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1325 FALSE);
1326
82fc1809
GH
1327 vc->gfx.scale_x -= VC_SCALE_STEP;
1328 vc->gfx.scale_y -= VC_SCALE_STEP;
c6158483 1329
82fc1809
GH
1330 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1331 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
c6158483 1332
e3500d1f 1333 gd_update_windowsize(vc);
c6158483
AL
1334}
1335
1336static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1337{
1338 GtkDisplayState *s = opaque;
e3500d1f 1339 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1340
e3500d1f
GH
1341 vc->gfx.scale_x = 1.0;
1342 vc->gfx.scale_y = 1.0;
c6158483 1343
e3500d1f 1344 gd_update_windowsize(vc);
c6158483
AL
1345}
1346
1347static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1348{
1349 GtkDisplayState *s = opaque;
e3500d1f 1350 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1351
1352 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1353 s->free_scale = TRUE;
1354 } else {
1355 s->free_scale = FALSE;
e3500d1f
GH
1356 vc->gfx.scale_x = 1.0;
1357 vc->gfx.scale_y = 1.0;
c6158483
AL
1358 }
1359
82fc1809 1360 gd_update_windowsize(vc);
e3500d1f 1361 gd_update_full_redraw(vc);
c6158483
AL
1362}
1363
a69fc693
GH
1364#if GTK_CHECK_VERSION(3, 20, 0)
1365static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
1366{
1367 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1368 GdkSeat *seat = gdk_display_get_default_seat(display);
1369 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
1370 GdkSeatCapabilities caps = 0;
1371 GdkCursor *cursor = NULL;
1372
1373 if (kbd) {
1374 caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
1375 }
1376 if (ptr) {
1377 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
1378 cursor = vc->s->null_cursor;
1379 }
1380
1381 if (caps) {
1382 gdk_seat_grab(seat, window, caps, false, cursor,
1383 NULL, NULL, NULL);
1384 } else {
1385 gdk_seat_ungrab(seat);
1386 }
1387}
1388#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1389static void gd_grab_devices(VirtualConsole *vc, bool grab,
1390 GdkInputSource source, GdkEventMask mask,
1391 GdkCursor *cursor)
1392{
e3500d1f 1393 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
655199da 1394 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1395 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1396 GList *tmp = devs;
1397
1398 for (tmp = devs; tmp; tmp = tmp->next) {
655199da 1399 GdkDevice *dev = tmp->data;
f50def91
GH
1400 if (gdk_device_get_source(dev) != source) {
1401 continue;
1402 }
1403 if (grab) {
1404 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1405 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1406 mask, cursor, GDK_CURRENT_TIME);
1407 } else {
1408 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
655199da 1409 }
655199da 1410 }
f50def91
GH
1411 g_list_free(devs);
1412}
1413#endif
1414
d531deef 1415static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
f50def91 1416{
aa4f4058
GH
1417 if (vc->s->kbd_owner) {
1418 if (vc->s->kbd_owner == vc) {
1419 return;
1420 } else {
1421 gd_ungrab_keyboard(vc->s);
1422 }
1423 }
1424
a69fc693
GH
1425#if GTK_CHECK_VERSION(3, 20, 0)
1426 gd_grab_update(vc, true, vc->s->ptr_owner == vc);
1427#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1428 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1429 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1430 NULL);
655199da 1431#else
e3500d1f 1432 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
5104a1f6
AL
1433 FALSE,
1434 GDK_CURRENT_TIME);
655199da 1435#endif
4c638e2e 1436 vc->s->kbd_owner = vc;
695cc59d 1437 gd_update_caption(vc->s);
d531deef 1438 trace_gd_grab(vc->label, "kbd", reason);
5104a1f6
AL
1439}
1440
4c638e2e 1441static void gd_ungrab_keyboard(GtkDisplayState *s)
5104a1f6 1442{
4c638e2e
GH
1443 VirtualConsole *vc = s->kbd_owner;
1444
1445 if (vc == NULL) {
1446 return;
1447 }
1448 s->kbd_owner = NULL;
1449
a69fc693
GH
1450#if GTK_CHECK_VERSION(3, 20, 0)
1451 gd_grab_update(vc, false, vc->s->ptr_owner == vc);
1452#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91 1453 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
655199da 1454#else
5104a1f6 1455 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
655199da 1456#endif
695cc59d 1457 gd_update_caption(s);
d531deef 1458 trace_gd_ungrab(vc->label, "kbd");
5104a1f6
AL
1459}
1460
d531deef 1461static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
2a05485d 1462{
e3500d1f 1463 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
aa4f4058
GH
1464
1465 if (vc->s->ptr_owner) {
1466 if (vc->s->ptr_owner == vc) {
1467 return;
1468 } else {
1469 gd_ungrab_pointer(vc->s);
1470 }
1471 }
1472
a69fc693
GH
1473#if GTK_CHECK_VERSION(3, 20, 0)
1474 gd_grab_update(vc, vc->s->kbd_owner == vc, true);
1475 gdk_device_get_position(gd_get_pointer(display),
1476 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1477#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1478 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1479 GDK_POINTER_MOTION_MASK |
1480 GDK_BUTTON_PRESS_MASK |
1481 GDK_BUTTON_RELEASE_MASK |
1482 GDK_BUTTON_MOTION_MASK |
1483 GDK_SCROLL_MASK,
1484 vc->s->null_cursor);
bb732ee7 1485 gdk_device_get_position(gd_get_pointer(display),
e3500d1f 1486 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
2a05485d 1487#else
e3500d1f 1488 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
2a05485d
DB
1489 FALSE, /* All events to come to our window directly */
1490 GDK_POINTER_MOTION_MASK |
1491 GDK_BUTTON_PRESS_MASK |
1492 GDK_BUTTON_RELEASE_MASK |
1493 GDK_BUTTON_MOTION_MASK |
1494 GDK_SCROLL_MASK,
1495 NULL, /* Allow cursor to move over entire desktop */
e3500d1f 1496 vc->s->null_cursor,
2a05485d 1497 GDK_CURRENT_TIME);
ecce1929 1498 gdk_display_get_pointer(display, NULL,
e3500d1f 1499 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
2a05485d 1500#endif
4c638e2e 1501 vc->s->ptr_owner = vc;
695cc59d 1502 gd_update_caption(vc->s);
d531deef 1503 trace_gd_grab(vc->label, "ptr", reason);
2a05485d
DB
1504}
1505
4c638e2e 1506static void gd_ungrab_pointer(GtkDisplayState *s)
2a05485d 1507{
4c638e2e 1508 VirtualConsole *vc = s->ptr_owner;
41cc5239 1509 GdkDisplay *display;
4c638e2e
GH
1510
1511 if (vc == NULL) {
1512 return;
1513 }
1514 s->ptr_owner = NULL;
1515
41cc5239 1516 display = gtk_widget_get_display(vc->gfx.drawing_area);
a69fc693
GH
1517#if GTK_CHECK_VERSION(3, 20, 0)
1518 gd_grab_update(vc, vc->s->kbd_owner == vc, false);
1519 gdk_device_warp(gd_get_pointer(display),
1520 gtk_widget_get_screen(vc->gfx.drawing_area),
1521 vc->s->grab_x_root, vc->s->grab_y_root);
1522#elif GTK_CHECK_VERSION(3, 0, 0)
f50def91 1523 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
bb732ee7 1524 gdk_device_warp(gd_get_pointer(display),
e3500d1f
GH
1525 gtk_widget_get_screen(vc->gfx.drawing_area),
1526 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d
DB
1527#else
1528 gdk_pointer_ungrab(GDK_CURRENT_TIME);
ecce1929 1529 gdk_display_warp_pointer(display,
e3500d1f
GH
1530 gtk_widget_get_screen(vc->gfx.drawing_area),
1531 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d 1532#endif
695cc59d 1533 gd_update_caption(s);
d531deef 1534 trace_gd_ungrab(vc->label, "ptr");
2a05485d
DB
1535}
1536
5104a1f6
AL
1537static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1538{
1539 GtkDisplayState *s = opaque;
e3500d1f 1540 VirtualConsole *vc = gd_vc_find_current(s);
5104a1f6
AL
1541
1542 if (gd_is_grab_active(s)) {
d531deef
GH
1543 gd_grab_keyboard(vc, "user-request-main-window");
1544 gd_grab_pointer(vc, "user-request-main-window");
5104a1f6 1545 } else {
4c638e2e
GH
1546 gd_ungrab_keyboard(s);
1547 gd_ungrab_pointer(s);
5104a1f6
AL
1548 }
1549
e3500d1f 1550 gd_update_cursor(vc);
5104a1f6
AL
1551}
1552
a4ccabcf
AL
1553static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1554 gpointer data)
1555{
1556 GtkDisplayState *s = data;
e3500d1f 1557 VirtualConsole *vc;
5104a1f6 1558 gboolean on_vga;
a4ccabcf
AL
1559
1560 if (!gtk_widget_get_realized(s->notebook)) {
1561 return;
1562 }
1563
6fa27697
GH
1564#ifdef VTE_RESIZE_HACK
1565 vc = gd_vc_find_current(s);
1566 if (vc && vc->type == GD_VC_VTE) {
1567 gtk_widget_hide(vc->vte.terminal);
1568 }
1569#endif
e3500d1f
GH
1570 vc = gd_vc_find_by_page(s, arg2);
1571 if (!vc) {
1572 return;
1573 }
6fa27697
GH
1574#ifdef VTE_RESIZE_HACK
1575 if (vc->type == GD_VC_VTE) {
1576 gtk_widget_show(vc->vte.terminal);
1577 }
1578#endif
e3500d1f
GH
1579 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1580 TRUE);
f8c223f6
GH
1581 on_vga = (vc->type == GD_VC_GFX &&
1582 qemu_console_is_graphic(vc->gfx.dcl.con));
5104a1f6
AL
1583 if (!on_vga) {
1584 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1585 FALSE);
c6158483
AL
1586 } else if (s->full_screen) {
1587 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1588 TRUE);
5104a1f6 1589 }
5104a1f6 1590 gtk_widget_set_sensitive(s->grab_item, on_vga);
a0815632
SH
1591#ifdef CONFIG_VTE
1592 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE);
1593#endif
5104a1f6 1594
82fc1809 1595 gd_update_windowsize(vc);
e3500d1f 1596 gd_update_cursor(vc);
a4ccabcf
AL
1597}
1598
e3500d1f
GH
1599static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1600 gpointer opaque)
5104a1f6 1601{
e3500d1f
GH
1602 VirtualConsole *vc = opaque;
1603 GtkDisplayState *s = vc->s;
5104a1f6 1604
2884cf5b 1605 if (gd_grab_on_hover(s)) {
d531deef 1606 gd_grab_keyboard(vc, "grab-on-hover");
5104a1f6 1607 }
5104a1f6
AL
1608 return TRUE;
1609}
1610
e3500d1f
GH
1611static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1612 gpointer opaque)
5104a1f6 1613{
e3500d1f
GH
1614 VirtualConsole *vc = opaque;
1615 GtkDisplayState *s = vc->s;
5104a1f6 1616
2884cf5b 1617 if (gd_grab_on_hover(s)) {
4c638e2e 1618 gd_ungrab_keyboard(s);
5104a1f6 1619 }
5104a1f6
AL
1620 return TRUE;
1621}
1622
6db253ca 1623static gboolean gd_focus_out_event(GtkWidget *widget,
e3500d1f 1624 GdkEventCrossing *crossing, gpointer opaque)
6db253ca 1625{
e3500d1f
GH
1626 VirtualConsole *vc = opaque;
1627 GtkDisplayState *s = vc->s;
6db253ca
JK
1628
1629 gtk_release_modifiers(s);
6db253ca
JK
1630 return TRUE;
1631}
1632
1301e515
GH
1633static gboolean gd_configure(GtkWidget *widget,
1634 GdkEventConfigure *cfg, gpointer opaque)
1635{
1636 VirtualConsole *vc = opaque;
1301e515 1637
925a0400 1638 gd_set_ui_info(vc, cfg->width, cfg->height);
1301e515
GH
1639 return FALSE;
1640}
1641
d861def3
AL
1642/** Virtual Console Callbacks **/
1643
ed1132e4 1644static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
cdeb7090 1645 int idx, GSList *group, GtkWidget *view_menu)
ed1132e4 1646{
cdeb7090 1647 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
277836c8
CR
1648 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1649 HOTKEY_MODIFIERS, 0,
1650 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1651#if GTK_CHECK_VERSION(3, 8, 0)
1652 gtk_accel_label_set_accel(
1653 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1654 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1655#endif
ed1132e4
GH
1656
1657 g_signal_connect(vc->menu_item, "activate",
1658 G_CALLBACK(gd_menu_switch_vc), s);
1659 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1660
277836c8 1661 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
ed1132e4
GH
1662 return group;
1663}
1664
ee5f31e4 1665#if defined(CONFIG_VTE)
44b31e0b
MT
1666static void gd_menu_copy(GtkMenuItem *item, void *opaque)
1667{
1668 GtkDisplayState *s = opaque;
1669 VirtualConsole *vc = gd_vc_find_current(s);
1670
1671 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
1672}
1673
0fb20d1c
CR
1674static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1675{
1676 VirtualConsole *vc = opaque;
1677
1678 if (gtk_adjustment_get_upper(adjustment) >
1679 gtk_adjustment_get_page_size(adjustment)) {
271a25c0 1680 gtk_widget_show(vc->vte.scrollbar);
0fb20d1c 1681 } else {
271a25c0 1682 gtk_widget_hide(vc->vte.scrollbar);
0fb20d1c
CR
1683 }
1684}
1685
d861def3
AL
1686static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1687{
1688 VirtualConsole *vc = chr->opaque;
1689
271a25c0 1690 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
d4370741 1691 return len;
d861def3
AL
1692}
1693
fba958c6
PB
1694static void gd_vc_chr_set_echo(CharDriverState *chr, bool echo)
1695{
1696 VirtualConsole *vc = chr->opaque;
1697
1698 vc->vte.echo = echo;
1699}
1700
d861def3
AL
1701static int nb_vcs;
1702static CharDriverState *vcs[MAX_VCS];
1703
919e11f3 1704static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
d861def3 1705{
b68e956a
MAL
1706 static const CharDriver gd_vc_driver = {
1707 .kind = CHARDEV_BACKEND_KIND_VC,
1708 .chr_write = gd_vc_chr_write,
1709 .chr_set_echo = gd_vc_chr_set_echo,
1710 };
1711
919e11f3 1712 ChardevCommon *common = qapi_ChardevVC_base(vc);
d861def3
AL
1713 CharDriverState *chr;
1714
c952b715
MAL
1715 if (nb_vcs == MAX_VCS) {
1716 error_setg(errp, "Maximum number of consoles reached");
1717 return NULL;
1718 }
1719
b68e956a 1720 chr = qemu_chr_alloc(&gd_vc_driver, common, errp);
919e11f3
DB
1721 if (!chr) {
1722 return NULL;
1723 }
1724
fba958c6 1725 /* Temporary, until gd_vc_vte_init runs. */
327d83ba 1726 chr->opaque = g_new0(VirtualConsole, 1);
fba958c6 1727
d861def3
AL
1728 vcs[nb_vcs++] = chr;
1729
1730 return chr;
1731}
1732
d4370741
CR
1733static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1734 gpointer user_data)
d861def3 1735{
d4370741 1736 VirtualConsole *vc = user_data;
d861def3 1737
fba958c6
PB
1738 if (vc->vte.echo) {
1739 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
1740 int i;
1741 for (i = 0; i < size; i++) {
1742 uint8_t c = text[i];
1743 if (c >= 128 || isprint(c)) {
1744 /* 8-bit characters are considered printable. */
1745 vte_terminal_feed(term, &text[i], 1);
1746 } else if (c == '\r' || c == '\n') {
1747 vte_terminal_feed(term, "\r\n", 2);
1748 } else {
1749 char ctrl[2] = { '^', 0};
1750 ctrl[1] = text[i] ^ 64;
1751 vte_terminal_feed(term, ctrl, 2);
1752 }
1753 }
1754 }
1755
271a25c0 1756 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
d861def3
AL
1757 return TRUE;
1758}
1759
ed1132e4
GH
1760static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1761 CharDriverState *chr, int idx,
e3500d1f 1762 GSList *group, GtkWidget *view_menu)
d861def3 1763{
d861def3 1764 char buffer[32];
0fb20d1c
CR
1765 GtkWidget *box;
1766 GtkWidget *scrollbar;
1767 GtkAdjustment *vadjustment;
fba958c6 1768 VirtualConsole *tmp_vc = chr->opaque;
d861def3 1769
e3500d1f 1770 vc->s = s;
fba958c6
PB
1771 vc->vte.echo = tmp_vc->vte.echo;
1772
ed1132e4 1773 vc->vte.chr = chr;
fba958c6
PB
1774 chr->opaque = vc;
1775 g_free(tmp_vc);
d861def3 1776
ed1132e4 1777 snprintf(buffer, sizeof(buffer), "vc%d", idx);
cdeb7090
GH
1778 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1779 ? vc->vte.chr->label : buffer);
1780 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
d861def3 1781
271a25c0
GH
1782 vc->vte.terminal = vte_terminal_new();
1783 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
d861def3 1784
fba958c6
PB
1785 /* The documentation says that the default is UTF-8, but actually it is
1786 * 7-bit ASCII at least in VTE 0.38.
1787 */
4d594233 1788#if VTE_CHECK_VERSION(0, 38, 0)
fba958c6
PB
1789 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
1790#else
1791 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
1792#endif
1793
271a25c0 1794 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
82fc1809
GH
1795 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1796 VC_TERM_X_MIN, VC_TERM_Y_MIN);
d861def3 1797
0fb20d1c 1798#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
271a25c0
GH
1799 vadjustment = gtk_scrollable_get_vadjustment
1800 (GTK_SCROLLABLE(vc->vte.terminal));
0fb20d1c 1801#else
271a25c0 1802 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
0fb20d1c
CR
1803#endif
1804
1805#if GTK_CHECK_VERSION(3, 0, 0)
1806 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1807 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1808#else
1809 box = gtk_hbox_new(false, 2);
1810 scrollbar = gtk_vscrollbar_new(vadjustment);
1811#endif
1812
271a25c0 1813 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
0fb20d1c
CR
1814 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1815
271a25c0
GH
1816 vc->vte.box = box;
1817 vc->vte.scrollbar = scrollbar;
0fb20d1c
CR
1818
1819 g_signal_connect(vadjustment, "changed",
1820 G_CALLBACK(gd_vc_adjustment_changed), vc);
d861def3 1821
e3500d1f 1822 vc->type = GD_VC_VTE;
271a25c0 1823 vc->tab_item = box;
9d677e1c 1824 vc->focus = vc->vte.terminal;
271a25c0 1825 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
cdeb7090 1826 gtk_label_new(vc->label));
d861def3 1827
271a25c0 1828 qemu_chr_be_generic_open(vc->vte.chr);
d861def3 1829
d861def3 1830 return group;
a4ccabcf
AL
1831}
1832
ed1132e4 1833static void gd_vcs_init(GtkDisplayState *s, GSList *group,
ee5f31e4
GH
1834 GtkWidget *view_menu)
1835{
1836 int i;
1837
1838 for (i = 0; i < nb_vcs; i++) {
ed1132e4
GH
1839 VirtualConsole *vc = &s->vc[s->nb_vcs];
1840 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
ee5f31e4
GH
1841 s->nb_vcs++;
1842 }
1843}
1844#endif /* CONFIG_VTE */
1845
a4ccabcf
AL
1846/** Window Creation **/
1847
e3500d1f
GH
1848static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1849{
1850#if GTK_CHECK_VERSION(3, 0, 0)
1851 g_signal_connect(vc->gfx.drawing_area, "draw",
1852 G_CALLBACK(gd_draw_event), vc);
925a0400
GH
1853#if defined(CONFIG_GTK_GL)
1854 if (display_opengl) {
1855 /* wire up GtkGlArea events */
1856 g_signal_connect(vc->gfx.drawing_area, "render",
1857 G_CALLBACK(gd_render_event), vc);
1858 g_signal_connect(vc->gfx.drawing_area, "resize",
1859 G_CALLBACK(gd_resize_event), vc);
1860 }
1861#endif
e3500d1f
GH
1862#else
1863 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1864 G_CALLBACK(gd_expose_event), vc);
1865#endif
f8c223f6
GH
1866 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1867 g_signal_connect(vc->gfx.drawing_area, "event",
1868 G_CALLBACK(gd_event), vc);
1869 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1870 G_CALLBACK(gd_button_event), vc);
1871 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1872 G_CALLBACK(gd_button_event), vc);
1873 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1874 G_CALLBACK(gd_scroll_event), vc);
1875 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1876 G_CALLBACK(gd_key_event), vc);
1877 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1878 G_CALLBACK(gd_key_event), vc);
1879
1880 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1881 G_CALLBACK(gd_enter_event), vc);
1882 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1883 G_CALLBACK(gd_leave_event), vc);
1884 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1885 G_CALLBACK(gd_focus_out_event), vc);
1301e515
GH
1886 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1887 G_CALLBACK(gd_configure), vc);
f8c223f6
GH
1888 } else {
1889 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1890 G_CALLBACK(gd_text_key_down), vc);
1891 }
e3500d1f
GH
1892}
1893
a4ccabcf
AL
1894static void gd_connect_signals(GtkDisplayState *s)
1895{
1896 g_signal_connect(s->show_tabs_item, "activate",
1897 G_CALLBACK(gd_menu_show_tabs), s);
cdeb7090
GH
1898 g_signal_connect(s->untabify_item, "activate",
1899 G_CALLBACK(gd_menu_untabify), s);
a4ccabcf
AL
1900
1901 g_signal_connect(s->window, "delete-event",
1902 G_CALLBACK(gd_window_close), s);
1903
30e8f22b
JK
1904 g_signal_connect(s->pause_item, "activate",
1905 G_CALLBACK(gd_menu_pause), s);
1906 g_signal_connect(s->reset_item, "activate",
1907 G_CALLBACK(gd_menu_reset), s);
1908 g_signal_connect(s->powerdown_item, "activate",
1909 G_CALLBACK(gd_menu_powerdown), s);
a4ccabcf
AL
1910 g_signal_connect(s->quit_item, "activate",
1911 G_CALLBACK(gd_menu_quit), s);
44b31e0b
MT
1912#if defined(CONFIG_VTE)
1913 g_signal_connect(s->copy_item, "activate",
1914 G_CALLBACK(gd_menu_copy), s);
1915#endif
c6158483
AL
1916 g_signal_connect(s->full_screen_item, "activate",
1917 G_CALLBACK(gd_menu_full_screen), s);
1918 g_signal_connect(s->zoom_in_item, "activate",
1919 G_CALLBACK(gd_menu_zoom_in), s);
1920 g_signal_connect(s->zoom_out_item, "activate",
1921 G_CALLBACK(gd_menu_zoom_out), s);
1922 g_signal_connect(s->zoom_fixed_item, "activate",
1923 G_CALLBACK(gd_menu_zoom_fixed), s);
1924 g_signal_connect(s->zoom_fit_item, "activate",
1925 G_CALLBACK(gd_menu_zoom_fit), s);
5104a1f6
AL
1926 g_signal_connect(s->grab_item, "activate",
1927 G_CALLBACK(gd_menu_grab_input), s);
a4ccabcf
AL
1928 g_signal_connect(s->notebook, "switch-page",
1929 G_CALLBACK(gd_change_page), s);
1930}
1931
400519d2 1932static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
a4ccabcf 1933{
bf9b255f 1934 GtkWidget *machine_menu;
a4ccabcf
AL
1935 GtkWidget *separator;
1936
bf9b255f 1937 machine_menu = gtk_menu_new();
400519d2 1938 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
30e8f22b
JK
1939
1940 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
bf9b255f 1941 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
30e8f22b
JK
1942
1943 separator = gtk_separator_menu_item_new();
bf9b255f 1944 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
30e8f22b 1945
9068f20d 1946 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
bf9b255f 1947 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
30e8f22b 1948
9068f20d 1949 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
bf9b255f 1950 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
30e8f22b
JK
1951
1952 separator = gtk_separator_menu_item_new();
bf9b255f 1953 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
a4ccabcf 1954
3d914488 1955 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
a4ccabcf 1956 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
30e8f22b 1957 "<QEMU>/Machine/Quit");
3d914488 1958 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
db1da1f2 1959 GDK_KEY_q, HOTKEY_MODIFIERS);
bf9b255f 1960 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
a4ccabcf 1961
bf9b255f
AL
1962 return machine_menu;
1963}
1964
e3500d1f 1965static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
ed1132e4 1966 QemuConsole *con, int idx,
e3500d1f
GH
1967 GSList *group, GtkWidget *view_menu)
1968{
779ce88f 1969 vc->label = qemu_console_get_label(con);
e3500d1f
GH
1970 vc->s = s;
1971 vc->gfx.scale_x = 1.0;
1972 vc->gfx.scale_y = 1.0;
1973
97edf3bd
GH
1974#if defined(CONFIG_OPENGL)
1975 if (display_opengl) {
925a0400
GH
1976#if defined(CONFIG_GTK_GL)
1977 vc->gfx.drawing_area = gtk_gl_area_new();
1978 vc->gfx.dcl.ops = &dcl_gl_area_ops;
1979#else
1980 vc->gfx.drawing_area = gtk_drawing_area_new();
97edf3bd
GH
1981 /*
1982 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1983 * It is required for opengl rendering on X11 though. A
1984 * proper replacement (native opengl support) is only
1985 * available in 3.16+. Silence the warning if possible.
1986 */
1987#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1988#pragma GCC diagnostic push
1989#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1990#endif
1991 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1992#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1993#pragma GCC diagnostic pop
1994#endif
1995 vc->gfx.dcl.ops = &dcl_egl_ops;
925a0400 1996#endif /* CONFIG_GTK_GL */
97edf3bd
GH
1997 } else
1998#endif
1999 {
925a0400 2000 vc->gfx.drawing_area = gtk_drawing_area_new();
97edf3bd
GH
2001 vc->gfx.dcl.ops = &dcl_ops;
2002 }
2003
925a0400
GH
2004
2005 gtk_widget_add_events(vc->gfx.drawing_area,
2006 GDK_POINTER_MOTION_MASK |
2007 GDK_BUTTON_PRESS_MASK |
2008 GDK_BUTTON_RELEASE_MASK |
2009 GDK_BUTTON_MOTION_MASK |
2010 GDK_ENTER_NOTIFY_MASK |
2011 GDK_LEAVE_NOTIFY_MASK |
2012 GDK_SCROLL_MASK |
2013 GDK_KEY_PRESS_MASK);
2014 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
2015
2016 vc->type = GD_VC_GFX;
2017 vc->tab_item = vc->gfx.drawing_area;
2018 vc->focus = vc->gfx.drawing_area;
2019 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
2020 vc->tab_item, gtk_label_new(vc->label));
2021
e3500d1f
GH
2022 vc->gfx.dcl.con = con;
2023 register_displaychangelistener(&vc->gfx.dcl);
2024
f8c223f6
GH
2025 gd_connect_vc_gfx_signals(vc);
2026 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
2027
1301e515
GH
2028 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
2029 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
1d73cd78 2030 s->free_scale = true;
1301e515
GH
2031 }
2032
e3500d1f
GH
2033 return group;
2034}
2035
400519d2 2036static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
bf9b255f
AL
2037{
2038 GSList *group = NULL;
2039 GtkWidget *view_menu;
2040 GtkWidget *separator;
ed1132e4
GH
2041 QemuConsole *con;
2042 int vc;
bf9b255f
AL
2043
2044 view_menu = gtk_menu_new();
400519d2 2045 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
a4ccabcf 2046
3d914488 2047 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
95414914 2048
44b31e0b
MT
2049#if defined(CONFIG_VTE)
2050 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
2051 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
2052#endif
2053
95414914
CR
2054 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
2055 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
2056#if GTK_CHECK_VERSION(3, 8, 0)
2057 gtk_accel_label_set_accel(
2058 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
2059 GDK_KEY_f, HOTKEY_MODIFIERS);
2060#endif
bf9b255f 2061 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
c6158483
AL
2062
2063 separator = gtk_separator_menu_item_new();
bf9b255f 2064 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 2065
3d914488 2066 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
c6158483
AL
2067 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
2068 "<QEMU>/View/Zoom In");
b1e749c0
JK
2069 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
2070 HOTKEY_MODIFIERS);
bf9b255f 2071 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
c6158483 2072
3d914488 2073 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
c6158483
AL
2074 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
2075 "<QEMU>/View/Zoom Out");
b1e749c0
JK
2076 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
2077 HOTKEY_MODIFIERS);
bf9b255f 2078 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
c6158483 2079
3d914488 2080 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
c6158483
AL
2081 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
2082 "<QEMU>/View/Zoom Fixed");
b1e749c0
JK
2083 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
2084 HOTKEY_MODIFIERS);
bf9b255f 2085 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
c6158483 2086
834574ea 2087 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
bf9b255f 2088 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
c6158483
AL
2089
2090 separator = gtk_separator_menu_item_new();
bf9b255f 2091 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 2092
834574ea 2093 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
bf9b255f 2094 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
5104a1f6 2095
834574ea 2096 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
5104a1f6
AL
2097 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
2098 "<QEMU>/View/Grab Input");
b1e749c0
JK
2099 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
2100 HOTKEY_MODIFIERS);
bf9b255f 2101 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
5104a1f6 2102
a4ccabcf 2103 separator = gtk_separator_menu_item_new();
bf9b255f 2104 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 2105
e3500d1f 2106 /* gfx */
ed1132e4
GH
2107 for (vc = 0;; vc++) {
2108 con = qemu_console_lookup_by_index(vc);
f8c223f6 2109 if (!con) {
ed1132e4
GH
2110 break;
2111 }
2112 group = gd_vc_gfx_init(s, &s->vc[vc], con,
2113 vc, group, view_menu);
2114 s->nb_vcs++;
2115 }
a4ccabcf 2116
ee5f31e4 2117#if defined(CONFIG_VTE)
e3500d1f 2118 /* vte */
ed1132e4 2119 gd_vcs_init(s, group, view_menu);
ee5f31e4 2120#endif
d861def3 2121
a4ccabcf 2122 separator = gtk_separator_menu_item_new();
bf9b255f 2123 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 2124
834574ea 2125 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
bf9b255f 2126 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
a4ccabcf 2127
cdeb7090
GH
2128 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2129 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
2130
bf9b255f
AL
2131 return view_menu;
2132}
2133
2134static void gd_create_menus(GtkDisplayState *s)
2135{
400519d2
CR
2136 s->accel_group = gtk_accel_group_new();
2137 s->machine_menu = gd_create_menu_machine(s);
2138 s->view_menu = gd_create_menu_view(s);
bf9b255f
AL
2139
2140 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
30e8f22b
JK
2141 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
2142 s->machine_menu);
2143 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
a4ccabcf 2144
bf9b255f 2145 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
a4ccabcf
AL
2146 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
2147 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
bf9b255f 2148
400519d2
CR
2149 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
2150 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
a4ccabcf
AL
2151}
2152
3158a348
BR
2153static void gd_set_keycode_type(GtkDisplayState *s)
2154{
0a337ed0 2155#ifdef GDK_WINDOWING_X11
3158a348 2156 GdkDisplay *display = gtk_widget_get_display(s->window);
0a337ed0
GH
2157 if (GDK_IS_X11_DISPLAY(display)) {
2158 Display *x11_display = gdk_x11_display_get_xdisplay(display);
2159 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
2160 XkbUseCoreKbd);
2161 char *keycodes = NULL;
2162
2163 if (desc && desc->names) {
2164 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
2165 }
2166 if (keycodes == NULL) {
2167 fprintf(stderr, "could not lookup keycode name\n");
2168 } else if (strstart(keycodes, "evdev", NULL)) {
2169 s->has_evdev = true;
2170 } else if (!strstart(keycodes, "xfree86", NULL)) {
2171 fprintf(stderr, "unknown keycodes `%s', please report to "
2172 "qemu-devel@nongnu.org\n", keycodes);
2173 }
84961407
CF
2174
2175 if (desc) {
2176 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
2177 }
2178 if (keycodes) {
2179 XFree(keycodes);
2180 }
3158a348
BR
2181 }
2182#endif
2183}
2184
060ab763
GH
2185static gboolean gtkinit;
2186
881249c7 2187void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
a4ccabcf 2188{
3d4da9d6
HP
2189 VirtualConsole *vc;
2190
a4ccabcf 2191 GtkDisplayState *s = g_malloc0(sizeof(*s));
d819cdcc 2192 char *filename;
63c67b6d 2193 GdkDisplay *window_display;
a4ccabcf 2194
060ab763
GH
2195 if (!gtkinit) {
2196 fprintf(stderr, "gtk initialization failed\n");
2197 exit(1);
2198 }
2199
a4ccabcf 2200 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
51572ab0
DB
2201#if GTK_CHECK_VERSION(3, 2, 0)
2202 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2203#else
a4ccabcf 2204 s->vbox = gtk_vbox_new(FALSE, 0);
51572ab0 2205#endif
a4ccabcf 2206 s->notebook = gtk_notebook_new();
a4ccabcf
AL
2207 s->menu_bar = gtk_menu_bar_new();
2208
c6158483 2209 s->free_scale = FALSE;
a4ccabcf 2210
2cb5d2a4
AG
2211 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2212 setlocale(LC_MESSAGES, "");
834574ea
AL
2213 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
2214 textdomain("qemu");
2215
63c67b6d
HR
2216 window_display = gtk_widget_get_display(s->window);
2217 s->null_cursor = gdk_cursor_new_for_display(window_display,
2218 GDK_BLANK_CURSOR);
a4ccabcf
AL
2219
2220 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
2221 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
2222 qemu_add_vm_change_state_handler(gd_change_runstate, s);
2223
f7da9c17 2224 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
d819cdcc
SW
2225 if (filename) {
2226 GError *error = NULL;
2227 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
2228 if (pixbuf) {
2229 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
2230 } else {
2231 g_error_free(error);
2232 }
2233 g_free(filename);
2234 }
2235
a4ccabcf
AL
2236 gd_create_menus(s);
2237
2238 gd_connect_signals(s);
2239
a4ccabcf
AL
2240 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
2241 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
2242
a4ccabcf
AL
2243 gd_update_caption(s);
2244
2245 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
2246 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
2247
2248 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
2249
2250 gtk_widget_show_all(s->window);
2251
6fa27697
GH
2252#ifdef VTE_RESIZE_HACK
2253 {
2254 VirtualConsole *cur = gd_vc_find_current(s);
b310a2a6
FZ
2255 if (cur) {
2256 int i;
2257
2258 for (i = 0; i < s->nb_vcs; i++) {
2259 VirtualConsole *vc = &s->vc[i];
2260 if (vc && vc->type == GD_VC_VTE && vc != cur) {
2261 gtk_widget_hide(vc->vte.terminal);
2262 }
6fa27697 2263 }
b310a2a6 2264 gd_update_windowsize(cur);
6fa27697 2265 }
6fa27697
GH
2266 }
2267#endif
2268
3d4da9d6
HP
2269 vc = gd_vc_find_current(s);
2270 gtk_widget_set_sensitive(s->view_menu, vc != NULL);
a0815632
SH
2271#ifdef CONFIG_VTE
2272 gtk_widget_set_sensitive(s->copy_item,
3d4da9d6 2273 vc && vc->type == GD_VC_VTE);
a0815632
SH
2274#endif
2275
787ba4f0
PW
2276 if (full_screen) {
2277 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
2278 }
881249c7
JK
2279 if (grab_on_hover) {
2280 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
2281 }
787ba4f0 2282
3158a348 2283 gd_set_keycode_type(s);
a4ccabcf 2284}
ee5f31e4 2285
97edf3bd 2286void early_gtk_display_init(int opengl)
ee5f31e4 2287{
2cb5d2a4
AG
2288 /* The QEMU code relies on the assumption that it's always run in
2289 * the C locale. Therefore it is not prepared to deal with
2290 * operations that produce different results depending on the
2291 * locale, such as printf's formatting of decimal numbers, and
2292 * possibly others.
2293 *
2294 * Since GTK+ calls setlocale() by default -importing the locale
2295 * settings from the environment- we must prevent it from doing so
2296 * using gtk_disable_setlocale().
2297 *
2298 * QEMU's GTK+ UI, however, _does_ have translations for some of
2299 * the menu items. As a trade-off between a functionally correct
2300 * QEMU and a fully internationalized UI we support importing
2301 * LC_MESSAGES from the environment (see the setlocale() call
2302 * earlier in this file). This allows us to display translated
2303 * messages leaving everything else untouched.
2304 */
2305 gtk_disable_setlocale();
060ab763
GH
2306 gtkinit = gtk_init_check(NULL, NULL);
2307 if (!gtkinit) {
2308 /* don't exit yet, that'll break -help */
2309 return;
2310 }
97edf3bd
GH
2311
2312 switch (opengl) {
2313 case -1: /* default */
2314 case 0: /* off */
2315 break;
2316 case 1: /* on */
2317#if defined(CONFIG_OPENGL)
925a0400
GH
2318#if defined(CONFIG_GTK_GL)
2319 gtk_gl_area_init();
2320#else
97edf3bd 2321 gtk_egl_init();
925a0400 2322#endif
97edf3bd
GH
2323#endif
2324 break;
2325 default:
2326 g_assert_not_reached();
2327 break;
2328 }
2329
ee5f31e4
GH
2330#if defined(CONFIG_VTE)
2331 register_vc_handler(gd_vc_handler);
2332#endif
2333}