94cb0acdaef79deb33bfc7d675c881a61332560b
[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     con->ui_info = *info;
1464     if (!dpy_ui_info_supported(con)) {
1465         return -1;
1466     }
1467
1468     /*
1469      * Typically we get a flood of these as the user resizes the window.
1470      * Wait until the dust has settled (one second without updates), then
1471      * go notify the guest.
1472      */
1473     timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1474     return 0;
1475 }
1476
1477 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1478 {
1479     DisplayState *s = con->ds;
1480     DisplayChangeListener *dcl;
1481     int width = w;
1482     int height = h;
1483
1484     if (con->surface) {
1485         width = surface_width(con->surface);
1486         height = surface_height(con->surface);
1487     }
1488     x = MAX(x, 0);
1489     y = MAX(y, 0);
1490     x = MIN(x, width);
1491     y = MIN(y, height);
1492     w = MIN(w, width - x);
1493     h = MIN(h, height - y);
1494
1495     if (!qemu_console_is_visible(con)) {
1496         return;
1497     }
1498     QLIST_FOREACH(dcl, &s->listeners, next) {
1499         if (con != (dcl->con ? dcl->con : active_console)) {
1500             continue;
1501         }
1502         if (dcl->ops->dpy_gfx_update) {
1503             dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1504         }
1505     }
1506 }
1507
1508 void dpy_gfx_replace_surface(QemuConsole *con,
1509                              DisplaySurface *surface)
1510 {
1511     DisplayState *s = con->ds;
1512     DisplaySurface *old_surface = con->surface;
1513     DisplayChangeListener *dcl;
1514
1515     con->surface = surface;
1516     QLIST_FOREACH(dcl, &s->listeners, next) {
1517         if (con != (dcl->con ? dcl->con : active_console)) {
1518             continue;
1519         }
1520         if (dcl->ops->dpy_gfx_switch) {
1521             dcl->ops->dpy_gfx_switch(dcl, surface);
1522         }
1523     }
1524     qemu_free_displaysurface(old_surface);
1525 }
1526
1527 bool dpy_gfx_check_format(QemuConsole *con,
1528                           pixman_format_code_t format)
1529 {
1530     DisplayChangeListener *dcl;
1531     DisplayState *s = con->ds;
1532
1533     QLIST_FOREACH(dcl, &s->listeners, next) {
1534         if (dcl->con && dcl->con != con) {
1535             /* dcl bound to another console -> skip */
1536             continue;
1537         }
1538         if (dcl->ops->dpy_gfx_check_format) {
1539             if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1540                 return false;
1541             }
1542         } else {
1543             /* default is to whitelist native 32 bpp only */
1544             if (format != qemu_default_pixman_format(32, true)) {
1545                 return false;
1546             }
1547         }
1548     }
1549     return true;
1550 }
1551
1552 static void dpy_refresh(DisplayState *s)
1553 {
1554     DisplayChangeListener *dcl;
1555
1556     QLIST_FOREACH(dcl, &s->listeners, next) {
1557         if (dcl->ops->dpy_refresh) {
1558             dcl->ops->dpy_refresh(dcl);
1559         }
1560     }
1561 }
1562
1563 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1564                   int dst_x, int dst_y, int w, int h)
1565 {
1566     DisplayState *s = con->ds;
1567     DisplayChangeListener *dcl;
1568
1569     if (!qemu_console_is_visible(con)) {
1570         return;
1571     }
1572     QLIST_FOREACH(dcl, &s->listeners, next) {
1573         if (con != (dcl->con ? dcl->con : active_console)) {
1574             continue;
1575         }
1576         if (dcl->ops->dpy_gfx_copy) {
1577             dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1578         } else { /* TODO */
1579             dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1580         }
1581     }
1582 }
1583
1584 void dpy_text_cursor(QemuConsole *con, int x, int y)
1585 {
1586     DisplayState *s = con->ds;
1587     DisplayChangeListener *dcl;
1588
1589     if (!qemu_console_is_visible(con)) {
1590         return;
1591     }
1592     QLIST_FOREACH(dcl, &s->listeners, next) {
1593         if (con != (dcl->con ? dcl->con : active_console)) {
1594             continue;
1595         }
1596         if (dcl->ops->dpy_text_cursor) {
1597             dcl->ops->dpy_text_cursor(dcl, x, y);
1598         }
1599     }
1600 }
1601
1602 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1603 {
1604     DisplayState *s = con->ds;
1605     DisplayChangeListener *dcl;
1606
1607     if (!qemu_console_is_visible(con)) {
1608         return;
1609     }
1610     QLIST_FOREACH(dcl, &s->listeners, next) {
1611         if (con != (dcl->con ? dcl->con : active_console)) {
1612             continue;
1613         }
1614         if (dcl->ops->dpy_text_update) {
1615             dcl->ops->dpy_text_update(dcl, x, y, w, h);
1616         }
1617     }
1618 }
1619
1620 void dpy_text_resize(QemuConsole *con, int w, int h)
1621 {
1622     DisplayState *s = con->ds;
1623     DisplayChangeListener *dcl;
1624
1625     if (!qemu_console_is_visible(con)) {
1626         return;
1627     }
1628     QLIST_FOREACH(dcl, &s->listeners, next) {
1629         if (con != (dcl->con ? dcl->con : active_console)) {
1630             continue;
1631         }
1632         if (dcl->ops->dpy_text_resize) {
1633             dcl->ops->dpy_text_resize(dcl, w, h);
1634         }
1635     }
1636 }
1637
1638 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1639 {
1640     DisplayState *s = con->ds;
1641     DisplayChangeListener *dcl;
1642
1643     if (!qemu_console_is_visible(con)) {
1644         return;
1645     }
1646     QLIST_FOREACH(dcl, &s->listeners, next) {
1647         if (con != (dcl->con ? dcl->con : active_console)) {
1648             continue;
1649         }
1650         if (dcl->ops->dpy_mouse_set) {
1651             dcl->ops->dpy_mouse_set(dcl, x, y, on);
1652         }
1653     }
1654 }
1655
1656 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1657 {
1658     DisplayState *s = con->ds;
1659     DisplayChangeListener *dcl;
1660
1661     if (!qemu_console_is_visible(con)) {
1662         return;
1663     }
1664     QLIST_FOREACH(dcl, &s->listeners, next) {
1665         if (con != (dcl->con ? dcl->con : active_console)) {
1666             continue;
1667         }
1668         if (dcl->ops->dpy_cursor_define) {
1669             dcl->ops->dpy_cursor_define(dcl, cursor);
1670         }
1671     }
1672 }
1673
1674 bool dpy_cursor_define_supported(QemuConsole *con)
1675 {
1676     DisplayState *s = con->ds;
1677     DisplayChangeListener *dcl;
1678
1679     QLIST_FOREACH(dcl, &s->listeners, next) {
1680         if (dcl->ops->dpy_cursor_define) {
1681             return true;
1682         }
1683     }
1684     return false;
1685 }
1686
1687 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1688                                 struct QEMUGLParams *qparams)
1689 {
1690     assert(con->gl);
1691     return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1692 }
1693
1694 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1695 {
1696     assert(con->gl);
1697     con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1698 }
1699
1700 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1701 {
1702     assert(con->gl);
1703     return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1704 }
1705
1706 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1707 {
1708     assert(con->gl);
1709     return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1710 }
1711
1712 void dpy_gl_scanout(QemuConsole *con,
1713                     uint32_t backing_id, bool backing_y_0_top,
1714                     uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1715 {
1716     assert(con->gl);
1717     con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1718                                  backing_y_0_top,
1719                                  x, y, width, height);
1720 }
1721
1722 void dpy_gl_update(QemuConsole *con,
1723                    uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1724 {
1725     assert(con->gl);
1726     con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1727 }
1728
1729 /***********************************************************/
1730 /* register display */
1731
1732 /* console.c internal use only */
1733 static DisplayState *get_alloc_displaystate(void)
1734 {
1735     if (!display_state) {
1736         display_state = g_new0(DisplayState, 1);
1737         cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1738                                     text_console_update_cursor, NULL);
1739     }
1740     return display_state;
1741 }
1742
1743 /*
1744  * Called by main(), after creating QemuConsoles
1745  * and before initializing ui (sdl/vnc/...).
1746  */
1747 DisplayState *init_displaystate(void)
1748 {
1749     gchar *name;
1750     int i;
1751
1752     get_alloc_displaystate();
1753     for (i = 0; i < nb_consoles; i++) {
1754         if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1755             consoles[i]->ds == NULL) {
1756             text_console_do_init(consoles[i]->chr, display_state);
1757         }
1758
1759         /* Hook up into the qom tree here (not in new_console()), once
1760          * all QemuConsoles are created and the order / numbering
1761          * doesn't change any more */
1762         name = g_strdup_printf("console[%d]", i);
1763         object_property_add_child(container_get(object_get_root(), "/backend"),
1764                                   name, OBJECT(consoles[i]), &error_abort);
1765         g_free(name);
1766     }
1767
1768     return display_state;
1769 }
1770
1771 void graphic_console_set_hwops(QemuConsole *con,
1772                                const GraphicHwOps *hw_ops,
1773                                void *opaque)
1774 {
1775     con->hw_ops = hw_ops;
1776     con->hw = opaque;
1777 }
1778
1779 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1780                                   const GraphicHwOps *hw_ops,
1781                                   void *opaque)
1782 {
1783     static const char noinit[] =
1784         "Guest has not initialized the display (yet).";
1785     int width = 640;
1786     int height = 480;
1787 #ifdef CONFIG_MARU
1788     if (initial_resolution_width > 0 &&
1789             initial_resolution_height > 0) {
1790         width = initial_resolution_width;
1791         height = initial_resolution_height;
1792     }
1793 #endif
1794     QemuConsole *s;
1795     DisplayState *ds;
1796
1797     ds = get_alloc_displaystate();
1798     trace_console_gfx_new();
1799     s = new_console(ds, GRAPHIC_CONSOLE, head);
1800     s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1801     graphic_console_set_hwops(s, hw_ops, opaque);
1802     if (dev) {
1803         object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1804                                  &error_abort);
1805     }
1806
1807     s->surface = qemu_create_message_surface(width, height, noinit);
1808     return s;
1809 }
1810
1811 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1812 {
1813     if (index >= nb_consoles) {
1814         return NULL;
1815     }
1816     return consoles[index];
1817 }
1818
1819 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1820 {
1821     Object *obj;
1822     uint32_t h;
1823     int i;
1824
1825     for (i = 0; i < nb_consoles; i++) {
1826         if (!consoles[i]) {
1827             continue;
1828         }
1829         obj = object_property_get_link(OBJECT(consoles[i]),
1830                                        "device", &error_abort);
1831         if (DEVICE(obj) != dev) {
1832             continue;
1833         }
1834         h = object_property_get_int(OBJECT(consoles[i]),
1835                                     "head", &error_abort);
1836         if (h != head) {
1837             continue;
1838         }
1839         return consoles[i];
1840     }
1841     return NULL;
1842 }
1843
1844 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1845                                                 uint32_t head, Error **errp)
1846 {
1847     DeviceState *dev;
1848     QemuConsole *con;
1849
1850     dev = qdev_find_recursive(sysbus_get_default(), device_id);
1851     if (dev == NULL) {
1852         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1853                   "Device '%s' not found", device_id);
1854         return NULL;
1855     }
1856
1857     con = qemu_console_lookup_by_device(dev, head);
1858     if (con == NULL) {
1859         error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1860                    device_id, head);
1861         return NULL;
1862     }
1863
1864     return con;
1865 }
1866
1867 bool qemu_console_is_visible(QemuConsole *con)
1868 {
1869     return (con == active_console) || (con->dcls > 0);
1870 }
1871
1872 bool qemu_console_is_graphic(QemuConsole *con)
1873 {
1874     if (con == NULL) {
1875         con = active_console;
1876     }
1877     return con && (con->console_type == GRAPHIC_CONSOLE);
1878 }
1879
1880 bool qemu_console_is_fixedsize(QemuConsole *con)
1881 {
1882     if (con == NULL) {
1883         con = active_console;
1884     }
1885     return con && (con->console_type != TEXT_CONSOLE);
1886 }
1887
1888 char *qemu_console_get_label(QemuConsole *con)
1889 {
1890     if (con->console_type == GRAPHIC_CONSOLE) {
1891         if (con->device) {
1892             return g_strdup(object_get_typename(con->device));
1893         }
1894         return g_strdup("VGA");
1895     } else {
1896         if (con->chr && con->chr->label) {
1897             return g_strdup(con->chr->label);
1898         }
1899         return g_strdup_printf("vc%d", con->index);
1900     }
1901 }
1902
1903 int qemu_console_get_index(QemuConsole *con)
1904 {
1905     if (con == NULL) {
1906         con = active_console;
1907     }
1908     return con ? con->index : -1;
1909 }
1910
1911 uint32_t qemu_console_get_head(QemuConsole *con)
1912 {
1913     if (con == NULL) {
1914         con = active_console;
1915     }
1916     return con ? con->head : -1;
1917 }
1918
1919 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1920 {
1921     assert(con != NULL);
1922     return &con->ui_info;
1923 }
1924
1925 int qemu_console_get_width(QemuConsole *con, int fallback)
1926 {
1927     if (con == NULL) {
1928         con = active_console;
1929     }
1930     return con ? surface_width(con->surface) : fallback;
1931 }
1932
1933 int qemu_console_get_height(QemuConsole *con, int fallback)
1934 {
1935     if (con == NULL) {
1936         con = active_console;
1937     }
1938     return con ? surface_height(con->surface) : fallback;
1939 }
1940
1941 static void text_console_set_echo(CharDriverState *chr, bool echo)
1942 {
1943     QemuConsole *s = chr->opaque;
1944
1945     s->echo = echo;
1946 }
1947
1948 static void text_console_update_cursor_timer(void)
1949 {
1950     timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1951               + CONSOLE_CURSOR_PERIOD / 2);
1952 }
1953
1954 static void text_console_update_cursor(void *opaque)
1955 {
1956     QemuConsole *s;
1957     int i, count = 0;
1958
1959     cursor_visible_phase = !cursor_visible_phase;
1960
1961     for (i = 0; i < nb_consoles; i++) {
1962         s = consoles[i];
1963         if (qemu_console_is_graphic(s) ||
1964             !qemu_console_is_visible(s)) {
1965             continue;
1966         }
1967         count++;
1968         graphic_hw_invalidate(s);
1969     }
1970
1971     if (count) {
1972         text_console_update_cursor_timer();
1973     }
1974 }
1975
1976 static const GraphicHwOps text_console_ops = {
1977     .invalidate  = text_console_invalidate,
1978     .text_update = text_console_update,
1979 };
1980
1981 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1982 {
1983     QemuConsole *s;
1984     int g_width = 80 * FONT_WIDTH;
1985     int g_height = 24 * FONT_HEIGHT;
1986
1987     s = chr->opaque;
1988
1989     chr->chr_write = console_puts;
1990
1991     s->out_fifo.buf = s->out_fifo_buf;
1992     s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1993     s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1994     s->ds = ds;
1995
1996     s->y_displayed = 0;
1997     s->y_base = 0;
1998     s->total_height = DEFAULT_BACKSCROLL;
1999     s->x = 0;
2000     s->y = 0;
2001     if (!s->surface) {
2002         if (active_console && active_console->surface) {
2003             g_width = surface_width(active_console->surface);
2004             g_height = surface_height(active_console->surface);
2005         }
2006         s->surface = qemu_create_displaysurface(g_width, g_height);
2007     }
2008
2009     s->hw_ops = &text_console_ops;
2010     s->hw = s;
2011
2012     /* Set text attribute defaults */
2013     s->t_attrib_default.bold = 0;
2014     s->t_attrib_default.uline = 0;
2015     s->t_attrib_default.blink = 0;
2016     s->t_attrib_default.invers = 0;
2017     s->t_attrib_default.unvisible = 0;
2018     s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2019     s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2020     /* set current text attributes to default */
2021     s->t_attrib = s->t_attrib_default;
2022     text_console_resize(s);
2023
2024     if (chr->label) {
2025         char msg[128];
2026         int len;
2027
2028         s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2029         len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2030         console_puts(chr, (uint8_t*)msg, len);
2031         s->t_attrib = s->t_attrib_default;
2032     }
2033
2034     qemu_chr_be_generic_open(chr);
2035     if (chr->init)
2036         chr->init(chr);
2037 }
2038
2039 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2040 {
2041     ChardevCommon *common = qapi_ChardevVC_base(vc);
2042     CharDriverState *chr;
2043     QemuConsole *s;
2044     unsigned width = 0;
2045     unsigned height = 0;
2046
2047     chr = qemu_chr_alloc(common, errp);
2048     if (!chr) {
2049         return NULL;
2050     }
2051
2052     if (vc->has_width) {
2053         width = vc->width;
2054     } else if (vc->has_cols) {
2055         width = vc->cols * FONT_WIDTH;
2056     }
2057
2058     if (vc->has_height) {
2059         height = vc->height;
2060     } else if (vc->has_rows) {
2061         height = vc->rows * FONT_HEIGHT;
2062     }
2063
2064     trace_console_txt_new(width, height);
2065     if (width == 0 || height == 0) {
2066         s = new_console(NULL, TEXT_CONSOLE, 0);
2067     } else {
2068         s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2069         s->surface = qemu_create_displaysurface(width, height);
2070     }
2071
2072     if (!s) {
2073         g_free(chr);
2074         error_setg(errp, "cannot create text console");
2075         return NULL;
2076     }
2077
2078     s->chr = chr;
2079     chr->opaque = s;
2080     chr->chr_set_echo = text_console_set_echo;
2081     /* console/chardev init sometimes completes elsewhere in a 2nd
2082      * stage, so defer OPENED events until they are fully initialized
2083      */
2084     chr->explicit_be_open = true;
2085
2086     if (display_state) {
2087         text_console_do_init(chr, display_state);
2088     }
2089     return chr;
2090 }
2091
2092 static VcHandler *vc_handler = text_console_init;
2093
2094 static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2095                                 ChardevReturn *ret, Error **errp)
2096 {
2097     return vc_handler(backend->u.vc.data, errp);
2098 }
2099
2100 void register_vc_handler(VcHandler *handler)
2101 {
2102     vc_handler = handler;
2103 }
2104
2105 void qemu_console_resize(QemuConsole *s, int width, int height)
2106 {
2107     DisplaySurface *surface;
2108
2109     assert(s->console_type == GRAPHIC_CONSOLE);
2110     surface = qemu_create_displaysurface(width, height);
2111     dpy_gfx_replace_surface(s, surface);
2112 }
2113
2114 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
2115                        int dst_x, int dst_y, int w, int h)
2116 {
2117     assert(con->console_type == GRAPHIC_CONSOLE);
2118     dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
2119 }
2120
2121 DisplaySurface *qemu_console_surface(QemuConsole *console)
2122 {
2123     return console->surface;
2124 }
2125
2126 PixelFormat qemu_default_pixelformat(int bpp)
2127 {
2128     pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2129     PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2130     return pf;
2131 }
2132
2133 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2134                               Error **errp)
2135 {
2136     int val;
2137     ChardevVC *vc;
2138
2139     vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2140     qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2141
2142     val = qemu_opt_get_number(opts, "width", 0);
2143     if (val != 0) {
2144         vc->has_width = true;
2145         vc->width = val;
2146     }
2147
2148     val = qemu_opt_get_number(opts, "height", 0);
2149     if (val != 0) {
2150         vc->has_height = true;
2151         vc->height = val;
2152     }
2153
2154     val = qemu_opt_get_number(opts, "cols", 0);
2155     if (val != 0) {
2156         vc->has_cols = true;
2157         vc->cols = val;
2158     }
2159
2160     val = qemu_opt_get_number(opts, "rows", 0);
2161     if (val != 0) {
2162         vc->has_rows = true;
2163         vc->rows = val;
2164     }
2165 }
2166
2167 static const TypeInfo qemu_console_info = {
2168     .name = TYPE_QEMU_CONSOLE,
2169     .parent = TYPE_OBJECT,
2170     .instance_size = sizeof(QemuConsole),
2171     .class_size = sizeof(QemuConsoleClass),
2172 };
2173
2174
2175 static void register_types(void)
2176 {
2177     type_register_static(&qemu_console_info);
2178     register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
2179                          vc_init);
2180 }
2181
2182 type_init(register_types);