gtk.c 68.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"

P
Peter Maydell 已提交
37
#include "qemu/osdep.h"
K
Kevin Wolf 已提交
38
#include "qemu-common.h"
39
#include "qemu/cutils.h"
K
Kevin Wolf 已提交
40

G
Gerd Hoffmann 已提交
41 42
#include "ui/console.h"
#include "ui/gtk.h"
K
Kevin Wolf 已提交
43

44
#include <glib/gi18n.h>
45
#include <locale.h>
S
Stefan Weil 已提交
46
#if defined(CONFIG_VTE)
A
Anthony Liguori 已提交
47
#include <vte/vte.h>
S
Stefan Weil 已提交
48
#endif
A
Anthony Liguori 已提交
49 50
#include <math.h>

51
#include "trace.h"
52
#include "ui/input.h"
A
Anthony Liguori 已提交
53 54 55 56
#include "sysemu/sysemu.h"
#include "qmp-commands.h"
#include "x_keymap.h"
#include "keymaps.h"
57
#include "sysemu/char.h"
G
Gerd Hoffmann 已提交
58
#include "qom/object.h"
A
Anthony Liguori 已提交
59

60
#define MAX_VCS 10
G
Gerd Hoffmann 已提交
61 62 63 64 65 66
#define VC_WINDOW_X_MIN  320
#define VC_WINDOW_Y_MIN  240
#define VC_TERM_X_MIN     80
#define VC_TERM_Y_MIN     25
#define VC_SCALE_MIN    0.25
#define VC_SCALE_STEP   0.25
67

S
Stefan Weil 已提交
68 69 70
#if !defined(CONFIG_VTE)
# define VTE_CHECK_VERSION(a, b, c) 0
#endif
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85
#if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
/*
 * The gtk2 vte terminal widget seriously messes up the window resize
 * for some reason.  You basically can't make the qemu window smaller
 * any more because the toplevel window geoemtry hints are overridden.
 *
 * Workaround that by hiding all vte widgets, except the one in the
 * current tab.
 *
 * Luckily everything works smooth in gtk3.
 */
# define VTE_RESIZE_HACK 1
#endif

86 87 88 89
#if !GTK_CHECK_VERSION(2, 20, 0)
#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
#endif

90 91 92 93 94 95 96
#ifndef GDK_IS_X11_DISPLAY
#define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
#endif
#ifndef GDK_IS_WIN32_DISPLAY
#define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
#endif

97 98 99 100 101 102
#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
103
#define GDK_KEY_q GDK_q
104 105
#define GDK_KEY_plus GDK_plus
#define GDK_KEY_minus GDK_minus
G
Gerd Hoffmann 已提交
106
#define GDK_KEY_Pause GDK_Pause
107
#endif
108

109 110 111 112 113 114 115 116 117
/* Some older mingw versions lack this constant or have
 * it conditionally defined */
#ifdef _WIN32
# ifndef MAPVK_VK_TO_VSC
#  define MAPVK_VK_TO_VSC 0
# endif
#endif


J
Jan Kiszka 已提交
118 119
#define HOTKEY_MODIFIERS        (GDK_CONTROL_MASK | GDK_MOD1_MASK)

120 121 122 123 124
static const int modifier_keycode[] = {
    /* shift, control, alt keys, meta keys, both left & right */
    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
};

125
struct GtkDisplayState {
A
Anthony Liguori 已提交
126 127 128 129
    GtkWidget *window;

    GtkWidget *menu_bar;

130 131
    GtkAccelGroup *accel_group;

132 133 134 135 136
    GtkWidget *machine_menu_item;
    GtkWidget *machine_menu;
    GtkWidget *pause_item;
    GtkWidget *reset_item;
    GtkWidget *powerdown_item;
A
Anthony Liguori 已提交
137 138 139 140
    GtkWidget *quit_item;

    GtkWidget *view_menu_item;
    GtkWidget *view_menu;
141
    GtkWidget *full_screen_item;
142
    GtkWidget *copy_item;
143 144 145 146
    GtkWidget *zoom_in_item;
    GtkWidget *zoom_out_item;
    GtkWidget *zoom_fixed_item;
    GtkWidget *zoom_fit_item;
147 148
    GtkWidget *grab_item;
    GtkWidget *grab_on_hover_item;
A
Anthony Liguori 已提交
149

150 151 152
    int nb_vcs;
    VirtualConsole vc[MAX_VCS];

A
Anthony Liguori 已提交
153
    GtkWidget *show_tabs_item;
154
    GtkWidget *untabify_item;
A
Anthony Liguori 已提交
155 156 157 158

    GtkWidget *vbox;
    GtkWidget *notebook;
    int button_mask;
159
    gboolean last_set;
A
Anthony Liguori 已提交
160 161
    int last_x;
    int last_y;
162 163
    int grab_x_root;
    int grab_y_root;
G
Gerd Hoffmann 已提交
164 165
    VirtualConsole *kbd_owner;
    VirtualConsole *ptr_owner;
A
Anthony Liguori 已提交
166

167
    gboolean full_screen;
A
Anthony Liguori 已提交
168 169 170

    GdkCursor *null_cursor;
    Notifier mouse_mode_notifier;
171
    gboolean free_scale;
172 173

    bool external_pause_update;
174 175

    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
176
    bool has_evdev;
177
    bool ignore_keys;
178
};
A
Anthony Liguori 已提交
179

G
Gerd Hoffmann 已提交
180
static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
G
Gerd Hoffmann 已提交
181
static void gd_ungrab_pointer(GtkDisplayState *s);
G
Gerd Hoffmann 已提交
182
static void gd_grab_keyboard(VirtualConsole *vc, const char *reason);
183
static void gd_ungrab_keyboard(GtkDisplayState *s);
G
Gerd Hoffmann 已提交
184

A
Anthony Liguori 已提交
185 186
/** Utility Functions **/

G
Gerd Hoffmann 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
{
    VirtualConsole *vc;
    gint i;

    for (i = 0; i < s->nb_vcs; i++) {
        vc = &s->vc[i];
        if (gtk_check_menu_item_get_active
            (GTK_CHECK_MENU_ITEM(vc->menu_item))) {
            return vc;
        }
    }
    return NULL;
}

static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
{
    VirtualConsole *vc;
    gint i, p;

    for (i = 0; i < s->nb_vcs; i++) {
        vc = &s->vc[i];
        p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
        if (p == page) {
            return vc;
        }
    }
    return NULL;
}

217 218 219 220 221 222 223 224
static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
{
    gint page;

    page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
    return gd_vc_find_by_page(s, page);
}

225 226 227 228 229 230 231 232 233 234
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));
}

235
static void gd_update_cursor(VirtualConsole *vc)
A
Anthony Liguori 已提交
236
{
237
    GtkDisplayState *s = vc->s;
A
Anthony Liguori 已提交
238 239
    GdkWindow *window;

240 241
    if (vc->type != GD_VC_GFX ||
        !qemu_console_is_graphic(vc->gfx.dcl.con)) {
242 243
        return;
    }
A
Anthony Liguori 已提交
244

245 246 247 248
    if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
        return;
    }

249
    window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
G
Gerd Hoffmann 已提交
250
    if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
A
Anthony Liguori 已提交
251 252 253 254 255 256 257 258 259
        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 = "";
G
Gerd Hoffmann 已提交
260
    gchar *prefix;
A
Anthony Liguori 已提交
261
    gchar *title;
262
    const char *grab = "";
263
    bool is_paused = !runstate_is_running();
G
Gerd Hoffmann 已提交
264 265 266 267 268 269 270
    int i;

    if (qemu_name) {
        prefix = g_strdup_printf("QEMU (%s)", qemu_name);
    } else {
        prefix = g_strdup_printf("QEMU");
    }
271

G
Gerd Hoffmann 已提交
272 273
    if (s->ptr_owner != NULL &&
        s->ptr_owner->window == NULL) {
274
        grab = _(" - Press Ctrl+Alt+G to release grab");
275
    }
A
Anthony Liguori 已提交
276

277
    if (is_paused) {
278
        status = _(" [Paused]");
A
Anthony Liguori 已提交
279
    }
280 281 282 283
    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 已提交
284

G
Gerd Hoffmann 已提交
285
    title = g_strdup_printf("%s%s%s", prefix, status, grab);
A
Anthony Liguori 已提交
286 287
    gtk_window_set_title(GTK_WINDOW(s->window), title);
    g_free(title);
G
Gerd Hoffmann 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

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

        if (!vc->window) {
            continue;
        }
        title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
                                vc == s->kbd_owner ? " +kbd" : "",
                                vc == s->ptr_owner ? " +ptr" : "");
        gtk_window_set_title(GTK_WINDOW(vc->window), title);
        g_free(title);
    }

    g_free(prefix);
A
Anthony Liguori 已提交
303 304
}

