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;
134 /* Graphic console state. */
139 const GraphicHwOps *hw_ops;
142 /* Text console state */
146 int backscroll_height;
148 int x_saved, y_saved;
151 TextAttributes t_attrib_default; /* default text attributes */
152 TextAttributes t_attrib; /* currently active text attributes */
154 int text_x[2], text_y[2], cursor_invalidate;
163 int esc_params[MAX_ESC_PARAMS];
166 CharDriverState *chr;
167 /* fifo for key pressed */
169 uint8_t out_fifo_buf[16];
170 QEMUTimer *kbd_timer;
173 struct DisplayState {
174 QEMUTimer *gui_timer;
175 uint64_t last_update;
176 uint64_t update_interval;
181 QLIST_HEAD(, DisplayChangeListener) listeners;
184 static DisplayState *display_state;
185 static QemuConsole *active_console;
186 static QemuConsole **consoles;
187 static int nb_consoles = 0;
188 static bool cursor_visible_phase;
189 static QEMUTimer *cursor_timer;
191 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
192 static void dpy_refresh(DisplayState *s);
193 static DisplayState *get_alloc_displaystate(void);
194 static void text_console_update_cursor_timer(void);
195 static void text_console_update_cursor(void *opaque);
197 static void gui_update(void *opaque)
199 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
200 uint64_t dcl_interval;
201 DisplayState *ds = opaque;
202 DisplayChangeListener *dcl;
205 ds->refreshing = true;
207 ds->refreshing = false;
209 QLIST_FOREACH(dcl, &ds->listeners, next) {
210 dcl_interval = dcl->update_interval ?
211 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
212 if (interval > dcl_interval) {
213 interval = dcl_interval;
216 if (ds->update_interval != interval) {
217 ds->update_interval = interval;
218 for (i = 0; i < nb_consoles; i++) {
219 if (consoles[i]->hw_ops->update_interval) {
220 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
223 trace_console_refresh(interval);
225 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
226 timer_mod(ds->gui_timer, ds->last_update + interval);
229 static void gui_setup_refresh(DisplayState *ds)
231 DisplayChangeListener *dcl;
232 bool need_timer = false;
233 bool have_gfx = false;
234 bool have_text = false;
236 QLIST_FOREACH(dcl, &ds->listeners, next) {
237 if (dcl->ops->dpy_refresh != NULL) {
240 if (dcl->ops->dpy_gfx_update != NULL) {
243 if (dcl->ops->dpy_text_update != NULL) {
248 if (need_timer && ds->gui_timer == NULL) {
249 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
250 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
252 if (!need_timer && ds->gui_timer != NULL) {
253 timer_del(ds->gui_timer);
254 timer_free(ds->gui_timer);
255 ds->gui_timer = NULL;
258 ds->have_gfx = have_gfx;
259 ds->have_text = have_text;
262 void graphic_hw_update(QemuConsole *con)
265 con = active_console;
267 if (con && con->hw_ops->gfx_update) {
268 con->hw_ops->gfx_update(con->hw);
272 void graphic_hw_gl_block(QemuConsole *con, bool block)
275 con = active_console;
277 if (con && con->hw_ops->gl_block) {
278 con->hw_ops->gl_block(con->hw, block);
282 void graphic_hw_invalidate(QemuConsole *con)
285 con = active_console;
287 if (con && con->hw_ops->invalidate) {
288 con->hw_ops->invalidate(con->hw);
292 static void ppm_save(const char *filename, DisplaySurface *ds,
295 int width = pixman_image_get_width(ds->image);
296 int height = pixman_image_get_height(ds->image);
301 pixman_image_t *linebuf;
303 trace_ppm_save(filename, ds);
304 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
306 error_setg(errp, "failed to open file '%s': %s", filename,
310 f = fdopen(fd, "wb");
311 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
316 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
317 for (y = 0; y < height; y++) {
318 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
320 ret = fwrite(pixman_image_get_data(linebuf), 1,
321 pixman_image_get_stride(linebuf), f);
329 qemu_pixman_image_unref(linebuf);
334 error_setg(errp, "failed to write to file '%s': %s", filename,
340 void qmp_screendump(const char *filename, Error **errp)
342 QemuConsole *con = qemu_console_lookup_by_index(0);
343 DisplaySurface *surface;
346 error_setg(errp, "There is no QemuConsole I can screendump from.");
350 graphic_hw_update(con);
351 surface = qemu_console_surface(con);
352 ppm_save(filename, surface, errp);
355 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
358 con = active_console;
360 if (con && con->hw_ops->text_update) {
361 con->hw_ops->text_update(con->hw, chardata);
365 static void vga_fill_rect(QemuConsole *con,
366 int posx, int posy, int width, int height,
367 pixman_color_t color)
369 DisplaySurface *surface = qemu_console_surface(con);
370 pixman_rectangle16_t rect = {
371 .x = posx, .y = posy, .width = width, .height = height
374 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
378 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
379 static void vga_bitblt(QemuConsole *con,
380 int xs, int ys, int xd, int yd, int w, int h)
382 DisplaySurface *surface = qemu_console_surface(con);
384 pixman_image_composite(PIXMAN_OP_SRC,
385 surface->image, NULL, surface->image,
386 xs, ys, 0, 0, xd, yd, w, h);
389 /***********************************************************/
390 /* basic char display */
392 #define FONT_HEIGHT 16
397 #define QEMU_RGB(r, g, b) \
398 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
400 static const pixman_color_t color_table_rgb[2][8] = {
402 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
403 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
404 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
405 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
406 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
407 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
408 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
409 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
412 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
413 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
414 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
415 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
416 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
417 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
418 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
419 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
423 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
424 TextAttributes *t_attrib)
426 static pixman_image_t *glyphs[256];
427 DisplaySurface *surface = qemu_console_surface(s);
428 pixman_color_t fgcol, bgcol;
430 if (t_attrib->invers) {
431 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
432 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
434 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
435 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
439 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
441 qemu_pixman_glyph_render(glyphs[ch], surface->image,
442 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
445 static void text_console_resize(QemuConsole *s)
447 TextCell *cells, *c, *c1;
448 int w1, x, y, last_width;
450 last_width = s->width;
451 s->width = surface_width(s->surface) / FONT_WIDTH;
452 s->height = surface_height(s->surface) / FONT_HEIGHT;
458 cells = g_new(TextCell, s->width * s->total_height);
459 for(y = 0; y < s->total_height; y++) {
460 c = &cells[y * s->width];
462 c1 = &s->cells[y * last_width];
463 for(x = 0; x < w1; x++) {
467 for(x = w1; x < s->width; x++) {
469 c->t_attrib = s->t_attrib_default;
477 static inline void text_update_xy(QemuConsole *s, int x, int y)
479 s->text_x[0] = MIN(s->text_x[0], x);
480 s->text_x[1] = MAX(s->text_x[1], x);
481 s->text_y[0] = MIN(s->text_y[0], y);
482 s->text_y[1] = MAX(s->text_y[1], y);
485 static void invalidate_xy(QemuConsole *s, int x, int y)
487 if (!qemu_console_is_visible(s)) {
490 if (s->update_x0 > x * FONT_WIDTH)
491 s->update_x0 = x * FONT_WIDTH;
492 if (s->update_y0 > y * FONT_HEIGHT)
493 s->update_y0 = y * FONT_HEIGHT;
494 if (s->update_x1 < (x + 1) * FONT_WIDTH)
495 s->update_x1 = (x + 1) * FONT_WIDTH;
496 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
497 s->update_y1 = (y + 1) * FONT_HEIGHT;
500 static void update_xy(QemuConsole *s, int x, int y)
505 if (s->ds->have_text) {
506 text_update_xy(s, x, y);
509 y1 = (s->y_base + y) % s->total_height;
510 y2 = y1 - s->y_displayed;
512 y2 += s->total_height;
514 if (y2 < s->height) {
515 c = &s->cells[y1 * s->width + x];
516 vga_putcharxy(s, x, y2, c->ch,
518 invalidate_xy(s, x, y2);
522 static void console_show_cursor(QemuConsole *s, int show)
528 if (s->ds->have_text) {
529 s->cursor_invalidate = 1;
535 y1 = (s->y_base + s->y) % s->total_height;
536 y = y1 - s->y_displayed;
538 y += s->total_height;
541 c = &s->cells[y1 * s->width + x];
542 if (show && cursor_visible_phase) {
543 TextAttributes t_attrib = s->t_attrib_default;
544 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
545 vga_putcharxy(s, x, y, c->ch, &t_attrib);
547 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
549 invalidate_xy(s, x, y);
553 static void console_refresh(QemuConsole *s)
555 DisplaySurface *surface = qemu_console_surface(s);
559 if (s->ds->have_text) {
562 s->text_x[1] = s->width - 1;
563 s->text_y[1] = s->height - 1;
564 s->cursor_invalidate = 1;
567 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
568 color_table_rgb[0][QEMU_COLOR_BLACK]);
570 for (y = 0; y < s->height; y++) {
571 c = s->cells + y1 * s->width;
572 for (x = 0; x < s->width; x++) {
573 vga_putcharxy(s, x, y, c->ch,
577 if (++y1 == s->total_height) {
581 console_show_cursor(s, 1);
582 dpy_gfx_update(s, 0, 0,
583 surface_width(surface), surface_height(surface));
586 static void console_scroll(QemuConsole *s, int ydelta)
591 for(i = 0; i < ydelta; i++) {
592 if (s->y_displayed == s->y_base)
594 if (++s->y_displayed == s->total_height)
599 i = s->backscroll_height;
600 if (i > s->total_height - s->height)
601 i = s->total_height - s->height;
604 y1 += s->total_height;
605 for(i = 0; i < ydelta; i++) {
606 if (s->y_displayed == y1)
608 if (--s->y_displayed < 0)
609 s->y_displayed = s->total_height - 1;
615 static void console_put_lf(QemuConsole *s)
621 if (s->y >= s->height) {
622 s->y = s->height - 1;
624 if (s->y_displayed == s->y_base) {
625 if (++s->y_displayed == s->total_height)
628 if (++s->y_base == s->total_height)
630 if (s->backscroll_height < s->total_height)
631 s->backscroll_height++;
632 y1 = (s->y_base + s->height - 1) % s->total_height;
633 c = &s->cells[y1 * s->width];
634 for(x = 0; x < s->width; x++) {
636 c->t_attrib = s->t_attrib_default;
639 if (s->y_displayed == s->y_base) {
640 if (s->ds->have_text) {
643 s->text_x[1] = s->width - 1;
644 s->text_y[1] = s->height - 1;
647 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
648 s->width * FONT_WIDTH,
649 (s->height - 1) * FONT_HEIGHT);
650 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
651 s->width * FONT_WIDTH, FONT_HEIGHT,
652 color_table_rgb[0][s->t_attrib_default.bgcol]);
655 s->update_x1 = s->width * FONT_WIDTH;
656 s->update_y1 = s->height * FONT_HEIGHT;
661 /* Set console attributes depending on the current escape codes.
662 * NOTE: I know this code is not very efficient (checking every color for it
663 * self) but it is more readable and better maintainable.
665 static void console_handle_escape(QemuConsole *s)
669 for (i=0; i<s->nb_esc_params; i++) {
670 switch (s->esc_params[i]) {
671 case 0: /* reset all console attributes to default */
672 s->t_attrib = s->t_attrib_default;
675 s->t_attrib.bold = 1;
678 s->t_attrib.uline = 1;
681 s->t_attrib.blink = 1;
684 s->t_attrib.invers = 1;
687 s->t_attrib.unvisible = 1;
690 s->t_attrib.bold = 0;
693 s->t_attrib.uline = 0;
696 s->t_attrib.blink = 0;
699 s->t_attrib.invers = 0;
702 s->t_attrib.unvisible = 0;
704 /* set foreground color */
706 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
709 s->t_attrib.fgcol = QEMU_COLOR_RED;
712 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
715 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
718 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
721 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
724 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
727 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
729 /* set background color */
731 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
734 s->t_attrib.bgcol = QEMU_COLOR_RED;
737 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
740 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
743 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
746 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
749 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
752 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
758 static void console_clear_xy(QemuConsole *s, int x, int y)
760 int y1 = (s->y_base + y) % s->total_height;
761 TextCell *c = &s->cells[y1 * s->width + x];
763 c->t_attrib = s->t_attrib_default;
767 static void console_put_one(QemuConsole *s, int ch)
771 if (s->x >= s->width) {
776 y1 = (s->y_base + s->y) % s->total_height;
777 c = &s->cells[y1 * s->width + s->x];
779 c->t_attrib = s->t_attrib;
780 update_xy(s, s->x, s->y);
784 static void console_respond_str(QemuConsole *s, const char *buf)
787 console_put_one(s, *buf);
792 /* set cursor, checking bounds */
793 static void set_cursor(QemuConsole *s, int x, int y)
801 if (y >= s->height) {
812 static void console_putchar(QemuConsole *s, int ch)
821 case '\r': /* carriage return */
824 case '\n': /* newline */
827 case '\b': /* backspace */
831 case '\t': /* tabspace */
832 if (s->x + (8 - (s->x % 8)) > s->width) {
836 s->x = s->x + (8 - (s->x % 8));
839 case '\a': /* alert aka. bell */
840 /* TODO: has to be implemented */
843 /* SI (shift in), character set 0 (ignored) */
846 /* SO (shift out), character set 1 (ignored) */
848 case 27: /* esc (introducing an escape sequence) */
849 s->state = TTY_STATE_ESC;
852 console_put_one(s, ch);
856 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
858 for(i=0;i<MAX_ESC_PARAMS;i++)
859 s->esc_params[i] = 0;
860 s->nb_esc_params = 0;
861 s->state = TTY_STATE_CSI;
863 s->state = TTY_STATE_NORM;
866 case TTY_STATE_CSI: /* handle escape sequence parameters */
867 if (ch >= '0' && ch <= '9') {
868 if (s->nb_esc_params < MAX_ESC_PARAMS) {
869 int *param = &s->esc_params[s->nb_esc_params];
870 int digit = (ch - '0');
872 *param = (*param <= (INT_MAX - digit) / 10) ?
873 *param * 10 + digit : INT_MAX;
876 if (s->nb_esc_params < MAX_ESC_PARAMS)
880 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
881 ch, s->nb_esc_params);
882 s->state = TTY_STATE_NORM;
886 if (s->esc_params[0] == 0) {
887 s->esc_params[0] = 1;
889 set_cursor(s, s->x, s->y - s->esc_params[0]);
892 /* move cursor down */
893 if (s->esc_params[0] == 0) {
894 s->esc_params[0] = 1;
896 set_cursor(s, s->x, s->y + s->esc_params[0]);
899 /* move cursor right */
900 if (s->esc_params[0] == 0) {
901 s->esc_params[0] = 1;
903 set_cursor(s, s->x + s->esc_params[0], s->y);
906 /* move cursor left */
907 if (s->esc_params[0] == 0) {
908 s->esc_params[0] = 1;
910 set_cursor(s, s->x - s->esc_params[0], s->y);
913 /* move cursor to column */
914 set_cursor(s, s->esc_params[0] - 1, s->y);
918 /* move cursor to row, column */
919 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
922 switch (s->esc_params[0]) {
924 /* clear to end of screen */
925 for (y = s->y; y < s->height; y++) {
926 for (x = 0; x < s->width; x++) {
927 if (y == s->y && x < s->x) {
930 console_clear_xy(s, x, y);
935 /* clear from beginning of screen */
936 for (y = 0; y <= s->y; y++) {
937 for (x = 0; x < s->width; x++) {
938 if (y == s->y && x > s->x) {
941 console_clear_xy(s, x, y);
946 /* clear entire screen */
947 for (y = 0; y <= s->height; y++) {
948 for (x = 0; x < s->width; x++) {
949 console_clear_xy(s, x, y);
956 switch (s->esc_params[0]) {
959 for(x = s->x; x < s->width; x++) {
960 console_clear_xy(s, x, s->y);
964 /* clear from beginning of line */
965 for (x = 0; x <= s->x; x++) {
966 console_clear_xy(s, x, s->y);
970 /* clear entire line */
971 for(x = 0; x < s->width; x++) {
972 console_clear_xy(s, x, s->y);
978 console_handle_escape(s);
981 switch (s->esc_params[0]) {
983 /* report console status (always succeed)*/
984 console_respond_str(s, "\033[0n");
987 /* report cursor position */
988 sprintf(response, "\033[%d;%dR",
989 (s->y_base + s->y) % s->total_height + 1,
991 console_respond_str(s, response);
996 /* save cursor position */
1001 /* restore cursor position */
1006 trace_console_putchar_unhandled(ch);
1014 void console_select(unsigned int index)
1016 DisplayChangeListener *dcl;
1019 trace_console_select(index);
1020 s = qemu_console_lookup_by_index(index);
1022 DisplayState *ds = s->ds;
1026 QLIST_FOREACH(dcl, &ds->listeners, next) {
1027 if (dcl->con != NULL) {
1030 if (dcl->ops->dpy_gfx_switch) {
1031 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1034 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1035 surface_height(s->surface));
1037 if (ds->have_text) {
1038 dpy_text_resize(s, s->width, s->height);
1040 text_console_update_cursor(NULL);
1044 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1046 QemuConsole *s = chr->opaque;
1049 s->update_x0 = s->width * FONT_WIDTH;
1050 s->update_y0 = s->height * FONT_HEIGHT;
1053 console_show_cursor(s, 0);
1054 for(i = 0; i < len; i++) {
1055 console_putchar(s, buf[i]);
1057 console_show_cursor(s, 1);
1058 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1059 dpy_gfx_update(s, s->update_x0, s->update_y0,
1060 s->update_x1 - s->update_x0,
1061 s->update_y1 - s->update_y0);
1066 static void kbd_send_chars(void *opaque)
1068 QemuConsole *s = opaque;
1072 len = qemu_chr_be_can_write(s->chr);
1073 if (len > s->out_fifo.count)
1074 len = s->out_fifo.count;
1076 if (len > sizeof(buf))
1078 qemu_fifo_read(&s->out_fifo, buf, len);
1079 qemu_chr_be_write(s->chr, buf, len);
1081 /* characters are pending: we send them a bit later (XXX:
1082 horrible, should change char device API) */
1083 if (s->out_fifo.count > 0) {
1084 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1088 /* called when an ascii key is pressed */
1089 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1091 uint8_t buf[16], *q;
1094 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1098 case QEMU_KEY_CTRL_UP:
1099 console_scroll(s, -1);
1101 case QEMU_KEY_CTRL_DOWN:
1102 console_scroll(s, 1);
1104 case QEMU_KEY_CTRL_PAGEUP:
1105 console_scroll(s, -10);
1107 case QEMU_KEY_CTRL_PAGEDOWN:
1108 console_scroll(s, 10);
1111 /* convert the QEMU keysym to VT100 key string */
1113 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1116 c = keysym - 0xe100;
1118 *q++ = '0' + (c / 10);
1119 *q++ = '0' + (c % 10);
1121 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1124 *q++ = keysym & 0xff;
1125 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1126 console_puts(s->chr, (const uint8_t *) "\r", 1);
1132 console_puts(s->chr, buf, q - buf);
1134 if (s->chr->chr_read) {
1135 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1142 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1143 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1144 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1145 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1146 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1147 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1148 [Q_KEY_CODE_END] = QEMU_KEY_END,
1149 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1150 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1151 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1154 bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1158 keysym = qcode_to_keysym[qcode];
1162 kbd_put_keysym_console(s, keysym);
1166 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1170 for (i = 0; i < len && str[i]; i++) {
1171 kbd_put_keysym_console(s, str[i]);
1175 void kbd_put_keysym(int keysym)
1177 kbd_put_keysym_console(active_console, keysym);
1180 static void text_console_invalidate(void *opaque)
1182 QemuConsole *s = (QemuConsole *) opaque;
1184 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1185 text_console_resize(s);
1190 static void text_console_update(void *opaque, console_ch_t *chardata)
1192 QemuConsole *s = (QemuConsole *) opaque;
1195 if (s->text_x[0] <= s->text_x[1]) {
1196 src = (s->y_base + s->text_y[0]) * s->width;
1197 chardata += s->text_y[0] * s->width;
1198 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1199 for (j = 0; j < s->width; j++, src++) {
1200 console_write_ch(chardata ++,
1201 ATTR2CHTYPE(s->cells[src].ch,
1202 s->cells[src].t_attrib.fgcol,
1203 s->cells[src].t_attrib.bgcol,
1204 s->cells[src].t_attrib.bold));
1206 dpy_text_update(s, s->text_x[0], s->text_y[0],
1207 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1208 s->text_x[0] = s->width;
1209 s->text_y[0] = s->height;
1213 if (s->cursor_invalidate) {
1214 dpy_text_cursor(s, s->x, s->y);
1215 s->cursor_invalidate = 0;
1219 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1226 obj = object_new(TYPE_QEMU_CONSOLE);
1227 s = QEMU_CONSOLE(obj);
1229 object_property_add_link(obj, "device", TYPE_DEVICE,
1230 (Object **)&s->device,
1231 object_property_allow_set_link,
1232 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1234 object_property_add_uint32_ptr(obj, "head",
1235 &s->head, &error_abort);
1237 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1238 (console_type == GRAPHIC_CONSOLE))) {
1242 s->console_type = console_type;
1244 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1245 if (console_type != GRAPHIC_CONSOLE) {
1246 s->index = nb_consoles;
1247 consoles[nb_consoles++] = s;
1249 /* HACK: Put graphical consoles before text consoles. */
1250 for (i = nb_consoles; i > 0; i--) {
1251 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1253 consoles[i] = consoles[i - 1];
1254 consoles[i]->index = i;
1263 static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1265 qemu_pixman_image_unref(surface->image);
1266 surface->image = NULL;
1268 surface->format = PIXMAN_x8r8g8b8;
1269 surface->image = pixman_image_create_bits(surface->format,
1272 assert(surface->image != NULL);
1274 surface->flags = QEMU_ALLOCATED_FLAG;
1277 DisplaySurface *qemu_create_displaysurface(int width, int height)
1279 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1281 trace_displaysurface_create(surface, width, height);
1282 qemu_alloc_display(surface, width, height);
1286 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1287 pixman_format_code_t format,
1288 int linesize, uint8_t *data)
1290 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1292 trace_displaysurface_create_from(surface, width, height, format);
1293 surface->format = format;
1294 surface->image = pixman_image_create_bits(surface->format,
1296 (void *)data, linesize);
1297 assert(surface->image != NULL);
1302 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1304 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1306 trace_displaysurface_create_pixman(surface);
1307 surface->format = pixman_image_get_format(image);
1308 surface->image = pixman_image_ref(image);
1313 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1316 void *data = pixman_image_get_data(image);
1317 uint32_t size = pixman_image_get_stride(image) *
1318 pixman_image_get_height(image);
1319 cpu_physical_memory_unmap(data, size, 0, 0);
1322 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1323 pixman_format_code_t format,
1324 int linesize, uint64_t addr)
1326 DisplaySurface *surface;
1330 if (linesize == 0) {
1331 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1334 size = (hwaddr)linesize * height;
1335 data = cpu_physical_memory_map(addr, &size, 0);
1336 if (size != (hwaddr)linesize * height) {
1337 cpu_physical_memory_unmap(data, size, 0, 0);
1341 surface = qemu_create_displaysurface_from
1342 (width, height, format, linesize, data);
1343 pixman_image_set_destroy_function
1344 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1349 static DisplaySurface *qemu_create_message_surface(int w, int h,
1352 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1353 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1354 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1355 pixman_image_t *glyph;
1359 x = (w / FONT_WIDTH - len) / 2;
1360 y = (h / FONT_HEIGHT - 1) / 2;
1361 for (i = 0; i < len; i++) {
1362 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1363 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1364 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1365 qemu_pixman_image_unref(glyph);
1370 void qemu_free_displaysurface(DisplaySurface *surface)
1372 if (surface == NULL) {
1375 trace_displaysurface_free(surface);
1376 qemu_pixman_image_unref(surface->image);
1380 bool console_has_gl(QemuConsole *con)
1382 return con->gl != NULL;
1385 void register_displaychangelistener(DisplayChangeListener *dcl)
1387 static const char nodev[] =
1388 "This VM has no graphic display device.";
1389 static DisplaySurface *dummy;
1392 if (dcl->ops->dpy_gl_ctx_create) {
1393 /* display has opengl support */
1396 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1397 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1403 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1404 dcl->ds = get_alloc_displaystate();
1405 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1406 gui_setup_refresh(dcl->ds);
1411 con = active_console;
1413 if (dcl->ops->dpy_gfx_switch) {
1415 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1418 dummy = qemu_create_message_surface(640, 480, nodev);
1420 dcl->ops->dpy_gfx_switch(dcl, dummy);
1423 text_console_update_cursor(NULL);
1426 void update_displaychangelistener(DisplayChangeListener *dcl,
1429 DisplayState *ds = dcl->ds;
1431 dcl->update_interval = interval;
1432 if (!ds->refreshing && ds->update_interval > interval) {
1433 timer_mod(ds->gui_timer, ds->last_update + interval);
1437 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1439 DisplayState *ds = dcl->ds;
1440 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1444 QLIST_REMOVE(dcl, next);
1445 gui_setup_refresh(ds);
1448 static void dpy_set_ui_info_timer(void *opaque)
1450 QemuConsole *con = opaque;
1452 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1455 bool dpy_ui_info_supported(QemuConsole *con)
1457 return con->hw_ops->ui_info != NULL;
1460 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1462 assert(con != NULL);
1464 if (!dpy_ui_info_supported(con)) {
1467 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1468 /* nothing changed -- ignore */
1473 * Typically we get a flood of these as the user resizes the window.
1474 * Wait until the dust has settled (one second without updates), then
1475 * go notify the guest.
1477 con->ui_info = *info;
1478 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1482 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1484 DisplayState *s = con->ds;
1485 DisplayChangeListener *dcl;
1490 width = surface_width(con->surface);
1491 height = surface_height(con->surface);
1497 w = MIN(w, width - x);
1498 h = MIN(h, height - y);
1500 if (!qemu_console_is_visible(con)) {
1503 QLIST_FOREACH(dcl, &s->listeners, next) {
1504 if (con != (dcl->con ? dcl->con : active_console)) {
1507 if (dcl->ops->dpy_gfx_update) {
1508 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1513 void dpy_gfx_replace_surface(QemuConsole *con,
1514 DisplaySurface *surface)
1516 DisplayState *s = con->ds;
1517 DisplaySurface *old_surface = con->surface;
1518 DisplayChangeListener *dcl;
1520 con->surface = surface;
1521 QLIST_FOREACH(dcl, &s->listeners, next) {
1522 if (con != (dcl->con ? dcl->con : active_console)) {
1525 if (dcl->ops->dpy_gfx_switch) {
1526 dcl->ops->dpy_gfx_switch(dcl, surface);
1529 qemu_free_displaysurface(old_surface);
1532 bool dpy_gfx_check_format(QemuConsole *con,
1533 pixman_format_code_t format)
1535 DisplayChangeListener *dcl;
1536 DisplayState *s = con->ds;
1538 QLIST_FOREACH(dcl, &s->listeners, next) {
1539 if (dcl->con && dcl->con != con) {
1540 /* dcl bound to another console -> skip */
1543 if (dcl->ops->dpy_gfx_check_format) {
1544 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1548 /* default is to whitelist native 32 bpp only */
1549 if (format != qemu_default_pixman_format(32, true)) {
1557 static void dpy_refresh(DisplayState *s)
1559 DisplayChangeListener *dcl;
1561 QLIST_FOREACH(dcl, &s->listeners, next) {
1562 if (dcl->ops->dpy_refresh) {
1563 dcl->ops->dpy_refresh(dcl);
1568 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1569 int dst_x, int dst_y, int w, int h)
1571 DisplayState *s = con->ds;
1572 DisplayChangeListener *dcl;
1574 if (!qemu_console_is_visible(con)) {
1577 QLIST_FOREACH(dcl, &s->listeners, next) {
1578 if (con != (dcl->con ? dcl->con : active_console)) {
1581 if (dcl->ops->dpy_gfx_copy) {
1582 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1584 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1589 void dpy_text_cursor(QemuConsole *con, int x, int y)
1591 DisplayState *s = con->ds;
1592 DisplayChangeListener *dcl;
1594 if (!qemu_console_is_visible(con)) {
1597 QLIST_FOREACH(dcl, &s->listeners, next) {
1598 if (con != (dcl->con ? dcl->con : active_console)) {
1601 if (dcl->ops->dpy_text_cursor) {
1602 dcl->ops->dpy_text_cursor(dcl, x, y);
1607 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1609 DisplayState *s = con->ds;
1610 DisplayChangeListener *dcl;
1612 if (!qemu_console_is_visible(con)) {
1615 QLIST_FOREACH(dcl, &s->listeners, next) {
1616 if (con != (dcl->con ? dcl->con : active_console)) {
1619 if (dcl->ops->dpy_text_update) {
1620 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1625 void dpy_text_resize(QemuConsole *con, int w, int h)
1627 DisplayState *s = con->ds;
1628 DisplayChangeListener *dcl;
1630 if (!qemu_console_is_visible(con)) {
1633 QLIST_FOREACH(dcl, &s->listeners, next) {
1634 if (con != (dcl->con ? dcl->con : active_console)) {
1637 if (dcl->ops->dpy_text_resize) {
1638 dcl->ops->dpy_text_resize(dcl, w, h);
1643 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1645 DisplayState *s = con->ds;
1646 DisplayChangeListener *dcl;
1648 if (!qemu_console_is_visible(con)) {
1651 QLIST_FOREACH(dcl, &s->listeners, next) {
1652 if (con != (dcl->con ? dcl->con : active_console)) {
1655 if (dcl->ops->dpy_mouse_set) {
1656 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1661 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1663 DisplayState *s = con->ds;
1664 DisplayChangeListener *dcl;
1666 if (!qemu_console_is_visible(con)) {
1669 QLIST_FOREACH(dcl, &s->listeners, next) {
1670 if (con != (dcl->con ? dcl->con : active_console)) {
1673 if (dcl->ops->dpy_cursor_define) {
1674 dcl->ops->dpy_cursor_define(dcl, cursor);
1679 bool dpy_cursor_define_supported(QemuConsole *con)
1681 DisplayState *s = con->ds;
1682 DisplayChangeListener *dcl;
1684 QLIST_FOREACH(dcl, &s->listeners, next) {
1685 if (dcl->ops->dpy_cursor_define) {
1692 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1693 struct QEMUGLParams *qparams)
1696 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1699 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1702 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1705 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1708 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1711 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1714 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1717 void dpy_gl_scanout(QemuConsole *con,
1718 uint32_t backing_id, bool backing_y_0_top,
1719 uint32_t backing_width, uint32_t backing_height,
1720 uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1723 con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1725 backing_width, backing_height,
1726 x, y, width, height);
1729 void dpy_gl_update(QemuConsole *con,
1730 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1733 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1736 /***********************************************************/
1737 /* register display */
1739 /* console.c internal use only */
1740 static DisplayState *get_alloc_displaystate(void)
1742 if (!display_state) {
1743 display_state = g_new0(DisplayState, 1);
1744 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1745 text_console_update_cursor, NULL);
1747 return display_state;
1751 * Called by main(), after creating QemuConsoles
1752 * and before initializing ui (sdl/vnc/...).
1754 DisplayState *init_displaystate(void)
1759 get_alloc_displaystate();
1760 for (i = 0; i < nb_consoles; i++) {
1761 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1762 consoles[i]->ds == NULL) {
1763 text_console_do_init(consoles[i]->chr, display_state);
1766 /* Hook up into the qom tree here (not in new_console()), once
1767 * all QemuConsoles are created and the order / numbering
1768 * doesn't change any more */
1769 name = g_strdup_printf("console[%d]", i);
1770 object_property_add_child(container_get(object_get_root(), "/backend"),
1771 name, OBJECT(consoles[i]), &error_abort);
1775 return display_state;
1778 void graphic_console_set_hwops(QemuConsole *con,
1779 const GraphicHwOps *hw_ops,
1782 con->hw_ops = hw_ops;
1786 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1787 const GraphicHwOps *hw_ops,
1790 static const char noinit[] =
1791 "Guest has not initialized the display (yet).";
1795 if (initial_resolution_width > 0 &&
1796 initial_resolution_height > 0) {
1797 width = initial_resolution_width;
1798 height = initial_resolution_height;
1804 ds = get_alloc_displaystate();
1805 trace_console_gfx_new();
1806 s = new_console(ds, GRAPHIC_CONSOLE, head);
1807 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1808 graphic_console_set_hwops(s, hw_ops, opaque);
1810 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1814 s->surface = qemu_create_message_surface(width, height, noinit);
1818 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1820 if (index >= nb_consoles) {
1823 return consoles[index];
1826 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1832 for (i = 0; i < nb_consoles; i++) {
1836 obj = object_property_get_link(OBJECT(consoles[i]),
1837 "device", &error_abort);
1838 if (DEVICE(obj) != dev) {
1841 h = object_property_get_int(OBJECT(consoles[i]),
1842 "head", &error_abort);
1851 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1852 uint32_t head, Error **errp)
1857 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1859 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1860 "Device '%s' not found", device_id);
1864 con = qemu_console_lookup_by_device(dev, head);
1866 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1874 bool qemu_console_is_visible(QemuConsole *con)
1876 return (con == active_console) || (con->dcls > 0);
1879 bool qemu_console_is_graphic(QemuConsole *con)
1882 con = active_console;
1884 return con && (con->console_type == GRAPHIC_CONSOLE);
1887 bool qemu_console_is_fixedsize(QemuConsole *con)
1890 con = active_console;
1892 return con && (con->console_type != TEXT_CONSOLE);
1895 char *qemu_console_get_label(QemuConsole *con)
1897 if (con->console_type == GRAPHIC_CONSOLE) {
1899 return g_strdup(object_get_typename(con->device));
1901 return g_strdup("VGA");
1903 if (con->chr && con->chr->label) {
1904 return g_strdup(con->chr->label);
1906 return g_strdup_printf("vc%d", con->index);
1910 int qemu_console_get_index(QemuConsole *con)
1913 con = active_console;
1915 return con ? con->index : -1;
1918 uint32_t qemu_console_get_head(QemuConsole *con)
1921 con = active_console;
1923 return con ? con->head : -1;
1926 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1928 assert(con != NULL);
1929 return &con->ui_info;
1932 int qemu_console_get_width(QemuConsole *con, int fallback)
1935 con = active_console;
1937 return con ? surface_width(con->surface) : fallback;
1940 int qemu_console_get_height(QemuConsole *con, int fallback)
1943 con = active_console;
1945 return con ? surface_height(con->surface) : fallback;
1948 static void text_console_set_echo(CharDriverState *chr, bool echo)
1950 QemuConsole *s = chr->opaque;
1955 static void text_console_update_cursor_timer(void)
1957 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1958 + CONSOLE_CURSOR_PERIOD / 2);
1961 static void text_console_update_cursor(void *opaque)
1966 cursor_visible_phase = !cursor_visible_phase;
1968 for (i = 0; i < nb_consoles; i++) {
1970 if (qemu_console_is_graphic(s) ||
1971 !qemu_console_is_visible(s)) {
1975 graphic_hw_invalidate(s);
1979 text_console_update_cursor_timer();
1983 static const GraphicHwOps text_console_ops = {
1984 .invalidate = text_console_invalidate,
1985 .text_update = text_console_update,
1988 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1991 int g_width = 80 * FONT_WIDTH;
1992 int g_height = 24 * FONT_HEIGHT;
1996 chr->chr_write = console_puts;
1998 s->out_fifo.buf = s->out_fifo_buf;
1999 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
2000 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
2005 s->total_height = DEFAULT_BACKSCROLL;
2009 if (active_console && active_console->surface) {
2010 g_width = surface_width(active_console->surface);
2011 g_height = surface_height(active_console->surface);
2013 s->surface = qemu_create_displaysurface(g_width, g_height);
2016 s->hw_ops = &text_console_ops;
2019 /* Set text attribute defaults */
2020 s->t_attrib_default.bold = 0;
2021 s->t_attrib_default.uline = 0;
2022 s->t_attrib_default.blink = 0;
2023 s->t_attrib_default.invers = 0;
2024 s->t_attrib_default.unvisible = 0;
2025 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2026 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2027 /* set current text attributes to default */
2028 s->t_attrib = s->t_attrib_default;
2029 text_console_resize(s);
2035 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2036 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2037 console_puts(chr, (uint8_t*)msg, len);
2038 s->t_attrib = s->t_attrib_default;
2041 qemu_chr_be_generic_open(chr);
2046 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2048 ChardevCommon *common = qapi_ChardevVC_base(vc);
2049 CharDriverState *chr;
2052 unsigned height = 0;
2054 chr = qemu_chr_alloc(common, errp);
2059 if (vc->has_width) {
2061 } else if (vc->has_cols) {
2062 width = vc->cols * FONT_WIDTH;
2065 if (vc->has_height) {
2066 height = vc->height;
2067 } else if (vc->has_rows) {
2068 height = vc->rows * FONT_HEIGHT;
2071 trace_console_txt_new(width, height);
2072 if (width == 0 || height == 0) {
2073 s = new_console(NULL, TEXT_CONSOLE, 0);
2075 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2076 s->surface = qemu_create_displaysurface(width, height);
2081 error_setg(errp, "cannot create text console");
2087 chr->chr_set_echo = text_console_set_echo;
2088 /* console/chardev init sometimes completes elsewhere in a 2nd
2089 * stage, so defer OPENED events until they are fully initialized
2091 chr->explicit_be_open = true;
2093 if (display_state) {
2094 text_console_do_init(chr, display_state);
2099 static VcHandler *vc_handler = text_console_init;
2101 static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2102 ChardevReturn *ret, Error **errp)
2104 return vc_handler(backend->u.vc.data, errp);
2107 void register_vc_handler(VcHandler *handler)
2109 vc_handler = handler;
2112 void qemu_console_resize(QemuConsole *s, int width, int height)
2114 DisplaySurface *surface;
2116 assert(s->console_type == GRAPHIC_CONSOLE);
2117 surface = qemu_create_displaysurface(width, height);
2118 dpy_gfx_replace_surface(s, surface);
2121 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
2122 int dst_x, int dst_y, int w, int h)
2124 assert(con->console_type == GRAPHIC_CONSOLE);
2125 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
2128 DisplaySurface *qemu_console_surface(QemuConsole *console)
2130 return console->surface;
2133 PixelFormat qemu_default_pixelformat(int bpp)
2135 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2136 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2140 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2146 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2147 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2149 val = qemu_opt_get_number(opts, "width", 0);
2151 vc->has_width = true;
2155 val = qemu_opt_get_number(opts, "height", 0);
2157 vc->has_height = true;
2161 val = qemu_opt_get_number(opts, "cols", 0);
2163 vc->has_cols = true;
2167 val = qemu_opt_get_number(opts, "rows", 0);
2169 vc->has_rows = true;
2174 static const TypeInfo qemu_console_info = {
2175 .name = TYPE_QEMU_CONSOLE,
2176 .parent = TYPE_OBJECT,
2177 .instance_size = sizeof(QemuConsole),
2178 .class_size = sizeof(QemuConsoleClass),
2182 static void register_types(void)
2184 type_register_static(&qemu_console_info);
2185 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
2189 type_init(register_types);