qemu/ui/gtk.c
<<
>>
Prefs
   1/*
   2 * GTK UI
   3 *
   4 * Copyright IBM, Corp. 2012
   5 *
   6 * Authors:
   7 *  Anthony Liguori   <aliguori@us.ibm.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10 * See the COPYING file in the top-level directory.
  11 *
  12 * Portions from gtk-vnc:
  13 *
  14 * GTK VNC Widget
  15 *
  16 * Copyright (C) 2006  Anthony Liguori <anthony@codemonkey.ws>
  17 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
  18 *
  19 * This library is free software; you can redistribute it and/or
  20 * modify it under the terms of the GNU Lesser General Public
  21 * License as published by the Free Software Foundation; either
  22 * version 2.0 of the License, or (at your option) any later version.
  23 *
  24 * This library is distributed in the hope that it will be useful,
  25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  27 * Lesser General Public License for more details.
  28 *
  29 * You should have received a copy of the GNU Lesser General Public
  30 * License along with this library; if not, write to the Free Software
  31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  32 */
  33
  34#define GETTEXT_PACKAGE "qemu"
  35#define LOCALEDIR "po"
  36
  37#ifdef _WIN32
  38# define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
  39#endif
  40
  41#include "qemu-common.h"
  42
  43#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
  44/* Work around an -Wstrict-prototypes warning in GTK headers */
  45#pragma GCC diagnostic push
  46#pragma GCC diagnostic ignored "-Wstrict-prototypes"
  47#endif
  48#include <gtk/gtk.h>
  49#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
  50#pragma GCC diagnostic pop
  51#endif
  52
  53
  54#include <gdk/gdkkeysyms.h>
  55#include <glib/gi18n.h>
  56#include <locale.h>
  57#if defined(CONFIG_VTE)
  58#include <vte/vte.h>
  59#endif
  60#include <math.h>
  61
  62#include "trace.h"
  63#include "ui/console.h"
  64#include "ui/input.h"
  65#include "sysemu/sysemu.h"
  66#include "qmp-commands.h"
  67#include "x_keymap.h"
  68#include "keymaps.h"
  69#include "sysemu/char.h"
  70#include "qom/object.h"
  71#ifdef GDK_WINDOWING_X11
  72#include <gdk/gdkx.h>
  73#include <X11/XKBlib.h>
  74#endif
  75
  76#define MAX_VCS 10
  77#define VC_WINDOW_X_MIN  320
  78#define VC_WINDOW_Y_MIN  240
  79#define VC_TERM_X_MIN     80
  80#define VC_TERM_Y_MIN     25
  81#define VC_SCALE_MIN    0.25
  82#define VC_SCALE_STEP   0.25
  83
  84#if !defined(CONFIG_VTE)
  85# define VTE_CHECK_VERSION(a, b, c) 0
  86#endif
  87
  88#if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
  89/*
  90 * The gtk2 vte terminal widget seriously messes up the window resize
  91 * for some reason.  You basically can't make the qemu window smaller
  92 * any more because the toplevel window geoemtry hints are overridden.
  93 *
  94 * Workaround that by hiding all vte widgets, except the one in the
  95 * current tab.
  96 *
  97 * Luckily everything works smooth in gtk3.
  98 */
  99# define VTE_RESIZE_HACK 1
 100#endif
 101
 102/* Compatibility define to let us build on both Gtk2 and Gtk3 */
 103#if GTK_CHECK_VERSION(3, 0, 0)
 104static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
 105{
 106    *ww = gdk_window_get_width(w);
 107    *wh = gdk_window_get_height(w);
 108}
 109#endif
 110
 111#if !GTK_CHECK_VERSION(2, 20, 0)
 112#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
 113#endif
 114
 115#ifndef GDK_IS_X11_DISPLAY
 116#define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
 117#endif
 118#ifndef GDK_IS_WIN32_DISPLAY
 119#define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
 120#endif
 121
 122#ifndef GDK_KEY_0
 123#define GDK_KEY_0 GDK_0
 124#define GDK_KEY_1 GDK_1
 125#define GDK_KEY_2 GDK_2
 126#define GDK_KEY_f GDK_f
 127#define GDK_KEY_g GDK_g
 128#define GDK_KEY_q GDK_q
 129#define GDK_KEY_plus GDK_plus
 130#define GDK_KEY_minus GDK_minus
 131#endif
 132
 133#define HOTKEY_MODIFIERS        (GDK_CONTROL_MASK | GDK_MOD1_MASK)
 134
 135static const int modifier_keycode[] = {
 136    /* shift, control, alt keys, meta keys, both left & right */
 137    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
 138};
 139
 140typedef struct GtkDisplayState GtkDisplayState;
 141
 142typedef struct VirtualGfxConsole {
 143    GtkWidget *drawing_area;
 144    DisplayChangeListener dcl;
 145    DisplaySurface *ds;
 146    pixman_image_t *convert;
 147    cairo_surface_t *surface;
 148    double scale_x;
 149    double scale_y;
 150} VirtualGfxConsole;
 151
 152#if defined(CONFIG_VTE)
 153typedef struct VirtualVteConsole {
 154    GtkWidget *box;
 155    GtkWidget *scrollbar;
 156    GtkWidget *terminal;
 157    CharDriverState *chr;
 158} VirtualVteConsole;
 159#endif
 160
 161typedef enum VirtualConsoleType {
 162    GD_VC_GFX,
 163    GD_VC_VTE,
 164} VirtualConsoleType;
 165
 166typedef struct VirtualConsole {
 167    GtkDisplayState *s;
 168    char *label;
 169    GtkWidget *window;
 170    GtkWidget *menu_item;
 171    GtkWidget *tab_item;
 172    VirtualConsoleType type;
 173    union {
 174        VirtualGfxConsole gfx;
 175#if defined(CONFIG_VTE)
 176        VirtualVteConsole vte;
 177#endif
 178    };
 179} VirtualConsole;
 180
 181struct GtkDisplayState {
 182    GtkWidget *window;
 183
 184    GtkWidget *menu_bar;
 185
 186    GtkAccelGroup *accel_group;
 187
 188    GtkWidget *machine_menu_item;
 189    GtkWidget *machine_menu;
 190    GtkWidget *pause_item;
 191    GtkWidget *reset_item;
 192    GtkWidget *powerdown_item;
 193    GtkWidget *quit_item;
 194
 195    GtkWidget *view_menu_item;
 196    GtkWidget *view_menu;
 197    GtkWidget *full_screen_item;
 198    GtkWidget *zoom_in_item;
 199    GtkWidget *zoom_out_item;
 200    GtkWidget *zoom_fixed_item;
 201    GtkWidget *zoom_fit_item;
 202    GtkWidget *grab_item;
 203    GtkWidget *grab_on_hover_item;
 204
 205    int nb_vcs;
 206    VirtualConsole vc[MAX_VCS];
 207
 208    GtkWidget *show_tabs_item;
 209    GtkWidget *untabify_item;
 210
 211    GtkWidget *vbox;
 212    GtkWidget *notebook;
 213    int button_mask;
 214    gboolean last_set;
 215    int last_x;
 216    int last_y;
 217    int grab_x_root;
 218    int grab_y_root;
 219    VirtualConsole *kbd_owner;
 220    VirtualConsole *ptr_owner;
 221
 222    gboolean full_screen;
 223
 224    GdkCursor *null_cursor;
 225    Notifier mouse_mode_notifier;
 226    gboolean free_scale;
 227
 228    bool external_pause_update;
 229
 230    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
 231    bool has_evdev;
 232};
 233
 234static void gd_grab_pointer(VirtualConsole *vc);
 235static void gd_ungrab_pointer(GtkDisplayState *s);
 236
 237/** Utility Functions **/
 238
 239static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
 240{
 241    VirtualConsole *vc;
 242    gint i;
 243
 244    for (i = 0; i < s->nb_vcs; i++) {
 245        vc = &s->vc[i];
 246        if (gtk_check_menu_item_get_active
 247            (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
 248            return vc;
 249        }
 250    }
 251    return NULL;
 252}
 253
 254static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
 255{
 256    VirtualConsole *vc;
 257    gint i, p;
 258
 259    for (i = 0; i < s->nb_vcs; i++) {
 260        vc = &s->vc[i];
 261        p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
 262        if (p == page) {
 263            return vc;
 264        }
 265    }
 266    return NULL;
 267}
 268
 269static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
 270{
 271    gint page;
 272
 273    page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
 274    return gd_vc_find_by_page(s, page);
 275}
 276
 277static bool gd_is_grab_active(GtkDisplayState *s)
 278{
 279    return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
 280}
 281
 282static bool gd_grab_on_hover(GtkDisplayState *s)
 283{
 284    return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
 285}
 286
 287static void gd_update_cursor(VirtualConsole *vc)
 288{
 289    GtkDisplayState *s = vc->s;
 290    GdkWindow *window;
 291
 292    if (vc->type != GD_VC_GFX) {
 293        return;
 294    }
 295
 296    window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
 297    if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
 298        gdk_window_set_cursor(window, s->null_cursor);
 299    } else {
 300        gdk_window_set_cursor(window, NULL);
 301    }
 302}
 303
 304static void gd_update_caption(GtkDisplayState *s)
 305{
 306    const char *status = "";
 307    gchar *prefix;
 308    gchar *title;
 309    const char *grab = "";
 310    bool is_paused = !runstate_is_running();
 311    int i;
 312
 313    if (qemu_name) {
 314        prefix = g_strdup_printf("QEMU (%s)", qemu_name);
 315    } else {
 316        prefix = g_strdup_printf("QEMU");
 317    }
 318
 319    if (s->ptr_owner != NULL &&
 320        s->ptr_owner->window == NULL) {
 321        grab = _(" - Press Ctrl+Alt+G to release grab");
 322    }
 323
 324    if (is_paused) {
 325        status = _(" [Paused]");
 326    }
 327    s->external_pause_update = true;
 328    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
 329                                   is_paused);
 330    s->external_pause_update = false;
 331
 332    title = g_strdup_printf("%s%s%s", prefix, status, grab);
 333    gtk_window_set_title(GTK_WINDOW(s->window), title);
 334    g_free(title);
 335
 336    for (i = 0; i < s->nb_vcs; i++) {
 337        VirtualConsole *vc = &s->vc[i];
 338
 339        if (!vc->window) {
 340            continue;
 341        }
 342        title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
 343                                vc == s->kbd_owner ? " +kbd" : "",
 344                                vc == s->ptr_owner ? " +ptr" : "");
 345        gtk_window_set_title(GTK_WINDOW(vc->window), title);
 346        g_free(title);
 347    }
 348
 349    g_free(prefix);
 350}
 351
 352static void gd_update_geometry_hints(VirtualConsole *vc)
 353{
 354    GtkDisplayState *s = vc->s;
 355    GdkWindowHints mask = 0;
 356    GdkGeometry geo = {};
 357    GtkWidget *geo_widget = NULL;
 358    GtkWindow *geo_window;
 359
 360    if (vc->type == GD_VC_GFX) {
 361        if (s->free_scale) {
 362            geo.min_width  = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
 363            geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
 364            mask |= GDK_HINT_MIN_SIZE;
 365        } else {
 366            geo.min_width  = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
 367            geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
 368            mask |= GDK_HINT_MIN_SIZE;
 369        }
 370        geo_widget = vc->gfx.drawing_area;
 371        gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
 372
 373#if defined(CONFIG_VTE)
 374    } else if (vc->type == GD_VC_VTE) {
 375        VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
 376        GtkBorder *ib;
 377
 378        geo.width_inc  = vte_terminal_get_char_width(term);
 379        geo.height_inc = vte_terminal_get_char_height(term);
 380        mask |= GDK_HINT_RESIZE_INC;
 381        geo.base_width  = geo.width_inc;
 382        geo.base_height = geo.height_inc;
 383        mask |= GDK_HINT_BASE_SIZE;
 384        geo.min_width  = geo.width_inc * VC_TERM_X_MIN;
 385        geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
 386        mask |= GDK_HINT_MIN_SIZE;
 387        gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
 388        geo.base_width  += ib->left + ib->right;
 389        geo.base_height += ib->top + ib->bottom;
 390        geo.min_width   += ib->left + ib->right;
 391        geo.min_height  += ib->top + ib->bottom;
 392        geo_widget = vc->vte.terminal;
 393#endif
 394    }
 395
 396    geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
 397    gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
 398}
 399
 400static void gd_update_windowsize(VirtualConsole *vc)
 401{
 402    GtkDisplayState *s = vc->s;
 403
 404    gd_update_geometry_hints(vc);
 405
 406    if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
 407        gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
 408                          VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
 409    }
 410}
 411
 412static void gd_update_full_redraw(VirtualConsole *vc)
 413{
 414    GtkWidget *area = vc->gfx.drawing_area;
 415    int ww, wh;
 416    gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
 417    gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
 418}
 419
 420static void gtk_release_modifiers(GtkDisplayState *s)
 421{
 422    VirtualConsole *vc = gd_vc_find_current(s);
 423    int i, keycode;
 424
 425    if (vc->type != GD_VC_GFX) {
 426        return;
 427    }
 428    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
 429        keycode = modifier_keycode[i];
 430        if (!s->modifier_pressed[i]) {
 431            continue;
 432        }
 433        qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
 434        s->modifier_pressed[i] = false;
 435    }
 436}
 437
 438/** DisplayState Callbacks **/
 439
 440static void gd_update(DisplayChangeListener *dcl,
 441                      int x, int y, int w, int h)
 442{
 443    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 444    int x1, x2, y1, y2;
 445    int mx, my;
 446    int fbw, fbh;
 447    int ww, wh;
 448
 449    trace_gd_update(vc->label, x, y, w, h);
 450
 451    if (vc->gfx.convert) {
 452        pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
 453                               NULL, vc->gfx.convert,
 454                               x, y, 0, 0, x, y, w, h);
 455    }
 456
 457    x1 = floor(x * vc->gfx.scale_x);
 458    y1 = floor(y * vc->gfx.scale_y);
 459
 460    x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
 461    y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
 462
 463    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
 464    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
 465
 466    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
 467                          &ww, &wh);
 468
 469    mx = my = 0;
 470    if (ww > fbw) {
 471        mx = (ww - fbw) / 2;
 472    }
 473    if (wh > fbh) {
 474        my = (wh - fbh) / 2;
 475    }
 476
 477    gtk_widget_queue_draw_area(vc->gfx.drawing_area,
 478                               mx + x1, my + y1, (x2 - x1), (y2 - y1));
 479}
 480
 481static void gd_refresh(DisplayChangeListener *dcl)
 482{
 483    graphic_hw_update(dcl->con);
 484}
 485
 486#if GTK_CHECK_VERSION(3, 0, 0)
 487static void gd_mouse_set(DisplayChangeListener *dcl,
 488                         int x, int y, int visible)
 489{
 490    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 491    GdkDisplay *dpy;
 492    GdkDeviceManager *mgr;
 493    gint x_root, y_root;
 494
 495    if (qemu_input_is_absolute()) {
 496        return;
 497    }
 498
 499    dpy = gtk_widget_get_display(vc->gfx.drawing_area);
 500    mgr = gdk_display_get_device_manager(dpy);
 501    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
 502                               x, y, &x_root, &y_root);
 503    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
 504                    gtk_widget_get_screen(vc->gfx.drawing_area),
 505                    x_root, y_root);
 506}
 507#else
 508static void gd_mouse_set(DisplayChangeListener *dcl,
 509                         int x, int y, int visible)
 510{
 511    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 512    gint x_root, y_root;
 513
 514    if (qemu_input_is_absolute()) {
 515        return;
 516    }
 517
 518    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
 519                               x, y, &x_root, &y_root);
 520    gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
 521                             gtk_widget_get_screen(vc->gfx.drawing_area),
 522                             x_root, y_root);
 523}
 524#endif
 525
 526static void gd_cursor_define(DisplayChangeListener *dcl,
 527                             QEMUCursor *c)
 528{
 529    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 530    GdkPixbuf *pixbuf;
 531    GdkCursor *cursor;
 532
 533    pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
 534                                      GDK_COLORSPACE_RGB, true, 8,
 535                                      c->width, c->height, c->width * 4,
 536                                      NULL, NULL);
 537    cursor = gdk_cursor_new_from_pixbuf
 538        (gtk_widget_get_display(vc->gfx.drawing_area),
 539         pixbuf, c->hot_x, c->hot_y);
 540    gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
 541    g_object_unref(pixbuf);
 542#if !GTK_CHECK_VERSION(3, 0, 0)
 543    gdk_cursor_unref(cursor);
 544#else
 545    g_object_unref(cursor);
 546#endif
 547}
 548
 549static void gd_switch(DisplayChangeListener *dcl,
 550                      DisplaySurface *surface)
 551{
 552    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 553    bool resized = true;
 554
 555    trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
 556
 557    if (vc->gfx.surface) {
 558        cairo_surface_destroy(vc->gfx.surface);
 559    }
 560
 561    if (vc->gfx.ds &&
 562        surface_width(vc->gfx.ds) == surface_width(surface) &&
 563        surface_height(vc->gfx.ds) == surface_height(surface)) {
 564        resized = false;
 565    }
 566    vc->gfx.ds = surface;
 567
 568    if (vc->gfx.convert) {
 569        pixman_image_unref(vc->gfx.convert);
 570        vc->gfx.convert = NULL;
 571    }
 572
 573    if (surface->format == PIXMAN_x8r8g8b8) {
 574        /*
 575         * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
 576         *
 577         * No need to convert, use surface directly.  Should be the
 578         * common case as this is qemu_default_pixelformat(32) too.
 579         */
 580        vc->gfx.surface = cairo_image_surface_create_for_data
 581            (surface_data(surface),
 582             CAIRO_FORMAT_RGB24,
 583             surface_width(surface),
 584             surface_height(surface),
 585             surface_stride(surface));
 586    } else {
 587        /* Must convert surface, use pixman to do it. */
 588        vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
 589                                                   surface_width(surface),
 590                                                   surface_height(surface),
 591                                                   NULL, 0);
 592        vc->gfx.surface = cairo_image_surface_create_for_data
 593            ((void *)pixman_image_get_data(vc->gfx.convert),
 594             CAIRO_FORMAT_RGB24,
 595             pixman_image_get_width(vc->gfx.convert),
 596             pixman_image_get_height(vc->gfx.convert),
 597             pixman_image_get_stride(vc->gfx.convert));
 598        pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
 599                               NULL, vc->gfx.convert,
 600                               0, 0, 0, 0, 0, 0,
 601                               pixman_image_get_width(vc->gfx.convert),
 602                               pixman_image_get_height(vc->gfx.convert));
 603    }
 604
 605    if (resized) {
 606        gd_update_windowsize(vc);
 607    } else {
 608        gd_update_full_redraw(vc);
 609    }
 610}
 611
 612/** QEMU Events **/
 613
 614static void gd_change_runstate(void *opaque, int running, RunState state)
 615{
 616    GtkDisplayState *s = opaque;
 617
 618    gd_update_caption(s);
 619}
 620
 621static void gd_mouse_mode_change(Notifier *notify, void *data)
 622{
 623    GtkDisplayState *s;
 624    int i;
 625
 626    s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
 627    /* release the grab at switching to absolute mode */
 628    if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
 629        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
 630                                       FALSE);
 631    }
 632    for (i = 0; i < s->nb_vcs; i++) {
 633        VirtualConsole *vc = &s->vc[i];
 634        gd_update_cursor(vc);
 635    }
 636}
 637
 638/** GTK Events **/
 639
 640static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
 641                                void *opaque)
 642{
 643    GtkDisplayState *s = opaque;
 644    int i;
 645
 646    if (!no_quit) {
 647        for (i = 0; i < s->nb_vcs; i++) {
 648            if (s->vc[i].type != GD_VC_GFX) {
 649                continue;
 650            }
 651            unregister_displaychangelistener(&s->vc[i].gfx.dcl);
 652        }
 653        qmp_quit(NULL);
 654        return FALSE;
 655    }
 656
 657    return TRUE;
 658}
 659
 660static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
 661{
 662    VirtualConsole *vc = opaque;
 663    GtkDisplayState *s = vc->s;
 664    int mx, my;
 665    int ww, wh;
 666    int fbw, fbh;
 667
 668    if (!gtk_widget_get_realized(widget)) {
 669        return FALSE;
 670    }
 671
 672    fbw = surface_width(vc->gfx.ds);
 673    fbh = surface_height(vc->gfx.ds);
 674
 675    gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
 676
 677    if (s->full_screen) {
 678        vc->gfx.scale_x = (double)ww / fbw;
 679        vc->gfx.scale_y = (double)wh / fbh;
 680    } else if (s->free_scale) {
 681        double sx, sy;
 682
 683        sx = (double)ww / fbw;
 684        sy = (double)wh / fbh;
 685
 686        vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
 687    }
 688
 689    fbw *= vc->gfx.scale_x;
 690    fbh *= vc->gfx.scale_y;
 691
 692    mx = my = 0;
 693    if (ww > fbw) {
 694        mx = (ww - fbw) / 2;
 695    }
 696    if (wh > fbh) {
 697        my = (wh - fbh) / 2;
 698    }
 699
 700    cairo_rectangle(cr, 0, 0, ww, wh);
 701
 702    /* Optionally cut out the inner area where the pixmap
 703       will be drawn. This avoids 'flashing' since we're
 704       not double-buffering. Note we're using the undocumented
 705       behaviour of drawing the rectangle from right to left
 706       to cut out the whole */
 707    cairo_rectangle(cr, mx + fbw, my,
 708                    -1 * fbw, fbh);
 709    cairo_fill(cr);
 710
 711    cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
 712    cairo_set_source_surface(cr, vc->gfx.surface,
 713                             mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
 714    cairo_paint(cr);
 715
 716    return TRUE;
 717}
 718
 719#if !GTK_CHECK_VERSION(3, 0, 0)
 720static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
 721                                void *opaque)
 722{
 723    cairo_t *cr;
 724    gboolean ret;
 725
 726    cr = gdk_cairo_create(gtk_widget_get_window(widget));
 727    cairo_rectangle(cr,
 728                    expose->area.x,
 729                    expose->area.y,
 730                    expose->area.width,
 731                    expose->area.height);
 732    cairo_clip(cr);
 733
 734    ret = gd_draw_event(widget, cr, opaque);
 735
 736    cairo_destroy(cr);
 737
 738    return ret;
 739}
 740#endif
 741
 742static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
 743                                void *opaque)
 744{
 745    VirtualConsole *vc = opaque;
 746    GtkDisplayState *s = vc->s;
 747    int x, y;
 748    int mx, my;
 749    int fbh, fbw;
 750    int ww, wh;
 751
 752    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
 753    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
 754
 755    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
 756                          &ww, &wh);
 757
 758    mx = my = 0;
 759    if (ww > fbw) {
 760        mx = (ww - fbw) / 2;
 761    }
 762    if (wh > fbh) {
 763        my = (wh - fbh) / 2;
 764    }
 765
 766    x = (motion->x - mx) / vc->gfx.scale_x;
 767    y = (motion->y - my) / vc->gfx.scale_y;
 768
 769    if (qemu_input_is_absolute()) {
 770        if (x < 0 || y < 0 ||
 771            x >= surface_width(vc->gfx.ds) ||
 772            y >= surface_height(vc->gfx.ds)) {
 773            return TRUE;
 774        }
 775        qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
 776                             surface_width(vc->gfx.ds));
 777        qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
 778                             surface_height(vc->gfx.ds));
 779        qemu_input_event_sync();
 780    } else if (s->last_set && s->ptr_owner == vc) {
 781        qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
 782        qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
 783        qemu_input_event_sync();
 784    }
 785    s->last_x = x;
 786    s->last_y = y;
 787    s->last_set = TRUE;
 788
 789    if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
 790        GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
 791        int x = (int)motion->x_root;
 792        int y = (int)motion->y_root;
 793
 794        /* In relative mode check to see if client pointer hit
 795         * one of the screen edges, and if so move it back by
 796         * 200 pixels. This is important because the pointer
 797         * in the server doesn't correspond 1-for-1, and so
 798         * may still be only half way across the screen. Without
 799         * this warp, the server pointer would thus appear to hit
 800         * an invisible wall */
 801        if (x == 0) {
 802            x += 200;
 803        }
 804        if (y == 0) {
 805            y += 200;
 806        }
 807        if (x == (gdk_screen_get_width(screen) - 1)) {
 808            x -= 200;
 809        }
 810        if (y == (gdk_screen_get_height(screen) - 1)) {
 811            y -= 200;
 812        }
 813
 814        if (x != (int)motion->x_root || y != (int)motion->y_root) {
 815#if GTK_CHECK_VERSION(3, 0, 0)
 816            GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
 817            gdk_device_warp(dev, screen, x, y);
 818#else
 819            GdkDisplay *display = gtk_widget_get_display(widget);
 820            gdk_display_warp_pointer(display, screen, x, y);
 821#endif
 822            s->last_set = FALSE;
 823            return FALSE;
 824        }
 825    }
 826    return TRUE;
 827}
 828
 829static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
 830                                void *opaque)
 831{
 832    VirtualConsole *vc = opaque;
 833    GtkDisplayState *s = vc->s;
 834    InputButton btn;
 835
 836    /* implicitly grab the input at the first click in the relative mode */
 837    if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
 838        !qemu_input_is_absolute() && s->ptr_owner != vc) {
 839        gd_ungrab_pointer(s);
 840        if (!vc->window) {
 841            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
 842                                           TRUE);
 843        } else {
 844            gd_grab_pointer(vc);
 845            gd_update_caption(s);
 846        }
 847        return TRUE;
 848    }
 849
 850    if (button->button == 1) {
 851        btn = INPUT_BUTTON_LEFT;
 852    } else if (button->button == 2) {
 853        btn = INPUT_BUTTON_MIDDLE;
 854    } else if (button->button == 3) {
 855        btn = INPUT_BUTTON_RIGHT;
 856    } else {
 857        return TRUE;
 858    }
 859
 860    qemu_input_queue_btn(vc->gfx.dcl.con, btn,
 861                         button->type == GDK_BUTTON_PRESS);
 862    qemu_input_event_sync();
 863    return TRUE;
 864}
 865
 866static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
 867                                void *opaque)
 868{
 869    VirtualConsole *vc = opaque;
 870    InputButton btn;
 871
 872    if (scroll->direction == GDK_SCROLL_UP) {
 873        btn = INPUT_BUTTON_WHEEL_UP;
 874    } else if (scroll->direction == GDK_SCROLL_DOWN) {
 875        btn = INPUT_BUTTON_WHEEL_DOWN;
 876    } else {
 877        return TRUE;
 878    }
 879
 880    qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
 881    qemu_input_event_sync();
 882    qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
 883    qemu_input_event_sync();
 884    return TRUE;
 885}
 886
 887static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
 888{
 889    int qemu_keycode;
 890
 891#ifdef GDK_WINDOWING_WIN32
 892    if (GDK_IS_WIN32_DISPLAY(dpy)) {
 893        qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
 894        switch (qemu_keycode) {
 895        case 103:   /* alt gr */
 896            qemu_keycode = 56 | SCANCODE_GREY;
 897            break;
 898        }
 899        return qemu_keycode;
 900    }
 901#endif
 902
 903    if (gdk_keycode < 9) {
 904        qemu_keycode = 0;
 905    } else if (gdk_keycode < 97) {
 906        qemu_keycode = gdk_keycode - 8;
 907#ifdef GDK_WINDOWING_X11
 908    } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
 909        if (s->has_evdev) {
 910            qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
 911        } else {
 912            qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
 913        }
 914#endif
 915    } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
 916        qemu_keycode = 0x70;
 917    } else if (gdk_keycode == 211) { /* backslash */
 918        qemu_keycode = 0x73;
 919    } else {
 920        qemu_keycode = 0;
 921    }
 922
 923    return qemu_keycode;
 924}
 925
 926static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
 927{
 928    VirtualConsole *vc = opaque;
 929    GtkDisplayState *s = vc->s;
 930    int gdk_keycode = key->hardware_keycode;
 931    int qemu_keycode;
 932    int i;
 933
 934    qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
 935                                  gdk_keycode);
 936
 937    trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
 938                       (key->type == GDK_KEY_PRESS) ? "down" : "up");
 939
 940    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
 941        if (qemu_keycode == modifier_keycode[i]) {
 942            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
 943        }
 944    }
 945
 946    qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
 947                                     key->type == GDK_KEY_PRESS);
 948
 949    return TRUE;
 950}
 951
 952static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
 953{
 954    if (event->type == GDK_MOTION_NOTIFY) {
 955        return gd_motion_event(widget, &event->motion, opaque);
 956    }
 957    return FALSE;
 958}
 959
 960/** Window Menu Actions **/
 961
 962static void gd_menu_pause(GtkMenuItem *item, void *opaque)
 963{
 964    GtkDisplayState *s = opaque;
 965
 966    if (s->external_pause_update) {
 967        return;
 968    }
 969    if (runstate_is_running()) {
 970        qmp_stop(NULL);
 971    } else {
 972        qmp_cont(NULL);
 973    }
 974}
 975
 976static void gd_menu_reset(GtkMenuItem *item, void *opaque)
 977{
 978    qmp_system_reset(NULL);
 979}
 980
 981static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
 982{
 983    qmp_system_powerdown(NULL);
 984}
 985
 986static void gd_menu_quit(GtkMenuItem *item, void *opaque)
 987{
 988    qmp_quit(NULL);
 989}
 990
 991static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
 992{
 993    GtkDisplayState *s = opaque;
 994    VirtualConsole *vc = gd_vc_find_by_menu(s);
 995    GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
 996    GtkWidget *child;
 997    gint page;
 998
 999    gtk_release_modifiers(s);
1000    if (vc) {
1001        page = gtk_notebook_page_num(nb, vc->tab_item);
1002        gtk_notebook_set_current_page(nb, page);
1003        child = gtk_notebook_get_nth_page(nb, page);
1004        gtk_widget_grab_focus(child);
1005    }
1006}
1007
1008static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
1009{
1010    GtkDisplayState *s = opaque;
1011    VirtualConsole *vc = gd_vc_find_current(s);
1012
1013    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
1014        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
1015    } else {
1016        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1017    }
1018    gd_update_windowsize(vc);
1019}
1020
1021static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
1022                                    void *opaque)
1023{
1024    VirtualConsole *vc = opaque;
1025    GtkDisplayState *s = vc->s;
1026
1027    gtk_widget_set_sensitive(vc->menu_item, true);
1028    gtk_widget_reparent(vc->tab_item, s->notebook);
1029    gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
1030                                    vc->tab_item, vc->label);
1031    gtk_widget_destroy(vc->window);
1032    vc->window = NULL;
1033    return TRUE;
1034}
1035
1036static gboolean gd_win_grab(void *opaque)
1037{
1038    VirtualConsole *vc = opaque;
1039
1040    fprintf(stderr, "%s: %s\n", __func__, vc->label);
1041    if (vc->s->ptr_owner) {
1042        gd_ungrab_pointer(vc->s);
1043    } else {
1044        gd_grab_pointer(vc);
1045    }
1046    gd_update_caption(vc->s);
1047    return TRUE;
1048}
1049
1050static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
1051{
1052    GtkDisplayState *s = opaque;
1053    VirtualConsole *vc = gd_vc_find_current(s);
1054
1055    if (vc->type == GD_VC_GFX) {
1056        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1057                                       FALSE);
1058    }
1059    if (!vc->window) {
1060        gtk_widget_set_sensitive(vc->menu_item, false);
1061        vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1062        gtk_widget_reparent(vc->tab_item, vc->window);
1063
1064        g_signal_connect(vc->window, "delete-event",
1065                         G_CALLBACK(gd_tab_window_close), vc);
1066        gtk_widget_show_all(vc->window);
1067
1068        GtkAccelGroup *ag = gtk_accel_group_new();
1069        gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1070
1071        GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab), vc, NULL);
1072        gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
1073
1074        gd_update_geometry_hints(vc);
1075        gd_update_caption(s);
1076    }
1077}
1078
1079static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
1080{
1081    GtkDisplayState *s = opaque;
1082    VirtualConsole *vc = gd_vc_find_current(s);
1083
1084    if (!s->full_screen) {
1085        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1086        gtk_widget_set_size_request(s->menu_bar, 0, 0);
1087        if (vc->type == GD_VC_GFX) {
1088            gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1089            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1090                                           TRUE);
1091        }
1092        gtk_window_fullscreen(GTK_WINDOW(s->window));
1093        s->full_screen = TRUE;
1094    } else {
1095        gtk_window_unfullscreen(GTK_WINDOW(s->window));
1096        gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1097        gtk_widget_set_size_request(s->menu_bar, -1, -1);
1098        s->full_screen = FALSE;
1099        if (vc->type == GD_VC_GFX) {
1100            vc->gfx.scale_x = 1.0;
1101            vc->gfx.scale_y = 1.0;
1102            gd_update_windowsize(vc);
1103            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1104                                           FALSE);
1105        }
1106    }
1107
1108    gd_update_cursor(vc);
1109}
1110
1111static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
1112{
1113    GtkDisplayState *s = opaque;
1114    VirtualConsole *vc = gd_vc_find_current(s);
1115
1116    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1117                                   FALSE);
1118
1119    vc->gfx.scale_x += VC_SCALE_STEP;
1120    vc->gfx.scale_y += VC_SCALE_STEP;
1121
1122    gd_update_windowsize(vc);
1123}
1124
1125static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
1126{
1127    GtkDisplayState *s = opaque;
1128    VirtualConsole *vc = gd_vc_find_current(s);
1129
1130    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
1131                                   FALSE);
1132
1133    vc->gfx.scale_x -= VC_SCALE_STEP;
1134    vc->gfx.scale_y -= VC_SCALE_STEP;
1135
1136    vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
1137    vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1138
1139    gd_update_windowsize(vc);
1140}
1141
1142static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
1143{
1144    GtkDisplayState *s = opaque;
1145    VirtualConsole *vc = gd_vc_find_current(s);
1146
1147    vc->gfx.scale_x = 1.0;
1148    vc->gfx.scale_y = 1.0;
1149
1150    gd_update_windowsize(vc);
1151}
1152
1153static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
1154{
1155    GtkDisplayState *s = opaque;
1156    VirtualConsole *vc = gd_vc_find_current(s);
1157
1158    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
1159        s->free_scale = TRUE;
1160    } else {
1161        s->free_scale = FALSE;
1162        vc->gfx.scale_x = 1.0;
1163        vc->gfx.scale_y = 1.0;
1164    }
1165
1166    gd_update_windowsize(vc);
1167    gd_update_full_redraw(vc);
1168}
1169
1170#if GTK_CHECK_VERSION(3, 0, 0)
1171static void gd_grab_devices(VirtualConsole *vc, bool grab,
1172                            GdkInputSource source, GdkEventMask mask,
1173                            GdkCursor *cursor)
1174{
1175    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1176    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1177    GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
1178    GList *tmp = devs;
1179
1180    for (tmp = devs; tmp; tmp = tmp->next) {
1181        GdkDevice *dev = tmp->data;
1182        if (gdk_device_get_source(dev) != source) {
1183            continue;
1184        }
1185        if (grab) {
1186            GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
1187            gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
1188                            mask, cursor, GDK_CURRENT_TIME);
1189        } else {
1190            gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1191        }
1192    }
1193    g_list_free(devs);
1194}
1195#endif
1196
1197static void gd_grab_keyboard(VirtualConsole *vc)
1198{
1199#if GTK_CHECK_VERSION(3, 0, 0)
1200    gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
1201                   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1202                   NULL);
1203#else
1204    gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1205                      FALSE,
1206                      GDK_CURRENT_TIME);
1207#endif
1208    vc->s->kbd_owner = vc;
1209    trace_gd_grab(vc->label, "kbd", true);
1210}
1211
1212static void gd_ungrab_keyboard(GtkDisplayState *s)
1213{
1214    VirtualConsole *vc = s->kbd_owner;
1215
1216    if (vc == NULL) {
1217        return;
1218    }
1219    s->kbd_owner = NULL;
1220
1221#if GTK_CHECK_VERSION(3, 0, 0)
1222    gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1223#else
1224    gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1225#endif
1226    trace_gd_grab(vc->label, "kbd", false);
1227}
1228
1229static void gd_grab_pointer(VirtualConsole *vc)
1230{
1231    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1232#if GTK_CHECK_VERSION(3, 0, 0)
1233    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1234    gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
1235                    GDK_POINTER_MOTION_MASK |
1236                    GDK_BUTTON_PRESS_MASK |
1237                    GDK_BUTTON_RELEASE_MASK |
1238                    GDK_BUTTON_MOTION_MASK |
1239                    GDK_SCROLL_MASK,
1240                    vc->s->null_cursor);
1241    gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1242                            NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1243#else
1244    gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1245                     FALSE, /* All events to come to our window directly */
1246                     GDK_POINTER_MOTION_MASK |
1247                     GDK_BUTTON_PRESS_MASK |
1248                     GDK_BUTTON_RELEASE_MASK |
1249                     GDK_BUTTON_MOTION_MASK |
1250                     GDK_SCROLL_MASK,
1251                     NULL, /* Allow cursor to move over entire desktop */
1252                     vc->s->null_cursor,
1253                     GDK_CURRENT_TIME);
1254    gdk_display_get_pointer(display, NULL,
1255                            &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1256#endif
1257    vc->s->ptr_owner = vc;
1258    trace_gd_grab(vc->label, "ptr", true);
1259}
1260
1261static void gd_ungrab_pointer(GtkDisplayState *s)
1262{
1263    VirtualConsole *vc = s->ptr_owner;
1264
1265    if (vc == NULL) {
1266        return;
1267    }
1268    s->ptr_owner = NULL;
1269
1270    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1271#if GTK_CHECK_VERSION(3, 0, 0)
1272    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1273    gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1274    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1275                    gtk_widget_get_screen(vc->gfx.drawing_area),
1276                    vc->s->grab_x_root, vc->s->grab_y_root);
1277#else
1278    gdk_pointer_ungrab(GDK_CURRENT_TIME);
1279    gdk_display_warp_pointer(display,
1280                             gtk_widget_get_screen(vc->gfx.drawing_area),
1281                             vc->s->grab_x_root, vc->s->grab_y_root);
1282#endif
1283    trace_gd_grab(vc->label, "ptr", false);
1284}
1285
1286static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
1287{
1288    GtkDisplayState *s = opaque;
1289    VirtualConsole *vc = gd_vc_find_current(s);
1290
1291    if (gd_is_grab_active(s)) {
1292        if (!gd_grab_on_hover(s)) {
1293            gd_grab_keyboard(vc);
1294        }
1295        gd_grab_pointer(vc);
1296    } else {
1297        gd_ungrab_keyboard(s);
1298        gd_ungrab_pointer(s);
1299    }
1300
1301    gd_update_caption(s);
1302    gd_update_cursor(vc);
1303}
1304
1305static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
1306                           gpointer data)
1307{
1308    GtkDisplayState *s = data;
1309    VirtualConsole *vc;
1310    gboolean on_vga;
1311
1312    if (!gtk_widget_get_realized(s->notebook)) {
1313        return;
1314    }
1315
1316#ifdef VTE_RESIZE_HACK
1317    vc = gd_vc_find_current(s);
1318    if (vc && vc->type == GD_VC_VTE) {
1319        gtk_widget_hide(vc->vte.terminal);
1320    }
1321#endif
1322    vc = gd_vc_find_by_page(s, arg2);
1323    if (!vc) {
1324        return;
1325    }
1326#ifdef VTE_RESIZE_HACK
1327    if (vc->type == GD_VC_VTE) {
1328        gtk_widget_show(vc->vte.terminal);
1329    }
1330#endif
1331    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
1332                                   TRUE);
1333    on_vga = (vc->type == GD_VC_GFX);
1334    if (!on_vga) {
1335        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1336                                       FALSE);
1337    } else if (s->full_screen) {
1338        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
1339                                       TRUE);
1340    }
1341    gtk_widget_set_sensitive(s->grab_item, on_vga);
1342
1343    gd_update_windowsize(vc);
1344    gd_update_cursor(vc);
1345}
1346
1347static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
1348                               gpointer opaque)
1349{
1350    VirtualConsole *vc = opaque;
1351    GtkDisplayState *s = vc->s;
1352
1353    if (gd_grab_on_hover(s)) {
1354        gd_ungrab_keyboard(s);
1355        gd_grab_keyboard(vc);
1356        gd_update_caption(s);
1357    }
1358    return TRUE;
1359}
1360
1361static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
1362                               gpointer opaque)
1363{
1364    VirtualConsole *vc = opaque;
1365    GtkDisplayState *s = vc->s;
1366
1367    if (gd_grab_on_hover(s)) {
1368        gd_ungrab_keyboard(s);
1369        gd_update_caption(s);
1370    }
1371    return TRUE;
1372}
1373
1374static gboolean gd_focus_out_event(GtkWidget *widget,
1375                                   GdkEventCrossing *crossing, gpointer opaque)
1376{
1377    VirtualConsole *vc = opaque;
1378    GtkDisplayState *s = vc->s;
1379
1380    gtk_release_modifiers(s);
1381    return TRUE;
1382}
1383
1384/** Virtual Console Callbacks **/
1385
1386static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1387                               int idx, GSList *group, GtkWidget *view_menu)
1388{
1389    char path[32];
1390
1391    snprintf(path, sizeof(path), "<QEMU>/View/VC%d", idx);
1392
1393    vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1394    group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1395    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
1396    gtk_accel_map_add_entry(path, GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
1397
1398    g_signal_connect(vc->menu_item, "activate",
1399                     G_CALLBACK(gd_menu_switch_vc), s);
1400    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1401
1402    return group;
1403}
1404
1405#if defined(CONFIG_VTE)
1406static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
1407{
1408    VirtualConsole *vc = opaque;
1409
1410    if (gtk_adjustment_get_upper(adjustment) >
1411        gtk_adjustment_get_page_size(adjustment)) {
1412        gtk_widget_show(vc->vte.scrollbar);
1413    } else {
1414        gtk_widget_hide(vc->vte.scrollbar);
1415    }
1416}
1417
1418static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1419{
1420    VirtualConsole *vc = chr->opaque;
1421
1422    vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
1423    return len;
1424}
1425
1426static int nb_vcs;
1427static CharDriverState *vcs[MAX_VCS];
1428
1429static CharDriverState *gd_vc_handler(ChardevVC *unused)
1430{
1431    CharDriverState *chr;
1432
1433    chr = g_malloc0(sizeof(*chr));
1434    chr->chr_write = gd_vc_chr_write;
1435    /* defer OPENED events until our vc is fully initialized */
1436    chr->explicit_be_open = true;
1437
1438    vcs[nb_vcs++] = chr;
1439
1440    return chr;
1441}
1442
1443static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
1444                         gpointer user_data)
1445{
1446    VirtualConsole *vc = user_data;
1447
1448    qemu_chr_be_write(vc->vte.chr, (uint8_t  *)text, (unsigned int)size);
1449    return TRUE;
1450}
1451
1452static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
1453                              CharDriverState *chr, int idx,
1454                              GSList *group, GtkWidget *view_menu)
1455{
1456    char buffer[32];
1457    GtkWidget *box;
1458    GtkWidget *scrollbar;
1459    GtkAdjustment *vadjustment;
1460
1461    vc->s = s;
1462    vc->vte.chr = chr;
1463
1464    snprintf(buffer, sizeof(buffer), "vc%d", idx);
1465    vc->label = g_strdup_printf("%s", vc->vte.chr->label
1466                                ? vc->vte.chr->label : buffer);
1467    group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1468
1469    vc->vte.terminal = vte_terminal_new();
1470    g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1471
1472    vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
1473    vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
1474                          VC_TERM_X_MIN, VC_TERM_Y_MIN);
1475
1476#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
1477    vadjustment = gtk_scrollable_get_vadjustment
1478        (GTK_SCROLLABLE(vc->vte.terminal));
1479#else
1480    vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1481#endif
1482
1483#if GTK_CHECK_VERSION(3, 0, 0)
1484    box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1485    scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
1486#else
1487    box = gtk_hbox_new(false, 2);
1488    scrollbar = gtk_vscrollbar_new(vadjustment);
1489#endif
1490
1491    gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1492    gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);
1493
1494    vc->vte.chr->opaque = vc;
1495    vc->vte.box = box;
1496    vc->vte.scrollbar = scrollbar;
1497
1498    g_signal_connect(vadjustment, "changed",
1499                     G_CALLBACK(gd_vc_adjustment_changed), vc);
1500
1501    vc->type = GD_VC_VTE;
1502    vc->tab_item = box;
1503    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1504                             gtk_label_new(vc->label));
1505
1506    qemu_chr_be_generic_open(vc->vte.chr);
1507    if (vc->vte.chr->init) {
1508        vc->vte.chr->init(vc->vte.chr);
1509    }
1510
1511    return group;
1512}
1513
1514static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1515                        GtkWidget *view_menu)
1516{
1517    int i;
1518
1519    for (i = 0; i < nb_vcs; i++) {
1520        VirtualConsole *vc = &s->vc[s->nb_vcs];
1521        group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1522        s->nb_vcs++;
1523    }
1524}
1525#endif /* CONFIG_VTE */
1526
1527/** Window Creation **/
1528
1529static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
1530{
1531#if GTK_CHECK_VERSION(3, 0, 0)
1532    g_signal_connect(vc->gfx.drawing_area, "draw",
1533                     G_CALLBACK(gd_draw_event), vc);
1534#else
1535    g_signal_connect(vc->gfx.drawing_area, "expose-event",
1536                     G_CALLBACK(gd_expose_event), vc);
1537#endif
1538    g_signal_connect(vc->gfx.drawing_area, "event",
1539                     G_CALLBACK(gd_event), vc);
1540    g_signal_connect(vc->gfx.drawing_area, "button-press-event",
1541                     G_CALLBACK(gd_button_event), vc);
1542    g_signal_connect(vc->gfx.drawing_area, "button-release-event",
1543                     G_CALLBACK(gd_button_event), vc);
1544    g_signal_connect(vc->gfx.drawing_area, "scroll-event",
1545                     G_CALLBACK(gd_scroll_event), vc);
1546    g_signal_connect(vc->gfx.drawing_area, "key-press-event",
1547                     G_CALLBACK(gd_key_event), vc);
1548    g_signal_connect(vc->gfx.drawing_area, "key-release-event",
1549                     G_CALLBACK(gd_key_event), vc);
1550
1551    g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
1552                     G_CALLBACK(gd_enter_event), vc);
1553    g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
1554                     G_CALLBACK(gd_leave_event), vc);
1555    g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
1556                     G_CALLBACK(gd_focus_out_event), vc);
1557}
1558
1559static void gd_connect_signals(GtkDisplayState *s)
1560{
1561    g_signal_connect(s->show_tabs_item, "activate",
1562                     G_CALLBACK(gd_menu_show_tabs), s);
1563    g_signal_connect(s->untabify_item, "activate",
1564                     G_CALLBACK(gd_menu_untabify), s);
1565
1566    g_signal_connect(s->window, "delete-event",
1567                     G_CALLBACK(gd_window_close), s);
1568
1569    g_signal_connect(s->pause_item, "activate",
1570                     G_CALLBACK(gd_menu_pause), s);
1571    g_signal_connect(s->reset_item, "activate",
1572                     G_CALLBACK(gd_menu_reset), s);
1573    g_signal_connect(s->powerdown_item, "activate",
1574                     G_CALLBACK(gd_menu_powerdown), s);
1575    g_signal_connect(s->quit_item, "activate",
1576                     G_CALLBACK(gd_menu_quit), s);
1577    g_signal_connect(s->full_screen_item, "activate",
1578                     G_CALLBACK(gd_menu_full_screen), s);
1579    g_signal_connect(s->zoom_in_item, "activate",
1580                     G_CALLBACK(gd_menu_zoom_in), s);
1581    g_signal_connect(s->zoom_out_item, "activate",
1582                     G_CALLBACK(gd_menu_zoom_out), s);
1583    g_signal_connect(s->zoom_fixed_item, "activate",
1584                     G_CALLBACK(gd_menu_zoom_fixed), s);
1585    g_signal_connect(s->zoom_fit_item, "activate",
1586                     G_CALLBACK(gd_menu_zoom_fit), s);
1587    g_signal_connect(s->grab_item, "activate",
1588                     G_CALLBACK(gd_menu_grab_input), s);
1589    g_signal_connect(s->notebook, "switch-page",
1590                     G_CALLBACK(gd_change_page), s);
1591}
1592
1593static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
1594{
1595    GtkWidget *machine_menu;
1596    GtkWidget *separator;
1597
1598    machine_menu = gtk_menu_new();
1599    gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1600
1601    s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
1602    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1603
1604    separator = gtk_separator_menu_item_new();
1605    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1606
1607    s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
1608    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1609
1610    s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
1611    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1612
1613    separator = gtk_separator_menu_item_new();
1614    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1615
1616    s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
1617    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1618                                 "<QEMU>/Machine/Quit");
1619    gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1620                            GDK_KEY_q, HOTKEY_MODIFIERS);
1621    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
1622
1623    return machine_menu;
1624}
1625
1626static const DisplayChangeListenerOps dcl_ops = {
1627    .dpy_name          = "gtk",
1628    .dpy_gfx_update    = gd_update,
1629    .dpy_gfx_switch    = gd_switch,
1630    .dpy_refresh       = gd_refresh,
1631    .dpy_mouse_set     = gd_mouse_set,
1632    .dpy_cursor_define = gd_cursor_define,
1633};
1634
1635static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1636                              QemuConsole *con, int idx,
1637                              GSList *group, GtkWidget *view_menu)
1638{
1639    Error *local_err = NULL;
1640    Object *obj;
1641
1642    obj = object_property_get_link(OBJECT(con), "device", &local_err);
1643    if (obj) {
1644        vc->label = g_strdup_printf("%s", object_get_typename(obj));
1645    } else {
1646        vc->label = g_strdup_printf("VGA");
1647    }
1648
1649    vc->s = s;
1650    vc->gfx.scale_x = 1.0;
1651    vc->gfx.scale_y = 1.0;
1652
1653    vc->gfx.drawing_area = gtk_drawing_area_new();
1654    gtk_widget_add_events(vc->gfx.drawing_area,
1655                          GDK_POINTER_MOTION_MASK |
1656                          GDK_BUTTON_PRESS_MASK |
1657                          GDK_BUTTON_RELEASE_MASK |
1658                          GDK_BUTTON_MOTION_MASK |
1659                          GDK_ENTER_NOTIFY_MASK |
1660                          GDK_LEAVE_NOTIFY_MASK |
1661                          GDK_SCROLL_MASK |
1662                          GDK_KEY_PRESS_MASK);
1663    gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
1664    gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);
1665
1666    vc->type = GD_VC_GFX;
1667    vc->tab_item = vc->gfx.drawing_area;
1668    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
1669                             vc->tab_item, gtk_label_new(vc->label));
1670    gd_connect_vc_gfx_signals(vc);
1671
1672    group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1673
1674    vc->gfx.dcl.ops = &dcl_ops;
1675    vc->gfx.dcl.con = con;
1676    register_displaychangelistener(&vc->gfx.dcl);
1677
1678    return group;
1679}
1680
1681static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
1682{
1683    GSList *group = NULL;
1684    GtkWidget *view_menu;
1685    GtkWidget *separator;
1686    QemuConsole *con;
1687    int vc;
1688
1689    view_menu = gtk_menu_new();
1690    gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
1691
1692    s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1693    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
1694                                 "<QEMU>/View/Full Screen");
1695    gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
1696                            HOTKEY_MODIFIERS);
1697    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1698
1699    separator = gtk_separator_menu_item_new();
1700    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1701
1702    s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1703    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
1704                                 "<QEMU>/View/Zoom In");
1705    gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
1706                            HOTKEY_MODIFIERS);
1707    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1708
1709    s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1710    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
1711                                 "<QEMU>/View/Zoom Out");
1712    gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
1713                            HOTKEY_MODIFIERS);
1714    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1715
1716    s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1717    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
1718                                 "<QEMU>/View/Zoom Fixed");
1719    gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
1720                            HOTKEY_MODIFIERS);
1721    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1722
1723    s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
1724    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1725
1726    separator = gtk_separator_menu_item_new();
1727    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1728
1729    s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
1730    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1731
1732    s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1733    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
1734                                 "<QEMU>/View/Grab Input");
1735    gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
1736                            HOTKEY_MODIFIERS);
1737    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1738
1739    separator = gtk_separator_menu_item_new();
1740    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1741
1742    /* gfx */
1743    for (vc = 0;; vc++) {
1744        con = qemu_console_lookup_by_index(vc);
1745        if (!con || !qemu_console_is_graphic(con)) {
1746            break;
1747        }
1748        group = gd_vc_gfx_init(s, &s->vc[vc], con,
1749                               vc, group, view_menu);
1750        s->nb_vcs++;
1751    }
1752
1753#if defined(CONFIG_VTE)
1754    /* vte */
1755    gd_vcs_init(s, group, view_menu);
1756#endif
1757
1758    separator = gtk_separator_menu_item_new();
1759    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1760
1761    s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
1762    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
1763
1764    s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
1765    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
1766
1767    return view_menu;
1768}
1769
1770static void gd_create_menus(GtkDisplayState *s)
1771{
1772    GtkAccelGroup *accel_group;
1773
1774    accel_group = gtk_accel_group_new();
1775    s->machine_menu = gd_create_menu_machine(s, accel_group);
1776    s->view_menu = gd_create_menu_view(s, accel_group);
1777
1778    s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1779    gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
1780                              s->machine_menu);
1781    gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
1782
1783    s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
1784    gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
1785    gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
1786
1787    g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
1788    gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
1789    s->accel_group = accel_group;
1790}
1791
1792static void gd_set_keycode_type(GtkDisplayState *s)
1793{
1794#ifdef GDK_WINDOWING_X11
1795    GdkDisplay *display = gtk_widget_get_display(s->window);
1796    if (GDK_IS_X11_DISPLAY(display)) {
1797        Display *x11_display = gdk_x11_display_get_xdisplay(display);
1798        XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
1799                                         XkbUseCoreKbd);
1800        char *keycodes = NULL;
1801
1802        if (desc && desc->names) {
1803            keycodes = XGetAtomName(x11_display, desc->names->keycodes);
1804        }
1805        if (keycodes == NULL) {
1806            fprintf(stderr, "could not lookup keycode name\n");
1807        } else if (strstart(keycodes, "evdev", NULL)) {
1808            s->has_evdev = true;
1809        } else if (!strstart(keycodes, "xfree86", NULL)) {
1810            fprintf(stderr, "unknown keycodes `%s', please report to "
1811                    "qemu-devel@nongnu.org\n", keycodes);
1812        }
1813    }
1814#endif
1815}
1816
1817void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
1818{
1819    GtkDisplayState *s = g_malloc0(sizeof(*s));
1820    char *filename;
1821
1822    gtk_init(NULL, NULL);
1823
1824    s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1825#if GTK_CHECK_VERSION(3, 2, 0)
1826    s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1827#else
1828    s->vbox = gtk_vbox_new(FALSE, 0);
1829#endif
1830    s->notebook = gtk_notebook_new();
1831    s->menu_bar = gtk_menu_bar_new();
1832
1833    s->free_scale = FALSE;
1834
1835    setlocale(LC_ALL, "");
1836    bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
1837    textdomain("qemu");
1838
1839    s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
1840
1841    s->mouse_mode_notifier.notify = gd_mouse_mode_change;
1842    qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
1843    qemu_add_vm_change_state_handler(gd_change_runstate, s);
1844
1845    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
1846    if (filename) {
1847        GError *error = NULL;
1848        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1849        if (pixbuf) {
1850            gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
1851        } else {
1852            g_error_free(error);
1853        }
1854        g_free(filename);
1855    }
1856
1857    gd_create_menus(s);
1858
1859    gd_connect_signals(s);
1860
1861    gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1862    gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);
1863
1864    gd_update_caption(s);
1865
1866    gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
1867    gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);
1868
1869    gtk_container_add(GTK_CONTAINER(s->window), s->vbox);
1870
1871    gtk_widget_show_all(s->window);
1872
1873#ifdef VTE_RESIZE_HACK
1874    {
1875        VirtualConsole *cur = gd_vc_find_current(s);
1876        int i;
1877
1878        for (i = 0; i < s->nb_vcs; i++) {
1879            VirtualConsole *vc = &s->vc[i];
1880            if (vc && vc->type == GD_VC_VTE && vc != cur) {
1881                gtk_widget_hide(vc->vte.terminal);
1882            }
1883        }
1884        gd_update_windowsize(cur);
1885    }
1886#endif
1887
1888    if (full_screen) {
1889        gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
1890    }
1891    if (grab_on_hover) {
1892        gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
1893    }
1894
1895    gd_set_keycode_type(s);
1896}
1897
1898void early_gtk_display_init(void)
1899{
1900#if defined(CONFIG_VTE)
1901    register_vc_handler(gd_vc_handler);
1902#endif
1903}
1904