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"
57 #include "chardev/char.h"
58 #include "qom/object.h"
61 #define VC_WINDOW_X_MIN 320
62 #define VC_WINDOW_Y_MIN 240
63 #define VC_TERM_X_MIN 80
64 #define VC_TERM_Y_MIN 25
65 #define VC_SCALE_MIN 0.25
66 #define VC_SCALE_STEP 0.25
68 #if !defined(CONFIG_VTE)
69 # define VTE_CHECK_VERSION(a, b, c) 0
72 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
74 * The gtk2 vte terminal widget seriously messes up the window resize
75 * for some reason. You basically can't make the qemu window smaller
76 * any more because the toplevel window geoemtry hints are overridden.
78 * Workaround that by hiding all vte widgets, except the one in the
81 * Luckily everything works smooth in gtk3.
83 # define VTE_RESIZE_HACK 1
86 #if !GTK_CHECK_VERSION(2, 20, 0)
87 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
90 #ifndef GDK_IS_X11_DISPLAY
91 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
93 #ifndef GDK_IS_WAYLAND_DISPLAY
94 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy)
96 #ifndef GDK_IS_WIN32_DISPLAY
97 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
100 #if !GTK_CHECK_VERSION(2, 22, 0)
101 #define GDK_KEY_0 GDK_0
102 #define GDK_KEY_1 GDK_1
103 #define GDK_KEY_2 GDK_2
104 #define GDK_KEY_f GDK_f
105 #define GDK_KEY_g GDK_g
106 #define GDK_KEY_q GDK_q
107 #define GDK_KEY_plus GDK_plus
108 #define GDK_KEY_equal GDK_equal
109 #define GDK_KEY_minus GDK_minus
110 #define GDK_KEY_Pause GDK_Pause
111 #define GDK_KEY_Delete GDK_Delete
114 /* Some older mingw versions lack this constant or have
115 * it conditionally defined */
117 # ifndef MAPVK_VK_TO_VSC
118 # define MAPVK_VK_TO_VSC 0
123 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
125 static const int modifier_keycode
[] = {
126 /* shift, control, alt keys, meta keys, both left & right */
127 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
130 struct GtkDisplayState
{
135 GtkAccelGroup
*accel_group
;
137 GtkWidget
*machine_menu_item
;
138 GtkWidget
*machine_menu
;
139 GtkWidget
*pause_item
;
140 GtkWidget
*reset_item
;
141 GtkWidget
*powerdown_item
;
142 GtkWidget
*quit_item
;
144 GtkWidget
*view_menu_item
;
145 GtkWidget
*view_menu
;
146 GtkWidget
*full_screen_item
;
147 GtkWidget
*copy_item
;
148 GtkWidget
*zoom_in_item
;
149 GtkWidget
*zoom_out_item
;
150 GtkWidget
*zoom_fixed_item
;
151 GtkWidget
*zoom_fit_item
;
152 GtkWidget
*grab_item
;
153 GtkWidget
*grab_on_hover_item
;
156 VirtualConsole vc
[MAX_VCS
];
158 GtkWidget
*show_tabs_item
;
159 GtkWidget
*untabify_item
;
169 VirtualConsole
*kbd_owner
;
170 VirtualConsole
*ptr_owner
;
172 gboolean full_screen
;
174 GdkCursor
*null_cursor
;
175 Notifier mouse_mode_notifier
;
178 bool external_pause_update
;
180 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
185 typedef struct VCChardev
{
187 VirtualConsole
*console
;
191 #define TYPE_CHARDEV_VC "chardev-vc"
192 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
194 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
195 static void gd_ungrab_pointer(GtkDisplayState
*s
);
196 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
197 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
199 /** Utility Functions **/
201 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
206 for (i
= 0; i
< s
->nb_vcs
; i
++) {
208 if (gtk_check_menu_item_get_active
209 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
216 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
221 for (i
= 0; i
< s
->nb_vcs
; i
++) {
223 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
231 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
235 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
236 return gd_vc_find_by_page(s
, page
);
239 static bool gd_is_grab_active(GtkDisplayState
*s
)
241 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
244 static bool gd_grab_on_hover(GtkDisplayState
*s
)
246 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
249 static void gd_update_cursor(VirtualConsole
*vc
)
251 GtkDisplayState
*s
= vc
->s
;
254 if (vc
->type
!= GD_VC_GFX
||
255 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
259 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
263 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
264 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
265 gdk_window_set_cursor(window
, s
->null_cursor
);
267 gdk_window_set_cursor(window
, NULL
);
271 static void gd_update_caption(GtkDisplayState
*s
)
273 const char *status
= "";
276 const char *grab
= "";
277 bool is_paused
= !runstate_is_running();
281 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
283 prefix
= g_strdup_printf("QEMU");
286 if (s
->ptr_owner
!= NULL
&&
287 s
->ptr_owner
->window
== NULL
) {
288 grab
= _(" - Press Ctrl+Alt+G to release grab");
292 status
= _(" [Paused]");
294 s
->external_pause_update
= true;
295 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
297 s
->external_pause_update
= false;
299 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
300 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
303 for (i
= 0; i
< s
->nb_vcs
; i
++) {
304 VirtualConsole
*vc
= &s
->vc
[i
];
309 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
310 vc
== s
->kbd_owner
? " +kbd" : "",
311 vc
== s
->ptr_owner
? " +ptr" : "");
312 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
319 static void gd_update_geometry_hints(VirtualConsole
*vc
)
321 GtkDisplayState
*s
= vc
->s
;
322 GdkWindowHints mask
= 0;
323 GdkGeometry geo
= {};
324 GtkWidget
*geo_widget
= NULL
;
325 GtkWindow
*geo_window
;
327 if (vc
->type
== GD_VC_GFX
) {
332 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
333 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
334 mask
|= GDK_HINT_MIN_SIZE
;
336 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
337 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
338 mask
|= GDK_HINT_MIN_SIZE
;
340 geo_widget
= vc
->gfx
.drawing_area
;
341 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
343 #if defined(CONFIG_VTE)
344 } else if (vc
->type
== GD_VC_VTE
) {
345 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
346 GtkBorder padding
= { 0 };
348 #if VTE_CHECK_VERSION(0, 37, 0)
349 gtk_style_context_get_padding(
350 gtk_widget_get_style_context(vc
->vte
.terminal
),
351 gtk_widget_get_state_flags(vc
->vte
.terminal
),
355 GtkBorder
*ib
= NULL
;
356 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
364 geo
.width_inc
= vte_terminal_get_char_width(term
);
365 geo
.height_inc
= vte_terminal_get_char_height(term
);
366 mask
|= GDK_HINT_RESIZE_INC
;
367 geo
.base_width
= geo
.width_inc
;
368 geo
.base_height
= geo
.height_inc
;
369 mask
|= GDK_HINT_BASE_SIZE
;
370 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
371 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
372 mask
|= GDK_HINT_MIN_SIZE
;
374 geo
.base_width
+= padding
.left
+ padding
.right
;
375 geo
.base_height
+= padding
.top
+ padding
.bottom
;
376 geo
.min_width
+= padding
.left
+ padding
.right
;
377 geo
.min_height
+= padding
.top
+ padding
.bottom
;
378 geo_widget
= vc
->vte
.terminal
;
382 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
383 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
386 void gd_update_windowsize(VirtualConsole
*vc
)
388 GtkDisplayState
*s
= vc
->s
;
390 gd_update_geometry_hints(vc
);
392 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
393 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
394 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
398 static void gd_update_full_redraw(VirtualConsole
*vc
)
400 GtkWidget
*area
= vc
->gfx
.drawing_area
;
402 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
403 #if defined(CONFIG_GTK_GL)
405 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
409 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
412 static void gtk_release_modifiers(GtkDisplayState
*s
)
414 VirtualConsole
*vc
= gd_vc_find_current(s
);
417 if (vc
->type
!= GD_VC_GFX
||
418 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
421 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
422 keycode
= modifier_keycode
[i
];
423 if (!s
->modifier_pressed
[i
]) {
426 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, keycode
, false);
427 s
->modifier_pressed
[i
] = false;
431 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
434 g_object_ref(G_OBJECT(widget
));
435 gtk_container_remove(GTK_CONTAINER(from
), widget
);
436 gtk_container_add(GTK_CONTAINER(to
), widget
);
437 g_object_unref(G_OBJECT(widget
));
440 /** DisplayState Callbacks **/
442 static void gd_update(DisplayChangeListener
*dcl
,
443 int x
, int y
, int w
, int h
)
445 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
452 trace_gd_update(vc
->label
, x
, y
, w
, h
);
454 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
458 if (vc
->gfx
.convert
) {
459 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
460 NULL
, vc
->gfx
.convert
,
461 x
, y
, 0, 0, x
, y
, w
, h
);
464 x1
= floor(x
* vc
->gfx
.scale_x
);
465 y1
= floor(y
* vc
->gfx
.scale_y
);
467 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
468 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
470 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
471 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
473 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
477 gdk_drawable_get_size(win
, &ww
, &wh
);
487 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
488 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
491 static void gd_refresh(DisplayChangeListener
*dcl
)
493 graphic_hw_update(dcl
->con
);
496 #if GTK_CHECK_VERSION(3, 0, 0)
497 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
499 #if GTK_CHECK_VERSION(3, 20, 0)
500 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
502 return gdk_device_manager_get_client_pointer(
503 gdk_display_get_device_manager(dpy
));
507 static void gd_mouse_set(DisplayChangeListener
*dcl
,
508 int x
, int y
, int visible
)
510 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
514 if (qemu_input_is_absolute()) {
518 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
519 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
520 x
, y
, &x_root
, &y_root
);
521 gdk_device_warp(gd_get_pointer(dpy
),
522 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
528 static void gd_mouse_set(DisplayChangeListener
*dcl
,
529 int x
, int y
, int visible
)
531 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
534 if (qemu_input_is_absolute()) {
538 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
539 x
, y
, &x_root
, &y_root
);
540 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
541 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
546 static void gd_cursor_define(DisplayChangeListener
*dcl
,
549 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
553 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
557 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
558 GDK_COLORSPACE_RGB
, true, 8,
559 c
->width
, c
->height
, c
->width
* 4,
561 cursor
= gdk_cursor_new_from_pixbuf
562 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
563 pixbuf
, c
->hot_x
, c
->hot_y
);
564 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
565 g_object_unref(pixbuf
);
566 #if !GTK_CHECK_VERSION(3, 0, 0)
567 gdk_cursor_unref(cursor
);
569 g_object_unref(cursor
);
573 static void gd_switch(DisplayChangeListener
*dcl
,
574 DisplaySurface
*surface
)
576 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
579 trace_gd_switch(vc
->label
,
580 surface
? surface_width(surface
) : 0,
581 surface
? surface_height(surface
) : 0);
583 if (vc
->gfx
.surface
) {
584 cairo_surface_destroy(vc
->gfx
.surface
);
585 vc
->gfx
.surface
= NULL
;
587 if (vc
->gfx
.convert
) {
588 pixman_image_unref(vc
->gfx
.convert
);
589 vc
->gfx
.convert
= NULL
;
592 if (vc
->gfx
.ds
&& surface
&&
593 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
594 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
597 vc
->gfx
.ds
= surface
;
603 if (surface
->format
== PIXMAN_x8r8g8b8
) {
605 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
607 * No need to convert, use surface directly. Should be the
608 * common case as this is qemu_default_pixelformat(32) too.
610 vc
->gfx
.surface
= cairo_image_surface_create_for_data
611 (surface_data(surface
),
613 surface_width(surface
),
614 surface_height(surface
),
615 surface_stride(surface
));
617 /* Must convert surface, use pixman to do it. */
618 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
619 surface_width(surface
),
620 surface_height(surface
),
622 vc
->gfx
.surface
= cairo_image_surface_create_for_data
623 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
625 pixman_image_get_width(vc
->gfx
.convert
),
626 pixman_image_get_height(vc
->gfx
.convert
),
627 pixman_image_get_stride(vc
->gfx
.convert
));
628 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
629 NULL
, vc
->gfx
.convert
,
631 pixman_image_get_width(vc
->gfx
.convert
),
632 pixman_image_get_height(vc
->gfx
.convert
));
636 gd_update_windowsize(vc
);
638 gd_update_full_redraw(vc
);
642 static const DisplayChangeListenerOps dcl_ops
= {
644 .dpy_gfx_update
= gd_update
,
645 .dpy_gfx_switch
= gd_switch
,
646 .dpy_gfx_check_format
= qemu_pixman_check_format
,
647 .dpy_refresh
= gd_refresh
,
648 .dpy_mouse_set
= gd_mouse_set
,
649 .dpy_cursor_define
= gd_cursor_define
,
653 #if defined(CONFIG_OPENGL)
655 /** DisplayState Callbacks (opengl version) **/
657 #if defined(CONFIG_GTK_GL)
659 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
660 .dpy_name
= "gtk-egl",
661 .dpy_gfx_update
= gd_gl_area_update
,
662 .dpy_gfx_switch
= gd_gl_area_switch
,
663 .dpy_gfx_check_format
= console_gl_check_format
,
664 .dpy_refresh
= gd_gl_area_refresh
,
665 .dpy_mouse_set
= gd_mouse_set
,
666 .dpy_cursor_define
= gd_cursor_define
,
668 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
669 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
670 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
671 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
672 .dpy_gl_scanout_texture
= gd_gl_area_scanout_texture
,
673 .dpy_gl_update
= gd_gl_area_scanout_flush
,
678 static const DisplayChangeListenerOps dcl_egl_ops
= {
679 .dpy_name
= "gtk-egl",
680 .dpy_gfx_update
= gd_egl_update
,
681 .dpy_gfx_switch
= gd_egl_switch
,
682 .dpy_gfx_check_format
= console_gl_check_format
,
683 .dpy_refresh
= gd_egl_refresh
,
684 .dpy_mouse_set
= gd_mouse_set
,
685 .dpy_cursor_define
= gd_cursor_define
,
687 .dpy_gl_ctx_create
= gd_egl_create_context
,
688 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
689 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
690 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
691 .dpy_gl_scanout_disable
= gd_egl_scanout_disable
,
692 .dpy_gl_scanout_texture
= gd_egl_scanout_texture
,
693 .dpy_gl_update
= gd_egl_scanout_flush
,
696 #endif /* CONFIG_GTK_GL */
697 #endif /* CONFIG_OPENGL */
701 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
703 GtkDisplayState
*s
= opaque
;
705 gd_update_caption(s
);
708 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
713 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
714 /* release the grab at switching to absolute mode */
715 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
716 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
719 for (i
= 0; i
< s
->nb_vcs
; i
++) {
720 VirtualConsole
*vc
= &s
->vc
[i
];
721 gd_update_cursor(vc
);
727 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
730 GtkDisplayState
*s
= opaque
;
734 for (i
= 0; i
< s
->nb_vcs
; i
++) {
735 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
738 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
747 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
751 memset(&info
, 0, sizeof(info
));
753 info
.height
= height
;
754 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
757 #if defined(CONFIG_GTK_GL)
759 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
762 VirtualConsole
*vc
= opaque
;
770 static void gd_resize_event(GtkGLArea
*area
,
771 gint width
, gint height
, gpointer
*opaque
)
773 VirtualConsole
*vc
= (void *)opaque
;
775 gd_set_ui_info(vc
, width
, height
);
780 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
782 VirtualConsole
*vc
= opaque
;
783 GtkDisplayState
*s
= vc
->s
;
788 #if defined(CONFIG_OPENGL)
790 #if defined(CONFIG_GTK_GL)
791 /* invoke render callback please */
800 if (!gtk_widget_get_realized(widget
)) {
807 fbw
= surface_width(vc
->gfx
.ds
);
808 fbh
= surface_height(vc
->gfx
.ds
);
810 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
812 if (s
->full_screen
) {
813 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
814 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
815 } else if (s
->free_scale
) {
818 sx
= (double)ww
/ fbw
;
819 sy
= (double)wh
/ fbh
;
821 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
824 fbw
*= vc
->gfx
.scale_x
;
825 fbh
*= vc
->gfx
.scale_y
;
835 cairo_rectangle(cr
, 0, 0, ww
, wh
);
837 /* Optionally cut out the inner area where the pixmap
838 will be drawn. This avoids 'flashing' since we're
839 not double-buffering. Note we're using the undocumented
840 behaviour of drawing the rectangle from right to left
841 to cut out the whole */
842 cairo_rectangle(cr
, mx
+ fbw
, my
,
846 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
847 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
848 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
854 #if !GTK_CHECK_VERSION(3, 0, 0)
855 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
861 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
866 expose
->area
.height
);
869 ret
= gd_draw_event(widget
, cr
, opaque
);
877 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
880 VirtualConsole
*vc
= opaque
;
881 GtkDisplayState
*s
= vc
->s
;
891 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
892 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
894 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
905 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
906 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
908 if (qemu_input_is_absolute()) {
909 if (x
< 0 || y
< 0 ||
910 x
>= surface_width(vc
->gfx
.ds
) ||
911 y
>= surface_height(vc
->gfx
.ds
)) {
914 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
915 0, surface_width(vc
->gfx
.ds
));
916 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
917 0, surface_height(vc
->gfx
.ds
));
918 qemu_input_event_sync();
919 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
920 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
921 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
922 qemu_input_event_sync();
928 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
929 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
930 int screen_width
, screen_height
;
932 int x
= (int)motion
->x_root
;
933 int y
= (int)motion
->y_root
;
935 #if GTK_CHECK_VERSION(3, 22, 0)
937 GdkDisplay
*dpy
= gtk_widget_get_display(widget
);
938 GdkWindow
*win
= gtk_widget_get_window(widget
);
939 GdkMonitor
*monitor
= gdk_display_get_monitor_at_window(dpy
, win
);
940 GdkRectangle geometry
;
941 gdk_monitor_get_geometry(monitor
, &geometry
);
942 screen_width
= geometry
.width
;
943 screen_height
= geometry
.height
;
947 screen_width
= gdk_screen_get_width(screen
);
948 screen_height
= gdk_screen_get_height(screen
);
952 /* In relative mode check to see if client pointer hit
953 * one of the screen edges, and if so move it back by
954 * 200 pixels. This is important because the pointer
955 * in the server doesn't correspond 1-for-1, and so
956 * may still be only half way across the screen. Without
957 * this warp, the server pointer would thus appear to hit
958 * an invisible wall */
965 if (x
== (screen_width
- 1)) {
968 if (y
== (screen_height
- 1)) {
972 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
973 #if GTK_CHECK_VERSION(3, 0, 0)
974 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
975 gdk_device_warp(dev
, screen
, x
, y
);
977 GdkDisplay
*display
= gtk_widget_get_display(widget
);
978 gdk_display_warp_pointer(display
, screen
, x
, y
);
987 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
990 VirtualConsole
*vc
= opaque
;
991 GtkDisplayState
*s
= vc
->s
;
994 /* implicitly grab the input at the first click in the relative mode */
995 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
996 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
998 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1001 gd_grab_pointer(vc
, "relative-mode-click");
1006 if (button
->button
== 1) {
1007 btn
= INPUT_BUTTON_LEFT
;
1008 } else if (button
->button
== 2) {
1009 btn
= INPUT_BUTTON_MIDDLE
;
1010 } else if (button
->button
== 3) {
1011 btn
= INPUT_BUTTON_RIGHT
;
1012 } else if (button
->button
== 8) {
1013 btn
= INPUT_BUTTON_SIDE
;
1014 } else if (button
->button
== 9) {
1015 btn
= INPUT_BUTTON_EXTRA
;
1020 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
1021 button
->type
== GDK_BUTTON_PRESS
);
1022 qemu_input_event_sync();
1026 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
1029 VirtualConsole
*vc
= opaque
;
1032 if (scroll
->direction
== GDK_SCROLL_UP
) {
1033 btn
= INPUT_BUTTON_WHEEL_UP
;
1034 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
1035 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1036 #if GTK_CHECK_VERSION(3, 4, 0)
1037 } else if (scroll
->direction
== GDK_SCROLL_SMOOTH
) {
1038 gdouble delta_x
, delta_y
;
1039 if (!gdk_event_get_scroll_deltas((GdkEvent
*)scroll
,
1040 &delta_x
, &delta_y
)) {
1044 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1046 btn
= INPUT_BUTTON_WHEEL_UP
;
1053 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
1054 qemu_input_event_sync();
1055 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
1056 qemu_input_event_sync();
1060 static int gd_map_keycode(GtkDisplayState
*s
, GdkDisplay
*dpy
, int gdk_keycode
)
1064 #ifdef GDK_WINDOWING_WIN32
1065 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1066 qemu_keycode
= MapVirtualKey(gdk_keycode
, MAPVK_VK_TO_VSC
);
1067 switch (qemu_keycode
) {
1068 case 103: /* alt gr */
1069 qemu_keycode
= 56 | SCANCODE_GREY
;
1072 return qemu_keycode
;
1076 if (gdk_keycode
< 9) {
1078 } else if (gdk_keycode
< 97) {
1079 qemu_keycode
= gdk_keycode
- 8;
1080 #ifdef GDK_WINDOWING_X11
1081 } else if (GDK_IS_X11_DISPLAY(dpy
) && gdk_keycode
< 158) {
1083 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
1085 qemu_keycode
= translate_xfree86_keycode(gdk_keycode
- 97);
1088 #ifdef GDK_WINDOWING_WAYLAND
1089 } else if (GDK_IS_WAYLAND_DISPLAY(dpy
) && gdk_keycode
< 158) {
1090 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
1092 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
1093 qemu_keycode
= 0x70;
1094 } else if (gdk_keycode
== 211) { /* backslash */
1095 qemu_keycode
= 0x73;
1100 return qemu_keycode
;
1103 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1104 GdkEventKey
*key
, void *opaque
)
1106 VirtualConsole
*vc
= opaque
;
1107 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1109 if (key
->keyval
== GDK_KEY_Delete
) {
1110 kbd_put_qcode_console(con
, Q_KEY_CODE_DELETE
);
1111 } else if (key
->length
) {
1112 kbd_put_string_console(con
, key
->string
, key
->length
);
1114 int num
= gd_map_keycode(vc
->s
, gtk_widget_get_display(widget
),
1115 key
->hardware_keycode
);
1116 int qcode
= qemu_input_key_number_to_qcode(num
);
1117 kbd_put_qcode_console(con
, qcode
);
1122 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1124 VirtualConsole
*vc
= opaque
;
1125 GtkDisplayState
*s
= vc
->s
;
1126 int gdk_keycode
= key
->hardware_keycode
;
1130 if (s
->ignore_keys
) {
1131 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1135 if (key
->keyval
== GDK_KEY_Pause
) {
1136 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
1137 key
->type
== GDK_KEY_PRESS
);
1141 qemu_keycode
= gd_map_keycode(s
, gtk_widget_get_display(widget
),
1144 trace_gd_key_event(vc
->label
, gdk_keycode
, qemu_keycode
,
1145 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1147 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
1148 if (qemu_keycode
== modifier_keycode
[i
]) {
1149 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
1153 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, qemu_keycode
,
1154 key
->type
== GDK_KEY_PRESS
);
1159 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1161 if (event
->type
== GDK_MOTION_NOTIFY
) {
1162 return gd_motion_event(widget
, &event
->motion
, opaque
);
1167 /** Window Menu Actions **/
1169 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1171 GtkDisplayState
*s
= opaque
;
1173 if (s
->external_pause_update
) {
1176 if (runstate_is_running()) {
1183 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1185 qmp_system_reset(NULL
);
1188 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1190 qmp_system_powerdown(NULL
);
1193 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1198 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1200 GtkDisplayState
*s
= opaque
;
1201 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1202 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1205 gtk_release_modifiers(s
);
1207 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1208 gtk_notebook_set_current_page(nb
, page
);
1209 gtk_widget_grab_focus(vc
->focus
);
1211 s
->ignore_keys
= false;
1214 static void gd_accel_switch_vc(void *opaque
)
1216 VirtualConsole
*vc
= opaque
;
1218 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1219 #if !GTK_CHECK_VERSION(3, 0, 0)
1220 /* GTK2 sends the accel key to the target console - ignore this until */
1221 vc
->s
->ignore_keys
= true;
1225 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1227 GtkDisplayState
*s
= opaque
;
1228 VirtualConsole
*vc
= gd_vc_find_current(s
);
1230 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1231 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1233 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1235 gd_update_windowsize(vc
);
1238 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1241 VirtualConsole
*vc
= opaque
;
1242 GtkDisplayState
*s
= vc
->s
;
1244 gtk_widget_set_sensitive(vc
->menu_item
, true);
1245 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1246 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1247 vc
->tab_item
, vc
->label
);
1248 gtk_widget_destroy(vc
->window
);
1253 static gboolean
gd_win_grab(void *opaque
)
1255 VirtualConsole
*vc
= opaque
;
1257 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1258 if (vc
->s
->ptr_owner
) {
1259 gd_ungrab_pointer(vc
->s
);
1261 gd_grab_pointer(vc
, "user-request-detached-tab");
1266 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1268 GtkDisplayState
*s
= opaque
;
1269 VirtualConsole
*vc
= gd_vc_find_current(s
);
1271 if (vc
->type
== GD_VC_GFX
&&
1272 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1273 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1277 gtk_widget_set_sensitive(vc
->menu_item
, false);
1278 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1279 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1281 g_signal_connect(vc
->window
, "delete-event",
1282 G_CALLBACK(gd_tab_window_close
), vc
);
1283 gtk_widget_show_all(vc
->window
);
1285 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1286 GtkAccelGroup
*ag
= gtk_accel_group_new();
1287 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1289 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1291 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1294 gd_update_geometry_hints(vc
);
1295 gd_update_caption(s
);
1299 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1301 GtkDisplayState
*s
= opaque
;
1302 VirtualConsole
*vc
= gd_vc_find_current(s
);
1304 if (!s
->full_screen
) {
1305 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1306 gtk_widget_hide(s
->menu_bar
);
1307 if (vc
->type
== GD_VC_GFX
) {
1308 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1310 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1311 s
->full_screen
= TRUE
;
1313 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1314 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1315 gtk_widget_show(s
->menu_bar
);
1316 s
->full_screen
= FALSE
;
1317 if (vc
->type
== GD_VC_GFX
) {
1318 vc
->gfx
.scale_x
= 1.0;
1319 vc
->gfx
.scale_y
= 1.0;
1320 gd_update_windowsize(vc
);
1324 gd_update_cursor(vc
);
1327 static void gd_accel_full_screen(void *opaque
)
1329 GtkDisplayState
*s
= opaque
;
1330 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1333 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1335 GtkDisplayState
*s
= opaque
;
1336 VirtualConsole
*vc
= gd_vc_find_current(s
);
1338 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1341 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1342 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1344 gd_update_windowsize(vc
);
1347 static void gd_accel_zoom_in(void *opaque
)
1349 GtkDisplayState
*s
= opaque
;
1350 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_in_item
));
1353 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1355 GtkDisplayState
*s
= opaque
;
1356 VirtualConsole
*vc
= gd_vc_find_current(s
);
1358 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1361 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1362 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1364 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1365 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1367 gd_update_windowsize(vc
);
1370 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1372 GtkDisplayState
*s
= opaque
;
1373 VirtualConsole
*vc
= gd_vc_find_current(s
);
1375 vc
->gfx
.scale_x
= 1.0;
1376 vc
->gfx
.scale_y
= 1.0;
1378 gd_update_windowsize(vc
);
1381 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1383 GtkDisplayState
*s
= opaque
;
1384 VirtualConsole
*vc
= gd_vc_find_current(s
);
1386 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1387 s
->free_scale
= TRUE
;
1389 s
->free_scale
= FALSE
;
1390 vc
->gfx
.scale_x
= 1.0;
1391 vc
->gfx
.scale_y
= 1.0;
1394 gd_update_windowsize(vc
);
1395 gd_update_full_redraw(vc
);
1398 #if GTK_CHECK_VERSION(3, 20, 0)
1399 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1401 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1402 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1403 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1404 GdkSeatCapabilities caps
= 0;
1405 GdkCursor
*cursor
= NULL
;
1408 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1411 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1412 cursor
= vc
->s
->null_cursor
;
1416 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1419 gdk_seat_ungrab(seat
);
1422 #elif GTK_CHECK_VERSION(3, 0, 0)
1423 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1424 GdkInputSource source
, GdkEventMask mask
,
1427 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1428 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1429 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1432 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1433 GdkDevice
*dev
= tmp
->data
;
1434 if (gdk_device_get_source(dev
) != source
) {
1438 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1439 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1440 mask
, cursor
, GDK_CURRENT_TIME
);
1442 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1449 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1451 if (vc
->s
->kbd_owner
) {
1452 if (vc
->s
->kbd_owner
== vc
) {
1455 gd_ungrab_keyboard(vc
->s
);
1459 #if GTK_CHECK_VERSION(3, 20, 0)
1460 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1461 #elif GTK_CHECK_VERSION(3, 0, 0)
1462 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1463 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1466 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1470 vc
->s
->kbd_owner
= vc
;
1471 gd_update_caption(vc
->s
);
1472 trace_gd_grab(vc
->label
, "kbd", reason
);
1475 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1477 VirtualConsole
*vc
= s
->kbd_owner
;
1482 s
->kbd_owner
= NULL
;
1484 #if GTK_CHECK_VERSION(3, 20, 0)
1485 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1486 #elif GTK_CHECK_VERSION(3, 0, 0)
1487 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1489 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1491 gd_update_caption(s
);
1492 trace_gd_ungrab(vc
->label
, "kbd");
1495 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1497 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1499 if (vc
->s
->ptr_owner
) {
1500 if (vc
->s
->ptr_owner
== vc
) {
1503 gd_ungrab_pointer(vc
->s
);
1507 #if GTK_CHECK_VERSION(3, 20, 0)
1508 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1509 gdk_device_get_position(gd_get_pointer(display
),
1510 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1511 #elif GTK_CHECK_VERSION(3, 0, 0)
1512 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1513 GDK_POINTER_MOTION_MASK
|
1514 GDK_BUTTON_PRESS_MASK
|
1515 GDK_BUTTON_RELEASE_MASK
|
1516 GDK_BUTTON_MOTION_MASK
|
1518 vc
->s
->null_cursor
);
1519 gdk_device_get_position(gd_get_pointer(display
),
1520 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1522 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1523 FALSE
, /* All events to come to our window directly */
1524 GDK_POINTER_MOTION_MASK
|
1525 GDK_BUTTON_PRESS_MASK
|
1526 GDK_BUTTON_RELEASE_MASK
|
1527 GDK_BUTTON_MOTION_MASK
|
1529 NULL
, /* Allow cursor to move over entire desktop */
1532 gdk_display_get_pointer(display
, NULL
,
1533 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1535 vc
->s
->ptr_owner
= vc
;
1536 gd_update_caption(vc
->s
);
1537 trace_gd_grab(vc
->label
, "ptr", reason
);
1540 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1542 VirtualConsole
*vc
= s
->ptr_owner
;
1543 GdkDisplay
*display
;
1548 s
->ptr_owner
= NULL
;
1550 display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1551 #if GTK_CHECK_VERSION(3, 20, 0)
1552 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1553 gdk_device_warp(gd_get_pointer(display
),
1554 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1555 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1556 #elif GTK_CHECK_VERSION(3, 0, 0)
1557 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1558 gdk_device_warp(gd_get_pointer(display
),
1559 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1560 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1562 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1563 gdk_display_warp_pointer(display
,
1564 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1565 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1567 gd_update_caption(s
);
1568 trace_gd_ungrab(vc
->label
, "ptr");
1571 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1573 GtkDisplayState
*s
= opaque
;
1574 VirtualConsole
*vc
= gd_vc_find_current(s
);
1576 if (gd_is_grab_active(s
)) {
1577 gd_grab_keyboard(vc
, "user-request-main-window");
1578 gd_grab_pointer(vc
, "user-request-main-window");
1580 gd_ungrab_keyboard(s
);
1581 gd_ungrab_pointer(s
);
1584 gd_update_cursor(vc
);
1587 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1590 GtkDisplayState
*s
= data
;
1594 if (!gtk_widget_get_realized(s
->notebook
)) {
1598 #ifdef VTE_RESIZE_HACK
1599 vc
= gd_vc_find_current(s
);
1600 if (vc
&& vc
->type
== GD_VC_VTE
) {
1601 gtk_widget_hide(vc
->vte
.terminal
);
1604 vc
= gd_vc_find_by_page(s
, arg2
);
1608 #ifdef VTE_RESIZE_HACK
1609 if (vc
->type
== GD_VC_VTE
) {
1610 gtk_widget_show(vc
->vte
.terminal
);
1613 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1615 on_vga
= (vc
->type
== GD_VC_GFX
&&
1616 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1618 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1620 } else if (s
->full_screen
) {
1621 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1624 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1626 gtk_widget_set_sensitive(s
->copy_item
, vc
->type
== GD_VC_VTE
);
1629 gd_update_windowsize(vc
);
1630 gd_update_cursor(vc
);
1633 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1636 VirtualConsole
*vc
= opaque
;
1637 GtkDisplayState
*s
= vc
->s
;
1639 if (gd_grab_on_hover(s
)) {
1640 gd_grab_keyboard(vc
, "grab-on-hover");
1645 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1648 VirtualConsole
*vc
= opaque
;
1649 GtkDisplayState
*s
= vc
->s
;
1651 if (gd_grab_on_hover(s
)) {
1652 gd_ungrab_keyboard(s
);
1657 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1658 GdkEventCrossing
*crossing
, gpointer opaque
)
1660 VirtualConsole
*vc
= opaque
;
1661 GtkDisplayState
*s
= vc
->s
;
1663 gtk_release_modifiers(s
);
1667 static gboolean
gd_configure(GtkWidget
*widget
,
1668 GdkEventConfigure
*cfg
, gpointer opaque
)
1670 VirtualConsole
*vc
= opaque
;
1672 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1676 /** Virtual Console Callbacks **/
1678 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1679 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1681 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1682 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1683 HOTKEY_MODIFIERS
, 0,
1684 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1685 #if GTK_CHECK_VERSION(3, 8, 0)
1686 gtk_accel_label_set_accel(
1687 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1688 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1691 g_signal_connect(vc
->menu_item
, "activate",
1692 G_CALLBACK(gd_menu_switch_vc
), s
);
1693 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1695 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1699 #if defined(CONFIG_VTE)
1700 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1702 GtkDisplayState
*s
= opaque
;
1703 VirtualConsole
*vc
= gd_vc_find_current(s
);
1705 #if VTE_CHECK_VERSION(0, 50, 0)
1706 vte_terminal_copy_clipboard_format(VTE_TERMINAL(vc
->vte
.terminal
),
1709 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1713 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1715 VirtualConsole
*vc
= opaque
;
1717 if (gtk_adjustment_get_upper(adjustment
) >
1718 gtk_adjustment_get_page_size(adjustment
)) {
1719 gtk_widget_show(vc
->vte
.scrollbar
);
1721 gtk_widget_hide(vc
->vte
.scrollbar
);
1725 static int gd_vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1727 VCChardev
*vcd
= VC_CHARDEV(chr
);
1728 VirtualConsole
*vc
= vcd
->console
;
1730 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1734 static void gd_vc_chr_set_echo(Chardev
*chr
, bool echo
)
1736 VCChardev
*vcd
= VC_CHARDEV(chr
);
1737 VirtualConsole
*vc
= vcd
->console
;
1740 vc
->vte
.echo
= echo
;
1747 static Chardev
*vcs
[MAX_VCS
];
1748 static void gd_vc_open(Chardev
*chr
,
1749 ChardevBackend
*backend
,
1753 if (nb_vcs
== MAX_VCS
) {
1754 error_setg(errp
, "Maximum number of consoles reached");
1758 vcs
[nb_vcs
++] = chr
;
1760 /* console/chardev init sometimes completes elsewhere in a 2nd
1761 * stage, so defer OPENED events until they are fully initialized
1766 static void char_gd_vc_class_init(ObjectClass
*oc
, void *data
)
1768 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1770 cc
->parse
= qemu_chr_parse_vc
;
1771 cc
->open
= gd_vc_open
;
1772 cc
->chr_write
= gd_vc_chr_write
;
1773 cc
->chr_set_echo
= gd_vc_chr_set_echo
;
1776 static const TypeInfo char_gd_vc_type_info
= {
1777 .name
= TYPE_CHARDEV_VC
,
1778 .parent
= TYPE_CHARDEV
,
1779 .instance_size
= sizeof(VCChardev
),
1780 .class_init
= char_gd_vc_class_init
,
1783 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1786 VirtualConsole
*vc
= user_data
;
1789 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1791 for (i
= 0; i
< size
; i
++) {
1792 uint8_t c
= text
[i
];
1793 if (c
>= 128 || isprint(c
)) {
1794 /* 8-bit characters are considered printable. */
1795 vte_terminal_feed(term
, &text
[i
], 1);
1796 } else if (c
== '\r' || c
== '\n') {
1797 vte_terminal_feed(term
, "\r\n", 2);
1799 char ctrl
[2] = { '^', 0};
1800 ctrl
[1] = text
[i
] ^ 64;
1801 vte_terminal_feed(term
, ctrl
, 2);
1806 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1810 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1811 Chardev
*chr
, int idx
,
1812 GSList
*group
, GtkWidget
*view_menu
)
1816 GtkWidget
*scrollbar
;
1817 GtkAdjustment
*vadjustment
;
1818 VCChardev
*vcd
= VC_CHARDEV(chr
);
1821 vc
->vte
.echo
= vcd
->echo
;
1825 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1826 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1827 ? vc
->vte
.chr
->label
: buffer
);
1828 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1830 vc
->vte
.terminal
= vte_terminal_new();
1831 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1833 /* The documentation says that the default is UTF-8, but actually it is
1834 * 7-bit ASCII at least in VTE 0.38.
1836 #if VTE_CHECK_VERSION(0, 38, 0)
1837 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1839 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1842 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1843 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1844 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1846 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1847 vadjustment
= gtk_scrollable_get_vadjustment
1848 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1850 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1853 #if GTK_CHECK_VERSION(3, 0, 0)
1854 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1855 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1857 box
= gtk_hbox_new(false, 2);
1858 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1861 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1862 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1865 vc
->vte
.scrollbar
= scrollbar
;
1867 g_signal_connect(vadjustment
, "changed",
1868 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1870 vc
->type
= GD_VC_VTE
;
1872 vc
->focus
= vc
->vte
.terminal
;
1873 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1874 gtk_label_new(vc
->label
));
1876 qemu_chr_be_event(vc
->vte
.chr
, CHR_EVENT_OPENED
);
1881 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1882 GtkWidget
*view_menu
)
1886 for (i
= 0; i
< nb_vcs
; i
++) {
1887 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1888 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1892 #endif /* CONFIG_VTE */
1894 /** Window Creation **/
1896 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1898 #if GTK_CHECK_VERSION(3, 0, 0)
1899 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1900 G_CALLBACK(gd_draw_event
), vc
);
1901 #if defined(CONFIG_GTK_GL)
1902 if (display_opengl
) {
1903 /* wire up GtkGlArea events */
1904 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1905 G_CALLBACK(gd_render_event
), vc
);
1906 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1907 G_CALLBACK(gd_resize_event
), vc
);
1911 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1912 G_CALLBACK(gd_expose_event
), vc
);
1914 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1915 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1916 G_CALLBACK(gd_event
), vc
);
1917 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1918 G_CALLBACK(gd_button_event
), vc
);
1919 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1920 G_CALLBACK(gd_button_event
), vc
);
1921 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1922 G_CALLBACK(gd_scroll_event
), vc
);
1923 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1924 G_CALLBACK(gd_key_event
), vc
);
1925 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1926 G_CALLBACK(gd_key_event
), vc
);
1928 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1929 G_CALLBACK(gd_enter_event
), vc
);
1930 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1931 G_CALLBACK(gd_leave_event
), vc
);
1932 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1933 G_CALLBACK(gd_focus_out_event
), vc
);
1934 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1935 G_CALLBACK(gd_configure
), vc
);
1937 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1938 G_CALLBACK(gd_text_key_down
), vc
);
1942 static void gd_connect_signals(GtkDisplayState
*s
)
1944 g_signal_connect(s
->show_tabs_item
, "activate",
1945 G_CALLBACK(gd_menu_show_tabs
), s
);
1946 g_signal_connect(s
->untabify_item
, "activate",
1947 G_CALLBACK(gd_menu_untabify
), s
);
1949 g_signal_connect(s
->window
, "delete-event",
1950 G_CALLBACK(gd_window_close
), s
);
1952 g_signal_connect(s
->pause_item
, "activate",
1953 G_CALLBACK(gd_menu_pause
), s
);
1954 g_signal_connect(s
->reset_item
, "activate",
1955 G_CALLBACK(gd_menu_reset
), s
);
1956 g_signal_connect(s
->powerdown_item
, "activate",
1957 G_CALLBACK(gd_menu_powerdown
), s
);
1958 g_signal_connect(s
->quit_item
, "activate",
1959 G_CALLBACK(gd_menu_quit
), s
);
1960 #if defined(CONFIG_VTE)
1961 g_signal_connect(s
->copy_item
, "activate",
1962 G_CALLBACK(gd_menu_copy
), s
);
1964 g_signal_connect(s
->full_screen_item
, "activate",
1965 G_CALLBACK(gd_menu_full_screen
), s
);
1966 g_signal_connect(s
->zoom_in_item
, "activate",
1967 G_CALLBACK(gd_menu_zoom_in
), s
);
1968 g_signal_connect(s
->zoom_out_item
, "activate",
1969 G_CALLBACK(gd_menu_zoom_out
), s
);
1970 g_signal_connect(s
->zoom_fixed_item
, "activate",
1971 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1972 g_signal_connect(s
->zoom_fit_item
, "activate",
1973 G_CALLBACK(gd_menu_zoom_fit
), s
);
1974 g_signal_connect(s
->grab_item
, "activate",
1975 G_CALLBACK(gd_menu_grab_input
), s
);
1976 g_signal_connect(s
->notebook
, "switch-page",
1977 G_CALLBACK(gd_change_page
), s
);
1980 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1982 GtkWidget
*machine_menu
;
1983 GtkWidget
*separator
;
1985 machine_menu
= gtk_menu_new();
1986 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1988 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1989 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1991 separator
= gtk_separator_menu_item_new();
1992 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1994 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1995 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1997 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1998 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
2000 separator
= gtk_separator_menu_item_new();
2001 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
2003 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
2004 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
2005 "<QEMU>/Machine/Quit");
2006 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
2007 GDK_KEY_q
, HOTKEY_MODIFIERS
);
2008 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
2010 return machine_menu
;
2013 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
2014 QemuConsole
*con
, int idx
,
2015 GSList
*group
, GtkWidget
*view_menu
)
2017 vc
->label
= qemu_console_get_label(con
);
2019 vc
->gfx
.scale_x
= 1.0;
2020 vc
->gfx
.scale_y
= 1.0;
2022 #if defined(CONFIG_OPENGL)
2023 if (display_opengl
) {
2024 #if defined(CONFIG_GTK_GL)
2025 vc
->gfx
.drawing_area
= gtk_gl_area_new();
2026 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
2028 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2030 * gtk_widget_set_double_buffered() was deprecated in 3.14.
2031 * It is required for opengl rendering on X11 though. A
2032 * proper replacement (native opengl support) is only
2033 * available in 3.16+. Silence the warning if possible.
2035 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2036 #pragma GCC diagnostic push
2037 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2039 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
2040 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
2041 #pragma GCC diagnostic pop
2043 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
2044 #endif /* CONFIG_GTK_GL */
2048 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
2049 vc
->gfx
.dcl
.ops
= &dcl_ops
;
2053 gtk_widget_add_events(vc
->gfx
.drawing_area
,
2054 GDK_POINTER_MOTION_MASK
|
2055 GDK_BUTTON_PRESS_MASK
|
2056 GDK_BUTTON_RELEASE_MASK
|
2057 GDK_BUTTON_MOTION_MASK
|
2058 GDK_ENTER_NOTIFY_MASK
|
2059 GDK_LEAVE_NOTIFY_MASK
|
2061 GDK_KEY_PRESS_MASK
);
2062 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
2064 vc
->type
= GD_VC_GFX
;
2065 vc
->tab_item
= vc
->gfx
.drawing_area
;
2066 vc
->focus
= vc
->gfx
.drawing_area
;
2067 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
2068 vc
->tab_item
, gtk_label_new(vc
->label
));
2070 vc
->gfx
.dcl
.con
= con
;
2071 register_displaychangelistener(&vc
->gfx
.dcl
);
2073 gd_connect_vc_gfx_signals(vc
);
2074 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
2076 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
2077 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
2078 s
->free_scale
= true;
2084 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2086 GSList
*group
= NULL
;
2087 GtkWidget
*view_menu
;
2088 GtkWidget
*separator
;
2092 view_menu
= gtk_menu_new();
2093 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2095 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2097 #if defined(CONFIG_VTE)
2098 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2099 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2102 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2103 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2104 #if GTK_CHECK_VERSION(3, 8, 0)
2105 gtk_accel_label_set_accel(
2106 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2107 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2109 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2111 separator
= gtk_separator_menu_item_new();
2112 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2114 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2115 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2116 "<QEMU>/View/Zoom In");
2117 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2119 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_equal
, HOTKEY_MODIFIERS
, 0,
2120 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in
), s
, NULL
));
2121 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2123 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2124 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2125 "<QEMU>/View/Zoom Out");
2126 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2128 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2130 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2131 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2132 "<QEMU>/View/Zoom Fixed");
2133 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2135 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2137 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2138 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2140 separator
= gtk_separator_menu_item_new();
2141 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2143 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2144 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2146 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2147 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2148 "<QEMU>/View/Grab Input");
2149 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2151 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2153 separator
= gtk_separator_menu_item_new();
2154 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2157 for (vc
= 0;; vc
++) {
2158 con
= qemu_console_lookup_by_index(vc
);
2162 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2163 vc
, group
, view_menu
);
2167 #if defined(CONFIG_VTE)
2169 gd_vcs_init(s
, group
, view_menu
);
2172 separator
= gtk_separator_menu_item_new();
2173 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2175 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2176 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2178 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2179 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2184 static void gd_create_menus(GtkDisplayState
*s
)
2186 s
->accel_group
= gtk_accel_group_new();
2187 s
->machine_menu
= gd_create_menu_machine(s
);
2188 s
->view_menu
= gd_create_menu_view(s
);
2190 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2191 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2193 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2195 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2196 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2197 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2199 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2200 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2203 static void gd_set_keycode_type(GtkDisplayState
*s
)
2205 #ifdef GDK_WINDOWING_X11
2206 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
2207 if (GDK_IS_X11_DISPLAY(display
)) {
2208 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
2209 XkbDescPtr desc
= XkbGetMap(x11_display
, XkbGBN_AllComponentsMask
,
2211 char *keycodes
= NULL
;
2214 (XkbGetNames(x11_display
, XkbKeycodesNameMask
, desc
) == Success
)) {
2215 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
2217 if (keycodes
== NULL
) {
2218 fprintf(stderr
, "could not lookup keycode name\n");
2219 } else if (strstart(keycodes
, "evdev", NULL
)) {
2220 s
->has_evdev
= true;
2221 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
2222 fprintf(stderr
, "unknown keycodes `%s', please report to "
2223 "qemu-devel@nongnu.org\n", keycodes
);
2227 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
2236 static gboolean gtkinit
;
2238 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
2242 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2244 GdkDisplay
*window_display
;
2247 fprintf(stderr
, "gtk initialization failed\n");
2251 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2252 #if GTK_CHECK_VERSION(3, 2, 0)
2253 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2255 s
->vbox
= gtk_vbox_new(FALSE
, 0);
2257 s
->notebook
= gtk_notebook_new();
2258 s
->menu_bar
= gtk_menu_bar_new();
2260 s
->free_scale
= FALSE
;
2262 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For
2263 * LC_CTYPE, we need to make sure that non-ASCII characters are considered
2264 * printable, but without changing any of the character classes to make
2265 * sure that we don't accidentally break implicit assumptions. */
2266 setlocale(LC_MESSAGES
, "");
2267 setlocale(LC_CTYPE
, "C.UTF-8");
2268 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2271 window_display
= gtk_widget_get_display(s
->window
);
2272 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2275 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2276 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2277 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2279 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
2281 GError
*error
= NULL
;
2282 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
2284 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
2286 g_error_free(error
);
2293 gd_connect_signals(s
);
2295 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2296 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2298 gd_update_caption(s
);
2300 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2301 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2303 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2305 gtk_widget_show_all(s
->window
);
2307 #ifdef VTE_RESIZE_HACK
2309 VirtualConsole
*cur
= gd_vc_find_current(s
);
2313 for (i
= 0; i
< s
->nb_vcs
; i
++) {
2314 VirtualConsole
*vc
= &s
->vc
[i
];
2315 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
2316 gtk_widget_hide(vc
->vte
.terminal
);
2319 gd_update_windowsize(cur
);
2324 vc
= gd_vc_find_current(s
);
2325 gtk_widget_set_sensitive(s
->view_menu
, vc
!= NULL
);
2327 gtk_widget_set_sensitive(s
->copy_item
,
2328 vc
&& vc
->type
== GD_VC_VTE
);
2332 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2334 if (grab_on_hover
) {
2335 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2338 gd_set_keycode_type(s
);
2341 void early_gtk_display_init(int opengl
)
2343 /* The QEMU code relies on the assumption that it's always run in
2344 * the C locale. Therefore it is not prepared to deal with
2345 * operations that produce different results depending on the
2346 * locale, such as printf's formatting of decimal numbers, and
2349 * Since GTK+ calls setlocale() by default -importing the locale
2350 * settings from the environment- we must prevent it from doing so
2351 * using gtk_disable_setlocale().
2353 * QEMU's GTK+ UI, however, _does_ have translations for some of
2354 * the menu items. As a trade-off between a functionally correct
2355 * QEMU and a fully internationalized UI we support importing
2356 * LC_MESSAGES from the environment (see the setlocale() call
2357 * earlier in this file). This allows us to display translated
2358 * messages leaving everything else untouched.
2360 gtk_disable_setlocale();
2361 gtkinit
= gtk_init_check(NULL
, NULL
);
2363 /* don't exit yet, that'll break -help */
2368 case -1: /* default */
2372 #if defined(CONFIG_OPENGL)
2373 #if defined(CONFIG_GTK_GL)
2381 g_assert_not_reached();
2385 #if defined(CONFIG_VTE)
2386 type_register(&char_gd_vc_type_info
);