gtk.c 45.5 KB
Newer Older
A
Anthony Liguori 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * GTK UI
 *
 * Copyright IBM, Corp. 2012
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 * Portions from gtk-vnc:
 *
 * GTK VNC Widget
 *
 * Copyright (C) 2006  Anthony Liguori <anthony@codemonkey.ws>
 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

34 35 36
#define GETTEXT_PACKAGE "qemu"
#define LOCALEDIR "po"

37 38 39 40
#ifdef _WIN32
# define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
#endif

K
Kevin Wolf 已提交
41 42 43 44
#include "qemu-common.h"

#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
/* Work around an -Wstrict-prototypes warning in GTK headers */
45
#pragma GCC diagnostic push
K
Kevin Wolf 已提交
46 47
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#endif
A
Anthony Liguori 已提交
48
#include <gtk/gtk.h>
K
Kevin Wolf 已提交
49
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
50
#pragma GCC diagnostic pop
K
Kevin Wolf 已提交
51 52 53
#endif


A
Anthony Liguori 已提交
54
#include <gdk/gdkkeysyms.h>
55
#include <glib/gi18n.h>
56
#include <locale.h>
A
Anthony Liguori 已提交
57 58 59
#include <vte/vte.h>
#include <math.h>

60
#include "trace.h"
A
Anthony Liguori 已提交
61
#include "ui/console.h"
62
#include "ui/input.h"
A
Anthony Liguori 已提交
63 64 65 66
#include "sysemu/sysemu.h"
#include "qmp-commands.h"
#include "x_keymap.h"
#include "keymaps.h"
67
#include "sysemu/char.h"
A
Anthony Liguori 已提交
68

69 70
#define MAX_VCS 10

71 72 73 74 75 76 77 78 79 80

/* Compatibility define to let us build on both Gtk2 and Gtk3 */
#if GTK_CHECK_VERSION(3, 0, 0)
static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
{
    *ww = gdk_window_get_width(w);
    *wh = gdk_window_get_height(w);
}
#endif

81 82 83 84
#if !GTK_CHECK_VERSION(2, 20, 0)
#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
#endif

85 86 87 88 89 90 91 92 93
#ifndef GDK_KEY_0
#define GDK_KEY_0 GDK_0
#define GDK_KEY_1 GDK_1
#define GDK_KEY_2 GDK_2
#define GDK_KEY_f GDK_f
#define GDK_KEY_g GDK_g
#define GDK_KEY_plus GDK_plus
#define GDK_KEY_minus GDK_minus
#endif
94

J
Jan Kiszka 已提交
95 96 97 98
#define HOTKEY_MODIFIERS        (GDK_CONTROL_MASK | GDK_MOD1_MASK)
#define IGNORE_MODIFIER_MASK \
    (GDK_MODIFIER_MASK & ~(GDK_LOCK_MASK | GDK_MOD2_MASK))

99 100 101 102 103
static const int modifier_keycode[] = {
    /* shift, control, alt keys, meta keys, both left & right */
    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
};

A
Anthony Liguori 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
typedef struct VirtualConsole
{
    GtkWidget *menu_item;
    GtkWidget *terminal;
    GtkWidget *scrolled_window;
    CharDriverState *chr;
    int fd;
} VirtualConsole;

typedef struct GtkDisplayState
{
    GtkWidget *window;

    GtkWidget *menu_bar;

119 120
    GtkAccelGroup *accel_group;

121 122 123 124 125
    GtkWidget *machine_menu_item;
    GtkWidget *machine_menu;
    GtkWidget *pause_item;
    GtkWidget *reset_item;
    GtkWidget *powerdown_item;
A
Anthony Liguori 已提交
126 127 128 129
    GtkWidget *quit_item;

    GtkWidget *view_menu_item;
    GtkWidget *view_menu;
130 131 132 133 134
    GtkWidget *full_screen_item;
    GtkWidget *zoom_in_item;
    GtkWidget *zoom_out_item;
    GtkWidget *zoom_fixed_item;
    GtkWidget *zoom_fit_item;
135 136
    GtkWidget *grab_item;
    GtkWidget *grab_on_hover_item;
A
Anthony Liguori 已提交
137 138
    GtkWidget *vga_item;

139 140 141
    int nb_vcs;
    VirtualConsole vc[MAX_VCS];

A
Anthony Liguori 已提交
142 143 144 145 146 147
    GtkWidget *show_tabs_item;

    GtkWidget *vbox;
    GtkWidget *notebook;
    GtkWidget *drawing_area;
    cairo_surface_t *surface;
148
    pixman_image_t *convert;
A
Anthony Liguori 已提交
149
    DisplayChangeListener dcl;
G
Gerd Hoffmann 已提交
150
    DisplaySurface *ds;
A
Anthony Liguori 已提交
151 152 153 154 155 156
    int button_mask;
    int last_x;
    int last_y;

    double scale_x;
    double scale_y;
157
    gboolean full_screen;
A
Anthony Liguori 已提交
158 159 160

    GdkCursor *null_cursor;
    Notifier mouse_mode_notifier;
161
    gboolean free_scale;
162 163

    bool external_pause_update;
164 165

    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
A
Anthony Liguori 已提交
166 167 168 169 170 171
} GtkDisplayState;

static GtkDisplayState *global_state;

/** Utility Functions **/

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static bool gd_is_grab_active(GtkDisplayState *s)
{
    return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
}

static bool gd_grab_on_hover(GtkDisplayState *s)
{
    return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
}

static bool gd_on_vga(GtkDisplayState *s)
{
    return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
}

A
Anthony Liguori 已提交
187 188 189 190 191 192 193
static void gd_update_cursor(GtkDisplayState *s, gboolean override)
{
    GdkWindow *window;
    bool on_vga;

    window = gtk_widget_get_window(GTK_WIDGET(s->drawing_area));

194
    on_vga = gd_on_vga(s);
A
Anthony Liguori 已提交
195

196
    if ((override || on_vga) &&
197
        (s->full_screen || qemu_input_is_absolute() || gd_is_grab_active(s))) {
A
Anthony Liguori 已提交
198 199 200 201 202 203 204 205 206 207
        gdk_window_set_cursor(window, s->null_cursor);
    } else {
        gdk_window_set_cursor(window, NULL);
    }
}