G
Gerd Hoffmann 已提交
305
static void gd_update_geometry_hints(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
306
{
307
    GtkDisplayState *s = vc->s;
G
Gerd Hoffmann 已提交
308 309 310 311
    GdkWindowHints mask = 0;
    GdkGeometry geo = {};
    GtkWidget *geo_widget = NULL;
    GtkWindow *geo_window;
312

G
Gerd Hoffmann 已提交
313
    if (vc->type == GD_VC_GFX) {
314 315 316
        if (!vc->gfx.ds) {
            return;
        }
G
Gerd Hoffmann 已提交
317 318 319 320 321 322 323 324 325 326 327
        if (s->free_scale) {
            geo.min_width  = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
            geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
            mask |= GDK_HINT_MIN_SIZE;
        } else {
            geo.min_width  = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
            geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
            mask |= GDK_HINT_MIN_SIZE;
        }
        geo_widget = vc->gfx.drawing_area;
        gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
G
Gerd Hoffmann 已提交
328

G
Gerd Hoffmann 已提交
329 330 331
#if defined(CONFIG_VTE)
    } else if (vc->type == GD_VC_VTE) {
        VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
332
        GtkBorder padding = { 0 };
G
Gerd Hoffmann 已提交
333

334 335 336 337 338 339
#if VTE_CHECK_VERSION(0, 37, 0)
        gtk_style_context_get_padding(
                gtk_widget_get_style_context(vc->vte.terminal),
                gtk_widget_get_state_flags(vc->vte.terminal),
                &padding);
#else
340 341 342 343 344 345 346 347
        {
            GtkBorder *ib = NULL;
            gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
            if (ib) {
                padding = *ib;
                gtk_border_free(ib);
            }
        }
348 349
#endif

G
Gerd Hoffmann 已提交
350 351 352 353 354 355 356 357 358
        geo.width_inc  = vte_terminal_get_char_width(term);
        geo.height_inc = vte_terminal_get_char_height(term);
        mask |= GDK_HINT_RESIZE_INC;
        geo.base_width  = geo.width_inc;
        geo.base_height = geo.height_inc;
        mask |= GDK_HINT_BASE_SIZE;
        geo.min_width  = geo.width_inc * VC_TERM_X_MIN;
        geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
        mask |= GDK_HINT_MIN_SIZE;
359

360 361 362 363
        geo.base_width  += padding.left + padding.right;
        geo.base_height += padding.top + padding.bottom;
        geo.min_width   += padding.left + padding.right;
        geo.min_height  += padding.top + padding.bottom;
G
Gerd Hoffmann 已提交
364 365
        geo_widget = vc->vte.terminal;
#endif
G
Gerd Hoffmann 已提交
366
    }
G
Gerd Hoffmann 已提交
367 368 369 370 371

    geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
    gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
}

372
void gd_update_windowsize(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
373 374 375 376 377 378 379 380
{
    GtkDisplayState *s = vc->s;

    gd_update_geometry_hints(vc);

    if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
        gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
                          VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
G
Gerd Hoffmann 已提交
381
    }
G
Gerd Hoffmann 已提交
382 383
}

384
static void gd_update_full_redraw(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
385
{
386
    GtkWidget *area = vc->gfx.drawing_area;
G
Gerd Hoffmann 已提交
387
    int ww, wh;
388
    gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
389 390 391 392 393 394
#if defined(CONFIG_GTK_GL)
    if (vc->gfx.gls) {
        gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
        return;
    }
#endif
395
    gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
G
Gerd Hoffmann 已提交
396 397
}

398 399
static void gtk_release_modifiers(GtkDisplayState *s)
{
400
    VirtualConsole *vc = gd_vc_find_current(s);
401 402
    int i, keycode;

403 404
    if (vc->type != GD_VC_GFX ||
        !qemu_console_is_graphic(vc->gfx.dcl.con)) {
405 406 407 408 409 410 411
        return;
    }
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        keycode = modifier_keycode[i];
        if (!s->modifier_pressed[i]) {
            continue;
        }
412
        qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
413 414 415 416
        s->modifier_pressed[i] = false;
    }
}

417 418 419 420 421 422 423 424 425
static void gd_widget_reparent(GtkWidget *from, GtkWidget *to,
                               GtkWidget *widget)
{
    g_object_ref(G_OBJECT(widget));
    gtk_container_remove(GTK_CONTAINER(from), widget);
    gtk_container_add(GTK_CONTAINER(to), widget);
    g_object_unref(G_OBJECT(widget));
}

A
Anthony Liguori 已提交
426 427
/** DisplayState Callbacks **/

428
static void gd_update(DisplayChangeListener *dcl,
429
                      int x, int y, int w, int h)
A
Anthony Liguori 已提交
430
{
431
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
432
    GdkWindow *win;
A
Anthony Liguori 已提交
433
    int x1, x2, y1, y2;
434 435 436
    int mx, my;
    int fbw, fbh;
    int ww, wh;
A
Anthony Liguori 已提交
437

G
Gerd Hoffmann 已提交
438
    trace_gd_update(vc->label, x, y, w, h);
A
Anthony Liguori 已提交
439

440 441 442 443
    if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
        return;
    }

444 445 446
    if (vc->gfx.convert) {
        pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
                               NULL, vc->gfx.convert,
447 448 449
                               x, y, 0, 0, x, y, w, h);
    }

450 451
    x1 = floor(x * vc->gfx.scale_x);
    y1 = floor(y * vc->gfx.scale_y);
A
Anthony Liguori 已提交
452

453 454
    x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
    y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
A
Anthony Liguori 已提交
455

456 457
    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
458

459 460 461 462 463
    win = gtk_widget_get_window(vc->gfx.drawing_area);
    if (!win) {
        return;
    }
    gdk_drawable_get_size(win, &ww, &wh);
464 465 466 467 468 469 470 471 472

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

473 474
    gtk_widget_queue_draw_area(vc->gfx.drawing_area,
                               mx + x1, my + y1, (x2 - x1), (y2 - y1));
A
Anthony Liguori 已提交
475 476
}

477
static void gd_refresh(DisplayChangeListener *dcl)
A
Anthony Liguori 已提交
478
{
479
    graphic_hw_update(dcl->con);
A
Anthony Liguori 已提交
480 481
}

482
#if GTK_CHECK_VERSION(3, 0, 0)
483 484 485 486 487 488 489 490 491 492
static GdkDevice *gd_get_pointer(GdkDisplay *dpy)
{
#if GTK_CHECK_VERSION(3, 20, 0)
    return gdk_seat_get_pointer(gdk_display_get_default_seat(dpy));
#else
    return gdk_device_manager_get_client_pointer(
        gdk_display_get_device_manager(dpy));
#endif
}

493 494 495
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
496
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
497 498 499
    GdkDisplay *dpy;
    gint x_root, y_root;

C
Cole Robinson 已提交
500 501 502 503
    if (qemu_input_is_absolute()) {
        return;
    }

504 505
    dpy = gtk_widget_get_display(vc->gfx.drawing_area);
    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
506
                               x, y, &x_root, &y_root);
507
    gdk_device_warp(gd_get_pointer(dpy),
508
                    gtk_widget_get_screen(vc->gfx.drawing_area),
C
Cole Robinson 已提交
509
                    x_root, y_root);
510 511
    vc->s->last_x = x;
    vc->s->last_y = y;
512 513
}
#else
G
Gerd Hoffmann 已提交
514 515 516
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
517
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
518 519
    gint x_root, y_root;

C
Cole Robinson 已提交
520 521 522 523
    if (qemu_input_is_absolute()) {
        return;
    }

524
    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
G
Gerd Hoffmann 已提交
525
                               x, y, &x_root, &y_root);
526 527
    gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
                             gtk_widget_get_screen(vc->gfx.drawing_area),
G
Gerd Hoffmann 已提交
528 529
                             x_root, y_root);
}
530
#endif
G
Gerd Hoffmann 已提交
531 532 533 534

static void gd_cursor_define(DisplayChangeListener *dcl,
                             QEMUCursor *c)
{
535
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
536 537 538
    GdkPixbuf *pixbuf;
    GdkCursor *cursor;

539 540 541 542
    if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
        return;
    }

G
Gerd Hoffmann 已提交
543 544 545 546
    pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
                                      GDK_COLORSPACE_RGB, true, 8,
                                      c->width, c->height, c->width * 4,
                                      NULL, NULL);
547 548 549 550
    cursor = gdk_cursor_new_from_pixbuf
        (gtk_widget_get_display(vc->gfx.drawing_area),
         pixbuf, c->hot_x, c->hot_y);
    gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
G
Gerd Hoffmann 已提交
551
    g_object_unref(pixbuf);
552
#if !GTK_CHECK_VERSION(3, 0, 0)
553
    gdk_cursor_unref(cursor);
554 555 556
#else
    g_object_unref(cursor);
#endif
G
Gerd Hoffmann 已提交
557 558
}

559 560
static void gd_switch(DisplayChangeListener *dcl,
                      DisplaySurface *surface)
A
Anthony Liguori 已提交
561
{
562
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
563
    bool resized = true;
A
Anthony Liguori 已提交
564

565 566 567
    trace_gd_switch(vc->label,
                    surface ? surface_width(surface)  : 0,
                    surface ? surface_height(surface) : 0);
A
Anthony Liguori 已提交
568

569 570
    if (vc->gfx.surface) {
        cairo_surface_destroy(vc->gfx.surface);
571 572 573 574 575
        vc->gfx.surface = NULL;
    }
    if (vc->gfx.convert) {
        pixman_image_unref(vc->gfx.convert);
        vc->gfx.convert = NULL;
A
Anthony Liguori 已提交
576 577
    }

578
    if (vc->gfx.ds && surface &&
579 580
        surface_width(vc->gfx.ds) == surface_width(surface) &&
        surface_height(vc->gfx.ds) == surface_height(surface)) {
G
Gerd Hoffmann 已提交
581 582
        resized = false;
    }
583
    vc->gfx.ds = surface;
A
Anthony Liguori 已提交
584

585 586
    if (!surface) {
        return;
587
    }
A
Anthony Liguori 已提交
588

589 590 591 592 593 594 595
    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.
         */
596
        vc->gfx.surface = cairo_image_surface_create_for_data
597 598 599 600 601 602 603
            (surface_data(surface),
             CAIRO_FORMAT_RGB24,
             surface_width(surface),
             surface_height(surface),
             surface_stride(surface));
    } else {
        /* Must convert surface, use pixman to do it. */
604 605 606 607 608 609
        vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
                                                   surface_width(surface),
                                                   surface_height(surface),
                                                   NULL, 0);
        vc->gfx.surface = cairo_image_surface_create_for_data
            ((void *)pixman_image_get_data(vc->gfx.convert),
610
             CAIRO_FORMAT_RGB24,
611 612 613 614 615
             pixman_image_get_width(vc->gfx.convert),
             pixman_image_get_height(vc->gfx.convert),
             pixman_image_get_stride(vc->gfx.convert));
        pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
                               NULL, vc->gfx.convert,
