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