static void gd_update_caption(GtkDisplayState *s)
{
    const char *status = "";
    gchar *title;
208
    const char *grab = "";
209
    bool is_paused = !runstate_is_running();
210 211

    if (gd_is_grab_active(s)) {
212
        grab = _(" - Press Ctrl+Alt+G to release grab");
213
    }
A
Anthony Liguori 已提交
214

215
    if (is_paused) {
216
        status = _(" [Paused]");
A
Anthony Liguori 已提交
217
    }
218 219 220 221
    s->external_pause_update = true;
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
                                   is_paused);
    s->external_pause_update = false;
A
Anthony Liguori 已提交
222 223

    if (qemu_name) {
224
        title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
A
Anthony Liguori 已提交
225
    } else {
226
        title = g_strdup_printf("QEMU%s%s", status, grab);
A
Anthony Liguori 已提交
227 228 229 230 231 232 233
    }

    gtk_window_set_title(GTK_WINDOW(s->window), title);

    g_free(title);
}

G
Gerd Hoffmann 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static void gd_update_windowsize(GtkDisplayState *s)
{
    if (!s->full_screen) {
        GtkRequisition req;
        double sx, sy;

        if (s->free_scale) {
            sx = s->scale_x;
            sy = s->scale_y;

            s->scale_y = 1.0;
            s->scale_x = 1.0;
        } else {
            sx = 1.0;
            sy = 1.0;
        }

        gtk_widget_set_size_request(s->drawing_area,
                                    surface_width(s->ds) * s->scale_x,
                                    surface_height(s->ds) * s->scale_y);
#if GTK_CHECK_VERSION(3, 0, 0)
        gtk_widget_get_preferred_size(s->vbox, NULL, &req);
#else
        gtk_widget_size_request(s->vbox, &req);
#endif

        gtk_window_resize(GTK_WINDOW(s->window),
                          req.width * sx, req.height * sy);
    }
}

static void gd_update_full_redraw(GtkDisplayState *s)
{
    int ww, wh;
    gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);
    gtk_widget_queue_draw_area(s->drawing_area, 0, 0, ww, wh);
}

272 273 274 275 276 277 278 279 280 281 282 283
static void gtk_release_modifiers(GtkDisplayState *s)
{
    int i, keycode;

    if (!gd_on_vga(s)) {
        return;
    }
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        keycode = modifier_keycode[i];
        if (!s->modifier_pressed[i]) {
            continue;
        }
284
        qemu_input_event_send_key_number(s->dcl.con, keycode, false);
285 286 287 288
        s->modifier_pressed[i] = false;
    }
}

A
Anthony Liguori 已提交
289 290
/** DisplayState Callbacks **/

291
static void gd_update(DisplayChangeListener *dcl,
292
                      int x, int y, int w, int h)
A
Anthony Liguori 已提交
293
{
294
    GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
A
Anthony Liguori 已提交
295
    int x1, x2, y1, y2;
296 297 298
    int mx, my;
    int fbw, fbh;
    int ww, wh;
A
Anthony Liguori 已提交
299

300
    trace_gd_update(x, y, w, h);
A
Anthony Liguori 已提交
301

302 303 304 305 306
    if (s->convert) {
        pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
                               x, y, 0, 0, x, y, w, h);
    }

A
Anthony Liguori 已提交
307 308 309 310 311 312
    x1 = floor(x * s->scale_x);
    y1 = floor(y * s->scale_y);

    x2 = ceil(x * s->scale_x + w * s->scale_x);
    y2 = ceil(y * s->scale_y + h * s->scale_y);

G
Gerd Hoffmann 已提交
313 314
    fbw = surface_width(s->ds) * s->scale_x;
    fbh = surface_height(s->ds) * s->scale_y;
315 316 317 318 319 320 321 322 323 324 325 326

    gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);

    mx = my = 0;
    if (ww > fbw) {
        mx = (ww - fbw) / 2;
    }
    if (wh > fbh) {
        my = (wh - fbh) / 2;
    }

    gtk_widget_queue_draw_area(s->drawing_area, mx + x1, my + y1, (x2 - x1), (y2 - y1));
A
Anthony Liguori 已提交
327 328
}

329
static void gd_refresh(DisplayChangeListener *dcl)
A
Anthony Liguori 已提交
330
{
331
    graphic_hw_update(dcl->con);
A
Anthony Liguori 已提交
332 333
}

334 335 336 337 338 339 340 341 342
#if GTK_CHECK_VERSION(3, 0, 0)
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
    GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
    GdkDisplay *dpy;
    GdkDeviceManager *mgr;
    gint x_root, y_root;

C
Cole Robinson 已提交
343 344 345 346
    if (qemu_input_is_absolute()) {
        return;
    }

347 348 349 350 351 352
    dpy = gtk_widget_get_display(s->drawing_area);
    mgr = gdk_display_get_device_manager(dpy);
    gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
                               x, y, &x_root, &y_root);
    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
                    gtk_widget_get_screen(s->drawing_area),
C
Cole Robinson 已提交
353
                    x_root, y_root);
354 355
}
#else
G
Gerd Hoffmann 已提交
356 357 358 359 360 361
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
    GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
    gint x_root, y_root;

C
Cole Robinson 已提交
362 363 364 365
    if (qemu_input_is_absolute()) {
        return;
    }

366
    gdk_window_get_root_coords(gtk_widget_get_window(s->drawing_area),
G
Gerd Hoffmann 已提交
367 368 369 370 371
                               x, y, &x_root, &y_root);
    gdk_display_warp_pointer(gtk_widget_get_display(s->drawing_area),
                             gtk_widget_get_screen(s->drawing_area),
                             x_root, y_root);
}
372
#endif
G
Gerd Hoffmann 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386

static void gd_cursor_define(DisplayChangeListener *dcl,
                             QEMUCursor *c)
{
    GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
    GdkPixbuf *pixbuf;
    GdkCursor *cursor;

    pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
                                      GDK_COLORSPACE_RGB, true, 8,
                                      c->width, c->height, c->width * 4,
                                      NULL, NULL);
    cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(s->drawing_area),
                                        pixbuf, c->hot_x, c->hot_y);