616
                               0, 0, 0, 0, 0, 0,
617 618
                               pixman_image_get_width(vc->gfx.convert),
                               pixman_image_get_height(vc->gfx.convert));
619
    }
620

G
Gerd Hoffmann 已提交
621
    if (resized) {
622
        gd_update_windowsize(vc);
G
Gerd Hoffmann 已提交
623
    } else {
624
        gd_update_full_redraw(vc);
625
    }
A
Anthony Liguori 已提交
626 627
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static const DisplayChangeListenerOps dcl_ops = {
    .dpy_name             = "gtk",
    .dpy_gfx_update       = gd_update,
    .dpy_gfx_switch       = gd_switch,
    .dpy_gfx_check_format = qemu_pixman_check_format,
    .dpy_refresh          = gd_refresh,
    .dpy_mouse_set        = gd_mouse_set,
    .dpy_cursor_define    = gd_cursor_define,
};


#if defined(CONFIG_OPENGL)

/** DisplayState Callbacks (opengl version) **/

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
#if defined(CONFIG_GTK_GL)

static const DisplayChangeListenerOps dcl_gl_area_ops = {
    .dpy_name             = "gtk-egl",
    .dpy_gfx_update       = gd_gl_area_update,
    .dpy_gfx_switch       = gd_gl_area_switch,
    .dpy_gfx_check_format = console_gl_check_format,
    .dpy_refresh          = gd_gl_area_refresh,
    .dpy_mouse_set        = gd_mouse_set,
    .dpy_cursor_define    = gd_cursor_define,

    .dpy_gl_ctx_create       = gd_gl_area_create_context,
    .dpy_gl_ctx_destroy      = gd_gl_area_destroy_context,
    .dpy_gl_ctx_make_current = gd_gl_area_make_current,
    .dpy_gl_ctx_get_current  = gd_gl_area_get_current_context,
    .dpy_gl_scanout          = gd_gl_area_scanout,
    .dpy_gl_update           = gd_gl_area_scanout_flush,
};

#else

664 665 666 667 668 669 670 671
static const DisplayChangeListenerOps dcl_egl_ops = {
    .dpy_name             = "gtk-egl",
    .dpy_gfx_update       = gd_egl_update,
    .dpy_gfx_switch       = gd_egl_switch,
    .dpy_gfx_check_format = console_gl_check_format,
    .dpy_refresh          = gd_egl_refresh,
    .dpy_mouse_set        = gd_mouse_set,
    .dpy_cursor_define    = gd_cursor_define,
672 673 674 675 676 677 678

    .dpy_gl_ctx_create       = gd_egl_create_context,
    .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
    .dpy_gl_ctx_make_current = gd_egl_make_current,
    .dpy_gl_ctx_get_current  = qemu_egl_get_current_context,
    .dpy_gl_scanout          = gd_egl_scanout,
    .dpy_gl_update           = gd_egl_scanout_flush,
679 680
};

681 682
#endif /* CONFIG_GTK_GL */
#endif /* CONFIG_OPENGL */
683

A
Anthony Liguori 已提交
684 685 686 687 688 689 690 691 692 693 694
/** 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)
{
695
    GtkDisplayState *s;
696
    int i;
697 698 699 700 701 702 703

    s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
    /* release the grab at switching to absolute mode */
    if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
    }
704 705 706 707
    for (i = 0; i < s->nb_vcs; i++) {
        VirtualConsole *vc = &s->vc[i];
        gd_update_cursor(vc);
    }
A
Anthony Liguori 已提交
708 709 710 711 712 713 714 715
}

/** GTK Events **/

static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
                                void *opaque)
{
    GtkDisplayState *s = opaque;
716
    int i;
A
Anthony Liguori 已提交
717 718

    if (!no_quit) {
719 720 721 722 723 724
        for (i = 0; i < s->nb_vcs; i++) {
            if (s->vc[i].type != GD_VC_GFX) {
                continue;
            }
            unregister_displaychangelistener(&s->vc[i].gfx.dcl);
        }
A
Anthony Liguori 已提交
725 726 727 728 729 730 731
        qmp_quit(NULL);
        return FALSE;
    }

    return TRUE;
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
static void gd_set_ui_info(VirtualConsole *vc, gint width, gint height)
{
    QemuUIInfo info;

    memset(&info, 0, sizeof(info));
    info.width = width;
    info.height = height;
    dpy_set_ui_info(vc->gfx.dcl.con, &info);
}

#if defined(CONFIG_GTK_GL)

static gboolean gd_render_event(GtkGLArea *area, GdkGLContext *context,
                                void *opaque)
{
    VirtualConsole *vc = opaque;

    if (vc->gfx.gls) {
        gd_gl_area_draw(vc);
    }
    return TRUE;
}

static void gd_resize_event(GtkGLArea *area,
                            gint width, gint height, gpointer *opaque)
{
    VirtualConsole *vc = (void *)opaque;

    gd_set_ui_info(vc, width, height);
}

#endif

A
Anthony Liguori 已提交
765 766
static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
{
767 768
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
769
    int mx, my;
A
Anthony Liguori 已提交
770 771 772
    int ww, wh;
    int fbw, fbh;

773 774
#if defined(CONFIG_OPENGL)
    if (vc->gfx.gls) {
775 776 777 778
#if defined(CONFIG_GTK_GL)
        /* invoke render callback please */
        return FALSE;
#else
779 780
        gd_egl_draw(vc);
        return TRUE;
781
#endif
782 783 784
    }
#endif

785 786 787
    if (!gtk_widget_get_realized(widget)) {
        return FALSE;
    }
788 789 790
    if (!vc->gfx.ds) {
        return FALSE;
    }
791

792 793
    fbw = surface_width(vc->gfx.ds);
    fbh = surface_height(vc->gfx.ds);
A
Anthony Liguori 已提交
794 795 796

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

797
    if (s->full_screen) {
798 799
        vc->gfx.scale_x = (double)ww / fbw;
        vc->gfx.scale_y = (double)wh / fbh;
800 801 802 803 804 805
    } else if (s->free_scale) {
        double sx, sy;

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

806
        vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
A
Anthony Liguori 已提交
807 808
    }

809 810
    fbw *= vc->gfx.scale_x;
    fbh *= vc->gfx.scale_y;
811

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
    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);

831 832 833
    cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
    cairo_set_source_surface(cr, vc->gfx.surface,
                             mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
A
Anthony Liguori 已提交
834 835 836 837 838
    cairo_paint(cr);

    return TRUE;
}

839
#if !GTK_CHECK_VERSION(3, 0, 0)
A
Anthony Liguori 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
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;
}
860
#endif
A
Anthony Liguori 已提交
861 862 863 864

static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
                                void *opaque)
{
865 866
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
A
Anthony Liguori 已提交
867
    int x, y;
868 869 870 871
    int mx, my;
    int fbh, fbw;
    int ww, wh;

872 873 874 875
    if (!vc->gfx.ds) {
        return TRUE;
    }

876 877
    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
A
Anthony Liguori 已提交
878

879 880
    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
                          &ww, &wh);
881 882 883 884 885 886 887 888 889

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

890 891
    x = (motion->x - mx) / vc->gfx.scale_x;
    y = (motion->y - my) / vc->gfx.scale_y;
892

893
    if (qemu_input_is_absolute()) {
894
        if (x < 0 || y < 0 ||
895 896
            x >= surface_width(vc->gfx.ds) ||
            y >= surface_height(vc->gfx.ds)) {
897 898
            return TRUE;
        }
899 900 901 902
        qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
                             surface_width(vc->gfx.ds));
        qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
                             surface_height(vc->gfx.ds));
903
        qemu_input_event_sync();
G
Gerd Hoffmann 已提交
904
    } else if (s->last_set && s->ptr_owner == vc) {
905 906
        qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
        qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
907
        qemu_input_event_sync();
A
Anthony Liguori 已提交
908 909 910
    }
    s->last_x = x;
    s->last_y = y;
911
    s->last_set = TRUE;
A
Anthony Liguori 已提交
912

G
Gerd Hoffmann 已提交
913
    if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
914
        GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
915 916
        int screen_width, screen_height;

917 918 919
        int x = (int)motion->x_root;
        int y = (int)motion->y_root;

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
#if GTK_CHECK_VERSION(3, 22, 0)
        {
            GdkDisplay *dpy = gtk_widget_get_display(widget);
            GdkWindow *win = gtk_widget_get_window(widget);
            GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
            GdkRectangle geometry;
            gdk_monitor_get_geometry(monitor, &geometry);
            screen_width = geometry.width;
            screen_height = geometry.height;
        }
