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
*copy_item
;
143 GtkWidget
*zoom_in_item
;
144 GtkWidget
*zoom_out_item
;
145 GtkWidget
*zoom_fixed_item
;
146 GtkWidget
*zoom_fit_item
;
147 GtkWidget
*grab_item
;
148 GtkWidget
*grab_on_hover_item
;
151 VirtualConsole vc
[MAX_VCS
];
153 GtkWidget
*show_tabs_item
;
154 GtkWidget
*untabify_item
;
164 VirtualConsole
*kbd_owner
;
165 VirtualConsole
*ptr_owner
;
167 gboolean full_screen
;
169 GdkCursor
*null_cursor
;
170 Notifier mouse_mode_notifier
;
173 bool external_pause_update
;
175 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
180 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
);
181 static void gd_ungrab_pointer(GtkDisplayState
*s
);
182 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
);
183 static void gd_ungrab_keyboard(GtkDisplayState
*s
);
185 /** Utility Functions **/
187 static VirtualConsole
*gd_vc_find_by_menu(GtkDisplayState
*s
)
192 for (i
= 0; i
< s
->nb_vcs
; i
++) {
194 if (gtk_check_menu_item_get_active
195 (GTK_CHECK_MENU_ITEM(vc
->menu_item
))) {
202 static VirtualConsole
*gd_vc_find_by_page(GtkDisplayState
*s
, gint page
)
207 for (i
= 0; i
< s
->nb_vcs
; i
++) {
209 p
= gtk_notebook_page_num(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
);
217 static VirtualConsole
*gd_vc_find_current(GtkDisplayState
*s
)
221 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
));
222 return gd_vc_find_by_page(s
, page
);
225 static bool gd_is_grab_active(GtkDisplayState
*s
)
227 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
230 static bool gd_grab_on_hover(GtkDisplayState
*s
)
232 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
235 static void gd_update_cursor(VirtualConsole
*vc
)
237 GtkDisplayState
*s
= vc
->s
;
240 if (vc
->type
!= GD_VC_GFX
||
241 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
245 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
249 window
= gtk_widget_get_window(GTK_WIDGET(vc
->gfx
.drawing_area
));
250 if (s
->full_screen
|| qemu_input_is_absolute() || s
->ptr_owner
== vc
) {
251 gdk_window_set_cursor(window
, s
->null_cursor
);
253 gdk_window_set_cursor(window
, NULL
);
257 static void gd_update_caption(GtkDisplayState
*s
)
259 const char *status
= "";
262 const char *grab
= "";
263 bool is_paused
= !runstate_is_running();
267 prefix
= g_strdup_printf("QEMU (%s)", qemu_name
);
269 prefix
= g_strdup_printf("QEMU");
272 if (s
->ptr_owner
!= NULL
&&
273 s
->ptr_owner
->window
== NULL
) {
274 grab
= _(" - Press Ctrl+Alt+G to release grab");
278 status
= _(" [Paused]");
280 s
->external_pause_update
= true;
281 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
283 s
->external_pause_update
= false;
285 title
= g_strdup_printf("%s%s%s", prefix
, status
, grab
);
286 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
289 for (i
= 0; i
< s
->nb_vcs
; i
++) {
290 VirtualConsole
*vc
= &s
->vc
[i
];
295 title
= g_strdup_printf("%s: %s%s%s", prefix
, vc
->label
,
296 vc
== s
->kbd_owner
? " +kbd" : "",
297 vc
== s
->ptr_owner
? " +ptr" : "");
298 gtk_window_set_title(GTK_WINDOW(vc
->window
), title
);
305 static void gd_update_geometry_hints(VirtualConsole
*vc
)
307 GtkDisplayState
*s
= vc
->s
;
308 GdkWindowHints mask
= 0;
309 GdkGeometry geo
= {};
310 GtkWidget
*geo_widget
= NULL
;
311 GtkWindow
*geo_window
;
313 if (vc
->type
== GD_VC_GFX
) {
318 geo
.min_width
= surface_width(vc
->gfx
.ds
) * VC_SCALE_MIN
;
319 geo
.min_height
= surface_height(vc
->gfx
.ds
) * VC_SCALE_MIN
;
320 mask
|= GDK_HINT_MIN_SIZE
;
322 geo
.min_width
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
323 geo
.min_height
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
324 mask
|= GDK_HINT_MIN_SIZE
;
326 geo_widget
= vc
->gfx
.drawing_area
;
327 gtk_widget_set_size_request(geo_widget
, geo
.min_width
, geo
.min_height
);
329 #if defined(CONFIG_VTE)
330 } else if (vc
->type
== GD_VC_VTE
) {
331 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
332 GtkBorder padding
= { 0 };
334 #if VTE_CHECK_VERSION(0, 37, 0)
335 gtk_style_context_get_padding(
336 gtk_widget_get_style_context(vc
->vte
.terminal
),
337 gtk_widget_get_state_flags(vc
->vte
.terminal
),
341 GtkBorder
*ib
= NULL
;
342 gtk_widget_style_get(vc
->vte
.terminal
, "inner-border", &ib
, NULL
);
350 geo
.width_inc
= vte_terminal_get_char_width(term
);
351 geo
.height_inc
= vte_terminal_get_char_height(term
);
352 mask
|= GDK_HINT_RESIZE_INC
;
353 geo
.base_width
= geo
.width_inc
;
354 geo
.base_height
= geo
.height_inc
;
355 mask
|= GDK_HINT_BASE_SIZE
;
356 geo
.min_width
= geo
.width_inc
* VC_TERM_X_MIN
;
357 geo
.min_height
= geo
.height_inc
* VC_TERM_Y_MIN
;
358 mask
|= GDK_HINT_MIN_SIZE
;
360 geo
.base_width
+= padding
.left
+ padding
.right
;
361 geo
.base_height
+= padding
.top
+ padding
.bottom
;
362 geo
.min_width
+= padding
.left
+ padding
.right
;
363 geo
.min_height
+= padding
.top
+ padding
.bottom
;
364 geo_widget
= vc
->vte
.terminal
;
368 geo_window
= GTK_WINDOW(vc
->window
? vc
->window
: s
->window
);
369 gtk_window_set_geometry_hints(geo_window
, geo_widget
, &geo
, mask
);
372 void gd_update_windowsize(VirtualConsole
*vc
)
374 GtkDisplayState
*s
= vc
->s
;
376 gd_update_geometry_hints(vc
);
378 if (vc
->type
== GD_VC_GFX
&& !s
->full_screen
&& !s
->free_scale
) {
379 gtk_window_resize(GTK_WINDOW(vc
->window
? vc
->window
: s
->window
),
380 VC_WINDOW_X_MIN
, VC_WINDOW_Y_MIN
);
384 static void gd_update_full_redraw(VirtualConsole
*vc
)
386 GtkWidget
*area
= vc
->gfx
.drawing_area
;
388 gdk_drawable_get_size(gtk_widget_get_window(area
), &ww
, &wh
);
389 #if defined(CONFIG_GTK_GL)
391 gtk_gl_area_queue_render(GTK_GL_AREA(vc
->gfx
.drawing_area
));
395 gtk_widget_queue_draw_area(area
, 0, 0, ww
, wh
);
398 static void gtk_release_modifiers(GtkDisplayState
*s
)
400 VirtualConsole
*vc
= gd_vc_find_current(s
);
403 if (vc
->type
!= GD_VC_GFX
||
404 !qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
407 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
408 keycode
= modifier_keycode
[i
];
409 if (!s
->modifier_pressed
[i
]) {
412 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, keycode
, false);
413 s
->modifier_pressed
[i
] = false;
417 static void gd_widget_reparent(GtkWidget
*from
, GtkWidget
*to
,
420 g_object_ref(G_OBJECT(widget
));
421 gtk_container_remove(GTK_CONTAINER(from
), widget
);
422 gtk_container_add(GTK_CONTAINER(to
), widget
);
423 g_object_unref(G_OBJECT(widget
));
426 /** DisplayState Callbacks **/
428 static void gd_update(DisplayChangeListener
*dcl
,
429 int x
, int y
, int w
, int h
)
431 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
438 trace_gd_update(vc
->label
, x
, y
, w
, h
);
440 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
444 if (vc
->gfx
.convert
) {
445 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
446 NULL
, vc
->gfx
.convert
,
447 x
, y
, 0, 0, x
, y
, w
, h
);
450 x1
= floor(x
* vc
->gfx
.scale_x
);
451 y1
= floor(y
* vc
->gfx
.scale_y
);
453 x2
= ceil(x
* vc
->gfx
.scale_x
+ w
* vc
->gfx
.scale_x
);
454 y2
= ceil(y
* vc
->gfx
.scale_y
+ h
* vc
->gfx
.scale_y
);
456 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
457 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
459 win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
463 gdk_drawable_get_size(win
, &ww
, &wh
);
473 gtk_widget_queue_draw_area(vc
->gfx
.drawing_area
,
474 mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
477 static void gd_refresh(DisplayChangeListener
*dcl
)
479 graphic_hw_update(dcl
->con
);
482 #if GTK_CHECK_VERSION(3, 0, 0)
483 static GdkDevice
*gd_get_pointer(GdkDisplay
*dpy
)
485 #if GTK_CHECK_VERSION(3, 20, 0)
486 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy
));
488 return gdk_device_manager_get_client_pointer(
489 gdk_display_get_device_manager(dpy
));
493 static void gd_mouse_set(DisplayChangeListener
*dcl
,
494 int x
, int y
, int visible
)
496 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
500 if (qemu_input_is_absolute()) {
504 dpy
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
505 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
506 x
, y
, &x_root
, &y_root
);
507 gdk_device_warp(gd_get_pointer(dpy
),
508 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
514 static void gd_mouse_set(DisplayChangeListener
*dcl
,
515 int x
, int y
, int visible
)
517 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
520 if (qemu_input_is_absolute()) {
524 gdk_window_get_root_coords(gtk_widget_get_window(vc
->gfx
.drawing_area
),
525 x
, y
, &x_root
, &y_root
);
526 gdk_display_warp_pointer(gtk_widget_get_display(vc
->gfx
.drawing_area
),
527 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
532 static void gd_cursor_define(DisplayChangeListener
*dcl
,
535 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
539 if (!gtk_widget_get_realized(vc
->gfx
.drawing_area
)) {
543 pixbuf
= gdk_pixbuf_new_from_data((guchar
*)(c
->data
),
544 GDK_COLORSPACE_RGB
, true, 8,
545 c
->width
, c
->height
, c
->width
* 4,
547 cursor
= gdk_cursor_new_from_pixbuf
548 (gtk_widget_get_display(vc
->gfx
.drawing_area
),
549 pixbuf
, c
->hot_x
, c
->hot_y
);
550 gdk_window_set_cursor(gtk_widget_get_window(vc
->gfx
.drawing_area
), cursor
);
551 g_object_unref(pixbuf
);
552 #if !GTK_CHECK_VERSION(3, 0, 0)
553 gdk_cursor_unref(cursor
);
555 g_object_unref(cursor
);
559 static void gd_switch(DisplayChangeListener
*dcl
,
560 DisplaySurface
*surface
)
562 VirtualConsole
*vc
= container_of(dcl
, VirtualConsole
, gfx
.dcl
);
565 trace_gd_switch(vc
->label
,
566 surface
? surface_width(surface
) : 0,
567 surface
? surface_height(surface
) : 0);
569 if (vc
->gfx
.surface
) {
570 cairo_surface_destroy(vc
->gfx
.surface
);
571 vc
->gfx
.surface
= NULL
;
573 if (vc
->gfx
.convert
) {
574 pixman_image_unref(vc
->gfx
.convert
);
575 vc
->gfx
.convert
= NULL
;
578 if (vc
->gfx
.ds
&& surface
&&
579 surface_width(vc
->gfx
.ds
) == surface_width(surface
) &&
580 surface_height(vc
->gfx
.ds
) == surface_height(surface
)) {
583 vc
->gfx
.ds
= surface
;
589 if (surface
->format
== PIXMAN_x8r8g8b8
) {
591 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
593 * No need to convert, use surface directly. Should be the
594 * common case as this is qemu_default_pixelformat(32) too.
596 vc
->gfx
.surface
= cairo_image_surface_create_for_data
597 (surface_data(surface
),
599 surface_width(surface
),
600 surface_height(surface
),
601 surface_stride(surface
));
603 /* Must convert surface, use pixman to do it. */
604 vc
->gfx
.convert
= pixman_image_create_bits(PIXMAN_x8r8g8b8
,
605 surface_width(surface
),
606 surface_height(surface
),
608 vc
->gfx
.surface
= cairo_image_surface_create_for_data
609 ((void *)pixman_image_get_data(vc
->gfx
.convert
),
611 pixman_image_get_width(vc
->gfx
.convert
),
612 pixman_image_get_height(vc
->gfx
.convert
),
613 pixman_image_get_stride(vc
->gfx
.convert
));
614 pixman_image_composite(PIXMAN_OP_SRC
, vc
->gfx
.ds
->image
,
615 NULL
, vc
->gfx
.convert
,
617 pixman_image_get_width(vc
->gfx
.convert
),
618 pixman_image_get_height(vc
->gfx
.convert
));
622 gd_update_windowsize(vc
);
624 gd_update_full_redraw(vc
);
628 static const DisplayChangeListenerOps dcl_ops
= {
630 .dpy_gfx_update
= gd_update
,
631 .dpy_gfx_switch
= gd_switch
,
632 .dpy_gfx_check_format
= qemu_pixman_check_format
,
633 .dpy_refresh
= gd_refresh
,
634 .dpy_mouse_set
= gd_mouse_set
,
635 .dpy_cursor_define
= gd_cursor_define
,
639 #if defined(CONFIG_OPENGL)
641 /** DisplayState Callbacks (opengl version) **/
643 #if defined(CONFIG_GTK_GL)
645 static const DisplayChangeListenerOps dcl_gl_area_ops
= {
646 .dpy_name
= "gtk-egl",
647 .dpy_gfx_update
= gd_gl_area_update
,
648 .dpy_gfx_switch
= gd_gl_area_switch
,
649 .dpy_gfx_check_format
= console_gl_check_format
,
650 .dpy_refresh
= gd_gl_area_refresh
,
651 .dpy_mouse_set
= gd_mouse_set
,
652 .dpy_cursor_define
= gd_cursor_define
,
654 .dpy_gl_ctx_create
= gd_gl_area_create_context
,
655 .dpy_gl_ctx_destroy
= gd_gl_area_destroy_context
,
656 .dpy_gl_ctx_make_current
= gd_gl_area_make_current
,
657 .dpy_gl_ctx_get_current
= gd_gl_area_get_current_context
,
658 .dpy_gl_scanout
= gd_gl_area_scanout
,
659 .dpy_gl_update
= gd_gl_area_scanout_flush
,
664 static const DisplayChangeListenerOps dcl_egl_ops
= {
665 .dpy_name
= "gtk-egl",
666 .dpy_gfx_update
= gd_egl_update
,
667 .dpy_gfx_switch
= gd_egl_switch
,
668 .dpy_gfx_check_format
= console_gl_check_format
,
669 .dpy_refresh
= gd_egl_refresh
,
670 .dpy_mouse_set
= gd_mouse_set
,
671 .dpy_cursor_define
= gd_cursor_define
,
673 .dpy_gl_ctx_create
= gd_egl_create_context
,
674 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
675 .dpy_gl_ctx_make_current
= gd_egl_make_current
,
676 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
677 .dpy_gl_scanout
= gd_egl_scanout
,
678 .dpy_gl_update
= gd_egl_scanout_flush
,
681 #endif /* CONFIG_GTK_GL */
682 #endif /* CONFIG_OPENGL */
686 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
688 GtkDisplayState
*s
= opaque
;
690 gd_update_caption(s
);
693 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
698 s
= container_of(notify
, GtkDisplayState
, mouse_mode_notifier
);
699 /* release the grab at switching to absolute mode */
700 if (qemu_input_is_absolute() && gd_is_grab_active(s
)) {
701 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
704 for (i
= 0; i
< s
->nb_vcs
; i
++) {
705 VirtualConsole
*vc
= &s
->vc
[i
];
706 gd_update_cursor(vc
);
712 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
715 GtkDisplayState
*s
= opaque
;
719 for (i
= 0; i
< s
->nb_vcs
; i
++) {
720 if (s
->vc
[i
].type
!= GD_VC_GFX
) {
723 unregister_displaychangelistener(&s
->vc
[i
].gfx
.dcl
);
732 static void gd_set_ui_info(VirtualConsole
*vc
, gint width
, gint height
)
736 memset(&info
, 0, sizeof(info
));
738 info
.height
= height
;
739 dpy_set_ui_info(vc
->gfx
.dcl
.con
, &info
);
742 #if defined(CONFIG_GTK_GL)
744 static gboolean
gd_render_event(GtkGLArea
*area
, GdkGLContext
*context
,
747 VirtualConsole
*vc
= opaque
;
755 static void gd_resize_event(GtkGLArea
*area
,
756 gint width
, gint height
, gpointer
*opaque
)
758 VirtualConsole
*vc
= (void *)opaque
;
760 gd_set_ui_info(vc
, width
, height
);
765 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
767 VirtualConsole
*vc
= opaque
;
768 GtkDisplayState
*s
= vc
->s
;
773 #if defined(CONFIG_OPENGL)
775 #if defined(CONFIG_GTK_GL)
776 /* invoke render callback please */
785 if (!gtk_widget_get_realized(widget
)) {
792 fbw
= surface_width(vc
->gfx
.ds
);
793 fbh
= surface_height(vc
->gfx
.ds
);
795 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
797 if (s
->full_screen
) {
798 vc
->gfx
.scale_x
= (double)ww
/ fbw
;
799 vc
->gfx
.scale_y
= (double)wh
/ fbh
;
800 } else if (s
->free_scale
) {
803 sx
= (double)ww
/ fbw
;
804 sy
= (double)wh
/ fbh
;
806 vc
->gfx
.scale_x
= vc
->gfx
.scale_y
= MIN(sx
, sy
);
809 fbw
*= vc
->gfx
.scale_x
;
810 fbh
*= vc
->gfx
.scale_y
;
820 cairo_rectangle(cr
, 0, 0, ww
, wh
);
822 /* Optionally cut out the inner area where the pixmap
823 will be drawn. This avoids 'flashing' since we're
824 not double-buffering. Note we're using the undocumented
825 behaviour of drawing the rectangle from right to left
826 to cut out the whole */
827 cairo_rectangle(cr
, mx
+ fbw
, my
,
831 cairo_scale(cr
, vc
->gfx
.scale_x
, vc
->gfx
.scale_y
);
832 cairo_set_source_surface(cr
, vc
->gfx
.surface
,
833 mx
/ vc
->gfx
.scale_x
, my
/ vc
->gfx
.scale_y
);
839 #if !GTK_CHECK_VERSION(3, 0, 0)
840 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
846 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
851 expose
->area
.height
);
854 ret
= gd_draw_event(widget
, cr
, opaque
);
862 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
865 VirtualConsole
*vc
= opaque
;
866 GtkDisplayState
*s
= vc
->s
;
876 fbw
= surface_width(vc
->gfx
.ds
) * vc
->gfx
.scale_x
;
877 fbh
= surface_height(vc
->gfx
.ds
) * vc
->gfx
.scale_y
;
879 gdk_drawable_get_size(gtk_widget_get_window(vc
->gfx
.drawing_area
),
890 x
= (motion
->x
- mx
) / vc
->gfx
.scale_x
;
891 y
= (motion
->y
- my
) / vc
->gfx
.scale_y
;
893 if (qemu_input_is_absolute()) {
894 if (x
< 0 || y
< 0 ||
895 x
>= surface_width(vc
->gfx
.ds
) ||
896 y
>= surface_height(vc
->gfx
.ds
)) {
899 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
,
900 surface_width(vc
->gfx
.ds
));
901 qemu_input_queue_abs(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
,
902 surface_height(vc
->gfx
.ds
));
903 qemu_input_event_sync();
904 } else if (s
->last_set
&& s
->ptr_owner
== vc
) {
905 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_X
, x
- s
->last_x
);
906 qemu_input_queue_rel(vc
->gfx
.dcl
.con
, INPUT_AXIS_Y
, y
- s
->last_y
);
907 qemu_input_event_sync();
913 if (!qemu_input_is_absolute() && s
->ptr_owner
== vc
) {
914 GdkScreen
*screen
= gtk_widget_get_screen(vc
->gfx
.drawing_area
);
915 int x
= (int)motion
->x_root
;
916 int y
= (int)motion
->y_root
;
918 /* In relative mode check to see if client pointer hit
919 * one of the screen edges, and if so move it back by
920 * 200 pixels. This is important because the pointer
921 * in the server doesn't correspond 1-for-1, and so
922 * may still be only half way across the screen. Without
923 * this warp, the server pointer would thus appear to hit
924 * an invisible wall */
931 if (x
== (gdk_screen_get_width(screen
) - 1)) {
934 if (y
== (gdk_screen_get_height(screen
) - 1)) {
938 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
939 #if GTK_CHECK_VERSION(3, 0, 0)
940 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
941 gdk_device_warp(dev
, screen
, x
, y
);
943 GdkDisplay
*display
= gtk_widget_get_display(widget
);
944 gdk_display_warp_pointer(display
, screen
, x
, y
);
953 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
956 VirtualConsole
*vc
= opaque
;
957 GtkDisplayState
*s
= vc
->s
;
960 /* implicitly grab the input at the first click in the relative mode */
961 if (button
->button
== 1 && button
->type
== GDK_BUTTON_PRESS
&&
962 !qemu_input_is_absolute() && s
->ptr_owner
!= vc
) {
964 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
967 gd_grab_pointer(vc
, "relative-mode-click");
972 if (button
->button
== 1) {
973 btn
= INPUT_BUTTON_LEFT
;
974 } else if (button
->button
== 2) {
975 btn
= INPUT_BUTTON_MIDDLE
;
976 } else if (button
->button
== 3) {
977 btn
= INPUT_BUTTON_RIGHT
;
982 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
,
983 button
->type
== GDK_BUTTON_PRESS
);
984 qemu_input_event_sync();
988 static gboolean
gd_scroll_event(GtkWidget
*widget
, GdkEventScroll
*scroll
,
991 VirtualConsole
*vc
= opaque
;
994 if (scroll
->direction
== GDK_SCROLL_UP
) {
995 btn
= INPUT_BUTTON_WHEEL_UP
;
996 } else if (scroll
->direction
== GDK_SCROLL_DOWN
) {
997 btn
= INPUT_BUTTON_WHEEL_DOWN
;
1002 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, true);
1003 qemu_input_event_sync();
1004 qemu_input_queue_btn(vc
->gfx
.dcl
.con
, btn
, false);
1005 qemu_input_event_sync();
1009 static int gd_map_keycode(GtkDisplayState
*s
, GdkDisplay
*dpy
, int gdk_keycode
)
1013 #ifdef GDK_WINDOWING_WIN32
1014 if (GDK_IS_WIN32_DISPLAY(dpy
)) {
1015 qemu_keycode
= MapVirtualKey(gdk_keycode
, MAPVK_VK_TO_VSC
);
1016 switch (qemu_keycode
) {
1017 case 103: /* alt gr */
1018 qemu_keycode
= 56 | SCANCODE_GREY
;
1021 return qemu_keycode
;
1025 if (gdk_keycode
< 9) {
1027 } else if (gdk_keycode
< 97) {
1028 qemu_keycode
= gdk_keycode
- 8;
1029 #ifdef GDK_WINDOWING_X11
1030 } else if (GDK_IS_X11_DISPLAY(dpy
) && gdk_keycode
< 158) {
1032 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
1034 qemu_keycode
= translate_xfree86_keycode(gdk_keycode
- 97);
1037 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
1038 qemu_keycode
= 0x70;
1039 } else if (gdk_keycode
== 211) { /* backslash */
1040 qemu_keycode
= 0x73;
1045 return qemu_keycode
;
1048 static gboolean
gd_text_key_down(GtkWidget
*widget
,
1049 GdkEventKey
*key
, void *opaque
)
1051 VirtualConsole
*vc
= opaque
;
1052 QemuConsole
*con
= vc
->gfx
.dcl
.con
;
1055 kbd_put_string_console(con
, key
->string
, key
->length
);
1057 int num
= gd_map_keycode(vc
->s
, gtk_widget_get_display(widget
),
1058 key
->hardware_keycode
);
1059 int qcode
= qemu_input_key_number_to_qcode(num
);
1060 kbd_put_qcode_console(con
, qcode
);
1065 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
1067 VirtualConsole
*vc
= opaque
;
1068 GtkDisplayState
*s
= vc
->s
;
1069 int gdk_keycode
= key
->hardware_keycode
;
1073 if (s
->ignore_keys
) {
1074 s
->ignore_keys
= (key
->type
== GDK_KEY_PRESS
);
1078 if (key
->keyval
== GDK_KEY_Pause
) {
1079 qemu_input_event_send_key_qcode(vc
->gfx
.dcl
.con
, Q_KEY_CODE_PAUSE
,
1080 key
->type
== GDK_KEY_PRESS
);
1084 qemu_keycode
= gd_map_keycode(s
, gtk_widget_get_display(widget
),
1087 trace_gd_key_event(vc
->label
, gdk_keycode
, qemu_keycode
,
1088 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
1090 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
1091 if (qemu_keycode
== modifier_keycode
[i
]) {
1092 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
1096 qemu_input_event_send_key_number(vc
->gfx
.dcl
.con
, qemu_keycode
,
1097 key
->type
== GDK_KEY_PRESS
);
1102 static gboolean
gd_event(GtkWidget
*widget
, GdkEvent
*event
, void *opaque
)
1104 if (event
->type
== GDK_MOTION_NOTIFY
) {
1105 return gd_motion_event(widget
, &event
->motion
, opaque
);
1110 /** Window Menu Actions **/
1112 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
1114 GtkDisplayState
*s
= opaque
;
1116 if (s
->external_pause_update
) {
1119 if (runstate_is_running()) {
1126 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
1128 qmp_system_reset(NULL
);
1131 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
1133 qmp_system_powerdown(NULL
);
1136 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
1141 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
1143 GtkDisplayState
*s
= opaque
;
1144 VirtualConsole
*vc
= gd_vc_find_by_menu(s
);
1145 GtkNotebook
*nb
= GTK_NOTEBOOK(s
->notebook
);
1148 gtk_release_modifiers(s
);
1150 page
= gtk_notebook_page_num(nb
, vc
->tab_item
);
1151 gtk_notebook_set_current_page(nb
, page
);
1152 gtk_widget_grab_focus(vc
->focus
);
1154 s
->ignore_keys
= false;
1157 static void gd_accel_switch_vc(void *opaque
)
1159 VirtualConsole
*vc
= opaque
;
1161 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1162 #if !GTK_CHECK_VERSION(3, 0, 0)
1163 /* GTK2 sends the accel key to the target console - ignore this until */
1164 vc
->s
->ignore_keys
= true;
1168 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
1170 GtkDisplayState
*s
= opaque
;
1171 VirtualConsole
*vc
= gd_vc_find_current(s
);
1173 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
1174 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
1176 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1178 gd_update_windowsize(vc
);
1181 static gboolean
gd_tab_window_close(GtkWidget
*widget
, GdkEvent
*event
,
1184 VirtualConsole
*vc
= opaque
;
1185 GtkDisplayState
*s
= vc
->s
;
1187 gtk_widget_set_sensitive(vc
->menu_item
, true);
1188 gd_widget_reparent(vc
->window
, s
->notebook
, vc
->tab_item
);
1189 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s
->notebook
),
1190 vc
->tab_item
, vc
->label
);
1191 gtk_widget_destroy(vc
->window
);
1196 static gboolean
gd_win_grab(void *opaque
)
1198 VirtualConsole
*vc
= opaque
;
1200 fprintf(stderr
, "%s: %s\n", __func__
, vc
->label
);
1201 if (vc
->s
->ptr_owner
) {
1202 gd_ungrab_pointer(vc
->s
);
1204 gd_grab_pointer(vc
, "user-request-detached-tab");
1209 static void gd_menu_untabify(GtkMenuItem
*item
, void *opaque
)
1211 GtkDisplayState
*s
= opaque
;
1212 VirtualConsole
*vc
= gd_vc_find_current(s
);
1214 if (vc
->type
== GD_VC_GFX
&&
1215 qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1216 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1220 gtk_widget_set_sensitive(vc
->menu_item
, false);
1221 vc
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1222 gd_widget_reparent(s
->notebook
, vc
->window
, vc
->tab_item
);
1224 g_signal_connect(vc
->window
, "delete-event",
1225 G_CALLBACK(gd_tab_window_close
), vc
);
1226 gtk_widget_show_all(vc
->window
);
1228 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1229 GtkAccelGroup
*ag
= gtk_accel_group_new();
1230 gtk_window_add_accel_group(GTK_WINDOW(vc
->window
), ag
);
1232 GClosure
*cb
= g_cclosure_new_swap(G_CALLBACK(gd_win_grab
),
1234 gtk_accel_group_connect(ag
, GDK_KEY_g
, HOTKEY_MODIFIERS
, 0, cb
);
1237 gd_update_geometry_hints(vc
);
1238 gd_update_caption(s
);
1242 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
1244 GtkDisplayState
*s
= opaque
;
1245 VirtualConsole
*vc
= gd_vc_find_current(s
);
1247 if (!s
->full_screen
) {
1248 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1249 gtk_widget_hide(s
->menu_bar
);
1250 if (vc
->type
== GD_VC_GFX
) {
1251 gtk_widget_set_size_request(vc
->gfx
.drawing_area
, -1, -1);
1253 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
1254 s
->full_screen
= TRUE
;
1256 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
1257 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
1258 gtk_widget_show(s
->menu_bar
);
1259 s
->full_screen
= FALSE
;
1260 if (vc
->type
== GD_VC_GFX
) {
1261 vc
->gfx
.scale_x
= 1.0;
1262 vc
->gfx
.scale_y
= 1.0;
1263 gd_update_windowsize(vc
);
1267 gd_update_cursor(vc
);
1270 static void gd_accel_full_screen(void *opaque
)
1272 GtkDisplayState
*s
= opaque
;
1273 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
1276 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
1278 GtkDisplayState
*s
= opaque
;
1279 VirtualConsole
*vc
= gd_vc_find_current(s
);
1281 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1284 vc
->gfx
.scale_x
+= VC_SCALE_STEP
;
1285 vc
->gfx
.scale_y
+= VC_SCALE_STEP
;
1287 gd_update_windowsize(vc
);
1290 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
1292 GtkDisplayState
*s
= opaque
;
1293 VirtualConsole
*vc
= gd_vc_find_current(s
);
1295 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
1298 vc
->gfx
.scale_x
-= VC_SCALE_STEP
;
1299 vc
->gfx
.scale_y
-= VC_SCALE_STEP
;
1301 vc
->gfx
.scale_x
= MAX(vc
->gfx
.scale_x
, VC_SCALE_MIN
);
1302 vc
->gfx
.scale_y
= MAX(vc
->gfx
.scale_y
, VC_SCALE_MIN
);
1304 gd_update_windowsize(vc
);
1307 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
1309 GtkDisplayState
*s
= opaque
;
1310 VirtualConsole
*vc
= gd_vc_find_current(s
);
1312 vc
->gfx
.scale_x
= 1.0;
1313 vc
->gfx
.scale_y
= 1.0;
1315 gd_update_windowsize(vc
);
1318 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
1320 GtkDisplayState
*s
= opaque
;
1321 VirtualConsole
*vc
= gd_vc_find_current(s
);
1323 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
1324 s
->free_scale
= TRUE
;
1326 s
->free_scale
= FALSE
;
1327 vc
->gfx
.scale_x
= 1.0;
1328 vc
->gfx
.scale_y
= 1.0;
1331 gd_update_windowsize(vc
);
1332 gd_update_full_redraw(vc
);
1335 #if GTK_CHECK_VERSION(3, 20, 0)
1336 static void gd_grab_update(VirtualConsole
*vc
, bool kbd
, bool ptr
)
1338 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1339 GdkSeat
*seat
= gdk_display_get_default_seat(display
);
1340 GdkWindow
*window
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1341 GdkSeatCapabilities caps
= 0;
1342 GdkCursor
*cursor
= NULL
;
1345 caps
|= GDK_SEAT_CAPABILITY_KEYBOARD
;
1348 caps
|= GDK_SEAT_CAPABILITY_ALL_POINTING
;
1349 cursor
= vc
->s
->null_cursor
;
1353 gdk_seat_grab(seat
, window
, caps
, false, cursor
,
1356 gdk_seat_ungrab(seat
);
1359 #elif GTK_CHECK_VERSION(3, 0, 0)
1360 static void gd_grab_devices(VirtualConsole
*vc
, bool grab
,
1361 GdkInputSource source
, GdkEventMask mask
,
1364 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1365 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
1366 GList
*devs
= gdk_device_manager_list_devices(mgr
, GDK_DEVICE_TYPE_MASTER
);
1369 for (tmp
= devs
; tmp
; tmp
= tmp
->next
) {
1370 GdkDevice
*dev
= tmp
->data
;
1371 if (gdk_device_get_source(dev
) != source
) {
1375 GdkWindow
*win
= gtk_widget_get_window(vc
->gfx
.drawing_area
);
1376 gdk_device_grab(dev
, win
, GDK_OWNERSHIP_NONE
, FALSE
,
1377 mask
, cursor
, GDK_CURRENT_TIME
);
1379 gdk_device_ungrab(dev
, GDK_CURRENT_TIME
);
1386 static void gd_grab_keyboard(VirtualConsole
*vc
, const char *reason
)
1388 if (vc
->s
->kbd_owner
) {
1389 if (vc
->s
->kbd_owner
== vc
) {
1392 gd_ungrab_keyboard(vc
->s
);
1396 #if GTK_CHECK_VERSION(3, 20, 0)
1397 gd_grab_update(vc
, true, vc
->s
->ptr_owner
== vc
);
1398 #elif GTK_CHECK_VERSION(3, 0, 0)
1399 gd_grab_devices(vc
, true, GDK_SOURCE_KEYBOARD
,
1400 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
1403 gdk_keyboard_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1407 vc
->s
->kbd_owner
= vc
;
1408 gd_update_caption(vc
->s
);
1409 trace_gd_grab(vc
->label
, "kbd", reason
);
1412 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
1414 VirtualConsole
*vc
= s
->kbd_owner
;
1419 s
->kbd_owner
= NULL
;
1421 #if GTK_CHECK_VERSION(3, 20, 0)
1422 gd_grab_update(vc
, false, vc
->s
->ptr_owner
== vc
);
1423 #elif GTK_CHECK_VERSION(3, 0, 0)
1424 gd_grab_devices(vc
, false, GDK_SOURCE_KEYBOARD
, 0, NULL
);
1426 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
1428 gd_update_caption(s
);
1429 trace_gd_ungrab(vc
->label
, "kbd");
1432 static void gd_grab_pointer(VirtualConsole
*vc
, const char *reason
)
1434 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1436 if (vc
->s
->ptr_owner
) {
1437 if (vc
->s
->ptr_owner
== vc
) {
1440 gd_ungrab_pointer(vc
->s
);
1444 #if GTK_CHECK_VERSION(3, 20, 0)
1445 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, true);
1446 gdk_device_get_position(gd_get_pointer(display
),
1447 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1448 #elif GTK_CHECK_VERSION(3, 0, 0)
1449 gd_grab_devices(vc
, true, GDK_SOURCE_MOUSE
,
1450 GDK_POINTER_MOTION_MASK
|
1451 GDK_BUTTON_PRESS_MASK
|
1452 GDK_BUTTON_RELEASE_MASK
|
1453 GDK_BUTTON_MOTION_MASK
|
1455 vc
->s
->null_cursor
);
1456 gdk_device_get_position(gd_get_pointer(display
),
1457 NULL
, &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
);
1459 gdk_pointer_grab(gtk_widget_get_window(vc
->gfx
.drawing_area
),
1460 FALSE
, /* All events to come to our window directly */
1461 GDK_POINTER_MOTION_MASK
|
1462 GDK_BUTTON_PRESS_MASK
|
1463 GDK_BUTTON_RELEASE_MASK
|
1464 GDK_BUTTON_MOTION_MASK
|
1466 NULL
, /* Allow cursor to move over entire desktop */
1469 gdk_display_get_pointer(display
, NULL
,
1470 &vc
->s
->grab_x_root
, &vc
->s
->grab_y_root
, NULL
);
1472 vc
->s
->ptr_owner
= vc
;
1473 gd_update_caption(vc
->s
);
1474 trace_gd_grab(vc
->label
, "ptr", reason
);
1477 static void gd_ungrab_pointer(GtkDisplayState
*s
)
1479 VirtualConsole
*vc
= s
->ptr_owner
;
1480 GdkDisplay
*display
= gtk_widget_get_display(vc
->gfx
.drawing_area
);
1485 s
->ptr_owner
= NULL
;
1487 #if GTK_CHECK_VERSION(3, 20, 0)
1488 gd_grab_update(vc
, vc
->s
->kbd_owner
== vc
, false);
1489 gdk_device_warp(gd_get_pointer(display
),
1490 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1491 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1492 #elif GTK_CHECK_VERSION(3, 0, 0)
1493 gd_grab_devices(vc
, false, GDK_SOURCE_MOUSE
, 0, NULL
);
1494 gdk_device_warp(gd_get_pointer(display
),
1495 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1496 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1498 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1499 gdk_display_warp_pointer(display
,
1500 gtk_widget_get_screen(vc
->gfx
.drawing_area
),
1501 vc
->s
->grab_x_root
, vc
->s
->grab_y_root
);
1503 gd_update_caption(s
);
1504 trace_gd_ungrab(vc
->label
, "ptr");
1507 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
1509 GtkDisplayState
*s
= opaque
;
1510 VirtualConsole
*vc
= gd_vc_find_current(s
);
1512 if (gd_is_grab_active(s
)) {
1513 gd_grab_keyboard(vc
, "user-request-main-window");
1514 gd_grab_pointer(vc
, "user-request-main-window");
1516 gd_ungrab_keyboard(s
);
1517 gd_ungrab_pointer(s
);
1520 gd_update_cursor(vc
);
1523 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
1526 GtkDisplayState
*s
= data
;
1530 if (!gtk_widget_get_realized(s
->notebook
)) {
1534 #ifdef VTE_RESIZE_HACK
1535 vc
= gd_vc_find_current(s
);
1536 if (vc
&& vc
->type
== GD_VC_VTE
) {
1537 gtk_widget_hide(vc
->vte
.terminal
);
1540 vc
= gd_vc_find_by_page(s
, arg2
);
1544 #ifdef VTE_RESIZE_HACK
1545 if (vc
->type
== GD_VC_VTE
) {
1546 gtk_widget_show(vc
->vte
.terminal
);
1549 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
),
1551 on_vga
= (vc
->type
== GD_VC_GFX
&&
1552 qemu_console_is_graphic(vc
->gfx
.dcl
.con
));
1554 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1556 } else if (s
->full_screen
) {
1557 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1560 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1562 gd_update_windowsize(vc
);
1563 gd_update_cursor(vc
);
1566 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1569 VirtualConsole
*vc
= opaque
;
1570 GtkDisplayState
*s
= vc
->s
;
1572 if (gd_grab_on_hover(s
)) {
1573 gd_grab_keyboard(vc
, "grab-on-hover");
1578 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
,
1581 VirtualConsole
*vc
= opaque
;
1582 GtkDisplayState
*s
= vc
->s
;
1584 if (gd_grab_on_hover(s
)) {
1585 gd_ungrab_keyboard(s
);
1590 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1591 GdkEventCrossing
*crossing
, gpointer opaque
)
1593 VirtualConsole
*vc
= opaque
;
1594 GtkDisplayState
*s
= vc
->s
;
1596 gtk_release_modifiers(s
);
1600 static gboolean
gd_configure(GtkWidget
*widget
,
1601 GdkEventConfigure
*cfg
, gpointer opaque
)
1603 VirtualConsole
*vc
= opaque
;
1605 gd_set_ui_info(vc
, cfg
->width
, cfg
->height
);
1609 /** Virtual Console Callbacks **/
1611 static GSList
*gd_vc_menu_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1612 int idx
, GSList
*group
, GtkWidget
*view_menu
)
1614 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, vc
->label
);
1615 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_1
+ idx
,
1616 HOTKEY_MODIFIERS
, 0,
1617 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc
), vc
, NULL
));
1618 #if GTK_CHECK_VERSION(3, 8, 0)
1619 gtk_accel_label_set_accel(
1620 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc
->menu_item
))),
1621 GDK_KEY_1
+ idx
, HOTKEY_MODIFIERS
);
1624 g_signal_connect(vc
->menu_item
, "activate",
1625 G_CALLBACK(gd_menu_switch_vc
), s
);
1626 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), vc
->menu_item
);
1628 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1632 #if defined(CONFIG_VTE)
1633 static void gd_menu_copy(GtkMenuItem
*item
, void *opaque
)
1635 GtkDisplayState
*s
= opaque
;
1636 VirtualConsole
*vc
= gd_vc_find_current(s
);
1638 vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
.terminal
));
1641 static void gd_vc_adjustment_changed(GtkAdjustment
*adjustment
, void *opaque
)
1643 VirtualConsole
*vc
= opaque
;
1645 if (gtk_adjustment_get_upper(adjustment
) >
1646 gtk_adjustment_get_page_size(adjustment
)) {
1647 gtk_widget_show(vc
->vte
.scrollbar
);
1649 gtk_widget_hide(vc
->vte
.scrollbar
);
1653 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1655 VirtualConsole
*vc
= chr
->opaque
;
1657 vte_terminal_feed(VTE_TERMINAL(vc
->vte
.terminal
), (const char *)buf
, len
);
1661 static void gd_vc_chr_set_echo(CharDriverState
*chr
, bool echo
)
1663 VirtualConsole
*vc
= chr
->opaque
;
1665 vc
->vte
.echo
= echo
;
1669 static CharDriverState
*vcs
[MAX_VCS
];
1671 static CharDriverState
*gd_vc_handler(ChardevVC
*vc
, Error
**errp
)
1673 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
1674 CharDriverState
*chr
;
1676 chr
= qemu_chr_alloc(common
, errp
);
1681 chr
->chr_write
= gd_vc_chr_write
;
1682 chr
->chr_set_echo
= gd_vc_chr_set_echo
;
1684 /* Temporary, until gd_vc_vte_init runs. */
1685 chr
->opaque
= g_new0(VirtualConsole
, 1);
1687 /* defer OPENED events until our vc is fully initialized */
1688 chr
->explicit_be_open
= true;
1690 vcs
[nb_vcs
++] = chr
;
1695 static gboolean
gd_vc_in(VteTerminal
*terminal
, gchar
*text
, guint size
,
1698 VirtualConsole
*vc
= user_data
;
1701 VteTerminal
*term
= VTE_TERMINAL(vc
->vte
.terminal
);
1703 for (i
= 0; i
< size
; i
++) {
1704 uint8_t c
= text
[i
];
1705 if (c
>= 128 || isprint(c
)) {
1706 /* 8-bit characters are considered printable. */
1707 vte_terminal_feed(term
, &text
[i
], 1);
1708 } else if (c
== '\r' || c
== '\n') {
1709 vte_terminal_feed(term
, "\r\n", 2);
1711 char ctrl
[2] = { '^', 0};
1712 ctrl
[1] = text
[i
] ^ 64;
1713 vte_terminal_feed(term
, ctrl
, 2);
1718 qemu_chr_be_write(vc
->vte
.chr
, (uint8_t *)text
, (unsigned int)size
);
1722 static GSList
*gd_vc_vte_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1723 CharDriverState
*chr
, int idx
,
1724 GSList
*group
, GtkWidget
*view_menu
)
1728 GtkWidget
*scrollbar
;
1729 GtkAdjustment
*vadjustment
;
1730 VirtualConsole
*tmp_vc
= chr
->opaque
;
1733 vc
->vte
.echo
= tmp_vc
->vte
.echo
;
1739 snprintf(buffer
, sizeof(buffer
), "vc%d", idx
);
1740 vc
->label
= g_strdup_printf("%s", vc
->vte
.chr
->label
1741 ? vc
->vte
.chr
->label
: buffer
);
1742 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1744 vc
->vte
.terminal
= vte_terminal_new();
1745 g_signal_connect(vc
->vte
.terminal
, "commit", G_CALLBACK(gd_vc_in
), vc
);
1747 /* The documentation says that the default is UTF-8, but actually it is
1748 * 7-bit ASCII at least in VTE 0.38.
1750 #if VTE_CHECK_VERSION(0, 40, 0)
1751 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8", NULL
);
1753 vte_terminal_set_encoding(VTE_TERMINAL(vc
->vte
.terminal
), "UTF-8");
1756 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
.terminal
), -1);
1757 vte_terminal_set_size(VTE_TERMINAL(vc
->vte
.terminal
),
1758 VC_TERM_X_MIN
, VC_TERM_Y_MIN
);
1760 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1761 vadjustment
= gtk_scrollable_get_vadjustment
1762 (GTK_SCROLLABLE(vc
->vte
.terminal
));
1764 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->vte
.terminal
));
1767 #if GTK_CHECK_VERSION(3, 0, 0)
1768 box
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 2);
1769 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vadjustment
);
1771 box
= gtk_hbox_new(false, 2);
1772 scrollbar
= gtk_vscrollbar_new(vadjustment
);
1775 gtk_box_pack_start(GTK_BOX(box
), vc
->vte
.terminal
, TRUE
, TRUE
, 0);
1776 gtk_box_pack_start(GTK_BOX(box
), scrollbar
, FALSE
, FALSE
, 0);
1779 vc
->vte
.scrollbar
= scrollbar
;
1781 g_signal_connect(vadjustment
, "changed",
1782 G_CALLBACK(gd_vc_adjustment_changed
), vc
);
1784 vc
->type
= GD_VC_VTE
;
1786 vc
->focus
= vc
->vte
.terminal
;
1787 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), vc
->tab_item
,
1788 gtk_label_new(vc
->label
));
1790 qemu_chr_be_generic_open(vc
->vte
.chr
);
1791 if (vc
->vte
.chr
->init
) {
1792 vc
->vte
.chr
->init(vc
->vte
.chr
);
1798 static void gd_vcs_init(GtkDisplayState
*s
, GSList
*group
,
1799 GtkWidget
*view_menu
)
1803 for (i
= 0; i
< nb_vcs
; i
++) {
1804 VirtualConsole
*vc
= &s
->vc
[s
->nb_vcs
];
1805 group
= gd_vc_vte_init(s
, vc
, vcs
[i
], s
->nb_vcs
, group
, view_menu
);
1809 #endif /* CONFIG_VTE */
1811 /** Window Creation **/
1813 static void gd_connect_vc_gfx_signals(VirtualConsole
*vc
)
1815 #if GTK_CHECK_VERSION(3, 0, 0)
1816 g_signal_connect(vc
->gfx
.drawing_area
, "draw",
1817 G_CALLBACK(gd_draw_event
), vc
);
1818 #if defined(CONFIG_GTK_GL)
1819 if (display_opengl
) {
1820 /* wire up GtkGlArea events */
1821 g_signal_connect(vc
->gfx
.drawing_area
, "render",
1822 G_CALLBACK(gd_render_event
), vc
);
1823 g_signal_connect(vc
->gfx
.drawing_area
, "resize",
1824 G_CALLBACK(gd_resize_event
), vc
);
1828 g_signal_connect(vc
->gfx
.drawing_area
, "expose-event",
1829 G_CALLBACK(gd_expose_event
), vc
);
1831 if (qemu_console_is_graphic(vc
->gfx
.dcl
.con
)) {
1832 g_signal_connect(vc
->gfx
.drawing_area
, "event",
1833 G_CALLBACK(gd_event
), vc
);
1834 g_signal_connect(vc
->gfx
.drawing_area
, "button-press-event",
1835 G_CALLBACK(gd_button_event
), vc
);
1836 g_signal_connect(vc
->gfx
.drawing_area
, "button-release-event",
1837 G_CALLBACK(gd_button_event
), vc
);
1838 g_signal_connect(vc
->gfx
.drawing_area
, "scroll-event",
1839 G_CALLBACK(gd_scroll_event
), vc
);
1840 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1841 G_CALLBACK(gd_key_event
), vc
);
1842 g_signal_connect(vc
->gfx
.drawing_area
, "key-release-event",
1843 G_CALLBACK(gd_key_event
), vc
);
1845 g_signal_connect(vc
->gfx
.drawing_area
, "enter-notify-event",
1846 G_CALLBACK(gd_enter_event
), vc
);
1847 g_signal_connect(vc
->gfx
.drawing_area
, "leave-notify-event",
1848 G_CALLBACK(gd_leave_event
), vc
);
1849 g_signal_connect(vc
->gfx
.drawing_area
, "focus-out-event",
1850 G_CALLBACK(gd_focus_out_event
), vc
);
1851 g_signal_connect(vc
->gfx
.drawing_area
, "configure-event",
1852 G_CALLBACK(gd_configure
), vc
);
1854 g_signal_connect(vc
->gfx
.drawing_area
, "key-press-event",
1855 G_CALLBACK(gd_text_key_down
), vc
);
1859 static void gd_connect_signals(GtkDisplayState
*s
)
1861 g_signal_connect(s
->show_tabs_item
, "activate",
1862 G_CALLBACK(gd_menu_show_tabs
), s
);
1863 g_signal_connect(s
->untabify_item
, "activate",
1864 G_CALLBACK(gd_menu_untabify
), s
);
1866 g_signal_connect(s
->window
, "delete-event",
1867 G_CALLBACK(gd_window_close
), s
);
1869 g_signal_connect(s
->pause_item
, "activate",
1870 G_CALLBACK(gd_menu_pause
), s
);
1871 g_signal_connect(s
->reset_item
, "activate",
1872 G_CALLBACK(gd_menu_reset
), s
);
1873 g_signal_connect(s
->powerdown_item
, "activate",
1874 G_CALLBACK(gd_menu_powerdown
), s
);
1875 g_signal_connect(s
->quit_item
, "activate",
1876 G_CALLBACK(gd_menu_quit
), s
);
1877 #if defined(CONFIG_VTE)
1878 g_signal_connect(s
->copy_item
, "activate",
1879 G_CALLBACK(gd_menu_copy
), s
);
1881 g_signal_connect(s
->full_screen_item
, "activate",
1882 G_CALLBACK(gd_menu_full_screen
), s
);
1883 g_signal_connect(s
->zoom_in_item
, "activate",
1884 G_CALLBACK(gd_menu_zoom_in
), s
);
1885 g_signal_connect(s
->zoom_out_item
, "activate",
1886 G_CALLBACK(gd_menu_zoom_out
), s
);
1887 g_signal_connect(s
->zoom_fixed_item
, "activate",
1888 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1889 g_signal_connect(s
->zoom_fit_item
, "activate",
1890 G_CALLBACK(gd_menu_zoom_fit
), s
);
1891 g_signal_connect(s
->grab_item
, "activate",
1892 G_CALLBACK(gd_menu_grab_input
), s
);
1893 g_signal_connect(s
->notebook
, "switch-page",
1894 G_CALLBACK(gd_change_page
), s
);
1897 static GtkWidget
*gd_create_menu_machine(GtkDisplayState
*s
)
1899 GtkWidget
*machine_menu
;
1900 GtkWidget
*separator
;
1902 machine_menu
= gtk_menu_new();
1903 gtk_menu_set_accel_group(GTK_MENU(machine_menu
), s
->accel_group
);
1905 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1906 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->pause_item
);
1908 separator
= gtk_separator_menu_item_new();
1909 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1911 s
->reset_item
= gtk_menu_item_new_with_mnemonic(_("_Reset"));
1912 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->reset_item
);
1914 s
->powerdown_item
= gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1915 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->powerdown_item
);
1917 separator
= gtk_separator_menu_item_new();
1918 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), separator
);
1920 s
->quit_item
= gtk_menu_item_new_with_mnemonic(_("_Quit"));
1921 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1922 "<QEMU>/Machine/Quit");
1923 gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1924 GDK_KEY_q
, HOTKEY_MODIFIERS
);
1925 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu
), s
->quit_item
);
1927 return machine_menu
;
1930 static GSList
*gd_vc_gfx_init(GtkDisplayState
*s
, VirtualConsole
*vc
,
1931 QemuConsole
*con
, int idx
,
1932 GSList
*group
, GtkWidget
*view_menu
)
1934 vc
->label
= qemu_console_get_label(con
);
1936 vc
->gfx
.scale_x
= 1.0;
1937 vc
->gfx
.scale_y
= 1.0;
1939 #if defined(CONFIG_OPENGL)
1940 if (display_opengl
) {
1941 #if defined(CONFIG_GTK_GL)
1942 vc
->gfx
.drawing_area
= gtk_gl_area_new();
1943 vc
->gfx
.dcl
.ops
= &dcl_gl_area_ops
;
1945 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1947 * gtk_widget_set_double_buffered() was deprecated in 3.14.
1948 * It is required for opengl rendering on X11 though. A
1949 * proper replacement (native opengl support) is only
1950 * available in 3.16+. Silence the warning if possible.
1952 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1953 #pragma GCC diagnostic push
1954 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1956 gtk_widget_set_double_buffered(vc
->gfx
.drawing_area
, FALSE
);
1957 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
1958 #pragma GCC diagnostic pop
1960 vc
->gfx
.dcl
.ops
= &dcl_egl_ops
;
1961 #endif /* CONFIG_GTK_GL */
1965 vc
->gfx
.drawing_area
= gtk_drawing_area_new();
1966 vc
->gfx
.dcl
.ops
= &dcl_ops
;
1970 gtk_widget_add_events(vc
->gfx
.drawing_area
,
1971 GDK_POINTER_MOTION_MASK
|
1972 GDK_BUTTON_PRESS_MASK
|
1973 GDK_BUTTON_RELEASE_MASK
|
1974 GDK_BUTTON_MOTION_MASK
|
1975 GDK_ENTER_NOTIFY_MASK
|
1976 GDK_LEAVE_NOTIFY_MASK
|
1978 GDK_KEY_PRESS_MASK
);
1979 gtk_widget_set_can_focus(vc
->gfx
.drawing_area
, TRUE
);
1981 vc
->type
= GD_VC_GFX
;
1982 vc
->tab_item
= vc
->gfx
.drawing_area
;
1983 vc
->focus
= vc
->gfx
.drawing_area
;
1984 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
),
1985 vc
->tab_item
, gtk_label_new(vc
->label
));
1987 vc
->gfx
.dcl
.con
= con
;
1988 register_displaychangelistener(&vc
->gfx
.dcl
);
1990 gd_connect_vc_gfx_signals(vc
);
1991 group
= gd_vc_menu_init(s
, vc
, idx
, group
, view_menu
);
1993 if (dpy_ui_info_supported(vc
->gfx
.dcl
.con
)) {
1994 gtk_menu_item_activate(GTK_MENU_ITEM(s
->zoom_fit_item
));
1995 s
->free_scale
= true;
2001 static GtkWidget
*gd_create_menu_view(GtkDisplayState
*s
)
2003 GSList
*group
= NULL
;
2004 GtkWidget
*view_menu
;
2005 GtkWidget
*separator
;
2009 view_menu
= gtk_menu_new();
2010 gtk_menu_set_accel_group(GTK_MENU(view_menu
), s
->accel_group
);
2012 s
->full_screen_item
= gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2014 #if defined(CONFIG_VTE)
2015 s
->copy_item
= gtk_menu_item_new_with_mnemonic(_("_Copy"));
2016 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->copy_item
);
2019 gtk_accel_group_connect(s
->accel_group
, GDK_KEY_f
, HOTKEY_MODIFIERS
, 0,
2020 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen
), s
, NULL
));
2021 #if GTK_CHECK_VERSION(3, 8, 0)
2022 gtk_accel_label_set_accel(
2023 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s
->full_screen_item
))),
2024 GDK_KEY_f
, HOTKEY_MODIFIERS
);
2026 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->full_screen_item
);
2028 separator
= gtk_separator_menu_item_new();
2029 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2031 s
->zoom_in_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2032 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
2033 "<QEMU>/View/Zoom In");
2034 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
,
2036 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_in_item
);
2038 s
->zoom_out_item
= gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2039 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
2040 "<QEMU>/View/Zoom Out");
2041 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
,
2043 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_out_item
);
2045 s
->zoom_fixed_item
= gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2046 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
2047 "<QEMU>/View/Zoom Fixed");
2048 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
,
2050 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fixed_item
);
2052 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
2053 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->zoom_fit_item
);
2055 separator
= gtk_separator_menu_item_new();
2056 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2058 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
2059 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_on_hover_item
);
2061 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2062 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
2063 "<QEMU>/View/Grab Input");
2064 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
,
2066 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->grab_item
);
2068 separator
= gtk_separator_menu_item_new();
2069 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2072 for (vc
= 0;; vc
++) {
2073 con
= qemu_console_lookup_by_index(vc
);
2077 group
= gd_vc_gfx_init(s
, &s
->vc
[vc
], con
,
2078 vc
, group
, view_menu
);
2082 #if defined(CONFIG_VTE)
2084 gd_vcs_init(s
, group
, view_menu
);
2087 separator
= gtk_separator_menu_item_new();
2088 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), separator
);
2090 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
2091 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->show_tabs_item
);
2093 s
->untabify_item
= gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
2094 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu
), s
->untabify_item
);
2099 static void gd_create_menus(GtkDisplayState
*s
)
2101 s
->accel_group
= gtk_accel_group_new();
2102 s
->machine_menu
= gd_create_menu_machine(s
);
2103 s
->view_menu
= gd_create_menu_view(s
);
2105 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
2106 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
2108 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
2110 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
2111 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
2112 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
2114 g_object_set_data(G_OBJECT(s
->window
), "accel_group", s
->accel_group
);
2115 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), s
->accel_group
);
2118 static void gd_set_keycode_type(GtkDisplayState
*s
)
2120 #ifdef GDK_WINDOWING_X11
2121 GdkDisplay
*display
= gtk_widget_get_display(s
->window
);
2122 if (GDK_IS_X11_DISPLAY(display
)) {
2123 Display
*x11_display
= gdk_x11_display_get_xdisplay(display
);
2124 XkbDescPtr desc
= XkbGetKeyboard(x11_display
, XkbGBN_AllComponentsMask
,
2126 char *keycodes
= NULL
;
2128 if (desc
&& desc
->names
) {
2129 keycodes
= XGetAtomName(x11_display
, desc
->names
->keycodes
);
2131 if (keycodes
== NULL
) {
2132 fprintf(stderr
, "could not lookup keycode name\n");
2133 } else if (strstart(keycodes
, "evdev", NULL
)) {
2134 s
->has_evdev
= true;
2135 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
2136 fprintf(stderr
, "unknown keycodes `%s', please report to "
2137 "qemu-devel@nongnu.org\n", keycodes
);
2141 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
2150 static gboolean gtkinit
;
2152 void gtk_display_init(DisplayState
*ds
, bool full_screen
, bool grab_on_hover
)
2154 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
2156 GdkDisplay
*window_display
;
2159 fprintf(stderr
, "gtk initialization failed\n");
2163 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2164 #if GTK_CHECK_VERSION(3, 2, 0)
2165 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
2167 s
->vbox
= gtk_vbox_new(FALSE
, 0);
2169 s
->notebook
= gtk_notebook_new();
2170 s
->menu_bar
= gtk_menu_bar_new();
2172 s
->free_scale
= FALSE
;
2174 /* LC_MESSAGES only. See early_gtk_display_init() for details */
2175 setlocale(LC_MESSAGES
, "");
2176 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
2179 window_display
= gtk_widget_get_display(s
->window
);
2180 s
->null_cursor
= gdk_cursor_new_for_display(window_display
,
2183 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
2184 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
2185 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
2187 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu_logo_no_text.svg");
2189 GError
*error
= NULL
;
2190 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
2192 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
2194 g_error_free(error
);
2201 gd_connect_signals(s
);
2203 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2204 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
2206 gd_update_caption(s
);
2208 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
2209 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
2211 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
2213 gtk_widget_show_all(s
->window
);
2215 #ifdef VTE_RESIZE_HACK
2217 VirtualConsole
*cur
= gd_vc_find_current(s
);
2221 for (i
= 0; i
< s
->nb_vcs
; i
++) {
2222 VirtualConsole
*vc
= &s
->vc
[i
];
2223 if (vc
&& vc
->type
== GD_VC_VTE
&& vc
!= cur
) {
2224 gtk_widget_hide(vc
->vte
.terminal
);
2227 gd_update_windowsize(cur
);
2233 gtk_menu_item_activate(GTK_MENU_ITEM(s
->full_screen_item
));
2235 if (grab_on_hover
) {
2236 gtk_menu_item_activate(GTK_MENU_ITEM(s
->grab_on_hover_item
));
2239 gd_set_keycode_type(s
);
2242 void early_gtk_display_init(int opengl
)
2244 /* The QEMU code relies on the assumption that it's always run in
2245 * the C locale. Therefore it is not prepared to deal with
2246 * operations that produce different results depending on the
2247 * locale, such as printf's formatting of decimal numbers, and
2250 * Since GTK+ calls setlocale() by default -importing the locale
2251 * settings from the environment- we must prevent it from doing so
2252 * using gtk_disable_setlocale().
2254 * QEMU's GTK+ UI, however, _does_ have translations for some of
2255 * the menu items. As a trade-off between a functionally correct
2256 * QEMU and a fully internationalized UI we support importing
2257 * LC_MESSAGES from the environment (see the setlocale() call
2258 * earlier in this file). This allows us to display translated
2259 * messages leaving everything else untouched.
2261 gtk_disable_setlocale();
2262 gtkinit
= gtk_init_check(NULL
, NULL
);
2264 /* don't exit yet, that'll break -help */
2269 case -1: /* default */
2273 #if defined(CONFIG_OPENGL)
2274 #if defined(CONFIG_GTK_GL)
2282 g_assert_not_reached();
2286 #if defined(CONFIG_VTE)
2287 register_vc_handler(gd_vc_handler
);