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-common.h"
39 #include "ui/console.h"
42 #include <glib/gi18n.h>
44 #if defined(CONFIG_VTE)
51 #include "sysemu/sysemu.h"
52 #include "qmp-commands.h"
55 #include "sysemu/char.h"
56 #include "qom/object.h"
59 #define VC_WINDOW_X_MIN 320
60 #define VC_WINDOW_Y_MIN 240
61 #define VC_TERM_X_MIN 80
62 #define VC_TERM_Y_MIN 25
63 #define VC_SCALE_MIN 0.25
64 #define VC_SCALE_STEP 0.25
66 #if !defined(CONFIG_VTE)
67 # define VTE_CHECK_VERSION(a, b, c) 0
70 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
72 * The gtk2 vte terminal widget seriously messes up the window resize
73 * for some reason. You basically can't make the qemu window smaller
74 * any more because the toplevel window geoemtry hints are overridden.
76 * Workaround that by hiding all vte widgets, except the one in the
79 * Luckily everything works smooth in gtk3.
81 # define VTE_RESIZE_HACK 1
84 #if !GTK_CHECK_VERSION(2, 20, 0)
85 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
88 #ifndef GDK_IS_X11_DISPLAY
89 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
91 #ifndef GDK_IS_WIN32_DISPLAY
92 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
96 #define GDK_KEY_0 GDK_0
97 #define GDK_KEY_1 GDK_1
98 #define GDK_KEY_2 GDK_2
99 #define GDK_KEY_f GDK_f
100 #define GDK_KEY_g GDK_g
101 #define GDK_KEY_q GDK_q
102 #define GDK_KEY_plus GDK_plus
103 #define GDK_KEY_minus GDK_minus
104 #define GDK_KEY_Pause GDK_Pause
107 /* Some older mingw versions lack this constant or have
108 * it conditionally defined */
110 # ifndef MAPVK_VK_TO_VSC
111 # define MAPVK_VK_TO_VSC 0
116 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
118 static const int modifier_keycode
[] = {
119 /* shift, control, alt keys, meta keys, both left & right */
120 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
123 struct GtkDisplayState
{
128 GtkAccelGroup
*accel_group
;
130 GtkWidget
*machine_menu_item
;
131 GtkWidget
*machine_menu
;
132 GtkWidget
*pause_item
;
133 GtkWidget
*reset_item
;
134 GtkWidget
*powerdown_item
;
135 GtkWidget
*quit_item
;
137 GtkWidget
*view_menu_item
;
138 GtkWidget
*view_menu
;
139 GtkWidget
*full_screen_item
;
140 GtkWidget
*zoom_in_item
;
141 GtkWidget
*zoom_out_item
;
142 GtkWidget
*zoom_fixed_item
;
143 GtkWidget
*zoom_fit_item
;
144 GtkWidget
*grab_item
;
145 GtkWidget
*grab_on_hover_item
;
148 VirtualConsole vc
[MAX_VCS
];
150 GtkWidget
*show_tabs_item
;
151 GtkWidget
*untabify_item
;
161 VirtualConsole
*kbd_owner
;
162 VirtualConsole
*ptr_owner
;
164 gboolean full_screen
;
166 GdkCursor
*null_cursor
;
167 Notifier mouse_mode_notifier
;
170 bool external_pause_update
;
172 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
177 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
178 static void gd_ungrab_pointer(GtkDisplayState
*s
);
179 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
180 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
182 /** Utility Functions **/
184 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
189 for (i
= 0; i
< s
->nb_vcs
; i
++) {
191 if (gtk_check_menu_item_get_active
192 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
199 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
204 for (i
= 0; i
< s
->nb_vcs
; i
++) {
206 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
214 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
218 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
219 return gd_vc_find_by_page(s
, page
);
222 static bool gd_is_grab_active(GtkDisplayState
*s
)
224 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
227 static bool gd_grab_on_hover(GtkDisplayState
*s
)
229 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
232 static void gd_update_cursor(VirtualConsole
*vc
)
234 GtkDisplayState
*s
= vc
->s
;
237 if (vc
->type
!= GD_VC_GFX
||
238 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
242 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
246 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
247 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
248 gdk_window_set_cursor(window
, s
->null_cursor
);
250 gdk_window_set_cursor(window
, NULL
);
254 static void gd_update_caption(GtkDisplayState
*s
)
256 const char *status
= "";
259 const char *grab
= "";
260 bool is_paused
= !runstate_is_running();
264 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
266 prefix
= g_strdup_printf("QEMU");
269 if (s
->ptr_owner
!= NULL
&&
270 s
->ptr_owner
->window
== NULL
) {
271 grab
= _(" - Press Ctrl+Alt+G to release grab");
275 status
= _(" [Paused]");
277 s
->external_pause_update
= true;
278 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
280 s
->external_pause_update
= false;
282 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
283 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
286 for (i
= 0; i
< s
->nb_vcs
; i
++) {
287 VirtualConsole
*vc
= &s
->vc
[i
];
292 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
293 vc
== s
->kbd_owner
? " +kbd" : "",
294 vc
== s
->ptr_owner
? " +ptr" : "");
295 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
302 static void gd_update_geometry_hints(VirtualConsole
*vc
)
304 GtkDisplayState
*s
= vc
->s
;
305 GdkWindowHints mask
= 0;
306 GdkGeometry geo
= {};
307 GtkWidget
*geo_widget
= NULL
;
308 GtkWindow
*geo_window
;
310 if (vc
->type
== GD_VC_GFX
) {
315 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
316 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
317 mask
|= GDK_HINT_MIN_SIZE
;
319 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
320 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
321 mask
|= GDK_HINT_MIN_SIZE
;
323 geo_widget
= vc
->gfx
.drawing_area
;
324 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
326 #if defined(CONFIG_VTE)
327 } else if (vc
->type
== GD_VC_VTE
) {
328 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
331 geo
.width_inc
= vte_terminal_get_char_width(term
);
332 geo
.height_inc
= vte_terminal_get_char_height(term
);
333 mask
|= GDK_HINT_RESIZE_INC
;
334 geo
.base_width
= geo
.width_inc
;
335 geo
.base_height
= geo
.height_inc
;
336 mask
|= GDK_HINT_BASE_SIZE
;
337 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
338 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
339 mask
|= GDK_HINT_MIN_SIZE
;
340 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
341 geo
.base_width
+= ib
->left
+ ib
->right
;
342 geo
.base_height
+= ib
->top
+ ib
->bottom
;
343 geo
.min_width
+= ib
->left
+ ib
->right
;
344 geo
.min_height
+= ib
->top
+ ib
->bottom
;
345 geo_widget
= vc
->vte
.terminal
;
349 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
350 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
353 void gd_update_windowsize(VirtualConsole
*vc
)
355 GtkDisplayState
*s
= vc
->s
;
357 gd_update_geometry_hints(vc
);
359 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
360 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
361 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
365 static void gd_update_full_redraw(VirtualConsole
*vc
)
367 GtkWidget
*area
= vc
->gfx
.drawing_area
;
369 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
370 #if defined(CONFIG_GTK_GL)
372 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
376 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
379 static void gtk_release_modifiers(GtkDisplayState
*s
)
381 VirtualConsole
*vc
= gd_vc_find_current(s
);
384 if (vc
->type
!= GD_VC_GFX
||
385 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
388 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
389 keycode
= modifier_keycode
[i
];
390 if (!s
->modifier_pressed
[i
]) {
393 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, keycode
, false);
394 s
->modifier_pressed
[i
] = false;
398 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
401 g_object_ref(G_OBJECT(widget
));
402 gtk_container_remove(GTK_CONTAINER(from
), widget
);
403 gtk_container_add(GTK_CONTAINER(to
), widget
);
404 g_object_unref(G_OBJECT(widget
));
407 /** DisplayState Callbacks **/
409 static void gd_update(DisplayChangeListener
*dcl
,
410 int x
, int y
, int w
, int h
)
412 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
419 trace_gd_update(vc
->label
, x
, y
, w
, h
);
421 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
425 if (vc
->gfx
.convert
) {
426 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
427 NULL
, vc
->gfx
.convert
,
428 x
, y
, 0, 0, x
, y
, w
, h
);
431 x1
= floor(x
* vc
->gfx
.scale_x
);
432 y1
= floor(y
* vc
->gfx
.scale_y
);
434 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
435 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
437 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
438 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
440 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
444 gdk_drawable_get_size(win
, &ww
, &wh
);
454 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
455 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
458 static void gd_refresh(DisplayChangeListener
*dcl
)
460 graphic_hw_update(dcl
->con
);
463 #if GTK_CHECK_VERSION(3, 0, 0)
464 static void gd_mouse_set(DisplayChangeListener
*dcl
,
465 int x
, int y
, int visible
)
467 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
469 GdkDeviceManager
*mgr
;
472 if (qemu_input_is_absolute()) {
476 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
477 mgr
= gdk_display_get_device_manager(dpy
);
478 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
479 x
, y
, &x_root
, &y_root
);
480 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
481 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
487 static void gd_mouse_set(DisplayChangeListener
*dcl
,
488 int x
, int y
, int visible
)
490 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
493 if (qemu_input_is_absolute()) {
497 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
498 x
, y
, &x_root
, &y_root
);
499 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
500 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
505 static void gd_cursor_define(DisplayChangeListener
*dcl
,
508 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
512 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
516 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
517 GDK_COLORSPACE_RGB
, true, 8,
518 c
->width
, c
->height
, c
->width
* 4,
520 cursor
= gdk_cursor_new_from_pixbuf
521 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
522 pixbuf
, c
->hot_x
, c
->hot_y
);
523 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
524 g_object_unref(pixbuf
);
525 #if !GTK_CHECK_VERSION(3, 0, 0)
526 gdk_cursor_unref(cursor
);
528 g_object_unref(cursor
);
532 static void gd_switch(DisplayChangeListener
*dcl
,
533 DisplaySurface
*surface
)
535 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
538 trace_gd_switch(vc
->label
,
539 surface
? surface_width(surface
) : 0,
540 surface
? surface_height(surface
) : 0);
542 if (vc
->gfx
.surface
) {
543 cairo_surface_destroy(vc
->gfx
.surface
);
544 vc
->gfx
.surface
= NULL
;
546 if (vc
->gfx
.convert
) {
547 pixman_image_unref(vc
->gfx
.convert
);
548 vc
->gfx
.convert
= NULL
;
551 if (vc
->gfx
.ds
&& surface
&&
552 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
553 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
556 vc
->gfx
.ds
= surface
;
562 if (surface
->format
== PIXMAN_x8r8g8b8
) {
564 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
566 * No need to convert, use surface directly. Should be the
567 * common case as this is qemu_default_pixelformat(32) too.
569 vc
->gfx
.surface
= cairo_image_surface_create_for_data
570 (surface_data(surface
),
572 surface_width(surface
),
573 surface_height(surface
),
574 surface_stride(surface
));
576 /* Must convert surface, use pixman to do it. */
577 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
578 surface_width(surface
),
579 surface_height(surface
),
581 vc
->gfx
.surface
= cairo_image_surface_create_for_data
582 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
584 pixman_image_get_width(vc
->gfx
.convert
),
585 pixman_image_get_height(vc
->gfx
.convert
),
586 pixman_image_get_stride(vc
->gfx
.convert
));
587 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
588 NULL
, vc
->gfx
.convert
,
590 pixman_image_get_width(vc
->gfx
.convert
),
591 pixman_image_get_height(vc
->gfx
.convert
));
595 gd_update_windowsize(vc
);
597 gd_update_full_redraw(vc
);
601 static const DisplayChangeListenerOps dcl_ops
= {
603 .dpy_gfx_update
= gd_update
,
604 .dpy_gfx_switch
= gd_switch
,
605 .dpy_gfx_check_format
= qemu_pixman_check_format
,
606 .dpy_refresh
= gd_refresh
,
607 .dpy_mouse_set
= gd_mouse_set
,
608 .dpy_cursor_define
= gd_cursor_define
,
612 #if defined(CONFIG_OPENGL)
614 /** DisplayState Callbacks (opengl version) **/
616 #if defined(CONFIG_GTK_GL)
618 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
619 .dpy_name
= "gtk-egl",
620 .dpy_gfx_update
= gd_gl_area_update
,
621 .dpy_gfx_switch
= gd_gl_area_switch
,
622 .dpy_gfx_check_format
= console_gl_check_format
,
623 .dpy_refresh
= gd_gl_area_refresh
,
624 .dpy_mouse_set
= gd_mouse_set
,
625 .dpy_cursor_define
= gd_cursor_define
,
627 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
628 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
629 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
630 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
631 .dpy_gl_scanout
= gd_gl_area_scanout
,
632 .dpy_gl_update
= gd_gl_area_scanout_flush
,
637 static const DisplayChangeListenerOps dcl_egl_ops
= {
638 .dpy_name
= "gtk-egl",
639 .dpy_gfx_update
= gd_egl_update
,
640 .dpy_gfx_switch
= gd_egl_switch
,
641 .dpy_gfx_check_format
= console_gl_check_format
,
642 .dpy_refresh
= gd_egl_refresh
,
643 .dpy_mouse_set
= gd_mouse_set
,
644 .dpy_cursor_define
= gd_cursor_define
,
646 .dpy_gl_ctx_create
= gd_egl_create_context
,
647 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
648 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
649 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
650 .dpy_gl_scanout
= gd_egl_scanout
,
651 .dpy_gl_update
= gd_egl_scanout_flush
,
654 #endif /* CONFIG_GTK_GL */
655 #endif /* CONFIG_OPENGL */
659 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
661 GtkDisplayState
*s
= opaque
;
663 gd_update_caption(s
);
666 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
671 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
672 /* release the grab at switching to absolute mode */
673 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
674 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
677 for (i
= 0; i
< s
->nb_vcs
; i
++) {
678 VirtualConsole
*vc
= &s
->vc
[i
];
679 gd_update_cursor(vc
);
685 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
688 GtkDisplayState
*s
= opaque
;
692 for (i
= 0; i
< s
->nb_vcs
; i
++) {
693 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
696 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
705 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
709 memset(&info
, 0, sizeof(info
));
711 info
.height
= height
;
712 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
715 #if defined(CONFIG_GTK_GL)
717 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
720 VirtualConsole
*vc
= opaque
;
728 static void gd_resize_event(GtkGLArea
*area
,
729 gint width
, gint height
, gpointer
*opaque
)
731 VirtualConsole
*vc
= (void *)opaque
;
733 gd_set_ui_info(vc
, width
, height
);
738 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
740 VirtualConsole
*vc
= opaque
;
741 GtkDisplayState
*s
= vc
->s
;
746 #if defined(CONFIG_OPENGL)
748 #if defined(CONFIG_GTK_GL)
749 /* invoke render callback please */
758 if (!gtk_widget_get_realized(widget
)) {
765 fbw
= surface_width(vc
->gfx
.ds
);
766 fbh
= surface_height(vc
->gfx
.ds
);
768 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
770 if (s
->full_screen
) {
771 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
772 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
773 } else if (s
->free_scale
) {
776 sx
= (double)ww
/ fbw
;
777 sy
= (double)wh
/ fbh
;
779 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
782 fbw
*= vc
->gfx
.scale_x
;
783 fbh
*= vc
->gfx
.scale_y
;
793 cairo_rectangle(cr
, 0, 0, ww
, wh
);
795 /* Optionally cut out the inner area where the pixmap
796 will be drawn. This avoids 'flashing' since we're
797 not double-buffering. Note we're using the undocumented
798 behaviour of drawing the rectangle from right to left
799 to cut out the whole */
800 cairo_rectangle(cr
, mx
+ fbw
, my
,
804 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
805 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
806 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
812 #if !GTK_CHECK_VERSION(3, 0, 0)
813 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
819 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
824 expose
->area
.height
);
827 ret
= gd_draw_event(widget
, cr
, opaque
);
835 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
838 VirtualConsole
*vc
= opaque
;
839 GtkDisplayState
*s
= vc
->s
;
849 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
850 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
852 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
863 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
864 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
866 if (qemu_input_is_absolute()) {
867 if (x
< 0 || y
< 0 ||
868 x
>= surface_width(vc
->gfx
.ds
) ||
869 y
>= surface_height(vc
->gfx
.ds
)) {
872 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
873 surface_width(vc
->gfx
.ds
));
874 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
875 surface_height(vc
->gfx
.ds
));
876 qemu_input_event_sync();
877 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
878 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
879 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
880 qemu_input_event_sync();
886 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
887 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
888 int x
= (int)motion
->x_root
;
889 int y
= (int)motion
->y_root
;
891 /* In relative mode check to see if client pointer hit
892 * one of the screen edges, and if so move it back by
893 * 200 pixels. This is important because the pointer
894 * in the server doesn't correspond 1-for-1, and so
895 * may still be only half way across the screen. Without
896 * this warp, the server pointer would thus appear to hit
897 * an invisible wall */
904 if (x
== (gdk_screen_get_width(screen
) - 1)) {
907 if (y
== (gdk_screen_get_height(screen
) - 1)) {
911 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
912 #if GTK_CHECK_VERSION(3, 0, 0)
913 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
914 gdk_device_warp(dev
, screen
, x
, y
);
916 GdkDisplay
*display
= gtk_widget_get_display(widget
);
917 gdk_display_warp_pointer(display
, screen
, x
, y
);
926 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
929 VirtualConsole
*vc
= opaque
;
930 GtkDisplayState
*s
= vc
->s
;
933 /* implicitly grab the input at the first click in the relative mode */
934 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
935 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
937 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
940 gd_grab_pointer(vc
, "relative-mode-click");
945 if (button
->button
== 1) {
946 btn
= INPUT_BUTTON_LEFT
;
947 } else if (button
->button
== 2) {
948 btn
= INPUT_BUTTON_MIDDLE
;
949 } else if (button
->button
== 3) {
950 btn
= INPUT_BUTTON_RIGHT
;
955 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
956 button
->type
== GDK_BUTTON_PRESS
);
957 qemu_input_event_sync();
961 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
964 VirtualConsole
*vc
= opaque
;
967 if (scroll
->direction
== GDK_SCROLL_UP
) {
968 btn
= INPUT_BUTTON_WHEELUP
;
969 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
970 btn
= INPUT_BUTTON_WHEELDOWN
;
975 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
976 qemu_input_event_sync();
977 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
978 qemu_input_event_sync();
982 static int gd_map_keycode(GtkDisplayState
*s
, GdkDisplay
*dpy
, int gdk_keycode
)
986 #ifdef GDK_WINDOWING_WIN32
987 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
988 qemu_keycode
= MapVirtualKey(gdk_keycode
, MAPVK_VK_TO_VSC
);
989 switch (qemu_keycode
) {
990 case 103: /* alt gr */
991 qemu_keycode
= 56 | SCANCODE_GREY
;
998 if (gdk_keycode
< 9) {
1000 } else if (gdk_keycode
< 97) {
1001 qemu_keycode
= gdk_keycode
- 8;
1002 #ifdef GDK_WINDOWING_X11
1003 } else if (GDK_IS_X11_DISPLAY(dpy
) && gdk_keycode
< 158) {
1005 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
1007 qemu_keycode
= translate_xfree86_keycode(gdk_keycode
- 97);
1010 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
1011 qemu_keycode
= 0x70;
1012 } else if (gdk_keycode
== 211) { /* backslash */
1013 qemu_keycode
= 0x73;
1018 return qemu_keycode
;
1021 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1022 GdkEventKey
*key
, void *opaque
)
1024 VirtualConsole
*vc
= opaque
;
1025 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1028 kbd_put_string_console(con
, key
->string
, key
->length
);
1030 int num
= gd_map_keycode(vc
->s
, gtk_widget_get_display(widget
),
1031 key
->hardware_keycode
);
1032 int qcode
= qemu_input_key_number_to_qcode(num
);
1033 kbd_put_qcode_console(con
, qcode
);
1038 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1040 VirtualConsole
*vc
= opaque
;
1041 GtkDisplayState
*s
= vc
->s
;
1042 int gdk_keycode
= key
->hardware_keycode
;
1046 if (s
->ignore_keys
) {
1047 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1051 if (key
->keyval
== GDK_KEY_Pause
) {
1052 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
1053 key
->type
== GDK_KEY_PRESS
);
1057 qemu_keycode
= gd_map_keycode(s
, gtk_widget_get_display(widget
),
1060 trace_gd_key_event(vc
->label
, gdk_keycode
, qemu_keycode
,
1061 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1063 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
1064 if (qemu_keycode
== modifier_keycode
[i
]) {
1065 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
1069 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, qemu_keycode
,
1070 key
->type
== GDK_KEY_PRESS
);
1075 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1077 if (event
->type
== GDK_MOTION_NOTIFY
) {
1078 return gd_motion_event(widget
, &event
->motion
, opaque
);
1083 /** Window Menu Actions **/
1085 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1087 GtkDisplayState
*s
= opaque
;
1089 if (s
->external_pause_update
) {
1092 if (runstate_is_running()) {
1099 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1101 qmp_system_reset(NULL
);
1104 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1106 qmp_system_powerdown(NULL
);
1109 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1114 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1116 GtkDisplayState
*s
= opaque
;
1117 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1118 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1121 gtk_release_modifiers(s
);
1123 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1124 gtk_notebook_set_current_page(nb
, page
);
1125 gtk_widget_grab_focus(vc
->focus
);
1127 s
->ignore_keys
= false;
1130 static void gd_accel_switch_vc(void *opaque
)
1132 VirtualConsole
*vc
= opaque
;
1134 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1135 #if !GTK_CHECK_VERSION(3, 0, 0)
1136 /* GTK2 sends the accel key to the target console - ignore this until */
1137 vc
->s
->ignore_keys
= true;
1141 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1143 GtkDisplayState
*s
= opaque
;
1144 VirtualConsole
*vc
= gd_vc_find_current(s
);
1146 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1147 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1149 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1151 gd_update_windowsize(vc
);
1154 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1157 VirtualConsole
*vc
= opaque
;
1158 GtkDisplayState
*s
= vc
->s
;
1160 gtk_widget_set_sensitive(vc
->menu_item
, true);
1161 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1162 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1163 vc
->tab_item
, vc
->label
);
1164 gtk_widget_destroy(vc
->window
);
1169 static gboolean
gd_win_grab(void *opaque
)
1171 VirtualConsole
*vc
= opaque
;
1173 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1174 if (vc
->s
->ptr_owner
) {
1175 gd_ungrab_pointer(vc
->s
);
1177 gd_grab_pointer(vc
, "user-request-detached-tab");
1182 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1184 GtkDisplayState
*s
= opaque
;
1185 VirtualConsole
*vc
= gd_vc_find_current(s
);
1187 if (vc
->type
== GD_VC_GFX
&&
1188 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1189 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1193 gtk_widget_set_sensitive(vc
->menu_item
, false);
1194 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1195 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1197 g_signal_connect(vc
->window
, "delete-event",
1198 G_CALLBACK(gd_tab_window_close
), vc
);
1199 gtk_widget_show_all(vc
->window
);
1201 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1202 GtkAccelGroup
*ag
= gtk_accel_group_new();
1203 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1205 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1207 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1210 gd_update_geometry_hints(vc
);
1211 gd_update_caption(s
);
1215 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1217 GtkDisplayState
*s
= opaque
;
1218 VirtualConsole
*vc
= gd_vc_find_current(s
);
1220 if (!s
->full_screen
) {
1221 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1222 gtk_widget_hide(s
->menu_bar
);
1223 if (vc
->type
== GD_VC_GFX
) {
1224 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1226 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1227 s
->full_screen
= TRUE
;
1229 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1230 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1231 gtk_widget_show(s
->menu_bar
);
1232 s
->full_screen
= FALSE
;
1233 if (vc
->type
== GD_VC_GFX
) {
1234 vc
->gfx
.scale_x
= 1.0;
1235 vc
->gfx
.scale_y
= 1.0;
1236 gd_update_windowsize(vc
);
1240 gd_update_cursor(vc
);
1243 static void gd_accel_full_screen(void *opaque
)
1245 GtkDisplayState
*s
= opaque
;
1246 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1249 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1251 GtkDisplayState
*s
= opaque
;
1252 VirtualConsole
*vc
= gd_vc_find_current(s
);
1254 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1257 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1258 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1260 gd_update_windowsize(vc
);
1263 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1265 GtkDisplayState
*s
= opaque
;
1266 VirtualConsole
*vc
= gd_vc_find_current(s
);
1268 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1271 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1272 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1274 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1275 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1277 gd_update_windowsize(vc
);
1280 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1282 GtkDisplayState
*s
= opaque
;
1283 VirtualConsole
*vc
= gd_vc_find_current(s
);
1285 vc
->gfx
.scale_x
= 1.0;
1286 vc
->gfx
.scale_y
= 1.0;
1288 gd_update_windowsize(vc
);
1291 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1293 GtkDisplayState
*s
= opaque
;
1294 VirtualConsole
*vc
= gd_vc_find_current(s
);
1296 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1297 s
->free_scale
= TRUE
;
1299 s
->free_scale
= FALSE
;
1300 vc
->gfx
.scale_x
= 1.0;
1301 vc
->gfx
.scale_y
= 1.0;
1304 gd_update_windowsize(vc
);
1305 gd_update_full_redraw(vc
);
1308 #if GTK_CHECK_VERSION(3, 0, 0)
1309 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1310 GdkInputSource source
, GdkEventMask mask
,
1313 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1314 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1315 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1318 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1319 GdkDevice
*dev
= tmp
->data
;
1320 if (gdk_device_get_source(dev
) != source
) {
1324 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1325 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1326 mask
, cursor
, GDK_CURRENT_TIME
);
1328 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1335 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1337 if (vc
->s
->kbd_owner
) {
1338 if (vc
->s
->kbd_owner
== vc
) {
1341 gd_ungrab_keyboard(vc
->s
);
1345 #if GTK_CHECK_VERSION(3, 0, 0)
1346 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1347 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1350 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1354 vc
->s
->kbd_owner
= vc
;
1355 gd_update_caption(vc
->s
);
1356 trace_gd_grab(vc
->label
, "kbd", reason
);
1359 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1361 VirtualConsole
*vc
= s
->kbd_owner
;
1366 s
->kbd_owner
= NULL
;
1368 #if GTK_CHECK_VERSION(3, 0, 0)
1369 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1371 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1373 gd_update_caption(s
);
1374 trace_gd_ungrab(vc
->label
, "kbd");
1377 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1379 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1381 if (vc
->s
->ptr_owner
) {
1382 if (vc
->s
->ptr_owner
== vc
) {
1385 gd_ungrab_pointer(vc
->s
);
1389 #if GTK_CHECK_VERSION(3, 0, 0)
1390 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1391 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1392 GDK_POINTER_MOTION_MASK
|
1393 GDK_BUTTON_PRESS_MASK
|
1394 GDK_BUTTON_RELEASE_MASK
|
1395 GDK_BUTTON_MOTION_MASK
|
1397 vc
->s
->null_cursor
);
1398 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr
),
1399 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1401 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1402 FALSE
, /* All events to come to our window directly */
1403 GDK_POINTER_MOTION_MASK
|
1404 GDK_BUTTON_PRESS_MASK
|
1405 GDK_BUTTON_RELEASE_MASK
|
1406 GDK_BUTTON_MOTION_MASK
|
1408 NULL
, /* Allow cursor to move over entire desktop */
1411 gdk_display_get_pointer(display
, NULL
,
1412 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1414 vc
->s
->ptr_owner
= vc
;
1415 gd_update_caption(vc
->s
);
1416 trace_gd_grab(vc
->label
, "ptr", reason
);
1419 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1421 VirtualConsole
*vc
= s
->ptr_owner
;
1426 s
->ptr_owner
= NULL
;
1428 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1429 #if GTK_CHECK_VERSION(3, 0, 0)
1430 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1431 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1432 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
1433 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1434 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1436 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1437 gdk_display_warp_pointer(display
,
1438 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1439 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1441 gd_update_caption(s
);
1442 trace_gd_ungrab(vc
->label
, "ptr");
1445 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1447 GtkDisplayState
*s
= opaque
;
1448 VirtualConsole
*vc
= gd_vc_find_current(s
);
1450 if (gd_is_grab_active(s
)) {
1451 gd_grab_keyboard(vc
, "user-request-main-window");
1452 gd_grab_pointer(vc
, "user-request-main-window");
1454 gd_ungrab_keyboard(s
);
1455 gd_ungrab_pointer(s
);
1458 gd_update_cursor(vc
);
1461 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1464 GtkDisplayState
*s
= data
;
1468 if (!gtk_widget_get_realized(s
->notebook
)) {
1472 #ifdef VTE_RESIZE_HACK
1473 vc
= gd_vc_find_current(s
);
1474 if (vc
&& vc
->type
== GD_VC_VTE
) {
1475 gtk_widget_hide(vc
->vte
.terminal
);
1478 vc
= gd_vc_find_by_page(s
, arg2
);
1482 #ifdef VTE_RESIZE_HACK
1483 if (vc
->type
== GD_VC_VTE
) {
1484 gtk_widget_show(vc
->vte
.terminal
);
1487 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1489 on_vga
= (vc
->type
== GD_VC_GFX
&&
1490 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1492 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1494 } else if (s
->full_screen
) {
1495 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1498 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1500 gd_update_windowsize(vc
);
1501 gd_update_cursor(vc
);
1504 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1507 VirtualConsole
*vc
= opaque
;
1508 GtkDisplayState
*s
= vc
->s
;
1510 if (gd_grab_on_hover(s
)) {
1511 gd_grab_keyboard(vc
, "grab-on-hover");
1516 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1519 VirtualConsole
*vc
= opaque
;
1520 GtkDisplayState
*s
= vc
->s
;
1522 if (gd_grab_on_hover(s
)) {
1523 gd_ungrab_keyboard(s
);
1528 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1529 GdkEventCrossing
*crossing
, gpointer opaque
)
1531 VirtualConsole
*vc
= opaque
;
1532 GtkDisplayState
*s
= vc
->s
;
1534 gtk_release_modifiers(s
);
1538 static gboolean
gd_configure(GtkWidget
*widget
,
1539 GdkEventConfigure
*cfg
, gpointer opaque
)
1541 VirtualConsole
*vc
= opaque
;
1543 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1547 /** Virtual Console Callbacks **/
1549 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1550 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1552 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1553 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1554 HOTKEY_MODIFIERS
, 0,
1555 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1556 #if GTK_CHECK_VERSION(3, 8, 0)
1557 gtk_accel_label_set_accel(
1558 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1559 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1562 g_signal_connect(vc
->menu_item
, "activate",
1563 G_CALLBACK(gd_menu_switch_vc
), s
);
1564 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1566 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1570 #if defined(CONFIG_VTE)
1571 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1573 VirtualConsole
*vc
= opaque
;
1575 if (gtk_adjustment_get_upper(adjustment
) >
1576 gtk_adjustment_get_page_size(adjustment
)) {
1577 gtk_widget_show(vc
->vte
.scrollbar
);
1579 gtk_widget_hide(vc
->vte
.scrollbar
);
1583 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1585 VirtualConsole
*vc
= chr
->opaque
;
1587 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1591 static void gd_vc_chr_set_echo(CharDriverState
*chr
, bool echo
)
1593 VirtualConsole
*vc
= chr
->opaque
;
1595 vc
->vte
.echo
= echo
;
1599 static CharDriverState
*vcs
[MAX_VCS
];
1601 static CharDriverState
*gd_vc_handler(ChardevVC
*vc
, Error
**errp
)
1603 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
1604 CharDriverState
*chr
;
1606 chr
= qemu_chr_alloc(common
, errp
);
1611 chr
->chr_write
= gd_vc_chr_write
;
1612 chr
->chr_set_echo
= gd_vc_chr_set_echo
;
1614 /* Temporary, until gd_vc_vte_init runs. */
1615 chr
->opaque
= g_new(VirtualConsole
, 1);
1617 /* defer OPENED events until our vc is fully initialized */
1618 chr
->explicit_be_open
= true;
1620 vcs
[nb_vcs
++] = chr
;
1625 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1628 VirtualConsole
*vc
= user_data
;
1631 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1633 for (i
= 0; i
< size
; i
++) {
1634 uint8_t c
= text
[i
];
1635 if (c
>= 128 || isprint(c
)) {
1636 /* 8-bit characters are considered printable. */
1637 vte_terminal_feed(term
, &text
[i
], 1);
1638 } else if (c
== '\r' || c
== '\n') {
1639 vte_terminal_feed(term
, "\r\n", 2);
1641 char ctrl
[2] = { '^', 0};
1642 ctrl
[1] = text
[i
] ^ 64;
1643 vte_terminal_feed(term
, ctrl
, 2);
1648 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1652 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1653 CharDriverState
*chr
, int idx
,
1654 GSList
*group
, GtkWidget
*view_menu
)
1658 GtkWidget
*scrollbar
;
1659 GtkAdjustment
*vadjustment
;
1660 VirtualConsole
*tmp_vc
= chr
->opaque
;
1663 vc
->vte
.echo
= tmp_vc
->vte
.echo
;
1669 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1670 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1671 ? vc
->vte
.chr
->label
: buffer
);
1672 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1674 vc
->vte
.terminal
= vte_terminal_new();
1675 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1677 /* The documentation says that the default is UTF-8, but actually it is
1678 * 7-bit ASCII at least in VTE 0.38.
1680 #if VTE_CHECK_VERSION(0, 40, 0)
1681 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1683 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1686 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1687 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1688 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1690 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1691 vadjustment
= gtk_scrollable_get_vadjustment
1692 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1694 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1697 #if GTK_CHECK_VERSION(3, 0, 0)
1698 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1699 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1701 box
= gtk_hbox_new(false, 2);
1702 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1705 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1706 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1709 vc
->vte
.scrollbar
= scrollbar
;
1711 g_signal_connect(vadjustment
, "changed",
1712 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1714 vc
->type
= GD_VC_VTE
;
1716 vc
->focus
= vc
->vte
.terminal
;
1717 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1718 gtk_label_new(vc
->label
));
1720 qemu_chr_be_generic_open(vc
->vte
.chr
);
1721 if (vc
->vte
.chr
->init
) {
1722 vc
->vte
.chr
->init(vc
->vte
.chr
);
1728 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1729 GtkWidget
*view_menu
)
1733 for (i
= 0; i
< nb_vcs
; i
++) {
1734 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1735 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1739 #endif /* CONFIG_VTE */
1741 /** Window Creation **/
1743 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1745 #if GTK_CHECK_VERSION(3, 0, 0)
1746 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1747 G_CALLBACK(gd_draw_event
), vc
);
1748 #if defined(CONFIG_GTK_GL)
1749 if (display_opengl
) {
1750 /* wire up GtkGlArea events */
1751 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1752 G_CALLBACK(gd_render_event
), vc
);
1753 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1754 G_CALLBACK(gd_resize_event
), vc
);
1758 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1759 G_CALLBACK(gd_expose_event
), vc
);
1761 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1762 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1763 G_CALLBACK(gd_event
), vc
);
1764 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1765 G_CALLBACK(gd_button_event
), vc
);
1766 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1767 G_CALLBACK(gd_button_event
), vc
);
1768 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1769 G_CALLBACK(gd_scroll_event
), vc
);
1770 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1771 G_CALLBACK(gd_key_event
), vc
);
1772 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1773 G_CALLBACK(gd_key_event
), vc
);
1775 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1776 G_CALLBACK(gd_enter_event
), vc
);
1777 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1778 G_CALLBACK(gd_leave_event
), vc
);
1779 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1780 G_CALLBACK(gd_focus_out_event
), vc
);
1781 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1782 G_CALLBACK(gd_configure
), vc
);
1784 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1785 G_CALLBACK(gd_text_key_down
), vc
);
1789 static void gd_connect_signals(GtkDisplayState
*s
)
1791 g_signal_connect(s
->show_tabs_item
, "activate",
1792 G_CALLBACK(gd_menu_show_tabs
), s
);
1793 g_signal_connect(s
->untabify_item
, "activate",
1794 G_CALLBACK(gd_menu_untabify
), s
);
1796 g_signal_connect(s
->window
, "delete-event",
1797 G_CALLBACK(gd_window_close
), s
);
1799 g_signal_connect(s
->pause_item
, "activate",
1800 G_CALLBACK(gd_menu_pause
), s
);
1801 g_signal_connect(s
->reset_item
, "activate",
1802 G_CALLBACK(gd_menu_reset
), s
);
1803 g_signal_connect(s
->powerdown_item
, "activate",
1804 G_CALLBACK(gd_menu_powerdown
), s
);
1805 g_signal_connect(s
->quit_item
, "activate",
1806 G_CALLBACK(gd_menu_quit
), s
);
1807 g_signal_connect(s
->full_screen_item
, "activate",
1808 G_CALLBACK(gd_menu_full_screen
), s
);
1809 g_signal_connect(s
->zoom_in_item
, "activate",
1810 G_CALLBACK(gd_menu_zoom_in
), s
);
1811 g_signal_connect(s
->zoom_out_item
, "activate",
1812 G_CALLBACK(gd_menu_zoom_out
), s
);
1813 g_signal_connect(s
->zoom_fixed_item
, "activate",
1814 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1815 g_signal_connect(s
->zoom_fit_item
, "activate",
1816 G_CALLBACK(gd_menu_zoom_fit
), s
);
1817 g_signal_connect(s
->grab_item
, "activate",
1818 G_CALLBACK(gd_menu_grab_input
), s
);
1819 g_signal_connect(s
->notebook
, "switch-page",
1820 G_CALLBACK(gd_change_page
), s
);
1823 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1825 GtkWidget
*machine_menu
;
1826 GtkWidget
*separator
;
1828 machine_menu
= gtk_menu_new();
1829 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1831 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1832 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1834 separator
= gtk_separator_menu_item_new();
1835 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1837 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1838 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1840 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1841 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1843 separator
= gtk_separator_menu_item_new();
1844 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1846 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1847 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1848 "<QEMU>/Machine/Quit");
1849 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1850 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1851 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1853 return machine_menu
;
1856 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1857 QemuConsole
*con
, int idx
,
1858 GSList
*group
, GtkWidget
*view_menu
)
1860 vc
->label
= qemu_console_get_label(con
);
1862 vc
->gfx
.scale_x
= 1.0;
1863 vc
->gfx
.scale_y
= 1.0;
1865 #if defined(CONFIG_OPENGL)
1866 if (display_opengl
) {
1867 #if defined(CONFIG_GTK_GL)
1868 vc
->gfx
.drawing_area
= gtk_gl_area_new();
1869 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
1871 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1873 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1874 * It is required for opengl rendering on X11 though. A
1875 * proper replacement (native opengl support) is only
1876 * available in 3.16+. Silence the warning if possible.
1878 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1879 #pragma GCC diagnostic push
1880 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1882 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
1883 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1884 #pragma GCC diagnostic pop
1886 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
1887 #endif /* CONFIG_GTK_GL */
1891 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1892 vc
->gfx
.dcl
.ops
= &dcl_ops
;
1896 gtk_widget_add_events(vc
->gfx
.drawing_area
,
1897 GDK_POINTER_MOTION_MASK
|
1898 GDK_BUTTON_PRESS_MASK
|
1899 GDK_BUTTON_RELEASE_MASK
|
1900 GDK_BUTTON_MOTION_MASK
|
1901 GDK_ENTER_NOTIFY_MASK
|
1902 GDK_LEAVE_NOTIFY_MASK
|
1904 GDK_KEY_PRESS_MASK
);
1905 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
1907 vc
->type
= GD_VC_GFX
;
1908 vc
->tab_item
= vc
->gfx
.drawing_area
;
1909 vc
->focus
= vc
->gfx
.drawing_area
;
1910 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
1911 vc
->tab_item
, gtk_label_new(vc
->label
));
1913 vc
->gfx
.dcl
.con
= con
;
1914 register_displaychangelistener(&vc
->gfx
.dcl
);
1916 gd_connect_vc_gfx_signals(vc
);
1917 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1919 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
1920 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
1921 s
->free_scale
= true;
1927 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
1929 GSList
*group
= NULL
;
1930 GtkWidget
*view_menu
;
1931 GtkWidget
*separator
;
1935 view_menu
= gtk_menu_new();
1936 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
1938 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1940 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
1941 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
1942 #if GTK_CHECK_VERSION(3, 8, 0)
1943 gtk_accel_label_set_accel(
1944 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
1945 GDK_KEY_f
, HOTKEY_MODIFIERS
);
1947 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
1949 separator
= gtk_separator_menu_item_new();
1950 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1952 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1953 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1954 "<QEMU>/View/Zoom In");
1955 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
1957 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
1959 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1960 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1961 "<QEMU>/View/Zoom Out");
1962 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
1964 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
1966 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1967 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1968 "<QEMU>/View/Zoom Fixed");
1969 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
1971 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
1973 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1974 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
1976 separator
= gtk_separator_menu_item_new();
1977 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1979 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1980 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
1982 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1983 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1984 "<QEMU>/View/Grab Input");
1985 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
1987 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
1989 separator
= gtk_separator_menu_item_new();
1990 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1993 for (vc
= 0;; vc
++) {
1994 con
= qemu_console_lookup_by_index(vc
);
1998 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
1999 vc
, group
, view_menu
);
2003 #if defined(CONFIG_VTE)
2005 gd_vcs_init(s
, group
, view_menu
);
2008 separator
= gtk_separator_menu_item_new();
2009 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2011 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2012 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2014 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2015 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2020 static void gd_create_menus(GtkDisplayState
*s
)
2022 s
->accel_group
= gtk_accel_group_new();
2023 s
->machine_menu
= gd_create_menu_machine(s
);
2024 s
->view_menu
= gd_create_menu_view(s
);
2026 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2027 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2029 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2031 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2032 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2033 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2035 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2036 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2039 static void gd_set_keycode_type(GtkDisplayState
*s
)
2041 #ifdef GDK_WINDOWING_X11
2042 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
2043 if (GDK_IS_X11_DISPLAY(display
)) {
2044 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
2045 XkbDescPtr desc
= XkbGetKeyboard(x11_display
, XkbGBN_AllComponentsMask
,
2047 char *keycodes
= NULL
;
2049 if (desc
&& desc
->names
) {
2050 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
2052 if (keycodes
== NULL
) {
2053 fprintf(stderr
, "could not lookup keycode name\n");
2054 } else if (strstart(keycodes
, "evdev", NULL
)) {
2055 s
->has_evdev
= true;
2056 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
2057 fprintf(stderr
, "unknown keycodes `%s', please report to "
2058 "qemu-devel@nongnu.org\n", keycodes
);
2062 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
2071 static gboolean gtkinit
;
2073 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
2075 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2077 GdkDisplay
*window_display
;
2080 fprintf(stderr
, "gtk initialization failed\n");
2084 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2085 #if GTK_CHECK_VERSION(3, 2, 0)
2086 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2088 s
->vbox
= gtk_vbox_new(FALSE
, 0);
2090 s
->notebook
= gtk_notebook_new();
2091 s
->menu_bar
= gtk_menu_bar_new();
2093 s
->free_scale
= FALSE
;
2095 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2096 setlocale(LC_MESSAGES
, "");
2097 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2100 window_display
= gtk_widget_get_display(s
->window
);
2101 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2104 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2105 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2106 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2108 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
2110 GError
*error
= NULL
;
2111 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
2113 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
2115 g_error_free(error
);
2122 gd_connect_signals(s
);
2124 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2125 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2127 gd_update_caption(s
);
2129 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2130 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2132 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2134 gtk_widget_show_all(s
->window
);
2136 #ifdef VTE_RESIZE_HACK
2138 VirtualConsole
*cur
= gd_vc_find_current(s
);
2142 for (i
= 0; i
< s
->nb_vcs
; i
++) {
2143 VirtualConsole
*vc
= &s
->vc
[i
];
2144 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
2145 gtk_widget_hide(vc
->vte
.terminal
);
2148 gd_update_windowsize(cur
);
2154 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2156 if (grab_on_hover
) {
2157 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2160 gd_set_keycode_type(s
);
2163 void early_gtk_display_init(int opengl
)
2165 /* The QEMU code relies on the assumption that it's always run in
2166 * the C locale. Therefore it is not prepared to deal with
2167 * operations that produce different results depending on the
2168 * locale, such as printf's formatting of decimal numbers, and
2171 * Since GTK+ calls setlocale() by default -importing the locale
2172 * settings from the environment- we must prevent it from doing so
2173 * using gtk_disable_setlocale().
2175 * QEMU's GTK+ UI, however, _does_ have translations for some of
2176 * the menu items. As a trade-off between a functionally correct
2177 * QEMU and a fully internationalized UI we support importing
2178 * LC_MESSAGES from the environment (see the setlocale() call
2179 * earlier in this file). This allows us to display translated
2180 * messages leaving everything else untouched.
2182 gtk_disable_setlocale();
2183 gtkinit
= gtk_init_check(NULL
, NULL
);
2185 /* don't exit yet, that'll break -help */
2190 case -1: /* default */
2194 #if defined(CONFIG_OPENGL)
2195 #if defined(CONFIG_GTK_GL)
2203 g_assert_not_reached();
2207 #if defined(CONFIG_VTE)
2208 register_vc_handler(gd_vc_handler
);