387
    gdk_window_set_cursor(gtk_widget_get_window(s->drawing_area), cursor);
G
Gerd Hoffmann 已提交
388
    g_object_unref(pixbuf);
389
#if !GTK_CHECK_VERSION(3, 0, 0)
390
    gdk_cursor_unref(cursor);
391 392 393
#else
    g_object_unref(cursor);
#endif
G
Gerd Hoffmann 已提交
394 395
}

396 397
static void gd_switch(DisplayChangeListener *dcl,
                      DisplaySurface *surface)
A
Anthony Liguori 已提交
398
{
399
    GtkDisplayState *s = container_of(dcl, GtkDisplayState, dcl);
G
Gerd Hoffmann 已提交
400
    bool resized = true;
A
Anthony Liguori 已提交
401

402
    trace_gd_switch(surface_width(surface), surface_height(surface));
A
Anthony Liguori 已提交
403 404 405 406 407

    if (s->surface) {
        cairo_surface_destroy(s->surface);
    }

G
Gerd Hoffmann 已提交
408 409 410 411 412 413
    if (s->ds &&
        surface_width(s->ds) == surface_width(surface) &&
        surface_height(s->ds) == surface_height(surface)) {
        resized = false;
    }
    s->ds = surface;
A
Anthony Liguori 已提交
414

415 416 417 418
    if (s->convert) {
        pixman_image_unref(s->convert);
        s->convert = NULL;
    }
A
Anthony Liguori 已提交
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    if (surface->format == PIXMAN_x8r8g8b8) {
        /*
         * PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
         *
         * No need to convert, use surface directly.  Should be the
         * common case as this is qemu_default_pixelformat(32) too.
         */
        s->surface = cairo_image_surface_create_for_data
            (surface_data(surface),
             CAIRO_FORMAT_RGB24,
             surface_width(surface),
             surface_height(surface),
             surface_stride(surface));
    } else {
        /* Must convert surface, use pixman to do it. */
        s->convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
                                              surface_width(surface),
                                              surface_height(surface),
                                              NULL, 0);
        s->surface = cairo_image_surface_create_for_data
            ((void *)pixman_image_get_data(s->convert),
             CAIRO_FORMAT_RGB24,
             pixman_image_get_width(s->convert),
             pixman_image_get_height(s->convert),
             pixman_image_get_stride(s->convert));
        pixman_image_composite(PIXMAN_OP_SRC, s->ds->image, NULL, s->convert,
                               0, 0, 0, 0, 0, 0,
                               pixman_image_get_width(s->convert),
                               pixman_image_get_height(s->convert));
    }
450

G
Gerd Hoffmann 已提交
451 452 453 454
    if (resized) {
        gd_update_windowsize(s);
    } else {
        gd_update_full_redraw(s);
455
    }
A
Anthony Liguori 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
}

/** QEMU Events **/

static void gd_change_runstate(void *opaque, int running, RunState state)
{
    GtkDisplayState *s = opaque;

    gd_update_caption(s);
}

static void gd_mouse_mode_change(Notifier *notify, void *data)
{
    gd_update_cursor(container_of(notify, GtkDisplayState, mouse_mode_notifier),
                     FALSE);
}

/** GTK Events **/

475 476 477 478 479
static gboolean gd_window_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
{
    GtkDisplayState *s = opaque;
    gboolean handled = FALSE;

J
Jan Kiszka 已提交
480 481
    if (!gd_is_grab_active(s) ||
        (key->state & IGNORE_MODIFIER_MASK) == HOTKEY_MODIFIERS) {
482 483
        handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
    }
484 485 486
    if (handled) {
        gtk_release_modifiers(s);
    } else {
487 488 489 490 491 492
        handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
    }

    return handled;
}

A
Anthony Liguori 已提交
493 494 495 496 497 498
static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
                                void *opaque)
{
    GtkDisplayState *s = opaque;

    if (!no_quit) {
499
        unregister_displaychangelistener(&s->dcl);
A
Anthony Liguori 已提交
500 501 502 503 504 505 506 507 508 509
        qmp_quit(NULL);
        return FALSE;
    }

    return TRUE;
}

static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
{
    GtkDisplayState *s = opaque;
510
    int mx, my;
A
Anthony Liguori 已提交
511 512 513
    int ww, wh;
    int fbw, fbh;

514 515 516 517
    if (!gtk_widget_get_realized(widget)) {
        return FALSE;
    }

G
Gerd Hoffmann 已提交
518 519
    fbw = surface_width(s->ds);
    fbh = surface_height(s->ds);
A
Anthony Liguori 已提交
520 521 522

    gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);

523
    if (s->full_screen) {
A
Anthony Liguori 已提交
524 525
        s->scale_x = (double)ww / fbw;
        s->scale_y = (double)wh / fbh;
526 527 528 529 530 531 532
    } else if (s->free_scale) {
        double sx, sy;

        sx = (double)ww / fbw;
        sy = (double)wh / fbh;

        s->scale_x = s->scale_y = MIN(sx, sy);
A
Anthony Liguori 已提交
533 534
    }

535 536 537
    fbw *= s->scale_x;
    fbh *= s->scale_y;

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    mx = my = 0;
    if (ww > fbw) {
        mx = (ww - fbw) / 2;
    }
    if (wh > fbh) {
        my = (wh - fbh) / 2;
    }

    cairo_rectangle(cr, 0, 0, ww, wh);

    /* Optionally cut out the inner area where the pixmap
       will be drawn. This avoids 'flashing' since we're
       not double-buffering. Note we're using the undocumented
       behaviour of drawing the rectangle from right to left
       to cut out the whole */
    cairo_rectangle(cr, mx + fbw, my,
                    -1 * fbw, fbh);
    cairo_fill(cr);

    cairo_scale(cr, s->scale_x, s->scale_y);
    cairo_set_source_surface(cr, s->surface, mx / s->scale_x, my / s->scale_y);
