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 "sysemu/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_WIN32_DISPLAY
94 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
98 #define GDK_KEY_0 GDK_0
99 #define GDK_KEY_1 GDK_1
100 #define GDK_KEY_2 GDK_2
101 #define GDK_KEY_f GDK_f
102 #define GDK_KEY_g GDK_g
103 #define GDK_KEY_q GDK_q
104 #define GDK_KEY_plus GDK_plus
105 #define GDK_KEY_minus GDK_minus
106 #define GDK_KEY_Pause GDK_Pause
109 /* Some older mingw versions lack this constant or have
110 * it conditionally defined */
112 # ifndef MAPVK_VK_TO_VSC
113 # define MAPVK_VK_TO_VSC 0
118 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
120 static const int modifier_keycode
[] = {
121 /* shift, control, alt keys, meta keys, both left & right */
122 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
125 struct GtkDisplayState
{
130 GtkAccelGroup
*accel_group
;
132 GtkWidget
*machine_menu_item
;
133 GtkWidget
*machine_menu
;
134 GtkWidget
*pause_item
;
135 GtkWidget
*reset_item
;
136 GtkWidget
*powerdown_item
;
137 GtkWidget
*quit_item
;
139 GtkWidget
*view_menu_item
;
140 GtkWidget
*view_menu
;
141 GtkWidget
*full_screen_item
;
142 GtkWidget
*zoom_in_item
;
143 GtkWidget
*zoom_out_item
;
144 GtkWidget
*zoom_fixed_item
;
145 GtkWidget
*zoom_fit_item
;
146 GtkWidget
*grab_item
;
147 GtkWidget
*grab_on_hover_item
;
150 VirtualConsole vc
[MAX_VCS
];
152 GtkWidget
*show_tabs_item
;
153 GtkWidget
*untabify_item
;
163 VirtualConsole
*kbd_owner
;
164 VirtualConsole
*ptr_owner
;
166 gboolean full_screen
;
168 GdkCursor
*null_cursor
;
169 Notifier mouse_mode_notifier
;
172 bool external_pause_update
;
174 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
179 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
180 static void gd_ungrab_pointer(GtkDisplayState
*s
);
181 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
182 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
184 /** Utility Functions **/
186 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
191 for (i
= 0; i
< s
->nb_vcs
; i
++) {
193 if (gtk_check_menu_item_get_active
194 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
201 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
206 for (i
= 0; i
< s
->nb_vcs
; i
++) {
208 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
216 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
220 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
221 return gd_vc_find_by_page(s
, page
);
224 static bool gd_is_grab_active(GtkDisplayState
*s
)
226 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
229 static bool gd_grab_on_hover(GtkDisplayState
*s
)
231 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
234 static void gd_update_cursor(VirtualConsole
*vc
)
236 GtkDisplayState
*s
= vc
->s
;
239 if (vc
->type
!= GD_VC_GFX
||
240 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
244 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
248 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
249 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
250 gdk_window_set_cursor(window
, s
->null_cursor
);
252 gdk_window_set_cursor(window
, NULL
);
256 static void gd_update_caption(GtkDisplayState
*s
)
258 const char *status
= "";
261 const char *grab
= "";
262 bool is_paused
= !runstate_is_running();
266 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
268 prefix
= g_strdup_printf("QEMU");
271 if (s
->ptr_owner
!= NULL
&&
272 s
->ptr_owner
->window
== NULL
) {
273 grab
= _(" - Press Ctrl+Alt+G to release grab");
277 status
= _(" [Paused]");
279 s
->external_pause_update
= true;
280 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
282 s
->external_pause_update
= false;
284 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
285 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
288 for (i
= 0; i
< s
->nb_vcs
; i
++) {
289 VirtualConsole
*vc
= &s
->vc
[i
];
294 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
295 vc
== s
->kbd_owner
? " +kbd" : "",
296 vc
== s
->ptr_owner
? " +ptr" : "");
297 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
304 static void gd_update_geometry_hints(VirtualConsole
*vc
)
306 GtkDisplayState
*s
= vc
->s
;
307 GdkWindowHints mask
= 0;
308 GdkGeometry geo
= {};
309 GtkWidget
*geo_widget
= NULL
;
310 GtkWindow
*geo_window
;
312 if (vc
->type
== GD_VC_GFX
) {
317 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
318 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
319 mask
|= GDK_HINT_MIN_SIZE
;
321 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
322 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
323 mask
|= GDK_HINT_MIN_SIZE
;
325 geo_widget
= vc
->gfx
.drawing_area
;
326 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
328 #if defined(CONFIG_VTE)
329 } else if (vc
->type
== GD_VC_VTE
) {
330 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
333 geo
.width_inc
= vte_terminal_get_char_width(term
);
334 geo
.height_inc
= vte_terminal_get_char_height(term
);
335 mask
|= GDK_HINT_RESIZE_INC
;
336 geo
.base_width
= geo
.width_inc
;
337 geo
.base_height
= geo
.height_inc
;
338 mask
|= GDK_HINT_BASE_SIZE
;
339 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
340 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
341 mask
|= GDK_HINT_MIN_SIZE
;
342 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
343 geo
.base_width
+= ib
->left
+ ib
->right
;
344 geo
.base_height
+= ib
->top
+ ib
->bottom
;
345 geo
.min_width
+= ib
->left
+ ib
->right
;
346 geo
.min_height
+= ib
->top
+ ib
->bottom
;
347 geo_widget
= vc
->vte
.terminal
;
351 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
352 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
355 void gd_update_windowsize(VirtualConsole
*vc
)
357 GtkDisplayState
*s
= vc
->s
;
359 gd_update_geometry_hints(vc
);
361 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
362 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
363 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
367 static void gd_update_full_redraw(VirtualConsole
*vc
)
369 GtkWidget
*area
= vc
->gfx
.drawing_area
;
371 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
372 #if defined(CONFIG_GTK_GL)
374 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
378 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
381 static void gtk_release_modifiers(GtkDisplayState
*s
)
383 VirtualConsole
*vc
= gd_vc_find_current(s
);
386 if (vc
->type
!= GD_VC_GFX
||
387 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
390 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
391 keycode
= modifier_keycode
[i
];
392 if (!s
->modifier_pressed
[i
]) {
395 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, keycode
, false);
396 s
->modifier_pressed
[i
] = false;
400 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
403 g_object_ref(G_OBJECT(widget
));
404 gtk_container_remove(GTK_CONTAINER(from
), widget
);
405 gtk_container_add(GTK_CONTAINER(to
), widget
);
406 g_object_unref(G_OBJECT(widget
));
409 /** DisplayState Callbacks **/
411 static void gd_update(DisplayChangeListener
*dcl
,
412 int x
, int y
, int w
, int h
)
414 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
421 trace_gd_update(vc
->label
, x
, y
, w
, h
);
423 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
427 if (vc
->gfx
.convert
) {
428 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
429 NULL
, vc
->gfx
.convert
,
430 x
, y
, 0, 0, x
, y
, w
, h
);
433 x1
= floor(x
* vc
->gfx
.scale_x
);
434 y1
= floor(y
* vc
->gfx
.scale_y
);
436 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
437 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
439 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
440 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
442 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
446 gdk_drawable_get_size(win
, &ww
, &wh
);
456 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
457 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
460 static void gd_refresh(DisplayChangeListener
*dcl
)
462 graphic_hw_update(dcl
->con
);
465 #if GTK_CHECK_VERSION(3, 0, 0)
466 static void gd_mouse_set(DisplayChangeListener
*dcl
,
467 int x
, int y
, int visible
)
469 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
471 GdkDeviceManager
*mgr
;
474 if (qemu_input_is_absolute()) {
478 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
479 mgr
= gdk_display_get_device_manager(dpy
);
480 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
481 x
, y
, &x_root
, &y_root
);
482 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
483 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
489 static void gd_mouse_set(DisplayChangeListener
*dcl
,
490 int x
, int y
, int visible
)
492 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
495 if (qemu_input_is_absolute()) {
499 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
500 x
, y
, &x_root
, &y_root
);
501 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
502 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
507 static void gd_cursor_define(DisplayChangeListener
*dcl
,
510 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
514 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
518 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
519 GDK_COLORSPACE_RGB
, true, 8,
520 c
->width
, c
->height
, c
->width
* 4,
522 cursor
= gdk_cursor_new_from_pixbuf
523 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
524 pixbuf
, c
->hot_x
, c
->hot_y
);
525 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
526 g_object_unref(pixbuf
);
527 #if !GTK_CHECK_VERSION(3, 0, 0)
528 gdk_cursor_unref(cursor
);
530 g_object_unref(cursor
);
534 static void gd_switch(DisplayChangeListener
*dcl
,
535 DisplaySurface
*surface
)
537 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
540 trace_gd_switch(vc
->label
,
541 surface
? surface_width(surface
) : 0,
542 surface
? surface_height(surface
) : 0);
544 if (vc
->gfx
.surface
) {
545 cairo_surface_destroy(vc
->gfx
.surface
);
546 vc
->gfx
.surface
= NULL
;
548 if (vc
->gfx
.convert
) {
549 pixman_image_unref(vc
->gfx
.convert
);
550 vc
->gfx
.convert
= NULL
;
553 if (vc
->gfx
.ds
&& surface
&&
554 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
555 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
558 vc
->gfx
.ds
= surface
;
564 if (surface
->format
== PIXMAN_x8r8g8b8
) {
566 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
568 * No need to convert, use surface directly. Should be the
569 * common case as this is qemu_default_pixelformat(32) too.
571 vc
->gfx
.surface
= cairo_image_surface_create_for_data
572 (surface_data(surface
),
574 surface_width(surface
),
575 surface_height(surface
),
576 surface_stride(surface
));
578 /* Must convert surface, use pixman to do it. */
579 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
580 surface_width(surface
),
581 surface_height(surface
),
583 vc
->gfx
.surface
= cairo_image_surface_create_for_data
584 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
586 pixman_image_get_width(vc
->gfx
.convert
),
587 pixman_image_get_height(vc
->gfx
.convert
),
588 pixman_image_get_stride(vc
->gfx
.convert
));
589 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
590 NULL
, vc
->gfx
.convert
,
592 pixman_image_get_width(vc
->gfx
.convert
),
593 pixman_image_get_height(vc
->gfx
.convert
));
597 gd_update_windowsize(vc
);
599 gd_update_full_redraw(vc
);
603 static const DisplayChangeListenerOps dcl_ops
= {
605 .dpy_gfx_update
= gd_update
,
606 .dpy_gfx_switch
= gd_switch
,
607 .dpy_gfx_check_format
= qemu_pixman_check_format
,
608 .dpy_refresh
= gd_refresh
,
609 .dpy_mouse_set
= gd_mouse_set
,
610 .dpy_cursor_define
= gd_cursor_define
,
614 #if defined(CONFIG_OPENGL)
616 /** DisplayState Callbacks (opengl version) **/
618 #if defined(CONFIG_GTK_GL)
620 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
621 .dpy_name
= "gtk-egl",
622 .dpy_gfx_update
= gd_gl_area_update
,
623 .dpy_gfx_switch
= gd_gl_area_switch
,
624 .dpy_gfx_check_format
= console_gl_check_format
,
625 .dpy_refresh
= gd_gl_area_refresh
,
626 .dpy_mouse_set
= gd_mouse_set
,
627 .dpy_cursor_define
= gd_cursor_define
,
629 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
630 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
631 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
632 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
633 .dpy_gl_scanout
= gd_gl_area_scanout
,
634 .dpy_gl_update
= gd_gl_area_scanout_flush
,
639 static const DisplayChangeListenerOps dcl_egl_ops
= {
640 .dpy_name
= "gtk-egl",
641 .dpy_gfx_update
= gd_egl_update
,
642 .dpy_gfx_switch
= gd_egl_switch
,
643 .dpy_gfx_check_format
= console_gl_check_format
,
644 .dpy_refresh
= gd_egl_refresh
,
645 .dpy_mouse_set
= gd_mouse_set
,
646 .dpy_cursor_define
= gd_cursor_define
,
648 .dpy_gl_ctx_create
= gd_egl_create_context
,
649 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
650 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
651 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
652 .dpy_gl_scanout
= gd_egl_scanout
,
653 .dpy_gl_update
= gd_egl_scanout_flush
,
656 #endif /* CONFIG_GTK_GL */
657 #endif /* CONFIG_OPENGL */
661 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
663 GtkDisplayState
*s
= opaque
;
665 gd_update_caption(s
);
668 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
673 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
674 /* release the grab at switching to absolute mode */
675 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
676 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
679 for (i
= 0; i
< s
->nb_vcs
; i
++) {
680 VirtualConsole
*vc
= &s
->vc
[i
];
681 gd_update_cursor(vc
);
687 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
690 GtkDisplayState
*s
= opaque
;
694 for (i
= 0; i
< s
->nb_vcs
; i
++) {
695 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
698 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
707 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
711 memset(&info
, 0, sizeof(info
));
713 info
.height
= height
;
714 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
717 #if defined(CONFIG_GTK_GL)
719 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
722 VirtualConsole
*vc
= opaque
;
730 static void gd_resize_event(GtkGLArea
*area
,
731 gint width
, gint height
, gpointer
*opaque
)
733 VirtualConsole
*vc
= (void *)opaque
;
735 gd_set_ui_info(vc
, width
, height
);
740 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
742 VirtualConsole
*vc
= opaque
;
743 GtkDisplayState
*s
= vc
->s
;
748 #if defined(CONFIG_OPENGL)
750 #if defined(CONFIG_GTK_GL)
751 /* invoke render callback please */
760 if (!gtk_widget_get_realized(widget
)) {
767 fbw
= surface_width(vc
->gfx
.ds
);
768 fbh
= surface_height(vc
->gfx
.ds
);
770 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
772 if (s
->full_screen
) {
773 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
774 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
775 } else if (s
->free_scale
) {
778 sx
= (double)ww
/ fbw
;
779 sy
= (double)wh
/ fbh
;
781 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
784 fbw
*= vc
->gfx
.scale_x
;
785 fbh
*= vc
->gfx
.scale_y
;
795 cairo_rectangle(cr
, 0, 0, ww
, wh
);
797 /* Optionally cut out the inner area where the pixmap
798 will be drawn. This avoids 'flashing' since we're
799 not double-buffering. Note we're using the undocumented
800 behaviour of drawing the rectangle from right to left
801 to cut out the whole */
802 cairo_rectangle(cr
, mx
+ fbw
, my
,
806 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
807 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
808 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
814 #if !GTK_CHECK_VERSION(3, 0, 0)
815 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
821 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
826 expose
->area
.height
);
829 ret
= gd_draw_event(widget
, cr
, opaque
);
837 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
840 VirtualConsole
*vc
= opaque
;
841 GtkDisplayState
*s
= vc
->s
;
851 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
852 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
854 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
865 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
866 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
868 if (qemu_input_is_absolute()) {
869 if (x
< 0 || y
< 0 ||
870 x
>= surface_width(vc
->gfx
.ds
) ||
871 y
>= surface_height(vc
->gfx
.ds
)) {
874 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
875 surface_width(vc
->gfx
.ds
));
876 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
877 surface_height(vc
->gfx
.ds
));
878 qemu_input_event_sync();
879 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
880 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
881 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
882 qemu_input_event_sync();
888 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
889 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
890 int x
= (int)motion
->x_root
;
891 int y
= (int)motion
->y_root
;
893 /* In relative mode check to see if client pointer hit
894 * one of the screen edges, and if so move it back by
895 * 200 pixels. This is important because the pointer
896 * in the server doesn't correspond 1-for-1, and so
897 * may still be only half way across the screen. Without
898 * this warp, the server pointer would thus appear to hit
899 * an invisible wall */
906 if (x
== (gdk_screen_get_width(screen
) - 1)) {
909 if (y
== (gdk_screen_get_height(screen
) - 1)) {
913 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
914 #if GTK_CHECK_VERSION(3, 0, 0)
915 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
916 gdk_device_warp(dev
, screen
, x
, y
);
918 GdkDisplay
*display
= gtk_widget_get_display(widget
);
919 gdk_display_warp_pointer(display
, screen
, x
, y
);
928 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
931 VirtualConsole
*vc
= opaque
;
932 GtkDisplayState
*s
= vc
->s
;
935 /* implicitly grab the input at the first click in the relative mode */
936 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
937 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
939 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
942 gd_grab_pointer(vc
, "relative-mode-click");
947 if (button
->button
== 1) {
948 btn
= INPUT_BUTTON_LEFT
;
949 } else if (button
->button
== 2) {
950 btn
= INPUT_BUTTON_MIDDLE
;
951 } else if (button
->button
== 3) {
952 btn
= INPUT_BUTTON_RIGHT
;
957 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
958 button
->type
== GDK_BUTTON_PRESS
);
959 qemu_input_event_sync();
963 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
966 VirtualConsole
*vc
= opaque
;
969 if (scroll
->direction
== GDK_SCROLL_UP
) {
970 btn
= INPUT_BUTTON_WHEEL_UP
;
971 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
972 btn
= INPUT_BUTTON_WHEEL_DOWN
;
977 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
978 qemu_input_event_sync();
979 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
980 qemu_input_event_sync();
984 static int gd_map_keycode(GtkDisplayState
*s
, GdkDisplay
*dpy
, int gdk_keycode
)
988 #ifdef GDK_WINDOWING_WIN32
989 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
990 qemu_keycode
= MapVirtualKey(gdk_keycode
, MAPVK_VK_TO_VSC
);
991 switch (qemu_keycode
) {
992 case 103: /* alt gr */
993 qemu_keycode
= 56 | SCANCODE_GREY
;
1000 if (gdk_keycode
< 9) {
1002 } else if (gdk_keycode
< 97) {
1003 qemu_keycode
= gdk_keycode
- 8;
1004 #ifdef GDK_WINDOWING_X11
1005 } else if (GDK_IS_X11_DISPLAY(dpy
) && gdk_keycode
< 158) {
1007 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
1009 qemu_keycode
= translate_xfree86_keycode(gdk_keycode
- 97);
1012 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
1013 qemu_keycode
= 0x70;
1014 } else if (gdk_keycode
== 211) { /* backslash */
1015 qemu_keycode
= 0x73;
1020 return qemu_keycode
;
1023 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1024 GdkEventKey
*key
, void *opaque
)
1026 VirtualConsole
*vc
= opaque
;
1027 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1030 kbd_put_string_console(con
, key
->string
, key
->length
);
1032 int num
= gd_map_keycode(vc
->s
, gtk_widget_get_display(widget
),
1033 key
->hardware_keycode
);
1034 int qcode
= qemu_input_key_number_to_qcode(num
);
1035 kbd_put_qcode_console(con
, qcode
);
1040 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1042 VirtualConsole
*vc
= opaque
;
1043 GtkDisplayState
*s
= vc
->s
;
1044 int gdk_keycode
= key
->hardware_keycode
;
1048 if (s
->ignore_keys
) {
1049 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1053 if (key
->keyval
== GDK_KEY_Pause
) {
1054 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
1055 key
->type
== GDK_KEY_PRESS
);
1059 qemu_keycode
= gd_map_keycode(s
, gtk_widget_get_display(widget
),
1062 trace_gd_key_event(vc
->label
, gdk_keycode
, qemu_keycode
,
1063 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1065 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
1066 if (qemu_keycode
== modifier_keycode
[i
]) {
1067 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
1071 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, qemu_keycode
,
1072 key
->type
== GDK_KEY_PRESS
);
1077 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1079 if (event
->type
== GDK_MOTION_NOTIFY
) {
1080 return gd_motion_event(widget
, &event
->motion
, opaque
);
1085 /** Window Menu Actions **/
1087 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1089 GtkDisplayState
*s
= opaque
;
1091 if (s
->external_pause_update
) {
1094 if (runstate_is_running()) {
1101 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1103 qmp_system_reset(NULL
);
1106 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1108 qmp_system_powerdown(NULL
);
1111 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1116 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1118 GtkDisplayState
*s
= opaque
;
1119 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1120 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1123 gtk_release_modifiers(s
);
1125 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1126 gtk_notebook_set_current_page(nb
, page
);
1127 gtk_widget_grab_focus(vc
->focus
);
1129 s
->ignore_keys
= false;
1132 static void gd_accel_switch_vc(void *opaque
)
1134 VirtualConsole
*vc
= opaque
;
1136 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1137 #if !GTK_CHECK_VERSION(3, 0, 0)
1138 /* GTK2 sends the accel key to the target console - ignore this until */
1139 vc
->s
->ignore_keys
= true;
1143 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1145 GtkDisplayState
*s
= opaque
;
1146 VirtualConsole
*vc
= gd_vc_find_current(s
);
1148 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1149 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1151 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1153 gd_update_windowsize(vc
);
1156 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1159 VirtualConsole
*vc
= opaque
;
1160 GtkDisplayState
*s
= vc
->s
;
1162 gtk_widget_set_sensitive(vc
->menu_item
, true);
1163 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1164 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1165 vc
->tab_item
, vc
->label
);
1166 gtk_widget_destroy(vc
->window
);
1171 static gboolean
gd_win_grab(void *opaque
)
1173 VirtualConsole
*vc
= opaque
;
1175 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1176 if (vc
->s
->ptr_owner
) {
1177 gd_ungrab_pointer(vc
->s
);
1179 gd_grab_pointer(vc
, "user-request-detached-tab");
1184 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1186 GtkDisplayState
*s
= opaque
;
1187 VirtualConsole
*vc
= gd_vc_find_current(s
);
1189 if (vc
->type
== GD_VC_GFX
&&
1190 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1191 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1195 gtk_widget_set_sensitive(vc
->menu_item
, false);
1196 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1197 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1199 g_signal_connect(vc
->window
, "delete-event",
1200 G_CALLBACK(gd_tab_window_close
), vc
);
1201 gtk_widget_show_all(vc
->window
);
1203 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1204 GtkAccelGroup
*ag
= gtk_accel_group_new();
1205 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1207 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1209 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1212 gd_update_geometry_hints(vc
);
1213 gd_update_caption(s
);
1217 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1219 GtkDisplayState
*s
= opaque
;
1220 VirtualConsole
*vc
= gd_vc_find_current(s
);
1222 if (!s
->full_screen
) {
1223 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1224 gtk_widget_hide(s
->menu_bar
);
1225 if (vc
->type
== GD_VC_GFX
) {
1226 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1228 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1229 s
->full_screen
= TRUE
;
1231 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1232 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1233 gtk_widget_show(s
->menu_bar
);
1234 s
->full_screen
= FALSE
;
1235 if (vc
->type
== GD_VC_GFX
) {
1236 vc
->gfx
.scale_x
= 1.0;
1237 vc
->gfx
.scale_y
= 1.0;
1238 gd_update_windowsize(vc
);
1242 gd_update_cursor(vc
);
1245 static void gd_accel_full_screen(void *opaque
)
1247 GtkDisplayState
*s
= opaque
;
1248 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1251 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1253 GtkDisplayState
*s
= opaque
;
1254 VirtualConsole
*vc
= gd_vc_find_current(s
);
1256 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1259 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1260 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1262 gd_update_windowsize(vc
);
1265 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1267 GtkDisplayState
*s
= opaque
;
1268 VirtualConsole
*vc
= gd_vc_find_current(s
);
1270 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1273 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1274 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1276 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1277 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1279 gd_update_windowsize(vc
);
1282 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1284 GtkDisplayState
*s
= opaque
;
1285 VirtualConsole
*vc
= gd_vc_find_current(s
);
1287 vc
->gfx
.scale_x
= 1.0;
1288 vc
->gfx
.scale_y
= 1.0;
1290 gd_update_windowsize(vc
);
1293 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1295 GtkDisplayState
*s
= opaque
;
1296 VirtualConsole
*vc
= gd_vc_find_current(s
);
1298 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1299 s
->free_scale
= TRUE
;
1301 s
->free_scale
= FALSE
;
1302 vc
->gfx
.scale_x
= 1.0;
1303 vc
->gfx
.scale_y
= 1.0;
1306 gd_update_windowsize(vc
);
1307 gd_update_full_redraw(vc
);
1310 #if GTK_CHECK_VERSION(3, 0, 0)
1311 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1312 GdkInputSource source
, GdkEventMask mask
,
1315 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1316 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1317 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1320 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1321 GdkDevice
*dev
= tmp
->data
;
1322 if (gdk_device_get_source(dev
) != source
) {
1326 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1327 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1328 mask
, cursor
, GDK_CURRENT_TIME
);
1330 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1337 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1339 if (vc
->s
->kbd_owner
) {
1340 if (vc
->s
->kbd_owner
== vc
) {
1343 gd_ungrab_keyboard(vc
->s
);
1347 #if GTK_CHECK_VERSION(3, 0, 0)
1348 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1349 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1352 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1356 vc
->s
->kbd_owner
= vc
;
1357 gd_update_caption(vc
->s
);
1358 trace_gd_grab(vc
->label
, "kbd", reason
);
1361 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1363 VirtualConsole
*vc
= s
->kbd_owner
;
1368 s
->kbd_owner
= NULL
;
1370 #if GTK_CHECK_VERSION(3, 0, 0)
1371 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1373 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1375 gd_update_caption(s
);
1376 trace_gd_ungrab(vc
->label
, "kbd");
1379 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1381 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1383 if (vc
->s
->ptr_owner
) {
1384 if (vc
->s
->ptr_owner
== vc
) {
1387 gd_ungrab_pointer(vc
->s
);
1391 #if GTK_CHECK_VERSION(3, 0, 0)
1392 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1393 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1394 GDK_POINTER_MOTION_MASK
|
1395 GDK_BUTTON_PRESS_MASK
|
1396 GDK_BUTTON_RELEASE_MASK
|
1397 GDK_BUTTON_MOTION_MASK
|
1399 vc
->s
->null_cursor
);
1400 gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr
),
1401 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1403 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1404 FALSE
, /* All events to come to our window directly */
1405 GDK_POINTER_MOTION_MASK
|
1406 GDK_BUTTON_PRESS_MASK
|
1407 GDK_BUTTON_RELEASE_MASK
|
1408 GDK_BUTTON_MOTION_MASK
|
1410 NULL
, /* Allow cursor to move over entire desktop */
1413 gdk_display_get_pointer(display
, NULL
,
1414 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1416 vc
->s
->ptr_owner
= vc
;
1417 gd_update_caption(vc
->s
);
1418 trace_gd_grab(vc
->label
, "ptr", reason
);
1421 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1423 VirtualConsole
*vc
= s
->ptr_owner
;
1428 s
->ptr_owner
= NULL
;
1430 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1431 #if GTK_CHECK_VERSION(3, 0, 0)
1432 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1433 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1434 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr
),
1435 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1436 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1438 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1439 gdk_display_warp_pointer(display
,
1440 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1441 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1443 gd_update_caption(s
);
1444 trace_gd_ungrab(vc
->label
, "ptr");
1447 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1449 GtkDisplayState
*s
= opaque
;
1450 VirtualConsole
*vc
= gd_vc_find_current(s
);
1452 if (gd_is_grab_active(s
)) {
1453 gd_grab_keyboard(vc
, "user-request-main-window");
1454 gd_grab_pointer(vc
, "user-request-main-window");
1456 gd_ungrab_keyboard(s
);
1457 gd_ungrab_pointer(s
);
1460 gd_update_cursor(vc
);
1463 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1466 GtkDisplayState
*s
= data
;
1470 if (!gtk_widget_get_realized(s
->notebook
)) {
1474 #ifdef VTE_RESIZE_HACK
1475 vc
= gd_vc_find_current(s
);
1476 if (vc
&& vc
->type
== GD_VC_VTE
) {
1477 gtk_widget_hide(vc
->vte
.terminal
);
1480 vc
= gd_vc_find_by_page(s
, arg2
);
1484 #ifdef VTE_RESIZE_HACK
1485 if (vc
->type
== GD_VC_VTE
) {
1486 gtk_widget_show(vc
->vte
.terminal
);
1489 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1491 on_vga
= (vc
->type
== GD_VC_GFX
&&
1492 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1494 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1496 } else if (s
->full_screen
) {
1497 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1500 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1502 gd_update_windowsize(vc
);
1503 gd_update_cursor(vc
);
1506 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1509 VirtualConsole
*vc
= opaque
;
1510 GtkDisplayState
*s
= vc
->s
;
1512 if (gd_grab_on_hover(s
)) {
1513 gd_grab_keyboard(vc
, "grab-on-hover");
1518 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1521 VirtualConsole
*vc
= opaque
;
1522 GtkDisplayState
*s
= vc
->s
;
1524 if (gd_grab_on_hover(s
)) {
1525 gd_ungrab_keyboard(s
);
1530 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1531 GdkEventCrossing
*crossing
, gpointer opaque
)
1533 VirtualConsole
*vc
= opaque
;
1534 GtkDisplayState
*s
= vc
->s
;
1536 gtk_release_modifiers(s
);
1540 static gboolean
gd_configure(GtkWidget
*widget
,
1541 GdkEventConfigure
*cfg
, gpointer opaque
)
1543 VirtualConsole
*vc
= opaque
;
1545 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1549 /** Virtual Console Callbacks **/
1551 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1552 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1554 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1555 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1556 HOTKEY_MODIFIERS
, 0,
1557 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1558 #if GTK_CHECK_VERSION(3, 8, 0)
1559 gtk_accel_label_set_accel(
1560 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1561 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1564 g_signal_connect(vc
->menu_item
, "activate",
1565 G_CALLBACK(gd_menu_switch_vc
), s
);
1566 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1568 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1572 #if defined(CONFIG_VTE)
1573 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1575 VirtualConsole
*vc
= opaque
;
1577 if (gtk_adjustment_get_upper(adjustment
) >
1578 gtk_adjustment_get_page_size(adjustment
)) {
1579 gtk_widget_show(vc
->vte
.scrollbar
);
1581 gtk_widget_hide(vc
->vte
.scrollbar
);
1585 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1587 VirtualConsole
*vc
= chr
->opaque
;
1589 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1593 static void gd_vc_chr_set_echo(CharDriverState
*chr
, bool echo
)
1595 VirtualConsole
*vc
= chr
->opaque
;
1597 vc
->vte
.echo
= echo
;
1601 static CharDriverState
*vcs
[MAX_VCS
];
1603 static CharDriverState
*gd_vc_handler(ChardevVC
*vc
, Error
**errp
)
1605 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
1606 CharDriverState
*chr
;
1608 chr
= qemu_chr_alloc(common
, errp
);
1613 chr
->chr_write
= gd_vc_chr_write
;
1614 chr
->chr_set_echo
= gd_vc_chr_set_echo
;
1616 /* Temporary, until gd_vc_vte_init runs. */
1617 chr
->opaque
= g_new0(VirtualConsole
, 1);
1619 /* defer OPENED events until our vc is fully initialized */
1620 chr
->explicit_be_open
= true;
1622 vcs
[nb_vcs
++] = chr
;
1627 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1630 VirtualConsole
*vc
= user_data
;
1633 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1635 for (i
= 0; i
< size
; i
++) {
1636 uint8_t c
= text
[i
];
1637 if (c
>= 128 || isprint(c
)) {
1638 /* 8-bit characters are considered printable. */
1639 vte_terminal_feed(term
, &text
[i
], 1);
1640 } else if (c
== '\r' || c
== '\n') {
1641 vte_terminal_feed(term
, "\r\n", 2);
1643 char ctrl
[2] = { '^', 0};
1644 ctrl
[1] = text
[i
] ^ 64;
1645 vte_terminal_feed(term
, ctrl
, 2);
1650 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1654 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1655 CharDriverState
*chr
, int idx
,
1656 GSList
*group
, GtkWidget
*view_menu
)
1660 GtkWidget
*scrollbar
;
1661 GtkAdjustment
*vadjustment
;
1662 VirtualConsole
*tmp_vc
= chr
->opaque
;
1665 vc
->vte
.echo
= tmp_vc
->vte
.echo
;
1671 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1672 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1673 ? vc
->vte
.chr
->label
: buffer
);
1674 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1676 vc
->vte
.terminal
= vte_terminal_new();
1677 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1679 /* The documentation says that the default is UTF-8, but actually it is
1680 * 7-bit ASCII at least in VTE 0.38.
1682 #if VTE_CHECK_VERSION(0, 40, 0)
1683 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1685 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1688 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1689 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1690 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1692 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1693 vadjustment
= gtk_scrollable_get_vadjustment
1694 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1696 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1699 #if GTK_CHECK_VERSION(3, 0, 0)
1700 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1701 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1703 box
= gtk_hbox_new(false, 2);
1704 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1707 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1708 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1711 vc
->vte
.scrollbar
= scrollbar
;
1713 g_signal_connect(vadjustment
, "changed",
1714 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1716 vc
->type
= GD_VC_VTE
;
1718 vc
->focus
= vc
->vte
.terminal
;
1719 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1720 gtk_label_new(vc
->label
));
1722 qemu_chr_be_generic_open(vc
->vte
.chr
);
1723 if (vc
->vte
.chr
->init
) {
1724 vc
->vte
.chr
->init(vc
->vte
.chr
);
1730 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1731 GtkWidget
*view_menu
)
1735 for (i
= 0; i
< nb_vcs
; i
++) {
1736 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1737 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1741 #endif /* CONFIG_VTE */
1743 /** Window Creation **/
1745 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1747 #if GTK_CHECK_VERSION(3, 0, 0)
1748 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1749 G_CALLBACK(gd_draw_event
), vc
);
1750 #if defined(CONFIG_GTK_GL)
1751 if (display_opengl
) {
1752 /* wire up GtkGlArea events */
1753 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1754 G_CALLBACK(gd_render_event
), vc
);
1755 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1756 G_CALLBACK(gd_resize_event
), vc
);
1760 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1761 G_CALLBACK(gd_expose_event
), vc
);
1763 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1764 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1765 G_CALLBACK(gd_event
), vc
);
1766 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1767 G_CALLBACK(gd_button_event
), vc
);
1768 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1769 G_CALLBACK(gd_button_event
), vc
);
1770 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1771 G_CALLBACK(gd_scroll_event
), vc
);
1772 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1773 G_CALLBACK(gd_key_event
), vc
);
1774 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1775 G_CALLBACK(gd_key_event
), vc
);
1777 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1778 G_CALLBACK(gd_enter_event
), vc
);
1779 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1780 G_CALLBACK(gd_leave_event
), vc
);
1781 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1782 G_CALLBACK(gd_focus_out_event
), vc
);
1783 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1784 G_CALLBACK(gd_configure
), vc
);
1786 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1787 G_CALLBACK(gd_text_key_down
), vc
);
1791 static void gd_connect_signals(GtkDisplayState
*s
)
1793 g_signal_connect(s
->show_tabs_item
, "activate",
1794 G_CALLBACK(gd_menu_show_tabs
), s
);
1795 g_signal_connect(s
->untabify_item
, "activate",
1796 G_CALLBACK(gd_menu_untabify
), s
);
1798 g_signal_connect(s
->window
, "delete-event",
1799 G_CALLBACK(gd_window_close
), s
);
1801 g_signal_connect(s
->pause_item
, "activate",
1802 G_CALLBACK(gd_menu_pause
), s
);
1803 g_signal_connect(s
->reset_item
, "activate",
1804 G_CALLBACK(gd_menu_reset
), s
);
1805 g_signal_connect(s
->powerdown_item
, "activate",
1806 G_CALLBACK(gd_menu_powerdown
), s
);
1807 g_signal_connect(s
->quit_item
, "activate",
1808 G_CALLBACK(gd_menu_quit
), s
);
1809 g_signal_connect(s
->full_screen_item
, "activate",
1810 G_CALLBACK(gd_menu_full_screen
), s
);
1811 g_signal_connect(s
->zoom_in_item
, "activate",
1812 G_CALLBACK(gd_menu_zoom_in
), s
);
1813 g_signal_connect(s
->zoom_out_item
, "activate",
1814 G_CALLBACK(gd_menu_zoom_out
), s
);
1815 g_signal_connect(s
->zoom_fixed_item
, "activate",
1816 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1817 g_signal_connect(s
->zoom_fit_item
, "activate",
1818 G_CALLBACK(gd_menu_zoom_fit
), s
);
1819 g_signal_connect(s
->grab_item
, "activate",
1820 G_CALLBACK(gd_menu_grab_input
), s
);
1821 g_signal_connect(s
->notebook
, "switch-page",
1822 G_CALLBACK(gd_change_page
), s
);
1825 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1827 GtkWidget
*machine_menu
;
1828 GtkWidget
*separator
;
1830 machine_menu
= gtk_menu_new();
1831 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1833 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1834 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1836 separator
= gtk_separator_menu_item_new();
1837 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1839 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1840 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1842 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1843 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1845 separator
= gtk_separator_menu_item_new();
1846 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1848 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1849 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1850 "<QEMU>/Machine/Quit");
1851 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1852 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1853 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1855 return machine_menu
;
1858 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1859 QemuConsole
*con
, int idx
,
1860 GSList
*group
, GtkWidget
*view_menu
)
1862 vc
->label
= qemu_console_get_label(con
);
1864 vc
->gfx
.scale_x
= 1.0;
1865 vc
->gfx
.scale_y
= 1.0;
1867 #if defined(CONFIG_OPENGL)
1868 if (display_opengl
) {
1869 #if defined(CONFIG_GTK_GL)
1870 vc
->gfx
.drawing_area
= gtk_gl_area_new();
1871 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
1873 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1875 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1876 * It is required for opengl rendering on X11 though. A
1877 * proper replacement (native opengl support) is only
1878 * available in 3.16+. Silence the warning if possible.
1880 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1881 #pragma GCC diagnostic push
1882 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1884 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
1885 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1886 #pragma GCC diagnostic pop
1888 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
1889 #endif /* CONFIG_GTK_GL */
1893 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1894 vc
->gfx
.dcl
.ops
= &dcl_ops
;
1898 gtk_widget_add_events(vc
->gfx
.drawing_area
,
1899 GDK_POINTER_MOTION_MASK
|
1900 GDK_BUTTON_PRESS_MASK
|
1901 GDK_BUTTON_RELEASE_MASK
|
1902 GDK_BUTTON_MOTION_MASK
|
1903 GDK_ENTER_NOTIFY_MASK
|
1904 GDK_LEAVE_NOTIFY_MASK
|
1906 GDK_KEY_PRESS_MASK
);
1907 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
1909 vc
->type
= GD_VC_GFX
;
1910 vc
->tab_item
= vc
->gfx
.drawing_area
;
1911 vc
->focus
= vc
->gfx
.drawing_area
;
1912 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
1913 vc
->tab_item
, gtk_label_new(vc
->label
));
1915 vc
->gfx
.dcl
.con
= con
;
1916 register_displaychangelistener(&vc
->gfx
.dcl
);
1918 gd_connect_vc_gfx_signals(vc
);
1919 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1921 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
1922 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
1923 s
->free_scale
= true;
1929 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
1931 GSList
*group
= NULL
;
1932 GtkWidget
*view_menu
;
1933 GtkWidget
*separator
;
1937 view_menu
= gtk_menu_new();
1938 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
1940 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1942 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
1943 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
1944 #if GTK_CHECK_VERSION(3, 8, 0)
1945 gtk_accel_label_set_accel(
1946 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
1947 GDK_KEY_f
, HOTKEY_MODIFIERS
);
1949 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
1951 separator
= gtk_separator_menu_item_new();
1952 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1954 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1955 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1956 "<QEMU>/View/Zoom In");
1957 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
1959 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
1961 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1962 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1963 "<QEMU>/View/Zoom Out");
1964 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
1966 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
1968 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1969 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1970 "<QEMU>/View/Zoom Fixed");
1971 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
1973 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
1975 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1976 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
1978 separator
= gtk_separator_menu_item_new();
1979 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1981 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1982 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
1984 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1985 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1986 "<QEMU>/View/Grab Input");
1987 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
1989 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
1991 separator
= gtk_separator_menu_item_new();
1992 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
1995 for (vc
= 0;; vc
++) {
1996 con
= qemu_console_lookup_by_index(vc
);
2000 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2001 vc
, group
, view_menu
);
2005 #if defined(CONFIG_VTE)
2007 gd_vcs_init(s
, group
, view_menu
);
2010 separator
= gtk_separator_menu_item_new();
2011 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2013 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2014 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2016 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2017 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2022 static void gd_create_menus(GtkDisplayState
*s
)
2024 s
->accel_group
= gtk_accel_group_new();
2025 s
->machine_menu
= gd_create_menu_machine(s
);
2026 s
->view_menu
= gd_create_menu_view(s
);
2028 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2029 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2031 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2033 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2034 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2035 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2037 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2038 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2041 static void gd_set_keycode_type(GtkDisplayState
*s
)
2043 #ifdef GDK_WINDOWING_X11
2044 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
2045 if (GDK_IS_X11_DISPLAY(display
)) {
2046 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
2047 XkbDescPtr desc
= XkbGetKeyboard(x11_display
, XkbGBN_AllComponentsMask
,
2049 char *keycodes
= NULL
;
2051 if (desc
&& desc
->names
) {
2052 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
2054 if (keycodes
== NULL
) {
2055 fprintf(stderr
, "could not lookup keycode name\n");
2056 } else if (strstart(keycodes
, "evdev", NULL
)) {
2057 s
->has_evdev
= true;
2058 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
2059 fprintf(stderr
, "unknown keycodes `%s', please report to "
2060 "qemu-devel@nongnu.org\n", keycodes
);
2064 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
2073 static gboolean gtkinit
;
2075 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
2077 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2079 GdkDisplay
*window_display
;
2082 fprintf(stderr
, "gtk initialization failed\n");
2086 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2087 #if GTK_CHECK_VERSION(3, 2, 0)
2088 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2090 s
->vbox
= gtk_vbox_new(FALSE
, 0);
2092 s
->notebook
= gtk_notebook_new();
2093 s
->menu_bar
= gtk_menu_bar_new();
2095 s
->free_scale
= FALSE
;
2097 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2098 setlocale(LC_MESSAGES
, "");
2099 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2102 window_display
= gtk_widget_get_display(s
->window
);
2103 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2106 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2107 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2108 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2110 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
2112 GError
*error
= NULL
;
2113 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
2115 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
2117 g_error_free(error
);
2124 gd_connect_signals(s
);
2126 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2127 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2129 gd_update_caption(s
);
2131 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2132 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2134 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2136 gtk_widget_show_all(s
->window
);
2138 #ifdef VTE_RESIZE_HACK
2140 VirtualConsole
*cur
= gd_vc_find_current(s
);
2144 for (i
= 0; i
< s
->nb_vcs
; i
++) {
2145 VirtualConsole
*vc
= &s
->vc
[i
];
2146 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
2147 gtk_widget_hide(vc
->vte
.terminal
);
2150 gd_update_windowsize(cur
);
2156 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2158 if (grab_on_hover
) {
2159 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2162 gd_set_keycode_type(s
);
2165 void early_gtk_display_init(int opengl
)
2167 /* The QEMU code relies on the assumption that it's always run in
2168 * the C locale. Therefore it is not prepared to deal with
2169 * operations that produce different results depending on the
2170 * locale, such as printf's formatting of decimal numbers, and
2173 * Since GTK+ calls setlocale() by default -importing the locale
2174 * settings from the environment- we must prevent it from doing so
2175 * using gtk_disable_setlocale().
2177 * QEMU's GTK+ UI, however, _does_ have translations for some of
2178 * the menu items. As a trade-off between a functionally correct
2179 * QEMU and a fully internationalized UI we support importing
2180 * LC_MESSAGES from the environment (see the setlocale() call
2181 * earlier in this file). This allows us to display translated
2182 * messages leaving everything else untouched.
2184 gtk_disable_setlocale();
2185 gtkinit
= gtk_init_check(NULL
, NULL
);
2187 /* don't exit yet, that'll break -help */
2192 case -1: /* default */
2196 #if defined(CONFIG_OPENGL)
2197 #if defined(CONFIG_GTK_GL)
2205 g_assert_not_reached();
2209 #if defined(CONFIG_VTE)
2210 register_vc_handler(gd_vc_handler
);