#else
        {
            screen_width = gdk_screen_get_width(screen);
            screen_height = gdk_screen_get_height(screen);
        }
#endif

937 938 939 940 941 942 943 944 945 946 947 948 949
        /* 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;
        }
950
        if (x == (screen_width - 1)) {
951 952
            x -= 200;
        }
953
        if (y == (screen_height - 1)) {
954 955 956 957
            y -= 200;
        }

        if (x != (int)motion->x_root || y != (int)motion->y_root) {
958 959 960 961 962
#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);
963
            gdk_display_warp_pointer(display, screen, x, y);
964
#endif
965
            s->last_set = FALSE;
966 967 968
            return FALSE;
        }
    }
A
Anthony Liguori 已提交
969 970 971 972 973 974
    return TRUE;
}

static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
                                void *opaque)
{
975 976
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
977
    InputButton btn;
A
Anthony Liguori 已提交
978

979 980
    /* implicitly grab the input at the first click in the relative mode */
    if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
G
Gerd Hoffmann 已提交
981 982 983 984 985
        !qemu_input_is_absolute() && s->ptr_owner != vc) {
        if (!vc->window) {
            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                           TRUE);
        } else {
G
Gerd Hoffmann 已提交
986
            gd_grab_pointer(vc, "relative-mode-click");
G
Gerd Hoffmann 已提交
987
        }
988 989 990
        return TRUE;
    }

A
Anthony Liguori 已提交
991
    if (button->button == 1) {
992
        btn = INPUT_BUTTON_LEFT;
A
Anthony Liguori 已提交
993
    } else if (button->button == 2) {
994
        btn = INPUT_BUTTON_MIDDLE;
A
Anthony Liguori 已提交
995
    } else if (button->button == 3) {
996
        btn = INPUT_BUTTON_RIGHT;
A
Anthony Liguori 已提交
997
    } else {
998
        return TRUE;
A
Anthony Liguori 已提交
999 1000
    }

1001 1002
    qemu_input_queue_btn(vc->gfx.dcl.con, btn,
                         button->type == GDK_BUTTON_PRESS);
1003
    qemu_input_event_sync();
A
Anthony Liguori 已提交
1004 1005 1006
    return TRUE;
}

J
Jan Kiszka 已提交
1007 1008 1009
static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
                                void *opaque)
{
1010
    VirtualConsole *vc = opaque;
J
Jan Kiszka 已提交
1011 1012 1013
    InputButton btn;

    if (scroll->direction == GDK_SCROLL_UP) {
G
Gerd Hoffmann 已提交
1014
        btn = INPUT_BUTTON_WHEEL_UP;
J
Jan Kiszka 已提交
1015
    } else if (scroll->direction == GDK_SCROLL_DOWN) {
G
Gerd Hoffmann 已提交
1016
        btn = INPUT_BUTTON_WHEEL_DOWN;
J
Jan Kiszka 已提交
1017 1018 1019 1020
    } else {
        return TRUE;
    }

1021
    qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
J
Jan Kiszka 已提交
1022
    qemu_input_event_sync();
1023
    qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
J
Jan Kiszka 已提交
1024 1025 1026 1027
    qemu_input_event_sync();
    return TRUE;
}

1028
static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
A
Anthony Liguori 已提交
1029
{
G
Gerd Hoffmann 已提交
1030
    int qemu_keycode;
A
Anthony Liguori 已提交
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040
#ifdef GDK_WINDOWING_WIN32
    if (GDK_IS_WIN32_DISPLAY(dpy)) {
        qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
        switch (qemu_keycode) {
        case 103:   /* alt gr */
            qemu_keycode = 56 | SCANCODE_GREY;
            break;
        }
        return qemu_keycode;
1041
    }
1042
#endif
A
Anthony Liguori 已提交
1043 1044 1045 1046 1047

    if (gdk_keycode < 9) {
        qemu_keycode = 0;
    } else if (gdk_keycode < 97) {
        qemu_keycode = gdk_keycode - 8;
1048 1049
#ifdef GDK_WINDOWING_X11
    } else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
1050 1051 1052 1053 1054
        if (s->has_evdev) {
            qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
        } else {
            qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
        }
1055
#endif
A
Anthony Liguori 已提交
1056 1057 1058 1059 1060 1061 1062 1063
    } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
        qemu_keycode = 0x70;
    } else if (gdk_keycode == 211) { /* backslash */
        qemu_keycode = 0x73;
    } else {
        qemu_keycode = 0;
    }

G
Gerd Hoffmann 已提交
1064 1065 1066
    return qemu_keycode;
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
static gboolean gd_text_key_down(GtkWidget *widget,
                                 GdkEventKey *key, void *opaque)
{
    VirtualConsole *vc = opaque;
    QemuConsole *con = vc->gfx.dcl.con;

    if (key->length) {
        kbd_put_string_console(con, key->string, key->length);
    } else {
        int num = gd_map_keycode(vc->s, gtk_widget_get_display(widget),
                                 key->hardware_keycode);
        int qcode = qemu_input_key_number_to_qcode(num);
        kbd_put_qcode_console(con, qcode);
    }
    return TRUE;
}

G
Gerd Hoffmann 已提交
1084 1085 1086 1087 1088 1089 1090 1091
static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
{
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
    int gdk_keycode = key->hardware_keycode;
    int qemu_keycode;
    int i;

1092 1093 1094 1095 1096
    if (s->ignore_keys) {
        s->ignore_keys = (key->type == GDK_KEY_PRESS);
        return TRUE;
    }

M
Martin Decky 已提交
1097 1098 1099 1100 1101 1102
    if (key->keyval == GDK_KEY_Pause) {
        qemu_input_event_send_key_qcode(vc->gfx.dcl.con, Q_KEY_CODE_PAUSE,
                                        key->type == GDK_KEY_PRESS);
        return TRUE;
    }

1103 1104
    qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
                                  gdk_keycode);
G
Gerd Hoffmann 已提交
1105

G
Gerd Hoffmann 已提交
1106
    trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
1107
                       (key->type == GDK_KEY_PRESS) ? "down" : "up");
A
Anthony Liguori 已提交
1108

1109 1110 1111 1112 1113 1114
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        if (qemu_keycode == modifier_keycode[i]) {
            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
        }
    }

1115
    qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
1116
                                     key->type == GDK_KEY_PRESS);
A
Anthony Liguori 已提交
1117 1118 1119 1120

    return TRUE;
}

1121 1122 1123 1124 1125 1126 1127 1128
static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
{
    if (event->type == GDK_MOTION_NOTIFY) {
        return gd_motion_event(widget, &event->motion, opaque);
    }
    return FALSE;
}

A
Anthony Liguori 已提交
1129 1130
/** Window Menu Actions **/

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
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 已提交
1155 1156 1157 1158 1159 1160 1161 1162
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;
1163
    VirtualConsole *vc = gd_vc_find_by_menu(s);
1164
    GtkNotebook *nb = GTK_NOTEBOOK(s->notebook);
1165
    gint page;
A
Anthony Liguori 已提交
1166

1167 1168
    gtk_release_modifiers(s);
    if (vc) {
1169 1170
        page = gtk_notebook_page_num(nb, vc->tab_item);
        gtk_notebook_set_current_page(nb, page);
J
Jan Kiszka 已提交
1171
        gtk_widget_grab_focus(vc->focus);
A
Anthony Liguori 已提交
1172
    }
1173
    s->ignore_keys = false;
A
Anthony Liguori 已提交
1174 1175
}

1176 1177 1178
static void gd_accel_switch_vc(void *opaque)
{
    VirtualConsole *vc = opaque;
1179

1180
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item), TRUE);
1181 1182 1183 1184
#if !GTK_CHECK_VERSION(3, 0, 0)
    /* GTK2 sends the accel key to the target console - ignore this until */
    vc->s->ignore_keys = true;
#endif
1185 1186
}

A
Anthony Liguori 已提交
1187 1188 1189
static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1190
    VirtualConsole *vc = gd_vc_find_current(s);
A
Anthony Liguori 已提交
1191 1192 1193 1194 1195 1196

    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);
    }
1197
    gd_update_windowsize(vc);
A
Anthony Liguori 已提交
1198 1199
}

1200 1201 1202 1203 1204 1205 1206
static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
                                    void *opaque)
{
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;

    gtk_widget_set_sensitive(vc->menu_item, true);
1207
    gd_widget_reparent(vc->window, s->notebook, vc->tab_item);
1208 1209 1210 1211 1212 1213 1214
    gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
                                    vc->tab_item, vc->label);
    gtk_widget_destroy(vc->window);
    vc->window = NULL;
    return TRUE;
}

1215 1216 1217 1218 1219 1220 1221 1222
static gboolean gd_win_grab(void *opaque)
{
    VirtualConsole *vc = opaque;

    fprintf(stderr, "%s: %s\n", __func__, vc->label);
    if (vc->s->ptr_owner) {
        gd_ungrab_pointer(vc->s);
    } else {
G
Gerd Hoffmann 已提交
1223
        gd_grab_pointer(vc, "user-request-detached-tab");
1224 1225 1226 1227
    }
    return TRUE;
}

1228 1229 1230 1231 1232
static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
    VirtualConsole *vc = gd_vc_find_current(s);

