4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 * Portions from gtk-vnc:
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.0 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
37 #include "qemu-common.h"
39 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
40 /* Work around an -Wstrict-prototypes warning in GTK headers */
41 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
44 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
45 #pragma GCC diagnostic error "-Wstrict-prototypes"
49 #include <gdk/gdkkeysyms.h>
50 #include <glib/gi18n.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
60 #include "ui/console.h"
61 #include "sysemu/sysemu.h"
62 #include "qmp-commands.h"
65 #include "sysemu/char.h"
70 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
72 #define DPRINTF(fmt, ...) do { } while (0)
78 /* Compatibility define to let us build on both Gtk2 and Gtk3 */
79 #if GTK_CHECK_VERSION(3, 0, 0)
80 static inline void gdk_drawable_get_size(GdkWindow
*w
, gint
*ww
, gint
*wh
)
82 *ww
= gdk_window_get_width(w
);
83 *wh
= gdk_window_get_height(w
);
87 #if !GTK_CHECK_VERSION(2, 20, 0)
88 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
92 #define GDK_KEY_0 GDK_0
93 #define GDK_KEY_1 GDK_1
94 #define GDK_KEY_2 GDK_2
95 #define GDK_KEY_f GDK_f
96 #define GDK_KEY_g GDK_g
97 #define GDK_KEY_plus GDK_plus
98 #define GDK_KEY_minus GDK_minus
101 static const int modifier_keycode
[] = {
102 /* shift, control, alt keys, meta keys, both left & right */
103 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
106 typedef struct VirtualConsole
108 GtkWidget
*menu_item
;
110 GtkWidget
*scrolled_window
;
111 CharDriverState
*chr
;
115 typedef struct GtkDisplayState
121 GtkAccelGroup
*accel_group
;
123 GtkWidget
*machine_menu_item
;
124 GtkWidget
*machine_menu
;
125 GtkWidget
*pause_item
;
126 GtkWidget
*reset_item
;
127 GtkWidget
*powerdown_item
;
128 GtkWidget
*quit_item
;
130 GtkWidget
*view_menu_item
;
131 GtkWidget
*view_menu
;
132 GtkWidget
*full_screen_item
;
133 GtkWidget
*zoom_in_item
;
134 GtkWidget
*zoom_out_item
;
135 GtkWidget
*zoom_fixed_item
;
136 GtkWidget
*zoom_fit_item
;
137 GtkWidget
*grab_item
;
138 GtkWidget
*grab_on_hover_item
;
142 VirtualConsole vc
[MAX_VCS
];
144 GtkWidget
*show_tabs_item
;
148 GtkWidget
*drawing_area
;
149 cairo_surface_t
*surface
;
150 DisplayChangeListener dcl
;
158 gboolean full_screen
;
160 GdkCursor
*null_cursor
;
161 Notifier mouse_mode_notifier
;
164 bool external_pause_update
;
166 bool modifier_pressed
[ARRAY_SIZE(modifier_keycode
)];
169 static GtkDisplayState
*global_state
;
171 /** Utility Functions **/
173 static bool gd_is_grab_active(GtkDisplayState
*s
)
175 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
178 static bool gd_grab_on_hover(GtkDisplayState
*s
)
180 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
183 static bool gd_on_vga(GtkDisplayState
*s
)
185 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
)) == 0;
188 static void gd_update_cursor(GtkDisplayState
*s
, gboolean override
)
193 window
= gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
));
195 on_vga
= gd_on_vga(s
);
197 if ((override
|| on_vga
) &&
198 (s
->full_screen
|| kbd_mouse_is_absolute() || gd_is_grab_active(s
))) {
199 gdk_window_set_cursor(window
, s
->null_cursor
);
201 gdk_window_set_cursor(window
, NULL
);
205 static void gd_update_caption(GtkDisplayState
*s
)
207 const char *status
= "";
209 const char *grab
= "";
210 bool is_paused
= !runstate_is_running();
212 if (gd_is_grab_active(s
)) {
213 grab
= _(" - Press Ctrl+Alt+G to release grab");
217 status
= _(" [Paused]");
219 s
->external_pause_update
= true;
220 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
222 s
->external_pause_update
= false;
225 title
= g_strdup_printf("QEMU (%s)%s%s", qemu_name
, status
, grab
);
227 title
= g_strdup_printf("QEMU%s%s", status
, grab
);
230 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
235 static void gd_update_windowsize(GtkDisplayState
*s
)
237 if (!s
->full_screen
) {
252 gtk_widget_set_size_request(s
->drawing_area
,
253 surface_width(s
->ds
) * s
->scale_x
,
254 surface_height(s
->ds
) * s
->scale_y
);
255 #if GTK_CHECK_VERSION(3, 0, 0)
256 gtk_widget_get_preferred_size(s
->vbox
, NULL
, &req
);
258 gtk_widget_size_request(s
->vbox
, &req
);
261 gtk_window_resize(GTK_WINDOW(s
->window
),
262 req
.width
* sx
, req
.height
* sy
);
266 static void gd_update_full_redraw(GtkDisplayState
*s
)
269 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
270 gtk_widget_queue_draw_area(s
->drawing_area
, 0, 0, ww
, wh
);
273 static void gtk_release_modifiers(GtkDisplayState
*s
)
280 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
281 keycode
= modifier_keycode
[i
];
282 if (!s
->modifier_pressed
[i
]) {
285 if (keycode
& SCANCODE_GREY
) {
286 kbd_put_keycode(SCANCODE_EMUL0
);
288 kbd_put_keycode(keycode
| SCANCODE_UP
);
289 s
->modifier_pressed
[i
] = false;
293 /** DisplayState Callbacks **/
295 static void gd_update(DisplayChangeListener
*dcl
,
296 int x
, int y
, int w
, int h
)
298 GtkDisplayState
*s
= container_of(dcl
, GtkDisplayState
, dcl
);
304 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x
, y
, w
, h
);
306 x1
= floor(x
* s
->scale_x
);
307 y1
= floor(y
* s
->scale_y
);
309 x2
= ceil(x
* s
->scale_x
+ w
* s
->scale_x
);
310 y2
= ceil(y
* s
->scale_y
+ h
* s
->scale_y
);
312 fbw
= surface_width(s
->ds
) * s
->scale_x
;
313 fbh
= surface_height(s
->ds
) * s
->scale_y
;
315 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
325 gtk_widget_queue_draw_area(s
->drawing_area
, mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
328 static void gd_refresh(DisplayChangeListener
*dcl
)
333 static void gd_switch(DisplayChangeListener
*dcl
,
334 DisplaySurface
*surface
)
336 GtkDisplayState
*s
= container_of(dcl
, GtkDisplayState
, dcl
);
341 DPRINTF("resize(width=%d, height=%d)\n",
342 surface_width(surface
), surface_height(surface
));
345 cairo_surface_destroy(s
->surface
);
349 surface_width(s
->ds
) == surface_width(surface
) &&
350 surface_height(s
->ds
) == surface_height(surface
)) {
354 switch (surface_bits_per_pixel(surface
)) {
356 kind
= CAIRO_FORMAT_A8
;
359 kind
= CAIRO_FORMAT_RGB16_565
;
362 kind
= CAIRO_FORMAT_RGB24
;
365 g_assert_not_reached();
369 stride
= cairo_format_stride_for_width(kind
, surface_width(surface
));
370 g_assert(surface_stride(surface
) == stride
);
372 s
->surface
= cairo_image_surface_create_for_data(surface_data(surface
),
374 surface_width(surface
),
375 surface_height(surface
),
376 surface_stride(surface
));
379 gd_update_windowsize(s
);
381 gd_update_full_redraw(s
);
387 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
389 GtkDisplayState
*s
= opaque
;
391 gd_update_caption(s
);
394 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
396 gd_update_cursor(container_of(notify
, GtkDisplayState
, mouse_mode_notifier
),
402 static gboolean
gd_window_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
404 GtkDisplayState
*s
= opaque
;
405 GtkAccelGroupEntry
*entries
;
407 gboolean propagate_accel
= TRUE
;
408 gboolean handled
= FALSE
;
410 entries
= gtk_accel_group_query(s
->accel_group
, key
->keyval
,
411 key
->state
, &n_entries
);
413 const char *quark
= g_quark_to_string(entries
[0].accel_path_quark
);
415 if (gd_is_grab_active(s
) && strstart(quark
, "<QEMU>/File/", NULL
)) {
416 propagate_accel
= FALSE
;
420 if (!handled
&& propagate_accel
) {
421 handled
= gtk_window_activate_key(GTK_WINDOW(widget
), key
);
424 gtk_release_modifiers(s
);
426 handled
= gtk_window_propagate_key_event(GTK_WINDOW(widget
), key
);
432 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
435 GtkDisplayState
*s
= opaque
;
438 unregister_displaychangelistener(&s
->dcl
);
446 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
448 GtkDisplayState
*s
= opaque
;
453 if (!gtk_widget_get_realized(widget
)) {
457 fbw
= surface_width(s
->ds
);
458 fbh
= surface_height(s
->ds
);
460 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
462 if (s
->full_screen
) {
463 s
->scale_x
= (double)ww
/ fbw
;
464 s
->scale_y
= (double)wh
/ fbh
;
465 } else if (s
->free_scale
) {
468 sx
= (double)ww
/ fbw
;
469 sy
= (double)wh
/ fbh
;
471 s
->scale_x
= s
->scale_y
= MIN(sx
, sy
);
485 cairo_rectangle(cr
, 0, 0, ww
, wh
);
487 /* Optionally cut out the inner area where the pixmap
488 will be drawn. This avoids 'flashing' since we're
489 not double-buffering. Note we're using the undocumented
490 behaviour of drawing the rectangle from right to left
491 to cut out the whole */
492 cairo_rectangle(cr
, mx
+ fbw
, my
,
496 cairo_scale(cr
, s
->scale_x
, s
->scale_y
);
497 cairo_set_source_surface(cr
, s
->surface
, mx
/ s
->scale_x
, my
/ s
->scale_y
);
503 #if !GTK_CHECK_VERSION(3, 0, 0)
504 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
510 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
515 expose
->area
.height
);
518 ret
= gd_draw_event(widget
, cr
, opaque
);
526 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
529 GtkDisplayState
*s
= opaque
;
536 fbw
= surface_width(s
->ds
) * s
->scale_x
;
537 fbh
= surface_height(s
->ds
) * s
->scale_y
;
539 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
549 x
= (motion
->x
- mx
) / s
->scale_x
;
550 y
= (motion
->y
- my
) / s
->scale_y
;
552 if (x
< 0 || y
< 0 ||
553 x
>= surface_width(s
->ds
) ||
554 y
>= surface_height(s
->ds
)) {
558 if (kbd_mouse_is_absolute()) {
559 dx
= x
* 0x7FFF / (surface_width(s
->ds
) - 1);
560 dy
= y
* 0x7FFF / (surface_height(s
->ds
) - 1);
561 } else if (s
->last_x
== -1 || s
->last_y
== -1) {
572 if (kbd_mouse_is_absolute() || gd_is_grab_active(s
)) {
573 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
576 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s
)) {
577 GdkScreen
*screen
= gtk_widget_get_screen(s
->drawing_area
);
578 int x
= (int)motion
->x_root
;
579 int y
= (int)motion
->y_root
;
581 /* In relative mode check to see if client pointer hit
582 * one of the screen edges, and if so move it back by
583 * 200 pixels. This is important because the pointer
584 * in the server doesn't correspond 1-for-1, and so
585 * may still be only half way across the screen. Without
586 * this warp, the server pointer would thus appear to hit
587 * an invisible wall */
594 if (x
== (gdk_screen_get_width(screen
) - 1)) {
597 if (y
== (gdk_screen_get_height(screen
) - 1)) {
601 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
602 #if GTK_CHECK_VERSION(3, 0, 0)
603 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
604 gdk_device_warp(dev
, screen
, x
, y
);
606 GdkDisplay
*display
= gtk_widget_get_display(widget
);
607 gdk_display_warp_pointer(display
, screen
, x
, y
);
617 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
620 GtkDisplayState
*s
= opaque
;
624 if (button
->button
== 1) {
626 } else if (button
->button
== 2) {
628 } else if (button
->button
== 3) {
634 if (button
->type
== GDK_BUTTON_PRESS
) {
636 } else if (button
->type
== GDK_BUTTON_RELEASE
) {
637 s
->button_mask
&= ~n
;
640 if (kbd_mouse_is_absolute()) {
641 dx
= s
->last_x
* 0x7FFF / (surface_width(s
->ds
) - 1);
642 dy
= s
->last_y
* 0x7FFF / (surface_height(s
->ds
) - 1);
648 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
653 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
655 GtkDisplayState
*s
= opaque
;
660 gdk_keycode
= key
->hardware_keycode
;
662 if (gdk_keycode
< 9) {
664 } else if (gdk_keycode
< 97) {
665 qemu_keycode
= gdk_keycode
- 8;
666 } else if (gdk_keycode
< 158) {
667 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
668 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
670 } else if (gdk_keycode
== 211) { /* backslash */
676 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
677 gdk_keycode
, qemu_keycode
,
678 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
680 for (i
= 0; i
< ARRAY_SIZE(modifier_keycode
); i
++) {
681 if (qemu_keycode
== modifier_keycode
[i
]) {
682 s
->modifier_pressed
[i
] = (key
->type
== GDK_KEY_PRESS
);
686 if (qemu_keycode
& SCANCODE_GREY
) {
687 kbd_put_keycode(SCANCODE_EMUL0
);
690 if (key
->type
== GDK_KEY_PRESS
) {
691 kbd_put_keycode(qemu_keycode
& SCANCODE_KEYCODEMASK
);
692 } else if (key
->type
== GDK_KEY_RELEASE
) {
693 kbd_put_keycode(qemu_keycode
| SCANCODE_UP
);
695 g_assert_not_reached();
701 /** Window Menu Actions **/
703 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
705 GtkDisplayState
*s
= opaque
;
707 if (s
->external_pause_update
) {
710 if (runstate_is_running()) {
717 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
719 qmp_system_reset(NULL
);
722 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
724 qmp_system_powerdown(NULL
);
727 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
732 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
734 GtkDisplayState
*s
= opaque
;
736 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vga_item
))) {
737 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), 0);
741 gtk_release_modifiers(s
);
742 for (i
= 0; i
< s
->nb_vcs
; i
++) {
743 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vc
[i
].menu_item
))) {
744 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), i
+ 1);
751 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
753 GtkDisplayState
*s
= opaque
;
755 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
756 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
758 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
762 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
764 GtkDisplayState
*s
= opaque
;
766 if (!s
->full_screen
) {
767 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
768 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
769 gtk_widget_set_size_request(s
->drawing_area
, -1, -1);
770 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
772 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), TRUE
);
774 s
->full_screen
= TRUE
;
776 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
777 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
778 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
779 gtk_widget_set_size_request(s
->drawing_area
,
780 surface_width(s
->ds
),
781 surface_height(s
->ds
));
782 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), FALSE
);
783 s
->full_screen
= FALSE
;
788 gd_update_cursor(s
, FALSE
);
791 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
793 GtkDisplayState
*s
= opaque
;
795 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
801 gd_update_windowsize(s
);
804 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
806 GtkDisplayState
*s
= opaque
;
808 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
814 s
->scale_x
= MAX(s
->scale_x
, .25);
815 s
->scale_y
= MAX(s
->scale_y
, .25);
817 gd_update_windowsize(s
);
820 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
822 GtkDisplayState
*s
= opaque
;
827 gd_update_windowsize(s
);
830 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
832 GtkDisplayState
*s
= opaque
;
834 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
835 s
->free_scale
= TRUE
;
837 s
->free_scale
= FALSE
;
840 gd_update_windowsize(s
);
841 gd_update_full_redraw(s
);
844 static void gd_grab_keyboard(GtkDisplayState
*s
)
846 #if GTK_CHECK_VERSION(3, 0, 0)
847 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
848 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
849 GList
*devices
= gdk_device_manager_list_devices(mgr
,
850 GDK_DEVICE_TYPE_MASTER
);
851 GList
*tmp
= devices
;
853 GdkDevice
*dev
= tmp
->data
;
854 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
856 gtk_widget_get_window(s
->drawing_area
),
859 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
865 g_list_free(devices
);
867 gdk_keyboard_grab(gtk_widget_get_window(s
->drawing_area
),
873 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
875 #if GTK_CHECK_VERSION(3, 0, 0)
876 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
877 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
878 GList
*devices
= gdk_device_manager_list_devices(mgr
,
879 GDK_DEVICE_TYPE_MASTER
);
880 GList
*tmp
= devices
;
882 GdkDevice
*dev
= tmp
->data
;
883 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
884 gdk_device_ungrab(dev
,
889 g_list_free(devices
);
891 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
895 static void gd_grab_pointer(GtkDisplayState
*s
)
897 #if GTK_CHECK_VERSION(3, 0, 0)
898 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
899 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
900 GList
*devices
= gdk_device_manager_list_devices(mgr
,
901 GDK_DEVICE_TYPE_MASTER
);
902 GList
*tmp
= devices
;
904 GdkDevice
*dev
= tmp
->data
;
905 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
907 gtk_widget_get_window(s
->drawing_area
),
909 FALSE
, /* All events to come to our
911 GDK_POINTER_MOTION_MASK
|
912 GDK_BUTTON_PRESS_MASK
|
913 GDK_BUTTON_RELEASE_MASK
|
914 GDK_BUTTON_MOTION_MASK
|
921 g_list_free(devices
);
923 gdk_pointer_grab(gtk_widget_get_window(s
->drawing_area
),
924 FALSE
, /* All events to come to our window directly */
925 GDK_POINTER_MOTION_MASK
|
926 GDK_BUTTON_PRESS_MASK
|
927 GDK_BUTTON_RELEASE_MASK
|
928 GDK_BUTTON_MOTION_MASK
|
930 NULL
, /* Allow cursor to move over entire desktop */
936 static void gd_ungrab_pointer(GtkDisplayState
*s
)
938 #if GTK_CHECK_VERSION(3, 0, 0)
939 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
940 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
941 GList
*devices
= gdk_device_manager_list_devices(mgr
,
942 GDK_DEVICE_TYPE_MASTER
);
943 GList
*tmp
= devices
;
945 GdkDevice
*dev
= tmp
->data
;
946 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
947 gdk_device_ungrab(dev
,
952 g_list_free(devices
);
954 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
958 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
960 GtkDisplayState
*s
= opaque
;
962 if (gd_is_grab_active(s
)) {
966 gd_ungrab_keyboard(s
);
967 gd_ungrab_pointer(s
);
970 gd_update_caption(s
);
971 gd_update_cursor(s
, FALSE
);
974 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
977 GtkDisplayState
*s
= data
;
981 if (!gtk_widget_get_realized(s
->notebook
)) {
985 last_page
= gtk_notebook_get_current_page(nb
);
988 gtk_widget_set_size_request(s
->vc
[last_page
- 1].terminal
, -1, -1);
994 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
996 } else if (s
->full_screen
) {
997 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
1002 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->vga_item
), TRUE
);
1004 VirtualConsole
*vc
= &s
->vc
[arg2
- 1];
1005 VteTerminal
*term
= VTE_TERMINAL(vc
->terminal
);
1008 width
= 80 * vte_terminal_get_char_width(term
);
1009 height
= 25 * vte_terminal_get_char_height(term
);
1011 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
1012 gtk_widget_set_size_request(vc
->terminal
, width
, height
);
1015 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
1017 gd_update_cursor(s
, TRUE
);
1020 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
1022 GtkDisplayState
*s
= data
;
1024 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
1025 gd_grab_keyboard(s
);
1031 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
1033 GtkDisplayState
*s
= data
;
1035 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
1036 gd_ungrab_keyboard(s
);
1042 static gboolean
gd_focus_out_event(GtkWidget
*widget
,
1043 GdkEventCrossing
*crossing
, gpointer data
)
1045 GtkDisplayState
*s
= data
;
1047 gtk_release_modifiers(s
);
1052 /** Virtual Console Callbacks **/
1054 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1056 VirtualConsole
*vc
= chr
->opaque
;
1058 return write(vc
->fd
, buf
, len
);
1062 static CharDriverState
*vcs
[MAX_VCS
];
1064 static CharDriverState
*gd_vc_handler(ChardevVC
*unused
)
1066 CharDriverState
*chr
;
1068 chr
= g_malloc0(sizeof(*chr
));
1069 chr
->chr_write
= gd_vc_chr_write
;
1071 vcs
[nb_vcs
++] = chr
;
1076 void early_gtk_display_init(void)
1078 register_vc_handler(gd_vc_handler
);
1081 static gboolean
gd_vc_in(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1083 VirtualConsole
*vc
= opaque
;
1084 uint8_t buffer
[1024];
1087 len
= read(vc
->fd
, buffer
, sizeof(buffer
));
1092 qemu_chr_be_write(vc
->chr
, buffer
, len
);
1097 static GSList
*gd_vc_init(GtkDisplayState
*s
, VirtualConsole
*vc
, int index
, GSList
*group
)
1102 #if VTE_CHECK_VERSION(0, 26, 0)
1106 GtkWidget
*scrolled_window
;
1107 GtkAdjustment
*vadjustment
;
1108 int master_fd
, slave_fd
, ret
;
1111 snprintf(buffer
, sizeof(buffer
), "vc%d", index
);
1112 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", index
);
1114 vc
->chr
= vcs
[index
];
1116 if (vc
->chr
->label
) {
1117 label
= vc
->chr
->label
;
1122 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, label
);
1123 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1124 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
1125 gtk_accel_map_add_entry(path
, GDK_KEY_2
+ index
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1127 vc
->terminal
= vte_terminal_new();
1129 ret
= openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
);
1130 g_assert(ret
!= -1);
1132 /* Set raw attributes on the pty. */
1133 tcgetattr(slave_fd
, &tty
);
1135 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1137 #if VTE_CHECK_VERSION(0, 26, 0)
1138 pty
= vte_pty_new_foreign(master_fd
, NULL
);
1139 vte_terminal_set_pty_object(VTE_TERMINAL(vc
->terminal
), pty
);
1141 vte_terminal_set_pty(VTE_TERMINAL(vc
->terminal
), master_fd
);
1144 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->terminal
), -1);
1146 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->terminal
));
1148 scrolled_window
= gtk_scrolled_window_new(NULL
, vadjustment
);
1149 gtk_container_add(GTK_CONTAINER(scrolled_window
), vc
->terminal
);
1151 vte_terminal_set_size(VTE_TERMINAL(vc
->terminal
), 80, 25);
1154 vc
->chr
->opaque
= vc
;
1155 vc
->scrolled_window
= scrolled_window
;
1157 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc
->scrolled_window
),
1158 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1160 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), scrolled_window
, gtk_label_new(label
));
1161 g_signal_connect(vc
->menu_item
, "activate",
1162 G_CALLBACK(gd_menu_switch_vc
), s
);
1164 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), vc
->menu_item
);
1166 qemu_chr_be_generic_open(vc
->chr
);
1167 if (vc
->chr
->init
) {
1168 vc
->chr
->init(vc
->chr
);
1171 chan
= g_io_channel_unix_new(vc
->fd
);
1172 g_io_add_watch(chan
, G_IO_IN
, gd_vc_in
, vc
);
1177 /** Window Creation **/
1179 static void gd_connect_signals(GtkDisplayState
*s
)
1181 g_signal_connect(s
->show_tabs_item
, "activate",
1182 G_CALLBACK(gd_menu_show_tabs
), s
);
1184 g_signal_connect(s
->window
, "key-press-event",
1185 G_CALLBACK(gd_window_key_event
), s
);
1186 g_signal_connect(s
->window
, "delete-event",
1187 G_CALLBACK(gd_window_close
), s
);
1189 #if GTK_CHECK_VERSION(3, 0, 0)
1190 g_signal_connect(s
->drawing_area
, "draw",
1191 G_CALLBACK(gd_draw_event
), s
);
1193 g_signal_connect(s
->drawing_area
, "expose-event",
1194 G_CALLBACK(gd_expose_event
), s
);
1196 g_signal_connect(s
->drawing_area
, "motion-notify-event",
1197 G_CALLBACK(gd_motion_event
), s
);
1198 g_signal_connect(s
->drawing_area
, "button-press-event",
1199 G_CALLBACK(gd_button_event
), s
);
1200 g_signal_connect(s
->drawing_area
, "button-release-event",
1201 G_CALLBACK(gd_button_event
), s
);
1202 g_signal_connect(s
->drawing_area
, "key-press-event",
1203 G_CALLBACK(gd_key_event
), s
);
1204 g_signal_connect(s
->drawing_area
, "key-release-event",
1205 G_CALLBACK(gd_key_event
), s
);
1207 g_signal_connect(s
->pause_item
, "activate",
1208 G_CALLBACK(gd_menu_pause
), s
);
1209 g_signal_connect(s
->reset_item
, "activate",
1210 G_CALLBACK(gd_menu_reset
), s
);
1211 g_signal_connect(s
->powerdown_item
, "activate",
1212 G_CALLBACK(gd_menu_powerdown
), s
);
1213 g_signal_connect(s
->quit_item
, "activate",
1214 G_CALLBACK(gd_menu_quit
), s
);
1215 g_signal_connect(s
->full_screen_item
, "activate",
1216 G_CALLBACK(gd_menu_full_screen
), s
);
1217 g_signal_connect(s
->zoom_in_item
, "activate",
1218 G_CALLBACK(gd_menu_zoom_in
), s
);
1219 g_signal_connect(s
->zoom_out_item
, "activate",
1220 G_CALLBACK(gd_menu_zoom_out
), s
);
1221 g_signal_connect(s
->zoom_fixed_item
, "activate",
1222 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1223 g_signal_connect(s
->zoom_fit_item
, "activate",
1224 G_CALLBACK(gd_menu_zoom_fit
), s
);
1225 g_signal_connect(s
->vga_item
, "activate",
1226 G_CALLBACK(gd_menu_switch_vc
), s
);
1227 g_signal_connect(s
->grab_item
, "activate",
1228 G_CALLBACK(gd_menu_grab_input
), s
);
1229 g_signal_connect(s
->notebook
, "switch-page",
1230 G_CALLBACK(gd_change_page
), s
);
1231 g_signal_connect(s
->drawing_area
, "enter-notify-event",
1232 G_CALLBACK(gd_enter_event
), s
);
1233 g_signal_connect(s
->drawing_area
, "leave-notify-event",
1234 G_CALLBACK(gd_leave_event
), s
);
1235 g_signal_connect(s
->drawing_area
, "focus-out-event",
1236 G_CALLBACK(gd_focus_out_event
), s
);
1239 static void gd_create_menus(GtkDisplayState
*s
)
1242 GtkAccelGroup
*accel_group
;
1243 GSList
*group
= NULL
;
1244 GtkWidget
*separator
;
1247 accel_group
= gtk_accel_group_new();
1248 s
->machine_menu
= gtk_menu_new();
1249 gtk_menu_set_accel_group(GTK_MENU(s
->machine_menu
), accel_group
);
1250 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1252 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1253 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->pause_item
);
1255 separator
= gtk_separator_menu_item_new();
1256 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1258 s
->reset_item
= gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1259 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->reset_item
);
1261 s
->powerdown_item
= gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1262 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->powerdown_item
);
1264 separator
= gtk_separator_menu_item_new();
1265 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1267 s
->quit_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, NULL
);
1268 gtk_stock_lookup(GTK_STOCK_QUIT
, &item
);
1269 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1270 "<QEMU>/Machine/Quit");
1271 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item
.keyval
, item
.modifier
);
1272 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->quit_item
);
1274 s
->view_menu
= gtk_menu_new();
1275 gtk_menu_set_accel_group(GTK_MENU(s
->view_menu
), accel_group
);
1276 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1278 s
->full_screen_item
=
1279 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN
, NULL
);
1280 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1281 "<QEMU>/View/Full Screen");
1282 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1283 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->full_screen_item
);
1285 separator
= gtk_separator_menu_item_new();
1286 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1288 s
->zoom_in_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN
, NULL
);
1289 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1290 "<QEMU>/View/Zoom In");
1291 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1292 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_in_item
);
1294 s
->zoom_out_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT
, NULL
);
1295 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1296 "<QEMU>/View/Zoom Out");
1297 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1298 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_out_item
);
1300 s
->zoom_fixed_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100
, NULL
);
1301 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1302 "<QEMU>/View/Zoom Fixed");
1303 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1304 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fixed_item
);
1306 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1307 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fit_item
);
1309 separator
= gtk_separator_menu_item_new();
1310 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1312 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1313 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_on_hover_item
);
1315 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1316 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1317 "<QEMU>/View/Grab Input");
1318 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1319 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_item
);
1321 separator
= gtk_separator_menu_item_new();
1322 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1324 s
->vga_item
= gtk_radio_menu_item_new_with_mnemonic(group
, "_VGA");
1325 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s
->vga_item
));
1326 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->vga_item
),
1328 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1329 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->vga_item
);
1331 for (i
= 0; i
< nb_vcs
; i
++) {
1332 VirtualConsole
*vc
= &s
->vc
[i
];
1334 group
= gd_vc_init(s
, vc
, i
, group
);
1338 separator
= gtk_separator_menu_item_new();
1339 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1341 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1342 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->show_tabs_item
);
1344 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1345 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1346 s
->accel_group
= accel_group
;
1348 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1350 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1352 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1353 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1356 static const DisplayChangeListenerOps dcl_ops
= {
1358 .dpy_gfx_update
= gd_update
,
1359 .dpy_gfx_switch
= gd_switch
,
1360 .dpy_refresh
= gd_refresh
,
1363 void gtk_display_init(DisplayState
*ds
)
1365 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1368 gtk_init(NULL
, NULL
);
1370 s
->dcl
.ops
= &dcl_ops
;
1372 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1373 #if GTK_CHECK_VERSION(3, 2, 0)
1374 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1376 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1378 s
->notebook
= gtk_notebook_new();
1379 s
->drawing_area
= gtk_drawing_area_new();
1380 s
->menu_bar
= gtk_menu_bar_new();
1384 s
->free_scale
= FALSE
;
1386 setlocale(LC_ALL
, "");
1387 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1390 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1392 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1393 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1394 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1396 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), s
->drawing_area
, gtk_label_new("VGA"));
1398 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
1400 GError
*error
= NULL
;
1401 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
1403 gtk_window_set_icon(GTK_WINDOW(s
->window
), pixbuf
);
1405 g_error_free(error
);
1412 gd_connect_signals(s
);
1414 gtk_widget_add_events(s
->drawing_area
,
1415 GDK_POINTER_MOTION_MASK
|
1416 GDK_BUTTON_PRESS_MASK
|
1417 GDK_BUTTON_RELEASE_MASK
|
1418 GDK_BUTTON_MOTION_MASK
|
1419 GDK_ENTER_NOTIFY_MASK
|
1420 GDK_LEAVE_NOTIFY_MASK
|
1422 GDK_KEY_PRESS_MASK
);
1423 gtk_widget_set_double_buffered(s
->drawing_area
, FALSE
);
1424 gtk_widget_set_can_focus(s
->drawing_area
, TRUE
);
1426 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1427 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1429 gd_update_caption(s
);
1431 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1432 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1434 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1436 gtk_widget_show_all(s
->window
);
1438 register_displaychangelistener(ds
, &s
->dcl
);