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