A
Anthony Liguori 已提交
559 560 561 562 563
    cairo_paint(cr);

    return TRUE;
}

564
#if !GTK_CHECK_VERSION(3, 0, 0)
A
Anthony Liguori 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
                                void *opaque)
{
    cairo_t *cr;
    gboolean ret;

    cr = gdk_cairo_create(gtk_widget_get_window(widget));
    cairo_rectangle(cr,
                    expose->area.x,
                    expose->area.y,
                    expose->area.width,
                    expose->area.height);
    cairo_clip(cr);

    ret = gd_draw_event(widget, cr, opaque);

    cairo_destroy(cr);

    return ret;
}
585
#endif
A
Anthony Liguori 已提交
586 587 588 589 590 591

static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
                                void *opaque)
{
    GtkDisplayState *s = opaque;
    int x, y;
592 593 594 595
    int mx, my;
    int fbh, fbw;
    int ww, wh;

G
Gerd Hoffmann 已提交
596 597
    fbw = surface_width(s->ds) * s->scale_x;
    fbh = surface_height(s->ds) * s->scale_y;
A
Anthony Liguori 已提交
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612
    gdk_drawable_get_size(gtk_widget_get_window(s->drawing_area), &ww, &wh);

    mx = my = 0;
    if (ww > fbw) {
        mx = (ww - fbw) / 2;
    }
    if (wh > fbh) {
        my = (wh - fbh) / 2;
    }

    x = (motion->x - mx) / s->scale_x;
    y = (motion->y - my) / s->scale_y;

    if (x < 0 || y < 0 ||
G
Gerd Hoffmann 已提交
613 614
        x >= surface_width(s->ds) ||
        y >= surface_height(s->ds)) {
615 616
        return TRUE;
    }
A
Anthony Liguori 已提交
617

618 619 620 621 622 623 624 625 626 627
    if (qemu_input_is_absolute()) {
        qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_X, x,
                             surface_width(s->ds));
        qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_Y, y,
                             surface_height(s->ds));
        qemu_input_event_sync();
    } else if (s->last_x != -1 && s->last_y != -1 && gd_is_grab_active(s)) {
        qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_X, x - s->last_x);
        qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_Y, y - s->last_y);
        qemu_input_event_sync();
A
Anthony Liguori 已提交
628 629 630 631
    }
    s->last_x = x;
    s->last_y = y;

632
    if (!qemu_input_is_absolute() && gd_is_grab_active(s)) {
633
        GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
        int x = (int)motion->x_root;
        int y = (int)motion->y_root;

        /* In relative mode check to see if client pointer hit
         * one of the screen edges, and if so move it back by
         * 200 pixels. This is important because the pointer
         * in the server doesn't correspond 1-for-1, and so
         * may still be only half way across the screen. Without
         * this warp, the server pointer would thus appear to hit
         * an invisible wall */
        if (x == 0) {
            x += 200;
        }
        if (y == 0) {
            y += 200;
        }
        if (x == (gdk_screen_get_width(screen) - 1)) {
            x -= 200;
        }
        if (y == (gdk_screen_get_height(screen) - 1)) {
            y -= 200;
        }

        if (x != (int)motion->x_root || y != (int)motion->y_root) {
658 659 660 661 662
#if GTK_CHECK_VERSION(3, 0, 0)
            GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
            gdk_device_warp(dev, screen, x, y);
#else
            GdkDisplay *display = gtk_widget_get_display(widget);
663
            gdk_display_warp_pointer(display, screen, x, y);
664
#endif
665 666 667 668 669
            s->last_x = -1;
            s->last_y = -1;
            return FALSE;
        }
    }
A
Anthony Liguori 已提交
670 671 672 673 674 675 676
    return TRUE;
}

static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
                                void *opaque)
{
    GtkDisplayState *s = opaque;
677
    InputButton btn;
A
Anthony Liguori 已提交
678 679

    if (button->button == 1) {
680
        btn = INPUT_BUTTON_LEFT;
A
Anthony Liguori 已提交
681
    } else if (button->button == 2) {
682
        btn = INPUT_BUTTON_MIDDLE;
A
Anthony Liguori 已提交
683
    } else if (button->button == 3) {
684
        btn = INPUT_BUTTON_RIGHT;
A
Anthony Liguori 已提交
685
    } else {
686
        return TRUE;
A
Anthony Liguori 已提交
687 688
    }

689 690
    qemu_input_queue_btn(s->dcl.con, btn, button->type == GDK_BUTTON_PRESS);
    qemu_input_event_sync();
A
Anthony Liguori 已提交
691 692 693
    return TRUE;
}

J
Jan Kiszka 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
                                void *opaque)
{
    GtkDisplayState *s = opaque;
    InputButton btn;

    if (scroll->direction == GDK_SCROLL_UP) {
        btn = INPUT_BUTTON_WHEEL_UP;
    } else if (scroll->direction == GDK_SCROLL_DOWN) {
        btn = INPUT_BUTTON_WHEEL_DOWN;
    } else {
        return TRUE;
    }

    qemu_input_queue_btn(s->dcl.con, btn, true);
    qemu_input_event_sync();
    qemu_input_queue_btn(s->dcl.con, btn, false);
    qemu_input_event_sync();
    return TRUE;
}

A
Anthony Liguori 已提交
715 716
static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
{
717
    GtkDisplayState *s = opaque;
718
    int gdk_keycode = key->hardware_keycode;
719
    int i;
A
Anthony Liguori 已提交
720

721 722 723 724 725 726 727 728 729
#ifdef _WIN32
    UINT qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
    switch (qemu_keycode) {
    case 103:   /* alt gr */
        qemu_keycode = 56 | SCANCODE_GREY;
        break;
    }
#else
    int qemu_keycode;
A
Anthony Liguori 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743

    if (gdk_keycode < 9) {
        qemu_keycode = 0;
    } else if (gdk_keycode < 97) {
        qemu_keycode = gdk_keycode - 8;
    } else if (gdk_keycode < 158) {
        qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
    } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
        qemu_keycode = 0x70;
    } else if (gdk_keycode == 211) { /* backslash */
        qemu_keycode = 0x73;
    } else {
        qemu_keycode = 0;
    }
