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#include "qemu/osdep.h"
  25#include "qemu-common.h"
  26#include "ui/console.h"
  27#include "hw/qdev-core.h"
  28#include "qemu/timer.h"
  29#include "qmp-commands.h"
  30#include "sysemu/char.h"
  31#include "trace.h"
  32#include "exec/memory.h"
  33
  34#define DEFAULT_BACKSCROLL 512
  35#define CONSOLE_CURSOR_PERIOD 500
  36
  37typedef struct TextAttributes {
  38    uint8_t fgcol:4;
  39    uint8_t bgcol:4;
  40    uint8_t bold:1;
  41    uint8_t uline:1;
  42    uint8_t blink:1;
  43    uint8_t invers:1;
  44    uint8_t unvisible:1;
  45} TextAttributes;
  46
  47typedef struct TextCell {
  48    uint8_t ch;
  49    TextAttributes t_attrib;
  50} TextCell;
  51
  52#define MAX_ESC_PARAMS 3
  53
  54enum TTYState {
  55    TTY_STATE_NORM,
  56    TTY_STATE_ESC,
  57    TTY_STATE_CSI,
  58};
  59
  60typedef struct QEMUFIFO {
  61    uint8_t *buf;
  62    int buf_size;
  63    int count, wptr, rptr;
  64} QEMUFIFO;
  65
  66static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
  67{
  68    int l, len;
  69
  70    l = f->buf_size - f->count;
  71    if (len1 > l)
  72        len1 = l;
  73    len = len1;
  74    while (len > 0) {
  75        l = f->buf_size - f->wptr;
  76        if (l > len)
  77            l = len;
  78        memcpy(f->buf + f->wptr, buf, l);
  79        f->wptr += l;
  80        if (f->wptr >= f->buf_size)
  81            f->wptr = 0;
  82        buf += l;
  83        len -= l;
  84    }
  85    f->count += len1;
  86    return len1;
  87}
  88
  89static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
  90{
  91    int l, len;
  92
  93    if (len1 > f->count)
  94        len1 = f->count;
  95    len = len1;
  96    while (len > 0) {
  97        l = f->buf_size - f->rptr;
  98        if (l > len)
  99            l = len;
 100        memcpy(buf, f->buf + f->rptr, l);
 101        f->rptr += l;
 102        if (f->rptr >= f->buf_size)
 103            f->rptr = 0;
 104        buf += l;
 105        len -= l;
 106    }
 107    f->count -= len1;
 108    return len1;
 109}
 110
 111typedef enum {
 112    GRAPHIC_CONSOLE,
 113    TEXT_CONSOLE,
 114    TEXT_CONSOLE_FIXED_SIZE
 115} console_type_t;
 116
 117struct QemuConsole {
 118    Object parent;
 119
 120    int index;
 121    console_type_t console_type;
 122    DisplayState *ds;
 123    DisplaySurface *surface;
 124    int dcls;
 125    DisplayChangeListener *gl;
 126    bool gl_block;
 127
 128    /* Graphic console state.  */
 129    Object *device;
 130    uint32_t head;
 131    QemuUIInfo ui_info;
 132    QEMUTimer *ui_timer;
 133    const GraphicHwOps *hw_ops;
 134    void *hw;
 135
 136    /* Text console state */
 137    int width;
 138    int height;
 139    int total_height;
 140    int backscroll_height;
 141    int x, y;
 142    int x_saved, y_saved;
 143    int y_displayed;
 144    int y_base;
 145    TextAttributes t_attrib_default; /* default text attributes */
 146    TextAttributes t_attrib; /* currently active text attributes */
 147    TextCell *cells;
 148    int text_x[2], text_y[2], cursor_invalidate;
 149    int echo;
 150
 151    int update_x0;
 152    int update_y0;
 153    int update_x1;
 154    int update_y1;
 155
 156    enum TTYState state;
 157    int esc_params[MAX_ESC_PARAMS];
 158    int nb_esc_params;
 159
 160    CharDriverState *chr;
 161    /* fifo for key pressed */
 162    QEMUFIFO out_fifo;
 163    uint8_t out_fifo_buf[16];
 164    QEMUTimer *kbd_timer;
 165};
 166
 167struct DisplayState {
 168    QEMUTimer *gui_timer;
 169    uint64_t last_update;
 170    uint64_t update_interval;
 171    bool refreshing;
 172    bool have_gfx;
 173    bool have_text;
 174
 175    QLIST_HEAD(, DisplayChangeListener) listeners;
 176};
 177
 178static DisplayState *display_state;
 179static QemuConsole *active_console;
 180static QemuConsole **consoles;
 181static int nb_consoles = 0;
 182static bool cursor_visible_phase;
 183static QEMUTimer *cursor_timer;
 184
 185static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
 186static void dpy_refresh(DisplayState *s);
 187static DisplayState *get_alloc_displaystate(void);
 188static void text_console_update_cursor_timer(void);
 189static void text_console_update_cursor(void *opaque);
 190
 191static void gui_update(void *opaque)
 192{
 193    uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
 194    uint64_t dcl_interval;
 195    DisplayState *ds = opaque;
 196    DisplayChangeListener *dcl;
 197    int i;
 198
 199    ds->refreshing = true;
 200    dpy_refresh(ds);
 201    ds->refreshing = false;
 202
 203    QLIST_FOREACH(dcl, &ds->listeners, next) {
 204        dcl_interval = dcl->update_interval ?
 205            dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
 206        if (interval > dcl_interval) {
 207            interval = dcl_interval;
 208        }
 209    }
 210    if (ds->update_interval != interval) {
 211        ds->update_interval = interval;
 212        for (i = 0; i < nb_consoles; i++) {
 213            if (consoles[i]->hw_ops->update_interval) {
 214                consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
 215            }
 216        }
 217        trace_console_refresh(interval);
 218    }
 219    ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
 220    timer_mod(ds->gui_timer, ds->last_update + interval);
 221}
 222
 223static void gui_setup_refresh(DisplayState *ds)
 224{
 225    DisplayChangeListener *dcl;
 226    bool need_timer = false;
 227    bool have_gfx = false;
 228    bool have_text = false;
 229
 230    QLIST_FOREACH(dcl, &ds->listeners, next) {
 231        if (dcl->ops->dpy_refresh != NULL) {
 232            need_timer = true;
 233        }
 234        if (dcl->ops->dpy_gfx_update != NULL) {
 235            have_gfx = true;
 236        }
 237        if (dcl->ops->dpy_text_update != NULL) {
 238            have_text = true;
 239        }
 240    }
 241
 242    if (need_timer && ds->gui_timer == NULL) {
 243        ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
 244        timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
 245    }
 246    if (!need_timer && ds->gui_timer != NULL) {
 247        timer_del(ds->gui_timer);
 248        timer_free(ds->gui_timer);
 249        ds->gui_timer = NULL;
 250    }
 251
 252    ds->have_gfx = have_gfx;
 253    ds->have_text = have_text;
 254}
 255
 256void graphic_hw_update(QemuConsole *con)
 257{
 258    if (!con) {
 259        con = active_console;
 260    }
 261    if (con && con->hw_ops->gfx_update) {
 262        con->hw_ops->gfx_update(con->hw);
 263    }
 264}
 265
 266void graphic_hw_gl_block(QemuConsole *con, bool block)
 267{
 268    assert(con != NULL);
 269
 270    con->gl_block = block;
 271    if (con->hw_ops->gl_block) {
 272        con->hw_ops->gl_block(con->hw, block);
 273    }
 274}
 275
 276void graphic_hw_invalidate(QemuConsole *con)
 277{
 278    if (!con) {
 279        con = active_console;
 280    }
 281    if (con && con->hw_ops->invalidate) {
 282        con->hw_ops->invalidate(con->hw);
 283    }
 284}
 285
 286static void ppm_save(const char *filename, DisplaySurface *ds,
 287                     Error **errp)
 288{
 289    int width = pixman_image_get_width(ds->image);
 290    int height = pixman_image_get_height(ds->image);
 291    int fd;
 292    FILE *f;
 293    int y;
 294    int ret;
 295    pixman_image_t *linebuf;
 296
 297    trace_ppm_save(filename, ds);
 298    fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
 299    if (fd == -1) {
 300        error_setg(errp, "failed to open file '%s': %s", filename,
 301                   strerror(errno));
 302        return;
 303    }
 304    f = fdopen(fd, "wb");
 305    ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
 306    if (ret < 0) {
 307        linebuf = NULL;
 308        goto write_err;
 309    }
 310    linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
 311    for (y = 0; y < height; y++) {
 312        qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
 313        clearerr(f);
 314        ret = fwrite(pixman_image_get_data(linebuf), 1,
 315                     pixman_image_get_stride(linebuf), f);
 316        (void)ret;
 317        if (ferror(f)) {
 318            goto write_err;
 319        }
 320    }
 321
 322out:
 323    qemu_pixman_image_unref(linebuf);
 324    fclose(f);
 325    return;
 326
 327write_err:
 328    error_setg(errp, "failed to write to file '%s': %s", filename,
 329               strerror(errno));
 330    unlink(filename);
 331    goto out;
 332}
 333
 334void qmp_screendump(const char *filename, Error **errp)
 335{
 336    QemuConsole *con = qemu_console_lookup_by_index(0);
 337    DisplaySurface *surface;
 338
 339    if (con == NULL) {
 340        error_setg(errp, "There is no QemuConsole I can screendump from.");
 341        return;
 342    }
 343
 344    graphic_hw_update(con);
 345    surface = qemu_console_surface(con);
 346    ppm_save(filename, surface, errp);
 347}
 348
 349void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
 350{
 351    if (!con) {
 352        con = active_console;
 353    }
 354    if (con && con->hw_ops->text_update) {
 355        con->hw_ops->text_update(con->hw, chardata);
 356    }
 357}
 358
 359static void vga_fill_rect(QemuConsole *con,
 360                          int posx, int posy, int width, int height,
 361                          pixman_color_t color)
 362{
 363    DisplaySurface *surface = qemu_console_surface(con);
 364    pixman_rectangle16_t rect = {
 365        .x = posx, .y = posy, .width = width, .height = height
 366    };
 367
 368    pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
 369                                 &color, 1, &rect);
 370}
 371
 372/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
 373static void vga_bitblt(QemuConsole *con,
 374                       int xs, int ys, int xd, int yd, int w, int h)
 375{
 376    DisplaySurface *surface = qemu_console_surface(con);
 377
 378    pixman_image_composite(PIXMAN_OP_SRC,
 379                           surface->image, NULL, surface->image,
 380                           xs, ys, 0, 0, xd, yd, w, h);
 381}
 382
 383/***********************************************************/
 384/* basic char display */
 385
 386#define FONT_HEIGHT 16
 387#define FONT_WIDTH 8
 388
 389#include "vgafont.h"
 390
 391#define QEMU_RGB(r, g, b)                                               \
 392    { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
 393
 394static const pixman_color_t color_table_rgb[2][8] = {
 395    {   /* dark */
 396        [QEMU_COLOR_BLACK]   = QEMU_RGB(0x00, 0x00, 0x00),  /* black */
 397        [QEMU_COLOR_BLUE]    = QEMU_RGB(0x00, 0x00, 0xaa),  /* blue */
 398        [QEMU_COLOR_GREEN]   = QEMU_RGB(0x00, 0xaa, 0x00),  /* green */
 399        [QEMU_COLOR_CYAN]    = QEMU_RGB(0x00, 0xaa, 0xaa),  /* cyan */
 400        [QEMU_COLOR_RED]     = QEMU_RGB(0xaa, 0x00, 0x00),  /* red */
 401        [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa),  /* magenta */
 402        [QEMU_COLOR_YELLOW]  = QEMU_RGB(0xaa, 0xaa, 0x00),  /* yellow */
 403        [QEMU_COLOR_WHITE]   = QEMU_RGB(0xaa, 0xaa, 0xaa),  /* white */
 404    },
 405    {   /* bright */
 406        [QEMU_COLOR_BLACK]   = QEMU_RGB(0x00, 0x00, 0x00),  /* black */
 407        [QEMU_COLOR_BLUE]    = QEMU_RGB(0x00, 0x00, 0xff),  /* blue */
 408        [QEMU_COLOR_GREEN]   = QEMU_RGB(0x00, 0xff, 0x00),  /* green */
 409        [QEMU_COLOR_CYAN]    = QEMU_RGB(0x00, 0xff, 0xff),  /* cyan */
 410        [QEMU_COLOR_RED]     = QEMU_RGB(0xff, 0x00, 0x00),  /* red */
 411        [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff),  /* magenta */
 412        [QEMU_COLOR_YELLOW]  = QEMU_RGB(0xff, 0xff, 0x00),  /* yellow */
 413        [QEMU_COLOR_WHITE]   = QEMU_RGB(0xff, 0xff, 0xff),  /* white */
 414    }
 415};
 416
 417static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
 418                          TextAttributes *t_attrib)
 419{
 420    static pixman_image_t *glyphs[256];
 421    DisplaySurface *surface = qemu_console_surface(s);
 422    pixman_color_t fgcol, bgcol;
 423
 424    if (t_attrib->invers) {
 425        bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
 426        fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
 427    } else {
 428        fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
 429        bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
 430    }
 431
 432    if (!glyphs[ch]) {
 433        glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
 434    }
 435    qemu_pixman_glyph_render(glyphs[ch], surface->image,
 436                             &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
 437}
 438
 439static void text_console_resize(QemuConsole *s)
 440{
 441    TextCell *cells, *c, *c1;
 442    int w1, x, y, last_width;
 443
 444    last_width = s->width;
 445    s->width = surface_width(s->surface) / FONT_WIDTH;
 446    s->height = surface_height(s->surface) / FONT_HEIGHT;
 447
 448    w1 = last_width;
 449    if (s->width < w1)
 450        w1 = s->width;
 451
 452    cells = g_new(TextCell, s->width * s->total_height);
 453    for(y = 0; y < s->total_height; y++) {
 454        c = &cells[y * s->width];
 455        if (w1 > 0) {
 456            c1 = &s->cells[y * last_width];
 457            for(x = 0; x < w1; x++) {
 458                *c++ = *c1++;
 459            }
 460        }
 461        for(x = w1; x < s->width; x++) {
 462            c->ch = ' ';
 463            c->t_attrib = s->t_attrib_default;
 464            c++;
 465        }
 466    }
 467    g_free(s->cells);
 468    s->cells = cells;
 469}
 470
 471static inline void text_update_xy(QemuConsole *s, int x, int y)
 472{
 473    s->text_x[0] = MIN(s->text_x[0], x);
 474    s->text_x[1] = MAX(s->text_x[1], x);
 475    s->text_y[0] = MIN(s->text_y[0], y);
 476    s->text_y[1] = MAX(s->text_y[1], y);
 477}
 478
 479static void invalidate_xy(QemuConsole *s, int x, int y)
 480{
 481    if (!qemu_console_is_visible(s)) {
 482        return;
 483    }
 484    if (s->update_x0 > x * FONT_WIDTH)
 485        s->update_x0 = x * FONT_WIDTH;
 486    if (s->update_y0 > y * FONT_HEIGHT)
 487        s->update_y0 = y * FONT_HEIGHT;
 488    if (s->update_x1 < (x + 1) * FONT_WIDTH)
 489        s->update_x1 = (x + 1) * FONT_WIDTH;
 490    if (s->update_y1 < (y + 1) * FONT_HEIGHT)
 491        s->update_y1 = (y + 1) * FONT_HEIGHT;
 492}
 493
 494static void update_xy(QemuConsole *s, int x, int y)
 495{
 496    TextCell *c;
 497    int y1, y2;
 498
 499    if (s->ds->have_text) {
 500        text_update_xy(s, x, y);
 501    }
 502
 503    y1 = (s->y_base + y) % s->total_height;
 504    y2 = y1 - s->y_displayed;
 505    if (y2 < 0) {
 506        y2 += s->total_height;
 507    }
 508    if (y2 < s->height) {
 509        c = &s->cells[y1 * s->width + x];
 510        vga_putcharxy(s, x, y2, c->ch,
 511                      &(c->t_attrib));
 512        invalidate_xy(s, x, y2);
 513    }
 514}
 515
 516static void console_show_cursor(QemuConsole *s, int show)
 517{
 518    TextCell *c;
 519    int y, y1;
 520    int x = s->x;
 521
 522    if (s->ds->have_text) {
 523        s->cursor_invalidate = 1;
 524    }
 525
 526    if (x >= s->width) {
 527        x = s->width - 1;
 528    }
 529    y1 = (s->y_base + s->y) % s->total_height;
 530    y = y1 - s->y_displayed;
 531    if (y < 0) {
 532        y += s->total_height;
 533    }
 534    if (y < s->height) {
 535        c = &s->cells[y1 * s->width + x];
 536        if (show && cursor_visible_phase) {
 537            TextAttributes t_attrib = s->t_attrib_default;
 538            t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
 539            vga_putcharxy(s, x, y, c->ch, &t_attrib);
 540        } else {
 541            vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
 542        }
 543        invalidate_xy(s, x, y);
 544    }
 545}
 546
 547static void console_refresh(QemuConsole *s)
 548{
 549    DisplaySurface *surface = qemu_console_surface(s);
 550    TextCell *c;
 551    int x, y, y1;
 552
 553    if (s->ds->have_text) {
 554        s->text_x[0] = 0;
 555        s->text_y[0] = 0;
 556        s->text_x[1] = s->width - 1;
 557        s->text_y[1] = s->height - 1;
 558        s->cursor_invalidate = 1;
 559    }
 560
 561    vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
 562                  color_table_rgb[0][QEMU_COLOR_BLACK]);
 563    y1 = s->y_displayed;
 564    for (y = 0; y < s->height; y++) {
 565        c = s->cells + y1 * s->width;
 566        for (x = 0; x < s->width; x++) {
 567            vga_putcharxy(s, x, y, c->ch,
 568                          &(c->t_attrib));
 569            c++;
 570        }
 571        if (++y1 == s->total_height) {
 572            y1 = 0;
 573        }
 574    }
 575    console_show_cursor(s, 1);
 576    dpy_gfx_update(s, 0, 0,
 577                   surface_width(surface), surface_height(surface));
 578}
 579
 580static void console_scroll(QemuConsole *s, int ydelta)
 581{
 582    int i, y1;
 583
 584    if (ydelta > 0) {
 585        for(i = 0; i < ydelta; i++) {
 586            if (s->y_displayed == s->y_base)
 587                break;
 588            if (++s->y_displayed == s->total_height)
 589                s->y_displayed = 0;
 590        }
 591    } else {
 592        ydelta = -ydelta;
 593        i = s->backscroll_height;
 594        if (i > s->total_height - s->height)
 595            i = s->total_height - s->height;
 596        y1 = s->y_base - i;
 597        if (y1 < 0)
 598            y1 += s->total_height;
 599        for(i = 0; i < ydelta; i++) {
 600            if (s->y_displayed == y1)
 601                break;
 602            if (--s->y_displayed < 0)
 603                s->y_displayed = s->total_height - 1;
 604        }
 605    }
 606    console_refresh(s);
 607}
 608
 609static void console_put_lf(QemuConsole *s)
 610{
 611    TextCell *c;
 612    int x, y1;
 613
 614    s->y++;
 615    if (s->y >= s->height) {
 616        s->y = s->height - 1;
 617
 618        if (s->y_displayed == s->y_base) {
 619            if (++s->y_displayed == s->total_height)
 620                s->y_displayed = 0;
 621        }
 622        if (++s->y_base == s->total_height)
 623            s->y_base = 0;
 624        if (s->backscroll_height < s->total_height)
 625            s->backscroll_height++;
 626        y1 = (s->y_base + s->height - 1) % s->total_height;
 627        c = &s->cells[y1 * s->width];
 628        for(x = 0; x < s->width; x++) {
 629            c->ch = ' ';
 630            c->t_attrib = s->t_attrib_default;
 631            c++;
 632        }
 633        if (s->y_displayed == s->y_base) {
 634            if (s->ds->have_text) {
 635                s->text_x[0] = 0;
 636                s->text_y[0] = 0;
 637                s->text_x[1] = s->width - 1;
 638                s->text_y[1] = s->height - 1;
 639            }
 640
 641            vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
 642                       s->width * FONT_WIDTH,
 643                       (s->height - 1) * FONT_HEIGHT);
 644            vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
 645                          s->width * FONT_WIDTH, FONT_HEIGHT,
 646                          color_table_rgb[0][s->t_attrib_default.bgcol]);
 647            s->update_x0 = 0;
 648            s->update_y0 = 0;
 649            s->update_x1 = s->width * FONT_WIDTH;
 650            s->update_y1 = s->height * FONT_HEIGHT;
 651        }
 652    }
 653}
 654
 655/* Set console attributes depending on the current escape codes.
 656 * NOTE: I know this code is not very efficient (checking every color for it
 657 * self) but it is more readable and better maintainable.
 658 */
 659static void console_handle_escape(QemuConsole *s)
 660{
 661    int i;
 662
 663    for (i=0; i<s->nb_esc_params; i++) {
 664        switch (s->esc_params[i]) {
 665            case 0: /* reset all console attributes to default */
 666                s->t_attrib = s->t_attrib_default;
 667                break;
 668            case 1:
 669                s->t_attrib.bold = 1;
 670                break;
 671            case 4:
 672                s->t_attrib.uline = 1;
 673                break;
 674            case 5:
 675                s->t_attrib.blink = 1;
 676                break;
 677            case 7:
 678                s->t_attrib.invers = 1;
 679                break;
 680            case 8:
 681                s->t_attrib.unvisible = 1;
 682                break;
 683            case 22:
 684                s->t_attrib.bold = 0;
 685                break;
 686            case 24:
 687                s->t_attrib.uline = 0;
 688                break;
 689            case 25:
 690                s->t_attrib.blink = 0;
 691                break;
 692            case 27:
 693                s->t_attrib.invers = 0;
 694                break;
 695            case 28:
 696                s->t_attrib.unvisible = 0;
 697                break;
 698            /* set foreground color */
 699            case 30:
 700                s->t_attrib.fgcol = QEMU_COLOR_BLACK;
 701                break;
 702            case 31:
 703                s->t_attrib.fgcol = QEMU_COLOR_RED;
 704                break;
 705            case 32:
 706                s->t_attrib.fgcol = QEMU_COLOR_GREEN;
 707                break;
 708            case 33:
 709                s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
 710                break;
 711            case 34:
 712                s->t_attrib.fgcol = QEMU_COLOR_BLUE;
 713                break;
 714            case 35:
 715                s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
 716                break;
 717            case 36:
 718                s->t_attrib.fgcol = QEMU_COLOR_CYAN;
 719                break;
 720            case 37:
 721                s->t_attrib.fgcol = QEMU_COLOR_WHITE;
 722                break;
 723            /* set background color */
 724            case 40:
 725                s->t_attrib.bgcol = QEMU_COLOR_BLACK;
 726                break;
 727            case 41:
 728                s->t_attrib.bgcol = QEMU_COLOR_RED;
 729                break;
 730            case 42:
 731                s->t_attrib.bgcol = QEMU_COLOR_GREEN;
 732                break;
 733            case 43:
 734                s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
 735                break;
 736            case 44:
 737                s->t_attrib.bgcol = QEMU_COLOR_BLUE;
 738                break;
 739            case 45:
 740                s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
 741                break;
 742            case 46:
 743                s->t_attrib.bgcol = QEMU_COLOR_CYAN;
 744                break;
 745            case 47:
 746                s->t_attrib.bgcol = QEMU_COLOR_WHITE;
 747                break;
 748        }
 749    }
 750}
 751
 752static void console_clear_xy(QemuConsole *s, int x, int y)
 753{
 754    int y1 = (s->y_base + y) % s->total_height;
 755    TextCell *c = &s->cells[y1 * s->width + x];
 756    c->ch = ' ';
 757    c->t_attrib = s->t_attrib_default;
 758    update_xy(s, x, y);
 759}
 760
 761static void console_put_one(QemuConsole *s, int ch)
 762{
 763    TextCell *c;
 764    int y1;
 765    if (s->x >= s->width) {
 766        /* line wrap */
 767        s->x = 0;
 768        console_put_lf(s);
 769    }
 770    y1 = (s->y_base + s->y) % s->total_height;
 771    c = &s->cells[y1 * s->width + s->x];
 772    c->ch = ch;
 773    c->t_attrib = s->t_attrib;
 774    update_xy(s, s->x, s->y);
 775    s->x++;
 776}
 777
 778static void console_respond_str(QemuConsole *s, const char *buf)
 779{
 780    while (*buf) {
 781        console_put_one(s, *buf);
 782        buf++;
 783    }
 784}
 785
 786/* set cursor, checking bounds */
 787static void set_cursor(QemuConsole *s, int x, int y)
 788{
 789    if (x < 0) {
 790        x = 0;
 791    }
 792    if (y < 0) {
 793        y = 0;
 794    }
 795    if (y >= s->height) {
 796        y = s->height - 1;
 797    }
 798    if (x >= s->width) {
 799        x = s->width - 1;
 800    }
 801
 802    s->x = x;
 803    s->y = y;
 804}
 805
 806static void console_putchar(QemuConsole *s, int ch)
 807{
 808    int i;
 809    int x, y;
 810    char response[40];
 811
 812    switch(s->state) {
 813    case TTY_STATE_NORM:
 814        switch(ch) {
 815        case '\r':  /* carriage return */
 816            s->x = 0;
 817            break;
 818        case '\n':  /* newline */
 819            console_put_lf(s);
 820            break;
 821        case '\b':  /* backspace */
 822            if (s->x > 0)
 823                s->x--;
 824            break;
 825        case '\t':  /* tabspace */
 826            if (s->x + (8 - (s->x % 8)) > s->width) {
 827                s->x = 0;
 828                console_put_lf(s);
 829            } else {
 830                s->x = s->x + (8 - (s->x % 8));
 831            }
 832            break;
 833        case '\a':  /* alert aka. bell */
 834            /* TODO: has to be implemented */
 835            break;
 836        case 14:
 837            /* SI (shift in), character set 0 (ignored) */
 838            break;
 839        case 15:
 840            /* SO (shift out), character set 1 (ignored) */
 841            break;
 842        case 27:    /* esc (introducing an escape sequence) */
 843            s->state = TTY_STATE_ESC;
 844            break;
 845        default:
 846            console_put_one(s, ch);
 847            break;
 848        }
 849        break;
 850    case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
 851        if (ch == '[') {
 852            for(i=0;i<MAX_ESC_PARAMS;i++)
 853                s->esc_params[i] = 0;
 854            s->nb_esc_params = 0;
 855            s->state = TTY_STATE_CSI;
 856        } else {
 857            s->state = TTY_STATE_NORM;
 858        }
 859        break;
 860    case TTY_STATE_CSI: /* handle escape sequence parameters */
 861        if (ch >= '0' && ch <= '9') {
 862            if (s->nb_esc_params < MAX_ESC_PARAMS) {
 863                int *param = &s->esc_params[s->nb_esc_params];
 864                int digit = (ch - '0');
 865
 866                *param = (*param <= (INT_MAX - digit) / 10) ?
 867                         *param * 10 + digit : INT_MAX;
 868            }
 869        } else {
 870            if (s->nb_esc_params < MAX_ESC_PARAMS)
 871                s->nb_esc_params++;
 872            if (ch == ';')
 873                break;
 874            trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
 875                                      ch, s->nb_esc_params);
 876            s->state = TTY_STATE_NORM;
 877            switch(ch) {
 878            case 'A':
 879                /* move cursor up */
 880                if (s->esc_params[0] == 0) {
 881                    s->esc_params[0] = 1;
 882                }
 883                set_cursor(s, s->x, s->y - s->esc_params[0]);
 884                break;
 885            case 'B':
 886                /* move cursor down */
 887                if (s->esc_params[0] == 0) {
 888                    s->esc_params[0] = 1;
 889                }
 890                set_cursor(s, s->x, s->y + s->esc_params[0]);
 891                break;
 892            case 'C':
 893                /* move cursor right */
 894                if (s->esc_params[0] == 0) {
 895                    s->esc_params[0] = 1;
 896                }
 897                set_cursor(s, s->x + s->esc_params[0], s->y);
 898                break;
 899            case 'D':
 900                /* move cursor left */
 901                if (s->esc_params[0] == 0) {
 902                    s->esc_params[0] = 1;
 903                }
 904                set_cursor(s, s->x - s->esc_params[0], s->y);
 905                break;
 906            case 'G':
 907                /* move cursor to column */
 908                set_cursor(s, s->esc_params[0] - 1, s->y);
 909                break;
 910            case 'f':
 911            case 'H':
 912                /* move cursor to row, column */
 913                set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
 914                break;
 915            case 'J':
 916                switch (s->esc_params[0]) {
 917                case 0:
 918                    /* clear to end of screen */
 919                    for (y = s->y; y < s->height; y++) {
 920                        for (x = 0; x < s->width; x++) {
 921                            if (y == s->y && x < s->x) {
 922                                continue;
 923                            }
 924                            console_clear_xy(s, x, y);
 925                        }
 926                    }
 927                    break;
 928                case 1:
 929                    /* clear from beginning of screen */
 930                    for (y = 0; y <= s->y; y++) {
 931                        for (x = 0; x < s->width; x++) {
 932                            if (y == s->y && x > s->x) {
 933                                break;
 934                            }
 935                            console_clear_xy(s, x, y);
 936                        }
 937                    }
 938                    break;
 939                case 2:
 940                    /* clear entire screen */
 941                    for (y = 0; y <= s->height; y++) {
 942                        for (x = 0; x < s->width; x++) {
 943                            console_clear_xy(s, x, y);
 944                        }
 945                    }
 946                    break;
 947                }
 948                break;
 949            case 'K':
 950                switch (s->esc_params[0]) {
 951                case 0:
 952                    /* clear to eol */
 953                    for(x = s->x; x < s->width; x++) {
 954                        console_clear_xy(s, x, s->y);
 955                    }
 956                    break;
 957                case 1:
 958                    /* clear from beginning of line */
 959                    for (x = 0; x <= s->x; x++) {
 960                        console_clear_xy(s, x, s->y);
 961                    }
 962                    break;
 963                case 2:
 964                    /* clear entire line */
 965                    for(x = 0; x < s->width; x++) {
 966                        console_clear_xy(s, x, s->y);
 967                    }
 968                    break;
 969                }
 970                break;
 971            case 'm':
 972                console_handle_escape(s);
 973                break;
 974            case 'n':
 975                switch (s->esc_params[0]) {
 976                case 5:
 977                    /* report console status (always succeed)*/
 978                    console_respond_str(s, "\033[0n");
 979                    break;
 980                case 6:
 981                    /* report cursor position */
 982                    sprintf(response, "\033[%d;%dR",
 983                           (s->y_base + s->y) % s->total_height + 1,
 984                            s->x + 1);
 985                    console_respond_str(s, response);
 986                    break;
 987                }
 988                break;
 989            case 's':
 990                /* save cursor position */
 991                s->x_saved = s->x;
 992                s->y_saved = s->y;
 993                break;
 994            case 'u':
 995                /* restore cursor position */
 996                s->x = s->x_saved;
 997                s->y = s->y_saved;
 998                break;
 999            default:
1000                trace_console_putchar_unhandled(ch);
1001                break;
1002            }
1003            break;
1004        }
1005    }
1006}
1007
1008void console_select(unsigned int index)
1009{
1010    DisplayChangeListener *dcl;
1011    QemuConsole *s;
1012
1013    trace_console_select(index);
1014    s = qemu_console_lookup_by_index(index);
1015    if (s) {
1016        DisplayState *ds = s->ds;
1017
1018        active_console = s;
1019        if (ds->have_gfx) {
1020            QLIST_FOREACH(dcl, &ds->listeners, next) {
1021                if (dcl->con != NULL) {
1022                    continue;
1023                }
1024                if (dcl->ops->dpy_gfx_switch) {
1025                    dcl->ops->dpy_gfx_switch(dcl, s->surface);
1026                }
1027            }
1028            dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1029                           surface_height(s->surface));
1030        }
1031        if (ds->have_text) {
1032            dpy_text_resize(s, s->width, s->height);
1033        }
1034        text_console_update_cursor(NULL);
1035    }
1036}
1037
1038static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1039{
1040    QemuConsole *s = chr->opaque;
1041    int i;
1042
1043    s->update_x0 = s->width * FONT_WIDTH;
1044    s->update_y0 = s->height * FONT_HEIGHT;
1045    s->update_x1 = 0;
1046    s->update_y1 = 0;
1047    console_show_cursor(s, 0);
1048    for(i = 0; i < len; i++) {
1049        console_putchar(s, buf[i]);
1050    }
1051    console_show_cursor(s, 1);
1052    if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1053        dpy_gfx_update(s, s->update_x0, s->update_y0,
1054                       s->update_x1 - s->update_x0,
1055                       s->update_y1 - s->update_y0);
1056    }
1057    return len;
1058}
1059
1060static void kbd_send_chars(void *opaque)
1061{
1062    QemuConsole *s = opaque;
1063    int len;
1064    uint8_t buf[16];
1065
1066    len = qemu_chr_be_can_write(s->chr);
1067    if (len > s->out_fifo.count)
1068        len = s->out_fifo.count;
1069    if (len > 0) {
1070        if (len > sizeof(buf))
1071            len = sizeof(buf);
1072        qemu_fifo_read(&s->out_fifo, buf, len);
1073        qemu_chr_be_write(s->chr, buf, len);
1074    }
1075    /* characters are pending: we send them a bit later (XXX:
1076       horrible, should change char device API) */
1077    if (s->out_fifo.count > 0) {
1078        timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1079    }
1080}
1081
1082/* called when an ascii key is pressed */
1083void kbd_put_keysym_console(QemuConsole *s, int keysym)
1084{
1085    uint8_t buf[16], *q;
1086    CharBackend *be;
1087    int c;
1088
1089    if (!s || (s->console_type == GRAPHIC_CONSOLE))
1090        return;
1091
1092    switch(keysym) {
1093    case QEMU_KEY_CTRL_UP:
1094        console_scroll(s, -1);
1095        break;
1096    case QEMU_KEY_CTRL_DOWN:
1097        console_scroll(s, 1);
1098        break;
1099    case QEMU_KEY_CTRL_PAGEUP:
1100        console_scroll(s, -10);
1101        break;
1102    case QEMU_KEY_CTRL_PAGEDOWN:
1103        console_scroll(s, 10);
1104        break;
1105    default:
1106        /* convert the QEMU keysym to VT100 key string */
1107        q = buf;
1108        if (keysym >= 0xe100 && keysym <= 0xe11f) {
1109            *q++ = '\033';
1110            *q++ = '[';
1111            c = keysym - 0xe100;
1112            if (c >= 10)
1113                *q++ = '0' + (c / 10);
1114            *q++ = '0' + (c % 10);
1115            *q++ = '~';
1116        } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1117            *q++ = '\033';
1118            *q++ = '[';
1119            *q++ = keysym & 0xff;
1120        } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1121            console_puts(s->chr, (const uint8_t *) "\r", 1);
1122            *q++ = '\n';
1123        } else {
1124            *q++ = keysym;
1125        }
1126        if (s->echo) {
1127            console_puts(s->chr, buf, q - buf);
1128        }
1129        be = s->chr->be;
1130        if (be && be->chr_read) {
1131            qemu_fifo_write(&s->out_fifo, buf, q - buf);
1132            kbd_send_chars(s);
1133        }
1134        break;
1135    }
1136}
1137
1138static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1139    [Q_KEY_CODE_UP]     = QEMU_KEY_UP,
1140    [Q_KEY_CODE_DOWN]   = QEMU_KEY_DOWN,
1141    [Q_KEY_CODE_RIGHT]  = QEMU_KEY_RIGHT,
1142    [Q_KEY_CODE_LEFT]   = QEMU_KEY_LEFT,
1143    [Q_KEY_CODE_HOME]   = QEMU_KEY_HOME,
1144    [Q_KEY_CODE_END]    = QEMU_KEY_END,
1145    [Q_KEY_CODE_PGUP]   = QEMU_KEY_PAGEUP,
1146    [Q_KEY_CODE_PGDN]   = QEMU_KEY_PAGEDOWN,
1147    [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1148    [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1149};
1150
1151bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1152{
1153    int keysym;
1154
1155    keysym = qcode_to_keysym[qcode];
1156    if (keysym == 0) {
1157        return false;
1158    }
1159    kbd_put_keysym_console(s, keysym);
1160    return true;
1161}
1162
1163void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1164{
1165    int i;
1166
1167    for (i = 0; i < len && str[i]; i++) {
1168        kbd_put_keysym_console(s, str[i]);
1169    }
1170}
1171
1172void kbd_put_keysym(int keysym)
1173{
1174    kbd_put_keysym_console(active_console, keysym);
1175}
1176
1177static void text_console_invalidate(void *opaque)
1178{
1179    QemuConsole *s = (QemuConsole *) opaque;
1180
1181    if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1182        text_console_resize(s);
1183    }
1184    console_refresh(s);
1185}
1186
1187static void text_console_update(void *opaque, console_ch_t *chardata)
1188{
1189    QemuConsole *s = (QemuConsole *) opaque;
1190    int i, j, src;
1191
1192    if (s->text_x[0] <= s->text_x[1]) {
1193        src = (s->y_base + s->text_y[0]) * s->width;
1194        chardata += s->text_y[0] * s->width;
1195        for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1196            for (j = 0; j < s->width; j++, src++) {
1197                console_write_ch(chardata ++,
1198                                 ATTR2CHTYPE(s->cells[src].ch,
1199                                             s->cells[src].t_attrib.fgcol,
1200                                             s->cells[src].t_attrib.bgcol,
1201                                             s->cells[src].t_attrib.bold));
1202            }
1203        dpy_text_update(s, s->text_x[0], s->text_y[0],
1204                        s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1205        s->text_x[0] = s->width;
1206        s->text_y[0] = s->height;
1207        s->text_x[1] = 0;
1208        s->text_y[1] = 0;
1209    }
1210    if (s->cursor_invalidate) {
1211        dpy_text_cursor(s, s->x, s->y);
1212        s->cursor_invalidate = 0;
1213    }
1214}
1215
1216static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1217                                uint32_t head)
1218{
1219    Object *obj;
1220    QemuConsole *s;
1221    int i;
1222
1223    obj = object_new(TYPE_QEMU_CONSOLE);
1224    s = QEMU_CONSOLE(obj);
1225    s->head = head;
1226    object_property_add_link(obj, "device", TYPE_DEVICE,
1227                             (Object **)&s->device,
1228                             object_property_allow_set_link,
1229                             OBJ_PROP_LINK_UNREF_ON_RELEASE,
1230                             &error_abort);
1231    object_property_add_uint32_ptr(obj, "head",
1232                                   &s->head, &error_abort);
1233
1234    if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1235        (console_type == GRAPHIC_CONSOLE))) {
1236        active_console = s;
1237    }
1238    s->ds = ds;
1239    s->console_type = console_type;
1240
1241    consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1242    if (console_type != GRAPHIC_CONSOLE) {
1243        s->index = nb_consoles;
1244        consoles[nb_consoles++] = s;
1245    } else {
1246        /* HACK: Put graphical consoles before text consoles.  */
1247        for (i = nb_consoles; i > 0; i--) {
1248            if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1249                break;
1250            consoles[i] = consoles[i - 1];
1251            consoles[i]->index = i;
1252        }
1253        s->index = i;
1254        consoles[i] = s;
1255        nb_consoles++;
1256    }
1257    return s;
1258}
1259
1260static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1261{
1262    qemu_pixman_image_unref(surface->image);
1263    surface->image = NULL;
1264
1265    surface->format = PIXMAN_x8r8g8b8;
1266    surface->image = pixman_image_create_bits(surface->format,
1267                                              width, height,
1268                                              NULL, width * 4);
1269    assert(surface->image != NULL);
1270
1271    surface->flags = QEMU_ALLOCATED_FLAG;
1272}
1273
1274DisplaySurface *qemu_create_displaysurface(int width, int height)
1275{
1276    DisplaySurface *surface = g_new0(DisplaySurface, 1);
1277
1278    trace_displaysurface_create(surface, width, height);
1279    qemu_alloc_display(surface, width, height);
1280    return surface;
1281}
1282
1283DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1284                                                pixman_format_code_t format,
1285                                                int linesize, uint8_t *data)
1286{
1287    DisplaySurface *surface = g_new0(DisplaySurface, 1);
1288
1289    trace_displaysurface_create_from(surface, width, height, format);
1290    surface->format = format;
1291    surface->image = pixman_image_create_bits(surface->format,
1292                                              width, height,
1293                                              (void *)data, linesize);
1294    assert(surface->image != NULL);
1295
1296    return surface;
1297}
1298
1299DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1300{
1301    DisplaySurface *surface = g_new0(DisplaySurface, 1);
1302
1303    trace_displaysurface_create_pixman(surface);
1304    surface->format = pixman_image_get_format(image);
1305    surface->image = pixman_image_ref(image);
1306
1307    return surface;
1308}
1309
1310static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1311                                               void *unused)
1312{
1313    void *data = pixman_image_get_data(image);
1314    uint32_t size = pixman_image_get_stride(image) *
1315        pixman_image_get_height(image);
1316    cpu_physical_memory_unmap(data, size, 0, 0);
1317}
1318
1319DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1320                                                    pixman_format_code_t format,
1321                                                    int linesize, uint64_t addr)
1322{
1323    DisplaySurface *surface;
1324    hwaddr size;
1325    void *data;
1326
1327    if (linesize == 0) {
1328        linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1329    }
1330
1331    size = (hwaddr)linesize * height;
1332    data = cpu_physical_memory_map(addr, &size, 0);
1333    if (size != (hwaddr)linesize * height) {
1334        cpu_physical_memory_unmap(data, size, 0, 0);
1335        return NULL;
1336    }
1337
1338    surface = qemu_create_displaysurface_from
1339        (width, height, format, linesize, data);
1340    pixman_image_set_destroy_function
1341        (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1342
1343    return surface;
1344}
1345
1346static DisplaySurface *qemu_create_message_surface(int w, int h,
1347                                                   const char *msg)
1348{
1349    DisplaySurface *surface = qemu_create_displaysurface(w, h);
1350    pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1351    pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1352    pixman_image_t *glyph;
1353    int len, x, y, i;
1354
1355    len = strlen(msg);
1356    x = (w / FONT_WIDTH  - len) / 2;
1357    y = (h / FONT_HEIGHT - 1)   / 2;
1358    for (i = 0; i < len; i++) {
1359        glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1360        qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1361                                 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1362        qemu_pixman_image_unref(glyph);
1363    }
1364    return surface;
1365}
1366
1367void qemu_free_displaysurface(DisplaySurface *surface)
1368{
1369    if (surface == NULL) {
1370        return;
1371    }
1372    trace_displaysurface_free(surface);
1373    qemu_pixman_image_unref(surface->image);
1374    g_free(surface);
1375}
1376
1377bool console_has_gl(QemuConsole *con)
1378{
1379    return con->gl != NULL;
1380}
1381
1382void register_displaychangelistener(DisplayChangeListener *dcl)
1383{
1384    static const char nodev[] =
1385        "This VM has no graphic display device.";
1386    static DisplaySurface *dummy;
1387    QemuConsole *con;
1388
1389    if (dcl->ops->dpy_gl_ctx_create) {
1390        /* display has opengl support */
1391        assert(dcl->con);
1392        if (dcl->con->gl) {
1393            fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1394                    dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1395            exit(1);
1396        }
1397        dcl->con->gl = dcl;
1398    }
1399
1400    trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1401    dcl->ds = get_alloc_displaystate();
1402    QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1403    gui_setup_refresh(dcl->ds);
1404    if (dcl->con) {
1405        dcl->con->dcls++;
1406        con = dcl->con;
1407    } else {
1408        con = active_console;
1409    }
1410    if (dcl->ops->dpy_gfx_switch) {
1411        if (con) {
1412            dcl->ops->dpy_gfx_switch(dcl, con->surface);
1413        } else {
1414            if (!dummy) {
1415                dummy = qemu_create_message_surface(640, 480, nodev);
1416            }
1417            dcl->ops->dpy_gfx_switch(dcl, dummy);
1418        }
1419    }
1420    text_console_update_cursor(NULL);
1421}
1422
1423void update_displaychangelistener(DisplayChangeListener *dcl,
1424                                  uint64_t interval)
1425{
1426    DisplayState *ds = dcl->ds;
1427
1428    dcl->update_interval = interval;
1429    if (!ds->refreshing && ds->update_interval > interval) {
1430        timer_mod(ds->gui_timer, ds->last_update + interval);
1431    }
1432}
1433
1434void unregister_displaychangelistener(DisplayChangeListener *dcl)
1435{
1436    DisplayState *ds = dcl->ds;
1437    trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1438    if (dcl->con) {
1439        dcl->con->dcls--;
1440    }
1441    QLIST_REMOVE(dcl, next);
1442    gui_setup_refresh(ds);
1443}
1444
1445static void dpy_set_ui_info_timer(void *opaque)
1446{
1447    QemuConsole *con = opaque;
1448
1449    con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1450}
1451
1452bool dpy_ui_info_supported(QemuConsole *con)
1453{
1454    return con->hw_ops->ui_info != NULL;
1455}
1456
1457int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1458{
1459    assert(con != NULL);
1460
1461    if (!dpy_ui_info_supported(con)) {
1462        return -1;
1463    }
1464    if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1465        /* nothing changed -- ignore */
1466        return 0;
1467    }
1468
1469    /*
1470     * Typically we get a flood of these as the user resizes the window.
1471     * Wait until the dust has settled (one second without updates), then
1472     * go notify the guest.
1473     */
1474    con->ui_info = *info;
1475    timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1476    return 0;
1477}
1478
1479void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1480{
1481    DisplayState *s = con->ds;
1482    DisplayChangeListener *dcl;
1483    int width = w;
1484    int height = h;
1485
1486    if (con->surface) {
1487        width = surface_width(con->surface);
1488        height = surface_height(con->surface);
1489    }
1490    x = MAX(x, 0);
1491    y = MAX(y, 0);
1492    x = MIN(x, width);
1493    y = MIN(y, height);
1494    w = MIN(w, width - x);
1495    h = MIN(h, height - y);
1496
1497    if (!qemu_console_is_visible(con)) {
1498        return;
1499    }
1500    QLIST_FOREACH(dcl, &s->listeners, next) {
1501        if (con != (dcl->con ? dcl->con : active_console)) {
1502            continue;
1503        }
1504        if (dcl->ops->dpy_gfx_update) {
1505            dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1506        }
1507    }
1508}
1509
1510void dpy_gfx_replace_surface(QemuConsole *con,
1511                             DisplaySurface *surface)
1512{
1513    DisplayState *s = con->ds;
1514    DisplaySurface *old_surface = con->surface;
1515    DisplayChangeListener *dcl;
1516
1517    con->surface = surface;
1518    QLIST_FOREACH(dcl, &s->listeners, next) {
1519        if (con != (dcl->con ? dcl->con : active_console)) {
1520            continue;
1521        }
1522        if (dcl->ops->dpy_gfx_switch) {
1523            dcl->ops->dpy_gfx_switch(dcl, surface);
1524        }
1525    }
1526    qemu_free_displaysurface(old_surface);
1527}
1528
1529bool dpy_gfx_check_format(QemuConsole *con,
1530                          pixman_format_code_t format)
1531{
1532    DisplayChangeListener *dcl;
1533    DisplayState *s = con->ds;
1534
1535    QLIST_FOREACH(dcl, &s->listeners, next) {
1536        if (dcl->con && dcl->con != con) {
1537            /* dcl bound to another console -> skip */
1538            continue;
1539        }
1540        if (dcl->ops->dpy_gfx_check_format) {
1541            if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1542                return false;
1543            }
1544        } else {
1545            /* default is to whitelist native 32 bpp only */
1546            if (format != qemu_default_pixman_format(32, true)) {
1547                return false;
1548            }
1549        }
1550    }
1551    return true;
1552}
1553
1554static void dpy_refresh(DisplayState *s)
1555{
1556    DisplayChangeListener *dcl;
1557
1558    QLIST_FOREACH(dcl, &s->listeners, next) {
1559        if (dcl->ops->dpy_refresh) {
1560            dcl->ops->dpy_refresh(dcl);
1561        }
1562    }
1563}
1564
1565void dpy_text_cursor(QemuConsole *con, int x, int y)
1566{
1567    DisplayState *s = con->ds;
1568    DisplayChangeListener *dcl;
1569
1570    if (!qemu_console_is_visible(con)) {
1571        return;
1572    }
1573    QLIST_FOREACH(dcl, &s->listeners, next) {
1574        if (con != (dcl->con ? dcl->con : active_console)) {
1575            continue;
1576        }
1577        if (dcl->ops->dpy_text_cursor) {
1578            dcl->ops->dpy_text_cursor(dcl, x, y);
1579        }
1580    }
1581}
1582
1583void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1584{
1585    DisplayState *s = con->ds;
1586    DisplayChangeListener *dcl;
1587
1588    if (!qemu_console_is_visible(con)) {
1589        return;
1590    }
1591    QLIST_FOREACH(dcl, &s->listeners, next) {
1592        if (con != (dcl->con ? dcl->con : active_console)) {
1593            continue;
1594        }
1595        if (dcl->ops->dpy_text_update) {
1596            dcl->ops->dpy_text_update(dcl, x, y, w, h);
1597        }
1598    }
1599}
1600
1601void dpy_text_resize(QemuConsole *con, int w, int h)
1602{
1603    DisplayState *s = con->ds;
1604    DisplayChangeListener *dcl;
1605
1606    if (!qemu_console_is_visible(con)) {
1607        return;
1608    }
1609    QLIST_FOREACH(dcl, &s->listeners, next) {
1610        if (con != (dcl->con ? dcl->con : active_console)) {
1611            continue;
1612        }
1613        if (dcl->ops->dpy_text_resize) {
1614            dcl->ops->dpy_text_resize(dcl, w, h);
1615        }
1616    }
1617}
1618
1619void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1620{
1621    DisplayState *s = con->ds;
1622    DisplayChangeListener *dcl;
1623
1624    if (!qemu_console_is_visible(con)) {
1625        return;
1626    }
1627    QLIST_FOREACH(dcl, &s->listeners, next) {
1628        if (con != (dcl->con ? dcl->con : active_console)) {
1629            continue;
1630        }
1631        if (dcl->ops->dpy_mouse_set) {
1632            dcl->ops->dpy_mouse_set(dcl, x, y, on);
1633        }
1634    }
1635}
1636
1637void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1638{
1639    DisplayState *s = con->ds;
1640    DisplayChangeListener *dcl;
1641
1642    if (!qemu_console_is_visible(con)) {
1643        return;
1644    }
1645    QLIST_FOREACH(dcl, &s->listeners, next) {
1646        if (con != (dcl->con ? dcl->con : active_console)) {
1647            continue;
1648        }
1649        if (dcl->ops->dpy_cursor_define) {
1650            dcl->ops->dpy_cursor_define(dcl, cursor);
1651        }
1652    }
1653}
1654
1655bool dpy_cursor_define_supported(QemuConsole *con)
1656{
1657    DisplayState *s = con->ds;
1658    DisplayChangeListener *dcl;
1659
1660    QLIST_FOREACH(dcl, &s->listeners, next) {
1661        if (dcl->ops->dpy_cursor_define) {
1662            return true;
1663        }
1664    }
1665    return false;
1666}
1667
1668QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1669                                struct QEMUGLParams *qparams)
1670{
1671    assert(con->gl);
1672    return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1673}
1674
1675void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1676{
1677    assert(con->gl);
1678    con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1679}
1680
1681int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1682{
1683    assert(con->gl);
1684    return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1685}
1686
1687QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1688{
1689    assert(con->gl);
1690    return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1691}
1692
1693void dpy_gl_scanout(QemuConsole *con,
1694                    uint32_t backing_id, bool backing_y_0_top,
1695                    uint32_t backing_width, uint32_t backing_height,
1696                    uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1697{
1698    assert(con->gl);
1699    con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1700                                 backing_y_0_top,
1701                                 backing_width, backing_height,
1702                                 x, y, width, height);
1703}
1704
1705void dpy_gl_update(QemuConsole *con,
1706                   uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1707{
1708    assert(con->gl);
1709    con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1710}
1711
1712/***********************************************************/
1713/* register display */
1714
1715/* console.c internal use only */
1716static DisplayState *get_alloc_displaystate(void)
1717{
1718    if (!display_state) {
1719        display_state = g_new0(DisplayState, 1);
1720        cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1721                                    text_console_update_cursor, NULL);
1722    }
1723    return display_state;
1724}
1725
1726/*
1727 * Called by main(), after creating QemuConsoles
1728 * and before initializing ui (sdl/vnc/...).
1729 */
1730DisplayState *init_displaystate(void)
1731{
1732    gchar *name;
1733    int i;
1734
1735    get_alloc_displaystate();
1736    for (i = 0; i < nb_consoles; i++) {
1737        if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1738            consoles[i]->ds == NULL) {
1739            text_console_do_init(consoles[i]->chr, display_state);
1740        }
1741
1742        /* Hook up into the qom tree here (not in new_console()), once
1743         * all QemuConsoles are created and the order / numbering
1744         * doesn't change any more */
1745        name = g_strdup_printf("console[%d]", i);
1746        object_property_add_child(container_get(object_get_root(), "/backend"),
1747                                  name, OBJECT(consoles[i]), &error_abort);
1748        g_free(name);
1749    }
1750
1751    return display_state;
1752}
1753
1754void graphic_console_set_hwops(QemuConsole *con,
1755                               const GraphicHwOps *hw_ops,
1756                               void *opaque)
1757{
1758    con->hw_ops = hw_ops;
1759    con->hw = opaque;
1760}
1761
1762QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1763                                  const GraphicHwOps *hw_ops,
1764                                  void *opaque)
1765{
1766    static const char noinit[] =
1767        "Guest has not initialized the display (yet).";
1768    int width = 640;
1769    int height = 480;
1770    QemuConsole *s;
1771    DisplayState *ds;
1772
1773    ds = get_alloc_displaystate();
1774    trace_console_gfx_new();
1775    s = new_console(ds, GRAPHIC_CONSOLE, head);
1776    s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1777    graphic_console_set_hwops(s, hw_ops, opaque);
1778    if (dev) {
1779        object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1780                                 &error_abort);
1781    }
1782
1783    s->surface = qemu_create_message_surface(width, height, noinit);
1784    return s;
1785}
1786
1787QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1788{
1789    if (index >= nb_consoles) {
1790        return NULL;
1791    }
1792    return consoles[index];
1793}
1794
1795QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1796{
1797    Object *obj;
1798    uint32_t h;
1799    int i;
1800
1801    for (i = 0; i < nb_consoles; i++) {
1802        if (!consoles[i]) {
1803            continue;
1804        }
1805        obj = object_property_get_link(OBJECT(consoles[i]),
1806                                       "device", &error_abort);
1807        if (DEVICE(obj) != dev) {
1808            continue;
1809        }
1810        h = object_property_get_int(OBJECT(consoles[i]),
1811                                    "head", &error_abort);
1812        if (h != head) {
1813            continue;
1814        }
1815        return consoles[i];
1816    }
1817    return NULL;
1818}
1819
1820QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1821                                                uint32_t head, Error **errp)
1822{
1823    DeviceState *dev;
1824    QemuConsole *con;
1825
1826    dev = qdev_find_recursive(sysbus_get_default(), device_id);
1827    if (dev == NULL) {
1828        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1829                  "Device '%s' not found", device_id);
1830        return NULL;
1831    }
1832
1833    con = qemu_console_lookup_by_device(dev, head);
1834    if (con == NULL) {
1835        error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1836                   device_id, head);
1837        return NULL;
1838    }
1839
1840    return con;
1841}
1842
1843bool qemu_console_is_visible(QemuConsole *con)
1844{
1845    return (con == active_console) || (con->dcls > 0);
1846}
1847
1848bool qemu_console_is_graphic(QemuConsole *con)
1849{
1850    if (con == NULL) {
1851        con = active_console;
1852    }
1853    return con && (con->console_type == GRAPHIC_CONSOLE);
1854}
1855
1856bool qemu_console_is_fixedsize(QemuConsole *con)
1857{
1858    if (con == NULL) {
1859        con = active_console;
1860    }
1861    return con && (con->console_type != TEXT_CONSOLE);
1862}
1863
1864bool qemu_console_is_gl_blocked(QemuConsole *con)
1865{
1866    assert(con != NULL);
1867    return con->gl_block;
1868}
1869
1870char *qemu_console_get_label(QemuConsole *con)
1871{
1872    if (con->console_type == GRAPHIC_CONSOLE) {
1873        if (con->device) {
1874            return g_strdup(object_get_typename(con->device));
1875        }
1876        return g_strdup("VGA");
1877    } else {
1878        if (con->chr && con->chr->label) {
1879            return g_strdup(con->chr->label);
1880        }
1881        return g_strdup_printf("vc%d", con->index);
1882    }
1883}
1884
1885int qemu_console_get_index(QemuConsole *con)
1886{
1887    if (con == NULL) {
1888        con = active_console;
1889    }
1890    return con ? con->index : -1;
1891}
1892
1893uint32_t qemu_console_get_head(QemuConsole *con)
1894{
1895    if (con == NULL) {
1896        con = active_console;
1897    }
1898    return con ? con->head : -1;
1899}
1900
1901QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1902{
1903    assert(con != NULL);
1904    return &con->ui_info;
1905}
1906
1907int qemu_console_get_width(QemuConsole *con, int fallback)
1908{
1909    if (con == NULL) {
1910        con = active_console;
1911    }
1912    return con ? surface_width(con->surface) : fallback;
1913}
1914
1915int qemu_console_get_height(QemuConsole *con, int fallback)
1916{
1917    if (con == NULL) {
1918        con = active_console;
1919    }
1920    return con ? surface_height(con->surface) : fallback;
1921}
1922
1923static void text_console_set_echo(CharDriverState *chr, bool echo)
1924{
1925    QemuConsole *s = chr->opaque;
1926
1927    s->echo = echo;
1928}
1929
1930static void text_console_update_cursor_timer(void)
1931{
1932    timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1933              + CONSOLE_CURSOR_PERIOD / 2);
1934}
1935
1936static void text_console_update_cursor(void *opaque)
1937{
1938    QemuConsole *s;
1939    int i, count = 0;
1940
1941    cursor_visible_phase = !cursor_visible_phase;
1942
1943    for (i = 0; i < nb_consoles; i++) {
1944        s = consoles[i];
1945        if (qemu_console_is_graphic(s) ||
1946            !qemu_console_is_visible(s)) {
1947            continue;
1948        }
1949        count++;
1950        graphic_hw_invalidate(s);
1951    }
1952
1953    if (count) {
1954        text_console_update_cursor_timer();
1955    }
1956}
1957
1958static const GraphicHwOps text_console_ops = {
1959    .invalidate  = text_console_invalidate,
1960    .text_update = text_console_update,
1961};
1962
1963static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1964{
1965    QemuConsole *s;
1966    int g_width = 80 * FONT_WIDTH;
1967    int g_height = 24 * FONT_HEIGHT;
1968
1969    s = chr->opaque;
1970
1971    chr->chr_write = console_puts;
1972
1973    s->out_fifo.buf = s->out_fifo_buf;
1974    s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1975    s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1976    s->ds = ds;
1977
1978    s->y_displayed = 0;
1979    s->y_base = 0;
1980    s->total_height = DEFAULT_BACKSCROLL;
1981    s->x = 0;
1982    s->y = 0;
1983    if (!s->surface) {
1984        if (active_console && active_console->surface) {
1985            g_width = surface_width(active_console->surface);
1986            g_height = surface_height(active_console->surface);
1987        }
1988        s->surface = qemu_create_displaysurface(g_width, g_height);
1989    }
1990
1991    s->hw_ops = &text_console_ops;
1992    s->hw = s;
1993
1994    /* Set text attribute defaults */
1995    s->t_attrib_default.bold = 0;
1996    s->t_attrib_default.uline = 0;
1997    s->t_attrib_default.blink = 0;
1998    s->t_attrib_default.invers = 0;
1999    s->t_attrib_default.unvisible = 0;
2000    s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2001    s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2002    /* set current text attributes to default */
2003    s->t_attrib = s->t_attrib_default;
2004    text_console_resize(s);
2005
2006    if (chr->label) {
2007        char msg[128];
2008        int len;
2009
2010        s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2011        len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2012        console_puts(chr, (uint8_t*)msg, len);
2013        s->t_attrib = s->t_attrib_default;
2014    }
2015
2016    qemu_chr_be_generic_open(chr);
2017}
2018
2019static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2020{
2021    ChardevCommon *common = qapi_ChardevVC_base(vc);
2022    CharDriverState *chr;
2023    QemuConsole *s;
2024    unsigned width = 0;
2025    unsigned height = 0;
2026
2027    chr = qemu_chr_alloc(common, errp);
2028    if (!chr) {
2029        return NULL;
2030    }
2031
2032    if (vc->has_width) {
2033        width = vc->width;
2034    } else if (vc->has_cols) {
2035        width = vc->cols * FONT_WIDTH;
2036    }
2037
2038    if (vc->has_height) {
2039        height = vc->height;
2040    } else if (vc->has_rows) {
2041        height = vc->rows * FONT_HEIGHT;
2042    }
2043
2044    trace_console_txt_new(width, height);
2045    if (width == 0 || height == 0) {
2046        s = new_console(NULL, TEXT_CONSOLE, 0);
2047    } else {
2048        s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2049        s->surface = qemu_create_displaysurface(width, height);
2050    }
2051
2052    if (!s) {
2053        g_free(chr);
2054        error_setg(errp, "cannot create text console");
2055        return NULL;
2056    }
2057
2058    s->chr = chr;
2059    chr->opaque = s;
2060    chr->chr_set_echo = text_console_set_echo;
2061
2062    if (display_state) {
2063        text_console_do_init(chr, display_state);
2064    }
2065    return chr;
2066}
2067
2068static VcHandler *vc_handler = text_console_init;
2069
2070static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2071                                ChardevReturn *ret, bool *be_opened,
2072                                Error **errp)
2073{
2074    /* console/chardev init sometimes completes elsewhere in a 2nd
2075     * stage, so defer OPENED events until they are fully initialized
2076     */
2077    *be_opened = false;
2078    return vc_handler(backend->u.vc.data, errp);
2079}
2080
2081void register_vc_handler(VcHandler *handler)
2082{
2083    vc_handler = handler;
2084}
2085
2086void qemu_console_resize(QemuConsole *s, int width, int height)
2087{
2088    DisplaySurface *surface;
2089
2090    assert(s->console_type == GRAPHIC_CONSOLE);
2091
2092    if (s->surface &&
2093        pixman_image_get_width(s->surface->image) == width &&
2094        pixman_image_get_height(s->surface->image) == height) {
2095        return;
2096    }
2097
2098    surface = qemu_create_displaysurface(width, height);
2099    dpy_gfx_replace_surface(s, surface);
2100}
2101
2102DisplaySurface *qemu_console_surface(QemuConsole *console)
2103{
2104    return console->surface;
2105}
2106
2107PixelFormat qemu_default_pixelformat(int bpp)
2108{
2109    pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2110    PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2111    return pf;
2112}
2113
2114static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2115                              Error **errp)
2116{
2117    int val;
2118    ChardevVC *vc;
2119
2120    vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2121    qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2122
2123    val = qemu_opt_get_number(opts, "width", 0);
2124    if (val != 0) {
2125        vc->has_width = true;
2126        vc->width = val;
2127    }
2128
2129    val = qemu_opt_get_number(opts, "height", 0);
2130    if (val != 0) {
2131        vc->has_height = true;
2132        vc->height = val;
2133    }
2134
2135    val = qemu_opt_get_number(opts, "cols", 0);
2136    if (val != 0) {
2137        vc->has_cols = true;
2138        vc->cols = val;
2139    }
2140
2141    val = qemu_opt_get_number(opts, "rows", 0);
2142    if (val != 0) {
2143        vc->has_rows = true;
2144        vc->rows = val;
2145    }
2146}
2147
2148static const TypeInfo qemu_console_info = {
2149    .name = TYPE_QEMU_CONSOLE,
2150    .parent = TYPE_OBJECT,
2151    .instance_size = sizeof(QemuConsole),
2152    .class_size = sizeof(QemuConsoleClass),
2153};
2154
2155
2156static void register_types(void)
2157{
2158    type_register_static(&qemu_console_info);
2159    register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
2160                         vc_init);
2161}
2162
2163type_init(register_types);
2164