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