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