1233 1234
    if (vc->type == GD_VC_GFX &&
        qemu_console_is_graphic(vc->gfx.dcl.con)) {
G
Gerd Hoffmann 已提交
1235 1236
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
1237 1238 1239 1240
    }
    if (!vc->window) {
        gtk_widget_set_sensitive(vc->menu_item, false);
        vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1241
        gd_widget_reparent(s->notebook, vc->window, vc->tab_item);
1242 1243 1244 1245

        g_signal_connect(vc->window, "delete-event",
                         G_CALLBACK(gd_tab_window_close), vc);
        gtk_widget_show_all(vc->window);
G
Gerd Hoffmann 已提交
1246

1247 1248 1249
        if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
            GtkAccelGroup *ag = gtk_accel_group_new();
            gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);
1250

1251 1252 1253 1254
            GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab),
                                               vc, NULL);
            gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);
        }
1255

G
Gerd Hoffmann 已提交
1256
        gd_update_geometry_hints(vc);
G
Gerd Hoffmann 已提交
1257
        gd_update_caption(s);
1258 1259 1260
    }
}

1261 1262 1263
static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1264
    VirtualConsole *vc = gd_vc_find_current(s);
1265

1266
    if (!s->full_screen) {
1267
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
1268
        gtk_widget_hide(s->menu_bar);
1269 1270
        if (vc->type == GD_VC_GFX) {
            gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
1271
        }
1272
        gtk_window_fullscreen(GTK_WINDOW(s->window));
1273 1274 1275 1276
        s->full_screen = TRUE;
    } else {
        gtk_window_unfullscreen(GTK_WINDOW(s->window));
        gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
1277
        gtk_widget_show(s->menu_bar);
1278
        s->full_screen = FALSE;
1279 1280 1281
        if (vc->type == GD_VC_GFX) {
            vc->gfx.scale_x = 1.0;
            vc->gfx.scale_y = 1.0;
G
Gerd Hoffmann 已提交
1282
            gd_update_windowsize(vc);
1283
        }
1284 1285
    }

1286
    gd_update_cursor(vc);
1287 1288
}

1289 1290 1291 1292 1293 1294
static void gd_accel_full_screen(void *opaque)
{
    GtkDisplayState *s = opaque;
    gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
}

1295 1296 1297
static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1298
    VirtualConsole *vc = gd_vc_find_current(s);
1299 1300 1301 1302

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

G
Gerd Hoffmann 已提交
1303 1304
    vc->gfx.scale_x += VC_SCALE_STEP;
    vc->gfx.scale_y += VC_SCALE_STEP;
1305

1306
    gd_update_windowsize(vc);
1307 1308 1309 1310 1311
}

static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1312
    VirtualConsole *vc = gd_vc_find_current(s);
1313 1314 1315 1316

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

G
Gerd Hoffmann 已提交
1317 1318
    vc->gfx.scale_x -= VC_SCALE_STEP;
    vc->gfx.scale_y -= VC_SCALE_STEP;
1319

G
Gerd Hoffmann 已提交
1320 1321
    vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
    vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1322

1323
    gd_update_windowsize(vc);
1324 1325 1326 1327 1328
}

static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1329
    VirtualConsole *vc = gd_vc_find_current(s);
1330

1331 1332
    vc->gfx.scale_x = 1.0;
    vc->gfx.scale_y = 1.0;
1333

1334
    gd_update_windowsize(vc);
1335 1336 1337 1338 1339
}

static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1340
    VirtualConsole *vc = gd_vc_find_current(s);
1341 1342 1343 1344 1345

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
        s->free_scale = TRUE;
    } else {
        s->free_scale = FALSE;
1346 1347
        vc->gfx.scale_x = 1.0;
        vc->gfx.scale_y = 1.0;
1348 1349
    }

G
Gerd Hoffmann 已提交
1350
    gd_update_windowsize(vc);
1351
    gd_update_full_redraw(vc);
1352 1353
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
#if GTK_CHECK_VERSION(3, 20, 0)
static void gd_grab_update(VirtualConsole *vc, bool kbd, bool ptr)
{
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
    GdkSeat *seat = gdk_display_get_default_seat(display);
    GdkWindow *window = gtk_widget_get_window(vc->gfx.drawing_area);
    GdkSeatCapabilities caps = 0;
    GdkCursor *cursor = NULL;

    if (kbd) {
        caps |= GDK_SEAT_CAPABILITY_KEYBOARD;
    }
    if (ptr) {
        caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
        cursor = vc->s->null_cursor;
    }

    if (caps) {
        gdk_seat_grab(seat, window, caps, false, cursor,
                      NULL, NULL, NULL);
    } else {
        gdk_seat_ungrab(seat);
    }
}
#elif GTK_CHECK_VERSION(3, 0, 0)
1379 1380 1381 1382
static void gd_grab_devices(VirtualConsole *vc, bool grab,
                            GdkInputSource source, GdkEventMask mask,
                            GdkCursor *cursor)
{
1383
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1384
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
1385 1386 1387 1388
    GList *devs = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devs;

    for (tmp = devs; tmp; tmp = tmp->next) {
1389
        GdkDevice *dev = tmp->data;
1390 1391 1392 1393 1394 1395 1396 1397 1398
        if (gdk_device_get_source(dev) != source) {
            continue;
        }
        if (grab) {
            GdkWindow *win = gtk_widget_get_window(vc->gfx.drawing_area);
            gdk_device_grab(dev, win, GDK_OWNERSHIP_NONE, FALSE,
                            mask, cursor, GDK_CURRENT_TIME);
        } else {
            gdk_device_ungrab(dev, GDK_CURRENT_TIME);
1399 1400
        }
    }
1401 1402 1403 1404
    g_list_free(devs);
}
#endif

G
Gerd Hoffmann 已提交
1405
static void gd_grab_keyboard(VirtualConsole *vc, const char *reason)
1406
{
1407 1408 1409 1410 1411 1412 1413 1414
    if (vc->s->kbd_owner) {
        if (vc->s->kbd_owner == vc) {
            return;
        } else {
            gd_ungrab_keyboard(vc->s);
        }
    }

1415 1416 1417
#if GTK_CHECK_VERSION(3, 20, 0)
    gd_grab_update(vc, true, vc->s->ptr_owner == vc);
#elif GTK_CHECK_VERSION(3, 0, 0)
1418 1419 1420
    gd_grab_devices(vc, true, GDK_SOURCE_KEYBOARD,
                   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
                   NULL);
1421
#else
1422
    gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1423 1424
                      FALSE,
                      GDK_CURRENT_TIME);
1425
#endif
G
Gerd Hoffmann 已提交
1426
    vc->s->kbd_owner = vc;
1427
    gd_update_caption(vc->s);
G
Gerd Hoffmann 已提交
1428
    trace_gd_grab(vc->label, "kbd", reason);
1429 1430
}

G
Gerd Hoffmann 已提交
1431
static void gd_ungrab_keyboard(GtkDisplayState *s)
1432
{
G
Gerd Hoffmann 已提交
1433 1434 1435 1436 1437 1438 1439
    VirtualConsole *vc = s->kbd_owner;

    if (vc == NULL) {
        return;
    }
    s->kbd_owner = NULL;

1440 1441 1442
#if GTK_CHECK_VERSION(3, 20, 0)
    gd_grab_update(vc, false, vc->s->ptr_owner == vc);
#elif GTK_CHECK_VERSION(3, 0, 0)
1443
    gd_grab_devices(vc, false, GDK_SOURCE_KEYBOARD, 0, NULL);
1444
#else
1445
    gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1446
#endif
1447
    gd_update_caption(s);
G
Gerd Hoffmann 已提交
1448
    trace_gd_ungrab(vc->label, "kbd");
1449 1450
}

