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 push 42 #pragma GCC diagnostic ignored "-Wstrict-prototypes" 43 #endif 44 #include <gtk/gtk.h> 45 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE 46 #pragma GCC diagnostic pop 47 #endif 48 49 50 #include <gdk/gdkkeysyms.h> 51 #include <glib/gi18n.h> 52 #include <locale.h> 53 #include <vte/vte.h> 54 #include <sys/types.h> 55 #include <sys/socket.h> 56 #include <sys/un.h> 57 #include <sys/wait.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 "sysemu/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 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) 81 { 82 *ww = gdk_window_get_width(w); 83 *wh = gdk_window_get_height(w); 84 } 85 #endif 86 87 #if !GTK_CHECK_VERSION(2, 20, 0) 88 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget) 89 #endif 90 91 #ifndef GDK_KEY_0 92 #define GDK_KEY_0 GDK_0 93 #define GDK_KEY_1 GDK_1 94 #define GDK_KEY_2 GDK_2 95 #define GDK_KEY_f GDK_f 96 #define GDK_KEY_g GDK_g 97 #define GDK_KEY_plus GDK_plus 98 #define GDK_KEY_minus GDK_minus 99 #endif 100 101 static const int modifier_keycode[] = { 102 /* shift, control, alt keys, meta keys, both left & right */ 103 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd, 104 }; 105 106 typedef struct VirtualConsole 107 { 108 GtkWidget *menu_item; 109 GtkWidget *terminal; 110 GtkWidget *scrolled_window; 111 CharDriverState *chr; 112 int fd; 113 } VirtualConsole; 114 115 typedef struct GtkDisplayState 116 { 117 GtkWidget *window; 118 119 GtkWidget *menu_bar; 120 121 GtkAccelGroup *accel_group; 122 123 GtkWidget *machine_menu_item; 124 GtkWidget *machine_menu; 125 GtkWidget *pause_item; 126 GtkWidget *reset_item; 127 GtkWidget *powerdown_item; 128 GtkWidget *quit_item; 129 130 GtkWidget *view_menu_item; 131 GtkWidget *view_menu; 132 GtkWidget *full_screen_item; 133 GtkWidget *zoom_in_item; 134 GtkWidget *zoom_out_item; 135 GtkWidget *zoom_fixed_item; 136 GtkWidget *zoom_fit_item; 137 GtkWidget *grab_item; 138 GtkWidget *grab_on_hover_item; 139 GtkWidget *vga_item; 140 141 int nb_vcs; 142 VirtualConsole vc[MAX_VCS]; 143 144 GtkWidget *show_tabs_item; 145 146 GtkWidget *vbox; 147 GtkWidget *notebook; 148 GtkWidget *drawing_area; 149 cairo_surface_t *surface; 150 pixman_image_t *convert; 151 DisplayChangeListener dcl; 152 DisplaySurface *ds; 153 int button_mask; 154 int last_x; 155 int last_y; 156 157 double scale_x; 158 double scale_y; 159 gboolean full_screen; 160 161 GdkCursor *null_cursor; 162 Notifier mouse_mode_notifier; 163 gboolean free_scale; 164 165 bool external_pause_update; 166 167 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)]; 168 } GtkDisplayState; 169 170 static GtkDisplayState *global_state; 171 172 /** Utility Functions **/ 173 174 static bool gd_is_grab_active(GtkDisplayState *s) 175 { 176 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item)); 177 } 178 179 static bool gd_grab_on_hover(GtkDisplayState *s) 180 { 181 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item)); 182 } 183 184 static bool gd_on_vga(GtkDisplayState *s) 185 { 186 return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0; 187 } 188 189 static void gd_update_cursor(GtkDisplayState *s, gboolean override) 190 { 191 GdkWindow *window; 192 bool on_vga; 193 194 window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area)); 195 196 on_vga = gd_on_vga(s); 197 198 if ((override || on_vga) && 199 (s->full_screen || kbd_mouse_is_absolute() || gd_is_grab_active(s))) { 200 gdk_window_set_cursor(window, s->null_cursor); 201 } else { 202 gdk_window_set_cursor(window, NULL); 203 } 204 } 205 206 static void gd_update_caption(GtkDisplayState *s) 207 { 208 const char *status = ""; 209 gchar *title; 210 const char *grab = ""; 211 bool is_paused = !runstate_is_running(); 212 213 if (gd_is_grab_active(s)) { 214 grab = _(" - Press Ctrl+Alt+G to release grab"); 215 } 216 217 if (is_paused) { 218 status = _(" [Paused]"); 219 } 220 s->external_pause_update = true; 221 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item), 222 is_paused); 223 s->external_pause_update = false; 224 225 if (qemu_name) { 226 title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab); 227 } else { 228 title = g_strdup_printf("QEMU%s%s", status, grab); 229 } 230 231 gtk_window_set_title(GTK_WINDOW(s->window), title); 232 233 g_free(title); 234 } 235 236 static void gd_update_windowsize(GtkDisplayState *s) 237 { 238 if (!s->full_screen) { 239 GtkRequisition req; 240 double sx, sy; 241 242 if (s->free_scale) { 243 sx = s->scale_x; 244 sy = s->scale_y; 245 246 s->scale_y = 1.0; 247 s->scale_x = 1.0; 248 } else { 249 sx = 1.0; 250 sy = 1.0; 251 } 252 253 gtk_widget_set_size_request(s->drawing_area, 254 surface_width(s->ds) * s->scale_x, 255 surface_height(s->ds) * s->scale_y); 256 #if GTK_CHECK_VERSION(3, 0, 0) 257 gtk_widget_get_preferred_size(s->vbox, NULL, &req); 258 #else 259 gtk_widget_size_request(s->vbox, &req); 260 #endif 261 262 gtk_window_resize(GTK_WINDOW(s->window), 263 req.width * sx, req.height * sy); 264 } 265 } 266 267 static void gd_update_full_redraw(GtkDisplayState *s) 268 { 269 int ww, wh; 270 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh); 271 gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh); 272 } 273 274 static void gtk_release_modifiers(GtkDisplayState *s) 275 { 276 int i, keycode; 277 278 if (!gd_on_vga(s)) { 279 return; 280 } 281 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) { 282 keycode = modifier_keycode[i]; 283 if (!s->modifier_pressed[i]) { 284 continue; 285 } 286 if (keycode & SCANCODE_GREY) { 287 kbd_put_keycode(SCANCODE_EMUL0); 288 } 289 kbd_put_keycode(keycode | SCANCODE_UP); 290 s->modifier_pressed[i] = false; 291 } 292 } 293 294 /** DisplayState Callbacks **/ 295 296 static void gd_update(DisplayChangeListener *dcl, 297 int x, int y, int w, int h) 298 { 299 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl); 300 int x1, x2, y1, y2; 301 int mx, my; 302 int fbw, fbh; 303 int ww, wh; 304 305 DPRINTF("update(x=%d, y=%d, w=%d, h=%d)\n", x, y, w, h); 306 307 if (s->convert) { 308 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert, 309 x, y, 0, 0, x, y, w, h); 310 } 311 312 x1 = floor(x * s->scale_x); 313 y1 = floor(y * s->scale_y); 314 315 x2 = ceil(x * s->scale_x + w * s->scale_x); 316 y2 = ceil(y * s->scale_y + h * s->scale_y); 317 318 fbw = surface_width(s->ds) * s->scale_x; 319 fbh = surface_height(s->ds) * s->scale_y; 320 321 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh); 322 323 mx = my = 0; 324 if (ww > fbw) { 325 mx = (ww - fbw) / 2; 326 } 327 if (wh > fbh) { 328 my = (wh - fbh) / 2; 329 } 330 331 gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1)); 332 } 333 334 static void gd_refresh(DisplayChangeListener *dcl) 335 { 336 graphic_hw_update(dcl->con); 337 } 338 339 #if GTK_CHECK_VERSION(3, 0, 0) 340 static void gd_mouse_set(DisplayChangeListener *dcl, 341 int x, int y, int visible) 342 { 343 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl); 344 GdkDisplay *dpy; 345 GdkDeviceManager *mgr; 346 gint x_root, y_root; 347 348 dpy = gtk_widget_get_display(s->drawing_area); 349 mgr = gdk_display_get_device_manager(dpy); 350 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area), 351 x, y, &x_root, &y_root); 352 gdk_device_warp(gdk_device_manager_get_client_pointer(mgr), 353 gtk_widget_get_screen(s->drawing_area), 354 x, y); 355 } 356 #else 357 static void gd_mouse_set(DisplayChangeListener *dcl, 358 int x, int y, int visible) 359 { 360 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl); 361 gint x_root, y_root; 362 363 gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area), 364 x, y, &x_root, &y_root); 365 gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area), 366 gtk_widget_get_screen(s->drawing_area), 367 x_root, y_root); 368 } 369 #endif 370 371 static void gd_cursor_define(DisplayChangeListener *dcl, 372 QEMUCursor *c) 373 { 374 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl); 375 GdkPixbuf *pixbuf; 376 GdkCursor *cursor; 377 378 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data), 379 GDK_COLORSPACE_RGB, true, 8, 380 c->width, c->height, c->width * 4, 381 NULL, NULL); 382 cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area), 383 pixbuf, c->hot_x, c->hot_y); 384 gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor); 385 g_object_unref(pixbuf); 386 #if !GTK_CHECK_VERSION(3, 0, 0) 387 gdk_cursor_unref(cursor); 388 #else 389 g_object_unref(cursor); 390 #endif 391 } 392 393 static void gd_switch(DisplayChangeListener *dcl, 394 DisplaySurface *surface) 395 { 396 GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl); 397 bool resized = true; 398 399 DPRINTF("resize(width=%d, height=%d)\n", 400 surface_width(surface), surface_height(surface)); 401 402 if (s->surface) { 403 cairo_surface_destroy(s->surface); 404 } 405 406 if (s->ds && 407 surface_width(s->ds) == surface_width(surface) && 408 surface_height(s->ds) == surface_height(surface)) { 409 resized = false; 410 } 411 s->ds = surface; 412 413 if (s->convert) { 414 pixman_image_unref(s->convert); 415 s->convert = NULL; 416 } 417 418 if (surface->format == PIXMAN_x8r8g8b8) { 419 /* 420 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24 421 * 422 * No need to convert, use surface directly. Should be the 423 * common case as this is qemu_default_pixelformat(32) too. 424 */ 425 s->surface = cairo_image_surface_create_for_data 426 (surface_data(surface), 427 CAIRO_FORMAT_RGB24, 428 surface_width(surface), 429 surface_height(surface), 430 surface_stride(surface)); 431 } else { 432 /* Must convert surface, use pixman to do it. */ 433 s->convert = pixman_image_create_bits(PIXMAN_x8r8g8b8, 434 surface_width(surface), 435 surface_height(surface), 436 NULL, 0); 437 s->surface = cairo_image_surface_create_for_data 438 ((void *)pixman_image_get_data(s->convert), 439 CAIRO_FORMAT_RGB24, 440 pixman_image_get_width(s->convert), 441 pixman_image_get_height(s->convert), 442 pixman_image_get_stride(s->convert)); 443 pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert, 444 0, 0, 0, 0, 0, 0, 445 pixman_image_get_width(s->convert), 446 pixman_image_get_height(s->convert)); 447 } 448 449 if (resized) { 450 gd_update_windowsize(s); 451 } else { 452 gd_update_full_redraw(s); 453 } 454 } 455 456 /** QEMU Events **/ 457 458 static void gd_change_runstate(void *opaque, int running, RunState state) 459 { 460 GtkDisplayState *s = opaque; 461 462 gd_update_caption(s); 463 } 464 465 static void gd_mouse_mode_change(Notifier *notify, void *data) 466 { 467 gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier), 468 FALSE); 469 } 470 471 /** GTK Events **/ 472 473 static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque) 474 { 475 GtkDisplayState *s = opaque; 476 GtkAccelGroupEntry *entries; 477 guint n_entries = 0; 478 gboolean propagate_accel = TRUE; 479 gboolean handled = FALSE; 480 481 entries = gtk_accel_group_query(s->accel_group, key->keyval, 482 key->state, &n_entries); 483 if (n_entries) { 484 const char *quark = g_quark_to_string(entries[0].accel_path_quark); 485 486 if (gd_is_grab_active(s) && strstart(quark, "<QEMU>/File/", NULL)) { 487 propagate_accel = FALSE; 488 } 489 } 490 491 if (!handled && propagate_accel) { 492 handled = gtk_window_activate_key(GTK_WINDOW(widget), key); 493 } 494 if (handled) { 495 gtk_release_modifiers(s); 496 } else { 497 handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key); 498 } 499 500 return handled; 501 } 502 503 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event, 504 void *opaque) 505 { 506 GtkDisplayState *s = opaque; 507 508 if (!no_quit) { 509 unregister_displaychangelistener(&s->dcl); 510 qmp_quit(NULL); 511 return FALSE; 512 } 513 514 return TRUE; 515 } 516 517 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque) 518 { 519 GtkDisplayState *s = opaque; 520 int mx, my; 521 int ww, wh; 522 int fbw, fbh; 523 524 if (!gtk_widget_get_realized(widget)) { 525 return FALSE; 526 } 527 528 fbw = surface_width(s->ds); 529 fbh = surface_height(s->ds); 530 531 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh); 532 533 if (s->full_screen) { 534 s->scale_x = (double)ww / fbw; 535 s->scale_y = (double)wh / fbh; 536 } else if (s->free_scale) { 537 double sx, sy; 538 539 sx = (double)ww / fbw; 540 sy = (double)wh / fbh; 541 542 s->scale_x = s->scale_y = MIN(sx, sy); 543 } 544 545 fbw *= s->scale_x; 546 fbh *= s->scale_y; 547 548 mx = my = 0; 549 if (ww > fbw) { 550 mx = (ww - fbw) / 2; 551 } 552 if (wh > fbh) { 553 my = (wh - fbh) / 2; 554 } 555 556 cairo_rectangle(cr, 0, 0, ww, wh); 557 558 /* Optionally cut out the inner area where the pixmap 559 will be drawn. This avoids 'flashing' since we're 560 not double-buffering. Note we're using the undocumented 561 behaviour of drawing the rectangle from right to left 562 to cut out the whole */ 563 cairo_rectangle(cr, mx + fbw, my, 564 -1 * fbw, fbh); 565 cairo_fill(cr); 566 567 cairo_scale(cr, s->scale_x, s->scale_y); 568 cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y); 569 cairo_paint(cr); 570 571 return TRUE; 572 } 573 574 #if !GTK_CHECK_VERSION(3, 0, 0) 575 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose, 576 void *opaque) 577 { 578 cairo_t *cr; 579 gboolean ret; 580 581 cr = gdk_cairo_create(gtk_widget_get_window(widget)); 582 cairo_rectangle(cr, 583 expose->area.x, 584 expose->area.y, 585 expose->area.width, 586 expose->area.height); 587 cairo_clip(cr); 588 589 ret = gd_draw_event(widget, cr, opaque); 590 591 cairo_destroy(cr); 592 593 return ret; 594 } 595 #endif 596 597 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion, 598 void *opaque) 599 { 600 GtkDisplayState *s = opaque; 601 int dx, dy; 602 int x, y; 603 int mx, my; 604 int fbh, fbw; 605 int ww, wh; 606 607 fbw = surface_width(s->ds) * s->scale_x; 608 fbh = surface_height(s->ds) * s->scale_y; 609 610 gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh); 611 612 mx = my = 0; 613 if (ww > fbw) { 614 mx = (ww - fbw) / 2; 615 } 616 if (wh > fbh) { 617 my = (wh - fbh) / 2; 618 } 619 620 x = (motion->x - mx) / s->scale_x; 621 y = (motion->y - my) / s->scale_y; 622 623 if (x < 0 || y < 0 || 624 x >= surface_width(s->ds) || 625 y >= surface_height(s->ds)) { 626 return TRUE; 627 } 628 629 if (kbd_mouse_is_absolute()) { 630 dx = x * 0x7FFF / (surface_width(s->ds) - 1); 631 dy = y * 0x7FFF / (surface_height(s->ds) - 1); 632 } else if (s->last_x == -1 || s->last_y == -1) { 633 dx = 0; 634 dy = 0; 635 } else { 636 dx = x - s->last_x; 637 dy = y - s->last_y; 638 } 639 640 s->last_x = x; 641 s->last_y = y; 642 643 if (kbd_mouse_is_absolute() || gd_is_grab_active(s)) { 644 kbd_mouse_event(dx, dy, 0, s->button_mask); 645 } 646 647 if (!kbd_mouse_is_absolute() && gd_is_grab_active(s)) { 648 GdkScreen *screen = gtk_widget_get_screen(s->drawing_area); 649 int x = (int)motion->x_root; 650 int y = (int)motion->y_root; 651 652 /* In relative mode check to see if client pointer hit 653 * one of the screen edges, and if so move it back by 654 * 200 pixels. This is important because the pointer 655 * in the server doesn't correspond 1-for-1, and so 656 * may still be only half way across the screen. Without 657 * this warp, the server pointer would thus appear to hit 658 * an invisible wall */ 659 if (x == 0) { 660 x += 200; 661 } 662 if (y == 0) { 663 y += 200; 664 } 665 if (x == (gdk_screen_get_width(screen) - 1)) { 666 x -= 200; 667 } 668 if (y == (gdk_screen_get_height(screen) - 1)) { 669 y -= 200; 670 } 671 672 if (x != (int)motion->x_root || y != (int)motion->y_root) { 673 #if GTK_CHECK_VERSION(3, 0, 0) 674 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion); 675 gdk_device_warp(dev, screen, x, y); 676 #else 677 GdkDisplay *display = gtk_widget_get_display(widget); 678 gdk_display_warp_pointer(display, screen, x, y); 679 #endif 680 s->last_x = -1; 681 s->last_y = -1; 682 return FALSE; 683 } 684 } 685 return TRUE; 686 } 687 688 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button, 689 void *opaque) 690 { 691 GtkDisplayState *s = opaque; 692 int dx, dy; 693 int n; 694 695 if (button->button == 1) { 696 n = 0x01; 697 } else if (button->button == 2) { 698 n = 0x04; 699 } else if (button->button == 3) { 700 n = 0x02; 701 } else { 702 n = 0x00; 703 } 704 705 if (button->type == GDK_BUTTON_PRESS) { 706 s->button_mask |= n; 707 } else if (button->type == GDK_BUTTON_RELEASE) { 708 s->button_mask &= ~n; 709 } 710 711 if (kbd_mouse_is_absolute()) { 712 dx = s->last_x * 0x7FFF / (surface_width(s->ds) - 1); 713 dy = s->last_y * 0x7FFF / (surface_height(s->ds) - 1); 714 } else { 715 dx = 0; 716 dy = 0; 717 } 718 719 kbd_mouse_event(dx, dy, 0, s->button_mask); 720 721 return TRUE; 722 } 723 724 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque) 725 { 726 GtkDisplayState *s = opaque; 727 int gdk_keycode; 728 int qemu_keycode; 729 int i; 730 731 gdk_keycode = key->hardware_keycode; 732 733 if (gdk_keycode < 9) { 734 qemu_keycode = 0; 735 } else if (gdk_keycode < 97) { 736 qemu_keycode = gdk_keycode - 8; 737 } else if (gdk_keycode < 158) { 738 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97); 739 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */ 740 qemu_keycode = 0x70; 741 } else if (gdk_keycode == 211) { /* backslash */ 742 qemu_keycode = 0x73; 743 } else { 744 qemu_keycode = 0; 745 } 746 747 DPRINTF("translated GDK keycode %d to QEMU keycode %d (%s)\n", 748 gdk_keycode, qemu_keycode, 749 (key->type == GDK_KEY_PRESS) ? "down" : "up"); 750 751 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) { 752 if (qemu_keycode == modifier_keycode[i]) { 753 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS); 754 } 755 } 756 757 if (qemu_keycode & SCANCODE_GREY) { 758 kbd_put_keycode(SCANCODE_EMUL0); 759 } 760 761 if (key->type == GDK_KEY_PRESS) { 762 kbd_put_keycode(qemu_keycode & SCANCODE_KEYCODEMASK); 763 } else if (key->type == GDK_KEY_RELEASE) { 764 kbd_put_keycode(qemu_keycode | SCANCODE_UP); 765 } else { 766 g_assert_not_reached(); 767 } 768 769 return TRUE; 770 } 771 772 /** Window Menu Actions **/ 773 774 static void gd_menu_pause(GtkMenuItem *item, void *opaque) 775 { 776 GtkDisplayState *s = opaque; 777 778 if (s->external_pause_update) { 779 return; 780 } 781 if (runstate_is_running()) { 782 qmp_stop(NULL); 783 } else { 784 qmp_cont(NULL); 785 } 786 } 787 788 static void gd_menu_reset(GtkMenuItem *item, void *opaque) 789 { 790 qmp_system_reset(NULL); 791 } 792 793 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque) 794 { 795 qmp_system_powerdown(NULL); 796 } 797 798 static void gd_menu_quit(GtkMenuItem *item, void *opaque) 799 { 800 qmp_quit(NULL); 801 } 802 803 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque) 804 { 805 GtkDisplayState *s = opaque; 806 807 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) { 808 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0); 809 } else { 810 int i; 811 812 gtk_release_modifiers(s); 813 for (i = 0; i < s->nb_vcs; i++) { 814 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) { 815 gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1); 816 break; 817 } 818 } 819 } 820 } 821 822 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque) 823 { 824 GtkDisplayState *s = opaque; 825 826 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) { 827 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE); 828 } else { 829 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE); 830 } 831 } 832 833 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque) 834 { 835 GtkDisplayState *s = opaque; 836 837 if (!s->full_screen) { 838 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE); 839 gtk_widget_set_size_request(s->menu_bar, 0, 0); 840 gtk_widget_set_size_request(s->drawing_area, -1, -1); 841 gtk_window_fullscreen(GTK_WINDOW(s->window)); 842 if (gd_on_vga(s)) { 843 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE); 844 } 845 s->full_screen = TRUE; 846 } else { 847 gtk_window_unfullscreen(GTK_WINDOW(s->window)); 848 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s); 849 gtk_widget_set_size_request(s->menu_bar, -1, -1); 850 gtk_widget_set_size_request(s->drawing_area, 851 surface_width(s->ds), 852 surface_height(s->ds)); 853 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE); 854 s->full_screen = FALSE; 855 s->scale_x = 1.0; 856 s->scale_y = 1.0; 857 } 858 859 gd_update_cursor(s, FALSE); 860 } 861 862 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque) 863 { 864 GtkDisplayState *s = opaque; 865 866 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item), 867 FALSE); 868 869 s->scale_x += .25; 870 s->scale_y += .25; 871 872 gd_update_windowsize(s); 873 } 874 875 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque) 876 { 877 GtkDisplayState *s = opaque; 878 879 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item), 880 FALSE); 881 882 s->scale_x -= .25; 883 s->scale_y -= .25; 884 885 s->scale_x = MAX(s->scale_x, .25); 886 s->scale_y = MAX(s->scale_y, .25); 887 888 gd_update_windowsize(s); 889 } 890 891 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque) 892 { 893 GtkDisplayState *s = opaque; 894 895 s->scale_x = 1.0; 896 s->scale_y = 1.0; 897 898 gd_update_windowsize(s); 899 } 900 901 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque) 902 { 903 GtkDisplayState *s = opaque; 904 905 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) { 906 s->free_scale = TRUE; 907 } else { 908 s->free_scale = FALSE; 909 s->scale_x = 1.0; 910 s->scale_y = 1.0; 911 gd_update_windowsize(s); 912 } 913 914 gd_update_full_redraw(s); 915 } 916 917 static void gd_grab_keyboard(GtkDisplayState *s) 918 { 919 #if GTK_CHECK_VERSION(3, 0, 0) 920 GdkDisplay *display = gtk_widget_get_display(s->drawing_area); 921 GdkDeviceManager *mgr = gdk_display_get_device_manager(display); 922 GList *devices = gdk_device_manager_list_devices(mgr, 923 GDK_DEVICE_TYPE_MASTER); 924 GList *tmp = devices; 925 while (tmp) { 926 GdkDevice *dev = tmp->data; 927 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) { 928 gdk_device_grab(dev, 929 gtk_widget_get_window(s->drawing_area), 930 GDK_OWNERSHIP_NONE, 931 FALSE, 932 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, 933 NULL, 934 GDK_CURRENT_TIME); 935 } 936 tmp = tmp->next; 937 } 938 g_list_free(devices); 939 #else 940 gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area), 941 FALSE, 942 GDK_CURRENT_TIME); 943 #endif 944 } 945 946 static void gd_ungrab_keyboard(GtkDisplayState *s) 947 { 948 #if GTK_CHECK_VERSION(3, 0, 0) 949 GdkDisplay *display = gtk_widget_get_display(s->drawing_area); 950 GdkDeviceManager *mgr = gdk_display_get_device_manager(display); 951 GList *devices = gdk_device_manager_list_devices(mgr, 952 GDK_DEVICE_TYPE_MASTER); 953 GList *tmp = devices; 954 while (tmp) { 955 GdkDevice *dev = tmp->data; 956 if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) { 957 gdk_device_ungrab(dev, 958 GDK_CURRENT_TIME); 959 } 960 tmp = tmp->next; 961 } 962 g_list_free(devices); 963 #else 964 gdk_keyboard_ungrab(GDK_CURRENT_TIME); 965 #endif 966 } 967 968 static void gd_grab_pointer(GtkDisplayState *s) 969 { 970 #if GTK_CHECK_VERSION(3, 0, 0) 971 GdkDisplay *display = gtk_widget_get_display(s->drawing_area); 972 GdkDeviceManager *mgr = gdk_display_get_device_manager(display); 973 GList *devices = gdk_device_manager_list_devices(mgr, 974 GDK_DEVICE_TYPE_MASTER); 975 GList *tmp = devices; 976 while (tmp) { 977 GdkDevice *dev = tmp->data; 978 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) { 979 gdk_device_grab(dev, 980 gtk_widget_get_window(s->drawing_area), 981 GDK_OWNERSHIP_NONE, 982 FALSE, /* All events to come to our 983 window directly */ 984 GDK_POINTER_MOTION_MASK | 985 GDK_BUTTON_PRESS_MASK | 986 GDK_BUTTON_RELEASE_MASK | 987 GDK_BUTTON_MOTION_MASK | 988 GDK_SCROLL_MASK, 989 s->null_cursor, 990 GDK_CURRENT_TIME); 991 } 992 tmp = tmp->next; 993 } 994 g_list_free(devices); 995 #else 996 gdk_pointer_grab(gtk_widget_get_window(s->drawing_area), 997 FALSE, /* All events to come to our window directly */ 998 GDK_POINTER_MOTION_MASK | 999 GDK_BUTTON_PRESS_MASK | 1000 GDK_BUTTON_RELEASE_MASK | 1001 GDK_BUTTON_MOTION_MASK | 1002 GDK_SCROLL_MASK, 1003 NULL, /* Allow cursor to move over entire desktop */ 1004 s->null_cursor, 1005 GDK_CURRENT_TIME); 1006 #endif 1007 } 1008 1009 static void gd_ungrab_pointer(GtkDisplayState *s) 1010 { 1011 #if GTK_CHECK_VERSION(3, 0, 0) 1012 GdkDisplay *display = gtk_widget_get_display(s->drawing_area); 1013 GdkDeviceManager *mgr = gdk_display_get_device_manager(display); 1014 GList *devices = gdk_device_manager_list_devices(mgr, 1015 GDK_DEVICE_TYPE_MASTER); 1016 GList *tmp = devices; 1017 while (tmp) { 1018 GdkDevice *dev = tmp->data; 1019 if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) { 1020 gdk_device_ungrab(dev, 1021 GDK_CURRENT_TIME); 1022 } 1023 tmp = tmp->next; 1024 } 1025 g_list_free(devices); 1026 #else 1027 gdk_pointer_ungrab(GDK_CURRENT_TIME); 1028 #endif 1029 } 1030 1031 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque) 1032 { 1033 GtkDisplayState *s = opaque; 1034 1035 if (gd_is_grab_active(s)) { 1036 gd_grab_keyboard(s); 1037 gd_grab_pointer(s); 1038 } else { 1039 gd_ungrab_keyboard(s); 1040 gd_ungrab_pointer(s); 1041 } 1042 1043 gd_update_caption(s); 1044 gd_update_cursor(s, FALSE); 1045 } 1046 1047 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2, 1048 gpointer data) 1049 { 1050 GtkDisplayState *s = data; 1051 guint last_page; 1052 gboolean on_vga; 1053 1054 if (!gtk_widget_get_realized(s->notebook)) { 1055 return; 1056 } 1057 1058 last_page = gtk_notebook_get_current_page(nb); 1059 1060 if (last_page) { 1061 gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1); 1062 } 1063 1064 on_vga = arg2 == 0; 1065 1066 if (!on_vga) { 1067 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 1068 FALSE); 1069 } else if (s->full_screen) { 1070 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 1071 TRUE); 1072 } 1073 1074 if (arg2 == 0) { 1075 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE); 1076 } else { 1077 VirtualConsole *vc = &s->vc[arg2 - 1]; 1078 VteTerminal *term = VTE_TERMINAL(vc->terminal); 1079 int width, height; 1080 1081 width = 80 * vte_terminal_get_char_width(term); 1082 height = 25 * vte_terminal_get_char_height(term); 1083 1084 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE); 1085 gtk_widget_set_size_request(vc->terminal, width, height); 1086 } 1087 1088 gtk_widget_set_sensitive(s->grab_item, on_vga); 1089 1090 gd_update_cursor(s, TRUE); 1091 } 1092 1093 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data) 1094 { 1095 GtkDisplayState *s = data; 1096 1097 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) { 1098 gd_grab_keyboard(s); 1099 } 1100 1101 return TRUE; 1102 } 1103 1104 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data) 1105 { 1106 GtkDisplayState *s = data; 1107 1108 if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) { 1109 gd_ungrab_keyboard(s); 1110 } 1111 1112 return TRUE; 1113 } 1114 1115 static gboolean gd_focus_out_event(GtkWidget *widget, 1116 GdkEventCrossing *crossing, gpointer data) 1117 { 1118 GtkDisplayState *s = data; 1119 1120 gtk_release_modifiers(s); 1121 1122 return TRUE; 1123 } 1124 1125 /** Virtual Console Callbacks **/ 1126 1127 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len) 1128 { 1129 VirtualConsole *vc = chr->opaque; 1130 1131 return write(vc->fd, buf, len); 1132 } 1133 1134 static int nb_vcs; 1135 static CharDriverState *vcs[MAX_VCS]; 1136 1137 static CharDriverState *gd_vc_handler(ChardevVC *unused) 1138 { 1139 CharDriverState *chr; 1140 1141 chr = g_malloc0(sizeof(*chr)); 1142 chr->chr_write = gd_vc_chr_write; 1143 /* defer OPENED events until our vc is fully initialized */ 1144 chr->explicit_be_open = true; 1145 1146 vcs[nb_vcs++] = chr; 1147 1148 return chr; 1149 } 1150 1151 void early_gtk_display_init(void) 1152 { 1153 register_vc_handler(gd_vc_handler); 1154 } 1155 1156 static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque) 1157 { 1158 VirtualConsole *vc = opaque; 1159 uint8_t buffer[1024]; 1160 ssize_t len; 1161 1162 len = read(vc->fd, buffer, sizeof(buffer)); 1163 if (len <= 0) { 1164 return FALSE; 1165 } 1166 1167 qemu_chr_be_write(vc->chr, buffer, len); 1168 1169 return TRUE; 1170 } 1171 1172 static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group, 1173 GtkWidget *view_menu) 1174 { 1175 const char *label; 1176 char buffer[32]; 1177 char path[32]; 1178 #if VTE_CHECK_VERSION(0, 26, 0) 1179 VtePty *pty; 1180 #endif 1181 GIOChannel *chan; 1182 GtkWidget *scrolled_window; 1183 GtkAdjustment *vadjustment; 1184 int master_fd, slave_fd; 1185 1186 snprintf(buffer, sizeof(buffer), "vc%d", index); 1187 snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index); 1188 1189 vc->chr = vcs[index]; 1190 1191 if (vc->chr->label) { 1192 label = vc->chr->label; 1193 } else { 1194 label = buffer; 1195 } 1196 1197 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label); 1198 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item)); 1199 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path); 1200 gtk_accel_map_add_entry(path, GDK_KEY_2 + index, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1201 1202 vc->terminal = vte_terminal_new(); 1203 1204 master_fd = qemu_openpty_raw(&slave_fd, NULL); 1205 g_assert(master_fd != -1); 1206 1207 #if VTE_CHECK_VERSION(0, 26, 0) 1208 pty = vte_pty_new_foreign(master_fd, NULL); 1209 vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty); 1210 #else 1211 vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd); 1212 #endif 1213 1214 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1); 1215 1216 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal)); 1217 1218 scrolled_window = gtk_scrolled_window_new(NULL, vadjustment); 1219 gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal); 1220 1221 vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25); 1222 1223 vc->fd = slave_fd; 1224 vc->chr->opaque = vc; 1225 vc->scrolled_window = scrolled_window; 1226 1227 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window), 1228 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); 1229 1230 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label)); 1231 g_signal_connect(vc->menu_item, "activate", 1232 G_CALLBACK(gd_menu_switch_vc), s); 1233 1234 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item); 1235 1236 qemu_chr_be_generic_open(vc->chr); 1237 if (vc->chr->init) { 1238 vc->chr->init(vc->chr); 1239 } 1240 1241 chan = g_io_channel_unix_new(vc->fd); 1242 g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc); 1243 1244 return group; 1245 } 1246 1247 /** Window Creation **/ 1248 1249 static void gd_connect_signals(GtkDisplayState *s) 1250 { 1251 g_signal_connect(s->show_tabs_item, "activate", 1252 G_CALLBACK(gd_menu_show_tabs), s); 1253 1254 g_signal_connect(s->window, "key-press-event", 1255 G_CALLBACK(gd_window_key_event), s); 1256 g_signal_connect(s->window, "delete-event", 1257 G_CALLBACK(gd_window_close), s); 1258 1259 #if GTK_CHECK_VERSION(3, 0, 0) 1260 g_signal_connect(s->drawing_area, "draw", 1261 G_CALLBACK(gd_draw_event), s); 1262 #else 1263 g_signal_connect(s->drawing_area, "expose-event", 1264 G_CALLBACK(gd_expose_event), s); 1265 #endif 1266 g_signal_connect(s->drawing_area, "motion-notify-event", 1267 G_CALLBACK(gd_motion_event), s); 1268 g_signal_connect(s->drawing_area, "button-press-event", 1269 G_CALLBACK(gd_button_event), s); 1270 g_signal_connect(s->drawing_area, "button-release-event", 1271 G_CALLBACK(gd_button_event), s); 1272 g_signal_connect(s->drawing_area, "key-press-event", 1273 G_CALLBACK(gd_key_event), s); 1274 g_signal_connect(s->drawing_area, "key-release-event", 1275 G_CALLBACK(gd_key_event), s); 1276 1277 g_signal_connect(s->pause_item, "activate", 1278 G_CALLBACK(gd_menu_pause), s); 1279 g_signal_connect(s->reset_item, "activate", 1280 G_CALLBACK(gd_menu_reset), s); 1281 g_signal_connect(s->powerdown_item, "activate", 1282 G_CALLBACK(gd_menu_powerdown), s); 1283 g_signal_connect(s->quit_item, "activate", 1284 G_CALLBACK(gd_menu_quit), s); 1285 g_signal_connect(s->full_screen_item, "activate", 1286 G_CALLBACK(gd_menu_full_screen), s); 1287 g_signal_connect(s->zoom_in_item, "activate", 1288 G_CALLBACK(gd_menu_zoom_in), s); 1289 g_signal_connect(s->zoom_out_item, "activate", 1290 G_CALLBACK(gd_menu_zoom_out), s); 1291 g_signal_connect(s->zoom_fixed_item, "activate", 1292 G_CALLBACK(gd_menu_zoom_fixed), s); 1293 g_signal_connect(s->zoom_fit_item, "activate", 1294 G_CALLBACK(gd_menu_zoom_fit), s); 1295 g_signal_connect(s->vga_item, "activate", 1296 G_CALLBACK(gd_menu_switch_vc), s); 1297 g_signal_connect(s->grab_item, "activate", 1298 G_CALLBACK(gd_menu_grab_input), s); 1299 g_signal_connect(s->notebook, "switch-page", 1300 G_CALLBACK(gd_change_page), s); 1301 g_signal_connect(s->drawing_area, "enter-notify-event", 1302 G_CALLBACK(gd_enter_event), s); 1303 g_signal_connect(s->drawing_area, "leave-notify-event", 1304 G_CALLBACK(gd_leave_event), s); 1305 g_signal_connect(s->drawing_area, "focus-out-event", 1306 G_CALLBACK(gd_focus_out_event), s); 1307 } 1308 1309 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group) 1310 { 1311 GtkWidget *machine_menu; 1312 GtkWidget *separator; 1313 GtkStockItem item; 1314 1315 machine_menu = gtk_menu_new(); 1316 gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group); 1317 1318 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause")); 1319 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item); 1320 1321 separator = gtk_separator_menu_item_new(); 1322 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator); 1323 1324 s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset")); 1325 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item); 1326 1327 s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down")); 1328 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item); 1329 1330 separator = gtk_separator_menu_item_new(); 1331 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator); 1332 1333 s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL); 1334 gtk_stock_lookup(GTK_STOCK_QUIT, &item); 1335 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item), 1336 "<QEMU>/Machine/Quit"); 1337 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier); 1338 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item); 1339 1340 return machine_menu; 1341 } 1342 1343 static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group) 1344 { 1345 GSList *group = NULL; 1346 GtkWidget *view_menu; 1347 GtkWidget *separator; 1348 int i; 1349 1350 view_menu = gtk_menu_new(); 1351 gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group); 1352 1353 s->full_screen_item = 1354 gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL); 1355 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item), 1356 "<QEMU>/View/Full Screen"); 1357 gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1358 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item); 1359 1360 separator = gtk_separator_menu_item_new(); 1361 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 1362 1363 s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL); 1364 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item), 1365 "<QEMU>/View/Zoom In"); 1366 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1367 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item); 1368 1369 s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL); 1370 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item), 1371 "<QEMU>/View/Zoom Out"); 1372 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1373 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item); 1374 1375 s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL); 1376 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item), 1377 "<QEMU>/View/Zoom Fixed"); 1378 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1379 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item); 1380 1381 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit")); 1382 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item); 1383 1384 separator = gtk_separator_menu_item_new(); 1385 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 1386 1387 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover")); 1388 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item); 1389 1390 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input")); 1391 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item), 1392 "<QEMU>/View/Grab Input"); 1393 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1394 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item); 1395 1396 separator = gtk_separator_menu_item_new(); 1397 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 1398 1399 s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA"); 1400 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item)); 1401 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item), 1402 "<QEMU>/View/VGA"); 1403 gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, GDK_CONTROL_MASK | GDK_MOD1_MASK); 1404 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item); 1405 1406 for (i = 0; i < nb_vcs; i++) { 1407 VirtualConsole *vc = &s->vc[i]; 1408 1409 group = gd_vc_init(s, vc, i, group, view_menu); 1410 s->nb_vcs++; 1411 } 1412 1413 separator = gtk_separator_menu_item_new(); 1414 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 1415 1416 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs")); 1417 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item); 1418 1419 return view_menu; 1420 } 1421 1422 static void gd_create_menus(GtkDisplayState *s) 1423 { 1424 GtkAccelGroup *accel_group; 1425 1426 accel_group = gtk_accel_group_new(); 1427 s->machine_menu = gd_create_menu_machine(s, accel_group); 1428 s->view_menu = gd_create_menu_view(s, accel_group); 1429 1430 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine")); 1431 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item), 1432 s->machine_menu); 1433 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item); 1434 1435 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View")); 1436 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu); 1437 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item); 1438 1439 g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group); 1440 gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group); 1441 s->accel_group = accel_group; 1442 } 1443 1444 static const DisplayChangeListenerOps dcl_ops = { 1445 .dpy_name = "gtk", 1446 .dpy_gfx_update = gd_update, 1447 .dpy_gfx_switch = gd_switch, 1448 .dpy_refresh = gd_refresh, 1449 .dpy_mouse_set = gd_mouse_set, 1450 .dpy_cursor_define = gd_cursor_define, 1451 }; 1452 1453 void gtk_display_init(DisplayState *ds, bool full_screen) 1454 { 1455 GtkDisplayState *s = g_malloc0(sizeof(*s)); 1456 char *filename; 1457 1458 gtk_init(NULL, NULL); 1459 1460 s->dcl.ops = &dcl_ops; 1461 s->dcl.con = qemu_console_lookup_by_index(0); 1462 1463 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 1464 #if GTK_CHECK_VERSION(3, 2, 0) 1465 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); 1466 #else 1467 s->vbox = gtk_vbox_new(FALSE, 0); 1468 #endif 1469 s->notebook = gtk_notebook_new(); 1470 s->drawing_area = gtk_drawing_area_new(); 1471 s->menu_bar = gtk_menu_bar_new(); 1472 1473 s->scale_x = 1.0; 1474 s->scale_y = 1.0; 1475 s->free_scale = FALSE; 1476 1477 setlocale(LC_ALL, ""); 1478 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR); 1479 textdomain("qemu"); 1480 1481 s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR); 1482 1483 s->mouse_mode_notifier.notify = gd_mouse_mode_change; 1484 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier); 1485 qemu_add_vm_change_state_handler(gd_change_runstate, s); 1486 1487 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA")); 1488 1489 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg"); 1490 if (filename) { 1491 GError *error = NULL; 1492 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error); 1493 if (pixbuf) { 1494 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf); 1495 } else { 1496 g_error_free(error); 1497 } 1498 g_free(filename); 1499 } 1500 1501 gd_create_menus(s); 1502 1503 gd_connect_signals(s); 1504 1505 gtk_widget_add_events(s->drawing_area, 1506 GDK_POINTER_MOTION_MASK | 1507 GDK_BUTTON_PRESS_MASK | 1508 GDK_BUTTON_RELEASE_MASK | 1509 GDK_BUTTON_MOTION_MASK | 1510 GDK_ENTER_NOTIFY_MASK | 1511 GDK_LEAVE_NOTIFY_MASK | 1512 GDK_SCROLL_MASK | 1513 GDK_KEY_PRESS_MASK); 1514 gtk_widget_set_double_buffered(s->drawing_area, FALSE); 1515 gtk_widget_set_can_focus(s->drawing_area, TRUE); 1516 1517 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE); 1518 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE); 1519 1520 gd_update_caption(s); 1521 1522 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0); 1523 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0); 1524 1525 gtk_container_add(GTK_CONTAINER(s->window), s->vbox); 1526 1527 gtk_widget_show_all(s->window); 1528 1529 if (full_screen) { 1530 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item)); 1531 } 1532 1533 register_displaychangelistener(&s->dcl); 1534 1535 global_state = s; 1536 } 1537