]> git.proxmox.com Git - qemu.git/blob - ui/gtk.c
ui/gtk: Use menu item from stock for full screen
[qemu.git] / ui / gtk.c
1 /*
2 * GTK UI
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 *
12 * Portions from gtk-vnc:
13 *
14 * GTK VNC Widget
15 *
16 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
18 *
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.
23 *
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.
28 *
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
32 */
33
34 #define GETTEXT_PACKAGE "qemu"
35 #define LOCALEDIR "po"
36
37 #include "qemu-common.h"
38
39 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
40 /* Work around an -Wstrict-prototypes warning in GTK headers */
41 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
42 #endif
43 #include <gtk/gtk.h>
44 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
45 #pragma GCC diagnostic error "-Wstrict-prototypes"
46 #endif
47
48
49 #include <gdk/gdkkeysyms.h>
50 #include <glib/gi18n.h>
51 #include <locale.h>
52 #include <vte/vte.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #include <sys/wait.h>
57 #include <pty.h>
58 #include <math.h>
59
60 #include "ui/console.h"
61 #include "sysemu/sysemu.h"
62 #include "qmp-commands.h"
63 #include "x_keymap.h"
64 #include "keymaps.h"
65 #include "char/char.h"
66
67 //#define DEBUG_GTK
68
69 #ifdef DEBUG_GTK
70 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
71 #else
72 #define DPRINTF(fmt, ...) do { } while (0)
73 #endif
74
75 #define MAX_VCS 10
76
77 typedef struct VirtualConsole
78 {
79 GtkWidget *menu_item;
80 GtkWidget *terminal;
81 GtkWidget *scrolled_window;
82 CharDriverState *chr;
83 int fd;
84 } VirtualConsole;
85
86 typedef struct GtkDisplayState
87 {
88 GtkWidget *window;
89
90 GtkWidget *menu_bar;
91
92 GtkAccelGroup *accel_group;
93
94 GtkWidget *file_menu_item;
95 GtkWidget *file_menu;
96 GtkWidget *quit_item;
97
98 GtkWidget *view_menu_item;
99 GtkWidget *view_menu;
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;
107 GtkWidget *vga_item;
108
109 int nb_vcs;
110 VirtualConsole vc[MAX_VCS];
111
112 GtkWidget *show_tabs_item;
113
114 GtkWidget *vbox;
115 GtkWidget *notebook;
116 GtkWidget *drawing_area;
117 cairo_surface_t *surface;
118 DisplayChangeListener dcl;
119 DisplayState *ds;
120 int button_mask;
121 int last_x;
122 int last_y;
123
124 double scale_x;
125 double scale_y;
126 gboolean full_screen;
127
128 GdkCursor *null_cursor;
129 Notifier mouse_mode_notifier;
130 gboolean free_scale;
131 } GtkDisplayState;
132
133 static GtkDisplayState *global_state;
134
135 /** Utility Functions **/
136
137 static bool gd_is_grab_active(GtkDisplayState *s)
138 {
139 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
140 }
141
142 static bool gd_grab_on_hover(GtkDisplayState *s)
143 {
144 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
145 }
146
147 static bool gd_on_vga(GtkDisplayState *s)
148 {
149 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
150 }
151
152 static void gd_update_cursor(GtkDisplayState *s, gboolean override)
153 {
154 GdkWindow *window;
155 bool on_vga;
156
157 window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));
158
159 on_vga = gd_on_vga(s);
160
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);
164 } else {
165 gdk_window_set_cursor(window, NULL);
166 }
167 }
168
169 static void gd_update_caption(GtkDisplayState *s)
170 {
171 const char *status = "";
172 gchar *title;
173 const char *grab = "";
174
175 if (gd_is_grab_active(s)) {
176 grab = " - Press Ctrl+Alt+G to release grab";
177 }
178
179 if (!runstate_is_running()) {
180 status = " [Stopped]";
181 }
182
183 if (qemu_name) {
184 title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
185 } else {
186 title = g_strdup_printf("QEMU%s%s", status, grab);
187 }
188
189 gtk_window_set_title(GTK_WINDOW(s->window), title);
190
191 g_free(title);
192 }
193
194 /** DisplayState Callbacks **/
195
196 static void gd_update(DisplayState *ds, int x, int y, int w, int h)
197 {
198 GtkDisplayState *s = ds->opaque;
199 int x1, x2, y1, y2;
200 int mx, my;
201 int fbw, fbh;
202 int ww, wh;
203
204 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x, y, w, h);
205
206 x1 = floor(x * s->scale_x);
207 y1 = floor(y * s->scale_y);
208
209 x2 = ceil(x * s->scale_x + w * s->scale_x);
210 y2 = ceil(y * s->scale_y + h * s->scale_y);
211
212 fbw = ds_get_width(s->ds) * s->scale_x;
213 fbh = ds_get_height(s->ds) * s->scale_y;
214
215 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
216
217 mx = my = 0;
218 if (ww > fbw) {
219 mx = (ww - fbw) / 2;
220 }
221 if (wh > fbh) {
222 my = (wh - fbh) / 2;
223 }
224
225 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
226 }
227
228 static void gd_refresh(DisplayState *ds)
229 {
230 vga_hw_update();
231 }
232
233 static void gd_resize(DisplayState *ds)
234 {
235 GtkDisplayState *s = ds->opaque;
236 cairo_format_t kind;
237 int stride;
238
239 DPRINTF("resize(width=%d, height=%d)\n",
240 ds_get_width(ds), ds_get_height(ds));
241
242 if (s->surface) {
243 cairo_surface_destroy(s->surface);
244 }
245
246 switch (ds->surface->pf.bits_per_pixel) {
247 case 8:
248 kind = CAIRO_FORMAT_A8;
249 break;
250 case 16:
251 kind = CAIRO_FORMAT_RGB16_565;
252 break;
253 case 32:
254 kind = CAIRO_FORMAT_RGB24;
255 break;
256 default:
257 g_assert_not_reached();
258 break;
259 }
260
261 stride = cairo_format_stride_for_width(kind, ds_get_width(ds));
262 g_assert(ds_get_linesize(ds) == stride);
263
264 s->surface = cairo_image_surface_create_for_data(ds_get_data(ds),
265 kind,
266 ds_get_width(ds),
267 ds_get_height(ds),
268 ds_get_linesize(ds));
269
270 if (!s->full_screen) {
271 GtkRequisition req;
272 double sx, sy;
273
274 if (s->free_scale) {
275 sx = s->scale_x;
276 sy = s->scale_y;
277
278 s->scale_y = 1.0;
279 s->scale_x = 1.0;
280 } else {
281 sx = 1.0;
282 sy = 1.0;
283 }
284
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);
289
290 gtk_window_resize(GTK_WINDOW(s->window),
291 req.width * sx, req.height * sy);
292 }
293 }
294
295 /** QEMU Events **/
296
297 static void gd_change_runstate(void *opaque, int running, RunState state)
298 {
299 GtkDisplayState *s = opaque;
300
301 gd_update_caption(s);
302 }
303
304 static void gd_mouse_mode_change(Notifier *notify, void *data)
305 {
306 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
307 FALSE);
308 }
309
310 /** GTK Events **/
311
312 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
313 {
314 GtkDisplayState *s = opaque;
315 GtkAccelGroupEntry *entries;
316 guint n_entries = 0;
317 gboolean propagate_accel = TRUE;
318 gboolean handled = FALSE;
319
320 entries = gtk_accel_group_query(s->accel_group, key->keyval,
321 key->state, &n_entries);
322 if (n_entries) {
323 const char *quark = g_quark_to_string(entries[0].accel_path_quark);
324
325 if (gd_is_grab_active(s) && strstart(quark, "<QEMU>/File/", NULL)) {
326 propagate_accel = FALSE;
327 }
328 }
329
330 if (!handled && propagate_accel) {
331 handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
332 }
333
334 if (!handled) {
335 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
336 }
337
338 return handled;
339 }
340
341 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
342 void *opaque)
343 {
344 GtkDisplayState *s = opaque;
345
346 if (!no_quit) {
347 unregister_displaychangelistener(s->ds, &s->dcl);
348 qmp_quit(NULL);
349 return FALSE;
350 }
351
352 return TRUE;
353 }
354
355 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
356 {
357 GtkDisplayState *s = opaque;
358 int mx, my;
359 int ww, wh;
360 int fbw, fbh;
361
362 if (!gtk_widget_get_realized(widget)) {
363 return FALSE;
364 }
365
366 fbw = ds_get_width(s->ds);
367 fbh = ds_get_height(s->ds);
368
369 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
370
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) {
375 double sx, sy;
376
377 sx = (double)ww / fbw;
378 sy = (double)wh / fbh;
379
380 s->scale_x = s->scale_y = MIN(sx, sy);
381 }
382
383 fbw *= s->scale_x;
384 fbh *= s->scale_y;
385
386 mx = my = 0;
387 if (ww > fbw) {
388 mx = (ww - fbw) / 2;
389 }
390 if (wh > fbh) {
391 my = (wh - fbh) / 2;
392 }
393
394 cairo_rectangle(cr, 0, 0, ww, wh);
395
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,
402 -1 * fbw, fbh);
403 cairo_fill(cr);
404
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);
407 cairo_paint(cr);
408
409 return TRUE;
410 }
411
412 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
413 void *opaque)
414 {
415 cairo_t *cr;
416 gboolean ret;
417
418 cr = gdk_cairo_create(gtk_widget_get_window(widget));
419 cairo_rectangle(cr,
420 expose->area.x,
421 expose->area.y,
422 expose->area.width,
423 expose->area.height);
424 cairo_clip(cr);
425
426 ret = gd_draw_event(widget, cr, opaque);
427
428 cairo_destroy(cr);
429
430 return ret;
431 }
432
433 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
434 void *opaque)
435 {
436 GtkDisplayState *s = opaque;
437 int dx, dy;
438 int x, y;
439 int mx, my;
440 int fbh, fbw;
441 int ww, wh;
442
443 fbw = ds_get_width(s->ds) * s->scale_x;
444 fbh = ds_get_height(s->ds) * s->scale_y;
445
446 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
447
448 mx = my = 0;
449 if (ww > fbw) {
450 mx = (ww - fbw) / 2;
451 }
452 if (wh > fbh) {
453 my = (wh - fbh) / 2;
454 }
455
456 x = (motion->x - mx) / s->scale_x;
457 y = (motion->y - my) / s->scale_y;
458
459 if (x < 0 || y < 0 ||
460 x >= ds_get_width(s->ds) ||
461 y >= ds_get_height(s->ds)) {
462 return TRUE;
463 }
464
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) {
469 dx = 0;
470 dy = 0;
471 } else {
472 dx = x - s->last_x;
473 dy = y - s->last_y;
474 }
475
476 s->last_x = x;
477 s->last_y = y;
478
479 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) {
480 kbd_mouse_event(dx, dy, 0, s->button_mask);
481 }
482
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;
489
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 */
497 if (x == 0) {
498 x += 200;
499 }
500 if (y == 0) {
501 y += 200;
502 }
503 if (x == (gdk_screen_get_width(screen) - 1)) {
504 x -= 200;
505 }
506 if (y == (gdk_screen_get_height(screen) - 1)) {
507 y -= 200;
508 }
509
510 if (x != (int)motion->x_root || y != (int)motion->y_root) {
511 gdk_display_warp_pointer(display, screen, x, y);
512 s->last_x = -1;
513 s->last_y = -1;
514 return FALSE;
515 }
516 }
517 return TRUE;
518 }
519
520 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
521 void *opaque)
522 {
523 GtkDisplayState *s = opaque;
524 int dx, dy;
525 int n;
526
527 if (button->button == 1) {
528 n = 0x01;
529 } else if (button->button == 2) {
530 n = 0x04;
531 } else if (button->button == 3) {
532 n = 0x02;
533 } else {
534 n = 0x00;
535 }
536
537 if (button->type == GDK_BUTTON_PRESS) {
538 s->button_mask |= n;
539 } else if (button->type == GDK_BUTTON_RELEASE) {
540 s->button_mask &= ~n;
541 }
542
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);
546 } else {
547 dx = 0;
548 dy = 0;
549 }
550
551 kbd_mouse_event(dx, dy, 0, s->button_mask);
552
553 return TRUE;
554 }
555
556 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
557 {
558 int gdk_keycode;
559 int qemu_keycode;
560
561 gdk_keycode = key->hardware_keycode;
562
563 if (gdk_keycode < 9) {
564 qemu_keycode = 0;
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 */
570 qemu_keycode = 0x70;
571 } else if (gdk_keycode == 211) { /* backslash */
572 qemu_keycode = 0x73;
573 } else {
574 qemu_keycode = 0;
575 }
576
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");
580
581 if (qemu_keycode & SCANCODE_GREY) {
582 kbd_put_keycode(SCANCODE_EMUL0);
583 }
584
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);
589 } else {
590 g_assert_not_reached();
591 }
592
593 return TRUE;
594 }
595
596 /** Window Menu Actions **/
597
598 static void gd_menu_quit(GtkMenuItem *item, void *opaque)
599 {
600 qmp_quit(NULL);
601 }
602
603 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
604 {
605 GtkDisplayState *s = opaque;
606
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);
609 } else {
610 int i;
611
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);
615 break;
616 }
617 }
618 }
619 }
620
621 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
622 {
623 GtkDisplayState *s = opaque;
624
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);
627 } else {
628 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
629 }
630 }
631
632 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
633 {
634 GtkDisplayState *s = opaque;
635
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));
641 if (gd_on_vga(s)) {
642 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
643 }
644 s->full_screen = TRUE;
645 } else {
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;
653 s->scale_x = 1.0;
654 s->scale_y = 1.0;
655 }
656
657 gd_update_cursor(s, FALSE);
658 }
659
660 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
661 {
662 GtkDisplayState *s = opaque;
663
664 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
665 FALSE);
666
667 s->scale_x += .25;
668 s->scale_y += .25;
669
670 gd_resize(s->ds);
671 }
672
673 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
674 {
675 GtkDisplayState *s = opaque;
676
677 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
678 FALSE);
679
680 s->scale_x -= .25;
681 s->scale_y -= .25;
682
683 s->scale_x = MAX(s->scale_x, .25);
684 s->scale_y = MAX(s->scale_y, .25);
685
686 gd_resize(s->ds);
687 }
688
689 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
690 {
691 GtkDisplayState *s = opaque;
692
693 s->scale_x = 1.0;
694 s->scale_y = 1.0;
695
696 gd_resize(s->ds);
697 }
698
699 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
700 {
701 GtkDisplayState *s = opaque;
702 int ww, wh;
703
704 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
705 s->free_scale = TRUE;
706 } else {
707 s->free_scale = FALSE;
708 }
709
710 gd_resize(s->ds);
711
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);
714 }
715
716 static void gd_grab_keyboard(GtkDisplayState *s)
717 {
718 gdk_keyboard_grab(gtk_widget_get_window(GTK_WIDGET(s->drawing_area)),
719 FALSE,
720 GDK_CURRENT_TIME);
721 }
722
723 static void gd_ungrab_keyboard(GtkDisplayState *s)
724 {
725 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
726 }
727
728 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
729 {
730 GtkDisplayState *s = opaque;
731
732 if (gd_is_grab_active(s)) {
733 gd_grab_keyboard(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 |
740 GDK_SCROLL_MASK,
741 NULL, /* Allow cursor to move over entire desktop */
742 s->null_cursor,
743 GDK_CURRENT_TIME);
744 } else {
745 gd_ungrab_keyboard(s);
746 gdk_pointer_ungrab(GDK_CURRENT_TIME);
747 }
748
749 gd_update_caption(s);
750 gd_update_cursor(s, FALSE);
751 }
752
753 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
754 gpointer data)
755 {
756 GtkDisplayState *s = data;
757 guint last_page;
758 gboolean on_vga;
759
760 if (!gtk_widget_get_realized(s->notebook)) {
761 return;
762 }
763
764 last_page = gtk_notebook_get_current_page(nb);
765
766 if (last_page) {
767 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
768 }
769
770 on_vga = arg2 == 0;
771
772 if (!on_vga) {
773 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
774 FALSE);
775 } else if (s->full_screen) {
776 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
777 TRUE);
778 }
779
780 if (arg2 == 0) {
781 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
782 } else {
783 VirtualConsole *vc = &s->vc[arg2 - 1];
784 VteTerminal *term = VTE_TERMINAL(vc->terminal);
785 int width, height;
786
787 width = 80 * vte_terminal_get_char_width(term);
788 height = 25 * vte_terminal_get_char_height(term);
789
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);
792 }
793
794 gtk_widget_set_sensitive(s->grab_item, on_vga);
795
796 gd_update_cursor(s, TRUE);
797 }
798
799 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
800 {
801 GtkDisplayState *s = data;
802
803 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
804 gd_grab_keyboard(s);
805 }
806
807 return TRUE;
808 }
809
810 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
811 {
812 GtkDisplayState *s = data;
813
814 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
815 gd_ungrab_keyboard(s);
816 }
817
818 return TRUE;
819 }
820
821 /** Virtual Console Callbacks **/
822
823 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
824 {
825 VirtualConsole *vc = chr->opaque;
826
827 return write(vc->fd, buf, len);
828 }
829
830 static int nb_vcs;
831 static CharDriverState *vcs[MAX_VCS];
832
833 static CharDriverState *gd_vc_handler(QemuOpts *opts)
834 {
835 CharDriverState *chr;
836
837 chr = g_malloc0(sizeof(*chr));
838 chr->chr_write = gd_vc_chr_write;
839
840 vcs[nb_vcs++] = chr;
841
842 return chr;
843 }
844
845 void early_gtk_display_init(void)
846 {
847 register_vc_handler(gd_vc_handler);
848 }
849
850 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
851 {
852 VirtualConsole *vc = opaque;
853 uint8_t buffer[1024];
854 ssize_t len;
855
856 len = read(vc->fd, buffer, sizeof(buffer));
857 if (len <= 0) {
858 return FALSE;
859 }
860
861 qemu_chr_be_write(vc->chr, buffer, len);
862
863 return TRUE;
864 }
865
866 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group)
867 {
868 const char *label;
869 char buffer[32];
870 char path[32];
871 #if VTE_CHECK_VERSION(0, 26, 0)
872 VtePty *pty;
873 #endif
874 GIOChannel *chan;
875 GtkWidget *scrolled_window;
876 GtkAdjustment *vadjustment;
877 int master_fd, slave_fd, ret;
878 struct termios tty;
879
880 snprintf(buffer, sizeof(buffer), "vc%d", index);
881 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
882
883 vc->chr = vcs[index];
884
885 if (vc->chr->label) {
886 label = vc->chr->label;
887 } else {
888 label = buffer;
889 }
890
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);
895
896 vc->terminal = vte_terminal_new();
897
898 ret = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
899 g_assert(ret != -1);
900
901 /* Set raw attributes on the pty. */
902 tcgetattr(slave_fd, &tty);
903 cfmakeraw(&tty);
904 tcsetattr(slave_fd, TCSAFLUSH, &tty);
905
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);
909 #else
910 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
911 #endif
912
913 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);
914
915 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));
916
917 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
918 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);
919
920 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);
921
922 vc->fd = slave_fd;
923 vc->chr->opaque = vc;
924 vc->scrolled_window = scrolled_window;
925
926 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
927 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
928
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);
932
933 gtk_menu_append(GTK_MENU(s->view_menu), vc->menu_item);
934
935 qemu_chr_generic_open(vc->chr);
936 if (vc->chr->init) {
937 vc->chr->init(vc->chr);
938 }
939
940 chan = g_io_channel_unix_new(vc->fd);
941 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);
942
943 return group;
944 }
945
946 /** Window Creation **/
947
948 static void gd_connect_signals(GtkDisplayState *s)
949 {
950 g_signal_connect(s->show_tabs_item, "activate",
951 G_CALLBACK(gd_menu_show_tabs), s);
952
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);
957
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);
970
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);
993 }
994
995 static void gd_create_menus(GtkDisplayState *s)
996 {
997 GtkStockItem item;
998 GtkAccelGroup *accel_group;
999 GSList *group = NULL;
1000 GtkWidget *separator;
1001 int i;
1002
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"));
1007
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);
1013
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"));
1017
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);
1024
1025 separator = gtk_separator_menu_item_new();
1026 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1027
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);
1033
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);
1039
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);
1045
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);
1048
1049 separator = gtk_separator_menu_item_new();
1050 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1051
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);
1054
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);
1060
1061 separator = gtk_separator_menu_item_new();
1062 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1063
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),
1067 "<QEMU>/View/VGA");
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);
1070
1071 for (i = 0; i < nb_vcs; i++) {
1072 VirtualConsole *vc = &s->vc[i];
1073
1074 group = gd_vc_init(s, vc, i, group);
1075 s->nb_vcs++;
1076 }
1077
1078 separator = gtk_separator_menu_item_new();
1079 gtk_menu_append(GTK_MENU(s->view_menu), separator);
1080
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);
1083
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;
1087
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);
1091
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);
1094 }
1095
1096 void gtk_display_init(DisplayState *ds)
1097 {
1098 GtkDisplayState *s = g_malloc0(sizeof(*s));
1099
1100 gtk_init(NULL, NULL);
1101
1102 ds->opaque = s;
1103 s->ds = ds;
1104 s->dcl.dpy_gfx_update = gd_update;
1105 s->dcl.dpy_gfx_resize = gd_resize;
1106 s->dcl.dpy_refresh = gd_refresh;
1107
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();
1113
1114 s->scale_x = 1.0;
1115 s->scale_y = 1.0;
1116 s->free_scale = FALSE;
1117
1118 setlocale(LC_ALL, "");
1119 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1120 textdomain("qemu");
1121
1122 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1123
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);
1127
1128 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));
1129
1130 gd_create_menus(s);
1131
1132 gd_connect_signals(s);
1133
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 |
1141 GDK_SCROLL_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);
1145
1146 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1147 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1148
1149 gd_update_caption(s);
1150
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);
1153
1154 gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1155
1156 gtk_widget_show_all(s->window);
1157
1158 register_displaychangelistener(ds, &s->dcl);
1159
1160 global_state = s;
1161 }