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