G
Gerd Hoffmann 已提交
1451
static void gd_grab_pointer(VirtualConsole *vc, const char *reason)
1452
{
1453
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1454 1455 1456 1457 1458 1459 1460 1461 1462

    if (vc->s->ptr_owner) {
        if (vc->s->ptr_owner == vc) {
            return;
        } else {
            gd_ungrab_pointer(vc->s);
        }
    }

1463 1464 1465 1466 1467
#if GTK_CHECK_VERSION(3, 20, 0)
    gd_grab_update(vc, vc->s->kbd_owner == vc, true);
    gdk_device_get_position(gd_get_pointer(display),
                            NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
#elif GTK_CHECK_VERSION(3, 0, 0)
1468 1469 1470 1471 1472 1473 1474
    gd_grab_devices(vc, true, GDK_SOURCE_MOUSE,
                    GDK_POINTER_MOTION_MASK |
                    GDK_BUTTON_PRESS_MASK |
                    GDK_BUTTON_RELEASE_MASK |
                    GDK_BUTTON_MOTION_MASK |
                    GDK_SCROLL_MASK,
                    vc->s->null_cursor);
1475
    gdk_device_get_position(gd_get_pointer(display),
1476
                            NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1477
#else
1478
    gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1479 1480 1481 1482 1483 1484 1485
                     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 */
1486
                     vc->s->null_cursor,
1487
                     GDK_CURRENT_TIME);
1488
    gdk_display_get_pointer(display, NULL,
1489
                            &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1490
#endif
G
Gerd Hoffmann 已提交
1491
    vc->s->ptr_owner = vc;
1492
    gd_update_caption(vc->s);
G
Gerd Hoffmann 已提交
1493
    trace_gd_grab(vc->label, "ptr", reason);
1494 1495
}

G
Gerd Hoffmann 已提交
1496
static void gd_ungrab_pointer(GtkDisplayState *s)
1497
{
G
Gerd Hoffmann 已提交
1498
    VirtualConsole *vc = s->ptr_owner;
1499
    GdkDisplay *display;
G
Gerd Hoffmann 已提交
1500 1501 1502 1503 1504 1505

    if (vc == NULL) {
        return;
    }
    s->ptr_owner = NULL;

1506
    display = gtk_widget_get_display(vc->gfx.drawing_area);
1507 1508 1509 1510 1511 1512
#if GTK_CHECK_VERSION(3, 20, 0)
    gd_grab_update(vc, vc->s->kbd_owner == vc, false);
    gdk_device_warp(gd_get_pointer(display),
                    gtk_widget_get_screen(vc->gfx.drawing_area),
                    vc->s->grab_x_root, vc->s->grab_y_root);
#elif GTK_CHECK_VERSION(3, 0, 0)
1513
    gd_grab_devices(vc, false, GDK_SOURCE_MOUSE, 0, NULL);
1514
    gdk_device_warp(gd_get_pointer(display),
1515 1516
                    gtk_widget_get_screen(vc->gfx.drawing_area),
                    vc->s->grab_x_root, vc->s->grab_y_root);
1517 1518
#else
    gdk_pointer_ungrab(GDK_CURRENT_TIME);
1519
    gdk_display_warp_pointer(display,
1520 1521
                             gtk_widget_get_screen(vc->gfx.drawing_area),
                             vc->s->grab_x_root, vc->s->grab_y_root);
1522
#endif
1523
    gd_update_caption(s);
G
Gerd Hoffmann 已提交
1524
    trace_gd_ungrab(vc->label, "ptr");
1525 1526
}

1527 1528 1529
static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1530
    VirtualConsole *vc = gd_vc_find_current(s);
1531 1532

    if (gd_is_grab_active(s)) {
G
Gerd Hoffmann 已提交
1533 1534
        gd_grab_keyboard(vc, "user-request-main-window");
        gd_grab_pointer(vc, "user-request-main-window");
1535
    } else {
G
Gerd Hoffmann 已提交
1536 1537
        gd_ungrab_keyboard(s);
        gd_ungrab_pointer(s);
1538 1539
    }

1540
    gd_update_cursor(vc);
1541 1542
}

A
Anthony Liguori 已提交
1543 1544 1545 1546
static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
                           gpointer data)
{
    GtkDisplayState *s = data;
1547
    VirtualConsole *vc;
1548
    gboolean on_vga;
A
Anthony Liguori 已提交
1549 1550 1551 1552 1553

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

1554 1555 1556 1557 1558 1559
#ifdef VTE_RESIZE_HACK
    vc = gd_vc_find_current(s);
    if (vc && vc->type == GD_VC_VTE) {
        gtk_widget_hide(vc->vte.terminal);
    }
#endif
1560 1561 1562 1563
    vc = gd_vc_find_by_page(s, arg2);
    if (!vc) {
        return;
    }
1564 1565 1566 1567 1568
#ifdef VTE_RESIZE_HACK
    if (vc->type == GD_VC_VTE) {
        gtk_widget_show(vc->vte.terminal);
    }
#endif
1569 1570
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
                                   TRUE);
1571 1572
    on_vga = (vc->type == GD_VC_GFX &&
              qemu_console_is_graphic(vc->gfx.dcl.con));
1573 1574 1575
    if (!on_vga) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
1576 1577 1578
    } else if (s->full_screen) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       TRUE);
1579 1580 1581
    }
    gtk_widget_set_sensitive(s->grab_item, on_vga);

G
Gerd Hoffmann 已提交
1582
    gd_update_windowsize(vc);
1583
    gd_update_cursor(vc);
A
Anthony Liguori 已提交
1584 1585
}

1586 1587
static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
                               gpointer opaque)
1588
{
1589 1590
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1591

G
Gerd Hoffmann 已提交
1592
    if (gd_grab_on_hover(s)) {
G
Gerd Hoffmann 已提交
1593
        gd_grab_keyboard(vc, "grab-on-hover");
1594 1595 1596 1597
    }
    return TRUE;
}

1598 1599
static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
                               gpointer opaque)
1600
{
1601 1602
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1603

G
Gerd Hoffmann 已提交
1604
    if (gd_grab_on_hover(s)) {
G
Gerd Hoffmann 已提交
1605
        gd_ungrab_keyboard(s);
1606 1607 1608 1609
    }
    return TRUE;
}

1610
static gboolean gd_focus_out_event(GtkWidget *widget,
1611
                                   GdkEventCrossing *crossing, gpointer opaque)
1612
{
1613 1614
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1615 1616 1617 1618 1619

    gtk_release_modifiers(s);
    return TRUE;
}

G
Gerd Hoffmann 已提交
1620 1621 1622 1623 1624
static gboolean gd_configure(GtkWidget *widget,
                             GdkEventConfigure *cfg, gpointer opaque)
{
    VirtualConsole *vc = opaque;

1625
    gd_set_ui_info(vc, cfg->width, cfg->height);
G
Gerd Hoffmann 已提交
1626 1627 1628
    return FALSE;
}

1629 1630
/** Virtual Console Callbacks **/

1631
static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1632
                               int idx, GSList *group, GtkWidget *view_menu)
1633
{
1634
    vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1635 1636 1637 1638 1639 1640 1641 1642
    gtk_accel_group_connect(s->accel_group, GDK_KEY_1 + idx,
            HOTKEY_MODIFIERS, 0,
            g_cclosure_new_swap(G_CALLBACK(gd_accel_switch_vc), vc, NULL));
#if GTK_CHECK_VERSION(3, 8, 0)
    gtk_accel_label_set_accel(
            GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(vc->menu_item))),
            GDK_KEY_1 + idx, HOTKEY_MODIFIERS);
#endif
1643 1644 1645 1646 1647

    g_signal_connect(vc->menu_item, "activate",
                     G_CALLBACK(gd_menu_switch_vc), s);
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);

1648
    group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
1649 1650 1651
    return group;
}

1652
#if defined(CONFIG_VTE)
1653 1654 1655 1656 1657 1658 1659 1660
static void gd_menu_copy(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
    VirtualConsole *vc = gd_vc_find_current(s);

    vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte.terminal));
}

1661 1662 1663 1664 1665 1666
static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
{
    VirtualConsole *vc = opaque;

    if (gtk_adjustment_get_upper(adjustment) >
        gtk_adjustment_get_page_size(adjustment)) {
G
Gerd Hoffmann 已提交
1667
        gtk_widget_show(vc->vte.scrollbar);
1668
    } else {
G
Gerd Hoffmann 已提交
1669
        gtk_widget_hide(vc->vte.scrollbar);
1670 1671 1672
    }
}

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

G
Gerd Hoffmann 已提交
1677
    vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
C
Cole Robinson 已提交
1678
    return len;
1679 1680
}

P
Paolo Bonzini 已提交
1681 1682 1683 1684 1685 1686 1687
static void gd_vc_chr_set_echo(CharDriverState *chr, bool echo)
{
    VirtualConsole *vc = chr->opaque;

    vc->vte.echo = echo;
}

1688 1689 1690
static int nb_vcs;
static CharDriverState *vcs[MAX_VCS];

1691
static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
1692
{
1693
    ChardevCommon *common = qapi_ChardevVC_base(vc);
1694 1695
    CharDriverState *chr;

1696 1697 1698 1699 1700
    chr = qemu_chr_alloc(common, errp);
    if (!chr) {
        return NULL;
    }

1701
    chr->chr_write = gd_vc_chr_write;
P
Paolo Bonzini 已提交
1702 1703 1704
    chr->chr_set_echo = gd_vc_chr_set_echo;

    /* Temporary, until gd_vc_vte_init runs.  */
1705
    chr->opaque = g_new0(VirtualConsole, 1);
P
Paolo Bonzini 已提交
1706

1707 1708 1709 1710 1711
    vcs[nb_vcs++] = chr;

    return chr;
}

C
Cole Robinson 已提交
1712 1713
static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
                         gpointer user_data)
1714
{
C
Cole Robinson 已提交
1715
    VirtualConsole *vc = user_data;
1716

P
Paolo Bonzini 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    if (vc->vte.echo) {
        VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
        int i;
        for (i = 0; i < size; i++) {
            uint8_t c = text[i];
            if (c >= 128 || isprint(c)) {
                /* 8-bit characters are considered printable.  */
                vte_terminal_feed(term, &text[i], 1);
            } else if (c == '\r' || c == '\n') {
                vte_terminal_feed(term, "\r\n", 2);
            } else {
                char ctrl[2] = { '^', 0};
                ctrl[1] = text[i] ^ 64;
                vte_terminal_feed(term, ctrl, 2);
            }
        }
    }

G
Gerd Hoffmann 已提交
1735
    qemu_chr_be_write(vc->vte.chr, (uint8_t  *)text, (unsigned int)size);
1736 1737 1738
    return TRUE;
}

1739 1740
static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
                              CharDriverState *chr, int idx,
1741
                              GSList *group, GtkWidget *view_menu)
