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