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-misc.h"
37 #include "qemu/cutils.h"
39 #include "ui/console.h"
42 #include <gdk/gdkwin32.h>
44 #include "ui/win32-kbd-hook.h"
46 #include <glib/gi18n.h>
48 #if defined(CONFIG_VTE)
54 #include "qemu/cutils.h"
56 #include "sysemu/runstate.h"
57 #include "sysemu/sysemu.h"
59 #include "chardev/char.h"
60 #include "qom/object.h"
63 #define VC_WINDOW_X_MIN 320
64 #define VC_WINDOW_Y_MIN 240
65 #define VC_TERM_X_MIN 80
66 #define VC_TERM_Y_MIN 25
67 #define VC_SCALE_MIN 0.25
68 #define VC_SCALE_STEP 0.25
70 #ifdef GDK_WINDOWING_X11
74 #ifndef GDK_IS_X11_DISPLAY
75 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
80 #ifdef GDK_WINDOWING_WAYLAND
82 #ifndef GDK_IS_WAYLAND_DISPLAY
83 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
88 #ifdef GDK_WINDOWING_WIN32
90 #ifndef GDK_IS_WIN32_DISPLAY
91 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
96 #ifdef GDK_WINDOWING_BROADWAY
98 #ifndef GDK_IS_BROADWAY_DISPLAY
99 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
104 #ifdef GDK_WINDOWING_QUARTZ
106 #ifndef GDK_IS_QUARTZ_DISPLAY
107 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
112 #if !defined(CONFIG_VTE)
113 # define VTE_CHECK_VERSION(a, b, c) 0
116 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
118 static const guint16
*keycode_map
;
119 static size_t keycode_maplen
;
121 struct GtkDisplayState
{
126 GtkAccelGroup
*accel_group
;
128 GtkWidget
*machine_menu_item
;
129 GtkWidget
*machine_menu
;
130 GtkWidget
*pause_item
;
131 GtkWidget
*reset_item
;
132 GtkWidget
*powerdown_item
;
133 GtkWidget
*quit_item
;
135 GtkWidget
*view_menu_item
;
136 GtkWidget
*view_menu
;
137 GtkWidget
*full_screen_item
;
138 GtkWidget
*copy_item
;
139 GtkWidget
*zoom_in_item
;
140 GtkWidget
*zoom_out_item
;
141 GtkWidget
*zoom_fixed_item
;
142 GtkWidget
*zoom_fit_item
;
143 GtkWidget
*grab_item
;
144 GtkWidget
*grab_on_hover_item
;
147 VirtualConsole vc
[MAX_VCS
];
149 GtkWidget
*show_tabs_item
;
150 GtkWidget
*untabify_item
;
151 GtkWidget
*show_menubar_item
;
161 VirtualConsole
*kbd_owner
;
162 VirtualConsole
*ptr_owner
;
164 gboolean full_screen
;
166 GdkCursor
*null_cursor
;
167 Notifier mouse_mode_notifier
;
170 bool external_pause_update
;
172 DisplayOptions
*opts
;
177 VirtualConsole
*console
;
180 typedef struct VCChardev VCChardev
;
182 #define TYPE_CHARDEV_VC "chardev-vc"
183 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
186 bool gtk_use_gl_area
;
188 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
189 static void gd_ungrab_pointer(GtkDisplayState
*s
);
190 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
191 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
193 /** Utility Functions **/
195 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
200 for (i
= 0; i
< s
->nb_vcs
; i
++) {
202 if (gtk_check_menu_item_get_active
203 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
210 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
215 for (i
= 0; i
< s
->nb_vcs
; i
++) {
217 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
225 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
229 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
230 return gd_vc_find_by_page(s
, page
);
233 static bool gd_is_grab_active(GtkDisplayState
*s
)
235 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
238 static bool gd_grab_on_hover(GtkDisplayState
*s
)
240 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
243 static void gd_update_cursor(VirtualConsole
*vc
)
245 GtkDisplayState
*s
= vc
->s
;
248 if (vc
->type
!= GD_VC_GFX
||
249 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
253 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
257 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
258 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
259 gdk_window_set_cursor(window
, s
->null_cursor
);
261 gdk_window_set_cursor(window
, NULL
);
265 static void gd_update_caption(GtkDisplayState
*s
)
267 const char *status
= "";
270 const char *grab
= "";
271 bool is_paused
= !runstate_is_running();
275 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
277 prefix
= g_strdup_printf("QEMU");
280 if (s
->ptr_owner
!= NULL
&&
281 s
->ptr_owner
->window
== NULL
) {
282 grab
= _(" - Press Ctrl+Alt+G to release grab");
286 status
= _(" [Paused]");
288 s
->external_pause_update
= true;
289 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
291 s
->external_pause_update
= false;
293 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
294 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
297 for (i
= 0; i
< s
->nb_vcs
; i
++) {
298 VirtualConsole
*vc
= &s
->vc
[i
];
303 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
304 vc
== s
->kbd_owner
? " +kbd" : "",
305 vc
== s
->ptr_owner
? " +ptr" : "");
306 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
313 static void gd_update_geometry_hints(VirtualConsole
*vc
)
315 GtkDisplayState
*s
= vc
->s
;
316 GdkWindowHints mask
= 0;
317 GdkGeometry geo
= {};
318 GtkWidget
*geo_widget
= NULL
;
319 GtkWindow
*geo_window
;
321 if (vc
->type
== GD_VC_GFX
) {
326 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
327 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
328 mask
|= GDK_HINT_MIN_SIZE
;
330 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
331 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
332 mask
|= GDK_HINT_MIN_SIZE
;
334 geo_widget
= vc
->gfx
.drawing_area
;
335 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
337 #if defined(CONFIG_VTE)
338 } else if (vc
->type
== GD_VC_VTE
) {
339 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
340 GtkBorder padding
= { 0 };
342 #if VTE_CHECK_VERSION(0, 37, 0)
343 gtk_style_context_get_padding(
344 gtk_widget_get_style_context(vc
->vte
.terminal
),
345 gtk_widget_get_state_flags(vc
->vte
.terminal
),
349 GtkBorder
*ib
= NULL
;
350 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
358 geo
.width_inc
= vte_terminal_get_char_width(term
);
359 geo
.height_inc
= vte_terminal_get_char_height(term
);
360 mask
|= GDK_HINT_RESIZE_INC
;
361 geo
.base_width
= geo
.width_inc
;
362 geo
.base_height
= geo
.height_inc
;
363 mask
|= GDK_HINT_BASE_SIZE
;
364 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
365 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
366 mask
|= GDK_HINT_MIN_SIZE
;
368 geo
.base_width
+= padding
.left
+ padding
.right
;
369 geo
.base_height
+= padding
.top
+ padding
.bottom
;
370 geo
.min_width
+= padding
.left
+ padding
.right
;
371 geo
.min_height
+= padding
.top
+ padding
.bottom
;
372 geo_widget
= vc
->vte
.terminal
;
376 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
377 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
380 void gd_update_windowsize(VirtualConsole
*vc
)
382 GtkDisplayState
*s
= vc
->s
;
384 gd_update_geometry_hints(vc
);
386 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
387 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
388 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
392 static void gd_update_full_redraw(VirtualConsole
*vc
)
394 GtkWidget
*area
= vc
->gfx
.drawing_area
;
396 ww
= gdk_window_get_width(gtk_widget_get_window(area
));
397 wh
= gdk_window_get_height(gtk_widget_get_window(area
));
398 #if defined(CONFIG_GTK_GL)
399 if (vc
->gfx
.gls
&& gtk_use_gl_area
) {
400 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
404 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
407 static void gtk_release_modifiers(GtkDisplayState
*s
)
409 VirtualConsole
*vc
= gd_vc_find_current(s
);
411 if (vc
->type
!= GD_VC_GFX
||
412 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
415 qkbd_state_lift_all_keys(vc
->gfx
.kbd
);
418 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
421 g_object_ref(G_OBJECT(widget
));
422 gtk_container_remove(GTK_CONTAINER(from
), widget
);
423 gtk_container_add(GTK_CONTAINER(to
), widget
);
424 g_object_unref(G_OBJECT(widget
));
427 static void *gd_win32_get_hwnd(VirtualConsole
*vc
)
430 return gdk_win32_window_get_impl_hwnd(
431 gtk_widget_get_window(vc
->window
? vc
->window
: vc
->s
->window
));
437 /** DisplayState Callbacks **/
439 static void gd_update(DisplayChangeListener
*dcl
,
440 int x
, int y
, int w
, int h
)
442 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
449 trace_gd_update(vc
->label
, x
, y
, w
, h
);
451 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
455 if (vc
->gfx
.convert
) {
456 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
457 NULL
, vc
->gfx
.convert
,
458 x
, y
, 0, 0, x
, y
, w
, h
);
461 x1
= floor(x
* vc
->gfx
.scale_x
);
462 y1
= floor(y
* vc
->gfx
.scale_y
);
464 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
465 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
467 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
468 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
470 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
474 ww
= gdk_window_get_width(win
);
475 wh
= gdk_window_get_height(win
);
485 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
486 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
489 static void gd_refresh(DisplayChangeListener
*dcl
)
491 graphic_hw_update(dcl
->con
);
494 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
496 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
499 static void gd_mouse_set(DisplayChangeListener
*dcl
,
500 int x
, int y
, int visible
)
502 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
506 if (qemu_input_is_absolute()) {
510 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
511 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
512 x
, y
, &x_root
, &y_root
);
513 gdk_device_warp(gd_get_pointer(dpy
),
514 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
520 static void gd_cursor_define(DisplayChangeListener
*dcl
,
523 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
527 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
531 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
532 GDK_COLORSPACE_RGB
, true, 8,
533 c
->width
, c
->height
, c
->width
* 4,
535 cursor
= gdk_cursor_new_from_pixbuf
536 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
537 pixbuf
, c
->hot_x
, c
->hot_y
);
538 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
539 g_object_unref(pixbuf
);
540 g_object_unref(cursor
);
543 static void gd_switch(DisplayChangeListener
*dcl
,
544 DisplaySurface
*surface
)
546 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
549 trace_gd_switch(vc
->label
,
550 surface
? surface_width(surface
) : 0,
551 surface
? surface_height(surface
) : 0);
553 if (vc
->gfx
.surface
) {
554 cairo_surface_destroy(vc
->gfx
.surface
);
555 vc
->gfx
.surface
= NULL
;
557 if (vc
->gfx
.convert
) {
558 pixman_image_unref(vc
->gfx
.convert
);
559 vc
->gfx
.convert
= NULL
;
562 if (vc
->gfx
.ds
&& surface
&&
563 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
564 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
567 vc
->gfx
.ds
= surface
;
573 if (surface
->format
== PIXMAN_x8r8g8b8
) {
575 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
577 * No need to convert, use surface directly. Should be the
578 * common case as this is qemu_default_pixelformat(32) too.
580 vc
->gfx
.surface
= cairo_image_surface_create_for_data
581 (surface_data(surface
),
583 surface_width(surface
),
584 surface_height(surface
),
585 surface_stride(surface
));
587 /* Must convert surface, use pixman to do it. */
588 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
589 surface_width(surface
),
590 surface_height(surface
),
592 vc
->gfx
.surface
= cairo_image_surface_create_for_data
593 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
595 pixman_image_get_width(vc
->gfx
.convert
),
596 pixman_image_get_height(vc
->gfx
.convert
),
597 pixman_image_get_stride(vc
->gfx
.convert
));
598 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
599 NULL
, vc
->gfx
.convert
,
601 pixman_image_get_width(vc
->gfx
.convert
),
602 pixman_image_get_height(vc
->gfx
.convert
));
606 gd_update_windowsize(vc
);
608 gd_update_full_redraw(vc
);
612 static const DisplayChangeListenerOps dcl_ops
= {
614 .dpy_gfx_update
= gd_update
,
615 .dpy_gfx_switch
= gd_switch
,
616 .dpy_gfx_check_format
= qemu_pixman_check_format
,
617 .dpy_refresh
= gd_refresh
,
618 .dpy_mouse_set
= gd_mouse_set
,
619 .dpy_cursor_define
= gd_cursor_define
,
623 #if defined(CONFIG_OPENGL)
625 /** DisplayState Callbacks (opengl version) **/
627 #if defined(CONFIG_GTK_GL)
629 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
630 .dpy_name
= "gtk-egl",
631 .dpy_gfx_update
= gd_gl_area_update
,
632 .dpy_gfx_switch
= gd_gl_area_switch
,
633 .dpy_gfx_check_format
= console_gl_check_format
,
634 .dpy_refresh
= gd_gl_area_refresh
,
635 .dpy_mouse_set
= gd_mouse_set
,
636 .dpy_cursor_define
= gd_cursor_define
,
638 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
639 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
640 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
641 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
642 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
643 .dpy_gl_update
= gd_gl_area_scanout_flush
,
646 #endif /* CONFIG_GTK_GL */
648 static const DisplayChangeListenerOps dcl_egl_ops
= {
649 .dpy_name
= "gtk-egl",
650 .dpy_gfx_update
= gd_egl_update
,
651 .dpy_gfx_switch
= gd_egl_switch
,
652 .dpy_gfx_check_format
= console_gl_check_format
,
653 .dpy_refresh
= gd_egl_refresh
,
654 .dpy_mouse_set
= gd_mouse_set
,
655 .dpy_cursor_define
= gd_cursor_define
,
657 .dpy_gl_ctx_create
= gd_egl_create_context
,
658 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
659 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
660 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
661 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
662 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
663 .dpy_gl_scanout_dmabuf
= gd_egl_scanout_dmabuf
,
664 .dpy_gl_cursor_dmabuf
= gd_egl_cursor_dmabuf
,
665 .dpy_gl_cursor_position
= gd_egl_cursor_position
,
666 .dpy_gl_release_dmabuf
= gd_egl_release_dmabuf
,
667 .dpy_gl_update
= gd_egl_scanout_flush
,
670 #endif /* CONFIG_OPENGL */
674 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
676 GtkDisplayState
*s
= opaque
;
678 gd_update_caption(s
);
681 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
686 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
687 /* release the grab at switching to absolute mode */
688 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
689 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
692 for (i
= 0; i
< s
->nb_vcs
; i
++) {
693 VirtualConsole
*vc
= &s
->vc
[i
];
694 gd_update_cursor(vc
);
700 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
703 GtkDisplayState
*s
= opaque
;
704 bool allow_close
= true;
706 if (s
->opts
->has_window_close
&& !s
->opts
->window_close
) {
717 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
721 memset(&info
, 0, sizeof(info
));
723 info
.height
= height
;
724 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
727 #if defined(CONFIG_GTK_GL)
729 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
732 VirtualConsole
*vc
= opaque
;
740 static void gd_resize_event(GtkGLArea
*area
,
741 gint width
, gint height
, gpointer
*opaque
)
743 VirtualConsole
*vc
= (void *)opaque
;
745 gd_set_ui_info(vc
, width
, height
);
751 * If available, return the refresh rate of the display in milli-Hertz,
754 static int gd_refresh_rate_millihz(GtkWidget
*window
)
756 #ifdef GDK_VERSION_3_22
757 GdkWindow
*win
= gtk_widget_get_window(window
);
760 GdkDisplay
*dpy
= gtk_widget_get_display(window
);
761 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
763 return gdk_monitor_get_refresh_rate(monitor
);
769 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
771 VirtualConsole
*vc
= opaque
;
772 GtkDisplayState
*s
= vc
->s
;
776 int refresh_rate_millihz
;
778 #if defined(CONFIG_OPENGL)
780 if (gtk_use_gl_area
) {
781 /* invoke render callback please */
790 if (!gtk_widget_get_realized(widget
)) {
797 refresh_rate_millihz
= gd_refresh_rate_millihz(vc
->window
?
798 vc
->window
: s
->window
);
799 if (refresh_rate_millihz
) {
800 vc
->gfx
.dcl
.update_interval
= MILLISEC_PER_SEC
/ refresh_rate_millihz
;
803 fbw
= surface_width(vc
->gfx
.ds
);
804 fbh
= surface_height(vc
->gfx
.ds
);
806 ww
= gdk_window_get_width(gtk_widget_get_window(widget
));
807 wh
= gdk_window_get_height(gtk_widget_get_window(widget
));
809 if (s
->full_screen
) {
810 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
811 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
812 } else if (s
->free_scale
) {
815 sx
= (double)ww
/ fbw
;
816 sy
= (double)wh
/ fbh
;
818 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
821 fbw
*= vc
->gfx
.scale_x
;
822 fbh
*= vc
->gfx
.scale_y
;
832 cairo_rectangle(cr
, 0, 0, ww
, wh
);
834 /* Optionally cut out the inner area where the pixmap
835 will be drawn. This avoids 'flashing' since we're
836 not double-buffering. Note we're using the undocumented
837 behaviour of drawing the rectangle from right to left
838 to cut out the whole */
839 cairo_rectangle(cr
, mx
+ fbw
, my
,
843 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
844 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
845 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
851 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
854 VirtualConsole
*vc
= opaque
;
855 GtkDisplayState
*s
= vc
->s
;
865 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
866 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
868 ww
= gdk_window_get_width(gtk_widget_get_window(vc
->gfx
.drawing_area
));
869 wh
= gdk_window_get_height(gtk_widget_get_window(vc
->gfx
.drawing_area
));
879 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
880 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
882 if (qemu_input_is_absolute()) {
883 if (x
< 0 || y
< 0 ||
884 x
>= surface_width(vc
->gfx
.ds
) ||
885 y
>= surface_height(vc
->gfx
.ds
)) {
888 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
889 0, surface_width(vc
->gfx
.ds
));
890 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
891 0, surface_height(vc
->gfx
.ds
));
892 qemu_input_event_sync();
893 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
894 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
895 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
896 qemu_input_event_sync();
902 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
903 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
904 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
905 GdkWindow
*win
= gtk_widget_get_window(widget
);
906 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
907 GdkRectangle geometry
;
908 int screen_width
, screen_height
;
910 int x
= (int)motion
->x_root
;
911 int y
= (int)motion
->y_root
;
913 gdk_monitor_get_geometry(monitor
, &geometry
);
914 screen_width
= geometry
.width
;
915 screen_height
= geometry
.height
;
917 /* In relative mode check to see if client pointer hit
918 * one of the screen edges, and if so move it back by
919 * 200 pixels. This is important because the pointer
920 * in the server doesn't correspond 1-for-1, and so
921 * may still be only half way across the screen. Without
922 * this warp, the server pointer would thus appear to hit
923 * an invisible wall */
930 if (x
== (screen_width
- 1)) {
933 if (y
== (screen_height
- 1)) {
937 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
938 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
939 gdk_device_warp(dev
, screen
, x
, y
);
947 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
950 VirtualConsole
*vc
= opaque
;
951 GtkDisplayState
*s
= vc
->s
;
954 /* implicitly grab the input at the first click in the relative mode */
955 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
956 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
958 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
961 gd_grab_pointer(vc
, "relative-mode-click");
966 if (button
->button
== 1) {
967 btn
= INPUT_BUTTON_LEFT
;
968 } else if (button
->button
== 2) {
969 btn
= INPUT_BUTTON_MIDDLE
;
970 } else if (button
->button
== 3) {
971 btn
= INPUT_BUTTON_RIGHT
;
972 } else if (button
->button
== 8) {
973 btn
= INPUT_BUTTON_SIDE
;
974 } else if (button
->button
== 9) {
975 btn
= INPUT_BUTTON_EXTRA
;
980 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
981 button
->type
== GDK_BUTTON_PRESS
);
982 qemu_input_event_sync();
986 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
989 VirtualConsole
*vc
= opaque
;
992 if (scroll
->direction
== GDK_SCROLL_UP
) {
993 btn
= INPUT_BUTTON_WHEEL_UP
;
994 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
995 btn
= INPUT_BUTTON_WHEEL_DOWN
;
996 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
997 gdouble delta_x
, delta_y
;
998 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
999 &delta_x
, &delta_y
)) {
1004 } else if (delta_y
> 0) {
1005 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1007 btn
= INPUT_BUTTON_WHEEL_UP
;
1013 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
1014 qemu_input_event_sync();
1015 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
1016 qemu_input_event_sync();
1021 static const guint16
*gd_get_keymap(size_t *maplen
)
1023 GdkDisplay
*dpy
= gdk_display_get_default();
1025 #ifdef GDK_WINDOWING_X11
1026 if (GDK_IS_X11_DISPLAY(dpy
)) {
1027 trace_gd_keymap_windowing("x11");
1028 return qemu_xkeymap_mapping_table(
1029 gdk_x11_display_get_xdisplay(dpy
), maplen
);
1033 #ifdef GDK_WINDOWING_WAYLAND
1034 if (GDK_IS_WAYLAND_DISPLAY(dpy
)) {
1035 trace_gd_keymap_windowing("wayland");
1036 *maplen
= qemu_input_map_xorgevdev_to_qcode_len
;
1037 return qemu_input_map_xorgevdev_to_qcode
;
1041 #ifdef GDK_WINDOWING_WIN32
1042 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1043 trace_gd_keymap_windowing("win32");
1044 *maplen
= qemu_input_map_atset1_to_qcode_len
;
1045 return qemu_input_map_atset1_to_qcode
;
1049 #ifdef GDK_WINDOWING_QUARTZ
1050 if (GDK_IS_QUARTZ_DISPLAY(dpy
)) {
1051 trace_gd_keymap_windowing("quartz");
1052 *maplen
= qemu_input_map_osx_to_qcode_len
;
1053 return qemu_input_map_osx_to_qcode
;
1057 #ifdef GDK_WINDOWING_BROADWAY
1058 if (GDK_IS_BROADWAY_DISPLAY(dpy
)) {
1059 trace_gd_keymap_windowing("broadway");
1060 g_warning("experimental: using broadway, x11 virtual keysym\n"
1061 "mapping - with very limited support. See also\n"
1062 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1063 *maplen
= qemu_input_map_x11_to_qcode_len
;
1064 return qemu_input_map_x11_to_qcode
;
1068 g_warning("Unsupported GDK Windowing platform.\n"
1069 "Disabling extended keycode tables.\n"
1070 "Please report to qemu-devel@nongnu.org\n"
1071 "including the following information:\n"
1073 " - Operating system\n"
1074 " - GDK Windowing system build\n");
1079 static int gd_map_keycode(int scancode
)
1084 if (scancode
> keycode_maplen
) {
1088 return keycode_map
[scancode
];
1091 static int gd_get_keycode(GdkEventKey
*key
)
1094 int scancode
= gdk_event_get_scancode((GdkEvent
*)key
);
1096 /* translate Windows native scancodes to atset1 keycodes */
1097 switch (scancode
& (KF_EXTENDED
| 0xff)) {
1098 case 0x145: /* NUMLOCK */
1099 return scancode
& 0xff;
1102 return scancode
& KF_EXTENDED
?
1103 0xe000 | (scancode
& 0xff) : scancode
& 0xff;
1106 return key
->hardware_keycode
;
1110 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1111 GdkEventKey
*key
, void *opaque
)
1113 VirtualConsole
*vc
= opaque
;
1114 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1116 if (key
->keyval
== GDK_KEY_Delete
) {
1117 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
, false);
1118 } else if (key
->length
) {
1119 kbd_put_string_console(con
, key
->string
, key
->length
);
1121 int qcode
= gd_map_keycode(gd_get_keycode(key
));
1122 kbd_put_qcode_console(con
, qcode
, false);
1127 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1129 VirtualConsole
*vc
= opaque
;
1133 /* on windows, we ought to ignore the reserved key event? */
1134 if (key
->hardware_keycode
== 0xff)
1137 if (!vc
->s
->kbd_owner
) {
1138 if (key
->hardware_keycode
== VK_LWIN
||
1139 key
->hardware_keycode
== VK_RWIN
) {
1145 if (key
->keyval
== GDK_KEY_Pause
1147 /* for some reason GDK does not fill keyval for VK_PAUSE
1148 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1150 || key
->hardware_keycode
== VK_PAUSE
1153 qkbd_state_key_event(vc
->gfx
.kbd
, Q_KEY_CODE_PAUSE
,
1154 key
->type
== GDK_KEY_PRESS
);
1158 keycode
= gd_get_keycode(key
);
1159 qcode
= gd_map_keycode(keycode
);
1161 trace_gd_key_event(vc
->label
, keycode
, qcode
,
1162 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1164 qkbd_state_key_event(vc
->gfx
.kbd
, qcode
,
1165 key
->type
== GDK_KEY_PRESS
);
1170 static gboolean
gd_grab_broken_event(GtkWidget
*widget
,
1171 GdkEventGrabBroken
*event
, void *opaque
)
1175 * On Windows the Ctrl-Alt-Del key combination can't be grabbed. This
1176 * key combination leaves all three keys in a stuck condition. We use
1177 * the grab-broken-event to release all keys.
1179 if (event
->keyboard
) {
1180 VirtualConsole
*vc
= opaque
;
1181 GtkDisplayState
*s
= vc
->s
;
1183 gtk_release_modifiers(s
);
1189 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1191 if (event
->type
== GDK_MOTION_NOTIFY
) {
1192 return gd_motion_event(widget
, &event
->motion
, opaque
);
1197 /** Window Menu Actions **/
1199 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1201 GtkDisplayState
*s
= opaque
;
1203 if (s
->external_pause_update
) {
1206 if (runstate_is_running()) {
1213 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1215 qmp_system_reset(NULL
);
1218 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1220 qmp_system_powerdown(NULL
);
1223 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1228 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1230 GtkDisplayState
*s
= opaque
;
1231 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1232 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1235 gtk_release_modifiers(s
);
1237 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1238 gtk_notebook_set_current_page(nb
, page
);
1239 gtk_widget_grab_focus(vc
->focus
);
1243 static void gd_accel_switch_vc(void *opaque
)
1245 VirtualConsole
*vc
= opaque
;
1247 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1250 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1252 GtkDisplayState
*s
= opaque
;
1253 VirtualConsole
*vc
= gd_vc_find_current(s
);
1255 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1256 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1258 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1260 gd_update_windowsize(vc
);
1263 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1266 VirtualConsole
*vc
= opaque
;
1267 GtkDisplayState
*s
= vc
->s
;
1269 gtk_widget_set_sensitive(vc
->menu_item
, true);
1270 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1271 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1272 vc
->tab_item
, vc
->label
);
1273 gtk_widget_destroy(vc
->window
);
1278 static gboolean
gd_win_grab(void *opaque
)
1280 VirtualConsole
*vc
= opaque
;
1282 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1283 if (vc
->s
->ptr_owner
) {
1284 gd_ungrab_pointer(vc
->s
);
1286 gd_grab_pointer(vc
, "user-request-detached-tab");
1291 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1293 GtkDisplayState
*s
= opaque
;
1294 VirtualConsole
*vc
= gd_vc_find_current(s
);
1296 if (vc
->type
== GD_VC_GFX
&&
1297 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1298 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1302 gtk_widget_set_sensitive(vc
->menu_item
, false);
1303 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1304 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1306 g_signal_connect(vc
->window
, "delete-event",
1307 G_CALLBACK(gd_tab_window_close
), vc
);
1308 gtk_widget_show_all(vc
->window
);
1310 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1311 GtkAccelGroup
*ag
= gtk_accel_group_new();
1312 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1314 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1316 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1319 gd_update_geometry_hints(vc
);
1320 gd_update_caption(s
);
1324 static void gd_menu_show_menubar(GtkMenuItem
*item
, void *opaque
)
1326 GtkDisplayState
*s
= opaque
;
1327 VirtualConsole
*vc
= gd_vc_find_current(s
);
1329 if (s
->full_screen
) {
1333 if (gtk_check_menu_item_get_active(
1334 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1335 gtk_widget_show(s
->menu_bar
);
1337 gtk_widget_hide(s
->menu_bar
);
1339 gd_update_windowsize(vc
);
1342 static void gd_accel_show_menubar(void *opaque
)
1344 GtkDisplayState
*s
= opaque
;
1345 gtk_menu_item_activate(GTK_MENU_ITEM(s
->show_menubar_item
));
1348 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1350 GtkDisplayState
*s
= opaque
;
1351 VirtualConsole
*vc
= gd_vc_find_current(s
);
1353 if (!s
->full_screen
) {
1354 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1355 gtk_widget_hide(s
->menu_bar
);
1356 if (vc
->type
== GD_VC_GFX
) {
1357 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1359 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1360 s
->full_screen
= TRUE
;
1362 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1363 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1364 if (gtk_check_menu_item_get_active(
1365 GTK_CHECK_MENU_ITEM(s
->show_menubar_item
))) {
1366 gtk_widget_show(s
->menu_bar
);
1368 s
->full_screen
= FALSE
;
1369 if (vc
->type
== GD_VC_GFX
) {
1370 vc
->gfx
.scale_x
= 1.0;
1371 vc
->gfx
.scale_y
= 1.0;
1372 gd_update_windowsize(vc
);
1376 gd_update_cursor(vc
);
1379 static void gd_accel_full_screen(void *opaque
)
1381 GtkDisplayState
*s
= opaque
;
1382 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1385 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1387 GtkDisplayState
*s
= opaque
;
1388 VirtualConsole
*vc
= gd_vc_find_current(s
);
1390 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1393 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1394 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1396 gd_update_windowsize(vc
);
1399 static void gd_accel_zoom_in(void *opaque
)
1401 GtkDisplayState
*s
= opaque
;
1402 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1405 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1407 GtkDisplayState
*s
= opaque
;
1408 VirtualConsole
*vc
= gd_vc_find_current(s
);
1410 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1413 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1414 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1416 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1417 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1419 gd_update_windowsize(vc
);
1422 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1424 GtkDisplayState
*s
= opaque
;
1425 VirtualConsole
*vc
= gd_vc_find_current(s
);
1427 vc
->gfx
.scale_x
= 1.0;
1428 vc
->gfx
.scale_y
= 1.0;
1430 gd_update_windowsize(vc
);
1433 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1435 GtkDisplayState
*s
= opaque
;
1436 VirtualConsole
*vc
= gd_vc_find_current(s
);
1438 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1439 s
->free_scale
= TRUE
;
1441 s
->free_scale
= FALSE
;
1442 vc
->gfx
.scale_x
= 1.0;
1443 vc
->gfx
.scale_y
= 1.0;
1446 gd_update_windowsize(vc
);
1447 gd_update_full_redraw(vc
);
1450 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1452 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1453 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1454 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1455 GdkSeatCapabilities caps
= 0;
1456 GdkCursor
*cursor
= NULL
;
1459 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1462 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1463 cursor
= vc
->s
->null_cursor
;
1467 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1470 gdk_seat_ungrab(seat
);
1474 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1476 if (vc
->s
->kbd_owner
) {
1477 if (vc
->s
->kbd_owner
== vc
) {
1480 gd_ungrab_keyboard(vc
->s
);
1484 win32_kbd_set_grab(true);
1485 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1486 vc
->s
->kbd_owner
= vc
;
1487 gd_update_caption(vc
->s
);
1488 trace_gd_grab(vc
->label
, "kbd", reason
);
1491 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1493 VirtualConsole
*vc
= s
->kbd_owner
;
1498 s
->kbd_owner
= NULL
;
1500 win32_kbd_set_grab(false);
1501 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1502 gd_update_caption(s
);
1503 trace_gd_ungrab(vc
->label
, "kbd");
1506 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1508 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1510 if (vc
->s
->ptr_owner
) {
1511 if (vc
->s
->ptr_owner
== vc
) {
1514 gd_ungrab_pointer(vc
->s
);
1518 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1519 gdk_device_get_position(gd_get_pointer(display
),
1520 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1521 vc
->s
->ptr_owner
= vc
;
1522 gd_update_caption(vc
->s
);
1523 trace_gd_grab(vc
->label
, "ptr", reason
);
1526 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1528 VirtualConsole
*vc
= s
->ptr_owner
;
1529 GdkDisplay
*display
;
1534 s
->ptr_owner
= NULL
;
1536 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1537 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1538 gdk_device_warp(gd_get_pointer(display
),
1539 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1540 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1541 gd_update_caption(s
);
1542 trace_gd_ungrab(vc
->label
, "ptr");
1545 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1547 GtkDisplayState
*s
= opaque
;
1548 VirtualConsole
*vc
= gd_vc_find_current(s
);
1550 if (gd_is_grab_active(s
)) {
1551 gd_grab_keyboard(vc
, "user-request-main-window");
1552 gd_grab_pointer(vc
, "user-request-main-window");
1554 gd_ungrab_keyboard(s
);
1555 gd_ungrab_pointer(s
);
1558 gd_update_cursor(vc
);
1561 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1564 GtkDisplayState
*s
= data
;
1568 if (!gtk_widget_get_realized(s
->notebook
)) {
1572 vc
= gd_vc_find_by_page(s
, arg2
);
1576 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1578 on_vga
= (vc
->type
== GD_VC_GFX
&&
1579 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1581 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1583 } else if (s
->full_screen
) {
1584 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1587 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1589 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1592 gd_update_windowsize(vc
);
1593 gd_update_cursor(vc
);
1596 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1599 VirtualConsole
*vc
= opaque
;
1600 GtkDisplayState
*s
= vc
->s
;
1602 if (gd_grab_on_hover(s
)) {
1603 gd_grab_keyboard(vc
, "grab-on-hover");
1608 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1611 VirtualConsole
*vc
= opaque
;
1612 GtkDisplayState
*s
= vc
->s
;
1614 if (gd_grab_on_hover(s
)) {
1615 gd_ungrab_keyboard(s
);
1620 static gboolean
gd_focus_in_event(GtkWidget
*widget
,
1621 GdkEventFocus
*event
, gpointer opaque
)
1623 VirtualConsole
*vc
= opaque
;
1625 win32_kbd_set_window(gd_win32_get_hwnd(vc
));
1629 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1630 GdkEventFocus
*event
, gpointer opaque
)
1632 VirtualConsole
*vc
= opaque
;
1633 GtkDisplayState
*s
= vc
->s
;
1635 win32_kbd_set_window(NULL
);
1636 gtk_release_modifiers(s
);
1640 static gboolean
gd_configure(GtkWidget
*widget
,
1641 GdkEventConfigure
*cfg
, gpointer opaque
)
1643 VirtualConsole
*vc
= opaque
;
1645 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1649 /** Virtual Console Callbacks **/
1651 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1652 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1654 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1655 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1656 HOTKEY_MODIFIERS
, 0,
1657 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1658 gtk_accel_label_set_accel(
1659 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1660 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1662 g_signal_connect(vc
->menu_item
, "activate",
1663 G_CALLBACK(gd_menu_switch_vc
), s
);
1664 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1666 return gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1669 #if defined(CONFIG_VTE)
1670 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1672 GtkDisplayState
*s
= opaque
;
1673 VirtualConsole
*vc
= gd_vc_find_current(s
);
1675 #if VTE_CHECK_VERSION(0, 50, 0)
1676 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1679 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1683 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1685 VirtualConsole
*vc
= opaque
;
1687 if (gtk_adjustment_get_upper(adjustment
) >
1688 gtk_adjustment_get_page_size(adjustment
)) {
1689 gtk_widget_show(vc
->vte
.scrollbar
);
1691 gtk_widget_hide(vc
->vte
.scrollbar
);
1695 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1697 VCChardev
*vcd
= VC_CHARDEV(chr
);
1698 VirtualConsole
*vc
= vcd
->console
;
1700 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1704 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1706 VCChardev
*vcd
= VC_CHARDEV(chr
);
1707 VirtualConsole
*vc
= vcd
->console
;
1710 vc
->vte
.echo
= echo
;
1717 static Chardev
*vcs
[MAX_VCS
];
1718 static void gd_vc_open(Chardev
*chr
,
1719 ChardevBackend
*backend
,
1723 if (nb_vcs
== MAX_VCS
) {
1724 error_setg(errp
, "Maximum number of consoles reached");
1728 vcs
[nb_vcs
++] = chr
;
1730 /* console/chardev init sometimes completes elsewhere in a 2nd
1731 * stage, so defer OPENED events until they are fully initialized
1736 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1738 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1740 cc
->parse
= qemu_chr_parse_vc
;
1741 cc
->open
= gd_vc_open
;
1742 cc
->chr_write
= gd_vc_chr_write
;
1743 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1746 static const TypeInfo char_gd_vc_type_info
= {
1747 .name
= TYPE_CHARDEV_VC
,
1748 .parent
= TYPE_CHARDEV
,
1749 .instance_size
= sizeof(VCChardev
),
1750 .class_init
= char_gd_vc_class_init
,
1753 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1756 VirtualConsole
*vc
= user_data
;
1759 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1761 for (i
= 0; i
< size
; i
++) {
1762 uint8_t c
= text
[i
];
1763 if (c
>= 128 || isprint(c
)) {
1764 /* 8-bit characters are considered printable. */
1765 vte_terminal_feed(term
, &text
[i
], 1);
1766 } else if (c
== '\r' || c
== '\n') {
1767 vte_terminal_feed(term
, "\r\n", 2);
1769 char ctrl
[2] = { '^', 0};
1770 ctrl
[1] = text
[i
] ^ 64;
1771 vte_terminal_feed(term
, ctrl
, 2);
1776 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1780 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1781 Chardev
*chr
, int idx
,
1782 GSList
*group
, GtkWidget
*view_menu
)
1786 GtkWidget
*scrollbar
;
1787 GtkAdjustment
*vadjustment
;
1788 VCChardev
*vcd
= VC_CHARDEV(chr
);
1791 vc
->vte
.echo
= vcd
->echo
;
1795 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1796 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1797 ? vc
->vte
.chr
->label
: buffer
);
1798 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1800 vc
->vte
.terminal
= vte_terminal_new();
1801 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1803 /* The documentation says that the default is UTF-8, but actually it is
1804 * 7-bit ASCII at least in VTE 0.38. The function is deprecated since
1805 * VTE 0.54 (only UTF-8 is supported now). */
1806 #if !VTE_CHECK_VERSION(0, 54, 0)
1807 #if VTE_CHECK_VERSION(0, 38, 0)
1808 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1810 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1814 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1815 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1816 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1818 #if VTE_CHECK_VERSION(0, 28, 0)
1819 vadjustment
= gtk_scrollable_get_vadjustment
1820 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1822 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1825 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1826 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1828 gtk_box_pack_end(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1829 gtk_box_pack_end(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1832 vc
->vte
.scrollbar
= scrollbar
;
1834 g_signal_connect(vadjustment
, "changed",
1835 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1837 vc
->type
= GD_VC_VTE
;
1839 vc
->focus
= vc
->vte
.terminal
;
1840 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1841 gtk_label_new(vc
->label
));
1843 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1848 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1849 GtkWidget
*view_menu
)
1853 for (i
= 0; i
< nb_vcs
; i
++) {
1854 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1855 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1859 #endif /* CONFIG_VTE */
1861 /** Window Creation **/
1863 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1865 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1866 G_CALLBACK(gd_draw_event
), vc
);
1867 #if defined(CONFIG_GTK_GL)
1868 if (gtk_use_gl_area
) {
1869 /* wire up GtkGlArea events */
1870 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1871 G_CALLBACK(gd_render_event
), vc
);
1872 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1873 G_CALLBACK(gd_resize_event
), vc
);
1876 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1877 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1878 G_CALLBACK(gd_event
), vc
);
1879 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1880 G_CALLBACK(gd_button_event
), vc
);
1881 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1882 G_CALLBACK(gd_button_event
), vc
);
1883 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1884 G_CALLBACK(gd_scroll_event
), vc
);
1885 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1886 G_CALLBACK(gd_key_event
), vc
);
1887 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1888 G_CALLBACK(gd_key_event
), vc
);
1890 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1891 G_CALLBACK(gd_enter_event
), vc
);
1892 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1893 G_CALLBACK(gd_leave_event
), vc
);
1894 g_signal_connect(vc
->gfx
.drawing_area
, "focus-in-event",
1895 G_CALLBACK(gd_focus_in_event
), vc
);
1896 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1897 G_CALLBACK(gd_focus_out_event
), vc
);
1898 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1899 G_CALLBACK(gd_configure
), vc
);
1900 g_signal_connect(vc
->gfx
.drawing_area
, "grab-broken-event",
1901 G_CALLBACK(gd_grab_broken_event
), vc
);
1903 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1904 G_CALLBACK(gd_text_key_down
), vc
);
1908 static void gd_connect_signals(GtkDisplayState
*s
)
1910 g_signal_connect(s
->show_tabs_item
, "activate",
1911 G_CALLBACK(gd_menu_show_tabs
), s
);
1912 g_signal_connect(s
->untabify_item
, "activate",
1913 G_CALLBACK(gd_menu_untabify
), s
);
1914 g_signal_connect(s
->show_menubar_item
, "activate",
1915 G_CALLBACK(gd_menu_show_menubar
), s
);
1917 g_signal_connect(s
->window
, "delete-event",
1918 G_CALLBACK(gd_window_close
), s
);
1920 g_signal_connect(s
->pause_item
, "activate",
1921 G_CALLBACK(gd_menu_pause
), s
);
1922 g_signal_connect(s
->reset_item
, "activate",
1923 G_CALLBACK(gd_menu_reset
), s
);
1924 g_signal_connect(s
->powerdown_item
, "activate",
1925 G_CALLBACK(gd_menu_powerdown
), s
);
1926 g_signal_connect(s
->quit_item
, "activate",
1927 G_CALLBACK(gd_menu_quit
), s
);
1928 #if defined(CONFIG_VTE)
1929 g_signal_connect(s
->copy_item
, "activate",
1930 G_CALLBACK(gd_menu_copy
), s
);
1932 g_signal_connect(s
->full_screen_item
, "activate",
1933 G_CALLBACK(gd_menu_full_screen
), s
);
1934 g_signal_connect(s
->zoom_in_item
, "activate",
1935 G_CALLBACK(gd_menu_zoom_in
), s
);
1936 g_signal_connect(s
->zoom_out_item
, "activate",
1937 G_CALLBACK(gd_menu_zoom_out
), s
);
1938 g_signal_connect(s
->zoom_fixed_item
, "activate",
1939 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1940 g_signal_connect(s
->zoom_fit_item
, "activate",
1941 G_CALLBACK(gd_menu_zoom_fit
), s
);
1942 g_signal_connect(s
->grab_item
, "activate",
1943 G_CALLBACK(gd_menu_grab_input
), s
);
1944 g_signal_connect(s
->notebook
, "switch-page",
1945 G_CALLBACK(gd_change_page
), s
);
1948 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1950 GtkWidget
*machine_menu
;
1951 GtkWidget
*separator
;
1953 machine_menu
= gtk_menu_new();
1954 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1956 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1957 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1959 separator
= gtk_separator_menu_item_new();
1960 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1962 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1963 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1965 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1966 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1968 separator
= gtk_separator_menu_item_new();
1969 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1971 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1972 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1973 "<QEMU>/Machine/Quit");
1974 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1975 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1976 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1978 return machine_menu
;
1981 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1982 QemuConsole
*con
, int idx
,
1983 GSList
*group
, GtkWidget
*view_menu
)
1985 bool zoom_to_fit
= false;
1987 vc
->label
= qemu_console_get_label(con
);
1989 vc
->gfx
.scale_x
= 1.0;
1990 vc
->gfx
.scale_y
= 1.0;
1992 #if defined(CONFIG_OPENGL)
1993 if (display_opengl
) {
1994 #if defined(CONFIG_GTK_GL)
1995 if (gtk_use_gl_area
) {
1996 vc
->gfx
.drawing_area
= gtk_gl_area_new();
1997 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
1999 #endif /* CONFIG_GTK_GL */
2001 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2003 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2004 * It is required for opengl rendering on X11 though. A
2005 * proper replacement (native opengl support) is only
2006 * available in 3.16+. Silence the warning if possible.
2008 #pragma GCC diagnostic push
2009 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2010 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2011 #pragma GCC diagnostic pop
2012 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2017 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2018 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2022 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2023 GDK_POINTER_MOTION_MASK
|
2024 GDK_BUTTON_PRESS_MASK
|
2025 GDK_BUTTON_RELEASE_MASK
|
2026 GDK_BUTTON_MOTION_MASK
|
2027 GDK_ENTER_NOTIFY_MASK
|
2028 GDK_LEAVE_NOTIFY_MASK
|
2030 GDK_SMOOTH_SCROLL_MASK
|
2031 GDK_KEY_PRESS_MASK
);
2032 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2034 vc
->type
= GD_VC_GFX
;
2035 vc
->tab_item
= vc
->gfx
.drawing_area
;
2036 vc
->focus
= vc
->gfx
.drawing_area
;
2037 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2038 vc
->tab_item
, gtk_label_new(vc
->label
));
2040 vc
->gfx
.kbd
= qkbd_state_init(con
);
2041 vc
->gfx
.dcl
.con
= con
;
2043 register_displaychangelistener(&vc
->gfx
.dcl
);
2045 gd_connect_vc_gfx_signals(vc
);
2046 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2048 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2051 if (s
->opts
->u
.gtk
.has_zoom_to_fit
) {
2052 zoom_to_fit
= s
->opts
->u
.gtk
.zoom_to_fit
;
2055 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2056 s
->free_scale
= true;
2062 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2064 GSList
*group
= NULL
;
2065 GtkWidget
*view_menu
;
2066 GtkWidget
*separator
;
2070 view_menu
= gtk_menu_new();
2071 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2073 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2075 #if defined(CONFIG_VTE)
2076 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2077 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2080 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2081 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2082 gtk_accel_label_set_accel(
2083 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2084 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2085 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2087 separator
= gtk_separator_menu_item_new();
2088 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2090 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2091 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2092 "<QEMU>/View/Zoom In");
2093 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2095 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2096 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2097 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2099 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2100 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2101 "<QEMU>/View/Zoom Out");
2102 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2104 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2106 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2107 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2108 "<QEMU>/View/Zoom Fixed");
2109 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2111 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2113 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2114 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2116 separator
= gtk_separator_menu_item_new();
2117 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2119 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2120 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2122 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2123 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2124 "<QEMU>/View/Grab Input");
2125 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2127 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2129 separator
= gtk_separator_menu_item_new();
2130 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2133 for (vc
= 0;; vc
++) {
2134 con
= qemu_console_lookup_by_index(vc
);
2138 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2139 vc
, group
, view_menu
);
2143 #if defined(CONFIG_VTE)
2145 gd_vcs_init(s
, group
, view_menu
);
2148 separator
= gtk_separator_menu_item_new();
2149 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2151 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2152 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2154 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2155 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2157 s
->show_menubar_item
= gtk_check_menu_item_new_with_mnemonic(
2159 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->show_menubar_item
),
2161 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_m
, HOTKEY_MODIFIERS
, 0,
2162 g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar
), s
, NULL
));
2163 gtk_accel_label_set_accel(
2164 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->show_menubar_item
))),
2165 GDK_KEY_m
, HOTKEY_MODIFIERS
);
2166 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_menubar_item
);
2171 static void gd_create_menus(GtkDisplayState
*s
)
2173 GtkSettings
*settings
;
2175 s
->accel_group
= gtk_accel_group_new();
2176 s
->machine_menu
= gd_create_menu_machine(s
);
2177 s
->view_menu
= gd_create_menu_view(s
);
2179 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2180 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2182 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2184 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2185 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2186 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2188 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2189 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2191 /* Disable the default "F10" menu shortcut. */
2192 settings
= gtk_widget_get_settings(s
->window
);
2193 g_object_set(G_OBJECT(settings
), "gtk-menu-bar-accel", "", NULL
);
2197 static gboolean gtkinit
;
2199 static void gtk_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2203 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2204 GdkDisplay
*window_display
;
2205 GtkIconTheme
*theme
;
2209 fprintf(stderr
, "gtk initialization failed\n");
2212 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2215 theme
= gtk_icon_theme_get_default();
2216 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
);
2217 gtk_icon_theme_prepend_search_path(theme
, dir
);
2219 g_set_prgname("qemu");
2221 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2222 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2223 s
->notebook
= gtk_notebook_new();
2224 s
->menu_bar
= gtk_menu_bar_new();
2226 s
->free_scale
= FALSE
;
2228 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2229 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2230 * printable, but without changing any of the character classes to make
2231 * sure that we don't accidentally break implicit assumptions. */
2232 setlocale(LC_MESSAGES
, "");
2233 setlocale(LC_CTYPE
, "C.UTF-8");
2234 dir
= get_relocated_path(CONFIG_QEMU_LOCALEDIR
);
2235 bindtextdomain("qemu", dir
);
2237 bind_textdomain_codeset("qemu", "UTF-8");
2240 window_display
= gtk_widget_get_display(s
->window
);
2241 if (s
->opts
->has_show_cursor
&& s
->opts
->show_cursor
) {
2242 s
->null_cursor
= NULL
; /* default pointer */
2244 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2248 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2249 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2250 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2252 gtk_window_set_icon_name(GTK_WINDOW(s
->window
), "qemu");
2256 gd_connect_signals(s
);
2258 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2259 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2261 gd_update_caption(s
);
2263 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2264 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2266 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2268 gtk_widget_show_all(s
->window
);
2270 vc
= gd_vc_find_current(s
);
2271 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2273 gtk_widget_set_sensitive(s
->copy_item
,
2274 vc
&& vc
->type
== GD_VC_VTE
);
2277 if (opts
->has_full_screen
&&
2278 opts
->full_screen
) {
2279 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2281 if (opts
->u
.gtk
.has_grab_on_hover
&&
2282 opts
->u
.gtk
.grab_on_hover
) {
2283 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2287 static void early_gtk_display_init(DisplayOptions
*opts
)
2289 /* The QEMU code relies on the assumption that it's always run in
2290 * the C locale. Therefore it is not prepared to deal with
2291 * operations that produce different results depending on the
2292 * locale, such as printf's formatting of decimal numbers, and
2295 * Since GTK+ calls setlocale() by default -importing the locale
2296 * settings from the environment- we must prevent it from doing so
2297 * using gtk_disable_setlocale().
2299 * QEMU's GTK+ UI, however, _does_ have translations for some of
2300 * the menu items. As a trade-off between a functionally correct
2301 * QEMU and a fully internationalized UI we support importing
2302 * LC_MESSAGES from the environment (see the setlocale() call
2303 * earlier in this file). This allows us to display translated
2304 * messages leaving everything else untouched.
2306 gtk_disable_setlocale();
2307 gtkinit
= gtk_init_check(NULL
, NULL
);
2309 /* don't exit yet, that'll break -help */
2313 assert(opts
->type
== DISPLAY_TYPE_GTK
);
2314 if (opts
->has_gl
&& opts
->gl
!= DISPLAYGL_MODE_OFF
) {
2315 #if defined(CONFIG_OPENGL)
2316 #if defined(CONFIG_GTK_GL) && defined(GDK_WINDOWING_WAYLAND)
2317 if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
2318 gtk_use_gl_area
= true;
2323 DisplayGLMode mode
= opts
->has_gl
? opts
->gl
: DISPLAYGL_MODE_ON
;
2329 keycode_map
= gd_get_keymap(&keycode_maplen
);
2331 #if defined(CONFIG_VTE)
2332 type_register(&char_gd_vc_type_info
);
2336 static QemuDisplay qemu_display_gtk
= {
2337 .type
= DISPLAY_TYPE_GTK
,
2338 .early_init
= early_gtk_display_init
,
2339 .init
= gtk_display_init
,
2342 static void register_gtk(void)
2344 qemu_display_register(&qemu_display_gtk
);
2347 type_init(register_gtk
);