ui/vc: move VCChardev declaration at the top
[qemu/ar7.git] / ui / console.c
blob5c8e3ad1df1b267ada7bd2b0043925dbf12c39ca
1 /*
2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-ui.h"
30 #include "qemu/coroutine.h"
31 #include "qemu/fifo8.h"
32 #include "qemu/error-report.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/module.h"
35 #include "qemu/option.h"
36 #include "qemu/timer.h"
37 #include "chardev/char.h"
38 #include "trace.h"
39 #include "exec/memory.h"
40 #include "qom/object.h"
42 #define DEFAULT_BACKSCROLL 512
43 #define CONSOLE_CURSOR_PERIOD 500
45 typedef struct TextAttributes {
46 uint8_t fgcol:4;
47 uint8_t bgcol:4;
48 uint8_t bold:1;
49 uint8_t uline:1;
50 uint8_t blink:1;
51 uint8_t invers:1;
52 uint8_t unvisible:1;
53 } TextAttributes;
55 typedef struct TextCell {
56 uint8_t ch;
57 TextAttributes t_attrib;
58 } TextCell;
60 #define MAX_ESC_PARAMS 3
62 enum TTYState {
63 TTY_STATE_NORM,
64 TTY_STATE_ESC,
65 TTY_STATE_CSI,
68 typedef enum {
69 GRAPHIC_CONSOLE,
70 TEXT_CONSOLE,
71 TEXT_CONSOLE_FIXED_SIZE
72 } console_type_t;
74 struct QemuConsole {
75 Object parent;
77 int index;
78 console_type_t console_type;
79 DisplayState *ds;
80 DisplaySurface *surface;
81 DisplayScanout scanout;
82 int dcls;
83 DisplayGLCtx *gl;
84 int gl_block;
85 QEMUTimer *gl_unblock_timer;
86 int window_id;
88 /* Graphic console state. */
89 Object *device;
90 uint32_t head;
91 QemuUIInfo ui_info;
92 QEMUTimer *ui_timer;
93 QEMUCursor *cursor;
94 int cursor_x, cursor_y, cursor_on;
95 const GraphicHwOps *hw_ops;
96 void *hw;
98 /* Text console state */
99 int width;
100 int height;
101 int total_height;
102 int backscroll_height;
103 int x, y;
104 int x_saved, y_saved;
105 int y_displayed;
106 int y_base;
107 TextAttributes t_attrib_default; /* default text attributes */
108 TextAttributes t_attrib; /* currently active text attributes */
109 TextCell *cells;
110 int text_x[2], text_y[2], cursor_invalidate;
111 int echo;
113 int update_x0;
114 int update_y0;
115 int update_x1;
116 int update_y1;
118 enum TTYState state;
119 int esc_params[MAX_ESC_PARAMS];
120 int nb_esc_params;
122 Chardev *chr;
123 /* fifo for key pressed */
124 Fifo8 out_fifo;
125 CoQueue dump_queue;
127 QTAILQ_ENTRY(QemuConsole) next;
130 struct VCChardev {
131 Chardev parent;
132 QemuConsole *console;
134 typedef struct VCChardev VCChardev;
136 struct DisplayState {
137 QEMUTimer *gui_timer;
138 uint64_t last_update;
139 uint64_t update_interval;
140 bool refreshing;
142 QLIST_HEAD(, DisplayChangeListener) listeners;
145 static DisplayState *display_state;
146 static QemuConsole *active_console;
147 static QTAILQ_HEAD(, QemuConsole) consoles =
148 QTAILQ_HEAD_INITIALIZER(consoles);
149 static bool cursor_visible_phase;
150 static QEMUTimer *cursor_timer;
152 static void text_console_do_init(Chardev *chr);
153 static void dpy_refresh(DisplayState *s);
154 static DisplayState *get_alloc_displaystate(void);
155 static void text_console_update_cursor_timer(void);
156 static void text_console_update_cursor(void *opaque);
157 static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
158 static bool console_compatible_with(QemuConsole *con,
159 DisplayChangeListener *dcl, Error **errp);
161 static void gui_update(void *opaque)
163 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
164 uint64_t dcl_interval;
165 DisplayState *ds = opaque;
166 DisplayChangeListener *dcl;
168 ds->refreshing = true;
169 dpy_refresh(ds);
170 ds->refreshing = false;
172 QLIST_FOREACH(dcl, &ds->listeners, next) {
173 dcl_interval = dcl->update_interval ?
174 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
175 if (interval > dcl_interval) {
176 interval = dcl_interval;
179 if (ds->update_interval != interval) {
180 ds->update_interval = interval;
181 trace_console_refresh(interval);
183 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
184 timer_mod(ds->gui_timer, ds->last_update + interval);
187 static void gui_setup_refresh(DisplayState *ds)
189 DisplayChangeListener *dcl;
190 bool need_timer = false;
192 QLIST_FOREACH(dcl, &ds->listeners, next) {
193 if (dcl->ops->dpy_refresh != NULL) {
194 need_timer = true;
198 if (need_timer && ds->gui_timer == NULL) {
199 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
200 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
202 if (!need_timer && ds->gui_timer != NULL) {
203 timer_free(ds->gui_timer);
204 ds->gui_timer = NULL;
208 void graphic_hw_update_done(QemuConsole *con)
210 if (con) {
211 qemu_co_enter_all(&con->dump_queue, NULL);
215 void graphic_hw_update(QemuConsole *con)
217 bool async = false;
218 con = con ? con : active_console;
219 if (!con) {
220 return;
222 if (con->hw_ops->gfx_update) {
223 con->hw_ops->gfx_update(con->hw);
224 async = con->hw_ops->gfx_update_async;
226 if (!async) {
227 graphic_hw_update_done(con);
231 static void graphic_hw_update_bh(void *con)
233 graphic_hw_update(con);
236 void qemu_console_co_wait_update(QemuConsole *con)
238 if (qemu_co_queue_empty(&con->dump_queue)) {
239 /* Defer the update, it will restart the pending coroutines */
240 aio_bh_schedule_oneshot(qemu_get_aio_context(),
241 graphic_hw_update_bh, con);
243 qemu_co_queue_wait(&con->dump_queue, NULL);
247 static void graphic_hw_gl_unblock_timer(void *opaque)
249 warn_report("console: no gl-unblock within one second");
252 void graphic_hw_gl_block(QemuConsole *con, bool block)
254 uint64_t timeout;
255 assert(con != NULL);
257 if (block) {
258 con->gl_block++;
259 } else {
260 con->gl_block--;
262 assert(con->gl_block >= 0);
263 if (!con->hw_ops->gl_block) {
264 return;
266 if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
267 return;
269 con->hw_ops->gl_block(con->hw, block);
271 if (block) {
272 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
273 timeout += 1000; /* one sec */
274 timer_mod(con->gl_unblock_timer, timeout);
275 } else {
276 timer_del(con->gl_unblock_timer);
280 int qemu_console_get_window_id(QemuConsole *con)
282 return con->window_id;
285 void qemu_console_set_window_id(QemuConsole *con, int window_id)
287 con->window_id = window_id;
290 void graphic_hw_invalidate(QemuConsole *con)
292 if (!con) {
293 con = active_console;
295 if (con && con->hw_ops->invalidate) {
296 con->hw_ops->invalidate(con->hw);
300 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
302 if (!con) {
303 con = active_console;
305 if (con && con->hw_ops->text_update) {
306 con->hw_ops->text_update(con->hw, chardata);
310 static void vga_fill_rect(QemuConsole *con,
311 int posx, int posy, int width, int height,
312 pixman_color_t color)
314 DisplaySurface *surface = qemu_console_surface(con);
315 pixman_rectangle16_t rect = {
316 .x = posx, .y = posy, .width = width, .height = height
319 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
320 &color, 1, &rect);
323 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
324 static void vga_bitblt(QemuConsole *con,
325 int xs, int ys, int xd, int yd, int w, int h)
327 DisplaySurface *surface = qemu_console_surface(con);
329 pixman_image_composite(PIXMAN_OP_SRC,
330 surface->image, NULL, surface->image,
331 xs, ys, 0, 0, xd, yd, w, h);
334 /***********************************************************/
335 /* basic char display */
337 #define FONT_HEIGHT 16
338 #define FONT_WIDTH 8
340 #include "vgafont.h"
342 #define QEMU_RGB(r, g, b) \
343 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
345 static const pixman_color_t color_table_rgb[2][8] = {
346 { /* dark */
347 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
348 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
349 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
350 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
351 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
352 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
353 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
354 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
356 { /* bright */
357 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
358 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
359 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
360 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
361 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
362 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
363 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
364 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
368 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
369 TextAttributes *t_attrib)
371 static pixman_image_t *glyphs[256];
372 DisplaySurface *surface = qemu_console_surface(s);
373 pixman_color_t fgcol, bgcol;
375 if (t_attrib->invers) {
376 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
377 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
378 } else {
379 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
380 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
383 if (!glyphs[ch]) {
384 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
386 qemu_pixman_glyph_render(glyphs[ch], surface->image,
387 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
390 static void text_console_resize(QemuConsole *s)
392 TextCell *cells, *c, *c1;
393 int w1, x, y, last_width;
395 assert(s->scanout.kind == SCANOUT_SURFACE);
397 last_width = s->width;
398 s->width = surface_width(s->surface) / FONT_WIDTH;
399 s->height = surface_height(s->surface) / FONT_HEIGHT;
401 w1 = last_width;
402 if (s->width < w1)
403 w1 = s->width;
405 cells = g_new(TextCell, s->width * s->total_height + 1);
406 for(y = 0; y < s->total_height; y++) {
407 c = &cells[y * s->width];
408 if (w1 > 0) {
409 c1 = &s->cells[y * last_width];
410 for(x = 0; x < w1; x++) {
411 *c++ = *c1++;
414 for(x = w1; x < s->width; x++) {
415 c->ch = ' ';
416 c->t_attrib = s->t_attrib_default;
417 c++;
420 g_free(s->cells);
421 s->cells = cells;
424 static inline void text_update_xy(QemuConsole *s, int x, int y)
426 s->text_x[0] = MIN(s->text_x[0], x);
427 s->text_x[1] = MAX(s->text_x[1], x);
428 s->text_y[0] = MIN(s->text_y[0], y);
429 s->text_y[1] = MAX(s->text_y[1], y);
432 static void invalidate_xy(QemuConsole *s, int x, int y)
434 if (!qemu_console_is_visible(s)) {
435 return;
437 if (s->update_x0 > x * FONT_WIDTH)
438 s->update_x0 = x * FONT_WIDTH;
439 if (s->update_y0 > y * FONT_HEIGHT)
440 s->update_y0 = y * FONT_HEIGHT;
441 if (s->update_x1 < (x + 1) * FONT_WIDTH)
442 s->update_x1 = (x + 1) * FONT_WIDTH;
443 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
444 s->update_y1 = (y + 1) * FONT_HEIGHT;
447 static void update_xy(QemuConsole *s, int x, int y)
449 TextCell *c;
450 int y1, y2;
452 text_update_xy(s, x, y);
454 y1 = (s->y_base + y) % s->total_height;
455 y2 = y1 - s->y_displayed;
456 if (y2 < 0) {
457 y2 += s->total_height;
459 if (y2 < s->height) {
460 if (x >= s->width) {
461 x = s->width - 1;
463 c = &s->cells[y1 * s->width + x];
464 vga_putcharxy(s, x, y2, c->ch,
465 &(c->t_attrib));
466 invalidate_xy(s, x, y2);
470 static void console_show_cursor(QemuConsole *s, int show)
472 TextCell *c;
473 int y, y1;
474 int x = s->x;
476 s->cursor_invalidate = 1;
478 if (x >= s->width) {
479 x = s->width - 1;
481 y1 = (s->y_base + s->y) % s->total_height;
482 y = y1 - s->y_displayed;
483 if (y < 0) {
484 y += s->total_height;
486 if (y < s->height) {
487 c = &s->cells[y1 * s->width + x];
488 if (show && cursor_visible_phase) {
489 TextAttributes t_attrib = s->t_attrib_default;
490 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
491 vga_putcharxy(s, x, y, c->ch, &t_attrib);
492 } else {
493 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
495 invalidate_xy(s, x, y);
499 static void console_refresh(QemuConsole *s)
501 DisplaySurface *surface = qemu_console_surface(s);
502 TextCell *c;
503 int x, y, y1;
505 s->text_x[0] = 0;
506 s->text_y[0] = 0;
507 s->text_x[1] = s->width - 1;
508 s->text_y[1] = s->height - 1;
509 s->cursor_invalidate = 1;
511 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
512 color_table_rgb[0][QEMU_COLOR_BLACK]);
513 y1 = s->y_displayed;
514 for (y = 0; y < s->height; y++) {
515 c = s->cells + y1 * s->width;
516 for (x = 0; x < s->width; x++) {
517 vga_putcharxy(s, x, y, c->ch,
518 &(c->t_attrib));
519 c++;
521 if (++y1 == s->total_height) {
522 y1 = 0;
525 console_show_cursor(s, 1);
526 dpy_gfx_update(s, 0, 0,
527 surface_width(surface), surface_height(surface));
530 static void console_scroll(QemuConsole *s, int ydelta)
532 int i, y1;
534 if (ydelta > 0) {
535 for(i = 0; i < ydelta; i++) {
536 if (s->y_displayed == s->y_base)
537 break;
538 if (++s->y_displayed == s->total_height)
539 s->y_displayed = 0;
541 } else {
542 ydelta = -ydelta;
543 i = s->backscroll_height;
544 if (i > s->total_height - s->height)
545 i = s->total_height - s->height;
546 y1 = s->y_base - i;
547 if (y1 < 0)
548 y1 += s->total_height;
549 for(i = 0; i < ydelta; i++) {
550 if (s->y_displayed == y1)
551 break;
552 if (--s->y_displayed < 0)
553 s->y_displayed = s->total_height - 1;
556 console_refresh(s);
559 static void console_put_lf(QemuConsole *s)
561 TextCell *c;
562 int x, y1;
564 s->y++;
565 if (s->y >= s->height) {
566 s->y = s->height - 1;
568 if (s->y_displayed == s->y_base) {
569 if (++s->y_displayed == s->total_height)
570 s->y_displayed = 0;
572 if (++s->y_base == s->total_height)
573 s->y_base = 0;
574 if (s->backscroll_height < s->total_height)
575 s->backscroll_height++;
576 y1 = (s->y_base + s->height - 1) % s->total_height;
577 c = &s->cells[y1 * s->width];
578 for(x = 0; x < s->width; x++) {
579 c->ch = ' ';
580 c->t_attrib = s->t_attrib_default;
581 c++;
583 if (s->y_displayed == s->y_base) {
584 s->text_x[0] = 0;
585 s->text_y[0] = 0;
586 s->text_x[1] = s->width - 1;
587 s->text_y[1] = s->height - 1;
589 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
590 s->width * FONT_WIDTH,
591 (s->height - 1) * FONT_HEIGHT);
592 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
593 s->width * FONT_WIDTH, FONT_HEIGHT,
594 color_table_rgb[0][s->t_attrib_default.bgcol]);
595 s->update_x0 = 0;
596 s->update_y0 = 0;
597 s->update_x1 = s->width * FONT_WIDTH;
598 s->update_y1 = s->height * FONT_HEIGHT;
603 /* Set console attributes depending on the current escape codes.
604 * NOTE: I know this code is not very efficient (checking every color for it
605 * self) but it is more readable and better maintainable.
607 static void console_handle_escape(QemuConsole *s)
609 int i;
611 for (i=0; i<s->nb_esc_params; i++) {
612 switch (s->esc_params[i]) {
613 case 0: /* reset all console attributes to default */
614 s->t_attrib = s->t_attrib_default;
615 break;
616 case 1:
617 s->t_attrib.bold = 1;
618 break;
619 case 4:
620 s->t_attrib.uline = 1;
621 break;
622 case 5:
623 s->t_attrib.blink = 1;
624 break;
625 case 7:
626 s->t_attrib.invers = 1;
627 break;
628 case 8:
629 s->t_attrib.unvisible = 1;
630 break;
631 case 22:
632 s->t_attrib.bold = 0;
633 break;
634 case 24:
635 s->t_attrib.uline = 0;
636 break;
637 case 25:
638 s->t_attrib.blink = 0;
639 break;
640 case 27:
641 s->t_attrib.invers = 0;
642 break;
643 case 28:
644 s->t_attrib.unvisible = 0;
645 break;
646 /* set foreground color */
647 case 30:
648 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
649 break;
650 case 31:
651 s->t_attrib.fgcol = QEMU_COLOR_RED;
652 break;
653 case 32:
654 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
655 break;
656 case 33:
657 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
658 break;
659 case 34:
660 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
661 break;
662 case 35:
663 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
664 break;
665 case 36:
666 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
667 break;
668 case 37:
669 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
670 break;
671 /* set background color */
672 case 40:
673 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
674 break;
675 case 41:
676 s->t_attrib.bgcol = QEMU_COLOR_RED;
677 break;
678 case 42:
679 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
680 break;
681 case 43:
682 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
683 break;
684 case 44:
685 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
686 break;
687 case 45:
688 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
689 break;
690 case 46:
691 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
692 break;
693 case 47:
694 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
695 break;
700 static void console_clear_xy(QemuConsole *s, int x, int y)
702 int y1 = (s->y_base + y) % s->total_height;
703 if (x >= s->width) {
704 x = s->width - 1;
706 TextCell *c = &s->cells[y1 * s->width + x];
707 c->ch = ' ';
708 c->t_attrib = s->t_attrib_default;
709 update_xy(s, x, y);
712 static void console_put_one(QemuConsole *s, int ch)
714 TextCell *c;
715 int y1;
716 if (s->x >= s->width) {
717 /* line wrap */
718 s->x = 0;
719 console_put_lf(s);
721 y1 = (s->y_base + s->y) % s->total_height;
722 c = &s->cells[y1 * s->width + s->x];
723 c->ch = ch;
724 c->t_attrib = s->t_attrib;
725 update_xy(s, s->x, s->y);
726 s->x++;
729 static void console_respond_str(QemuConsole *s, const char *buf)
731 while (*buf) {
732 console_put_one(s, *buf);
733 buf++;
737 /* set cursor, checking bounds */
738 static void set_cursor(QemuConsole *s, int x, int y)
740 if (x < 0) {
741 x = 0;
743 if (y < 0) {
744 y = 0;
746 if (y >= s->height) {
747 y = s->height - 1;
749 if (x >= s->width) {
750 x = s->width - 1;
753 s->x = x;
754 s->y = y;
757 static void console_putchar(QemuConsole *s, int ch)
759 int i;
760 int x, y;
761 char response[40];
763 switch(s->state) {
764 case TTY_STATE_NORM:
765 switch(ch) {
766 case '\r': /* carriage return */
767 s->x = 0;
768 break;
769 case '\n': /* newline */
770 console_put_lf(s);
771 break;
772 case '\b': /* backspace */
773 if (s->x > 0)
774 s->x--;
775 break;
776 case '\t': /* tabspace */
777 if (s->x + (8 - (s->x % 8)) > s->width) {
778 s->x = 0;
779 console_put_lf(s);
780 } else {
781 s->x = s->x + (8 - (s->x % 8));
783 break;
784 case '\a': /* alert aka. bell */
785 /* TODO: has to be implemented */
786 break;
787 case 14:
788 /* SI (shift in), character set 0 (ignored) */
789 break;
790 case 15:
791 /* SO (shift out), character set 1 (ignored) */
792 break;
793 case 27: /* esc (introducing an escape sequence) */
794 s->state = TTY_STATE_ESC;
795 break;
796 default:
797 console_put_one(s, ch);
798 break;
800 break;
801 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
802 if (ch == '[') {
803 for(i=0;i<MAX_ESC_PARAMS;i++)
804 s->esc_params[i] = 0;
805 s->nb_esc_params = 0;
806 s->state = TTY_STATE_CSI;
807 } else {
808 s->state = TTY_STATE_NORM;
810 break;
811 case TTY_STATE_CSI: /* handle escape sequence parameters */
812 if (ch >= '0' && ch <= '9') {
813 if (s->nb_esc_params < MAX_ESC_PARAMS) {
814 int *param = &s->esc_params[s->nb_esc_params];
815 int digit = (ch - '0');
817 *param = (*param <= (INT_MAX - digit) / 10) ?
818 *param * 10 + digit : INT_MAX;
820 } else {
821 if (s->nb_esc_params < MAX_ESC_PARAMS)
822 s->nb_esc_params++;
823 if (ch == ';' || ch == '?') {
824 break;
826 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
827 ch, s->nb_esc_params);
828 s->state = TTY_STATE_NORM;
829 switch(ch) {
830 case 'A':
831 /* move cursor up */
832 if (s->esc_params[0] == 0) {
833 s->esc_params[0] = 1;
835 set_cursor(s, s->x, s->y - s->esc_params[0]);
836 break;
837 case 'B':
838 /* move cursor down */
839 if (s->esc_params[0] == 0) {
840 s->esc_params[0] = 1;
842 set_cursor(s, s->x, s->y + s->esc_params[0]);
843 break;
844 case 'C':
845 /* move cursor right */
846 if (s->esc_params[0] == 0) {
847 s->esc_params[0] = 1;
849 set_cursor(s, s->x + s->esc_params[0], s->y);
850 break;
851 case 'D':
852 /* move cursor left */
853 if (s->esc_params[0] == 0) {
854 s->esc_params[0] = 1;
856 set_cursor(s, s->x - s->esc_params[0], s->y);
857 break;
858 case 'G':
859 /* move cursor to column */
860 set_cursor(s, s->esc_params[0] - 1, s->y);
861 break;
862 case 'f':
863 case 'H':
864 /* move cursor to row, column */
865 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
866 break;
867 case 'J':
868 switch (s->esc_params[0]) {
869 case 0:
870 /* clear to end of screen */
871 for (y = s->y; y < s->height; y++) {
872 for (x = 0; x < s->width; x++) {
873 if (y == s->y && x < s->x) {
874 continue;
876 console_clear_xy(s, x, y);
879 break;
880 case 1:
881 /* clear from beginning of screen */
882 for (y = 0; y <= s->y; y++) {
883 for (x = 0; x < s->width; x++) {
884 if (y == s->y && x > s->x) {
885 break;
887 console_clear_xy(s, x, y);
890 break;
891 case 2:
892 /* clear entire screen */
893 for (y = 0; y <= s->height; y++) {
894 for (x = 0; x < s->width; x++) {
895 console_clear_xy(s, x, y);
898 break;
900 break;
901 case 'K':
902 switch (s->esc_params[0]) {
903 case 0:
904 /* clear to eol */
905 for(x = s->x; x < s->width; x++) {
906 console_clear_xy(s, x, s->y);
908 break;
909 case 1:
910 /* clear from beginning of line */
911 for (x = 0; x <= s->x && x < s->width; x++) {
912 console_clear_xy(s, x, s->y);
914 break;
915 case 2:
916 /* clear entire line */
917 for(x = 0; x < s->width; x++) {
918 console_clear_xy(s, x, s->y);
920 break;
922 break;
923 case 'm':
924 console_handle_escape(s);
925 break;
926 case 'n':
927 switch (s->esc_params[0]) {
928 case 5:
929 /* report console status (always succeed)*/
930 console_respond_str(s, "\033[0n");
931 break;
932 case 6:
933 /* report cursor position */
934 sprintf(response, "\033[%d;%dR",
935 (s->y_base + s->y) % s->total_height + 1,
936 s->x + 1);
937 console_respond_str(s, response);
938 break;
940 break;
941 case 's':
942 /* save cursor position */
943 s->x_saved = s->x;
944 s->y_saved = s->y;
945 break;
946 case 'u':
947 /* restore cursor position */
948 s->x = s->x_saved;
949 s->y = s->y_saved;
950 break;
951 default:
952 trace_console_putchar_unhandled(ch);
953 break;
955 break;
960 static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
961 struct DisplaySurface *new_surface,
962 bool update)
964 if (dcl->ops->dpy_gfx_switch) {
965 dcl->ops->dpy_gfx_switch(dcl, new_surface);
968 if (update && dcl->ops->dpy_gfx_update) {
969 dcl->ops->dpy_gfx_update(dcl, 0, 0,
970 surface_width(new_surface),
971 surface_height(new_surface));
975 static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
977 if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
978 con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
982 static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
984 if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
985 con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
989 static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
990 int x, int y, int w, int h)
992 if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
993 con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
997 static void displaychangelistener_display_console(DisplayChangeListener *dcl,
998 QemuConsole *con,
999 Error **errp)
1001 static const char nodev[] =
1002 "This VM has no graphic display device.";
1003 static DisplaySurface *dummy;
1005 if (!con || !console_compatible_with(con, dcl, errp)) {
1006 if (!dummy) {
1007 dummy = qemu_create_placeholder_surface(640, 480, nodev);
1009 if (con) {
1010 dpy_gfx_create_texture(con, dummy);
1012 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
1013 return;
1016 dpy_gfx_create_texture(con, con->surface);
1017 displaychangelistener_gfx_switch(dcl, con->surface,
1018 con->scanout.kind == SCANOUT_SURFACE);
1020 if (con->scanout.kind == SCANOUT_DMABUF &&
1021 displaychangelistener_has_dmabuf(dcl)) {
1022 dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
1023 } else if (con->scanout.kind == SCANOUT_TEXTURE &&
1024 dcl->ops->dpy_gl_scanout_texture) {
1025 dcl->ops->dpy_gl_scanout_texture(dcl,
1026 con->scanout.texture.backing_id,
1027 con->scanout.texture.backing_y_0_top,
1028 con->scanout.texture.backing_width,
1029 con->scanout.texture.backing_height,
1030 con->scanout.texture.x,
1031 con->scanout.texture.y,
1032 con->scanout.texture.width,
1033 con->scanout.texture.height,
1034 con->scanout.texture.d3d_tex2d);
1038 void console_select(unsigned int index)
1040 DisplayChangeListener *dcl;
1041 QemuConsole *s;
1043 trace_console_select(index);
1044 s = qemu_console_lookup_by_index(index);
1045 if (s) {
1046 DisplayState *ds = s->ds;
1048 active_console = s;
1049 QLIST_FOREACH (dcl, &ds->listeners, next) {
1050 if (dcl->con != NULL) {
1051 continue;
1053 displaychangelistener_display_console(dcl, s, NULL);
1055 dpy_text_resize(s, s->width, s->height);
1056 text_console_update_cursor(NULL);
1060 #define TYPE_CHARDEV_VC "chardev-vc"
1061 DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
1062 TYPE_CHARDEV_VC)
1064 static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1066 VCChardev *drv = VC_CHARDEV(chr);
1067 QemuConsole *s = drv->console;
1068 int i;
1070 s->update_x0 = s->width * FONT_WIDTH;
1071 s->update_y0 = s->height * FONT_HEIGHT;
1072 s->update_x1 = 0;
1073 s->update_y1 = 0;
1074 console_show_cursor(s, 0);
1075 for(i = 0; i < len; i++) {
1076 console_putchar(s, buf[i]);
1078 console_show_cursor(s, 1);
1079 if (s->update_x0 < s->update_x1) {
1080 dpy_gfx_update(s, s->update_x0, s->update_y0,
1081 s->update_x1 - s->update_x0,
1082 s->update_y1 - s->update_y0);
1084 return len;
1087 static void kbd_send_chars(QemuConsole *s)
1089 uint32_t len, avail;
1091 len = qemu_chr_be_can_write(s->chr);
1092 avail = fifo8_num_used(&s->out_fifo);
1093 while (len > 0 && avail > 0) {
1094 const uint8_t *buf;
1095 uint32_t size;
1097 buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
1098 qemu_chr_be_write(s->chr, buf, size);
1099 len = qemu_chr_be_can_write(s->chr);
1100 avail -= size;
1104 /* called when an ascii key is pressed */
1105 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1107 uint8_t buf[16], *q;
1108 int c;
1109 uint32_t num_free;
1111 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1112 return;
1114 switch(keysym) {
1115 case QEMU_KEY_CTRL_UP:
1116 console_scroll(s, -1);
1117 break;
1118 case QEMU_KEY_CTRL_DOWN:
1119 console_scroll(s, 1);
1120 break;
1121 case QEMU_KEY_CTRL_PAGEUP:
1122 console_scroll(s, -10);
1123 break;
1124 case QEMU_KEY_CTRL_PAGEDOWN:
1125 console_scroll(s, 10);
1126 break;
1127 default:
1128 /* convert the QEMU keysym to VT100 key string */
1129 q = buf;
1130 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1131 *q++ = '\033';
1132 *q++ = '[';
1133 c = keysym - 0xe100;
1134 if (c >= 10)
1135 *q++ = '0' + (c / 10);
1136 *q++ = '0' + (c % 10);
1137 *q++ = '~';
1138 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1139 *q++ = '\033';
1140 *q++ = '[';
1141 *q++ = keysym & 0xff;
1142 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1143 qemu_chr_write(s->chr, (uint8_t *)"\r", 1, true);
1144 *q++ = '\n';
1145 } else {
1146 *q++ = keysym;
1148 if (s->echo) {
1149 qemu_chr_write(s->chr, buf, q - buf, true);
1151 num_free = fifo8_num_free(&s->out_fifo);
1152 fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
1153 kbd_send_chars(s);
1154 break;
1158 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1159 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1160 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1161 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1162 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1163 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1164 [Q_KEY_CODE_END] = QEMU_KEY_END,
1165 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1166 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1167 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1168 [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
1169 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1172 static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
1173 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
1174 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
1175 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
1176 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
1177 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
1178 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
1179 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
1180 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
1183 bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
1185 int keysym;
1187 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
1188 if (keysym == 0) {
1189 return false;
1191 kbd_put_keysym_console(s, keysym);
1192 return true;
1195 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1197 int i;
1199 for (i = 0; i < len && str[i]; i++) {
1200 kbd_put_keysym_console(s, str[i]);
1204 void kbd_put_keysym(int keysym)
1206 kbd_put_keysym_console(active_console, keysym);
1209 static void text_console_invalidate(void *opaque)
1211 QemuConsole *s = (QemuConsole *) opaque;
1213 if (s->console_type == TEXT_CONSOLE) {
1214 text_console_resize(s);
1216 console_refresh(s);
1219 static void text_console_update(void *opaque, console_ch_t *chardata)
1221 QemuConsole *s = (QemuConsole *) opaque;
1222 int i, j, src;
1224 if (s->text_x[0] <= s->text_x[1]) {
1225 src = (s->y_base + s->text_y[0]) * s->width;
1226 chardata += s->text_y[0] * s->width;
1227 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1228 for (j = 0; j < s->width; j++, src++) {
1229 console_write_ch(chardata ++,
1230 ATTR2CHTYPE(s->cells[src].ch,
1231 s->cells[src].t_attrib.fgcol,
1232 s->cells[src].t_attrib.bgcol,
1233 s->cells[src].t_attrib.bold));
1235 dpy_text_update(s, s->text_x[0], s->text_y[0],
1236 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1237 s->text_x[0] = s->width;
1238 s->text_y[0] = s->height;
1239 s->text_x[1] = 0;
1240 s->text_y[1] = 0;
1242 if (s->cursor_invalidate) {
1243 dpy_text_cursor(s, s->x, s->y);
1244 s->cursor_invalidate = 0;
1248 static QemuConsole *new_console(console_type_t console_type, uint32_t head)
1250 DisplayState *ds = get_alloc_displaystate();
1251 Object *obj;
1252 QemuConsole *s;
1253 int i;
1255 obj = object_new(TYPE_QEMU_CONSOLE);
1256 s = QEMU_CONSOLE(obj);
1257 qemu_co_queue_init(&s->dump_queue);
1258 s->head = head;
1259 object_property_add_link(obj, "device", TYPE_DEVICE,
1260 (Object **)&s->device,
1261 object_property_allow_set_link,
1262 OBJ_PROP_LINK_STRONG);
1263 object_property_add_uint32_ptr(obj, "head", &s->head,
1264 OBJ_PROP_FLAG_READ);
1266 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1267 (console_type == GRAPHIC_CONSOLE))) {
1268 active_console = s;
1270 s->ds = ds;
1271 s->console_type = console_type;
1272 s->window_id = -1;
1274 if (QTAILQ_EMPTY(&consoles)) {
1275 s->index = 0;
1276 QTAILQ_INSERT_TAIL(&consoles, s, next);
1277 } else if (console_type != GRAPHIC_CONSOLE || phase_check(PHASE_MACHINE_READY)) {
1278 QemuConsole *last = QTAILQ_LAST(&consoles);
1279 s->index = last->index + 1;
1280 QTAILQ_INSERT_TAIL(&consoles, s, next);
1281 } else {
1283 * HACK: Put graphical consoles before text consoles.
1285 * Only do that for coldplugged devices. After initial device
1286 * initialization we will not renumber the consoles any more.
1288 QemuConsole *c = QTAILQ_FIRST(&consoles);
1290 while (QTAILQ_NEXT(c, next) != NULL &&
1291 c->console_type == GRAPHIC_CONSOLE) {
1292 c = QTAILQ_NEXT(c, next);
1294 if (c->console_type == GRAPHIC_CONSOLE) {
1295 /* have no text consoles */
1296 s->index = c->index + 1;
1297 QTAILQ_INSERT_AFTER(&consoles, c, s, next);
1298 } else {
1299 s->index = c->index;
1300 QTAILQ_INSERT_BEFORE(c, s, next);
1301 /* renumber text consoles */
1302 for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
1303 c->index = i;
1307 return s;
1310 #ifdef WIN32
1311 void qemu_displaysurface_win32_set_handle(DisplaySurface *surface,
1312 HANDLE h, uint32_t offset)
1314 assert(!surface->handle);
1316 surface->handle = h;
1317 surface->handle_offset = offset;
1320 static void
1321 win32_pixman_image_destroy(pixman_image_t *image, void *data)
1323 DisplaySurface *surface = data;
1325 if (!surface->handle) {
1326 return;
1329 assert(surface->handle_offset == 0);
1331 qemu_win32_map_free(
1332 pixman_image_get_data(surface->image),
1333 surface->handle,
1334 &error_warn
1337 #endif
1339 DisplaySurface *qemu_create_displaysurface(int width, int height)
1341 DisplaySurface *surface;
1342 void *bits = NULL;
1343 #ifdef WIN32
1344 HANDLE handle = NULL;
1345 #endif
1347 trace_displaysurface_create(width, height);
1349 #ifdef WIN32
1350 bits = qemu_win32_map_alloc(width * height * 4, &handle, &error_abort);
1351 #endif
1353 surface = qemu_create_displaysurface_from(
1354 width, height,
1355 PIXMAN_x8r8g8b8,
1356 width * 4, bits
1358 surface->flags = QEMU_ALLOCATED_FLAG;
1360 #ifdef WIN32
1361 qemu_displaysurface_win32_set_handle(surface, handle, 0);
1362 #endif
1363 return surface;
1366 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1367 pixman_format_code_t format,
1368 int linesize, uint8_t *data)
1370 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1372 trace_displaysurface_create_from(surface, width, height, format);
1373 surface->format = format;
1374 surface->image = pixman_image_create_bits(surface->format,
1375 width, height,
1376 (void *)data, linesize);
1377 assert(surface->image != NULL);
1378 #ifdef WIN32
1379 pixman_image_set_destroy_function(surface->image,
1380 win32_pixman_image_destroy, surface);
1381 #endif
1383 return surface;
1386 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1388 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1390 trace_displaysurface_create_pixman(surface);
1391 surface->format = pixman_image_get_format(image);
1392 surface->image = pixman_image_ref(image);
1394 return surface;
1397 DisplaySurface *qemu_create_placeholder_surface(int w, int h,
1398 const char *msg)
1400 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1401 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1402 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1403 pixman_image_t *glyph;
1404 int len, x, y, i;
1406 len = strlen(msg);
1407 x = (w / FONT_WIDTH - len) / 2;
1408 y = (h / FONT_HEIGHT - 1) / 2;
1409 for (i = 0; i < len; i++) {
1410 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1411 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1412 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1413 qemu_pixman_image_unref(glyph);
1415 surface->flags |= QEMU_PLACEHOLDER_FLAG;
1416 return surface;
1419 void qemu_free_displaysurface(DisplaySurface *surface)
1421 if (surface == NULL) {
1422 return;
1424 trace_displaysurface_free(surface);
1425 qemu_pixman_image_unref(surface->image);
1426 g_free(surface);
1429 bool console_has_gl(QemuConsole *con)
1431 return con->gl != NULL;
1434 static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
1436 if (dcl->ops->dpy_has_dmabuf) {
1437 return dcl->ops->dpy_has_dmabuf(dcl);
1440 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1441 return true;
1444 return false;
1447 static bool console_compatible_with(QemuConsole *con,
1448 DisplayChangeListener *dcl, Error **errp)
1450 int flags;
1452 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
1454 if (console_has_gl(con) &&
1455 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
1456 error_setg(errp, "Display %s is incompatible with the GL context",
1457 dcl->ops->dpy_name);
1458 return false;
1461 if (flags & GRAPHIC_FLAGS_GL &&
1462 !console_has_gl(con)) {
1463 error_setg(errp, "The console requires a GL context.");
1464 return false;
1468 if (flags & GRAPHIC_FLAGS_DMABUF &&
1469 !displaychangelistener_has_dmabuf(dcl)) {
1470 error_setg(errp, "The console requires display DMABUF support.");
1471 return false;
1474 return true;
1477 void console_handle_touch_event(QemuConsole *con,
1478 struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
1479 uint64_t num_slot,
1480 int width, int height,
1481 double x, double y,
1482 InputMultiTouchType type,
1483 Error **errp)
1485 struct touch_slot *slot;
1486 bool needs_sync = false;
1487 int update;
1488 int i;
1490 if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
1491 error_setg(errp,
1492 "Unexpected touch slot number: % " PRId64" >= %d",
1493 num_slot, INPUT_EVENT_SLOTS_MAX);
1494 return;
1497 slot = &touch_slots[num_slot];
1498 slot->x = x;
1499 slot->y = y;
1501 if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
1502 slot->tracking_id = num_slot;
1505 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
1506 if (i == num_slot) {
1507 update = type;
1508 } else {
1509 update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
1512 slot = &touch_slots[i];
1514 if (slot->tracking_id == -1) {
1515 continue;
1518 if (update == INPUT_MULTI_TOUCH_TYPE_END) {
1519 slot->tracking_id = -1;
1520 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
1521 needs_sync = true;
1522 } else {
1523 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
1524 qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
1525 qemu_input_queue_mtt_abs(con,
1526 INPUT_AXIS_X, (int) slot->x,
1527 0, width,
1528 i, slot->tracking_id);
1529 qemu_input_queue_mtt_abs(con,
1530 INPUT_AXIS_Y, (int) slot->y,
1531 0, height,
1532 i, slot->tracking_id);
1533 needs_sync = true;
1537 if (needs_sync) {
1538 qemu_input_event_sync();
1542 void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
1544 /* display has opengl support */
1545 assert(con);
1546 if (con->gl) {
1547 error_report("The console already has an OpenGL context.");
1548 exit(1);
1550 con->gl = gl;
1553 void register_displaychangelistener(DisplayChangeListener *dcl)
1555 QemuConsole *con;
1557 assert(!dcl->ds);
1559 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1560 dcl->ds = get_alloc_displaystate();
1561 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1562 gui_setup_refresh(dcl->ds);
1563 if (dcl->con) {
1564 dcl->con->dcls++;
1565 con = dcl->con;
1566 } else {
1567 con = active_console;
1569 displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
1570 if (con && con->cursor && dcl->ops->dpy_cursor_define) {
1571 dcl->ops->dpy_cursor_define(dcl, con->cursor);
1573 if (con && dcl->ops->dpy_mouse_set) {
1574 dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
1576 text_console_update_cursor(NULL);
1579 void update_displaychangelistener(DisplayChangeListener *dcl,
1580 uint64_t interval)
1582 DisplayState *ds = dcl->ds;
1584 dcl->update_interval = interval;
1585 if (!ds->refreshing && ds->update_interval > interval) {
1586 timer_mod(ds->gui_timer, ds->last_update + interval);
1590 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1592 DisplayState *ds = dcl->ds;
1593 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1594 if (dcl->con) {
1595 dcl->con->dcls--;
1597 QLIST_REMOVE(dcl, next);
1598 dcl->ds = NULL;
1599 gui_setup_refresh(ds);
1602 static void dpy_set_ui_info_timer(void *opaque)
1604 QemuConsole *con = opaque;
1606 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1609 bool dpy_ui_info_supported(QemuConsole *con)
1611 if (con == NULL) {
1612 con = active_console;
1615 return con->hw_ops->ui_info != NULL;
1618 const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
1620 if (con == NULL) {
1621 con = active_console;
1624 return &con->ui_info;
1627 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
1629 if (con == NULL) {
1630 con = active_console;
1633 if (!dpy_ui_info_supported(con)) {
1634 return -1;
1636 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1637 /* nothing changed -- ignore */
1638 return 0;
1642 * Typically we get a flood of these as the user resizes the window.
1643 * Wait until the dust has settled (one second without updates), then
1644 * go notify the guest.
1646 con->ui_info = *info;
1647 timer_mod(con->ui_timer,
1648 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
1649 return 0;
1652 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1654 DisplayState *s = con->ds;
1655 DisplayChangeListener *dcl;
1656 int width = qemu_console_get_width(con, x + w);
1657 int height = qemu_console_get_height(con, y + h);
1659 x = MAX(x, 0);
1660 y = MAX(y, 0);
1661 x = MIN(x, width);
1662 y = MIN(y, height);
1663 w = MIN(w, width - x);
1664 h = MIN(h, height - y);
1666 if (!qemu_console_is_visible(con)) {
1667 return;
1669 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
1670 QLIST_FOREACH(dcl, &s->listeners, next) {
1671 if (con != (dcl->con ? dcl->con : active_console)) {
1672 continue;
1674 if (dcl->ops->dpy_gfx_update) {
1675 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1680 void dpy_gfx_update_full(QemuConsole *con)
1682 int w = qemu_console_get_width(con, 0);
1683 int h = qemu_console_get_height(con, 0);
1685 dpy_gfx_update(con, 0, 0, w, h);
1688 void dpy_gfx_replace_surface(QemuConsole *con,
1689 DisplaySurface *surface)
1691 static const char placeholder_msg[] = "Display output is not active.";
1692 DisplayState *s = con->ds;
1693 DisplaySurface *old_surface = con->surface;
1694 DisplaySurface *new_surface = surface;
1695 DisplayChangeListener *dcl;
1696 int width;
1697 int height;
1699 if (!surface) {
1700 if (old_surface) {
1701 width = surface_width(old_surface);
1702 height = surface_height(old_surface);
1703 } else {
1704 width = 640;
1705 height = 480;
1708 new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
1711 assert(old_surface != new_surface);
1713 con->scanout.kind = SCANOUT_SURFACE;
1714 con->surface = new_surface;
1715 dpy_gfx_create_texture(con, new_surface);
1716 QLIST_FOREACH(dcl, &s->listeners, next) {
1717 if (con != (dcl->con ? dcl->con : active_console)) {
1718 continue;
1720 displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
1722 dpy_gfx_destroy_texture(con, old_surface);
1723 qemu_free_displaysurface(old_surface);
1726 bool dpy_gfx_check_format(QemuConsole *con,
1727 pixman_format_code_t format)
1729 DisplayChangeListener *dcl;
1730 DisplayState *s = con->ds;
1732 QLIST_FOREACH(dcl, &s->listeners, next) {
1733 if (dcl->con && dcl->con != con) {
1734 /* dcl bound to another console -> skip */
1735 continue;
1737 if (dcl->ops->dpy_gfx_check_format) {
1738 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1739 return false;
1741 } else {
1742 /* default is to allow native 32 bpp only */
1743 if (format != qemu_default_pixman_format(32, true)) {
1744 return false;
1748 return true;
1751 static void dpy_refresh(DisplayState *s)
1753 DisplayChangeListener *dcl;
1755 QLIST_FOREACH(dcl, &s->listeners, next) {
1756 if (dcl->ops->dpy_refresh) {
1757 dcl->ops->dpy_refresh(dcl);
1762 void dpy_text_cursor(QemuConsole *con, int x, int y)
1764 DisplayState *s = con->ds;
1765 DisplayChangeListener *dcl;
1767 if (!qemu_console_is_visible(con)) {
1768 return;
1770 QLIST_FOREACH(dcl, &s->listeners, next) {
1771 if (con != (dcl->con ? dcl->con : active_console)) {
1772 continue;
1774 if (dcl->ops->dpy_text_cursor) {
1775 dcl->ops->dpy_text_cursor(dcl, x, y);
1780 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1782 DisplayState *s = con->ds;
1783 DisplayChangeListener *dcl;
1785 if (!qemu_console_is_visible(con)) {
1786 return;
1788 QLIST_FOREACH(dcl, &s->listeners, next) {
1789 if (con != (dcl->con ? dcl->con : active_console)) {
1790 continue;
1792 if (dcl->ops->dpy_text_update) {
1793 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1798 void dpy_text_resize(QemuConsole *con, int w, int h)
1800 DisplayState *s = con->ds;
1801 DisplayChangeListener *dcl;
1803 if (!qemu_console_is_visible(con)) {
1804 return;
1806 QLIST_FOREACH(dcl, &s->listeners, next) {
1807 if (con != (dcl->con ? dcl->con : active_console)) {
1808 continue;
1810 if (dcl->ops->dpy_text_resize) {
1811 dcl->ops->dpy_text_resize(dcl, w, h);
1816 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1818 DisplayState *s = con->ds;
1819 DisplayChangeListener *dcl;
1821 con->cursor_x = x;
1822 con->cursor_y = y;
1823 con->cursor_on = on;
1824 if (!qemu_console_is_visible(con)) {
1825 return;
1827 QLIST_FOREACH(dcl, &s->listeners, next) {
1828 if (con != (dcl->con ? dcl->con : active_console)) {
1829 continue;
1831 if (dcl->ops->dpy_mouse_set) {
1832 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1837 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1839 DisplayState *s = con->ds;
1840 DisplayChangeListener *dcl;
1842 cursor_unref(con->cursor);
1843 con->cursor = cursor_ref(cursor);
1844 if (!qemu_console_is_visible(con)) {
1845 return;
1847 QLIST_FOREACH(dcl, &s->listeners, next) {
1848 if (con != (dcl->con ? dcl->con : active_console)) {
1849 continue;
1851 if (dcl->ops->dpy_cursor_define) {
1852 dcl->ops->dpy_cursor_define(dcl, cursor);
1857 bool dpy_cursor_define_supported(QemuConsole *con)
1859 DisplayState *s = con->ds;
1860 DisplayChangeListener *dcl;
1862 QLIST_FOREACH(dcl, &s->listeners, next) {
1863 if (dcl->ops->dpy_cursor_define) {
1864 return true;
1867 return false;
1870 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1871 struct QEMUGLParams *qparams)
1873 assert(con->gl);
1874 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1877 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1879 assert(con->gl);
1880 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1883 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1885 assert(con->gl);
1886 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1889 void dpy_gl_scanout_disable(QemuConsole *con)
1891 DisplayState *s = con->ds;
1892 DisplayChangeListener *dcl;
1894 if (con->scanout.kind != SCANOUT_SURFACE) {
1895 con->scanout.kind = SCANOUT_NONE;
1897 QLIST_FOREACH(dcl, &s->listeners, next) {
1898 if (con != (dcl->con ? dcl->con : active_console)) {
1899 continue;
1901 if (dcl->ops->dpy_gl_scanout_disable) {
1902 dcl->ops->dpy_gl_scanout_disable(dcl);
1907 void dpy_gl_scanout_texture(QemuConsole *con,
1908 uint32_t backing_id,
1909 bool backing_y_0_top,
1910 uint32_t backing_width,
1911 uint32_t backing_height,
1912 uint32_t x, uint32_t y,
1913 uint32_t width, uint32_t height,
1914 void *d3d_tex2d)
1916 DisplayState *s = con->ds;
1917 DisplayChangeListener *dcl;
1919 con->scanout.kind = SCANOUT_TEXTURE;
1920 con->scanout.texture = (ScanoutTexture) {
1921 backing_id, backing_y_0_top, backing_width, backing_height,
1922 x, y, width, height, d3d_tex2d,
1924 QLIST_FOREACH(dcl, &s->listeners, next) {
1925 if (con != (dcl->con ? dcl->con : active_console)) {
1926 continue;
1928 if (dcl->ops->dpy_gl_scanout_texture) {
1929 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1930 backing_y_0_top,
1931 backing_width, backing_height,
1932 x, y, width, height,
1933 d3d_tex2d);
1938 void dpy_gl_scanout_dmabuf(QemuConsole *con,
1939 QemuDmaBuf *dmabuf)
1941 DisplayState *s = con->ds;
1942 DisplayChangeListener *dcl;
1944 con->scanout.kind = SCANOUT_DMABUF;
1945 con->scanout.dmabuf = dmabuf;
1946 QLIST_FOREACH(dcl, &s->listeners, next) {
1947 if (con != (dcl->con ? dcl->con : active_console)) {
1948 continue;
1950 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1951 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1956 void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1957 bool have_hot, uint32_t hot_x, uint32_t hot_y)
1959 DisplayState *s = con->ds;
1960 DisplayChangeListener *dcl;
1962 QLIST_FOREACH(dcl, &s->listeners, next) {
1963 if (con != (dcl->con ? dcl->con : active_console)) {
1964 continue;
1966 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1967 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
1968 have_hot, hot_x, hot_y);
1973 void dpy_gl_cursor_position(QemuConsole *con,
1974 uint32_t pos_x, uint32_t pos_y)
1976 DisplayState *s = con->ds;
1977 DisplayChangeListener *dcl;
1979 QLIST_FOREACH(dcl, &s->listeners, next) {
1980 if (con != (dcl->con ? dcl->con : active_console)) {
1981 continue;
1983 if (dcl->ops->dpy_gl_cursor_position) {
1984 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1989 void dpy_gl_release_dmabuf(QemuConsole *con,
1990 QemuDmaBuf *dmabuf)
1992 DisplayState *s = con->ds;
1993 DisplayChangeListener *dcl;
1995 QLIST_FOREACH(dcl, &s->listeners, next) {
1996 if (con != (dcl->con ? dcl->con : active_console)) {
1997 continue;
1999 if (dcl->ops->dpy_gl_release_dmabuf) {
2000 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
2005 void dpy_gl_update(QemuConsole *con,
2006 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
2008 DisplayState *s = con->ds;
2009 DisplayChangeListener *dcl;
2011 assert(con->gl);
2013 graphic_hw_gl_block(con, true);
2014 QLIST_FOREACH(dcl, &s->listeners, next) {
2015 if (con != (dcl->con ? dcl->con : active_console)) {
2016 continue;
2018 if (dcl->ops->dpy_gl_update) {
2019 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
2022 graphic_hw_gl_block(con, false);
2025 /***********************************************************/
2026 /* register display */
2028 /* console.c internal use only */
2029 static DisplayState *get_alloc_displaystate(void)
2031 if (!display_state) {
2032 display_state = g_new0(DisplayState, 1);
2033 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2034 text_console_update_cursor, NULL);
2036 return display_state;
2040 * Called by main(), after creating QemuConsoles
2041 * and before initializing ui (sdl/vnc/...).
2043 DisplayState *init_displaystate(void)
2045 gchar *name;
2046 QemuConsole *con;
2048 QTAILQ_FOREACH(con, &consoles, next) {
2049 /* Hook up into the qom tree here (not in new_console()), once
2050 * all QemuConsoles are created and the order / numbering
2051 * doesn't change any more */
2052 name = g_strdup_printf("console[%d]", con->index);
2053 object_property_add_child(container_get(object_get_root(), "/backend"),
2054 name, OBJECT(con));
2055 g_free(name);
2058 return display_state;
2061 void graphic_console_set_hwops(QemuConsole *con,
2062 const GraphicHwOps *hw_ops,
2063 void *opaque)
2065 con->hw_ops = hw_ops;
2066 con->hw = opaque;
2069 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
2070 const GraphicHwOps *hw_ops,
2071 void *opaque)
2073 static const char noinit[] =
2074 "Guest has not initialized the display (yet).";
2075 int width = 640;
2076 int height = 480;
2077 QemuConsole *s;
2078 DisplaySurface *surface;
2080 s = qemu_console_lookup_unused();
2081 if (s) {
2082 trace_console_gfx_reuse(s->index);
2083 width = qemu_console_get_width(s, 0);
2084 height = qemu_console_get_height(s, 0);
2085 } else {
2086 trace_console_gfx_new();
2087 s = new_console(GRAPHIC_CONSOLE, head);
2088 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2089 dpy_set_ui_info_timer, s);
2091 graphic_console_set_hwops(s, hw_ops, opaque);
2092 if (dev) {
2093 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
2094 &error_abort);
2097 surface = qemu_create_placeholder_surface(width, height, noinit);
2098 dpy_gfx_replace_surface(s, surface);
2099 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2100 graphic_hw_gl_unblock_timer, s);
2101 return s;
2104 static const GraphicHwOps unused_ops = {
2105 /* no callbacks */
2108 void graphic_console_close(QemuConsole *con)
2110 static const char unplugged[] =
2111 "Guest display has been unplugged";
2112 DisplaySurface *surface;
2113 int width = qemu_console_get_width(con, 640);
2114 int height = qemu_console_get_height(con, 480);
2116 trace_console_gfx_close(con->index);
2117 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
2118 graphic_console_set_hwops(con, &unused_ops, NULL);
2120 if (con->gl) {
2121 dpy_gl_scanout_disable(con);
2123 surface = qemu_create_placeholder_surface(width, height, unplugged);
2124 dpy_gfx_replace_surface(con, surface);
2127 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
2129 QemuConsole *con;
2131 QTAILQ_FOREACH(con, &consoles, next) {
2132 if (con->index == index) {
2133 return con;
2136 return NULL;
2139 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
2141 QemuConsole *con;
2142 Object *obj;
2143 uint32_t h;
2145 QTAILQ_FOREACH(con, &consoles, next) {
2146 obj = object_property_get_link(OBJECT(con),
2147 "device", &error_abort);
2148 if (DEVICE(obj) != dev) {
2149 continue;
2151 h = object_property_get_uint(OBJECT(con),
2152 "head", &error_abort);
2153 if (h != head) {
2154 continue;
2156 return con;
2158 return NULL;
2161 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
2162 uint32_t head, Error **errp)
2164 DeviceState *dev;
2165 QemuConsole *con;
2167 dev = qdev_find_recursive(sysbus_get_default(), device_id);
2168 if (dev == NULL) {
2169 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2170 "Device '%s' not found", device_id);
2171 return NULL;
2174 con = qemu_console_lookup_by_device(dev, head);
2175 if (con == NULL) {
2176 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
2177 device_id, head);
2178 return NULL;
2181 return con;
2184 QemuConsole *qemu_console_lookup_unused(void)
2186 QemuConsole *con;
2187 Object *obj;
2189 QTAILQ_FOREACH(con, &consoles, next) {
2190 if (con->hw_ops != &unused_ops) {
2191 continue;
2193 obj = object_property_get_link(OBJECT(con),
2194 "device", &error_abort);
2195 if (obj != NULL) {
2196 continue;
2198 return con;
2200 return NULL;
2203 QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
2205 if (con == NULL) {
2206 con = active_console;
2208 return con ? con->cursor : NULL;
2211 bool qemu_console_is_visible(QemuConsole *con)
2213 return (con == active_console) || (con->dcls > 0);
2216 bool qemu_console_is_graphic(QemuConsole *con)
2218 if (con == NULL) {
2219 con = active_console;
2221 return con && (con->console_type == GRAPHIC_CONSOLE);
2224 bool qemu_console_is_fixedsize(QemuConsole *con)
2226 if (con == NULL) {
2227 con = active_console;
2229 return con && (con->console_type != TEXT_CONSOLE);
2232 bool qemu_console_is_gl_blocked(QemuConsole *con)
2234 assert(con != NULL);
2235 return con->gl_block;
2238 bool qemu_console_is_multihead(DeviceState *dev)
2240 QemuConsole *con;
2241 Object *obj;
2242 uint32_t f = 0xffffffff;
2243 uint32_t h;
2245 QTAILQ_FOREACH(con, &consoles, next) {
2246 obj = object_property_get_link(OBJECT(con),
2247 "device", &error_abort);
2248 if (DEVICE(obj) != dev) {
2249 continue;
2252 h = object_property_get_uint(OBJECT(con),
2253 "head", &error_abort);
2254 if (f == 0xffffffff) {
2255 f = h;
2256 } else if (h != f) {
2257 return true;
2260 return false;
2263 char *qemu_console_get_label(QemuConsole *con)
2265 if (con->console_type == GRAPHIC_CONSOLE) {
2266 if (con->device) {
2267 DeviceState *dev;
2268 bool multihead;
2270 dev = DEVICE(con->device);
2271 multihead = qemu_console_is_multihead(dev);
2272 if (multihead) {
2273 return g_strdup_printf("%s.%d", dev->id ?
2274 dev->id :
2275 object_get_typename(con->device),
2276 con->head);
2277 } else {
2278 return g_strdup_printf("%s", dev->id ?
2279 dev->id :
2280 object_get_typename(con->device));
2283 return g_strdup("VGA");
2284 } else {
2285 if (con->chr && con->chr->label) {
2286 return g_strdup(con->chr->label);
2288 return g_strdup_printf("vc%d", con->index);
2292 int qemu_console_get_index(QemuConsole *con)
2294 if (con == NULL) {
2295 con = active_console;
2297 return con ? con->index : -1;
2300 uint32_t qemu_console_get_head(QemuConsole *con)
2302 if (con == NULL) {
2303 con = active_console;
2305 return con ? con->head : -1;
2308 int qemu_console_get_width(QemuConsole *con, int fallback)
2310 if (con == NULL) {
2311 con = active_console;
2313 if (con == NULL) {
2314 return fallback;
2316 switch (con->scanout.kind) {
2317 case SCANOUT_DMABUF:
2318 return con->scanout.dmabuf->width;
2319 case SCANOUT_TEXTURE:
2320 return con->scanout.texture.width;
2321 case SCANOUT_SURFACE:
2322 return surface_width(con->surface);
2323 default:
2324 return fallback;
2328 int qemu_console_get_height(QemuConsole *con, int fallback)
2330 if (con == NULL) {
2331 con = active_console;
2333 if (con == NULL) {
2334 return fallback;
2336 switch (con->scanout.kind) {
2337 case SCANOUT_DMABUF:
2338 return con->scanout.dmabuf->height;
2339 case SCANOUT_TEXTURE:
2340 return con->scanout.texture.height;
2341 case SCANOUT_SURFACE:
2342 return surface_height(con->surface);
2343 default:
2344 return fallback;
2348 static void vc_chr_accept_input(Chardev *chr)
2350 VCChardev *drv = VC_CHARDEV(chr);
2351 QemuConsole *s = drv->console;
2353 kbd_send_chars(s);
2356 static void vc_chr_set_echo(Chardev *chr, bool echo)
2358 VCChardev *drv = VC_CHARDEV(chr);
2359 QemuConsole *s = drv->console;
2361 s->echo = echo;
2364 static void text_console_update_cursor_timer(void)
2366 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2367 + CONSOLE_CURSOR_PERIOD / 2);
2370 static void text_console_update_cursor(void *opaque)
2372 QemuConsole *s;
2373 int count = 0;
2375 cursor_visible_phase = !cursor_visible_phase;
2377 QTAILQ_FOREACH(s, &consoles, next) {
2378 if (qemu_console_is_graphic(s) ||
2379 !qemu_console_is_visible(s)) {
2380 continue;
2382 count++;
2383 graphic_hw_invalidate(s);
2386 if (count) {
2387 text_console_update_cursor_timer();
2391 static const GraphicHwOps text_console_ops = {
2392 .invalidate = text_console_invalidate,
2393 .text_update = text_console_update,
2396 static void text_console_do_init(Chardev *chr)
2398 VCChardev *drv = VC_CHARDEV(chr);
2399 QemuConsole *s = drv->console;
2400 int g_width = 80 * FONT_WIDTH;
2401 int g_height = 24 * FONT_HEIGHT;
2403 fifo8_create(&s->out_fifo, 16);
2405 s->y_displayed = 0;
2406 s->y_base = 0;
2407 s->total_height = DEFAULT_BACKSCROLL;
2408 s->x = 0;
2409 s->y = 0;
2410 if (s->scanout.kind != SCANOUT_SURFACE) {
2411 if (active_console && active_console->scanout.kind == SCANOUT_SURFACE) {
2412 g_width = qemu_console_get_width(active_console, g_width);
2413 g_height = qemu_console_get_height(active_console, g_height);
2415 s->surface = qemu_create_displaysurface(g_width, g_height);
2416 s->scanout.kind = SCANOUT_SURFACE;
2419 s->hw_ops = &text_console_ops;
2420 s->hw = s;
2422 /* Set text attribute defaults */
2423 s->t_attrib_default.bold = 0;
2424 s->t_attrib_default.uline = 0;
2425 s->t_attrib_default.blink = 0;
2426 s->t_attrib_default.invers = 0;
2427 s->t_attrib_default.unvisible = 0;
2428 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2429 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2430 /* set current text attributes to default */
2431 s->t_attrib = s->t_attrib_default;
2432 text_console_resize(s);
2434 if (chr->label) {
2435 char *msg;
2437 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2438 msg = g_strdup_printf("%s console\r\n", chr->label);
2439 qemu_chr_write(chr, (uint8_t *)msg, strlen(msg), true);
2440 g_free(msg);
2441 s->t_attrib = s->t_attrib_default;
2444 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
2447 static void vc_chr_open(Chardev *chr,
2448 ChardevBackend *backend,
2449 bool *be_opened,
2450 Error **errp)
2452 ChardevVC *vc = backend->u.vc.data;
2453 VCChardev *drv = VC_CHARDEV(chr);
2454 QemuConsole *s;
2455 unsigned width = 0;
2456 unsigned height = 0;
2458 if (vc->has_width) {
2459 width = vc->width;
2460 } else if (vc->has_cols) {
2461 width = vc->cols * FONT_WIDTH;
2464 if (vc->has_height) {
2465 height = vc->height;
2466 } else if (vc->has_rows) {
2467 height = vc->rows * FONT_HEIGHT;
2470 trace_console_txt_new(width, height);
2471 if (width == 0 || height == 0) {
2472 s = new_console(TEXT_CONSOLE, 0);
2473 } else {
2474 s = new_console(TEXT_CONSOLE_FIXED_SIZE, 0);
2475 s->scanout.kind = SCANOUT_SURFACE;
2476 s->surface = qemu_create_displaysurface(width, height);
2479 s->chr = chr;
2480 drv->console = s;
2482 text_console_do_init(chr);
2484 /* console/chardev init sometimes completes elsewhere in a 2nd
2485 * stage, so defer OPENED events until they are fully initialized
2487 *be_opened = false;
2490 void qemu_console_resize(QemuConsole *s, int width, int height)
2492 DisplaySurface *surface = qemu_console_surface(s);
2494 assert(s->console_type == GRAPHIC_CONSOLE);
2496 if ((s->scanout.kind != SCANOUT_SURFACE ||
2497 (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
2498 qemu_console_get_width(s, -1) == width &&
2499 qemu_console_get_height(s, -1) == height) {
2500 return;
2503 surface = qemu_create_displaysurface(width, height);
2504 dpy_gfx_replace_surface(s, surface);
2507 DisplaySurface *qemu_console_surface(QemuConsole *console)
2509 switch (console->scanout.kind) {
2510 case SCANOUT_SURFACE:
2511 return console->surface;
2512 default:
2513 return NULL;
2517 PixelFormat qemu_default_pixelformat(int bpp)
2519 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2520 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2521 return pf;
2524 static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2526 void qemu_display_register(QemuDisplay *ui)
2528 assert(ui->type < DISPLAY_TYPE__MAX);
2529 dpys[ui->type] = ui;
2532 bool qemu_display_find_default(DisplayOptions *opts)
2534 static DisplayType prio[] = {
2535 #if defined(CONFIG_GTK)
2536 DISPLAY_TYPE_GTK,
2537 #endif
2538 #if defined(CONFIG_SDL)
2539 DISPLAY_TYPE_SDL,
2540 #endif
2541 #if defined(CONFIG_COCOA)
2542 DISPLAY_TYPE_COCOA
2543 #endif
2545 int i;
2547 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
2548 if (dpys[prio[i]] == NULL) {
2549 Error *local_err = NULL;
2550 int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
2551 if (rv < 0) {
2552 error_report_err(local_err);
2555 if (dpys[prio[i]] == NULL) {
2556 continue;
2558 opts->type = prio[i];
2559 return true;
2561 return false;
2564 void qemu_display_early_init(DisplayOptions *opts)
2566 assert(opts->type < DISPLAY_TYPE__MAX);
2567 if (opts->type == DISPLAY_TYPE_NONE) {
2568 return;
2570 if (dpys[opts->type] == NULL) {
2571 Error *local_err = NULL;
2572 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
2573 if (rv < 0) {
2574 error_report_err(local_err);
2577 if (dpys[opts->type] == NULL) {
2578 error_report("Display '%s' is not available.",
2579 DisplayType_str(opts->type));
2580 exit(1);
2582 if (dpys[opts->type]->early_init) {
2583 dpys[opts->type]->early_init(opts);
2587 void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2589 assert(opts->type < DISPLAY_TYPE__MAX);
2590 if (opts->type == DISPLAY_TYPE_NONE) {
2591 return;
2593 assert(dpys[opts->type] != NULL);
2594 dpys[opts->type]->init(ds, opts);
2597 void qemu_display_help(void)
2599 int idx;
2601 printf("Available display backend types:\n");
2602 printf("none\n");
2603 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
2604 if (!dpys[idx]) {
2605 Error *local_err = NULL;
2606 int rv = ui_module_load(DisplayType_str(idx), &local_err);
2607 if (rv < 0) {
2608 error_report_err(local_err);
2611 if (dpys[idx]) {
2612 printf("%s\n", DisplayType_str(dpys[idx]->type));
2617 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
2619 int val;
2620 ChardevVC *vc;
2622 backend->type = CHARDEV_BACKEND_KIND_VC;
2623 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2624 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2626 val = qemu_opt_get_number(opts, "width", 0);
2627 if (val != 0) {
2628 vc->has_width = true;
2629 vc->width = val;
2632 val = qemu_opt_get_number(opts, "height", 0);
2633 if (val != 0) {
2634 vc->has_height = true;
2635 vc->height = val;
2638 val = qemu_opt_get_number(opts, "cols", 0);
2639 if (val != 0) {
2640 vc->has_cols = true;
2641 vc->cols = val;
2644 val = qemu_opt_get_number(opts, "rows", 0);
2645 if (val != 0) {
2646 vc->has_rows = true;
2647 vc->rows = val;
2651 static const TypeInfo qemu_console_info = {
2652 .name = TYPE_QEMU_CONSOLE,
2653 .parent = TYPE_OBJECT,
2654 .instance_size = sizeof(QemuConsole),
2655 .class_size = sizeof(QemuConsoleClass),
2658 static void char_vc_class_init(ObjectClass *oc, void *data)
2660 ChardevClass *cc = CHARDEV_CLASS(oc);
2662 cc->parse = qemu_chr_parse_vc;
2663 cc->open = vc_chr_open;
2664 cc->chr_write = vc_chr_write;
2665 cc->chr_accept_input = vc_chr_accept_input;
2666 cc->chr_set_echo = vc_chr_set_echo;
2669 static const TypeInfo char_vc_type_info = {
2670 .name = TYPE_CHARDEV_VC,
2671 .parent = TYPE_CHARDEV,
2672 .instance_size = sizeof(VCChardev),
2673 .class_init = char_vc_class_init,
2676 void qemu_console_early_init(void)
2678 /* set the default vc driver */
2679 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2680 type_register(&char_vc_type_info);
2684 static void register_types(void)
2686 type_register_static(&qemu_console_info);
2689 type_init(register_types);