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