4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
12 * Portions from gtk-vnc:
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
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.
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.
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
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
38 # define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
41 #include "qemu-common.h"
43 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
44 /* Work around an -Wstrict-prototypes warning in GTK headers */
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
49 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
50 #pragma GCC diagnostic pop
54 #include <gdk/gdkkeysyms.h>
55 #include <glib/gi18n.h>
57 #if defined(CONFIG_VTE)
63 #include "ui/console.h"
65 #include "sysemu/sysemu.h"
66 #include "qmp-commands.h"
69 #include "sysemu/char.h"
70 #include "qom/object.h"
71 #ifdef GDK_WINDOWING_X11
73 #include <X11/XKBlib.h>
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
84 #if !defined(CONFIG_VTE)
85 # define VTE_CHECK_VERSION(a, b, c) 0
88 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
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.
94 * Workaround that by hiding all vte widgets, except the one in the
97 * Luckily everything works smooth in gtk3.
99 # define VTE_RESIZE_HACK 1
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
)
106 *ww
= gdk_window_get_width(w
);
107 *wh
= gdk_window_get_height(w
);
111 #if !GTK_CHECK_VERSION(2, 20, 0)
112 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
115 #ifndef GDK_IS_X11_DISPLAY
116 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
118 #ifndef GDK_IS_WIN32_DISPLAY
119 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
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
128 #define GDK_KEY_q GDK_q
129 #define GDK_KEY_plus GDK_plus
130 #define GDK_KEY_minus GDK_minus
133 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
135 static const int modifier_keycode
[] = {
136 /* shift, control, alt keys, meta keys, both left & right */
137 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
140 typedef struct GtkDisplayState GtkDisplayState
;
142 typedef struct VirtualGfxConsole
{
143 GtkWidget
*drawing_area
;
144 DisplayChangeListener dcl
;
146 pixman_image_t
*convert
;
147 cairo_surface_t
*surface
;
152 #if defined(CONFIG_VTE)
153 typedef struct VirtualVteConsole
{
155 GtkWidget
*scrollbar
;
157 CharDriverState
*chr
;
161 typedef enum VirtualConsoleType
{
164 } VirtualConsoleType
;
166 typedef struct VirtualConsole
{
170 GtkWidget
*menu_item
;
172 VirtualConsoleType type
;
174 VirtualGfxConsole gfx
;
175 #if defined(CONFIG_VTE)
176 VirtualVteConsole vte
;
181 struct GtkDisplayState
{
186 GtkAccelGroup
*accel_group
;
188 GtkWidget
*machine_menu_item
;
189 GtkWidget
*machine_menu
;
190 GtkWidget
*pause_item
;
191 GtkWidget
*reset_item
;
192 GtkWidget
*powerdown_item
;
193 GtkWidget
*quit_item
;
195 GtkWidget
*view_menu_item
;
196 GtkWidget
*view_menu
;
197 GtkWidget
*full_screen_item
;
198 GtkWidget
*zoom_in_item
;
199 GtkWidget
*zoom_out_item
;
200 GtkWidget
*zoom_fixed_item
;
201 GtkWidget
*zoom_fit_item
;
202 GtkWidget
*grab_item
;
203 GtkWidget
*grab_on_hover_item
;
206 VirtualConsole vc
[MAX_VCS
];
208 GtkWidget
*show_tabs_item
;
209 GtkWidget
*untabify_item
;
219 VirtualConsole
*kbd_owner
;
220 VirtualConsole
*ptr_owner
;
222 gboolean full_screen
;
224 GdkCursor
*null_cursor
;
225 Notifier mouse_mode_notifier
;
228 bool external_pause_update
;
230 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
234 static void gd_grab_pointer(VirtualConsole
*vc
);
235 static void gd_ungrab_pointer(GtkDisplayState
*s
);
237 /** Utility Functions **/
239 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
244 for (i
= 0; i
< s
->nb_vcs
; i
++) {
246 if (gtk_check_menu_item_get_active
247 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
254 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
259 for (i
= 0; i
< s
->nb_vcs
; i
++) {
261 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
269 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
273 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
274 return gd_vc_find_by_page(s
, page
);
277 static bool gd_is_grab_active(GtkDisplayState
*s
)
279 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
282 static bool gd_grab_on_hover(GtkDisplayState
*s
)
284 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
287 static void gd_update_cursor(VirtualConsole
*vc
)
289 GtkDisplayState
*s
= vc
->s
;
292 if (vc
->type
!= GD_VC_GFX
) {
296 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
297 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
298 gdk_window_set_cursor(window
, s
->null_cursor
);
300 gdk_window_set_cursor(window
, NULL
);
304 static void gd_update_caption(GtkDisplayState
*s
)
306 const char *status
= "";
309 const char *grab
= "";
310 bool is_paused
= !runstate_is_running();
314 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
316 prefix
= g_strdup_printf("QEMU");
319 if (s
->ptr_owner
!= NULL
&&
320 s
->ptr_owner
->window
== NULL
) {
321 grab
= _(" - Press Ctrl+Alt+G to release grab");
325 status
= _(" [Paused]");
327 s
->external_pause_update
= true;
328 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
330 s
->external_pause_update
= false;
332 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
333 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
336 for (i
= 0; i
< s
->nb_vcs
; i
++) {
337 VirtualConsole
*vc
= &s
->vc
[i
];
342 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
343 vc
== s
->kbd_owner
? " +kbd" : "",
344 vc
== s
->ptr_owner
? " +ptr" : "");
345 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
352 static void gd_update_geometry_hints(VirtualConsole
*vc
)
354 GtkDisplayState
*s
= vc
->s
;
355 GdkWindowHints mask
= 0;
356 GdkGeometry geo
= {};
357 GtkWidget
*geo_widget
= NULL
;
358 GtkWindow
*geo_window
;
360 if (vc
->type
== GD_VC_GFX
) {
362 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
363 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
364 mask
|= GDK_HINT_MIN_SIZE
;
366 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
367 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
368 mask
|= GDK_HINT_MIN_SIZE
;
370 geo_widget
= vc
->gfx
.drawing_area
;
371 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
373 #if defined(CONFIG_VTE)
374 } else if (vc
->type
== GD_VC_VTE
) {
375 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
378 geo
.width_inc
= vte_terminal_get_char_width(term
);
379 geo
.height_inc
= vte_terminal_get_char_height(term
);
380 mask
|= GDK_HINT_RESIZE_INC
;
381 geo
.base_width
= geo
.width_inc
;
382 geo
.base_height
= geo
.height_inc
;
383 mask
|= GDK_HINT_BASE_SIZE
;
384 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
385 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
386 mask
|= GDK_HINT_MIN_SIZE
;
387 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
388 geo
.base_width
+= ib
->left
+ ib
->right
;
389 geo
.base_height
+= ib
->top
+ ib
->bottom
;
390 geo
.min_width
+= ib
->left
+ ib
->right
;
391 geo
.min_height
+= ib
->top
+ ib
->bottom
;
392 geo_widget
= vc
->vte
.terminal
;
396 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
397 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
400 static void gd_update_windowsize(VirtualConsole
*vc
)
402 GtkDisplayState
*s
= vc
->s
;
404 gd_update_geometry_hints(vc
);
406 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
407 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
408 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
412 static void gd_update_full_redraw(VirtualConsole
*vc
)
414 GtkWidget
*area
= vc
->gfx
.drawing_area
;
416 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
417 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
420 static void gtk_release_modifiers(GtkDisplayState
*s
)
422 VirtualConsole
*vc
= gd_vc_find_current(s
);
425 if (vc
->type
!= GD_VC_GFX
) {
428 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
429 keycode
= modifier_keycode
[i
];
430 if (!s
->modifier_pressed
[i
]) {
433 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, keycode
, false);
434 s
->modifier_pressed
[i
] = false;
438 /** DisplayState Callbacks **/
440 static void gd_update(DisplayChangeListener
*dcl
,
441 int x
, int y
, int w
, int h
)
443 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
449 trace_gd_update(vc
->label
, x
, y
, w
, h
);
451 if (vc
->gfx
.convert
) {
452 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
453 NULL
, vc
->gfx
.convert
,
454 x
, y
, 0, 0, x
, y
, w
, h
);
457 x1
= floor(x
* vc
->gfx
.scale_x
);
458 y1
= floor(y
* vc
->gfx
.scale_y
);
460 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
461 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
463 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
464 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
466 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
477 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
478 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
481 static void gd_refresh(DisplayChangeListener
*dcl
)
483 graphic_hw_update(dcl
->con
);
486 #if GTK_CHECK_VERSION(3, 0, 0)
487 static void gd_mouse_set(DisplayChangeListener
*dcl
,
488 int x
, int y
, int visible
)
490 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
492 GdkDeviceManager
*mgr
;
495 if (qemu_input_is_absolute()) {
499 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
500 mgr
= gdk_display_get_device_manager(dpy
);
501 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
502 x
, y
, &x_root
, &y_root
);
503 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
504 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
508 static void gd_mouse_set(DisplayChangeListener
*dcl
,
509 int x
, int y
, int visible
)
511 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
514 if (qemu_input_is_absolute()) {
518 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
519 x
, y
, &x_root
, &y_root
);
520 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
521 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
526 static void gd_cursor_define(DisplayChangeListener
*dcl
,
529 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
533 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
534 GDK_COLORSPACE_RGB
, true, 8,
535 c
->width
, c
->height
, c
->width
* 4,
537 cursor
= gdk_cursor_new_from_pixbuf
538 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
539 pixbuf
, c
->hot_x
, c
->hot_y
);
540 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
541 g_object_unref(pixbuf
);
542 #if !GTK_CHECK_VERSION(3, 0, 0)
543 gdk_cursor_unref(cursor
);
545 g_object_unref(cursor
);
549 static void gd_switch(DisplayChangeListener
*dcl
,
550 DisplaySurface
*surface
)
552 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
555 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
557 if (vc
->gfx
.surface
) {
558 cairo_surface_destroy(vc
->gfx
.surface
);
562 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
563 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
566 vc
->gfx
.ds
= surface
;
568 if (vc
->gfx
.convert
) {
569 pixman_image_unref(vc
->gfx
.convert
);
570 vc
->gfx
.convert
= NULL
;
573 if (surface
->format
== PIXMAN_x8r8g8b8
) {
575 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
577 * No need to convert, use surface directly. Should be the
578 * common case as this is qemu_default_pixelformat(32) too.
580 vc
->gfx
.surface
= cairo_image_surface_create_for_data
581 (surface_data(surface
),
583 surface_width(surface
),
584 surface_height(surface
),
585 surface_stride(surface
));
587 /* Must convert surface, use pixman to do it. */
588 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
589 surface_width(surface
),
590 surface_height(surface
),
592 vc
->gfx
.surface
= cairo_image_surface_create_for_data
593 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
595 pixman_image_get_width(vc
->gfx
.convert
),
596 pixman_image_get_height(vc
->gfx
.convert
),
597 pixman_image_get_stride(vc
->gfx
.convert
));
598 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
599 NULL
, vc
->gfx
.convert
,
601 pixman_image_get_width(vc
->gfx
.convert
),
602 pixman_image_get_height(vc
->gfx
.convert
));
606 gd_update_windowsize(vc
);
608 gd_update_full_redraw(vc
);
614 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
616 GtkDisplayState
*s
= opaque
;
618 gd_update_caption(s
);
621 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
626 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
627 /* release the grab at switching to absolute mode */
628 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
629 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
632 for (i
= 0; i
< s
->nb_vcs
; i
++) {
633 VirtualConsole
*vc
= &s
->vc
[i
];
634 gd_update_cursor(vc
);
640 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
643 GtkDisplayState
*s
= opaque
;
647 for (i
= 0; i
< s
->nb_vcs
; i
++) {
648 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
651 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
660 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
662 VirtualConsole
*vc
= opaque
;
663 GtkDisplayState
*s
= vc
->s
;
668 if (!gtk_widget_get_realized(widget
)) {
672 fbw
= surface_width(vc
->gfx
.ds
);
673 fbh
= surface_height(vc
->gfx
.ds
);
675 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
677 if (s
->full_screen
) {
678 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
679 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
680 } else if (s
->free_scale
) {
683 sx
= (double)ww
/ fbw
;
684 sy
= (double)wh
/ fbh
;
686 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
689 fbw
*= vc
->gfx
.scale_x
;
690 fbh
*= vc
->gfx
.scale_y
;
700 cairo_rectangle(cr
, 0, 0, ww
, wh
);
702 /* Optionally cut out the inner area where the pixmap
703 will be drawn. This avoids 'flashing' since we're
704 not double-buffering. Note we're using the undocumented
705 behaviour of drawing the rectangle from right to left
706 to cut out the whole */
707 cairo_rectangle(cr
, mx
+ fbw
, my
,
711 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
712 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
713 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
719 #if !GTK_CHECK_VERSION(3, 0, 0)
720 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
726 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
731 expose
->area
.height
);
734 ret
= gd_draw_event(widget
, cr
, opaque
);
742 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
745 VirtualConsole
*vc
= opaque
;
746 GtkDisplayState
*s
= vc
->s
;
752 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
753 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
755 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
766 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
767 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
769 if (qemu_input_is_absolute()) {
770 if (x
< 0 || y
< 0 ||
771 x
>= surface_width(vc
->gfx
.ds
) ||
772 y
>= surface_height(vc
->gfx
.ds
)) {
775 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
776 surface_width(vc
->gfx
.ds
));
777 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
778 surface_height(vc
->gfx
.ds
));
779 qemu_input_event_sync();
780 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
781 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
782 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
783 qemu_input_event_sync();
789 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
790 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
791 int x
= (int)motion
->x_root
;
792 int y
= (int)motion
->y_root
;
794 /* In relative mode check to see if client pointer hit
795 * one of the screen edges, and if so move it back by
796 * 200 pixels. This is important because the pointer
797 * in the server doesn't correspond 1-for-1, and so
798 * may still be only half way across the screen. Without
799 * this warp, the server pointer would thus appear to hit
800 * an invisible wall */
807 if (x
== (gdk_screen_get_width(screen
) - 1)) {
810 if (y
== (gdk_screen_get_height(screen
) - 1)) {
814 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
815 #if GTK_CHECK_VERSION(3, 0, 0)
816 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
817 gdk_device_warp(dev
, screen
, x
, y
);
819 GdkDisplay
*display
= gtk_widget_get_display(widget
);
820 gdk_display_warp_pointer(display
, screen
, x
, y
);
829 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
832 VirtualConsole
*vc
= opaque
;
833 GtkDisplayState
*s
= vc
->s
;
836 /* implicitly grab the input at the first click in the relative mode */
837 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
838 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
839 gd_ungrab_pointer(s
);
841 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
845 gd_update_caption(s
);
850 if (button
->button
== 1) {
851 btn
= INPUT_BUTTON_LEFT
;
852 } else if (button
->button
== 2) {
853 btn
= INPUT_BUTTON_MIDDLE
;
854 } else if (button
->button
== 3) {
855 btn
= INPUT_BUTTON_RIGHT
;
860 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
861 button
->type
== GDK_BUTTON_PRESS
);
862 qemu_input_event_sync();
866 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
869 VirtualConsole
*vc
= opaque
;
872 if (scroll
->direction
== GDK_SCROLL_UP
) {
873 btn
= INPUT_BUTTON_WHEEL_UP
;
874 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
875 btn
= INPUT_BUTTON_WHEEL_DOWN
;
880 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
881 qemu_input_event_sync();
882 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
883 qemu_input_event_sync();
887 static int gd_map_keycode(GtkDisplayState
*s
, GdkDisplay
*dpy
, int gdk_keycode
)
891 #ifdef GDK_WINDOWING_WIN32
892 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
893 qemu_keycode
= MapVirtualKey(gdk_keycode
, MAPVK_VK_TO_VSC
);
894 switch (qemu_keycode
) {
895 case 103: /* alt gr */
896 qemu_keycode
= 56 | SCANCODE_GREY
;
903 if (gdk_keycode
< 9) {
905 } else if (gdk_keycode
< 97) {
906 qemu_keycode
= gdk_keycode
- 8;
907 #ifdef GDK_WINDOWING_X11
908 } else if (GDK_IS_X11_DISPLAY(dpy
) && gdk_keycode
< 158) {
910 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
912 qemu_keycode
= translate_xfree86_keycode(gdk_keycode
- 97);
915 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
917 } else if (gdk_keycode
== 211) { /* backslash */
926 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
928 VirtualConsole
*vc
= opaque
;
929 GtkDisplayState
*s
= vc
->s
;
930 int gdk_keycode
= key
->hardware_keycode
;
934 qemu_keycode
= gd_map_keycode(s
, gtk_widget_get_display(widget
),
937 trace_gd_key_event(vc
->label
, gdk_keycode
, qemu_keycode
,
938 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
940 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
941 if (qemu_keycode
== modifier_keycode
[i
]) {
942 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
946 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, qemu_keycode
,
947 key
->type
== GDK_KEY_PRESS
);
952 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
954 if (event
->type
== GDK_MOTION_NOTIFY
) {
955 return gd_motion_event(widget
, &event
->motion
, opaque
);
960 /** Window Menu Actions **/
962 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
964 GtkDisplayState
*s
= opaque
;
966 if (s
->external_pause_update
) {
969 if (runstate_is_running()) {
976 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
978 qmp_system_reset(NULL
);
981 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
983 qmp_system_powerdown(NULL
);
986 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
991 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
993 GtkDisplayState
*s
= opaque
;
994 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
995 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
999 gtk_release_modifiers(s
);
1001 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1002 gtk_notebook_set_current_page(nb
, page
);
1003 child
= gtk_notebook_get_nth_page(nb
, page
);
1004 gtk_widget_grab_focus(child
);
1008 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1010 GtkDisplayState
*s
= opaque
;
1011 VirtualConsole
*vc
= gd_vc_find_current(s
);
1013 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1014 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1016 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1018 gd_update_windowsize(vc
);
1021 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1024 VirtualConsole
*vc
= opaque
;
1025 GtkDisplayState
*s
= vc
->s
;
1027 gtk_widget_set_sensitive(vc
->menu_item
, true);
1028 gtk_widget_reparent(vc
->tab_item
, s
->notebook
);
1029 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1030 vc
->tab_item
, vc
->label
);
1031 gtk_widget_destroy(vc
->window
);
1036 static gboolean
gd_win_grab(void *opaque
)
1038 VirtualConsole
*vc
= opaque
;
1040 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1041 if (vc
->s
->ptr_owner
) {
1042 gd_ungrab_pointer(vc
->s
);
1044 gd_grab_pointer(vc
);
1046 gd_update_caption(vc
->s
);
1050 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1052 GtkDisplayState
*s
= opaque
;
1053 VirtualConsole
*vc
= gd_vc_find_current(s
);
1055 if (vc
->type
== GD_VC_GFX
) {
1056 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1060 gtk_widget_set_sensitive(vc
->menu_item
, false);
1061 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1062 gtk_widget_reparent(vc
->tab_item
, vc
->window
);
1064 g_signal_connect(vc
->window
, "delete-event",
1065 G_CALLBACK(gd_tab_window_close
), vc
);
1066 gtk_widget_show_all(vc
->window
);
1068 GtkAccelGroup
*ag
= gtk_accel_group_new();
1069 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1071 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
), vc
, NULL
);
1072 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1074 gd_update_geometry_hints(vc
);
1075 gd_update_caption(s
);
1079 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1081 GtkDisplayState
*s
= opaque
;
1082 VirtualConsole
*vc
= gd_vc_find_current(s
);
1084 if (!s
->full_screen
) {
1085 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1086 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
1087 if (vc
->type
== GD_VC_GFX
) {
1088 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1089 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1092 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1093 s
->full_screen
= TRUE
;
1095 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1096 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1097 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
1098 s
->full_screen
= FALSE
;
1099 if (vc
->type
== GD_VC_GFX
) {
1100 vc
->gfx
.scale_x
= 1.0;
1101 vc
->gfx
.scale_y
= 1.0;
1102 gd_update_windowsize(vc
);
1103 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1108 gd_update_cursor(vc
);
1111 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1113 GtkDisplayState
*s
= opaque
;
1114 VirtualConsole
*vc
= gd_vc_find_current(s
);
1116 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1119 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1120 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1122 gd_update_windowsize(vc
);
1125 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1127 GtkDisplayState
*s
= opaque
;
1128 VirtualConsole
*vc
= gd_vc_find_current(s
);
1130 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1133 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1134 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1136 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1137 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1139 gd_update_windowsize(vc
);
1142 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1144 GtkDisplayState
*s
= opaque
;
1145 VirtualConsole
*vc
= gd_vc_find_current(s
);
1147 vc
->gfx
.scale_x
= 1.0;
1148 vc
->gfx
.scale_y
= 1.0;
1150 gd_update_windowsize(vc
);
1153 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1155 GtkDisplayState
*s
= opaque
;
1156 VirtualConsole
*vc
= gd_vc_find_current(s
);
1158 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1159 s
->free_scale
= TRUE
;
1161 s
->free_scale
= FALSE
;
1162 vc
->gfx
.scale_x
= 1.0;
1163 vc
->gfx
.scale_y
= 1.0;
1166 gd_update_windowsize(vc
);
1167 gd_update_full_redraw(vc
);
1170 #if GTK_CHECK_VERSION(3, 0, 0)
1171 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1172 GdkInputSource source
, GdkEventMask mask
,
1175 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1176 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1177 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1180 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1181 GdkDevice
*dev
= tmp
->data
;
1182 if (gdk_device_get_source(dev
) != source
) {
1186 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1187 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1188 mask
, cursor
, GDK_CURRENT_TIME
);
1190 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1197 static void gd_grab_keyboard(VirtualConsole
*vc
)
1199 #if GTK_CHECK_VERSION(3, 0, 0)
1200 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1201 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1204 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1208 vc
->s
->kbd_owner
= vc
;
1209 trace_gd_grab(vc
->label
, "kbd", true);
1212 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1214 VirtualConsole
*vc
= s
->kbd_owner
;
1219 s
->kbd_owner
= NULL
;
1221 #if GTK_CHECK_VERSION(3, 0, 0)
1222 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1224 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1226 trace_gd_grab(vc
->label
, "kbd", false);
1229 static void gd_grab_pointer(VirtualConsole
*vc
)
1231 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1232 #if GTK_CHECK_VERSION(3, 0, 0)
1233 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1234 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1235 GDK_POINTER_MOTION_MASK
|
1236 GDK_BUTTON_PRESS_MASK
|
1237 GDK_BUTTON_RELEASE_MASK
|
1238 GDK_BUTTON_MOTION_MASK
|
1240 vc
->s
->null_cursor
);
1241 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr
),
1242 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1244 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1245 FALSE
, /* All events to come to our window directly */
1246 GDK_POINTER_MOTION_MASK
|
1247 GDK_BUTTON_PRESS_MASK
|
1248 GDK_BUTTON_RELEASE_MASK
|
1249 GDK_BUTTON_MOTION_MASK
|
1251 NULL
, /* Allow cursor to move over entire desktop */
1254 gdk_display_get_pointer(display
, NULL
,
1255 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1257 vc
->s
->ptr_owner
= vc
;
1258 trace_gd_grab(vc
->label
, "ptr", true);
1261 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1263 VirtualConsole
*vc
= s
->ptr_owner
;
1268 s
->ptr_owner
= NULL
;
1270 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1271 #if GTK_CHECK_VERSION(3, 0, 0)
1272 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1273 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1274 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
1275 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1276 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1278 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1279 gdk_display_warp_pointer(display
,
1280 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1281 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1283 trace_gd_grab(vc
->label
, "ptr", false);
1286 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1288 GtkDisplayState
*s
= opaque
;
1289 VirtualConsole
*vc
= gd_vc_find_current(s
);
1291 if (gd_is_grab_active(s
)) {
1292 if (!gd_grab_on_hover(s
)) {
1293 gd_grab_keyboard(vc
);
1295 gd_grab_pointer(vc
);
1297 gd_ungrab_keyboard(s
);
1298 gd_ungrab_pointer(s
);
1301 gd_update_caption(s
);
1302 gd_update_cursor(vc
);
1305 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1308 GtkDisplayState
*s
= data
;
1312 if (!gtk_widget_get_realized(s
->notebook
)) {
1316 #ifdef VTE_RESIZE_HACK
1317 vc
= gd_vc_find_current(s
);
1318 if (vc
&& vc
->type
== GD_VC_VTE
) {
1319 gtk_widget_hide(vc
->vte
.terminal
);
1322 vc
= gd_vc_find_by_page(s
, arg2
);
1326 #ifdef VTE_RESIZE_HACK
1327 if (vc
->type
== GD_VC_VTE
) {
1328 gtk_widget_show(vc
->vte
.terminal
);
1331 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1333 on_vga
= (vc
->type
== GD_VC_GFX
);
1335 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1337 } else if (s
->full_screen
) {
1338 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1341 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1343 gd_update_windowsize(vc
);
1344 gd_update_cursor(vc
);
1347 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1350 VirtualConsole
*vc
= opaque
;
1351 GtkDisplayState
*s
= vc
->s
;
1353 if (gd_grab_on_hover(s
)) {
1354 gd_ungrab_keyboard(s
);
1355 gd_grab_keyboard(vc
);
1356 gd_update_caption(s
);
1361 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1364 VirtualConsole
*vc
= opaque
;
1365 GtkDisplayState
*s
= vc
->s
;
1367 if (gd_grab_on_hover(s
)) {
1368 gd_ungrab_keyboard(s
);
1369 gd_update_caption(s
);
1374 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1375 GdkEventCrossing
*crossing
, gpointer opaque
)
1377 VirtualConsole
*vc
= opaque
;
1378 GtkDisplayState
*s
= vc
->s
;
1380 gtk_release_modifiers(s
);
1384 /** Virtual Console Callbacks **/
1386 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1387 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1391 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", idx
);
1393 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1394 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1395 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
1396 gtk_accel_map_add_entry(path
, GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1398 g_signal_connect(vc
->menu_item
, "activate",
1399 G_CALLBACK(gd_menu_switch_vc
), s
);
1400 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1405 #if defined(CONFIG_VTE)
1406 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1408 VirtualConsole
*vc
= opaque
;
1410 if (gtk_adjustment_get_upper(adjustment
) >
1411 gtk_adjustment_get_page_size(adjustment
)) {
1412 gtk_widget_show(vc
->vte
.scrollbar
);
1414 gtk_widget_hide(vc
->vte
.scrollbar
);
1418 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1420 VirtualConsole
*vc
= chr
->opaque
;
1422 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1427 static CharDriverState
*vcs
[MAX_VCS
];
1429 static CharDriverState
*gd_vc_handler(ChardevVC
*unused
)
1431 CharDriverState
*chr
;
1433 chr
= g_malloc0(sizeof(*chr
));
1434 chr
->chr_write
= gd_vc_chr_write
;
1435 /* defer OPENED events until our vc is fully initialized */
1436 chr
->explicit_be_open
= true;
1438 vcs
[nb_vcs
++] = chr
;
1443 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1446 VirtualConsole
*vc
= user_data
;
1448 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1452 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1453 CharDriverState
*chr
, int idx
,
1454 GSList
*group
, GtkWidget
*view_menu
)
1458 GtkWidget
*scrollbar
;
1459 GtkAdjustment
*vadjustment
;
1464 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1465 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1466 ? vc
->vte
.chr
->label
: buffer
);
1467 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1469 vc
->vte
.terminal
= vte_terminal_new();
1470 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1472 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1473 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1474 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1476 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1477 vadjustment
= gtk_scrollable_get_vadjustment
1478 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1480 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1483 #if GTK_CHECK_VERSION(3, 0, 0)
1484 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1485 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1487 box
= gtk_hbox_new(false, 2);
1488 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1491 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1492 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1494 vc
->vte
.chr
->opaque
= vc
;
1496 vc
->vte
.scrollbar
= scrollbar
;
1498 g_signal_connect(vadjustment
, "changed",
1499 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1501 vc
->type
= GD_VC_VTE
;
1503 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1504 gtk_label_new(vc
->label
));
1506 qemu_chr_be_generic_open(vc
->vte
.chr
);
1507 if (vc
->vte
.chr
->init
) {
1508 vc
->vte
.chr
->init(vc
->vte
.chr
);
1514 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1515 GtkWidget
*view_menu
)
1519 for (i
= 0; i
< nb_vcs
; i
++) {
1520 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1521 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1525 #endif /* CONFIG_VTE */
1527 /** Window Creation **/
1529 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1531 #if GTK_CHECK_VERSION(3, 0, 0)
1532 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1533 G_CALLBACK(gd_draw_event
), vc
);
1535 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1536 G_CALLBACK(gd_expose_event
), vc
);
1538 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1539 G_CALLBACK(gd_event
), vc
);
1540 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1541 G_CALLBACK(gd_button_event
), vc
);
1542 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1543 G_CALLBACK(gd_button_event
), vc
);
1544 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1545 G_CALLBACK(gd_scroll_event
), vc
);
1546 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1547 G_CALLBACK(gd_key_event
), vc
);
1548 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1549 G_CALLBACK(gd_key_event
), vc
);
1551 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1552 G_CALLBACK(gd_enter_event
), vc
);
1553 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1554 G_CALLBACK(gd_leave_event
), vc
);
1555 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1556 G_CALLBACK(gd_focus_out_event
), vc
);
1559 static void gd_connect_signals(GtkDisplayState
*s
)
1561 g_signal_connect(s
->show_tabs_item
, "activate",
1562 G_CALLBACK(gd_menu_show_tabs
), s
);
1563 g_signal_connect(s
->untabify_item
, "activate",
1564 G_CALLBACK(gd_menu_untabify
), s
);
1566 g_signal_connect(s
->window
, "delete-event",
1567 G_CALLBACK(gd_window_close
), s
);
1569 g_signal_connect(s
->pause_item
, "activate",
1570 G_CALLBACK(gd_menu_pause
), s
);
1571 g_signal_connect(s
->reset_item
, "activate",
1572 G_CALLBACK(gd_menu_reset
), s
);
1573 g_signal_connect(s
->powerdown_item
, "activate",
1574 G_CALLBACK(gd_menu_powerdown
), s
);
1575 g_signal_connect(s
->quit_item
, "activate",
1576 G_CALLBACK(gd_menu_quit
), s
);
1577 g_signal_connect(s
->full_screen_item
, "activate",
1578 G_CALLBACK(gd_menu_full_screen
), s
);
1579 g_signal_connect(s
->zoom_in_item
, "activate",
1580 G_CALLBACK(gd_menu_zoom_in
), s
);
1581 g_signal_connect(s
->zoom_out_item
, "activate",
1582 G_CALLBACK(gd_menu_zoom_out
), s
);
1583 g_signal_connect(s
->zoom_fixed_item
, "activate",
1584 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1585 g_signal_connect(s
->zoom_fit_item
, "activate",
1586 G_CALLBACK(gd_menu_zoom_fit
), s
);
1587 g_signal_connect(s
->grab_item
, "activate",
1588 G_CALLBACK(gd_menu_grab_input
), s
);
1589 g_signal_connect(s
->notebook
, "switch-page",
1590 G_CALLBACK(gd_change_page
), s
);
1593 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
, GtkAccelGroup
*accel_group
)
1595 GtkWidget
*machine_menu
;
1596 GtkWidget
*separator
;
1598 machine_menu
= gtk_menu_new();
1599 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), accel_group
);
1601 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1602 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1604 separator
= gtk_separator_menu_item_new();
1605 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1607 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1608 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1610 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1611 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1613 separator
= gtk_separator_menu_item_new();
1614 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1616 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1617 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1618 "<QEMU>/Machine/Quit");
1619 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1620 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1621 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1623 return machine_menu
;
1626 static const DisplayChangeListenerOps dcl_ops
= {
1628 .dpy_gfx_update
= gd_update
,
1629 .dpy_gfx_switch
= gd_switch
,
1630 .dpy_refresh
= gd_refresh
,
1631 .dpy_mouse_set
= gd_mouse_set
,
1632 .dpy_cursor_define
= gd_cursor_define
,
1635 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1636 QemuConsole
*con
, int idx
,
1637 GSList
*group
, GtkWidget
*view_menu
)
1639 Error
*local_err
= NULL
;
1642 obj
= object_property_get_link(OBJECT(con
), "device", &local_err
);
1644 vc
->label
= g_strdup_printf("%s", object_get_typename(obj
));
1646 vc
->label
= g_strdup_printf("VGA");
1650 vc
->gfx
.scale_x
= 1.0;
1651 vc
->gfx
.scale_y
= 1.0;
1653 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1654 gtk_widget_add_events(vc
->gfx
.drawing_area
,
1655 GDK_POINTER_MOTION_MASK
|
1656 GDK_BUTTON_PRESS_MASK
|
1657 GDK_BUTTON_RELEASE_MASK
|
1658 GDK_BUTTON_MOTION_MASK
|
1659 GDK_ENTER_NOTIFY_MASK
|
1660 GDK_LEAVE_NOTIFY_MASK
|
1662 GDK_KEY_PRESS_MASK
);
1663 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
1664 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
1666 vc
->type
= GD_VC_GFX
;
1667 vc
->tab_item
= vc
->gfx
.drawing_area
;
1668 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
1669 vc
->tab_item
, gtk_label_new(vc
->label
));
1670 gd_connect_vc_gfx_signals(vc
);
1672 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1674 vc
->gfx
.dcl
.ops
= &dcl_ops
;
1675 vc
->gfx
.dcl
.con
= con
;
1676 register_displaychangelistener(&vc
->gfx
.dcl
);
1681 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
, GtkAccelGroup
*accel_group
)
1683 GSList
*group
= NULL
;
1684 GtkWidget
*view_menu
;
1685 GtkWidget
*separator
;
1689 view_menu
= gtk_menu_new();
1690 gtk_menu_set_accel_group(GTK_MENU(view_menu
), accel_group
);
1692 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1693 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1694 "<QEMU>/View/Full Screen");
1695 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
,
1697 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
1699 separator
= gtk_separator_menu_item_new();
1700 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1702 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1703 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1704 "<QEMU>/View/Zoom In");
1705 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
1707 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
1709 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1710 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1711 "<QEMU>/View/Zoom Out");
1712 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
1714 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
1716 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1717 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1718 "<QEMU>/View/Zoom Fixed");
1719 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
1721 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
1723 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1724 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
1726 separator
= gtk_separator_menu_item_new();
1727 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1729 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1730 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
1732 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1733 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1734 "<QEMU>/View/Grab Input");
1735 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
1737 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
1739 separator
= gtk_separator_menu_item_new();
1740 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1743 for (vc
= 0;; vc
++) {
1744 con
= qemu_console_lookup_by_index(vc
);
1745 if (!con
|| !qemu_console_is_graphic(con
)) {
1748 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
1749 vc
, group
, view_menu
);
1753 #if defined(CONFIG_VTE)
1755 gd_vcs_init(s
, group
, view_menu
);
1758 separator
= gtk_separator_menu_item_new();
1759 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1761 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1762 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
1764 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1765 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
1770 static void gd_create_menus(GtkDisplayState
*s
)
1772 GtkAccelGroup
*accel_group
;
1774 accel_group
= gtk_accel_group_new();
1775 s
->machine_menu
= gd_create_menu_machine(s
, accel_group
);
1776 s
->view_menu
= gd_create_menu_view(s
, accel_group
);
1778 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1779 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1781 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1783 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1784 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1785 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1787 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1788 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1789 s
->accel_group
= accel_group
;
1792 static void gd_set_keycode_type(GtkDisplayState
*s
)
1794 #ifdef GDK_WINDOWING_X11
1795 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
1796 if (GDK_IS_X11_DISPLAY(display
)) {
1797 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
1798 XkbDescPtr desc
= XkbGetKeyboard(x11_display
, XkbGBN_AllComponentsMask
,
1800 char *keycodes
= NULL
;
1802 if (desc
&& desc
->names
) {
1803 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
1805 if (keycodes
== NULL
) {
1806 fprintf(stderr
, "could not lookup keycode name\n");
1807 } else if (strstart(keycodes
, "evdev", NULL
)) {
1808 s
->has_evdev
= true;
1809 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
1810 fprintf(stderr
, "unknown keycodes `%s', please report to "
1811 "qemu-devel@nongnu.org\n", keycodes
);
1817 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
1819 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1822 gtk_init(NULL
, NULL
);
1824 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1825 #if GTK_CHECK_VERSION(3, 2, 0)
1826 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1828 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1830 s
->notebook
= gtk_notebook_new();
1831 s
->menu_bar
= gtk_menu_bar_new();
1833 s
->free_scale
= FALSE
;
1835 setlocale(LC_ALL
, "");
1836 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1839 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1841 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1842 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1843 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1845 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
1847 GError
*error
= NULL
;
1848 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
1850 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
1852 g_error_free(error
);
1859 gd_connect_signals(s
);
1861 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1862 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1864 gd_update_caption(s
);
1866 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1867 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1869 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1871 gtk_widget_show_all(s
->window
);
1873 #ifdef VTE_RESIZE_HACK
1875 VirtualConsole
*cur
= gd_vc_find_current(s
);
1878 for (i
= 0; i
< s
->nb_vcs
; i
++) {
1879 VirtualConsole
*vc
= &s
->vc
[i
];
1880 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
1881 gtk_widget_hide(vc
->vte
.terminal
);
1884 gd_update_windowsize(cur
);
1889 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1891 if (grab_on_hover
) {
1892 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
1895 gd_set_keycode_type(s
);
1898 void early_gtk_display_init(void)
1900 #if defined(CONFIG_VTE)
1901 register_vc_handler(gd_vc_handler
);