]> git.proxmox.com Git - mirror_qemu.git/blame - ui/gtk.c
gtk: create gtk.h
[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
342static void gd_update_windowsize(VirtualConsole *vc)
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);
b087143b
IM
466}
467#else
9697f5d2
GH
468static void gd_mouse_set(DisplayChangeListener *dcl,
469 int x, int y, int visible)
470{
e3500d1f 471 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9697f5d2
GH
472 gint x_root, y_root;
473
2bda6602
CR
474 if (qemu_input_is_absolute()) {
475 return;
476 }
477
e3500d1f 478 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
9697f5d2 479 x, y, &x_root, &y_root);
e3500d1f
GH
480 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
481 gtk_widget_get_screen(vc->gfx.drawing_area),
9697f5d2
GH
482 x_root, y_root);
483}
b087143b 484#endif
9697f5d2
GH
485
486static void gd_cursor_define(DisplayChangeListener *dcl,
487 QEMUCursor *c)
488{
e3500d1f 489 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9697f5d2
GH
490 GdkPixbuf *pixbuf;
491 GdkCursor *cursor;
492
4cdfc935
HP
493 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
494 return;
495 }
496
9697f5d2
GH
497 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
498 GDK_COLORSPACE_RGB, true, 8,
499 c->width, c->height, c->width * 4,
500 NULL, NULL);
e3500d1f
GH
501 cursor = gdk_cursor_new_from_pixbuf
502 (gtk_widget_get_display(vc->gfx.drawing_area),
503 pixbuf, c->hot_x, c->hot_y);
504 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
9697f5d2 505 g_object_unref(pixbuf);
030b4b7d 506#if !GTK_CHECK_VERSION(3, 0, 0)
17139240 507 gdk_cursor_unref(cursor);
030b4b7d
SW
508#else
509 g_object_unref(cursor);
510#endif
9697f5d2
GH
511}
512
c12aeb86 513static void gd_switch(DisplayChangeListener *dcl,
c12aeb86 514 DisplaySurface *surface)
a4ccabcf 515{
e3500d1f 516 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
9d9801cf 517 bool resized = true;
a4ccabcf 518
f98f43ea
GH
519 trace_gd_switch(vc->label,
520 surface ? surface_width(surface) : 0,
521 surface ? surface_height(surface) : 0);
a4ccabcf 522
e3500d1f
GH
523 if (vc->gfx.surface) {
524 cairo_surface_destroy(vc->gfx.surface);
f98f43ea
GH
525 vc->gfx.surface = NULL;
526 }
527 if (vc->gfx.convert) {
528 pixman_image_unref(vc->gfx.convert);
529 vc->gfx.convert = NULL;
a4ccabcf
AL
530 }
531
f98f43ea 532 if (vc->gfx.ds && surface &&
e3500d1f
GH
533 surface_width(vc->gfx.ds) == surface_width(surface) &&
534 surface_height(vc->gfx.ds) == surface_height(surface)) {
9d9801cf
GH
535 resized = false;
536 }
e3500d1f 537 vc->gfx.ds = surface;
a4ccabcf 538
f98f43ea
GH
539 if (!surface) {
540 return;
f0875536 541 }
a4ccabcf 542
f0875536
GH
543 if (surface->format == PIXMAN_x8r8g8b8) {
544 /*
545 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
546 *
547 * No need to convert, use surface directly. Should be the
548 * common case as this is qemu_default_pixelformat(32) too.
549 */
e3500d1f 550 vc->gfx.surface = cairo_image_surface_create_for_data
f0875536
GH
551 (surface_data(surface),
552 CAIRO_FORMAT_RGB24,
553 surface_width(surface),
554 surface_height(surface),
555 surface_stride(surface));
556 } else {
557 /* Must convert surface, use pixman to do it. */
e3500d1f
GH
558 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
559 surface_width(surface),
560 surface_height(surface),
561 NULL, 0);
562 vc->gfx.surface = cairo_image_surface_create_for_data
563 ((void *)pixman_image_get_data(vc->gfx.convert),
f0875536 564 CAIRO_FORMAT_RGB24,
e3500d1f
GH
565 pixman_image_get_width(vc->gfx.convert),
566 pixman_image_get_height(vc->gfx.convert),
567 pixman_image_get_stride(vc->gfx.convert));
568 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
569 NULL, vc->gfx.convert,
f0875536 570 0, 0, 0, 0, 0, 0,
e3500d1f
GH
571 pixman_image_get_width(vc->gfx.convert),
572 pixman_image_get_height(vc->gfx.convert));
f0875536 573 }
c6158483 574
9d9801cf 575 if (resized) {
e3500d1f 576 gd_update_windowsize(vc);
9d9801cf 577 } else {
e3500d1f 578 gd_update_full_redraw(vc);
c6158483 579 }
a4ccabcf
AL
580}
581
582/** QEMU Events **/
583
584static void gd_change_runstate(void *opaque, int running, RunState state)
585{
586 GtkDisplayState *s = opaque;
587
588 gd_update_caption(s);
589}
590
591static void gd_mouse_mode_change(Notifier *notify, void *data)
592{
800b0e81 593 GtkDisplayState *s;
99623c90 594 int i;
800b0e81
TI
595
596 s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
597 /* release the grab at switching to absolute mode */
598 if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
599 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
600 FALSE);
601 }
99623c90
GH
602 for (i = 0; i < s->nb_vcs; i++) {
603 VirtualConsole *vc = &s->vc[i];
604 gd_update_cursor(vc);
605 }
a4ccabcf
AL
606}
607
608/** GTK Events **/
609
610static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
611 void *opaque)
612{
613 GtkDisplayState *s = opaque;
e3500d1f 614 int i;
a4ccabcf
AL
615
616 if (!no_quit) {
e3500d1f
GH
617 for (i = 0; i < s->nb_vcs; i++) {
618 if (s->vc[i].type != GD_VC_GFX) {
619 continue;
620 }
621 unregister_displaychangelistener(&s->vc[i].gfx.dcl);
622 }
a4ccabcf
AL
623 qmp_quit(NULL);
624 return FALSE;
625 }
626
627 return TRUE;
628}
629
630static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
631{
e3500d1f
GH
632 VirtualConsole *vc = opaque;
633 GtkDisplayState *s = vc->s;
c6158483 634 int mx, my;
a4ccabcf
AL
635 int ww, wh;
636 int fbw, fbh;
637
c6158483
AL
638 if (!gtk_widget_get_realized(widget)) {
639 return FALSE;
640 }
f98f43ea
GH
641 if (!vc->gfx.ds) {
642 return FALSE;
643 }
c6158483 644
e3500d1f
GH
645 fbw = surface_width(vc->gfx.ds);
646 fbh = surface_height(vc->gfx.ds);
a4ccabcf
AL
647
648 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
649
c6158483 650 if (s->full_screen) {
e3500d1f
GH
651 vc->gfx.scale_x = (double)ww / fbw;
652 vc->gfx.scale_y = (double)wh / fbh;
c6158483
AL
653 } else if (s->free_scale) {
654 double sx, sy;
655
656 sx = (double)ww / fbw;
657 sy = (double)wh / fbh;
658
e3500d1f 659 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
a4ccabcf
AL
660 }
661
e3500d1f
GH
662 fbw *= vc->gfx.scale_x;
663 fbh *= vc->gfx.scale_y;
5104a1f6 664
c6158483
AL
665 mx = my = 0;
666 if (ww > fbw) {
667 mx = (ww - fbw) / 2;
668 }
669 if (wh > fbh) {
670 my = (wh - fbh) / 2;
671 }
672
673 cairo_rectangle(cr, 0, 0, ww, wh);
674
675 /* Optionally cut out the inner area where the pixmap
676 will be drawn. This avoids 'flashing' since we're
677 not double-buffering. Note we're using the undocumented
678 behaviour of drawing the rectangle from right to left
679 to cut out the whole */
680 cairo_rectangle(cr, mx + fbw, my,
681 -1 * fbw, fbh);
682 cairo_fill(cr);
683
e3500d1f
GH
684 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
685 cairo_set_source_surface(cr, vc->gfx.surface,
686 mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
a4ccabcf
AL
687 cairo_paint(cr);
688
689 return TRUE;
690}
691
fe43bca8 692#if !GTK_CHECK_VERSION(3, 0, 0)
a4ccabcf
AL
693static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
694 void *opaque)
695{
696 cairo_t *cr;
697 gboolean ret;
698
699 cr = gdk_cairo_create(gtk_widget_get_window(widget));
700 cairo_rectangle(cr,
701 expose->area.x,
702 expose->area.y,
703 expose->area.width,
704 expose->area.height);
705 cairo_clip(cr);
706
707 ret = gd_draw_event(widget, cr, opaque);
708
709 cairo_destroy(cr);
710
711 return ret;
712}
fe43bca8 713#endif
a4ccabcf
AL
714
715static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
716 void *opaque)
717{
e3500d1f
GH
718 VirtualConsole *vc = opaque;
719 GtkDisplayState *s = vc->s;
a4ccabcf 720 int x, y;
c6158483
AL
721 int mx, my;
722 int fbh, fbw;
723 int ww, wh;
724
f98f43ea
GH
725 if (!vc->gfx.ds) {
726 return TRUE;
727 }
728
e3500d1f
GH
729 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
730 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
a4ccabcf 731
e3500d1f
GH
732 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
733 &ww, &wh);
c6158483
AL
734
735 mx = my = 0;
736 if (ww > fbw) {
737 mx = (ww - fbw) / 2;
738 }
739 if (wh > fbh) {
740 my = (wh - fbh) / 2;
741 }
742
e3500d1f
GH
743 x = (motion->x - mx) / vc->gfx.scale_x;
744 y = (motion->y - my) / vc->gfx.scale_y;
c6158483 745
192f81bf 746 if (qemu_input_is_absolute()) {
e61031cd 747 if (x < 0 || y < 0 ||
e3500d1f
GH
748 x >= surface_width(vc->gfx.ds) ||
749 y >= surface_height(vc->gfx.ds)) {
e61031cd
TI
750 return TRUE;
751 }
e3500d1f
GH
752 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
753 surface_width(vc->gfx.ds));
754 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
755 surface_height(vc->gfx.ds));
192f81bf 756 qemu_input_event_sync();
2884cf5b 757 } else if (s->last_set && s->ptr_owner == vc) {
e3500d1f
GH
758 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
759 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
192f81bf 760 qemu_input_event_sync();
a4ccabcf 761 }
a4ccabcf
AL
762 s->last_x = x;
763 s->last_y = y;
e61031cd 764 s->last_set = TRUE;
a4ccabcf 765
2884cf5b 766 if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
e3500d1f 767 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
5104a1f6
AL
768 int x = (int)motion->x_root;
769 int y = (int)motion->y_root;
770
771 /* In relative mode check to see if client pointer hit
772 * one of the screen edges, and if so move it back by
773 * 200 pixels. This is important because the pointer
774 * in the server doesn't correspond 1-for-1, and so
775 * may still be only half way across the screen. Without
776 * this warp, the server pointer would thus appear to hit
777 * an invisible wall */
778 if (x == 0) {
779 x += 200;
780 }
781 if (y == 0) {
782 y += 200;
783 }
784 if (x == (gdk_screen_get_width(screen) - 1)) {
785 x -= 200;
786 }
787 if (y == (gdk_screen_get_height(screen) - 1)) {
788 y -= 200;
789 }
790
791 if (x != (int)motion->x_root || y != (int)motion->y_root) {
8906de76
DB
792#if GTK_CHECK_VERSION(3, 0, 0)
793 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
794 gdk_device_warp(dev, screen, x, y);
795#else
796 GdkDisplay *display = gtk_widget_get_display(widget);
5104a1f6 797 gdk_display_warp_pointer(display, screen, x, y);
8906de76 798#endif
e61031cd 799 s->last_set = FALSE;
5104a1f6
AL
800 return FALSE;
801 }
802 }
a4ccabcf
AL
803 return TRUE;
804}
805
806static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
807 void *opaque)
808{
e3500d1f
GH
809 VirtualConsole *vc = opaque;
810 GtkDisplayState *s = vc->s;
192f81bf 811 InputButton btn;
a4ccabcf 812
800b0e81
TI
813 /* implicitly grab the input at the first click in the relative mode */
814 if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
2884cf5b
GH
815 !qemu_input_is_absolute() && s->ptr_owner != vc) {
816 gd_ungrab_pointer(s);
817 if (!vc->window) {
818 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
819 TRUE);
820 } else {
2884cf5b 821 gd_grab_pointer(vc);
2884cf5b
GH
822 gd_update_caption(s);
823 }
800b0e81
TI
824 return TRUE;
825 }
826
a4ccabcf 827 if (button->button == 1) {
192f81bf 828 btn = INPUT_BUTTON_LEFT;
a4ccabcf 829 } else if (button->button == 2) {
192f81bf 830 btn = INPUT_BUTTON_MIDDLE;
a4ccabcf 831 } else if (button->button == 3) {
192f81bf 832 btn = INPUT_BUTTON_RIGHT;
a4ccabcf 833 } else {
192f81bf 834 return TRUE;
a4ccabcf
AL
835 }
836
e3500d1f
GH
837 qemu_input_queue_btn(vc->gfx.dcl.con, btn,
838 button->type == GDK_BUTTON_PRESS);
192f81bf 839 qemu_input_event_sync();
a4ccabcf
AL
840 return TRUE;
841}
842
d58b9122
JK
843static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
844 void *opaque)
845{
e3500d1f 846 VirtualConsole *vc = opaque;
d58b9122
JK
847 InputButton btn;
848
849 if (scroll->direction == GDK_SCROLL_UP) {
850 btn = INPUT_BUTTON_WHEEL_UP;
851 } else if (scroll->direction == GDK_SCROLL_DOWN) {
852 btn = INPUT_BUTTON_WHEEL_DOWN;
853 } else {
854 return TRUE;
855 }
856
e3500d1f 857 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
d58b9122 858 qemu_input_event_sync();
e3500d1f 859 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
d58b9122
JK
860 qemu_input_event_sync();
861 return TRUE;
862}
863
0a337ed0 864static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
a4ccabcf 865{
932f2d7e 866 int qemu_keycode;
a4ccabcf 867
0a337ed0
GH
868#ifdef GDK_WINDOWING_WIN32
869 if (GDK_IS_WIN32_DISPLAY(dpy)) {
870 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
871 switch (qemu_keycode) {
872 case 103: /* alt gr */
873 qemu_keycode = 56 | SCANCODE_GREY;
874 break;
875 }
876 return qemu_keycode;
2777ccc5 877 }
0a337ed0 878#endif
a4ccabcf
AL
879
880 if (gdk_keycode < 9) {
881 qemu_keycode = 0;
882 } else if (gdk_keycode < 97) {
883 qemu_keycode = gdk_keycode - 8;
0a337ed0
GH
884#ifdef GDK_WINDOWING_X11
885 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
3158a348
BR
886 if (s->has_evdev) {
887 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
888 } else {
889 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
890 }
0a337ed0 891#endif
a4ccabcf
AL
892 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
893 qemu_keycode = 0x70;
894 } else if (gdk_keycode == 211) { /* backslash */
895 qemu_keycode = 0x73;
896 } else {
897 qemu_keycode = 0;
898 }
899
932f2d7e
GH
900 return qemu_keycode;
901}
902
f8c223f6
GH
903static gboolean gd_text_key_down(GtkWidget *widget,
904 GdkEventKey *key, void *opaque)
905{
906 VirtualConsole *vc = opaque;
907 QemuConsole *con = vc->gfx.dcl.con;
908
909 if (key->length) {
910 kbd_put_string_console(con, key->string, key->length);
911 } else {
912 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
913 key->hardware_keycode);
914 int qcode = qemu_input_key_number_to_qcode(num);
915 kbd_put_qcode_console(con, qcode);
916 }
917 return TRUE;
918}
919
932f2d7e
GH
920static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
921{
922 VirtualConsole *vc = opaque;
923 GtkDisplayState *s = vc->s;
924 int gdk_keycode = key->hardware_keycode;
925 int qemu_keycode;
926 int i;
927
1a01716a
JK
928 if (s->ignore_keys) {
929 s->ignore_keys = (key->type == GDK_KEY_PRESS);
930 return TRUE;
931 }
932
5c960521
MD
933 if (key->keyval == GDK_KEY_Pause) {
934 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
935 key->type == GDK_KEY_PRESS);
936 return TRUE;
937 }
938
0a337ed0
GH
939 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
940 gdk_keycode);
932f2d7e 941
74444bc1 942 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
ef0dd982 943 (key->type == GDK_KEY_PRESS) ? "down" : "up");
a4ccabcf 944
6db253ca
JK
945 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
946 if (qemu_keycode == modifier_keycode[i]) {
947 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
948 }
949 }
950
e3500d1f 951 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
af98ba92 952 key->type == GDK_KEY_PRESS);
a4ccabcf
AL
953
954 return TRUE;
955}
956
0d0e044d
TI
957static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
958{
959 if (event->type == GDK_MOTION_NOTIFY) {
960 return gd_motion_event(widget, &event->motion, opaque);
961 }
962 return FALSE;
963}
964
a4ccabcf
AL
965/** Window Menu Actions **/
966
30e8f22b
JK
967static void gd_menu_pause(GtkMenuItem *item, void *opaque)
968{
969 GtkDisplayState *s = opaque;
970
971 if (s->external_pause_update) {
972 return;
973 }
974 if (runstate_is_running()) {
975 qmp_stop(NULL);
976 } else {
977 qmp_cont(NULL);
978 }
979}
980
981static void gd_menu_reset(GtkMenuItem *item, void *opaque)
982{
983 qmp_system_reset(NULL);
984}
985
986static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
987{
988 qmp_system_powerdown(NULL);
989}
990
a4ccabcf
AL
991static void gd_menu_quit(GtkMenuItem *item, void *opaque)
992{
993 qmp_quit(NULL);
994}
995
996static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
997{
998 GtkDisplayState *s = opaque;
e3500d1f 999 VirtualConsole *vc = gd_vc_find_by_menu(s);
e72b59fa 1000 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
832189c9 1001 gint page;
a4ccabcf 1002
e3500d1f
GH
1003 gtk_release_modifiers(s);
1004 if (vc) {
e72b59fa
JS
1005 page = gtk_notebook_page_num(nb, vc->tab_item);
1006 gtk_notebook_set_current_page(nb, page);
9d677e1c 1007 gtk_widget_grab_focus(vc->focus);
a4ccabcf 1008 }
1a01716a 1009 s->ignore_keys = false;
a4ccabcf
AL
1010}
1011
277836c8
CR
1012static void gd_accel_switch_vc(void *opaque)
1013{
1014 VirtualConsole *vc = opaque;
1a01716a 1015
277836c8 1016 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1a01716a
JK
1017#if !GTK_CHECK_VERSION(3, 0, 0)
1018 /* GTK2 sends the accel key to the target console - ignore this until */
1019 vc->s->ignore_keys = true;
1020#endif
277836c8
CR
1021}
1022
a4ccabcf
AL
1023static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1024{
1025 GtkDisplayState *s = opaque;
fa7a1e52 1026 VirtualConsole *vc = gd_vc_find_current(s);
a4ccabcf
AL
1027
1028 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1029 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1030 } else {
1031 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1032 }
fa7a1e52 1033 gd_update_windowsize(vc);
a4ccabcf
AL
1034}
1035
cdeb7090
GH
1036static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1037 void *opaque)
1038{
1039 VirtualConsole *vc = opaque;
1040 GtkDisplayState *s = vc->s;
1041
1042 gtk_widget_set_sensitive(vc->menu_item, true);
316cb068 1043 gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
cdeb7090
GH
1044 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1045 vc->tab_item, vc->label);
1046 gtk_widget_destroy(vc->window);
1047 vc->window = NULL;
1048 return TRUE;
1049}
1050
0c77a37f
GH
1051static gboolean gd_win_grab(void *opaque)
1052{
1053 VirtualConsole *vc = opaque;
1054
1055 fprintf(stderr, "%s: %s\n", __func__, vc->label);
1056 if (vc->s->ptr_owner) {
1057 gd_ungrab_pointer(vc->s);
1058 } else {
1059 gd_grab_pointer(vc);
1060 }
1061 gd_update_caption(vc->s);
1062 return TRUE;
1063}
1064
cdeb7090
GH
1065static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1066{
1067 GtkDisplayState *s = opaque;
1068 VirtualConsole *vc = gd_vc_find_current(s);
cdeb7090 1069
f8c223f6
GH
1070 if (vc->type == GD_VC_GFX &&
1071 qemu_console_is_graphic(vc->gfx.dcl.con)) {
aa0a55d4
GH
1072 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1073 FALSE);
cdeb7090
GH
1074 }
1075 if (!vc->window) {
1076 gtk_widget_set_sensitive(vc->menu_item, false);
1077 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
316cb068 1078 gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
cdeb7090 1079
cdeb7090
GH
1080 g_signal_connect(vc->window, "delete-event",
1081 G_CALLBACK(gd_tab_window_close), vc);
1082 gtk_widget_show_all(vc->window);
4eeaa3a8 1083
f8c223f6
GH
1084 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1085 GtkAccelGroup *ag = gtk_accel_group_new();
1086 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
0c77a37f 1087
f8c223f6
GH
1088 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
1089 vc, NULL);
1090 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1091 }
0c77a37f 1092
82fc1809 1093 gd_update_geometry_hints(vc);
4eeaa3a8 1094 gd_update_caption(s);
cdeb7090
GH
1095 }
1096}
1097
c6158483
AL
1098static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1099{
1100 GtkDisplayState *s = opaque;
e3500d1f 1101 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1102
10409282 1103 if (!s->full_screen) {
c6158483 1104 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
b0f31820 1105 gtk_widget_hide(s->menu_bar);
e3500d1f
GH
1106 if (vc->type == GD_VC_GFX) {
1107 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
f8c223f6
GH
1108 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1109 gtk_check_menu_item_set_active
1110 (GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
1111 }
c6158483 1112 }
e3500d1f 1113 gtk_window_fullscreen(GTK_WINDOW(s->window));
c6158483
AL
1114 s->full_screen = TRUE;
1115 } else {
1116 gtk_window_unfullscreen(GTK_WINDOW(s->window));
1117 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
b0f31820 1118 gtk_widget_show(s->menu_bar);
c6158483 1119 s->full_screen = FALSE;
e3500d1f
GH
1120 if (vc->type == GD_VC_GFX) {
1121 vc->gfx.scale_x = 1.0;
1122 vc->gfx.scale_y = 1.0;
82fc1809 1123 gd_update_windowsize(vc);
e3500d1f
GH
1124 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1125 FALSE);
1126 }
c6158483
AL
1127 }
1128
e3500d1f 1129 gd_update_cursor(vc);
c6158483
AL
1130}
1131
95414914
CR
1132static void gd_accel_full_screen(void *opaque)
1133{
1134 GtkDisplayState *s = opaque;
1135 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1136}
1137
c6158483
AL
1138static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1139{
1140 GtkDisplayState *s = opaque;
e3500d1f 1141 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1142
1143 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1144 FALSE);
1145
82fc1809
GH
1146 vc->gfx.scale_x += VC_SCALE_STEP;
1147 vc->gfx.scale_y += VC_SCALE_STEP;
c6158483 1148
e3500d1f 1149 gd_update_windowsize(vc);
c6158483
AL
1150}
1151
1152static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1153{
1154 GtkDisplayState *s = opaque;
e3500d1f 1155 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1156
1157 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1158 FALSE);
1159
82fc1809
GH
1160 vc->gfx.scale_x -= VC_SCALE_STEP;
1161 vc->gfx.scale_y -= VC_SCALE_STEP;
c6158483 1162
82fc1809
GH
1163 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1164 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
c6158483 1165
e3500d1f 1166 gd_update_windowsize(vc);
c6158483
AL
1167}
1168
1169static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1170{
1171 GtkDisplayState *s = opaque;
e3500d1f 1172 VirtualConsole *vc = gd_vc_find_current(s);
c6158483 1173
e3500d1f
GH
1174 vc->gfx.scale_x = 1.0;
1175 vc->gfx.scale_y = 1.0;
c6158483 1176
e3500d1f 1177 gd_update_windowsize(vc);
c6158483
AL
1178}
1179
1180static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1181{
1182 GtkDisplayState *s = opaque;
e3500d1f 1183 VirtualConsole *vc = gd_vc_find_current(s);
c6158483
AL
1184
1185 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1186 s->free_scale = TRUE;
1187 } else {
1188 s->free_scale = FALSE;
e3500d1f
GH
1189 vc->gfx.scale_x = 1.0;
1190 vc->gfx.scale_y = 1.0;
c6158483
AL
1191 }
1192
82fc1809 1193 gd_update_windowsize(vc);
e3500d1f 1194 gd_update_full_redraw(vc);
c6158483
AL
1195}
1196
655199da 1197#if GTK_CHECK_VERSION(3, 0, 0)
f50def91
GH
1198static void gd_grab_devices(VirtualConsole *vc, bool grab,
1199 GdkInputSource source, GdkEventMask mask,
1200 GdkCursor *cursor)
1201{
e3500d1f 1202 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
655199da 1203 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1204 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1205 GList *tmp = devs;
1206
1207 for (tmp = devs; tmp; tmp = tmp->next) {
655199da 1208 GdkDevice *dev = tmp->data;
f50def91
GH
1209 if (gdk_device_get_source(dev) != source) {
1210 continue;
1211 }
1212 if (grab) {
1213 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1214 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1215 mask, cursor, GDK_CURRENT_TIME);
1216 } else {
1217 gdk_device_ungrab(dev, GDK_CURRENT_TIME);
655199da 1218 }
655199da 1219 }
f50def91
GH
1220 g_list_free(devs);
1221}
1222#endif
1223
1224static void gd_grab_keyboard(VirtualConsole *vc)
1225{
1226#if GTK_CHECK_VERSION(3, 0, 0)
1227 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1228 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1229 NULL);
655199da 1230#else
e3500d1f 1231 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
5104a1f6
AL
1232 FALSE,
1233 GDK_CURRENT_TIME);
655199da 1234#endif
4c638e2e 1235 vc->s->kbd_owner = vc;
1c856da5 1236 trace_gd_grab(vc->label, "kbd", true);
5104a1f6
AL
1237}
1238
4c638e2e 1239static void gd_ungrab_keyboard(GtkDisplayState *s)
5104a1f6 1240{
4c638e2e
GH
1241 VirtualConsole *vc = s->kbd_owner;
1242
1243 if (vc == NULL) {
1244 return;
1245 }
1246 s->kbd_owner = NULL;
1247
655199da 1248#if GTK_CHECK_VERSION(3, 0, 0)
f50def91 1249 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
655199da 1250#else
5104a1f6 1251 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
655199da 1252#endif
1c856da5 1253 trace_gd_grab(vc->label, "kbd", false);
5104a1f6
AL
1254}
1255
e3500d1f 1256static void gd_grab_pointer(VirtualConsole *vc)
2a05485d 1257{
e3500d1f 1258 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
ecce1929 1259#if GTK_CHECK_VERSION(3, 0, 0)
2a05485d 1260 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91
GH
1261 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1262 GDK_POINTER_MOTION_MASK |
1263 GDK_BUTTON_PRESS_MASK |
1264 GDK_BUTTON_RELEASE_MASK |
1265 GDK_BUTTON_MOTION_MASK |
1266 GDK_SCROLL_MASK,
1267 vc->s->null_cursor);
ecce1929 1268 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
e3500d1f 1269 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
2a05485d 1270#else
e3500d1f 1271 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
2a05485d
DB
1272 FALSE, /* All events to come to our window directly */
1273 GDK_POINTER_MOTION_MASK |
1274 GDK_BUTTON_PRESS_MASK |
1275 GDK_BUTTON_RELEASE_MASK |
1276 GDK_BUTTON_MOTION_MASK |
1277 GDK_SCROLL_MASK,
1278 NULL, /* Allow cursor to move over entire desktop */
e3500d1f 1279 vc->s->null_cursor,
2a05485d 1280 GDK_CURRENT_TIME);
ecce1929 1281 gdk_display_get_pointer(display, NULL,
e3500d1f 1282 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
2a05485d 1283#endif
4c638e2e 1284 vc->s->ptr_owner = vc;
1c856da5 1285 trace_gd_grab(vc->label, "ptr", true);
2a05485d
DB
1286}
1287
4c638e2e 1288static void gd_ungrab_pointer(GtkDisplayState *s)
2a05485d 1289{
4c638e2e
GH
1290 VirtualConsole *vc = s->ptr_owner;
1291
1292 if (vc == NULL) {
1293 return;
1294 }
1295 s->ptr_owner = NULL;
1296
e3500d1f 1297 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
ecce1929 1298#if GTK_CHECK_VERSION(3, 0, 0)
2a05485d 1299 GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
f50def91 1300 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
ecce1929 1301 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
e3500d1f
GH
1302 gtk_widget_get_screen(vc->gfx.drawing_area),
1303 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d
DB
1304#else
1305 gdk_pointer_ungrab(GDK_CURRENT_TIME);
ecce1929 1306 gdk_display_warp_pointer(display,
e3500d1f
GH
1307 gtk_widget_get_screen(vc->gfx.drawing_area),
1308 vc->s->grab_x_root, vc->s->grab_y_root);
2a05485d 1309#endif
1c856da5 1310 trace_gd_grab(vc->label, "ptr", false);
2a05485d
DB
1311}
1312
5104a1f6
AL
1313static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1314{
1315 GtkDisplayState *s = opaque;
e3500d1f 1316 VirtualConsole *vc = gd_vc_find_current(s);
5104a1f6
AL
1317
1318 if (gd_is_grab_active(s)) {
746b8670
GH
1319 if (!gd_grab_on_hover(s)) {
1320 gd_grab_keyboard(vc);
1321 }
e3500d1f 1322 gd_grab_pointer(vc);
5104a1f6 1323 } else {
4c638e2e
GH
1324 gd_ungrab_keyboard(s);
1325 gd_ungrab_pointer(s);
5104a1f6
AL
1326 }
1327
1328 gd_update_caption(s);
e3500d1f 1329 gd_update_cursor(vc);
5104a1f6
AL
1330}
1331
a4ccabcf
AL
1332static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1333 gpointer data)
1334{
1335 GtkDisplayState *s = data;
e3500d1f 1336 VirtualConsole *vc;
5104a1f6 1337 gboolean on_vga;
a4ccabcf
AL
1338
1339 if (!gtk_widget_get_realized(s->notebook)) {
1340 return;
1341 }
1342
6fa27697
GH
1343#ifdef VTE_RESIZE_HACK
1344 vc = gd_vc_find_current(s);
1345 if (vc && vc->type == GD_VC_VTE) {
1346 gtk_widget_hide(vc->vte.terminal);
1347 }
1348#endif
e3500d1f
GH
1349 vc = gd_vc_find_by_page(s, arg2);
1350 if (!vc) {
1351 return;
1352 }
6fa27697
GH
1353#ifdef VTE_RESIZE_HACK
1354 if (vc->type == GD_VC_VTE) {
1355 gtk_widget_show(vc->vte.terminal);
1356 }
1357#endif
e3500d1f
GH
1358 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1359 TRUE);
f8c223f6
GH
1360 on_vga = (vc->type == GD_VC_GFX &&
1361 qemu_console_is_graphic(vc->gfx.dcl.con));
5104a1f6
AL
1362 if (!on_vga) {
1363 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1364 FALSE);
c6158483
AL
1365 } else if (s->full_screen) {
1366 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1367 TRUE);
5104a1f6 1368 }
5104a1f6
AL
1369 gtk_widget_set_sensitive(s->grab_item, on_vga);
1370
82fc1809 1371 gd_update_windowsize(vc);
e3500d1f 1372 gd_update_cursor(vc);
a4ccabcf
AL
1373}
1374
e3500d1f
GH
1375static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1376 gpointer opaque)
5104a1f6 1377{
e3500d1f
GH
1378 VirtualConsole *vc = opaque;
1379 GtkDisplayState *s = vc->s;
5104a1f6 1380
2884cf5b
GH
1381 if (gd_grab_on_hover(s)) {
1382 gd_ungrab_keyboard(s);
e3500d1f 1383 gd_grab_keyboard(vc);
2884cf5b 1384 gd_update_caption(s);
5104a1f6 1385 }
5104a1f6
AL
1386 return TRUE;
1387}
1388
e3500d1f
GH
1389static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1390 gpointer opaque)
5104a1f6 1391{
e3500d1f
GH
1392 VirtualConsole *vc = opaque;
1393 GtkDisplayState *s = vc->s;
5104a1f6 1394
2884cf5b 1395 if (gd_grab_on_hover(s)) {
4c638e2e 1396 gd_ungrab_keyboard(s);
2884cf5b 1397 gd_update_caption(s);
5104a1f6 1398 }
5104a1f6
AL
1399 return TRUE;
1400}
1401
6db253ca 1402static gboolean gd_focus_out_event(GtkWidget *widget,
e3500d1f 1403 GdkEventCrossing *crossing, gpointer opaque)
6db253ca 1404{
e3500d1f
GH
1405 VirtualConsole *vc = opaque;
1406 GtkDisplayState *s = vc->s;
6db253ca
JK
1407
1408 gtk_release_modifiers(s);
6db253ca
JK
1409 return TRUE;
1410}
1411
1301e515
GH
1412static gboolean gd_configure(GtkWidget *widget,
1413 GdkEventConfigure *cfg, gpointer opaque)
1414{
1415 VirtualConsole *vc = opaque;
1416 QemuUIInfo info;
1417
1418 memset(&info, 0, sizeof(info));
1419 info.width = cfg->width;
1420 info.height = cfg->height;
1421 dpy_set_ui_info(vc->gfx.dcl.con, &info);
1422 return FALSE;
1423}
1424
d861def3
AL
1425/** Virtual Console Callbacks **/
1426
ed1132e4 1427static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
cdeb7090 1428 int idx, GSList *group, GtkWidget *view_menu)
ed1132e4 1429{
cdeb7090 1430 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
277836c8
CR
1431 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
1432 HOTKEY_MODIFIERS, 0,
1433 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
1434#if GTK_CHECK_VERSION(3, 8, 0)
1435 gtk_accel_label_set_accel(
1436 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
1437 GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1438#endif
ed1132e4
GH
1439
1440 g_signal_connect(vc->menu_item, "activate",
1441 G_CALLBACK(gd_menu_switch_vc), s);
1442 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1443
277836c8 1444 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
ed1132e4
GH
1445 return group;
1446}
1447
ee5f31e4 1448#if defined(CONFIG_VTE)
0fb20d1c
CR
1449static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1450{
1451 VirtualConsole *vc = opaque;
1452
1453 if (gtk_adjustment_get_upper(adjustment) >
1454 gtk_adjustment_get_page_size(adjustment)) {
271a25c0 1455 gtk_widget_show(vc->vte.scrollbar);
0fb20d1c 1456 } else {
271a25c0 1457 gtk_widget_hide(vc->vte.scrollbar);
0fb20d1c
CR
1458 }
1459}
1460
d861def3
AL
1461static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1462{
1463 VirtualConsole *vc = chr->opaque;
1464
271a25c0 1465 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
d4370741 1466 return len;
d861def3
AL
1467}
1468
1469static int nb_vcs;
1470static CharDriverState *vcs[MAX_VCS];
1471
702ec69c 1472static CharDriverState *gd_vc_handler(ChardevVC *unused)
d861def3
AL
1473{
1474 CharDriverState *chr;
1475
1476 chr = g_malloc0(sizeof(*chr));
1477 chr->chr_write = gd_vc_chr_write;
bd5c51ee
MR
1478 /* defer OPENED events until our vc is fully initialized */
1479 chr->explicit_be_open = true;
d861def3
AL
1480
1481 vcs[nb_vcs++] = chr;
1482
1483 return chr;
1484}
1485
d4370741
CR
1486static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1487 gpointer user_data)
d861def3 1488{
d4370741 1489 VirtualConsole *vc = user_data;
d861def3 1490
271a25c0 1491 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size);
d861def3
AL
1492 return TRUE;
1493}
1494
ed1132e4
GH
1495static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1496 CharDriverState *chr, int idx,
e3500d1f 1497 GSList *group, GtkWidget *view_menu)
d861def3 1498{
d861def3 1499 char buffer[32];
0fb20d1c
CR
1500 GtkWidget *box;
1501 GtkWidget *scrollbar;
1502 GtkAdjustment *vadjustment;
d861def3 1503
e3500d1f 1504 vc->s = s;
ed1132e4 1505 vc->vte.chr = chr;
d861def3 1506
ed1132e4 1507 snprintf(buffer, sizeof(buffer), "vc%d", idx);
cdeb7090
GH
1508 vc->label = g_strdup_printf("%s", vc->vte.chr->label
1509 ? vc->vte.chr->label : buffer);
1510 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
d861def3 1511
271a25c0
GH
1512 vc->vte.terminal = vte_terminal_new();
1513 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
d861def3 1514
271a25c0 1515 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
82fc1809
GH
1516 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1517 VC_TERM_X_MIN, VC_TERM_Y_MIN);
d861def3 1518
0fb20d1c 1519#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
271a25c0
GH
1520 vadjustment = gtk_scrollable_get_vadjustment
1521 (GTK_SCROLLABLE(vc->vte.terminal));
0fb20d1c 1522#else
271a25c0 1523 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
0fb20d1c
CR
1524#endif
1525
1526#if GTK_CHECK_VERSION(3, 0, 0)
1527 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1528 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1529#else
1530 box = gtk_hbox_new(false, 2);
1531 scrollbar = gtk_vscrollbar_new(vadjustment);
1532#endif
1533
271a25c0 1534 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
0fb20d1c
CR
1535 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1536
271a25c0
GH
1537 vc->vte.chr->opaque = vc;
1538 vc->vte.box = box;
1539 vc->vte.scrollbar = scrollbar;
0fb20d1c
CR
1540
1541 g_signal_connect(vadjustment, "changed",
1542 G_CALLBACK(gd_vc_adjustment_changed), vc);
d861def3 1543
e3500d1f 1544 vc->type = GD_VC_VTE;
271a25c0 1545 vc->tab_item = box;
9d677e1c 1546 vc->focus = vc->vte.terminal;
271a25c0 1547 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
cdeb7090 1548 gtk_label_new(vc->label));
d861def3 1549
271a25c0
GH
1550 qemu_chr_be_generic_open(vc->vte.chr);
1551 if (vc->vte.chr->init) {
1552 vc->vte.chr->init(vc->vte.chr);
d861def3
AL
1553 }
1554
d861def3 1555 return group;
a4ccabcf
AL
1556}
1557
ed1132e4 1558static void gd_vcs_init(GtkDisplayState *s, GSList *group,
ee5f31e4
GH
1559 GtkWidget *view_menu)
1560{
1561 int i;
1562
1563 for (i = 0; i < nb_vcs; i++) {
ed1132e4
GH
1564 VirtualConsole *vc = &s->vc[s->nb_vcs];
1565 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
ee5f31e4
GH
1566 s->nb_vcs++;
1567 }
1568}
1569#endif /* CONFIG_VTE */
1570
a4ccabcf
AL
1571/** Window Creation **/
1572
e3500d1f
GH
1573static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1574{
1575#if GTK_CHECK_VERSION(3, 0, 0)
1576 g_signal_connect(vc->gfx.drawing_area, "draw",
1577 G_CALLBACK(gd_draw_event), vc);
1578#else
1579 g_signal_connect(vc->gfx.drawing_area, "expose-event",
1580 G_CALLBACK(gd_expose_event), vc);
1581#endif
f8c223f6
GH
1582 if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
1583 g_signal_connect(vc->gfx.drawing_area, "event",
1584 G_CALLBACK(gd_event), vc);
1585 g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1586 G_CALLBACK(gd_button_event), vc);
1587 g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1588 G_CALLBACK(gd_button_event), vc);
1589 g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1590 G_CALLBACK(gd_scroll_event), vc);
1591 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1592 G_CALLBACK(gd_key_event), vc);
1593 g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1594 G_CALLBACK(gd_key_event), vc);
1595
1596 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1597 G_CALLBACK(gd_enter_event), vc);
1598 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1599 G_CALLBACK(gd_leave_event), vc);
1600 g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1601 G_CALLBACK(gd_focus_out_event), vc);
1301e515
GH
1602 g_signal_connect(vc->gfx.drawing_area, "configure-event",
1603 G_CALLBACK(gd_configure), vc);
f8c223f6
GH
1604 } else {
1605 g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1606 G_CALLBACK(gd_text_key_down), vc);
1607 }
e3500d1f
GH
1608}
1609
a4ccabcf
AL
1610static void gd_connect_signals(GtkDisplayState *s)
1611{
1612 g_signal_connect(s->show_tabs_item, "activate",
1613 G_CALLBACK(gd_menu_show_tabs), s);
cdeb7090
GH
1614 g_signal_connect(s->untabify_item, "activate",
1615 G_CALLBACK(gd_menu_untabify), s);
a4ccabcf
AL
1616
1617 g_signal_connect(s->window, "delete-event",
1618 G_CALLBACK(gd_window_close), s);
1619
30e8f22b
JK
1620 g_signal_connect(s->pause_item, "activate",
1621 G_CALLBACK(gd_menu_pause), s);
1622 g_signal_connect(s->reset_item, "activate",
1623 G_CALLBACK(gd_menu_reset), s);
1624 g_signal_connect(s->powerdown_item, "activate",
1625 G_CALLBACK(gd_menu_powerdown), s);
a4ccabcf
AL
1626 g_signal_connect(s->quit_item, "activate",
1627 G_CALLBACK(gd_menu_quit), s);
c6158483
AL
1628 g_signal_connect(s->full_screen_item, "activate",
1629 G_CALLBACK(gd_menu_full_screen), s);
1630 g_signal_connect(s->zoom_in_item, "activate",
1631 G_CALLBACK(gd_menu_zoom_in), s);
1632 g_signal_connect(s->zoom_out_item, "activate",
1633 G_CALLBACK(gd_menu_zoom_out), s);
1634 g_signal_connect(s->zoom_fixed_item, "activate",
1635 G_CALLBACK(gd_menu_zoom_fixed), s);
1636 g_signal_connect(s->zoom_fit_item, "activate",
1637 G_CALLBACK(gd_menu_zoom_fit), s);
5104a1f6
AL
1638 g_signal_connect(s->grab_item, "activate",
1639 G_CALLBACK(gd_menu_grab_input), s);
a4ccabcf
AL
1640 g_signal_connect(s->notebook, "switch-page",
1641 G_CALLBACK(gd_change_page), s);
1642}
1643
400519d2 1644static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
a4ccabcf 1645{
bf9b255f 1646 GtkWidget *machine_menu;
a4ccabcf
AL
1647 GtkWidget *separator;
1648
bf9b255f 1649 machine_menu = gtk_menu_new();
400519d2 1650 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
30e8f22b
JK
1651
1652 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
bf9b255f 1653 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
30e8f22b
JK
1654
1655 separator = gtk_separator_menu_item_new();
bf9b255f 1656 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
30e8f22b 1657
9068f20d 1658 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
bf9b255f 1659 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
30e8f22b 1660
9068f20d 1661 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
bf9b255f 1662 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
30e8f22b
JK
1663
1664 separator = gtk_separator_menu_item_new();
bf9b255f 1665 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
a4ccabcf 1666
3d914488 1667 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
a4ccabcf 1668 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
30e8f22b 1669 "<QEMU>/Machine/Quit");
3d914488 1670 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
db1da1f2 1671 GDK_KEY_q, HOTKEY_MODIFIERS);
bf9b255f 1672 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
a4ccabcf 1673
bf9b255f
AL
1674 return machine_menu;
1675}
1676
e3500d1f 1677static const DisplayChangeListenerOps dcl_ops = {
fcf73af6
BH
1678 .dpy_name = "gtk",
1679 .dpy_gfx_update = gd_update,
1680 .dpy_gfx_switch = gd_switch,
1681 .dpy_gfx_check_format = qemu_pixman_check_format,
1682 .dpy_refresh = gd_refresh,
1683 .dpy_mouse_set = gd_mouse_set,
1684 .dpy_cursor_define = gd_cursor_define,
e3500d1f
GH
1685};
1686
1687static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
ed1132e4 1688 QemuConsole *con, int idx,
e3500d1f
GH
1689 GSList *group, GtkWidget *view_menu)
1690{
779ce88f 1691 vc->label = qemu_console_get_label(con);
e3500d1f
GH
1692 vc->s = s;
1693 vc->gfx.scale_x = 1.0;
1694 vc->gfx.scale_y = 1.0;
1695
1696 vc->gfx.drawing_area = gtk_drawing_area_new();
1697 gtk_widget_add_events(vc->gfx.drawing_area,
1698 GDK_POINTER_MOTION_MASK |
1699 GDK_BUTTON_PRESS_MASK |
1700 GDK_BUTTON_RELEASE_MASK |
1701 GDK_BUTTON_MOTION_MASK |
1702 GDK_ENTER_NOTIFY_MASK |
1703 GDK_LEAVE_NOTIFY_MASK |
1704 GDK_SCROLL_MASK |
1705 GDK_KEY_PRESS_MASK);
e3500d1f
GH
1706 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1707
1708 vc->type = GD_VC_GFX;
1709 vc->tab_item = vc->gfx.drawing_area;
9d677e1c 1710 vc->focus = vc->gfx.drawing_area;
e3500d1f 1711 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
cdeb7090 1712 vc->tab_item, gtk_label_new(vc->label));
e3500d1f
GH
1713
1714 vc->gfx.dcl.ops = &dcl_ops;
1715 vc->gfx.dcl.con = con;
1716 register_displaychangelistener(&vc->gfx.dcl);
1717
f8c223f6
GH
1718 gd_connect_vc_gfx_signals(vc);
1719 group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1720
1301e515
GH
1721 if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
1722 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
1723 }
1724
e3500d1f
GH
1725 return group;
1726}
1727
400519d2 1728static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
bf9b255f
AL
1729{
1730 GSList *group = NULL;
1731 GtkWidget *view_menu;
1732 GtkWidget *separator;
ed1132e4
GH
1733 QemuConsole *con;
1734 int vc;
bf9b255f
AL
1735
1736 view_menu = gtk_menu_new();
400519d2 1737 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
a4ccabcf 1738
3d914488 1739 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
95414914
CR
1740
1741 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
1742 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
1743#if GTK_CHECK_VERSION(3, 8, 0)
1744 gtk_accel_label_set_accel(
1745 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
1746 GDK_KEY_f, HOTKEY_MODIFIERS);
1747#endif
bf9b255f 1748 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
c6158483
AL
1749
1750 separator = gtk_separator_menu_item_new();
bf9b255f 1751 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 1752
3d914488 1753 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
c6158483
AL
1754 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1755 "<QEMU>/View/Zoom In");
b1e749c0
JK
1756 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1757 HOTKEY_MODIFIERS);
bf9b255f 1758 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
c6158483 1759
3d914488 1760 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
c6158483
AL
1761 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1762 "<QEMU>/View/Zoom Out");
b1e749c0
JK
1763 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1764 HOTKEY_MODIFIERS);
bf9b255f 1765 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
c6158483 1766
3d914488 1767 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
c6158483
AL
1768 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1769 "<QEMU>/View/Zoom Fixed");
b1e749c0
JK
1770 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1771 HOTKEY_MODIFIERS);
bf9b255f 1772 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
c6158483 1773
834574ea 1774 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
bf9b255f 1775 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
c6158483
AL
1776
1777 separator = gtk_separator_menu_item_new();
bf9b255f 1778 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
c6158483 1779
834574ea 1780 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
bf9b255f 1781 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
5104a1f6 1782
834574ea 1783 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
5104a1f6
AL
1784 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1785 "<QEMU>/View/Grab Input");
b1e749c0
JK
1786 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1787 HOTKEY_MODIFIERS);
bf9b255f 1788 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
5104a1f6 1789
a4ccabcf 1790 separator = gtk_separator_menu_item_new();
bf9b255f 1791 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 1792
e3500d1f 1793 /* gfx */
ed1132e4
GH
1794 for (vc = 0;; vc++) {
1795 con = qemu_console_lookup_by_index(vc);
f8c223f6 1796 if (!con) {
ed1132e4
GH
1797 break;
1798 }
1799 group = gd_vc_gfx_init(s, &s->vc[vc], con,
1800 vc, group, view_menu);
1801 s->nb_vcs++;
1802 }
a4ccabcf 1803
ee5f31e4 1804#if defined(CONFIG_VTE)
e3500d1f 1805 /* vte */
ed1132e4 1806 gd_vcs_init(s, group, view_menu);
ee5f31e4 1807#endif
d861def3 1808
a4ccabcf 1809 separator = gtk_separator_menu_item_new();
bf9b255f 1810 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
a4ccabcf 1811
834574ea 1812 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
bf9b255f 1813 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
a4ccabcf 1814
cdeb7090
GH
1815 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1816 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
1817
bf9b255f
AL
1818 return view_menu;
1819}
1820
1821static void gd_create_menus(GtkDisplayState *s)
1822{
400519d2
CR
1823 s->accel_group = gtk_accel_group_new();
1824 s->machine_menu = gd_create_menu_machine(s);
1825 s->view_menu = gd_create_menu_view(s);
bf9b255f
AL
1826
1827 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
30e8f22b
JK
1828 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1829 s->machine_menu);
1830 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
a4ccabcf 1831
bf9b255f 1832 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
a4ccabcf
AL
1833 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1834 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
bf9b255f 1835
400519d2
CR
1836 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
1837 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
a4ccabcf
AL
1838}
1839
3158a348
BR
1840static void gd_set_keycode_type(GtkDisplayState *s)
1841{
0a337ed0 1842#ifdef GDK_WINDOWING_X11
3158a348 1843 GdkDisplay *display = gtk_widget_get_display(s->window);
0a337ed0
GH
1844 if (GDK_IS_X11_DISPLAY(display)) {
1845 Display *x11_display = gdk_x11_display_get_xdisplay(display);
1846 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
1847 XkbUseCoreKbd);
1848 char *keycodes = NULL;
1849
1850 if (desc && desc->names) {
1851 keycodes = XGetAtomName(x11_display, desc->names->keycodes);
1852 }
1853 if (keycodes == NULL) {
1854 fprintf(stderr, "could not lookup keycode name\n");
1855 } else if (strstart(keycodes, "evdev", NULL)) {
1856 s->has_evdev = true;
1857 } else if (!strstart(keycodes, "xfree86", NULL)) {
1858 fprintf(stderr, "unknown keycodes `%s', please report to "
1859 "qemu-devel@nongnu.org\n", keycodes);
1860 }
84961407
CF
1861
1862 if (desc) {
1863 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
1864 }
1865 if (keycodes) {
1866 XFree(keycodes);
1867 }
3158a348
BR
1868 }
1869#endif
1870}
1871
881249c7 1872void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
a4ccabcf
AL
1873{
1874 GtkDisplayState *s = g_malloc0(sizeof(*s));
d819cdcc 1875 char *filename;
a4ccabcf
AL
1876
1877 gtk_init(NULL, NULL);
1878
a4ccabcf 1879 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
51572ab0
DB
1880#if GTK_CHECK_VERSION(3, 2, 0)
1881 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1882#else
a4ccabcf 1883 s->vbox = gtk_vbox_new(FALSE, 0);
51572ab0 1884#endif
a4ccabcf 1885 s->notebook = gtk_notebook_new();
a4ccabcf
AL
1886 s->menu_bar = gtk_menu_bar_new();
1887
c6158483 1888 s->free_scale = FALSE;
a4ccabcf 1889
834574ea
AL
1890 setlocale(LC_ALL, "");
1891 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1892 textdomain("qemu");
1893
a4ccabcf
AL
1894 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1895
1896 s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1897 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1898 qemu_add_vm_change_state_handler(gd_change_runstate, s);
1899
f7da9c17 1900 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
d819cdcc
SW
1901 if (filename) {
1902 GError *error = NULL;
1903 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1904 if (pixbuf) {
1905 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1906 } else {
1907 g_error_free(error);
1908 }
1909 g_free(filename);
1910 }
1911
a4ccabcf
AL
1912 gd_create_menus(s);
1913
1914 gd_connect_signals(s);
1915
a4ccabcf
AL
1916 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1917 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1918
a4ccabcf
AL
1919 gd_update_caption(s);
1920
1921 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1922 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1923
1924 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1925
1926 gtk_widget_show_all(s->window);
1927
6fa27697
GH
1928#ifdef VTE_RESIZE_HACK
1929 {
1930 VirtualConsole *cur = gd_vc_find_current(s);
b310a2a6
FZ
1931 if (cur) {
1932 int i;
1933
1934 for (i = 0; i < s->nb_vcs; i++) {
1935 VirtualConsole *vc = &s->vc[i];
1936 if (vc && vc->type == GD_VC_VTE && vc != cur) {
1937 gtk_widget_hide(vc->vte.terminal);
1938 }
6fa27697 1939 }
b310a2a6 1940 gd_update_windowsize(cur);
6fa27697 1941 }
6fa27697
GH
1942 }
1943#endif
1944
787ba4f0
PW
1945 if (full_screen) {
1946 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1947 }
881249c7
JK
1948 if (grab_on_hover) {
1949 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1950 }
787ba4f0 1951
3158a348 1952 gd_set_keycode_type(s);
a4ccabcf 1953}
ee5f31e4
GH
1954
1955void early_gtk_display_init(void)
1956{
1957#if defined(CONFIG_VTE)
1958 register_vc_handler(gd_vc_handler);
1959#endif
1960}