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