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