744
#endif
A
Anthony Liguori 已提交
745

746 747
    trace_gd_key_event(gdk_keycode, qemu_keycode,
                       (key->type == GDK_KEY_PRESS) ? "down" : "up");
A
Anthony Liguori 已提交
748

749 750 751 752 753 754
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        if (qemu_keycode == modifier_keycode[i]) {
            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
        }
    }

755 756
    qemu_input_event_send_key_number(s->dcl.con, qemu_keycode,
                                     key->type == GDK_KEY_PRESS);
A
Anthony Liguori 已提交
757 758 759 760 761 762

    return TRUE;
}

/** Window Menu Actions **/

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
static void gd_menu_pause(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    if (s->external_pause_update) {
        return;
    }
    if (runstate_is_running()) {
        qmp_stop(NULL);
    } else {
        qmp_cont(NULL);
    }
}

static void gd_menu_reset(GtkMenuItem *item, void *opaque)
{
    qmp_system_reset(NULL);
}

static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
{
    qmp_system_powerdown(NULL);
}

A
Anthony Liguori 已提交
787 788 789 790 791 792 793 794 795 796 797
static void gd_menu_quit(GtkMenuItem *item, void *opaque)
{
    qmp_quit(NULL);
}

static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vga_item))) {
        gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), 0);
798 799 800
    } else {
        int i;

801
        gtk_release_modifiers(s);
802 803 804 805 806 807
        for (i = 0; i < s->nb_vcs; i++) {
            if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
                gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
                break;
            }
        }
A
Anthony Liguori 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821
    }
}

static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
    } else {
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
    }
}

822 823 824 825
static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

826
    if (!s->full_screen) {
827 828 829 830 831 832 833 834 835 836 837 838 839
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
        gtk_widget_set_size_request(s->menu_bar, 0, 0);
        gtk_widget_set_size_request(s->drawing_area, -1, -1);
        gtk_window_fullscreen(GTK_WINDOW(s->window));
        if (gd_on_vga(s)) {
            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
        }
        s->full_screen = TRUE;
    } else {
        gtk_window_unfullscreen(GTK_WINDOW(s->window));
        gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
        gtk_widget_set_size_request(s->menu_bar, -1, -1);
        gtk_widget_set_size_request(s->drawing_area,
G
Gerd Hoffmann 已提交
840 841
                                    surface_width(s->ds),
                                    surface_height(s->ds));
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), FALSE);
        s->full_screen = FALSE;
        s->scale_x = 1.0;
        s->scale_y = 1.0;
    }

    gd_update_cursor(s, FALSE);
}

static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
                                   FALSE);

    s->scale_x += .25;
    s->scale_y += .25;

G
Gerd Hoffmann 已提交
861
    gd_update_windowsize(s);
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
}

static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
                                   FALSE);

    s->scale_x -= .25;
    s->scale_y -= .25;

    s->scale_x = MAX(s->scale_x, .25);
    s->scale_y = MAX(s->scale_y, .25);

G
Gerd Hoffmann 已提交
877
    gd_update_windowsize(s);
878 879 880 881 882 883 884 885 886
}

static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    s->scale_x = 1.0;
    s->scale_y = 1.0;

G
Gerd Hoffmann 已提交
887
    gd_update_windowsize(s);
888 889 890 891 892 893 894 895 896 897
}

static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
        s->free_scale = TRUE;
    } else {
        s->free_scale = FALSE;
898 899 900
        s->scale_x = 1.0;
        s->scale_y = 1.0;
        gd_update_windowsize(s);
901 902
    }

G
Gerd Hoffmann 已提交
903
    gd_update_full_redraw(s);
904 905
}

906 907
static void gd_grab_keyboard(GtkDisplayState *s)
{
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
#if GTK_CHECK_VERSION(3, 0, 0)
    GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
            gdk_device_grab(dev,
                            gtk_widget_get_window(s->drawing_area),
                            GDK_OWNERSHIP_NONE,
                            FALSE,
                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
                            NULL,
                            GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
    gdk_keyboard_grab(gtk_widget_get_window(s->drawing_area),
930 931
                      FALSE,
                      GDK_CURRENT_TIME);
932
#endif
933 934 935 936
}

static void gd_ungrab_keyboard(GtkDisplayState *s)
{
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
#if GTK_CHECK_VERSION(3, 0, 0)
    GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
            gdk_device_ungrab(dev,
                              GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
953
    gdk_keyboard_ungrab(GDK_CURRENT_TIME);
954
#endif
955 956
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
static void gd_grab_pointer(GtkDisplayState *s)
{
#if GTK_CHECK_VERSION(3, 0, 0)
    GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
            gdk_device_grab(dev,
                            gtk_widget_get_window(s->drawing_area),
                            GDK_OWNERSHIP_NONE,
                            FALSE, /* All events to come to our
                                      window directly */
                            GDK_POINTER_MOTION_MASK |
                            GDK_BUTTON_PRESS_MASK |
                            GDK_BUTTON_RELEASE_MASK |
                            GDK_BUTTON_MOTION_MASK |
                            GDK_SCROLL_MASK,
                            s->null_cursor,
                            GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
    gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
                     FALSE, /* All events to come to our window directly */
                     GDK_POINTER_MOTION_MASK |
                     GDK_BUTTON_PRESS_MASK |
                     GDK_BUTTON_RELEASE_MASK |
                     GDK_BUTTON_MOTION_MASK |
                     GDK_SCROLL_MASK,
                     NULL, /* Allow cursor to move over entire desktop */
                     s->null_cursor,
                     GDK_CURRENT_TIME);
#endif
}

static void gd_ungrab_pointer(GtkDisplayState *s)
{
#if GTK_CHECK_VERSION(3, 0, 0)
    GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
            gdk_device_ungrab(dev,
                              GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
    gdk_pointer_ungrab(GDK_CURRENT_TIME);
#endif
}

1020 1021 1022 1023 1024 1025
static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    if (gd_is_grab_active(s)) {
        gd_grab_keyboard(s);
1026
        gd_grab_pointer(s);
1027 1028
    } else {
        gd_ungrab_keyboard(s);
1029
        gd_ungrab_pointer(s);
1030 1031 1032 1033 1034 1035
    }

    gd_update_caption(s);
    gd_update_cursor(s, FALSE);
}

A
Anthony Liguori 已提交
1036 1037 1038 1039
static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
                           gpointer data)
{
    GtkDisplayState *s = data;
1040
    guint last_page;
1041
    gboolean on_vga;
A
Anthony Liguori 已提交
1042 1043 1044 1045 1046

    if (!gtk_widget_get_realized(s->notebook)) {
        return;
    }

1047 1048 1049 1050 1051 1052
    last_page = gtk_notebook_get_current_page(nb);

    if (last_page) {
        gtk_widget_set_size_request(s->vc[last_page - 1].terminal, -1, -1);
    }

1053 1054 1055 1056 1057
    on_vga = arg2 == 0;

    if (!on_vga) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
1058 1059 1060
    } else if (s->full_screen) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       TRUE);
1061 1062
    }

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    if (arg2 == 0) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->vga_item), TRUE);
    } else {
        VirtualConsole *vc = &s->vc[arg2 - 1];
        VteTerminal *term = VTE_TERMINAL(vc->terminal);
        int width, height;

        width = 80 * vte_terminal_get_char_width(term);
        height = 25 * vte_terminal_get_char_height(term);

        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
        gtk_widget_set_size_request(vc->terminal, width, height);
    }

