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 /** DisplayState Callbacks **/
230 static void gd_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
232 GtkDisplayState
*s
= ds
->opaque
;
238 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x
, y
, w
, h
);
240 x1
= floor(x
* s
->scale_x
);
241 y1
= floor(y
* s
->scale_y
);
243 x2
= ceil(x
* s
->scale_x
+ w
* s
->scale_x
);
244 y2
= ceil(y
* s
->scale_y
+ h
* s
->scale_y
);
246 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
247 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
249 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
259 gtk_widget_queue_draw_area(s
->drawing_area
, mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
262 static void gd_refresh(DisplayState
*ds
)
267 static void gd_resize(DisplayState
*ds
)
269 GtkDisplayState
*s
= ds
->opaque
;
273 DPRINTF("resize(width=%d, height=%d)\n",
274 ds_get_width(ds
), ds_get_height(ds
));
277 cairo_surface_destroy(s
->surface
);
280 switch (ds
->surface
->pf
.bits_per_pixel
) {
282 kind
= CAIRO_FORMAT_A8
;
285 kind
= CAIRO_FORMAT_RGB16_565
;
288 kind
= CAIRO_FORMAT_RGB24
;
291 g_assert_not_reached();
295 stride
= cairo_format_stride_for_width(kind
, ds_get_width(ds
));
296 g_assert(ds_get_linesize(ds
) == stride
);
298 s
->surface
= cairo_image_surface_create_for_data(ds_get_data(ds
),
302 ds_get_linesize(ds
));
304 if (!s
->full_screen
) {
319 gtk_widget_set_size_request(s
->drawing_area
,
320 ds_get_width(ds
) * s
->scale_x
,
321 ds_get_height(ds
) * s
->scale_y
);
322 #if GTK_CHECK_VERSION(3, 0, 0)
323 gtk_widget_get_preferred_size(s
->vbox
, NULL
, &req
);
325 gtk_widget_size_request(s
->vbox
, &req
);
328 gtk_window_resize(GTK_WINDOW(s
->window
),
329 req
.width
* sx
, req
.height
* sy
);
335 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
337 GtkDisplayState
*s
= opaque
;
339 gd_update_caption(s
);
342 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
344 gd_update_cursor(container_of(notify
, GtkDisplayState
, mouse_mode_notifier
),
350 static gboolean
gd_window_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
352 GtkDisplayState
*s
= opaque
;
353 GtkAccelGroupEntry
*entries
;
355 gboolean propagate_accel
= TRUE
;
356 gboolean handled
= FALSE
;
358 entries
= gtk_accel_group_query(s
->accel_group
, key
->keyval
,
359 key
->state
, &n_entries
);
361 const char *quark
= g_quark_to_string(entries
[0].accel_path_quark
);
363 if (gd_is_grab_active(s
) && strstart(quark
, "<QEMU>/File/", NULL
)) {
364 propagate_accel
= FALSE
;
368 if (!handled
&& propagate_accel
) {
369 handled
= gtk_window_activate_key(GTK_WINDOW(widget
), key
);
373 handled
= gtk_window_propagate_key_event(GTK_WINDOW(widget
), key
);
379 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
382 GtkDisplayState
*s
= opaque
;
385 unregister_displaychangelistener(s
->ds
, &s
->dcl
);
393 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
395 GtkDisplayState
*s
= opaque
;
400 if (!gtk_widget_get_realized(widget
)) {
404 fbw
= ds_get_width(s
->ds
);
405 fbh
= ds_get_height(s
->ds
);
407 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
409 if (s
->full_screen
) {
410 s
->scale_x
= (double)ww
/ fbw
;
411 s
->scale_y
= (double)wh
/ fbh
;
412 } else if (s
->free_scale
) {
415 sx
= (double)ww
/ fbw
;
416 sy
= (double)wh
/ fbh
;
418 s
->scale_x
= s
->scale_y
= MIN(sx
, sy
);
432 cairo_rectangle(cr
, 0, 0, ww
, wh
);
434 /* Optionally cut out the inner area where the pixmap
435 will be drawn. This avoids 'flashing' since we're
436 not double-buffering. Note we're using the undocumented
437 behaviour of drawing the rectangle from right to left
438 to cut out the whole */
439 cairo_rectangle(cr
, mx
+ fbw
, my
,
443 cairo_scale(cr
, s
->scale_x
, s
->scale_y
);
444 cairo_set_source_surface(cr
, s
->surface
, mx
/ s
->scale_x
, my
/ s
->scale_y
);
450 #if !GTK_CHECK_VERSION(3, 0, 0)
451 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
457 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
462 expose
->area
.height
);
465 ret
= gd_draw_event(widget
, cr
, opaque
);
473 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
476 GtkDisplayState
*s
= opaque
;
483 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
484 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
486 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
496 x
= (motion
->x
- mx
) / s
->scale_x
;
497 y
= (motion
->y
- my
) / s
->scale_y
;
499 if (x
< 0 || y
< 0 ||
500 x
>= ds_get_width(s
->ds
) ||
501 y
>= ds_get_height(s
->ds
)) {
505 if (kbd_mouse_is_absolute()) {
506 dx
= x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
507 dy
= y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
508 } else if (s
->last_x
== -1 || s
->last_y
== -1) {
519 if (kbd_mouse_is_absolute() || gd_is_grab_active(s
)) {
520 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
523 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s
)) {
524 GdkScreen
*screen
= gtk_widget_get_screen(s
->drawing_area
);
525 int x
= (int)motion
->x_root
;
526 int y
= (int)motion
->y_root
;
528 /* In relative mode check to see if client pointer hit
529 * one of the screen edges, and if so move it back by
530 * 200 pixels. This is important because the pointer
531 * in the server doesn't correspond 1-for-1, and so
532 * may still be only half way across the screen. Without
533 * this warp, the server pointer would thus appear to hit
534 * an invisible wall */
541 if (x
== (gdk_screen_get_width(screen
) - 1)) {
544 if (y
== (gdk_screen_get_height(screen
) - 1)) {
548 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
549 #if GTK_CHECK_VERSION(3, 0, 0)
550 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
551 gdk_device_warp(dev
, screen
, x
, y
);
553 GdkDisplay
*display
= gtk_widget_get_display(widget
);
554 gdk_display_warp_pointer(display
, screen
, x
, y
);
564 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
567 GtkDisplayState
*s
= opaque
;
571 if (button
->button
== 1) {
573 } else if (button
->button
== 2) {
575 } else if (button
->button
== 3) {
581 if (button
->type
== GDK_BUTTON_PRESS
) {
583 } else if (button
->type
== GDK_BUTTON_RELEASE
) {
584 s
->button_mask
&= ~n
;
587 if (kbd_mouse_is_absolute()) {
588 dx
= s
->last_x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
589 dy
= s
->last_y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
595 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
600 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
605 gdk_keycode
= key
->hardware_keycode
;
607 if (gdk_keycode
< 9) {
609 } else if (gdk_keycode
< 97) {
610 qemu_keycode
= gdk_keycode
- 8;
611 } else if (gdk_keycode
< 158) {
612 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
613 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
615 } else if (gdk_keycode
== 211) { /* backslash */
621 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
622 gdk_keycode
, qemu_keycode
,
623 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
625 if (qemu_keycode
& SCANCODE_GREY
) {
626 kbd_put_keycode(SCANCODE_EMUL0
);
629 if (key
->type
== GDK_KEY_PRESS
) {
630 kbd_put_keycode(qemu_keycode
& SCANCODE_KEYCODEMASK
);
631 } else if (key
->type
== GDK_KEY_RELEASE
) {
632 kbd_put_keycode(qemu_keycode
| SCANCODE_UP
);
634 g_assert_not_reached();
640 /** Window Menu Actions **/
642 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
644 GtkDisplayState
*s
= opaque
;
646 if (s
->external_pause_update
) {
649 if (runstate_is_running()) {
656 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
658 qmp_system_reset(NULL
);
661 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
663 qmp_system_powerdown(NULL
);
666 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
671 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
673 GtkDisplayState
*s
= opaque
;
675 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vga_item
))) {
676 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), 0);
680 for (i
= 0; i
< s
->nb_vcs
; i
++) {
681 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vc
[i
].menu_item
))) {
682 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), i
+ 1);
689 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
691 GtkDisplayState
*s
= opaque
;
693 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
694 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
696 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
700 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
702 GtkDisplayState
*s
= opaque
;
704 if (!s
->full_screen
) {
705 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
706 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
707 gtk_widget_set_size_request(s
->drawing_area
, -1, -1);
708 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
710 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), TRUE
);
712 s
->full_screen
= TRUE
;
714 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
715 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
716 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
717 gtk_widget_set_size_request(s
->drawing_area
,
718 ds_get_width(s
->ds
), ds_get_height(s
->ds
));
719 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), FALSE
);
720 s
->full_screen
= FALSE
;
725 gd_update_cursor(s
, FALSE
);
728 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
730 GtkDisplayState
*s
= opaque
;
732 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
741 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
743 GtkDisplayState
*s
= opaque
;
745 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
751 s
->scale_x
= MAX(s
->scale_x
, .25);
752 s
->scale_y
= MAX(s
->scale_y
, .25);
757 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
759 GtkDisplayState
*s
= opaque
;
767 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
769 GtkDisplayState
*s
= opaque
;
772 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
773 s
->free_scale
= TRUE
;
775 s
->free_scale
= FALSE
;
780 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
781 gtk_widget_queue_draw_area(s
->drawing_area
, 0, 0, ww
, wh
);
784 static void gd_grab_keyboard(GtkDisplayState
*s
)
786 #if GTK_CHECK_VERSION(3, 0, 0)
787 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
788 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
789 GList
*devices
= gdk_device_manager_list_devices(mgr
,
790 GDK_DEVICE_TYPE_MASTER
);
791 GList
*tmp
= devices
;
793 GdkDevice
*dev
= tmp
->data
;
794 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
796 gtk_widget_get_window(s
->drawing_area
),
799 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
805 g_list_free(devices
);
807 gdk_keyboard_grab(gtk_widget_get_window(s
->drawing_area
),
813 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
815 #if GTK_CHECK_VERSION(3, 0, 0)
816 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
817 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
818 GList
*devices
= gdk_device_manager_list_devices(mgr
,
819 GDK_DEVICE_TYPE_MASTER
);
820 GList
*tmp
= devices
;
822 GdkDevice
*dev
= tmp
->data
;
823 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
824 gdk_device_ungrab(dev
,
829 g_list_free(devices
);
831 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
835 static void gd_grab_pointer(GtkDisplayState
*s
)
837 #if GTK_CHECK_VERSION(3, 0, 0)
838 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
839 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
840 GList
*devices
= gdk_device_manager_list_devices(mgr
,
841 GDK_DEVICE_TYPE_MASTER
);
842 GList
*tmp
= devices
;
844 GdkDevice
*dev
= tmp
->data
;
845 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
847 gtk_widget_get_window(s
->drawing_area
),
849 FALSE
, /* All events to come to our
851 GDK_POINTER_MOTION_MASK
|
852 GDK_BUTTON_PRESS_MASK
|
853 GDK_BUTTON_RELEASE_MASK
|
854 GDK_BUTTON_MOTION_MASK
|
861 g_list_free(devices
);
863 gdk_pointer_grab(gtk_widget_get_window(s
->drawing_area
),
864 FALSE
, /* All events to come to our window directly */
865 GDK_POINTER_MOTION_MASK
|
866 GDK_BUTTON_PRESS_MASK
|
867 GDK_BUTTON_RELEASE_MASK
|
868 GDK_BUTTON_MOTION_MASK
|
870 NULL
, /* Allow cursor to move over entire desktop */
876 static void gd_ungrab_pointer(GtkDisplayState
*s
)
878 #if GTK_CHECK_VERSION(3, 0, 0)
879 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
880 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
881 GList
*devices
= gdk_device_manager_list_devices(mgr
,
882 GDK_DEVICE_TYPE_MASTER
);
883 GList
*tmp
= devices
;
885 GdkDevice
*dev
= tmp
->data
;
886 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
887 gdk_device_ungrab(dev
,
892 g_list_free(devices
);
894 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
898 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
900 GtkDisplayState
*s
= opaque
;
902 if (gd_is_grab_active(s
)) {
906 gd_ungrab_keyboard(s
);
907 gd_ungrab_pointer(s
);
910 gd_update_caption(s
);
911 gd_update_cursor(s
, FALSE
);
914 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
917 GtkDisplayState
*s
= data
;
921 if (!gtk_widget_get_realized(s
->notebook
)) {
925 last_page
= gtk_notebook_get_current_page(nb
);
928 gtk_widget_set_size_request(s
->vc
[last_page
- 1].terminal
, -1, -1);
934 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
936 } else if (s
->full_screen
) {
937 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
942 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->vga_item
), TRUE
);
944 VirtualConsole
*vc
= &s
->vc
[arg2
- 1];
945 VteTerminal
*term
= VTE_TERMINAL(vc
->terminal
);
948 width
= 80 * vte_terminal_get_char_width(term
);
949 height
= 25 * vte_terminal_get_char_height(term
);
951 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
952 gtk_widget_set_size_request(vc
->terminal
, width
, height
);
955 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
957 gd_update_cursor(s
, TRUE
);
960 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
962 GtkDisplayState
*s
= data
;
964 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
971 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
973 GtkDisplayState
*s
= data
;
975 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
976 gd_ungrab_keyboard(s
);
982 /** Virtual Console Callbacks **/
984 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
986 VirtualConsole
*vc
= chr
->opaque
;
988 return write(vc
->fd
, buf
, len
);
992 static CharDriverState
*vcs
[MAX_VCS
];
994 static CharDriverState
*gd_vc_handler(QemuOpts
*opts
)
996 CharDriverState
*chr
;
998 chr
= g_malloc0(sizeof(*chr
));
999 chr
->chr_write
= gd_vc_chr_write
;
1001 vcs
[nb_vcs
++] = chr
;
1006 void early_gtk_display_init(void)
1008 register_vc_handler(gd_vc_handler
);
1011 static gboolean
gd_vc_in(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
1013 VirtualConsole
*vc
= opaque
;
1014 uint8_t buffer
[1024];
1017 len
= read(vc
->fd
, buffer
, sizeof(buffer
));
1022 qemu_chr_be_write(vc
->chr
, buffer
, len
);
1027 static GSList
*gd_vc_init(GtkDisplayState
*s
, VirtualConsole
*vc
, int index
, GSList
*group
)
1032 #if VTE_CHECK_VERSION(0, 26, 0)
1036 GtkWidget
*scrolled_window
;
1037 GtkAdjustment
*vadjustment
;
1038 int master_fd
, slave_fd
, ret
;
1041 snprintf(buffer
, sizeof(buffer
), "vc%d", index
);
1042 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", index
);
1044 vc
->chr
= vcs
[index
];
1046 if (vc
->chr
->label
) {
1047 label
= vc
->chr
->label
;
1052 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, label
);
1053 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1054 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
1055 gtk_accel_map_add_entry(path
, GDK_KEY_2
+ index
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1057 vc
->terminal
= vte_terminal_new();
1059 ret
= openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
);
1060 g_assert(ret
!= -1);
1062 /* Set raw attributes on the pty. */
1063 tcgetattr(slave_fd
, &tty
);
1065 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1067 #if VTE_CHECK_VERSION(0, 26, 0)
1068 pty
= vte_pty_new_foreign(master_fd
, NULL
);
1069 vte_terminal_set_pty_object(VTE_TERMINAL(vc
->terminal
), pty
);
1071 vte_terminal_set_pty(VTE_TERMINAL(vc
->terminal
), master_fd
);
1074 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->terminal
), -1);
1076 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->terminal
));
1078 scrolled_window
= gtk_scrolled_window_new(NULL
, vadjustment
);
1079 gtk_container_add(GTK_CONTAINER(scrolled_window
), vc
->terminal
);
1081 vte_terminal_set_size(VTE_TERMINAL(vc
->terminal
), 80, 25);
1084 vc
->chr
->opaque
= vc
;
1085 vc
->scrolled_window
= scrolled_window
;
1087 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc
->scrolled_window
),
1088 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1090 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), scrolled_window
, gtk_label_new(label
));
1091 g_signal_connect(vc
->menu_item
, "activate",
1092 G_CALLBACK(gd_menu_switch_vc
), s
);
1094 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), vc
->menu_item
);
1096 qemu_chr_generic_open(vc
->chr
);
1097 if (vc
->chr
->init
) {
1098 vc
->chr
->init(vc
->chr
);
1101 chan
= g_io_channel_unix_new(vc
->fd
);
1102 g_io_add_watch(chan
, G_IO_IN
, gd_vc_in
, vc
);
1107 /** Window Creation **/
1109 static void gd_connect_signals(GtkDisplayState
*s
)
1111 g_signal_connect(s
->show_tabs_item
, "activate",
1112 G_CALLBACK(gd_menu_show_tabs
), s
);
1114 g_signal_connect(s
->window
, "key-press-event",
1115 G_CALLBACK(gd_window_key_event
), s
);
1116 g_signal_connect(s
->window
, "delete-event",
1117 G_CALLBACK(gd_window_close
), s
);
1119 #if GTK_CHECK_VERSION(3, 0, 0)
1120 g_signal_connect(s
->drawing_area
, "draw",
1121 G_CALLBACK(gd_draw_event
), s
);
1123 g_signal_connect(s
->drawing_area
, "expose-event",
1124 G_CALLBACK(gd_expose_event
), s
);
1126 g_signal_connect(s
->drawing_area
, "motion-notify-event",
1127 G_CALLBACK(gd_motion_event
), s
);
1128 g_signal_connect(s
->drawing_area
, "button-press-event",
1129 G_CALLBACK(gd_button_event
), s
);
1130 g_signal_connect(s
->drawing_area
, "button-release-event",
1131 G_CALLBACK(gd_button_event
), s
);
1132 g_signal_connect(s
->drawing_area
, "key-press-event",
1133 G_CALLBACK(gd_key_event
), s
);
1134 g_signal_connect(s
->drawing_area
, "key-release-event",
1135 G_CALLBACK(gd_key_event
), s
);
1137 g_signal_connect(s
->pause_item
, "activate",
1138 G_CALLBACK(gd_menu_pause
), s
);
1139 g_signal_connect(s
->reset_item
, "activate",
1140 G_CALLBACK(gd_menu_reset
), s
);
1141 g_signal_connect(s
->powerdown_item
, "activate",
1142 G_CALLBACK(gd_menu_powerdown
), s
);
1143 g_signal_connect(s
->quit_item
, "activate",
1144 G_CALLBACK(gd_menu_quit
), s
);
1145 g_signal_connect(s
->full_screen_item
, "activate",
1146 G_CALLBACK(gd_menu_full_screen
), s
);
1147 g_signal_connect(s
->zoom_in_item
, "activate",
1148 G_CALLBACK(gd_menu_zoom_in
), s
);
1149 g_signal_connect(s
->zoom_out_item
, "activate",
1150 G_CALLBACK(gd_menu_zoom_out
), s
);
1151 g_signal_connect(s
->zoom_fixed_item
, "activate",
1152 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1153 g_signal_connect(s
->zoom_fit_item
, "activate",
1154 G_CALLBACK(gd_menu_zoom_fit
), s
);
1155 g_signal_connect(s
->vga_item
, "activate",
1156 G_CALLBACK(gd_menu_switch_vc
), s
);
1157 g_signal_connect(s
->grab_item
, "activate",
1158 G_CALLBACK(gd_menu_grab_input
), s
);
1159 g_signal_connect(s
->notebook
, "switch-page",
1160 G_CALLBACK(gd_change_page
), s
);
1161 g_signal_connect(s
->drawing_area
, "enter-notify-event",
1162 G_CALLBACK(gd_enter_event
), s
);
1163 g_signal_connect(s
->drawing_area
, "leave-notify-event",
1164 G_CALLBACK(gd_leave_event
), s
);
1167 static void gd_create_menus(GtkDisplayState
*s
)
1170 GtkAccelGroup
*accel_group
;
1171 GSList
*group
= NULL
;
1172 GtkWidget
*separator
;
1175 accel_group
= gtk_accel_group_new();
1176 s
->machine_menu
= gtk_menu_new();
1177 gtk_menu_set_accel_group(GTK_MENU(s
->machine_menu
), accel_group
);
1178 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1180 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1181 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->pause_item
);
1183 separator
= gtk_separator_menu_item_new();
1184 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1186 s
->reset_item
= gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1187 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->reset_item
);
1189 s
->powerdown_item
= gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1190 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->powerdown_item
);
1192 separator
= gtk_separator_menu_item_new();
1193 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1195 s
->quit_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, NULL
);
1196 gtk_stock_lookup(GTK_STOCK_QUIT
, &item
);
1197 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1198 "<QEMU>/Machine/Quit");
1199 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item
.keyval
, item
.modifier
);
1200 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->quit_item
);
1202 s
->view_menu
= gtk_menu_new();
1203 gtk_menu_set_accel_group(GTK_MENU(s
->view_menu
), accel_group
);
1204 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1206 s
->full_screen_item
=
1207 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN
, NULL
);
1208 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1209 "<QEMU>/View/Full Screen");
1210 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1211 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->full_screen_item
);
1213 separator
= gtk_separator_menu_item_new();
1214 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1216 s
->zoom_in_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN
, NULL
);
1217 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1218 "<QEMU>/View/Zoom In");
1219 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1220 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_in_item
);
1222 s
->zoom_out_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT
, NULL
);
1223 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1224 "<QEMU>/View/Zoom Out");
1225 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1226 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_out_item
);
1228 s
->zoom_fixed_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100
, NULL
);
1229 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1230 "<QEMU>/View/Zoom Fixed");
1231 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1232 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fixed_item
);
1234 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1235 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fit_item
);
1237 separator
= gtk_separator_menu_item_new();
1238 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1240 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1241 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_on_hover_item
);
1243 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1244 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1245 "<QEMU>/View/Grab Input");
1246 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1247 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_item
);
1249 separator
= gtk_separator_menu_item_new();
1250 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1252 s
->vga_item
= gtk_radio_menu_item_new_with_mnemonic(group
, "_VGA");
1253 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s
->vga_item
));
1254 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->vga_item
),
1256 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1257 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->vga_item
);
1259 for (i
= 0; i
< nb_vcs
; i
++) {
1260 VirtualConsole
*vc
= &s
->vc
[i
];
1262 group
= gd_vc_init(s
, vc
, i
, group
);
1266 separator
= gtk_separator_menu_item_new();
1267 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1269 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1270 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->show_tabs_item
);
1272 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1273 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1274 s
->accel_group
= accel_group
;
1276 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1278 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1280 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1281 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1284 void gtk_display_init(DisplayState
*ds
)
1286 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1288 gtk_init(NULL
, NULL
);
1292 s
->dcl
.dpy_gfx_update
= gd_update
;
1293 s
->dcl
.dpy_gfx_resize
= gd_resize
;
1294 s
->dcl
.dpy_refresh
= gd_refresh
;
1296 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1297 #if GTK_CHECK_VERSION(3, 2, 0)
1298 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1300 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1302 s
->notebook
= gtk_notebook_new();
1303 s
->drawing_area
= gtk_drawing_area_new();
1304 s
->menu_bar
= gtk_menu_bar_new();
1308 s
->free_scale
= FALSE
;
1310 setlocale(LC_ALL
, "");
1311 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1314 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1316 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1317 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1318 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1320 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), s
->drawing_area
, gtk_label_new("VGA"));
1324 gd_connect_signals(s
);
1326 gtk_widget_add_events(s
->drawing_area
,
1327 GDK_POINTER_MOTION_MASK
|
1328 GDK_BUTTON_PRESS_MASK
|
1329 GDK_BUTTON_RELEASE_MASK
|
1330 GDK_BUTTON_MOTION_MASK
|
1331 GDK_ENTER_NOTIFY_MASK
|
1332 GDK_LEAVE_NOTIFY_MASK
|
1334 GDK_KEY_PRESS_MASK
);
1335 gtk_widget_set_double_buffered(s
->drawing_area
, FALSE
);
1336 gtk_widget_set_can_focus(s
->drawing_area
, TRUE
);
1338 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1339 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1341 gd_update_caption(s
);
1343 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1344 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1346 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1348 gtk_widget_show_all(s
->window
);
1350 register_displaychangelistener(ds
, &s
->dcl
);