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 "char/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 typedef struct VirtualConsole
103 GtkWidget
*menu_item
;
105 GtkWidget
*scrolled_window
;
106 CharDriverState
*chr
;
110 typedef struct GtkDisplayState
116 GtkAccelGroup
*accel_group
;
118 GtkWidget
*machine_menu_item
;
119 GtkWidget
*machine_menu
;
120 GtkWidget
*pause_item
;
121 GtkWidget
*reset_item
;
122 GtkWidget
*powerdown_item
;
123 GtkWidget
*quit_item
;
125 GtkWidget
*view_menu_item
;
126 GtkWidget
*view_menu
;
127 GtkWidget
*full_screen_item
;
128 GtkWidget
*zoom_in_item
;
129 GtkWidget
*zoom_out_item
;
130 GtkWidget
*zoom_fixed_item
;
131 GtkWidget
*zoom_fit_item
;
132 GtkWidget
*grab_item
;
133 GtkWidget
*grab_on_hover_item
;
137 VirtualConsole vc
[MAX_VCS
];
139 GtkWidget
*show_tabs_item
;
143 GtkWidget
*drawing_area
;
144 cairo_surface_t
*surface
;
145 DisplayChangeListener dcl
;
153 gboolean full_screen
;
155 GdkCursor
*null_cursor
;
156 Notifier mouse_mode_notifier
;
159 bool external_pause_update
;
162 static GtkDisplayState
*global_state
;
164 /** Utility Functions **/
166 static bool gd_is_grab_active(GtkDisplayState
*s
)
168 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
171 static bool gd_grab_on_hover(GtkDisplayState
*s
)
173 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
176 static bool gd_on_vga(GtkDisplayState
*s
)
178 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
)) == 0;
181 static void gd_update_cursor(GtkDisplayState
*s
, gboolean override
)
186 window
= gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
));
188 on_vga
= gd_on_vga(s
);
190 if ((override
|| on_vga
) &&
191 (s
->full_screen
|| kbd_mouse_is_absolute() || gd_is_grab_active(s
))) {
192 gdk_window_set_cursor(window
, s
->null_cursor
);
194 gdk_window_set_cursor(window
, NULL
);
198 static void gd_update_caption(GtkDisplayState
*s
)
200 const char *status
= "";
202 const char *grab
= "";
203 bool is_paused
= !runstate_is_running();
205 if (gd_is_grab_active(s
)) {
206 grab
= " - Press Ctrl+Alt+G to release grab";
210 status
= " [Paused]";
212 s
->external_pause_update
= true;
213 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
215 s
->external_pause_update
= false;
218 title
= g_strdup_printf("QEMU (%s)%s%s", qemu_name
, status
, grab
);
220 title
= g_strdup_printf("QEMU%s%s", status
, grab
);
223 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
228 static void gd_update_windowsize(GtkDisplayState
*s
)
230 if (!s
->full_screen
) {
245 gtk_widget_set_size_request(s
->drawing_area
,
246 surface_width(s
->ds
) * s
->scale_x
,
247 surface_height(s
->ds
) * s
->scale_y
);
248 #if GTK_CHECK_VERSION(3, 0, 0)
249 gtk_widget_get_preferred_size(s
->vbox
, NULL
, &req
);
251 gtk_widget_size_request(s
->vbox
, &req
);
254 gtk_window_resize(GTK_WINDOW(s
->window
),
255 req
.width
* sx
, req
.height
* sy
);
259 static void gd_update_full_redraw(GtkDisplayState
*s
)
262 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
263 gtk_widget_queue_draw_area(s
->drawing_area
, 0, 0, ww
, wh
);
266 /** DisplayState Callbacks **/
268 static void gd_update(DisplayChangeListener
*dcl
,
269 int x
, int y
, int w
, int h
)
271 GtkDisplayState
*s
= container_of(dcl
, GtkDisplayState
, dcl
);
277 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x
, y
, w
, h
);
279 x1
= floor(x
* s
->scale_x
);
280 y1
= floor(y
* s
->scale_y
);
282 x2
= ceil(x
* s
->scale_x
+ w
* s
->scale_x
);
283 y2
= ceil(y
* s
->scale_y
+ h
* s
->scale_y
);
285 fbw
= surface_width(s
->ds
) * s
->scale_x
;
286 fbh
= surface_height(s
->ds
) * s
->scale_y
;
288 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
298 gtk_widget_queue_draw_area(s
->drawing_area
, mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
301 static void gd_refresh(DisplayChangeListener
*dcl
)
306 static void gd_switch(DisplayChangeListener
*dcl
,
307 DisplaySurface
*surface
)
309 GtkDisplayState
*s
= container_of(dcl
, GtkDisplayState
, dcl
);
314 DPRINTF("resize(width=%d, height=%d)\n",
315 surface_width(surface
), surface_height(surface
));
318 cairo_surface_destroy(s
->surface
);
322 surface_width(s
->ds
) == surface_width(surface
) &&
323 surface_height(s
->ds
) == surface_height(surface
)) {
327 switch (surface_bits_per_pixel(surface
)) {
329 kind
= CAIRO_FORMAT_A8
;
332 kind
= CAIRO_FORMAT_RGB16_565
;
335 kind
= CAIRO_FORMAT_RGB24
;
338 g_assert_not_reached();
342 stride
= cairo_format_stride_for_width(kind
, surface_width(surface
));
343 g_assert(surface_stride(surface
) == stride
);
345 s
->surface
= cairo_image_surface_create_for_data(surface_data(surface
),
347 surface_width(surface
),
348 surface_height(surface
),
349 surface_stride(surface
));
352 gd_update_windowsize(s
);
354 gd_update_full_redraw(s
);
360 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
362 GtkDisplayState
*s
= opaque
;
364 gd_update_caption(s
);
367 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
369 gd_update_cursor(container_of(notify
, GtkDisplayState
, mouse_mode_notifier
),
375 static gboolean
gd_window_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
377 GtkDisplayState
*s
= opaque
;
378 GtkAccelGroupEntry
*entries
;
380 gboolean propagate_accel
= TRUE
;
381 gboolean handled
= FALSE
;
383 entries
= gtk_accel_group_query(s
->accel_group
, key
->keyval
,
384 key
->state
, &n_entries
);
386 const char *quark
= g_quark_to_string(entries
[0].accel_path_quark
);
388 if (gd_is_grab_active(s
) && strstart(quark
, "<QEMU>/File/", NULL
)) {
389 propagate_accel
= FALSE
;
393 if (!handled
&& propagate_accel
) {
394 handled
= gtk_window_activate_key(GTK_WINDOW(widget
), key
);
398 handled
= gtk_window_propagate_key_event(GTK_WINDOW(widget
), key
);
404 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
407 GtkDisplayState
*s
= opaque
;
410 unregister_displaychangelistener(&s
->dcl
);
418 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
420 GtkDisplayState
*s
= opaque
;
425 if (!gtk_widget_get_realized(widget
)) {
429 fbw
= surface_width(s
->ds
);
430 fbh
= surface_height(s
->ds
);
432 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
434 if (s
->full_screen
) {
435 s
->scale_x
= (double)ww
/ fbw
;
436 s
->scale_y
= (double)wh
/ fbh
;
437 } else if (s
->free_scale
) {
440 sx
= (double)ww
/ fbw
;
441 sy
= (double)wh
/ fbh
;
443 s
->scale_x
= s
->scale_y
= MIN(sx
, sy
);
457 cairo_rectangle(cr
, 0, 0, ww
, wh
);
459 /* Optionally cut out the inner area where the pixmap
460 will be drawn. This avoids 'flashing' since we're
461 not double-buffering. Note we're using the undocumented
462 behaviour of drawing the rectangle from right to left
463 to cut out the whole */
464 cairo_rectangle(cr
, mx
+ fbw
, my
,
468 cairo_scale(cr
, s
->scale_x
, s
->scale_y
);
469 cairo_set_source_surface(cr
, s
->surface
, mx
/ s
->scale_x
, my
/ s
->scale_y
);
475 #if !GTK_CHECK_VERSION(3, 0, 0)
476 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
482 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
487 expose
->area
.height
);
490 ret
= gd_draw_event(widget
, cr
, opaque
);
498 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
501 GtkDisplayState
*s
= opaque
;
508 fbw
= surface_width(s
->ds
) * s
->scale_x
;
509 fbh
= surface_height(s
->ds
) * s
->scale_y
;
511 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
521 x
= (motion
->x
- mx
) / s
->scale_x
;
522 y
= (motion
->y
- my
) / s
->scale_y
;
524 if (x
< 0 || y
< 0 ||
525 x
>= surface_width(s
->ds
) ||
526 y
>= surface_height(s
->ds
)) {
530 if (kbd_mouse_is_absolute()) {
531 dx
= x
* 0x7FFF / (surface_width(s
->ds
) - 1);
532 dy
= y
* 0x7FFF / (surface_height(s
->ds
) - 1);
533 } else if (s
->last_x
== -1 || s
->last_y
== -1) {
544 if (kbd_mouse_is_absolute() || gd_is_grab_active(s
)) {
545 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
548 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s
)) {
549 GdkScreen
*screen
= gtk_widget_get_screen(s
->drawing_area
);
550 int x
= (int)motion
->x_root
;
551 int y
= (int)motion
->y_root
;
553 /* In relative mode check to see if client pointer hit
554 * one of the screen edges, and if so move it back by
555 * 200 pixels. This is important because the pointer
556 * in the server doesn't correspond 1-for-1, and so
557 * may still be only half way across the screen. Without
558 * this warp, the server pointer would thus appear to hit
559 * an invisible wall */
566 if (x
== (gdk_screen_get_width(screen
) - 1)) {
569 if (y
== (gdk_screen_get_height(screen
) - 1)) {
573 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
574 #if GTK_CHECK_VERSION(3, 0, 0)
575 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
576 gdk_device_warp(dev
, screen
, x
, y
);
578 GdkDisplay
*display
= gtk_widget_get_display(widget
);
579 gdk_display_warp_pointer(display
, screen
, x
, y
);
589 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
592 GtkDisplayState
*s
= opaque
;
596 if (button
->button
== 1) {
598 } else if (button
->button
== 2) {
600 } else if (button
->button
== 3) {
606 if (button
->type
== GDK_BUTTON_PRESS
) {
608 } else if (button
->type
== GDK_BUTTON_RELEASE
) {
609 s
->button_mask
&= ~n
;
612 if (kbd_mouse_is_absolute()) {
613 dx
= s
->last_x
* 0x7FFF / (surface_width(s
->ds
) - 1);
614 dy
= s
->last_y
* 0x7FFF / (surface_height(s
->ds
) - 1);
620 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
625 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
630 gdk_keycode
= key
->hardware_keycode
;
632 if (gdk_keycode
< 9) {
634 } else if (gdk_keycode
< 97) {
635 qemu_keycode
= gdk_keycode
- 8;
636 } else if (gdk_keycode
< 158) {
637 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
638 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
640 } else if (gdk_keycode
== 211) { /* backslash */
646 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
647 gdk_keycode
, qemu_keycode
,
648 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
650 if (qemu_keycode
& SCANCODE_GREY
) {
651 kbd_put_keycode(SCANCODE_EMUL0
);
654 if (key
->type
== GDK_KEY_PRESS
) {
655 kbd_put_keycode(qemu_keycode
& SCANCODE_KEYCODEMASK
);
656 } else if (key
->type
== GDK_KEY_RELEASE
) {
657 kbd_put_keycode(qemu_keycode
| SCANCODE_UP
);
659 g_assert_not_reached();
665 /** Window Menu Actions **/
667 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
669 GtkDisplayState
*s
= opaque
;
671 if (s
->external_pause_update
) {
674 if (runstate_is_running()) {
681 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
683 qmp_system_reset(NULL
);
686 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
688 qmp_system_powerdown(NULL
);
691 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
696 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
698 GtkDisplayState
*s
= opaque
;
700 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vga_item
))) {
701 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), 0);
705 for (i
= 0; i
< s
->nb_vcs
; i
++) {
706 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vc
[i
].menu_item
))) {
707 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), i
+ 1);
714 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
716 GtkDisplayState
*s
= opaque
;
718 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
719 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
721 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
725 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
727 GtkDisplayState
*s
= opaque
;
729 if (!s
->full_screen
) {
730 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
731 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
732 gtk_widget_set_size_request(s
->drawing_area
, -1, -1);
733 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
735 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), TRUE
);
737 s
->full_screen
= TRUE
;
739 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
740 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
741 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
742 gtk_widget_set_size_request(s
->drawing_area
,
743 surface_width(s
->ds
),
744 surface_height(s
->ds
));
745 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), FALSE
);
746 s
->full_screen
= FALSE
;
751 gd_update_cursor(s
, FALSE
);
754 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
756 GtkDisplayState
*s
= opaque
;
758 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
764 gd_update_windowsize(s
);
767 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
769 GtkDisplayState
*s
= opaque
;
771 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
777 s
->scale_x
= MAX(s
->scale_x
, .25);
778 s
->scale_y
= MAX(s
->scale_y
, .25);
780 gd_update_windowsize(s
);
783 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
785 GtkDisplayState
*s
= opaque
;
790 gd_update_windowsize(s
);
793 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
795 GtkDisplayState
*s
= opaque
;
797 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
798 s
->free_scale
= TRUE
;
800 s
->free_scale
= FALSE
;
803 gd_update_windowsize(s
);
804 gd_update_full_redraw(s
);
807 static void gd_grab_keyboard(GtkDisplayState
*s
)
809 #if GTK_CHECK_VERSION(3, 0, 0)
810 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
811 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
812 GList
*devices
= gdk_device_manager_list_devices(mgr
,
813 GDK_DEVICE_TYPE_MASTER
);
814 GList
*tmp
= devices
;
816 GdkDevice
*dev
= tmp
->data
;
817 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
819 gtk_widget_get_window(s
->drawing_area
),
822 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
828 g_list_free(devices
);
830 gdk_keyboard_grab(gtk_widget_get_window(s
->drawing_area
),
836 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
838 #if GTK_CHECK_VERSION(3, 0, 0)
839 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
840 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
841 GList
*devices
= gdk_device_manager_list_devices(mgr
,
842 GDK_DEVICE_TYPE_MASTER
);
843 GList
*tmp
= devices
;
845 GdkDevice
*dev
= tmp
->data
;
846 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
847 gdk_device_ungrab(dev
,
852 g_list_free(devices
);
854 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
858 static void gd_grab_pointer(GtkDisplayState
*s
)
860 #if GTK_CHECK_VERSION(3, 0, 0)
861 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
862 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
863 GList
*devices
= gdk_device_manager_list_devices(mgr
,
864 GDK_DEVICE_TYPE_MASTER
);
865 GList
*tmp
= devices
;
867 GdkDevice
*dev
= tmp
->data
;
868 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
870 gtk_widget_get_window(s
->drawing_area
),
872 FALSE
, /* All events to come to our
874 GDK_POINTER_MOTION_MASK
|
875 GDK_BUTTON_PRESS_MASK
|
876 GDK_BUTTON_RELEASE_MASK
|
877 GDK_BUTTON_MOTION_MASK
|
884 g_list_free(devices
);
886 gdk_pointer_grab(gtk_widget_get_window(s
->drawing_area
),
887 FALSE
, /* All events to come to our window directly */
888 GDK_POINTER_MOTION_MASK
|
889 GDK_BUTTON_PRESS_MASK
|
890 GDK_BUTTON_RELEASE_MASK
|
891 GDK_BUTTON_MOTION_MASK
|
893 NULL
, /* Allow cursor to move over entire desktop */
899 static void gd_ungrab_pointer(GtkDisplayState
*s
)
901 #if GTK_CHECK_VERSION(3, 0, 0)
902 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
903 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
904 GList
*devices
= gdk_device_manager_list_devices(mgr
,
905 GDK_DEVICE_TYPE_MASTER
);
906 GList
*tmp
= devices
;
908 GdkDevice
*dev
= tmp
->data
;
909 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
910 gdk_device_ungrab(dev
,
915 g_list_free(devices
);
917 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
921 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
923 GtkDisplayState
*s
= opaque
;
925 if (gd_is_grab_active(s
)) {
929 gd_ungrab_keyboard(s
);
930 gd_ungrab_pointer(s
);
933 gd_update_caption(s
);
934 gd_update_cursor(s
, FALSE
);
937 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
940 GtkDisplayState
*s
= data
;
944 if (!gtk_widget_get_realized(s
->notebook
)) {
948 last_page
= gtk_notebook_get_current_page(nb
);
951 gtk_widget_set_size_request(s
->vc
[last_page
- 1].terminal
, -1, -1);
957 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
959 } else if (s
->full_screen
) {
960 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
965 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->vga_item
), TRUE
);
967 VirtualConsole
*vc
= &s
->vc
[arg2
- 1];
968 VteTerminal
*term
= VTE_TERMINAL(vc
->terminal
);
971 width
= 80 * vte_terminal_get_char_width(term
);
972 height
= 25 * vte_terminal_get_char_height(term
);
974 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
975 gtk_widget_set_size_request(vc
->terminal
, width
, height
);
978 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
980 gd_update_cursor(s
, TRUE
);
983 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
985 GtkDisplayState
*s
= data
;
987 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
994 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
996 GtkDisplayState
*s
= data
;
998 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
999 gd_ungrab_keyboard(s
);
1005 /** Virtual Console Callbacks **/
1007 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1009 VirtualConsole
*vc
= chr
->opaque
;
1011 return write(vc
->fd
, buf
, len
);
1015 static CharDriverState
*vcs
[MAX_VCS
];
1017 static CharDriverState
*gd_vc_handler(ChardevVC
*unused
)
1019 CharDriverState
*chr
;
1021 chr
= g_malloc0(sizeof(*chr
));
1022 chr
->chr_write
= gd_vc_chr_write
;
1024 vcs
[nb_vcs
++] = chr
;
1029 void early_gtk_display_init(void)
1031 register_vc_handler(gd_vc_handler
);
1034 static gboolean
gd_vc_in(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1036 VirtualConsole
*vc
= opaque
;
1037 uint8_t buffer
[1024];
1040 len
= read(vc
->fd
, buffer
, sizeof(buffer
));
1045 qemu_chr_be_write(vc
->chr
, buffer
, len
);
1050 static GSList
*gd_vc_init(GtkDisplayState
*s
, VirtualConsole
*vc
, int index
, GSList
*group
)
1055 #if VTE_CHECK_VERSION(0, 26, 0)
1059 GtkWidget
*scrolled_window
;
1060 GtkAdjustment
*vadjustment
;
1061 int master_fd
, slave_fd
, ret
;
1064 snprintf(buffer
, sizeof(buffer
), "vc%d", index
);
1065 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", index
);
1067 vc
->chr
= vcs
[index
];
1069 if (vc
->chr
->label
) {
1070 label
= vc
->chr
->label
;
1075 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, label
);
1076 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1077 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
1078 gtk_accel_map_add_entry(path
, GDK_KEY_2
+ index
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1080 vc
->terminal
= vte_terminal_new();
1082 ret
= openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
);
1083 g_assert(ret
!= -1);
1085 /* Set raw attributes on the pty. */
1086 tcgetattr(slave_fd
, &tty
);
1088 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1090 #if VTE_CHECK_VERSION(0, 26, 0)
1091 pty
= vte_pty_new_foreign(master_fd
, NULL
);
1092 vte_terminal_set_pty_object(VTE_TERMINAL(vc
->terminal
), pty
);
1094 vte_terminal_set_pty(VTE_TERMINAL(vc
->terminal
), master_fd
);
1097 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->terminal
), -1);
1099 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->terminal
));
1101 scrolled_window
= gtk_scrolled_window_new(NULL
, vadjustment
);
1102 gtk_container_add(GTK_CONTAINER(scrolled_window
), vc
->terminal
);
1104 vte_terminal_set_size(VTE_TERMINAL(vc
->terminal
), 80, 25);
1107 vc
->chr
->opaque
= vc
;
1108 vc
->scrolled_window
= scrolled_window
;
1110 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc
->scrolled_window
),
1111 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1113 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), scrolled_window
, gtk_label_new(label
));
1114 g_signal_connect(vc
->menu_item
, "activate",
1115 G_CALLBACK(gd_menu_switch_vc
), s
);
1117 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), vc
->menu_item
);
1119 qemu_chr_generic_open(vc
->chr
);
1120 if (vc
->chr
->init
) {
1121 vc
->chr
->init(vc
->chr
);
1124 chan
= g_io_channel_unix_new(vc
->fd
);
1125 g_io_add_watch(chan
, G_IO_IN
, gd_vc_in
, vc
);
1130 /** Window Creation **/
1132 static void gd_connect_signals(GtkDisplayState
*s
)
1134 g_signal_connect(s
->show_tabs_item
, "activate",
1135 G_CALLBACK(gd_menu_show_tabs
), s
);
1137 g_signal_connect(s
->window
, "key-press-event",
1138 G_CALLBACK(gd_window_key_event
), s
);
1139 g_signal_connect(s
->window
, "delete-event",
1140 G_CALLBACK(gd_window_close
), s
);
1142 #if GTK_CHECK_VERSION(3, 0, 0)
1143 g_signal_connect(s
->drawing_area
, "draw",
1144 G_CALLBACK(gd_draw_event
), s
);
1146 g_signal_connect(s
->drawing_area
, "expose-event",
1147 G_CALLBACK(gd_expose_event
), s
);
1149 g_signal_connect(s
->drawing_area
, "motion-notify-event",
1150 G_CALLBACK(gd_motion_event
), s
);
1151 g_signal_connect(s
->drawing_area
, "button-press-event",
1152 G_CALLBACK(gd_button_event
), s
);
1153 g_signal_connect(s
->drawing_area
, "button-release-event",
1154 G_CALLBACK(gd_button_event
), s
);
1155 g_signal_connect(s
->drawing_area
, "key-press-event",
1156 G_CALLBACK(gd_key_event
), s
);
1157 g_signal_connect(s
->drawing_area
, "key-release-event",
1158 G_CALLBACK(gd_key_event
), s
);
1160 g_signal_connect(s
->pause_item
, "activate",
1161 G_CALLBACK(gd_menu_pause
), s
);
1162 g_signal_connect(s
->reset_item
, "activate",
1163 G_CALLBACK(gd_menu_reset
), s
);
1164 g_signal_connect(s
->powerdown_item
, "activate",
1165 G_CALLBACK(gd_menu_powerdown
), s
);
1166 g_signal_connect(s
->quit_item
, "activate",
1167 G_CALLBACK(gd_menu_quit
), s
);
1168 g_signal_connect(s
->full_screen_item
, "activate",
1169 G_CALLBACK(gd_menu_full_screen
), s
);
1170 g_signal_connect(s
->zoom_in_item
, "activate",
1171 G_CALLBACK(gd_menu_zoom_in
), s
);
1172 g_signal_connect(s
->zoom_out_item
, "activate",
1173 G_CALLBACK(gd_menu_zoom_out
), s
);
1174 g_signal_connect(s
->zoom_fixed_item
, "activate",
1175 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1176 g_signal_connect(s
->zoom_fit_item
, "activate",
1177 G_CALLBACK(gd_menu_zoom_fit
), s
);
1178 g_signal_connect(s
->vga_item
, "activate",
1179 G_CALLBACK(gd_menu_switch_vc
), s
);
1180 g_signal_connect(s
->grab_item
, "activate",
1181 G_CALLBACK(gd_menu_grab_input
), s
);
1182 g_signal_connect(s
->notebook
, "switch-page",
1183 G_CALLBACK(gd_change_page
), s
);
1184 g_signal_connect(s
->drawing_area
, "enter-notify-event",
1185 G_CALLBACK(gd_enter_event
), s
);
1186 g_signal_connect(s
->drawing_area
, "leave-notify-event",
1187 G_CALLBACK(gd_leave_event
), s
);
1190 static void gd_create_menus(GtkDisplayState
*s
)
1193 GtkAccelGroup
*accel_group
;
1194 GSList
*group
= NULL
;
1195 GtkWidget
*separator
;
1198 accel_group
= gtk_accel_group_new();
1199 s
->machine_menu
= gtk_menu_new();
1200 gtk_menu_set_accel_group(GTK_MENU(s
->machine_menu
), accel_group
);
1201 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1203 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1204 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->pause_item
);
1206 separator
= gtk_separator_menu_item_new();
1207 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1209 s
->reset_item
= gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1210 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->reset_item
);
1212 s
->powerdown_item
= gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1213 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->powerdown_item
);
1215 separator
= gtk_separator_menu_item_new();
1216 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1218 s
->quit_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, NULL
);
1219 gtk_stock_lookup(GTK_STOCK_QUIT
, &item
);
1220 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1221 "<QEMU>/Machine/Quit");
1222 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item
.keyval
, item
.modifier
);
1223 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->quit_item
);
1225 s
->view_menu
= gtk_menu_new();
1226 gtk_menu_set_accel_group(GTK_MENU(s
->view_menu
), accel_group
);
1227 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1229 s
->full_screen_item
=
1230 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN
, NULL
);
1231 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1232 "<QEMU>/View/Full Screen");
1233 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1234 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->full_screen_item
);
1236 separator
= gtk_separator_menu_item_new();
1237 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1239 s
->zoom_in_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN
, NULL
);
1240 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1241 "<QEMU>/View/Zoom In");
1242 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1243 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_in_item
);
1245 s
->zoom_out_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT
, NULL
);
1246 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1247 "<QEMU>/View/Zoom Out");
1248 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1249 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_out_item
);
1251 s
->zoom_fixed_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100
, NULL
);
1252 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1253 "<QEMU>/View/Zoom Fixed");
1254 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1255 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fixed_item
);
1257 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1258 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fit_item
);
1260 separator
= gtk_separator_menu_item_new();
1261 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1263 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1264 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_on_hover_item
);
1266 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1267 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1268 "<QEMU>/View/Grab Input");
1269 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1270 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_item
);
1272 separator
= gtk_separator_menu_item_new();
1273 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1275 s
->vga_item
= gtk_radio_menu_item_new_with_mnemonic(group
, "_VGA");
1276 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s
->vga_item
));
1277 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->vga_item
),
1279 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1280 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->vga_item
);
1282 for (i
= 0; i
< nb_vcs
; i
++) {
1283 VirtualConsole
*vc
= &s
->vc
[i
];
1285 group
= gd_vc_init(s
, vc
, i
, group
);
1289 separator
= gtk_separator_menu_item_new();
1290 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1292 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1293 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->show_tabs_item
);
1295 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1296 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1297 s
->accel_group
= accel_group
;
1299 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1301 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1303 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1304 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1307 static const DisplayChangeListenerOps dcl_ops
= {
1309 .dpy_gfx_update
= gd_update
,
1310 .dpy_gfx_switch
= gd_switch
,
1311 .dpy_refresh
= gd_refresh
,
1314 void gtk_display_init(DisplayState
*ds
)
1316 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1318 gtk_init(NULL
, NULL
);
1320 s
->dcl
.ops
= &dcl_ops
;
1322 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1323 #if GTK_CHECK_VERSION(3, 2, 0)
1324 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1326 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1328 s
->notebook
= gtk_notebook_new();
1329 s
->drawing_area
= gtk_drawing_area_new();
1330 s
->menu_bar
= gtk_menu_bar_new();
1334 s
->free_scale
= FALSE
;
1336 setlocale(LC_ALL
, "");
1337 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1340 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1342 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1343 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1344 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1346 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), s
->drawing_area
, gtk_label_new("VGA"));
1350 gd_connect_signals(s
);
1352 gtk_widget_add_events(s
->drawing_area
,
1353 GDK_POINTER_MOTION_MASK
|
1354 GDK_BUTTON_PRESS_MASK
|
1355 GDK_BUTTON_RELEASE_MASK
|
1356 GDK_BUTTON_MOTION_MASK
|
1357 GDK_ENTER_NOTIFY_MASK
|
1358 GDK_LEAVE_NOTIFY_MASK
|
1360 GDK_KEY_PRESS_MASK
);
1361 gtk_widget_set_double_buffered(s
->drawing_area
, FALSE
);
1362 gtk_widget_set_can_focus(s
->drawing_area
, TRUE
);
1364 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1365 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1367 gd_update_caption(s
);
1369 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1370 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1372 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1374 gtk_widget_show_all(s
->window
);
1376 register_displaychangelistener(ds
, &s
->dcl
);