4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * Portions from gtk-vnc:
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.0 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
37 #include "qemu/osdep.h"
38 #include "qemu-common.h"
39 #include "qemu/cutils.h"
41 #include "ui/console.h"
44 #include <glib/gi18n.h>
46 #if defined(CONFIG_VTE)
53 #include "sysemu/sysemu.h"
54 #include "qmp-commands.h"
56 #include "chardev/char.h"
57 #include "qom/object.h"
60 #define VC_WINDOW_X_MIN 320
61 #define VC_WINDOW_Y_MIN 240
62 #define VC_TERM_X_MIN 80
63 #define VC_TERM_Y_MIN 25
64 #define VC_SCALE_MIN 0.25
65 #define VC_SCALE_STEP 0.25
67 #ifdef GDK_WINDOWING_X11
68 #include "ui/x_keymap.h"
71 #ifndef GDK_IS_X11_DISPLAY
72 #define GDK_IS_X11_DISPLAY(dpy) (dpy != NULL)
77 #ifdef GDK_WINDOWING_WAYLAND
79 #ifndef GDK_IS_WAYLAND_DISPLAY
80 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy != NULL)
85 #ifdef GDK_WINDOWING_WIN32
87 #ifndef GDK_IS_WIN32_DISPLAY
88 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy != NULL)
93 #ifdef GDK_WINDOWING_BROADWAY
95 #ifndef GDK_IS_BROADWAY_DISPLAY
96 #define GDK_IS_BROADWAY_DISPLAY(dpy) (dpy != NULL)
101 #ifdef GDK_WINDOWING_QUARTZ
103 #ifndef GDK_IS_QUARTZ_DISPLAY
104 #define GDK_IS_QUARTZ_DISPLAY(dpy) (dpy != NULL)
109 #if !defined(CONFIG_VTE)
110 # define VTE_CHECK_VERSION(a, b, c) 0
113 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
115 * The gtk2 vte terminal widget seriously messes up the window resize
116 * for some reason. You basically can't make the qemu window smaller
117 * any more because the toplevel window geoemtry hints are overridden.
119 * Workaround that by hiding all vte widgets, except the one in the
122 * Luckily everything works smooth in gtk3.
124 # define VTE_RESIZE_HACK 1
127 #if !GTK_CHECK_VERSION(2, 20, 0)
128 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
131 #ifndef GDK_IS_X11_DISPLAY
132 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
134 #ifndef GDK_IS_WAYLAND_DISPLAY
135 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
137 #ifndef GDK_IS_WIN32_DISPLAY
138 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
141 #if !GTK_CHECK_VERSION(2, 22, 0)
142 #define GDK_KEY_0 GDK_0
143 #define GDK_KEY_1 GDK_1
144 #define GDK_KEY_2 GDK_2
145 #define GDK_KEY_f GDK_f
146 #define GDK_KEY_g GDK_g
147 #define GDK_KEY_q GDK_q
148 #define GDK_KEY_plus GDK_plus
149 #define GDK_KEY_equal GDK_equal
150 #define GDK_KEY_minus GDK_minus
151 #define GDK_KEY_Pause GDK_Pause
152 #define GDK_KEY_Delete GDK_Delete
155 /* Some older mingw versions lack this constant or have
156 * it conditionally defined */
158 # ifndef MAPVK_VK_TO_VSC
159 # define MAPVK_VK_TO_VSC 0
164 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
166 static const int modifier_keycode
[] = {
177 static const guint16
*keycode_map
;
178 static size_t keycode_maplen
;
180 struct GtkDisplayState
{
185 GtkAccelGroup
*accel_group
;
187 GtkWidget
*machine_menu_item
;
188 GtkWidget
*machine_menu
;
189 GtkWidget
*pause_item
;
190 GtkWidget
*reset_item
;
191 GtkWidget
*powerdown_item
;
192 GtkWidget
*quit_item
;
194 GtkWidget
*view_menu_item
;
195 GtkWidget
*view_menu
;
196 GtkWidget
*full_screen_item
;
197 GtkWidget
*copy_item
;
198 GtkWidget
*zoom_in_item
;
199 GtkWidget
*zoom_out_item
;
200 GtkWidget
*zoom_fixed_item
;
201 GtkWidget
*zoom_fit_item
;
202 GtkWidget
*grab_item
;
203 GtkWidget
*grab_on_hover_item
;
206 VirtualConsole vc
[MAX_VCS
];
208 GtkWidget
*show_tabs_item
;
209 GtkWidget
*untabify_item
;
219 VirtualConsole
*kbd_owner
;
220 VirtualConsole
*ptr_owner
;
222 gboolean full_screen
;
224 GdkCursor
*null_cursor
;
225 Notifier mouse_mode_notifier
;
228 bool external_pause_update
;
230 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
234 typedef struct VCChardev
{
236 VirtualConsole
*console
;
240 #define TYPE_CHARDEV_VC "chardev-vc"
241 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
243 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
244 static void gd_ungrab_pointer(GtkDisplayState
*s
);
245 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
246 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
248 /** Utility Functions **/
250 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
255 for (i
= 0; i
< s
->nb_vcs
; i
++) {
257 if (gtk_check_menu_item_get_active
258 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
265 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
270 for (i
= 0; i
< s
->nb_vcs
; i
++) {
272 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
280 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
284 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
285 return gd_vc_find_by_page(s
, page
);
288 static bool gd_is_grab_active(GtkDisplayState
*s
)
290 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
293 static bool gd_grab_on_hover(GtkDisplayState
*s
)
295 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
298 static void gd_update_cursor(VirtualConsole
*vc
)
300 GtkDisplayState
*s
= vc
->s
;
303 if (vc
->type
!= GD_VC_GFX
||
304 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
308 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
312 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
313 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
314 gdk_window_set_cursor(window
, s
->null_cursor
);
316 gdk_window_set_cursor(window
, NULL
);
320 static void gd_update_caption(GtkDisplayState
*s
)
322 const char *status
= "";
325 const char *grab
= "";
326 bool is_paused
= !runstate_is_running();
330 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
332 prefix
= g_strdup_printf("QEMU");
335 if (s
->ptr_owner
!= NULL
&&
336 s
->ptr_owner
->window
== NULL
) {
337 grab
= _(" - Press Ctrl+Alt+G to release grab");
341 status
= _(" [Paused]");
343 s
->external_pause_update
= true;
344 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
346 s
->external_pause_update
= false;
348 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
349 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
352 for (i
= 0; i
< s
->nb_vcs
; i
++) {
353 VirtualConsole
*vc
= &s
->vc
[i
];
358 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
359 vc
== s
->kbd_owner
? " +kbd" : "",
360 vc
== s
->ptr_owner
? " +ptr" : "");
361 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
368 static void gd_update_geometry_hints(VirtualConsole
*vc
)
370 GtkDisplayState
*s
= vc
->s
;
371 GdkWindowHints mask
= 0;
372 GdkGeometry geo
= {};
373 GtkWidget
*geo_widget
= NULL
;
374 GtkWindow
*geo_window
;
376 if (vc
->type
== GD_VC_GFX
) {
381 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
382 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
383 mask
|= GDK_HINT_MIN_SIZE
;
385 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
386 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
387 mask
|= GDK_HINT_MIN_SIZE
;
389 geo_widget
= vc
->gfx
.drawing_area
;
390 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
392 #if defined(CONFIG_VTE)
393 } else if (vc
->type
== GD_VC_VTE
) {
394 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
395 GtkBorder padding
= { 0 };
397 #if VTE_CHECK_VERSION(0, 37, 0)
398 gtk_style_context_get_padding(
399 gtk_widget_get_style_context(vc
->vte
.terminal
),
400 gtk_widget_get_state_flags(vc
->vte
.terminal
),
404 GtkBorder
*ib
= NULL
;
405 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
413 geo
.width_inc
= vte_terminal_get_char_width(term
);
414 geo
.height_inc
= vte_terminal_get_char_height(term
);
415 mask
|= GDK_HINT_RESIZE_INC
;
416 geo
.base_width
= geo
.width_inc
;
417 geo
.base_height
= geo
.height_inc
;
418 mask
|= GDK_HINT_BASE_SIZE
;
419 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
420 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
421 mask
|= GDK_HINT_MIN_SIZE
;
423 geo
.base_width
+= padding
.left
+ padding
.right
;
424 geo
.base_height
+= padding
.top
+ padding
.bottom
;
425 geo
.min_width
+= padding
.left
+ padding
.right
;
426 geo
.min_height
+= padding
.top
+ padding
.bottom
;
427 geo_widget
= vc
->vte
.terminal
;
431 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
432 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
435 void gd_update_windowsize(VirtualConsole
*vc
)
437 GtkDisplayState
*s
= vc
->s
;
439 gd_update_geometry_hints(vc
);
441 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
442 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
443 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
447 static void gd_update_full_redraw(VirtualConsole
*vc
)
449 GtkWidget
*area
= vc
->gfx
.drawing_area
;
451 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
452 #if defined(CONFIG_GTK_GL)
454 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
458 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
461 static void gtk_release_modifiers(GtkDisplayState
*s
)
463 VirtualConsole
*vc
= gd_vc_find_current(s
);
466 if (vc
->type
!= GD_VC_GFX
||
467 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
470 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
471 qcode
= modifier_keycode
[i
];
472 if (!s
->modifier_pressed
[i
]) {
475 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, qcode
, false);
476 s
->modifier_pressed
[i
] = false;
480 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
483 g_object_ref(G_OBJECT(widget
));
484 gtk_container_remove(GTK_CONTAINER(from
), widget
);
485 gtk_container_add(GTK_CONTAINER(to
), widget
);
486 g_object_unref(G_OBJECT(widget
));
489 /** DisplayState Callbacks **/
491 static void gd_update(DisplayChangeListener
*dcl
,
492 int x
, int y
, int w
, int h
)
494 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
501 trace_gd_update(vc
->label
, x
, y
, w
, h
);
503 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
507 if (vc
->gfx
.convert
) {
508 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
509 NULL
, vc
->gfx
.convert
,
510 x
, y
, 0, 0, x
, y
, w
, h
);
513 x1
= floor(x
* vc
->gfx
.scale_x
);
514 y1
= floor(y
* vc
->gfx
.scale_y
);
516 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
517 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
519 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
520 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
522 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
526 gdk_drawable_get_size(win
, &ww
, &wh
);
536 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
537 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
540 static void gd_refresh(DisplayChangeListener
*dcl
)
542 graphic_hw_update(dcl
->con
);
545 #if GTK_CHECK_VERSION(3, 0, 0)
546 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
548 #if GTK_CHECK_VERSION(3, 20, 0)
549 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
551 return gdk_device_manager_get_client_pointer(
552 gdk_display_get_device_manager(dpy
));
556 static void gd_mouse_set(DisplayChangeListener
*dcl
,
557 int x
, int y
, int visible
)
559 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
563 if (qemu_input_is_absolute()) {
567 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
568 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
569 x
, y
, &x_root
, &y_root
);
570 gdk_device_warp(gd_get_pointer(dpy
),
571 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
577 static void gd_mouse_set(DisplayChangeListener
*dcl
,
578 int x
, int y
, int visible
)
580 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
583 if (qemu_input_is_absolute()) {
587 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
588 x
, y
, &x_root
, &y_root
);
589 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
590 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
595 static void gd_cursor_define(DisplayChangeListener
*dcl
,
598 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
602 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
606 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
607 GDK_COLORSPACE_RGB
, true, 8,
608 c
->width
, c
->height
, c
->width
* 4,
610 cursor
= gdk_cursor_new_from_pixbuf
611 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
612 pixbuf
, c
->hot_x
, c
->hot_y
);
613 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
614 g_object_unref(pixbuf
);
615 #if !GTK_CHECK_VERSION(3, 0, 0)
616 gdk_cursor_unref(cursor
);
618 g_object_unref(cursor
);
622 static void gd_switch(DisplayChangeListener
*dcl
,
623 DisplaySurface
*surface
)
625 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
628 trace_gd_switch(vc
->label
,
629 surface
? surface_width(surface
) : 0,
630 surface
? surface_height(surface
) : 0);
632 if (vc
->gfx
.surface
) {
633 cairo_surface_destroy(vc
->gfx
.surface
);
634 vc
->gfx
.surface
= NULL
;
636 if (vc
->gfx
.convert
) {
637 pixman_image_unref(vc
->gfx
.convert
);
638 vc
->gfx
.convert
= NULL
;
641 if (vc
->gfx
.ds
&& surface
&&
642 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
643 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
646 vc
->gfx
.ds
= surface
;
652 if (surface
->format
== PIXMAN_x8r8g8b8
) {
654 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
656 * No need to convert, use surface directly. Should be the
657 * common case as this is qemu_default_pixelformat(32) too.
659 vc
->gfx
.surface
= cairo_image_surface_create_for_data
660 (surface_data(surface
),
662 surface_width(surface
),
663 surface_height(surface
),
664 surface_stride(surface
));
666 /* Must convert surface, use pixman to do it. */
667 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
668 surface_width(surface
),
669 surface_height(surface
),
671 vc
->gfx
.surface
= cairo_image_surface_create_for_data
672 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
674 pixman_image_get_width(vc
->gfx
.convert
),
675 pixman_image_get_height(vc
->gfx
.convert
),
676 pixman_image_get_stride(vc
->gfx
.convert
));
677 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
678 NULL
, vc
->gfx
.convert
,
680 pixman_image_get_width(vc
->gfx
.convert
),
681 pixman_image_get_height(vc
->gfx
.convert
));
685 gd_update_windowsize(vc
);
687 gd_update_full_redraw(vc
);
691 static const DisplayChangeListenerOps dcl_ops
= {
693 .dpy_gfx_update
= gd_update
,
694 .dpy_gfx_switch
= gd_switch
,
695 .dpy_gfx_check_format
= qemu_pixman_check_format
,
696 .dpy_refresh
= gd_refresh
,
697 .dpy_mouse_set
= gd_mouse_set
,
698 .dpy_cursor_define
= gd_cursor_define
,
702 #if defined(CONFIG_OPENGL)
704 /** DisplayState Callbacks (opengl version) **/
706 #if defined(CONFIG_GTK_GL)
708 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
709 .dpy_name
= "gtk-egl",
710 .dpy_gfx_update
= gd_gl_area_update
,
711 .dpy_gfx_switch
= gd_gl_area_switch
,
712 .dpy_gfx_check_format
= console_gl_check_format
,
713 .dpy_refresh
= gd_gl_area_refresh
,
714 .dpy_mouse_set
= gd_mouse_set
,
715 .dpy_cursor_define
= gd_cursor_define
,
717 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
718 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
719 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
720 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
721 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
722 .dpy_gl_update
= gd_gl_area_scanout_flush
,
727 static const DisplayChangeListenerOps dcl_egl_ops
= {
728 .dpy_name
= "gtk-egl",
729 .dpy_gfx_update
= gd_egl_update
,
730 .dpy_gfx_switch
= gd_egl_switch
,
731 .dpy_gfx_check_format
= console_gl_check_format
,
732 .dpy_refresh
= gd_egl_refresh
,
733 .dpy_mouse_set
= gd_mouse_set
,
734 .dpy_cursor_define
= gd_cursor_define
,
736 .dpy_gl_ctx_create
= gd_egl_create_context
,
737 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
738 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
739 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
740 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
741 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
742 .dpy_gl_update
= gd_egl_scanout_flush
,
745 #endif /* CONFIG_GTK_GL */
746 #endif /* CONFIG_OPENGL */
750 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
752 GtkDisplayState
*s
= opaque
;
754 gd_update_caption(s
);
757 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
762 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
763 /* release the grab at switching to absolute mode */
764 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
765 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
768 for (i
= 0; i
< s
->nb_vcs
; i
++) {
769 VirtualConsole
*vc
= &s
->vc
[i
];
770 gd_update_cursor(vc
);
776 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
779 GtkDisplayState
*s
= opaque
;
783 for (i
= 0; i
< s
->nb_vcs
; i
++) {
784 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
787 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
796 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
800 memset(&info
, 0, sizeof(info
));
802 info
.height
= height
;
803 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
806 #if defined(CONFIG_GTK_GL)
808 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
811 VirtualConsole
*vc
= opaque
;
819 static void gd_resize_event(GtkGLArea
*area
,
820 gint width
, gint height
, gpointer
*opaque
)
822 VirtualConsole
*vc
= (void *)opaque
;
824 gd_set_ui_info(vc
, width
, height
);
829 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
831 VirtualConsole
*vc
= opaque
;
832 GtkDisplayState
*s
= vc
->s
;
837 #if defined(CONFIG_OPENGL)
839 #if defined(CONFIG_GTK_GL)
840 /* invoke render callback please */
849 if (!gtk_widget_get_realized(widget
)) {
856 fbw
= surface_width(vc
->gfx
.ds
);
857 fbh
= surface_height(vc
->gfx
.ds
);
859 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
861 if (s
->full_screen
) {
862 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
863 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
864 } else if (s
->free_scale
) {
867 sx
= (double)ww
/ fbw
;
868 sy
= (double)wh
/ fbh
;
870 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
873 fbw
*= vc
->gfx
.scale_x
;
874 fbh
*= vc
->gfx
.scale_y
;
884 cairo_rectangle(cr
, 0, 0, ww
, wh
);
886 /* Optionally cut out the inner area where the pixmap
887 will be drawn. This avoids 'flashing' since we're
888 not double-buffering. Note we're using the undocumented
889 behaviour of drawing the rectangle from right to left
890 to cut out the whole */
891 cairo_rectangle(cr
, mx
+ fbw
, my
,
895 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
896 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
897 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
903 #if !GTK_CHECK_VERSION(3, 0, 0)
904 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
910 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
915 expose
->area
.height
);
918 ret
= gd_draw_event(widget
, cr
, opaque
);
926 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
929 VirtualConsole
*vc
= opaque
;
930 GtkDisplayState
*s
= vc
->s
;
940 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
941 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
943 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
954 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
955 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
957 if (qemu_input_is_absolute()) {
958 if (x
< 0 || y
< 0 ||
959 x
>= surface_width(vc
->gfx
.ds
) ||
960 y
>= surface_height(vc
->gfx
.ds
)) {
963 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
964 0, surface_width(vc
->gfx
.ds
));
965 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
966 0, surface_height(vc
->gfx
.ds
));
967 qemu_input_event_sync();
968 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
969 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
970 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
971 qemu_input_event_sync();
977 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
978 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
979 int screen_width
, screen_height
;
981 int x
= (int)motion
->x_root
;
982 int y
= (int)motion
->y_root
;
984 #if GTK_CHECK_VERSION(3, 22, 0)
986 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
987 GdkWindow
*win
= gtk_widget_get_window(widget
);
988 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
989 GdkRectangle geometry
;
990 gdk_monitor_get_geometry(monitor
, &geometry
);
991 screen_width
= geometry
.width
;
992 screen_height
= geometry
.height
;
996 screen_width
= gdk_screen_get_width(screen
);
997 screen_height
= gdk_screen_get_height(screen
);
1001 /* In relative mode check to see if client pointer hit
1002 * one of the screen edges, and if so move it back by
1003 * 200 pixels. This is important because the pointer
1004 * in the server doesn't correspond 1-for-1, and so
1005 * may still be only half way across the screen. Without
1006 * this warp, the server pointer would thus appear to hit
1007 * an invisible wall */
1014 if (x
== (screen_width
- 1)) {
1017 if (y
== (screen_height
- 1)) {
1021 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
1022 #if GTK_CHECK_VERSION(3, 0, 0)
1023 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
1024 gdk_device_warp(dev
, screen
, x
, y
);
1026 GdkDisplay
*display
= gtk_widget_get_display(widget
);
1027 gdk_display_warp_pointer(display
, screen
, x
, y
);
1029 s
->last_set
= FALSE
;
1036 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
1039 VirtualConsole
*vc
= opaque
;
1040 GtkDisplayState
*s
= vc
->s
;
1043 /* implicitly grab the input at the first click in the relative mode */
1044 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
1045 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
1047 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1050 gd_grab_pointer(vc
, "relative-mode-click");
1055 if (button
->button
== 1) {
1056 btn
= INPUT_BUTTON_LEFT
;
1057 } else if (button
->button
== 2) {
1058 btn
= INPUT_BUTTON_MIDDLE
;
1059 } else if (button
->button
== 3) {
1060 btn
= INPUT_BUTTON_RIGHT
;
1061 } else if (button
->button
== 8) {
1062 btn
= INPUT_BUTTON_SIDE
;
1063 } else if (button
->button
== 9) {
1064 btn
= INPUT_BUTTON_EXTRA
;
1069 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
1070 button
->type
== GDK_BUTTON_PRESS
);
1071 qemu_input_event_sync();
1075 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
1078 VirtualConsole
*vc
= opaque
;
1081 if (scroll
->direction
== GDK_SCROLL_UP
) {
1082 btn
= INPUT_BUTTON_WHEEL_UP
;
1083 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
1084 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1085 #if GTK_CHECK_VERSION(3, 4, 0)
1086 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
1087 gdouble delta_x
, delta_y
;
1088 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
1089 &delta_x
, &delta_y
)) {
1093 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1095 btn
= INPUT_BUTTON_WHEEL_UP
;
1102 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
1103 qemu_input_event_sync();
1104 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
1105 qemu_input_event_sync();
1110 static const guint16
*gd_get_keymap(size_t *maplen
)
1112 GdkDisplay
*dpy
= gdk_display_get_default();
1114 #ifdef GDK_WINDOWING_X11
1115 if (GDK_IS_X11_DISPLAY(dpy
)) {
1116 trace_gd_keymap_windowing("x11");
1117 return qemu_xkeymap_mapping_table(
1118 gdk_x11_display_get_xdisplay(dpy
), maplen
);
1122 #ifdef GDK_WINDOWING_WAYLAND
1123 if (GDK_IS_WAYLAND_DISPLAY(dpy
)) {
1124 trace_gd_keymap_windowing("wayland");
1125 *maplen
= qemu_input_map_xorgevdev_to_qcode_len
;
1126 return qemu_input_map_xorgevdev_to_qcode
;
1130 #ifdef GDK_WINDOWING_WIN32
1131 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1132 trace_gd_keymap_windowing("win32");
1133 *maplen
= qemu_input_map_win32_to_qcode_len
;
1134 return qemu_input_map_win32_to_qcode
;
1138 #ifdef GDK_WINDOWING_QUARTZ
1139 if (GDK_IS_QUARTZ_DISPLAY(dpy
)) {
1140 trace_gd_keymap_windowing("quartz");
1141 *maplen
= qemu_input_map_osx_to_qcode_len
;
1142 return qemu_input_map_osx_to_qcode
;
1146 #ifdef GDK_WINDOWING_BROADWAY
1147 if (GDK_IS_BROADWAY_DISPLAY(dpy
)) {
1148 trace_gd_keymap_windowing("broadway");
1149 g_warning("experimental: using broadway, x11 virtual keysym\n"
1150 "mapping - with very limited support. See also\n"
1151 "https://bugzilla.gnome.org/show_bug.cgi?id=700105");
1152 *maplen
= qemu_input_map_x11_to_qcode_len
;
1153 return qemu_input_map_x11_to_qcode
;
1157 g_warning("Unsupported GDK Windowing platform.\n"
1158 "Disabling extended keycode tables.\n"
1159 "Please report to qemu-devel@nongnu.org\n"
1160 "including the following information:\n"
1162 " - Operating system\n"
1163 " - GDK Windowing system build\n");
1168 static int gd_map_keycode(int scancode
)
1173 if (scancode
> keycode_maplen
) {
1177 return keycode_map
[scancode
];
1180 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1181 GdkEventKey
*key
, void *opaque
)
1183 VirtualConsole
*vc
= opaque
;
1184 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1186 if (key
->keyval
== GDK_KEY_Delete
) {
1187 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
);
1188 } else if (key
->length
) {
1189 kbd_put_string_console(con
, key
->string
, key
->length
);
1191 int qcode
= gd_map_keycode(key
->hardware_keycode
);
1192 kbd_put_qcode_console(con
, qcode
);
1197 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1199 VirtualConsole
*vc
= opaque
;
1200 GtkDisplayState
*s
= vc
->s
;
1204 if (s
->ignore_keys
) {
1205 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1210 /* on windows, we ought to ignore the reserved key event? */
1211 if (key
->hardware_keycode
== 0xff)
1215 if (key
->keyval
== GDK_KEY_Pause
1217 /* for some reason GDK does not fill keyval for VK_PAUSE
1218 * See https://bugzilla.gnome.org/show_bug.cgi?id=769214
1220 || key
->hardware_keycode
== VK_PAUSE
1223 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
1224 key
->type
== GDK_KEY_PRESS
);
1228 qcode
= gd_map_keycode(key
->hardware_keycode
);
1230 trace_gd_key_event(vc
->label
, key
->hardware_keycode
, qcode
,
1231 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1233 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
1234 if (qcode
== modifier_keycode
[i
]) {
1235 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
1239 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, qcode
,
1240 key
->type
== GDK_KEY_PRESS
);
1245 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1247 if (event
->type
== GDK_MOTION_NOTIFY
) {
1248 return gd_motion_event(widget
, &event
->motion
, opaque
);
1253 /** Window Menu Actions **/
1255 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1257 GtkDisplayState
*s
= opaque
;
1259 if (s
->external_pause_update
) {
1262 if (runstate_is_running()) {
1269 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1271 qmp_system_reset(NULL
);
1274 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1276 qmp_system_powerdown(NULL
);
1279 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1284 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1286 GtkDisplayState
*s
= opaque
;
1287 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1288 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1291 gtk_release_modifiers(s
);
1293 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1294 gtk_notebook_set_current_page(nb
, page
);
1295 gtk_widget_grab_focus(vc
->focus
);
1297 s
->ignore_keys
= false;
1300 static void gd_accel_switch_vc(void *opaque
)
1302 VirtualConsole
*vc
= opaque
;
1304 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1305 #if !GTK_CHECK_VERSION(3, 0, 0)
1306 /* GTK2 sends the accel key to the target console - ignore this until */
1307 vc
->s
->ignore_keys
= true;
1311 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1313 GtkDisplayState
*s
= opaque
;
1314 VirtualConsole
*vc
= gd_vc_find_current(s
);
1316 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1317 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1319 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1321 gd_update_windowsize(vc
);
1324 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1327 VirtualConsole
*vc
= opaque
;
1328 GtkDisplayState
*s
= vc
->s
;
1330 gtk_widget_set_sensitive(vc
->menu_item
, true);
1331 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1332 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1333 vc
->tab_item
, vc
->label
);
1334 gtk_widget_destroy(vc
->window
);
1339 static gboolean
gd_win_grab(void *opaque
)
1341 VirtualConsole
*vc
= opaque
;
1343 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1344 if (vc
->s
->ptr_owner
) {
1345 gd_ungrab_pointer(vc
->s
);
1347 gd_grab_pointer(vc
, "user-request-detached-tab");
1352 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1354 GtkDisplayState
*s
= opaque
;
1355 VirtualConsole
*vc
= gd_vc_find_current(s
);
1357 if (vc
->type
== GD_VC_GFX
&&
1358 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1359 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1363 gtk_widget_set_sensitive(vc
->menu_item
, false);
1364 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1365 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1367 g_signal_connect(vc
->window
, "delete-event",
1368 G_CALLBACK(gd_tab_window_close
), vc
);
1369 gtk_widget_show_all(vc
->window
);
1371 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1372 GtkAccelGroup
*ag
= gtk_accel_group_new();
1373 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1375 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1377 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1380 gd_update_geometry_hints(vc
);
1381 gd_update_caption(s
);
1385 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1387 GtkDisplayState
*s
= opaque
;
1388 VirtualConsole
*vc
= gd_vc_find_current(s
);
1390 if (!s
->full_screen
) {
1391 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1392 gtk_widget_hide(s
->menu_bar
);
1393 if (vc
->type
== GD_VC_GFX
) {
1394 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1396 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1397 s
->full_screen
= TRUE
;
1399 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1400 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1401 gtk_widget_show(s
->menu_bar
);
1402 s
->full_screen
= FALSE
;
1403 if (vc
->type
== GD_VC_GFX
) {
1404 vc
->gfx
.scale_x
= 1.0;
1405 vc
->gfx
.scale_y
= 1.0;
1406 gd_update_windowsize(vc
);
1410 gd_update_cursor(vc
);
1413 static void gd_accel_full_screen(void *opaque
)
1415 GtkDisplayState
*s
= opaque
;
1416 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1419 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1421 GtkDisplayState
*s
= opaque
;
1422 VirtualConsole
*vc
= gd_vc_find_current(s
);
1424 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1427 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1428 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1430 gd_update_windowsize(vc
);
1433 static void gd_accel_zoom_in(void *opaque
)
1435 GtkDisplayState
*s
= opaque
;
1436 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1439 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1441 GtkDisplayState
*s
= opaque
;
1442 VirtualConsole
*vc
= gd_vc_find_current(s
);
1444 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1447 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1448 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1450 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1451 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1453 gd_update_windowsize(vc
);
1456 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1458 GtkDisplayState
*s
= opaque
;
1459 VirtualConsole
*vc
= gd_vc_find_current(s
);
1461 vc
->gfx
.scale_x
= 1.0;
1462 vc
->gfx
.scale_y
= 1.0;
1464 gd_update_windowsize(vc
);
1467 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1469 GtkDisplayState
*s
= opaque
;
1470 VirtualConsole
*vc
= gd_vc_find_current(s
);
1472 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1473 s
->free_scale
= TRUE
;
1475 s
->free_scale
= FALSE
;
1476 vc
->gfx
.scale_x
= 1.0;
1477 vc
->gfx
.scale_y
= 1.0;
1480 gd_update_windowsize(vc
);
1481 gd_update_full_redraw(vc
);
1484 #if GTK_CHECK_VERSION(3, 20, 0)
1485 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1487 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1488 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1489 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1490 GdkSeatCapabilities caps
= 0;
1491 GdkCursor
*cursor
= NULL
;
1494 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1497 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1498 cursor
= vc
->s
->null_cursor
;
1502 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1505 gdk_seat_ungrab(seat
);
1508 #elif GTK_CHECK_VERSION(3, 0, 0)
1509 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1510 GdkInputSource source
, GdkEventMask mask
,
1513 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1514 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1515 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1518 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1519 GdkDevice
*dev
= tmp
->data
;
1520 if (gdk_device_get_source(dev
) != source
) {
1524 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1525 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1526 mask
, cursor
, GDK_CURRENT_TIME
);
1528 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1535 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1537 if (vc
->s
->kbd_owner
) {
1538 if (vc
->s
->kbd_owner
== vc
) {
1541 gd_ungrab_keyboard(vc
->s
);
1545 #if GTK_CHECK_VERSION(3, 20, 0)
1546 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1547 #elif GTK_CHECK_VERSION(3, 0, 0)
1548 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1549 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1552 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1556 vc
->s
->kbd_owner
= vc
;
1557 gd_update_caption(vc
->s
);
1558 trace_gd_grab(vc
->label
, "kbd", reason
);
1561 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1563 VirtualConsole
*vc
= s
->kbd_owner
;
1568 s
->kbd_owner
= NULL
;
1570 #if GTK_CHECK_VERSION(3, 20, 0)
1571 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1572 #elif GTK_CHECK_VERSION(3, 0, 0)
1573 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1575 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1577 gd_update_caption(s
);
1578 trace_gd_ungrab(vc
->label
, "kbd");
1581 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1583 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1585 if (vc
->s
->ptr_owner
) {
1586 if (vc
->s
->ptr_owner
== vc
) {
1589 gd_ungrab_pointer(vc
->s
);
1593 #if GTK_CHECK_VERSION(3, 20, 0)
1594 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1595 gdk_device_get_position(gd_get_pointer(display
),
1596 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1597 #elif GTK_CHECK_VERSION(3, 0, 0)
1598 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1599 GDK_POINTER_MOTION_MASK
|
1600 GDK_BUTTON_PRESS_MASK
|
1601 GDK_BUTTON_RELEASE_MASK
|
1602 GDK_BUTTON_MOTION_MASK
|
1604 vc
->s
->null_cursor
);
1605 gdk_device_get_position(gd_get_pointer(display
),
1606 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1608 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1609 FALSE
, /* All events to come to our window directly */
1610 GDK_POINTER_MOTION_MASK
|
1611 GDK_BUTTON_PRESS_MASK
|
1612 GDK_BUTTON_RELEASE_MASK
|
1613 GDK_BUTTON_MOTION_MASK
|
1615 NULL
, /* Allow cursor to move over entire desktop */
1618 gdk_display_get_pointer(display
, NULL
,
1619 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1621 vc
->s
->ptr_owner
= vc
;
1622 gd_update_caption(vc
->s
);
1623 trace_gd_grab(vc
->label
, "ptr", reason
);
1626 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1628 VirtualConsole
*vc
= s
->ptr_owner
;
1629 GdkDisplay
*display
;
1634 s
->ptr_owner
= NULL
;
1636 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1637 #if GTK_CHECK_VERSION(3, 20, 0)
1638 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1639 gdk_device_warp(gd_get_pointer(display
),
1640 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1641 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1642 #elif GTK_CHECK_VERSION(3, 0, 0)
1643 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1644 gdk_device_warp(gd_get_pointer(display
),
1645 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1646 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1648 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1649 gdk_display_warp_pointer(display
,
1650 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1651 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1653 gd_update_caption(s
);
1654 trace_gd_ungrab(vc
->label
, "ptr");
1657 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1659 GtkDisplayState
*s
= opaque
;
1660 VirtualConsole
*vc
= gd_vc_find_current(s
);
1662 if (gd_is_grab_active(s
)) {
1663 gd_grab_keyboard(vc
, "user-request-main-window");
1664 gd_grab_pointer(vc
, "user-request-main-window");
1666 gd_ungrab_keyboard(s
);
1667 gd_ungrab_pointer(s
);
1670 gd_update_cursor(vc
);
1673 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1676 GtkDisplayState
*s
= data
;
1680 if (!gtk_widget_get_realized(s
->notebook
)) {
1684 #ifdef VTE_RESIZE_HACK
1685 vc
= gd_vc_find_current(s
);
1686 if (vc
&& vc
->type
== GD_VC_VTE
) {
1687 gtk_widget_hide(vc
->vte
.terminal
);
1690 vc
= gd_vc_find_by_page(s
, arg2
);
1694 #ifdef VTE_RESIZE_HACK
1695 if (vc
->type
== GD_VC_VTE
) {
1696 gtk_widget_show(vc
->vte
.terminal
);
1699 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1701 on_vga
= (vc
->type
== GD_VC_GFX
&&
1702 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1704 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1706 } else if (s
->full_screen
) {
1707 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1710 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1712 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1715 gd_update_windowsize(vc
);
1716 gd_update_cursor(vc
);
1719 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1722 VirtualConsole
*vc
= opaque
;
1723 GtkDisplayState
*s
= vc
->s
;
1725 if (gd_grab_on_hover(s
)) {
1726 gd_grab_keyboard(vc
, "grab-on-hover");
1731 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1734 VirtualConsole
*vc
= opaque
;
1735 GtkDisplayState
*s
= vc
->s
;
1737 if (gd_grab_on_hover(s
)) {
1738 gd_ungrab_keyboard(s
);
1743 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1744 GdkEventCrossing
*crossing
, gpointer opaque
)
1746 VirtualConsole
*vc
= opaque
;
1747 GtkDisplayState
*s
= vc
->s
;
1749 gtk_release_modifiers(s
);
1753 static gboolean
gd_configure(GtkWidget
*widget
,
1754 GdkEventConfigure
*cfg
, gpointer opaque
)
1756 VirtualConsole
*vc
= opaque
;
1758 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1762 /** Virtual Console Callbacks **/
1764 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1765 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1767 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1768 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1769 HOTKEY_MODIFIERS
, 0,
1770 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1771 #if GTK_CHECK_VERSION(3, 8, 0)
1772 gtk_accel_label_set_accel(
1773 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1774 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1777 g_signal_connect(vc
->menu_item
, "activate",
1778 G_CALLBACK(gd_menu_switch_vc
), s
);
1779 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1781 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1785 #if defined(CONFIG_VTE)
1786 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1788 GtkDisplayState
*s
= opaque
;
1789 VirtualConsole
*vc
= gd_vc_find_current(s
);
1791 #if VTE_CHECK_VERSION(0, 50, 0)
1792 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1795 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1799 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1801 VirtualConsole
*vc
= opaque
;
1803 if (gtk_adjustment_get_upper(adjustment
) >
1804 gtk_adjustment_get_page_size(adjustment
)) {
1805 gtk_widget_show(vc
->vte
.scrollbar
);
1807 gtk_widget_hide(vc
->vte
.scrollbar
);
1811 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1813 VCChardev
*vcd
= VC_CHARDEV(chr
);
1814 VirtualConsole
*vc
= vcd
->console
;
1816 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1820 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1822 VCChardev
*vcd
= VC_CHARDEV(chr
);
1823 VirtualConsole
*vc
= vcd
->console
;
1826 vc
->vte
.echo
= echo
;
1833 static Chardev
*vcs
[MAX_VCS
];
1834 static void gd_vc_open(Chardev
*chr
,
1835 ChardevBackend
*backend
,
1839 if (nb_vcs
== MAX_VCS
) {
1840 error_setg(errp
, "Maximum number of consoles reached");
1844 vcs
[nb_vcs
++] = chr
;
1846 /* console/chardev init sometimes completes elsewhere in a 2nd
1847 * stage, so defer OPENED events until they are fully initialized
1852 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1854 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1856 cc
->parse
= qemu_chr_parse_vc
;
1857 cc
->open
= gd_vc_open
;
1858 cc
->chr_write
= gd_vc_chr_write
;
1859 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1862 static const TypeInfo char_gd_vc_type_info
= {
1863 .name
= TYPE_CHARDEV_VC
,
1864 .parent
= TYPE_CHARDEV
,
1865 .instance_size
= sizeof(VCChardev
),
1866 .class_init
= char_gd_vc_class_init
,
1869 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1872 VirtualConsole
*vc
= user_data
;
1875 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1877 for (i
= 0; i
< size
; i
++) {
1878 uint8_t c
= text
[i
];
1879 if (c
>= 128 || isprint(c
)) {
1880 /* 8-bit characters are considered printable. */
1881 vte_terminal_feed(term
, &text
[i
], 1);
1882 } else if (c
== '\r' || c
== '\n') {
1883 vte_terminal_feed(term
, "\r\n", 2);
1885 char ctrl
[2] = { '^', 0};
1886 ctrl
[1] = text
[i
] ^ 64;
1887 vte_terminal_feed(term
, ctrl
, 2);
1892 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1896 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1897 Chardev
*chr
, int idx
,
1898 GSList
*group
, GtkWidget
*view_menu
)
1902 GtkWidget
*scrollbar
;
1903 GtkAdjustment
*vadjustment
;
1904 VCChardev
*vcd
= VC_CHARDEV(chr
);
1907 vc
->vte
.echo
= vcd
->echo
;
1911 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1912 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1913 ? vc
->vte
.chr
->label
: buffer
);
1914 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1916 vc
->vte
.terminal
= vte_terminal_new();
1917 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1919 /* The documentation says that the default is UTF-8, but actually it is
1920 * 7-bit ASCII at least in VTE 0.38.
1922 #if VTE_CHECK_VERSION(0, 38, 0)
1923 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1925 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1928 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1929 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1930 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1932 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1933 vadjustment
= gtk_scrollable_get_vadjustment
1934 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1936 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1939 #if GTK_CHECK_VERSION(3, 0, 0)
1940 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1941 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1943 box
= gtk_hbox_new(false, 2);
1944 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1947 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1948 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1951 vc
->vte
.scrollbar
= scrollbar
;
1953 g_signal_connect(vadjustment
, "changed",
1954 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1956 vc
->type
= GD_VC_VTE
;
1958 vc
->focus
= vc
->vte
.terminal
;
1959 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1960 gtk_label_new(vc
->label
));
1962 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1967 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1968 GtkWidget
*view_menu
)
1972 for (i
= 0; i
< nb_vcs
; i
++) {
1973 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1974 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1978 #endif /* CONFIG_VTE */
1980 /** Window Creation **/
1982 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1984 #if GTK_CHECK_VERSION(3, 0, 0)
1985 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1986 G_CALLBACK(gd_draw_event
), vc
);
1987 #if defined(CONFIG_GTK_GL)
1988 if (display_opengl
) {
1989 /* wire up GtkGlArea events */
1990 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1991 G_CALLBACK(gd_render_event
), vc
);
1992 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1993 G_CALLBACK(gd_resize_event
), vc
);
1997 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1998 G_CALLBACK(gd_expose_event
), vc
);
2000 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
2001 g_signal_connect(vc
->gfx
.drawing_area
, "event",
2002 G_CALLBACK(gd_event
), vc
);
2003 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
2004 G_CALLBACK(gd_button_event
), vc
);
2005 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
2006 G_CALLBACK(gd_button_event
), vc
);
2007 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
2008 G_CALLBACK(gd_scroll_event
), vc
);
2009 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
2010 G_CALLBACK(gd_key_event
), vc
);
2011 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
2012 G_CALLBACK(gd_key_event
), vc
);
2014 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
2015 G_CALLBACK(gd_enter_event
), vc
);
2016 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
2017 G_CALLBACK(gd_leave_event
), vc
);
2018 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
2019 G_CALLBACK(gd_focus_out_event
), vc
);
2020 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
2021 G_CALLBACK(gd_configure
), vc
);
2023 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
2024 G_CALLBACK(gd_text_key_down
), vc
);
2028 static void gd_connect_signals(GtkDisplayState
*s
)
2030 g_signal_connect(s
->show_tabs_item
, "activate",
2031 G_CALLBACK(gd_menu_show_tabs
), s
);
2032 g_signal_connect(s
->untabify_item
, "activate",
2033 G_CALLBACK(gd_menu_untabify
), s
);
2035 g_signal_connect(s
->window
, "delete-event",
2036 G_CALLBACK(gd_window_close
), s
);
2038 g_signal_connect(s
->pause_item
, "activate",
2039 G_CALLBACK(gd_menu_pause
), s
);
2040 g_signal_connect(s
->reset_item
, "activate",
2041 G_CALLBACK(gd_menu_reset
), s
);
2042 g_signal_connect(s
->powerdown_item
, "activate",
2043 G_CALLBACK(gd_menu_powerdown
), s
);
2044 g_signal_connect(s
->quit_item
, "activate",
2045 G_CALLBACK(gd_menu_quit
), s
);
2046 #if defined(CONFIG_VTE)
2047 g_signal_connect(s
->copy_item
, "activate",
2048 G_CALLBACK(gd_menu_copy
), s
);
2050 g_signal_connect(s
->full_screen_item
, "activate",
2051 G_CALLBACK(gd_menu_full_screen
), s
);
2052 g_signal_connect(s
->zoom_in_item
, "activate",
2053 G_CALLBACK(gd_menu_zoom_in
), s
);
2054 g_signal_connect(s
->zoom_out_item
, "activate",
2055 G_CALLBACK(gd_menu_zoom_out
), s
);
2056 g_signal_connect(s
->zoom_fixed_item
, "activate",
2057 G_CALLBACK(gd_menu_zoom_fixed
), s
);
2058 g_signal_connect(s
->zoom_fit_item
, "activate",
2059 G_CALLBACK(gd_menu_zoom_fit
), s
);
2060 g_signal_connect(s
->grab_item
, "activate",
2061 G_CALLBACK(gd_menu_grab_input
), s
);
2062 g_signal_connect(s
->notebook
, "switch-page",
2063 G_CALLBACK(gd_change_page
), s
);
2066 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
2068 GtkWidget
*machine_menu
;
2069 GtkWidget
*separator
;
2071 machine_menu
= gtk_menu_new();
2072 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
2074 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
2075 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
2077 separator
= gtk_separator_menu_item_new();
2078 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
2080 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
2081 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
2083 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
2084 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
2086 separator
= gtk_separator_menu_item_new();
2087 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
2089 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
2090 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
2091 "<QEMU>/Machine/Quit");
2092 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2093 GDK_KEY_q
, HOTKEY_MODIFIERS
);
2094 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
2096 return machine_menu
;
2099 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
2100 QemuConsole
*con
, int idx
,
2101 GSList
*group
, GtkWidget
*view_menu
)
2103 vc
->label
= qemu_console_get_label(con
);
2105 vc
->gfx
.scale_x
= 1.0;
2106 vc
->gfx
.scale_y
= 1.0;
2108 #if defined(CONFIG_OPENGL)
2109 if (display_opengl
) {
2110 #if defined(CONFIG_GTK_GL)
2111 vc
->gfx
.drawing_area
= gtk_gl_area_new();
2112 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
2114 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2116 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2117 * It is required for opengl rendering on X11 though. A
2118 * proper replacement (native opengl support) is only
2119 * available in 3.16+. Silence the warning if possible.
2121 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2122 #pragma GCC diagnostic push
2123 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2125 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2126 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2127 #pragma GCC diagnostic pop
2129 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2130 #endif /* CONFIG_GTK_GL */
2134 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2135 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2139 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2140 GDK_POINTER_MOTION_MASK
|
2141 GDK_BUTTON_PRESS_MASK
|
2142 GDK_BUTTON_RELEASE_MASK
|
2143 GDK_BUTTON_MOTION_MASK
|
2144 GDK_ENTER_NOTIFY_MASK
|
2145 GDK_LEAVE_NOTIFY_MASK
|
2147 GDK_KEY_PRESS_MASK
);
2148 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2150 vc
->type
= GD_VC_GFX
;
2151 vc
->tab_item
= vc
->gfx
.drawing_area
;
2152 vc
->focus
= vc
->gfx
.drawing_area
;
2153 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2154 vc
->tab_item
, gtk_label_new(vc
->label
));
2156 vc
->gfx
.dcl
.con
= con
;
2157 register_displaychangelistener(&vc
->gfx
.dcl
);
2159 gd_connect_vc_gfx_signals(vc
);
2160 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2162 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2163 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2164 s
->free_scale
= true;
2170 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2172 GSList
*group
= NULL
;
2173 GtkWidget
*view_menu
;
2174 GtkWidget
*separator
;
2178 view_menu
= gtk_menu_new();
2179 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2181 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2183 #if defined(CONFIG_VTE)
2184 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2185 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2188 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2189 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2190 #if GTK_CHECK_VERSION(3, 8, 0)
2191 gtk_accel_label_set_accel(
2192 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2193 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2195 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2197 separator
= gtk_separator_menu_item_new();
2198 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2200 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2201 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2202 "<QEMU>/View/Zoom In");
2203 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2205 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2206 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2207 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2209 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2210 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2211 "<QEMU>/View/Zoom Out");
2212 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2214 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2216 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2217 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2218 "<QEMU>/View/Zoom Fixed");
2219 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2221 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2223 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2224 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2226 separator
= gtk_separator_menu_item_new();
2227 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2229 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2230 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2232 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2233 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2234 "<QEMU>/View/Grab Input");
2235 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2237 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2239 separator
= gtk_separator_menu_item_new();
2240 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2243 for (vc
= 0;; vc
++) {
2244 con
= qemu_console_lookup_by_index(vc
);
2248 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2249 vc
, group
, view_menu
);
2253 #if defined(CONFIG_VTE)
2255 gd_vcs_init(s
, group
, view_menu
);
2258 separator
= gtk_separator_menu_item_new();
2259 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2261 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2262 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2264 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2265 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2270 static void gd_create_menus(GtkDisplayState
*s
)
2272 s
->accel_group
= gtk_accel_group_new();
2273 s
->machine_menu
= gd_create_menu_machine(s
);
2274 s
->view_menu
= gd_create_menu_view(s
);
2276 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2277 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2279 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2281 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2282 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2283 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2285 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2286 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2290 static gboolean gtkinit
;
2292 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
2296 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2298 GdkDisplay
*window_display
;
2301 fprintf(stderr
, "gtk initialization failed\n");
2305 #if !GTK_CHECK_VERSION(3, 0, 0)
2306 g_printerr("Running QEMU with GTK 2.x is deprecated, and will be removed\n"
2307 "in a future release. Please switch to GTK 3.x instead\n");
2310 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2311 #if GTK_CHECK_VERSION(3, 2, 0)
2312 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2314 s
->vbox
= gtk_vbox_new(FALSE
, 0);
2316 s
->notebook
= gtk_notebook_new();
2317 s
->menu_bar
= gtk_menu_bar_new();
2319 s
->free_scale
= FALSE
;
2321 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2322 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2323 * printable, but without changing any of the character classes to make
2324 * sure that we don't accidentally break implicit assumptions. */
2325 setlocale(LC_MESSAGES
, "");
2326 setlocale(LC_CTYPE
, "C.UTF-8");
2327 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2330 window_display
= gtk_widget_get_display(s
->window
);
2331 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2334 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2335 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2336 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2338 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
2340 GError
*error
= NULL
;
2341 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
2343 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
2345 g_error_free(error
);
2352 gd_connect_signals(s
);
2354 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2355 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2357 gd_update_caption(s
);
2359 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2360 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2362 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2364 gtk_widget_show_all(s
->window
);
2366 #ifdef VTE_RESIZE_HACK
2368 VirtualConsole
*cur
= gd_vc_find_current(s
);
2372 for (i
= 0; i
< s
->nb_vcs
; i
++) {
2373 VirtualConsole
*vc
= &s
->vc
[i
];
2374 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
2375 gtk_widget_hide(vc
->vte
.terminal
);
2378 gd_update_windowsize(cur
);
2383 vc
= gd_vc_find_current(s
);
2384 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2386 gtk_widget_set_sensitive(s
->copy_item
,
2387 vc
&& vc
->type
== GD_VC_VTE
);
2391 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2393 if (grab_on_hover
) {
2394 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2398 void early_gtk_display_init(int opengl
)
2400 /* The QEMU code relies on the assumption that it's always run in
2401 * the C locale. Therefore it is not prepared to deal with
2402 * operations that produce different results depending on the
2403 * locale, such as printf's formatting of decimal numbers, and
2406 * Since GTK+ calls setlocale() by default -importing the locale
2407 * settings from the environment- we must prevent it from doing so
2408 * using gtk_disable_setlocale().
2410 * QEMU's GTK+ UI, however, _does_ have translations for some of
2411 * the menu items. As a trade-off between a functionally correct
2412 * QEMU and a fully internationalized UI we support importing
2413 * LC_MESSAGES from the environment (see the setlocale() call
2414 * earlier in this file). This allows us to display translated
2415 * messages leaving everything else untouched.
2417 gtk_disable_setlocale();
2418 gtkinit
= gtk_init_check(NULL
, NULL
);
2420 /* don't exit yet, that'll break -help */
2425 case -1: /* default */
2429 #if defined(CONFIG_OPENGL)
2430 #if defined(CONFIG_GTK_GL)
2438 g_assert_not_reached();
2442 keycode_map
= gd_get_keymap(&keycode_maplen
);
2444 #if defined(CONFIG_VTE)
2445 type_register(&char_gd_vc_type_info
);