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"
37 #include "qemu/osdep.h"
38 #include "qemu-common.h"
39 #include "qapi/error.h"
40 #include "qapi/qapi-commands-misc.h"
41 #include "qemu/cutils.h"
43 #include "ui/console.h"
46 #include <glib/gi18n.h>
48 #if defined(CONFIG_VTE)
55 #include "sysemu/sysemu.h"
57 #include "chardev/char.h"
58 #include "qom/object.h"
61 #define VC_WINDOW_X_MIN 320
62 #define VC_WINDOW_Y_MIN 240
63 #define VC_TERM_X_MIN 80
64 #define VC_TERM_Y_MIN 25
65 #define VC_SCALE_MIN 0.25
66 #define VC_SCALE_STEP 0.25
68 #ifdef GDK_WINDOWING_X11
72 #ifndef GDK_IS_X11_DISPLAY
73 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
78 #ifdef GDK_WINDOWING_WAYLAND
80 #ifndef GDK_IS_WAYLAND_DISPLAY
81 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
86 #ifdef GDK_WINDOWING_WIN32
88 #ifndef GDK_IS_WIN32_DISPLAY
89 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
94 #ifdef GDK_WINDOWING_BROADWAY
96 #ifndef GDK_IS_BROADWAY_DISPLAY
97 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
102 #ifdef GDK_WINDOWING_QUARTZ
104 #ifndef GDK_IS_QUARTZ_DISPLAY
105 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
110 #if !defined(CONFIG_VTE)
111 # define VTE_CHECK_VERSION(a, b, c) 0
114 /* Some older mingw versions lack this constant or have
115 * it conditionally defined */
117 # ifndef MAPVK_VK_TO_VSC
118 # define MAPVK_VK_TO_VSC 0
123 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
125 static const int modifier_keycode
[] = {
136 static const guint16
*keycode_map
;
137 static size_t keycode_maplen
;
139 struct GtkDisplayState
{
144 GtkAccelGroup
*accel_group
;
146 GtkWidget
*machine_menu_item
;
147 GtkWidget
*machine_menu
;
148 GtkWidget
*pause_item
;
149 GtkWidget
*reset_item
;
150 GtkWidget
*powerdown_item
;
151 GtkWidget
*quit_item
;
153 GtkWidget
*view_menu_item
;
154 GtkWidget
*view_menu
;
155 GtkWidget
*full_screen_item
;
156 GtkWidget
*copy_item
;
157 GtkWidget
*zoom_in_item
;
158 GtkWidget
*zoom_out_item
;
159 GtkWidget
*zoom_fixed_item
;
160 GtkWidget
*zoom_fit_item
;
161 GtkWidget
*grab_item
;
162 GtkWidget
*grab_on_hover_item
;
165 VirtualConsole vc
[MAX_VCS
];
167 GtkWidget
*show_tabs_item
;
168 GtkWidget
*untabify_item
;
169 GtkWidget
*show_menubar_item
;
179 VirtualConsole
*kbd_owner
;
180 VirtualConsole
*ptr_owner
;
182 gboolean full_screen
;
184 GdkCursor
*null_cursor
;
185 Notifier mouse_mode_notifier
;
188 bool external_pause_update
;
190 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
193 DisplayOptions
*opts
;
196 typedef struct VCChardev
{
198 VirtualConsole
*console
;
202 #define TYPE_CHARDEV_VC "chardev-vc"
203 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
205 bool gtk_use_gl_area
;
207 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
208 static void gd_ungrab_pointer(GtkDisplayState
*s
);
209 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
210 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
212 /** Utility Functions **/
214 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
219 for (i
= 0; i
< s
->nb_vcs
; i
++) {
221 if (gtk_check_menu_item_get_active
222 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
229 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
234 for (i
= 0; i
< s
->nb_vcs
; i
++) {
236 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
244 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
248 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
249 return gd_vc_find_by_page(s
, page
);
252 static bool gd_is_grab_active(GtkDisplayState
*s
)
254 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
257 static bool gd_grab_on_hover(GtkDisplayState
*s
)
259 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
262 static void gd_update_cursor(VirtualConsole
*vc
)
264 GtkDisplayState
*s
= vc
->s
;
267 if (vc
->type
!= GD_VC_GFX
||
268 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
272 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
276 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
277 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
278 gdk_window_set_cursor(window
, s
->null_cursor
);
280 gdk_window_set_cursor(window
, NULL
);
284 static void gd_update_caption(GtkDisplayState
*s
)
286 const char *status
= "";
289 const char *grab
= "";
290 bool is_paused
= !runstate_is_running();
294 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
296 prefix
= g_strdup_printf("QEMU");
299 if (s
->ptr_owner
!= NULL
&&
300 s
->ptr_owner
->window
== NULL
) {
301 grab
= _(" - Press Ctrl+Alt+G to release grab");
305 status
= _(" [Paused]");
307 s
->external_pause_update
= true;
308 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
310 s
->external_pause_update
= false;
312 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
313 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
316 for (i
= 0; i
< s
->nb_vcs
; i
++) {
317 VirtualConsole
*vc
= &s
->vc
[i
];
322 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
323 vc
== s
->kbd_owner
? " +kbd" : "",
324 vc
== s
->ptr_owner
? " +ptr" : "");
325 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
332 static void gd_update_geometry_hints(VirtualConsole
*vc
)
334 GtkDisplayState
*s
= vc
->s
;
335 GdkWindowHints mask
= 0;
336 GdkGeometry geo
= {};
337 GtkWidget
*geo_widget
= NULL
;
338 GtkWindow
*geo_window
;
340 if (vc
->type
== GD_VC_GFX
) {
345 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
346 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
347 mask
|= GDK_HINT_MIN_SIZE
;
349 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
350 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
351 mask
|= GDK_HINT_MIN_SIZE
;
353 geo_widget
= vc
->gfx
.drawing_area
;
354 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
356 #if defined(CONFIG_VTE)
357 } else if (vc
->type
== GD_VC_VTE
) {
358 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
359 GtkBorder padding
= { 0 };
361 #if VTE_CHECK_VERSION(0, 37, 0)
362 gtk_style_context_get_padding(
363 gtk_widget_get_style_context(vc
->vte
.terminal
),
364 gtk_widget_get_state_flags(vc
->vte
.terminal
),
368 GtkBorder
*ib
= NULL
;
369 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
377 geo
.width_inc
= vte_terminal_get_char_width(term
);
378 geo
.height_inc
= vte_terminal_get_char_height(term
);
379 mask
|= GDK_HINT_RESIZE_INC
;
380 geo
.base_width
= geo
.width_inc
;
381 geo
.base_height
= geo
.height_inc
;
382 mask
|= GDK_HINT_BASE_SIZE
;
383 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
384 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
385 mask
|= GDK_HINT_MIN_SIZE
;
387 geo
.base_width
+= padding
.left
+ padding
.right
;
388 geo
.base_height
+= padding
.top
+ padding
.bottom
;
389 geo
.min_width
+= padding
.left
+ padding
.right
;
390 geo
.min_height
+= padding
.top
+ padding
.bottom
;
391 geo_widget
= vc
->vte
.terminal
;
395 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
396 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
399 void gd_update_windowsize(VirtualConsole
*vc
)
401 GtkDisplayState
*s
= vc
->s
;
403 gd_update_geometry_hints(vc
);
405 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
406 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
407 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
411 static void gd_update_full_redraw(VirtualConsole
*vc
)
413 GtkWidget
*area
= vc
->gfx
.drawing_area
;
415 ww
= gdk_window_get_width(gtk_widget_get_window(area
));
416 wh
= gdk_window_get_height(gtk_widget_get_window(area
));
417 #if defined(CONFIG_GTK_GL)
418 if (vc
->gfx
.gls
&& gtk_use_gl_area
) {
419 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
423 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
426 static void gtk_release_modifiers(GtkDisplayState
*s
)
428 VirtualConsole
*vc
= gd_vc_find_current(s
);
431 if (vc
->type
!= GD_VC_GFX
||
432 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
435 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
436 qcode
= modifier_keycode
[i
];
437 if (!s
->modifier_pressed
[i
]) {
440 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, qcode
, false);
441 s
->modifier_pressed
[i
] = false;
445 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
448 g_object_ref(G_OBJECT(widget
));
449 gtk_container_remove(GTK_CONTAINER(from
), widget
);
450 gtk_container_add(GTK_CONTAINER(to
), widget
);
451 g_object_unref(G_OBJECT(widget
));
454 /** DisplayState Callbacks **/
456 static void gd_update(DisplayChangeListener
*dcl
,
457 int x
, int y
, int w
, int h
)
459 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
466 trace_gd_update(vc
->label
, x
, y
, w
, h
);
468 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
472 if (vc
->gfx
.convert
) {
473 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
474 NULL
, vc
->gfx
.convert
,
475 x
, y
, 0, 0, x
, y
, w
, h
);
478 x1
= floor(x
* vc
->gfx
.scale_x
);
479 y1
= floor(y
* vc
->gfx
.scale_y
);
481 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
482 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
484 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
485 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
487 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
491 ww
= gdk_window_get_width(win
);
492 wh
= gdk_window_get_height(win
);
502 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
503 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
506 static void gd_refresh(DisplayChangeListener
*dcl
)
508 graphic_hw_update(dcl
->con
);
511 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
513 #if GTK_CHECK_VERSION(3, 20, 0)
514 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
516 return gdk_device_manager_get_client_pointer(
517 gdk_display_get_device_manager(dpy
));
521 static void gd_mouse_set(DisplayChangeListener
*dcl
,
522 int x
, int y
, int visible
)
524 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
528 if (qemu_input_is_absolute()) {
532 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
533 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
534 x
, y
, &x_root
, &y_root
);
535 gdk_device_warp(gd_get_pointer(dpy
),
536 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
542 static void gd_cursor_define(DisplayChangeListener
*dcl
,
545 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
549 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
553 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
554 GDK_COLORSPACE_RGB
, true, 8,
555 c
->width
, c
->height
, c
->width
* 4,
557 cursor
= gdk_cursor_new_from_pixbuf
558 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
559 pixbuf
, c
->hot_x
, c
->hot_y
);
560 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
561 g_object_unref(pixbuf
);
562 g_object_unref(cursor
);
565 static void gd_switch(DisplayChangeListener
*dcl
,
566 DisplaySurface
*surface
)
568 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
571 trace_gd_switch(vc
->label
,
572 surface
? surface_width(surface
) : 0,
573 surface
? surface_height(surface
) : 0);
575 if (vc
->gfx
.surface
) {
576 cairo_surface_destroy(vc
->gfx
.surface
);
577 vc
->gfx
.surface
= NULL
;
579 if (vc
->gfx
.convert
) {
580 pixman_image_unref(vc
->gfx
.convert
);
581 vc
->gfx
.convert
= NULL
;
584 if (vc
->gfx
.ds
&& surface
&&
585 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
586 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
589 vc
->gfx
.ds
= surface
;
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
);
634 static const DisplayChangeListenerOps dcl_ops
= {
636 .dpy_gfx_update
= gd_update
,
637 .dpy_gfx_switch
= gd_switch
,
638 .dpy_gfx_check_format
= qemu_pixman_check_format
,
639 .dpy_refresh
= gd_refresh
,
640 .dpy_mouse_set
= gd_mouse_set
,
641 .dpy_cursor_define
= gd_cursor_define
,
645 #if defined(CONFIG_OPENGL)
647 /** DisplayState Callbacks (opengl version) **/
649 #if defined(CONFIG_GTK_GL)
651 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
652 .dpy_name
= "gtk-egl",
653 .dpy_gfx_update
= gd_gl_area_update
,
654 .dpy_gfx_switch
= gd_gl_area_switch
,
655 .dpy_gfx_check_format
= console_gl_check_format
,
656 .dpy_refresh
= gd_gl_area_refresh
,
657 .dpy_mouse_set
= gd_mouse_set
,
658 .dpy_cursor_define
= gd_cursor_define
,
660 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
661 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
662 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
663 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
664 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
665 .dpy_gl_update
= gd_gl_area_scanout_flush
,
668 #endif /* CONFIG_GTK_GL */
670 static const DisplayChangeListenerOps dcl_egl_ops
= {
671 .dpy_name
= "gtk-egl",
672 .dpy_gfx_update
= gd_egl_update
,
673 .dpy_gfx_switch
= gd_egl_switch
,
674 .dpy_gfx_check_format
= console_gl_check_format
,
675 .dpy_refresh
= gd_egl_refresh
,
676 .dpy_mouse_set
= gd_mouse_set
,
677 .dpy_cursor_define
= gd_cursor_define
,
679 .dpy_gl_ctx_create
= gd_egl_create_context
,
680 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
681 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
682 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
683 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
684 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
685 .dpy_gl_scanout_dmabuf
= gd_egl_scanout_dmabuf
,
686 .dpy_gl_cursor_dmabuf
= gd_egl_cursor_dmabuf
,
687 .dpy_gl_cursor_position
= gd_egl_cursor_position
,
688 .dpy_gl_release_dmabuf
= gd_egl_release_dmabuf
,
689 .dpy_gl_update
= gd_egl_scanout_flush
,
692 #endif /* CONFIG_OPENGL */
696 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
698 GtkDisplayState
*s
= opaque
;
700 gd_update_caption(s
);
703 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
708 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
709 /* release the grab at switching to absolute mode */
710 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
711 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
714 for (i
= 0; i
< s
->nb_vcs
; i
++) {
715 VirtualConsole
*vc
= &s
->vc
[i
];
716 gd_update_cursor(vc
);
722 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
725 GtkDisplayState
*s
= opaque
;
726 bool allow_close
= true;
728 if (s
->opts
->has_window_close
&& !s
->opts
->window_close
) {
739 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
743 memset(&info
, 0, sizeof(info
));
745 info
.height
= height
;
746 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
749 #if defined(CONFIG_GTK_GL)
751 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
754 VirtualConsole
*vc
= opaque
;
762 static void gd_resize_event(GtkGLArea
*area
,
763 gint width
, gint height
, gpointer
*opaque
)
765 VirtualConsole
*vc
= (void *)opaque
;
767 gd_set_ui_info(vc
, width
, height
);
772 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
774 VirtualConsole
*vc
= opaque
;
775 GtkDisplayState
*s
= vc
->s
;
780 #if defined(CONFIG_OPENGL)
782 if (gtk_use_gl_area
) {
783 /* invoke render callback please */
792 if (!gtk_widget_get_realized(widget
)) {
799 fbw
= surface_width(vc
->gfx
.ds
);
800 fbh
= surface_height(vc
->gfx
.ds
);
802 ww
= gdk_window_get_width(gtk_widget_get_window(widget
));
803 wh
= gdk_window_get_height(gtk_widget_get_window(widget
));
805 if (s
->full_screen
) {
806 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
807 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
808 } else if (s
->free_scale
) {
811 sx
= (double)ww
/ fbw
;
812 sy
= (double)wh
/ fbh
;
814 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
817 fbw
*= vc
->gfx
.scale_x
;
818 fbh
*= vc
->gfx
.scale_y
;
828 cairo_rectangle(cr
, 0, 0, ww
, wh
);
830 /* Optionally cut out the inner area where the pixmap
831 will be drawn. This avoids 'flashing' since we're
832 not double-buffering. Note we're using the undocumented
833 behaviour of drawing the rectangle from right to left
834 to cut out the whole */
835 cairo_rectangle(cr
, mx
+ fbw
, my
,
839 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
840 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
841 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
847 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
850 VirtualConsole
*vc
= opaque
;
851 GtkDisplayState
*s
= vc
->s
;
861 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
862 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
864 ww
= gdk_window_get_width(gtk_widget_get_window(vc
->gfx
.drawing_area
));
865 wh
= gdk_window_get_height(gtk_widget_get_window(vc
->gfx
.drawing_area
));
875 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
876 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
878 if (qemu_input_is_absolute()) {
879 if (x
< 0 || y
< 0 ||
880 x
>= surface_width(vc
->gfx
.ds
) ||
881 y
>= surface_height(vc
->gfx
.ds
)) {
884 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
885 0, surface_width(vc
->gfx
.ds
));
886 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
887 0, surface_height(vc
->gfx
.ds
));
888 qemu_input_event_sync();
889 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
890 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
891 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
892 qemu_input_event_sync();
898 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
899 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
900 int screen_width
, screen_height
;
902 int x
= (int)motion
->x_root
;
903 int y
= (int)motion
->y_root
;
905 #if GTK_CHECK_VERSION(3, 22, 0)
907 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
908 GdkWindow
*win
= gtk_widget_get_window(widget
);
909 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
910 GdkRectangle geometry
;
911 gdk_monitor_get_geometry(monitor
, &geometry
);
912 screen_width
= geometry
.width
;
913 screen_height
= geometry
.height
;
917 screen_width
= gdk_screen_get_width(screen
);
918 screen_height
= gdk_screen_get_height(screen
);
922 /* In relative mode check to see if client pointer hit
923 * one of the screen edges, and if so move it back by
924 * 200 pixels. This is important because the pointer
925 * in the server doesn't correspond 1-for-1, and so
926 * may still be only half way across the screen. Without
927 * this warp, the server pointer would thus appear to hit
928 * an invisible wall */
935 if (x
== (screen_width
- 1)) {
938 if (y
== (screen_height
- 1)) {
942 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
943 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
944 gdk_device_warp(dev
, screen
, x
, y
);
952 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
955 VirtualConsole
*vc
= opaque
;
956 GtkDisplayState
*s
= vc
->s
;
959 /* implicitly grab the input at the first click in the relative mode */
960 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
961 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
963 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
966 gd_grab_pointer(vc
, "relative-mode-click");
971 if (button
->button
== 1) {
972 btn
= INPUT_BUTTON_LEFT
;
973 } else if (button
->button
== 2) {
974 btn
= INPUT_BUTTON_MIDDLE
;
975 } else if (button
->button
== 3) {
976 btn
= INPUT_BUTTON_RIGHT
;
977 } else if (button
->button
== 8) {
978 btn
= INPUT_BUTTON_SIDE
;
979 } else if (button
->button
== 9) {
980 btn
= INPUT_BUTTON_EXTRA
;
985 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
986 button
->type
== GDK_BUTTON_PRESS
);
987 qemu_input_event_sync();
991 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
994 VirtualConsole
*vc
= opaque
;
997 if (scroll
->direction
== GDK_SCROLL_UP
) {
998 btn
= INPUT_BUTTON_WHEEL_UP
;
999 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
1000 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1001 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
1002 gdouble delta_x
, delta_y
;
1003 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
1004 &delta_x
, &delta_y
)) {
1008 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1010 btn
= INPUT_BUTTON_WHEEL_UP
;
1016 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
1017 qemu_input_event_sync();
1018 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
1019 qemu_input_event_sync();
1024 static const guint16
*gd_get_keymap(size_t *maplen
)
1026 GdkDisplay
*dpy
= gdk_display_get_default();
1028 #ifdef GDK_WINDOWING_X11
1029 if (GDK_IS_X11_DISPLAY(dpy
)) {
1030 trace_gd_keymap_windowing("x11");
1031 return qemu_xkeymap_mapping_table(
1032 gdk_x11_display_get_xdisplay(dpy
), maplen
);
1036 #ifdef GDK_WINDOWING_WAYLAND
1037 if (GDK_IS_WAYLAND_DISPLAY(dpy
)) {
1038 trace_gd_keymap_windowing("wayland");
1039 *maplen
= qemu_input_map_xorgevdev_to_qcode_len
;
1040 return qemu_input_map_xorgevdev_to_qcode
;
1044 #ifdef GDK_WINDOWING_WIN32
1045 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1046 trace_gd_keymap_windowing("win32");
1047 *maplen
= qemu_input_map_win32_to_qcode_len
;
1048 return qemu_input_map_win32_to_qcode
;
1052 #ifdef GDK_WINDOWING_QUARTZ
1053 if (GDK_IS_QUARTZ_DISPLAY(dpy
)) {
1054 trace_gd_keymap_windowing("quartz");
1055 *maplen
= qemu_input_map_osx_to_qcode_len
;
1056 return qemu_input_map_osx_to_qcode
;
1060 #ifdef GDK_WINDOWING_BROADWAY
1061 if (GDK_IS_BROADWAY_DISPLAY(dpy
)) {
1062 trace_gd_keymap_windowing("broadway");
1063 g_warning("experimental: using broadway, x11 virtual keysym\n"
1064 "mapping - with very limited support. See also\n"
1065 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1066 *maplen
= qemu_input_map_x11_to_qcode_len
;
1067 return qemu_input_map_x11_to_qcode
;
1071 g_warning("Unsupported GDK Windowing platform.\n"
1072 "Disabling extended keycode tables.\n"
1073 "Please report to qemu-devel@nongnu.org\n"
1074 "including the following information:\n"
1076 " - Operating system\n"
1077 " - GDK Windowing system build\n");
1082 static int gd_map_keycode(int scancode
)
1087 if (scancode
> keycode_maplen
) {
1091 return keycode_map
[scancode
];
1094 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1095 GdkEventKey
*key
, void *opaque
)
1097 VirtualConsole
*vc
= opaque
;
1098 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1100 if (key
->keyval
== GDK_KEY_Delete
) {
1101 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
, false);
1102 } else if (key
->length
) {
1103 kbd_put_string_console(con
, key
->string
, key
->length
);
1105 int qcode
= gd_map_keycode(key
->hardware_keycode
);
1106 kbd_put_qcode_console(con
, qcode
, false);
1111 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1113 VirtualConsole
*vc
= opaque
;
1114 GtkDisplayState
*s
= vc
->s
;
1118 if (s
->ignore_keys
) {
1119 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1124 /* on windows, we ought to ignore the reserved key event? */
1125 if (key
->hardware_keycode
== 0xff)
1129 if (key
->keyval
== GDK_KEY_Pause
1131 /* for some reason GDK does not fill keyval for VK_PAUSE
1132 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1134 || key
->hardware_keycode
== VK_PAUSE
1137 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
1138 key
->type
== GDK_KEY_PRESS
);
1142 qcode
= gd_map_keycode(key
->hardware_keycode
);
1144 trace_gd_key_event(vc
->label
, key
->hardware_keycode
, qcode
,
1145 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1147 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
1148 if (qcode
== modifier_keycode
[i
]) {
1149 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
1153 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, qcode
,
1154 key
->type
== GDK_KEY_PRESS
);
1159 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1161 if (event
->type
== GDK_MOTION_NOTIFY
) {
1162 return gd_motion_event(widget
, &event
->motion
, opaque
);
1167 /** Window Menu Actions **/
1169 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1171 GtkDisplayState
*s
= opaque
;
1173 if (s
->external_pause_update
) {
1176 if (runstate_is_running()) {
1183 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1185 qmp_system_reset(NULL
);
1188 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1190 qmp_system_powerdown(NULL
);
1193 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1198 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1200 GtkDisplayState
*s
= opaque
;
1201 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1202 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1205 gtk_release_modifiers(s
);
1207 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1208 gtk_notebook_set_current_page(nb
, page
);
1209 gtk_widget_grab_focus(vc
->focus
);
1211 s
->ignore_keys
= false;
1214 static void gd_accel_switch_vc(void *opaque
)
1216 VirtualConsole
*vc
= opaque
;
1218 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1221 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1223 GtkDisplayState
*s
= opaque
;
1224 VirtualConsole
*vc
= gd_vc_find_current(s
);
1226 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1227 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1229 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1231 gd_update_windowsize(vc
);
1234 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1237 VirtualConsole
*vc
= opaque
;
1238 GtkDisplayState
*s
= vc
->s
;
1240 gtk_widget_set_sensitive(vc
->menu_item
, true);
1241 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1242 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1243 vc
->tab_item
, vc
->label
);
1244 gtk_widget_destroy(vc
->window
);
1249 static gboolean
gd_win_grab(void *opaque
)
1251 VirtualConsole
*vc
= opaque
;
1253 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1254 if (vc
->s
->ptr_owner
) {
1255 gd_ungrab_pointer(vc
->s
);
1257 gd_grab_pointer(vc
, "user-request-detached-tab");
1262 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1264 GtkDisplayState
*s
= opaque
;
1265 VirtualConsole
*vc
= gd_vc_find_current(s
);
1267 if (vc
->type
== GD_VC_GFX
&&
1268 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1269 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1273 gtk_widget_set_sensitive(vc
->menu_item
, false);
1274 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1275 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1277 g_signal_connect(vc
->window
, "delete-event",
1278 G_CALLBACK(gd_tab_window_close
), vc
);
1279 gtk_widget_show_all(vc
->window
);
1281 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1282 GtkAccelGroup
*ag
= gtk_accel_group_new();
1283 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1285 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1287 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1290 gd_update_geometry_hints(vc
);
1291 gd_update_caption(s
);
1295 static void gd_menu_show_menubar(GtkMenuItem
*item
, void *opaque
)
1297 GtkDisplayState
*s
= opaque
;
1298 VirtualConsole
*vc
= gd_vc_find_current(s
);
1300 if (s
->full_screen
) {
1304 if (gtk_check_menu_item_get_active(
1305 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1306 gtk_widget_show(s
->menu_bar
);
1308 gtk_widget_hide(s
->menu_bar
);
1310 gd_update_windowsize(vc
);
1313 static void gd_accel_show_menubar(void *opaque
)
1315 GtkDisplayState
*s
= opaque
;
1316 gtk_menu_item_activate(GTK_MENU_ITEM(s
->show_menubar_item
));
1319 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1321 GtkDisplayState
*s
= opaque
;
1322 VirtualConsole
*vc
= gd_vc_find_current(s
);
1324 if (!s
->full_screen
) {
1325 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1326 gtk_widget_hide(s
->menu_bar
);
1327 if (vc
->type
== GD_VC_GFX
) {
1328 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1330 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1331 s
->full_screen
= TRUE
;
1333 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1334 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1335 if (gtk_check_menu_item_get_active(
1336 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1337 gtk_widget_show(s
->menu_bar
);
1339 s
->full_screen
= FALSE
;
1340 if (vc
->type
== GD_VC_GFX
) {
1341 vc
->gfx
.scale_x
= 1.0;
1342 vc
->gfx
.scale_y
= 1.0;
1343 gd_update_windowsize(vc
);
1347 gd_update_cursor(vc
);
1350 static void gd_accel_full_screen(void *opaque
)
1352 GtkDisplayState
*s
= opaque
;
1353 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1356 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1358 GtkDisplayState
*s
= opaque
;
1359 VirtualConsole
*vc
= gd_vc_find_current(s
);
1361 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1364 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1365 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1367 gd_update_windowsize(vc
);
1370 static void gd_accel_zoom_in(void *opaque
)
1372 GtkDisplayState
*s
= opaque
;
1373 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1376 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1378 GtkDisplayState
*s
= opaque
;
1379 VirtualConsole
*vc
= gd_vc_find_current(s
);
1381 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1384 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1385 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1387 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1388 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1390 gd_update_windowsize(vc
);
1393 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1395 GtkDisplayState
*s
= opaque
;
1396 VirtualConsole
*vc
= gd_vc_find_current(s
);
1398 vc
->gfx
.scale_x
= 1.0;
1399 vc
->gfx
.scale_y
= 1.0;
1401 gd_update_windowsize(vc
);
1404 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1406 GtkDisplayState
*s
= opaque
;
1407 VirtualConsole
*vc
= gd_vc_find_current(s
);
1409 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1410 s
->free_scale
= TRUE
;
1412 s
->free_scale
= FALSE
;
1413 vc
->gfx
.scale_x
= 1.0;
1414 vc
->gfx
.scale_y
= 1.0;
1417 gd_update_windowsize(vc
);
1418 gd_update_full_redraw(vc
);
1421 #if GTK_CHECK_VERSION(3, 20, 0)
1422 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1424 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1425 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1426 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1427 GdkSeatCapabilities caps
= 0;
1428 GdkCursor
*cursor
= NULL
;
1431 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1434 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1435 cursor
= vc
->s
->null_cursor
;
1439 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1442 gdk_seat_ungrab(seat
);
1446 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1447 GdkInputSource source
, GdkEventMask mask
,
1450 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1451 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1452 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1455 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1456 GdkDevice
*dev
= tmp
->data
;
1457 if (gdk_device_get_source(dev
) != source
) {
1461 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1462 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1463 mask
, cursor
, GDK_CURRENT_TIME
);
1465 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1472 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1474 if (vc
->s
->kbd_owner
) {
1475 if (vc
->s
->kbd_owner
== vc
) {
1478 gd_ungrab_keyboard(vc
->s
);
1482 #if GTK_CHECK_VERSION(3, 20, 0)
1483 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1485 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1486 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1489 vc
->s
->kbd_owner
= vc
;
1490 gd_update_caption(vc
->s
);
1491 trace_gd_grab(vc
->label
, "kbd", reason
);
1494 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1496 VirtualConsole
*vc
= s
->kbd_owner
;
1501 s
->kbd_owner
= NULL
;
1503 #if GTK_CHECK_VERSION(3, 20, 0)
1504 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1506 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1508 gd_update_caption(s
);
1509 trace_gd_ungrab(vc
->label
, "kbd");
1512 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1514 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1516 if (vc
->s
->ptr_owner
) {
1517 if (vc
->s
->ptr_owner
== vc
) {
1520 gd_ungrab_pointer(vc
->s
);
1524 #if GTK_CHECK_VERSION(3, 20, 0)
1525 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1526 gdk_device_get_position(gd_get_pointer(display
),
1527 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1529 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1530 GDK_POINTER_MOTION_MASK
|
1531 GDK_BUTTON_PRESS_MASK
|
1532 GDK_BUTTON_RELEASE_MASK
|
1533 GDK_BUTTON_MOTION_MASK
|
1535 vc
->s
->null_cursor
);
1536 gdk_device_get_position(gd_get_pointer(display
),
1537 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1539 vc
->s
->ptr_owner
= vc
;
1540 gd_update_caption(vc
->s
);
1541 trace_gd_grab(vc
->label
, "ptr", reason
);
1544 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1546 VirtualConsole
*vc
= s
->ptr_owner
;
1547 GdkDisplay
*display
;
1552 s
->ptr_owner
= NULL
;
1554 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1555 #if GTK_CHECK_VERSION(3, 20, 0)
1556 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1557 gdk_device_warp(gd_get_pointer(display
),
1558 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1559 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1561 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1562 gdk_device_warp(gd_get_pointer(display
),
1563 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1564 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1566 gd_update_caption(s
);
1567 trace_gd_ungrab(vc
->label
, "ptr");
1570 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1572 GtkDisplayState
*s
= opaque
;
1573 VirtualConsole
*vc
= gd_vc_find_current(s
);
1575 if (gd_is_grab_active(s
)) {
1576 gd_grab_keyboard(vc
, "user-request-main-window");
1577 gd_grab_pointer(vc
, "user-request-main-window");
1579 gd_ungrab_keyboard(s
);
1580 gd_ungrab_pointer(s
);
1583 gd_update_cursor(vc
);
1586 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1589 GtkDisplayState
*s
= data
;
1593 if (!gtk_widget_get_realized(s
->notebook
)) {
1597 vc
= gd_vc_find_by_page(s
, arg2
);
1601 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1603 on_vga
= (vc
->type
== GD_VC_GFX
&&
1604 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1606 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1608 } else if (s
->full_screen
) {
1609 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1612 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1614 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1617 gd_update_windowsize(vc
);
1618 gd_update_cursor(vc
);
1621 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1624 VirtualConsole
*vc
= opaque
;
1625 GtkDisplayState
*s
= vc
->s
;
1627 if (gd_grab_on_hover(s
)) {
1628 gd_grab_keyboard(vc
, "grab-on-hover");
1633 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1636 VirtualConsole
*vc
= opaque
;
1637 GtkDisplayState
*s
= vc
->s
;
1639 if (gd_grab_on_hover(s
)) {
1640 gd_ungrab_keyboard(s
);
1645 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1646 GdkEventCrossing
*crossing
, gpointer opaque
)
1648 VirtualConsole
*vc
= opaque
;
1649 GtkDisplayState
*s
= vc
->s
;
1651 gtk_release_modifiers(s
);
1655 static gboolean
gd_configure(GtkWidget
*widget
,
1656 GdkEventConfigure
*cfg
, gpointer opaque
)
1658 VirtualConsole
*vc
= opaque
;
1660 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1664 /** Virtual Console Callbacks **/
1666 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1667 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1669 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1670 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1671 HOTKEY_MODIFIERS
, 0,
1672 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1673 gtk_accel_label_set_accel(
1674 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1675 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1677 g_signal_connect(vc
->menu_item
, "activate",
1678 G_CALLBACK(gd_menu_switch_vc
), s
);
1679 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1681 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1685 #if defined(CONFIG_VTE)
1686 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1688 GtkDisplayState
*s
= opaque
;
1689 VirtualConsole
*vc
= gd_vc_find_current(s
);
1691 #if VTE_CHECK_VERSION(0, 50, 0)
1692 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1695 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1699 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1701 VirtualConsole
*vc
= opaque
;
1703 if (gtk_adjustment_get_upper(adjustment
) >
1704 gtk_adjustment_get_page_size(adjustment
)) {
1705 gtk_widget_show(vc
->vte
.scrollbar
);
1707 gtk_widget_hide(vc
->vte
.scrollbar
);
1711 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1713 VCChardev
*vcd
= VC_CHARDEV(chr
);
1714 VirtualConsole
*vc
= vcd
->console
;
1716 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1720 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1722 VCChardev
*vcd
= VC_CHARDEV(chr
);
1723 VirtualConsole
*vc
= vcd
->console
;
1726 vc
->vte
.echo
= echo
;
1733 static Chardev
*vcs
[MAX_VCS
];
1734 static void gd_vc_open(Chardev
*chr
,
1735 ChardevBackend
*backend
,
1739 if (nb_vcs
== MAX_VCS
) {
1740 error_setg(errp
, "Maximum number of consoles reached");
1744 vcs
[nb_vcs
++] = chr
;
1746 /* console/chardev init sometimes completes elsewhere in a 2nd
1747 * stage, so defer OPENED events until they are fully initialized
1752 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1754 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1756 cc
->parse
= qemu_chr_parse_vc
;
1757 cc
->open
= gd_vc_open
;
1758 cc
->chr_write
= gd_vc_chr_write
;
1759 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1762 static const TypeInfo char_gd_vc_type_info
= {
1763 .name
= TYPE_CHARDEV_VC
,
1764 .parent
= TYPE_CHARDEV
,
1765 .instance_size
= sizeof(VCChardev
),
1766 .class_init
= char_gd_vc_class_init
,
1769 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1772 VirtualConsole
*vc
= user_data
;
1775 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1777 for (i
= 0; i
< size
; i
++) {
1778 uint8_t c
= text
[i
];
1779 if (c
>= 128 || isprint(c
)) {
1780 /* 8-bit characters are considered printable. */
1781 vte_terminal_feed(term
, &text
[i
], 1);
1782 } else if (c
== '\r' || c
== '\n') {
1783 vte_terminal_feed(term
, "\r\n", 2);
1785 char ctrl
[2] = { '^', 0};
1786 ctrl
[1] = text
[i
] ^ 64;
1787 vte_terminal_feed(term
, ctrl
, 2);
1792 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1796 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1797 Chardev
*chr
, int idx
,
1798 GSList
*group
, GtkWidget
*view_menu
)
1802 GtkWidget
*scrollbar
;
1803 GtkAdjustment
*vadjustment
;
1804 VCChardev
*vcd
= VC_CHARDEV(chr
);
1807 vc
->vte
.echo
= vcd
->echo
;
1811 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1812 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1813 ? vc
->vte
.chr
->label
: buffer
);
1814 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1816 vc
->vte
.terminal
= vte_terminal_new();
1817 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1819 /* The documentation says that the default is UTF-8, but actually it is
1820 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1821 * VTE 0.54 (only UTF-8 is supported now). */
1822 #if !VTE_CHECK_VERSION(0, 54, 0)
1823 #if VTE_CHECK_VERSION(0, 38, 0)
1824 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1826 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1830 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1831 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1832 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1834 #if VTE_CHECK_VERSION(0, 28, 0)
1835 vadjustment
= gtk_scrollable_get_vadjustment
1836 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1838 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1841 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1842 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1844 gtk_box_pack_end(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1845 gtk_box_pack_end(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1848 vc
->vte
.scrollbar
= scrollbar
;
1850 g_signal_connect(vadjustment
, "changed",
1851 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1853 vc
->type
= GD_VC_VTE
;
1855 vc
->focus
= vc
->vte
.terminal
;
1856 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1857 gtk_label_new(vc
->label
));
1859 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1864 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1865 GtkWidget
*view_menu
)
1869 for (i
= 0; i
< nb_vcs
; i
++) {
1870 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1871 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1875 #endif /* CONFIG_VTE */
1877 /** Window Creation **/
1879 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1881 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1882 G_CALLBACK(gd_draw_event
), vc
);
1883 #if defined(CONFIG_GTK_GL)
1884 if (gtk_use_gl_area
) {
1885 /* wire up GtkGlArea events */
1886 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1887 G_CALLBACK(gd_render_event
), vc
);
1888 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1889 G_CALLBACK(gd_resize_event
), vc
);
1892 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1893 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1894 G_CALLBACK(gd_event
), vc
);
1895 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1896 G_CALLBACK(gd_button_event
), vc
);
1897 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1898 G_CALLBACK(gd_button_event
), vc
);
1899 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1900 G_CALLBACK(gd_scroll_event
), vc
);
1901 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1902 G_CALLBACK(gd_key_event
), vc
);
1903 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1904 G_CALLBACK(gd_key_event
), vc
);
1906 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1907 G_CALLBACK(gd_enter_event
), vc
);
1908 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1909 G_CALLBACK(gd_leave_event
), vc
);
1910 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1911 G_CALLBACK(gd_focus_out_event
), vc
);
1912 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1913 G_CALLBACK(gd_configure
), vc
);
1915 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1916 G_CALLBACK(gd_text_key_down
), vc
);
1920 static void gd_connect_signals(GtkDisplayState
*s
)
1922 g_signal_connect(s
->show_tabs_item
, "activate",
1923 G_CALLBACK(gd_menu_show_tabs
), s
);
1924 g_signal_connect(s
->untabify_item
, "activate",
1925 G_CALLBACK(gd_menu_untabify
), s
);
1926 g_signal_connect(s
->show_menubar_item
, "activate",
1927 G_CALLBACK(gd_menu_show_menubar
), s
);
1929 g_signal_connect(s
->window
, "delete-event",
1930 G_CALLBACK(gd_window_close
), s
);
1932 g_signal_connect(s
->pause_item
, "activate",
1933 G_CALLBACK(gd_menu_pause
), s
);
1934 g_signal_connect(s
->reset_item
, "activate",
1935 G_CALLBACK(gd_menu_reset
), s
);
1936 g_signal_connect(s
->powerdown_item
, "activate",
1937 G_CALLBACK(gd_menu_powerdown
), s
);
1938 g_signal_connect(s
->quit_item
, "activate",
1939 G_CALLBACK(gd_menu_quit
), s
);
1940 #if defined(CONFIG_VTE)
1941 g_signal_connect(s
->copy_item
, "activate",
1942 G_CALLBACK(gd_menu_copy
), s
);
1944 g_signal_connect(s
->full_screen_item
, "activate",
1945 G_CALLBACK(gd_menu_full_screen
), s
);
1946 g_signal_connect(s
->zoom_in_item
, "activate",
1947 G_CALLBACK(gd_menu_zoom_in
), s
);
1948 g_signal_connect(s
->zoom_out_item
, "activate",
1949 G_CALLBACK(gd_menu_zoom_out
), s
);
1950 g_signal_connect(s
->zoom_fixed_item
, "activate",
1951 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1952 g_signal_connect(s
->zoom_fit_item
, "activate",
1953 G_CALLBACK(gd_menu_zoom_fit
), s
);
1954 g_signal_connect(s
->grab_item
, "activate",
1955 G_CALLBACK(gd_menu_grab_input
), s
);
1956 g_signal_connect(s
->notebook
, "switch-page",
1957 G_CALLBACK(gd_change_page
), s
);
1960 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1962 GtkWidget
*machine_menu
;
1963 GtkWidget
*separator
;
1965 machine_menu
= gtk_menu_new();
1966 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1968 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1969 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1971 separator
= gtk_separator_menu_item_new();
1972 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1974 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1975 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1977 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1978 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1980 separator
= gtk_separator_menu_item_new();
1981 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1983 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1984 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1985 "<QEMU>/Machine/Quit");
1986 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1987 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1988 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1990 return machine_menu
;
1993 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1994 QemuConsole
*con
, int idx
,
1995 GSList
*group
, GtkWidget
*view_menu
)
1997 bool zoom_to_fit
= false;
1999 vc
->label
= qemu_console_get_label(con
);
2001 vc
->gfx
.scale_x
= 1.0;
2002 vc
->gfx
.scale_y
= 1.0;
2004 #if defined(CONFIG_OPENGL)
2005 if (display_opengl
) {
2006 #if defined(CONFIG_GTK_GL)
2007 if (gtk_use_gl_area
) {
2008 vc
->gfx
.drawing_area
= gtk_gl_area_new();
2009 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
2011 #endif /* CONFIG_GTK_GL */
2013 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2015 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2016 * It is required for opengl rendering on X11 though. A
2017 * proper replacement (native opengl support) is only
2018 * available in 3.16+. Silence the warning if possible.
2020 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2021 #pragma GCC diagnostic push
2022 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2024 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2025 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2026 #pragma GCC diagnostic pop
2028 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2033 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2034 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2038 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2039 GDK_POINTER_MOTION_MASK
|
2040 GDK_BUTTON_PRESS_MASK
|
2041 GDK_BUTTON_RELEASE_MASK
|
2042 GDK_BUTTON_MOTION_MASK
|
2043 GDK_ENTER_NOTIFY_MASK
|
2044 GDK_LEAVE_NOTIFY_MASK
|
2046 GDK_KEY_PRESS_MASK
);
2047 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2049 vc
->type
= GD_VC_GFX
;
2050 vc
->tab_item
= vc
->gfx
.drawing_area
;
2051 vc
->focus
= vc
->gfx
.drawing_area
;
2052 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2053 vc
->tab_item
, gtk_label_new(vc
->label
));
2055 vc
->gfx
.dcl
.con
= con
;
2056 register_displaychangelistener(&vc
->gfx
.dcl
);
2058 gd_connect_vc_gfx_signals(vc
);
2059 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2061 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2064 if (s
->opts
->u
.gtk
.has_zoom_to_fit
) {
2065 zoom_to_fit
= s
->opts
->u
.gtk
.zoom_to_fit
;
2068 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2069 s
->free_scale
= true;
2075 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2077 GSList
*group
= NULL
;
2078 GtkWidget
*view_menu
;
2079 GtkWidget
*separator
;
2083 view_menu
= gtk_menu_new();
2084 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2086 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2088 #if defined(CONFIG_VTE)
2089 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2090 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2093 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2094 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2095 gtk_accel_label_set_accel(
2096 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2097 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2098 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2100 separator
= gtk_separator_menu_item_new();
2101 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2103 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2104 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2105 "<QEMU>/View/Zoom In");
2106 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2108 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2109 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2110 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2112 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2113 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2114 "<QEMU>/View/Zoom Out");
2115 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2117 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2119 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2120 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2121 "<QEMU>/View/Zoom Fixed");
2122 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2124 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2126 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2127 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2129 separator
= gtk_separator_menu_item_new();
2130 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2132 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2133 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2135 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2136 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2137 "<QEMU>/View/Grab Input");
2138 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2140 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2142 separator
= gtk_separator_menu_item_new();
2143 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2146 for (vc
= 0;; vc
++) {
2147 con
= qemu_console_lookup_by_index(vc
);
2151 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2152 vc
, group
, view_menu
);
2156 #if defined(CONFIG_VTE)
2158 gd_vcs_init(s
, group
, view_menu
);
2161 separator
= gtk_separator_menu_item_new();
2162 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2164 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2165 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2167 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2168 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2170 s
->show_menubar_item
= gtk_check_menu_item_new_with_mnemonic(
2172 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->show_menubar_item
),
2174 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_m
, HOTKEY_MODIFIERS
, 0,
2175 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar
), s
, NULL
));
2176 gtk_accel_label_set_accel(
2177 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->show_menubar_item
))),
2178 GDK_KEY_m
, HOTKEY_MODIFIERS
);
2179 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_menubar_item
);
2184 static void gd_create_menus(GtkDisplayState
*s
)
2186 GtkSettings
*settings
;
2188 s
->accel_group
= gtk_accel_group_new();
2189 s
->machine_menu
= gd_create_menu_machine(s
);
2190 s
->view_menu
= gd_create_menu_view(s
);
2192 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2193 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2195 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2197 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2198 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2199 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2201 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2202 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2204 /* Disable the default "F10" menu shortcut. */
2205 settings
= gtk_widget_get_settings(s
->window
);
2206 g_object_set(G_OBJECT(settings
), "gtk-menu-bar-accel", "", NULL
);
2210 static gboolean gtkinit
;
2212 static void gtk_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2216 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2218 GdkDisplay
*window_display
;
2221 fprintf(stderr
, "gtk initialization failed\n");
2224 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2227 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2228 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2229 s
->notebook
= gtk_notebook_new();
2230 s
->menu_bar
= gtk_menu_bar_new();
2232 s
->free_scale
= FALSE
;
2234 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2235 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2236 * printable, but without changing any of the character classes to make
2237 * sure that we don't accidentally break implicit assumptions. */
2238 setlocale(LC_MESSAGES
, "");
2239 setlocale(LC_CTYPE
, "C.UTF-8");
2240 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2243 window_display
= gtk_widget_get_display(s
->window
);
2244 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2247 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2248 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2249 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2251 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
2253 GError
*error
= NULL
;
2254 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
2256 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
2258 g_error_free(error
);
2265 gd_connect_signals(s
);
2267 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2268 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2270 gd_update_caption(s
);
2272 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2273 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2275 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2277 gtk_widget_show_all(s
->window
);
2279 vc
= gd_vc_find_current(s
);
2280 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2282 gtk_widget_set_sensitive(s
->copy_item
,
2283 vc
&& vc
->type
== GD_VC_VTE
);
2286 if (opts
->has_full_screen
&&
2287 opts
->full_screen
) {
2288 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2290 if (opts
->u
.gtk
.has_grab_on_hover
&&
2291 opts
->u
.gtk
.grab_on_hover
) {
2292 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2296 static void early_gtk_display_init(DisplayOptions
*opts
)
2298 /* The QEMU code relies on the assumption that it's always run in
2299 * the C locale. Therefore it is not prepared to deal with
2300 * operations that produce different results depending on the
2301 * locale, such as printf's formatting of decimal numbers, and
2304 * Since GTK+ calls setlocale() by default -importing the locale
2305 * settings from the environment- we must prevent it from doing so
2306 * using gtk_disable_setlocale().
2308 * QEMU's GTK+ UI, however, _does_ have translations for some of
2309 * the menu items. As a trade-off between a functionally correct
2310 * QEMU and a fully internationalized UI we support importing
2311 * LC_MESSAGES from the environment (see the setlocale() call
2312 * earlier in this file). This allows us to display translated
2313 * messages leaving everything else untouched.
2315 gtk_disable_setlocale();
2316 gtkinit
= gtk_init_check(NULL
, NULL
);
2318 /* don't exit yet, that'll break -help */
2322 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2323 if (opts
->has_gl
&& opts
->gl
!= DISPLAYGL_MODE_OFF
) {
2324 #if defined(CONFIG_OPENGL)
2325 #if defined(CONFIG_GTK_GL) && defined(GDK_WINDOWING_WAYLAND)
2326 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2327 gtk_use_gl_area
= true;
2332 DisplayGLMode mode
= opts
->has_gl
? opts
->gl
: DISPLAYGL_MODE_ON
;
2338 keycode_map
= gd_get_keymap(&keycode_maplen
);
2340 #if defined(CONFIG_VTE)
2341 type_register(&char_gd_vc_type_info
);
2345 static QemuDisplay qemu_display_gtk
= {
2346 .type
= DISPLAY_TYPE_GTK
,
2347 .early_init
= early_gtk_display_init
,
2348 .init
= gtk_display_init
,
2351 static void register_gtk(void)
2353 qemu_display_register(&qemu_display_gtk
);
2356 type_init(register_gtk
);