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