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)
77 typedef struct VirtualConsole
81 GtkWidget
*scrolled_window
;
86 typedef struct GtkDisplayState
92 GtkAccelGroup
*accel_group
;
94 GtkWidget
*file_menu_item
;
98 GtkWidget
*view_menu_item
;
100 GtkWidget
*full_screen_item
;
101 GtkWidget
*zoom_in_item
;
102 GtkWidget
*zoom_out_item
;
103 GtkWidget
*zoom_fixed_item
;
104 GtkWidget
*zoom_fit_item
;
105 GtkWidget
*grab_item
;
106 GtkWidget
*grab_on_hover_item
;
110 VirtualConsole vc
[MAX_VCS
];
112 GtkWidget
*show_tabs_item
;
116 GtkWidget
*drawing_area
;
117 cairo_surface_t
*surface
;
118 DisplayChangeListener dcl
;
126 gboolean full_screen
;
128 GdkCursor
*null_cursor
;
129 Notifier mouse_mode_notifier
;
133 static GtkDisplayState
*global_state
;
135 /** Utility Functions **/
137 static bool gd_is_grab_active(GtkDisplayState
*s
)
139 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_item
));
142 static bool gd_grab_on_hover(GtkDisplayState
*s
)
144 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->grab_on_hover_item
));
147 static bool gd_on_vga(GtkDisplayState
*s
)
149 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s
->notebook
)) == 0;
152 static void gd_update_cursor(GtkDisplayState
*s
, gboolean override
)
157 window
= gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
));
159 on_vga
= gd_on_vga(s
);
161 if ((override
|| on_vga
) &&
162 (s
->full_screen
|| kbd_mouse_is_absolute() || gd_is_grab_active(s
))) {
163 gdk_window_set_cursor(window
, s
->null_cursor
);
165 gdk_window_set_cursor(window
, NULL
);
169 static void gd_update_caption(GtkDisplayState
*s
)
171 const char *status
= "";
173 const char *grab
= "";
175 if (gd_is_grab_active(s
)) {
176 grab
= " - Press Ctrl+Alt+G to release grab";
179 if (!runstate_is_running()) {
180 status
= " [Stopped]";
184 title
= g_strdup_printf("QEMU (%s)%s%s", qemu_name
, status
, grab
);
186 title
= g_strdup_printf("QEMU%s%s", status
, grab
);
189 gtk_window_set_title(GTK_WINDOW(s
->window
), title
);
194 /** DisplayState Callbacks **/
196 static void gd_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
198 GtkDisplayState
*s
= ds
->opaque
;
204 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x
, y
, w
, h
);
206 x1
= floor(x
* s
->scale_x
);
207 y1
= floor(y
* s
->scale_y
);
209 x2
= ceil(x
* s
->scale_x
+ w
* s
->scale_x
);
210 y2
= ceil(y
* s
->scale_y
+ h
* s
->scale_y
);
212 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
213 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
215 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
225 gtk_widget_queue_draw_area(s
->drawing_area
, mx
+ x1
, my
+ y1
, (x2
- x1
), (y2
- y1
));
228 static void gd_refresh(DisplayState
*ds
)
233 static void gd_resize(DisplayState
*ds
)
235 GtkDisplayState
*s
= ds
->opaque
;
239 DPRINTF("resize(width=%d, height=%d)\n",
240 ds_get_width(ds
), ds_get_height(ds
));
243 cairo_surface_destroy(s
->surface
);
246 switch (ds
->surface
->pf
.bits_per_pixel
) {
248 kind
= CAIRO_FORMAT_A8
;
251 kind
= CAIRO_FORMAT_RGB16_565
;
254 kind
= CAIRO_FORMAT_RGB24
;
257 g_assert_not_reached();
261 stride
= cairo_format_stride_for_width(kind
, ds_get_width(ds
));
262 g_assert(ds_get_linesize(ds
) == stride
);
264 s
->surface
= cairo_image_surface_create_for_data(ds_get_data(ds
),
268 ds_get_linesize(ds
));
270 if (!s
->full_screen
) {
285 gtk_widget_set_size_request(s
->drawing_area
,
286 ds_get_width(ds
) * s
->scale_x
,
287 ds_get_height(ds
) * s
->scale_y
);
288 gtk_widget_size_request(s
->vbox
, &req
);
290 gtk_window_resize(GTK_WINDOW(s
->window
),
291 req
.width
* sx
, req
.height
* sy
);
297 static void gd_change_runstate(void *opaque
, int running
, RunState state
)
299 GtkDisplayState
*s
= opaque
;
301 gd_update_caption(s
);
304 static void gd_mouse_mode_change(Notifier
*notify
, void *data
)
306 gd_update_cursor(container_of(notify
, GtkDisplayState
, mouse_mode_notifier
),
312 static gboolean
gd_window_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
314 GtkDisplayState
*s
= opaque
;
315 GtkAccelGroupEntry
*entries
;
317 gboolean propagate_accel
= TRUE
;
318 gboolean handled
= FALSE
;
320 entries
= gtk_accel_group_query(s
->accel_group
, key
->keyval
,
321 key
->state
, &n_entries
);
323 const char *quark
= g_quark_to_string(entries
[0].accel_path_quark
);
325 if (gd_is_grab_active(s
) && strstart(quark
, "<QEMU>/File/", NULL
)) {
326 propagate_accel
= FALSE
;
330 if (!handled
&& propagate_accel
) {
331 handled
= gtk_window_activate_key(GTK_WINDOW(widget
), key
);
335 handled
= gtk_window_propagate_key_event(GTK_WINDOW(widget
), key
);
341 static gboolean
gd_window_close(GtkWidget
*widget
, GdkEvent
*event
,
344 GtkDisplayState
*s
= opaque
;
347 unregister_displaychangelistener(s
->ds
, &s
->dcl
);
355 static gboolean
gd_draw_event(GtkWidget
*widget
, cairo_t
*cr
, void *opaque
)
357 GtkDisplayState
*s
= opaque
;
362 if (!gtk_widget_get_realized(widget
)) {
366 fbw
= ds_get_width(s
->ds
);
367 fbh
= ds_get_height(s
->ds
);
369 gdk_drawable_get_size(gtk_widget_get_window(widget
), &ww
, &wh
);
371 if (s
->full_screen
) {
372 s
->scale_x
= (double)ww
/ fbw
;
373 s
->scale_y
= (double)wh
/ fbh
;
374 } else if (s
->free_scale
) {
377 sx
= (double)ww
/ fbw
;
378 sy
= (double)wh
/ fbh
;
380 s
->scale_x
= s
->scale_y
= MIN(sx
, sy
);
394 cairo_rectangle(cr
, 0, 0, ww
, wh
);
396 /* Optionally cut out the inner area where the pixmap
397 will be drawn. This avoids 'flashing' since we're
398 not double-buffering. Note we're using the undocumented
399 behaviour of drawing the rectangle from right to left
400 to cut out the whole */
401 cairo_rectangle(cr
, mx
+ fbw
, my
,
405 cairo_scale(cr
, s
->scale_x
, s
->scale_y
);
406 cairo_set_source_surface(cr
, s
->surface
, mx
/ s
->scale_x
, my
/ s
->scale_y
);
412 static gboolean
gd_expose_event(GtkWidget
*widget
, GdkEventExpose
*expose
,
418 cr
= gdk_cairo_create(gtk_widget_get_window(widget
));
423 expose
->area
.height
);
426 ret
= gd_draw_event(widget
, cr
, opaque
);
433 static gboolean
gd_motion_event(GtkWidget
*widget
, GdkEventMotion
*motion
,
436 GtkDisplayState
*s
= opaque
;
443 fbw
= ds_get_width(s
->ds
) * s
->scale_x
;
444 fbh
= ds_get_height(s
->ds
) * s
->scale_y
;
446 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
456 x
= (motion
->x
- mx
) / s
->scale_x
;
457 y
= (motion
->y
- my
) / s
->scale_y
;
459 if (x
< 0 || y
< 0 ||
460 x
>= ds_get_width(s
->ds
) ||
461 y
>= ds_get_height(s
->ds
)) {
465 if (kbd_mouse_is_absolute()) {
466 dx
= x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
467 dy
= y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
468 } else if (s
->last_x
== -1 || s
->last_y
== -1) {
479 if (kbd_mouse_is_absolute() || gd_is_grab_active(s
)) {
480 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
483 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s
)) {
484 GdkDrawable
*drawable
= GDK_DRAWABLE(gtk_widget_get_window(s
->drawing_area
));
485 GdkDisplay
*display
= gdk_drawable_get_display(drawable
);
486 GdkScreen
*screen
= gdk_drawable_get_screen(drawable
);
487 int x
= (int)motion
->x_root
;
488 int y
= (int)motion
->y_root
;
490 /* In relative mode check to see if client pointer hit
491 * one of the screen edges, and if so move it back by
492 * 200 pixels. This is important because the pointer
493 * in the server doesn't correspond 1-for-1, and so
494 * may still be only half way across the screen. Without
495 * this warp, the server pointer would thus appear to hit
496 * an invisible wall */
503 if (x
== (gdk_screen_get_width(screen
) - 1)) {
506 if (y
== (gdk_screen_get_height(screen
) - 1)) {
510 if (x
!= (int)motion
->x_root
|| y
!= (int)motion
->y_root
) {
511 gdk_display_warp_pointer(display
, screen
, x
, y
);
520 static gboolean
gd_button_event(GtkWidget
*widget
, GdkEventButton
*button
,
523 GtkDisplayState
*s
= opaque
;
527 if (button
->button
== 1) {
529 } else if (button
->button
== 2) {
531 } else if (button
->button
== 3) {
537 if (button
->type
== GDK_BUTTON_PRESS
) {
539 } else if (button
->type
== GDK_BUTTON_RELEASE
) {
540 s
->button_mask
&= ~n
;
543 if (kbd_mouse_is_absolute()) {
544 dx
= s
->last_x
* 0x7FFF / (ds_get_width(s
->ds
) - 1);
545 dy
= s
->last_y
* 0x7FFF / (ds_get_height(s
->ds
) - 1);
551 kbd_mouse_event(dx
, dy
, 0, s
->button_mask
);
556 static gboolean
gd_key_event(GtkWidget
*widget
, GdkEventKey
*key
, void *opaque
)
561 gdk_keycode
= key
->hardware_keycode
;
563 if (gdk_keycode
< 9) {
565 } else if (gdk_keycode
< 97) {
566 qemu_keycode
= gdk_keycode
- 8;
567 } else if (gdk_keycode
< 158) {
568 qemu_keycode
= translate_evdev_keycode(gdk_keycode
- 97);
569 } else if (gdk_keycode
== 208) { /* Hiragana_Katakana */
571 } else if (gdk_keycode
== 211) { /* backslash */
577 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n",
578 gdk_keycode
, qemu_keycode
,
579 (key
->type
== GDK_KEY_PRESS
) ? "down" : "up");
581 if (qemu_keycode
& SCANCODE_GREY
) {
582 kbd_put_keycode(SCANCODE_EMUL0
);
585 if (key
->type
== GDK_KEY_PRESS
) {
586 kbd_put_keycode(qemu_keycode
& SCANCODE_KEYCODEMASK
);
587 } else if (key
->type
== GDK_KEY_RELEASE
) {
588 kbd_put_keycode(qemu_keycode
| SCANCODE_UP
);
590 g_assert_not_reached();
596 /** Window Menu Actions **/
598 static void gd_menu_quit(GtkMenuItem
*item
, void *opaque
)
603 static void gd_menu_switch_vc(GtkMenuItem
*item
, void *opaque
)
605 GtkDisplayState
*s
= opaque
;
607 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vga_item
))) {
608 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), 0);
612 for (i
= 0; i
< s
->nb_vcs
; i
++) {
613 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->vc
[i
].menu_item
))) {
614 gtk_notebook_set_current_page(GTK_NOTEBOOK(s
->notebook
), i
+ 1);
621 static void gd_menu_show_tabs(GtkMenuItem
*item
, void *opaque
)
623 GtkDisplayState
*s
= opaque
;
625 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->show_tabs_item
))) {
626 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), TRUE
);
628 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
632 static void gd_menu_full_screen(GtkMenuItem
*item
, void *opaque
)
634 GtkDisplayState
*s
= opaque
;
636 if (!s
->full_screen
) {
637 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
638 gtk_widget_set_size_request(s
->menu_bar
, 0, 0);
639 gtk_widget_set_size_request(s
->drawing_area
, -1, -1);
640 gtk_window_fullscreen(GTK_WINDOW(s
->window
));
642 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), TRUE
);
644 s
->full_screen
= TRUE
;
646 gtk_window_unfullscreen(GTK_WINDOW(s
->window
));
647 gd_menu_show_tabs(GTK_MENU_ITEM(s
->show_tabs_item
), s
);
648 gtk_widget_set_size_request(s
->menu_bar
, -1, -1);
649 gtk_widget_set_size_request(s
->drawing_area
,
650 ds_get_width(s
->ds
), ds_get_height(s
->ds
));
651 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
), FALSE
);
652 s
->full_screen
= FALSE
;
657 gd_update_cursor(s
, FALSE
);
660 static void gd_menu_zoom_in(GtkMenuItem
*item
, void *opaque
)
662 GtkDisplayState
*s
= opaque
;
664 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
673 static void gd_menu_zoom_out(GtkMenuItem
*item
, void *opaque
)
675 GtkDisplayState
*s
= opaque
;
677 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
),
683 s
->scale_x
= MAX(s
->scale_x
, .25);
684 s
->scale_y
= MAX(s
->scale_y
, .25);
689 static void gd_menu_zoom_fixed(GtkMenuItem
*item
, void *opaque
)
691 GtkDisplayState
*s
= opaque
;
699 static void gd_menu_zoom_fit(GtkMenuItem
*item
, void *opaque
)
701 GtkDisplayState
*s
= opaque
;
704 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s
->zoom_fit_item
))) {
705 s
->free_scale
= TRUE
;
707 s
->free_scale
= FALSE
;
712 gdk_drawable_get_size(gtk_widget_get_window(s
->drawing_area
), &ww
, &wh
);
713 gtk_widget_queue_draw_area(s
->drawing_area
, 0, 0, ww
, wh
);
716 static void gd_grab_keyboard(GtkDisplayState
*s
)
718 gdk_keyboard_grab(gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
)),
723 static void gd_ungrab_keyboard(GtkDisplayState
*s
)
725 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
728 static void gd_menu_grab_input(GtkMenuItem
*item
, void *opaque
)
730 GtkDisplayState
*s
= opaque
;
732 if (gd_is_grab_active(s
)) {
734 gdk_pointer_grab(gtk_widget_get_window(GTK_WIDGET(s
->drawing_area
)),
735 FALSE
, /* All events to come to our window directly */
736 GDK_POINTER_MOTION_MASK
|
737 GDK_BUTTON_PRESS_MASK
|
738 GDK_BUTTON_RELEASE_MASK
|
739 GDK_BUTTON_MOTION_MASK
|
741 NULL
, /* Allow cursor to move over entire desktop */
745 gd_ungrab_keyboard(s
);
746 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
749 gd_update_caption(s
);
750 gd_update_cursor(s
, FALSE
);
753 static void gd_change_page(GtkNotebook
*nb
, gpointer arg1
, guint arg2
,
756 GtkDisplayState
*s
= data
;
760 if (!gtk_widget_get_realized(s
->notebook
)) {
764 last_page
= gtk_notebook_get_current_page(nb
);
767 gtk_widget_set_size_request(s
->vc
[last_page
- 1].terminal
, -1, -1);
773 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
775 } else if (s
->full_screen
) {
776 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->grab_item
),
781 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s
->vga_item
), TRUE
);
783 VirtualConsole
*vc
= &s
->vc
[arg2
- 1];
784 VteTerminal
*term
= VTE_TERMINAL(vc
->terminal
);
787 width
= 80 * vte_terminal_get_char_width(term
);
788 height
= 25 * vte_terminal_get_char_height(term
);
790 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc
->menu_item
), TRUE
);
791 gtk_widget_set_size_request(vc
->terminal
, width
, height
);
794 gtk_widget_set_sensitive(s
->grab_item
, on_vga
);
796 gd_update_cursor(s
, TRUE
);
799 static gboolean
gd_enter_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
801 GtkDisplayState
*s
= data
;
803 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
810 static gboolean
gd_leave_event(GtkWidget
*widget
, GdkEventCrossing
*crossing
, gpointer data
)
812 GtkDisplayState
*s
= data
;
814 if (!gd_is_grab_active(s
) && gd_grab_on_hover(s
)) {
815 gd_ungrab_keyboard(s
);
821 /** Virtual Console Callbacks **/
823 static int gd_vc_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
825 VirtualConsole
*vc
= chr
->opaque
;
827 return write(vc
->fd
, buf
, len
);
831 static CharDriverState
*vcs
[MAX_VCS
];
833 static CharDriverState
*gd_vc_handler(QemuOpts
*opts
)
835 CharDriverState
*chr
;
837 chr
= g_malloc0(sizeof(*chr
));
838 chr
->chr_write
= gd_vc_chr_write
;
845 void early_gtk_display_init(void)
847 register_vc_handler(gd_vc_handler
);
850 static gboolean
gd_vc_in(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
852 VirtualConsole
*vc
= opaque
;
853 uint8_t buffer
[1024];
856 len
= read(vc
->fd
, buffer
, sizeof(buffer
));
861 qemu_chr_be_write(vc
->chr
, buffer
, len
);
866 static GSList
*gd_vc_init(GtkDisplayState
*s
, VirtualConsole
*vc
, int index
, GSList
*group
)
871 #if VTE_CHECK_VERSION(0, 26, 0)
875 GtkWidget
*scrolled_window
;
876 GtkAdjustment
*vadjustment
;
877 int master_fd
, slave_fd
, ret
;
880 snprintf(buffer
, sizeof(buffer
), "vc%d", index
);
881 snprintf(path
, sizeof(path
), "<QEMU>/View/VC%d", index
);
883 vc
->chr
= vcs
[index
];
885 if (vc
->chr
->label
) {
886 label
= vc
->chr
->label
;
891 vc
->menu_item
= gtk_radio_menu_item_new_with_mnemonic(group
, label
);
892 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc
->menu_item
));
893 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc
->menu_item
), path
);
894 gtk_accel_map_add_entry(path
, GDK_KEY_2
+ index
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
896 vc
->terminal
= vte_terminal_new();
898 ret
= openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
);
901 /* Set raw attributes on the pty. */
902 tcgetattr(slave_fd
, &tty
);
904 tcsetattr(slave_fd
, TCSAFLUSH
, &tty
);
906 #if VTE_CHECK_VERSION(0, 26, 0)
907 pty
= vte_pty_new_foreign(master_fd
, NULL
);
908 vte_terminal_set_pty_object(VTE_TERMINAL(vc
->terminal
), pty
);
910 vte_terminal_set_pty(VTE_TERMINAL(vc
->terminal
), master_fd
);
913 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->terminal
), -1);
915 vadjustment
= vte_terminal_get_adjustment(VTE_TERMINAL(vc
->terminal
));
917 scrolled_window
= gtk_scrolled_window_new(NULL
, vadjustment
);
918 gtk_container_add(GTK_CONTAINER(scrolled_window
), vc
->terminal
);
920 vte_terminal_set_size(VTE_TERMINAL(vc
->terminal
), 80, 25);
923 vc
->chr
->opaque
= vc
;
924 vc
->scrolled_window
= scrolled_window
;
926 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc
->scrolled_window
),
927 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
929 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), scrolled_window
, gtk_label_new(label
));
930 g_signal_connect(vc
->menu_item
, "activate",
931 G_CALLBACK(gd_menu_switch_vc
), s
);
933 gtk_menu_append(GTK_MENU(s
->view_menu
), vc
->menu_item
);
935 qemu_chr_generic_open(vc
->chr
);
937 vc
->chr
->init(vc
->chr
);
940 chan
= g_io_channel_unix_new(vc
->fd
);
941 g_io_add_watch(chan
, G_IO_IN
, gd_vc_in
, vc
);
946 /** Window Creation **/
948 static void gd_connect_signals(GtkDisplayState
*s
)
950 g_signal_connect(s
->show_tabs_item
, "activate",
951 G_CALLBACK(gd_menu_show_tabs
), s
);
953 g_signal_connect(s
->window
, "key-press-event",
954 G_CALLBACK(gd_window_key_event
), s
);
955 g_signal_connect(s
->window
, "delete-event",
956 G_CALLBACK(gd_window_close
), s
);
958 g_signal_connect(s
->drawing_area
, "expose-event",
959 G_CALLBACK(gd_expose_event
), s
);
960 g_signal_connect(s
->drawing_area
, "motion-notify-event",
961 G_CALLBACK(gd_motion_event
), s
);
962 g_signal_connect(s
->drawing_area
, "button-press-event",
963 G_CALLBACK(gd_button_event
), s
);
964 g_signal_connect(s
->drawing_area
, "button-release-event",
965 G_CALLBACK(gd_button_event
), s
);
966 g_signal_connect(s
->drawing_area
, "key-press-event",
967 G_CALLBACK(gd_key_event
), s
);
968 g_signal_connect(s
->drawing_area
, "key-release-event",
969 G_CALLBACK(gd_key_event
), s
);
971 g_signal_connect(s
->quit_item
, "activate",
972 G_CALLBACK(gd_menu_quit
), s
);
973 g_signal_connect(s
->full_screen_item
, "activate",
974 G_CALLBACK(gd_menu_full_screen
), s
);
975 g_signal_connect(s
->zoom_in_item
, "activate",
976 G_CALLBACK(gd_menu_zoom_in
), s
);
977 g_signal_connect(s
->zoom_out_item
, "activate",
978 G_CALLBACK(gd_menu_zoom_out
), s
);
979 g_signal_connect(s
->zoom_fixed_item
, "activate",
980 G_CALLBACK(gd_menu_zoom_fixed
), s
);
981 g_signal_connect(s
->zoom_fit_item
, "activate",
982 G_CALLBACK(gd_menu_zoom_fit
), s
);
983 g_signal_connect(s
->vga_item
, "activate",
984 G_CALLBACK(gd_menu_switch_vc
), s
);
985 g_signal_connect(s
->grab_item
, "activate",
986 G_CALLBACK(gd_menu_grab_input
), s
);
987 g_signal_connect(s
->notebook
, "switch-page",
988 G_CALLBACK(gd_change_page
), s
);
989 g_signal_connect(s
->drawing_area
, "enter-notify-event",
990 G_CALLBACK(gd_enter_event
), s
);
991 g_signal_connect(s
->drawing_area
, "leave-notify-event",
992 G_CALLBACK(gd_leave_event
), s
);
995 static void gd_create_menus(GtkDisplayState
*s
)
998 GtkAccelGroup
*accel_group
;
999 GSList
*group
= NULL
;
1000 GtkWidget
*separator
;
1003 accel_group
= gtk_accel_group_new();
1004 s
->file_menu
= gtk_menu_new();
1005 gtk_menu_set_accel_group(GTK_MENU(s
->file_menu
), accel_group
);
1006 s
->file_menu_item
= gtk_menu_item_new_with_mnemonic(_("_File"));
1008 s
->quit_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, NULL
);
1009 gtk_stock_lookup(GTK_STOCK_QUIT
, &item
);
1010 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->quit_item
),
1011 "<QEMU>/File/Quit");
1012 gtk_accel_map_add_entry("<QEMU>/File/Quit", item
.keyval
, item
.modifier
);
1014 s
->view_menu
= gtk_menu_new();
1015 gtk_menu_set_accel_group(GTK_MENU(s
->view_menu
), accel_group
);
1016 s
->view_menu_item
= gtk_menu_item_new_with_mnemonic(_("_View"));
1018 s
->full_screen_item
=
1019 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN
, NULL
);
1020 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->full_screen_item
),
1021 "<QEMU>/View/Full Screen");
1022 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1023 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->full_screen_item
);
1025 separator
= gtk_separator_menu_item_new();
1026 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1028 s
->zoom_in_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN
, NULL
);
1029 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_in_item
),
1030 "<QEMU>/View/Zoom In");
1031 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1032 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_in_item
);
1034 s
->zoom_out_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT
, NULL
);
1035 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_out_item
),
1036 "<QEMU>/View/Zoom Out");
1037 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1038 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_out_item
);
1040 s
->zoom_fixed_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100
, NULL
);
1041 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->zoom_fixed_item
),
1042 "<QEMU>/View/Zoom Fixed");
1043 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1044 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_fixed_item
);
1046 s
->zoom_fit_item
= gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1047 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->zoom_fit_item
);
1049 separator
= gtk_separator_menu_item_new();
1050 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1052 s
->grab_on_hover_item
= gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1053 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->grab_on_hover_item
);
1055 s
->grab_item
= gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1056 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->grab_item
),
1057 "<QEMU>/View/Grab Input");
1058 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1059 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->grab_item
);
1061 separator
= gtk_separator_menu_item_new();
1062 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1064 s
->vga_item
= gtk_radio_menu_item_new_with_mnemonic(group
, "_VGA");
1065 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s
->vga_item
));
1066 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s
->vga_item
),
1068 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1
, GDK_CONTROL_MASK
| GDK_MOD1_MASK
);
1069 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->vga_item
);
1071 for (i
= 0; i
< nb_vcs
; i
++) {
1072 VirtualConsole
*vc
= &s
->vc
[i
];
1074 group
= gd_vc_init(s
, vc
, i
, group
);
1078 separator
= gtk_separator_menu_item_new();
1079 gtk_menu_append(GTK_MENU(s
->view_menu
), separator
);
1081 s
->show_tabs_item
= gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1082 gtk_menu_append(GTK_MENU(s
->view_menu
), s
->show_tabs_item
);
1084 g_object_set_data(G_OBJECT(s
->window
), "accel_group", accel_group
);
1085 gtk_window_add_accel_group(GTK_WINDOW(s
->window
), accel_group
);
1086 s
->accel_group
= accel_group
;
1088 gtk_menu_append(GTK_MENU(s
->file_menu
), s
->quit_item
);
1089 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->file_menu_item
), s
->file_menu
);
1090 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->file_menu_item
);
1092 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s
->view_menu_item
), s
->view_menu
);
1093 gtk_menu_shell_append(GTK_MENU_SHELL(s
->menu_bar
), s
->view_menu_item
);
1096 void gtk_display_init(DisplayState
*ds
)
1098 GtkDisplayState
*s
= g_malloc0(sizeof(*s
));
1100 gtk_init(NULL
, NULL
);
1104 s
->dcl
.dpy_gfx_update
= gd_update
;
1105 s
->dcl
.dpy_gfx_resize
= gd_resize
;
1106 s
->dcl
.dpy_refresh
= gd_refresh
;
1108 s
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1109 s
->vbox
= gtk_vbox_new(FALSE
, 0);
1110 s
->notebook
= gtk_notebook_new();
1111 s
->drawing_area
= gtk_drawing_area_new();
1112 s
->menu_bar
= gtk_menu_bar_new();
1116 s
->free_scale
= FALSE
;
1118 setlocale(LC_ALL
, "");
1119 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR
);
1122 s
->null_cursor
= gdk_cursor_new(GDK_BLANK_CURSOR
);
1124 s
->mouse_mode_notifier
.notify
= gd_mouse_mode_change
;
1125 qemu_add_mouse_mode_change_notifier(&s
->mouse_mode_notifier
);
1126 qemu_add_vm_change_state_handler(gd_change_runstate
, s
);
1128 gtk_notebook_append_page(GTK_NOTEBOOK(s
->notebook
), s
->drawing_area
, gtk_label_new("VGA"));
1132 gd_connect_signals(s
);
1134 gtk_widget_add_events(s
->drawing_area
,
1135 GDK_POINTER_MOTION_MASK
|
1136 GDK_BUTTON_PRESS_MASK
|
1137 GDK_BUTTON_RELEASE_MASK
|
1138 GDK_BUTTON_MOTION_MASK
|
1139 GDK_ENTER_NOTIFY_MASK
|
1140 GDK_LEAVE_NOTIFY_MASK
|
1142 GDK_KEY_PRESS_MASK
);
1143 gtk_widget_set_double_buffered(s
->drawing_area
, FALSE
);
1144 gtk_widget_set_can_focus(s
->drawing_area
, TRUE
);
1146 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1147 gtk_notebook_set_show_border(GTK_NOTEBOOK(s
->notebook
), FALSE
);
1149 gd_update_caption(s
);
1151 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->menu_bar
, FALSE
, TRUE
, 0);
1152 gtk_box_pack_start(GTK_BOX(s
->vbox
), s
->notebook
, TRUE
, TRUE
, 0);
1154 gtk_container_add(GTK_CONTAINER(s
->window
), s
->vbox
);
1156 gtk_widget_show_all(s
->window
);
1158 register_displaychangelistener(ds
, &s
->dcl
);