1077 1078
    gtk_widget_set_sensitive(s->grab_item, on_vga);

A
Anthony Liguori 已提交
1079 1080 1081
    gd_update_cursor(s, TRUE);
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
{
    GtkDisplayState *s = data;

    if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
        gd_grab_keyboard(s);
    }

    return TRUE;
}

static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing, gpointer data)
{
    GtkDisplayState *s = data;

    if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
        gd_ungrab_keyboard(s);
    }

    return TRUE;
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
static gboolean gd_focus_out_event(GtkWidget *widget,
                                   GdkEventCrossing *crossing, gpointer data)
{
    GtkDisplayState *s = data;

    gtk_release_modifiers(s);

    return TRUE;
}

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
/** Virtual Console Callbacks **/

static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    VirtualConsole *vc = chr->opaque;

    return write(vc->fd, buf, len);
}

static int nb_vcs;
static CharDriverState *vcs[MAX_VCS];

G
Gerd Hoffmann 已提交
1126
static CharDriverState *gd_vc_handler(ChardevVC *unused)
1127 1128 1129 1130 1131
{
    CharDriverState *chr;

    chr = g_malloc0(sizeof(*chr));
    chr->chr_write = gd_vc_chr_write;
1132 1133
    /* defer OPENED events until our vc is fully initialized */
    chr->explicit_be_open = true;
1134 1135 1136 1137 1138 1139

    vcs[nb_vcs++] = chr;

    return chr;
}

A
Anthony Liguori 已提交
1140 1141
void early_gtk_display_init(void)
{
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    register_vc_handler(gd_vc_handler);
}

static gboolean gd_vc_in(GIOChannel *chan, GIOCondition cond, void *opaque)
{
    VirtualConsole *vc = opaque;
    uint8_t buffer[1024];
    ssize_t len;

    len = read(vc->fd, buffer, sizeof(buffer));
    if (len <= 0) {
        return FALSE;
    }

    qemu_chr_be_write(vc->chr, buffer, len);

    return TRUE;
}

A
Anthony Liguori 已提交
1161 1162
static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSList *group,
                          GtkWidget *view_menu)
1163 1164 1165 1166
{
    const char *label;
    char buffer[32];
    char path[32];
1167
#if VTE_CHECK_VERSION(0, 26, 0)
1168
    VtePty *pty;
1169
#endif
1170 1171 1172
    GIOChannel *chan;
    GtkWidget *scrolled_window;
    GtkAdjustment *vadjustment;
1173
    int master_fd, slave_fd;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

    snprintf(buffer, sizeof(buffer), "vc%d", index);
    snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);

    vc->chr = vcs[index];

    if (vc->chr->label) {
        label = vc->chr->label;
    } else {
        label = buffer;
    }

    vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, label);
    group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
J
Jan Kiszka 已提交
1189
    gtk_accel_map_add_entry(path, GDK_KEY_2 + index, HOTKEY_MODIFIERS);
1190 1191 1192

    vc->terminal = vte_terminal_new();

1193 1194
    master_fd = qemu_openpty_raw(&slave_fd, NULL);
    g_assert(master_fd != -1);
1195

1196
#if VTE_CHECK_VERSION(0, 26, 0)
1197 1198
    pty = vte_pty_new_foreign(master_fd, NULL);
    vte_terminal_set_pty_object(VTE_TERMINAL(vc->terminal), pty);
1199 1200 1201
#else
    vte_terminal_set_pty(VTE_TERMINAL(vc->terminal), master_fd);
#endif
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

    vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->terminal), -1);

    vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->terminal));

    scrolled_window = gtk_scrolled_window_new(NULL, vadjustment);
    gtk_container_add(GTK_CONTAINER(scrolled_window), vc->terminal);

    vte_terminal_set_size(VTE_TERMINAL(vc->terminal), 80, 25);

    vc->fd = slave_fd;
    vc->chr->opaque = vc;
    vc->scrolled_window = scrolled_window;

    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vc->scrolled_window),
                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), scrolled_window, gtk_label_new(label));
    g_signal_connect(vc->menu_item, "activate",
                     G_CALLBACK(gd_menu_switch_vc), s);

A
Anthony Liguori 已提交
1223
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);
1224

1225
    qemu_chr_be_generic_open(vc->chr);
1226 1227 1228 1229 1230 1231 1232 1233
    if (vc->chr->init) {
        vc->chr->init(vc->chr);
    }

    chan = g_io_channel_unix_new(vc->fd);
    g_io_add_watch(chan, G_IO_IN, gd_vc_in, vc);

    return group;
A
Anthony Liguori 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242
}

/** Window Creation **/

