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"
40 #include "ui/console.h"
43 #include <gdk/gdkwin32.h>
45 #include "ui/win32-kbd-hook.h"
47 #include <glib/gi18n.h>
49 #if defined(CONFIG_VTE)
55 #include "qemu/cutils.h"
57 #include "sysemu/runstate.h"
58 #include "sysemu/sysemu.h"
60 #include "chardev/char.h"
61 #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
;
122 struct GtkDisplayState
{
127 GtkAccelGroup
*accel_group
;
129 GtkWidget
*machine_menu_item
;
130 GtkWidget
*machine_menu
;
131 GtkWidget
*pause_item
;
132 GtkWidget
*reset_item
;
133 GtkWidget
*powerdown_item
;
134 GtkWidget
*quit_item
;
136 GtkWidget
*view_menu_item
;
137 GtkWidget
*view_menu
;
138 GtkWidget
*full_screen_item
;
139 GtkWidget
*copy_item
;
140 GtkWidget
*zoom_in_item
;
141 GtkWidget
*zoom_out_item
;
142 GtkWidget
*zoom_fixed_item
;
143 GtkWidget
*zoom_fit_item
;
144 GtkWidget
*grab_item
;
145 GtkWidget
*grab_on_hover_item
;
148 VirtualConsole vc
[MAX_VCS
];
150 GtkWidget
*show_tabs_item
;
151 GtkWidget
*untabify_item
;
152 GtkWidget
*show_menubar_item
;
162 VirtualConsole
*kbd_owner
;
163 VirtualConsole
*ptr_owner
;
165 gboolean full_screen
;
167 GdkCursor
*null_cursor
;
168 Notifier mouse_mode_notifier
;
171 bool external_pause_update
;
173 DisplayOptions
*opts
;
178 VirtualConsole
*console
;
181 typedef struct VCChardev VCChardev
;
183 #define TYPE_CHARDEV_VC "chardev-vc"
184 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
187 bool gtk_use_gl_area
;
189 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
190 static void gd_ungrab_pointer(GtkDisplayState
*s
);
191 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
192 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
194 /** Utility Functions **/
196 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
201 for (i
= 0; i
< s
->nb_vcs
; i
++) {
203 if (gtk_check_menu_item_get_active
204 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
211 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
216 for (i
= 0; i
< s
->nb_vcs
; i
++) {
218 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
226 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
230 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
231 return gd_vc_find_by_page(s
, page
);
234 static bool gd_is_grab_active(GtkDisplayState
*s
)
236 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
239 static bool gd_grab_on_hover(GtkDisplayState
*s
)
241 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
244 static void gd_update_cursor(VirtualConsole
*vc
)
246 GtkDisplayState
*s
= vc
->s
;
249 if (vc
->type
!= GD_VC_GFX
||
250 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
254 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
258 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
259 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
260 gdk_window_set_cursor(window
, s
->null_cursor
);
262 gdk_window_set_cursor(window
, NULL
);
266 static void gd_update_caption(GtkDisplayState
*s
)
268 const char *status
= "";
271 const char *grab
= "";
272 bool is_paused
= !runstate_is_running();
276 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
278 prefix
= g_strdup_printf("QEMU");
281 if (s
->ptr_owner
!= NULL
&&
282 s
->ptr_owner
->window
== NULL
) {
283 grab
= _(" - Press Ctrl+Alt+G to release grab");
287 status
= _(" [Paused]");
289 s
->external_pause_update
= true;
290 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
292 s
->external_pause_update
= false;
294 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
295 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
298 for (i
= 0; i
< s
->nb_vcs
; i
++) {
299 VirtualConsole
*vc
= &s
->vc
[i
];
304 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
305 vc
== s
->kbd_owner
? " +kbd" : "",
306 vc
== s
->ptr_owner
? " +ptr" : "");
307 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
314 static void gd_update_geometry_hints(VirtualConsole
*vc
)
316 GtkDisplayState
*s
= vc
->s
;
317 GdkWindowHints mask
= 0;
318 GdkGeometry geo
= {};
319 GtkWidget
*geo_widget
= NULL
;
320 GtkWindow
*geo_window
;
322 if (vc
->type
== GD_VC_GFX
) {
327 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
328 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
329 mask
|= GDK_HINT_MIN_SIZE
;
331 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
332 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
333 mask
|= GDK_HINT_MIN_SIZE
;
335 geo_widget
= vc
->gfx
.drawing_area
;
336 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
338 #if defined(CONFIG_VTE)
339 } else if (vc
->type
== GD_VC_VTE
) {
340 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
341 GtkBorder padding
= { 0 };
343 #if VTE_CHECK_VERSION(0, 37, 0)
344 gtk_style_context_get_padding(
345 gtk_widget_get_style_context(vc
->vte
.terminal
),
346 gtk_widget_get_state_flags(vc
->vte
.terminal
),
350 GtkBorder
*ib
= NULL
;
351 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
359 geo
.width_inc
= vte_terminal_get_char_width(term
);
360 geo
.height_inc
= vte_terminal_get_char_height(term
);
361 mask
|= GDK_HINT_RESIZE_INC
;
362 geo
.base_width
= geo
.width_inc
;
363 geo
.base_height
= geo
.height_inc
;
364 mask
|= GDK_HINT_BASE_SIZE
;
365 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
366 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
367 mask
|= GDK_HINT_MIN_SIZE
;
369 geo
.base_width
+= padding
.left
+ padding
.right
;
370 geo
.base_height
+= padding
.top
+ padding
.bottom
;
371 geo
.min_width
+= padding
.left
+ padding
.right
;
372 geo
.min_height
+= padding
.top
+ padding
.bottom
;
373 geo_widget
= vc
->vte
.terminal
;
377 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
378 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
381 void gd_update_windowsize(VirtualConsole
*vc
)
383 GtkDisplayState
*s
= vc
->s
;
385 gd_update_geometry_hints(vc
);
387 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
388 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
389 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
393 static void gd_update_full_redraw(VirtualConsole
*vc
)
395 GtkWidget
*area
= vc
->gfx
.drawing_area
;
397 ww
= gdk_window_get_width(gtk_widget_get_window(area
));
398 wh
= gdk_window_get_height(gtk_widget_get_window(area
));
399 #if defined(CONFIG_OPENGL)
400 if (vc
->gfx
.gls
&& gtk_use_gl_area
) {
401 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
405 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
408 static void gtk_release_modifiers(GtkDisplayState
*s
)
410 VirtualConsole
*vc
= gd_vc_find_current(s
);
412 if (vc
->type
!= GD_VC_GFX
||
413 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
416 qkbd_state_lift_all_keys(vc
->gfx
.kbd
);
419 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
422 g_object_ref(G_OBJECT(widget
));
423 gtk_container_remove(GTK_CONTAINER(from
), widget
);
424 gtk_container_add(GTK_CONTAINER(to
), widget
);
425 g_object_unref(G_OBJECT(widget
));
428 static void *gd_win32_get_hwnd(VirtualConsole
*vc
)
431 return gdk_win32_window_get_impl_hwnd(
432 gtk_widget_get_window(vc
->window
? vc
->window
: vc
->s
->window
));
438 /** DisplayState Callbacks **/
440 static void gd_update(DisplayChangeListener
*dcl
,
441 int x
, int y
, int w
, int h
)
443 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
450 trace_gd_update(vc
->label
, x
, y
, w
, h
);
452 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
456 if (vc
->gfx
.convert
) {
457 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
458 NULL
, vc
->gfx
.convert
,
459 x
, y
, 0, 0, x
, y
, w
, h
);
462 x1
= floor(x
* vc
->gfx
.scale_x
);
463 y1
= floor(y
* vc
->gfx
.scale_y
);
465 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
466 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
468 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
469 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
471 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
475 ww
= gdk_window_get_width(win
);
476 wh
= gdk_window_get_height(win
);
486 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
487 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
490 static void gd_refresh(DisplayChangeListener
*dcl
)
492 graphic_hw_update(dcl
->con
);
495 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
497 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
500 static void gd_mouse_set(DisplayChangeListener
*dcl
,
501 int x
, int y
, int visible
)
503 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
507 if (qemu_input_is_absolute()) {
511 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
512 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
513 x
, y
, &x_root
, &y_root
);
514 gdk_device_warp(gd_get_pointer(dpy
),
515 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
521 static void gd_cursor_define(DisplayChangeListener
*dcl
,
524 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
528 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
532 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
533 GDK_COLORSPACE_RGB
, true, 8,
534 c
->width
, c
->height
, c
->width
* 4,
536 cursor
= gdk_cursor_new_from_pixbuf
537 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
538 pixbuf
, c
->hot_x
, c
->hot_y
);
539 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
540 g_object_unref(pixbuf
);
541 g_object_unref(cursor
);
544 static void gd_switch(DisplayChangeListener
*dcl
,
545 DisplaySurface
*surface
)
547 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
550 trace_gd_switch(vc
->label
,
551 surface
? surface_width(surface
) : 0,
552 surface
? surface_height(surface
) : 0);
554 if (vc
->gfx
.surface
) {
555 cairo_surface_destroy(vc
->gfx
.surface
);
556 vc
->gfx
.surface
= NULL
;
558 if (vc
->gfx
.convert
) {
559 pixman_image_unref(vc
->gfx
.convert
);
560 vc
->gfx
.convert
= NULL
;
563 if (vc
->gfx
.ds
&& surface
&&
564 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
565 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
568 vc
->gfx
.ds
= surface
;
574 if (surface
->format
== PIXMAN_x8r8g8b8
) {
576 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
578 * No need to convert, use surface directly. Should be the
579 * common case as this is qemu_default_pixelformat(32) too.
581 vc
->gfx
.surface
= cairo_image_surface_create_for_data
582 (surface_data(surface
),
584 surface_width(surface
),
585 surface_height(surface
),
586 surface_stride(surface
));
588 /* Must convert surface, use pixman to do it. */
589 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
590 surface_width(surface
),
591 surface_height(surface
),
593 vc
->gfx
.surface
= cairo_image_surface_create_for_data
594 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
596 pixman_image_get_width(vc
->gfx
.convert
),
597 pixman_image_get_height(vc
->gfx
.convert
),
598 pixman_image_get_stride(vc
->gfx
.convert
));
599 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
600 NULL
, vc
->gfx
.convert
,
602 pixman_image_get_width(vc
->gfx
.convert
),
603 pixman_image_get_height(vc
->gfx
.convert
));
607 gd_update_windowsize(vc
);
609 gd_update_full_redraw(vc
);
613 static const DisplayChangeListenerOps dcl_ops
= {
615 .dpy_gfx_update
= gd_update
,
616 .dpy_gfx_switch
= gd_switch
,
617 .dpy_gfx_check_format
= qemu_pixman_check_format
,
618 .dpy_refresh
= gd_refresh
,
619 .dpy_mouse_set
= gd_mouse_set
,
620 .dpy_cursor_define
= gd_cursor_define
,
624 #if defined(CONFIG_OPENGL)
626 /** DisplayState Callbacks (opengl version) **/
628 #if defined(CONFIG_OPENGL)
630 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
631 .dpy_name
= "gtk-egl",
632 .dpy_gfx_update
= gd_gl_area_update
,
633 .dpy_gfx_switch
= gd_gl_area_switch
,
634 .dpy_gfx_check_format
= console_gl_check_format
,
635 .dpy_refresh
= gd_gl_area_refresh
,
636 .dpy_mouse_set
= gd_mouse_set
,
637 .dpy_cursor_define
= gd_cursor_define
,
639 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
640 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
641 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
642 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
643 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
644 .dpy_gl_update
= gd_gl_area_scanout_flush
,
647 #endif /* CONFIG_OPENGL */
649 static const DisplayChangeListenerOps dcl_egl_ops
= {
650 .dpy_name
= "gtk-egl",
651 .dpy_gfx_update
= gd_egl_update
,
652 .dpy_gfx_switch
= gd_egl_switch
,
653 .dpy_gfx_check_format
= console_gl_check_format
,
654 .dpy_refresh
= gd_egl_refresh
,
655 .dpy_mouse_set
= gd_mouse_set
,
656 .dpy_cursor_define
= gd_cursor_define
,
658 .dpy_gl_ctx_create
= gd_egl_create_context
,
659 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
660 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
661 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
662 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
663 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
664 .dpy_gl_scanout_dmabuf
= gd_egl_scanout_dmabuf
,
665 .dpy_gl_cursor_dmabuf
= gd_egl_cursor_dmabuf
,
666 .dpy_gl_cursor_position
= gd_egl_cursor_position
,
667 .dpy_gl_release_dmabuf
= gd_egl_release_dmabuf
,
668 .dpy_gl_update
= gd_egl_scanout_flush
,
671 #endif /* CONFIG_OPENGL */
675 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
677 GtkDisplayState
*s
= opaque
;
679 gd_update_caption(s
);
682 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
687 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
688 /* release the grab at switching to absolute mode */
689 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
690 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
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_info(VirtualConsole
*vc
, gint width
, gint height
)
722 memset(&info
, 0, sizeof(info
));
724 info
.height
= height
;
725 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
728 #if defined(CONFIG_OPENGL)
730 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
733 VirtualConsole
*vc
= opaque
;
741 static void gd_resize_event(GtkGLArea
*area
,
742 gint width
, gint height
, gpointer
*opaque
)
744 VirtualConsole
*vc
= (void *)opaque
;
746 gd_set_ui_info(vc
, width
, height
);
752 * If available, return the refresh rate of the display in milli-Hertz,
755 static int gd_refresh_rate_millihz(GtkWidget
*widget
)
757 #ifdef GDK_VERSION_3_22
758 GdkWindow
*win
= gtk_widget_get_window(widget
);
761 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
762 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
764 return gdk_monitor_get_refresh_rate(monitor
);
770 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
772 VirtualConsole
*vc
= opaque
;
773 GtkDisplayState
*s
= vc
->s
;
777 int refresh_rate_millihz
;
779 #if defined(CONFIG_OPENGL)
781 if (gtk_use_gl_area
) {
782 /* invoke render callback please */
791 if (!gtk_widget_get_realized(widget
)) {
798 refresh_rate_millihz
= gd_refresh_rate_millihz(vc
->window
?
799 vc
->window
: s
->window
);
800 if (refresh_rate_millihz
) {
801 /* T = 1 / f = 1 [s*Hz] / f = 1000*1000 [ms*mHz] / f */
802 vc
->gfx
.dcl
.update_interval
= 1000 * 1000 / refresh_rate_millihz
;
805 fbw
= surface_width(vc
->gfx
.ds
);
806 fbh
= surface_height(vc
->gfx
.ds
);
808 ww
= gdk_window_get_width(gtk_widget_get_window(widget
));
809 wh
= gdk_window_get_height(gtk_widget_get_window(widget
));
811 if (s
->full_screen
) {
812 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
813 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
814 } else if (s
->free_scale
) {
817 sx
= (double)ww
/ fbw
;
818 sy
= (double)wh
/ fbh
;
820 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
823 fbw
*= vc
->gfx
.scale_x
;
824 fbh
*= vc
->gfx
.scale_y
;
834 cairo_rectangle(cr
, 0, 0, ww
, wh
);
836 /* Optionally cut out the inner area where the pixmap
837 will be drawn. This avoids 'flashing' since we're
838 not double-buffering. Note we're using the undocumented
839 behaviour of drawing the rectangle from right to left
840 to cut out the whole */
841 cairo_rectangle(cr
, mx
+ fbw
, my
,
845 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
846 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
847 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
853 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
856 VirtualConsole
*vc
= opaque
;
857 GtkDisplayState
*s
= vc
->s
;
867 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
868 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
870 ww
= gdk_window_get_width(gtk_widget_get_window(vc
->gfx
.drawing_area
));
871 wh
= gdk_window_get_height(gtk_widget_get_window(vc
->gfx
.drawing_area
));
881 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
882 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
884 if (qemu_input_is_absolute()) {
885 if (x
< 0 || y
< 0 ||
886 x
>= surface_width(vc
->gfx
.ds
) ||
887 y
>= surface_height(vc
->gfx
.ds
)) {
890 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
891 0, surface_width(vc
->gfx
.ds
));
892 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
893 0, surface_height(vc
->gfx
.ds
));
894 qemu_input_event_sync();
895 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
896 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
897 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
898 qemu_input_event_sync();
904 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
905 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
906 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
907 GdkWindow
*win
= gtk_widget_get_window(widget
);
908 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
909 GdkRectangle geometry
;
910 int screen_width
, screen_height
;
912 int x
= (int)motion
->x_root
;
913 int y
= (int)motion
->y_root
;
915 gdk_monitor_get_geometry(monitor
, &geometry
);
916 screen_width
= geometry
.width
;
917 screen_height
= geometry
.height
;
919 /* In relative mode check to see if client pointer hit
920 * one of the screen edges, and if so move it back by
921 * 200 pixels. This is important because the pointer
922 * in the server doesn't correspond 1-for-1, and so
923 * may still be only half way across the screen. Without
924 * this warp, the server pointer would thus appear to hit
925 * an invisible wall */
932 if (x
== (screen_width
- 1)) {
935 if (y
== (screen_height
- 1)) {
939 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
940 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
941 gdk_device_warp(dev
, screen
, x
, y
);
949 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
952 VirtualConsole
*vc
= opaque
;
953 GtkDisplayState
*s
= vc
->s
;
956 /* implicitly grab the input at the first click in the relative mode */
957 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
958 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
960 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
963 gd_grab_pointer(vc
, "relative-mode-click");
968 if (button
->button
== 1) {
969 btn
= INPUT_BUTTON_LEFT
;
970 } else if (button
->button
== 2) {
971 btn
= INPUT_BUTTON_MIDDLE
;
972 } else if (button
->button
== 3) {
973 btn
= INPUT_BUTTON_RIGHT
;
974 } else if (button
->button
== 8) {
975 btn
= INPUT_BUTTON_SIDE
;
976 } else if (button
->button
== 9) {
977 btn
= INPUT_BUTTON_EXTRA
;
982 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
983 button
->type
== GDK_BUTTON_PRESS
);
984 qemu_input_event_sync();
988 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
991 VirtualConsole
*vc
= opaque
;
994 if (scroll
->direction
== GDK_SCROLL_UP
) {
995 btn
= INPUT_BUTTON_WHEEL_UP
;
996 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
997 btn
= INPUT_BUTTON_WHEEL_DOWN
;
998 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
999 gdouble delta_x
, delta_y
;
1000 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
1001 &delta_x
, &delta_y
)) {
1006 } else if (delta_y
> 0) {
1007 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1009 btn
= INPUT_BUTTON_WHEEL_UP
;
1015 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
1016 qemu_input_event_sync();
1017 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
1018 qemu_input_event_sync();
1023 static const guint16
*gd_get_keymap(size_t *maplen
)
1025 GdkDisplay
*dpy
= gdk_display_get_default();
1027 #ifdef GDK_WINDOWING_X11
1028 if (GDK_IS_X11_DISPLAY(dpy
)) {
1029 trace_gd_keymap_windowing("x11");
1030 return qemu_xkeymap_mapping_table(
1031 gdk_x11_display_get_xdisplay(dpy
), maplen
);
1035 #ifdef GDK_WINDOWING_WAYLAND
1036 if (GDK_IS_WAYLAND_DISPLAY(dpy
)) {
1037 trace_gd_keymap_windowing("wayland");
1038 *maplen
= qemu_input_map_xorgevdev_to_qcode_len
;
1039 return qemu_input_map_xorgevdev_to_qcode
;
1043 #ifdef GDK_WINDOWING_WIN32
1044 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1045 trace_gd_keymap_windowing("win32");
1046 *maplen
= qemu_input_map_atset1_to_qcode_len
;
1047 return qemu_input_map_atset1_to_qcode
;
1051 #ifdef GDK_WINDOWING_QUARTZ
1052 if (GDK_IS_QUARTZ_DISPLAY(dpy
)) {
1053 trace_gd_keymap_windowing("quartz");
1054 *maplen
= qemu_input_map_osx_to_qcode_len
;
1055 return qemu_input_map_osx_to_qcode
;
1059 #ifdef GDK_WINDOWING_BROADWAY
1060 if (GDK_IS_BROADWAY_DISPLAY(dpy
)) {
1061 trace_gd_keymap_windowing("broadway");
1062 g_warning("experimental: using broadway, x11 virtual keysym\n"
1063 "mapping - with very limited support. See also\n"
1064 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1065 *maplen
= qemu_input_map_x11_to_qcode_len
;
1066 return qemu_input_map_x11_to_qcode
;
1070 g_warning("Unsupported GDK Windowing platform.\n"
1071 "Disabling extended keycode tables.\n"
1072 "Please report to qemu-devel@nongnu.org\n"
1073 "including the following information:\n"
1075 " - Operating system\n"
1076 " - GDK Windowing system build\n");
1081 static int gd_map_keycode(int scancode
)
1086 if (scancode
> keycode_maplen
) {
1090 return keycode_map
[scancode
];
1093 static int gd_get_keycode(GdkEventKey
*key
)
1096 int scancode
= gdk_event_get_scancode((GdkEvent
*)key
);
1098 /* translate Windows native scancodes to atset1 keycodes */
1099 switch (scancode
& (KF_EXTENDED
| 0xff)) {
1100 case 0x145: /* NUMLOCK */
1101 return scancode
& 0xff;
1104 return scancode
& KF_EXTENDED
?
1105 0xe000 | (scancode
& 0xff) : scancode
& 0xff;
1108 return key
->hardware_keycode
;
1112 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1113 GdkEventKey
*key
, void *opaque
)
1115 VirtualConsole
*vc
= opaque
;
1116 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1118 if (key
->keyval
== GDK_KEY_Delete
) {
1119 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
, false);
1120 } else if (key
->length
) {
1121 kbd_put_string_console(con
, key
->string
, key
->length
);
1123 int qcode
= gd_map_keycode(gd_get_keycode(key
));
1124 kbd_put_qcode_console(con
, qcode
, false);
1129 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1131 VirtualConsole
*vc
= opaque
;
1135 /* on windows, we ought to ignore the reserved key event? */
1136 if (key
->hardware_keycode
== 0xff)
1139 if (!vc
->s
->kbd_owner
) {
1140 if (key
->hardware_keycode
== VK_LWIN
||
1141 key
->hardware_keycode
== VK_RWIN
) {
1147 if (key
->keyval
== GDK_KEY_Pause
1149 /* for some reason GDK does not fill keyval for VK_PAUSE
1150 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1152 || key
->hardware_keycode
== VK_PAUSE
1155 qkbd_state_key_event(vc
->gfx
.kbd
, Q_KEY_CODE_PAUSE
,
1156 key
->type
== GDK_KEY_PRESS
);
1160 keycode
= gd_get_keycode(key
);
1161 qcode
= gd_map_keycode(keycode
);
1163 trace_gd_key_event(vc
->label
, keycode
, qcode
,
1164 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1166 qkbd_state_key_event(vc
->gfx
.kbd
, qcode
,
1167 key
->type
== GDK_KEY_PRESS
);
1172 static gboolean
gd_grab_broken_event(GtkWidget
*widget
,
1173 GdkEventGrabBroken
*event
, void *opaque
)
1177 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1178 * key combination leaves all three keys in a stuck condition. We use
1179 * the grab-broken-event to release all keys.
1181 if (event
->keyboard
) {
1182 VirtualConsole
*vc
= opaque
;
1183 GtkDisplayState
*s
= vc
->s
;
1185 gtk_release_modifiers(s
);
1191 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1193 if (event
->type
== GDK_MOTION_NOTIFY
) {
1194 return gd_motion_event(widget
, &event
->motion
, opaque
);
1199 /** Window Menu Actions **/
1201 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1203 GtkDisplayState
*s
= opaque
;
1205 if (s
->external_pause_update
) {
1208 if (runstate_is_running()) {
1215 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1217 qmp_system_reset(NULL
);
1220 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1222 qmp_system_powerdown(NULL
);
1225 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1230 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1232 GtkDisplayState
*s
= opaque
;
1233 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1234 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1237 gtk_release_modifiers(s
);
1239 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1240 gtk_notebook_set_current_page(nb
, page
);
1241 gtk_widget_grab_focus(vc
->focus
);
1245 static void gd_accel_switch_vc(void *opaque
)
1247 VirtualConsole
*vc
= opaque
;
1249 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1252 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1254 GtkDisplayState
*s
= opaque
;
1255 VirtualConsole
*vc
= gd_vc_find_current(s
);
1257 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1258 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1260 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1262 gd_update_windowsize(vc
);
1265 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1268 VirtualConsole
*vc
= opaque
;
1269 GtkDisplayState
*s
= vc
->s
;
1271 gtk_widget_set_sensitive(vc
->menu_item
, true);
1272 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1273 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1274 vc
->tab_item
, vc
->label
);
1275 gtk_widget_destroy(vc
->window
);
1280 static gboolean
gd_win_grab(void *opaque
)
1282 VirtualConsole
*vc
= opaque
;
1284 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1285 if (vc
->s
->ptr_owner
) {
1286 gd_ungrab_pointer(vc
->s
);
1288 gd_grab_pointer(vc
, "user-request-detached-tab");
1293 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1295 GtkDisplayState
*s
= opaque
;
1296 VirtualConsole
*vc
= gd_vc_find_current(s
);
1298 if (vc
->type
== GD_VC_GFX
&&
1299 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1300 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1304 gtk_widget_set_sensitive(vc
->menu_item
, false);
1305 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1306 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1308 g_signal_connect(vc
->window
, "delete-event",
1309 G_CALLBACK(gd_tab_window_close
), vc
);
1310 gtk_widget_show_all(vc
->window
);
1312 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1313 GtkAccelGroup
*ag
= gtk_accel_group_new();
1314 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1316 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1318 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1321 gd_update_geometry_hints(vc
);
1322 gd_update_caption(s
);
1326 static void gd_menu_show_menubar(GtkMenuItem
*item
, void *opaque
)
1328 GtkDisplayState
*s
= opaque
;
1329 VirtualConsole
*vc
= gd_vc_find_current(s
);
1331 if (s
->full_screen
) {
1335 if (gtk_check_menu_item_get_active(
1336 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1337 gtk_widget_show(s
->menu_bar
);
1339 gtk_widget_hide(s
->menu_bar
);
1341 gd_update_windowsize(vc
);
1344 static void gd_accel_show_menubar(void *opaque
)
1346 GtkDisplayState
*s
= opaque
;
1347 gtk_menu_item_activate(GTK_MENU_ITEM(s
->show_menubar_item
));
1350 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1352 GtkDisplayState
*s
= opaque
;
1353 VirtualConsole
*vc
= gd_vc_find_current(s
);
1355 if (!s
->full_screen
) {
1356 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1357 gtk_widget_hide(s
->menu_bar
);
1358 if (vc
->type
== GD_VC_GFX
) {
1359 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1361 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1362 s
->full_screen
= TRUE
;
1364 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1365 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1366 if (gtk_check_menu_item_get_active(
1367 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1368 gtk_widget_show(s
->menu_bar
);
1370 s
->full_screen
= FALSE
;
1371 if (vc
->type
== GD_VC_GFX
) {
1372 vc
->gfx
.scale_x
= 1.0;
1373 vc
->gfx
.scale_y
= 1.0;
1374 gd_update_windowsize(vc
);
1378 gd_update_cursor(vc
);
1381 static void gd_accel_full_screen(void *opaque
)
1383 GtkDisplayState
*s
= opaque
;
1384 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1387 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1389 GtkDisplayState
*s
= opaque
;
1390 VirtualConsole
*vc
= gd_vc_find_current(s
);
1392 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1395 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1396 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1398 gd_update_windowsize(vc
);
1401 static void gd_accel_zoom_in(void *opaque
)
1403 GtkDisplayState
*s
= opaque
;
1404 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1407 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1409 GtkDisplayState
*s
= opaque
;
1410 VirtualConsole
*vc
= gd_vc_find_current(s
);
1412 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1415 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1416 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1418 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1419 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1421 gd_update_windowsize(vc
);
1424 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1426 GtkDisplayState
*s
= opaque
;
1427 VirtualConsole
*vc
= gd_vc_find_current(s
);
1429 vc
->gfx
.scale_x
= 1.0;
1430 vc
->gfx
.scale_y
= 1.0;
1432 gd_update_windowsize(vc
);
1435 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1437 GtkDisplayState
*s
= opaque
;
1438 VirtualConsole
*vc
= gd_vc_find_current(s
);
1440 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1441 s
->free_scale
= TRUE
;
1443 s
->free_scale
= FALSE
;
1444 vc
->gfx
.scale_x
= 1.0;
1445 vc
->gfx
.scale_y
= 1.0;
1448 gd_update_windowsize(vc
);
1449 gd_update_full_redraw(vc
);
1452 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1454 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1455 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1456 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1457 GdkSeatCapabilities caps
= 0;
1458 GdkCursor
*cursor
= NULL
;
1461 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1464 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1465 cursor
= vc
->s
->null_cursor
;
1469 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1472 gdk_seat_ungrab(seat
);
1476 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1478 if (vc
->s
->kbd_owner
) {
1479 if (vc
->s
->kbd_owner
== vc
) {
1482 gd_ungrab_keyboard(vc
->s
);
1486 win32_kbd_set_grab(true);
1487 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1488 vc
->s
->kbd_owner
= vc
;
1489 gd_update_caption(vc
->s
);
1490 trace_gd_grab(vc
->label
, "kbd", reason
);
1493 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1495 VirtualConsole
*vc
= s
->kbd_owner
;
1500 s
->kbd_owner
= NULL
;
1502 win32_kbd_set_grab(false);
1503 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1504 gd_update_caption(s
);
1505 trace_gd_ungrab(vc
->label
, "kbd");
1508 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1510 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1512 if (vc
->s
->ptr_owner
) {
1513 if (vc
->s
->ptr_owner
== vc
) {
1516 gd_ungrab_pointer(vc
->s
);
1520 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1521 gdk_device_get_position(gd_get_pointer(display
),
1522 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1523 vc
->s
->ptr_owner
= vc
;
1524 gd_update_caption(vc
->s
);
1525 trace_gd_grab(vc
->label
, "ptr", reason
);
1528 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1530 VirtualConsole
*vc
= s
->ptr_owner
;
1531 GdkDisplay
*display
;
1536 s
->ptr_owner
= NULL
;
1538 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1539 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1540 gdk_device_warp(gd_get_pointer(display
),
1541 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1542 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1543 gd_update_caption(s
);
1544 trace_gd_ungrab(vc
->label
, "ptr");
1547 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1549 GtkDisplayState
*s
= opaque
;
1550 VirtualConsole
*vc
= gd_vc_find_current(s
);
1552 if (gd_is_grab_active(s
)) {
1553 gd_grab_keyboard(vc
, "user-request-main-window");
1554 gd_grab_pointer(vc
, "user-request-main-window");
1556 gd_ungrab_keyboard(s
);
1557 gd_ungrab_pointer(s
);
1560 gd_update_cursor(vc
);
1563 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1566 GtkDisplayState
*s
= data
;
1570 if (!gtk_widget_get_realized(s
->notebook
)) {
1574 vc
= gd_vc_find_by_page(s
, arg2
);
1578 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1580 on_vga
= (vc
->type
== GD_VC_GFX
&&
1581 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1583 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1585 } else if (s
->full_screen
) {
1586 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1589 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1591 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1594 gd_update_windowsize(vc
);
1595 gd_update_cursor(vc
);
1598 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1601 VirtualConsole
*vc
= opaque
;
1602 GtkDisplayState
*s
= vc
->s
;
1604 if (gd_grab_on_hover(s
)) {
1605 gd_grab_keyboard(vc
, "grab-on-hover");
1610 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1613 VirtualConsole
*vc
= opaque
;
1614 GtkDisplayState
*s
= vc
->s
;
1616 if (gd_grab_on_hover(s
)) {
1617 gd_ungrab_keyboard(s
);
1622 static gboolean
gd_focus_in_event(GtkWidget
*widget
,
1623 GdkEventFocus
*event
, gpointer opaque
)
1625 VirtualConsole
*vc
= opaque
;
1627 win32_kbd_set_window(gd_win32_get_hwnd(vc
));
1631 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1632 GdkEventFocus
*event
, gpointer opaque
)
1634 VirtualConsole
*vc
= opaque
;
1635 GtkDisplayState
*s
= vc
->s
;
1637 win32_kbd_set_window(NULL
);
1638 gtk_release_modifiers(s
);
1642 static gboolean
gd_configure(GtkWidget
*widget
,
1643 GdkEventConfigure
*cfg
, gpointer opaque
)
1645 VirtualConsole
*vc
= opaque
;
1647 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1651 /** Virtual Console Callbacks **/
1653 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1654 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1656 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1657 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1658 HOTKEY_MODIFIERS
, 0,
1659 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1660 gtk_accel_label_set_accel(
1661 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1662 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1664 g_signal_connect(vc
->menu_item
, "activate",
1665 G_CALLBACK(gd_menu_switch_vc
), s
);
1666 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1668 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1671 #if defined(CONFIG_VTE)
1672 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1674 GtkDisplayState
*s
= opaque
;
1675 VirtualConsole
*vc
= gd_vc_find_current(s
);
1677 #if VTE_CHECK_VERSION(0, 50, 0)
1678 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1681 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1685 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1687 VirtualConsole
*vc
= opaque
;
1689 if (gtk_adjustment_get_upper(adjustment
) >
1690 gtk_adjustment_get_page_size(adjustment
)) {
1691 gtk_widget_show(vc
->vte
.scrollbar
);
1693 gtk_widget_hide(vc
->vte
.scrollbar
);
1697 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1699 VCChardev
*vcd
= VC_CHARDEV(chr
);
1700 VirtualConsole
*vc
= vcd
->console
;
1702 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1706 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1708 VCChardev
*vcd
= VC_CHARDEV(chr
);
1709 VirtualConsole
*vc
= vcd
->console
;
1712 vc
->vte
.echo
= echo
;
1719 static Chardev
*vcs
[MAX_VCS
];
1720 static void gd_vc_open(Chardev
*chr
,
1721 ChardevBackend
*backend
,
1725 if (nb_vcs
== MAX_VCS
) {
1726 error_setg(errp
, "Maximum number of consoles reached");
1730 vcs
[nb_vcs
++] = chr
;
1732 /* console/chardev init sometimes completes elsewhere in a 2nd
1733 * stage, so defer OPENED events until they are fully initialized
1738 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1740 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1742 cc
->parse
= qemu_chr_parse_vc
;
1743 cc
->open
= gd_vc_open
;
1744 cc
->chr_write
= gd_vc_chr_write
;
1745 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1748 static const TypeInfo char_gd_vc_type_info
= {
1749 .name
= TYPE_CHARDEV_VC
,
1750 .parent
= TYPE_CHARDEV
,
1751 .instance_size
= sizeof(VCChardev
),
1752 .class_init
= char_gd_vc_class_init
,
1755 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1758 VirtualConsole
*vc
= user_data
;
1761 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1763 for (i
= 0; i
< size
; i
++) {
1764 uint8_t c
= text
[i
];
1765 if (c
>= 128 || isprint(c
)) {
1766 /* 8-bit characters are considered printable. */
1767 vte_terminal_feed(term
, &text
[i
], 1);
1768 } else if (c
== '\r' || c
== '\n') {
1769 vte_terminal_feed(term
, "\r\n", 2);
1771 char ctrl
[2] = { '^', 0};
1772 ctrl
[1] = text
[i
] ^ 64;
1773 vte_terminal_feed(term
, ctrl
, 2);
1778 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1782 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1783 Chardev
*chr
, int idx
,
1784 GSList
*group
, GtkWidget
*view_menu
)
1788 GtkWidget
*scrollbar
;
1789 GtkAdjustment
*vadjustment
;
1790 VCChardev
*vcd
= VC_CHARDEV(chr
);
1793 vc
->vte
.echo
= vcd
->echo
;
1797 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1798 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1799 ? vc
->vte
.chr
->label
: buffer
);
1800 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1802 vc
->vte
.terminal
= vte_terminal_new();
1803 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1805 /* The documentation says that the default is UTF-8, but actually it is
1806 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1807 * VTE 0.54 (only UTF-8 is supported now). */
1808 #if !VTE_CHECK_VERSION(0, 54, 0)
1809 #if VTE_CHECK_VERSION(0, 38, 0)
1810 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1812 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1816 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1817 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1818 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1820 #if VTE_CHECK_VERSION(0, 28, 0)
1821 vadjustment
= gtk_scrollable_get_vadjustment
1822 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1824 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1827 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1828 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1830 gtk_box_pack_end(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1831 gtk_box_pack_end(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1834 vc
->vte
.scrollbar
= scrollbar
;
1836 g_signal_connect(vadjustment
, "changed",
1837 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1839 vc
->type
= GD_VC_VTE
;
1841 vc
->focus
= vc
->vte
.terminal
;
1842 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1843 gtk_label_new(vc
->label
));
1845 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1850 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1851 GtkWidget
*view_menu
)
1855 for (i
= 0; i
< nb_vcs
; i
++) {
1856 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1857 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1861 #endif /* CONFIG_VTE */
1863 /** Window Creation **/
1865 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1867 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1868 G_CALLBACK(gd_draw_event
), vc
);
1869 #if defined(CONFIG_OPENGL)
1870 if (gtk_use_gl_area
) {
1871 /* wire up GtkGlArea events */
1872 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1873 G_CALLBACK(gd_render_event
), vc
);
1874 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1875 G_CALLBACK(gd_resize_event
), vc
);
1878 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1879 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1880 G_CALLBACK(gd_event
), vc
);
1881 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1882 G_CALLBACK(gd_button_event
), vc
);
1883 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1884 G_CALLBACK(gd_button_event
), vc
);
1885 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1886 G_CALLBACK(gd_scroll_event
), vc
);
1887 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1888 G_CALLBACK(gd_key_event
), vc
);
1889 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1890 G_CALLBACK(gd_key_event
), vc
);
1892 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1893 G_CALLBACK(gd_enter_event
), vc
);
1894 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1895 G_CALLBACK(gd_leave_event
), vc
);
1896 g_signal_connect(vc
->gfx
.drawing_area
, "focus-in-event",
1897 G_CALLBACK(gd_focus_in_event
), vc
);
1898 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1899 G_CALLBACK(gd_focus_out_event
), vc
);
1900 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1901 G_CALLBACK(gd_configure
), vc
);
1902 g_signal_connect(vc
->gfx
.drawing_area
, "grab-broken-event",
1903 G_CALLBACK(gd_grab_broken_event
), vc
);
1905 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1906 G_CALLBACK(gd_text_key_down
), vc
);
1910 static void gd_connect_signals(GtkDisplayState
*s
)
1912 g_signal_connect(s
->show_tabs_item
, "activate",
1913 G_CALLBACK(gd_menu_show_tabs
), s
);
1914 g_signal_connect(s
->untabify_item
, "activate",
1915 G_CALLBACK(gd_menu_untabify
), s
);
1916 g_signal_connect(s
->show_menubar_item
, "activate",
1917 G_CALLBACK(gd_menu_show_menubar
), s
);
1919 g_signal_connect(s
->window
, "delete-event",
1920 G_CALLBACK(gd_window_close
), s
);
1922 g_signal_connect(s
->pause_item
, "activate",
1923 G_CALLBACK(gd_menu_pause
), s
);
1924 g_signal_connect(s
->reset_item
, "activate",
1925 G_CALLBACK(gd_menu_reset
), s
);
1926 g_signal_connect(s
->powerdown_item
, "activate",
1927 G_CALLBACK(gd_menu_powerdown
), s
);
1928 g_signal_connect(s
->quit_item
, "activate",
1929 G_CALLBACK(gd_menu_quit
), s
);
1930 #if defined(CONFIG_VTE)
1931 g_signal_connect(s
->copy_item
, "activate",
1932 G_CALLBACK(gd_menu_copy
), s
);
1934 g_signal_connect(s
->full_screen_item
, "activate",
1935 G_CALLBACK(gd_menu_full_screen
), s
);
1936 g_signal_connect(s
->zoom_in_item
, "activate",
1937 G_CALLBACK(gd_menu_zoom_in
), s
);
1938 g_signal_connect(s
->zoom_out_item
, "activate",
1939 G_CALLBACK(gd_menu_zoom_out
), s
);
1940 g_signal_connect(s
->zoom_fixed_item
, "activate",
1941 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1942 g_signal_connect(s
->zoom_fit_item
, "activate",
1943 G_CALLBACK(gd_menu_zoom_fit
), s
);
1944 g_signal_connect(s
->grab_item
, "activate",
1945 G_CALLBACK(gd_menu_grab_input
), s
);
1946 g_signal_connect(s
->notebook
, "switch-page",
1947 G_CALLBACK(gd_change_page
), s
);
1950 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1952 GtkWidget
*machine_menu
;
1953 GtkWidget
*separator
;
1955 machine_menu
= gtk_menu_new();
1956 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1958 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1959 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1961 separator
= gtk_separator_menu_item_new();
1962 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1964 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1965 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1967 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1968 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1970 separator
= gtk_separator_menu_item_new();
1971 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1973 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1974 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1975 "<QEMU>/Machine/Quit");
1976 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1977 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1978 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1980 return machine_menu
;
1983 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1984 QemuConsole
*con
, int idx
,
1985 GSList
*group
, GtkWidget
*view_menu
)
1987 bool zoom_to_fit
= false;
1989 vc
->label
= qemu_console_get_label(con
);
1991 vc
->gfx
.scale_x
= 1.0;
1992 vc
->gfx
.scale_y
= 1.0;
1994 #if defined(CONFIG_OPENGL)
1995 if (display_opengl
) {
1996 #if defined(CONFIG_OPENGL)
1997 if (gtk_use_gl_area
) {
1998 vc
->gfx
.drawing_area
= gtk_gl_area_new();
1999 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
2001 #endif /* CONFIG_OPENGL */
2003 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2005 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2006 * It is required for opengl rendering on X11 though. A
2007 * proper replacement (native opengl support) is only
2008 * available in 3.16+. Silence the warning if possible.
2010 #pragma GCC diagnostic push
2011 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2012 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2013 #pragma GCC diagnostic pop
2014 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2019 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2020 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2024 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2025 GDK_POINTER_MOTION_MASK
|
2026 GDK_BUTTON_PRESS_MASK
|
2027 GDK_BUTTON_RELEASE_MASK
|
2028 GDK_BUTTON_MOTION_MASK
|
2029 GDK_ENTER_NOTIFY_MASK
|
2030 GDK_LEAVE_NOTIFY_MASK
|
2032 GDK_SMOOTH_SCROLL_MASK
|
2033 GDK_KEY_PRESS_MASK
);
2034 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2036 vc
->type
= GD_VC_GFX
;
2037 vc
->tab_item
= vc
->gfx
.drawing_area
;
2038 vc
->focus
= vc
->gfx
.drawing_area
;
2039 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2040 vc
->tab_item
, gtk_label_new(vc
->label
));
2042 vc
->gfx
.kbd
= qkbd_state_init(con
);
2043 vc
->gfx
.dcl
.con
= con
;
2045 register_displaychangelistener(&vc
->gfx
.dcl
);
2047 gd_connect_vc_gfx_signals(vc
);
2048 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2050 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2053 if (s
->opts
->u
.gtk
.has_zoom_to_fit
) {
2054 zoom_to_fit
= s
->opts
->u
.gtk
.zoom_to_fit
;
2057 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2058 s
->free_scale
= true;
2064 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2066 GSList
*group
= NULL
;
2067 GtkWidget
*view_menu
;
2068 GtkWidget
*separator
;
2072 view_menu
= gtk_menu_new();
2073 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2075 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2077 #if defined(CONFIG_VTE)
2078 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2079 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2082 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2083 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2084 gtk_accel_label_set_accel(
2085 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2086 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2087 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2089 separator
= gtk_separator_menu_item_new();
2090 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2092 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2093 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2094 "<QEMU>/View/Zoom In");
2095 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2097 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2098 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2099 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2101 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2102 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2103 "<QEMU>/View/Zoom Out");
2104 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2106 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2108 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2109 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2110 "<QEMU>/View/Zoom Fixed");
2111 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2113 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2115 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2116 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2118 separator
= gtk_separator_menu_item_new();
2119 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2121 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2122 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2124 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2125 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2126 "<QEMU>/View/Grab Input");
2127 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2129 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2131 separator
= gtk_separator_menu_item_new();
2132 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2135 for (vc
= 0;; vc
++) {
2136 con
= qemu_console_lookup_by_index(vc
);
2140 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2141 vc
, group
, view_menu
);
2145 #if defined(CONFIG_VTE)
2147 gd_vcs_init(s
, group
, view_menu
);
2150 separator
= gtk_separator_menu_item_new();
2151 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2153 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2154 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2156 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2157 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2159 s
->show_menubar_item
= gtk_check_menu_item_new_with_mnemonic(
2161 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->show_menubar_item
),
2163 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_m
, HOTKEY_MODIFIERS
, 0,
2164 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar
), s
, NULL
));
2165 gtk_accel_label_set_accel(
2166 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->show_menubar_item
))),
2167 GDK_KEY_m
, HOTKEY_MODIFIERS
);
2168 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_menubar_item
);
2173 static void gd_create_menus(GtkDisplayState
*s
)
2175 GtkSettings
*settings
;
2177 s
->accel_group
= gtk_accel_group_new();
2178 s
->machine_menu
= gd_create_menu_machine(s
);
2179 s
->view_menu
= gd_create_menu_view(s
);
2181 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2182 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2184 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2186 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2187 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2188 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2190 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2191 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2193 /* Disable the default "F10" menu shortcut. */
2194 settings
= gtk_widget_get_settings(s
->window
);
2195 g_object_set(G_OBJECT(settings
), "gtk-menu-bar-accel", "", NULL
);
2199 static gboolean gtkinit
;
2201 static void gtk_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2205 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2206 GdkDisplay
*window_display
;
2207 GtkIconTheme
*theme
;
2211 fprintf(stderr
, "gtk initialization failed\n");
2214 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2217 theme
= gtk_icon_theme_get_default();
2218 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
);
2219 gtk_icon_theme_prepend_search_path(theme
, dir
);
2221 g_set_prgname("qemu");
2223 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2224 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2225 s
->notebook
= gtk_notebook_new();
2226 s
->menu_bar
= gtk_menu_bar_new();
2228 s
->free_scale
= FALSE
;
2230 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2231 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2232 * printable, but without changing any of the character classes to make
2233 * sure that we don't accidentally break implicit assumptions. */
2234 setlocale(LC_MESSAGES
, "");
2235 setlocale(LC_CTYPE
, "C.UTF-8");
2236 dir
= get_relocated_path(CONFIG_QEMU_LOCALEDIR
);
2237 bindtextdomain("qemu", dir
);
2239 bind_textdomain_codeset("qemu", "UTF-8");
2242 window_display
= gtk_widget_get_display(s
->window
);
2243 if (s
->opts
->has_show_cursor
&& s
->opts
->show_cursor
) {
2244 s
->null_cursor
= NULL
; /* default pointer */
2246 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2250 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2251 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2252 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2254 gtk_window_set_icon_name(GTK_WINDOW(s
->window
), "qemu");
2258 gd_connect_signals(s
);
2260 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2261 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2263 gd_update_caption(s
);
2265 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2266 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2268 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2270 gtk_widget_show_all(s
->window
);
2272 vc
= gd_vc_find_current(s
);
2273 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2275 gtk_widget_set_sensitive(s
->copy_item
,
2276 vc
&& vc
->type
== GD_VC_VTE
);
2279 if (opts
->has_full_screen
&&
2280 opts
->full_screen
) {
2281 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2283 if (opts
->u
.gtk
.has_grab_on_hover
&&
2284 opts
->u
.gtk
.grab_on_hover
) {
2285 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2289 static void early_gtk_display_init(DisplayOptions
*opts
)
2291 /* The QEMU code relies on the assumption that it's always run in
2292 * the C locale. Therefore it is not prepared to deal with
2293 * operations that produce different results depending on the
2294 * locale, such as printf's formatting of decimal numbers, and
2297 * Since GTK+ calls setlocale() by default -importing the locale
2298 * settings from the environment- we must prevent it from doing so
2299 * using gtk_disable_setlocale().
2301 * QEMU's GTK+ UI, however, _does_ have translations for some of
2302 * the menu items. As a trade-off between a functionally correct
2303 * QEMU and a fully internationalized UI we support importing
2304 * LC_MESSAGES from the environment (see the setlocale() call
2305 * earlier in this file). This allows us to display translated
2306 * messages leaving everything else untouched.
2308 gtk_disable_setlocale();
2309 gtkinit
= gtk_init_check(NULL
, NULL
);
2311 /* don't exit yet, that'll break -help */
2315 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2316 if (opts
->has_gl
&& opts
->gl
!= DISPLAYGL_MODE_OFF
) {
2317 #if defined(CONFIG_OPENGL)
2318 #if defined(GDK_WINDOWING_WAYLAND)
2319 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2320 gtk_use_gl_area
= true;
2325 DisplayGLMode mode
= opts
->has_gl
? opts
->gl
: DISPLAYGL_MODE_ON
;
2331 keycode_map
= gd_get_keymap(&keycode_maplen
);
2333 #if defined(CONFIG_VTE)
2334 type_register(&char_gd_vc_type_info
);
2338 static QemuDisplay qemu_display_gtk
= {
2339 .type
= DISPLAY_TYPE_GTK
,
2340 .early_init
= early_gtk_display_init
,
2341 .init
= gtk_display_init
,
2344 static void register_gtk(void)
2346 qemu_display_register(&qemu_display_gtk
);
2349 type_init(register_gtk
);