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 gtk_widget_size_request(s
->vbox
, &req
);
310 gtk_window_resize(GTK_WINDOW(s
->window
),
311 req
.width
* sx
, req
.height
* sy
);
317 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
319 GtkDisplayState
*s
= opaque
;
321 gd_update_caption(s
);
324 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
326 gd_update_cursor(container_of(notify
, GtkDisplayState
, mouse_mode_notifier
),
332 static gboolean
gd_window_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
334 GtkDisplayState
*s
= opaque
;
335 GtkAccelGroupEntry
*entries
;
337 gboolean propagate_accel
= TRUE
;
338 gboolean handled
= FALSE
;
340 entries
= gtk_accel_group_query(s
->accel_group
, key
->keyval
,
341 key
->state
, &n_entries
);
343 const char *quark
= g_quark_to_string(entries
[0].accel_path_quark
);
345 if (gd_is_grab_active(s
) && strstart(quark
, "<QEMU>/File/", NULL
)) {
346 propagate_accel
= FALSE
;
350 if (!handled
&& propagate_accel
) {
351 handled
= gtk_window_activate_key(GTK_WINDOW(widget
), key
);
355 handled
= gtk_window_propagate_key_event(GTK_WINDOW(widget
), key
);
361 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
364 GtkDisplayState
*s
= opaque
;
367 unregister_displaychangelistener(s
->ds
, &s
->dcl
);
375 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
377 GtkDisplayState
*s
= opaque
;
382 if (!gtk_widget_get_realized(widget
)) {
386 fbw
= ds_get_width(s
->ds
);
387 fbh
= ds_get_height(s
->ds
);
389 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
391 if (s
->full_screen
) {
392 s
->scale_x
= (double)ww
/ fbw
;
393 s
->scale_y
= (double)wh
/ fbh
;
394 } else if (s
->free_scale
) {
397 sx
= (double)ww
/ fbw
;
398 sy
= (double)wh
/ fbh
;
400 s
->scale_x
= s
->scale_y
= MIN(sx
, sy
);
414 cairo_rectangle(cr
, 0, 0, ww
, wh
);
416 /* Optionally cut out the inner area where the pixmap
417 will be drawn. This avoids 'flashing' since we're
418 not double-buffering. Note we're using the undocumented
419 behaviour of drawing the rectangle from right to left
420 to cut out the whole */
421 cairo_rectangle(cr
, mx
+ fbw
, my
,
425 cairo_scale(cr
, s
->scale_x
, s
->scale_y
);
426 cairo_set_source_surface(cr
, s
->surface
, mx
/ s
->scale_x
, my
/ s
->scale_y
);
432 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
438 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
443 expose
->area
.height
);
446 ret
= gd_draw_event(widget
, cr
, opaque
);
453 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
456 GtkDisplayState
*s
= opaque
;
463 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
464 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
466 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
476 x
= (motion
->x
- mx
) / s
->scale_x
;
477 y
= (motion
->y
- my
) / s
->scale_y
;
479 if (x
< 0 || y
< 0 ||
480 x
>= ds_get_width(s
->ds
) ||
481 y
>= ds_get_height(s
->ds
)) {
485 if (kbd_mouse_is_absolute()) {
486 dx
= x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
487 dy
= y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
488 } else if (s
->last_x
== -1 || s
->last_y
== -1) {
499 if (kbd_mouse_is_absolute() || gd_is_grab_active(s
)) {
500 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
503 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s
)) {
504 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
505 GdkScreen
*screen
= gtk_widget_get_screen(s
->drawing_area
);
506 int x
= (int)motion
->x_root
;
507 int y
= (int)motion
->y_root
;
509 /* In relative mode check to see if client pointer hit
510 * one of the screen edges, and if so move it back by
511 * 200 pixels. This is important because the pointer
512 * in the server doesn't correspond 1-for-1, and so
513 * may still be only half way across the screen. Without
514 * this warp, the server pointer would thus appear to hit
515 * an invisible wall */
522 if (x
== (gdk_screen_get_width(screen
) - 1)) {
525 if (y
== (gdk_screen_get_height(screen
) - 1)) {
529 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
530 gdk_display_warp_pointer(display
, screen
, x
, y
);
539 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
542 GtkDisplayState
*s
= opaque
;
546 if (button
->button
== 1) {
548 } else if (button
->button
== 2) {
550 } else if (button
->button
== 3) {
556 if (button
->type
== GDK_BUTTON_PRESS
) {
558 } else if (button
->type
== GDK_BUTTON_RELEASE
) {
559 s
->button_mask
&= ~n
;
562 if (kbd_mouse_is_absolute()) {
563 dx
= s
->last_x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
564 dy
= s
->last_y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
570 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
575 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
580 gdk_keycode
= key
->hardware_keycode
;
582 if (gdk_keycode
< 9) {
584 } else if (gdk_keycode
< 97) {
585 qemu_keycode
= gdk_keycode
- 8;
586 } else if (gdk_keycode
< 158) {
587 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
588 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
590 } else if (gdk_keycode
== 211) { /* backslash */
596 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
597 gdk_keycode
, qemu_keycode
,
598 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
600 if (qemu_keycode
& SCANCODE_GREY
) {
601 kbd_put_keycode(SCANCODE_EMUL0
);
604 if (key
->type
== GDK_KEY_PRESS
) {
605 kbd_put_keycode(qemu_keycode
& SCANCODE_KEYCODEMASK
);
606 } else if (key
->type
== GDK_KEY_RELEASE
) {
607 kbd_put_keycode(qemu_keycode
| SCANCODE_UP
);
609 g_assert_not_reached();
615 /** Window Menu Actions **/
617 static void gd_menu_pause(GtkMenuItem
*item
, void *opaque
)
619 GtkDisplayState
*s
= opaque
;
621 if (s
->external_pause_update
) {
624 if (runstate_is_running()) {
631 static void gd_menu_reset(GtkMenuItem
*item
, void *opaque
)
633 qmp_system_reset(NULL
);
636 static void gd_menu_powerdown(GtkMenuItem
*item
, void *opaque
)
638 qmp_system_powerdown(NULL
);
641 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
646 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
648 GtkDisplayState
*s
= opaque
;
650 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vga_item
))) {
651 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), 0);
655 for (i
= 0; i
< s
->nb_vcs
; i
++) {
656 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vc
[i
].menu_item
))) {
657 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), i
+ 1);
664 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
666 GtkDisplayState
*s
= opaque
;
668 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
669 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
671 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
675 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
677 GtkDisplayState
*s
= opaque
;
679 if (!s
->full_screen
) {
680 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
681 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
682 gtk_widget_set_size_request(s
->drawing_area
, -1, -1);
683 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
685 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), TRUE
);
687 s
->full_screen
= TRUE
;
689 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
690 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
691 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
692 gtk_widget_set_size_request(s
->drawing_area
,
693 ds_get_width(s
->ds
), ds_get_height(s
->ds
));
694 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), FALSE
);
695 s
->full_screen
= FALSE
;
700 gd_update_cursor(s
, FALSE
);
703 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
705 GtkDisplayState
*s
= opaque
;
707 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
716 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
718 GtkDisplayState
*s
= opaque
;
720 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
726 s
->scale_x
= MAX(s
->scale_x
, .25);
727 s
->scale_y
= MAX(s
->scale_y
, .25);
732 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
734 GtkDisplayState
*s
= opaque
;
742 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
744 GtkDisplayState
*s
= opaque
;
747 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
748 s
->free_scale
= TRUE
;
750 s
->free_scale
= FALSE
;
755 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
756 gtk_widget_queue_draw_area(s
->drawing_area
, 0, 0, ww
, wh
);
759 static void gd_grab_keyboard(GtkDisplayState
*s
)
761 #if GTK_CHECK_VERSION(3, 0, 0)
762 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
763 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
764 GList
*devices
= gdk_device_manager_list_devices(mgr
,
765 GDK_DEVICE_TYPE_MASTER
);
766 GList
*tmp
= devices
;
768 GdkDevice
*dev
= tmp
->data
;
769 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
771 gtk_widget_get_window(s
->drawing_area
),
774 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
,
780 g_list_free(devices
);
782 gdk_keyboard_grab(gtk_widget_get_window(s
->drawing_area
),
788 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
790 #if GTK_CHECK_VERSION(3, 0, 0)
791 GdkDisplay
*display
= gtk_widget_get_display(s
->drawing_area
);
792 GdkDeviceManager
*mgr
= gdk_display_get_device_manager(display
);
793 GList
*devices
= gdk_device_manager_list_devices(mgr
,
794 GDK_DEVICE_TYPE_MASTER
);
795 GList
*tmp
= devices
;
797 GdkDevice
*dev
= tmp
->data
;
798 if (gdk_device_get_source(dev
) == GDK_SOURCE_KEYBOARD
) {
799 gdk_device_ungrab(dev
,
804 g_list_free(devices
);
806 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
810 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
812 GtkDisplayState
*s
= opaque
;
814 if (gd_is_grab_active(s
)) {
816 gdk_pointer_grab(gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
)),
817 FALSE
, /* All events to come to our window directly */
818 GDK_POINTER_MOTION_MASK
|
819 GDK_BUTTON_PRESS_MASK
|
820 GDK_BUTTON_RELEASE_MASK
|
821 GDK_BUTTON_MOTION_MASK
|
823 NULL
, /* Allow cursor to move over entire desktop */
827 gd_ungrab_keyboard(s
);
828 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
831 gd_update_caption(s
);
832 gd_update_cursor(s
, FALSE
);
835 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
838 GtkDisplayState
*s
= data
;
842 if (!gtk_widget_get_realized(s
->notebook
)) {
846 last_page
= gtk_notebook_get_current_page(nb
);
849 gtk_widget_set_size_request(s
->vc
[last_page
- 1].terminal
, -1, -1);
855 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
857 } else if (s
->full_screen
) {
858 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
863 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->vga_item
), TRUE
);
865 VirtualConsole
*vc
= &s
->vc
[arg2
- 1];
866 VteTerminal
*term
= VTE_TERMINAL(vc
->terminal
);
869 width
= 80 * vte_terminal_get_char_width(term
);
870 height
= 25 * vte_terminal_get_char_height(term
);
872 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
873 gtk_widget_set_size_request(vc
->terminal
, width
, height
);
876 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
878 gd_update_cursor(s
, TRUE
);
881 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
883 GtkDisplayState
*s
= data
;
885 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
892 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
894 GtkDisplayState
*s
= data
;
896 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
897 gd_ungrab_keyboard(s
);
903 /** Virtual Console Callbacks **/
905 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
907 VirtualConsole
*vc
= chr
->opaque
;
909 return write(vc
->fd
, buf
, len
);
913 static CharDriverState
*vcs
[MAX_VCS
];
915 static CharDriverState
*gd_vc_handler(QemuOpts
*opts
)
917 CharDriverState
*chr
;
919 chr
= g_malloc0(sizeof(*chr
));
920 chr
->chr_write
= gd_vc_chr_write
;
927 void early_gtk_display_init(void)
929 register_vc_handler(gd_vc_handler
);
932 static gboolean
gd_vc_in(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
934 VirtualConsole
*vc
= opaque
;
935 uint8_t buffer
[1024];
938 len
= read(vc
->fd
, buffer
, sizeof(buffer
));
943 qemu_chr_be_write(vc
->chr
, buffer
, len
);
948 static GSList
*gd_vc_init(GtkDisplayState
*s
, VirtualConsole
*vc
, int index
, GSList
*group
)
953 #if VTE_CHECK_VERSION(0, 26, 0)
957 GtkWidget
*scrolled_window
;
958 GtkAdjustment
*vadjustment
;
959 int master_fd
, slave_fd
, ret
;
962 snprintf(buffer
, sizeof(buffer
), "vc%d", index
);
963 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", index
);
965 vc
->chr
= vcs
[index
];
967 if (vc
->chr
->label
) {
968 label
= vc
->chr
->label
;
973 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, label
);
974 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
975 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
976 gtk_accel_map_add_entry(path
, GDK_KEY_2
+ index
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
978 vc
->terminal
= vte_terminal_new();
980 ret
= openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
);
983 /* Set raw attributes on the pty. */
984 tcgetattr(slave_fd
, &tty
);
986 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
988 #if VTE_CHECK_VERSION(0, 26, 0)
989 pty
= vte_pty_new_foreign(master_fd
, NULL
);
990 vte_terminal_set_pty_object(VTE_TERMINAL(vc
->terminal
), pty
);
992 vte_terminal_set_pty(VTE_TERMINAL(vc
->terminal
), master_fd
);
995 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->terminal
), -1);
997 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->terminal
));
999 scrolled_window
= gtk_scrolled_window_new(NULL
, vadjustment
);
1000 gtk_container_add(GTK_CONTAINER(scrolled_window
), vc
->terminal
);
1002 vte_terminal_set_size(VTE_TERMINAL(vc
->terminal
), 80, 25);
1005 vc
->chr
->opaque
= vc
;
1006 vc
->scrolled_window
= scrolled_window
;
1008 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc
->scrolled_window
),
1009 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1011 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), scrolled_window
, gtk_label_new(label
));
1012 g_signal_connect(vc
->menu_item
, "activate",
1013 G_CALLBACK(gd_menu_switch_vc
), s
);
1015 gtk_menu_append(GTK_MENU(s
->view_menu
), vc
->menu_item
);
1017 qemu_chr_generic_open(vc
->chr
);
1018 if (vc
->chr
->init
) {
1019 vc
->chr
->init(vc
->chr
);
1022 chan
= g_io_channel_unix_new(vc
->fd
);
1023 g_io_add_watch(chan
, G_IO_IN
, gd_vc_in
, vc
);
1028 /** Window Creation **/
1030 static void gd_connect_signals(GtkDisplayState
*s
)
1032 g_signal_connect(s
->show_tabs_item
, "activate",
1033 G_CALLBACK(gd_menu_show_tabs
), s
);
1035 g_signal_connect(s
->window
, "key-press-event",
1036 G_CALLBACK(gd_window_key_event
), s
);
1037 g_signal_connect(s
->window
, "delete-event",
1038 G_CALLBACK(gd_window_close
), s
);
1040 g_signal_connect(s
->drawing_area
, "expose-event",
1041 G_CALLBACK(gd_expose_event
), s
);
1042 g_signal_connect(s
->drawing_area
, "motion-notify-event",
1043 G_CALLBACK(gd_motion_event
), s
);
1044 g_signal_connect(s
->drawing_area
, "button-press-event",
1045 G_CALLBACK(gd_button_event
), s
);
1046 g_signal_connect(s
->drawing_area
, "button-release-event",
1047 G_CALLBACK(gd_button_event
), s
);
1048 g_signal_connect(s
->drawing_area
, "key-press-event",
1049 G_CALLBACK(gd_key_event
), s
);
1050 g_signal_connect(s
->drawing_area
, "key-release-event",
1051 G_CALLBACK(gd_key_event
), s
);
1053 g_signal_connect(s
->pause_item
, "activate",
1054 G_CALLBACK(gd_menu_pause
), s
);
1055 g_signal_connect(s
->reset_item
, "activate",
1056 G_CALLBACK(gd_menu_reset
), s
);
1057 g_signal_connect(s
->powerdown_item
, "activate",
1058 G_CALLBACK(gd_menu_powerdown
), s
);
1059 g_signal_connect(s
->quit_item
, "activate",
1060 G_CALLBACK(gd_menu_quit
), s
);
1061 g_signal_connect(s
->full_screen_item
, "activate",
1062 G_CALLBACK(gd_menu_full_screen
), s
);
1063 g_signal_connect(s
->zoom_in_item
, "activate",
1064 G_CALLBACK(gd_menu_zoom_in
), s
);
1065 g_signal_connect(s
->zoom_out_item
, "activate",
1066 G_CALLBACK(gd_menu_zoom_out
), s
);
1067 g_signal_connect(s
->zoom_fixed_item
, "activate",
1068 G_CALLBACK(gd_menu_zoom_fixed
), s
);
1069 g_signal_connect(s
->zoom_fit_item
, "activate",
1070 G_CALLBACK(gd_menu_zoom_fit
), s
);
1071 g_signal_connect(s
->vga_item
, "activate",
1072 G_CALLBACK(gd_menu_switch_vc
), s
);
1073 g_signal_connect(s
->grab_item
, "activate",
1074 G_CALLBACK(gd_menu_grab_input
), s
);
1075 g_signal_connect(s
->notebook
, "switch-page",
1076 G_CALLBACK(gd_change_page
), s
);
1077 g_signal_connect(s
->drawing_area
, "enter-notify-event",
1078 G_CALLBACK(gd_enter_event
), s
);
1079 g_signal_connect(s
->drawing_area
, "leave-notify-event",
1080 G_CALLBACK(gd_leave_event
), s
);
1083 static void gd_create_menus(GtkDisplayState
*s
)
1086 GtkAccelGroup
*accel_group
;
1087 GSList
*group
= NULL
;
1088 GtkWidget
*separator
;
1091 accel_group
= gtk_accel_group_new();
1092 s
->machine_menu
= gtk_menu_new();
1093 gtk_menu_set_accel_group(GTK_MENU(s
->machine_menu
), accel_group
);
1094 s
->machine_menu_item
= gtk_menu_item_new_with_mnemonic(_("_Machine"));
1096 s
->pause_item
= gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1097 gtk_menu_append(GTK_MENU(s
->machine_menu
), s
->pause_item
);
1099 separator
= gtk_separator_menu_item_new();
1100 gtk_menu_append(GTK_MENU(s
->machine_menu
), separator
);
1102 s
->reset_item
= gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
1103 gtk_menu_append(GTK_MENU(s
->machine_menu
), s
->reset_item
);
1105 s
->powerdown_item
= gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
1106 gtk_menu_append(GTK_MENU(s
->machine_menu
), s
->powerdown_item
);
1108 separator
= gtk_separator_menu_item_new();
1109 gtk_menu_append(GTK_MENU(s
->machine_menu
), separator
);
1111 s
->quit_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, NULL
);
1112 gtk_stock_lookup(GTK_STOCK_QUIT
, &item
);
1113 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1114 "<QEMU>/Machine/Quit");
1115 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item
.keyval
, item
.modifier
);
1116 gtk_menu_append(GTK_MENU(s
->machine_menu
), s
->quit_item
);
1118 s
->view_menu
= gtk_menu_new();
1119 gtk_menu_set_accel_group(GTK_MENU(s
->view_menu
), accel_group
);
1120 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1122 s
->full_screen_item
=
1123 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN
, NULL
);
1124 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1125 "<QEMU>/View/Full Screen");
1126 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1127 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->full_screen_item
);
1129 separator
= gtk_separator_menu_item_new();
1130 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1132 s
->zoom_in_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN
, NULL
);
1133 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1134 "<QEMU>/View/Zoom In");
1135 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1136 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_in_item
);
1138 s
->zoom_out_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT
, NULL
);
1139 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1140 "<QEMU>/View/Zoom Out");
1141 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1142 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_out_item
);
1144 s
->zoom_fixed_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100
, NULL
);
1145 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1146 "<QEMU>/View/Zoom Fixed");
1147 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1148 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_fixed_item
);
1150 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1151 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_fit_item
);
1153 separator
= gtk_separator_menu_item_new();
1154 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1156 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1157 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->grab_on_hover_item
);
1159 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1160 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1161 "<QEMU>/View/Grab Input");
1162 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1163 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->grab_item
);
1165 separator
= gtk_separator_menu_item_new();
1166 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1168 s
->vga_item
= gtk_radio_menu_item_new_with_mnemonic(group
, "_VGA");
1169 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s
->vga_item
));
1170 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->vga_item
),
1172 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1173 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->vga_item
);
1175 for (i
= 0; i
< nb_vcs
; i
++) {
1176 VirtualConsole
*vc
= &s
->vc
[i
];
1178 group
= gd_vc_init(s
, vc
, i
, group
);
1182 separator
= gtk_separator_menu_item_new();
1183 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1185 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1186 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->show_tabs_item
);
1188 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1189 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1190 s
->accel_group
= accel_group
;
1192 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->machine_menu_item
),
1194 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->machine_menu_item
);
1196 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1197 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1200 void gtk_display_init(DisplayState
*ds
)
1202 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1204 gtk_init(NULL
, NULL
);
1208 s
->dcl
.dpy_gfx_update
= gd_update
;
1209 s
->dcl
.dpy_gfx_resize
= gd_resize
;
1210 s
->dcl
.dpy_refresh
= gd_refresh
;
1212 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1213 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1214 s
->notebook
= gtk_notebook_new();
1215 s
->drawing_area
= gtk_drawing_area_new();
1216 s
->menu_bar
= gtk_menu_bar_new();
1220 s
->free_scale
= FALSE
;
1222 setlocale(LC_ALL
, "");
1223 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1226 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1228 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1229 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1230 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1232 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), s
->drawing_area
, gtk_label_new("VGA"));
1236 gd_connect_signals(s
);
1238 gtk_widget_add_events(s
->drawing_area
,
1239 GDK_POINTER_MOTION_MASK
|
1240 GDK_BUTTON_PRESS_MASK
|
1241 GDK_BUTTON_RELEASE_MASK
|
1242 GDK_BUTTON_MOTION_MASK
|
1243 GDK_ENTER_NOTIFY_MASK
|
1244 GDK_LEAVE_NOTIFY_MASK
|
1246 GDK_KEY_PRESS_MASK
);
1247 gtk_widget_set_double_buffered(s
->drawing_area
, FALSE
);
1248 gtk_widget_set_can_focus(s
->drawing_area
, TRUE
);
1250 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1251 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1253 gd_update_caption(s
);
1255 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1256 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1258 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1260 gtk_widget_show_all(s
->window
);
1262 register_displaychangelistener(ds
, &s
->dcl
);