static void gd_connect_signals(GtkDisplayState *s)
{
    g_signal_connect(s->show_tabs_item, "activate",
                     G_CALLBACK(gd_menu_show_tabs), s);

1243 1244
    g_signal_connect(s->window, "key-press-event",
                     G_CALLBACK(gd_window_key_event), s);
A
Anthony Liguori 已提交
1245 1246 1247
    g_signal_connect(s->window, "delete-event",
                     G_CALLBACK(gd_window_close), s);

1248 1249 1250 1251
#if GTK_CHECK_VERSION(3, 0, 0)
    g_signal_connect(s->drawing_area, "draw",
                     G_CALLBACK(gd_draw_event), s);
#else
A
Anthony Liguori 已提交
1252 1253
    g_signal_connect(s->drawing_area, "expose-event",
                     G_CALLBACK(gd_expose_event), s);
1254
#endif
A
Anthony Liguori 已提交
1255 1256 1257 1258 1259 1260
    g_signal_connect(s->drawing_area, "motion-notify-event",
                     G_CALLBACK(gd_motion_event), s);
    g_signal_connect(s->drawing_area, "button-press-event",
                     G_CALLBACK(gd_button_event), s);
    g_signal_connect(s->drawing_area, "button-release-event",
                     G_CALLBACK(gd_button_event), s);
J
Jan Kiszka 已提交
1261 1262
    g_signal_connect(s->drawing_area, "scroll-event",
                     G_CALLBACK(gd_scroll_event), s);
A
Anthony Liguori 已提交
1263 1264 1265 1266 1267
    g_signal_connect(s->drawing_area, "key-press-event",
                     G_CALLBACK(gd_key_event), s);
    g_signal_connect(s->drawing_area, "key-release-event",
                     G_CALLBACK(gd_key_event), s);

1268 1269 1270 1271 1272 1273
    g_signal_connect(s->pause_item, "activate",
                     G_CALLBACK(gd_menu_pause), s);
    g_signal_connect(s->reset_item, "activate",
                     G_CALLBACK(gd_menu_reset), s);
    g_signal_connect(s->powerdown_item, "activate",
                     G_CALLBACK(gd_menu_powerdown), s);
A
Anthony Liguori 已提交
1274 1275
    g_signal_connect(s->quit_item, "activate",
                     G_CALLBACK(gd_menu_quit), s);
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    g_signal_connect(s->full_screen_item, "activate",
                     G_CALLBACK(gd_menu_full_screen), s);
    g_signal_connect(s->zoom_in_item, "activate",
                     G_CALLBACK(gd_menu_zoom_in), s);
    g_signal_connect(s->zoom_out_item, "activate",
                     G_CALLBACK(gd_menu_zoom_out), s);
    g_signal_connect(s->zoom_fixed_item, "activate",
                     G_CALLBACK(gd_menu_zoom_fixed), s);
    g_signal_connect(s->zoom_fit_item, "activate",
                     G_CALLBACK(gd_menu_zoom_fit), s);
A
Anthony Liguori 已提交
1286 1287
    g_signal_connect(s->vga_item, "activate",
                     G_CALLBACK(gd_menu_switch_vc), s);
1288 1289
    g_signal_connect(s->grab_item, "activate",
                     G_CALLBACK(gd_menu_grab_input), s);
A
Anthony Liguori 已提交
1290 1291
    g_signal_connect(s->notebook, "switch-page",
                     G_CALLBACK(gd_change_page), s);
1292 1293 1294 1295
    g_signal_connect(s->drawing_area, "enter-notify-event",
                     G_CALLBACK(gd_enter_event), s);
    g_signal_connect(s->drawing_area, "leave-notify-event",
                     G_CALLBACK(gd_leave_event), s);
1296 1297
    g_signal_connect(s->drawing_area, "focus-out-event",
                     G_CALLBACK(gd_focus_out_event), s);
A
Anthony Liguori 已提交
1298 1299
}

A
Anthony Liguori 已提交
1300
static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
A
Anthony Liguori 已提交
1301
{
A
Anthony Liguori 已提交
1302
    GtkWidget *machine_menu;
A
Anthony Liguori 已提交
1303
    GtkWidget *separator;
A
Anthony Liguori 已提交
1304
    GtkStockItem item;
A
Anthony Liguori 已提交
1305

A
Anthony Liguori 已提交
1306 1307
    machine_menu = gtk_menu_new();
    gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1308 1309

    s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
A
Anthony Liguori 已提交
1310
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1311 1312

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1313
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1314 1315

    s->reset_item = gtk_image_menu_item_new_with_mnemonic(_("_Reset"));
A
Anthony Liguori 已提交
1316
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1317 1318

    s->powerdown_item = gtk_image_menu_item_new_with_mnemonic(_("Power _Down"));
A
Anthony Liguori 已提交
1319
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1320 1321

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1322
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
A
Anthony Liguori 已提交
1323 1324 1325 1326

    s->quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
    gtk_stock_lookup(GTK_STOCK_QUIT, &item);
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1327 1328
                                 "<QEMU>/Machine/Quit");
    gtk_accel_map_add_entry("<QEMU>/Machine/Quit", item.keyval, item.modifier);
A
Anthony Liguori 已提交
1329
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
A
Anthony Liguori 已提交
1330

A
Anthony Liguori 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    return machine_menu;
}

static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
{
    GSList *group = NULL;
    GtkWidget *view_menu;
    GtkWidget *separator;
    int i;

    view_menu = gtk_menu_new();
    gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
A
Anthony Liguori 已提交
1343

1344 1345
    s->full_screen_item =
        gtk_image_menu_item_new_from_stock(GTK_STOCK_FULLSCREEN, NULL);
1346 1347
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
                                 "<QEMU>/View/Full Screen");
J
Jan Kiszka 已提交
1348 1349
    gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1350
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1351 1352

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1353
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1354 1355 1356 1357

    s->zoom_in_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_IN, NULL);
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
                                 "<QEMU>/View/Zoom In");
J
Jan Kiszka 已提交
1358 1359
    gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1360
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1361 1362 1363 1364

    s->zoom_out_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_OUT, NULL);
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
                                 "<QEMU>/View/Zoom Out");
