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