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