]> git.proxmox.com Git - mirror_qemu.git/blob - ui/console.c
char: move callbacks in CharDriver
[mirror_qemu.git] / ui / console.c
1 /*
2 * QEMU graphical console
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qemu/timer.h"
29 #include "qmp-commands.h"
30 #include "sysemu/char.h"
31 #include "trace.h"
32 #include "exec/memory.h"
33
34 #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 CharDriverState *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(CharDriverState *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 == ';')
884 break;
885 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
886 ch, s->nb_esc_params);
887 s->state = TTY_STATE_NORM;
888 switch(ch) {
889 case 'A':
890 /* move cursor up */
891 if (s->esc_params[0] == 0) {
892 s->esc_params[0] = 1;
893 }
894 set_cursor(s, s->x, s->y - s->esc_params[0]);
895 break;
896 case 'B':
897 /* move cursor down */
898 if (s->esc_params[0] == 0) {
899 s->esc_params[0] = 1;
900 }
901 set_cursor(s, s->x, s->y + s->esc_params[0]);
902 break;
903 case 'C':
904 /* move cursor right */
905 if (s->esc_params[0] == 0) {
906 s->esc_params[0] = 1;
907 }
908 set_cursor(s, s->x + s->esc_params[0], s->y);
909 break;
910 case 'D':
911 /* move cursor left */
912 if (s->esc_params[0] == 0) {
913 s->esc_params[0] = 1;
914 }
915 set_cursor(s, s->x - s->esc_params[0], s->y);
916 break;
917 case 'G':
918 /* move cursor to column */
919 set_cursor(s, s->esc_params[0] - 1, s->y);
920 break;
921 case 'f':
922 case 'H':
923 /* move cursor to row, column */
924 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
925 break;
926 case 'J':
927 switch (s->esc_params[0]) {
928 case 0:
929 /* clear to end of screen */
930 for (y = s->y; y < s->height; y++) {
931 for (x = 0; x < s->width; x++) {
932 if (y == s->y && x < s->x) {
933 continue;
934 }
935 console_clear_xy(s, x, y);
936 }
937 }
938 break;
939 case 1:
940 /* clear from beginning of screen */
941 for (y = 0; y <= s->y; y++) {
942 for (x = 0; x < s->width; x++) {
943 if (y == s->y && x > s->x) {
944 break;
945 }
946 console_clear_xy(s, x, y);
947 }
948 }
949 break;
950 case 2:
951 /* clear entire screen */
952 for (y = 0; y <= s->height; y++) {
953 for (x = 0; x < s->width; x++) {
954 console_clear_xy(s, x, y);
955 }
956 }
957 break;
958 }
959 break;
960 case 'K':
961 switch (s->esc_params[0]) {
962 case 0:
963 /* clear to eol */
964 for(x = s->x; x < s->width; x++) {
965 console_clear_xy(s, x, s->y);
966 }
967 break;
968 case 1:
969 /* clear from beginning of line */
970 for (x = 0; x <= s->x; x++) {
971 console_clear_xy(s, x, s->y);
972 }
973 break;
974 case 2:
975 /* clear entire line */
976 for(x = 0; x < s->width; x++) {
977 console_clear_xy(s, x, s->y);
978 }
979 break;
980 }
981 break;
982 case 'm':
983 console_handle_escape(s);
984 break;
985 case 'n':
986 switch (s->esc_params[0]) {
987 case 5:
988 /* report console status (always succeed)*/
989 console_respond_str(s, "\033[0n");
990 break;
991 case 6:
992 /* report cursor position */
993 sprintf(response, "\033[%d;%dR",
994 (s->y_base + s->y) % s->total_height + 1,
995 s->x + 1);
996 console_respond_str(s, response);
997 break;
998 }
999 break;
1000 case 's':
1001 /* save cursor position */
1002 s->x_saved = s->x;
1003 s->y_saved = s->y;
1004 break;
1005 case 'u':
1006 /* restore cursor position */
1007 s->x = s->x_saved;
1008 s->y = s->y_saved;
1009 break;
1010 default:
1011 trace_console_putchar_unhandled(ch);
1012 break;
1013 }
1014 break;
1015 }
1016 }
1017 }
1018
1019 void console_select(unsigned int index)
1020 {
1021 DisplayChangeListener *dcl;
1022 QemuConsole *s;
1023
1024 trace_console_select(index);
1025 s = qemu_console_lookup_by_index(index);
1026 if (s) {
1027 DisplayState *ds = s->ds;
1028
1029 active_console = s;
1030 if (ds->have_gfx) {
1031 QLIST_FOREACH(dcl, &ds->listeners, next) {
1032 if (dcl->con != NULL) {
1033 continue;
1034 }
1035 if (dcl->ops->dpy_gfx_switch) {
1036 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1037 }
1038 }
1039 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1040 surface_height(s->surface));
1041 }
1042 if (ds->have_text) {
1043 dpy_text_resize(s, s->width, s->height);
1044 }
1045 text_console_update_cursor(NULL);
1046 }
1047 }
1048
1049 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1050 {
1051 QemuConsole *s = chr->opaque;
1052 int i;
1053
1054 if (!s->ds) {
1055 return 0;
1056 }
1057
1058 s->update_x0 = s->width * FONT_WIDTH;
1059 s->update_y0 = s->height * FONT_HEIGHT;
1060 s->update_x1 = 0;
1061 s->update_y1 = 0;
1062 console_show_cursor(s, 0);
1063 for(i = 0; i < len; i++) {
1064 console_putchar(s, buf[i]);
1065 }
1066 console_show_cursor(s, 1);
1067 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1068 dpy_gfx_update(s, s->update_x0, s->update_y0,
1069 s->update_x1 - s->update_x0,
1070 s->update_y1 - s->update_y0);
1071 }
1072 return len;
1073 }
1074
1075 static void kbd_send_chars(void *opaque)
1076 {
1077 QemuConsole *s = opaque;
1078 int len;
1079 uint8_t buf[16];
1080
1081 len = qemu_chr_be_can_write(s->chr);
1082 if (len > s->out_fifo.count)
1083 len = s->out_fifo.count;
1084 if (len > 0) {
1085 if (len > sizeof(buf))
1086 len = sizeof(buf);
1087 qemu_fifo_read(&s->out_fifo, buf, len);
1088 qemu_chr_be_write(s->chr, buf, len);
1089 }
1090 /* characters are pending: we send them a bit later (XXX:
1091 horrible, should change char device API) */
1092 if (s->out_fifo.count > 0) {
1093 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1094 }
1095 }
1096
1097 /* called when an ascii key is pressed */
1098 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1099 {
1100 uint8_t buf[16], *q;
1101 CharBackend *be;
1102 int c;
1103
1104 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1105 return;
1106
1107 switch(keysym) {
1108 case QEMU_KEY_CTRL_UP:
1109 console_scroll(s, -1);
1110 break;
1111 case QEMU_KEY_CTRL_DOWN:
1112 console_scroll(s, 1);
1113 break;
1114 case QEMU_KEY_CTRL_PAGEUP:
1115 console_scroll(s, -10);
1116 break;
1117 case QEMU_KEY_CTRL_PAGEDOWN:
1118 console_scroll(s, 10);
1119 break;
1120 default:
1121 /* convert the QEMU keysym to VT100 key string */
1122 q = buf;
1123 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1124 *q++ = '\033';
1125 *q++ = '[';
1126 c = keysym - 0xe100;
1127 if (c >= 10)
1128 *q++ = '0' + (c / 10);
1129 *q++ = '0' + (c % 10);
1130 *q++ = '~';
1131 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1132 *q++ = '\033';
1133 *q++ = '[';
1134 *q++ = keysym & 0xff;
1135 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1136 console_puts(s->chr, (const uint8_t *) "\r", 1);
1137 *q++ = '\n';
1138 } else {
1139 *q++ = keysym;
1140 }
1141 if (s->echo) {
1142 console_puts(s->chr, buf, q - buf);
1143 }
1144 be = s->chr->be;
1145 if (be && be->chr_read) {
1146 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1147 kbd_send_chars(s);
1148 }
1149 break;
1150 }
1151 }
1152
1153 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1154 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1155 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1156 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1157 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1158 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1159 [Q_KEY_CODE_END] = QEMU_KEY_END,
1160 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1161 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1162 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1163 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1164 };
1165
1166 bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1167 {
1168 int keysym;
1169
1170 keysym = qcode_to_keysym[qcode];
1171 if (keysym == 0) {
1172 return false;
1173 }
1174 kbd_put_keysym_console(s, keysym);
1175 return true;
1176 }
1177
1178 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1179 {
1180 int i;
1181
1182 for (i = 0; i < len && str[i]; i++) {
1183 kbd_put_keysym_console(s, str[i]);
1184 }
1185 }
1186
1187 void kbd_put_keysym(int keysym)
1188 {
1189 kbd_put_keysym_console(active_console, keysym);
1190 }
1191
1192 static void text_console_invalidate(void *opaque)
1193 {
1194 QemuConsole *s = (QemuConsole *) opaque;
1195
1196 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1197 text_console_resize(s);
1198 }
1199 console_refresh(s);
1200 }
1201
1202 static void text_console_update(void *opaque, console_ch_t *chardata)
1203 {
1204 QemuConsole *s = (QemuConsole *) opaque;
1205 int i, j, src;
1206
1207 if (s->text_x[0] <= s->text_x[1]) {
1208 src = (s->y_base + s->text_y[0]) * s->width;
1209 chardata += s->text_y[0] * s->width;
1210 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1211 for (j = 0; j < s->width; j++, src++) {
1212 console_write_ch(chardata ++,
1213 ATTR2CHTYPE(s->cells[src].ch,
1214 s->cells[src].t_attrib.fgcol,
1215 s->cells[src].t_attrib.bgcol,
1216 s->cells[src].t_attrib.bold));
1217 }
1218 dpy_text_update(s, s->text_x[0], s->text_y[0],
1219 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1220 s->text_x[0] = s->width;
1221 s->text_y[0] = s->height;
1222 s->text_x[1] = 0;
1223 s->text_y[1] = 0;
1224 }
1225 if (s->cursor_invalidate) {
1226 dpy_text_cursor(s, s->x, s->y);
1227 s->cursor_invalidate = 0;
1228 }
1229 }
1230
1231 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1232 uint32_t head)
1233 {
1234 Object *obj;
1235 QemuConsole *s;
1236 int i;
1237
1238 obj = object_new(TYPE_QEMU_CONSOLE);
1239 s = QEMU_CONSOLE(obj);
1240 s->head = head;
1241 object_property_add_link(obj, "device", TYPE_DEVICE,
1242 (Object **)&s->device,
1243 object_property_allow_set_link,
1244 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1245 &error_abort);
1246 object_property_add_uint32_ptr(obj, "head",
1247 &s->head, &error_abort);
1248
1249 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1250 (console_type == GRAPHIC_CONSOLE))) {
1251 active_console = s;
1252 }
1253 s->ds = ds;
1254 s->console_type = console_type;
1255
1256 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1257 if (console_type != GRAPHIC_CONSOLE) {
1258 s->index = nb_consoles;
1259 consoles[nb_consoles++] = s;
1260 } else {
1261 /* HACK: Put graphical consoles before text consoles. */
1262 for (i = nb_consoles; i > 0; i--) {
1263 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1264 break;
1265 consoles[i] = consoles[i - 1];
1266 consoles[i]->index = i;
1267 }
1268 s->index = i;
1269 consoles[i] = s;
1270 nb_consoles++;
1271 }
1272 return s;
1273 }
1274
1275 static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1276 {
1277 qemu_pixman_image_unref(surface->image);
1278 surface->image = NULL;
1279
1280 surface->format = PIXMAN_x8r8g8b8;
1281 surface->image = pixman_image_create_bits(surface->format,
1282 width, height,
1283 NULL, width * 4);
1284 assert(surface->image != NULL);
1285
1286 surface->flags = QEMU_ALLOCATED_FLAG;
1287 }
1288
1289 DisplaySurface *qemu_create_displaysurface(int width, int height)
1290 {
1291 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1292
1293 trace_displaysurface_create(surface, width, height);
1294 qemu_alloc_display(surface, width, height);
1295 return surface;
1296 }
1297
1298 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1299 pixman_format_code_t format,
1300 int linesize, uint8_t *data)
1301 {
1302 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1303
1304 trace_displaysurface_create_from(surface, width, height, format);
1305 surface->format = format;
1306 surface->image = pixman_image_create_bits(surface->format,
1307 width, height,
1308 (void *)data, linesize);
1309 assert(surface->image != NULL);
1310
1311 return surface;
1312 }
1313
1314 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1315 {
1316 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1317
1318 trace_displaysurface_create_pixman(surface);
1319 surface->format = pixman_image_get_format(image);
1320 surface->image = pixman_image_ref(image);
1321
1322 return surface;
1323 }
1324
1325 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1326 void *unused)
1327 {
1328 void *data = pixman_image_get_data(image);
1329 uint32_t size = pixman_image_get_stride(image) *
1330 pixman_image_get_height(image);
1331 cpu_physical_memory_unmap(data, size, 0, 0);
1332 }
1333
1334 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1335 pixman_format_code_t format,
1336 int linesize, uint64_t addr)
1337 {
1338 DisplaySurface *surface;
1339 hwaddr size;
1340 void *data;
1341
1342 if (linesize == 0) {
1343 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1344 }
1345
1346 size = (hwaddr)linesize * height;
1347 data = cpu_physical_memory_map(addr, &size, 0);
1348 if (size != (hwaddr)linesize * height) {
1349 cpu_physical_memory_unmap(data, size, 0, 0);
1350 return NULL;
1351 }
1352
1353 surface = qemu_create_displaysurface_from
1354 (width, height, format, linesize, data);
1355 pixman_image_set_destroy_function
1356 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1357
1358 return surface;
1359 }
1360
1361 static DisplaySurface *qemu_create_message_surface(int w, int h,
1362 const char *msg)
1363 {
1364 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1365 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1366 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1367 pixman_image_t *glyph;
1368 int len, x, y, i;
1369
1370 len = strlen(msg);
1371 x = (w / FONT_WIDTH - len) / 2;
1372 y = (h / FONT_HEIGHT - 1) / 2;
1373 for (i = 0; i < len; i++) {
1374 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1375 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1376 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1377 qemu_pixman_image_unref(glyph);
1378 }
1379 return surface;
1380 }
1381
1382 void qemu_free_displaysurface(DisplaySurface *surface)
1383 {
1384 if (surface == NULL) {
1385 return;
1386 }
1387 trace_displaysurface_free(surface);
1388 qemu_pixman_image_unref(surface->image);
1389 g_free(surface);
1390 }
1391
1392 bool console_has_gl(QemuConsole *con)
1393 {
1394 return con->gl != NULL;
1395 }
1396
1397 void register_displaychangelistener(DisplayChangeListener *dcl)
1398 {
1399 static const char nodev[] =
1400 "This VM has no graphic display device.";
1401 static DisplaySurface *dummy;
1402 QemuConsole *con;
1403
1404 if (dcl->ops->dpy_gl_ctx_create) {
1405 /* display has opengl support */
1406 assert(dcl->con);
1407 if (dcl->con->gl) {
1408 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1409 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1410 exit(1);
1411 }
1412 dcl->con->gl = dcl;
1413 }
1414
1415 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1416 dcl->ds = get_alloc_displaystate();
1417 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1418 gui_setup_refresh(dcl->ds);
1419 if (dcl->con) {
1420 dcl->con->dcls++;
1421 con = dcl->con;
1422 } else {
1423 con = active_console;
1424 }
1425 if (dcl->ops->dpy_gfx_switch) {
1426 if (con) {
1427 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1428 } else {
1429 if (!dummy) {
1430 dummy = qemu_create_message_surface(640, 480, nodev);
1431 }
1432 dcl->ops->dpy_gfx_switch(dcl, dummy);
1433 }
1434 }
1435 text_console_update_cursor(NULL);
1436 }
1437
1438 void update_displaychangelistener(DisplayChangeListener *dcl,
1439 uint64_t interval)
1440 {
1441 DisplayState *ds = dcl->ds;
1442
1443 dcl->update_interval = interval;
1444 if (!ds->refreshing && ds->update_interval > interval) {
1445 timer_mod(ds->gui_timer, ds->last_update + interval);
1446 }
1447 }
1448
1449 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1450 {
1451 DisplayState *ds = dcl->ds;
1452 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1453 if (dcl->con) {
1454 dcl->con->dcls--;
1455 }
1456 QLIST_REMOVE(dcl, next);
1457 gui_setup_refresh(ds);
1458 }
1459
1460 static void dpy_set_ui_info_timer(void *opaque)
1461 {
1462 QemuConsole *con = opaque;
1463
1464 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1465 }
1466
1467 bool dpy_ui_info_supported(QemuConsole *con)
1468 {
1469 return con->hw_ops->ui_info != NULL;
1470 }
1471
1472 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1473 {
1474 assert(con != NULL);
1475
1476 if (!dpy_ui_info_supported(con)) {
1477 return -1;
1478 }
1479 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1480 /* nothing changed -- ignore */
1481 return 0;
1482 }
1483
1484 /*
1485 * Typically we get a flood of these as the user resizes the window.
1486 * Wait until the dust has settled (one second without updates), then
1487 * go notify the guest.
1488 */
1489 con->ui_info = *info;
1490 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1491 return 0;
1492 }
1493
1494 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1495 {
1496 DisplayState *s = con->ds;
1497 DisplayChangeListener *dcl;
1498 int width = w;
1499 int height = h;
1500
1501 if (con->surface) {
1502 width = surface_width(con->surface);
1503 height = surface_height(con->surface);
1504 }
1505 x = MAX(x, 0);
1506 y = MAX(y, 0);
1507 x = MIN(x, width);
1508 y = MIN(y, height);
1509 w = MIN(w, width - x);
1510 h = MIN(h, height - y);
1511
1512 if (!qemu_console_is_visible(con)) {
1513 return;
1514 }
1515 QLIST_FOREACH(dcl, &s->listeners, next) {
1516 if (con != (dcl->con ? dcl->con : active_console)) {
1517 continue;
1518 }
1519 if (dcl->ops->dpy_gfx_update) {
1520 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1521 }
1522 }
1523 }
1524
1525 void dpy_gfx_replace_surface(QemuConsole *con,
1526 DisplaySurface *surface)
1527 {
1528 DisplayState *s = con->ds;
1529 DisplaySurface *old_surface = con->surface;
1530 DisplayChangeListener *dcl;
1531
1532 con->surface = surface;
1533 QLIST_FOREACH(dcl, &s->listeners, next) {
1534 if (con != (dcl->con ? dcl->con : active_console)) {
1535 continue;
1536 }
1537 if (dcl->ops->dpy_gfx_switch) {
1538 dcl->ops->dpy_gfx_switch(dcl, surface);
1539 }
1540 }
1541 qemu_free_displaysurface(old_surface);
1542 }
1543
1544 bool dpy_gfx_check_format(QemuConsole *con,
1545 pixman_format_code_t format)
1546 {
1547 DisplayChangeListener *dcl;
1548 DisplayState *s = con->ds;
1549
1550 QLIST_FOREACH(dcl, &s->listeners, next) {
1551 if (dcl->con && dcl->con != con) {
1552 /* dcl bound to another console -> skip */
1553 continue;
1554 }
1555 if (dcl->ops->dpy_gfx_check_format) {
1556 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1557 return false;
1558 }
1559 } else {
1560 /* default is to whitelist native 32 bpp only */
1561 if (format != qemu_default_pixman_format(32, true)) {
1562 return false;
1563 }
1564 }
1565 }
1566 return true;
1567 }
1568
1569 static void dpy_refresh(DisplayState *s)
1570 {
1571 DisplayChangeListener *dcl;
1572
1573 QLIST_FOREACH(dcl, &s->listeners, next) {
1574 if (dcl->ops->dpy_refresh) {
1575 dcl->ops->dpy_refresh(dcl);
1576 }
1577 }
1578 }
1579
1580 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1581 int dst_x, int dst_y, int w, int h)
1582 {
1583 DisplayState *s = con->ds;
1584 DisplayChangeListener *dcl;
1585
1586 if (!qemu_console_is_visible(con)) {
1587 return;
1588 }
1589 QLIST_FOREACH(dcl, &s->listeners, next) {
1590 if (con != (dcl->con ? dcl->con : active_console)) {
1591 continue;
1592 }
1593 if (dcl->ops->dpy_gfx_copy) {
1594 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1595 } else { /* TODO */
1596 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1597 }
1598 }
1599 }
1600
1601 void dpy_text_cursor(QemuConsole *con, int x, int y)
1602 {
1603 DisplayState *s = con->ds;
1604 DisplayChangeListener *dcl;
1605
1606 if (!qemu_console_is_visible(con)) {
1607 return;
1608 }
1609 QLIST_FOREACH(dcl, &s->listeners, next) {
1610 if (con != (dcl->con ? dcl->con : active_console)) {
1611 continue;
1612 }
1613 if (dcl->ops->dpy_text_cursor) {
1614 dcl->ops->dpy_text_cursor(dcl, x, y);
1615 }
1616 }
1617 }
1618
1619 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1620 {
1621 DisplayState *s = con->ds;
1622 DisplayChangeListener *dcl;
1623
1624 if (!qemu_console_is_visible(con)) {
1625 return;
1626 }
1627 QLIST_FOREACH(dcl, &s->listeners, next) {
1628 if (con != (dcl->con ? dcl->con : active_console)) {
1629 continue;
1630 }
1631 if (dcl->ops->dpy_text_update) {
1632 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1633 }
1634 }
1635 }
1636
1637 void dpy_text_resize(QemuConsole *con, int w, int h)
1638 {
1639 DisplayState *s = con->ds;
1640 DisplayChangeListener *dcl;
1641
1642 if (!qemu_console_is_visible(con)) {
1643 return;
1644 }
1645 QLIST_FOREACH(dcl, &s->listeners, next) {
1646 if (con != (dcl->con ? dcl->con : active_console)) {
1647 continue;
1648 }
1649 if (dcl->ops->dpy_text_resize) {
1650 dcl->ops->dpy_text_resize(dcl, w, h);
1651 }
1652 }
1653 }
1654
1655 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1656 {
1657 DisplayState *s = con->ds;
1658 DisplayChangeListener *dcl;
1659
1660 if (!qemu_console_is_visible(con)) {
1661 return;
1662 }
1663 QLIST_FOREACH(dcl, &s->listeners, next) {
1664 if (con != (dcl->con ? dcl->con : active_console)) {
1665 continue;
1666 }
1667 if (dcl->ops->dpy_mouse_set) {
1668 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1669 }
1670 }
1671 }
1672
1673 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1674 {
1675 DisplayState *s = con->ds;
1676 DisplayChangeListener *dcl;
1677
1678 if (!qemu_console_is_visible(con)) {
1679 return;
1680 }
1681 QLIST_FOREACH(dcl, &s->listeners, next) {
1682 if (con != (dcl->con ? dcl->con : active_console)) {
1683 continue;
1684 }
1685 if (dcl->ops->dpy_cursor_define) {
1686 dcl->ops->dpy_cursor_define(dcl, cursor);
1687 }
1688 }
1689 }
1690
1691 bool dpy_cursor_define_supported(QemuConsole *con)
1692 {
1693 DisplayState *s = con->ds;
1694 DisplayChangeListener *dcl;
1695
1696 QLIST_FOREACH(dcl, &s->listeners, next) {
1697 if (dcl->ops->dpy_cursor_define) {
1698 return true;
1699 }
1700 }
1701 return false;
1702 }
1703
1704 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1705 struct QEMUGLParams *qparams)
1706 {
1707 assert(con->gl);
1708 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1709 }
1710
1711 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1712 {
1713 assert(con->gl);
1714 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1715 }
1716
1717 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1718 {
1719 assert(con->gl);
1720 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1721 }
1722
1723 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1724 {
1725 assert(con->gl);
1726 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1727 }
1728
1729 void dpy_gl_scanout(QemuConsole *con,
1730 uint32_t backing_id, bool backing_y_0_top,
1731 uint32_t backing_width, uint32_t backing_height,
1732 uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1733 {
1734 assert(con->gl);
1735 con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1736 backing_y_0_top,
1737 backing_width, backing_height,
1738 x, y, width, height);
1739 }
1740
1741 void dpy_gl_update(QemuConsole *con,
1742 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1743 {
1744 assert(con->gl);
1745 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1746 }
1747
1748 /***********************************************************/
1749 /* register display */
1750
1751 /* console.c internal use only */
1752 static DisplayState *get_alloc_displaystate(void)
1753 {
1754 if (!display_state) {
1755 display_state = g_new0(DisplayState, 1);
1756 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1757 text_console_update_cursor, NULL);
1758 }
1759 return display_state;
1760 }
1761
1762 /*
1763 * Called by main(), after creating QemuConsoles
1764 * and before initializing ui (sdl/vnc/...).
1765 */
1766 DisplayState *init_displaystate(void)
1767 {
1768 gchar *name;
1769 int i;
1770
1771 get_alloc_displaystate();
1772 for (i = 0; i < nb_consoles; i++) {
1773 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1774 consoles[i]->ds == NULL) {
1775 text_console_do_init(consoles[i]->chr, display_state);
1776 }
1777
1778 /* Hook up into the qom tree here (not in new_console()), once
1779 * all QemuConsoles are created and the order / numbering
1780 * doesn't change any more */
1781 name = g_strdup_printf("console[%d]", i);
1782 object_property_add_child(container_get(object_get_root(), "/backend"),
1783 name, OBJECT(consoles[i]), &error_abort);
1784 g_free(name);
1785 }
1786
1787 return display_state;
1788 }
1789
1790 void graphic_console_set_hwops(QemuConsole *con,
1791 const GraphicHwOps *hw_ops,
1792 void *opaque)
1793 {
1794 con->hw_ops = hw_ops;
1795 con->hw = opaque;
1796 }
1797
1798 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1799 const GraphicHwOps *hw_ops,
1800 void *opaque)
1801 {
1802 static const char noinit[] =
1803 "Guest has not initialized the display (yet).";
1804 int width = 640;
1805 int height = 480;
1806 QemuConsole *s;
1807 DisplayState *ds;
1808
1809 ds = get_alloc_displaystate();
1810 trace_console_gfx_new();
1811 s = new_console(ds, GRAPHIC_CONSOLE, head);
1812 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1813 graphic_console_set_hwops(s, hw_ops, opaque);
1814 if (dev) {
1815 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1816 &error_abort);
1817 }
1818
1819 s->surface = qemu_create_message_surface(width, height, noinit);
1820 return s;
1821 }
1822
1823 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1824 {
1825 if (index >= nb_consoles) {
1826 return NULL;
1827 }
1828 return consoles[index];
1829 }
1830
1831 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1832 {
1833 Object *obj;
1834 uint32_t h;
1835 int i;
1836
1837 for (i = 0; i < nb_consoles; i++) {
1838 if (!consoles[i]) {
1839 continue;
1840 }
1841 obj = object_property_get_link(OBJECT(consoles[i]),
1842 "device", &error_abort);
1843 if (DEVICE(obj) != dev) {
1844 continue;
1845 }
1846 h = object_property_get_int(OBJECT(consoles[i]),
1847 "head", &error_abort);
1848 if (h != head) {
1849 continue;
1850 }
1851 return consoles[i];
1852 }
1853 return NULL;
1854 }
1855
1856 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1857 uint32_t head, Error **errp)
1858 {
1859 DeviceState *dev;
1860 QemuConsole *con;
1861
1862 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1863 if (dev == NULL) {
1864 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1865 "Device '%s' not found", device_id);
1866 return NULL;
1867 }
1868
1869 con = qemu_console_lookup_by_device(dev, head);
1870 if (con == NULL) {
1871 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1872 device_id, head);
1873 return NULL;
1874 }
1875
1876 return con;
1877 }
1878
1879 bool qemu_console_is_visible(QemuConsole *con)
1880 {
1881 return (con == active_console) || (con->dcls > 0);
1882 }
1883
1884 bool qemu_console_is_graphic(QemuConsole *con)
1885 {
1886 if (con == NULL) {
1887 con = active_console;
1888 }
1889 return con && (con->console_type == GRAPHIC_CONSOLE);
1890 }
1891
1892 bool qemu_console_is_fixedsize(QemuConsole *con)
1893 {
1894 if (con == NULL) {
1895 con = active_console;
1896 }
1897 return con && (con->console_type != TEXT_CONSOLE);
1898 }
1899
1900 bool qemu_console_is_gl_blocked(QemuConsole *con)
1901 {
1902 assert(con != NULL);
1903 return con->gl_block;
1904 }
1905
1906 char *qemu_console_get_label(QemuConsole *con)
1907 {
1908 if (con->console_type == GRAPHIC_CONSOLE) {
1909 if (con->device) {
1910 return g_strdup(object_get_typename(con->device));
1911 }
1912 return g_strdup("VGA");
1913 } else {
1914 if (con->chr && con->chr->label) {
1915 return g_strdup(con->chr->label);
1916 }
1917 return g_strdup_printf("vc%d", con->index);
1918 }
1919 }
1920
1921 int qemu_console_get_index(QemuConsole *con)
1922 {
1923 if (con == NULL) {
1924 con = active_console;
1925 }
1926 return con ? con->index : -1;
1927 }
1928
1929 uint32_t qemu_console_get_head(QemuConsole *con)
1930 {
1931 if (con == NULL) {
1932 con = active_console;
1933 }
1934 return con ? con->head : -1;
1935 }
1936
1937 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1938 {
1939 assert(con != NULL);
1940 return &con->ui_info;
1941 }
1942
1943 int qemu_console_get_width(QemuConsole *con, int fallback)
1944 {
1945 if (con == NULL) {
1946 con = active_console;
1947 }
1948 return con ? surface_width(con->surface) : fallback;
1949 }
1950
1951 int qemu_console_get_height(QemuConsole *con, int fallback)
1952 {
1953 if (con == NULL) {
1954 con = active_console;
1955 }
1956 return con ? surface_height(con->surface) : fallback;
1957 }
1958
1959 static void text_console_set_echo(CharDriverState *chr, bool echo)
1960 {
1961 QemuConsole *s = chr->opaque;
1962
1963 s->echo = echo;
1964 }
1965
1966 static void text_console_update_cursor_timer(void)
1967 {
1968 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1969 + CONSOLE_CURSOR_PERIOD / 2);
1970 }
1971
1972 static void text_console_update_cursor(void *opaque)
1973 {
1974 QemuConsole *s;
1975 int i, count = 0;
1976
1977 cursor_visible_phase = !cursor_visible_phase;
1978
1979 for (i = 0; i < nb_consoles; i++) {
1980 s = consoles[i];
1981 if (qemu_console_is_graphic(s) ||
1982 !qemu_console_is_visible(s)) {
1983 continue;
1984 }
1985 count++;
1986 graphic_hw_invalidate(s);
1987 }
1988
1989 if (count) {
1990 text_console_update_cursor_timer();
1991 }
1992 }
1993
1994 static const GraphicHwOps text_console_ops = {
1995 .invalidate = text_console_invalidate,
1996 .text_update = text_console_update,
1997 };
1998
1999 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
2000 {
2001 QemuConsole *s;
2002 int g_width = 80 * FONT_WIDTH;
2003 int g_height = 24 * FONT_HEIGHT;
2004
2005 s = chr->opaque;
2006
2007 s->out_fifo.buf = s->out_fifo_buf;
2008 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
2009 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
2010 s->ds = ds;
2011
2012 s->y_displayed = 0;
2013 s->y_base = 0;
2014 s->total_height = DEFAULT_BACKSCROLL;
2015 s->x = 0;
2016 s->y = 0;
2017 if (!s->surface) {
2018 if (active_console && active_console->surface) {
2019 g_width = surface_width(active_console->surface);
2020 g_height = surface_height(active_console->surface);
2021 }
2022 s->surface = qemu_create_displaysurface(g_width, g_height);
2023 }
2024
2025 s->hw_ops = &text_console_ops;
2026 s->hw = s;
2027
2028 /* Set text attribute defaults */
2029 s->t_attrib_default.bold = 0;
2030 s->t_attrib_default.uline = 0;
2031 s->t_attrib_default.blink = 0;
2032 s->t_attrib_default.invers = 0;
2033 s->t_attrib_default.unvisible = 0;
2034 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2035 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2036 /* set current text attributes to default */
2037 s->t_attrib = s->t_attrib_default;
2038 text_console_resize(s);
2039
2040 if (chr->label) {
2041 char msg[128];
2042 int len;
2043
2044 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2045 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2046 console_puts(chr, (uint8_t*)msg, len);
2047 s->t_attrib = s->t_attrib_default;
2048 }
2049
2050 qemu_chr_be_generic_open(chr);
2051 }
2052
2053 static const CharDriver vc_driver;
2054
2055 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2056 {
2057 ChardevCommon *common = qapi_ChardevVC_base(vc);
2058 CharDriverState *chr;
2059 QemuConsole *s;
2060 unsigned width = 0;
2061 unsigned height = 0;
2062
2063 chr = qemu_chr_alloc(&vc_driver, common, errp);
2064 if (!chr) {
2065 return NULL;
2066 }
2067
2068 if (vc->has_width) {
2069 width = vc->width;
2070 } else if (vc->has_cols) {
2071 width = vc->cols * FONT_WIDTH;
2072 }
2073
2074 if (vc->has_height) {
2075 height = vc->height;
2076 } else if (vc->has_rows) {
2077 height = vc->rows * FONT_HEIGHT;
2078 }
2079
2080 trace_console_txt_new(width, height);
2081 if (width == 0 || height == 0) {
2082 s = new_console(NULL, TEXT_CONSOLE, 0);
2083 } else {
2084 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2085 s->surface = qemu_create_displaysurface(width, height);
2086 }
2087
2088 if (!s) {
2089 g_free(chr);
2090 error_setg(errp, "cannot create text console");
2091 return NULL;
2092 }
2093
2094 s->chr = chr;
2095 chr->opaque = s;
2096
2097 if (display_state) {
2098 text_console_do_init(chr, display_state);
2099 }
2100 return chr;
2101 }
2102
2103 static VcHandler *vc_handler = text_console_init;
2104
2105 static CharDriverState *vc_init(const CharDriver *driver,
2106 const char *id, ChardevBackend *backend,
2107 ChardevReturn *ret, bool *be_opened,
2108 Error **errp)
2109 {
2110 /* console/chardev init sometimes completes elsewhere in a 2nd
2111 * stage, so defer OPENED events until they are fully initialized
2112 */
2113 *be_opened = false;
2114 return vc_handler(backend->u.vc.data, errp);
2115 }
2116
2117 void register_vc_handler(VcHandler *handler)
2118 {
2119 vc_handler = handler;
2120 }
2121
2122 void qemu_console_resize(QemuConsole *s, int width, int height)
2123 {
2124 DisplaySurface *surface;
2125
2126 assert(s->console_type == GRAPHIC_CONSOLE);
2127
2128 if (s->surface &&
2129 pixman_image_get_width(s->surface->image) == width &&
2130 pixman_image_get_height(s->surface->image) == height) {
2131 return;
2132 }
2133
2134 surface = qemu_create_displaysurface(width, height);
2135 dpy_gfx_replace_surface(s, surface);
2136 }
2137
2138 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
2139 int dst_x, int dst_y, int w, int h)
2140 {
2141 assert(con->console_type == GRAPHIC_CONSOLE);
2142 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
2143 }
2144
2145 DisplaySurface *qemu_console_surface(QemuConsole *console)
2146 {
2147 return console->surface;
2148 }
2149
2150 PixelFormat qemu_default_pixelformat(int bpp)
2151 {
2152 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2153 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2154 return pf;
2155 }
2156
2157 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2158 Error **errp)
2159 {
2160 int val;
2161 ChardevVC *vc;
2162
2163 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2164 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2165
2166 val = qemu_opt_get_number(opts, "width", 0);
2167 if (val != 0) {
2168 vc->has_width = true;
2169 vc->width = val;
2170 }
2171
2172 val = qemu_opt_get_number(opts, "height", 0);
2173 if (val != 0) {
2174 vc->has_height = true;
2175 vc->height = val;
2176 }
2177
2178 val = qemu_opt_get_number(opts, "cols", 0);
2179 if (val != 0) {
2180 vc->has_cols = true;
2181 vc->cols = val;
2182 }
2183
2184 val = qemu_opt_get_number(opts, "rows", 0);
2185 if (val != 0) {
2186 vc->has_rows = true;
2187 vc->rows = val;
2188 }
2189 }
2190
2191 static const TypeInfo qemu_console_info = {
2192 .name = TYPE_QEMU_CONSOLE,
2193 .parent = TYPE_OBJECT,
2194 .instance_size = sizeof(QemuConsole),
2195 .class_size = sizeof(QemuConsoleClass),
2196 };
2197
2198 static const CharDriver vc_driver = {
2199 .kind = CHARDEV_BACKEND_KIND_VC,
2200 .parse = qemu_chr_parse_vc,
2201 .create = vc_init,
2202 .chr_write = console_puts,
2203 .chr_set_echo = text_console_set_echo,
2204 };
2205
2206 static void register_types(void)
2207 {
2208 type_register_static(&qemu_console_info);
2209 register_char_driver(&vc_driver);
2210 }
2211
2212 type_init(register_types);