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