J
Jan Kiszka 已提交
1365 1366
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1367
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1368 1369 1370 1371

    s->zoom_fixed_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_ZOOM_100, NULL);
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
                                 "<QEMU>/View/Zoom Fixed");
J
Jan Kiszka 已提交
1372 1373
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1374
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1375

1376
    s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
A
Anthony Liguori 已提交
1377
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1378 1379

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1380
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1381

1382
    s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
A
Anthony Liguori 已提交
1383
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1384

1385
    s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1386 1387
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
                                 "<QEMU>/View/Grab Input");
J
Jan Kiszka 已提交
1388 1389
    gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1390
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1391

A
Anthony Liguori 已提交
1392
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1393
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
1394 1395 1396 1397 1398

    s->vga_item = gtk_radio_menu_item_new_with_mnemonic(group, "_VGA");
    group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(s->vga_item));
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->vga_item),
                                 "<QEMU>/View/VGA");
J
Jan Kiszka 已提交
1399
    gtk_accel_map_add_entry("<QEMU>/View/VGA", GDK_KEY_1, HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1400
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->vga_item);
A
Anthony Liguori 已提交
1401

1402 1403 1404
    for (i = 0; i < nb_vcs; i++) {
        VirtualConsole *vc = &s->vc[i];

A
Anthony Liguori 已提交
1405
        group = gd_vc_init(s, vc, i, group, view_menu);
1406 1407 1408
        s->nb_vcs++;
    }

A
Anthony Liguori 已提交
1409
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1410
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
1411

1412
    s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
A
Anthony Liguori 已提交
1413
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
A
Anthony Liguori 已提交
1414

A
Anthony Liguori 已提交
1415 1416 1417 1418 1419 1420
    return view_menu;
}

static void gd_create_menus(GtkDisplayState *s)
{
    GtkAccelGroup *accel_group;
A
Anthony Liguori 已提交
1421

A
Anthony Liguori 已提交
1422 1423 1424 1425 1426
    accel_group = gtk_accel_group_new();
    s->machine_menu = gd_create_menu_machine(s, accel_group);
    s->view_menu = gd_create_menu_view(s, accel_group);

    s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1427 1428 1429
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
                              s->machine_menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
A
Anthony Liguori 已提交
1430

A
Anthony Liguori 已提交
1431
    s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
A
Anthony Liguori 已提交
1432 1433
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
A
Anthony Liguori 已提交
1434 1435 1436 1437

    g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
    gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
    s->accel_group = accel_group;
A
Anthony Liguori 已提交
1438 1439
}

1440 1441 1442
static const DisplayChangeListenerOps dcl_ops = {
    .dpy_name          = "gtk",
    .dpy_gfx_update    = gd_update,
1443
    .dpy_gfx_switch    = gd_switch,
1444
    .dpy_refresh       = gd_refresh,
G
Gerd Hoffmann 已提交
1445 1446
    .dpy_mouse_set     = gd_mouse_set,
    .dpy_cursor_define = gd_cursor_define,
1447 1448
};

1449
void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
A
Anthony Liguori 已提交
1450 1451
{
    GtkDisplayState *s = g_malloc0(sizeof(*s));
S
Stefan Weil 已提交
1452
    char *filename;
A
Anthony Liguori 已提交
1453 1454 1455

    gtk_init(NULL, NULL);

1456
    s->dcl.ops = &dcl_ops;
1457
    s->dcl.con = qemu_console_lookup_by_index(0);
A
Anthony Liguori 已提交
1458 1459

    s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1460 1461 1462
#if GTK_CHECK_VERSION(3, 2, 0)
    s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
#else
A
Anthony Liguori 已提交
1463
    s->vbox = gtk_vbox_new(FALSE, 0);
1464
#endif
A
Anthony Liguori 已提交
1465 1466 1467 1468 1469 1470
    s->notebook = gtk_notebook_new();
    s->drawing_area = gtk_drawing_area_new();
    s->menu_bar = gtk_menu_bar_new();

    s->scale_x = 1.0;
    s->scale_y = 1.0;
1471
    s->free_scale = FALSE;
A
Anthony Liguori 已提交
1472

1473 1474 1475 1476
    setlocale(LC_ALL, "");
    bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
    textdomain("qemu");

A
Anthony Liguori 已提交
1477 1478 1479 1480 1481 1482 1483 1484
    s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);

    s->mouse_mode_notifier.notify = gd_mouse_mode_change;
    qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
    qemu_add_vm_change_state_handler(gd_change_runstate, s);

    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), s->drawing_area, gtk_label_new("VGA"));

A
Anthony Liguori 已提交
1485
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
S
Stefan Weil 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
    if (filename) {
        GError *error = NULL;
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
        if (pixbuf) {
            gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
        } else {
            g_error_free(error);
        }
        g_free(filename);
    }

A
Anthony Liguori 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505
    gd_create_menus(s);

    gd_connect_signals(s);

    gtk_widget_add_events(s->drawing_area,
                          GDK_POINTER_MOTION_MASK |
                          GDK_BUTTON_PRESS_MASK |
                          GDK_BUTTON_RELEASE_MASK |
                          GDK_BUTTON_MOTION_MASK |
1506 1507
                          GDK_ENTER_NOTIFY_MASK |
                          GDK_LEAVE_NOTIFY_MASK |
A
Anthony Liguori 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
                          GDK_SCROLL_MASK |
                          GDK_KEY_PRESS_MASK);
    gtk_widget_set_double_buffered(s->drawing_area, FALSE);
    gtk_widget_set_can_focus(s->drawing_area, TRUE);

    gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
    gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);

    gd_update_caption(s);

    gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);

    gtk_container_add(GTK_CONTAINER(s->window), s->vbox);

    gtk_widget_show_all(s->window);

P
Peter Wu 已提交
1525 1526 1527
    if (full_screen) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
    }
1528 1529 1530
    if (grab_on_hover) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
    }
P
Peter Wu 已提交
1531

1532
    register_displaychangelistener(&s->dcl);
A
Anthony Liguori 已提交
1533 1534 1535

    global_state = s;
}