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