qemu/ui/console.c
<<
>>
Prefs
   1/*
   2 * QEMU graphical console
   3 *
   4 * Copyright (c) 2004 Fabrice Bellard
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24
  25#include "qemu/osdep.h"
  26#include "ui/console.h"
  27#include "hw/qdev-core.h"
  28#include "qapi/error.h"
  29#include "qapi/qapi-commands-ui.h"
  30#include "qemu/fifo8.h"
  31#include "qemu/main-loop.h"
  32#include "qemu/module.h"
  33#include "qemu/option.h"
  34#include "qemu/timer.h"
  35#include "chardev/char.h"
  36#include "trace.h"
  37#include "exec/memory.h"
  38#include "io/channel-file.h"
  39#include "qom/object.h"
  40#ifdef CONFIG_PNG
  41#include <png.h>
  42#endif
  43
  44#define DEFAULT_BACKSCROLL 512
  45#define CONSOLE_CURSOR_PERIOD 500
  46
  47typedef struct TextAttributes {
  48    uint8_t fgcol:4;
  49    uint8_t bgcol:4;
  50    uint8_t bold:1;
  51    uint8_t uline:1;
  52    uint8_t blink:1;
  53    uint8_t invers:1;
  54    uint8_t unvisible:1;
  55} TextAttributes;
  56
  57typedef struct TextCell {
  58    uint8_t ch;
  59    TextAttributes t_attrib;
  60} TextCell;
  61
  62#define MAX_ESC_PARAMS 3
  63
  64enum TTYState {
  65    TTY_STATE_NORM,
  66    TTY_STATE_ESC,
  67    TTY_STATE_CSI,
  68};
  69
  70typedef enum {
  71    GRAPHIC_CONSOLE,
  72    TEXT_CONSOLE,
  73    TEXT_CONSOLE_FIXED_SIZE
  74} console_type_t;
  75
  76struct QemuConsole {
  77    Object parent;
  78
  79    int index;
  80    console_type_t console_type;
  81    DisplayState *ds;
  82    DisplaySurface *surface;
  83    DisplayScanout scanout;
  84    int dcls;
  85    DisplayGLCtx *gl;
  86    int gl_block;
  87    QEMUTimer *gl_unblock_timer;
  88    int window_id;
  89
  90    /* Graphic console state.  */
  91    Object *device;
  92    uint32_t head;
  93    QemuUIInfo ui_info;
  94    QEMUTimer *ui_timer;
  95    const GraphicHwOps *hw_ops;
  96    void *hw;
  97
  98    /* Text console state */
  99    int width;
 100    int height;
 101    int total_height;
 102    int backscroll_height;
 103    int x, y;
 104    int x_saved, y_saved;
 105    int y_displayed;
 106    int y_base;
 107    TextAttributes t_attrib_default; /* default text attributes */
 108    TextAttributes t_attrib; /* currently active text attributes */
 109    TextCell *cells;
 110    int text_x[2], text_y[2], cursor_invalidate;
 111    int echo;
 112
 113    int update_x0;
 114    int update_y0;
 115    int update_x1;
 116    int update_y1;
 117
 118    enum TTYState state;
 119    int esc_params[MAX_ESC_PARAMS];
 120    int nb_esc_params;
 121
 122    Chardev *chr;
 123    /* fifo for key pressed */
 124    Fifo8 out_fifo;
 125    CoQueue dump_queue;
 126
 127    QTAILQ_ENTRY(QemuConsole) next;
 128};
 129
 130struct DisplayState {
 131    QEMUTimer *gui_timer;
 132    uint64_t last_update;
 133    uint64_t update_interval;
 134    bool refreshing;
 135    bool have_gfx;
 136    bool have_text;
 137
 138    QLIST_HEAD(, DisplayChangeListener) listeners;
 139};
 140
 141static DisplayState *display_state;
 142static QemuConsole *active_console;
 143static QTAILQ_HEAD(, QemuConsole) consoles =
 144    QTAILQ_HEAD_INITIALIZER(consoles);
 145static bool cursor_visible_phase;
 146static QEMUTimer *cursor_timer;
 147
 148static void text_console_do_init(Chardev *chr, DisplayState *ds);
 149static void dpy_refresh(DisplayState *s);
 150static DisplayState *get_alloc_displaystate(void);
 151static void text_console_update_cursor_timer(void);
 152static void text_console_update_cursor(void *opaque);
 153static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
 154static bool console_compatible_with(QemuConsole *con,
 155                                    DisplayChangeListener *dcl, Error **errp);
 156
 157static void gui_update(void *opaque)
 158{
 159    uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
 160    uint64_t dcl_interval;
 161    DisplayState *ds = opaque;
 162    DisplayChangeListener *dcl;
 163
 164    ds->refreshing = true;
 165    dpy_refresh(ds);
 166    ds->refreshing = false;
 167
 168    QLIST_FOREACH(dcl, &ds->listeners, next) {
 169        dcl_interval = dcl->update_interval ?
 170            dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
 171        if (interval > dcl_interval) {
 172            interval = dcl_interval;
 173        }
 174    }
 175    if (ds->update_interval != interval) {
 176        ds->update_interval = interval;
 177        trace_console_refresh(interval);
 178    }
 179    ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
 180    timer_mod(ds->gui_timer, ds->last_update + interval);
 181}
 182
 183static void gui_setup_refresh(DisplayState *ds)
 184{
 185    DisplayChangeListener *dcl;
 186    bool need_timer = false;
 187    bool have_gfx = false;
 188    bool have_text = false;
 189
 190    QLIST_FOREACH(dcl, &ds->listeners, next) {
 191        if (dcl->ops->dpy_refresh != NULL) {
 192            need_timer = true;
 193        }
 194        if (dcl->ops->dpy_gfx_update != NULL) {
 195            have_gfx = true;
 196        }
 197        if (dcl->ops->dpy_text_update != NULL) {
 198            have_text = true;
 199        }
 200    }
 201
 202    if (need_timer && ds->gui_timer == NULL) {
 203        ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
 204        timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
 205    }
 206    if (!need_timer && ds->gui_timer != NULL) {
 207        timer_free(ds->gui_timer);
 208        ds->gui_timer = NULL;
 209    }
 210
 211    ds->have_gfx = have_gfx;
 212    ds->have_text = have_text;
 213}
 214
 215void graphic_hw_update_done(QemuConsole *con)
 216{
 217    if (con) {
 218        qemu_co_enter_all(&con->dump_queue, NULL);
 219    }
 220}
 221
 222void graphic_hw_update(QemuConsole *con)
 223{
 224    bool async = false;
 225    con = con ? con : active_console;
 226    if (!con) {
 227        return;
 228    }
 229    if (con->hw_ops->gfx_update) {
 230        con->hw_ops->gfx_update(con->hw);
 231        async = con->hw_ops->gfx_update_async;
 232    }
 233    if (!async) {
 234        graphic_hw_update_done(con);
 235    }
 236}
 237
 238static void graphic_hw_gl_unblock_timer(void *opaque)
 239{
 240    warn_report("console: no gl-unblock within one second");
 241}
 242
 243void graphic_hw_gl_block(QemuConsole *con, bool block)
 244{
 245    uint64_t timeout;
 246    assert(con != NULL);
 247
 248    if (block) {
 249        con->gl_block++;
 250    } else {
 251        con->gl_block--;
 252    }
 253    assert(con->gl_block >= 0);
 254    if (!con->hw_ops->gl_block) {
 255        return;
 256    }
 257    if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
 258        return;
 259    }
 260    con->hw_ops->gl_block(con->hw, block);
 261
 262    if (block) {
 263        timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
 264        timeout += 1000; /* one sec */
 265        timer_mod(con->gl_unblock_timer, timeout);
 266    } else {
 267        timer_del(con->gl_unblock_timer);
 268    }
 269}
 270
 271int qemu_console_get_window_id(QemuConsole *con)
 272{
 273    return con->window_id;
 274}
 275
 276void qemu_console_set_window_id(QemuConsole *con, int window_id)
 277{
 278    con->window_id = window_id;
 279}
 280
 281void graphic_hw_invalidate(QemuConsole *con)
 282{
 283    if (!con) {
 284        con = active_console;
 285    }
 286    if (con && con->hw_ops->invalidate) {
 287        con->hw_ops->invalidate(con->hw);
 288    }
 289}
 290
 291#ifdef CONFIG_PNG
 292/**
 293 * png_save: Take a screenshot as PNG
 294 *
 295 * Saves screendump as a PNG file
 296 *
 297 * Returns true for success or false for error.
 298 *
 299 * @fd: File descriptor for PNG file.
 300 * @image: Image data in pixman format.
 301 * @errp: Pointer to an error.
 302 */
 303static bool png_save(int fd, pixman_image_t *image, Error **errp)
 304{
 305    int width = pixman_image_get_width(image);
 306    int height = pixman_image_get_height(image);
 307    png_struct *png_ptr;
 308    png_info *info_ptr;
 309    g_autoptr(pixman_image_t) linebuf =
 310        qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
 311    uint8_t *buf = (uint8_t *)pixman_image_get_data(linebuf);
 312    FILE *f = fdopen(fd, "wb");
 313    int y;
 314    if (!f) {
 315        error_setg_errno(errp, errno,
 316                         "Failed to create file from file descriptor");
 317        return false;
 318    }
 319
 320    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
 321                                      NULL, NULL);
 322    if (!png_ptr) {
 323        error_setg(errp, "PNG creation failed. Unable to write struct");
 324        fclose(f);
 325        return false;
 326    }
 327
 328    info_ptr = png_create_info_struct(png_ptr);
 329
 330    if (!info_ptr) {
 331        error_setg(errp, "PNG creation failed. Unable to write info");
 332        fclose(f);
 333        png_destroy_write_struct(&png_ptr, &info_ptr);
 334        return false;
 335    }
 336
 337    png_init_io(png_ptr, f);
 338
 339    png_set_IHDR(png_ptr, info_ptr, width, height, 8,
 340                 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
 341                 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 342
 343    png_write_info(png_ptr, info_ptr);
 344
 345    for (y = 0; y < height; ++y) {
 346        qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
 347        png_write_row(png_ptr, buf);
 348    }
 349
 350    png_write_end(png_ptr, NULL);
 351
 352    png_destroy_write_struct(&png_ptr, &info_ptr);
 353
 354    if (fclose(f) != 0) {
 355        error_setg_errno(errp, errno,
 356                         "PNG creation failed. Unable to close file");
 357        return false;
 358    }
 359
 360    return true;
 361}
 362
 363#else /* no png support */
 364
 365static bool png_save(int fd, pixman_image_t *image, Error **errp)
 366{
 367    error_setg(errp, "Enable PNG support with libpng for screendump");
 368    return false;
 369}
 370
 371#endif /* CONFIG_PNG */
 372
 373static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
 374{
 375    int width = pixman_image_get_width(image);
 376    int height = pixman_image_get_height(image);
 377    g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
 378    g_autofree char *header = NULL;
 379    g_autoptr(pixman_image_t) linebuf = NULL;
 380    int y;
 381
 382    trace_ppm_save(fd, image);
 383
 384    header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
 385    if (qio_channel_write_all(QIO_CHANNEL(ioc),
 386                              header, strlen(header), errp) < 0) {
 387        return false;
 388    }
 389
 390    linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
 391    for (y = 0; y < height; y++) {
 392        qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
 393        if (qio_channel_write_all(QIO_CHANNEL(ioc),
 394                                  (char *)pixman_image_get_data(linebuf),
 395                                  pixman_image_get_stride(linebuf), errp) < 0) {
 396            return false;
 397        }
 398    }
 399
 400    return true;
 401}
 402
 403static void graphic_hw_update_bh(void *con)
 404{
 405    graphic_hw_update(con);
 406}
 407
 408/* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
 409void coroutine_fn
 410qmp_screendump(const char *filename, bool has_device, const char *device,
 411               bool has_head, int64_t head,
 412               bool has_format, ImageFormat format, Error **errp)
 413{
 414    g_autoptr(pixman_image_t) image = NULL;
 415    QemuConsole *con;
 416    DisplaySurface *surface;
 417    int fd;
 418
 419    if (has_device) {
 420        con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
 421                                                 errp);
 422        if (!con) {
 423            return;
 424        }
 425    } else {
 426        if (has_head) {
 427            error_setg(errp, "'head' must be specified together with 'device'");
 428            return;
 429        }
 430        con = qemu_console_lookup_by_index(0);
 431        if (!con) {
 432            error_setg(errp, "There is no console to take a screendump from");
 433            return;
 434        }
 435    }
 436
 437    if (qemu_co_queue_empty(&con->dump_queue)) {
 438        /* Defer the update, it will restart the pending coroutines */
 439        aio_bh_schedule_oneshot(qemu_get_aio_context(),
 440                                graphic_hw_update_bh, con);
 441    }
 442    qemu_co_queue_wait(&con->dump_queue, NULL);
 443
 444    /*
 445     * All pending coroutines are woken up, while the BQL is held.  No
 446     * further graphic update are possible until it is released.  Take
 447     * an image ref before that.
 448     */
 449    surface = qemu_console_surface(con);
 450    if (!surface) {
 451        error_setg(errp, "no surface");
 452        return;
 453    }
 454    image = pixman_image_ref(surface->image);
 455
 456    fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
 457    if (fd == -1) {
 458        error_setg(errp, "failed to open file '%s': %s", filename,
 459                   strerror(errno));
 460        return;
 461    }
 462
 463    /*
 464     * The image content could potentially be updated as the coroutine
 465     * yields and releases the BQL. It could produce corrupted dump, but
 466     * it should be otherwise safe.
 467     */
 468    if (has_format && format == IMAGE_FORMAT_PNG) {
 469        /* PNG format specified for screendump */
 470        if (!png_save(fd, image, errp)) {
 471            qemu_unlink(filename);
 472        }
 473    } else {
 474        /* PPM format specified/default for screendump */
 475        if (!ppm_save(fd, image, errp)) {
 476            qemu_unlink(filename);
 477        }
 478    }
 479}
 480
 481void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
 482{
 483    if (!con) {
 484        con = active_console;
 485    }
 486    if (con && con->hw_ops->text_update) {
 487        con->hw_ops->text_update(con->hw, chardata);
 488    }
 489}
 490
 491static void vga_fill_rect(QemuConsole *con,
 492                          int posx, int posy, int width, int height,
 493                          pixman_color_t color)
 494{
 495    DisplaySurface *surface = qemu_console_surface(con);
 496    pixman_rectangle16_t rect = {
 497        .x = posx, .y = posy, .width = width, .height = height
 498    };
 499
 500    pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
 501                                 &color, 1, &rect);
 502}
 503
 504/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
 505static void vga_bitblt(QemuConsole *con,
 506                       int xs, int ys, int xd, int yd, int w, int h)
 507{
 508    DisplaySurface *surface = qemu_console_surface(con);
 509
 510    pixman_image_composite(PIXMAN_OP_SRC,
 511                           surface->image, NULL, surface->image,
 512                           xs, ys, 0, 0, xd, yd, w, h);
 513}
 514
 515/***********************************************************/
 516/* basic char display */
 517
 518#define FONT_HEIGHT 16
 519#define FONT_WIDTH 8
 520
 521#include "vgafont.h"
 522
 523#define QEMU_RGB(r, g, b)                                               \
 524    { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
 525
 526static const pixman_color_t color_table_rgb[2][8] = {
 527    {   /* dark */
 528        [QEMU_COLOR_BLACK]   = QEMU_RGB(0x00, 0x00, 0x00),  /* black */
 529        [QEMU_COLOR_BLUE]    = QEMU_RGB(0x00, 0x00, 0xaa),  /* blue */
 530        [QEMU_COLOR_GREEN]   = QEMU_RGB(0x00, 0xaa, 0x00),  /* green */
 531        [QEMU_COLOR_CYAN]    = QEMU_RGB(0x00, 0xaa, 0xaa),  /* cyan */
 532        [QEMU_COLOR_RED]     = QEMU_RGB(0xaa, 0x00, 0x00),  /* red */
 533        [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa),  /* magenta */
 534        [QEMU_COLOR_YELLOW]  = QEMU_RGB(0xaa, 0xaa, 0x00),  /* yellow */
 535        [QEMU_COLOR_WHITE]   = QEMU_RGB(0xaa, 0xaa, 0xaa),  /* white */
 536    },
 537    {   /* bright */
 538        [QEMU_COLOR_BLACK]   = QEMU_RGB(0x00, 0x00, 0x00),  /* black */
 539        [QEMU_COLOR_BLUE]    = QEMU_RGB(0x00, 0x00, 0xff),  /* blue */
 540        [QEMU_COLOR_GREEN]   = QEMU_RGB(0x00, 0xff, 0x00),  /* green */
 541        [QEMU_COLOR_CYAN]    = QEMU_RGB(0x00, 0xff, 0xff),  /* cyan */
 542        [QEMU_COLOR_RED]     = QEMU_RGB(0xff, 0x00, 0x00),  /* red */
 543        [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff),  /* magenta */
 544        [QEMU_COLOR_YELLOW]  = QEMU_RGB(0xff, 0xff, 0x00),  /* yellow */
 545        [QEMU_COLOR_WHITE]   = QEMU_RGB(0xff, 0xff, 0xff),  /* white */
 546    }
 547};
 548
 549static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
 550                          TextAttributes *t_attrib)
 551{
 552    static pixman_image_t *glyphs[256];
 553    DisplaySurface *surface = qemu_console_surface(s);
 554    pixman_color_t fgcol, bgcol;
 555
 556    if (t_attrib->invers) {
 557        bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
 558        fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
 559    } else {
 560        fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
 561        bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
 562    }
 563
 564    if (!glyphs[ch]) {
 565        glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
 566    }
 567    qemu_pixman_glyph_render(glyphs[ch], surface->image,
 568                             &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
 569}
 570
 571static void text_console_resize(QemuConsole *s)
 572{
 573    TextCell *cells, *c, *c1;
 574    int w1, x, y, last_width;
 575
 576    assert(s->scanout.kind == SCANOUT_SURFACE);
 577
 578    last_width = s->width;
 579    s->width = surface_width(s->surface) / FONT_WIDTH;
 580    s->height = surface_height(s->surface) / FONT_HEIGHT;
 581
 582    w1 = last_width;
 583    if (s->width < w1)
 584        w1 = s->width;
 585
 586    cells = g_new(TextCell, s->width * s->total_height + 1);
 587    for(y = 0; y < s->total_height; y++) {
 588        c = &cells[y * s->width];
 589        if (w1 > 0) {
 590            c1 = &s->cells[y * last_width];
 591            for(x = 0; x < w1; x++) {
 592                *c++ = *c1++;
 593            }
 594        }
 595        for(x = w1; x < s->width; x++) {
 596            c->ch = ' ';
 597            c->t_attrib = s->t_attrib_default;
 598            c++;
 599        }
 600    }
 601    g_free(s->cells);
 602    s->cells = cells;
 603}
 604
 605static inline void text_update_xy(QemuConsole *s, int x, int y)
 606{
 607    s->text_x[0] = MIN(s->text_x[0], x);
 608    s->text_x[1] = MAX(s->text_x[1], x);
 609    s->text_y[0] = MIN(s->text_y[0], y);
 610    s->text_y[1] = MAX(s->text_y[1], y);
 611}
 612
 613static void invalidate_xy(QemuConsole *s, int x, int y)
 614{
 615    if (!qemu_console_is_visible(s)) {
 616        return;
 617    }
 618    if (s->update_x0 > x * FONT_WIDTH)
 619        s->update_x0 = x * FONT_WIDTH;
 620    if (s->update_y0 > y * FONT_HEIGHT)
 621        s->update_y0 = y * FONT_HEIGHT;
 622    if (s->update_x1 < (x + 1) * FONT_WIDTH)
 623        s->update_x1 = (x + 1) * FONT_WIDTH;
 624    if (s->update_y1 < (y + 1) * FONT_HEIGHT)
 625        s->update_y1 = (y + 1) * FONT_HEIGHT;
 626}
 627
 628static void update_xy(QemuConsole *s, int x, int y)
 629{
 630    TextCell *c;
 631    int y1, y2;
 632
 633    if (s->ds->have_text) {
 634        text_update_xy(s, x, y);
 635    }
 636
 637    y1 = (s->y_base + y) % s->total_height;
 638    y2 = y1 - s->y_displayed;
 639    if (y2 < 0) {
 640        y2 += s->total_height;
 641    }
 642    if (y2 < s->height) {
 643        if (x >= s->width) {
 644            x = s->width - 1;
 645        }
 646        c = &s->cells[y1 * s->width + x];
 647        vga_putcharxy(s, x, y2, c->ch,
 648                      &(c->t_attrib));
 649        invalidate_xy(s, x, y2);
 650    }
 651}
 652
 653static void console_show_cursor(QemuConsole *s, int show)
 654{
 655    TextCell *c;
 656    int y, y1;
 657    int x = s->x;
 658
 659    if (s->ds->have_text) {
 660        s->cursor_invalidate = 1;
 661    }
 662
 663    if (x >= s->width) {
 664        x = s->width - 1;
 665    }
 666    y1 = (s->y_base + s->y) % s->total_height;
 667    y = y1 - s->y_displayed;
 668    if (y < 0) {
 669        y += s->total_height;
 670    }
 671    if (y < s->height) {
 672        c = &s->cells[y1 * s->width + x];
 673        if (show && cursor_visible_phase) {
 674            TextAttributes t_attrib = s->t_attrib_default;
 675            t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
 676            vga_putcharxy(s, x, y, c->ch, &t_attrib);
 677        } else {
 678            vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
 679        }
 680        invalidate_xy(s, x, y);
 681    }
 682}
 683
 684static void console_refresh(QemuConsole *s)
 685{
 686    DisplaySurface *surface = qemu_console_surface(s);
 687    TextCell *c;
 688    int x, y, y1;
 689
 690    if (s->ds->have_text) {
 691        s->text_x[0] = 0;
 692        s->text_y[0] = 0;
 693        s->text_x[1] = s->width - 1;
 694        s->text_y[1] = s->height - 1;
 695        s->cursor_invalidate = 1;
 696    }
 697
 698    vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
 699                  color_table_rgb[0][QEMU_COLOR_BLACK]);
 700    y1 = s->y_displayed;
 701    for (y = 0; y < s->height; y++) {
 702        c = s->cells + y1 * s->width;
 703        for (x = 0; x < s->width; x++) {
 704            vga_putcharxy(s, x, y, c->ch,
 705                          &(c->t_attrib));
 706            c++;
 707        }
 708        if (++y1 == s->total_height) {
 709            y1 = 0;
 710        }
 711    }
 712    console_show_cursor(s, 1);
 713    dpy_gfx_update(s, 0, 0,
 714                   surface_width(surface), surface_height(surface));
 715}
 716
 717static void console_scroll(QemuConsole *s, int ydelta)
 718{
 719    int i, y1;
 720
 721    if (ydelta > 0) {
 722        for(i = 0; i < ydelta; i++) {
 723            if (s->y_displayed == s->y_base)
 724                break;
 725            if (++s->y_displayed == s->total_height)
 726                s->y_displayed = 0;
 727        }
 728    } else {
 729        ydelta = -ydelta;
 730        i = s->backscroll_height;
 731        if (i > s->total_height - s->height)
 732            i = s->total_height - s->height;
 733        y1 = s->y_base - i;
 734        if (y1 < 0)
 735            y1 += s->total_height;
 736        for(i = 0; i < ydelta; i++) {
 737            if (s->y_displayed == y1)
 738                break;
 739            if (--s->y_displayed < 0)
 740                s->y_displayed = s->total_height - 1;
 741        }
 742    }
 743    console_refresh(s);
 744}
 745
 746static void console_put_lf(QemuConsole *s)
 747{
 748    TextCell *c;
 749    int x, y1;
 750
 751    s->y++;
 752    if (s->y >= s->height) {
 753        s->y = s->height - 1;
 754
 755        if (s->y_displayed == s->y_base) {
 756            if (++s->y_displayed == s->total_height)
 757                s->y_displayed = 0;
 758        }
 759        if (++s->y_base == s->total_height)
 760            s->y_base = 0;
 761        if (s->backscroll_height < s->total_height)
 762            s->backscroll_height++;
 763        y1 = (s->y_base + s->height - 1) % s->total_height;
 764        c = &s->cells[y1 * s->width];
 765        for(x = 0; x < s->width; x++) {
 766            c->ch = ' ';
 767            c->t_attrib = s->t_attrib_default;
 768            c++;
 769        }
 770        if (s->y_displayed == s->y_base) {
 771            if (s->ds->have_text) {
 772                s->text_x[0] = 0;
 773                s->text_y[0] = 0;
 774                s->text_x[1] = s->width - 1;
 775                s->text_y[1] = s->height - 1;
 776            }
 777
 778            vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
 779                       s->width * FONT_WIDTH,
 780                       (s->height - 1) * FONT_HEIGHT);
 781            vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
 782                          s->width * FONT_WIDTH, FONT_HEIGHT,
 783                          color_table_rgb[0][s->t_attrib_default.bgcol]);
 784            s->update_x0 = 0;
 785            s->update_y0 = 0;
 786            s->update_x1 = s->width * FONT_WIDTH;
 787            s->update_y1 = s->height * FONT_HEIGHT;
 788        }
 789    }
 790}
 791
 792/* Set console attributes depending on the current escape codes.
 793 * NOTE: I know this code is not very efficient (checking every color for it
 794 * self) but it is more readable and better maintainable.
 795 */
 796static void console_handle_escape(QemuConsole *s)
 797{
 798    int i;
 799
 800    for (i=0; i<s->nb_esc_params; i++) {
 801        switch (s->esc_params[i]) {
 802            case 0: /* reset all console attributes to default */
 803                s->t_attrib = s->t_attrib_default;
 804                break;
 805            case 1:
 806                s->t_attrib.bold = 1;
 807                break;
 808            case 4:
 809                s->t_attrib.uline = 1;
 810                break;
 811            case 5:
 812                s->t_attrib.blink = 1;
 813                break;
 814            case 7:
 815                s->t_attrib.invers = 1;
 816                break;
 817            case 8:
 818                s->t_attrib.unvisible = 1;
 819                break;
 820            case 22:
 821                s->t_attrib.bold = 0;
 822                break;
 823            case 24:
 824                s->t_attrib.uline = 0;
 825                break;
 826            case 25:
 827                s->t_attrib.blink = 0;
 828                break;
 829            case 27:
 830                s->t_attrib.invers = 0;
 831                break;
 832            case 28:
 833                s->t_attrib.unvisible = 0;
 834                break;
 835            /* set foreground color */
 836            case 30:
 837                s->t_attrib.fgcol = QEMU_COLOR_BLACK;
 838                break;
 839            case 31:
 840                s->t_attrib.fgcol = QEMU_COLOR_RED;
 841                break;
 842            case 32:
 843                s->t_attrib.fgcol = QEMU_COLOR_GREEN;
 844                break;
 845            case 33:
 846                s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
 847                break;
 848            case 34:
 849                s->t_attrib.fgcol = QEMU_COLOR_BLUE;
 850                break;
 851            case 35:
 852                s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
 853                break;
 854            case 36:
 855                s->t_attrib.fgcol = QEMU_COLOR_CYAN;
 856                break;
 857            case 37:
 858                s->t_attrib.fgcol = QEMU_COLOR_WHITE;
 859                break;
 860            /* set background color */
 861            case 40:
 862                s->t_attrib.bgcol = QEMU_COLOR_BLACK;
 863                break;
 864            case 41:
 865                s->t_attrib.bgcol = QEMU_COLOR_RED;
 866                break;
 867            case 42:
 868                s->t_attrib.bgcol = QEMU_COLOR_GREEN;
 869                break;
 870            case 43:
 871                s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
 872                break;
 873            case 44:
 874                s->t_attrib.bgcol = QEMU_COLOR_BLUE;
 875                break;
 876            case 45:
 877                s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
 878                break;
 879            case 46:
 880                s->t_attrib.bgcol = QEMU_COLOR_CYAN;
 881                break;
 882            case 47:
 883                s->t_attrib.bgcol = QEMU_COLOR_WHITE;
 884                break;
 885        }
 886    }
 887}
 888
 889static void console_clear_xy(QemuConsole *s, int x, int y)
 890{
 891    int y1 = (s->y_base + y) % s->total_height;
 892    if (x >= s->width) {
 893        x = s->width - 1;
 894    }
 895    TextCell *c = &s->cells[y1 * s->width + x];
 896    c->ch = ' ';
 897    c->t_attrib = s->t_attrib_default;
 898    update_xy(s, x, y);
 899}
 900
 901static void console_put_one(QemuConsole *s, int ch)
 902{
 903    TextCell *c;
 904    int y1;
 905    if (s->x >= s->width) {
 906        /* line wrap */
 907        s->x = 0;
 908        console_put_lf(s);
 909    }
 910    y1 = (s->y_base + s->y) % s->total_height;
 911    c = &s->cells[y1 * s->width + s->x];
 912    c->ch = ch;
 913    c->t_attrib = s->t_attrib;
 914    update_xy(s, s->x, s->y);
 915    s->x++;
 916}
 917
 918static void console_respond_str(QemuConsole *s, const char *buf)
 919{
 920    while (*buf) {
 921        console_put_one(s, *buf);
 922        buf++;
 923    }
 924}
 925
 926/* set cursor, checking bounds */
 927static void set_cursor(QemuConsole *s, int x, int y)
 928{
 929    if (x < 0) {
 930        x = 0;
 931    }
 932    if (y < 0) {
 933        y = 0;
 934    }
 935    if (y >= s->height) {
 936        y = s->height - 1;
 937    }
 938    if (x >= s->width) {
 939        x = s->width - 1;
 940    }
 941
 942    s->x = x;
 943    s->y = y;
 944}
 945
 946static void console_putchar(QemuConsole *s, int ch)
 947{
 948    int i;
 949    int x, y;
 950    char response[40];
 951
 952    switch(s->state) {
 953    case TTY_STATE_NORM:
 954        switch(ch) {
 955        case '\r':  /* carriage return */
 956            s->x = 0;
 957            break;
 958        case '\n':  /* newline */
 959            console_put_lf(s);
 960            break;
 961        case '\b':  /* backspace */
 962            if (s->x > 0)
 963                s->x--;
 964            break;
 965        case '\t':  /* tabspace */
 966            if (s->x + (8 - (s->x % 8)) > s->width) {
 967                s->x = 0;
 968                console_put_lf(s);
 969            } else {
 970                s->x = s->x + (8 - (s->x % 8));
 971            }
 972            break;
 973        case '\a':  /* alert aka. bell */
 974            /* TODO: has to be implemented */
 975            break;
 976        case 14:
 977            /* SI (shift in), character set 0 (ignored) */
 978            break;
 979        case 15:
 980            /* SO (shift out), character set 1 (ignored) */
 981            break;
 982        case 27:    /* esc (introducing an escape sequence) */
 983            s->state = TTY_STATE_ESC;
 984            break;
 985        default:
 986            console_put_one(s, ch);
 987            break;
 988        }
 989        break;
 990    case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
 991        if (ch == '[') {
 992            for(i=0;i<MAX_ESC_PARAMS;i++)
 993                s->esc_params[i] = 0;
 994            s->nb_esc_params = 0;
 995            s->state = TTY_STATE_CSI;
 996        } else {
 997            s->state = TTY_STATE_NORM;
 998        }
 999        break;
1000    case TTY_STATE_CSI: /* handle escape sequence parameters */
1001        if (ch >= '0' && ch <= '9') {
1002            if (s->nb_esc_params < MAX_ESC_PARAMS) {
1003                int *param = &s->esc_params[s->nb_esc_params];
1004                int digit = (ch - '0');
1005
1006                *param = (*param <= (INT_MAX - digit) / 10) ?
1007                         *param * 10 + digit : INT_MAX;
1008            }
1009        } else {
1010            if (s->nb_esc_params < MAX_ESC_PARAMS)
1011                s->nb_esc_params++;
1012            if (ch == ';' || ch == '?') {
1013                break;
1014            }
1015            trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
1016                                      ch, s->nb_esc_params);
1017            s->state = TTY_STATE_NORM;
1018            switch(ch) {
1019            case 'A':
1020                /* move cursor up */
1021                if (s->esc_params[0] == 0) {
1022                    s->esc_params[0] = 1;
1023                }
1024                set_cursor(s, s->x, s->y - s->esc_params[0]);
1025                break;
1026            case 'B':
1027                /* move cursor down */
1028                if (s->esc_params[0] == 0) {
1029                    s->esc_params[0] = 1;
1030                }
1031                set_cursor(s, s->x, s->y + s->esc_params[0]);
1032                break;
1033            case 'C':
1034                /* move cursor right */
1035                if (s->esc_params[0] == 0) {
1036                    s->esc_params[0] = 1;
1037                }
1038                set_cursor(s, s->x + s->esc_params[0], s->y);
1039                break;
1040            case 'D':
1041                /* move cursor left */
1042                if (s->esc_params[0] == 0) {
1043                    s->esc_params[0] = 1;
1044                }
1045                set_cursor(s, s->x - s->esc_params[0], s->y);
1046                break;
1047            case 'G':
1048                /* move cursor to column */
1049                set_cursor(s, s->esc_params[0] - 1, s->y);
1050                break;
1051            case 'f':
1052            case 'H':
1053                /* move cursor to row, column */
1054                set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
1055                break;
1056            case 'J':
1057                switch (s->esc_params[0]) {
1058                case 0:
1059                    /* clear to end of screen */
1060                    for (y = s->y; y < s->height; y++) {
1061                        for (x = 0; x < s->width; x++) {
1062                            if (y == s->y && x < s->x) {
1063                                continue;
1064                            }
1065                            console_clear_xy(s, x, y);
1066                        }
1067                    }
1068                    break;
1069                case 1:
1070                    /* clear from beginning of screen */
1071                    for (y = 0; y <= s->y; y++) {
1072                        for (x = 0; x < s->width; x++) {
1073                            if (y == s->y && x > s->x) {
1074                                break;
1075                            }
1076                            console_clear_xy(s, x, y);
1077                        }
1078                    }
1079                    break;
1080                case 2:
1081                    /* clear entire screen */
1082                    for (y = 0; y <= s->height; y++) {
1083                        for (x = 0; x < s->width; x++) {
1084                            console_clear_xy(s, x, y);
1085                        }
1086                    }
1087                    break;
1088                }
1089                break;
1090            case 'K':
1091                switch (s->esc_params[0]) {
1092                case 0:
1093                    /* clear to eol */
1094                    for(x = s->x; x < s->width; x++) {
1095                        console_clear_xy(s, x, s->y);
1096                    }
1097                    break;
1098                case 1:
1099                    /* clear from beginning of line */
1100                    for (x = 0; x <= s->x && x < s->width; x++) {
1101                        console_clear_xy(s, x, s->y);
1102                    }
1103                    break;
1104                case 2:
1105                    /* clear entire line */
1106                    for(x = 0; x < s->width; x++) {
1107                        console_clear_xy(s, x, s->y);
1108                    }
1109                    break;
1110                }
1111                break;
1112            case 'm':
1113                console_handle_escape(s);
1114                break;
1115            case 'n':
1116                switch (s->esc_params[0]) {
1117                case 5:
1118                    /* report console status (always succeed)*/
1119                    console_respond_str(s, "\033[0n");
1120                    break;
1121                case 6:
1122                    /* report cursor position */
1123                    sprintf(response, "\033[%d;%dR",
1124                           (s->y_base + s->y) % s->total_height + 1,
1125                            s->x + 1);
1126                    console_respond_str(s, response);
1127                    break;
1128                }
1129                break;
1130            case 's':
1131                /* save cursor position */
1132                s->x_saved = s->x;
1133                s->y_saved = s->y;
1134                break;
1135            case 'u':
1136                /* restore cursor position */
1137                s->x = s->x_saved;
1138                s->y = s->y_saved;
1139                break;
1140            default:
1141                trace_console_putchar_unhandled(ch);
1142                break;
1143            }
1144            break;
1145        }
1146    }
1147}
1148
1149static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
1150                                             struct DisplaySurface *new_surface,
1151                                             bool update)
1152{
1153    if (dcl->ops->dpy_gfx_switch) {
1154        dcl->ops->dpy_gfx_switch(dcl, new_surface);
1155    }
1156
1157    if (update && dcl->ops->dpy_gfx_update) {
1158        dcl->ops->dpy_gfx_update(dcl, 0, 0,
1159                                 surface_width(new_surface),
1160                                 surface_height(new_surface));
1161    }
1162}
1163
1164static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
1165{
1166    if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
1167        con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
1168    }
1169}
1170
1171static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
1172{
1173    if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
1174        con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
1175    }
1176}
1177
1178static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
1179                                   int x, int y, int w, int h)
1180{
1181    if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
1182        con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
1183    }
1184}
1185
1186static void displaychangelistener_display_console(DisplayChangeListener *dcl,
1187                                                  QemuConsole *con,
1188                                                  Error **errp)
1189{
1190    static const char nodev[] =
1191        "This VM has no graphic display device.";
1192    static DisplaySurface *dummy;
1193
1194    if (!con || !console_compatible_with(con, dcl, errp)) {
1195        if (!dummy) {
1196            dummy = qemu_create_placeholder_surface(640, 480, nodev);
1197        }
1198        if (con) {
1199            dpy_gfx_create_texture(con, dummy);
1200        }
1201        displaychangelistener_gfx_switch(dcl, dummy, TRUE);
1202        return;
1203    }
1204
1205    dpy_gfx_create_texture(con, con->surface);
1206    displaychangelistener_gfx_switch(dcl, con->surface,
1207                                     con->scanout.kind == SCANOUT_SURFACE);
1208
1209    if (con->scanout.kind == SCANOUT_DMABUF &&
1210        displaychangelistener_has_dmabuf(dcl)) {
1211        dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
1212    } else if (con->scanout.kind == SCANOUT_TEXTURE &&
1213               dcl->ops->dpy_gl_scanout_texture) {
1214        dcl->ops->dpy_gl_scanout_texture(dcl,
1215                                         con->scanout.texture.backing_id,
1216                                         con->scanout.texture.backing_y_0_top,
1217                                         con->scanout.texture.backing_width,
1218                                         con->scanout.texture.backing_height,
1219                                         con->scanout.texture.x,
1220                                         con->scanout.texture.y,
1221                                         con->scanout.texture.width,
1222                                         con->scanout.texture.height);
1223    }
1224}
1225
1226void console_select(unsigned int index)
1227{
1228    DisplayChangeListener *dcl;
1229    QemuConsole *s;
1230
1231    trace_console_select(index);
1232    s = qemu_console_lookup_by_index(index);
1233    if (s) {
1234        DisplayState *ds = s->ds;
1235
1236        active_console = s;
1237        if (ds->have_gfx) {
1238            QLIST_FOREACH(dcl, &ds->listeners, next) {
1239                if (dcl->con != NULL) {
1240                    continue;
1241                }
1242                displaychangelistener_display_console(dcl, s, NULL);
1243            }
1244        }
1245        if (ds->have_text) {
1246            dpy_text_resize(s, s->width, s->height);
1247        }
1248        text_console_update_cursor(NULL);
1249    }
1250}
1251
1252struct VCChardev {
1253    Chardev parent;
1254    QemuConsole *console;
1255};
1256typedef struct VCChardev VCChardev;
1257
1258#define TYPE_CHARDEV_VC "chardev-vc"
1259DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
1260                         TYPE_CHARDEV_VC)
1261
1262static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1263{
1264    VCChardev *drv = VC_CHARDEV(chr);
1265    QemuConsole *s = drv->console;
1266    int i;
1267
1268    if (!s->ds) {
1269        return 0;
1270    }
1271
1272    s->update_x0 = s->width * FONT_WIDTH;
1273    s->update_y0 = s->height * FONT_HEIGHT;
1274    s->update_x1 = 0;
1275    s->update_y1 = 0;
1276    console_show_cursor(s, 0);
1277    for(i = 0; i < len; i++) {
1278        console_putchar(s, buf[i]);
1279    }
1280    console_show_cursor(s, 1);
1281    if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1282        dpy_gfx_update(s, s->update_x0, s->update_y0,
1283                       s->update_x1 - s->update_x0,
1284                       s->update_y1 - s->update_y0);
1285    }
1286    return len;
1287}
1288
1289static void kbd_send_chars(QemuConsole *s)
1290{
1291    uint32_t len, avail;
1292
1293    len = qemu_chr_be_can_write(s->chr);
1294    avail = fifo8_num_used(&s->out_fifo);
1295    while (len > 0 && avail > 0) {
1296        const uint8_t *buf;
1297        uint32_t size;
1298
1299        buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
1300        qemu_chr_be_write(s->chr, buf, size);
1301        len = qemu_chr_be_can_write(s->chr);
1302        avail -= size;
1303    }
1304}
1305
1306/* called when an ascii key is pressed */
1307void kbd_put_keysym_console(QemuConsole *s, int keysym)
1308{
1309    uint8_t buf[16], *q;
1310    int c;
1311    uint32_t num_free;
1312
1313    if (!s || (s->console_type == GRAPHIC_CONSOLE))
1314        return;
1315
1316    switch(keysym) {
1317    case QEMU_KEY_CTRL_UP:
1318        console_scroll(s, -1);
1319        break;
1320    case QEMU_KEY_CTRL_DOWN:
1321        console_scroll(s, 1);
1322        break;
1323    case QEMU_KEY_CTRL_PAGEUP:
1324        console_scroll(s, -10);
1325        break;
1326    case QEMU_KEY_CTRL_PAGEDOWN:
1327        console_scroll(s, 10);
1328        break;
1329    default:
1330        /* convert the QEMU keysym to VT100 key string */
1331        q = buf;
1332        if (keysym >= 0xe100 && keysym <= 0xe11f) {
1333            *q++ = '\033';
1334            *q++ = '[';
1335            c = keysym - 0xe100;
1336            if (c >= 10)
1337                *q++ = '0' + (c / 10);
1338            *q++ = '0' + (c % 10);
1339            *q++ = '~';
1340        } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1341            *q++ = '\033';
1342            *q++ = '[';
1343            *q++ = keysym & 0xff;
1344        } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1345            vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
1346            *q++ = '\n';
1347        } else {
1348            *q++ = keysym;
1349        }
1350        if (s->echo) {
1351            vc_chr_write(s->chr, buf, q - buf);
1352        }
1353        num_free = fifo8_num_free(&s->out_fifo);
1354        fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
1355        kbd_send_chars(s);
1356        break;
1357    }
1358}
1359
1360static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1361    [Q_KEY_CODE_UP]     = QEMU_KEY_UP,
1362    [Q_KEY_CODE_DOWN]   = QEMU_KEY_DOWN,
1363    [Q_KEY_CODE_RIGHT]  = QEMU_KEY_RIGHT,
1364    [Q_KEY_CODE_LEFT]   = QEMU_KEY_LEFT,
1365    [Q_KEY_CODE_HOME]   = QEMU_KEY_HOME,
1366    [Q_KEY_CODE_END]    = QEMU_KEY_END,
1367    [Q_KEY_CODE_PGUP]   = QEMU_KEY_PAGEUP,
1368    [Q_KEY_CODE_PGDN]   = QEMU_KEY_PAGEDOWN,
1369    [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1370    [Q_KEY_CODE_TAB]    = QEMU_KEY_TAB,
1371    [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1372};
1373
1374static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
1375    [Q_KEY_CODE_UP]     = QEMU_KEY_CTRL_UP,
1376    [Q_KEY_CODE_DOWN]   = QEMU_KEY_CTRL_DOWN,
1377    [Q_KEY_CODE_RIGHT]  = QEMU_KEY_CTRL_RIGHT,
1378    [Q_KEY_CODE_LEFT]   = QEMU_KEY_CTRL_LEFT,
1379    [Q_KEY_CODE_HOME]   = QEMU_KEY_CTRL_HOME,
1380    [Q_KEY_CODE_END]    = QEMU_KEY_CTRL_END,
1381    [Q_KEY_CODE_PGUP]   = QEMU_KEY_CTRL_PAGEUP,
1382    [Q_KEY_CODE_PGDN]   = QEMU_KEY_CTRL_PAGEDOWN,
1383};
1384
1385bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
1386{
1387    int keysym;
1388
1389    keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
1390    if (keysym == 0) {
1391        return false;
1392    }
1393    kbd_put_keysym_console(s, keysym);
1394    return true;
1395}
1396
1397void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1398{
1399    int i;
1400
1401    for (i = 0; i < len && str[i]; i++) {
1402        kbd_put_keysym_console(s, str[i]);
1403    }
1404}
1405
1406void kbd_put_keysym(int keysym)
1407{
1408    kbd_put_keysym_console(active_console, keysym);
1409}
1410
1411static void text_console_invalidate(void *opaque)
1412{
1413    QemuConsole *s = (QemuConsole *) opaque;
1414
1415    if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1416        text_console_resize(s);
1417    }
1418    console_refresh(s);
1419}
1420
1421static void text_console_update(void *opaque, console_ch_t *chardata)
1422{
1423    QemuConsole *s = (QemuConsole *) opaque;
1424    int i, j, src;
1425
1426    if (s->text_x[0] <= s->text_x[1]) {
1427        src = (s->y_base + s->text_y[0]) * s->width;
1428        chardata += s->text_y[0] * s->width;
1429        for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1430            for (j = 0; j < s->width; j++, src++) {
1431                console_write_ch(chardata ++,
1432                                 ATTR2CHTYPE(s->cells[src].ch,
1433                                             s->cells[src].t_attrib.fgcol,
1434                                             s->cells[src].t_attrib.bgcol,
1435                                             s->cells[src].t_attrib.bold));
1436            }
1437        dpy_text_update(s, s->text_x[0], s->text_y[0],
1438                        s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1439        s->text_x[0] = s->width;
1440        s->text_y[0] = s->height;
1441        s->text_x[1] = 0;
1442        s->text_y[1] = 0;
1443    }
1444    if (s->cursor_invalidate) {
1445        dpy_text_cursor(s, s->x, s->y);
1446        s->cursor_invalidate = 0;
1447    }
1448}
1449
1450static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1451                                uint32_t head)
1452{
1453    Object *obj;
1454    QemuConsole *s;
1455    int i;
1456
1457    obj = object_new(TYPE_QEMU_CONSOLE);
1458    s = QEMU_CONSOLE(obj);
1459    qemu_co_queue_init(&s->dump_queue);
1460    s->head = head;
1461    object_property_add_link(obj, "device", TYPE_DEVICE,
1462                             (Object **)&s->device,
1463                             object_property_allow_set_link,
1464                             OBJ_PROP_LINK_STRONG);
1465    object_property_add_uint32_ptr(obj, "head", &s->head,
1466                                   OBJ_PROP_FLAG_READ);
1467
1468    if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1469        (console_type == GRAPHIC_CONSOLE))) {
1470        active_console = s;
1471    }
1472    s->ds = ds;
1473    s->console_type = console_type;
1474    s->window_id = -1;
1475
1476    if (QTAILQ_EMPTY(&consoles)) {
1477        s->index = 0;
1478        QTAILQ_INSERT_TAIL(&consoles, s, next);
1479    } else if (console_type != GRAPHIC_CONSOLE || phase_check(PHASE_MACHINE_READY)) {
1480        QemuConsole *last = QTAILQ_LAST(&consoles);
1481        s->index = last->index + 1;
1482        QTAILQ_INSERT_TAIL(&consoles, s, next);
1483    } else {
1484        /*
1485         * HACK: Put graphical consoles before text consoles.
1486         *
1487         * Only do that for coldplugged devices.  After initial device
1488         * initialization we will not renumber the consoles any more.
1489         */
1490        QemuConsole *c = QTAILQ_FIRST(&consoles);
1491
1492        while (QTAILQ_NEXT(c, next) != NULL &&
1493               c->console_type == GRAPHIC_CONSOLE) {
1494            c = QTAILQ_NEXT(c, next);
1495        }
1496        if (c->console_type == GRAPHIC_CONSOLE) {
1497            /* have no text consoles */
1498            s->index = c->index + 1;
1499            QTAILQ_INSERT_AFTER(&consoles, c, s, next);
1500        } else {
1501            s->index = c->index;
1502            QTAILQ_INSERT_BEFORE(c, s, next);
1503            /* renumber text consoles */
1504            for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
1505                c->index = i;
1506            }
1507        }
1508    }
1509    return s;
1510}
1511
1512DisplaySurface *qemu_create_displaysurface(int width, int height)
1513{
1514    DisplaySurface *surface = g_new0(DisplaySurface, 1);
1515
1516    trace_displaysurface_create(surface, width, height);
1517    surface->format = PIXMAN_x8r8g8b8;
1518    surface->image = pixman_image_create_bits(surface->format,
1519                                              width, height,
1520                                              NULL, width * 4);
1521    assert(surface->image != NULL);
1522    surface->flags = QEMU_ALLOCATED_FLAG;
1523
1524    return surface;
1525}
1526
1527DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1528                                                pixman_format_code_t format,
1529                                                int linesize, uint8_t *data)
1530{
1531    DisplaySurface *surface = g_new0(DisplaySurface, 1);
1532
1533    trace_displaysurface_create_from(surface, width, height, format);
1534    surface->format = format;
1535    surface->image = pixman_image_create_bits(surface->format,
1536                                              width, height,
1537                                              (void *)data, linesize);
1538    assert(surface->image != NULL);
1539
1540    return surface;
1541}
1542
1543DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1544{
1545    DisplaySurface *surface = g_new0(DisplaySurface, 1);
1546
1547    trace_displaysurface_create_pixman(surface);
1548    surface->format = pixman_image_get_format(image);
1549    surface->image = pixman_image_ref(image);
1550
1551    return surface;
1552}
1553
1554DisplaySurface *qemu_create_placeholder_surface(int w, int h,
1555                                                const char *msg)
1556{
1557    DisplaySurface *surface = qemu_create_displaysurface(w, h);
1558    pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1559    pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1560    pixman_image_t *glyph;
1561    int len, x, y, i;
1562
1563    len = strlen(msg);
1564    x = (w / FONT_WIDTH  - len) / 2;
1565    y = (h / FONT_HEIGHT - 1)   / 2;
1566    for (i = 0; i < len; i++) {
1567        glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1568        qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1569                                 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1570        qemu_pixman_image_unref(glyph);
1571    }
1572    surface->flags |= QEMU_PLACEHOLDER_FLAG;
1573    return surface;
1574}
1575
1576void qemu_free_displaysurface(DisplaySurface *surface)
1577{
1578    if (surface == NULL) {
1579        return;
1580    }
1581    trace_displaysurface_free(surface);
1582    qemu_pixman_image_unref(surface->image);
1583    g_free(surface);
1584}
1585
1586bool console_has_gl(QemuConsole *con)
1587{
1588    return con->gl != NULL;
1589}
1590
1591static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
1592{
1593    if (dcl->ops->dpy_has_dmabuf) {
1594        return dcl->ops->dpy_has_dmabuf(dcl);
1595    }
1596
1597    if (dcl->ops->dpy_gl_scanout_dmabuf) {
1598        return true;
1599    }
1600
1601    return false;
1602}
1603
1604static bool console_compatible_with(QemuConsole *con,
1605                                    DisplayChangeListener *dcl, Error **errp)
1606{
1607    int flags;
1608
1609    flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
1610
1611    if (console_has_gl(con) &&
1612        !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
1613        error_setg(errp, "Display %s is incompatible with the GL context",
1614                   dcl->ops->dpy_name);
1615        return false;
1616    }
1617
1618    if (flags & GRAPHIC_FLAGS_GL &&
1619        !console_has_gl(con)) {
1620        error_setg(errp, "The console requires a GL context.");
1621        return false;
1622
1623    }
1624
1625    if (flags & GRAPHIC_FLAGS_DMABUF &&
1626        !displaychangelistener_has_dmabuf(dcl)) {
1627        error_setg(errp, "The console requires display DMABUF support.");
1628        return false;
1629    }
1630
1631    return true;
1632}
1633
1634void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
1635{
1636    /* display has opengl support */
1637    assert(con);
1638    if (con->gl) {
1639        error_report("The console already has an OpenGL context.");
1640        exit(1);
1641    }
1642    con->gl = gl;
1643}
1644
1645void register_displaychangelistener(DisplayChangeListener *dcl)
1646{
1647    QemuConsole *con;
1648
1649    assert(!dcl->ds);
1650
1651    trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1652    dcl->ds = get_alloc_displaystate();
1653    QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1654    gui_setup_refresh(dcl->ds);
1655    if (dcl->con) {
1656        dcl->con->dcls++;
1657        con = dcl->con;
1658    } else {
1659        con = active_console;
1660    }
1661    displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
1662    text_console_update_cursor(NULL);
1663}
1664
1665void update_displaychangelistener(DisplayChangeListener *dcl,
1666                                  uint64_t interval)
1667{
1668    DisplayState *ds = dcl->ds;
1669
1670    dcl->update_interval = interval;
1671    if (!ds->refreshing && ds->update_interval > interval) {
1672        timer_mod(ds->gui_timer, ds->last_update + interval);
1673    }
1674}
1675
1676void unregister_displaychangelistener(DisplayChangeListener *dcl)
1677{
1678    DisplayState *ds = dcl->ds;
1679    trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1680    if (dcl->con) {
1681        dcl->con->dcls--;
1682    }
1683    QLIST_REMOVE(dcl, next);
1684    dcl->ds = NULL;
1685    gui_setup_refresh(ds);
1686}
1687
1688static void dpy_set_ui_info_timer(void *opaque)
1689{
1690    QemuConsole *con = opaque;
1691
1692    con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1693}
1694
1695bool dpy_ui_info_supported(QemuConsole *con)
1696{
1697    if (con == NULL) {
1698        con = active_console;
1699    }
1700    if (con == NULL) {
1701        return false;
1702    }
1703
1704    return con->hw_ops->ui_info != NULL;
1705}
1706
1707const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
1708{
1709    if (con == NULL) {
1710        con = active_console;
1711    }
1712
1713    return &con->ui_info;
1714}
1715
1716int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
1717{
1718    if (con == NULL) {
1719        con = active_console;
1720    }
1721
1722    if (!dpy_ui_info_supported(con)) {
1723        return -1;
1724    }
1725    if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1726        /* nothing changed -- ignore */
1727        return 0;
1728    }
1729
1730    /*
1731     * Typically we get a flood of these as the user resizes the window.
1732     * Wait until the dust has settled (one second without updates), then
1733     * go notify the guest.
1734     */
1735    con->ui_info = *info;
1736    timer_mod(con->ui_timer,
1737              qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
1738    return 0;
1739}
1740
1741void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1742{
1743    DisplayState *s = con->ds;
1744    DisplayChangeListener *dcl;
1745    int width = qemu_console_get_width(con, x + w);
1746    int height = qemu_console_get_height(con, y + h);
1747
1748    x = MAX(x, 0);
1749    y = MAX(y, 0);
1750    x = MIN(x, width);
1751    y = MIN(y, height);
1752    w = MIN(w, width - x);
1753    h = MIN(h, height - y);
1754
1755    if (!qemu_console_is_visible(con)) {
1756        return;
1757    }
1758    dpy_gfx_update_texture(con, con->surface, x, y, w, h);
1759    QLIST_FOREACH(dcl, &s->listeners, next) {
1760        if (con != (dcl->con ? dcl->con : active_console)) {
1761            continue;
1762        }
1763        if (dcl->ops->dpy_gfx_update) {
1764            dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1765        }
1766    }
1767}
1768
1769void dpy_gfx_update_full(QemuConsole *con)
1770{
1771    int w = qemu_console_get_width(con, 0);
1772    int h = qemu_console_get_height(con, 0);
1773
1774    dpy_gfx_update(con, 0, 0, w, h);
1775}
1776
1777void dpy_gfx_replace_surface(QemuConsole *con,
1778                             DisplaySurface *surface)
1779{
1780    static const char placeholder_msg[] = "Display output is not active.";
1781    DisplayState *s = con->ds;
1782    DisplaySurface *old_surface = con->surface;
1783    DisplayChangeListener *dcl;
1784    int width;
1785    int height;
1786
1787    if (!surface) {
1788        if (old_surface) {
1789            width = surface_width(old_surface);
1790            height = surface_height(old_surface);
1791        } else {
1792            width = 640;
1793            height = 480;
1794        }
1795
1796        surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
1797    }
1798
1799    assert(old_surface != surface);
1800
1801    con->scanout.kind = SCANOUT_SURFACE;
1802    con->surface = surface;
1803    dpy_gfx_create_texture(con, surface);
1804    QLIST_FOREACH(dcl, &s->listeners, next) {
1805        if (con != (dcl->con ? dcl->con : active_console)) {
1806            continue;
1807        }
1808        displaychangelistener_gfx_switch(dcl, surface, FALSE);
1809    }
1810    dpy_gfx_destroy_texture(con, old_surface);
1811    qemu_free_displaysurface(old_surface);
1812}
1813
1814bool dpy_gfx_check_format(QemuConsole *con,
1815                          pixman_format_code_t format)
1816{
1817    DisplayChangeListener *dcl;
1818    DisplayState *s = con->ds;
1819
1820    QLIST_FOREACH(dcl, &s->listeners, next) {
1821        if (dcl->con && dcl->con != con) {
1822            /* dcl bound to another console -> skip */
1823            continue;
1824        }
1825        if (dcl->ops->dpy_gfx_check_format) {
1826            if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1827                return false;
1828            }
1829        } else {
1830            /* default is to allow native 32 bpp only */
1831            if (format != qemu_default_pixman_format(32, true)) {
1832                return false;
1833            }
1834        }
1835    }
1836    return true;
1837}
1838
1839static void dpy_refresh(DisplayState *s)
1840{
1841    DisplayChangeListener *dcl;
1842
1843    QLIST_FOREACH(dcl, &s->listeners, next) {
1844        if (dcl->ops->dpy_refresh) {
1845            dcl->ops->dpy_refresh(dcl);
1846        }
1847    }
1848}
1849
1850void dpy_text_cursor(QemuConsole *con, int x, int y)
1851{
1852    DisplayState *s = con->ds;
1853    DisplayChangeListener *dcl;
1854
1855    if (!qemu_console_is_visible(con)) {
1856        return;
1857    }
1858    QLIST_FOREACH(dcl, &s->listeners, next) {
1859        if (con != (dcl->con ? dcl->con : active_console)) {
1860            continue;
1861        }
1862        if (dcl->ops->dpy_text_cursor) {
1863            dcl->ops->dpy_text_cursor(dcl, x, y);
1864        }
1865    }
1866}
1867
1868void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1869{
1870    DisplayState *s = con->ds;
1871    DisplayChangeListener *dcl;
1872
1873    if (!qemu_console_is_visible(con)) {
1874        return;
1875    }
1876    QLIST_FOREACH(dcl, &s->listeners, next) {
1877        if (con != (dcl->con ? dcl->con : active_console)) {
1878            continue;
1879        }
1880        if (dcl->ops->dpy_text_update) {
1881            dcl->ops->dpy_text_update(dcl, x, y, w, h);
1882        }
1883    }
1884}
1885
1886void dpy_text_resize(QemuConsole *con, int w, int h)
1887{
1888    DisplayState *s = con->ds;
1889    DisplayChangeListener *dcl;
1890
1891    if (!qemu_console_is_visible(con)) {
1892        return;
1893    }
1894    QLIST_FOREACH(dcl, &s->listeners, next) {
1895        if (con != (dcl->con ? dcl->con : active_console)) {
1896            continue;
1897        }
1898        if (dcl->ops->dpy_text_resize) {
1899            dcl->ops->dpy_text_resize(dcl, w, h);
1900        }
1901    }
1902}
1903
1904void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1905{
1906    DisplayState *s = con->ds;
1907    DisplayChangeListener *dcl;
1908
1909    if (!qemu_console_is_visible(con)) {
1910        return;
1911    }
1912    QLIST_FOREACH(dcl, &s->listeners, next) {
1913        if (con != (dcl->con ? dcl->con : active_console)) {
1914            continue;
1915        }
1916        if (dcl->ops->dpy_mouse_set) {
1917            dcl->ops->dpy_mouse_set(dcl, x, y, on);
1918        }
1919    }
1920}
1921
1922void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1923{
1924    DisplayState *s = con->ds;
1925    DisplayChangeListener *dcl;
1926
1927    if (!qemu_console_is_visible(con)) {
1928        return;
1929    }
1930    QLIST_FOREACH(dcl, &s->listeners, next) {
1931        if (con != (dcl->con ? dcl->con : active_console)) {
1932            continue;
1933        }
1934        if (dcl->ops->dpy_cursor_define) {
1935            dcl->ops->dpy_cursor_define(dcl, cursor);
1936        }
1937    }
1938}
1939
1940bool dpy_cursor_define_supported(QemuConsole *con)
1941{
1942    DisplayState *s = con->ds;
1943    DisplayChangeListener *dcl;
1944
1945    QLIST_FOREACH(dcl, &s->listeners, next) {
1946        if (dcl->ops->dpy_cursor_define) {
1947            return true;
1948        }
1949    }
1950    return false;
1951}
1952
1953QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1954                                struct QEMUGLParams *qparams)
1955{
1956    assert(con->gl);
1957    return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1958}
1959
1960void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1961{
1962    assert(con->gl);
1963    con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1964}
1965
1966int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1967{
1968    assert(con->gl);
1969    return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1970}
1971
1972void dpy_gl_scanout_disable(QemuConsole *con)
1973{
1974    DisplayState *s = con->ds;
1975    DisplayChangeListener *dcl;
1976
1977    if (con->scanout.kind != SCANOUT_SURFACE) {
1978        con->scanout.kind = SCANOUT_NONE;
1979    }
1980    QLIST_FOREACH(dcl, &s->listeners, next) {
1981        if (con != (dcl->con ? dcl->con : active_console)) {
1982            continue;
1983        }
1984        if (dcl->ops->dpy_gl_scanout_disable) {
1985            dcl->ops->dpy_gl_scanout_disable(dcl);
1986        }
1987    }
1988}
1989
1990void dpy_gl_scanout_texture(QemuConsole *con,
1991                            uint32_t backing_id,
1992                            bool backing_y_0_top,
1993                            uint32_t backing_width,
1994                            uint32_t backing_height,
1995                            uint32_t x, uint32_t y,
1996                            uint32_t width, uint32_t height)
1997{
1998    DisplayState *s = con->ds;
1999    DisplayChangeListener *dcl;
2000
2001    con->scanout.kind = SCANOUT_TEXTURE;
2002    con->scanout.texture = (ScanoutTexture) {
2003        backing_id, backing_y_0_top, backing_width, backing_height,
2004        x, y, width, height
2005    };
2006    QLIST_FOREACH(dcl, &s->listeners, next) {
2007        if (con != (dcl->con ? dcl->con : active_console)) {
2008            continue;
2009        }
2010        if (dcl->ops->dpy_gl_scanout_texture) {
2011            dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
2012                                             backing_y_0_top,
2013                                             backing_width, backing_height,
2014                                             x, y, width, height);
2015        }
2016    }
2017}
2018
2019void dpy_gl_scanout_dmabuf(QemuConsole *con,
2020                           QemuDmaBuf *dmabuf)
2021{
2022    DisplayState *s = con->ds;
2023    DisplayChangeListener *dcl;
2024
2025    con->scanout.kind = SCANOUT_DMABUF;
2026    con->scanout.dmabuf = dmabuf;
2027    QLIST_FOREACH(dcl, &s->listeners, next) {
2028        if (con != (dcl->con ? dcl->con : active_console)) {
2029            continue;
2030        }
2031        if (dcl->ops->dpy_gl_scanout_dmabuf) {
2032            dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
2033        }
2034    }
2035}
2036
2037void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
2038                          bool have_hot, uint32_t hot_x, uint32_t hot_y)
2039{
2040    DisplayState *s = con->ds;
2041    DisplayChangeListener *dcl;
2042
2043    QLIST_FOREACH(dcl, &s->listeners, next) {
2044        if (con != (dcl->con ? dcl->con : active_console)) {
2045            continue;
2046        }
2047        if (dcl->ops->dpy_gl_cursor_dmabuf) {
2048            dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
2049                                           have_hot, hot_x, hot_y);
2050        }
2051    }
2052}
2053
2054void dpy_gl_cursor_position(QemuConsole *con,
2055                            uint32_t pos_x, uint32_t pos_y)
2056{
2057    DisplayState *s = con->ds;
2058    DisplayChangeListener *dcl;
2059
2060    QLIST_FOREACH(dcl, &s->listeners, next) {
2061        if (con != (dcl->con ? dcl->con : active_console)) {
2062            continue;
2063        }
2064        if (dcl->ops->dpy_gl_cursor_position) {
2065            dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
2066        }
2067    }
2068}
2069
2070void dpy_gl_release_dmabuf(QemuConsole *con,
2071                          QemuDmaBuf *dmabuf)
2072{
2073    DisplayState *s = con->ds;
2074    DisplayChangeListener *dcl;
2075
2076    QLIST_FOREACH(dcl, &s->listeners, next) {
2077        if (con != (dcl->con ? dcl->con : active_console)) {
2078            continue;
2079        }
2080        if (dcl->ops->dpy_gl_release_dmabuf) {
2081            dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
2082        }
2083    }
2084}
2085
2086void dpy_gl_update(QemuConsole *con,
2087                   uint32_t x, uint32_t y, uint32_t w, uint32_t h)
2088{
2089    DisplayState *s = con->ds;
2090    DisplayChangeListener *dcl;
2091
2092    assert(con->gl);
2093
2094    graphic_hw_gl_block(con, true);
2095    QLIST_FOREACH(dcl, &s->listeners, next) {
2096        if (con != (dcl->con ? dcl->con : active_console)) {
2097            continue;
2098        }
2099        if (dcl->ops->dpy_gl_update) {
2100            dcl->ops->dpy_gl_update(dcl, x, y, w, h);
2101        }
2102    }
2103    graphic_hw_gl_block(con, false);
2104}
2105
2106/***********************************************************/
2107/* register display */
2108
2109/* console.c internal use only */
2110static DisplayState *get_alloc_displaystate(void)
2111{
2112    if (!display_state) {
2113        display_state = g_new0(DisplayState, 1);
2114        cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2115                                    text_console_update_cursor, NULL);
2116    }
2117    return display_state;
2118}
2119
2120/*
2121 * Called by main(), after creating QemuConsoles
2122 * and before initializing ui (sdl/vnc/...).
2123 */
2124DisplayState *init_displaystate(void)
2125{
2126    gchar *name;
2127    QemuConsole *con;
2128
2129    get_alloc_displaystate();
2130    QTAILQ_FOREACH(con, &consoles, next) {
2131        if (con->console_type != GRAPHIC_CONSOLE &&
2132            con->ds == NULL) {
2133            text_console_do_init(con->chr, display_state);
2134        }
2135
2136        /* Hook up into the qom tree here (not in new_console()), once
2137         * all QemuConsoles are created and the order / numbering
2138         * doesn't change any more */
2139        name = g_strdup_printf("console[%d]", con->index);
2140        object_property_add_child(container_get(object_get_root(), "/backend"),
2141                                  name, OBJECT(con));
2142        g_free(name);
2143    }
2144
2145    return display_state;
2146}
2147
2148void graphic_console_set_hwops(QemuConsole *con,
2149                               const GraphicHwOps *hw_ops,
2150                               void *opaque)
2151{
2152    con->hw_ops = hw_ops;
2153    con->hw = opaque;
2154}
2155
2156QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
2157                                  const GraphicHwOps *hw_ops,
2158                                  void *opaque)
2159{
2160    static const char noinit[] =
2161        "Guest has not initialized the display (yet).";
2162    int width = 640;
2163    int height = 480;
2164    QemuConsole *s;
2165    DisplayState *ds;
2166    DisplaySurface *surface;
2167
2168    ds = get_alloc_displaystate();
2169    s = qemu_console_lookup_unused();
2170    if (s) {
2171        trace_console_gfx_reuse(s->index);
2172        width = qemu_console_get_width(s, 0);
2173        height = qemu_console_get_height(s, 0);
2174    } else {
2175        trace_console_gfx_new();
2176        s = new_console(ds, GRAPHIC_CONSOLE, head);
2177        s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2178                                   dpy_set_ui_info_timer, s);
2179    }
2180    graphic_console_set_hwops(s, hw_ops, opaque);
2181    if (dev) {
2182        object_property_set_link(OBJECT(s), "device", OBJECT(dev),
2183                                 &error_abort);
2184    }
2185
2186    surface = qemu_create_placeholder_surface(width, height, noinit);
2187    dpy_gfx_replace_surface(s, surface);
2188    s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2189                                       graphic_hw_gl_unblock_timer, s);
2190    return s;
2191}
2192
2193static const GraphicHwOps unused_ops = {
2194    /* no callbacks */
2195};
2196
2197void graphic_console_close(QemuConsole *con)
2198{
2199    static const char unplugged[] =
2200        "Guest display has been unplugged";
2201    DisplaySurface *surface;
2202    int width = qemu_console_get_width(con, 640);
2203    int height = qemu_console_get_height(con, 480);
2204
2205    trace_console_gfx_close(con->index);
2206    object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
2207    graphic_console_set_hwops(con, &unused_ops, NULL);
2208
2209    if (con->gl) {
2210        dpy_gl_scanout_disable(con);
2211    }
2212    surface = qemu_create_placeholder_surface(width, height, unplugged);
2213    dpy_gfx_replace_surface(con, surface);
2214}
2215
2216QemuConsole *qemu_console_lookup_by_index(unsigned int index)
2217{
2218    QemuConsole *con;
2219
2220    QTAILQ_FOREACH(con, &consoles, next) {
2221        if (con->index == index) {
2222            return con;
2223        }
2224    }
2225    return NULL;
2226}
2227
2228QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
2229{
2230    QemuConsole *con;
2231    Object *obj;
2232    uint32_t h;
2233
2234    QTAILQ_FOREACH(con, &consoles, next) {
2235        obj = object_property_get_link(OBJECT(con),
2236                                       "device", &error_abort);
2237        if (DEVICE(obj) != dev) {
2238            continue;
2239        }
2240        h = object_property_get_uint(OBJECT(con),
2241                                     "head", &error_abort);
2242        if (h != head) {
2243            continue;
2244        }
2245        return con;
2246    }
2247    return NULL;
2248}
2249
2250QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
2251                                                uint32_t head, Error **errp)
2252{
2253    DeviceState *dev;
2254    QemuConsole *con;
2255
2256    dev = qdev_find_recursive(sysbus_get_default(), device_id);
2257    if (dev == NULL) {
2258        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2259                  "Device '%s' not found", device_id);
2260        return NULL;
2261    }
2262
2263    con = qemu_console_lookup_by_device(dev, head);
2264    if (con == NULL) {
2265        error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
2266                   device_id, head);
2267        return NULL;
2268    }
2269
2270    return con;
2271}
2272
2273QemuConsole *qemu_console_lookup_unused(void)
2274{
2275    QemuConsole *con;
2276    Object *obj;
2277
2278    QTAILQ_FOREACH(con, &consoles, next) {
2279        if (con->hw_ops != &unused_ops) {
2280            continue;
2281        }
2282        obj = object_property_get_link(OBJECT(con),
2283                                       "device", &error_abort);
2284        if (obj != NULL) {
2285            continue;
2286        }
2287        return con;
2288    }
2289    return NULL;
2290}
2291
2292bool qemu_console_is_visible(QemuConsole *con)
2293{
2294    return (con == active_console) || (con->dcls > 0);
2295}
2296
2297bool qemu_console_is_graphic(QemuConsole *con)
2298{
2299    if (con == NULL) {
2300        con = active_console;
2301    }
2302    return con && (con->console_type == GRAPHIC_CONSOLE);
2303}
2304
2305bool qemu_console_is_fixedsize(QemuConsole *con)
2306{
2307    if (con == NULL) {
2308        con = active_console;
2309    }
2310    return con && (con->console_type != TEXT_CONSOLE);
2311}
2312
2313bool qemu_console_is_gl_blocked(QemuConsole *con)
2314{
2315    assert(con != NULL);
2316    return con->gl_block;
2317}
2318
2319bool qemu_console_is_multihead(DeviceState *dev)
2320{
2321    QemuConsole *con;
2322    Object *obj;
2323    uint32_t f = 0xffffffff;
2324    uint32_t h;
2325
2326    QTAILQ_FOREACH(con, &consoles, next) {
2327        obj = object_property_get_link(OBJECT(con),
2328                                       "device", &error_abort);
2329        if (DEVICE(obj) != dev) {
2330            continue;
2331        }
2332
2333        h = object_property_get_uint(OBJECT(con),
2334                                     "head", &error_abort);
2335        if (f == 0xffffffff) {
2336            f = h;
2337        } else if (h != f) {
2338            return true;
2339        }
2340    }
2341    return false;
2342}
2343
2344char *qemu_console_get_label(QemuConsole *con)
2345{
2346    if (con->console_type == GRAPHIC_CONSOLE) {
2347        if (con->device) {
2348            DeviceState *dev;
2349            bool multihead;
2350
2351            dev = DEVICE(con->device);
2352            multihead = qemu_console_is_multihead(dev);
2353            if (multihead) {
2354                return g_strdup_printf("%s.%d", dev->id ?
2355                                       dev->id :
2356                                       object_get_typename(con->device),
2357                                       con->head);
2358            } else {
2359                return g_strdup_printf("%s", dev->id ?
2360                                       dev->id :
2361                                       object_get_typename(con->device));
2362            }
2363        }
2364        return g_strdup("VGA");
2365    } else {
2366        if (con->chr && con->chr->label) {
2367            return g_strdup(con->chr->label);
2368        }
2369        return g_strdup_printf("vc%d", con->index);
2370    }
2371}
2372
2373int qemu_console_get_index(QemuConsole *con)
2374{
2375    if (con == NULL) {
2376        con = active_console;
2377    }
2378    return con ? con->index : -1;
2379}
2380
2381uint32_t qemu_console_get_head(QemuConsole *con)
2382{
2383    if (con == NULL) {
2384        con = active_console;
2385    }
2386    return con ? con->head : -1;
2387}
2388
2389int qemu_console_get_width(QemuConsole *con, int fallback)
2390{
2391    if (con == NULL) {
2392        con = active_console;
2393    }
2394    if (con == NULL) {
2395        return fallback;
2396    }
2397    switch (con->scanout.kind) {
2398    case SCANOUT_DMABUF:
2399        return con->scanout.dmabuf->width;
2400    case SCANOUT_TEXTURE:
2401        return con->scanout.texture.width;
2402    case SCANOUT_SURFACE:
2403        return surface_width(con->surface);
2404    default:
2405        return fallback;
2406    }
2407}
2408
2409int qemu_console_get_height(QemuConsole *con, int fallback)
2410{
2411    if (con == NULL) {
2412        con = active_console;
2413    }
2414    if (con == NULL) {
2415        return fallback;
2416    }
2417    switch (con->scanout.kind) {
2418    case SCANOUT_DMABUF:
2419        return con->scanout.dmabuf->height;
2420    case SCANOUT_TEXTURE:
2421        return con->scanout.texture.height;
2422    case SCANOUT_SURFACE:
2423        return surface_height(con->surface);
2424    default:
2425        return fallback;
2426    }
2427}
2428
2429static void vc_chr_accept_input(Chardev *chr)
2430{
2431    VCChardev *drv = VC_CHARDEV(chr);
2432    QemuConsole *s = drv->console;
2433
2434    kbd_send_chars(s);
2435}
2436
2437static void vc_chr_set_echo(Chardev *chr, bool echo)
2438{
2439    VCChardev *drv = VC_CHARDEV(chr);
2440    QemuConsole *s = drv->console;
2441
2442    s->echo = echo;
2443}
2444
2445static void text_console_update_cursor_timer(void)
2446{
2447    timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2448              + CONSOLE_CURSOR_PERIOD / 2);
2449}
2450
2451static void text_console_update_cursor(void *opaque)
2452{
2453    QemuConsole *s;
2454    int count = 0;
2455
2456    cursor_visible_phase = !cursor_visible_phase;
2457
2458    QTAILQ_FOREACH(s, &consoles, next) {
2459        if (qemu_console_is_graphic(s) ||
2460            !qemu_console_is_visible(s)) {
2461            continue;
2462        }
2463        count++;
2464        graphic_hw_invalidate(s);
2465    }
2466
2467    if (count) {
2468        text_console_update_cursor_timer();
2469    }
2470}
2471
2472static const GraphicHwOps text_console_ops = {
2473    .invalidate  = text_console_invalidate,
2474    .text_update = text_console_update,
2475};
2476
2477static void text_console_do_init(Chardev *chr, DisplayState *ds)
2478{
2479    VCChardev *drv = VC_CHARDEV(chr);
2480    QemuConsole *s = drv->console;
2481    int g_width = 80 * FONT_WIDTH;
2482    int g_height = 24 * FONT_HEIGHT;
2483
2484    fifo8_create(&s->out_fifo, 16);
2485    s->ds = ds;
2486
2487    s->y_displayed = 0;
2488    s->y_base = 0;
2489    s->total_height = DEFAULT_BACKSCROLL;
2490    s->x = 0;
2491    s->y = 0;
2492    if (s->scanout.kind != SCANOUT_SURFACE) {
2493        if (active_console && active_console->scanout.kind == SCANOUT_SURFACE) {
2494            g_width = qemu_console_get_width(active_console, g_width);
2495            g_height = qemu_console_get_height(active_console, g_height);
2496        }
2497        s->surface = qemu_create_displaysurface(g_width, g_height);
2498        s->scanout.kind = SCANOUT_SURFACE;
2499    }
2500
2501    s->hw_ops = &text_console_ops;
2502    s->hw = s;
2503
2504    /* Set text attribute defaults */
2505    s->t_attrib_default.bold = 0;
2506    s->t_attrib_default.uline = 0;
2507    s->t_attrib_default.blink = 0;
2508    s->t_attrib_default.invers = 0;
2509    s->t_attrib_default.unvisible = 0;
2510    s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2511    s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2512    /* set current text attributes to default */
2513    s->t_attrib = s->t_attrib_default;
2514    text_console_resize(s);
2515
2516    if (chr->label) {
2517        char *msg;
2518
2519        s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2520        msg = g_strdup_printf("%s console\r\n", chr->label);
2521        vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
2522        g_free(msg);
2523        s->t_attrib = s->t_attrib_default;
2524    }
2525
2526    qemu_chr_be_event(chr, CHR_EVENT_OPENED);
2527}
2528
2529static void vc_chr_open(Chardev *chr,
2530                        ChardevBackend *backend,
2531                        bool *be_opened,
2532                        Error **errp)
2533{
2534    ChardevVC *vc = backend->u.vc.data;
2535    VCChardev *drv = VC_CHARDEV(chr);
2536    QemuConsole *s;
2537    unsigned width = 0;
2538    unsigned height = 0;
2539
2540    if (vc->has_width) {
2541        width = vc->width;
2542    } else if (vc->has_cols) {
2543        width = vc->cols * FONT_WIDTH;
2544    }
2545
2546    if (vc->has_height) {
2547        height = vc->height;
2548    } else if (vc->has_rows) {
2549        height = vc->rows * FONT_HEIGHT;
2550    }
2551
2552    trace_console_txt_new(width, height);
2553    if (width == 0 || height == 0) {
2554        s = new_console(NULL, TEXT_CONSOLE, 0);
2555    } else {
2556        s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2557        s->scanout.kind = SCANOUT_SURFACE;
2558        s->surface = qemu_create_displaysurface(width, height);
2559    }
2560
2561    if (!s) {
2562        error_setg(errp, "cannot create text console");
2563        return;
2564    }
2565
2566    s->chr = chr;
2567    drv->console = s;
2568
2569    if (display_state) {
2570        text_console_do_init(chr, display_state);
2571    }
2572
2573    /* console/chardev init sometimes completes elsewhere in a 2nd
2574     * stage, so defer OPENED events until they are fully initialized
2575     */
2576    *be_opened = false;
2577}
2578
2579void qemu_console_resize(QemuConsole *s, int width, int height)
2580{
2581    DisplaySurface *surface = qemu_console_surface(s);
2582
2583    assert(s->console_type == GRAPHIC_CONSOLE);
2584
2585    if ((s->scanout.kind != SCANOUT_SURFACE ||
2586         (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
2587        qemu_console_get_width(s, -1) == width &&
2588        qemu_console_get_height(s, -1) == height) {
2589        return;
2590    }
2591
2592    surface = qemu_create_displaysurface(width, height);
2593    dpy_gfx_replace_surface(s, surface);
2594}
2595
2596DisplaySurface *qemu_console_surface(QemuConsole *console)
2597{
2598    switch (console->scanout.kind) {
2599    case SCANOUT_SURFACE:
2600        return console->surface;
2601    default:
2602        return NULL;
2603    }
2604}
2605
2606PixelFormat qemu_default_pixelformat(int bpp)
2607{
2608    pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2609    PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2610    return pf;
2611}
2612
2613static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2614
2615void qemu_display_register(QemuDisplay *ui)
2616{
2617    assert(ui->type < DISPLAY_TYPE__MAX);
2618    dpys[ui->type] = ui;
2619}
2620
2621bool qemu_display_find_default(DisplayOptions *opts)
2622{
2623    static DisplayType prio[] = {
2624#if defined(CONFIG_GTK)
2625        DISPLAY_TYPE_GTK,
2626#endif
2627#if defined(CONFIG_SDL)
2628        DISPLAY_TYPE_SDL,
2629#endif
2630#if defined(CONFIG_COCOA)
2631        DISPLAY_TYPE_COCOA
2632#endif
2633    };
2634    int i;
2635
2636    for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
2637        if (dpys[prio[i]] == NULL) {
2638            Error *local_err = NULL;
2639            int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
2640            if (rv < 0) {
2641                error_report_err(local_err);
2642            }
2643        }
2644        if (dpys[prio[i]] == NULL) {
2645            continue;
2646        }
2647        opts->type = prio[i];
2648        return true;
2649    }
2650    return false;
2651}
2652
2653void qemu_display_early_init(DisplayOptions *opts)
2654{
2655    assert(opts->type < DISPLAY_TYPE__MAX);
2656    if (opts->type == DISPLAY_TYPE_NONE) {
2657        return;
2658    }
2659    if (dpys[opts->type] == NULL) {
2660        Error *local_err = NULL;
2661        int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
2662        if (rv < 0) {
2663            error_report_err(local_err);
2664        }
2665    }
2666    if (dpys[opts->type] == NULL) {
2667        error_report("Display '%s' is not available.",
2668                     DisplayType_str(opts->type));
2669        exit(1);
2670    }
2671    if (dpys[opts->type]->early_init) {
2672        dpys[opts->type]->early_init(opts);
2673    }
2674}
2675
2676void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2677{
2678    assert(opts->type < DISPLAY_TYPE__MAX);
2679    if (opts->type == DISPLAY_TYPE_NONE) {
2680        return;
2681    }
2682    assert(dpys[opts->type] != NULL);
2683    dpys[opts->type]->init(ds, opts);
2684}
2685
2686void qemu_display_help(void)
2687{
2688    int idx;
2689
2690    printf("Available display backend types:\n");
2691    printf("none\n");
2692    for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
2693        if (!dpys[idx]) {
2694            Error *local_err = NULL;
2695            int rv = ui_module_load(DisplayType_str(idx), &local_err);
2696            if (rv < 0) {
2697                error_report_err(local_err);
2698            }
2699        }
2700        if (dpys[idx]) {
2701            printf("%s\n",  DisplayType_str(dpys[idx]->type));
2702        }
2703    }
2704}
2705
2706void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
2707{
2708    int val;
2709    ChardevVC *vc;
2710
2711    backend->type = CHARDEV_BACKEND_KIND_VC;
2712    vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2713    qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2714
2715    val = qemu_opt_get_number(opts, "width", 0);
2716    if (val != 0) {
2717        vc->has_width = true;
2718        vc->width = val;
2719    }
2720
2721    val = qemu_opt_get_number(opts, "height", 0);
2722    if (val != 0) {
2723        vc->has_height = true;
2724        vc->height = val;
2725    }
2726
2727    val = qemu_opt_get_number(opts, "cols", 0);
2728    if (val != 0) {
2729        vc->has_cols = true;
2730        vc->cols = val;
2731    }
2732
2733    val = qemu_opt_get_number(opts, "rows", 0);
2734    if (val != 0) {
2735        vc->has_rows = true;
2736        vc->rows = val;
2737    }
2738}
2739
2740static const TypeInfo qemu_console_info = {
2741    .name = TYPE_QEMU_CONSOLE,
2742    .parent = TYPE_OBJECT,
2743    .instance_size = sizeof(QemuConsole),
2744    .class_size = sizeof(QemuConsoleClass),
2745};
2746
2747static void char_vc_class_init(ObjectClass *oc, void *data)
2748{
2749    ChardevClass *cc = CHARDEV_CLASS(oc);
2750
2751    cc->parse = qemu_chr_parse_vc;
2752    cc->open = vc_chr_open;
2753    cc->chr_write = vc_chr_write;
2754    cc->chr_accept_input = vc_chr_accept_input;
2755    cc->chr_set_echo = vc_chr_set_echo;
2756}
2757
2758static const TypeInfo char_vc_type_info = {
2759    .name = TYPE_CHARDEV_VC,
2760    .parent = TYPE_CHARDEV,
2761    .instance_size = sizeof(VCChardev),
2762    .class_init = char_vc_class_init,
2763};
2764
2765void qemu_console_early_init(void)
2766{
2767    /* set the default vc driver */
2768    if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2769        type_register(&char_vc_type_info);
2770    }
2771}
2772
2773static void register_types(void)
2774{
2775    type_register_static(&qemu_console_info);
2776}
2777
2778type_init(register_types);
2779