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-control.h"
36 #include "qapi/qapi-commands-machine.h"
37 #include "qapi/qapi-commands-misc.h"
38 #include "qemu/cutils.h"
39 #include "qemu/error-report.h"
40 #include "qemu/main-loop.h"
42 #include "ui/console.h"
45 #include <gdk/gdkwin32.h>
47 #include "ui/win32-kbd-hook.h"
49 #include <glib/gi18n.h>
51 #if defined(CONFIG_VTE)
58 #include "sysemu/runstate.h"
59 #include "sysemu/sysemu.h"
61 #include "chardev/char.h"
62 #include "qom/object.h"
64 #define VC_WINDOW_X_MIN 320
65 #define VC_WINDOW_Y_MIN 240
66 #define VC_TERM_X_MIN 80
67 #define VC_TERM_Y_MIN 25
68 #define VC_SCALE_MIN 0.25
69 #define VC_SCALE_STEP 0.25
71 #ifdef GDK_WINDOWING_X11
75 #ifndef GDK_IS_X11_DISPLAY
76 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
81 #ifdef GDK_WINDOWING_WAYLAND
83 #ifndef GDK_IS_WAYLAND_DISPLAY
84 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
89 #ifdef GDK_WINDOWING_WIN32
91 #ifndef GDK_IS_WIN32_DISPLAY
92 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
97 #ifdef GDK_WINDOWING_BROADWAY
99 #ifndef GDK_IS_BROADWAY_DISPLAY
100 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
105 #ifdef GDK_WINDOWING_QUARTZ
107 #ifndef GDK_IS_QUARTZ_DISPLAY
108 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
113 #if !defined(CONFIG_VTE)
114 # define VTE_CHECK_VERSION(a, b, c) 0
117 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
119 static const guint16
*keycode_map
;
120 static size_t keycode_maplen
;
124 VirtualConsole
*console
;
127 typedef struct VCChardev VCChardev
;
129 #define TYPE_CHARDEV_VC "chardev-vc"
130 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
133 bool gtk_use_gl_area
;
135 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
136 static void gd_ungrab_pointer(GtkDisplayState
*s
);
137 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
138 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
140 /** Utility Functions **/
142 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
147 for (i
= 0; i
< s
->nb_vcs
; i
++) {
149 if (gtk_check_menu_item_get_active
150 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
157 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
162 for (i
= 0; i
< s
->nb_vcs
; i
++) {
164 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
172 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
176 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
177 return gd_vc_find_by_page(s
, page
);
180 static bool gd_is_grab_active(GtkDisplayState
*s
)
182 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
185 static bool gd_grab_on_hover(GtkDisplayState
*s
)
187 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
190 static void gd_update_cursor(VirtualConsole
*vc
)
192 GtkDisplayState
*s
= vc
->s
;
195 if (vc
->type
!= GD_VC_GFX
||
196 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
200 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
204 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
205 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
206 gdk_window_set_cursor(window
, s
->null_cursor
);
208 gdk_window_set_cursor(window
, NULL
);
212 static void gd_update_caption(GtkDisplayState
*s
)
214 const char *status
= "";
217 const char *grab
= "";
218 bool is_paused
= !runstate_is_running();
222 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
224 prefix
= g_strdup_printf("QEMU");
227 if (s
->ptr_owner
!= NULL
&&
228 s
->ptr_owner
->window
== NULL
) {
229 grab
= _(" - Press Ctrl+Alt+G to release grab");
233 status
= _(" [Paused]");
235 s
->external_pause_update
= true;
236 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
238 s
->external_pause_update
= false;
240 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
241 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
244 for (i
= 0; i
< s
->nb_vcs
; i
++) {
245 VirtualConsole
*vc
= &s
->vc
[i
];
250 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
251 vc
== s
->kbd_owner
? " +kbd" : "",
252 vc
== s
->ptr_owner
? " +ptr" : "");
253 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
260 static void gd_update_geometry_hints(VirtualConsole
*vc
)
262 GtkDisplayState
*s
= vc
->s
;
263 GdkWindowHints mask
= 0;
264 GdkGeometry geo
= {};
265 GtkWidget
*geo_widget
= NULL
;
266 GtkWindow
*geo_window
;
268 if (vc
->type
== GD_VC_GFX
) {
273 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
274 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
275 mask
|= GDK_HINT_MIN_SIZE
;
277 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
278 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
279 mask
|= GDK_HINT_MIN_SIZE
;
281 geo_widget
= vc
->gfx
.drawing_area
;
282 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
284 #if defined(CONFIG_VTE)
285 } else if (vc
->type
== GD_VC_VTE
) {
286 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
287 GtkBorder padding
= { 0 };
289 #if VTE_CHECK_VERSION(0, 37, 0)
290 gtk_style_context_get_padding(
291 gtk_widget_get_style_context(vc
->vte
.terminal
),
292 gtk_widget_get_state_flags(vc
->vte
.terminal
),
296 GtkBorder
*ib
= NULL
;
297 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
305 geo
.width_inc
= vte_terminal_get_char_width(term
);
306 geo
.height_inc
= vte_terminal_get_char_height(term
);
307 mask
|= GDK_HINT_RESIZE_INC
;
308 geo
.base_width
= geo
.width_inc
;
309 geo
.base_height
= geo
.height_inc
;
310 mask
|= GDK_HINT_BASE_SIZE
;
311 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
312 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
313 mask
|= GDK_HINT_MIN_SIZE
;
315 geo
.base_width
+= padding
.left
+ padding
.right
;
316 geo
.base_height
+= padding
.top
+ padding
.bottom
;
317 geo
.min_width
+= padding
.left
+ padding
.right
;
318 geo
.min_height
+= padding
.top
+ padding
.bottom
;
319 geo_widget
= vc
->vte
.terminal
;
323 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
324 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
327 void gd_update_windowsize(VirtualConsole
*vc
)
329 GtkDisplayState
*s
= vc
->s
;
331 gd_update_geometry_hints(vc
);
333 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
334 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
335 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
339 static void gd_update_full_redraw(VirtualConsole
*vc
)
341 GtkWidget
*area
= vc
->gfx
.drawing_area
;
343 ww
= gdk_window_get_width(gtk_widget_get_window(area
));
344 wh
= gdk_window_get_height(gtk_widget_get_window(area
));
345 #if defined(CONFIG_OPENGL)
346 if (vc
->gfx
.gls
&& gtk_use_gl_area
) {
347 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
351 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
354 static void gtk_release_modifiers(GtkDisplayState
*s
)
356 VirtualConsole
*vc
= gd_vc_find_current(s
);
358 if (vc
->type
!= GD_VC_GFX
||
359 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
362 qkbd_state_lift_all_keys(vc
->gfx
.kbd
);
365 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
368 g_object_ref(G_OBJECT(widget
));
369 gtk_container_remove(GTK_CONTAINER(from
), widget
);
370 gtk_container_add(GTK_CONTAINER(to
), widget
);
371 g_object_unref(G_OBJECT(widget
));
374 static void *gd_win32_get_hwnd(VirtualConsole
*vc
)
377 return gdk_win32_window_get_impl_hwnd(
378 gtk_widget_get_window(vc
->window
? vc
->window
: vc
->s
->window
));
384 /** DisplayState Callbacks **/
386 static void gd_update(DisplayChangeListener
*dcl
,
387 int x
, int y
, int w
, int h
)
389 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
396 trace_gd_update(vc
->label
, x
, y
, w
, h
);
398 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
402 if (vc
->gfx
.convert
) {
403 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
404 NULL
, vc
->gfx
.convert
,
405 x
, y
, 0, 0, x
, y
, w
, h
);
408 x1
= floor(x
* vc
->gfx
.scale_x
);
409 y1
= floor(y
* vc
->gfx
.scale_y
);
411 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
412 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
414 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
415 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
417 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
421 ww
= gdk_window_get_width(win
);
422 wh
= gdk_window_get_height(win
);
432 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
433 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
436 static void gd_refresh(DisplayChangeListener
*dcl
)
438 graphic_hw_update(dcl
->con
);
441 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
443 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
446 static void gd_mouse_set(DisplayChangeListener
*dcl
,
447 int x
, int y
, int visible
)
449 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
453 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
) ||
454 qemu_input_is_absolute()) {
458 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
459 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
460 x
, y
, &x_root
, &y_root
);
461 gdk_device_warp(gd_get_pointer(dpy
),
462 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
468 static void gd_cursor_define(DisplayChangeListener
*dcl
,
471 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
475 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
479 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
480 GDK_COLORSPACE_RGB
, true, 8,
481 c
->width
, c
->height
, c
->width
* 4,
483 cursor
= gdk_cursor_new_from_pixbuf
484 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
485 pixbuf
, c
->hot_x
, c
->hot_y
);
486 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
487 g_object_unref(pixbuf
);
488 g_object_unref(cursor
);
491 static void gd_switch(DisplayChangeListener
*dcl
,
492 DisplaySurface
*surface
)
494 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
497 trace_gd_switch(vc
->label
, surface_width(surface
), surface_height(surface
));
499 if (vc
->gfx
.surface
) {
500 cairo_surface_destroy(vc
->gfx
.surface
);
501 vc
->gfx
.surface
= NULL
;
503 if (vc
->gfx
.convert
) {
504 pixman_image_unref(vc
->gfx
.convert
);
505 vc
->gfx
.convert
= NULL
;
509 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
510 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
513 vc
->gfx
.ds
= surface
;
515 if (surface
->format
== PIXMAN_x8r8g8b8
) {
517 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
519 * No need to convert, use surface directly. Should be the
520 * common case as this is qemu_default_pixelformat(32) too.
522 vc
->gfx
.surface
= cairo_image_surface_create_for_data
523 (surface_data(surface
),
525 surface_width(surface
),
526 surface_height(surface
),
527 surface_stride(surface
));
529 /* Must convert surface, use pixman to do it. */
530 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
531 surface_width(surface
),
532 surface_height(surface
),
534 vc
->gfx
.surface
= cairo_image_surface_create_for_data
535 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
537 pixman_image_get_width(vc
->gfx
.convert
),
538 pixman_image_get_height(vc
->gfx
.convert
),
539 pixman_image_get_stride(vc
->gfx
.convert
));
540 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
541 NULL
, vc
->gfx
.convert
,
543 pixman_image_get_width(vc
->gfx
.convert
),
544 pixman_image_get_height(vc
->gfx
.convert
));
548 gd_update_windowsize(vc
);
550 gd_update_full_redraw(vc
);
554 static const DisplayChangeListenerOps dcl_ops
= {
556 .dpy_gfx_update
= gd_update
,
557 .dpy_gfx_switch
= gd_switch
,
558 .dpy_gfx_check_format
= qemu_pixman_check_format
,
559 .dpy_refresh
= gd_refresh
,
560 .dpy_mouse_set
= gd_mouse_set
,
561 .dpy_cursor_define
= gd_cursor_define
,
565 #if defined(CONFIG_OPENGL)
567 static bool gd_has_dmabuf(DisplayChangeListener
*dcl
)
569 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
571 if (gtk_use_gl_area
&& !gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
572 /* FIXME: Assume it will work, actual check done after realize */
573 /* fixing this would require delaying listener registration */
577 return vc
->gfx
.has_dmabuf
;
580 static void gd_gl_release_dmabuf(DisplayChangeListener
*dcl
,
584 egl_dmabuf_release_texture(dmabuf
);
588 void gd_hw_gl_flushed(void *vcon
)
590 VirtualConsole
*vc
= vcon
;
591 QemuDmaBuf
*dmabuf
= vc
->gfx
.guest_fb
.dmabuf
;
593 qemu_set_fd_handler(dmabuf
->fence_fd
, NULL
, NULL
, NULL
);
594 close(dmabuf
->fence_fd
);
595 dmabuf
->fence_fd
= -1;
596 graphic_hw_gl_block(vc
->gfx
.dcl
.con
, false);
599 /** DisplayState Callbacks (opengl version) **/
601 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
602 .dpy_name
= "gtk-egl",
603 .dpy_gfx_update
= gd_gl_area_update
,
604 .dpy_gfx_switch
= gd_gl_area_switch
,
605 .dpy_gfx_check_format
= console_gl_check_format
,
606 .dpy_refresh
= gd_gl_area_refresh
,
607 .dpy_mouse_set
= gd_mouse_set
,
608 .dpy_cursor_define
= gd_cursor_define
,
610 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
611 .dpy_gl_scanout_disable
= gd_gl_area_scanout_disable
,
612 .dpy_gl_update
= gd_gl_area_scanout_flush
,
613 .dpy_gl_scanout_dmabuf
= gd_gl_area_scanout_dmabuf
,
614 .dpy_gl_release_dmabuf
= gd_gl_release_dmabuf
,
615 .dpy_has_dmabuf
= gd_has_dmabuf
,
619 gd_gl_area_is_compatible_dcl(DisplayGLCtx
*dgc
,
620 DisplayChangeListener
*dcl
)
622 return dcl
->ops
== &dcl_gl_area_ops
;
625 static const DisplayGLCtxOps gl_area_ctx_ops
= {
626 .dpy_gl_ctx_is_compatible_dcl
= gd_gl_area_is_compatible_dcl
,
627 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
628 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
629 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
633 static const DisplayChangeListenerOps dcl_egl_ops
= {
634 .dpy_name
= "gtk-egl",
635 .dpy_gfx_update
= gd_egl_update
,
636 .dpy_gfx_switch
= gd_egl_switch
,
637 .dpy_gfx_check_format
= console_gl_check_format
,
638 .dpy_refresh
= gd_egl_refresh
,
639 .dpy_mouse_set
= gd_mouse_set
,
640 .dpy_cursor_define
= gd_cursor_define
,
642 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
643 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
644 .dpy_gl_scanout_dmabuf
= gd_egl_scanout_dmabuf
,
645 .dpy_gl_cursor_dmabuf
= gd_egl_cursor_dmabuf
,
646 .dpy_gl_cursor_position
= gd_egl_cursor_position
,
647 .dpy_gl_update
= gd_egl_flush
,
648 .dpy_gl_release_dmabuf
= gd_gl_release_dmabuf
,
649 .dpy_has_dmabuf
= gd_has_dmabuf
,
653 gd_egl_is_compatible_dcl(DisplayGLCtx
*dgc
,
654 DisplayChangeListener
*dcl
)
656 return dcl
->ops
== &dcl_egl_ops
;
659 static const DisplayGLCtxOps egl_ctx_ops
= {
660 .dpy_gl_ctx_is_compatible_dcl
= gd_egl_is_compatible_dcl
,
661 .dpy_gl_ctx_create
= gd_egl_create_context
,
662 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
663 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
667 #endif /* CONFIG_OPENGL */
671 static void gd_change_runstate(void *opaque
, bool running
, RunState state
)
673 GtkDisplayState
*s
= opaque
;
675 gd_update_caption(s
);
678 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
683 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
684 /* release the grab at switching to absolute mode */
685 if (qemu_input_is_absolute() && s
->ptr_owner
) {
686 if (!s
->ptr_owner
->window
) {
687 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
690 gd_ungrab_pointer(s
);
693 for (i
= 0; i
< s
->nb_vcs
; i
++) {
694 VirtualConsole
*vc
= &s
->vc
[i
];
695 gd_update_cursor(vc
);
701 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
704 GtkDisplayState
*s
= opaque
;
705 bool allow_close
= true;
707 if (s
->opts
->has_window_close
&& !s
->opts
->window_close
) {
718 static void gd_set_ui_refresh_rate(VirtualConsole
*vc
, int refresh_rate
)
722 info
= *dpy_get_ui_info(vc
->gfx
.dcl
.con
);
723 info
.refresh_rate
= refresh_rate
;
724 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
, true);
727 static void gd_set_ui_size(VirtualConsole
*vc
, gint width
, gint height
)
731 info
= *dpy_get_ui_info(vc
->gfx
.dcl
.con
);
733 info
.height
= height
;
734 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
, true);
737 #if defined(CONFIG_OPENGL)
739 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
742 VirtualConsole
*vc
= opaque
;
750 static void gd_resize_event(GtkGLArea
*area
,
751 gint width
, gint height
, gpointer
*opaque
)
753 VirtualConsole
*vc
= (void *)opaque
;
755 gd_set_ui_size(vc
, width
, height
);
760 void gd_update_monitor_refresh_rate(VirtualConsole
*vc
, GtkWidget
*widget
)
762 #ifdef GDK_VERSION_3_22
763 GdkWindow
*win
= gtk_widget_get_window(widget
);
767 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
768 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
769 refresh_rate
= gdk_monitor_get_refresh_rate(monitor
); /* [mHz] */
774 gd_set_ui_refresh_rate(vc
, refresh_rate
);
776 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
777 vc
->gfx
.dcl
.update_interval
= refresh_rate
?
778 MIN(1000 * 1000 / refresh_rate
, GUI_REFRESH_INTERVAL_DEFAULT
) :
779 GUI_REFRESH_INTERVAL_DEFAULT
;
783 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
785 VirtualConsole
*vc
= opaque
;
786 GtkDisplayState
*s
= vc
->s
;
791 #if defined(CONFIG_OPENGL)
793 if (gtk_use_gl_area
) {
794 /* invoke render callback please */
807 if (!gtk_widget_get_realized(widget
)) {
813 if (!vc
->gfx
.surface
) {
817 gd_update_monitor_refresh_rate(vc
, vc
->window
? vc
->window
: s
->window
);
819 fbw
= surface_width(vc
->gfx
.ds
);
820 fbh
= surface_height(vc
->gfx
.ds
);
822 ww
= gdk_window_get_width(gtk_widget_get_window(widget
));
823 wh
= gdk_window_get_height(gtk_widget_get_window(widget
));
825 if (s
->full_screen
) {
826 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
827 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
828 } else if (s
->free_scale
) {
831 sx
= (double)ww
/ fbw
;
832 sy
= (double)wh
/ fbh
;
834 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
837 fbw
*= vc
->gfx
.scale_x
;
838 fbh
*= vc
->gfx
.scale_y
;
848 cairo_rectangle(cr
, 0, 0, ww
, wh
);
850 /* Optionally cut out the inner area where the pixmap
851 will be drawn. This avoids 'flashing' since we're
852 not double-buffering. Note we're using the undocumented
853 behaviour of drawing the rectangle from right to left
854 to cut out the whole */
855 cairo_rectangle(cr
, mx
+ fbw
, my
,
859 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
860 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
861 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
867 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
870 VirtualConsole
*vc
= opaque
;
871 GtkDisplayState
*s
= vc
->s
;
882 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
883 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
885 window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
886 ww
= gdk_window_get_width(window
);
887 wh
= gdk_window_get_height(window
);
888 ws
= gdk_window_get_scale_factor(window
);
898 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
* ws
;
899 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
* ws
;
901 if (qemu_input_is_absolute()) {
902 if (x
< 0 || y
< 0 ||
903 x
>= surface_width(vc
->gfx
.ds
) ||
904 y
>= surface_height(vc
->gfx
.ds
)) {
907 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
908 0, surface_width(vc
->gfx
.ds
));
909 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
910 0, surface_height(vc
->gfx
.ds
));
911 qemu_input_event_sync();
912 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
913 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
914 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
915 qemu_input_event_sync();
921 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
922 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
923 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
924 GdkWindow
*win
= gtk_widget_get_window(widget
);
925 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
926 GdkRectangle geometry
;
928 int x
= (int)motion
->x_root
;
929 int y
= (int)motion
->y_root
;
931 gdk_monitor_get_geometry(monitor
, &geometry
);
933 /* In relative mode check to see if client pointer hit
934 * one of the monitor edges, and if so move it back to the
935 * center of the monitor. This is important because the pointer
936 * in the server doesn't correspond 1-for-1, and so
937 * may still be only half way across the screen. Without
938 * this warp, the server pointer would thus appear to hit
939 * an invisible wall */
940 if (x
<= geometry
.x
|| x
- geometry
.x
>= geometry
.width
- 1 ||
941 y
<= geometry
.y
|| y
- geometry
.y
>= geometry
.height
- 1) {
942 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
943 x
= geometry
.x
+ geometry
.width
/ 2;
944 y
= geometry
.y
+ geometry
.height
/ 2;
946 gdk_device_warp(dev
, screen
, x
, y
);
954 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
957 VirtualConsole
*vc
= opaque
;
958 GtkDisplayState
*s
= vc
->s
;
961 /* implicitly grab the input at the first click in the relative mode */
962 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
963 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
965 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
968 gd_grab_pointer(vc
, "relative-mode-click");
973 if (button
->button
== 1) {
974 btn
= INPUT_BUTTON_LEFT
;
975 } else if (button
->button
== 2) {
976 btn
= INPUT_BUTTON_MIDDLE
;
977 } else if (button
->button
== 3) {
978 btn
= INPUT_BUTTON_RIGHT
;
979 } else if (button
->button
== 8) {
980 btn
= INPUT_BUTTON_SIDE
;
981 } else if (button
->button
== 9) {
982 btn
= INPUT_BUTTON_EXTRA
;
987 if (button
->type
== GDK_2BUTTON_PRESS
|| button
->type
== GDK_3BUTTON_PRESS
) {
991 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
992 button
->type
== GDK_BUTTON_PRESS
);
993 qemu_input_event_sync();
997 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
1000 VirtualConsole
*vc
= opaque
;
1001 InputButton btn_vertical
;
1002 InputButton btn_horizontal
;
1003 bool has_vertical
= false;
1004 bool has_horizontal
= false;
1006 if (scroll
->direction
== GDK_SCROLL_UP
) {
1007 btn_vertical
= INPUT_BUTTON_WHEEL_UP
;
1008 has_vertical
= true;
1009 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
1010 btn_vertical
= INPUT_BUTTON_WHEEL_DOWN
;
1011 has_vertical
= true;
1012 } else if (scroll
->direction
== GDK_SCROLL_LEFT
) {
1013 btn_horizontal
= INPUT_BUTTON_WHEEL_LEFT
;
1014 has_horizontal
= true;
1015 } else if (scroll
->direction
== GDK_SCROLL_RIGHT
) {
1016 btn_horizontal
= INPUT_BUTTON_WHEEL_RIGHT
;
1017 has_horizontal
= true;
1018 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
1019 gdouble delta_x
, delta_y
;
1020 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
1021 &delta_x
, &delta_y
)) {
1026 btn_vertical
= INPUT_BUTTON_WHEEL_DOWN
;
1027 has_vertical
= true;
1028 } else if (delta_y
< 0) {
1029 btn_vertical
= INPUT_BUTTON_WHEEL_UP
;
1030 has_vertical
= true;
1031 } else if (delta_x
> 0) {
1032 btn_horizontal
= INPUT_BUTTON_WHEEL_RIGHT
;
1033 has_horizontal
= true;
1034 } else if (delta_x
< 0) {
1035 btn_horizontal
= INPUT_BUTTON_WHEEL_LEFT
;
1036 has_horizontal
= true;
1045 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn_vertical
, true);
1046 qemu_input_event_sync();
1047 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn_vertical
, false);
1048 qemu_input_event_sync();
1051 if (has_horizontal
) {
1052 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn_horizontal
, true);
1053 qemu_input_event_sync();
1054 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn_horizontal
, false);
1055 qemu_input_event_sync();
1062 static const guint16
*gd_get_keymap(size_t *maplen
)
1064 GdkDisplay
*dpy
= gdk_display_get_default();
1066 #ifdef GDK_WINDOWING_X11
1067 if (GDK_IS_X11_DISPLAY(dpy
)) {
1068 trace_gd_keymap_windowing("x11");
1069 return qemu_xkeymap_mapping_table(
1070 gdk_x11_display_get_xdisplay(dpy
), maplen
);
1074 #ifdef GDK_WINDOWING_WAYLAND
1075 if (GDK_IS_WAYLAND_DISPLAY(dpy
)) {
1076 trace_gd_keymap_windowing("wayland");
1077 *maplen
= qemu_input_map_xorgevdev_to_qcode_len
;
1078 return qemu_input_map_xorgevdev_to_qcode
;
1082 #ifdef GDK_WINDOWING_WIN32
1083 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1084 trace_gd_keymap_windowing("win32");
1085 *maplen
= qemu_input_map_atset1_to_qcode_len
;
1086 return qemu_input_map_atset1_to_qcode
;
1090 #ifdef GDK_WINDOWING_QUARTZ
1091 if (GDK_IS_QUARTZ_DISPLAY(dpy
)) {
1092 trace_gd_keymap_windowing("quartz");
1093 *maplen
= qemu_input_map_osx_to_qcode_len
;
1094 return qemu_input_map_osx_to_qcode
;
1098 #ifdef GDK_WINDOWING_BROADWAY
1099 if (GDK_IS_BROADWAY_DISPLAY(dpy
)) {
1100 trace_gd_keymap_windowing("broadway");
1101 g_warning("experimental: using broadway, x11 virtual keysym\n"
1102 "mapping - with very limited support. See also\n"
1103 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1104 *maplen
= qemu_input_map_x11_to_qcode_len
;
1105 return qemu_input_map_x11_to_qcode
;
1109 g_warning("Unsupported GDK Windowing platform.\n"
1110 "Disabling extended keycode tables.\n"
1111 "Please report to qemu-devel@nongnu.org\n"
1112 "including the following information:\n"
1114 " - Operating system\n"
1115 " - GDK Windowing system build\n");
1120 static int gd_map_keycode(int scancode
)
1125 if (scancode
> keycode_maplen
) {
1129 return keycode_map
[scancode
];
1132 static int gd_get_keycode(GdkEventKey
*key
)
1135 int scancode
= gdk_event_get_scancode((GdkEvent
*)key
);
1137 /* translate Windows native scancodes to atset1 keycodes */
1138 switch (scancode
& (KF_EXTENDED
| 0xff)) {
1139 case 0x145: /* NUMLOCK */
1140 return scancode
& 0xff;
1143 return scancode
& KF_EXTENDED
?
1144 0xe000 | (scancode
& 0xff) : scancode
& 0xff;
1147 return key
->hardware_keycode
;
1151 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1152 GdkEventKey
*key
, void *opaque
)
1154 VirtualConsole
*vc
= opaque
;
1155 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1157 if (key
->keyval
== GDK_KEY_Delete
) {
1158 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
, false);
1159 } else if (key
->length
) {
1160 kbd_put_string_console(con
, key
->string
, key
->length
);
1162 int qcode
= gd_map_keycode(gd_get_keycode(key
));
1163 kbd_put_qcode_console(con
, qcode
, false);
1168 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1170 VirtualConsole
*vc
= opaque
;
1174 /* on windows, we ought to ignore the reserved key event? */
1175 if (key
->hardware_keycode
== 0xff)
1178 if (!vc
->s
->kbd_owner
) {
1179 if (key
->hardware_keycode
== VK_LWIN
||
1180 key
->hardware_keycode
== VK_RWIN
) {
1186 if (key
->keyval
== GDK_KEY_Pause
1188 /* for some reason GDK does not fill keyval for VK_PAUSE
1189 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1191 || key
->hardware_keycode
== VK_PAUSE
1194 qkbd_state_key_event(vc
->gfx
.kbd
, Q_KEY_CODE_PAUSE
,
1195 key
->type
== GDK_KEY_PRESS
);
1199 keycode
= gd_get_keycode(key
);
1200 qcode
= gd_map_keycode(keycode
);
1202 trace_gd_key_event(vc
->label
, keycode
, qcode
,
1203 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1205 qkbd_state_key_event(vc
->gfx
.kbd
, qcode
,
1206 key
->type
== GDK_KEY_PRESS
);
1211 static gboolean
gd_grab_broken_event(GtkWidget
*widget
,
1212 GdkEventGrabBroken
*event
, void *opaque
)
1216 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1217 * key combination leaves all three keys in a stuck condition. We use
1218 * the grab-broken-event to release all keys.
1220 if (event
->keyboard
) {
1221 VirtualConsole
*vc
= opaque
;
1222 GtkDisplayState
*s
= vc
->s
;
1224 gtk_release_modifiers(s
);
1230 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1232 if (event
->type
== GDK_MOTION_NOTIFY
) {
1233 return gd_motion_event(widget
, &event
->motion
, opaque
);
1238 /** Window Menu Actions **/
1240 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1242 GtkDisplayState
*s
= opaque
;
1244 if (s
->external_pause_update
) {
1247 if (runstate_is_running()) {
1254 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1256 qmp_system_reset(NULL
);
1259 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1261 qmp_system_powerdown(NULL
);
1264 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1269 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1271 GtkDisplayState
*s
= opaque
;
1272 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1273 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1276 gtk_release_modifiers(s
);
1278 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1279 gtk_notebook_set_current_page(nb
, page
);
1280 gtk_widget_grab_focus(vc
->focus
);
1284 static void gd_accel_switch_vc(void *opaque
)
1286 VirtualConsole
*vc
= opaque
;
1288 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1291 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1293 GtkDisplayState
*s
= opaque
;
1294 VirtualConsole
*vc
= gd_vc_find_current(s
);
1296 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1297 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1299 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1301 gd_update_windowsize(vc
);
1304 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1307 VirtualConsole
*vc
= opaque
;
1308 GtkDisplayState
*s
= vc
->s
;
1310 gtk_widget_set_sensitive(vc
->menu_item
, true);
1311 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1312 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1313 vc
->tab_item
, vc
->label
);
1314 gtk_widget_destroy(vc
->window
);
1316 #if defined(CONFIG_OPENGL)
1317 if (vc
->gfx
.esurface
) {
1318 eglDestroySurface(qemu_egl_display
, vc
->gfx
.esurface
);
1319 vc
->gfx
.esurface
= NULL
;
1322 eglDestroyContext(qemu_egl_display
, vc
->gfx
.ectx
);
1323 vc
->gfx
.ectx
= NULL
;
1329 static gboolean
gd_win_grab(void *opaque
)
1331 VirtualConsole
*vc
= opaque
;
1333 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1334 if (vc
->s
->ptr_owner
) {
1335 gd_ungrab_pointer(vc
->s
);
1337 gd_grab_pointer(vc
, "user-request-detached-tab");
1342 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1344 GtkDisplayState
*s
= opaque
;
1345 VirtualConsole
*vc
= gd_vc_find_current(s
);
1347 if (vc
->type
== GD_VC_GFX
&&
1348 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1349 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1353 gtk_widget_set_sensitive(vc
->menu_item
, false);
1354 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1355 #if defined(CONFIG_OPENGL)
1356 if (vc
->gfx
.esurface
) {
1357 eglDestroySurface(qemu_egl_display
, vc
->gfx
.esurface
);
1358 vc
->gfx
.esurface
= NULL
;
1360 if (vc
->gfx
.esurface
) {
1361 eglDestroyContext(qemu_egl_display
, vc
->gfx
.ectx
);
1362 vc
->gfx
.ectx
= NULL
;
1365 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1367 g_signal_connect(vc
->window
, "delete-event",
1368 G_CALLBACK(gd_tab_window_close
), vc
);
1369 gtk_widget_show_all(vc
->window
);
1371 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1372 GtkAccelGroup
*ag
= gtk_accel_group_new();
1373 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1375 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1377 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1380 gd_update_geometry_hints(vc
);
1381 gd_update_caption(s
);
1385 static void gd_menu_show_menubar(GtkMenuItem
*item
, void *opaque
)
1387 GtkDisplayState
*s
= opaque
;
1388 VirtualConsole
*vc
= gd_vc_find_current(s
);
1390 if (s
->full_screen
) {
1394 if (gtk_check_menu_item_get_active(
1395 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1396 gtk_widget_show(s
->menu_bar
);
1398 gtk_widget_hide(s
->menu_bar
);
1400 gd_update_windowsize(vc
);
1403 static void gd_accel_show_menubar(void *opaque
)
1405 GtkDisplayState
*s
= opaque
;
1406 gtk_menu_item_activate(GTK_MENU_ITEM(s
->show_menubar_item
));
1409 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1411 GtkDisplayState
*s
= opaque
;
1412 VirtualConsole
*vc
= gd_vc_find_current(s
);
1414 if (!s
->full_screen
) {
1415 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1416 gtk_widget_hide(s
->menu_bar
);
1417 if (vc
->type
== GD_VC_GFX
) {
1418 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1420 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1421 s
->full_screen
= TRUE
;
1423 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1424 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1425 if (gtk_check_menu_item_get_active(
1426 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1427 gtk_widget_show(s
->menu_bar
);
1429 s
->full_screen
= FALSE
;
1430 if (vc
->type
== GD_VC_GFX
) {
1431 vc
->gfx
.scale_x
= 1.0;
1432 vc
->gfx
.scale_y
= 1.0;
1433 gd_update_windowsize(vc
);
1437 gd_update_cursor(vc
);
1440 static void gd_accel_full_screen(void *opaque
)
1442 GtkDisplayState
*s
= opaque
;
1443 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1446 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1448 GtkDisplayState
*s
= opaque
;
1449 VirtualConsole
*vc
= gd_vc_find_current(s
);
1451 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1454 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1455 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1457 gd_update_windowsize(vc
);
1460 static void gd_accel_zoom_in(void *opaque
)
1462 GtkDisplayState
*s
= opaque
;
1463 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1466 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1468 GtkDisplayState
*s
= opaque
;
1469 VirtualConsole
*vc
= gd_vc_find_current(s
);
1471 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1474 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1475 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1477 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1478 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1480 gd_update_windowsize(vc
);
1483 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1485 GtkDisplayState
*s
= opaque
;
1486 VirtualConsole
*vc
= gd_vc_find_current(s
);
1488 vc
->gfx
.scale_x
= 1.0;
1489 vc
->gfx
.scale_y
= 1.0;
1491 gd_update_windowsize(vc
);
1494 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1496 GtkDisplayState
*s
= opaque
;
1497 VirtualConsole
*vc
= gd_vc_find_current(s
);
1499 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1500 s
->free_scale
= TRUE
;
1502 s
->free_scale
= FALSE
;
1503 vc
->gfx
.scale_x
= 1.0;
1504 vc
->gfx
.scale_y
= 1.0;
1507 gd_update_windowsize(vc
);
1508 gd_update_full_redraw(vc
);
1511 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1513 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1514 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1515 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1516 GdkSeatCapabilities caps
= 0;
1517 GdkCursor
*cursor
= NULL
;
1520 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1523 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1524 cursor
= vc
->s
->null_cursor
;
1528 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1531 gdk_seat_ungrab(seat
);
1535 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1537 if (vc
->s
->kbd_owner
) {
1538 if (vc
->s
->kbd_owner
== vc
) {
1541 gd_ungrab_keyboard(vc
->s
);
1545 win32_kbd_set_grab(true);
1546 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1547 vc
->s
->kbd_owner
= vc
;
1548 gd_update_caption(vc
->s
);
1549 trace_gd_grab(vc
->label
, "kbd", reason
);
1552 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1554 VirtualConsole
*vc
= s
->kbd_owner
;
1559 s
->kbd_owner
= NULL
;
1561 win32_kbd_set_grab(false);
1562 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1563 gd_update_caption(s
);
1564 trace_gd_ungrab(vc
->label
, "kbd");
1567 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1569 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1571 if (vc
->s
->ptr_owner
) {
1572 if (vc
->s
->ptr_owner
== vc
) {
1575 gd_ungrab_pointer(vc
->s
);
1579 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1580 gdk_device_get_position(gd_get_pointer(display
),
1581 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1582 vc
->s
->ptr_owner
= vc
;
1583 gd_update_caption(vc
->s
);
1584 trace_gd_grab(vc
->label
, "ptr", reason
);
1587 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1589 VirtualConsole
*vc
= s
->ptr_owner
;
1590 GdkDisplay
*display
;
1595 s
->ptr_owner
= NULL
;
1597 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1598 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1599 gdk_device_warp(gd_get_pointer(display
),
1600 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1601 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1602 gd_update_caption(s
);
1603 trace_gd_ungrab(vc
->label
, "ptr");
1606 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1608 GtkDisplayState
*s
= opaque
;
1609 VirtualConsole
*vc
= gd_vc_find_current(s
);
1611 if (gd_is_grab_active(s
)) {
1612 gd_grab_keyboard(vc
, "user-request-main-window");
1613 gd_grab_pointer(vc
, "user-request-main-window");
1615 gd_ungrab_keyboard(s
);
1616 gd_ungrab_pointer(s
);
1619 gd_update_cursor(vc
);
1622 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1625 GtkDisplayState
*s
= data
;
1629 if (!gtk_widget_get_realized(s
->notebook
)) {
1633 vc
= gd_vc_find_by_page(s
, arg2
);
1637 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1639 on_vga
= (vc
->type
== GD_VC_GFX
&&
1640 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1642 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1644 } else if (s
->full_screen
) {
1645 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1648 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1650 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1653 gd_update_windowsize(vc
);
1654 gd_update_cursor(vc
);
1657 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1660 VirtualConsole
*vc
= opaque
;
1661 GtkDisplayState
*s
= vc
->s
;
1663 if (gd_grab_on_hover(s
)) {
1664 gd_grab_keyboard(vc
, "grab-on-hover");
1669 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1672 VirtualConsole
*vc
= opaque
;
1673 GtkDisplayState
*s
= vc
->s
;
1675 if (gd_grab_on_hover(s
)) {
1676 gd_ungrab_keyboard(s
);
1681 static gboolean
gd_focus_in_event(GtkWidget
*widget
,
1682 GdkEventFocus
*event
, gpointer opaque
)
1684 VirtualConsole
*vc
= opaque
;
1686 win32_kbd_set_window(gd_win32_get_hwnd(vc
));
1690 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1691 GdkEventFocus
*event
, gpointer opaque
)
1693 VirtualConsole
*vc
= opaque
;
1694 GtkDisplayState
*s
= vc
->s
;
1696 win32_kbd_set_window(NULL
);
1697 gtk_release_modifiers(s
);
1701 static gboolean
gd_configure(GtkWidget
*widget
,
1702 GdkEventConfigure
*cfg
, gpointer opaque
)
1704 VirtualConsole
*vc
= opaque
;
1706 gd_set_ui_size(vc
, cfg
->width
, cfg
->height
);
1710 /** Virtual Console Callbacks **/
1712 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1713 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1715 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1716 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1717 HOTKEY_MODIFIERS
, 0,
1718 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1719 gtk_accel_label_set_accel(
1720 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1721 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1723 g_signal_connect(vc
->menu_item
, "activate",
1724 G_CALLBACK(gd_menu_switch_vc
), s
);
1725 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1727 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1730 #if defined(CONFIG_VTE)
1731 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1733 GtkDisplayState
*s
= opaque
;
1734 VirtualConsole
*vc
= gd_vc_find_current(s
);
1736 #if VTE_CHECK_VERSION(0, 50, 0)
1737 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1740 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1744 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1746 VirtualConsole
*vc
= opaque
;
1748 if (gtk_adjustment_get_upper(adjustment
) >
1749 gtk_adjustment_get_page_size(adjustment
)) {
1750 gtk_widget_show(vc
->vte
.scrollbar
);
1752 gtk_widget_hide(vc
->vte
.scrollbar
);
1756 static void gd_vc_send_chars(VirtualConsole
*vc
)
1758 uint32_t len
, avail
;
1760 len
= qemu_chr_be_can_write(vc
->vte
.chr
);
1761 avail
= fifo8_num_used(&vc
->vte
.out_fifo
);
1762 while (len
> 0 && avail
> 0) {
1766 buf
= fifo8_pop_buf(&vc
->vte
.out_fifo
, MIN(len
, avail
), &size
);
1767 qemu_chr_be_write(vc
->vte
.chr
, buf
, size
);
1768 len
= qemu_chr_be_can_write(vc
->vte
.chr
);
1773 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1775 VCChardev
*vcd
= VC_CHARDEV(chr
);
1776 VirtualConsole
*vc
= vcd
->console
;
1778 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1782 static void gd_vc_chr_accept_input(Chardev
*chr
)
1784 VCChardev
*vcd
= VC_CHARDEV(chr
);
1785 VirtualConsole
*vc
= vcd
->console
;
1788 gd_vc_send_chars(vc
);
1792 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1794 VCChardev
*vcd
= VC_CHARDEV(chr
);
1795 VirtualConsole
*vc
= vcd
->console
;
1798 vc
->vte
.echo
= echo
;
1805 static Chardev
*vcs
[MAX_VCS
];
1806 static void gd_vc_open(Chardev
*chr
,
1807 ChardevBackend
*backend
,
1811 if (nb_vcs
== MAX_VCS
) {
1812 error_setg(errp
, "Maximum number of consoles reached");
1816 vcs
[nb_vcs
++] = chr
;
1818 /* console/chardev init sometimes completes elsewhere in a 2nd
1819 * stage, so defer OPENED events until they are fully initialized
1824 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1826 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1828 cc
->parse
= qemu_chr_parse_vc
;
1829 cc
->open
= gd_vc_open
;
1830 cc
->chr_write
= gd_vc_chr_write
;
1831 cc
->chr_accept_input
= gd_vc_chr_accept_input
;
1832 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1835 static const TypeInfo char_gd_vc_type_info
= {
1836 .name
= TYPE_CHARDEV_VC
,
1837 .parent
= TYPE_CHARDEV
,
1838 .instance_size
= sizeof(VCChardev
),
1839 .class_init
= char_gd_vc_class_init
,
1842 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1845 VirtualConsole
*vc
= user_data
;
1849 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1851 for (i
= 0; i
< size
; i
++) {
1852 uint8_t c
= text
[i
];
1853 if (c
>= 128 || isprint(c
)) {
1854 /* 8-bit characters are considered printable. */
1855 vte_terminal_feed(term
, &text
[i
], 1);
1856 } else if (c
== '\r' || c
== '\n') {
1857 vte_terminal_feed(term
, "\r\n", 2);
1859 char ctrl
[2] = { '^', 0};
1860 ctrl
[1] = text
[i
] ^ 64;
1861 vte_terminal_feed(term
, ctrl
, 2);
1866 free
= fifo8_num_free(&vc
->vte
.out_fifo
);
1867 fifo8_push_all(&vc
->vte
.out_fifo
, (uint8_t *)text
, MIN(free
, size
));
1868 gd_vc_send_chars(vc
);
1873 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1874 Chardev
*chr
, int idx
,
1875 GSList
*group
, GtkWidget
*view_menu
)
1879 GtkWidget
*scrollbar
;
1880 GtkAdjustment
*vadjustment
;
1881 VCChardev
*vcd
= VC_CHARDEV(chr
);
1884 vc
->vte
.echo
= vcd
->echo
;
1886 fifo8_create(&vc
->vte
.out_fifo
, 4096);
1889 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1890 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1891 ? vc
->vte
.chr
->label
: buffer
);
1892 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1894 vc
->vte
.terminal
= vte_terminal_new();
1895 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1897 /* The documentation says that the default is UTF-8, but actually it is
1898 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1899 * VTE 0.54 (only UTF-8 is supported now). */
1900 #if !VTE_CHECK_VERSION(0, 54, 0)
1901 #if VTE_CHECK_VERSION(0, 38, 0)
1902 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1904 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1908 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1909 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1910 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1912 #if VTE_CHECK_VERSION(0, 28, 0)
1913 vadjustment
= gtk_scrollable_get_vadjustment
1914 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1916 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1919 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1920 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1922 gtk_box_pack_end(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1923 gtk_box_pack_end(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1926 vc
->vte
.scrollbar
= scrollbar
;
1928 g_signal_connect(vadjustment
, "changed",
1929 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1931 vc
->type
= GD_VC_VTE
;
1933 vc
->focus
= vc
->vte
.terminal
;
1934 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1935 gtk_label_new(vc
->label
));
1937 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1942 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1943 GtkWidget
*view_menu
)
1947 for (i
= 0; i
< nb_vcs
; i
++) {
1948 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1949 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1953 #endif /* CONFIG_VTE */
1955 /** Window Creation **/
1957 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1959 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1960 G_CALLBACK(gd_draw_event
), vc
);
1961 #if defined(CONFIG_OPENGL)
1962 if (gtk_use_gl_area
) {
1963 /* wire up GtkGlArea events */
1964 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1965 G_CALLBACK(gd_render_event
), vc
);
1966 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1967 G_CALLBACK(gd_resize_event
), vc
);
1970 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1971 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1972 G_CALLBACK(gd_event
), vc
);
1973 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1974 G_CALLBACK(gd_button_event
), vc
);
1975 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1976 G_CALLBACK(gd_button_event
), vc
);
1977 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1978 G_CALLBACK(gd_scroll_event
), vc
);
1979 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1980 G_CALLBACK(gd_key_event
), vc
);
1981 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1982 G_CALLBACK(gd_key_event
), vc
);
1984 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1985 G_CALLBACK(gd_enter_event
), vc
);
1986 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1987 G_CALLBACK(gd_leave_event
), vc
);
1988 g_signal_connect(vc
->gfx
.drawing_area
, "focus-in-event",
1989 G_CALLBACK(gd_focus_in_event
), vc
);
1990 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1991 G_CALLBACK(gd_focus_out_event
), vc
);
1992 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1993 G_CALLBACK(gd_configure
), vc
);
1994 g_signal_connect(vc
->gfx
.drawing_area
, "grab-broken-event",
1995 G_CALLBACK(gd_grab_broken_event
), vc
);
1997 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1998 G_CALLBACK(gd_text_key_down
), vc
);
2002 static void gd_connect_signals(GtkDisplayState
*s
)
2004 g_signal_connect(s
->show_tabs_item
, "activate",
2005 G_CALLBACK(gd_menu_show_tabs
), s
);
2006 g_signal_connect(s
->untabify_item
, "activate",
2007 G_CALLBACK(gd_menu_untabify
), s
);
2008 g_signal_connect(s
->show_menubar_item
, "activate",
2009 G_CALLBACK(gd_menu_show_menubar
), s
);
2011 g_signal_connect(s
->window
, "delete-event",
2012 G_CALLBACK(gd_window_close
), s
);
2014 g_signal_connect(s
->pause_item
, "activate",
2015 G_CALLBACK(gd_menu_pause
), s
);
2016 g_signal_connect(s
->reset_item
, "activate",
2017 G_CALLBACK(gd_menu_reset
), s
);
2018 g_signal_connect(s
->powerdown_item
, "activate",
2019 G_CALLBACK(gd_menu_powerdown
), s
);
2020 g_signal_connect(s
->quit_item
, "activate",
2021 G_CALLBACK(gd_menu_quit
), s
);
2022 #if defined(CONFIG_VTE)
2023 g_signal_connect(s
->copy_item
, "activate",
2024 G_CALLBACK(gd_menu_copy
), s
);
2026 g_signal_connect(s
->full_screen_item
, "activate",
2027 G_CALLBACK(gd_menu_full_screen
), s
);
2028 g_signal_connect(s
->zoom_in_item
, "activate",
2029 G_CALLBACK(gd_menu_zoom_in
), s
);
2030 g_signal_connect(s
->zoom_out_item
, "activate",
2031 G_CALLBACK(gd_menu_zoom_out
), s
);
2032 g_signal_connect(s
->zoom_fixed_item
, "activate",
2033 G_CALLBACK(gd_menu_zoom_fixed
), s
);
2034 g_signal_connect(s
->zoom_fit_item
, "activate",
2035 G_CALLBACK(gd_menu_zoom_fit
), s
);
2036 g_signal_connect(s
->grab_item
, "activate",
2037 G_CALLBACK(gd_menu_grab_input
), s
);
2038 g_signal_connect(s
->notebook
, "switch-page",
2039 G_CALLBACK(gd_change_page
), s
);
2042 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
2044 GtkWidget
*machine_menu
;
2045 GtkWidget
*separator
;
2047 machine_menu
= gtk_menu_new();
2048 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
2050 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2051 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
2053 separator
= gtk_separator_menu_item_new();
2054 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
2056 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
2057 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
2059 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2060 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
2062 separator
= gtk_separator_menu_item_new();
2063 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
2065 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
2066 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
2067 "<QEMU>/Machine/Quit");
2068 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2069 GDK_KEY_q
, HOTKEY_MODIFIERS
);
2070 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
2072 return machine_menu
;
2075 #if defined(CONFIG_OPENGL)
2076 static void gl_area_realize(GtkGLArea
*area
, VirtualConsole
*vc
)
2078 gtk_gl_area_make_current(area
);
2079 qemu_egl_display
= eglGetCurrentDisplay();
2080 vc
->gfx
.has_dmabuf
= qemu_egl_has_dmabuf();
2081 if (!vc
->gfx
.has_dmabuf
) {
2082 error_report("GtkGLArea console lacks DMABUF support.");
2087 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
2088 QemuConsole
*con
, int idx
,
2089 GSList
*group
, GtkWidget
*view_menu
)
2091 bool zoom_to_fit
= false;
2093 vc
->label
= qemu_console_get_label(con
);
2095 vc
->gfx
.scale_x
= 1.0;
2096 vc
->gfx
.scale_y
= 1.0;
2098 #if defined(CONFIG_OPENGL)
2099 if (display_opengl
) {
2100 if (gtk_use_gl_area
) {
2101 vc
->gfx
.drawing_area
= gtk_gl_area_new();
2102 g_signal_connect(vc
->gfx
.drawing_area
, "realize",
2103 G_CALLBACK(gl_area_realize
), vc
);
2104 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
2105 vc
->gfx
.dgc
.ops
= &gl_area_ctx_ops
;
2108 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2110 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2111 * It is required for opengl rendering on X11 though. A
2112 * proper replacement (native opengl support) is only
2113 * available in 3.16+. Silence the warning if possible.
2115 #pragma GCC diagnostic push
2116 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2117 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2118 #pragma GCC diagnostic pop
2119 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2120 vc
->gfx
.dgc
.ops
= &egl_ctx_ops
;
2121 vc
->gfx
.has_dmabuf
= qemu_egl_has_dmabuf();
2129 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2130 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2134 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2135 GDK_POINTER_MOTION_MASK
|
2136 GDK_BUTTON_PRESS_MASK
|
2137 GDK_BUTTON_RELEASE_MASK
|
2138 GDK_BUTTON_MOTION_MASK
|
2139 GDK_ENTER_NOTIFY_MASK
|
2140 GDK_LEAVE_NOTIFY_MASK
|
2142 GDK_SMOOTH_SCROLL_MASK
|
2143 GDK_KEY_PRESS_MASK
);
2144 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2146 vc
->type
= GD_VC_GFX
;
2147 vc
->tab_item
= vc
->gfx
.drawing_area
;
2148 vc
->focus
= vc
->gfx
.drawing_area
;
2149 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2150 vc
->tab_item
, gtk_label_new(vc
->label
));
2152 vc
->gfx
.kbd
= qkbd_state_init(con
);
2153 vc
->gfx
.dcl
.con
= con
;
2155 if (display_opengl
) {
2156 qemu_console_set_display_gl_ctx(con
, &vc
->gfx
.dgc
);
2158 register_displaychangelistener(&vc
->gfx
.dcl
);
2160 gd_connect_vc_gfx_signals(vc
);
2161 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2163 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2166 if (s
->opts
->u
.gtk
.has_zoom_to_fit
) {
2167 zoom_to_fit
= s
->opts
->u
.gtk
.zoom_to_fit
;
2170 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2171 s
->free_scale
= true;
2177 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
, DisplayOptions
*opts
)
2179 GSList
*group
= NULL
;
2180 GtkWidget
*view_menu
;
2181 GtkWidget
*separator
;
2185 view_menu
= gtk_menu_new();
2186 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2188 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2190 #if defined(CONFIG_VTE)
2191 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2192 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2195 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2196 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2197 gtk_accel_label_set_accel(
2198 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2199 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2200 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2202 separator
= gtk_separator_menu_item_new();
2203 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2205 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2206 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2207 "<QEMU>/View/Zoom In");
2208 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2210 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2211 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2212 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2214 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2215 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2216 "<QEMU>/View/Zoom Out");
2217 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2219 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2221 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2222 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2223 "<QEMU>/View/Zoom Fixed");
2224 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2226 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2228 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2229 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2231 separator
= gtk_separator_menu_item_new();
2232 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2234 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2235 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2237 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2238 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2239 "<QEMU>/View/Grab Input");
2240 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2242 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2244 separator
= gtk_separator_menu_item_new();
2245 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2248 for (vc
= 0;; vc
++) {
2249 con
= qemu_console_lookup_by_index(vc
);
2253 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2254 vc
, group
, view_menu
);
2258 #if defined(CONFIG_VTE)
2260 gd_vcs_init(s
, group
, view_menu
);
2263 separator
= gtk_separator_menu_item_new();
2264 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2266 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2267 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2269 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2270 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2272 s
->show_menubar_item
= gtk_check_menu_item_new_with_mnemonic(
2274 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->show_menubar_item
),
2275 !opts
->u
.gtk
.has_show_menubar
||
2276 opts
->u
.gtk
.show_menubar
);
2277 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_m
, HOTKEY_MODIFIERS
, 0,
2278 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar
), s
, NULL
));
2279 gtk_accel_label_set_accel(
2280 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->show_menubar_item
))),
2281 GDK_KEY_m
, HOTKEY_MODIFIERS
);
2282 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_menubar_item
);
2287 static void gd_create_menus(GtkDisplayState
*s
, DisplayOptions
*opts
)
2289 GtkSettings
*settings
;
2291 s
->accel_group
= gtk_accel_group_new();
2292 s
->machine_menu
= gd_create_menu_machine(s
);
2293 s
->view_menu
= gd_create_menu_view(s
, opts
);
2295 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2296 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2298 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2300 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2301 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2302 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2304 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2305 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2307 /* Disable the default "F10" menu shortcut. */
2308 settings
= gtk_widget_get_settings(s
->window
);
2309 g_object_set(G_OBJECT(settings
), "gtk-menu-bar-accel", "", NULL
);
2313 static gboolean gtkinit
;
2315 static void gtk_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2319 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2320 GdkDisplay
*window_display
;
2321 GtkIconTheme
*theme
;
2325 fprintf(stderr
, "gtk initialization failed\n");
2328 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2331 theme
= gtk_icon_theme_get_default();
2332 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
);
2333 gtk_icon_theme_prepend_search_path(theme
, dir
);
2335 g_set_prgname("qemu");
2337 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2338 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2339 s
->notebook
= gtk_notebook_new();
2340 s
->menu_bar
= gtk_menu_bar_new();
2342 s
->free_scale
= FALSE
;
2344 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2345 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2346 * printable, but without changing any of the character classes to make
2347 * sure that we don't accidentally break implicit assumptions. */
2348 setlocale(LC_MESSAGES
, "");
2349 setlocale(LC_CTYPE
, "C.UTF-8");
2350 dir
= get_relocated_path(CONFIG_QEMU_LOCALEDIR
);
2351 bindtextdomain("qemu", dir
);
2353 bind_textdomain_codeset("qemu", "UTF-8");
2356 window_display
= gtk_widget_get_display(s
->window
);
2357 if (s
->opts
->has_show_cursor
&& s
->opts
->show_cursor
) {
2358 s
->null_cursor
= NULL
; /* default pointer */
2360 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2364 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2365 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2366 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2368 gtk_window_set_icon_name(GTK_WINDOW(s
->window
), "qemu");
2370 gd_create_menus(s
, opts
);
2372 gd_connect_signals(s
);
2374 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2375 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2377 gd_update_caption(s
);
2379 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2380 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2382 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2384 gtk_widget_show_all(s
->window
);
2385 if (opts
->u
.gtk
.has_show_menubar
&&
2386 !opts
->u
.gtk
.show_menubar
) {
2387 gtk_widget_hide(s
->menu_bar
);
2390 vc
= gd_vc_find_current(s
);
2391 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2393 gtk_widget_set_sensitive(s
->copy_item
,
2394 vc
&& vc
->type
== GD_VC_VTE
);
2397 if (opts
->has_full_screen
&&
2398 opts
->full_screen
) {
2399 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2401 if (opts
->u
.gtk
.has_grab_on_hover
&&
2402 opts
->u
.gtk
.grab_on_hover
) {
2403 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2405 if (opts
->u
.gtk
.has_show_tabs
&&
2406 opts
->u
.gtk
.show_tabs
) {
2407 gtk_menu_item_activate(GTK_MENU_ITEM(s
->show_tabs_item
));
2409 #ifdef CONFIG_GTK_CLIPBOARD
2410 gd_clipboard_init(s
);
2411 #endif /* CONFIG_GTK_CLIPBOARD */
2414 static void early_gtk_display_init(DisplayOptions
*opts
)
2416 /* The QEMU code relies on the assumption that it's always run in
2417 * the C locale. Therefore it is not prepared to deal with
2418 * operations that produce different results depending on the
2419 * locale, such as printf's formatting of decimal numbers, and
2422 * Since GTK+ calls setlocale() by default -importing the locale
2423 * settings from the environment- we must prevent it from doing so
2424 * using gtk_disable_setlocale().
2426 * QEMU's GTK+ UI, however, _does_ have translations for some of
2427 * the menu items. As a trade-off between a functionally correct
2428 * QEMU and a fully internationalized UI we support importing
2429 * LC_MESSAGES from the environment (see the setlocale() call
2430 * earlier in this file). This allows us to display translated
2431 * messages leaving everything else untouched.
2433 gtk_disable_setlocale();
2434 gtkinit
= gtk_init_check(NULL
, NULL
);
2436 /* don't exit yet, that'll break -help */
2440 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2441 if (opts
->has_gl
&& opts
->gl
!= DISPLAYGL_MODE_OFF
) {
2442 #if defined(CONFIG_OPENGL)
2443 #if defined(GDK_WINDOWING_WAYLAND)
2444 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2445 gtk_use_gl_area
= true;
2451 DisplayGLMode mode
= opts
->has_gl
? opts
->gl
: DISPLAYGL_MODE_ON
;
2458 keycode_map
= gd_get_keymap(&keycode_maplen
);
2460 #if defined(CONFIG_VTE)
2461 type_register(&char_gd_vc_type_info
);
2465 static QemuDisplay qemu_display_gtk
= {
2466 .type
= DISPLAY_TYPE_GTK
,
2467 .early_init
= early_gtk_display_init
,
2468 .init
= gtk_display_init
,
2471 static void register_gtk(void)
2473 qemu_display_register(&qemu_display_gtk
);
2476 type_init(register_gtk
);
2478 #ifdef CONFIG_OPENGL
2479 module_dep("ui-opengl");