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
);
997 gtk_release_modifiers(s
);
999 page
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
),
1001 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), page
);
1005 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1007 GtkDisplayState
*s
= opaque
;
1008 VirtualConsole
*vc
= gd_vc_find_current(s
);
1010 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1011 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1013 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1015 gd_update_windowsize(vc
);
1018 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1021 VirtualConsole
*vc
= opaque
;
1022 GtkDisplayState
*s
= vc
->s
;
1024 gtk_widget_set_sensitive(vc
->menu_item
, true);
1025 gtk_widget_reparent(vc
->tab_item
, s
->notebook
);
1026 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1027 vc
->tab_item
, vc
->label
);
1028 gtk_widget_destroy(vc
->window
);
1033 static gboolean
gd_win_grab(void *opaque
)
1035 VirtualConsole
*vc
= opaque
;
1037 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1038 if (vc
->s
->ptr_owner
) {
1039 gd_ungrab_pointer(vc
->s
);
1041 gd_grab_pointer(vc
);
1043 gd_update_caption(vc
->s
);
1047 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1049 GtkDisplayState
*s
= opaque
;
1050 VirtualConsole
*vc
= gd_vc_find_current(s
);
1052 if (vc
->type
== GD_VC_GFX
) {
1053 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1057 gtk_widget_set_sensitive(vc
->menu_item
, false);
1058 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1059 gtk_widget_reparent(vc
->tab_item
, vc
->window
);
1061 g_signal_connect(vc
->window
, "delete-event",
1062 G_CALLBACK(gd_tab_window_close
), vc
);
1063 gtk_widget_show_all(vc
->window
);
1065 GtkAccelGroup
*ag
= gtk_accel_group_new();
1066 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1068 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
), vc
, NULL
);
1069 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1071 gd_update_geometry_hints(vc
);
1072 gd_update_caption(s
);
1076 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1078 GtkDisplayState
*s
= opaque
;
1079 VirtualConsole
*vc
= gd_vc_find_current(s
);
1081 if (!s
->full_screen
) {
1082 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1083 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
1084 if (vc
->type
== GD_VC_GFX
) {
1085 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1086 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1089 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1090 s
->full_screen
= TRUE
;
1092 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1093 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1094 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
1095 s
->full_screen
= FALSE
;
1096 if (vc
->type
== GD_VC_GFX
) {
1097 vc
->gfx
.scale_x
= 1.0;
1098 vc
->gfx
.scale_y
= 1.0;
1099 gd_update_windowsize(vc
);
1100 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1105 gd_update_cursor(vc
);
1108 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1110 GtkDisplayState
*s
= opaque
;
1111 VirtualConsole
*vc
= gd_vc_find_current(s
);
1113 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1116 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1117 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1119 gd_update_windowsize(vc
);
1122 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1124 GtkDisplayState
*s
= opaque
;
1125 VirtualConsole
*vc
= gd_vc_find_current(s
);
1127 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1130 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1131 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1133 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1134 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1136 gd_update_windowsize(vc
);
1139 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1141 GtkDisplayState
*s
= opaque
;
1142 VirtualConsole
*vc
= gd_vc_find_current(s
);
1144 vc
->gfx
.scale_x
= 1.0;
1145 vc
->gfx
.scale_y
= 1.0;
1147 gd_update_windowsize(vc
);
1150 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1152 GtkDisplayState
*s
= opaque
;
1153 VirtualConsole
*vc
= gd_vc_find_current(s
);
1155 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1156 s
->free_scale
= TRUE
;
1158 s
->free_scale
= FALSE
;
1159 vc
->gfx
.scale_x
= 1.0;
1160 vc
->gfx
.scale_y
= 1.0;
1163 gd_update_windowsize(vc
);
1164 gd_update_full_redraw(vc
);
1167 #if GTK_CHECK_VERSION(3, 0, 0)
1168 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1169 GdkInputSource source
, GdkEventMask mask
,
1172 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1173 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1174 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1177 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1178 GdkDevice
*dev
= tmp
->data
;
1179 if (gdk_device_get_source(dev
) != source
) {
1183 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1184 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1185 mask
, cursor
, GDK_CURRENT_TIME
);
1187 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1194 static void gd_grab_keyboard(VirtualConsole
*vc
)
1196 #if GTK_CHECK_VERSION(3, 0, 0)
1197 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1198 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1201 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1205 vc
->s
->kbd_owner
= vc
;
1206 trace_gd_grab(vc
->label
, "kbd", true);
1209 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1211 VirtualConsole
*vc
= s
->kbd_owner
;
1216 s
->kbd_owner
= NULL
;
1218 #if GTK_CHECK_VERSION(3, 0, 0)
1219 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1221 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1223 trace_gd_grab(vc
->label
, "kbd", false);
1226 static void gd_grab_pointer(VirtualConsole
*vc
)
1228 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1229 #if GTK_CHECK_VERSION(3, 0, 0)
1230 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1231 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1232 GDK_POINTER_MOTION_MASK
|
1233 GDK_BUTTON_PRESS_MASK
|
1234 GDK_BUTTON_RELEASE_MASK
|
1235 GDK_BUTTON_MOTION_MASK
|
1237 vc
->s
->null_cursor
);
1238 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr
),
1239 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1241 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1242 FALSE
, /* All events to come to our window directly */
1243 GDK_POINTER_MOTION_MASK
|
1244 GDK_BUTTON_PRESS_MASK
|
1245 GDK_BUTTON_RELEASE_MASK
|
1246 GDK_BUTTON_MOTION_MASK
|
1248 NULL
, /* Allow cursor to move over entire desktop */
1251 gdk_display_get_pointer(display
, NULL
,
1252 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1254 vc
->s
->ptr_owner
= vc
;
1255 trace_gd_grab(vc
->label
, "ptr", true);
1258 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1260 VirtualConsole
*vc
= s
->ptr_owner
;
1265 s
->ptr_owner
= NULL
;
1267 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1268 #if GTK_CHECK_VERSION(3, 0, 0)
1269 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1270 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1271 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
1272 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1273 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1275 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1276 gdk_display_warp_pointer(display
,
1277 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1278 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1280 trace_gd_grab(vc
->label
, "ptr", false);
1283 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1285 GtkDisplayState
*s
= opaque
;
1286 VirtualConsole
*vc
= gd_vc_find_current(s
);
1288 if (gd_is_grab_active(s
)) {
1289 if (!gd_grab_on_hover(s
)) {
1290 gd_grab_keyboard(vc
);
1292 gd_grab_pointer(vc
);
1294 gd_ungrab_keyboard(s
);
1295 gd_ungrab_pointer(s
);
1298 gd_update_caption(s
);
1299 gd_update_cursor(vc
);
1302 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1305 GtkDisplayState
*s
= data
;
1309 if (!gtk_widget_get_realized(s
->notebook
)) {
1313 #ifdef VTE_RESIZE_HACK
1314 vc
= gd_vc_find_current(s
);
1315 if (vc
&& vc
->type
== GD_VC_VTE
) {
1316 gtk_widget_hide(vc
->vte
.terminal
);
1319 vc
= gd_vc_find_by_page(s
, arg2
);
1323 #ifdef VTE_RESIZE_HACK
1324 if (vc
->type
== GD_VC_VTE
) {
1325 gtk_widget_show(vc
->vte
.terminal
);
1328 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1330 on_vga
= (vc
->type
== GD_VC_GFX
);
1332 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1334 } else if (s
->full_screen
) {
1335 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1338 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1340 gd_update_windowsize(vc
);
1341 gd_update_cursor(vc
);
1344 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1347 VirtualConsole
*vc
= opaque
;
1348 GtkDisplayState
*s
= vc
->s
;
1350 if (gd_grab_on_hover(s
)) {
1351 gd_ungrab_keyboard(s
);
1352 gd_grab_keyboard(vc
);
1353 gd_update_caption(s
);
1358 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1361 VirtualConsole
*vc
= opaque
;
1362 GtkDisplayState
*s
= vc
->s
;
1364 if (gd_grab_on_hover(s
)) {
1365 gd_ungrab_keyboard(s
);
1366 gd_update_caption(s
);
1371 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1372 GdkEventCrossing
*crossing
, gpointer opaque
)
1374 VirtualConsole
*vc
= opaque
;
1375 GtkDisplayState
*s
= vc
->s
;
1377 gtk_release_modifiers(s
);
1381 /** Virtual Console Callbacks **/
1383 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1384 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1388 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", idx
);
1390 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1391 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1392 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
1393 gtk_accel_map_add_entry(path
, GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1395 g_signal_connect(vc
->menu_item
, "activate",
1396 G_CALLBACK(gd_menu_switch_vc
), s
);
1397 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1402 #if defined(CONFIG_VTE)
1403 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1405 VirtualConsole
*vc
= opaque
;
1407 if (gtk_adjustment_get_upper(adjustment
) >
1408 gtk_adjustment_get_page_size(adjustment
)) {
1409 gtk_widget_show(vc
->vte
.scrollbar
);
1411 gtk_widget_hide(vc
->vte
.scrollbar
);
1415 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1417 VirtualConsole
*vc
= chr
->opaque
;
1419 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1424 static CharDriverState
*vcs
[MAX_VCS
];
1426 static CharDriverState
*gd_vc_handler(ChardevVC
*unused
)
1428 CharDriverState
*chr
;
1430 chr
= g_malloc0(sizeof(*chr
));
1431 chr
->chr_write
= gd_vc_chr_write
;
1432 /* defer OPENED events until our vc is fully initialized */
1433 chr
->explicit_be_open
= true;
1435 vcs
[nb_vcs
++] = chr
;
1440 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1443 VirtualConsole
*vc
= user_data
;
1445 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1449 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1450 CharDriverState
*chr
, int idx
,
1451 GSList
*group
, GtkWidget
*view_menu
)
1455 GtkWidget
*scrollbar
;
1456 GtkAdjustment
*vadjustment
;
1461 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1462 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1463 ? vc
->vte
.chr
->label
: buffer
);
1464 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1466 vc
->vte
.terminal
= vte_terminal_new();
1467 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1469 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1470 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1471 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1473 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1474 vadjustment
= gtk_scrollable_get_vadjustment
1475 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1477 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1480 #if GTK_CHECK_VERSION(3, 0, 0)
1481 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1482 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1484 box
= gtk_hbox_new(false, 2);
1485 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1488 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1489 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1491 vc
->vte
.chr
->opaque
= vc
;
1493 vc
->vte
.scrollbar
= scrollbar
;
1495 g_signal_connect(vadjustment
, "changed",
1496 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1498 vc
->type
= GD_VC_VTE
;
1500 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1501 gtk_label_new(vc
->label
));
1503 qemu_chr_be_generic_open(vc
->vte
.chr
);
1504 if (vc
->vte
.chr
->init
) {
1505 vc
->vte
.chr
->init(vc
->vte
.chr
);
1511 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1512 GtkWidget
*view_menu
)
1516 for (i
= 0; i
< nb_vcs
; i
++) {
1517 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1518 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1522 #endif /* CONFIG_VTE */
1524 /** Window Creation **/
1526 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1528 #if GTK_CHECK_VERSION(3, 0, 0)
1529 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1530 G_CALLBACK(gd_draw_event
), vc
);
1532 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1533 G_CALLBACK(gd_expose_event
), vc
);
1535 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1536 G_CALLBACK(gd_event
), vc
);
1537 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1538 G_CALLBACK(gd_button_event
), vc
);
1539 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1540 G_CALLBACK(gd_button_event
), vc
);
1541 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1542 G_CALLBACK(gd_scroll_event
), vc
);
1543 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1544 G_CALLBACK(gd_key_event
), vc
);
1545 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1546 G_CALLBACK(gd_key_event
), vc
);
1548 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1549 G_CALLBACK(gd_enter_event
), vc
);
1550 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1551 G_CALLBACK(gd_leave_event
), vc
);
1552 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1553 G_CALLBACK(gd_focus_out_event
), vc
);
1556 static void gd_connect_signals(GtkDisplayState
*s
)
1558 g_signal_connect(s
->show_tabs_item
, "activate",
1559 G_CALLBACK(gd_menu_show_tabs
), s
);
1560 g_signal_connect(s
->untabify_item
, "activate",
1561 G_CALLBACK(gd_menu_untabify
), s
);
1563 g_signal_connect(s
->window
, "delete-event",
1564 G_CALLBACK(gd_window_close
), s
);
1566 g_signal_connect(s
->pause_item
, "activate",
1567 G_CALLBACK(gd_menu_pause
), s
);
1568 g_signal_connect(s
->reset_item
, "activate",
1569 G_CALLBACK(gd_menu_reset
), s
);
1570 g_signal_connect(s
->powerdown_item
, "activate",
1571 G_CALLBACK(gd_menu_powerdown
), s
);
1572 g_signal_connect(s
->quit_item
, "activate",
1573 G_CALLBACK(gd_menu_quit
), s
);
1574 g_signal_connect(s
->full_screen_item
, "activate",
1575 G_CALLBACK(gd_menu_full_screen
), s
);
1576 g_signal_connect(s
->zoom_in_item
, "activate",
1577 G_CALLBACK(gd_menu_zoom_in
), s
);
1578 g_signal_connect(s
->zoom_out_item
, "activate",
1579 G_CALLBACK(gd_menu_zoom_out
), s
);
1580 g_signal_connect(s
->zoom_fixed_item
, "activate",
1581 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1582 g_signal_connect(s
->zoom_fit_item
, "activate",
1583 G_CALLBACK(gd_menu_zoom_fit
), s
);
1584 g_signal_connect(s
->grab_item
, "activate",
1585 G_CALLBACK(gd_menu_grab_input
), s
);
1586 g_signal_connect(s
->notebook
, "switch-page",
1587 G_CALLBACK(gd_change_page
), s
);
1590 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
, GtkAccelGroup
*accel_group
)
1592 GtkWidget
*machine_menu
;
1593 GtkWidget
*separator
;
1595 machine_menu
= gtk_menu_new();
1596 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), accel_group
);
1598 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1599 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1601 separator
= gtk_separator_menu_item_new();
1602 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1604 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1605 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1607 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1608 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1610 separator
= gtk_separator_menu_item_new();
1611 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1613 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1614 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1615 "<QEMU>/Machine/Quit");
1616 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1617 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1618 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1620 return machine_menu
;
1623 static const DisplayChangeListenerOps dcl_ops
= {
1625 .dpy_gfx_update
= gd_update
,
1626 .dpy_gfx_switch
= gd_switch
,
1627 .dpy_refresh
= gd_refresh
,
1628 .dpy_mouse_set
= gd_mouse_set
,
1629 .dpy_cursor_define
= gd_cursor_define
,
1632 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1633 QemuConsole
*con
, int idx
,
1634 GSList
*group
, GtkWidget
*view_menu
)
1636 Error
*local_err
= NULL
;
1639 obj
= object_property_get_link(OBJECT(con
), "device", &local_err
);
1641 vc
->label
= g_strdup_printf("%s", object_get_typename(obj
));
1643 vc
->label
= g_strdup_printf("VGA");
1647 vc
->gfx
.scale_x
= 1.0;
1648 vc
->gfx
.scale_y
= 1.0;
1650 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1651 gtk_widget_add_events(vc
->gfx
.drawing_area
,
1652 GDK_POINTER_MOTION_MASK
|
1653 GDK_BUTTON_PRESS_MASK
|
1654 GDK_BUTTON_RELEASE_MASK
|
1655 GDK_BUTTON_MOTION_MASK
|
1656 GDK_ENTER_NOTIFY_MASK
|
1657 GDK_LEAVE_NOTIFY_MASK
|
1659 GDK_KEY_PRESS_MASK
);
1660 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
1661 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
1663 vc
->type
= GD_VC_GFX
;
1664 vc
->tab_item
= vc
->gfx
.drawing_area
;
1665 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
1666 vc
->tab_item
, gtk_label_new(vc
->label
));
1667 gd_connect_vc_gfx_signals(vc
);
1669 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1671 vc
->gfx
.dcl
.ops
= &dcl_ops
;
1672 vc
->gfx
.dcl
.con
= con
;
1673 register_displaychangelistener(&vc
->gfx
.dcl
);
1678 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
, GtkAccelGroup
*accel_group
)
1680 GSList
*group
= NULL
;
1681 GtkWidget
*view_menu
;
1682 GtkWidget
*separator
;
1686 view_menu
= gtk_menu_new();
1687 gtk_menu_set_accel_group(GTK_MENU(view_menu
), accel_group
);
1689 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1690 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1691 "<QEMU>/View/Full Screen");
1692 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
,
1694 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
1696 separator
= gtk_separator_menu_item_new();
1697 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1699 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1700 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1701 "<QEMU>/View/Zoom In");
1702 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
1704 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
1706 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1707 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1708 "<QEMU>/View/Zoom Out");
1709 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
1711 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
1713 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1714 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1715 "<QEMU>/View/Zoom Fixed");
1716 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
1718 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
1720 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1721 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
1723 separator
= gtk_separator_menu_item_new();
1724 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1726 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1727 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
1729 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1730 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1731 "<QEMU>/View/Grab Input");
1732 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
1734 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
1736 separator
= gtk_separator_menu_item_new();
1737 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1740 for (vc
= 0;; vc
++) {
1741 con
= qemu_console_lookup_by_index(vc
);
1742 if (!con
|| !qemu_console_is_graphic(con
)) {
1745 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
1746 vc
, group
, view_menu
);
1750 #if defined(CONFIG_VTE)
1752 gd_vcs_init(s
, group
, view_menu
);
1755 separator
= gtk_separator_menu_item_new();
1756 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1758 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1759 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
1761 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1762 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
1767 static void gd_create_menus(GtkDisplayState
*s
)
1769 GtkAccelGroup
*accel_group
;
1771 accel_group
= gtk_accel_group_new();
1772 s
->machine_menu
= gd_create_menu_machine(s
, accel_group
);
1773 s
->view_menu
= gd_create_menu_view(s
, accel_group
);
1775 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1776 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1778 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1780 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1781 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1782 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1784 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1785 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1786 s
->accel_group
= accel_group
;
1789 static void gd_set_keycode_type(GtkDisplayState
*s
)
1791 #ifdef GDK_WINDOWING_X11
1792 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
1793 if (GDK_IS_X11_DISPLAY(display
)) {
1794 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
1795 XkbDescPtr desc
= XkbGetKeyboard(x11_display
, XkbGBN_AllComponentsMask
,
1797 char *keycodes
= NULL
;
1799 if (desc
&& desc
->names
) {
1800 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
1802 if (keycodes
== NULL
) {
1803 fprintf(stderr
, "could not lookup keycode name\n");
1804 } else if (strstart(keycodes
, "evdev", NULL
)) {
1805 s
->has_evdev
= true;
1806 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
1807 fprintf(stderr
, "unknown keycodes `%s', please report to "
1808 "qemu-devel@nongnu.org\n", keycodes
);
1814 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
1816 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1819 gtk_init(NULL
, NULL
);
1821 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1822 #if GTK_CHECK_VERSION(3, 2, 0)
1823 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1825 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1827 s
->notebook
= gtk_notebook_new();
1828 s
->menu_bar
= gtk_menu_bar_new();
1830 s
->free_scale
= FALSE
;
1832 setlocale(LC_ALL
, "");
1833 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1836 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1838 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1839 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1840 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1842 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
1844 GError
*error
= NULL
;
1845 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
1847 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
1849 g_error_free(error
);
1856 gd_connect_signals(s
);
1858 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1859 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1861 gd_update_caption(s
);
1863 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1864 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1866 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1868 gtk_widget_show_all(s
->window
);
1870 #ifdef VTE_RESIZE_HACK
1872 VirtualConsole
*cur
= gd_vc_find_current(s
);
1875 for (i
= 0; i
< s
->nb_vcs
; i
++) {
1876 VirtualConsole
*vc
= &s
->vc
[i
];
1877 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
1878 gtk_widget_hide(vc
->vte
.terminal
);
1881 gd_update_windowsize(cur
);
1886 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1888 if (grab_on_hover
) {
1889 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
1892 gd_set_keycode_type(s
);
1895 void early_gtk_display_init(void)
1897 #if defined(CONFIG_VTE)
1898 register_vc_handler(gd_vc_handler
);