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