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 typedef struct VirtualConsole
91 GtkWidget
*scrolled_window
;
96 typedef struct GtkDisplayState
102 GtkAccelGroup
*accel_group
;
104 GtkWidget
*machine_menu_item
;
105 GtkWidget
*machine_menu
;
106 GtkWidget
*pause_item
;
107 GtkWidget
*reset_item
;
108 GtkWidget
*powerdown_item
;
109 GtkWidget
*quit_item
;
111 GtkWidget
*view_menu_item
;
112 GtkWidget
*view_menu
;
113 GtkWidget
*full_screen_item
;
114 GtkWidget
*zoom_in_item
;
115 GtkWidget
*zoom_out_item
;
116 GtkWidget
*zoom_fixed_item
;
117 GtkWidget
*zoom_fit_item
;
118 GtkWidget
*grab_item
;
119 GtkWidget
*grab_on_hover_item
;
123 VirtualConsole vc
[MAX_VCS
];
125 GtkWidget
*show_tabs_item
;
129 GtkWidget
*drawing_area
;
130 cairo_surface_t
*surface
;
131 DisplayChangeListener dcl
;
139 gboolean full_screen
;
141 GdkCursor
*null_cursor
;
142 Notifier mouse_mode_notifier
;
145 bool external_pause_update
;
148 static GtkDisplayState
*global_state
;
150 /** Utility Functions **/
152 static bool gd_is_grab_active(GtkDisplayState
*s
)
154 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
157 static bool gd_grab_on_hover(GtkDisplayState
*s
)
159 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
162 static bool gd_on_vga(GtkDisplayState
*s
)
164 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
)) == 0;
167 static void gd_update_cursor(GtkDisplayState
*s
, gboolean override
)
172 window
= gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
));
174 on_vga
= gd_on_vga(s
);
176 if ((override
|| on_vga
) &&
177 (s
->full_screen
|| kbd_mouse_is_absolute() || gd_is_grab_active(s
))) {
178 gdk_window_set_cursor(window
, s
->null_cursor
);
180 gdk_window_set_cursor(window
, NULL
);
184 static void gd_update_caption(GtkDisplayState
*s
)
186 const char *status
= "";
188 const char *grab
= "";
189 bool is_paused
= !runstate_is_running();
191 if (gd_is_grab_active(s
)) {
192 grab
= " - Press Ctrl+Alt+G to release grab";
196 status
= " [Paused]";
198 s
->external_pause_update
= true;
199 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->pause_item
),
201 s
->external_pause_update
= false;
204 title
= g_strdup_printf("QEMU (%s)%s%s", qemu_name
, status
, grab
);
206 title
= g_strdup_printf("QEMU%s%s", status
, grab
);
209 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
214 /** DisplayState Callbacks **/
216 static void gd_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
218 GtkDisplayState
*s
= ds
->opaque
;
224 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x
, y
, w
, h
);
226 x1
= floor(x
* s
->scale_x
);
227 y1
= floor(y
* s
->scale_y
);
229 x2
= ceil(x
* s
->scale_x
+ w
* s
->scale_x
);
230 y2
= ceil(y
* s
->scale_y
+ h
* s
->scale_y
);
232 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
233 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
235 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
245 gtk_widget_queue_draw_area(s
->drawing_area
, mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
248 static void gd_refresh(DisplayState
*ds
)
253 static void gd_resize(DisplayState
*ds
)
255 GtkDisplayState
*s
= ds
->opaque
;
259 DPRINTF("resize(width=%d, height=%d)\n",
260 ds_get_width(ds
), ds_get_height(ds
));
263 cairo_surface_destroy(s
->surface
);
266 switch (ds
->surface
->pf
.bits_per_pixel
) {
268 kind
= CAIRO_FORMAT_A8
;
271 kind
= CAIRO_FORMAT_RGB16_565
;
274 kind
= CAIRO_FORMAT_RGB24
;
277 g_assert_not_reached();
281 stride
= cairo_format_stride_for_width(kind
, ds_get_width(ds
));
282 g_assert(ds_get_linesize(ds
) == stride
);
284 s
->surface
= cairo_image_surface_create_for_data(ds_get_data(ds
),
288 ds_get_linesize(ds
));
290 if (!s
->full_screen
) {
305 gtk_widget_set_size_request(s
->drawing_area
,
306 ds_get_width(ds
) * s
->scale_x
,
307 ds_get_height(ds
) * s
->scale_y
);
308 #if GTK_CHECK_VERSION(3, 0, 0)
309 gtk_widget_get_preferred_size(s
->vbox
, NULL
, &req
);
311 gtk_widget_size_request(s
->vbox
, &req
);
314 gtk_window_resize(GTK_WINDOW(s
->window
),
315 req
.width
* sx
, req
.height
* sy
);
321 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
323 GtkDisplayState
*s
= opaque
;
325 gd_update_caption(s
);
328 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
330 gd_update_cursor(container_of(notify
, GtkDisplayState
, mouse_mode_notifier
),
336 static gboolean
gd_window_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
338 GtkDisplayState
*s
= opaque
;
339 GtkAccelGroupEntry
*entries
;
341 gboolean propagate_accel
= TRUE
;
342 gboolean handled
= FALSE
;
344 entries
= gtk_accel_group_query(s
->accel_group
, key
->keyval
,
345 key
->state
, &n_entries
);
347 const char *quark
= g_quark_to_string(entries
[0].accel_path_quark
);
349 if (gd_is_grab_active(s
) && strstart(quark
, "<QEMU>/File/", NULL
)) {
350 propagate_accel
= FALSE
;
354 if (!handled
&& propagate_accel
) {
355 handled
= gtk_window_activate_key(GTK_WINDOW(widget
), key
);
359 handled
= gtk_window_propagate_key_event(GTK_WINDOW(widget
), key
);
365 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
368 GtkDisplayState
*s
= opaque
;
371 unregister_displaychangelistener(s
->ds
, &s
->dcl
);
379 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
381 GtkDisplayState
*s
= opaque
;
386 if (!gtk_widget_get_realized(widget
)) {
390 fbw
= ds_get_width(s
->ds
);
391 fbh
= ds_get_height(s
->ds
);
393 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
395 if (s
->full_screen
) {
396 s
->scale_x
= (double)ww
/ fbw
;
397 s
->scale_y
= (double)wh
/ fbh
;
398 } else if (s
->free_scale
) {
401 sx
= (double)ww
/ fbw
;
402 sy
= (double)wh
/ fbh
;
404 s
->scale_x
= s
->scale_y
= MIN(sx
, sy
);
418 cairo_rectangle(cr
, 0, 0, ww
, wh
);
420 /* Optionally cut out the inner area where the pixmap
421 will be drawn. This avoids 'flashing' since we're
422 not double-buffering. Note we're using the undocumented
423 behaviour of drawing the rectangle from right to left
424 to cut out the whole */
425 cairo_rectangle(cr
, mx
+ fbw
, my
,
429 cairo_scale(cr
, s
->scale_x
, s
->scale_y
);
430 cairo_set_source_surface(cr
, s
->surface
, mx
/ s
->scale_x
, my
/ s
->scale_y
);
436 #if !GTK_CHECK_VERSION(3, 0, 0)
437 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
443 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
448 expose
->area
.height
);
451 ret
= gd_draw_event(widget
, cr
, opaque
);
459 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
462 GtkDisplayState
*s
= opaque
;
469 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
470 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
472 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
482 x
= (motion
->x
- mx
) / s
->scale_x
;
483 y
= (motion
->y
- my
) / s
->scale_y
;
485 if (x
< 0 || y
< 0 ||
486 x
>= ds_get_width(s
->ds
) ||
487 y
>= ds_get_height(s
->ds
)) {
491 if (kbd_mouse_is_absolute()) {
492 dx
= x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
493 dy
= y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
494 } else if (s
->last_x
== -1 || s
->last_y
== -1) {
505 if (kbd_mouse_is_absolute() || gd_is_grab_active(s
)) {
506 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
509 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s
)) {
510 GdkScreen
*screen
= gtk_widget_get_screen(s
->drawing_area
);
511 int x
= (int)motion
->x_root
;
512 int y
= (int)motion
->y_root
;
514 /* In relative mode check to see if client pointer hit
515 * one of the screen edges, and if so move it back by
516 * 200 pixels. This is important because the pointer
517 * in the server doesn't correspond 1-for-1, and so
518 * may still be only half way across the screen. Without
519 * this warp, the server pointer would thus appear to hit
520 * an invisible wall */
527 if (x
== (gdk_screen_get_width(screen
) - 1)) {
530 if (y
== (gdk_screen_get_height(screen
) - 1)) {
534 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
535 #if GTK_CHECK_VERSION(3, 0, 0)
536 GdkDevice
*dev
= gdk_event_get_device((GdkEvent
*)motion
);
537 gdk_device_warp(dev
, screen
, x
, y
);
539 GdkDisplay
*display
= gtk_widget_get_display(widget
);
540 gdk_display_warp_pointer(display
, screen
, x
, y
);
550 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
553 GtkDisplayState
*s
= opaque
;
557 if (button
->button
== 1) {
559 } else if (button
->button
== 2) {
561 } else if (button
->button
== 3) {
567 if (button
->type
== GDK_BUTTON_PRESS
) {
569 } else if (button
->type
== GDK_BUTTON_RELEASE
) {
570 s
->button_mask
&= ~n
;
573 if (kbd_mouse_is_absolute()) {
574 dx
= s
->last_x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
575 dy
= s
->last_y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
581 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
586 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
591 gdk_keycode
= key
->hardware_keycode
;
593 if (gdk_keycode
< 9) {
595 } else if (gdk_keycode
< 97) {
596 qemu_keycode
= gdk_keycode
- 8;
597 } else if (gdk_keycode
< 158) {
598 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
599 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
601 } else if (gdk_keycode
== 211) { /* backslash */
607 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
608 gdk_keycode
, qemu_keycode
,
609 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
611 if (qemu_keycode
& SCANCODE_GREY
) {
612 kbd_put_keycode(SCANCODE_EMUL0
);
615 if (key
->type
== GDK_KEY_PRESS
) {
616 kbd_put_keycode(qemu_keycode
& SCANCODE_KEYCODEMASK
);
617 } else if (key
->type
== GDK_KEY_RELEASE
) {
618 kbd_put_keycode(qemu_keycode
| SCANCODE_UP
);
620 g_assert_not_reached();
626 /** Window Menu Actions **/
628 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
630 GtkDisplayState
*s
= opaque
;
632 if (s
->external_pause_update
) {
635 if (runstate_is_running()) {
642 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
644 qmp_system_reset(NULL
);
647 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
649 qmp_system_powerdown(NULL
);
652 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
657 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
659 GtkDisplayState
*s
= opaque
;
661 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vga_item
))) {
662 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), 0);
666 for (i
= 0; i
< s
->nb_vcs
; i
++) {
667 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vc
[i
].menu_item
))) {
668 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), i
+ 1);
675 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
677 GtkDisplayState
*s
= opaque
;
679 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
680 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
682 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
686 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
688 GtkDisplayState
*s
= opaque
;
690 if (!s
->full_screen
) {
691 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
692 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
693 gtk_widget_set_size_request(s
->drawing_area
, -1, -1);
694 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
696 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), TRUE
);
698 s
->full_screen
= TRUE
;
700 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
701 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
702 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
703 gtk_widget_set_size_request(s
->drawing_area
,
704 ds_get_width(s
->ds
), ds_get_height(s
->ds
));
705 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), FALSE
);
706 s
->full_screen
= FALSE
;
711 gd_update_cursor(s
, FALSE
);
714 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
716 GtkDisplayState
*s
= opaque
;
718 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
727 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
729 GtkDisplayState
*s
= opaque
;
731 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
737 s
->scale_x
= MAX(s
->scale_x
, .25);
738 s
->scale_y
= MAX(s
->scale_y
, .25);
743 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
745 GtkDisplayState
*s
= opaque
;
753 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
755 GtkDisplayState
*s
= opaque
;
758 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
759 s
->free_scale
= TRUE
;
761 s
->free_scale
= FALSE
;
766 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
767 gtk_widget_queue_draw_area(s
->drawing_area
, 0, 0, ww
, wh
);
770 static void gd_grab_keyboard(GtkDisplayState
*s
)
772 #if GTK_CHECK_VERSION(3, 0, 0)
773 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
774 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
775 GList
*devices
= gdk_device_manager_list_devices(mgr
,
776 GDK_DEVICE_TYPE_MASTER
);
777 GList
*tmp
= devices
;
779 GdkDevice
*dev
= tmp
->data
;
780 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
782 gtk_widget_get_window(s
->drawing_area
),
785 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
791 g_list_free(devices
);
793 gdk_keyboard_grab(gtk_widget_get_window(s
->drawing_area
),
799 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
801 #if GTK_CHECK_VERSION(3, 0, 0)
802 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
803 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
804 GList
*devices
= gdk_device_manager_list_devices(mgr
,
805 GDK_DEVICE_TYPE_MASTER
);
806 GList
*tmp
= devices
;
808 GdkDevice
*dev
= tmp
->data
;
809 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
810 gdk_device_ungrab(dev
,
815 g_list_free(devices
);
817 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
821 static void gd_grab_pointer(GtkDisplayState
*s
)
823 #if GTK_CHECK_VERSION(3, 0, 0)
824 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
825 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
826 GList
*devices
= gdk_device_manager_list_devices(mgr
,
827 GDK_DEVICE_TYPE_MASTER
);
828 GList
*tmp
= devices
;
830 GdkDevice
*dev
= tmp
->data
;
831 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
833 gtk_widget_get_window(s
->drawing_area
),
835 FALSE
, /* All events to come to our
837 GDK_POINTER_MOTION_MASK
|
838 GDK_BUTTON_PRESS_MASK
|
839 GDK_BUTTON_RELEASE_MASK
|
840 GDK_BUTTON_MOTION_MASK
|
847 g_list_free(devices
);
849 gdk_pointer_grab(gtk_widget_get_window(s
->drawing_area
),
850 FALSE
, /* All events to come to our window directly */
851 GDK_POINTER_MOTION_MASK
|
852 GDK_BUTTON_PRESS_MASK
|
853 GDK_BUTTON_RELEASE_MASK
|
854 GDK_BUTTON_MOTION_MASK
|
856 NULL
, /* Allow cursor to move over entire desktop */
862 static void gd_ungrab_pointer(GtkDisplayState
*s
)
864 #if GTK_CHECK_VERSION(3, 0, 0)
865 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
866 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
867 GList
*devices
= gdk_device_manager_list_devices(mgr
,
868 GDK_DEVICE_TYPE_MASTER
);
869 GList
*tmp
= devices
;
871 GdkDevice
*dev
= tmp
->data
;
872 if (gdk_device_get_source(dev
) == GDK_SOURCE_MOUSE
) {
873 gdk_device_ungrab(dev
,
878 g_list_free(devices
);
880 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
884 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
886 GtkDisplayState
*s
= opaque
;
888 if (gd_is_grab_active(s
)) {
892 gd_ungrab_keyboard(s
);
893 gd_ungrab_pointer(s
);
896 gd_update_caption(s
);
897 gd_update_cursor(s
, FALSE
);
900 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
903 GtkDisplayState
*s
= data
;
907 if (!gtk_widget_get_realized(s
->notebook
)) {
911 last_page
= gtk_notebook_get_current_page(nb
);
914 gtk_widget_set_size_request(s
->vc
[last_page
- 1].terminal
, -1, -1);
920 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
922 } else if (s
->full_screen
) {
923 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
928 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->vga_item
), TRUE
);
930 VirtualConsole
*vc
= &s
->vc
[arg2
- 1];
931 VteTerminal
*term
= VTE_TERMINAL(vc
->terminal
);
934 width
= 80 * vte_terminal_get_char_width(term
);
935 height
= 25 * vte_terminal_get_char_height(term
);
937 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
938 gtk_widget_set_size_request(vc
->terminal
, width
, height
);
941 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
943 gd_update_cursor(s
, TRUE
);
946 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
948 GtkDisplayState
*s
= data
;
950 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
957 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
959 GtkDisplayState
*s
= data
;
961 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
962 gd_ungrab_keyboard(s
);
968 /** Virtual Console Callbacks **/
970 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
972 VirtualConsole
*vc
= chr
->opaque
;
974 return write(vc
->fd
, buf
, len
);
978 static CharDriverState
*vcs
[MAX_VCS
];
980 static CharDriverState
*gd_vc_handler(QemuOpts
*opts
)
982 CharDriverState
*chr
;
984 chr
= g_malloc0(sizeof(*chr
));
985 chr
->chr_write
= gd_vc_chr_write
;
992 void early_gtk_display_init(void)
994 register_vc_handler(gd_vc_handler
);
997 static gboolean
gd_vc_in(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
999 VirtualConsole
*vc
= opaque
;
1000 uint8_t buffer
[1024];
1003 len
= read(vc
->fd
, buffer
, sizeof(buffer
));
1008 qemu_chr_be_write(vc
->chr
, buffer
, len
);
1013 static GSList
*gd_vc_init(GtkDisplayState
*s
, VirtualConsole
*vc
, int index
, GSList
*group
)
1018 #if VTE_CHECK_VERSION(0, 26, 0)
1022 GtkWidget
*scrolled_window
;
1023 GtkAdjustment
*vadjustment
;
1024 int master_fd
, slave_fd
, ret
;
1027 snprintf(buffer
, sizeof(buffer
), "vc%d", index
);
1028 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", index
);
1030 vc
->chr
= vcs
[index
];
1032 if (vc
->chr
->label
) {
1033 label
= vc
->chr
->label
;
1038 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, label
);
1039 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
1040 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
1041 gtk_accel_map_add_entry(path
, GDK_KEY_2
+ index
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1043 vc
->terminal
= vte_terminal_new();
1045 ret
= openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
);
1046 g_assert(ret
!= -1);
1048 /* Set raw attributes on the pty. */
1049 tcgetattr(slave_fd
, &tty
);
1051 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
1053 #if VTE_CHECK_VERSION(0, 26, 0)
1054 pty
= vte_pty_new_foreign(master_fd
, NULL
);
1055 vte_terminal_set_pty_object(VTE_TERMINAL(vc
->terminal
), pty
);
1057 vte_terminal_set_pty(VTE_TERMINAL(vc
->terminal
), master_fd
);
1060 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->terminal
), -1);
1062 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->terminal
));
1064 scrolled_window
= gtk_scrolled_window_new(NULL
, vadjustment
);
1065 gtk_container_add(GTK_CONTAINER(scrolled_window
), vc
->terminal
);
1067 vte_terminal_set_size(VTE_TERMINAL(vc
->terminal
), 80, 25);
1070 vc
->chr
->opaque
= vc
;
1071 vc
->scrolled_window
= scrolled_window
;
1073 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc
->scrolled_window
),
1074 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1076 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), scrolled_window
, gtk_label_new(label
));
1077 g_signal_connect(vc
->menu_item
, "activate",
1078 G_CALLBACK(gd_menu_switch_vc
), s
);
1080 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), vc
->menu_item
);
1082 qemu_chr_generic_open(vc
->chr
);
1083 if (vc
->chr
->init
) {
1084 vc
->chr
->init(vc
->chr
);
1087 chan
= g_io_channel_unix_new(vc
->fd
);
1088 g_io_add_watch(chan
, G_IO_IN
, gd_vc_in
, vc
);
1093 /** Window Creation **/
1095 static void gd_connect_signals(GtkDisplayState
*s
)
1097 g_signal_connect(s
->show_tabs_item
, "activate",
1098 G_CALLBACK(gd_menu_show_tabs
), s
);
1100 g_signal_connect(s
->window
, "key-press-event",
1101 G_CALLBACK(gd_window_key_event
), s
);
1102 g_signal_connect(s
->window
, "delete-event",
1103 G_CALLBACK(gd_window_close
), s
);
1105 #if GTK_CHECK_VERSION(3, 0, 0)
1106 g_signal_connect(s
->drawing_area
, "draw",
1107 G_CALLBACK(gd_draw_event
), s
);
1109 g_signal_connect(s
->drawing_area
, "expose-event",
1110 G_CALLBACK(gd_expose_event
), s
);
1112 g_signal_connect(s
->drawing_area
, "motion-notify-event",
1113 G_CALLBACK(gd_motion_event
), s
);
1114 g_signal_connect(s
->drawing_area
, "button-press-event",
1115 G_CALLBACK(gd_button_event
), s
);
1116 g_signal_connect(s
->drawing_area
, "button-release-event",
1117 G_CALLBACK(gd_button_event
), s
);
1118 g_signal_connect(s
->drawing_area
, "key-press-event",
1119 G_CALLBACK(gd_key_event
), s
);
1120 g_signal_connect(s
->drawing_area
, "key-release-event",
1121 G_CALLBACK(gd_key_event
), s
);
1123 g_signal_connect(s
->pause_item
, "activate",
1124 G_CALLBACK(gd_menu_pause
), s
);
1125 g_signal_connect(s
->reset_item
, "activate",
1126 G_CALLBACK(gd_menu_reset
), s
);
1127 g_signal_connect(s
->powerdown_item
, "activate",
1128 G_CALLBACK(gd_menu_powerdown
), s
);
1129 g_signal_connect(s
->quit_item
, "activate",
1130 G_CALLBACK(gd_menu_quit
), s
);
1131 g_signal_connect(s
->full_screen_item
, "activate",
1132 G_CALLBACK(gd_menu_full_screen
), s
);
1133 g_signal_connect(s
->zoom_in_item
, "activate",
1134 G_CALLBACK(gd_menu_zoom_in
), s
);
1135 g_signal_connect(s
->zoom_out_item
, "activate",
1136 G_CALLBACK(gd_menu_zoom_out
), s
);
1137 g_signal_connect(s
->zoom_fixed_item
, "activate",
1138 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1139 g_signal_connect(s
->zoom_fit_item
, "activate",
1140 G_CALLBACK(gd_menu_zoom_fit
), s
);
1141 g_signal_connect(s
->vga_item
, "activate",
1142 G_CALLBACK(gd_menu_switch_vc
), s
);
1143 g_signal_connect(s
->grab_item
, "activate",
1144 G_CALLBACK(gd_menu_grab_input
), s
);
1145 g_signal_connect(s
->notebook
, "switch-page",
1146 G_CALLBACK(gd_change_page
), s
);
1147 g_signal_connect(s
->drawing_area
, "enter-notify-event",
1148 G_CALLBACK(gd_enter_event
), s
);
1149 g_signal_connect(s
->drawing_area
, "leave-notify-event",
1150 G_CALLBACK(gd_leave_event
), s
);
1153 static void gd_create_menus(GtkDisplayState
*s
)
1156 GtkAccelGroup
*accel_group
;
1157 GSList
*group
= NULL
;
1158 GtkWidget
*separator
;
1161 accel_group
= gtk_accel_group_new();
1162 s
->machine_menu
= gtk_menu_new();
1163 gtk_menu_set_accel_group(GTK_MENU(s
->machine_menu
), accel_group
);
1164 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1166 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1167 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->pause_item
);
1169 separator
= gtk_separator_menu_item_new();
1170 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1172 s
->reset_item
= gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1173 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->reset_item
);
1175 s
->powerdown_item
= gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1176 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->powerdown_item
);
1178 separator
= gtk_separator_menu_item_new();
1179 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), separator
);
1181 s
->quit_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, NULL
);
1182 gtk_stock_lookup(GTK_STOCK_QUIT
, &item
);
1183 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1184 "<QEMU>/Machine/Quit");
1185 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item
.keyval
, item
.modifier
);
1186 gtk_menu_shell_append(GTK_MENU_SHELL(s
->machine_menu
), s
->quit_item
);
1188 s
->view_menu
= gtk_menu_new();
1189 gtk_menu_set_accel_group(GTK_MENU(s
->view_menu
), accel_group
);
1190 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1192 s
->full_screen_item
=
1193 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN
, NULL
);
1194 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1195 "<QEMU>/View/Full Screen");
1196 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1197 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->full_screen_item
);
1199 separator
= gtk_separator_menu_item_new();
1200 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1202 s
->zoom_in_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN
, NULL
);
1203 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1204 "<QEMU>/View/Zoom In");
1205 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1206 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_in_item
);
1208 s
->zoom_out_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT
, NULL
);
1209 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1210 "<QEMU>/View/Zoom Out");
1211 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1212 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_out_item
);
1214 s
->zoom_fixed_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100
, NULL
);
1215 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1216 "<QEMU>/View/Zoom Fixed");
1217 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1218 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fixed_item
);
1220 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1221 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->zoom_fit_item
);
1223 separator
= gtk_separator_menu_item_new();
1224 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1226 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1227 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_on_hover_item
);
1229 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1230 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1231 "<QEMU>/View/Grab Input");
1232 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1233 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->grab_item
);
1235 separator
= gtk_separator_menu_item_new();
1236 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1238 s
->vga_item
= gtk_radio_menu_item_new_with_mnemonic(group
, "_VGA");
1239 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s
->vga_item
));
1240 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->vga_item
),
1242 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1243 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->vga_item
);
1245 for (i
= 0; i
< nb_vcs
; i
++) {
1246 VirtualConsole
*vc
= &s
->vc
[i
];
1248 group
= gd_vc_init(s
, vc
, i
, group
);
1252 separator
= gtk_separator_menu_item_new();
1253 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), separator
);
1255 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1256 gtk_menu_shell_append(GTK_MENU_SHELL(s
->view_menu
), s
->show_tabs_item
);
1258 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1259 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1260 s
->accel_group
= accel_group
;
1262 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1264 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1266 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1267 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1270 void gtk_display_init(DisplayState
*ds
)
1272 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1274 gtk_init(NULL
, NULL
);
1278 s
->dcl
.dpy_gfx_update
= gd_update
;
1279 s
->dcl
.dpy_gfx_resize
= gd_resize
;
1280 s
->dcl
.dpy_refresh
= gd_refresh
;
1282 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1283 #if GTK_CHECK_VERSION(3, 2, 0)
1284 s
->vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
1286 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1288 s
->notebook
= gtk_notebook_new();
1289 s
->drawing_area
= gtk_drawing_area_new();
1290 s
->menu_bar
= gtk_menu_bar_new();
1294 s
->free_scale
= FALSE
;
1296 setlocale(LC_ALL
, "");
1297 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1300 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1302 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1303 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1304 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1306 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), s
->drawing_area
, gtk_label_new("VGA"));
1310 gd_connect_signals(s
);
1312 gtk_widget_add_events(s
->drawing_area
,
1313 GDK_POINTER_MOTION_MASK
|
1314 GDK_BUTTON_PRESS_MASK
|
1315 GDK_BUTTON_RELEASE_MASK
|
1316 GDK_BUTTON_MOTION_MASK
|
1317 GDK_ENTER_NOTIFY_MASK
|
1318 GDK_LEAVE_NOTIFY_MASK
|
1320 GDK_KEY_PRESS_MASK
);
1321 gtk_widget_set_double_buffered(s
->drawing_area
, FALSE
);
1322 gtk_widget_set_can_focus(s
->drawing_area
, TRUE
);
1324 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1325 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1327 gd_update_caption(s
);
1329 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1330 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1332 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1334 gtk_widget_show_all(s
->window
);
1336 register_displaychangelistener(ds
, &s
->dcl
);