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
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/fifo8.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/timer.h"
34 #include "chardev/char-fe.h"
36 #include "exec/memory.h"
37 #include "io/channel-file.h"
38 #include "qom/object.h"
40 #define DEFAULT_BACKSCROLL 512
41 #define CONSOLE_CURSOR_PERIOD 500
43 typedef struct TextAttributes {
53 typedef struct TextCell {
55 TextAttributes t_attrib;
58 #define MAX_ESC_PARAMS 3
69 TEXT_CONSOLE_FIXED_SIZE
76 console_type_t console_type;
78 DisplaySurface *surface;
80 DisplayChangeListener *gl;
84 /* Graphic console state. */
89 const GraphicHwOps *hw_ops;
92 /* Text console state */
96 int backscroll_height;
101 TextAttributes t_attrib_default; /* default text attributes */
102 TextAttributes t_attrib; /* currently active text attributes */
104 int text_x[2], text_y[2], cursor_invalidate;
113 int esc_params[MAX_ESC_PARAMS];
117 /* fifo for key pressed */
121 QTAILQ_ENTRY(QemuConsole) next;
124 struct DisplayState {
125 QEMUTimer *gui_timer;
126 uint64_t last_update;
127 uint64_t update_interval;
132 QLIST_HEAD(, DisplayChangeListener) listeners;
135 static DisplayState *display_state;
136 static QemuConsole *active_console;
137 static QTAILQ_HEAD(, QemuConsole) consoles =
138 QTAILQ_HEAD_INITIALIZER(consoles);
139 static bool cursor_visible_phase;
140 static QEMUTimer *cursor_timer;
142 static void text_console_do_init(Chardev *chr, DisplayState *ds);
143 static void dpy_refresh(DisplayState *s);
144 static DisplayState *get_alloc_displaystate(void);
145 static void text_console_update_cursor_timer(void);
146 static void text_console_update_cursor(void *opaque);
148 static void gui_update(void *opaque)
150 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
151 uint64_t dcl_interval;
152 DisplayState *ds = opaque;
153 DisplayChangeListener *dcl;
156 ds->refreshing = true;
158 ds->refreshing = false;
160 QLIST_FOREACH(dcl, &ds->listeners, next) {
161 dcl_interval = dcl->update_interval ?
162 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
163 if (interval > dcl_interval) {
164 interval = dcl_interval;
167 if (ds->update_interval != interval) {
168 ds->update_interval = interval;
169 QTAILQ_FOREACH(con, &consoles, next) {
170 if (con->hw_ops->update_interval) {
171 con->hw_ops->update_interval(con->hw, interval);
174 trace_console_refresh(interval);
176 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
177 timer_mod(ds->gui_timer, ds->last_update + interval);
180 static void gui_setup_refresh(DisplayState *ds)
182 DisplayChangeListener *dcl;
183 bool need_timer = false;
184 bool have_gfx = false;
185 bool have_text = false;
187 QLIST_FOREACH(dcl, &ds->listeners, next) {
188 if (dcl->ops->dpy_refresh != NULL) {
191 if (dcl->ops->dpy_gfx_update != NULL) {
194 if (dcl->ops->dpy_text_update != NULL) {
199 if (need_timer && ds->gui_timer == NULL) {
200 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
201 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
203 if (!need_timer && ds->gui_timer != NULL) {
204 timer_free(ds->gui_timer);
205 ds->gui_timer = NULL;
208 ds->have_gfx = have_gfx;
209 ds->have_text = have_text;
212 void graphic_hw_update_done(QemuConsole *con)
215 qemu_co_queue_restart_all(&con->dump_queue);
219 void graphic_hw_update(QemuConsole *con)
222 con = con ? con : active_console;
226 if (con->hw_ops->gfx_update) {
227 con->hw_ops->gfx_update(con->hw);
228 async = con->hw_ops->gfx_update_async;
231 graphic_hw_update_done(con);
235 void graphic_hw_gl_block(QemuConsole *con, bool block)
239 con->gl_block = block;
240 if (con->hw_ops->gl_block) {
241 con->hw_ops->gl_block(con->hw, block);
245 void graphic_hw_gl_flushed(QemuConsole *con)
249 if (con->hw_ops->gl_flushed) {
250 con->hw_ops->gl_flushed(con->hw);
254 int qemu_console_get_window_id(QemuConsole *con)
256 return con->window_id;
259 void qemu_console_set_window_id(QemuConsole *con, int window_id)
261 con->window_id = window_id;
264 void graphic_hw_invalidate(QemuConsole *con)
267 con = active_console;
269 if (con && con->hw_ops->invalidate) {
270 con->hw_ops->invalidate(con->hw);
274 static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
276 int width = pixman_image_get_width(image);
277 int height = pixman_image_get_height(image);
278 g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
279 g_autofree char *header = NULL;
280 g_autoptr(pixman_image_t) linebuf = NULL;
283 trace_ppm_save(fd, image);
285 header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
286 if (qio_channel_write_all(QIO_CHANNEL(ioc),
287 header, strlen(header), errp) < 0) {
291 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
292 for (y = 0; y < height; y++) {
293 qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
294 if (qio_channel_write_all(QIO_CHANNEL(ioc),
295 (char *)pixman_image_get_data(linebuf),
296 pixman_image_get_stride(linebuf), errp) < 0) {
304 static void graphic_hw_update_bh(void *con)
306 graphic_hw_update(con);
309 /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
311 qmp_screendump(const char *filename, bool has_device, const char *device,
312 bool has_head, int64_t head, Error **errp)
314 g_autoptr(pixman_image_t) image = NULL;
316 DisplaySurface *surface;
320 con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
327 error_setg(errp, "'head' must be specified together with 'device'");
330 con = qemu_console_lookup_by_index(0);
332 error_setg(errp, "There is no console to take a screendump from");
337 if (qemu_co_queue_empty(&con->dump_queue)) {
338 /* Defer the update, it will restart the pending coroutines */
339 aio_bh_schedule_oneshot(qemu_get_aio_context(),
340 graphic_hw_update_bh, con);
342 qemu_co_queue_wait(&con->dump_queue, NULL);
345 * All pending coroutines are woken up, while the BQL is held. No
346 * further graphic update are possible until it is released. Take
347 * an image ref before that.
349 surface = qemu_console_surface(con);
351 error_setg(errp, "no surface");
354 image = pixman_image_ref(surface->image);
356 fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
358 error_setg(errp, "failed to open file '%s': %s", filename,
364 * The image content could potentially be updated as the coroutine
365 * yields and releases the BQL. It could produce corrupted dump, but
366 * it should be otherwise safe.
368 if (!ppm_save(fd, image, errp)) {
369 qemu_unlink(filename);
373 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
376 con = active_console;
378 if (con && con->hw_ops->text_update) {
379 con->hw_ops->text_update(con->hw, chardata);
383 static void vga_fill_rect(QemuConsole *con,
384 int posx, int posy, int width, int height,
385 pixman_color_t color)
387 DisplaySurface *surface = qemu_console_surface(con);
388 pixman_rectangle16_t rect = {
389 .x = posx, .y = posy, .width = width, .height = height
392 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
396 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
397 static void vga_bitblt(QemuConsole *con,
398 int xs, int ys, int xd, int yd, int w, int h)
400 DisplaySurface *surface = qemu_console_surface(con);
402 pixman_image_composite(PIXMAN_OP_SRC,
403 surface->image, NULL, surface->image,
404 xs, ys, 0, 0, xd, yd, w, h);
407 /***********************************************************/
408 /* basic char display */
410 #define FONT_HEIGHT 16
415 #define QEMU_RGB(r, g, b) \
416 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
418 static const pixman_color_t color_table_rgb[2][8] = {
420 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
421 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
422 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
423 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
424 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
425 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
426 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
427 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
430 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
431 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
432 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
433 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
434 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
435 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
436 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
437 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
441 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
442 TextAttributes *t_attrib)
444 static pixman_image_t *glyphs[256];
445 DisplaySurface *surface = qemu_console_surface(s);
446 pixman_color_t fgcol, bgcol;
448 if (t_attrib->invers) {
449 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
450 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
452 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
453 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
457 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
459 qemu_pixman_glyph_render(glyphs[ch], surface->image,
460 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
463 static void text_console_resize(QemuConsole *s)
465 TextCell *cells, *c, *c1;
466 int w1, x, y, last_width;
468 last_width = s->width;
469 s->width = surface_width(s->surface) / FONT_WIDTH;
470 s->height = surface_height(s->surface) / FONT_HEIGHT;
476 cells = g_new(TextCell, s->width * s->total_height + 1);
477 for(y = 0; y < s->total_height; y++) {
478 c = &cells[y * s->width];
480 c1 = &s->cells[y * last_width];
481 for(x = 0; x < w1; x++) {
485 for(x = w1; x < s->width; x++) {
487 c->t_attrib = s->t_attrib_default;
495 static inline void text_update_xy(QemuConsole *s, int x, int y)
497 s->text_x[0] = MIN(s->text_x[0], x);
498 s->text_x[1] = MAX(s->text_x[1], x);
499 s->text_y[0] = MIN(s->text_y[0], y);
500 s->text_y[1] = MAX(s->text_y[1], y);
503 static void invalidate_xy(QemuConsole *s, int x, int y)
505 if (!qemu_console_is_visible(s)) {
508 if (s->update_x0 > x * FONT_WIDTH)
509 s->update_x0 = x * FONT_WIDTH;
510 if (s->update_y0 > y * FONT_HEIGHT)
511 s->update_y0 = y * FONT_HEIGHT;
512 if (s->update_x1 < (x + 1) * FONT_WIDTH)
513 s->update_x1 = (x + 1) * FONT_WIDTH;
514 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
515 s->update_y1 = (y + 1) * FONT_HEIGHT;
518 static void update_xy(QemuConsole *s, int x, int y)
523 if (s->ds->have_text) {
524 text_update_xy(s, x, y);
527 y1 = (s->y_base + y) % s->total_height;
528 y2 = y1 - s->y_displayed;
530 y2 += s->total_height;
532 if (y2 < s->height) {
536 c = &s->cells[y1 * s->width + x];
537 vga_putcharxy(s, x, y2, c->ch,
539 invalidate_xy(s, x, y2);
543 static void console_show_cursor(QemuConsole *s, int show)
549 if (s->ds->have_text) {
550 s->cursor_invalidate = 1;
556 y1 = (s->y_base + s->y) % s->total_height;
557 y = y1 - s->y_displayed;
559 y += s->total_height;
562 c = &s->cells[y1 * s->width + x];
563 if (show && cursor_visible_phase) {
564 TextAttributes t_attrib = s->t_attrib_default;
565 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
566 vga_putcharxy(s, x, y, c->ch, &t_attrib);
568 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
570 invalidate_xy(s, x, y);
574 static void console_refresh(QemuConsole *s)
576 DisplaySurface *surface = qemu_console_surface(s);
580 if (s->ds->have_text) {
583 s->text_x[1] = s->width - 1;
584 s->text_y[1] = s->height - 1;
585 s->cursor_invalidate = 1;
588 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
589 color_table_rgb[0][QEMU_COLOR_BLACK]);
591 for (y = 0; y < s->height; y++) {
592 c = s->cells + y1 * s->width;
593 for (x = 0; x < s->width; x++) {
594 vga_putcharxy(s, x, y, c->ch,
598 if (++y1 == s->total_height) {
602 console_show_cursor(s, 1);
603 dpy_gfx_update(s, 0, 0,
604 surface_width(surface), surface_height(surface));
607 static void console_scroll(QemuConsole *s, int ydelta)
612 for(i = 0; i < ydelta; i++) {
613 if (s->y_displayed == s->y_base)
615 if (++s->y_displayed == s->total_height)
620 i = s->backscroll_height;
621 if (i > s->total_height - s->height)
622 i = s->total_height - s->height;
625 y1 += s->total_height;
626 for(i = 0; i < ydelta; i++) {
627 if (s->y_displayed == y1)
629 if (--s->y_displayed < 0)
630 s->y_displayed = s->total_height - 1;
636 static void console_put_lf(QemuConsole *s)
642 if (s->y >= s->height) {
643 s->y = s->height - 1;
645 if (s->y_displayed == s->y_base) {
646 if (++s->y_displayed == s->total_height)
649 if (++s->y_base == s->total_height)
651 if (s->backscroll_height < s->total_height)
652 s->backscroll_height++;
653 y1 = (s->y_base + s->height - 1) % s->total_height;
654 c = &s->cells[y1 * s->width];
655 for(x = 0; x < s->width; x++) {
657 c->t_attrib = s->t_attrib_default;
660 if (s->y_displayed == s->y_base) {
661 if (s->ds->have_text) {
664 s->text_x[1] = s->width - 1;
665 s->text_y[1] = s->height - 1;
668 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
669 s->width * FONT_WIDTH,
670 (s->height - 1) * FONT_HEIGHT);
671 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
672 s->width * FONT_WIDTH, FONT_HEIGHT,
673 color_table_rgb[0][s->t_attrib_default.bgcol]);
676 s->update_x1 = s->width * FONT_WIDTH;
677 s->update_y1 = s->height * FONT_HEIGHT;
682 /* Set console attributes depending on the current escape codes.
683 * NOTE: I know this code is not very efficient (checking every color for it
684 * self) but it is more readable and better maintainable.
686 static void console_handle_escape(QemuConsole *s)
690 for (i=0; i<s->nb_esc_params; i++) {
691 switch (s->esc_params[i]) {
692 case 0: /* reset all console attributes to default */
693 s->t_attrib = s->t_attrib_default;
696 s->t_attrib.bold = 1;
699 s->t_attrib.uline = 1;
702 s->t_attrib.blink = 1;
705 s->t_attrib.invers = 1;
708 s->t_attrib.unvisible = 1;
711 s->t_attrib.bold = 0;
714 s->t_attrib.uline = 0;
717 s->t_attrib.blink = 0;
720 s->t_attrib.invers = 0;
723 s->t_attrib.unvisible = 0;
725 /* set foreground color */
727 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
730 s->t_attrib.fgcol = QEMU_COLOR_RED;
733 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
736 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
739 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
742 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
745 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
748 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
750 /* set background color */
752 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
755 s->t_attrib.bgcol = QEMU_COLOR_RED;
758 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
761 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
764 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
767 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
770 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
773 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
779 static void console_clear_xy(QemuConsole *s, int x, int y)
781 int y1 = (s->y_base + y) % s->total_height;
785 TextCell *c = &s->cells[y1 * s->width + x];
787 c->t_attrib = s->t_attrib_default;
791 static void console_put_one(QemuConsole *s, int ch)
795 if (s->x >= s->width) {
800 y1 = (s->y_base + s->y) % s->total_height;
801 c = &s->cells[y1 * s->width + s->x];
803 c->t_attrib = s->t_attrib;
804 update_xy(s, s->x, s->y);
808 static void console_respond_str(QemuConsole *s, const char *buf)
811 console_put_one(s, *buf);
816 /* set cursor, checking bounds */
817 static void set_cursor(QemuConsole *s, int x, int y)
825 if (y >= s->height) {
836 static void console_putchar(QemuConsole *s, int ch)
845 case '\r': /* carriage return */
848 case '\n': /* newline */
851 case '\b': /* backspace */
855 case '\t': /* tabspace */
856 if (s->x + (8 - (s->x % 8)) > s->width) {
860 s->x = s->x + (8 - (s->x % 8));
863 case '\a': /* alert aka. bell */
864 /* TODO: has to be implemented */
867 /* SI (shift in), character set 0 (ignored) */
870 /* SO (shift out), character set 1 (ignored) */
872 case 27: /* esc (introducing an escape sequence) */
873 s->state = TTY_STATE_ESC;
876 console_put_one(s, ch);
880 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
882 for(i=0;i<MAX_ESC_PARAMS;i++)
883 s->esc_params[i] = 0;
884 s->nb_esc_params = 0;
885 s->state = TTY_STATE_CSI;
887 s->state = TTY_STATE_NORM;
890 case TTY_STATE_CSI: /* handle escape sequence parameters */
891 if (ch >= '0' && ch <= '9') {
892 if (s->nb_esc_params < MAX_ESC_PARAMS) {
893 int *param = &s->esc_params[s->nb_esc_params];
894 int digit = (ch - '0');
896 *param = (*param <= (INT_MAX - digit) / 10) ?
897 *param * 10 + digit : INT_MAX;
900 if (s->nb_esc_params < MAX_ESC_PARAMS)
902 if (ch == ';' || ch == '?') {
905 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
906 ch, s->nb_esc_params);
907 s->state = TTY_STATE_NORM;
911 if (s->esc_params[0] == 0) {
912 s->esc_params[0] = 1;
914 set_cursor(s, s->x, s->y - s->esc_params[0]);
917 /* move cursor down */
918 if (s->esc_params[0] == 0) {
919 s->esc_params[0] = 1;
921 set_cursor(s, s->x, s->y + s->esc_params[0]);
924 /* move cursor right */
925 if (s->esc_params[0] == 0) {
926 s->esc_params[0] = 1;
928 set_cursor(s, s->x + s->esc_params[0], s->y);
931 /* move cursor left */
932 if (s->esc_params[0] == 0) {
933 s->esc_params[0] = 1;
935 set_cursor(s, s->x - s->esc_params[0], s->y);
938 /* move cursor to column */
939 set_cursor(s, s->esc_params[0] - 1, s->y);
943 /* move cursor to row, column */
944 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
947 switch (s->esc_params[0]) {
949 /* clear to end of screen */
950 for (y = s->y; y < s->height; y++) {
951 for (x = 0; x < s->width; x++) {
952 if (y == s->y && x < s->x) {
955 console_clear_xy(s, x, y);
960 /* clear from beginning of screen */
961 for (y = 0; y <= s->y; y++) {
962 for (x = 0; x < s->width; x++) {
963 if (y == s->y && x > s->x) {
966 console_clear_xy(s, x, y);
971 /* clear entire screen */
972 for (y = 0; y <= s->height; y++) {
973 for (x = 0; x < s->width; x++) {
974 console_clear_xy(s, x, y);
981 switch (s->esc_params[0]) {
984 for(x = s->x; x < s->width; x++) {
985 console_clear_xy(s, x, s->y);
989 /* clear from beginning of line */
990 for (x = 0; x <= s->x && x < s->width; x++) {
991 console_clear_xy(s, x, s->y);
995 /* clear entire line */
996 for(x = 0; x < s->width; x++) {
997 console_clear_xy(s, x, s->y);
1003 console_handle_escape(s);
1006 switch (s->esc_params[0]) {
1008 /* report console status (always succeed)*/
1009 console_respond_str(s, "\033[0n");
1012 /* report cursor position */
1013 sprintf(response, "\033[%d;%dR",
1014 (s->y_base + s->y) % s->total_height + 1,
1016 console_respond_str(s, response);
1021 /* save cursor position */
1026 /* restore cursor position */
1031 trace_console_putchar_unhandled(ch);
1039 void console_select(unsigned int index)
1041 DisplayChangeListener *dcl;
1044 trace_console_select(index);
1045 s = qemu_console_lookup_by_index(index);
1047 DisplayState *ds = s->ds;
1051 QLIST_FOREACH(dcl, &ds->listeners, next) {
1052 if (dcl->con != NULL) {
1055 if (dcl->ops->dpy_gfx_switch) {
1056 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1060 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1061 surface_height(s->surface));
1064 if (ds->have_text) {
1065 dpy_text_resize(s, s->width, s->height);
1067 text_console_update_cursor(NULL);
1073 QemuConsole *console;
1075 typedef struct VCChardev VCChardev;
1077 #define TYPE_CHARDEV_VC "chardev-vc"
1078 DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
1081 static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1083 VCChardev *drv = VC_CHARDEV(chr);
1084 QemuConsole *s = drv->console;
1091 s->update_x0 = s->width * FONT_WIDTH;
1092 s->update_y0 = s->height * FONT_HEIGHT;
1095 console_show_cursor(s, 0);
1096 for(i = 0; i < len; i++) {
1097 console_putchar(s, buf[i]);
1099 console_show_cursor(s, 1);
1100 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1101 dpy_gfx_update(s, s->update_x0, s->update_y0,
1102 s->update_x1 - s->update_x0,
1103 s->update_y1 - s->update_y0);
1108 static void kbd_send_chars(QemuConsole *s)
1110 uint32_t len, avail;
1112 len = qemu_chr_be_can_write(s->chr);
1113 avail = fifo8_num_used(&s->out_fifo);
1114 while (len > 0 && avail > 0) {
1118 buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
1119 qemu_chr_be_write(s->chr, (uint8_t *)buf, size);
1120 len = qemu_chr_be_can_write(s->chr);
1125 /* called when an ascii key is pressed */
1126 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1128 uint8_t buf[16], *q;
1133 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1137 case QEMU_KEY_CTRL_UP:
1138 console_scroll(s, -1);
1140 case QEMU_KEY_CTRL_DOWN:
1141 console_scroll(s, 1);
1143 case QEMU_KEY_CTRL_PAGEUP:
1144 console_scroll(s, -10);
1146 case QEMU_KEY_CTRL_PAGEDOWN:
1147 console_scroll(s, 10);
1150 /* convert the QEMU keysym to VT100 key string */
1152 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1155 c = keysym - 0xe100;
1157 *q++ = '0' + (c / 10);
1158 *q++ = '0' + (c % 10);
1160 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1163 *q++ = keysym & 0xff;
1164 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1165 vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
1171 vc_chr_write(s->chr, buf, q - buf);
1174 if (be && be->chr_read) {
1175 num_free = fifo8_num_free(&s->out_fifo);
1176 fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
1183 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1184 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1185 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1186 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1187 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1188 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1189 [Q_KEY_CODE_END] = QEMU_KEY_END,
1190 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1191 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1192 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1193 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1196 static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
1197 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
1198 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
1199 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
1200 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
1201 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
1202 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
1203 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
1204 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
1207 bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
1211 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
1215 kbd_put_keysym_console(s, keysym);
1219 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1223 for (i = 0; i < len && str[i]; i++) {
1224 kbd_put_keysym_console(s, str[i]);
1228 void kbd_put_keysym(int keysym)
1230 kbd_put_keysym_console(active_console, keysym);
1233 static void text_console_invalidate(void *opaque)
1235 QemuConsole *s = (QemuConsole *) opaque;
1237 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1238 text_console_resize(s);
1243 static void text_console_update(void *opaque, console_ch_t *chardata)
1245 QemuConsole *s = (QemuConsole *) opaque;
1248 if (s->text_x[0] <= s->text_x[1]) {
1249 src = (s->y_base + s->text_y[0]) * s->width;
1250 chardata += s->text_y[0] * s->width;
1251 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1252 for (j = 0; j < s->width; j++, src++) {
1253 console_write_ch(chardata ++,
1254 ATTR2CHTYPE(s->cells[src].ch,
1255 s->cells[src].t_attrib.fgcol,
1256 s->cells[src].t_attrib.bgcol,
1257 s->cells[src].t_attrib.bold));
1259 dpy_text_update(s, s->text_x[0], s->text_y[0],
1260 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1261 s->text_x[0] = s->width;
1262 s->text_y[0] = s->height;
1266 if (s->cursor_invalidate) {
1267 dpy_text_cursor(s, s->x, s->y);
1268 s->cursor_invalidate = 0;
1272 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1279 obj = object_new(TYPE_QEMU_CONSOLE);
1280 s = QEMU_CONSOLE(obj);
1281 qemu_co_queue_init(&s->dump_queue);
1283 object_property_add_link(obj, "device", TYPE_DEVICE,
1284 (Object **)&s->device,
1285 object_property_allow_set_link,
1286 OBJ_PROP_LINK_STRONG);
1287 object_property_add_uint32_ptr(obj, "head", &s->head,
1288 OBJ_PROP_FLAG_READ);
1290 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1291 (console_type == GRAPHIC_CONSOLE))) {
1295 s->console_type = console_type;
1298 if (QTAILQ_EMPTY(&consoles)) {
1300 QTAILQ_INSERT_TAIL(&consoles, s, next);
1301 } else if (console_type != GRAPHIC_CONSOLE || phase_check(PHASE_MACHINE_READY)) {
1302 QemuConsole *last = QTAILQ_LAST(&consoles);
1303 s->index = last->index + 1;
1304 QTAILQ_INSERT_TAIL(&consoles, s, next);
1307 * HACK: Put graphical consoles before text consoles.
1309 * Only do that for coldplugged devices. After initial device
1310 * initialization we will not renumber the consoles any more.
1312 QemuConsole *c = QTAILQ_FIRST(&consoles);
1314 while (QTAILQ_NEXT(c, next) != NULL &&
1315 c->console_type == GRAPHIC_CONSOLE) {
1316 c = QTAILQ_NEXT(c, next);
1318 if (c->console_type == GRAPHIC_CONSOLE) {
1319 /* have no text consoles */
1320 s->index = c->index + 1;
1321 QTAILQ_INSERT_AFTER(&consoles, c, s, next);
1323 s->index = c->index;
1324 QTAILQ_INSERT_BEFORE(c, s, next);
1325 /* renumber text consoles */
1326 for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
1334 DisplaySurface *qemu_create_displaysurface(int width, int height)
1336 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1338 trace_displaysurface_create(surface, width, height);
1339 surface->format = PIXMAN_x8r8g8b8;
1340 surface->image = pixman_image_create_bits(surface->format,
1343 assert(surface->image != NULL);
1344 surface->flags = QEMU_ALLOCATED_FLAG;
1349 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1350 pixman_format_code_t format,
1351 int linesize, uint8_t *data)
1353 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1355 trace_displaysurface_create_from(surface, width, height, format);
1356 surface->format = format;
1357 surface->image = pixman_image_create_bits(surface->format,
1359 (void *)data, linesize);
1360 assert(surface->image != NULL);
1365 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1367 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1369 trace_displaysurface_create_pixman(surface);
1370 surface->format = pixman_image_get_format(image);
1371 surface->image = pixman_image_ref(image);
1376 DisplaySurface *qemu_create_placeholder_surface(int w, int h,
1379 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1380 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1381 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1382 pixman_image_t *glyph;
1386 x = (w / FONT_WIDTH - len) / 2;
1387 y = (h / FONT_HEIGHT - 1) / 2;
1388 for (i = 0; i < len; i++) {
1389 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1390 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1391 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1392 qemu_pixman_image_unref(glyph);
1394 surface->flags |= QEMU_PLACEHOLDER_FLAG;
1398 void qemu_free_displaysurface(DisplaySurface *surface)
1400 if (surface == NULL) {
1403 trace_displaysurface_free(surface);
1404 qemu_pixman_image_unref(surface->image);
1408 bool console_has_gl(QemuConsole *con)
1410 return con->gl != NULL;
1413 static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
1415 if (dcl->ops->dpy_has_dmabuf) {
1416 return dcl->ops->dpy_has_dmabuf(dcl);
1419 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1426 static bool dpy_compatible_with(QemuConsole *con,
1427 DisplayChangeListener *dcl, Error **errp)
1431 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
1433 if (flags & GRAPHIC_FLAGS_GL &&
1434 !console_has_gl(con)) {
1435 error_setg(errp, "The console requires a GL context.");
1440 if (flags & GRAPHIC_FLAGS_DMABUF &&
1441 !displaychangelistener_has_dmabuf(dcl)) {
1442 error_setg(errp, "The console requires display DMABUF support.");
1449 void register_displaychangelistener(DisplayChangeListener *dcl)
1451 static const char nodev[] =
1452 "This VM has no graphic display device.";
1453 static DisplaySurface *dummy;
1458 if (dcl->ops->dpy_gl_ctx_create) {
1459 /* display has opengl support */
1462 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1463 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1470 dpy_compatible_with(dcl->con, dcl, &error_fatal);
1473 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1474 dcl->ds = get_alloc_displaystate();
1475 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1476 gui_setup_refresh(dcl->ds);
1481 con = active_console;
1483 if (dcl->ops->dpy_gfx_switch) {
1485 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1488 dummy = qemu_create_placeholder_surface(640, 480, nodev);
1490 dcl->ops->dpy_gfx_switch(dcl, dummy);
1493 text_console_update_cursor(NULL);
1496 void update_displaychangelistener(DisplayChangeListener *dcl,
1499 DisplayState *ds = dcl->ds;
1501 dcl->update_interval = interval;
1502 if (!ds->refreshing && ds->update_interval > interval) {
1503 timer_mod(ds->gui_timer, ds->last_update + interval);
1507 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1509 DisplayState *ds = dcl->ds;
1510 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1514 QLIST_REMOVE(dcl, next);
1516 gui_setup_refresh(ds);
1519 static void dpy_set_ui_info_timer(void *opaque)
1521 QemuConsole *con = opaque;
1523 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1526 bool dpy_ui_info_supported(QemuConsole *con)
1529 con = active_console;
1532 return con->hw_ops->ui_info != NULL;
1535 const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
1538 con = active_console;
1541 return &con->ui_info;
1544 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1547 con = active_console;
1550 if (!dpy_ui_info_supported(con)) {
1553 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1554 /* nothing changed -- ignore */
1559 * Typically we get a flood of these as the user resizes the window.
1560 * Wait until the dust has settled (one second without updates), then
1561 * go notify the guest.
1563 con->ui_info = *info;
1564 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1568 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1570 DisplayState *s = con->ds;
1571 DisplayChangeListener *dcl;
1576 width = surface_width(con->surface);
1577 height = surface_height(con->surface);
1583 w = MIN(w, width - x);
1584 h = MIN(h, height - y);
1586 if (!qemu_console_is_visible(con)) {
1589 QLIST_FOREACH(dcl, &s->listeners, next) {
1590 if (con != (dcl->con ? dcl->con : active_console)) {
1593 if (dcl->ops->dpy_gfx_update) {
1594 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1599 void dpy_gfx_update_full(QemuConsole *con)
1601 if (!con->surface) {
1604 dpy_gfx_update(con, 0, 0,
1605 surface_width(con->surface),
1606 surface_height(con->surface));
1609 void dpy_gfx_replace_surface(QemuConsole *con,
1610 DisplaySurface *surface)
1612 static const char placeholder_msg[] = "Display output is not active.";
1613 DisplayState *s = con->ds;
1614 DisplaySurface *old_surface = con->surface;
1615 DisplayChangeListener *dcl;
1621 width = surface_width(old_surface);
1622 height = surface_height(old_surface);
1628 surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
1631 assert(old_surface != surface);
1633 con->surface = surface;
1634 QLIST_FOREACH(dcl, &s->listeners, next) {
1635 if (con != (dcl->con ? dcl->con : active_console)) {
1638 if (dcl->ops->dpy_gfx_switch) {
1639 dcl->ops->dpy_gfx_switch(dcl, surface);
1642 qemu_free_displaysurface(old_surface);
1645 bool dpy_gfx_check_format(QemuConsole *con,
1646 pixman_format_code_t format)
1648 DisplayChangeListener *dcl;
1649 DisplayState *s = con->ds;
1651 QLIST_FOREACH(dcl, &s->listeners, next) {
1652 if (dcl->con && dcl->con != con) {
1653 /* dcl bound to another console -> skip */
1656 if (dcl->ops->dpy_gfx_check_format) {
1657 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1661 /* default is to allow native 32 bpp only */
1662 if (format != qemu_default_pixman_format(32, true)) {
1670 static void dpy_refresh(DisplayState *s)
1672 DisplayChangeListener *dcl;
1674 QLIST_FOREACH(dcl, &s->listeners, next) {
1675 if (dcl->ops->dpy_refresh) {
1676 dcl->ops->dpy_refresh(dcl);
1681 void dpy_text_cursor(QemuConsole *con, int x, int y)
1683 DisplayState *s = con->ds;
1684 DisplayChangeListener *dcl;
1686 if (!qemu_console_is_visible(con)) {
1689 QLIST_FOREACH(dcl, &s->listeners, next) {
1690 if (con != (dcl->con ? dcl->con : active_console)) {
1693 if (dcl->ops->dpy_text_cursor) {
1694 dcl->ops->dpy_text_cursor(dcl, x, y);
1699 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1701 DisplayState *s = con->ds;
1702 DisplayChangeListener *dcl;
1704 if (!qemu_console_is_visible(con)) {
1707 QLIST_FOREACH(dcl, &s->listeners, next) {
1708 if (con != (dcl->con ? dcl->con : active_console)) {
1711 if (dcl->ops->dpy_text_update) {
1712 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1717 void dpy_text_resize(QemuConsole *con, int w, int h)
1719 DisplayState *s = con->ds;
1720 DisplayChangeListener *dcl;
1722 if (!qemu_console_is_visible(con)) {
1725 QLIST_FOREACH(dcl, &s->listeners, next) {
1726 if (con != (dcl->con ? dcl->con : active_console)) {
1729 if (dcl->ops->dpy_text_resize) {
1730 dcl->ops->dpy_text_resize(dcl, w, h);
1735 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1737 DisplayState *s = con->ds;
1738 DisplayChangeListener *dcl;
1740 if (!qemu_console_is_visible(con)) {
1743 QLIST_FOREACH(dcl, &s->listeners, next) {
1744 if (con != (dcl->con ? dcl->con : active_console)) {
1747 if (dcl->ops->dpy_mouse_set) {
1748 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1753 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1755 DisplayState *s = con->ds;
1756 DisplayChangeListener *dcl;
1758 if (!qemu_console_is_visible(con)) {
1761 QLIST_FOREACH(dcl, &s->listeners, next) {
1762 if (con != (dcl->con ? dcl->con : active_console)) {
1765 if (dcl->ops->dpy_cursor_define) {
1766 dcl->ops->dpy_cursor_define(dcl, cursor);
1771 bool dpy_cursor_define_supported(QemuConsole *con)
1773 DisplayState *s = con->ds;
1774 DisplayChangeListener *dcl;
1776 QLIST_FOREACH(dcl, &s->listeners, next) {
1777 if (dcl->ops->dpy_cursor_define) {
1784 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1785 struct QEMUGLParams *qparams)
1788 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1791 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1794 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1797 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1800 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1803 void dpy_gl_scanout_disable(QemuConsole *con)
1806 con->gl->ops->dpy_gl_scanout_disable(con->gl);
1809 void dpy_gl_scanout_texture(QemuConsole *con,
1810 uint32_t backing_id,
1811 bool backing_y_0_top,
1812 uint32_t backing_width,
1813 uint32_t backing_height,
1814 uint32_t x, uint32_t y,
1815 uint32_t width, uint32_t height)
1818 con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id,
1820 backing_width, backing_height,
1821 x, y, width, height);
1824 void dpy_gl_scanout_dmabuf(QemuConsole *con,
1828 con->gl->ops->dpy_gl_scanout_dmabuf(con->gl, dmabuf);
1831 void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1832 bool have_hot, uint32_t hot_x, uint32_t hot_y)
1836 if (con->gl->ops->dpy_gl_cursor_dmabuf) {
1837 con->gl->ops->dpy_gl_cursor_dmabuf(con->gl, dmabuf,
1838 have_hot, hot_x, hot_y);
1842 void dpy_gl_cursor_position(QemuConsole *con,
1843 uint32_t pos_x, uint32_t pos_y)
1847 if (con->gl->ops->dpy_gl_cursor_position) {
1848 con->gl->ops->dpy_gl_cursor_position(con->gl, pos_x, pos_y);
1852 void dpy_gl_release_dmabuf(QemuConsole *con,
1857 if (con->gl->ops->dpy_gl_release_dmabuf) {
1858 con->gl->ops->dpy_gl_release_dmabuf(con->gl, dmabuf);
1862 void dpy_gl_update(QemuConsole *con,
1863 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1866 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1869 /***********************************************************/
1870 /* register display */
1872 /* console.c internal use only */
1873 static DisplayState *get_alloc_displaystate(void)
1875 if (!display_state) {
1876 display_state = g_new0(DisplayState, 1);
1877 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1878 text_console_update_cursor, NULL);
1880 return display_state;
1884 * Called by main(), after creating QemuConsoles
1885 * and before initializing ui (sdl/vnc/...).
1887 DisplayState *init_displaystate(void)
1892 get_alloc_displaystate();
1893 QTAILQ_FOREACH(con, &consoles, next) {
1894 if (con->console_type != GRAPHIC_CONSOLE &&
1896 text_console_do_init(con->chr, display_state);
1899 /* Hook up into the qom tree here (not in new_console()), once
1900 * all QemuConsoles are created and the order / numbering
1901 * doesn't change any more */
1902 name = g_strdup_printf("console[%d]", con->index);
1903 object_property_add_child(container_get(object_get_root(), "/backend"),
1908 return display_state;
1911 void graphic_console_set_hwops(QemuConsole *con,
1912 const GraphicHwOps *hw_ops,
1915 con->hw_ops = hw_ops;
1919 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1920 const GraphicHwOps *hw_ops,
1923 static const char noinit[] =
1924 "Guest has not initialized the display (yet).";
1929 DisplaySurface *surface;
1931 ds = get_alloc_displaystate();
1932 s = qemu_console_lookup_unused();
1934 trace_console_gfx_reuse(s->index);
1936 width = surface_width(s->surface);
1937 height = surface_height(s->surface);
1940 trace_console_gfx_new();
1941 s = new_console(ds, GRAPHIC_CONSOLE, head);
1942 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1943 dpy_set_ui_info_timer, s);
1945 graphic_console_set_hwops(s, hw_ops, opaque);
1947 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
1951 surface = qemu_create_placeholder_surface(width, height, noinit);
1952 dpy_gfx_replace_surface(s, surface);
1956 static const GraphicHwOps unused_ops = {
1960 void graphic_console_close(QemuConsole *con)
1962 static const char unplugged[] =
1963 "Guest display has been unplugged";
1964 DisplaySurface *surface;
1969 width = surface_width(con->surface);
1970 height = surface_height(con->surface);
1973 trace_console_gfx_close(con->index);
1974 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
1975 graphic_console_set_hwops(con, &unused_ops, NULL);
1978 dpy_gl_scanout_disable(con);
1980 surface = qemu_create_placeholder_surface(width, height, unplugged);
1981 dpy_gfx_replace_surface(con, surface);
1984 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1988 QTAILQ_FOREACH(con, &consoles, next) {
1989 if (con->index == index) {
1996 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
2002 QTAILQ_FOREACH(con, &consoles, next) {
2003 obj = object_property_get_link(OBJECT(con),
2004 "device", &error_abort);
2005 if (DEVICE(obj) != dev) {
2008 h = object_property_get_uint(OBJECT(con),
2009 "head", &error_abort);
2018 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
2019 uint32_t head, Error **errp)
2024 dev = qdev_find_recursive(sysbus_get_default(), device_id);
2026 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2027 "Device '%s' not found", device_id);
2031 con = qemu_console_lookup_by_device(dev, head);
2033 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
2041 QemuConsole *qemu_console_lookup_unused(void)
2046 QTAILQ_FOREACH(con, &consoles, next) {
2047 if (con->hw_ops != &unused_ops) {
2050 obj = object_property_get_link(OBJECT(con),
2051 "device", &error_abort);
2060 bool qemu_console_is_visible(QemuConsole *con)
2062 return (con == active_console) || (con->dcls > 0);
2065 bool qemu_console_is_graphic(QemuConsole *con)
2068 con = active_console;
2070 return con && (con->console_type == GRAPHIC_CONSOLE);
2073 bool qemu_console_is_fixedsize(QemuConsole *con)
2076 con = active_console;
2078 return con && (con->console_type != TEXT_CONSOLE);
2081 bool qemu_console_is_gl_blocked(QemuConsole *con)
2083 assert(con != NULL);
2084 return con->gl_block;
2087 char *qemu_console_get_label(QemuConsole *con)
2089 if (con->console_type == GRAPHIC_CONSOLE) {
2091 return g_strdup(object_get_typename(con->device));
2093 return g_strdup("VGA");
2095 if (con->chr && con->chr->label) {
2096 return g_strdup(con->chr->label);
2098 return g_strdup_printf("vc%d", con->index);
2102 int qemu_console_get_index(QemuConsole *con)
2105 con = active_console;
2107 return con ? con->index : -1;
2110 uint32_t qemu_console_get_head(QemuConsole *con)
2113 con = active_console;
2115 return con ? con->head : -1;
2118 int qemu_console_get_width(QemuConsole *con, int fallback)
2121 con = active_console;
2123 return con ? surface_width(con->surface) : fallback;
2126 int qemu_console_get_height(QemuConsole *con, int fallback)
2129 con = active_console;
2131 return con ? surface_height(con->surface) : fallback;
2134 static void vc_chr_accept_input(Chardev *chr)
2136 VCChardev *drv = VC_CHARDEV(chr);
2137 QemuConsole *s = drv->console;
2142 static void vc_chr_set_echo(Chardev *chr, bool echo)
2144 VCChardev *drv = VC_CHARDEV(chr);
2145 QemuConsole *s = drv->console;
2150 static void text_console_update_cursor_timer(void)
2152 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2153 + CONSOLE_CURSOR_PERIOD / 2);
2156 static void text_console_update_cursor(void *opaque)
2161 cursor_visible_phase = !cursor_visible_phase;
2163 QTAILQ_FOREACH(s, &consoles, next) {
2164 if (qemu_console_is_graphic(s) ||
2165 !qemu_console_is_visible(s)) {
2169 graphic_hw_invalidate(s);
2173 text_console_update_cursor_timer();
2177 static const GraphicHwOps text_console_ops = {
2178 .invalidate = text_console_invalidate,
2179 .text_update = text_console_update,
2182 static void text_console_do_init(Chardev *chr, DisplayState *ds)
2184 VCChardev *drv = VC_CHARDEV(chr);
2185 QemuConsole *s = drv->console;
2186 int g_width = 80 * FONT_WIDTH;
2187 int g_height = 24 * FONT_HEIGHT;
2189 fifo8_create(&s->out_fifo, 16);
2194 s->total_height = DEFAULT_BACKSCROLL;
2198 if (active_console && active_console->surface) {
2199 g_width = surface_width(active_console->surface);
2200 g_height = surface_height(active_console->surface);
2202 s->surface = qemu_create_displaysurface(g_width, g_height);
2205 s->hw_ops = &text_console_ops;
2208 /* Set text attribute defaults */
2209 s->t_attrib_default.bold = 0;
2210 s->t_attrib_default.uline = 0;
2211 s->t_attrib_default.blink = 0;
2212 s->t_attrib_default.invers = 0;
2213 s->t_attrib_default.unvisible = 0;
2214 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2215 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2216 /* set current text attributes to default */
2217 s->t_attrib = s->t_attrib_default;
2218 text_console_resize(s);
2223 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2224 msg = g_strdup_printf("%s console\r\n", chr->label);
2225 vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
2227 s->t_attrib = s->t_attrib_default;
2230 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
2233 static void vc_chr_open(Chardev *chr,
2234 ChardevBackend *backend,
2238 ChardevVC *vc = backend->u.vc.data;
2239 VCChardev *drv = VC_CHARDEV(chr);
2242 unsigned height = 0;
2244 if (vc->has_width) {
2246 } else if (vc->has_cols) {
2247 width = vc->cols * FONT_WIDTH;
2250 if (vc->has_height) {
2251 height = vc->height;
2252 } else if (vc->has_rows) {
2253 height = vc->rows * FONT_HEIGHT;
2256 trace_console_txt_new(width, height);
2257 if (width == 0 || height == 0) {
2258 s = new_console(NULL, TEXT_CONSOLE, 0);
2260 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2261 s->surface = qemu_create_displaysurface(width, height);
2265 error_setg(errp, "cannot create text console");
2272 if (display_state) {
2273 text_console_do_init(chr, display_state);
2276 /* console/chardev init sometimes completes elsewhere in a 2nd
2277 * stage, so defer OPENED events until they are fully initialized
2282 void qemu_console_resize(QemuConsole *s, int width, int height)
2284 DisplaySurface *surface;
2286 assert(s->console_type == GRAPHIC_CONSOLE);
2288 if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) &&
2289 pixman_image_get_width(s->surface->image) == width &&
2290 pixman_image_get_height(s->surface->image) == height) {
2294 surface = qemu_create_displaysurface(width, height);
2295 dpy_gfx_replace_surface(s, surface);
2298 DisplaySurface *qemu_console_surface(QemuConsole *console)
2300 return console->surface;
2303 PixelFormat qemu_default_pixelformat(int bpp)
2305 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2306 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2310 static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2312 void qemu_display_register(QemuDisplay *ui)
2314 assert(ui->type < DISPLAY_TYPE__MAX);
2315 dpys[ui->type] = ui;
2318 bool qemu_display_find_default(DisplayOptions *opts)
2320 static DisplayType prio[] = {
2321 #if defined(CONFIG_GTK)
2324 #if defined(CONFIG_SDL)
2327 #if defined(CONFIG_COCOA)
2333 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
2334 if (dpys[prio[i]] == NULL) {
2335 ui_module_load_one(DisplayType_str(prio[i]));
2337 if (dpys[prio[i]] == NULL) {
2340 opts->type = prio[i];
2346 void qemu_display_early_init(DisplayOptions *opts)
2348 assert(opts->type < DISPLAY_TYPE__MAX);
2349 if (opts->type == DISPLAY_TYPE_NONE) {
2352 if (dpys[opts->type] == NULL) {
2353 ui_module_load_one(DisplayType_str(opts->type));
2355 if (dpys[opts->type] == NULL) {
2356 error_report("Display '%s' is not available.",
2357 DisplayType_str(opts->type));
2360 if (dpys[opts->type]->early_init) {
2361 dpys[opts->type]->early_init(opts);
2365 void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2367 assert(opts->type < DISPLAY_TYPE__MAX);
2368 if (opts->type == DISPLAY_TYPE_NONE) {
2371 assert(dpys[opts->type] != NULL);
2372 dpys[opts->type]->init(ds, opts);
2375 void qemu_display_help(void)
2379 printf("Available display backend types:\n");
2381 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
2383 ui_module_load_one(DisplayType_str(idx));
2386 printf("%s\n", DisplayType_str(dpys[idx]->type));
2391 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
2396 backend->type = CHARDEV_BACKEND_KIND_VC;
2397 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2398 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2400 val = qemu_opt_get_number(opts, "width", 0);
2402 vc->has_width = true;
2406 val = qemu_opt_get_number(opts, "height", 0);
2408 vc->has_height = true;
2412 val = qemu_opt_get_number(opts, "cols", 0);
2414 vc->has_cols = true;
2418 val = qemu_opt_get_number(opts, "rows", 0);
2420 vc->has_rows = true;
2425 static const TypeInfo qemu_console_info = {
2426 .name = TYPE_QEMU_CONSOLE,
2427 .parent = TYPE_OBJECT,
2428 .instance_size = sizeof(QemuConsole),
2429 .class_size = sizeof(QemuConsoleClass),
2432 static void char_vc_class_init(ObjectClass *oc, void *data)
2434 ChardevClass *cc = CHARDEV_CLASS(oc);
2436 cc->parse = qemu_chr_parse_vc;
2437 cc->open = vc_chr_open;
2438 cc->chr_write = vc_chr_write;
2439 cc->chr_accept_input = vc_chr_accept_input;
2440 cc->chr_set_echo = vc_chr_set_echo;
2443 static const TypeInfo char_vc_type_info = {
2444 .name = TYPE_CHARDEV_VC,
2445 .parent = TYPE_CHARDEV,
2446 .instance_size = sizeof(VCChardev),
2447 .class_init = char_vc_class_init,
2450 void qemu_console_early_init(void)
2452 /* set the default vc driver */
2453 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2454 type_register(&char_vc_type_info);
2458 static void register_types(void)
2460 type_register_static(&qemu_console_info);
2463 type_init(register_types);