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