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/osdep.h" 38 #include "qemu-common.h" 39 #include "qemu/cutils.h" 40 41 #include "ui/console.h" 42 #include "ui/gtk.h" 43 44 #include <glib/gi18n.h> 45 #include <locale.h> 46 #if defined(CONFIG_VTE) 47 #include <vte/vte.h> 48 #endif 49 #include <math.h> 50 51 #include "trace.h" 52 #include "ui/input.h" 53 #include "sysemu/sysemu.h" 54 #include "qmp-commands.h" 55 #include "x_keymap.h" 56 #include "keymaps.h" 57 #include "sysemu/char.h" 58 #include "qom/object.h" 59 60 #define MAX_VCS 10 61 #define VC_WINDOW_X_MIN 320 62 #define VC_WINDOW_Y_MIN 240 63 #define VC_TERM_X_MIN 80 64 #define VC_TERM_Y_MIN 25 65 #define VC_SCALE_MIN 0.25 66 #define VC_SCALE_STEP 0.25 67 68 #if !defined(CONFIG_VTE) 69 # define VTE_CHECK_VERSION(a, b, c) 0 70 #endif 71 72 #if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0) 73 /* 74 * The gtk2 vte terminal widget seriously messes up the window resize 75 * for some reason. You basically can't make the qemu window smaller 76 * any more because the toplevel window geoemtry hints are overridden. 77 * 78 * Workaround that by hiding all vte widgets, except the one in the 79 * current tab. 80 * 81 * Luckily everything works smooth in gtk3. 82 */ 83 # define VTE_RESIZE_HACK 1 84 #endif 85 86 #if !GTK_CHECK_VERSION(2, 20, 0) 87 #define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget) 88 #endif 89 90 #ifndef GDK_IS_X11_DISPLAY 91 #define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy) 92 #endif 93 #ifndef GDK_IS_WAYLAND_DISPLAY 94 #define GDK_IS_WAYLAND_DISPLAY(dpy) (dpy == dpy) 95 #endif 96 #ifndef GDK_IS_WIN32_DISPLAY 97 #define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy) 98 #endif 99 100 #if !GTK_CHECK_VERSION(2, 22, 0) 101 #define GDK_KEY_0 GDK_0 102 #define GDK_KEY_1 GDK_1 103 #define GDK_KEY_2 GDK_2 104 #define GDK_KEY_f GDK_f 105 #define GDK_KEY_g GDK_g 106 #define GDK_KEY_q GDK_q 107 #define GDK_KEY_plus GDK_plus 108 #define GDK_KEY_equal GDK_equal 109 #define GDK_KEY_minus GDK_minus 110 #define GDK_KEY_Pause GDK_Pause 111 #define GDK_KEY_Delete GDK_Delete 112 #endif 113 114 /* Some older mingw versions lack this constant or have 115 * it conditionally defined */ 116 #ifdef _WIN32 117 # ifndef MAPVK_VK_TO_VSC 118 # define MAPVK_VK_TO_VSC 0 119 # endif 120 #endif 121 122 123 #define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK) 124 125 static const int modifier_keycode[] = { 126 /* shift, control, alt keys, meta keys, both left & right */ 127 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd, 128 }; 129 130 struct GtkDisplayState { 131 GtkWidget *window; 132 133 GtkWidget *menu_bar; 134 135 GtkAccelGroup *accel_group; 136 137 GtkWidget *machine_menu_item; 138 GtkWidget *machine_menu; 139 GtkWidget *pause_item; 140 GtkWidget *reset_item; 141 GtkWidget *powerdown_item; 142 GtkWidget *quit_item; 143 144 GtkWidget *view_menu_item; 145 GtkWidget *view_menu; 146 GtkWidget *full_screen_item; 147 GtkWidget *copy_item; 148 GtkWidget *zoom_in_item; 149 GtkWidget *zoom_out_item; 150 GtkWidget *zoom_fixed_item; 151 GtkWidget *zoom_fit_item; 152 GtkWidget *grab_item; 153 GtkWidget *grab_on_hover_item; 154 155 int nb_vcs; 156 VirtualConsole vc[MAX_VCS]; 157 158 GtkWidget *show_tabs_item; 159 GtkWidget *untabify_item; 160 161 GtkWidget *vbox; 162 GtkWidget *notebook; 163 int button_mask; 164 gboolean last_set; 165 int last_x; 166 int last_y; 167 int grab_x_root; 168 int grab_y_root; 169 VirtualConsole *kbd_owner; 170 VirtualConsole *ptr_owner; 171 172 gboolean full_screen; 173 174 GdkCursor *null_cursor; 175 Notifier mouse_mode_notifier; 176 gboolean free_scale; 177 178 bool external_pause_update; 179 180 bool modifier_pressed[ARRAY_SIZE(modifier_keycode)]; 181 bool has_evdev; 182 bool ignore_keys; 183 }; 184 185 typedef struct VCChardev { 186 Chardev parent; 187 VirtualConsole *console; 188 bool echo; 189 } VCChardev; 190 191 #define TYPE_CHARDEV_VC "chardev-vc" 192 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC) 193 194 static void gd_grab_pointer(VirtualConsole *vc, const char *reason); 195 static void gd_ungrab_pointer(GtkDisplayState *s); 196 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason); 197 static void gd_ungrab_keyboard(GtkDisplayState *s); 198 199 /** Utility Functions **/ 200 201 static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s) 202 { 203 VirtualConsole *vc; 204 gint i; 205 206 for (i = 0; i < s->nb_vcs; i++) { 207 vc = &s->vc[i]; 208 if (gtk_check_menu_item_get_active 209 (GTK_CHECK_MENU_ITEM(vc->menu_item))) { 210 return vc; 211 } 212 } 213 return NULL; 214 } 215 216 static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page) 217 { 218 VirtualConsole *vc; 219 gint i, p; 220 221 for (i = 0; i < s->nb_vcs; i++) { 222 vc = &s->vc[i]; 223 p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item); 224 if (p == page) { 225 return vc; 226 } 227 } 228 return NULL; 229 } 230 231 static VirtualConsole *gd_vc_find_current(GtkDisplayState *s) 232 { 233 gint page; 234 235 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)); 236 return gd_vc_find_by_page(s, page); 237 } 238 239 static bool gd_is_grab_active(GtkDisplayState *s) 240 { 241 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item)); 242 } 243 244 static bool gd_grab_on_hover(GtkDisplayState *s) 245 { 246 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item)); 247 } 248 249 static void gd_update_cursor(VirtualConsole *vc) 250 { 251 GtkDisplayState *s = vc->s; 252 GdkWindow *window; 253 254 if (vc->type != GD_VC_GFX || 255 !qemu_console_is_graphic(vc->gfx.dcl.con)) { 256 return; 257 } 258 259 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) { 260 return; 261 } 262 263 window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area)); 264 if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) { 265 gdk_window_set_cursor(window, s->null_cursor); 266 } else { 267 gdk_window_set_cursor(window, NULL); 268 } 269 } 270 271 static void gd_update_caption(GtkDisplayState *s) 272 { 273 const char *status = ""; 274 gchar *prefix; 275 gchar *title; 276 const char *grab = ""; 277 bool is_paused = !runstate_is_running(); 278 int i; 279 280 if (qemu_name) { 281 prefix = g_strdup_printf("QEMU (%s)", qemu_name); 282 } else { 283 prefix = g_strdup_printf("QEMU"); 284 } 285 286 if (s->ptr_owner != NULL && 287 s->ptr_owner->window == NULL) { 288 grab = _(" - Press Ctrl+Alt+G to release grab"); 289 } 290 291 if (is_paused) { 292 status = _(" [Paused]"); 293 } 294 s->external_pause_update = true; 295 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item), 296 is_paused); 297 s->external_pause_update = false; 298 299 title = g_strdup_printf("%s%s%s", prefix, status, grab); 300 gtk_window_set_title(GTK_WINDOW(s->window), title); 301 g_free(title); 302 303 for (i = 0; i < s->nb_vcs; i++) { 304 VirtualConsole *vc = &s->vc[i]; 305 306 if (!vc->window) { 307 continue; 308 } 309 title = g_strdup_printf("%s: %s%s%s", prefix, vc->label, 310 vc == s->kbd_owner ? " +kbd" : "", 311 vc == s->ptr_owner ? " +ptr" : ""); 312 gtk_window_set_title(GTK_WINDOW(vc->window), title); 313 g_free(title); 314 } 315 316 g_free(prefix); 317 } 318 319 static void gd_update_geometry_hints(VirtualConsole *vc) 320 { 321 GtkDisplayState *s = vc->s; 322 GdkWindowHints mask = 0; 323 GdkGeometry geo = {}; 324 GtkWidget *geo_widget = NULL; 325 GtkWindow *geo_window; 326 327 if (vc->type == GD_VC_GFX) { 328 if (!vc->gfx.ds) { 329 return; 330 } 331 if (s->free_scale) { 332 geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN; 333 geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN; 334 mask |= GDK_HINT_MIN_SIZE; 335 } else { 336 geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x; 337 geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y; 338 mask |= GDK_HINT_MIN_SIZE; 339 } 340 geo_widget = vc->gfx.drawing_area; 341 gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height); 342 343 #if defined(CONFIG_VTE) 344 } else if (vc->type == GD_VC_VTE) { 345 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal); 346 GtkBorder padding = { 0 }; 347 348 #if VTE_CHECK_VERSION(0, 37, 0) 349 gtk_style_context_get_padding( 350 gtk_widget_get_style_context(vc->vte.terminal), 351 gtk_widget_get_state_flags(vc->vte.terminal), 352 &padding); 353 #else 354 { 355 GtkBorder *ib = NULL; 356 gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL); 357 if (ib) { 358 padding = *ib; 359 gtk_border_free(ib); 360 } 361 } 362 #endif 363 364 geo.width_inc = vte_terminal_get_char_width(term); 365 geo.height_inc = vte_terminal_get_char_height(term); 366 mask |= GDK_HINT_RESIZE_INC; 367 geo.base_width = geo.width_inc; 368 geo.base_height = geo.height_inc; 369 mask |= GDK_HINT_BASE_SIZE; 370 geo.min_width = geo.width_inc * VC_TERM_X_MIN; 371 geo.min_height = geo.height_inc * VC_TERM_Y_MIN; 372 mask |= GDK_HINT_MIN_SIZE; 373 374 geo.base_width += padding.left + padding.right; 375 geo.base_height += padding.top + padding.bottom; 376 geo.min_width += padding.left + padding.right; 377 geo.min_height += padding.top + padding.bottom; 378 geo_widget = vc->vte.terminal; 379 #endif 380 } 381 382 geo_window = GTK_WINDOW(vc->window ? vc->window : s->window); 383 gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask); 384 } 385 386 void gd_update_windowsize(VirtualConsole *vc) 387 { 388 GtkDisplayState *s = vc->s; 389 390 gd_update_geometry_hints(vc); 391 392 if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) { 393 gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window), 394 VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN); 395 } 396 } 397 398 static void gd_update_full_redraw(VirtualConsole *vc) 399 { 400 GtkWidget *area = vc->gfx.drawing_area; 401 int ww, wh; 402 gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh); 403 #if defined(CONFIG_GTK_GL) 404 if (vc->gfx.gls) { 405 gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area)); 406 return; 407 } 408 #endif 409 gtk_widget_queue_draw_area(area, 0, 0, ww, wh); 410 } 411 412 static void gtk_release_modifiers(GtkDisplayState *s) 413 { 414 VirtualConsole *vc = gd_vc_find_current(s); 415 int i, keycode; 416 417 if (vc->type != GD_VC_GFX || 418 !qemu_console_is_graphic(vc->gfx.dcl.con)) { 419 return; 420 } 421 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) { 422 keycode = modifier_keycode[i]; 423 if (!s->modifier_pressed[i]) { 424 continue; 425 } 426 qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false); 427 s->modifier_pressed[i] = false; 428 } 429 } 430 431 static void gd_widget_reparent(GtkWidget *from, GtkWidget *to, 432 GtkWidget *widget) 433 { 434 g_object_ref(G_OBJECT(widget)); 435 gtk_container_remove(GTK_CONTAINER(from), widget); 436 gtk_container_add(GTK_CONTAINER(to), widget); 437 g_object_unref(G_OBJECT(widget)); 438 } 439 440 /** DisplayState Callbacks **/ 441 442 static void gd_update(DisplayChangeListener *dcl, 443 int x, int y, int w, int h) 444 { 445 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 446 GdkWindow *win; 447 int x1, x2, y1, y2; 448 int mx, my; 449 int fbw, fbh; 450 int ww, wh; 451 452 trace_gd_update(vc->label, x, y, w, h); 453 454 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) { 455 return; 456 } 457 458 if (vc->gfx.convert) { 459 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image, 460 NULL, vc->gfx.convert, 461 x, y, 0, 0, x, y, w, h); 462 } 463 464 x1 = floor(x * vc->gfx.scale_x); 465 y1 = floor(y * vc->gfx.scale_y); 466 467 x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x); 468 y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y); 469 470 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x; 471 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y; 472 473 win = gtk_widget_get_window(vc->gfx.drawing_area); 474 if (!win) { 475 return; 476 } 477 gdk_drawable_get_size(win, &ww, &wh); 478 479 mx = my = 0; 480 if (ww > fbw) { 481 mx = (ww - fbw) / 2; 482 } 483 if (wh > fbh) { 484 my = (wh - fbh) / 2; 485 } 486 487 gtk_widget_queue_draw_area(vc->gfx.drawing_area, 488 mx + x1, my + y1, (x2 - x1), (y2 - y1)); 489 } 490 491 static void gd_refresh(DisplayChangeListener *dcl) 492 { 493 graphic_hw_update(dcl->con); 494 } 495 496 #if GTK_CHECK_VERSION(3, 0, 0) 497 static GdkDevice *gd_get_pointer(GdkDisplay *dpy) 498 { 499 #if GTK_CHECK_VERSION(3, 20, 0) 500 return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy)); 501 #else 502 return gdk_device_manager_get_client_pointer( 503 gdk_display_get_device_manager(dpy)); 504 #endif 505 } 506 507 static void gd_mouse_set(DisplayChangeListener *dcl, 508 int x, int y, int visible) 509 { 510 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 511 GdkDisplay *dpy; 512 gint x_root, y_root; 513 514 if (qemu_input_is_absolute()) { 515 return; 516 } 517 518 dpy = gtk_widget_get_display(vc->gfx.drawing_area); 519 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area), 520 x, y, &x_root, &y_root); 521 gdk_device_warp(gd_get_pointer(dpy), 522 gtk_widget_get_screen(vc->gfx.drawing_area), 523 x_root, y_root); 524 vc->s->last_x = x; 525 vc->s->last_y = y; 526 } 527 #else 528 static void gd_mouse_set(DisplayChangeListener *dcl, 529 int x, int y, int visible) 530 { 531 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 532 gint x_root, y_root; 533 534 if (qemu_input_is_absolute()) { 535 return; 536 } 537 538 gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area), 539 x, y, &x_root, &y_root); 540 gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area), 541 gtk_widget_get_screen(vc->gfx.drawing_area), 542 x_root, y_root); 543 } 544 #endif 545 546 static void gd_cursor_define(DisplayChangeListener *dcl, 547 QEMUCursor *c) 548 { 549 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 550 GdkPixbuf *pixbuf; 551 GdkCursor *cursor; 552 553 if (!gtk_widget_get_realized(vc->gfx.drawing_area)) { 554 return; 555 } 556 557 pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data), 558 GDK_COLORSPACE_RGB, true, 8, 559 c->width, c->height, c->width * 4, 560 NULL, NULL); 561 cursor = gdk_cursor_new_from_pixbuf 562 (gtk_widget_get_display(vc->gfx.drawing_area), 563 pixbuf, c->hot_x, c->hot_y); 564 gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor); 565 g_object_unref(pixbuf); 566 #if !GTK_CHECK_VERSION(3, 0, 0) 567 gdk_cursor_unref(cursor); 568 #else 569 g_object_unref(cursor); 570 #endif 571 } 572 573 static void gd_switch(DisplayChangeListener *dcl, 574 DisplaySurface *surface) 575 { 576 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 577 bool resized = true; 578 579 trace_gd_switch(vc->label, 580 surface ? surface_width(surface) : 0, 581 surface ? surface_height(surface) : 0); 582 583 if (vc->gfx.surface) { 584 cairo_surface_destroy(vc->gfx.surface); 585 vc->gfx.surface = NULL; 586 } 587 if (vc->gfx.convert) { 588 pixman_image_unref(vc->gfx.convert); 589 vc->gfx.convert = NULL; 590 } 591 592 if (vc->gfx.ds && surface && 593 surface_width(vc->gfx.ds) == surface_width(surface) && 594 surface_height(vc->gfx.ds) == surface_height(surface)) { 595 resized = false; 596 } 597 vc->gfx.ds = surface; 598 599 if (!surface) { 600 return; 601 } 602 603 if (surface->format == PIXMAN_x8r8g8b8) { 604 /* 605 * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24 606 * 607 * No need to convert, use surface directly. Should be the 608 * common case as this is qemu_default_pixelformat(32) too. 609 */ 610 vc->gfx.surface = cairo_image_surface_create_for_data 611 (surface_data(surface), 612 CAIRO_FORMAT_RGB24, 613 surface_width(surface), 614 surface_height(surface), 615 surface_stride(surface)); 616 } else { 617 /* Must convert surface, use pixman to do it. */ 618 vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8, 619 surface_width(surface), 620 surface_height(surface), 621 NULL, 0); 622 vc->gfx.surface = cairo_image_surface_create_for_data 623 ((void *)pixman_image_get_data(vc->gfx.convert), 624 CAIRO_FORMAT_RGB24, 625 pixman_image_get_width(vc->gfx.convert), 626 pixman_image_get_height(vc->gfx.convert), 627 pixman_image_get_stride(vc->gfx.convert)); 628 pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image, 629 NULL, vc->gfx.convert, 630 0, 0, 0, 0, 0, 0, 631 pixman_image_get_width(vc->gfx.convert), 632 pixman_image_get_height(vc->gfx.convert)); 633 } 634 635 if (resized) { 636 gd_update_windowsize(vc); 637 } else { 638 gd_update_full_redraw(vc); 639 } 640 } 641 642 static const DisplayChangeListenerOps dcl_ops = { 643 .dpy_name = "gtk", 644 .dpy_gfx_update = gd_update, 645 .dpy_gfx_switch = gd_switch, 646 .dpy_gfx_check_format = qemu_pixman_check_format, 647 .dpy_refresh = gd_refresh, 648 .dpy_mouse_set = gd_mouse_set, 649 .dpy_cursor_define = gd_cursor_define, 650 }; 651 652 653 #if defined(CONFIG_OPENGL) 654 655 /** DisplayState Callbacks (opengl version) **/ 656 657 #if defined(CONFIG_GTK_GL) 658 659 static const DisplayChangeListenerOps dcl_gl_area_ops = { 660 .dpy_name = "gtk-egl", 661 .dpy_gfx_update = gd_gl_area_update, 662 .dpy_gfx_switch = gd_gl_area_switch, 663 .dpy_gfx_check_format = console_gl_check_format, 664 .dpy_refresh = gd_gl_area_refresh, 665 .dpy_mouse_set = gd_mouse_set, 666 .dpy_cursor_define = gd_cursor_define, 667 668 .dpy_gl_ctx_create = gd_gl_area_create_context, 669 .dpy_gl_ctx_destroy = gd_gl_area_destroy_context, 670 .dpy_gl_ctx_make_current = gd_gl_area_make_current, 671 .dpy_gl_ctx_get_current = gd_gl_area_get_current_context, 672 .dpy_gl_scanout = gd_gl_area_scanout, 673 .dpy_gl_update = gd_gl_area_scanout_flush, 674 }; 675 676 #else 677 678 static const DisplayChangeListenerOps dcl_egl_ops = { 679 .dpy_name = "gtk-egl", 680 .dpy_gfx_update = gd_egl_update, 681 .dpy_gfx_switch = gd_egl_switch, 682 .dpy_gfx_check_format = console_gl_check_format, 683 .dpy_refresh = gd_egl_refresh, 684 .dpy_mouse_set = gd_mouse_set, 685 .dpy_cursor_define = gd_cursor_define, 686 687 .dpy_gl_ctx_create = gd_egl_create_context, 688 .dpy_gl_ctx_destroy = qemu_egl_destroy_context, 689 .dpy_gl_ctx_make_current = gd_egl_make_current, 690 .dpy_gl_ctx_get_current = qemu_egl_get_current_context, 691 .dpy_gl_scanout = gd_egl_scanout, 692 .dpy_gl_update = gd_egl_scanout_flush, 693 }; 694 695 #endif /* CONFIG_GTK_GL */ 696 #endif /* CONFIG_OPENGL */ 697 698 /** QEMU Events **/ 699 700 static void gd_change_runstate(void *opaque, int running, RunState state) 701 { 702 GtkDisplayState *s = opaque; 703 704 gd_update_caption(s); 705 } 706 707 static void gd_mouse_mode_change(Notifier *notify, void *data) 708 { 709 GtkDisplayState *s; 710 int i; 711 712 s = container_of(notify, GtkDisplayState, mouse_mode_notifier); 713 /* release the grab at switching to absolute mode */ 714 if (qemu_input_is_absolute() && gd_is_grab_active(s)) { 715 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 716 FALSE); 717 } 718 for (i = 0; i < s->nb_vcs; i++) { 719 VirtualConsole *vc = &s->vc[i]; 720 gd_update_cursor(vc); 721 } 722 } 723 724 /** GTK Events **/ 725 726 static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event, 727 void *opaque) 728 { 729 GtkDisplayState *s = opaque; 730 int i; 731 732 if (!no_quit) { 733 for (i = 0; i < s->nb_vcs; i++) { 734 if (s->vc[i].type != GD_VC_GFX) { 735 continue; 736 } 737 unregister_displaychangelistener(&s->vc[i].gfx.dcl); 738 } 739 qmp_quit(NULL); 740 return FALSE; 741 } 742 743 return TRUE; 744 } 745 746 static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height) 747 { 748 QemuUIInfo info; 749 750 memset(&info, 0, sizeof(info)); 751 info.width = width; 752 info.height = height; 753 dpy_set_ui_info(vc->gfx.dcl.con, &info); 754 } 755 756 #if defined(CONFIG_GTK_GL) 757 758 static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context, 759 void *opaque) 760 { 761 VirtualConsole *vc = opaque; 762 763 if (vc->gfx.gls) { 764 gd_gl_area_draw(vc); 765 } 766 return TRUE; 767 } 768 769 static void gd_resize_event(GtkGLArea *area, 770 gint width, gint height, gpointer *opaque) 771 { 772 VirtualConsole *vc = (void *)opaque; 773 774 gd_set_ui_info(vc, width, height); 775 } 776 777 #endif 778 779 static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque) 780 { 781 VirtualConsole *vc = opaque; 782 GtkDisplayState *s = vc->s; 783 int mx, my; 784 int ww, wh; 785 int fbw, fbh; 786 787 #if defined(CONFIG_OPENGL) 788 if (vc->gfx.gls) { 789 #if defined(CONFIG_GTK_GL) 790 /* invoke render callback please */ 791 return FALSE; 792 #else 793 gd_egl_draw(vc); 794 return TRUE; 795 #endif 796 } 797 #endif 798 799 if (!gtk_widget_get_realized(widget)) { 800 return FALSE; 801 } 802 if (!vc->gfx.ds) { 803 return FALSE; 804 } 805 806 fbw = surface_width(vc->gfx.ds); 807 fbh = surface_height(vc->gfx.ds); 808 809 gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh); 810 811 if (s->full_screen) { 812 vc->gfx.scale_x = (double)ww / fbw; 813 vc->gfx.scale_y = (double)wh / fbh; 814 } else if (s->free_scale) { 815 double sx, sy; 816 817 sx = (double)ww / fbw; 818 sy = (double)wh / fbh; 819 820 vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy); 821 } 822 823 fbw *= vc->gfx.scale_x; 824 fbh *= vc->gfx.scale_y; 825 826 mx = my = 0; 827 if (ww > fbw) { 828 mx = (ww - fbw) / 2; 829 } 830 if (wh > fbh) { 831 my = (wh - fbh) / 2; 832 } 833 834 cairo_rectangle(cr, 0, 0, ww, wh); 835 836 /* Optionally cut out the inner area where the pixmap 837 will be drawn. This avoids 'flashing' since we're 838 not double-buffering. Note we're using the undocumented 839 behaviour of drawing the rectangle from right to left 840 to cut out the whole */ 841 cairo_rectangle(cr, mx + fbw, my, 842 -1 * fbw, fbh); 843 cairo_fill(cr); 844 845 cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y); 846 cairo_set_source_surface(cr, vc->gfx.surface, 847 mx / vc->gfx.scale_x, my / vc->gfx.scale_y); 848 cairo_paint(cr); 849 850 return TRUE; 851 } 852 853 #if !GTK_CHECK_VERSION(3, 0, 0) 854 static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose, 855 void *opaque) 856 { 857 cairo_t *cr; 858 gboolean ret; 859 860 cr = gdk_cairo_create(gtk_widget_get_window(widget)); 861 cairo_rectangle(cr, 862 expose->area.x, 863 expose->area.y, 864 expose->area.width, 865 expose->area.height); 866 cairo_clip(cr); 867 868 ret = gd_draw_event(widget, cr, opaque); 869 870 cairo_destroy(cr); 871 872 return ret; 873 } 874 #endif 875 876 static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion, 877 void *opaque) 878 { 879 VirtualConsole *vc = opaque; 880 GtkDisplayState *s = vc->s; 881 int x, y; 882 int mx, my; 883 int fbh, fbw; 884 int ww, wh; 885 886 if (!vc->gfx.ds) { 887 return TRUE; 888 } 889 890 fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x; 891 fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y; 892 893 gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area), 894 &ww, &wh); 895 896 mx = my = 0; 897 if (ww > fbw) { 898 mx = (ww - fbw) / 2; 899 } 900 if (wh > fbh) { 901 my = (wh - fbh) / 2; 902 } 903 904 x = (motion->x - mx) / vc->gfx.scale_x; 905 y = (motion->y - my) / vc->gfx.scale_y; 906 907 if (qemu_input_is_absolute()) { 908 if (x < 0 || y < 0 || 909 x >= surface_width(vc->gfx.ds) || 910 y >= surface_height(vc->gfx.ds)) { 911 return TRUE; 912 } 913 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x, 914 surface_width(vc->gfx.ds)); 915 qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y, 916 surface_height(vc->gfx.ds)); 917 qemu_input_event_sync(); 918 } else if (s->last_set && s->ptr_owner == vc) { 919 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x); 920 qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y); 921 qemu_input_event_sync(); 922 } 923 s->last_x = x; 924 s->last_y = y; 925 s->last_set = TRUE; 926 927 if (!qemu_input_is_absolute() && s->ptr_owner == vc) { 928 GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area); 929 int screen_width, screen_height; 930 931 int x = (int)motion->x_root; 932 int y = (int)motion->y_root; 933 934 #if GTK_CHECK_VERSION(3, 22, 0) 935 { 936 GdkDisplay *dpy = gtk_widget_get_display(widget); 937 GdkWindow *win = gtk_widget_get_window(widget); 938 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win); 939 GdkRectangle geometry; 940 gdk_monitor_get_geometry(monitor, &geometry); 941 screen_width = geometry.width; 942 screen_height = geometry.height; 943 } 944 #else 945 { 946 screen_width = gdk_screen_get_width(screen); 947 screen_height = gdk_screen_get_height(screen); 948 } 949 #endif 950 951 /* In relative mode check to see if client pointer hit 952 * one of the screen edges, and if so move it back by 953 * 200 pixels. This is important because the pointer 954 * in the server doesn't correspond 1-for-1, and so 955 * may still be only half way across the screen. Without 956 * this warp, the server pointer would thus appear to hit 957 * an invisible wall */ 958 if (x == 0) { 959 x += 200; 960 } 961 if (y == 0) { 962 y += 200; 963 } 964 if (x == (screen_width - 1)) { 965 x -= 200; 966 } 967 if (y == (screen_height - 1)) { 968 y -= 200; 969 } 970 971 if (x != (int)motion->x_root || y != (int)motion->y_root) { 972 #if GTK_CHECK_VERSION(3, 0, 0) 973 GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion); 974 gdk_device_warp(dev, screen, x, y); 975 #else 976 GdkDisplay *display = gtk_widget_get_display(widget); 977 gdk_display_warp_pointer(display, screen, x, y); 978 #endif 979 s->last_set = FALSE; 980 return FALSE; 981 } 982 } 983 return TRUE; 984 } 985 986 static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button, 987 void *opaque) 988 { 989 VirtualConsole *vc = opaque; 990 GtkDisplayState *s = vc->s; 991 InputButton btn; 992 993 /* implicitly grab the input at the first click in the relative mode */ 994 if (button->button == 1 && button->type == GDK_BUTTON_PRESS && 995 !qemu_input_is_absolute() && s->ptr_owner != vc) { 996 if (!vc->window) { 997 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 998 TRUE); 999 } else { 1000 gd_grab_pointer(vc, "relative-mode-click"); 1001 } 1002 return TRUE; 1003 } 1004 1005 if (button->button == 1) { 1006 btn = INPUT_BUTTON_LEFT; 1007 } else if (button->button == 2) { 1008 btn = INPUT_BUTTON_MIDDLE; 1009 } else if (button->button == 3) { 1010 btn = INPUT_BUTTON_RIGHT; 1011 } else if (button->button == 8) { 1012 btn = INPUT_BUTTON_SIDE; 1013 } else if (button->button == 9) { 1014 btn = INPUT_BUTTON_EXTRA; 1015 } else { 1016 return TRUE; 1017 } 1018 1019 qemu_input_queue_btn(vc->gfx.dcl.con, btn, 1020 button->type == GDK_BUTTON_PRESS); 1021 qemu_input_event_sync(); 1022 return TRUE; 1023 } 1024 1025 static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll, 1026 void *opaque) 1027 { 1028 VirtualConsole *vc = opaque; 1029 InputButton btn; 1030 1031 if (scroll->direction == GDK_SCROLL_UP) { 1032 btn = INPUT_BUTTON_WHEEL_UP; 1033 } else if (scroll->direction == GDK_SCROLL_DOWN) { 1034 btn = INPUT_BUTTON_WHEEL_DOWN; 1035 #if GTK_CHECK_VERSION(3, 4, 0) 1036 } else if (scroll->direction == GDK_SCROLL_SMOOTH) { 1037 gdouble delta_x, delta_y; 1038 if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll, 1039 &delta_x, &delta_y)) { 1040 return TRUE; 1041 } 1042 if (delta_y > 0) { 1043 btn = INPUT_BUTTON_WHEEL_DOWN; 1044 } else { 1045 btn = INPUT_BUTTON_WHEEL_UP; 1046 } 1047 #endif 1048 } else { 1049 return TRUE; 1050 } 1051 1052 qemu_input_queue_btn(vc->gfx.dcl.con, btn, true); 1053 qemu_input_event_sync(); 1054 qemu_input_queue_btn(vc->gfx.dcl.con, btn, false); 1055 qemu_input_event_sync(); 1056 return TRUE; 1057 } 1058 1059 static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode) 1060 { 1061 int qemu_keycode; 1062 1063 #ifdef GDK_WINDOWING_WIN32 1064 if (GDK_IS_WIN32_DISPLAY(dpy)) { 1065 qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC); 1066 switch (qemu_keycode) { 1067 case 103: /* alt gr */ 1068 qemu_keycode = 56 | SCANCODE_GREY; 1069 break; 1070 } 1071 return qemu_keycode; 1072 } 1073 #endif 1074 1075 if (gdk_keycode < 9) { 1076 qemu_keycode = 0; 1077 } else if (gdk_keycode < 97) { 1078 qemu_keycode = gdk_keycode - 8; 1079 #ifdef GDK_WINDOWING_X11 1080 } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) { 1081 if (s->has_evdev) { 1082 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97); 1083 } else { 1084 qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97); 1085 } 1086 #endif 1087 #ifdef GDK_WINDOWING_WAYLAND 1088 } else if (GDK_IS_WAYLAND_DISPLAY(dpy) && gdk_keycode < 158) { 1089 qemu_keycode = translate_evdev_keycode(gdk_keycode - 97); 1090 #endif 1091 } else if (gdk_keycode == 208) { /* Hiragana_Katakana */ 1092 qemu_keycode = 0x70; 1093 } else if (gdk_keycode == 211) { /* backslash */ 1094 qemu_keycode = 0x73; 1095 } else { 1096 qemu_keycode = 0; 1097 } 1098 1099 return qemu_keycode; 1100 } 1101 1102 static gboolean gd_text_key_down(GtkWidget *widget, 1103 GdkEventKey *key, void *opaque) 1104 { 1105 VirtualConsole *vc = opaque; 1106 QemuConsole *con = vc->gfx.dcl.con; 1107 1108 if (key->keyval == GDK_KEY_Delete) { 1109 kbd_put_qcode_console(con, Q_KEY_CODE_DELETE); 1110 } else if (key->length) { 1111 kbd_put_string_console(con, key->string, key->length); 1112 } else { 1113 int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget), 1114 key->hardware_keycode); 1115 int qcode = qemu_input_key_number_to_qcode(num); 1116 kbd_put_qcode_console(con, qcode); 1117 } 1118 return TRUE; 1119 } 1120 1121 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque) 1122 { 1123 VirtualConsole *vc = opaque; 1124 GtkDisplayState *s = vc->s; 1125 int gdk_keycode = key->hardware_keycode; 1126 int qemu_keycode; 1127 int i; 1128 1129 if (s->ignore_keys) { 1130 s->ignore_keys = (key->type == GDK_KEY_PRESS); 1131 return TRUE; 1132 } 1133 1134 if (key->keyval == GDK_KEY_Pause) { 1135 qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE, 1136 key->type == GDK_KEY_PRESS); 1137 return TRUE; 1138 } 1139 1140 qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget), 1141 gdk_keycode); 1142 1143 trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode, 1144 (key->type == GDK_KEY_PRESS) ? "down" : "up"); 1145 1146 for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) { 1147 if (qemu_keycode == modifier_keycode[i]) { 1148 s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS); 1149 } 1150 } 1151 1152 qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode, 1153 key->type == GDK_KEY_PRESS); 1154 1155 return TRUE; 1156 } 1157 1158 static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque) 1159 { 1160 if (event->type == GDK_MOTION_NOTIFY) { 1161 return gd_motion_event(widget, &event->motion, opaque); 1162 } 1163 return FALSE; 1164 } 1165 1166 /** Window Menu Actions **/ 1167 1168 static void gd_menu_pause(GtkMenuItem *item, void *opaque) 1169 { 1170 GtkDisplayState *s = opaque; 1171 1172 if (s->external_pause_update) { 1173 return; 1174 } 1175 if (runstate_is_running()) { 1176 qmp_stop(NULL); 1177 } else { 1178 qmp_cont(NULL); 1179 } 1180 } 1181 1182 static void gd_menu_reset(GtkMenuItem *item, void *opaque) 1183 { 1184 qmp_system_reset(NULL); 1185 } 1186 1187 static void gd_menu_powerdown(GtkMenuItem *item, void *opaque) 1188 { 1189 qmp_system_powerdown(NULL); 1190 } 1191 1192 static void gd_menu_quit(GtkMenuItem *item, void *opaque) 1193 { 1194 qmp_quit(NULL); 1195 } 1196 1197 static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque) 1198 { 1199 GtkDisplayState *s = opaque; 1200 VirtualConsole *vc = gd_vc_find_by_menu(s); 1201 GtkNotebook *nb = GTK_NOTEBOOK(s->notebook); 1202 gint page; 1203 1204 gtk_release_modifiers(s); 1205 if (vc) { 1206 page = gtk_notebook_page_num(nb, vc->tab_item); 1207 gtk_notebook_set_current_page(nb, page); 1208 gtk_widget_grab_focus(vc->focus); 1209 } 1210 s->ignore_keys = false; 1211 } 1212 1213 static void gd_accel_switch_vc(void *opaque) 1214 { 1215 VirtualConsole *vc = opaque; 1216 1217 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE); 1218 #if !GTK_CHECK_VERSION(3, 0, 0) 1219 /* GTK2 sends the accel key to the target console - ignore this until */ 1220 vc->s->ignore_keys = true; 1221 #endif 1222 } 1223 1224 static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque) 1225 { 1226 GtkDisplayState *s = opaque; 1227 VirtualConsole *vc = gd_vc_find_current(s); 1228 1229 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) { 1230 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE); 1231 } else { 1232 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE); 1233 } 1234 gd_update_windowsize(vc); 1235 } 1236 1237 static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event, 1238 void *opaque) 1239 { 1240 VirtualConsole *vc = opaque; 1241 GtkDisplayState *s = vc->s; 1242 1243 gtk_widget_set_sensitive(vc->menu_item, true); 1244 gd_widget_reparent(vc->window, s->notebook, vc->tab_item); 1245 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook), 1246 vc->tab_item, vc->label); 1247 gtk_widget_destroy(vc->window); 1248 vc->window = NULL; 1249 return TRUE; 1250 } 1251 1252 static gboolean gd_win_grab(void *opaque) 1253 { 1254 VirtualConsole *vc = opaque; 1255 1256 fprintf(stderr, "%s: %s\n", __func__, vc->label); 1257 if (vc->s->ptr_owner) { 1258 gd_ungrab_pointer(vc->s); 1259 } else { 1260 gd_grab_pointer(vc, "user-request-detached-tab"); 1261 } 1262 return TRUE; 1263 } 1264 1265 static void gd_menu_untabify(GtkMenuItem *item, void *opaque) 1266 { 1267 GtkDisplayState *s = opaque; 1268 VirtualConsole *vc = gd_vc_find_current(s); 1269 1270 if (vc->type == GD_VC_GFX && 1271 qemu_console_is_graphic(vc->gfx.dcl.con)) { 1272 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 1273 FALSE); 1274 } 1275 if (!vc->window) { 1276 gtk_widget_set_sensitive(vc->menu_item, false); 1277 vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 1278 gd_widget_reparent(s->notebook, vc->window, vc->tab_item); 1279 1280 g_signal_connect(vc->window, "delete-event", 1281 G_CALLBACK(gd_tab_window_close), vc); 1282 gtk_widget_show_all(vc->window); 1283 1284 if (qemu_console_is_graphic(vc->gfx.dcl.con)) { 1285 GtkAccelGroup *ag = gtk_accel_group_new(); 1286 gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag); 1287 1288 GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab), 1289 vc, NULL); 1290 gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb); 1291 } 1292 1293 gd_update_geometry_hints(vc); 1294 gd_update_caption(s); 1295 } 1296 } 1297 1298 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque) 1299 { 1300 GtkDisplayState *s = opaque; 1301 VirtualConsole *vc = gd_vc_find_current(s); 1302 1303 if (!s->full_screen) { 1304 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE); 1305 gtk_widget_hide(s->menu_bar); 1306 if (vc->type == GD_VC_GFX) { 1307 gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1); 1308 } 1309 gtk_window_fullscreen(GTK_WINDOW(s->window)); 1310 s->full_screen = TRUE; 1311 } else { 1312 gtk_window_unfullscreen(GTK_WINDOW(s->window)); 1313 gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s); 1314 gtk_widget_show(s->menu_bar); 1315 s->full_screen = FALSE; 1316 if (vc->type == GD_VC_GFX) { 1317 vc->gfx.scale_x = 1.0; 1318 vc->gfx.scale_y = 1.0; 1319 gd_update_windowsize(vc); 1320 } 1321 } 1322 1323 gd_update_cursor(vc); 1324 } 1325 1326 static void gd_accel_full_screen(void *opaque) 1327 { 1328 GtkDisplayState *s = opaque; 1329 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item)); 1330 } 1331 1332 static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque) 1333 { 1334 GtkDisplayState *s = opaque; 1335 VirtualConsole *vc = gd_vc_find_current(s); 1336 1337 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item), 1338 FALSE); 1339 1340 vc->gfx.scale_x += VC_SCALE_STEP; 1341 vc->gfx.scale_y += VC_SCALE_STEP; 1342 1343 gd_update_windowsize(vc); 1344 } 1345 1346 static void gd_accel_zoom_in(void *opaque) 1347 { 1348 GtkDisplayState *s = opaque; 1349 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item)); 1350 } 1351 1352 static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque) 1353 { 1354 GtkDisplayState *s = opaque; 1355 VirtualConsole *vc = gd_vc_find_current(s); 1356 1357 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item), 1358 FALSE); 1359 1360 vc->gfx.scale_x -= VC_SCALE_STEP; 1361 vc->gfx.scale_y -= VC_SCALE_STEP; 1362 1363 vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN); 1364 vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN); 1365 1366 gd_update_windowsize(vc); 1367 } 1368 1369 static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque) 1370 { 1371 GtkDisplayState *s = opaque; 1372 VirtualConsole *vc = gd_vc_find_current(s); 1373 1374 vc->gfx.scale_x = 1.0; 1375 vc->gfx.scale_y = 1.0; 1376 1377 gd_update_windowsize(vc); 1378 } 1379 1380 static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque) 1381 { 1382 GtkDisplayState *s = opaque; 1383 VirtualConsole *vc = gd_vc_find_current(s); 1384 1385 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) { 1386 s->free_scale = TRUE; 1387 } else { 1388 s->free_scale = FALSE; 1389 vc->gfx.scale_x = 1.0; 1390 vc->gfx.scale_y = 1.0; 1391 } 1392 1393 gd_update_windowsize(vc); 1394 gd_update_full_redraw(vc); 1395 } 1396 1397 #if GTK_CHECK_VERSION(3, 20, 0) 1398 static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr) 1399 { 1400 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area); 1401 GdkSeat *seat = gdk_display_get_default_seat(display); 1402 GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area); 1403 GdkSeatCapabilities caps = 0; 1404 GdkCursor *cursor = NULL; 1405 1406 if (kbd) { 1407 caps |= GDK_SEAT_CAPABILITY_KEYBOARD; 1408 } 1409 if (ptr) { 1410 caps |= GDK_SEAT_CAPABILITY_ALL_POINTING; 1411 cursor = vc->s->null_cursor; 1412 } 1413 1414 if (caps) { 1415 gdk_seat_grab(seat, window, caps, false, cursor, 1416 NULL, NULL, NULL); 1417 } else { 1418 gdk_seat_ungrab(seat); 1419 } 1420 } 1421 #elif GTK_CHECK_VERSION(3, 0, 0) 1422 static void gd_grab_devices(VirtualConsole *vc, bool grab, 1423 GdkInputSource source, GdkEventMask mask, 1424 GdkCursor *cursor) 1425 { 1426 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area); 1427 GdkDeviceManager *mgr = gdk_display_get_device_manager(display); 1428 GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER); 1429 GList *tmp = devs; 1430 1431 for (tmp = devs; tmp; tmp = tmp->next) { 1432 GdkDevice *dev = tmp->data; 1433 if (gdk_device_get_source(dev) != source) { 1434 continue; 1435 } 1436 if (grab) { 1437 GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area); 1438 gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE, 1439 mask, cursor, GDK_CURRENT_TIME); 1440 } else { 1441 gdk_device_ungrab(dev, GDK_CURRENT_TIME); 1442 } 1443 } 1444 g_list_free(devs); 1445 } 1446 #endif 1447 1448 static void gd_grab_keyboard(VirtualConsole *vc, const char *reason) 1449 { 1450 if (vc->s->kbd_owner) { 1451 if (vc->s->kbd_owner == vc) { 1452 return; 1453 } else { 1454 gd_ungrab_keyboard(vc->s); 1455 } 1456 } 1457 1458 #if GTK_CHECK_VERSION(3, 20, 0) 1459 gd_grab_update(vc, true, vc->s->ptr_owner == vc); 1460 #elif GTK_CHECK_VERSION(3, 0, 0) 1461 gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD, 1462 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, 1463 NULL); 1464 #else 1465 gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area), 1466 FALSE, 1467 GDK_CURRENT_TIME); 1468 #endif 1469 vc->s->kbd_owner = vc; 1470 gd_update_caption(vc->s); 1471 trace_gd_grab(vc->label, "kbd", reason); 1472 } 1473 1474 static void gd_ungrab_keyboard(GtkDisplayState *s) 1475 { 1476 VirtualConsole *vc = s->kbd_owner; 1477 1478 if (vc == NULL) { 1479 return; 1480 } 1481 s->kbd_owner = NULL; 1482 1483 #if GTK_CHECK_VERSION(3, 20, 0) 1484 gd_grab_update(vc, false, vc->s->ptr_owner == vc); 1485 #elif GTK_CHECK_VERSION(3, 0, 0) 1486 gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL); 1487 #else 1488 gdk_keyboard_ungrab(GDK_CURRENT_TIME); 1489 #endif 1490 gd_update_caption(s); 1491 trace_gd_ungrab(vc->label, "kbd"); 1492 } 1493 1494 static void gd_grab_pointer(VirtualConsole *vc, const char *reason) 1495 { 1496 GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area); 1497 1498 if (vc->s->ptr_owner) { 1499 if (vc->s->ptr_owner == vc) { 1500 return; 1501 } else { 1502 gd_ungrab_pointer(vc->s); 1503 } 1504 } 1505 1506 #if GTK_CHECK_VERSION(3, 20, 0) 1507 gd_grab_update(vc, vc->s->kbd_owner == vc, true); 1508 gdk_device_get_position(gd_get_pointer(display), 1509 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root); 1510 #elif GTK_CHECK_VERSION(3, 0, 0) 1511 gd_grab_devices(vc, true, GDK_SOURCE_MOUSE, 1512 GDK_POINTER_MOTION_MASK | 1513 GDK_BUTTON_PRESS_MASK | 1514 GDK_BUTTON_RELEASE_MASK | 1515 GDK_BUTTON_MOTION_MASK | 1516 GDK_SCROLL_MASK, 1517 vc->s->null_cursor); 1518 gdk_device_get_position(gd_get_pointer(display), 1519 NULL, &vc->s->grab_x_root, &vc->s->grab_y_root); 1520 #else 1521 gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area), 1522 FALSE, /* All events to come to our window directly */ 1523 GDK_POINTER_MOTION_MASK | 1524 GDK_BUTTON_PRESS_MASK | 1525 GDK_BUTTON_RELEASE_MASK | 1526 GDK_BUTTON_MOTION_MASK | 1527 GDK_SCROLL_MASK, 1528 NULL, /* Allow cursor to move over entire desktop */ 1529 vc->s->null_cursor, 1530 GDK_CURRENT_TIME); 1531 gdk_display_get_pointer(display, NULL, 1532 &vc->s->grab_x_root, &vc->s->grab_y_root, NULL); 1533 #endif 1534 vc->s->ptr_owner = vc; 1535 gd_update_caption(vc->s); 1536 trace_gd_grab(vc->label, "ptr", reason); 1537 } 1538 1539 static void gd_ungrab_pointer(GtkDisplayState *s) 1540 { 1541 VirtualConsole *vc = s->ptr_owner; 1542 GdkDisplay *display; 1543 1544 if (vc == NULL) { 1545 return; 1546 } 1547 s->ptr_owner = NULL; 1548 1549 display = gtk_widget_get_display(vc->gfx.drawing_area); 1550 #if GTK_CHECK_VERSION(3, 20, 0) 1551 gd_grab_update(vc, vc->s->kbd_owner == vc, false); 1552 gdk_device_warp(gd_get_pointer(display), 1553 gtk_widget_get_screen(vc->gfx.drawing_area), 1554 vc->s->grab_x_root, vc->s->grab_y_root); 1555 #elif GTK_CHECK_VERSION(3, 0, 0) 1556 gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL); 1557 gdk_device_warp(gd_get_pointer(display), 1558 gtk_widget_get_screen(vc->gfx.drawing_area), 1559 vc->s->grab_x_root, vc->s->grab_y_root); 1560 #else 1561 gdk_pointer_ungrab(GDK_CURRENT_TIME); 1562 gdk_display_warp_pointer(display, 1563 gtk_widget_get_screen(vc->gfx.drawing_area), 1564 vc->s->grab_x_root, vc->s->grab_y_root); 1565 #endif 1566 gd_update_caption(s); 1567 trace_gd_ungrab(vc->label, "ptr"); 1568 } 1569 1570 static void gd_menu_grab_input(GtkMenuItem *item, void *opaque) 1571 { 1572 GtkDisplayState *s = opaque; 1573 VirtualConsole *vc = gd_vc_find_current(s); 1574 1575 if (gd_is_grab_active(s)) { 1576 gd_grab_keyboard(vc, "user-request-main-window"); 1577 gd_grab_pointer(vc, "user-request-main-window"); 1578 } else { 1579 gd_ungrab_keyboard(s); 1580 gd_ungrab_pointer(s); 1581 } 1582 1583 gd_update_cursor(vc); 1584 } 1585 1586 static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2, 1587 gpointer data) 1588 { 1589 GtkDisplayState *s = data; 1590 VirtualConsole *vc; 1591 gboolean on_vga; 1592 1593 if (!gtk_widget_get_realized(s->notebook)) { 1594 return; 1595 } 1596 1597 #ifdef VTE_RESIZE_HACK 1598 vc = gd_vc_find_current(s); 1599 if (vc && vc->type == GD_VC_VTE) { 1600 gtk_widget_hide(vc->vte.terminal); 1601 } 1602 #endif 1603 vc = gd_vc_find_by_page(s, arg2); 1604 if (!vc) { 1605 return; 1606 } 1607 #ifdef VTE_RESIZE_HACK 1608 if (vc->type == GD_VC_VTE) { 1609 gtk_widget_show(vc->vte.terminal); 1610 } 1611 #endif 1612 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), 1613 TRUE); 1614 on_vga = (vc->type == GD_VC_GFX && 1615 qemu_console_is_graphic(vc->gfx.dcl.con)); 1616 if (!on_vga) { 1617 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 1618 FALSE); 1619 } else if (s->full_screen) { 1620 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), 1621 TRUE); 1622 } 1623 gtk_widget_set_sensitive(s->grab_item, on_vga); 1624 #ifdef CONFIG_VTE 1625 gtk_widget_set_sensitive(s->copy_item, vc->type == GD_VC_VTE); 1626 #endif 1627 1628 gd_update_windowsize(vc); 1629 gd_update_cursor(vc); 1630 } 1631 1632 static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, 1633 gpointer opaque) 1634 { 1635 VirtualConsole *vc = opaque; 1636 GtkDisplayState *s = vc->s; 1637 1638 if (gd_grab_on_hover(s)) { 1639 gd_grab_keyboard(vc, "grab-on-hover"); 1640 } 1641 return TRUE; 1642 } 1643 1644 static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, 1645 gpointer opaque) 1646 { 1647 VirtualConsole *vc = opaque; 1648 GtkDisplayState *s = vc->s; 1649 1650 if (gd_grab_on_hover(s)) { 1651 gd_ungrab_keyboard(s); 1652 } 1653 return TRUE; 1654 } 1655 1656 static gboolean gd_focus_out_event(GtkWidget *widget, 1657 GdkEventCrossing *crossing, gpointer opaque) 1658 { 1659 VirtualConsole *vc = opaque; 1660 GtkDisplayState *s = vc->s; 1661 1662 gtk_release_modifiers(s); 1663 return TRUE; 1664 } 1665 1666 static gboolean gd_configure(GtkWidget *widget, 1667 GdkEventConfigure *cfg, gpointer opaque) 1668 { 1669 VirtualConsole *vc = opaque; 1670 1671 gd_set_ui_info(vc, cfg->width, cfg->height); 1672 return FALSE; 1673 } 1674 1675 /** Virtual Console Callbacks **/ 1676 1677 static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc, 1678 int idx, GSList *group, GtkWidget *view_menu) 1679 { 1680 vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label); 1681 gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx, 1682 HOTKEY_MODIFIERS, 0, 1683 g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL)); 1684 #if GTK_CHECK_VERSION(3, 8, 0) 1685 gtk_accel_label_set_accel( 1686 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))), 1687 GDK_KEY_1 + idx, HOTKEY_MODIFIERS); 1688 #endif 1689 1690 g_signal_connect(vc->menu_item, "activate", 1691 G_CALLBACK(gd_menu_switch_vc), s); 1692 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item); 1693 1694 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item)); 1695 return group; 1696 } 1697 1698 #if defined(CONFIG_VTE) 1699 static void gd_menu_copy(GtkMenuItem *item, void *opaque) 1700 { 1701 GtkDisplayState *s = opaque; 1702 VirtualConsole *vc = gd_vc_find_current(s); 1703 1704 vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal)); 1705 } 1706 1707 static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque) 1708 { 1709 VirtualConsole *vc = opaque; 1710 1711 if (gtk_adjustment_get_upper(adjustment) > 1712 gtk_adjustment_get_page_size(adjustment)) { 1713 gtk_widget_show(vc->vte.scrollbar); 1714 } else { 1715 gtk_widget_hide(vc->vte.scrollbar); 1716 } 1717 } 1718 1719 static int gd_vc_chr_write(Chardev *chr, const uint8_t *buf, int len) 1720 { 1721 VCChardev *vcd = VC_CHARDEV(chr); 1722 VirtualConsole *vc = vcd->console; 1723 1724 vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len); 1725 return len; 1726 } 1727 1728 static void gd_vc_chr_set_echo(Chardev *chr, bool echo) 1729 { 1730 VCChardev *vcd = VC_CHARDEV(chr); 1731 VirtualConsole *vc = vcd->console; 1732 1733 if (vc) { 1734 vc->vte.echo = echo; 1735 } else { 1736 vcd->echo = echo; 1737 } 1738 } 1739 1740 static int nb_vcs; 1741 static Chardev *vcs[MAX_VCS]; 1742 static void gd_vc_open(Chardev *chr, 1743 ChardevBackend *backend, 1744 bool *be_opened, 1745 Error **errp) 1746 { 1747 if (nb_vcs == MAX_VCS) { 1748 error_setg(errp, "Maximum number of consoles reached"); 1749 return; 1750 } 1751 1752 vcs[nb_vcs++] = chr; 1753 1754 /* console/chardev init sometimes completes elsewhere in a 2nd 1755 * stage, so defer OPENED events until they are fully initialized 1756 */ 1757 *be_opened = false; 1758 } 1759 1760 static void char_gd_vc_class_init(ObjectClass *oc, void *data) 1761 { 1762 ChardevClass *cc = CHARDEV_CLASS(oc); 1763 1764 cc->parse = qemu_chr_parse_vc; 1765 cc->open = gd_vc_open; 1766 cc->chr_write = gd_vc_chr_write; 1767 cc->chr_set_echo = gd_vc_chr_set_echo; 1768 } 1769 1770 static const TypeInfo char_gd_vc_type_info = { 1771 .name = TYPE_CHARDEV_VC, 1772 .parent = TYPE_CHARDEV, 1773 .instance_size = sizeof(VCChardev), 1774 .class_init = char_gd_vc_class_init, 1775 }; 1776 1777 static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size, 1778 gpointer user_data) 1779 { 1780 VirtualConsole *vc = user_data; 1781 1782 if (vc->vte.echo) { 1783 VteTerminal *term = VTE_TERMINAL(vc->vte.terminal); 1784 int i; 1785 for (i = 0; i < size; i++) { 1786 uint8_t c = text[i]; 1787 if (c >= 128 || isprint(c)) { 1788 /* 8-bit characters are considered printable. */ 1789 vte_terminal_feed(term, &text[i], 1); 1790 } else if (c == '\r' || c == '\n') { 1791 vte_terminal_feed(term, "\r\n", 2); 1792 } else { 1793 char ctrl[2] = { '^', 0}; 1794 ctrl[1] = text[i] ^ 64; 1795 vte_terminal_feed(term, ctrl, 2); 1796 } 1797 } 1798 } 1799 1800 qemu_chr_be_write(vc->vte.chr, (uint8_t *)text, (unsigned int)size); 1801 return TRUE; 1802 } 1803 1804 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc, 1805 Chardev *chr, int idx, 1806 GSList *group, GtkWidget *view_menu) 1807 { 1808 char buffer[32]; 1809 GtkWidget *box; 1810 GtkWidget *scrollbar; 1811 GtkAdjustment *vadjustment; 1812 VCChardev *vcd = VC_CHARDEV(chr); 1813 1814 vc->s = s; 1815 vc->vte.echo = vcd->echo; 1816 vc->vte.chr = chr; 1817 vcd->console = vc; 1818 1819 snprintf(buffer, sizeof(buffer), "vc%d", idx); 1820 vc->label = g_strdup_printf("%s", vc->vte.chr->label 1821 ? vc->vte.chr->label : buffer); 1822 group = gd_vc_menu_init(s, vc, idx, group, view_menu); 1823 1824 vc->vte.terminal = vte_terminal_new(); 1825 g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc); 1826 1827 /* The documentation says that the default is UTF-8, but actually it is 1828 * 7-bit ASCII at least in VTE 0.38. 1829 */ 1830 #if VTE_CHECK_VERSION(0, 38, 0) 1831 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL); 1832 #else 1833 vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8"); 1834 #endif 1835 1836 vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1); 1837 vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal), 1838 VC_TERM_X_MIN, VC_TERM_Y_MIN); 1839 1840 #if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0) 1841 vadjustment = gtk_scrollable_get_vadjustment 1842 (GTK_SCROLLABLE(vc->vte.terminal)); 1843 #else 1844 vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal)); 1845 #endif 1846 1847 #if GTK_CHECK_VERSION(3, 0, 0) 1848 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2); 1849 scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment); 1850 #else 1851 box = gtk_hbox_new(false, 2); 1852 scrollbar = gtk_vscrollbar_new(vadjustment); 1853 #endif 1854 1855 gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0); 1856 gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0); 1857 1858 vc->vte.box = box; 1859 vc->vte.scrollbar = scrollbar; 1860 1861 g_signal_connect(vadjustment, "changed", 1862 G_CALLBACK(gd_vc_adjustment_changed), vc); 1863 1864 vc->type = GD_VC_VTE; 1865 vc->tab_item = box; 1866 vc->focus = vc->vte.terminal; 1867 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item, 1868 gtk_label_new(vc->label)); 1869 1870 qemu_chr_be_generic_open(vc->vte.chr); 1871 1872 return group; 1873 } 1874 1875 static void gd_vcs_init(GtkDisplayState *s, GSList *group, 1876 GtkWidget *view_menu) 1877 { 1878 int i; 1879 1880 for (i = 0; i < nb_vcs; i++) { 1881 VirtualConsole *vc = &s->vc[s->nb_vcs]; 1882 group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu); 1883 s->nb_vcs++; 1884 } 1885 } 1886 #endif /* CONFIG_VTE */ 1887 1888 /** Window Creation **/ 1889 1890 static void gd_connect_vc_gfx_signals(VirtualConsole *vc) 1891 { 1892 #if GTK_CHECK_VERSION(3, 0, 0) 1893 g_signal_connect(vc->gfx.drawing_area, "draw", 1894 G_CALLBACK(gd_draw_event), vc); 1895 #if defined(CONFIG_GTK_GL) 1896 if (display_opengl) { 1897 /* wire up GtkGlArea events */ 1898 g_signal_connect(vc->gfx.drawing_area, "render", 1899 G_CALLBACK(gd_render_event), vc); 1900 g_signal_connect(vc->gfx.drawing_area, "resize", 1901 G_CALLBACK(gd_resize_event), vc); 1902 } 1903 #endif 1904 #else 1905 g_signal_connect(vc->gfx.drawing_area, "expose-event", 1906 G_CALLBACK(gd_expose_event), vc); 1907 #endif 1908 if (qemu_console_is_graphic(vc->gfx.dcl.con)) { 1909 g_signal_connect(vc->gfx.drawing_area, "event", 1910 G_CALLBACK(gd_event), vc); 1911 g_signal_connect(vc->gfx.drawing_area, "button-press-event", 1912 G_CALLBACK(gd_button_event), vc); 1913 g_signal_connect(vc->gfx.drawing_area, "button-release-event", 1914 G_CALLBACK(gd_button_event), vc); 1915 g_signal_connect(vc->gfx.drawing_area, "scroll-event", 1916 G_CALLBACK(gd_scroll_event), vc); 1917 g_signal_connect(vc->gfx.drawing_area, "key-press-event", 1918 G_CALLBACK(gd_key_event), vc); 1919 g_signal_connect(vc->gfx.drawing_area, "key-release-event", 1920 G_CALLBACK(gd_key_event), vc); 1921 1922 g_signal_connect(vc->gfx.drawing_area, "enter-notify-event", 1923 G_CALLBACK(gd_enter_event), vc); 1924 g_signal_connect(vc->gfx.drawing_area, "leave-notify-event", 1925 G_CALLBACK(gd_leave_event), vc); 1926 g_signal_connect(vc->gfx.drawing_area, "focus-out-event", 1927 G_CALLBACK(gd_focus_out_event), vc); 1928 g_signal_connect(vc->gfx.drawing_area, "configure-event", 1929 G_CALLBACK(gd_configure), vc); 1930 } else { 1931 g_signal_connect(vc->gfx.drawing_area, "key-press-event", 1932 G_CALLBACK(gd_text_key_down), vc); 1933 } 1934 } 1935 1936 static void gd_connect_signals(GtkDisplayState *s) 1937 { 1938 g_signal_connect(s->show_tabs_item, "activate", 1939 G_CALLBACK(gd_menu_show_tabs), s); 1940 g_signal_connect(s->untabify_item, "activate", 1941 G_CALLBACK(gd_menu_untabify), s); 1942 1943 g_signal_connect(s->window, "delete-event", 1944 G_CALLBACK(gd_window_close), s); 1945 1946 g_signal_connect(s->pause_item, "activate", 1947 G_CALLBACK(gd_menu_pause), s); 1948 g_signal_connect(s->reset_item, "activate", 1949 G_CALLBACK(gd_menu_reset), s); 1950 g_signal_connect(s->powerdown_item, "activate", 1951 G_CALLBACK(gd_menu_powerdown), s); 1952 g_signal_connect(s->quit_item, "activate", 1953 G_CALLBACK(gd_menu_quit), s); 1954 #if defined(CONFIG_VTE) 1955 g_signal_connect(s->copy_item, "activate", 1956 G_CALLBACK(gd_menu_copy), s); 1957 #endif 1958 g_signal_connect(s->full_screen_item, "activate", 1959 G_CALLBACK(gd_menu_full_screen), s); 1960 g_signal_connect(s->zoom_in_item, "activate", 1961 G_CALLBACK(gd_menu_zoom_in), s); 1962 g_signal_connect(s->zoom_out_item, "activate", 1963 G_CALLBACK(gd_menu_zoom_out), s); 1964 g_signal_connect(s->zoom_fixed_item, "activate", 1965 G_CALLBACK(gd_menu_zoom_fixed), s); 1966 g_signal_connect(s->zoom_fit_item, "activate", 1967 G_CALLBACK(gd_menu_zoom_fit), s); 1968 g_signal_connect(s->grab_item, "activate", 1969 G_CALLBACK(gd_menu_grab_input), s); 1970 g_signal_connect(s->notebook, "switch-page", 1971 G_CALLBACK(gd_change_page), s); 1972 } 1973 1974 static GtkWidget *gd_create_menu_machine(GtkDisplayState *s) 1975 { 1976 GtkWidget *machine_menu; 1977 GtkWidget *separator; 1978 1979 machine_menu = gtk_menu_new(); 1980 gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group); 1981 1982 s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause")); 1983 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item); 1984 1985 separator = gtk_separator_menu_item_new(); 1986 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator); 1987 1988 s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset")); 1989 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item); 1990 1991 s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down")); 1992 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item); 1993 1994 separator = gtk_separator_menu_item_new(); 1995 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator); 1996 1997 s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit")); 1998 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item), 1999 "<QEMU>/Machine/Quit"); 2000 gtk_accel_map_add_entry("<QEMU>/Machine/Quit", 2001 GDK_KEY_q, HOTKEY_MODIFIERS); 2002 gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item); 2003 2004 return machine_menu; 2005 } 2006 2007 static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc, 2008 QemuConsole *con, int idx, 2009 GSList *group, GtkWidget *view_menu) 2010 { 2011 vc->label = qemu_console_get_label(con); 2012 vc->s = s; 2013 vc->gfx.scale_x = 1.0; 2014 vc->gfx.scale_y = 1.0; 2015 2016 #if defined(CONFIG_OPENGL) 2017 if (display_opengl) { 2018 #if defined(CONFIG_GTK_GL) 2019 vc->gfx.drawing_area = gtk_gl_area_new(); 2020 vc->gfx.dcl.ops = &dcl_gl_area_ops; 2021 #else 2022 vc->gfx.drawing_area = gtk_drawing_area_new(); 2023 /* 2024 * gtk_widget_set_double_buffered() was deprecated in 3.14. 2025 * It is required for opengl rendering on X11 though. A 2026 * proper replacement (native opengl support) is only 2027 * available in 3.16+. Silence the warning if possible. 2028 */ 2029 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE 2030 #pragma GCC diagnostic push 2031 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 2032 #endif 2033 gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE); 2034 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE 2035 #pragma GCC diagnostic pop 2036 #endif 2037 vc->gfx.dcl.ops = &dcl_egl_ops; 2038 #endif /* CONFIG_GTK_GL */ 2039 } else 2040 #endif 2041 { 2042 vc->gfx.drawing_area = gtk_drawing_area_new(); 2043 vc->gfx.dcl.ops = &dcl_ops; 2044 } 2045 2046 2047 gtk_widget_add_events(vc->gfx.drawing_area, 2048 GDK_POINTER_MOTION_MASK | 2049 GDK_BUTTON_PRESS_MASK | 2050 GDK_BUTTON_RELEASE_MASK | 2051 GDK_BUTTON_MOTION_MASK | 2052 GDK_ENTER_NOTIFY_MASK | 2053 GDK_LEAVE_NOTIFY_MASK | 2054 GDK_SCROLL_MASK | 2055 GDK_KEY_PRESS_MASK); 2056 gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE); 2057 2058 vc->type = GD_VC_GFX; 2059 vc->tab_item = vc->gfx.drawing_area; 2060 vc->focus = vc->gfx.drawing_area; 2061 gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), 2062 vc->tab_item, gtk_label_new(vc->label)); 2063 2064 vc->gfx.dcl.con = con; 2065 register_displaychangelistener(&vc->gfx.dcl); 2066 2067 gd_connect_vc_gfx_signals(vc); 2068 group = gd_vc_menu_init(s, vc, idx, group, view_menu); 2069 2070 if (dpy_ui_info_supported(vc->gfx.dcl.con)) { 2071 gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item)); 2072 s->free_scale = true; 2073 } 2074 2075 return group; 2076 } 2077 2078 static GtkWidget *gd_create_menu_view(GtkDisplayState *s) 2079 { 2080 GSList *group = NULL; 2081 GtkWidget *view_menu; 2082 GtkWidget *separator; 2083 QemuConsole *con; 2084 int vc; 2085 2086 view_menu = gtk_menu_new(); 2087 gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group); 2088 2089 s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen")); 2090 2091 #if defined(CONFIG_VTE) 2092 s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy")); 2093 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item); 2094 #endif 2095 2096 gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0, 2097 g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL)); 2098 #if GTK_CHECK_VERSION(3, 8, 0) 2099 gtk_accel_label_set_accel( 2100 GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))), 2101 GDK_KEY_f, HOTKEY_MODIFIERS); 2102 #endif 2103 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item); 2104 2105 separator = gtk_separator_menu_item_new(); 2106 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 2107 2108 s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In")); 2109 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item), 2110 "<QEMU>/View/Zoom In"); 2111 gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, 2112 HOTKEY_MODIFIERS); 2113 gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0, 2114 g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL)); 2115 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item); 2116 2117 s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out")); 2118 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item), 2119 "<QEMU>/View/Zoom Out"); 2120 gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus, 2121 HOTKEY_MODIFIERS); 2122 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item); 2123 2124 s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit")); 2125 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item), 2126 "<QEMU>/View/Zoom Fixed"); 2127 gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0, 2128 HOTKEY_MODIFIERS); 2129 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item); 2130 2131 s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit")); 2132 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item); 2133 2134 separator = gtk_separator_menu_item_new(); 2135 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 2136 2137 s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover")); 2138 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item); 2139 2140 s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input")); 2141 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item), 2142 "<QEMU>/View/Grab Input"); 2143 gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g, 2144 HOTKEY_MODIFIERS); 2145 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item); 2146 2147 separator = gtk_separator_menu_item_new(); 2148 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 2149 2150 /* gfx */ 2151 for (vc = 0;; vc++) { 2152 con = qemu_console_lookup_by_index(vc); 2153 if (!con) { 2154 break; 2155 } 2156 group = gd_vc_gfx_init(s, &s->vc[vc], con, 2157 vc, group, view_menu); 2158 s->nb_vcs++; 2159 } 2160 2161 #if defined(CONFIG_VTE) 2162 /* vte */ 2163 gd_vcs_init(s, group, view_menu); 2164 #endif 2165 2166 separator = gtk_separator_menu_item_new(); 2167 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator); 2168 2169 s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs")); 2170 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item); 2171 2172 s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab")); 2173 gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item); 2174 2175 return view_menu; 2176 } 2177 2178 static void gd_create_menus(GtkDisplayState *s) 2179 { 2180 s->accel_group = gtk_accel_group_new(); 2181 s->machine_menu = gd_create_menu_machine(s); 2182 s->view_menu = gd_create_menu_view(s); 2183 2184 s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine")); 2185 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item), 2186 s->machine_menu); 2187 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item); 2188 2189 s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View")); 2190 gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu); 2191 gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item); 2192 2193 g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group); 2194 gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group); 2195 } 2196 2197 static void gd_set_keycode_type(GtkDisplayState *s) 2198 { 2199 #ifdef GDK_WINDOWING_X11 2200 GdkDisplay *display = gtk_widget_get_display(s->window); 2201 if (GDK_IS_X11_DISPLAY(display)) { 2202 Display *x11_display = gdk_x11_display_get_xdisplay(display); 2203 XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask, 2204 XkbUseCoreKbd); 2205 char *keycodes = NULL; 2206 2207 if (desc && desc->names) { 2208 keycodes = XGetAtomName(x11_display, desc->names->keycodes); 2209 } 2210 if (keycodes == NULL) { 2211 fprintf(stderr, "could not lookup keycode name\n"); 2212 } else if (strstart(keycodes, "evdev", NULL)) { 2213 s->has_evdev = true; 2214 } else if (!strstart(keycodes, "xfree86", NULL)) { 2215 fprintf(stderr, "unknown keycodes `%s', please report to " 2216 "qemu-devel@nongnu.org\n", keycodes); 2217 } 2218 2219 if (desc) { 2220 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True); 2221 } 2222 if (keycodes) { 2223 XFree(keycodes); 2224 } 2225 } 2226 #endif 2227 } 2228 2229 static gboolean gtkinit; 2230 2231 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover) 2232 { 2233 VirtualConsole *vc; 2234 2235 GtkDisplayState *s = g_malloc0(sizeof(*s)); 2236 char *filename; 2237 GdkDisplay *window_display; 2238 2239 if (!gtkinit) { 2240 fprintf(stderr, "gtk initialization failed\n"); 2241 exit(1); 2242 } 2243 2244 s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 2245 #if GTK_CHECK_VERSION(3, 2, 0) 2246 s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); 2247 #else 2248 s->vbox = gtk_vbox_new(FALSE, 0); 2249 #endif 2250 s->notebook = gtk_notebook_new(); 2251 s->menu_bar = gtk_menu_bar_new(); 2252 2253 s->free_scale = FALSE; 2254 2255 /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For 2256 * LC_CTYPE, we need to make sure that non-ASCII characters are considered 2257 * printable, but without changing any of the character classes to make 2258 * sure that we don't accidentally break implicit assumptions. */ 2259 setlocale(LC_MESSAGES, ""); 2260 setlocale(LC_CTYPE, "C.UTF-8"); 2261 bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR); 2262 textdomain("qemu"); 2263 2264 window_display = gtk_widget_get_display(s->window); 2265 s->null_cursor = gdk_cursor_new_for_display(window_display, 2266 GDK_BLANK_CURSOR); 2267 2268 s->mouse_mode_notifier.notify = gd_mouse_mode_change; 2269 qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier); 2270 qemu_add_vm_change_state_handler(gd_change_runstate, s); 2271 2272 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg"); 2273 if (filename) { 2274 GError *error = NULL; 2275 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error); 2276 if (pixbuf) { 2277 gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf); 2278 } else { 2279 g_error_free(error); 2280 } 2281 g_free(filename); 2282 } 2283 2284 gd_create_menus(s); 2285 2286 gd_connect_signals(s); 2287 2288 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE); 2289 gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE); 2290 2291 gd_update_caption(s); 2292 2293 gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0); 2294 gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0); 2295 2296 gtk_container_add(GTK_CONTAINER(s->window), s->vbox); 2297 2298 gtk_widget_show_all(s->window); 2299 2300 #ifdef VTE_RESIZE_HACK 2301 { 2302 VirtualConsole *cur = gd_vc_find_current(s); 2303 if (cur) { 2304 int i; 2305 2306 for (i = 0; i < s->nb_vcs; i++) { 2307 VirtualConsole *vc = &s->vc[i]; 2308 if (vc && vc->type == GD_VC_VTE && vc != cur) { 2309 gtk_widget_hide(vc->vte.terminal); 2310 } 2311 } 2312 gd_update_windowsize(cur); 2313 } 2314 } 2315 #endif 2316 2317 vc = gd_vc_find_current(s); 2318 gtk_widget_set_sensitive(s->view_menu, vc != NULL); 2319 #ifdef CONFIG_VTE 2320 gtk_widget_set_sensitive(s->copy_item, 2321 vc && vc->type == GD_VC_VTE); 2322 #endif 2323 2324 if (full_screen) { 2325 gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item)); 2326 } 2327 if (grab_on_hover) { 2328 gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item)); 2329 } 2330 2331 gd_set_keycode_type(s); 2332 } 2333 2334 void early_gtk_display_init(int opengl) 2335 { 2336 /* The QEMU code relies on the assumption that it's always run in 2337 * the C locale. Therefore it is not prepared to deal with 2338 * operations that produce different results depending on the 2339 * locale, such as printf's formatting of decimal numbers, and 2340 * possibly others. 2341 * 2342 * Since GTK+ calls setlocale() by default -importing the locale 2343 * settings from the environment- we must prevent it from doing so 2344 * using gtk_disable_setlocale(). 2345 * 2346 * QEMU's GTK+ UI, however, _does_ have translations for some of 2347 * the menu items. As a trade-off between a functionally correct 2348 * QEMU and a fully internationalized UI we support importing 2349 * LC_MESSAGES from the environment (see the setlocale() call 2350 * earlier in this file). This allows us to display translated 2351 * messages leaving everything else untouched. 2352 */ 2353 gtk_disable_setlocale(); 2354 gtkinit = gtk_init_check(NULL, NULL); 2355 if (!gtkinit) { 2356 /* don't exit yet, that'll break -help */ 2357 return; 2358 } 2359 2360 switch (opengl) { 2361 case -1: /* default */ 2362 case 0: /* off */ 2363 break; 2364 case 1: /* on */ 2365 #if defined(CONFIG_OPENGL) 2366 #if defined(CONFIG_GTK_GL) 2367 gtk_gl_area_init(); 2368 #else 2369 gtk_egl_init(); 2370 #endif 2371 #endif 2372 break; 2373 default: 2374 g_assert_not_reached(); 2375 break; 2376 } 2377 2378 #if defined(CONFIG_VTE) 2379 type_register(&char_gd_vc_type_info); 2380 #endif 2381 } 2382