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