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