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