1742 1743
{
    char buffer[32];
1744 1745 1746
    GtkWidget *box;
    GtkWidget *scrollbar;
    GtkAdjustment *vadjustment;
P
Paolo Bonzini 已提交
1747
    VirtualConsole *tmp_vc = chr->opaque;
1748

1749
    vc->s = s;
P
Paolo Bonzini 已提交
1750 1751
    vc->vte.echo = tmp_vc->vte.echo;

1752
    vc->vte.chr = chr;
P
Paolo Bonzini 已提交
1753 1754
    chr->opaque = vc;
    g_free(tmp_vc);
1755

1756
    snprintf(buffer, sizeof(buffer), "vc%d", idx);
1757 1758 1759
    vc->label = g_strdup_printf("%s", vc->vte.chr->label
                                ? vc->vte.chr->label : buffer);
    group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1760

G
Gerd Hoffmann 已提交
1761 1762
    vc->vte.terminal = vte_terminal_new();
    g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1763

P
Paolo Bonzini 已提交
1764 1765 1766
    /* The documentation says that the default is UTF-8, but actually it is
     * 7-bit ASCII at least in VTE 0.38.
     */
O
Olaf Hering 已提交
1767
#if VTE_CHECK_VERSION(0, 38, 0)
P
Paolo Bonzini 已提交
1768 1769 1770 1771 1772
    vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8", NULL);
#else
    vte_terminal_set_encoding(VTE_TERMINAL(vc->vte.terminal), "UTF-8");
#endif

G
Gerd Hoffmann 已提交
1773
    vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
G
Gerd Hoffmann 已提交
1774 1775
    vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
                          VC_TERM_X_MIN, VC_TERM_Y_MIN);
1776

1777
#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
G
Gerd Hoffmann 已提交
1778 1779
    vadjustment = gtk_scrollable_get_vadjustment
        (GTK_SCROLLABLE(vc->vte.terminal));
1780
#else
G
Gerd Hoffmann 已提交
1781
    vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
#endif

#if GTK_CHECK_VERSION(3, 0, 0)
    box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
#else
    box = gtk_hbox_new(false, 2);
    scrollbar = gtk_vscrollbar_new(vadjustment);
#endif

G
Gerd Hoffmann 已提交
1792
    gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1793 1794
    gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);

G
Gerd Hoffmann 已提交
1795 1796
    vc->vte.box = box;
    vc->vte.scrollbar = scrollbar;
1797 1798 1799

    g_signal_connect(vadjustment, "changed",
                     G_CALLBACK(gd_vc_adjustment_changed), vc);
1800

1801
    vc->type = GD_VC_VTE;
G
Gerd Hoffmann 已提交
1802
    vc->tab_item = box;
J
Jan Kiszka 已提交
1803
    vc->focus = vc->vte.terminal;
G
Gerd Hoffmann 已提交
1804
    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1805
                             gtk_label_new(vc->label));
1806

G
Gerd Hoffmann 已提交
1807
    qemu_chr_be_generic_open(vc->vte.chr);
1808 1809

    return group;
A
Anthony Liguori 已提交
1810 1811
}

1812
static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1813 1814 1815 1816 1817
                        GtkWidget *view_menu)
{
    int i;

    for (i = 0; i < nb_vcs; i++) {
1818 1819
        VirtualConsole *vc = &s->vc[s->nb_vcs];
        group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1820 1821 1822 1823 1824
        s->nb_vcs++;
    }
}
#endif /* CONFIG_VTE */

A
Anthony Liguori 已提交
1825 1826
/** Window Creation **/

1827 1828 1829 1830 1831
static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
{
#if GTK_CHECK_VERSION(3, 0, 0)
    g_signal_connect(vc->gfx.drawing_area, "draw",
                     G_CALLBACK(gd_draw_event), vc);
1832 1833 1834 1835 1836 1837 1838 1839 1840
#if defined(CONFIG_GTK_GL)
    if (display_opengl) {
        /* wire up GtkGlArea events */
        g_signal_connect(vc->gfx.drawing_area, "render",
                         G_CALLBACK(gd_render_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "resize",
                         G_CALLBACK(gd_resize_event), vc);
    }
#endif
1841 1842 1843 1844
#else
    g_signal_connect(vc->gfx.drawing_area, "expose-event",
                     G_CALLBACK(gd_expose_event), vc);
#endif
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
    if (qemu_console_is_graphic(vc->gfx.dcl.con)) {
        g_signal_connect(vc->gfx.drawing_area, "event",
                         G_CALLBACK(gd_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "button-press-event",
                         G_CALLBACK(gd_button_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "button-release-event",
                         G_CALLBACK(gd_button_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "scroll-event",
                         G_CALLBACK(gd_scroll_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "key-press-event",
                         G_CALLBACK(gd_key_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "key-release-event",
                         G_CALLBACK(gd_key_event), vc);

        g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
                         G_CALLBACK(gd_enter_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
                         G_CALLBACK(gd_leave_event), vc);
        g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
                         G_CALLBACK(gd_focus_out_event), vc);
G
Gerd Hoffmann 已提交
1865 1866
        g_signal_connect(vc->gfx.drawing_area, "configure-event",
                         G_CALLBACK(gd_configure), vc);
1867 1868 1869 1870
    } else {
        g_signal_connect(vc->gfx.drawing_area, "key-press-event",
                         G_CALLBACK(gd_text_key_down), vc);
    }
1871 1872
}

A
Anthony Liguori 已提交
1873 1874 1875 1876
static void gd_connect_signals(GtkDisplayState *s)
{
    g_signal_connect(s->show_tabs_item, "activate",
                     G_CALLBACK(gd_menu_show_tabs), s);
1877 1878
    g_signal_connect(s->untabify_item, "activate",
                     G_CALLBACK(gd_menu_untabify), s);
A
Anthony Liguori 已提交
1879 1880 1881 1882

    g_signal_connect(s->window, "delete-event",
                     G_CALLBACK(gd_window_close), s);

1883 1884 1885 1886 1887 1888
    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 已提交
1889 1890
    g_signal_connect(s->quit_item, "activate",
                     G_CALLBACK(gd_menu_quit), s);
1891 1892 1893 1894
#if defined(CONFIG_VTE)
    g_signal_connect(s->copy_item, "activate",
                     G_CALLBACK(gd_menu_copy), s);
#endif
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
    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);
1905 1906
    g_signal_connect(s->grab_item, "activate",
                     G_CALLBACK(gd_menu_grab_input), s);
A
Anthony Liguori 已提交
1907 1908 1909 1910
    g_signal_connect(s->notebook, "switch-page",
                     G_CALLBACK(gd_change_page), s);
}

1911
static GtkWidget *gd_create_menu_machine(GtkDisplayState *s)
A
Anthony Liguori 已提交
1912
{
A
Anthony Liguori 已提交
1913
    GtkWidget *machine_menu;
A
Anthony Liguori 已提交
1914 1915
    GtkWidget *separator;

A
Anthony Liguori 已提交
1916
    machine_menu = gtk_menu_new();
1917
    gtk_menu_set_accel_group(GTK_MENU(machine_menu), s->accel_group);
1918 1919

    s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
A
Anthony Liguori 已提交
1920
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1921 1922

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1923
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1924

1925
    s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
A
Anthony Liguori 已提交
1926
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1927

1928
    s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
A
Anthony Liguori 已提交
1929
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1930 1931

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1932
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
A
Anthony Liguori 已提交
1933

1934
    s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
A
Anthony Liguori 已提交
1935
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1936
                                 "<QEMU>/Machine/Quit");
1937
    gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1938
                            GDK_KEY_q, HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1939
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
A
Anthony Liguori 已提交
1940

A
Anthony Liguori 已提交
1941 1942 1943
    return machine_menu;
}

1944
static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1945
                              QemuConsole *con, int idx,
1946 1947
                              GSList *group, GtkWidget *view_menu)
{
1948
    vc->label = qemu_console_get_label(con);
1949 1950 1951 1952
    vc->s = s;
    vc->gfx.scale_x = 1.0;
    vc->gfx.scale_y = 1.0;

1953 1954
#if defined(CONFIG_OPENGL)
    if (display_opengl) {
1955 1956 1957 1958 1959
#if defined(CONFIG_GTK_GL)
        vc->gfx.drawing_area = gtk_gl_area_new();
        vc->gfx.dcl.ops = &dcl_gl_area_ops;
#else
        vc->gfx.drawing_area = gtk_drawing_area_new();
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
        /*
         * gtk_widget_set_double_buffered() was deprecated in 3.14.
         * It is required for opengl rendering on X11 though.  A
         * proper replacement (native opengl support) is only
         * available in 3.16+.  Silence the warning if possible.
         */
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
        gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
#pragma GCC diagnostic pop
#endif
        vc->gfx.dcl.ops = &dcl_egl_ops;
1975
#endif /* CONFIG_GTK_GL */
1976 1977 1978
    } else
#endif
    {
1979
        vc->gfx.drawing_area = gtk_drawing_area_new();
1980 1981 1982
        vc->gfx.dcl.ops = &dcl_ops;
    }

1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000

    gtk_widget_add_events(vc->gfx.drawing_area,
                          GDK_POINTER_MOTION_MASK |
                          GDK_BUTTON_PRESS_MASK |
                          GDK_BUTTON_RELEASE_MASK |
                          GDK_BUTTON_MOTION_MASK |
                          GDK_ENTER_NOTIFY_MASK |
                          GDK_LEAVE_NOTIFY_MASK |
                          GDK_SCROLL_MASK |
                          GDK_KEY_PRESS_MASK);
    gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);

    vc->type = GD_VC_GFX;
    vc->tab_item = vc->gfx.drawing_area;
    vc->focus = vc->gfx.drawing_area;
    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
                             vc->tab_item, gtk_label_new(vc->label));

2001 2002 2003
    vc->gfx.dcl.con = con;
    register_displaychangelistener(&vc->gfx.dcl);

2004 2005 2006
    gd_connect_vc_gfx_signals(vc);
    group = gd_vc_menu_init(s, vc, idx, group, view_menu);

G
Gerd Hoffmann 已提交
2007 2008
    if (dpy_ui_info_supported(vc->gfx.dcl.con)) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_fit_item));
