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
131 #define GDK_KEY_Pause GDK_Pause
134 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
136 static const int modifier_keycode
[] = {
137 /* shift, control, alt keys, meta keys, both left & right */
138 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
141 typedef struct GtkDisplayState GtkDisplayState
;
143 typedef struct VirtualGfxConsole
{
144 GtkWidget
*drawing_area
;
145 DisplayChangeListener dcl
;
147 pixman_image_t
*convert
;
148 cairo_surface_t
*surface
;
153 #if defined(CONFIG_VTE)
154 typedef struct VirtualVteConsole
{
156 GtkWidget
*scrollbar
;
158 CharDriverState
*chr
;
162 typedef enum VirtualConsoleType
{
165 } VirtualConsoleType
;
167 typedef struct VirtualConsole
{
171 GtkWidget
*menu_item
;
173 VirtualConsoleType type
;
175 VirtualGfxConsole gfx
;
176 #if defined(CONFIG_VTE)
177 VirtualVteConsole vte
;
182 struct GtkDisplayState
{
187 GtkAccelGroup
*accel_group
;
189 GtkWidget
*machine_menu_item
;
190 GtkWidget
*machine_menu
;
191 GtkWidget
*pause_item
;
192 GtkWidget
*reset_item
;
193 GtkWidget
*powerdown_item
;
194 GtkWidget
*quit_item
;
196 GtkWidget
*view_menu_item
;
197 GtkWidget
*view_menu
;
198 GtkWidget
*full_screen_item
;
199 GtkWidget
*zoom_in_item
;
200 GtkWidget
*zoom_out_item
;
201 GtkWidget
*zoom_fixed_item
;
202 GtkWidget
*zoom_fit_item
;
203 GtkWidget
*grab_item
;
204 GtkWidget
*grab_on_hover_item
;
207 VirtualConsole vc
[MAX_VCS
];
209 GtkWidget
*show_tabs_item
;
210 GtkWidget
*untabify_item
;
220 VirtualConsole
*kbd_owner
;
221 VirtualConsole
*ptr_owner
;
223 gboolean full_screen
;
225 GdkCursor
*null_cursor
;
226 Notifier mouse_mode_notifier
;
229 bool external_pause_update
;
231 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
235 static void gd_grab_pointer(VirtualConsole
*vc
);
236 static void gd_ungrab_pointer(GtkDisplayState
*s
);
238 /** Utility Functions **/
240 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
245 for (i
= 0; i
< s
->nb_vcs
; i
++) {
247 if (gtk_check_menu_item_get_active
248 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
255 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
260 for (i
= 0; i
< s
->nb_vcs
; i
++) {
262 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
270 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
274 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
275 return gd_vc_find_by_page(s
, page
);
278 static bool gd_is_grab_active(GtkDisplayState
*s
)
280 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
283 static bool gd_grab_on_hover(GtkDisplayState
*s
)
285 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
288 static void gd_update_cursor(VirtualConsole
*vc
)
290 GtkDisplayState
*s
= vc
->s
;
293 if (vc
->type
!= GD_VC_GFX
) {
297 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
301 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
302 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
303 gdk_window_set_cursor(window
, s
->null_cursor
);
305 gdk_window_set_cursor(window
, NULL
);
309 static void gd_update_caption(GtkDisplayState
*s
)
311 const char *status
= "";
314 const char *grab
= "";
315 bool is_paused
= !runstate_is_running();
319 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
321 prefix
= g_strdup_printf("QEMU");
324 if (s
->ptr_owner
!= NULL
&&
325 s
->ptr_owner
->window
== NULL
) {
326 grab
= _(" - Press Ctrl+Alt+G to release grab");
330 status
= _(" [Paused]");
332 s
->external_pause_update
= true;
333 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
335 s
->external_pause_update
= false;
337 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
338 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
341 for (i
= 0; i
< s
->nb_vcs
; i
++) {
342 VirtualConsole
*vc
= &s
->vc
[i
];
347 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
348 vc
== s
->kbd_owner
? " +kbd" : "",
349 vc
== s
->ptr_owner
? " +ptr" : "");
350 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
357 static void gd_update_geometry_hints(VirtualConsole
*vc
)
359 GtkDisplayState
*s
= vc
->s
;
360 GdkWindowHints mask
= 0;
361 GdkGeometry geo
= {};
362 GtkWidget
*geo_widget
= NULL
;
363 GtkWindow
*geo_window
;
365 if (vc
->type
== GD_VC_GFX
) {
367 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
368 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
369 mask
|= GDK_HINT_MIN_SIZE
;
371 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
372 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
373 mask
|= GDK_HINT_MIN_SIZE
;
375 geo_widget
= vc
->gfx
.drawing_area
;
376 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
378 #if defined(CONFIG_VTE)
379 } else if (vc
->type
== GD_VC_VTE
) {
380 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
383 geo
.width_inc
= vte_terminal_get_char_width(term
);
384 geo
.height_inc
= vte_terminal_get_char_height(term
);
385 mask
|= GDK_HINT_RESIZE_INC
;
386 geo
.base_width
= geo
.width_inc
;
387 geo
.base_height
= geo
.height_inc
;
388 mask
|= GDK_HINT_BASE_SIZE
;
389 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
390 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
391 mask
|= GDK_HINT_MIN_SIZE
;
392 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
393 geo
.base_width
+= ib
->left
+ ib
->right
;
394 geo
.base_height
+= ib
->top
+ ib
->bottom
;
395 geo
.min_width
+= ib
->left
+ ib
->right
;
396 geo
.min_height
+= ib
->top
+ ib
->bottom
;
397 geo_widget
= vc
->vte
.terminal
;
401 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
402 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
405 static void gd_update_windowsize(VirtualConsole
*vc
)
407 GtkDisplayState
*s
= vc
->s
;
409 gd_update_geometry_hints(vc
);
411 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
412 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
413 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
417 static void gd_update_full_redraw(VirtualConsole
*vc
)
419 GtkWidget
*area
= vc
->gfx
.drawing_area
;
421 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
422 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
425 static void gtk_release_modifiers(GtkDisplayState
*s
)
427 VirtualConsole
*vc
= gd_vc_find_current(s
);
430 if (vc
->type
!= GD_VC_GFX
) {
433 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
434 keycode
= modifier_keycode
[i
];
435 if (!s
->modifier_pressed
[i
]) {
438 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, keycode
, false);
439 s
->modifier_pressed
[i
] = false;
443 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
446 g_object_ref(G_OBJECT(widget
));
447 gtk_container_remove(GTK_CONTAINER(from
), widget
);
448 gtk_container_add(GTK_CONTAINER(to
), widget
);
449 g_object_unref(G_OBJECT(widget
));
452 /** DisplayState Callbacks **/
454 static void gd_update(DisplayChangeListener
*dcl
,
455 int x
, int y
, int w
, int h
)
457 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
463 trace_gd_update(vc
->label
, x
, y
, w
, h
);
465 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
469 if (vc
->gfx
.convert
) {
470 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
471 NULL
, vc
->gfx
.convert
,
472 x
, y
, 0, 0, x
, y
, w
, h
);
475 x1
= floor(x
* vc
->gfx
.scale_x
);
476 y1
= floor(y
* vc
->gfx
.scale_y
);
478 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
479 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
481 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
482 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
484 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
495 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
496 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
499 static void gd_refresh(DisplayChangeListener
*dcl
)
501 graphic_hw_update(dcl
->con
);
504 #if GTK_CHECK_VERSION(3, 0, 0)
505 static void gd_mouse_set(DisplayChangeListener
*dcl
,
506 int x
, int y
, int visible
)
508 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
510 GdkDeviceManager
*mgr
;
513 if (qemu_input_is_absolute()) {
517 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
518 mgr
= gdk_display_get_device_manager(dpy
);
519 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
520 x
, y
, &x_root
, &y_root
);
521 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
522 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
526 static void gd_mouse_set(DisplayChangeListener
*dcl
,
527 int x
, int y
, int visible
)
529 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
532 if (qemu_input_is_absolute()) {
536 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
537 x
, y
, &x_root
, &y_root
);
538 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
539 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
544 static void gd_cursor_define(DisplayChangeListener
*dcl
,
547 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
551 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
555 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
556 GDK_COLORSPACE_RGB
, true, 8,
557 c
->width
, c
->height
, c
->width
* 4,
559 cursor
= gdk_cursor_new_from_pixbuf
560 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
561 pixbuf
, c
->hot_x
, c
->hot_y
);
562 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
563 g_object_unref(pixbuf
);
564 #if !GTK_CHECK_VERSION(3, 0, 0)
565 gdk_cursor_unref(cursor
);
567 g_object_unref(cursor
);
571 static void gd_switch(DisplayChangeListener
*dcl
,
572 DisplaySurface
*surface
)
574 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
577 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
579 if (vc
->gfx
.surface
) {
580 cairo_surface_destroy(vc
->gfx
.surface
);
584 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
585 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
588 vc
->gfx
.ds
= surface
;
590 if (vc
->gfx
.convert
) {
591 pixman_image_unref(vc
->gfx
.convert
);
592 vc
->gfx
.convert
= NULL
;
595 if (surface
->format
== PIXMAN_x8r8g8b8
) {
597 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
599 * No need to convert, use surface directly. Should be the
600 * common case as this is qemu_default_pixelformat(32) too.
602 vc
->gfx
.surface
= cairo_image_surface_create_for_data
603 (surface_data(surface
),
605 surface_width(surface
),
606 surface_height(surface
),
607 surface_stride(surface
));
609 /* Must convert surface, use pixman to do it. */
610 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
611 surface_width(surface
),
612 surface_height(surface
),
614 vc
->gfx
.surface
= cairo_image_surface_create_for_data
615 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
617 pixman_image_get_width(vc
->gfx
.convert
),
618 pixman_image_get_height(vc
->gfx
.convert
),
619 pixman_image_get_stride(vc
->gfx
.convert
));
620 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
621 NULL
, vc
->gfx
.convert
,
623 pixman_image_get_width(vc
->gfx
.convert
),
624 pixman_image_get_height(vc
->gfx
.convert
));
628 gd_update_windowsize(vc
);
630 gd_update_full_redraw(vc
);
636 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
638 GtkDisplayState
*s
= opaque
;
640 gd_update_caption(s
);
643 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
648 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
649 /* release the grab at switching to absolute mode */
650 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
651 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
654 for (i
= 0; i
< s
->nb_vcs
; i
++) {
655 VirtualConsole
*vc
= &s
->vc
[i
];
656 gd_update_cursor(vc
);
662 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
665 GtkDisplayState
*s
= opaque
;
669 for (i
= 0; i
< s
->nb_vcs
; i
++) {
670 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
673 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
682 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
684 VirtualConsole
*vc
= opaque
;
685 GtkDisplayState
*s
= vc
->s
;
690 if (!gtk_widget_get_realized(widget
)) {
694 fbw
= surface_width(vc
->gfx
.ds
);
695 fbh
= surface_height(vc
->gfx
.ds
);
697 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
699 if (s
->full_screen
) {
700 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
701 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
702 } else if (s
->free_scale
) {
705 sx
= (double)ww
/ fbw
;
706 sy
= (double)wh
/ fbh
;
708 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
711 fbw
*= vc
->gfx
.scale_x
;
712 fbh
*= vc
->gfx
.scale_y
;
722 cairo_rectangle(cr
, 0, 0, ww
, wh
);
724 /* Optionally cut out the inner area where the pixmap
725 will be drawn. This avoids 'flashing' since we're
726 not double-buffering. Note we're using the undocumented
727 behaviour of drawing the rectangle from right to left
728 to cut out the whole */
729 cairo_rectangle(cr
, mx
+ fbw
, my
,
733 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
734 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
735 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
741 #if !GTK_CHECK_VERSION(3, 0, 0)
742 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
748 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
753 expose
->area
.height
);
756 ret
= gd_draw_event(widget
, cr
, opaque
);
764 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
767 VirtualConsole
*vc
= opaque
;
768 GtkDisplayState
*s
= vc
->s
;
774 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
775 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
777 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
788 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
789 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
791 if (qemu_input_is_absolute()) {
792 if (x
< 0 || y
< 0 ||
793 x
>= surface_width(vc
->gfx
.ds
) ||
794 y
>= surface_height(vc
->gfx
.ds
)) {
797 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
798 surface_width(vc
->gfx
.ds
));
799 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
800 surface_height(vc
->gfx
.ds
));
801 qemu_input_event_sync();
802 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
803 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
804 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
805 qemu_input_event_sync();
811 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
812 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
813 int x
= (int)motion
->x_root
;
814 int y
= (int)motion
->y_root
;
816 /* In relative mode check to see if client pointer hit
817 * one of the screen edges, and if so move it back by
818 * 200 pixels. This is important because the pointer
819 * in the server doesn't correspond 1-for-1, and so
820 * may still be only half way across the screen. Without
821 * this warp, the server pointer would thus appear to hit
822 * an invisible wall */
829 if (x
== (gdk_screen_get_width(screen
) - 1)) {
832 if (y
== (gdk_screen_get_height(screen
) - 1)) {
836 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
837 #if GTK_CHECK_VERSION(3, 0, 0)
838 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
839 gdk_device_warp(dev
, screen
, x
, y
);
841 GdkDisplay
*display
= gtk_widget_get_display(widget
);
842 gdk_display_warp_pointer(display
, screen
, x
, y
);
851 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
854 VirtualConsole
*vc
= opaque
;
855 GtkDisplayState
*s
= vc
->s
;
858 /* implicitly grab the input at the first click in the relative mode */
859 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
860 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
861 gd_ungrab_pointer(s
);
863 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
867 gd_update_caption(s
);
872 if (button
->button
== 1) {
873 btn
= INPUT_BUTTON_LEFT
;
874 } else if (button
->button
== 2) {
875 btn
= INPUT_BUTTON_MIDDLE
;
876 } else if (button
->button
== 3) {
877 btn
= INPUT_BUTTON_RIGHT
;
882 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
883 button
->type
== GDK_BUTTON_PRESS
);
884 qemu_input_event_sync();
888 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
891 VirtualConsole
*vc
= opaque
;
894 if (scroll
->direction
== GDK_SCROLL_UP
) {
895 btn
= INPUT_BUTTON_WHEEL_UP
;
896 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
897 btn
= INPUT_BUTTON_WHEEL_DOWN
;
902 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
903 qemu_input_event_sync();
904 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
905 qemu_input_event_sync();
909 static int gd_map_keycode(GtkDisplayState
*s
, GdkDisplay
*dpy
, int gdk_keycode
)
913 #ifdef GDK_WINDOWING_WIN32
914 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
915 qemu_keycode
= MapVirtualKey(gdk_keycode
, MAPVK_VK_TO_VSC
);
916 switch (qemu_keycode
) {
917 case 103: /* alt gr */
918 qemu_keycode
= 56 | SCANCODE_GREY
;
925 if (gdk_keycode
< 9) {
927 } else if (gdk_keycode
< 97) {
928 qemu_keycode
= gdk_keycode
- 8;
929 #ifdef GDK_WINDOWING_X11
930 } else if (GDK_IS_X11_DISPLAY(dpy
) && gdk_keycode
< 158) {
932 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
934 qemu_keycode
= translate_xfree86_keycode(gdk_keycode
- 97);
937 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
939 } else if (gdk_keycode
== 211) { /* backslash */
948 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
950 VirtualConsole
*vc
= opaque
;
951 GtkDisplayState
*s
= vc
->s
;
952 int gdk_keycode
= key
->hardware_keycode
;
956 if (key
->keyval
== GDK_KEY_Pause
) {
957 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
958 key
->type
== GDK_KEY_PRESS
);
962 qemu_keycode
= gd_map_keycode(s
, gtk_widget_get_display(widget
),
965 trace_gd_key_event(vc
->label
, gdk_keycode
, qemu_keycode
,
966 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
968 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
969 if (qemu_keycode
== modifier_keycode
[i
]) {
970 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
974 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, qemu_keycode
,
975 key
->type
== GDK_KEY_PRESS
);
980 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
982 if (event
->type
== GDK_MOTION_NOTIFY
) {
983 return gd_motion_event(widget
, &event
->motion
, opaque
);
988 /** Window Menu Actions **/
990 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
992 GtkDisplayState
*s
= opaque
;
994 if (s
->external_pause_update
) {
997 if (runstate_is_running()) {
1004 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1006 qmp_system_reset(NULL
);
1009 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1011 qmp_system_powerdown(NULL
);
1014 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1019 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1021 GtkDisplayState
*s
= opaque
;
1022 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1023 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1027 gtk_release_modifiers(s
);
1029 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1030 gtk_notebook_set_current_page(nb
, page
);
1031 child
= gtk_notebook_get_nth_page(nb
, page
);
1032 gtk_widget_grab_focus(child
);
1036 static void gd_accel_switch_vc(void *opaque
)
1038 VirtualConsole
*vc
= opaque
;
1039 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1042 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1044 GtkDisplayState
*s
= opaque
;
1045 VirtualConsole
*vc
= gd_vc_find_current(s
);
1047 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1048 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1050 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1052 gd_update_windowsize(vc
);
1055 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1058 VirtualConsole
*vc
= opaque
;
1059 GtkDisplayState
*s
= vc
->s
;
1061 gtk_widget_set_sensitive(vc
->menu_item
, true);
1062 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1063 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1064 vc
->tab_item
, vc
->label
);
1065 gtk_widget_destroy(vc
->window
);
1070 static gboolean
gd_win_grab(void *opaque
)
1072 VirtualConsole
*vc
= opaque
;
1074 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1075 if (vc
->s
->ptr_owner
) {
1076 gd_ungrab_pointer(vc
->s
);
1078 gd_grab_pointer(vc
);
1080 gd_update_caption(vc
->s
);
1084 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1086 GtkDisplayState
*s
= opaque
;
1087 VirtualConsole
*vc
= gd_vc_find_current(s
);
1089 if (vc
->type
== GD_VC_GFX
) {
1090 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1094 gtk_widget_set_sensitive(vc
->menu_item
, false);
1095 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1096 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1098 g_signal_connect(vc
->window
, "delete-event",
1099 G_CALLBACK(gd_tab_window_close
), vc
);
1100 gtk_widget_show_all(vc
->window
);
1102 GtkAccelGroup
*ag
= gtk_accel_group_new();
1103 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1105 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
), vc
, NULL
);
1106 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1108 gd_update_geometry_hints(vc
);
1109 gd_update_caption(s
);
1113 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1115 GtkDisplayState
*s
= opaque
;
1116 VirtualConsole
*vc
= gd_vc_find_current(s
);
1118 if (!s
->full_screen
) {
1119 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1120 gtk_widget_hide(s
->menu_bar
);
1121 if (vc
->type
== GD_VC_GFX
) {
1122 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1123 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1126 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1127 s
->full_screen
= TRUE
;
1129 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1130 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1131 gtk_widget_show(s
->menu_bar
);
1132 s
->full_screen
= FALSE
;
1133 if (vc
->type
== GD_VC_GFX
) {
1134 vc
->gfx
.scale_x
= 1.0;
1135 vc
->gfx
.scale_y
= 1.0;
1136 gd_update_windowsize(vc
);
1137 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1142 gd_update_cursor(vc
);
1145 static void gd_accel_full_screen(void *opaque
)
1147 GtkDisplayState
*s
= opaque
;
1148 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1151 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1153 GtkDisplayState
*s
= opaque
;
1154 VirtualConsole
*vc
= gd_vc_find_current(s
);
1156 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1159 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1160 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1162 gd_update_windowsize(vc
);
1165 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1167 GtkDisplayState
*s
= opaque
;
1168 VirtualConsole
*vc
= gd_vc_find_current(s
);
1170 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1173 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1174 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1176 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1177 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1179 gd_update_windowsize(vc
);
1182 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1184 GtkDisplayState
*s
= opaque
;
1185 VirtualConsole
*vc
= gd_vc_find_current(s
);
1187 vc
->gfx
.scale_x
= 1.0;
1188 vc
->gfx
.scale_y
= 1.0;
1190 gd_update_windowsize(vc
);
1193 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1195 GtkDisplayState
*s
= opaque
;
1196 VirtualConsole
*vc
= gd_vc_find_current(s
);
1198 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1199 s
->free_scale
= TRUE
;
1201 s
->free_scale
= FALSE
;
1202 vc
->gfx
.scale_x
= 1.0;
1203 vc
->gfx
.scale_y
= 1.0;
1206 gd_update_windowsize(vc
);
1207 gd_update_full_redraw(vc
);
1210 #if GTK_CHECK_VERSION(3, 0, 0)
1211 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1212 GdkInputSource source
, GdkEventMask mask
,
1215 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1216 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1217 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1220 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1221 GdkDevice
*dev
= tmp
->data
;
1222 if (gdk_device_get_source(dev
) != source
) {
1226 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1227 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1228 mask
, cursor
, GDK_CURRENT_TIME
);
1230 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1237 static void gd_grab_keyboard(VirtualConsole
*vc
)
1239 #if GTK_CHECK_VERSION(3, 0, 0)
1240 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1241 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1244 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1248 vc
->s
->kbd_owner
= vc
;
1249 trace_gd_grab(vc
->label
, "kbd", true);
1252 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1254 VirtualConsole
*vc
= s
->kbd_owner
;
1259 s
->kbd_owner
= NULL
;
1261 #if GTK_CHECK_VERSION(3, 0, 0)
1262 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1264 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1266 trace_gd_grab(vc
->label
, "kbd", false);
1269 static void gd_grab_pointer(VirtualConsole
*vc
)
1271 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1272 #if GTK_CHECK_VERSION(3, 0, 0)
1273 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1274 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1275 GDK_POINTER_MOTION_MASK
|
1276 GDK_BUTTON_PRESS_MASK
|
1277 GDK_BUTTON_RELEASE_MASK
|
1278 GDK_BUTTON_MOTION_MASK
|
1280 vc
->s
->null_cursor
);
1281 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr
),
1282 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1284 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1285 FALSE
, /* All events to come to our window directly */
1286 GDK_POINTER_MOTION_MASK
|
1287 GDK_BUTTON_PRESS_MASK
|
1288 GDK_BUTTON_RELEASE_MASK
|
1289 GDK_BUTTON_MOTION_MASK
|
1291 NULL
, /* Allow cursor to move over entire desktop */
1294 gdk_display_get_pointer(display
, NULL
,
1295 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1297 vc
->s
->ptr_owner
= vc
;
1298 trace_gd_grab(vc
->label
, "ptr", true);
1301 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1303 VirtualConsole
*vc
= s
->ptr_owner
;
1308 s
->ptr_owner
= NULL
;
1310 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1311 #if GTK_CHECK_VERSION(3, 0, 0)
1312 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1313 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1314 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
1315 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1316 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1318 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1319 gdk_display_warp_pointer(display
,
1320 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1321 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1323 trace_gd_grab(vc
->label
, "ptr", false);
1326 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1328 GtkDisplayState
*s
= opaque
;
1329 VirtualConsole
*vc
= gd_vc_find_current(s
);
1331 if (gd_is_grab_active(s
)) {
1332 if (!gd_grab_on_hover(s
)) {
1333 gd_grab_keyboard(vc
);
1335 gd_grab_pointer(vc
);
1337 gd_ungrab_keyboard(s
);
1338 gd_ungrab_pointer(s
);
1341 gd_update_caption(s
);
1342 gd_update_cursor(vc
);
1345 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1348 GtkDisplayState
*s
= data
;
1352 if (!gtk_widget_get_realized(s
->notebook
)) {
1356 #ifdef VTE_RESIZE_HACK
1357 vc
= gd_vc_find_current(s
);
1358 if (vc
&& vc
->type
== GD_VC_VTE
) {
1359 gtk_widget_hide(vc
->vte
.terminal
);
1362 vc
= gd_vc_find_by_page(s
, arg2
);
1366 #ifdef VTE_RESIZE_HACK
1367 if (vc
->type
== GD_VC_VTE
) {
1368 gtk_widget_show(vc
->vte
.terminal
);
1371 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1373 on_vga
= (vc
->type
== GD_VC_GFX
);
1375 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1377 } else if (s
->full_screen
) {
1378 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1381 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1383 gd_update_windowsize(vc
);
1384 gd_update_cursor(vc
);
1387 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1390 VirtualConsole
*vc
= opaque
;
1391 GtkDisplayState
*s
= vc
->s
;
1393 if (gd_grab_on_hover(s
)) {
1394 gd_ungrab_keyboard(s
);
1395 gd_grab_keyboard(vc
);
1396 gd_update_caption(s
);
1401 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1404 VirtualConsole
*vc
= opaque
;
1405 GtkDisplayState
*s
= vc
->s
;
1407 if (gd_grab_on_hover(s
)) {
1408 gd_ungrab_keyboard(s
);
1409 gd_update_caption(s
);
1414 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1415 GdkEventCrossing
*crossing
, gpointer opaque
)
1417 VirtualConsole
*vc
= opaque
;
1418 GtkDisplayState
*s
= vc
->s
;
1420 gtk_release_modifiers(s
);
1424 /** Virtual Console Callbacks **/
1426 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1427 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1429 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1430 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1431 HOTKEY_MODIFIERS
, 0,
1432 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1433 #if GTK_CHECK_VERSION(3, 8, 0)
1434 gtk_accel_label_set_accel(
1435 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1436 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1439 g_signal_connect(vc
->menu_item
, "activate",
1440 G_CALLBACK(gd_menu_switch_vc
), s
);
1441 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1443 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1447 #if defined(CONFIG_VTE)
1448 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1450 VirtualConsole
*vc
= opaque
;
1452 if (gtk_adjustment_get_upper(adjustment
) >
1453 gtk_adjustment_get_page_size(adjustment
)) {
1454 gtk_widget_show(vc
->vte
.scrollbar
);
1456 gtk_widget_hide(vc
->vte
.scrollbar
);
1460 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1462 VirtualConsole
*vc
= chr
->opaque
;
1464 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1469 static CharDriverState
*vcs
[MAX_VCS
];
1471 static CharDriverState
*gd_vc_handler(ChardevVC
*unused
)
1473 CharDriverState
*chr
;
1475 chr
= g_malloc0(sizeof(*chr
));
1476 chr
->chr_write
= gd_vc_chr_write
;
1477 /* defer OPENED events until our vc is fully initialized */
1478 chr
->explicit_be_open
= true;
1480 vcs
[nb_vcs
++] = chr
;
1485 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1488 VirtualConsole
*vc
= user_data
;
1490 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1494 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1495 CharDriverState
*chr
, int idx
,
1496 GSList
*group
, GtkWidget
*view_menu
)
1500 GtkWidget
*scrollbar
;
1501 GtkAdjustment
*vadjustment
;
1506 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1507 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1508 ? vc
->vte
.chr
->label
: buffer
);
1509 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1511 vc
->vte
.terminal
= vte_terminal_new();
1512 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1514 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1515 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1516 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1518 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1519 vadjustment
= gtk_scrollable_get_vadjustment
1520 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1522 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1525 #if GTK_CHECK_VERSION(3, 0, 0)
1526 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1527 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1529 box
= gtk_hbox_new(false, 2);
1530 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1533 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1534 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1536 vc
->vte
.chr
->opaque
= vc
;
1538 vc
->vte
.scrollbar
= scrollbar
;
1540 g_signal_connect(vadjustment
, "changed",
1541 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1543 vc
->type
= GD_VC_VTE
;
1545 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1546 gtk_label_new(vc
->label
));
1548 qemu_chr_be_generic_open(vc
->vte
.chr
);
1549 if (vc
->vte
.chr
->init
) {
1550 vc
->vte
.chr
->init(vc
->vte
.chr
);
1556 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1557 GtkWidget
*view_menu
)
1561 for (i
= 0; i
< nb_vcs
; i
++) {
1562 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1563 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1567 #endif /* CONFIG_VTE */
1569 /** Window Creation **/
1571 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1573 #if GTK_CHECK_VERSION(3, 0, 0)
1574 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1575 G_CALLBACK(gd_draw_event
), vc
);
1577 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1578 G_CALLBACK(gd_expose_event
), vc
);
1580 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1581 G_CALLBACK(gd_event
), vc
);
1582 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1583 G_CALLBACK(gd_button_event
), vc
);
1584 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1585 G_CALLBACK(gd_button_event
), vc
);
1586 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1587 G_CALLBACK(gd_scroll_event
), vc
);
1588 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1589 G_CALLBACK(gd_key_event
), vc
);
1590 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1591 G_CALLBACK(gd_key_event
), vc
);
1593 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1594 G_CALLBACK(gd_enter_event
), vc
);
1595 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1596 G_CALLBACK(gd_leave_event
), vc
);
1597 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1598 G_CALLBACK(gd_focus_out_event
), vc
);
1601 static void gd_connect_signals(GtkDisplayState
*s
)
1603 g_signal_connect(s
->show_tabs_item
, "activate",
1604 G_CALLBACK(gd_menu_show_tabs
), s
);
1605 g_signal_connect(s
->untabify_item
, "activate",
1606 G_CALLBACK(gd_menu_untabify
), s
);
1608 g_signal_connect(s
->window
, "delete-event",
1609 G_CALLBACK(gd_window_close
), s
);
1611 g_signal_connect(s
->pause_item
, "activate",
1612 G_CALLBACK(gd_menu_pause
), s
);
1613 g_signal_connect(s
->reset_item
, "activate",
1614 G_CALLBACK(gd_menu_reset
), s
);
1615 g_signal_connect(s
->powerdown_item
, "activate",
1616 G_CALLBACK(gd_menu_powerdown
), s
);
1617 g_signal_connect(s
->quit_item
, "activate",
1618 G_CALLBACK(gd_menu_quit
), s
);
1619 g_signal_connect(s
->full_screen_item
, "activate",
1620 G_CALLBACK(gd_menu_full_screen
), s
);
1621 g_signal_connect(s
->zoom_in_item
, "activate",
1622 G_CALLBACK(gd_menu_zoom_in
), s
);
1623 g_signal_connect(s
->zoom_out_item
, "activate",
1624 G_CALLBACK(gd_menu_zoom_out
), s
);
1625 g_signal_connect(s
->zoom_fixed_item
, "activate",
1626 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1627 g_signal_connect(s
->zoom_fit_item
, "activate",
1628 G_CALLBACK(gd_menu_zoom_fit
), s
);
1629 g_signal_connect(s
->grab_item
, "activate",
1630 G_CALLBACK(gd_menu_grab_input
), s
);
1631 g_signal_connect(s
->notebook
, "switch-page",
1632 G_CALLBACK(gd_change_page
), s
);
1635 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1637 GtkWidget
*machine_menu
;
1638 GtkWidget
*separator
;
1640 machine_menu
= gtk_menu_new();
1641 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1643 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1644 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1646 separator
= gtk_separator_menu_item_new();
1647 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1649 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1650 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1652 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1653 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1655 separator
= gtk_separator_menu_item_new();
1656 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1658 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1659 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1660 "<QEMU>/Machine/Quit");
1661 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1662 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1663 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1665 return machine_menu
;
1668 static const DisplayChangeListenerOps dcl_ops
= {
1670 .dpy_gfx_update
= gd_update
,
1671 .dpy_gfx_switch
= gd_switch
,
1672 .dpy_gfx_check_format
= qemu_pixman_check_format
,
1673 .dpy_refresh
= gd_refresh
,
1674 .dpy_mouse_set
= gd_mouse_set
,
1675 .dpy_cursor_define
= gd_cursor_define
,
1678 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1679 QemuConsole
*con
, int idx
,
1680 GSList
*group
, GtkWidget
*view_menu
)
1684 obj
= object_property_get_link(OBJECT(con
), "device", NULL
);
1686 vc
->label
= g_strdup_printf("%s", object_get_typename(obj
));
1688 vc
->label
= g_strdup_printf("VGA");
1692 vc
->gfx
.scale_x
= 1.0;
1693 vc
->gfx
.scale_y
= 1.0;
1695 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1696 gtk_widget_add_events(vc
->gfx
.drawing_area
,
1697 GDK_POINTER_MOTION_MASK
|
1698 GDK_BUTTON_PRESS_MASK
|
1699 GDK_BUTTON_RELEASE_MASK
|
1700 GDK_BUTTON_MOTION_MASK
|
1701 GDK_ENTER_NOTIFY_MASK
|
1702 GDK_LEAVE_NOTIFY_MASK
|
1704 GDK_KEY_PRESS_MASK
);
1705 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
1707 vc
->type
= GD_VC_GFX
;
1708 vc
->tab_item
= vc
->gfx
.drawing_area
;
1709 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
1710 vc
->tab_item
, gtk_label_new(vc
->label
));
1711 gd_connect_vc_gfx_signals(vc
);
1713 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1715 vc
->gfx
.dcl
.ops
= &dcl_ops
;
1716 vc
->gfx
.dcl
.con
= con
;
1717 register_displaychangelistener(&vc
->gfx
.dcl
);
1722 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
1724 GSList
*group
= NULL
;
1725 GtkWidget
*view_menu
;
1726 GtkWidget
*separator
;
1730 view_menu
= gtk_menu_new();
1731 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
1733 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1735 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
1736 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
1737 #if GTK_CHECK_VERSION(3, 8, 0)
1738 gtk_accel_label_set_accel(
1739 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
1740 GDK_KEY_f
, HOTKEY_MODIFIERS
);
1742 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
1744 separator
= gtk_separator_menu_item_new();
1745 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1747 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1748 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1749 "<QEMU>/View/Zoom In");
1750 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
1752 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
1754 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1755 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1756 "<QEMU>/View/Zoom Out");
1757 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
1759 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
1761 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1762 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1763 "<QEMU>/View/Zoom Fixed");
1764 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
1766 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
1768 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1769 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
1771 separator
= gtk_separator_menu_item_new();
1772 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1774 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1775 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
1777 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1778 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1779 "<QEMU>/View/Grab Input");
1780 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
1782 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
1784 separator
= gtk_separator_menu_item_new();
1785 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1788 for (vc
= 0;; vc
++) {
1789 con
= qemu_console_lookup_by_index(vc
);
1790 if (!con
|| !qemu_console_is_graphic(con
)) {
1793 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
1794 vc
, group
, view_menu
);
1798 #if defined(CONFIG_VTE)
1800 gd_vcs_init(s
, group
, view_menu
);
1803 separator
= gtk_separator_menu_item_new();
1804 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1806 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1807 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
1809 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1810 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
1815 static void gd_create_menus(GtkDisplayState
*s
)
1817 s
->accel_group
= gtk_accel_group_new();
1818 s
->machine_menu
= gd_create_menu_machine(s
);
1819 s
->view_menu
= gd_create_menu_view(s
);
1821 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1822 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1824 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1826 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1827 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1828 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1830 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
1831 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
1834 static void gd_set_keycode_type(GtkDisplayState
*s
)
1836 #ifdef GDK_WINDOWING_X11
1837 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
1838 if (GDK_IS_X11_DISPLAY(display
)) {
1839 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
1840 XkbDescPtr desc
= XkbGetKeyboard(x11_display
, XkbGBN_AllComponentsMask
,
1842 char *keycodes
= NULL
;
1844 if (desc
&& desc
->names
) {
1845 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
1847 if (keycodes
== NULL
) {
1848 fprintf(stderr
, "could not lookup keycode name\n");
1849 } else if (strstart(keycodes
, "evdev", NULL
)) {
1850 s
->has_evdev
= true;
1851 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
1852 fprintf(stderr
, "unknown keycodes `%s', please report to "
1853 "qemu-devel@nongnu.org\n", keycodes
);
1857 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
1866 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
1868 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1871 gtk_init(NULL
, NULL
);
1873 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1874 #if GTK_CHECK_VERSION(3, 2, 0)
1875 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1877 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1879 s
->notebook
= gtk_notebook_new();
1880 s
->menu_bar
= gtk_menu_bar_new();
1882 s
->free_scale
= FALSE
;
1884 setlocale(LC_ALL
, "");
1885 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1888 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1890 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1891 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1892 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1894 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
1896 GError
*error
= NULL
;
1897 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
1899 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
1901 g_error_free(error
);
1908 gd_connect_signals(s
);
1910 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1911 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1913 gd_update_caption(s
);
1915 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1916 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1918 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1920 gtk_widget_show_all(s
->window
);
1922 #ifdef VTE_RESIZE_HACK
1924 VirtualConsole
*cur
= gd_vc_find_current(s
);
1928 for (i
= 0; i
< s
->nb_vcs
; i
++) {
1929 VirtualConsole
*vc
= &s
->vc
[i
];
1930 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
1931 gtk_widget_hide(vc
->vte
.terminal
);
1934 gd_update_windowsize(cur
);
1940 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1942 if (grab_on_hover
) {
1943 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
1946 gd_set_keycode_type(s
);
1949 void early_gtk_display_init(void)
1951 #if defined(CONFIG_VTE)
1952 register_vc_handler(gd_vc_handler
);