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
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qemu/timer.h"
29 #include "qmp-commands.h"
30 #include "sysemu/char.h"
32 #include "exec/memory.h"
35 #include "sysemu/sysemu.h"
37 extern int initial_resolution_width;
38 extern int initial_resolution_height;
41 #define DEFAULT_BACKSCROLL 512
42 #define CONSOLE_CURSOR_PERIOD 500
44 typedef struct TextAttributes {
54 typedef struct TextCell {
56 TextAttributes t_attrib;
59 #define MAX_ESC_PARAMS 3
67 typedef struct QEMUFIFO {
70 int count, wptr, rptr;
73 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
77 l = f->buf_size - f->count;
82 l = f->buf_size - f->wptr;
85 memcpy(f->buf + f->wptr, buf, l);
87 if (f->wptr >= f->buf_size)
96 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
104 l = f->buf_size - f->rptr;
107 memcpy(buf, f->buf + f->rptr, l);
109 if (f->rptr >= f->buf_size)
121 TEXT_CONSOLE_FIXED_SIZE
128 console_type_t console_type;
130 DisplaySurface *surface;
132 DisplayChangeListener *gl;
135 /* Graphic console state. */
140 const GraphicHwOps *hw_ops;
143 /* Text console state */
147 int backscroll_height;
149 int x_saved, y_saved;
152 TextAttributes t_attrib_default; /* default text attributes */
153 TextAttributes t_attrib; /* currently active text attributes */
155 int text_x[2], text_y[2], cursor_invalidate;
164 int esc_params[MAX_ESC_PARAMS];
167 CharDriverState *chr;
168 /* fifo for key pressed */
170 uint8_t out_fifo_buf[16];
171 QEMUTimer *kbd_timer;
174 struct DisplayState {
175 QEMUTimer *gui_timer;
176 uint64_t last_update;
177 uint64_t update_interval;
182 QLIST_HEAD(, DisplayChangeListener) listeners;
185 static DisplayState *display_state;
186 static QemuConsole *active_console;
187 static QemuConsole **consoles;
188 static int nb_consoles = 0;
189 static bool cursor_visible_phase;
190 static QEMUTimer *cursor_timer;
192 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
193 static void dpy_refresh(DisplayState *s);
194 static DisplayState *get_alloc_displaystate(void);
195 static void text_console_update_cursor_timer(void);
196 static void text_console_update_cursor(void *opaque);
198 static void gui_update(void *opaque)
200 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
201 uint64_t dcl_interval;
202 DisplayState *ds = opaque;
203 DisplayChangeListener *dcl;
206 ds->refreshing = true;
208 ds->refreshing = false;
210 QLIST_FOREACH(dcl, &ds->listeners, next) {
211 dcl_interval = dcl->update_interval ?
212 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
213 if (interval > dcl_interval) {
214 interval = dcl_interval;
217 if (ds->update_interval != interval) {
218 ds->update_interval = interval;
219 for (i = 0; i < nb_consoles; i++) {
220 if (consoles[i]->hw_ops->update_interval) {
221 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
224 trace_console_refresh(interval);
226 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
227 timer_mod(ds->gui_timer, ds->last_update + interval);
230 static void gui_setup_refresh(DisplayState *ds)
232 DisplayChangeListener *dcl;
233 bool need_timer = false;
234 bool have_gfx = false;
235 bool have_text = false;
237 QLIST_FOREACH(dcl, &ds->listeners, next) {
238 if (dcl->ops->dpy_refresh != NULL) {
241 if (dcl->ops->dpy_gfx_update != NULL) {
244 if (dcl->ops->dpy_text_update != NULL) {
249 if (need_timer && ds->gui_timer == NULL) {
250 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
251 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
253 if (!need_timer && ds->gui_timer != NULL) {
254 timer_del(ds->gui_timer);
255 timer_free(ds->gui_timer);
256 ds->gui_timer = NULL;
259 ds->have_gfx = have_gfx;
260 ds->have_text = have_text;
263 void graphic_hw_update(QemuConsole *con)
266 con = active_console;
268 if (con && con->hw_ops->gfx_update) {
269 con->hw_ops->gfx_update(con->hw);
273 void graphic_hw_gl_block(QemuConsole *con, bool block)
277 con->gl_block = block;
278 if (con->hw_ops->gl_block) {
279 con->hw_ops->gl_block(con->hw, block);
283 void graphic_hw_invalidate(QemuConsole *con)
286 con = active_console;
288 if (con && con->hw_ops->invalidate) {
289 con->hw_ops->invalidate(con->hw);
293 static void ppm_save(const char *filename, DisplaySurface *ds,
296 int width = pixman_image_get_width(ds->image);
297 int height = pixman_image_get_height(ds->image);
302 pixman_image_t *linebuf;
304 trace_ppm_save(filename, ds);
305 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
307 error_setg(errp, "failed to open file '%s': %s", filename,
311 f = fdopen(fd, "wb");
312 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
317 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
318 for (y = 0; y < height; y++) {
319 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
321 ret = fwrite(pixman_image_get_data(linebuf), 1,
322 pixman_image_get_stride(linebuf), f);
330 qemu_pixman_image_unref(linebuf);
335 error_setg(errp, "failed to write to file '%s': %s", filename,
341 void qmp_screendump(const char *filename, Error **errp)
343 QemuConsole *con = qemu_console_lookup_by_index(0);
344 DisplaySurface *surface;
347 error_setg(errp, "There is no QemuConsole I can screendump from.");
351 graphic_hw_update(con);
352 surface = qemu_console_surface(con);
353 ppm_save(filename, surface, errp);
356 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
359 con = active_console;
361 if (con && con->hw_ops->text_update) {
362 con->hw_ops->text_update(con->hw, chardata);
366 static void vga_fill_rect(QemuConsole *con,
367 int posx, int posy, int width, int height,
368 pixman_color_t color)
370 DisplaySurface *surface = qemu_console_surface(con);
371 pixman_rectangle16_t rect = {
372 .x = posx, .y = posy, .width = width, .height = height
375 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
379 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
380 static void vga_bitblt(QemuConsole *con,
381 int xs, int ys, int xd, int yd, int w, int h)
383 DisplaySurface *surface = qemu_console_surface(con);
385 pixman_image_composite(PIXMAN_OP_SRC,
386 surface->image, NULL, surface->image,
387 xs, ys, 0, 0, xd, yd, w, h);
390 /***********************************************************/
391 /* basic char display */
393 #define FONT_HEIGHT 16
398 #define QEMU_RGB(r, g, b) \
399 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
401 static const pixman_color_t color_table_rgb[2][8] = {
403 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
404 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
405 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
406 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
407 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
408 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
409 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
410 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
413 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
414 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
415 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
416 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
417 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
418 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
419 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
420 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
424 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
425 TextAttributes *t_attrib)
427 static pixman_image_t *glyphs[256];
428 DisplaySurface *surface = qemu_console_surface(s);
429 pixman_color_t fgcol, bgcol;
431 if (t_attrib->invers) {
432 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
433 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
435 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
436 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
440 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
442 qemu_pixman_glyph_render(glyphs[ch], surface->image,
443 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
446 static void text_console_resize(QemuConsole *s)
448 TextCell *cells, *c, *c1;
449 int w1, x, y, last_width;
451 last_width = s->width;
452 s->width = surface_width(s->surface) / FONT_WIDTH;
453 s->height = surface_height(s->surface) / FONT_HEIGHT;
459 cells = g_new(TextCell, s->width * s->total_height);
460 for(y = 0; y < s->total_height; y++) {
461 c = &cells[y * s->width];
463 c1 = &s->cells[y * last_width];
464 for(x = 0; x < w1; x++) {
468 for(x = w1; x < s->width; x++) {
470 c->t_attrib = s->t_attrib_default;
478 static inline void text_update_xy(QemuConsole *s, int x, int y)
480 s->text_x[0] = MIN(s->text_x[0], x);
481 s->text_x[1] = MAX(s->text_x[1], x);
482 s->text_y[0] = MIN(s->text_y[0], y);
483 s->text_y[1] = MAX(s->text_y[1], y);
486 static void invalidate_xy(QemuConsole *s, int x, int y)
488 if (!qemu_console_is_visible(s)) {
491 if (s->update_x0 > x * FONT_WIDTH)
492 s->update_x0 = x * FONT_WIDTH;
493 if (s->update_y0 > y * FONT_HEIGHT)
494 s->update_y0 = y * FONT_HEIGHT;
495 if (s->update_x1 < (x + 1) * FONT_WIDTH)
496 s->update_x1 = (x + 1) * FONT_WIDTH;
497 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
498 s->update_y1 = (y + 1) * FONT_HEIGHT;
501 static void update_xy(QemuConsole *s, int x, int y)
506 if (s->ds->have_text) {
507 text_update_xy(s, x, y);
510 y1 = (s->y_base + y) % s->total_height;
511 y2 = y1 - s->y_displayed;
513 y2 += s->total_height;
515 if (y2 < s->height) {
516 c = &s->cells[y1 * s->width + x];
517 vga_putcharxy(s, x, y2, c->ch,
519 invalidate_xy(s, x, y2);
523 static void console_show_cursor(QemuConsole *s, int show)
529 if (s->ds->have_text) {
530 s->cursor_invalidate = 1;
536 y1 = (s->y_base + s->y) % s->total_height;
537 y = y1 - s->y_displayed;
539 y += s->total_height;
542 c = &s->cells[y1 * s->width + x];
543 if (show && cursor_visible_phase) {
544 TextAttributes t_attrib = s->t_attrib_default;
545 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
546 vga_putcharxy(s, x, y, c->ch, &t_attrib);
548 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
550 invalidate_xy(s, x, y);
554 static void console_refresh(QemuConsole *s)
556 DisplaySurface *surface = qemu_console_surface(s);
560 if (s->ds->have_text) {
563 s->text_x[1] = s->width - 1;
564 s->text_y[1] = s->height - 1;
565 s->cursor_invalidate = 1;
568 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
569 color_table_rgb[0][QEMU_COLOR_BLACK]);
571 for (y = 0; y < s->height; y++) {
572 c = s->cells + y1 * s->width;
573 for (x = 0; x < s->width; x++) {
574 vga_putcharxy(s, x, y, c->ch,
578 if (++y1 == s->total_height) {
582 console_show_cursor(s, 1);
583 dpy_gfx_update(s, 0, 0,
584 surface_width(surface), surface_height(surface));
587 static void console_scroll(QemuConsole *s, int ydelta)
592 for(i = 0; i < ydelta; i++) {
593 if (s->y_displayed == s->y_base)
595 if (++s->y_displayed == s->total_height)
600 i = s->backscroll_height;
601 if (i > s->total_height - s->height)
602 i = s->total_height - s->height;
605 y1 += s->total_height;
606 for(i = 0; i < ydelta; i++) {
607 if (s->y_displayed == y1)
609 if (--s->y_displayed < 0)
610 s->y_displayed = s->total_height - 1;
616 static void console_put_lf(QemuConsole *s)
622 if (s->y >= s->height) {
623 s->y = s->height - 1;
625 if (s->y_displayed == s->y_base) {
626 if (++s->y_displayed == s->total_height)
629 if (++s->y_base == s->total_height)
631 if (s->backscroll_height < s->total_height)
632 s->backscroll_height++;
633 y1 = (s->y_base + s->height - 1) % s->total_height;
634 c = &s->cells[y1 * s->width];
635 for(x = 0; x < s->width; x++) {
637 c->t_attrib = s->t_attrib_default;
640 if (s->y_displayed == s->y_base) {
641 if (s->ds->have_text) {
644 s->text_x[1] = s->width - 1;
645 s->text_y[1] = s->height - 1;
648 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
649 s->width * FONT_WIDTH,
650 (s->height - 1) * FONT_HEIGHT);
651 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
652 s->width * FONT_WIDTH, FONT_HEIGHT,
653 color_table_rgb[0][s->t_attrib_default.bgcol]);
656 s->update_x1 = s->width * FONT_WIDTH;
657 s->update_y1 = s->height * FONT_HEIGHT;
662 /* Set console attributes depending on the current escape codes.
663 * NOTE: I know this code is not very efficient (checking every color for it
664 * self) but it is more readable and better maintainable.
666 static void console_handle_escape(QemuConsole *s)
670 for (i=0; i<s->nb_esc_params; i++) {
671 switch (s->esc_params[i]) {
672 case 0: /* reset all console attributes to default */
673 s->t_attrib = s->t_attrib_default;
676 s->t_attrib.bold = 1;
679 s->t_attrib.uline = 1;
682 s->t_attrib.blink = 1;
685 s->t_attrib.invers = 1;
688 s->t_attrib.unvisible = 1;
691 s->t_attrib.bold = 0;
694 s->t_attrib.uline = 0;
697 s->t_attrib.blink = 0;
700 s->t_attrib.invers = 0;
703 s->t_attrib.unvisible = 0;
705 /* set foreground color */
707 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
710 s->t_attrib.fgcol = QEMU_COLOR_RED;
713 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
716 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
719 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
722 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
725 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
728 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
730 /* set background color */
732 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
735 s->t_attrib.bgcol = QEMU_COLOR_RED;
738 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
741 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
744 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
747 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
750 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
753 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
759 static void console_clear_xy(QemuConsole *s, int x, int y)
761 int y1 = (s->y_base + y) % s->total_height;
762 TextCell *c = &s->cells[y1 * s->width + x];
764 c->t_attrib = s->t_attrib_default;
768 static void console_put_one(QemuConsole *s, int ch)
772 if (s->x >= s->width) {
777 y1 = (s->y_base + s->y) % s->total_height;
778 c = &s->cells[y1 * s->width + s->x];
780 c->t_attrib = s->t_attrib;
781 update_xy(s, s->x, s->y);
785 static void console_respond_str(QemuConsole *s, const char *buf)
788 console_put_one(s, *buf);
793 /* set cursor, checking bounds */
794 static void set_cursor(QemuConsole *s, int x, int y)
802 if (y >= s->height) {
813 static void console_putchar(QemuConsole *s, int ch)
822 case '\r': /* carriage return */
825 case '\n': /* newline */
828 case '\b': /* backspace */
832 case '\t': /* tabspace */
833 if (s->x + (8 - (s->x % 8)) > s->width) {
837 s->x = s->x + (8 - (s->x % 8));
840 case '\a': /* alert aka. bell */
841 /* TODO: has to be implemented */
844 /* SI (shift in), character set 0 (ignored) */
847 /* SO (shift out), character set 1 (ignored) */
849 case 27: /* esc (introducing an escape sequence) */
850 s->state = TTY_STATE_ESC;
853 console_put_one(s, ch);
857 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
859 for(i=0;i<MAX_ESC_PARAMS;i++)
860 s->esc_params[i] = 0;
861 s->nb_esc_params = 0;
862 s->state = TTY_STATE_CSI;
864 s->state = TTY_STATE_NORM;
867 case TTY_STATE_CSI: /* handle escape sequence parameters */
868 if (ch >= '0' && ch <= '9') {
869 if (s->nb_esc_params < MAX_ESC_PARAMS) {
870 int *param = &s->esc_params[s->nb_esc_params];
871 int digit = (ch - '0');
873 *param = (*param <= (INT_MAX - digit) / 10) ?
874 *param * 10 + digit : INT_MAX;
877 if (s->nb_esc_params < MAX_ESC_PARAMS)
881 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
882 ch, s->nb_esc_params);
883 s->state = TTY_STATE_NORM;
887 if (s->esc_params[0] == 0) {
888 s->esc_params[0] = 1;
890 set_cursor(s, s->x, s->y - s->esc_params[0]);
893 /* move cursor down */
894 if (s->esc_params[0] == 0) {
895 s->esc_params[0] = 1;
897 set_cursor(s, s->x, s->y + s->esc_params[0]);
900 /* move cursor right */
901 if (s->esc_params[0] == 0) {
902 s->esc_params[0] = 1;
904 set_cursor(s, s->x + s->esc_params[0], s->y);
907 /* move cursor left */
908 if (s->esc_params[0] == 0) {
909 s->esc_params[0] = 1;
911 set_cursor(s, s->x - s->esc_params[0], s->y);
914 /* move cursor to column */
915 set_cursor(s, s->esc_params[0] - 1, s->y);
919 /* move cursor to row, column */
920 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
923 switch (s->esc_params[0]) {
925 /* clear to end of screen */
926 for (y = s->y; y < s->height; y++) {
927 for (x = 0; x < s->width; x++) {
928 if (y == s->y && x < s->x) {
931 console_clear_xy(s, x, y);
936 /* clear from beginning of screen */
937 for (y = 0; y <= s->y; y++) {
938 for (x = 0; x < s->width; x++) {
939 if (y == s->y && x > s->x) {
942 console_clear_xy(s, x, y);
947 /* clear entire screen */
948 for (y = 0; y <= s->height; y++) {
949 for (x = 0; x < s->width; x++) {
950 console_clear_xy(s, x, y);
957 switch (s->esc_params[0]) {
960 for(x = s->x; x < s->width; x++) {
961 console_clear_xy(s, x, s->y);
965 /* clear from beginning of line */
966 for (x = 0; x <= s->x; x++) {
967 console_clear_xy(s, x, s->y);
971 /* clear entire line */
972 for(x = 0; x < s->width; x++) {
973 console_clear_xy(s, x, s->y);
979 console_handle_escape(s);
982 switch (s->esc_params[0]) {
984 /* report console status (always succeed)*/
985 console_respond_str(s, "\033[0n");
988 /* report cursor position */
989 sprintf(response, "\033[%d;%dR",
990 (s->y_base + s->y) % s->total_height + 1,
992 console_respond_str(s, response);
997 /* save cursor position */
1002 /* restore cursor position */
1007 trace_console_putchar_unhandled(ch);
1015 void console_select(unsigned int index)
1017 DisplayChangeListener *dcl;
1020 trace_console_select(index);
1021 s = qemu_console_lookup_by_index(index);
1023 DisplayState *ds = s->ds;
1027 QLIST_FOREACH(dcl, &ds->listeners, next) {
1028 if (dcl->con != NULL) {
1031 if (dcl->ops->dpy_gfx_switch) {
1032 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1035 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1036 surface_height(s->surface));
1038 if (ds->have_text) {
1039 dpy_text_resize(s, s->width, s->height);
1041 text_console_update_cursor(NULL);
1045 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1047 QemuConsole *s = chr->opaque;
1050 s->update_x0 = s->width * FONT_WIDTH;
1051 s->update_y0 = s->height * FONT_HEIGHT;
1054 console_show_cursor(s, 0);
1055 for(i = 0; i < len; i++) {
1056 console_putchar(s, buf[i]);
1058 console_show_cursor(s, 1);
1059 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1060 dpy_gfx_update(s, s->update_x0, s->update_y0,
1061 s->update_x1 - s->update_x0,
1062 s->update_y1 - s->update_y0);
1067 static void kbd_send_chars(void *opaque)
1069 QemuConsole *s = opaque;
1073 len = qemu_chr_be_can_write(s->chr);
1074 if (len > s->out_fifo.count)
1075 len = s->out_fifo.count;
1077 if (len > sizeof(buf))
1079 qemu_fifo_read(&s->out_fifo, buf, len);
1080 qemu_chr_be_write(s->chr, buf, len);
1082 /* characters are pending: we send them a bit later (XXX:
1083 horrible, should change char device API) */
1084 if (s->out_fifo.count > 0) {
1085 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1089 /* called when an ascii key is pressed */
1090 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1092 uint8_t buf[16], *q;
1096 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1100 case QEMU_KEY_CTRL_UP:
1101 console_scroll(s, -1);
1103 case QEMU_KEY_CTRL_DOWN:
1104 console_scroll(s, 1);
1106 case QEMU_KEY_CTRL_PAGEUP:
1107 console_scroll(s, -10);
1109 case QEMU_KEY_CTRL_PAGEDOWN:
1110 console_scroll(s, 10);
1113 /* convert the QEMU keysym to VT100 key string */
1115 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1118 c = keysym - 0xe100;
1120 *q++ = '0' + (c / 10);
1121 *q++ = '0' + (c % 10);
1123 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1126 *q++ = keysym & 0xff;
1127 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1128 console_puts(s->chr, (const uint8_t *) "\r", 1);
1134 console_puts(s->chr, buf, q - buf);
1137 if (be && be->chr_read) {
1138 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1145 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1146 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1147 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1148 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1149 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1150 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1151 [Q_KEY_CODE_END] = QEMU_KEY_END,
1152 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1153 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1154 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1155 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1158 bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1162 keysym = qcode_to_keysym[qcode];
1166 kbd_put_keysym_console(s, keysym);
1170 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1174 for (i = 0; i < len && str[i]; i++) {
1175 kbd_put_keysym_console(s, str[i]);
1179 void kbd_put_keysym(int keysym)
1181 kbd_put_keysym_console(active_console, keysym);
1184 static void text_console_invalidate(void *opaque)
1186 QemuConsole *s = (QemuConsole *) opaque;
1188 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1189 text_console_resize(s);
1194 static void text_console_update(void *opaque, console_ch_t *chardata)
1196 QemuConsole *s = (QemuConsole *) opaque;
1199 if (s->text_x[0] <= s->text_x[1]) {
1200 src = (s->y_base + s->text_y[0]) * s->width;
1201 chardata += s->text_y[0] * s->width;
1202 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1203 for (j = 0; j < s->width; j++, src++) {
1204 console_write_ch(chardata ++,
1205 ATTR2CHTYPE(s->cells[src].ch,
1206 s->cells[src].t_attrib.fgcol,
1207 s->cells[src].t_attrib.bgcol,
1208 s->cells[src].t_attrib.bold));
1210 dpy_text_update(s, s->text_x[0], s->text_y[0],
1211 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1212 s->text_x[0] = s->width;
1213 s->text_y[0] = s->height;
1217 if (s->cursor_invalidate) {
1218 dpy_text_cursor(s, s->x, s->y);
1219 s->cursor_invalidate = 0;
1223 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1230 obj = object_new(TYPE_QEMU_CONSOLE);
1231 s = QEMU_CONSOLE(obj);
1233 object_property_add_link(obj, "device", TYPE_DEVICE,
1234 (Object **)&s->device,
1235 object_property_allow_set_link,
1236 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1238 object_property_add_uint32_ptr(obj, "head",
1239 &s->head, &error_abort);
1241 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1242 (console_type == GRAPHIC_CONSOLE))) {
1246 s->console_type = console_type;
1248 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1249 if (console_type != GRAPHIC_CONSOLE) {
1250 s->index = nb_consoles;
1251 consoles[nb_consoles++] = s;
1253 /* HACK: Put graphical consoles before text consoles. */
1254 for (i = nb_consoles; i > 0; i--) {
1255 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1257 consoles[i] = consoles[i - 1];
1258 consoles[i]->index = i;
1267 static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1269 qemu_pixman_image_unref(surface->image);
1270 surface->image = NULL;
1272 surface->format = PIXMAN_x8r8g8b8;
1273 surface->image = pixman_image_create_bits(surface->format,
1276 assert(surface->image != NULL);
1278 surface->flags = QEMU_ALLOCATED_FLAG;
1281 DisplaySurface *qemu_create_displaysurface(int width, int height)
1283 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1285 trace_displaysurface_create(surface, width, height);
1286 qemu_alloc_display(surface, width, height);
1290 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1291 pixman_format_code_t format,
1292 int linesize, uint8_t *data)
1294 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1296 trace_displaysurface_create_from(surface, width, height, format);
1297 surface->format = format;
1298 surface->image = pixman_image_create_bits(surface->format,
1300 (void *)data, linesize);
1301 assert(surface->image != NULL);
1306 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1308 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1310 trace_displaysurface_create_pixman(surface);
1311 surface->format = pixman_image_get_format(image);
1312 surface->image = pixman_image_ref(image);
1317 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1320 void *data = pixman_image_get_data(image);
1321 uint32_t size = pixman_image_get_stride(image) *
1322 pixman_image_get_height(image);
1323 cpu_physical_memory_unmap(data, size, 0, 0);
1326 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1327 pixman_format_code_t format,
1328 int linesize, uint64_t addr)
1330 DisplaySurface *surface;
1334 if (linesize == 0) {
1335 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1338 size = (hwaddr)linesize * height;
1339 data = cpu_physical_memory_map(addr, &size, 0);
1340 if (size != (hwaddr)linesize * height) {
1341 cpu_physical_memory_unmap(data, size, 0, 0);
1345 surface = qemu_create_displaysurface_from
1346 (width, height, format, linesize, data);
1347 pixman_image_set_destroy_function
1348 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1353 static DisplaySurface *qemu_create_message_surface(int w, int h,
1356 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1357 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1358 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1359 pixman_image_t *glyph;
1363 x = (w / FONT_WIDTH - len) / 2;
1364 y = (h / FONT_HEIGHT - 1) / 2;
1365 for (i = 0; i < len; i++) {
1366 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1367 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1368 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1369 qemu_pixman_image_unref(glyph);
1374 void qemu_free_displaysurface(DisplaySurface *surface)
1376 if (surface == NULL) {
1379 trace_displaysurface_free(surface);
1380 qemu_pixman_image_unref(surface->image);
1384 bool console_has_gl(QemuConsole *con)
1386 return con->gl != NULL;
1389 void register_displaychangelistener(DisplayChangeListener *dcl)
1391 static const char nodev[] =
1392 "This VM has no graphic display device.";
1393 static DisplaySurface *dummy;
1396 if (dcl->ops->dpy_gl_ctx_create) {
1397 /* display has opengl support */
1400 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1401 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1407 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1408 dcl->ds = get_alloc_displaystate();
1409 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1410 gui_setup_refresh(dcl->ds);
1415 con = active_console;
1417 if (dcl->ops->dpy_gfx_switch) {
1419 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1422 dummy = qemu_create_message_surface(640, 480, nodev);
1424 dcl->ops->dpy_gfx_switch(dcl, dummy);
1427 text_console_update_cursor(NULL);
1430 void update_displaychangelistener(DisplayChangeListener *dcl,
1433 DisplayState *ds = dcl->ds;
1435 dcl->update_interval = interval;
1436 if (!ds->refreshing && ds->update_interval > interval) {
1437 timer_mod(ds->gui_timer, ds->last_update + interval);
1441 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1443 DisplayState *ds = dcl->ds;
1444 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1448 QLIST_REMOVE(dcl, next);
1449 gui_setup_refresh(ds);
1452 static void dpy_set_ui_info_timer(void *opaque)
1454 QemuConsole *con = opaque;
1456 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1459 bool dpy_ui_info_supported(QemuConsole *con)
1461 return con->hw_ops->ui_info != NULL;
1464 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1466 assert(con != NULL);
1468 if (!dpy_ui_info_supported(con)) {
1471 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1472 /* nothing changed -- ignore */
1477 * Typically we get a flood of these as the user resizes the window.
1478 * Wait until the dust has settled (one second without updates), then
1479 * go notify the guest.
1481 con->ui_info = *info;
1482 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1486 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1488 DisplayState *s = con->ds;
1489 DisplayChangeListener *dcl;
1494 width = surface_width(con->surface);
1495 height = surface_height(con->surface);
1501 w = MIN(w, width - x);
1502 h = MIN(h, height - y);
1504 if (!qemu_console_is_visible(con)) {
1507 QLIST_FOREACH(dcl, &s->listeners, next) {
1508 if (con != (dcl->con ? dcl->con : active_console)) {
1511 if (dcl->ops->dpy_gfx_update) {
1512 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1517 void dpy_gfx_replace_surface(QemuConsole *con,
1518 DisplaySurface *surface)
1520 DisplayState *s = con->ds;
1521 DisplaySurface *old_surface = con->surface;
1522 DisplayChangeListener *dcl;
1524 con->surface = surface;
1525 QLIST_FOREACH(dcl, &s->listeners, next) {
1526 if (con != (dcl->con ? dcl->con : active_console)) {
1529 if (dcl->ops->dpy_gfx_switch) {
1530 dcl->ops->dpy_gfx_switch(dcl, surface);
1533 qemu_free_displaysurface(old_surface);
1536 bool dpy_gfx_check_format(QemuConsole *con,
1537 pixman_format_code_t format)
1539 DisplayChangeListener *dcl;
1540 DisplayState *s = con->ds;
1542 QLIST_FOREACH(dcl, &s->listeners, next) {
1543 if (dcl->con && dcl->con != con) {
1544 /* dcl bound to another console -> skip */
1547 if (dcl->ops->dpy_gfx_check_format) {
1548 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1552 /* default is to whitelist native 32 bpp only */
1553 if (format != qemu_default_pixman_format(32, true)) {
1561 static void dpy_refresh(DisplayState *s)
1563 DisplayChangeListener *dcl;
1565 QLIST_FOREACH(dcl, &s->listeners, next) {
1566 if (dcl->ops->dpy_refresh) {
1567 dcl->ops->dpy_refresh(dcl);
1572 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1573 int dst_x, int dst_y, int w, int h)
1575 DisplayState *s = con->ds;
1576 DisplayChangeListener *dcl;
1578 if (!qemu_console_is_visible(con)) {
1581 QLIST_FOREACH(dcl, &s->listeners, next) {
1582 if (con != (dcl->con ? dcl->con : active_console)) {
1585 if (dcl->ops->dpy_gfx_copy) {
1586 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1588 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1593 void dpy_text_cursor(QemuConsole *con, int x, int y)
1595 DisplayState *s = con->ds;
1596 DisplayChangeListener *dcl;
1598 if (!qemu_console_is_visible(con)) {
1601 QLIST_FOREACH(dcl, &s->listeners, next) {
1602 if (con != (dcl->con ? dcl->con : active_console)) {
1605 if (dcl->ops->dpy_text_cursor) {
1606 dcl->ops->dpy_text_cursor(dcl, x, y);
1611 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1613 DisplayState *s = con->ds;
1614 DisplayChangeListener *dcl;
1616 if (!qemu_console_is_visible(con)) {
1619 QLIST_FOREACH(dcl, &s->listeners, next) {
1620 if (con != (dcl->con ? dcl->con : active_console)) {
1623 if (dcl->ops->dpy_text_update) {
1624 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1629 void dpy_text_resize(QemuConsole *con, int w, int h)
1631 DisplayState *s = con->ds;
1632 DisplayChangeListener *dcl;
1634 if (!qemu_console_is_visible(con)) {
1637 QLIST_FOREACH(dcl, &s->listeners, next) {
1638 if (con != (dcl->con ? dcl->con : active_console)) {
1641 if (dcl->ops->dpy_text_resize) {
1642 dcl->ops->dpy_text_resize(dcl, w, h);
1647 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1649 DisplayState *s = con->ds;
1650 DisplayChangeListener *dcl;
1652 if (!qemu_console_is_visible(con)) {
1655 QLIST_FOREACH(dcl, &s->listeners, next) {
1656 if (con != (dcl->con ? dcl->con : active_console)) {
1659 if (dcl->ops->dpy_mouse_set) {
1660 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1665 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1667 DisplayState *s = con->ds;
1668 DisplayChangeListener *dcl;
1670 if (!qemu_console_is_visible(con)) {
1673 QLIST_FOREACH(dcl, &s->listeners, next) {
1674 if (con != (dcl->con ? dcl->con : active_console)) {
1677 if (dcl->ops->dpy_cursor_define) {
1678 dcl->ops->dpy_cursor_define(dcl, cursor);
1683 bool dpy_cursor_define_supported(QemuConsole *con)
1685 DisplayState *s = con->ds;
1686 DisplayChangeListener *dcl;
1688 QLIST_FOREACH(dcl, &s->listeners, next) {
1689 if (dcl->ops->dpy_cursor_define) {
1696 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1697 struct QEMUGLParams *qparams)
1700 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1703 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1706 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1709 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1712 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1715 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1718 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1721 void dpy_gl_scanout(QemuConsole *con,
1722 uint32_t backing_id, bool backing_y_0_top,
1723 uint32_t backing_width, uint32_t backing_height,
1724 uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1727 con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1729 backing_width, backing_height,
1730 x, y, width, height);
1733 void dpy_gl_update(QemuConsole *con,
1734 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1737 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1740 /***********************************************************/
1741 /* register display */
1743 /* console.c internal use only */
1744 static DisplayState *get_alloc_displaystate(void)
1746 if (!display_state) {
1747 display_state = g_new0(DisplayState, 1);
1748 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1749 text_console_update_cursor, NULL);
1751 return display_state;
1755 * Called by main(), after creating QemuConsoles
1756 * and before initializing ui (sdl/vnc/...).
1758 DisplayState *init_displaystate(void)
1763 get_alloc_displaystate();
1764 for (i = 0; i < nb_consoles; i++) {
1765 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1766 consoles[i]->ds == NULL) {
1767 text_console_do_init(consoles[i]->chr, display_state);
1770 /* Hook up into the qom tree here (not in new_console()), once
1771 * all QemuConsoles are created and the order / numbering
1772 * doesn't change any more */
1773 name = g_strdup_printf("console[%d]", i);
1774 object_property_add_child(container_get(object_get_root(), "/backend"),
1775 name, OBJECT(consoles[i]), &error_abort);
1779 return display_state;
1782 void graphic_console_set_hwops(QemuConsole *con,
1783 const GraphicHwOps *hw_ops,
1786 con->hw_ops = hw_ops;
1790 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1791 const GraphicHwOps *hw_ops,
1794 static const char noinit[] =
1795 "Guest has not initialized the display (yet).";
1799 if (initial_resolution_width > 0 &&
1800 initial_resolution_height > 0) {
1801 width = initial_resolution_width;
1802 height = initial_resolution_height;
1808 ds = get_alloc_displaystate();
1809 trace_console_gfx_new();
1810 s = new_console(ds, GRAPHIC_CONSOLE, head);
1811 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1812 graphic_console_set_hwops(s, hw_ops, opaque);
1814 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1818 s->surface = qemu_create_message_surface(width, height, noinit);
1822 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1824 if (index >= nb_consoles) {
1827 return consoles[index];
1830 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1836 for (i = 0; i < nb_consoles; i++) {
1840 obj = object_property_get_link(OBJECT(consoles[i]),
1841 "device", &error_abort);
1842 if (DEVICE(obj) != dev) {
1845 h = object_property_get_int(OBJECT(consoles[i]),
1846 "head", &error_abort);
1855 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1856 uint32_t head, Error **errp)
1861 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1863 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1864 "Device '%s' not found", device_id);
1868 con = qemu_console_lookup_by_device(dev, head);
1870 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1878 bool qemu_console_is_visible(QemuConsole *con)
1880 return (con == active_console) || (con->dcls > 0);
1883 bool qemu_console_is_graphic(QemuConsole *con)
1886 con = active_console;
1888 return con && (con->console_type == GRAPHIC_CONSOLE);
1891 bool qemu_console_is_fixedsize(QemuConsole *con)
1894 con = active_console;
1896 return con && (con->console_type != TEXT_CONSOLE);
1899 bool qemu_console_is_gl_blocked(QemuConsole *con)
1901 assert(con != NULL);
1902 return con->gl_block;
1905 char *qemu_console_get_label(QemuConsole *con)
1907 if (con->console_type == GRAPHIC_CONSOLE) {
1909 return g_strdup(object_get_typename(con->device));
1911 return g_strdup("VGA");
1913 if (con->chr && con->chr->label) {
1914 return g_strdup(con->chr->label);
1916 return g_strdup_printf("vc%d", con->index);
1920 int qemu_console_get_index(QemuConsole *con)
1923 con = active_console;
1925 return con ? con->index : -1;
1928 uint32_t qemu_console_get_head(QemuConsole *con)
1931 con = active_console;
1933 return con ? con->head : -1;
1936 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1938 assert(con != NULL);
1939 return &con->ui_info;
1942 int qemu_console_get_width(QemuConsole *con, int fallback)
1945 con = active_console;
1947 return con ? surface_width(con->surface) : fallback;
1950 int qemu_console_get_height(QemuConsole *con, int fallback)
1953 con = active_console;
1955 return con ? surface_height(con->surface) : fallback;
1958 static void text_console_set_echo(CharDriverState *chr, bool echo)
1960 QemuConsole *s = chr->opaque;
1965 static void text_console_update_cursor_timer(void)
1967 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1968 + CONSOLE_CURSOR_PERIOD / 2);
1971 static void text_console_update_cursor(void *opaque)
1976 cursor_visible_phase = !cursor_visible_phase;
1978 for (i = 0; i < nb_consoles; i++) {
1980 if (qemu_console_is_graphic(s) ||
1981 !qemu_console_is_visible(s)) {
1985 graphic_hw_invalidate(s);
1989 text_console_update_cursor_timer();
1993 static const GraphicHwOps text_console_ops = {
1994 .invalidate = text_console_invalidate,
1995 .text_update = text_console_update,
1998 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
2001 int g_width = 80 * FONT_WIDTH;
2002 int g_height = 24 * FONT_HEIGHT;
2006 chr->chr_write = console_puts;
2008 s->out_fifo.buf = s->out_fifo_buf;
2009 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
2010 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
2015 s->total_height = DEFAULT_BACKSCROLL;
2019 if (active_console && active_console->surface) {
2020 g_width = surface_width(active_console->surface);
2021 g_height = surface_height(active_console->surface);
2023 s->surface = qemu_create_displaysurface(g_width, g_height);
2026 s->hw_ops = &text_console_ops;
2029 /* Set text attribute defaults */
2030 s->t_attrib_default.bold = 0;
2031 s->t_attrib_default.uline = 0;
2032 s->t_attrib_default.blink = 0;
2033 s->t_attrib_default.invers = 0;
2034 s->t_attrib_default.unvisible = 0;
2035 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2036 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2037 /* set current text attributes to default */
2038 s->t_attrib = s->t_attrib_default;
2039 text_console_resize(s);
2045 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2046 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2047 console_puts(chr, (uint8_t*)msg, len);
2048 s->t_attrib = s->t_attrib_default;
2051 qemu_chr_be_generic_open(chr);
2054 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2056 ChardevCommon *common = qapi_ChardevVC_base(vc);
2057 CharDriverState *chr;
2060 unsigned height = 0;
2062 chr = qemu_chr_alloc(common, errp);
2067 if (vc->has_width) {
2069 } else if (vc->has_cols) {
2070 width = vc->cols * FONT_WIDTH;
2073 if (vc->has_height) {
2074 height = vc->height;
2075 } else if (vc->has_rows) {
2076 height = vc->rows * FONT_HEIGHT;
2079 trace_console_txt_new(width, height);
2080 if (width == 0 || height == 0) {
2081 s = new_console(NULL, TEXT_CONSOLE, 0);
2083 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2084 s->surface = qemu_create_displaysurface(width, height);
2089 error_setg(errp, "cannot create text console");
2095 chr->chr_set_echo = text_console_set_echo;
2097 if (display_state) {
2098 text_console_do_init(chr, display_state);
2103 static VcHandler *vc_handler = text_console_init;
2105 static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2106 ChardevReturn *ret, bool *be_opened,
2109 /* console/chardev init sometimes completes elsewhere in a 2nd
2110 * stage, so defer OPENED events until they are fully initialized
2113 return vc_handler(backend->u.vc.data, errp);
2116 void register_vc_handler(VcHandler *handler)
2118 vc_handler = handler;
2121 void qemu_console_resize(QemuConsole *s, int width, int height)
2123 DisplaySurface *surface;
2125 assert(s->console_type == GRAPHIC_CONSOLE);
2128 pixman_image_get_width(s->surface->image) == width &&
2129 pixman_image_get_height(s->surface->image) == height) {
2133 surface = qemu_create_displaysurface(width, height);
2134 dpy_gfx_replace_surface(s, surface);
2137 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
2138 int dst_x, int dst_y, int w, int h)
2140 assert(con->console_type == GRAPHIC_CONSOLE);
2141 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
2144 DisplaySurface *qemu_console_surface(QemuConsole *console)
2146 return console->surface;
2149 PixelFormat qemu_default_pixelformat(int bpp)
2151 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2152 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2156 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2162 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2163 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2165 val = qemu_opt_get_number(opts, "width", 0);
2167 vc->has_width = true;
2171 val = qemu_opt_get_number(opts, "height", 0);
2173 vc->has_height = true;
2177 val = qemu_opt_get_number(opts, "cols", 0);
2179 vc->has_cols = true;
2183 val = qemu_opt_get_number(opts, "rows", 0);
2185 vc->has_rows = true;
2190 static const TypeInfo qemu_console_info = {
2191 .name = TYPE_QEMU_CONSOLE,
2192 .parent = TYPE_OBJECT,
2193 .instance_size = sizeof(QemuConsole),
2194 .class_size = sizeof(QemuConsoleClass),
2198 static void register_types(void)
2200 type_register_static(&qemu_console_info);
2201 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
2205 type_init(register_types);