4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * Portions from gtk-vnc (originally licensed under the LGPL v2+):
26 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
27 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
30 #define GETTEXT_PACKAGE "qemu"
31 #define LOCALEDIR "po"
33 #include "qemu/osdep.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-commands-misc.h"
36 #include "qemu/cutils.h"
38 #include "ui/console.h"
41 #include <glib/gi18n.h>
43 #if defined(CONFIG_VTE)
50 #include "sysemu/runstate.h"
51 #include "sysemu/sysemu.h"
53 #include "chardev/char.h"
54 #include "qom/object.h"
57 #define VC_WINDOW_X_MIN 320
58 #define VC_WINDOW_Y_MIN 240
59 #define VC_TERM_X_MIN 80
60 #define VC_TERM_Y_MIN 25
61 #define VC_SCALE_MIN 0.25
62 #define VC_SCALE_STEP 0.25
64 #ifdef GDK_WINDOWING_X11
68 #ifndef GDK_IS_X11_DISPLAY
69 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
74 #ifdef GDK_WINDOWING_WAYLAND
76 #ifndef GDK_IS_WAYLAND_DISPLAY
77 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
82 #ifdef GDK_WINDOWING_WIN32
84 #ifndef GDK_IS_WIN32_DISPLAY
85 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
90 #ifdef GDK_WINDOWING_BROADWAY
92 #ifndef GDK_IS_BROADWAY_DISPLAY
93 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
98 #ifdef GDK_WINDOWING_QUARTZ
100 #ifndef GDK_IS_QUARTZ_DISPLAY
101 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
106 #if !defined(CONFIG_VTE)
107 # define VTE_CHECK_VERSION(a, b, c) 0
110 /* Some older mingw versions lack this constant or have
111 * it conditionally defined */
113 # ifndef MAPVK_VK_TO_VSC
114 # define MAPVK_VK_TO_VSC 0
119 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
121 static const guint16
*keycode_map
;
122 static size_t keycode_maplen
;
124 struct GtkDisplayState
{
129 GtkAccelGroup
*accel_group
;
131 GtkWidget
*machine_menu_item
;
132 GtkWidget
*machine_menu
;
133 GtkWidget
*pause_item
;
134 GtkWidget
*reset_item
;
135 GtkWidget
*powerdown_item
;
136 GtkWidget
*quit_item
;
138 GtkWidget
*view_menu_item
;
139 GtkWidget
*view_menu
;
140 GtkWidget
*full_screen_item
;
141 GtkWidget
*copy_item
;
142 GtkWidget
*zoom_in_item
;
143 GtkWidget
*zoom_out_item
;
144 GtkWidget
*zoom_fixed_item
;
145 GtkWidget
*zoom_fit_item
;
146 GtkWidget
*grab_item
;
147 GtkWidget
*grab_on_hover_item
;
150 VirtualConsole vc
[MAX_VCS
];
152 GtkWidget
*show_tabs_item
;
153 GtkWidget
*untabify_item
;
154 GtkWidget
*show_menubar_item
;
164 VirtualConsole
*kbd_owner
;
165 VirtualConsole
*ptr_owner
;
167 gboolean full_screen
;
169 GdkCursor
*null_cursor
;
170 Notifier mouse_mode_notifier
;
173 bool external_pause_update
;
177 DisplayOptions
*opts
;
180 typedef struct VCChardev
{
182 VirtualConsole
*console
;
186 #define TYPE_CHARDEV_VC "chardev-vc"
187 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
189 bool gtk_use_gl_area
;
191 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
192 static void gd_ungrab_pointer(GtkDisplayState
*s
);
193 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
194 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
196 /** Utility Functions **/
198 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
203 for (i
= 0; i
< s
->nb_vcs
; i
++) {
205 if (gtk_check_menu_item_get_active
206 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
213 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
218 for (i
= 0; i
< s
->nb_vcs
; i
++) {
220 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
228 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
232 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
233 return gd_vc_find_by_page(s
, page
);
236 static bool gd_is_grab_active(GtkDisplayState
*s
)
238 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
241 static bool gd_grab_on_hover(GtkDisplayState
*s
)
243 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
246 static void gd_update_cursor(VirtualConsole
*vc
)
248 GtkDisplayState
*s
= vc
->s
;
251 if (vc
->type
!= GD_VC_GFX
||
252 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
256 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
260 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
261 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
262 gdk_window_set_cursor(window
, s
->null_cursor
);
264 gdk_window_set_cursor(window
, NULL
);
268 static void gd_update_caption(GtkDisplayState
*s
)
270 const char *status
= "";
273 const char *grab
= "";
274 bool is_paused
= !runstate_is_running();
278 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
280 prefix
= g_strdup_printf("QEMU");
283 if (s
->ptr_owner
!= NULL
&&
284 s
->ptr_owner
->window
== NULL
) {
285 grab
= _(" - Press Ctrl+Alt+G to release grab");
289 status
= _(" [Paused]");
291 s
->external_pause_update
= true;
292 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
294 s
->external_pause_update
= false;
296 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
297 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
300 for (i
= 0; i
< s
->nb_vcs
; i
++) {
301 VirtualConsole
*vc
= &s
->vc
[i
];
306 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
307 vc
== s
->kbd_owner
? " +kbd" : "",
308 vc
== s
->ptr_owner
? " +ptr" : "");
309 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
316 static void gd_update_geometry_hints(VirtualConsole
*vc
)
318 GtkDisplayState
*s
= vc
->s
;
319 GdkWindowHints mask
= 0;
320 GdkGeometry geo
= {};
321 GtkWidget
*geo_widget
= NULL
;
322 GtkWindow
*geo_window
;
324 if (vc
->type
== GD_VC_GFX
) {
329 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
330 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
331 mask
|= GDK_HINT_MIN_SIZE
;
333 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
334 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
335 mask
|= GDK_HINT_MIN_SIZE
;
337 geo_widget
= vc
->gfx
.drawing_area
;
338 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
340 #if defined(CONFIG_VTE)
341 } else if (vc
->type
== GD_VC_VTE
) {
342 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
343 GtkBorder padding
= { 0 };
345 #if VTE_CHECK_VERSION(0, 37, 0)
346 gtk_style_context_get_padding(
347 gtk_widget_get_style_context(vc
->vte
.terminal
),
348 gtk_widget_get_state_flags(vc
->vte
.terminal
),
352 GtkBorder
*ib
= NULL
;
353 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
361 geo
.width_inc
= vte_terminal_get_char_width(term
);
362 geo
.height_inc
= vte_terminal_get_char_height(term
);
363 mask
|= GDK_HINT_RESIZE_INC
;
364 geo
.base_width
= geo
.width_inc
;
365 geo
.base_height
= geo
.height_inc
;
366 mask
|= GDK_HINT_BASE_SIZE
;
367 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
368 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
369 mask
|= GDK_HINT_MIN_SIZE
;
371 geo
.base_width
+= padding
.left
+ padding
.right
;
372 geo
.base_height
+= padding
.top
+ padding
.bottom
;
373 geo
.min_width
+= padding
.left
+ padding
.right
;
374 geo
.min_height
+= padding
.top
+ padding
.bottom
;
375 geo_widget
= vc
->vte
.terminal
;
379 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
380 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
383 void gd_update_windowsize(VirtualConsole
*vc
)
385 GtkDisplayState
*s
= vc
->s
;
387 gd_update_geometry_hints(vc
);
389 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
390 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
391 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
395 static void gd_update_full_redraw(VirtualConsole
*vc
)
397 GtkWidget
*area
= vc
->gfx
.drawing_area
;
399 ww
= gdk_window_get_width(gtk_widget_get_window(area
));
400 wh
= gdk_window_get_height(gtk_widget_get_window(area
));
401 #if defined(CONFIG_GTK_GL)
402 if (vc
->gfx
.gls
&& gtk_use_gl_area
) {
403 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
407 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
410 static void gtk_release_modifiers(GtkDisplayState
*s
)
412 VirtualConsole
*vc
= gd_vc_find_current(s
);
414 if (vc
->type
!= GD_VC_GFX
||
415 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
418 qkbd_state_lift_all_keys(vc
->gfx
.kbd
);
421 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
424 g_object_ref(G_OBJECT(widget
));
425 gtk_container_remove(GTK_CONTAINER(from
), widget
);
426 gtk_container_add(GTK_CONTAINER(to
), widget
);
427 g_object_unref(G_OBJECT(widget
));
430 /** DisplayState Callbacks **/
432 static void gd_update(DisplayChangeListener
*dcl
,
433 int x
, int y
, int w
, int h
)
435 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
442 trace_gd_update(vc
->label
, x
, y
, w
, h
);
444 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
448 if (vc
->gfx
.convert
) {
449 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
450 NULL
, vc
->gfx
.convert
,
451 x
, y
, 0, 0, x
, y
, w
, h
);
454 x1
= floor(x
* vc
->gfx
.scale_x
);
455 y1
= floor(y
* vc
->gfx
.scale_y
);
457 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
458 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
460 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
461 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
463 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
467 ww
= gdk_window_get_width(win
);
468 wh
= gdk_window_get_height(win
);
478 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
479 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
482 static void gd_refresh(DisplayChangeListener
*dcl
)
484 graphic_hw_update(dcl
->con
);
487 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
489 #if GTK_CHECK_VERSION(3, 20, 0)
490 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
492 return gdk_device_manager_get_client_pointer(
493 gdk_display_get_device_manager(dpy
));
497 static void gd_mouse_set(DisplayChangeListener
*dcl
,
498 int x
, int y
, int visible
)
500 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
504 if (qemu_input_is_absolute()) {
508 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
509 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
510 x
, y
, &x_root
, &y_root
);
511 gdk_device_warp(gd_get_pointer(dpy
),
512 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
518 static void gd_cursor_define(DisplayChangeListener
*dcl
,
521 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
525 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
529 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
530 GDK_COLORSPACE_RGB
, true, 8,
531 c
->width
, c
->height
, c
->width
* 4,
533 cursor
= gdk_cursor_new_from_pixbuf
534 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
535 pixbuf
, c
->hot_x
, c
->hot_y
);
536 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
537 g_object_unref(pixbuf
);
538 g_object_unref(cursor
);
541 static void gd_switch(DisplayChangeListener
*dcl
,
542 DisplaySurface
*surface
)
544 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
547 trace_gd_switch(vc
->label
,
548 surface
? surface_width(surface
) : 0,
549 surface
? surface_height(surface
) : 0);
551 if (vc
->gfx
.surface
) {
552 cairo_surface_destroy(vc
->gfx
.surface
);
553 vc
->gfx
.surface
= NULL
;
555 if (vc
->gfx
.convert
) {
556 pixman_image_unref(vc
->gfx
.convert
);
557 vc
->gfx
.convert
= NULL
;
560 if (vc
->gfx
.ds
&& surface
&&
561 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
562 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
565 vc
->gfx
.ds
= surface
;
571 if (surface
->format
== PIXMAN_x8r8g8b8
) {
573 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
575 * No need to convert, use surface directly. Should be the
576 * common case as this is qemu_default_pixelformat(32) too.
578 vc
->gfx
.surface
= cairo_image_surface_create_for_data
579 (surface_data(surface
),
581 surface_width(surface
),
582 surface_height(surface
),
583 surface_stride(surface
));
585 /* Must convert surface, use pixman to do it. */
586 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
587 surface_width(surface
),
588 surface_height(surface
),
590 vc
->gfx
.surface
= cairo_image_surface_create_for_data
591 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
593 pixman_image_get_width(vc
->gfx
.convert
),
594 pixman_image_get_height(vc
->gfx
.convert
),
595 pixman_image_get_stride(vc
->gfx
.convert
));
596 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
597 NULL
, vc
->gfx
.convert
,
599 pixman_image_get_width(vc
->gfx
.convert
),
600 pixman_image_get_height(vc
->gfx
.convert
));
604 gd_update_windowsize(vc
);
606 gd_update_full_redraw(vc
);
610 static const DisplayChangeListenerOps dcl_ops
= {
612 .dpy_gfx_update
= gd_update
,
613 .dpy_gfx_switch
= gd_switch
,
614 .dpy_gfx_check_format
= qemu_pixman_check_format
,
615 .dpy_refresh
= gd_refresh
,
616 .dpy_mouse_set
= gd_mouse_set
,
617 .dpy_cursor_define
= gd_cursor_define
,
621 #if defined(CONFIG_OPENGL)
623 /** DisplayState Callbacks (opengl version) **/
625 #if defined(CONFIG_GTK_GL)
627 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
628 .dpy_name
= "gtk-egl",
629 .dpy_gfx_update
= gd_gl_area_update
,
630 .dpy_gfx_switch
= gd_gl_area_switch
,
631 .dpy_gfx_check_format
= console_gl_check_format
,
632 .dpy_refresh
= gd_gl_area_refresh
,
633 .dpy_mouse_set
= gd_mouse_set
,
634 .dpy_cursor_define
= gd_cursor_define
,
636 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
637 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
638 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
639 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
640 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
641 .dpy_gl_update
= gd_gl_area_scanout_flush
,
644 #endif /* CONFIG_GTK_GL */
646 static const DisplayChangeListenerOps dcl_egl_ops
= {
647 .dpy_name
= "gtk-egl",
648 .dpy_gfx_update
= gd_egl_update
,
649 .dpy_gfx_switch
= gd_egl_switch
,
650 .dpy_gfx_check_format
= console_gl_check_format
,
651 .dpy_refresh
= gd_egl_refresh
,
652 .dpy_mouse_set
= gd_mouse_set
,
653 .dpy_cursor_define
= gd_cursor_define
,
655 .dpy_gl_ctx_create
= gd_egl_create_context
,
656 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
657 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
658 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
659 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
660 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
661 .dpy_gl_scanout_dmabuf
= gd_egl_scanout_dmabuf
,
662 .dpy_gl_cursor_dmabuf
= gd_egl_cursor_dmabuf
,
663 .dpy_gl_cursor_position
= gd_egl_cursor_position
,
664 .dpy_gl_release_dmabuf
= gd_egl_release_dmabuf
,
665 .dpy_gl_update
= gd_egl_scanout_flush
,
668 #endif /* CONFIG_OPENGL */
672 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
674 GtkDisplayState
*s
= opaque
;
676 gd_update_caption(s
);
679 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
684 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
685 /* release the grab at switching to absolute mode */
686 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
687 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
690 for (i
= 0; i
< s
->nb_vcs
; i
++) {
691 VirtualConsole
*vc
= &s
->vc
[i
];
692 gd_update_cursor(vc
);
698 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
701 GtkDisplayState
*s
= opaque
;
702 bool allow_close
= true;
704 if (s
->opts
->has_window_close
&& !s
->opts
->window_close
) {
715 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
719 memset(&info
, 0, sizeof(info
));
721 info
.height
= height
;
722 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
725 #if defined(CONFIG_GTK_GL)
727 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
730 VirtualConsole
*vc
= opaque
;
738 static void gd_resize_event(GtkGLArea
*area
,
739 gint width
, gint height
, gpointer
*opaque
)
741 VirtualConsole
*vc
= (void *)opaque
;
743 gd_set_ui_info(vc
, width
, height
);
748 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
750 VirtualConsole
*vc
= opaque
;
751 GtkDisplayState
*s
= vc
->s
;
756 #if defined(CONFIG_OPENGL)
758 if (gtk_use_gl_area
) {
759 /* invoke render callback please */
768 if (!gtk_widget_get_realized(widget
)) {
775 fbw
= surface_width(vc
->gfx
.ds
);
776 fbh
= surface_height(vc
->gfx
.ds
);
778 ww
= gdk_window_get_width(gtk_widget_get_window(widget
));
779 wh
= gdk_window_get_height(gtk_widget_get_window(widget
));
781 if (s
->full_screen
) {
782 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
783 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
784 } else if (s
->free_scale
) {
787 sx
= (double)ww
/ fbw
;
788 sy
= (double)wh
/ fbh
;
790 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
793 fbw
*= vc
->gfx
.scale_x
;
794 fbh
*= vc
->gfx
.scale_y
;
804 cairo_rectangle(cr
, 0, 0, ww
, wh
);
806 /* Optionally cut out the inner area where the pixmap
807 will be drawn. This avoids 'flashing' since we're
808 not double-buffering. Note we're using the undocumented
809 behaviour of drawing the rectangle from right to left
810 to cut out the whole */
811 cairo_rectangle(cr
, mx
+ fbw
, my
,
815 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
816 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
817 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
823 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
826 VirtualConsole
*vc
= opaque
;
827 GtkDisplayState
*s
= vc
->s
;
837 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
838 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
840 ww
= gdk_window_get_width(gtk_widget_get_window(vc
->gfx
.drawing_area
));
841 wh
= gdk_window_get_height(gtk_widget_get_window(vc
->gfx
.drawing_area
));
851 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
852 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
854 if (qemu_input_is_absolute()) {
855 if (x
< 0 || y
< 0 ||
856 x
>= surface_width(vc
->gfx
.ds
) ||
857 y
>= surface_height(vc
->gfx
.ds
)) {
860 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
861 0, surface_width(vc
->gfx
.ds
));
862 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
863 0, surface_height(vc
->gfx
.ds
));
864 qemu_input_event_sync();
865 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
866 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
867 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
868 qemu_input_event_sync();
874 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
875 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
876 int screen_width
, screen_height
;
878 int x
= (int)motion
->x_root
;
879 int y
= (int)motion
->y_root
;
881 #if GTK_CHECK_VERSION(3, 22, 0)
883 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
884 GdkWindow
*win
= gtk_widget_get_window(widget
);
885 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
886 GdkRectangle geometry
;
887 gdk_monitor_get_geometry(monitor
, &geometry
);
888 screen_width
= geometry
.width
;
889 screen_height
= geometry
.height
;
893 screen_width
= gdk_screen_get_width(screen
);
894 screen_height
= gdk_screen_get_height(screen
);
898 /* In relative mode check to see if client pointer hit
899 * one of the screen edges, and if so move it back by
900 * 200 pixels. This is important because the pointer
901 * in the server doesn't correspond 1-for-1, and so
902 * may still be only half way across the screen. Without
903 * this warp, the server pointer would thus appear to hit
904 * an invisible wall */
911 if (x
== (screen_width
- 1)) {
914 if (y
== (screen_height
- 1)) {
918 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
919 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
920 gdk_device_warp(dev
, screen
, x
, y
);
928 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
931 VirtualConsole
*vc
= opaque
;
932 GtkDisplayState
*s
= vc
->s
;
935 /* implicitly grab the input at the first click in the relative mode */
936 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
937 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
939 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
942 gd_grab_pointer(vc
, "relative-mode-click");
947 if (button
->button
== 1) {
948 btn
= INPUT_BUTTON_LEFT
;
949 } else if (button
->button
== 2) {
950 btn
= INPUT_BUTTON_MIDDLE
;
951 } else if (button
->button
== 3) {
952 btn
= INPUT_BUTTON_RIGHT
;
953 } else if (button
->button
== 8) {
954 btn
= INPUT_BUTTON_SIDE
;
955 } else if (button
->button
== 9) {
956 btn
= INPUT_BUTTON_EXTRA
;
961 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
962 button
->type
== GDK_BUTTON_PRESS
);
963 qemu_input_event_sync();
967 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
970 VirtualConsole
*vc
= opaque
;
973 if (scroll
->direction
== GDK_SCROLL_UP
) {
974 btn
= INPUT_BUTTON_WHEEL_UP
;
975 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
976 btn
= INPUT_BUTTON_WHEEL_DOWN
;
977 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
978 gdouble delta_x
, delta_y
;
979 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
980 &delta_x
, &delta_y
)) {
985 } else if (delta_y
> 0) {
986 btn
= INPUT_BUTTON_WHEEL_DOWN
;
988 btn
= INPUT_BUTTON_WHEEL_UP
;
994 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
995 qemu_input_event_sync();
996 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
997 qemu_input_event_sync();
1002 static const guint16
*gd_get_keymap(size_t *maplen
)
1004 GdkDisplay
*dpy
= gdk_display_get_default();
1006 #ifdef GDK_WINDOWING_X11
1007 if (GDK_IS_X11_DISPLAY(dpy
)) {
1008 trace_gd_keymap_windowing("x11");
1009 return qemu_xkeymap_mapping_table(
1010 gdk_x11_display_get_xdisplay(dpy
), maplen
);
1014 #ifdef GDK_WINDOWING_WAYLAND
1015 if (GDK_IS_WAYLAND_DISPLAY(dpy
)) {
1016 trace_gd_keymap_windowing("wayland");
1017 *maplen
= qemu_input_map_xorgevdev_to_qcode_len
;
1018 return qemu_input_map_xorgevdev_to_qcode
;
1022 #ifdef GDK_WINDOWING_WIN32
1023 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1024 trace_gd_keymap_windowing("win32");
1025 *maplen
= qemu_input_map_win32_to_qcode_len
;
1026 return qemu_input_map_win32_to_qcode
;
1030 #ifdef GDK_WINDOWING_QUARTZ
1031 if (GDK_IS_QUARTZ_DISPLAY(dpy
)) {
1032 trace_gd_keymap_windowing("quartz");
1033 *maplen
= qemu_input_map_osx_to_qcode_len
;
1034 return qemu_input_map_osx_to_qcode
;
1038 #ifdef GDK_WINDOWING_BROADWAY
1039 if (GDK_IS_BROADWAY_DISPLAY(dpy
)) {
1040 trace_gd_keymap_windowing("broadway");
1041 g_warning("experimental: using broadway, x11 virtual keysym\n"
1042 "mapping - with very limited support. See also\n"
1043 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1044 *maplen
= qemu_input_map_x11_to_qcode_len
;
1045 return qemu_input_map_x11_to_qcode
;
1049 g_warning("Unsupported GDK Windowing platform.\n"
1050 "Disabling extended keycode tables.\n"
1051 "Please report to qemu-devel@nongnu.org\n"
1052 "including the following information:\n"
1054 " - Operating system\n"
1055 " - GDK Windowing system build\n");
1060 static int gd_map_keycode(int scancode
)
1065 if (scancode
> keycode_maplen
) {
1069 return keycode_map
[scancode
];
1072 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1073 GdkEventKey
*key
, void *opaque
)
1075 VirtualConsole
*vc
= opaque
;
1076 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1078 if (key
->keyval
== GDK_KEY_Delete
) {
1079 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
, false);
1080 } else if (key
->length
) {
1081 kbd_put_string_console(con
, key
->string
, key
->length
);
1083 int qcode
= gd_map_keycode(key
->hardware_keycode
);
1084 kbd_put_qcode_console(con
, qcode
, false);
1089 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1091 VirtualConsole
*vc
= opaque
;
1092 GtkDisplayState
*s
= vc
->s
;
1095 if (s
->ignore_keys
) {
1096 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1101 /* on windows, we ought to ignore the reserved key event? */
1102 if (key
->hardware_keycode
== 0xff)
1106 if (key
->keyval
== GDK_KEY_Pause
1108 /* for some reason GDK does not fill keyval for VK_PAUSE
1109 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1111 || key
->hardware_keycode
== VK_PAUSE
1114 qkbd_state_key_event(vc
->gfx
.kbd
, Q_KEY_CODE_PAUSE
,
1115 key
->type
== GDK_KEY_PRESS
);
1119 qcode
= gd_map_keycode(key
->hardware_keycode
);
1121 trace_gd_key_event(vc
->label
, key
->hardware_keycode
, qcode
,
1122 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1124 qkbd_state_key_event(vc
->gfx
.kbd
, qcode
,
1125 key
->type
== GDK_KEY_PRESS
);
1130 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1132 if (event
->type
== GDK_MOTION_NOTIFY
) {
1133 return gd_motion_event(widget
, &event
->motion
, opaque
);
1138 /** Window Menu Actions **/
1140 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1142 GtkDisplayState
*s
= opaque
;
1144 if (s
->external_pause_update
) {
1147 if (runstate_is_running()) {
1154 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1156 qmp_system_reset(NULL
);
1159 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1161 qmp_system_powerdown(NULL
);
1164 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1169 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1171 GtkDisplayState
*s
= opaque
;
1172 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1173 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1176 gtk_release_modifiers(s
);
1178 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1179 gtk_notebook_set_current_page(nb
, page
);
1180 gtk_widget_grab_focus(vc
->focus
);
1182 s
->ignore_keys
= false;
1185 static void gd_accel_switch_vc(void *opaque
)
1187 VirtualConsole
*vc
= opaque
;
1189 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1192 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1194 GtkDisplayState
*s
= opaque
;
1195 VirtualConsole
*vc
= gd_vc_find_current(s
);
1197 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1198 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1200 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1202 gd_update_windowsize(vc
);
1205 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1208 VirtualConsole
*vc
= opaque
;
1209 GtkDisplayState
*s
= vc
->s
;
1211 gtk_widget_set_sensitive(vc
->menu_item
, true);
1212 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1213 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1214 vc
->tab_item
, vc
->label
);
1215 gtk_widget_destroy(vc
->window
);
1220 static gboolean
gd_win_grab(void *opaque
)
1222 VirtualConsole
*vc
= opaque
;
1224 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1225 if (vc
->s
->ptr_owner
) {
1226 gd_ungrab_pointer(vc
->s
);
1228 gd_grab_pointer(vc
, "user-request-detached-tab");
1233 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1235 GtkDisplayState
*s
= opaque
;
1236 VirtualConsole
*vc
= gd_vc_find_current(s
);
1238 if (vc
->type
== GD_VC_GFX
&&
1239 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1240 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1244 gtk_widget_set_sensitive(vc
->menu_item
, false);
1245 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1246 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1248 g_signal_connect(vc
->window
, "delete-event",
1249 G_CALLBACK(gd_tab_window_close
), vc
);
1250 gtk_widget_show_all(vc
->window
);
1252 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1253 GtkAccelGroup
*ag
= gtk_accel_group_new();
1254 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1256 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1258 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1261 gd_update_geometry_hints(vc
);
1262 gd_update_caption(s
);
1266 static void gd_menu_show_menubar(GtkMenuItem
*item
, void *opaque
)
1268 GtkDisplayState
*s
= opaque
;
1269 VirtualConsole
*vc
= gd_vc_find_current(s
);
1271 if (s
->full_screen
) {
1275 if (gtk_check_menu_item_get_active(
1276 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1277 gtk_widget_show(s
->menu_bar
);
1279 gtk_widget_hide(s
->menu_bar
);
1281 gd_update_windowsize(vc
);
1284 static void gd_accel_show_menubar(void *opaque
)
1286 GtkDisplayState
*s
= opaque
;
1287 gtk_menu_item_activate(GTK_MENU_ITEM(s
->show_menubar_item
));
1290 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1292 GtkDisplayState
*s
= opaque
;
1293 VirtualConsole
*vc
= gd_vc_find_current(s
);
1295 if (!s
->full_screen
) {
1296 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1297 gtk_widget_hide(s
->menu_bar
);
1298 if (vc
->type
== GD_VC_GFX
) {
1299 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1301 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1302 s
->full_screen
= TRUE
;
1304 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1305 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1306 if (gtk_check_menu_item_get_active(
1307 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1308 gtk_widget_show(s
->menu_bar
);
1310 s
->full_screen
= FALSE
;
1311 if (vc
->type
== GD_VC_GFX
) {
1312 vc
->gfx
.scale_x
= 1.0;
1313 vc
->gfx
.scale_y
= 1.0;
1314 gd_update_windowsize(vc
);
1318 gd_update_cursor(vc
);
1321 static void gd_accel_full_screen(void *opaque
)
1323 GtkDisplayState
*s
= opaque
;
1324 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1327 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1329 GtkDisplayState
*s
= opaque
;
1330 VirtualConsole
*vc
= gd_vc_find_current(s
);
1332 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1335 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1336 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1338 gd_update_windowsize(vc
);
1341 static void gd_accel_zoom_in(void *opaque
)
1343 GtkDisplayState
*s
= opaque
;
1344 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1347 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1349 GtkDisplayState
*s
= opaque
;
1350 VirtualConsole
*vc
= gd_vc_find_current(s
);
1352 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1355 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1356 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1358 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1359 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1361 gd_update_windowsize(vc
);
1364 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1366 GtkDisplayState
*s
= opaque
;
1367 VirtualConsole
*vc
= gd_vc_find_current(s
);
1369 vc
->gfx
.scale_x
= 1.0;
1370 vc
->gfx
.scale_y
= 1.0;
1372 gd_update_windowsize(vc
);
1375 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1377 GtkDisplayState
*s
= opaque
;
1378 VirtualConsole
*vc
= gd_vc_find_current(s
);
1380 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1381 s
->free_scale
= TRUE
;
1383 s
->free_scale
= FALSE
;
1384 vc
->gfx
.scale_x
= 1.0;
1385 vc
->gfx
.scale_y
= 1.0;
1388 gd_update_windowsize(vc
);
1389 gd_update_full_redraw(vc
);
1392 #if GTK_CHECK_VERSION(3, 20, 0)
1393 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1395 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1396 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1397 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1398 GdkSeatCapabilities caps
= 0;
1399 GdkCursor
*cursor
= NULL
;
1402 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1405 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1406 cursor
= vc
->s
->null_cursor
;
1410 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1413 gdk_seat_ungrab(seat
);
1417 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1418 GdkInputSource source
, GdkEventMask mask
,
1421 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1422 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1423 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1426 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1427 GdkDevice
*dev
= tmp
->data
;
1428 if (gdk_device_get_source(dev
) != source
) {
1432 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1433 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1434 mask
, cursor
, GDK_CURRENT_TIME
);
1436 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1443 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1445 if (vc
->s
->kbd_owner
) {
1446 if (vc
->s
->kbd_owner
== vc
) {
1449 gd_ungrab_keyboard(vc
->s
);
1453 #if GTK_CHECK_VERSION(3, 20, 0)
1454 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1456 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1457 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1460 vc
->s
->kbd_owner
= vc
;
1461 gd_update_caption(vc
->s
);
1462 trace_gd_grab(vc
->label
, "kbd", reason
);
1465 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1467 VirtualConsole
*vc
= s
->kbd_owner
;
1472 s
->kbd_owner
= NULL
;
1474 #if GTK_CHECK_VERSION(3, 20, 0)
1475 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1477 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1479 gd_update_caption(s
);
1480 trace_gd_ungrab(vc
->label
, "kbd");
1483 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1485 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1487 if (vc
->s
->ptr_owner
) {
1488 if (vc
->s
->ptr_owner
== vc
) {
1491 gd_ungrab_pointer(vc
->s
);
1495 #if GTK_CHECK_VERSION(3, 20, 0)
1496 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1497 gdk_device_get_position(gd_get_pointer(display
),
1498 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1500 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1501 GDK_POINTER_MOTION_MASK
|
1502 GDK_BUTTON_PRESS_MASK
|
1503 GDK_BUTTON_RELEASE_MASK
|
1504 GDK_BUTTON_MOTION_MASK
|
1506 vc
->s
->null_cursor
);
1507 gdk_device_get_position(gd_get_pointer(display
),
1508 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1510 vc
->s
->ptr_owner
= vc
;
1511 gd_update_caption(vc
->s
);
1512 trace_gd_grab(vc
->label
, "ptr", reason
);
1515 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1517 VirtualConsole
*vc
= s
->ptr_owner
;
1518 GdkDisplay
*display
;
1523 s
->ptr_owner
= NULL
;
1525 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1526 #if GTK_CHECK_VERSION(3, 20, 0)
1527 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1528 gdk_device_warp(gd_get_pointer(display
),
1529 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1530 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1532 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1533 gdk_device_warp(gd_get_pointer(display
),
1534 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1535 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1537 gd_update_caption(s
);
1538 trace_gd_ungrab(vc
->label
, "ptr");
1541 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1543 GtkDisplayState
*s
= opaque
;
1544 VirtualConsole
*vc
= gd_vc_find_current(s
);
1546 if (gd_is_grab_active(s
)) {
1547 gd_grab_keyboard(vc
, "user-request-main-window");
1548 gd_grab_pointer(vc
, "user-request-main-window");
1550 gd_ungrab_keyboard(s
);
1551 gd_ungrab_pointer(s
);
1554 gd_update_cursor(vc
);
1557 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1560 GtkDisplayState
*s
= data
;
1564 if (!gtk_widget_get_realized(s
->notebook
)) {
1568 vc
= gd_vc_find_by_page(s
, arg2
);
1572 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1574 on_vga
= (vc
->type
== GD_VC_GFX
&&
1575 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1577 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1579 } else if (s
->full_screen
) {
1580 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1583 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1585 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1588 gd_update_windowsize(vc
);
1589 gd_update_cursor(vc
);
1592 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1595 VirtualConsole
*vc
= opaque
;
1596 GtkDisplayState
*s
= vc
->s
;
1598 if (gd_grab_on_hover(s
)) {
1599 gd_grab_keyboard(vc
, "grab-on-hover");
1604 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1607 VirtualConsole
*vc
= opaque
;
1608 GtkDisplayState
*s
= vc
->s
;
1610 if (gd_grab_on_hover(s
)) {
1611 gd_ungrab_keyboard(s
);
1616 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1617 GdkEventCrossing
*crossing
, gpointer opaque
)
1619 VirtualConsole
*vc
= opaque
;
1620 GtkDisplayState
*s
= vc
->s
;
1622 gtk_release_modifiers(s
);
1626 static gboolean
gd_configure(GtkWidget
*widget
,
1627 GdkEventConfigure
*cfg
, gpointer opaque
)
1629 VirtualConsole
*vc
= opaque
;
1631 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1635 /** Virtual Console Callbacks **/
1637 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1638 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1640 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1641 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1642 HOTKEY_MODIFIERS
, 0,
1643 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1644 gtk_accel_label_set_accel(
1645 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1646 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1648 g_signal_connect(vc
->menu_item
, "activate",
1649 G_CALLBACK(gd_menu_switch_vc
), s
);
1650 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1652 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1656 #if defined(CONFIG_VTE)
1657 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1659 GtkDisplayState
*s
= opaque
;
1660 VirtualConsole
*vc
= gd_vc_find_current(s
);
1662 #if VTE_CHECK_VERSION(0, 50, 0)
1663 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1666 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1670 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1672 VirtualConsole
*vc
= opaque
;
1674 if (gtk_adjustment_get_upper(adjustment
) >
1675 gtk_adjustment_get_page_size(adjustment
)) {
1676 gtk_widget_show(vc
->vte
.scrollbar
);
1678 gtk_widget_hide(vc
->vte
.scrollbar
);
1682 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1684 VCChardev
*vcd
= VC_CHARDEV(chr
);
1685 VirtualConsole
*vc
= vcd
->console
;
1687 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1691 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1693 VCChardev
*vcd
= VC_CHARDEV(chr
);
1694 VirtualConsole
*vc
= vcd
->console
;
1697 vc
->vte
.echo
= echo
;
1704 static Chardev
*vcs
[MAX_VCS
];
1705 static void gd_vc_open(Chardev
*chr
,
1706 ChardevBackend
*backend
,
1710 if (nb_vcs
== MAX_VCS
) {
1711 error_setg(errp
, "Maximum number of consoles reached");
1715 vcs
[nb_vcs
++] = chr
;
1717 /* console/chardev init sometimes completes elsewhere in a 2nd
1718 * stage, so defer OPENED events until they are fully initialized
1723 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1725 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1727 cc
->parse
= qemu_chr_parse_vc
;
1728 cc
->open
= gd_vc_open
;
1729 cc
->chr_write
= gd_vc_chr_write
;
1730 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1733 static const TypeInfo char_gd_vc_type_info
= {
1734 .name
= TYPE_CHARDEV_VC
,
1735 .parent
= TYPE_CHARDEV
,
1736 .instance_size
= sizeof(VCChardev
),
1737 .class_init
= char_gd_vc_class_init
,
1740 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1743 VirtualConsole
*vc
= user_data
;
1746 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1748 for (i
= 0; i
< size
; i
++) {
1749 uint8_t c
= text
[i
];
1750 if (c
>= 128 || isprint(c
)) {
1751 /* 8-bit characters are considered printable. */
1752 vte_terminal_feed(term
, &text
[i
], 1);
1753 } else if (c
== '\r' || c
== '\n') {
1754 vte_terminal_feed(term
, "\r\n", 2);
1756 char ctrl
[2] = { '^', 0};
1757 ctrl
[1] = text
[i
] ^ 64;
1758 vte_terminal_feed(term
, ctrl
, 2);
1763 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1767 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1768 Chardev
*chr
, int idx
,
1769 GSList
*group
, GtkWidget
*view_menu
)
1773 GtkWidget
*scrollbar
;
1774 GtkAdjustment
*vadjustment
;
1775 VCChardev
*vcd
= VC_CHARDEV(chr
);
1778 vc
->vte
.echo
= vcd
->echo
;
1782 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1783 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1784 ? vc
->vte
.chr
->label
: buffer
);
1785 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1787 vc
->vte
.terminal
= vte_terminal_new();
1788 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1790 /* The documentation says that the default is UTF-8, but actually it is
1791 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1792 * VTE 0.54 (only UTF-8 is supported now). */
1793 #if !VTE_CHECK_VERSION(0, 54, 0)
1794 #if VTE_CHECK_VERSION(0, 38, 0)
1795 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1797 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1801 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1802 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1803 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1805 #if VTE_CHECK_VERSION(0, 28, 0)
1806 vadjustment
= gtk_scrollable_get_vadjustment
1807 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1809 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1812 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1813 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1815 gtk_box_pack_end(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1816 gtk_box_pack_end(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1819 vc
->vte
.scrollbar
= scrollbar
;
1821 g_signal_connect(vadjustment
, "changed",
1822 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1824 vc
->type
= GD_VC_VTE
;
1826 vc
->focus
= vc
->vte
.terminal
;
1827 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1828 gtk_label_new(vc
->label
));
1830 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1835 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1836 GtkWidget
*view_menu
)
1840 for (i
= 0; i
< nb_vcs
; i
++) {
1841 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1842 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1846 #endif /* CONFIG_VTE */
1848 /** Window Creation **/
1850 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1852 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1853 G_CALLBACK(gd_draw_event
), vc
);
1854 #if defined(CONFIG_GTK_GL)
1855 if (gtk_use_gl_area
) {
1856 /* wire up GtkGlArea events */
1857 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1858 G_CALLBACK(gd_render_event
), vc
);
1859 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1860 G_CALLBACK(gd_resize_event
), vc
);
1863 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1864 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1865 G_CALLBACK(gd_event
), vc
);
1866 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1867 G_CALLBACK(gd_button_event
), vc
);
1868 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1869 G_CALLBACK(gd_button_event
), vc
);
1870 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1871 G_CALLBACK(gd_scroll_event
), vc
);
1872 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1873 G_CALLBACK(gd_key_event
), vc
);
1874 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1875 G_CALLBACK(gd_key_event
), vc
);
1877 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1878 G_CALLBACK(gd_enter_event
), vc
);
1879 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1880 G_CALLBACK(gd_leave_event
), vc
);
1881 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1882 G_CALLBACK(gd_focus_out_event
), vc
);
1883 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1884 G_CALLBACK(gd_configure
), vc
);
1886 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1887 G_CALLBACK(gd_text_key_down
), vc
);
1891 static void gd_connect_signals(GtkDisplayState
*s
)
1893 g_signal_connect(s
->show_tabs_item
, "activate",
1894 G_CALLBACK(gd_menu_show_tabs
), s
);
1895 g_signal_connect(s
->untabify_item
, "activate",
1896 G_CALLBACK(gd_menu_untabify
), s
);
1897 g_signal_connect(s
->show_menubar_item
, "activate",
1898 G_CALLBACK(gd_menu_show_menubar
), s
);
1900 g_signal_connect(s
->window
, "delete-event",
1901 G_CALLBACK(gd_window_close
), s
);
1903 g_signal_connect(s
->pause_item
, "activate",
1904 G_CALLBACK(gd_menu_pause
), s
);
1905 g_signal_connect(s
->reset_item
, "activate",
1906 G_CALLBACK(gd_menu_reset
), s
);
1907 g_signal_connect(s
->powerdown_item
, "activate",
1908 G_CALLBACK(gd_menu_powerdown
), s
);
1909 g_signal_connect(s
->quit_item
, "activate",
1910 G_CALLBACK(gd_menu_quit
), s
);
1911 #if defined(CONFIG_VTE)
1912 g_signal_connect(s
->copy_item
, "activate",
1913 G_CALLBACK(gd_menu_copy
), s
);
1915 g_signal_connect(s
->full_screen_item
, "activate",
1916 G_CALLBACK(gd_menu_full_screen
), s
);
1917 g_signal_connect(s
->zoom_in_item
, "activate",
1918 G_CALLBACK(gd_menu_zoom_in
), s
);
1919 g_signal_connect(s
->zoom_out_item
, "activate",
1920 G_CALLBACK(gd_menu_zoom_out
), s
);
1921 g_signal_connect(s
->zoom_fixed_item
, "activate",
1922 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1923 g_signal_connect(s
->zoom_fit_item
, "activate",
1924 G_CALLBACK(gd_menu_zoom_fit
), s
);
1925 g_signal_connect(s
->grab_item
, "activate",
1926 G_CALLBACK(gd_menu_grab_input
), s
);
1927 g_signal_connect(s
->notebook
, "switch-page",
1928 G_CALLBACK(gd_change_page
), s
);
1931 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1933 GtkWidget
*machine_menu
;
1934 GtkWidget
*separator
;
1936 machine_menu
= gtk_menu_new();
1937 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1939 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1940 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1942 separator
= gtk_separator_menu_item_new();
1943 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1945 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1946 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1948 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1949 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1951 separator
= gtk_separator_menu_item_new();
1952 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1954 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1955 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1956 "<QEMU>/Machine/Quit");
1957 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1958 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1959 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1961 return machine_menu
;
1965 * If available, return the refresh rate of the display in milli-Hertz,
1968 static int gd_refresh_rate_millihz(GtkDisplayState
*s
)
1970 #ifdef GDK_VERSION_3_22
1971 GdkDisplay
*dpy
= gtk_widget_get_display(s
->window
);
1972 GdkWindow
*win
= gtk_widget_get_window(s
->window
);
1973 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
1975 return gdk_monitor_get_refresh_rate(monitor
);
1981 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1982 QemuConsole
*con
, int idx
,
1983 GSList
*group
, GtkWidget
*view_menu
)
1985 bool zoom_to_fit
= false;
1986 int refresh_rate_millihz
;
1988 vc
->label
= qemu_console_get_label(con
);
1990 vc
->gfx
.scale_x
= 1.0;
1991 vc
->gfx
.scale_y
= 1.0;
1993 #if defined(CONFIG_OPENGL)
1994 if (display_opengl
) {
1995 #if defined(CONFIG_GTK_GL)
1996 if (gtk_use_gl_area
) {
1997 vc
->gfx
.drawing_area
= gtk_gl_area_new();
1998 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
2000 #endif /* CONFIG_GTK_GL */
2002 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2004 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2005 * It is required for opengl rendering on X11 though. A
2006 * proper replacement (native opengl support) is only
2007 * available in 3.16+. Silence the warning if possible.
2009 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2010 #pragma GCC diagnostic push
2011 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2013 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2014 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2015 #pragma GCC diagnostic pop
2017 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2022 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2023 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2027 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2028 GDK_POINTER_MOTION_MASK
|
2029 GDK_BUTTON_PRESS_MASK
|
2030 GDK_BUTTON_RELEASE_MASK
|
2031 GDK_BUTTON_MOTION_MASK
|
2032 GDK_ENTER_NOTIFY_MASK
|
2033 GDK_LEAVE_NOTIFY_MASK
|
2035 GDK_SMOOTH_SCROLL_MASK
|
2036 GDK_KEY_PRESS_MASK
);
2037 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2039 vc
->type
= GD_VC_GFX
;
2040 vc
->tab_item
= vc
->gfx
.drawing_area
;
2041 vc
->focus
= vc
->gfx
.drawing_area
;
2042 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2043 vc
->tab_item
, gtk_label_new(vc
->label
));
2045 vc
->gfx
.kbd
= qkbd_state_init(con
);
2046 vc
->gfx
.dcl
.con
= con
;
2048 refresh_rate_millihz
= gd_refresh_rate_millihz(s
);
2049 if (refresh_rate_millihz
) {
2050 vc
->gfx
.dcl
.update_interval
= MILLISEC_PER_SEC
/ refresh_rate_millihz
;
2053 register_displaychangelistener(&vc
->gfx
.dcl
);
2055 gd_connect_vc_gfx_signals(vc
);
2056 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2058 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2061 if (s
->opts
->u
.gtk
.has_zoom_to_fit
) {
2062 zoom_to_fit
= s
->opts
->u
.gtk
.zoom_to_fit
;
2065 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2066 s
->free_scale
= true;
2072 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2074 GSList
*group
= NULL
;
2075 GtkWidget
*view_menu
;
2076 GtkWidget
*separator
;
2080 view_menu
= gtk_menu_new();
2081 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2083 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2085 #if defined(CONFIG_VTE)
2086 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2087 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2090 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2091 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2092 gtk_accel_label_set_accel(
2093 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2094 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2095 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2097 separator
= gtk_separator_menu_item_new();
2098 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2100 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2101 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2102 "<QEMU>/View/Zoom In");
2103 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2105 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2106 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2107 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2109 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2110 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2111 "<QEMU>/View/Zoom Out");
2112 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2114 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2116 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2117 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2118 "<QEMU>/View/Zoom Fixed");
2119 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2121 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2123 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2124 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2126 separator
= gtk_separator_menu_item_new();
2127 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2129 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2130 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2132 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2133 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2134 "<QEMU>/View/Grab Input");
2135 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2137 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2139 separator
= gtk_separator_menu_item_new();
2140 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2143 for (vc
= 0;; vc
++) {
2144 con
= qemu_console_lookup_by_index(vc
);
2148 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2149 vc
, group
, view_menu
);
2153 #if defined(CONFIG_VTE)
2155 gd_vcs_init(s
, group
, view_menu
);
2158 separator
= gtk_separator_menu_item_new();
2159 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2161 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2162 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2164 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2165 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2167 s
->show_menubar_item
= gtk_check_menu_item_new_with_mnemonic(
2169 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->show_menubar_item
),
2171 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_m
, HOTKEY_MODIFIERS
, 0,
2172 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar
), s
, NULL
));
2173 gtk_accel_label_set_accel(
2174 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->show_menubar_item
))),
2175 GDK_KEY_m
, HOTKEY_MODIFIERS
);
2176 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_menubar_item
);
2181 static void gd_create_menus(GtkDisplayState
*s
)
2183 GtkSettings
*settings
;
2185 s
->accel_group
= gtk_accel_group_new();
2186 s
->machine_menu
= gd_create_menu_machine(s
);
2187 s
->view_menu
= gd_create_menu_view(s
);
2189 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2190 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2192 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2194 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2195 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2196 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2198 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2199 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2201 /* Disable the default "F10" menu shortcut. */
2202 settings
= gtk_widget_get_settings(s
->window
);
2203 g_object_set(G_OBJECT(settings
), "gtk-menu-bar-accel", "", NULL
);
2207 static gboolean gtkinit
;
2209 static void gtk_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2213 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2214 GdkDisplay
*window_display
;
2215 GtkIconTheme
*theme
;
2218 fprintf(stderr
, "gtk initialization failed\n");
2221 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2224 theme
= gtk_icon_theme_get_default();
2225 gtk_icon_theme_prepend_search_path(theme
, CONFIG_QEMU_ICONDIR
);
2226 g_set_prgname("qemu");
2228 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2229 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2230 s
->notebook
= gtk_notebook_new();
2231 s
->menu_bar
= gtk_menu_bar_new();
2233 s
->free_scale
= FALSE
;
2235 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2236 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2237 * printable, but without changing any of the character classes to make
2238 * sure that we don't accidentally break implicit assumptions. */
2239 setlocale(LC_MESSAGES
, "");
2240 setlocale(LC_CTYPE
, "C.UTF-8");
2241 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2242 bind_textdomain_codeset("qemu", "UTF-8");
2245 window_display
= gtk_widget_get_display(s
->window
);
2246 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2249 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2250 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2251 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2253 gtk_window_set_icon_name(GTK_WINDOW(s
->window
), "qemu");
2257 gd_connect_signals(s
);
2259 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2260 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2262 gd_update_caption(s
);
2264 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2265 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2267 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2269 gtk_widget_show_all(s
->window
);
2271 vc
= gd_vc_find_current(s
);
2272 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2274 gtk_widget_set_sensitive(s
->copy_item
,
2275 vc
&& vc
->type
== GD_VC_VTE
);
2278 if (opts
->has_full_screen
&&
2279 opts
->full_screen
) {
2280 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2282 if (opts
->u
.gtk
.has_grab_on_hover
&&
2283 opts
->u
.gtk
.grab_on_hover
) {
2284 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2288 static void early_gtk_display_init(DisplayOptions
*opts
)
2290 /* The QEMU code relies on the assumption that it's always run in
2291 * the C locale. Therefore it is not prepared to deal with
2292 * operations that produce different results depending on the
2293 * locale, such as printf's formatting of decimal numbers, and
2296 * Since GTK+ calls setlocale() by default -importing the locale
2297 * settings from the environment- we must prevent it from doing so
2298 * using gtk_disable_setlocale().
2300 * QEMU's GTK+ UI, however, _does_ have translations for some of
2301 * the menu items. As a trade-off between a functionally correct
2302 * QEMU and a fully internationalized UI we support importing
2303 * LC_MESSAGES from the environment (see the setlocale() call
2304 * earlier in this file). This allows us to display translated
2305 * messages leaving everything else untouched.
2307 gtk_disable_setlocale();
2308 gtkinit
= gtk_init_check(NULL
, NULL
);
2310 /* don't exit yet, that'll break -help */
2314 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2315 if (opts
->has_gl
&& opts
->gl
!= DISPLAYGL_MODE_OFF
) {
2316 #if defined(CONFIG_OPENGL)
2317 #if defined(CONFIG_GTK_GL) && defined(GDK_WINDOWING_WAYLAND)
2318 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2319 gtk_use_gl_area
= true;
2324 DisplayGLMode mode
= opts
->has_gl
? opts
->gl
: DISPLAYGL_MODE_ON
;
2330 keycode_map
= gd_get_keymap(&keycode_maplen
);
2332 #if defined(CONFIG_VTE)
2333 type_register(&char_gd_vc_type_info
);
2337 static QemuDisplay qemu_display_gtk
= {
2338 .type
= DISPLAY_TYPE_GTK
,
2339 .early_init
= early_gtk_display_init
,
2340 .init
= gtk_display_init
,
2343 static void register_gtk(void)
2345 qemu_display_register(&qemu_display_gtk
);
2348 type_init(register_gtk
);