2009
        s->free_scale = true;
G
Gerd Hoffmann 已提交
2010 2011
    }

2012 2013 2014
    return group;
}

2015
static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
A
Anthony Liguori 已提交
2016 2017 2018 2019
{
    GSList *group = NULL;
    GtkWidget *view_menu;
    GtkWidget *separator;
2020 2021
    QemuConsole *con;
    int vc;
A
Anthony Liguori 已提交
2022 2023

    view_menu = gtk_menu_new();
2024
    gtk_menu_set_accel_group(GTK_MENU(view_menu), s->accel_group);
A
Anthony Liguori 已提交
2025

2026
    s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
2027

2028 2029 2030 2031 2032
#if defined(CONFIG_VTE)
    s->copy_item = gtk_menu_item_new_with_mnemonic(_("_Copy"));
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->copy_item);
#endif

2033 2034 2035 2036 2037 2038 2039
    gtk_accel_group_connect(s->accel_group, GDK_KEY_f, HOTKEY_MODIFIERS, 0,
            g_cclosure_new_swap(G_CALLBACK(gd_accel_full_screen), s, NULL));
#if GTK_CHECK_VERSION(3, 8, 0)
    gtk_accel_label_set_accel(
            GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->full_screen_item))),
            GDK_KEY_f, HOTKEY_MODIFIERS);
#endif
A
Anthony Liguori 已提交
2040
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
2041 2042

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

2045
    s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2046 2047
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
                                 "<QEMU>/View/Zoom In");
J
Jan Kiszka 已提交
2048 2049
    gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
2050
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
2051

2052
    s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2053 2054
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
                                 "<QEMU>/View/Zoom Out");
J
Jan Kiszka 已提交
2055 2056
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
2057
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
2058

2059
    s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
2060 2061
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
                                 "<QEMU>/View/Zoom Fixed");
J
Jan Kiszka 已提交
2062 2063
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
2064
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
2065

2066
    s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
A
Anthony Liguori 已提交
2067
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
2068 2069

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

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

2075
    s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
2076 2077
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
                                 "<QEMU>/View/Grab Input");
J
Jan Kiszka 已提交
2078 2079
    gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
2080
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
2081

A
Anthony Liguori 已提交
2082
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
2083
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
2084

2085
    /* gfx */
2086 2087
    for (vc = 0;; vc++) {
        con = qemu_console_lookup_by_index(vc);
2088
        if (!con) {
2089 2090 2091 2092 2093 2094
            break;
        }
        group = gd_vc_gfx_init(s, &s->vc[vc], con,
                               vc, group, view_menu);
        s->nb_vcs++;
    }
A
Anthony Liguori 已提交
2095

2096
#if defined(CONFIG_VTE)
2097
    /* vte */
2098
    gd_vcs_init(s, group, view_menu);
2099
#endif
2100

A
Anthony Liguori 已提交
2101
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
2102
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
2103

2104
    s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
A
Anthony Liguori 已提交
2105
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
A
Anthony Liguori 已提交
2106

2107 2108 2109
    s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);

A
Anthony Liguori 已提交
2110 2111 2112 2113 2114
    return view_menu;
}

static void gd_create_menus(GtkDisplayState *s)
{
2115 2116 2117
    s->accel_group = gtk_accel_group_new();
    s->machine_menu = gd_create_menu_machine(s);
    s->view_menu = gd_create_menu_view(s);
A
Anthony Liguori 已提交
2118 2119

    s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
2120 2121 2122
    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 已提交
2123

A
Anthony Liguori 已提交
2124
    s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
A
Anthony Liguori 已提交
2125 2126
    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 已提交
2127

2128 2129
    g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
    gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
A
Anthony Liguori 已提交
2130 2131
}

2132 2133
static void gd_set_keycode_type(GtkDisplayState *s)
{
2134
#ifdef GDK_WINDOWING_X11
2135
    GdkDisplay *display = gtk_widget_get_display(s->window);
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
    if (GDK_IS_X11_DISPLAY(display)) {
        Display *x11_display = gdk_x11_display_get_xdisplay(display);
        XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
                                         XkbUseCoreKbd);
        char *keycodes = NULL;

        if (desc && desc->names) {
            keycodes = XGetAtomName(x11_display, desc->names->keycodes);
        }
        if (keycodes == NULL) {
            fprintf(stderr, "could not lookup keycode name\n");
        } else if (strstart(keycodes, "evdev", NULL)) {
            s->has_evdev = true;
        } else if (!strstart(keycodes, "xfree86", NULL)) {
            fprintf(stderr, "unknown keycodes `%s', please report to "
                    "qemu-devel@nongnu.org\n", keycodes);
        }
2153 2154 2155 2156 2157 2158 2159

        if (desc) {
            XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
        }
        if (keycodes) {
            XFree(keycodes);
        }
2160 2161 2162 2163
    }
#endif
}

2164 2165
static gboolean gtkinit;

2166
void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
A
Anthony Liguori 已提交
2167 2168
{
    GtkDisplayState *s = g_malloc0(sizeof(*s));
S
Stefan Weil 已提交
2169
    char *filename;
M
Max Reitz 已提交
2170
    GdkDisplay *window_display;
A
Anthony Liguori 已提交
2171

2172 2173 2174 2175 2176
    if (!gtkinit) {
        fprintf(stderr, "gtk initialization failed\n");
        exit(1);
    }

A
Anthony Liguori 已提交
2177
    s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2178 2179 2180
#if GTK_CHECK_VERSION(3, 2, 0)
    s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
#else
A
Anthony Liguori 已提交
2181
    s->vbox = gtk_vbox_new(FALSE, 0);
2182
#endif
A
Anthony Liguori 已提交
2183 2184 2185
    s->notebook = gtk_notebook_new();
    s->menu_bar = gtk_menu_bar_new();

2186
    s->free_scale = FALSE;
A
Anthony Liguori 已提交
2187

2188 2189
    /* LC_MESSAGES only. See early_gtk_display_init() for details */
    setlocale(LC_MESSAGES, "");
2190 2191 2192
    bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
    textdomain("qemu");

M
Max Reitz 已提交
2193 2194 2195
    window_display = gtk_widget_get_display(s->window);
    s->null_cursor = gdk_cursor_new_for_display(window_display,
                                                GDK_BLANK_CURSOR);
A
Anthony Liguori 已提交
2196 2197 2198 2199 2200

    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);

A
Anthony Liguori 已提交
2201
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
S
Stefan Weil 已提交
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
    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 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
    gd_create_menus(s);

    gd_connect_signals(s);

    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);

2229 2230 2231
#ifdef VTE_RESIZE_HACK
    {
        VirtualConsole *cur = gd_vc_find_current(s);
F
Fam Zheng 已提交
2232 2233 2234 2235 2236 2237 2238 2239
        if (cur) {
            int i;

            for (i = 0; i < s->nb_vcs; i++) {
                VirtualConsole *vc = &s->vc[i];
                if (vc && vc->type == GD_VC_VTE && vc != cur) {
                    gtk_widget_hide(vc->vte.terminal);
                }
2240
            }
F
Fam Zheng 已提交
2241
            gd_update_windowsize(cur);
2242 2243 2244 2245
        }
    }
#endif

P
Peter Wu 已提交
2246 2247 2248
    if (full_screen) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
    }
2249 2250 2251
    if (grab_on_hover) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
    }
P
Peter Wu 已提交
2252

2253
    gd_set_keycode_type(s);
A
Anthony Liguori 已提交
2254
}
2255

2256
void early_gtk_display_init(int opengl)
2257
{
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
    /* The QEMU code relies on the assumption that it's always run in
     * the C locale. Therefore it is not prepared to deal with
     * operations that produce different results depending on the
     * locale, such as printf's formatting of decimal numbers, and
     * possibly others.
     *
     * Since GTK+ calls setlocale() by default -importing the locale
     * settings from the environment- we must prevent it from doing so
     * using gtk_disable_setlocale().
     *
     * QEMU's GTK+ UI, however, _does_ have translations for some of
     * the menu items. As a trade-off between a functionally correct
     * QEMU and a fully internationalized UI we support importing
     * LC_MESSAGES from the environment (see the setlocale() call
     * earlier in this file). This allows us to display translated
     * messages leaving everything else untouched.
     */
    gtk_disable_setlocale();
2276 2277 2278 2279 2280
    gtkinit = gtk_init_check(NULL, NULL);
    if (!gtkinit) {
        /* don't exit yet, that'll break -help */
        return;
    }
2281 2282 2283 2284 2285 2286 2287

    switch (opengl) {
    case -1: /* default */
    case 0:  /* off */
        break;
    case 1: /* on */
#if defined(CONFIG_OPENGL)
2288 2289 2290
#if defined(CONFIG_GTK_GL)
        gtk_gl_area_init();
#else
2291
        gtk_egl_init();
2292
#endif
2293 2294 2295 2296 2297 2298 2299
#endif
        break;
    default:
        g_assert_not_reached();
        break;
    }

2300 2301 2302 2303
#if defined(CONFIG_VTE)
    register_vc_handler(gd_vc_handler);
#endif
}