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-common.h"
25 #include "ui/console.h"
26 #include "hw/qdev-core.h"
27 #include "qemu/timer.h"
28 #include "qmp-commands.h"
29 #include "sysemu/char.h"
32 #define DEFAULT_BACKSCROLL 512
33 #define MAX_CONSOLES 12
34 #define CONSOLE_CURSOR_PERIOD 500
36 typedef struct TextAttributes {
46 typedef struct TextCell {
48 TextAttributes t_attrib;
51 #define MAX_ESC_PARAMS 3
59 typedef struct QEMUFIFO {
62 int count, wptr, rptr;
65 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
69 l = f->buf_size - f->count;
74 l = f->buf_size - f->wptr;
77 memcpy(f->buf + f->wptr, buf, l);
79 if (f->wptr >= f->buf_size)
88 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
96 l = f->buf_size - f->rptr;
99 memcpy(buf, f->buf + f->rptr, l);
101 if (f->rptr >= f->buf_size)
113 TEXT_CONSOLE_FIXED_SIZE
120 console_type_t console_type;
122 DisplaySurface *surface;
125 /* Graphic console state. */
128 const GraphicHwOps *hw_ops;
131 /* Text console state */
135 int backscroll_height;
137 int x_saved, y_saved;
140 TextAttributes t_attrib_default; /* default text attributes */
141 TextAttributes t_attrib; /* currently active text attributes */
143 int text_x[2], text_y[2], cursor_invalidate;
145 bool cursor_visible_phase;
146 QEMUTimer *cursor_timer;
154 int esc_params[MAX_ESC_PARAMS];
157 CharDriverState *chr;
158 /* fifo for key pressed */
160 uint8_t out_fifo_buf[16];
161 QEMUTimer *kbd_timer;
164 struct DisplayState {
165 QEMUTimer *gui_timer;
166 uint64_t last_update;
167 uint64_t update_interval;
172 QLIST_HEAD(, DisplayChangeListener) listeners;
175 static DisplayState *display_state;
176 static QemuConsole *active_console;
177 static QemuConsole *consoles[MAX_CONSOLES];
178 static int nb_consoles = 0;
180 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
181 static void dpy_refresh(DisplayState *s);
182 static DisplayState *get_alloc_displaystate(void);
184 static void gui_update(void *opaque)
186 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
187 uint64_t dcl_interval;
188 DisplayState *ds = opaque;
189 DisplayChangeListener *dcl;
192 ds->refreshing = true;
194 ds->refreshing = false;
196 QLIST_FOREACH(dcl, &ds->listeners, next) {
197 dcl_interval = dcl->update_interval ?
198 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
199 if (interval > dcl_interval) {
200 interval = dcl_interval;
203 if (ds->update_interval != interval) {
204 ds->update_interval = interval;
205 for (i = 0; i < nb_consoles; i++) {
206 if (consoles[i]->hw_ops->update_interval) {
207 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
210 trace_console_refresh(interval);
212 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
213 timer_mod(ds->gui_timer, ds->last_update + interval);
216 static void gui_setup_refresh(DisplayState *ds)
218 DisplayChangeListener *dcl;
219 bool need_timer = false;
220 bool have_gfx = false;
221 bool have_text = false;
223 QLIST_FOREACH(dcl, &ds->listeners, next) {
224 if (dcl->ops->dpy_refresh != NULL) {
227 if (dcl->ops->dpy_gfx_update != NULL) {
230 if (dcl->ops->dpy_text_update != NULL) {
235 if (need_timer && ds->gui_timer == NULL) {
236 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
237 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
239 if (!need_timer && ds->gui_timer != NULL) {
240 timer_del(ds->gui_timer);
241 timer_free(ds->gui_timer);
242 ds->gui_timer = NULL;
245 ds->have_gfx = have_gfx;
246 ds->have_text = have_text;
249 void graphic_hw_update(QemuConsole *con)
252 con = active_console;
254 if (con && con->hw_ops->gfx_update) {
255 con->hw_ops->gfx_update(con->hw);
259 void graphic_hw_invalidate(QemuConsole *con)
262 con = active_console;
264 if (con && con->hw_ops->invalidate) {
265 con->hw_ops->invalidate(con->hw);
269 static void ppm_save(const char *filename, struct DisplaySurface *ds,
272 int width = pixman_image_get_width(ds->image);
273 int height = pixman_image_get_height(ds->image);
278 pixman_image_t *linebuf;
280 trace_ppm_save(filename, ds);
281 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
283 error_setg(errp, "failed to open file '%s': %s", filename,
287 f = fdopen(fd, "wb");
288 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
293 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
294 for (y = 0; y < height; y++) {
295 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
297 ret = fwrite(pixman_image_get_data(linebuf), 1,
298 pixman_image_get_stride(linebuf), f);
306 qemu_pixman_image_unref(linebuf);
311 error_setg(errp, "failed to write to file '%s': %s", filename,
317 void qmp_screendump(const char *filename, Error **errp)
319 QemuConsole *con = qemu_console_lookup_by_index(0);
320 DisplaySurface *surface;
323 error_setg(errp, "There is no QemuConsole I can screendump from.");
327 graphic_hw_update(con);
328 surface = qemu_console_surface(con);
329 ppm_save(filename, surface, errp);
332 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
335 con = active_console;
337 if (con && con->hw_ops->text_update) {
338 con->hw_ops->text_update(con->hw, chardata);
342 static void vga_fill_rect(QemuConsole *con,
343 int posx, int posy, int width, int height,
344 pixman_color_t color)
346 DisplaySurface *surface = qemu_console_surface(con);
347 pixman_rectangle16_t rect = {
348 .x = posx, .y = posy, .width = width, .height = height
351 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
355 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
356 static void vga_bitblt(QemuConsole *con,
357 int xs, int ys, int xd, int yd, int w, int h)
359 DisplaySurface *surface = qemu_console_surface(con);
361 pixman_image_composite(PIXMAN_OP_SRC,
362 surface->image, NULL, surface->image,
363 xs, ys, 0, 0, xd, yd, w, h);
366 /***********************************************************/
367 /* basic char display */
369 #define FONT_HEIGHT 16
374 #ifndef CONFIG_CURSES
387 #define QEMU_RGB(r, g, b) \
388 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
390 static const pixman_color_t color_table_rgb[2][8] = {
392 QEMU_RGB(0x00, 0x00, 0x00), /* black */
393 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
394 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
395 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
396 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
397 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
398 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
399 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
402 QEMU_RGB(0x00, 0x00, 0x00), /* black */
403 QEMU_RGB(0xff, 0x00, 0x00), /* red */
404 QEMU_RGB(0x00, 0xff, 0x00), /* green */
405 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
406 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
407 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
408 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
409 QEMU_RGB(0xff, 0xff, 0xff), /* white */
413 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
414 TextAttributes *t_attrib)
416 static pixman_image_t *glyphs[256];
417 DisplaySurface *surface = qemu_console_surface(s);
418 pixman_color_t fgcol, bgcol;
420 if (t_attrib->invers) {
421 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
422 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
424 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
425 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
429 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
431 qemu_pixman_glyph_render(glyphs[ch], surface->image,
432 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
435 static void text_console_resize(QemuConsole *s)
437 TextCell *cells, *c, *c1;
438 int w1, x, y, last_width;
440 last_width = s->width;
441 s->width = surface_width(s->surface) / FONT_WIDTH;
442 s->height = surface_height(s->surface) / FONT_HEIGHT;
448 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
449 for(y = 0; y < s->total_height; y++) {
450 c = &cells[y * s->width];
452 c1 = &s->cells[y * last_width];
453 for(x = 0; x < w1; x++) {
457 for(x = w1; x < s->width; x++) {
459 c->t_attrib = s->t_attrib_default;
467 static inline void text_update_xy(QemuConsole *s, int x, int y)
469 s->text_x[0] = MIN(s->text_x[0], x);
470 s->text_x[1] = MAX(s->text_x[1], x);
471 s->text_y[0] = MIN(s->text_y[0], y);
472 s->text_y[1] = MAX(s->text_y[1], y);
475 static void invalidate_xy(QemuConsole *s, int x, int y)
477 if (s->update_x0 > x * FONT_WIDTH)
478 s->update_x0 = x * FONT_WIDTH;
479 if (s->update_y0 > y * FONT_HEIGHT)
480 s->update_y0 = y * FONT_HEIGHT;
481 if (s->update_x1 < (x + 1) * FONT_WIDTH)
482 s->update_x1 = (x + 1) * FONT_WIDTH;
483 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
484 s->update_y1 = (y + 1) * FONT_HEIGHT;
487 static void update_xy(QemuConsole *s, int x, int y)
492 if (!qemu_console_is_visible(s)) {
496 if (s->ds->have_text) {
497 text_update_xy(s, x, y);
500 if (s->ds->have_gfx) {
501 y1 = (s->y_base + y) % s->total_height;
502 y2 = y1 - s->y_displayed;
504 y2 += s->total_height;
505 if (y2 < s->height) {
506 c = &s->cells[y1 * s->width + x];
507 vga_putcharxy(s, x, y2, c->ch,
509 invalidate_xy(s, x, y2);
514 static void console_show_cursor(QemuConsole *s, int show)
520 if (!qemu_console_is_visible(s)) {
524 if (s->ds->have_text) {
525 s->cursor_invalidate = 1;
528 if (s->ds->have_gfx) {
532 y1 = (s->y_base + s->y) % s->total_height;
533 y = y1 - s->y_displayed;
535 y += s->total_height;
537 c = &s->cells[y1 * s->width + x];
538 if (show && s->cursor_visible_phase) {
539 TextAttributes t_attrib = s->t_attrib_default;
540 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
541 vga_putcharxy(s, x, y, c->ch, &t_attrib);
543 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
545 invalidate_xy(s, x, y);
550 static void console_refresh(QemuConsole *s)
552 DisplaySurface *surface = qemu_console_surface(s);
556 if (!qemu_console_is_visible(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 if (s->ds->have_gfx) {
569 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
570 color_table_rgb[0][COLOR_BLACK]);
572 for (y = 0; y < s->height; y++) {
573 c = s->cells + y1 * s->width;
574 for (x = 0; x < s->width; x++) {
575 vga_putcharxy(s, x, y, c->ch,
579 if (++y1 == s->total_height) {
583 console_show_cursor(s, 1);
584 dpy_gfx_update(s, 0, 0,
585 surface_width(surface), surface_height(surface));
589 static void console_scroll(QemuConsole *s, int ydelta)
594 for(i = 0; i < ydelta; i++) {
595 if (s->y_displayed == s->y_base)
597 if (++s->y_displayed == s->total_height)
602 i = s->backscroll_height;
603 if (i > s->total_height - s->height)
604 i = s->total_height - s->height;
607 y1 += s->total_height;
608 for(i = 0; i < ydelta; i++) {
609 if (s->y_displayed == y1)
611 if (--s->y_displayed < 0)
612 s->y_displayed = s->total_height - 1;
618 static void console_put_lf(QemuConsole *s)
624 if (s->y >= s->height) {
625 s->y = s->height - 1;
627 if (s->y_displayed == s->y_base) {
628 if (++s->y_displayed == s->total_height)
631 if (++s->y_base == s->total_height)
633 if (s->backscroll_height < s->total_height)
634 s->backscroll_height++;
635 y1 = (s->y_base + s->height - 1) % s->total_height;
636 c = &s->cells[y1 * s->width];
637 for(x = 0; x < s->width; x++) {
639 c->t_attrib = s->t_attrib_default;
642 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
643 if (s->ds->have_text) {
646 s->text_x[1] = s->width - 1;
647 s->text_y[1] = s->height - 1;
650 if (s->ds->have_gfx) {
651 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
652 s->width * FONT_WIDTH,
653 (s->height - 1) * FONT_HEIGHT);
654 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
655 s->width * FONT_WIDTH, FONT_HEIGHT,
656 color_table_rgb[0][s->t_attrib_default.bgcol]);
659 s->update_x1 = s->width * FONT_WIDTH;
660 s->update_y1 = s->height * FONT_HEIGHT;
666 /* Set console attributes depending on the current escape codes.
667 * NOTE: I know this code is not very efficient (checking every color for it
668 * self) but it is more readable and better maintainable.
670 static void console_handle_escape(QemuConsole *s)
674 for (i=0; i<s->nb_esc_params; i++) {
675 switch (s->esc_params[i]) {
676 case 0: /* reset all console attributes to default */
677 s->t_attrib = s->t_attrib_default;
680 s->t_attrib.bold = 1;
683 s->t_attrib.uline = 1;
686 s->t_attrib.blink = 1;
689 s->t_attrib.invers = 1;
692 s->t_attrib.unvisible = 1;
695 s->t_attrib.bold = 0;
698 s->t_attrib.uline = 0;
701 s->t_attrib.blink = 0;
704 s->t_attrib.invers = 0;
707 s->t_attrib.unvisible = 0;
709 /* set foreground color */
711 s->t_attrib.fgcol=COLOR_BLACK;
714 s->t_attrib.fgcol=COLOR_RED;
717 s->t_attrib.fgcol=COLOR_GREEN;
720 s->t_attrib.fgcol=COLOR_YELLOW;
723 s->t_attrib.fgcol=COLOR_BLUE;
726 s->t_attrib.fgcol=COLOR_MAGENTA;
729 s->t_attrib.fgcol=COLOR_CYAN;
732 s->t_attrib.fgcol=COLOR_WHITE;
734 /* set background color */
736 s->t_attrib.bgcol=COLOR_BLACK;
739 s->t_attrib.bgcol=COLOR_RED;
742 s->t_attrib.bgcol=COLOR_GREEN;
745 s->t_attrib.bgcol=COLOR_YELLOW;
748 s->t_attrib.bgcol=COLOR_BLUE;
751 s->t_attrib.bgcol=COLOR_MAGENTA;
754 s->t_attrib.bgcol=COLOR_CYAN;
757 s->t_attrib.bgcol=COLOR_WHITE;
763 static void console_clear_xy(QemuConsole *s, int x, int y)
765 int y1 = (s->y_base + y) % s->total_height;
766 TextCell *c = &s->cells[y1 * s->width + x];
768 c->t_attrib = s->t_attrib_default;
772 /* set cursor, checking bounds */
773 static void set_cursor(QemuConsole *s, int x, int y)
781 if (y >= s->height) {
792 static void console_putchar(QemuConsole *s, int ch)
801 case '\r': /* carriage return */
804 case '\n': /* newline */
807 case '\b': /* backspace */
811 case '\t': /* tabspace */
812 if (s->x + (8 - (s->x % 8)) > s->width) {
816 s->x = s->x + (8 - (s->x % 8));
819 case '\a': /* alert aka. bell */
820 /* TODO: has to be implemented */
823 /* SI (shift in), character set 0 (ignored) */
826 /* SO (shift out), character set 1 (ignored) */
828 case 27: /* esc (introducing an escape sequence) */
829 s->state = TTY_STATE_ESC;
832 if (s->x >= s->width) {
837 y1 = (s->y_base + s->y) % s->total_height;
838 c = &s->cells[y1 * s->width + s->x];
840 c->t_attrib = s->t_attrib;
841 update_xy(s, s->x, s->y);
846 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
848 for(i=0;i<MAX_ESC_PARAMS;i++)
849 s->esc_params[i] = 0;
850 s->nb_esc_params = 0;
851 s->state = TTY_STATE_CSI;
853 s->state = TTY_STATE_NORM;
856 case TTY_STATE_CSI: /* handle escape sequence parameters */
857 if (ch >= '0' && ch <= '9') {
858 if (s->nb_esc_params < MAX_ESC_PARAMS) {
859 int *param = &s->esc_params[s->nb_esc_params];
860 int digit = (ch - '0');
862 *param = (*param <= (INT_MAX - digit) / 10) ?
863 *param * 10 + digit : INT_MAX;
866 if (s->nb_esc_params < MAX_ESC_PARAMS)
870 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
871 ch, s->nb_esc_params);
872 s->state = TTY_STATE_NORM;
876 if (s->esc_params[0] == 0) {
877 s->esc_params[0] = 1;
879 set_cursor(s, s->x, s->y - s->esc_params[0]);
882 /* move cursor down */
883 if (s->esc_params[0] == 0) {
884 s->esc_params[0] = 1;
886 set_cursor(s, s->x, s->y + s->esc_params[0]);
889 /* move cursor right */
890 if (s->esc_params[0] == 0) {
891 s->esc_params[0] = 1;
893 set_cursor(s, s->x + s->esc_params[0], s->y);
896 /* move cursor left */
897 if (s->esc_params[0] == 0) {
898 s->esc_params[0] = 1;
900 set_cursor(s, s->x - s->esc_params[0], s->y);
903 /* move cursor to column */
904 set_cursor(s, s->esc_params[0] - 1, s->y);
908 /* move cursor to row, column */
909 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
912 switch (s->esc_params[0]) {
914 /* clear to end of screen */
915 for (y = s->y; y < s->height; y++) {
916 for (x = 0; x < s->width; x++) {
917 if (y == s->y && x < s->x) {
920 console_clear_xy(s, x, y);
925 /* clear from beginning of screen */
926 for (y = 0; y <= s->y; 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 entire screen */
937 for (y = 0; y <= s->height; y++) {
938 for (x = 0; x < s->width; x++) {
939 console_clear_xy(s, x, y);
946 switch (s->esc_params[0]) {
949 for(x = s->x; x < s->width; x++) {
950 console_clear_xy(s, x, s->y);
954 /* clear from beginning of line */
955 for (x = 0; x <= s->x; x++) {
956 console_clear_xy(s, x, s->y);
960 /* clear entire line */
961 for(x = 0; x < s->width; x++) {
962 console_clear_xy(s, x, s->y);
968 console_handle_escape(s);
971 /* report cursor position */
972 /* TODO: send ESC[row;colR */
975 /* save cursor position */
980 /* restore cursor position */
985 trace_console_putchar_unhandled(ch);
993 void console_select(unsigned int index)
995 DisplayChangeListener *dcl;
998 if (index >= MAX_CONSOLES)
1001 trace_console_select(index);
1002 s = qemu_console_lookup_by_index(index);
1004 DisplayState *ds = s->ds;
1006 if (active_console && active_console->cursor_timer) {
1007 timer_del(active_console->cursor_timer);
1011 QLIST_FOREACH(dcl, &ds->listeners, next) {
1012 if (dcl->con != NULL) {
1015 if (dcl->ops->dpy_gfx_switch) {
1016 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1019 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1020 surface_height(s->surface));
1022 if (ds->have_text) {
1023 dpy_text_resize(s, s->width, s->height);
1025 if (s->cursor_timer) {
1026 timer_mod(s->cursor_timer,
1027 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1032 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1034 QemuConsole *s = chr->opaque;
1037 s->update_x0 = s->width * FONT_WIDTH;
1038 s->update_y0 = s->height * FONT_HEIGHT;
1041 console_show_cursor(s, 0);
1042 for(i = 0; i < len; i++) {
1043 console_putchar(s, buf[i]);
1045 console_show_cursor(s, 1);
1046 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1047 dpy_gfx_update(s, s->update_x0, s->update_y0,
1048 s->update_x1 - s->update_x0,
1049 s->update_y1 - s->update_y0);
1054 static void kbd_send_chars(void *opaque)
1056 QemuConsole *s = opaque;
1060 len = qemu_chr_be_can_write(s->chr);
1061 if (len > s->out_fifo.count)
1062 len = s->out_fifo.count;
1064 if (len > sizeof(buf))
1066 qemu_fifo_read(&s->out_fifo, buf, len);
1067 qemu_chr_be_write(s->chr, buf, len);
1069 /* characters are pending: we send them a bit later (XXX:
1070 horrible, should change char device API) */
1071 if (s->out_fifo.count > 0) {
1072 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1076 /* called when an ascii key is pressed */
1077 void kbd_put_keysym(int keysym)
1080 uint8_t buf[16], *q;
1084 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1088 case QEMU_KEY_CTRL_UP:
1089 console_scroll(s, -1);
1091 case QEMU_KEY_CTRL_DOWN:
1092 console_scroll(s, 1);
1094 case QEMU_KEY_CTRL_PAGEUP:
1095 console_scroll(s, -10);
1097 case QEMU_KEY_CTRL_PAGEDOWN:
1098 console_scroll(s, 10);
1101 /* convert the QEMU keysym to VT100 key string */
1103 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1106 c = keysym - 0xe100;
1108 *q++ = '0' + (c / 10);
1109 *q++ = '0' + (c % 10);
1111 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1114 *q++ = keysym & 0xff;
1115 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1116 console_puts(s->chr, (const uint8_t *) "\r", 1);
1122 console_puts(s->chr, buf, q - buf);
1124 if (s->chr->chr_read) {
1125 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1132 static void text_console_invalidate(void *opaque)
1134 QemuConsole *s = (QemuConsole *) opaque;
1136 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1137 text_console_resize(s);
1142 static void text_console_update(void *opaque, console_ch_t *chardata)
1144 QemuConsole *s = (QemuConsole *) opaque;
1147 if (s->text_x[0] <= s->text_x[1]) {
1148 src = (s->y_base + s->text_y[0]) * s->width;
1149 chardata += s->text_y[0] * s->width;
1150 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1151 for (j = 0; j < s->width; j ++, src ++)
1152 console_write_ch(chardata ++, s->cells[src].ch |
1153 (s->cells[src].t_attrib.fgcol << 12) |
1154 (s->cells[src].t_attrib.bgcol << 8) |
1155 (s->cells[src].t_attrib.bold << 21));
1156 dpy_text_update(s, s->text_x[0], s->text_y[0],
1157 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1158 s->text_x[0] = s->width;
1159 s->text_y[0] = s->height;
1163 if (s->cursor_invalidate) {
1164 dpy_text_cursor(s, s->x, s->y);
1165 s->cursor_invalidate = 0;
1169 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1171 Error *local_err = NULL;
1176 if (nb_consoles >= MAX_CONSOLES)
1179 obj = object_new(TYPE_QEMU_CONSOLE);
1180 s = QEMU_CONSOLE(obj);
1181 object_property_add_link(obj, "device", TYPE_DEVICE,
1182 (Object **)&s->device, &local_err);
1183 object_property_add_uint32_ptr(obj, "head",
1184 &s->head, &local_err);
1186 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1187 (console_type == GRAPHIC_CONSOLE))) {
1191 s->console_type = console_type;
1192 if (console_type != GRAPHIC_CONSOLE) {
1193 s->index = nb_consoles;
1194 consoles[nb_consoles++] = s;
1196 /* HACK: Put graphical consoles before text consoles. */
1197 for (i = nb_consoles; i > 0; i--) {
1198 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1200 consoles[i] = consoles[i - 1];
1201 consoles[i]->index = i;
1210 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1211 int linesize, PixelFormat pf, int newflags)
1215 qemu_pixman_image_unref(surface->image);
1216 surface->image = NULL;
1218 surface->format = qemu_pixman_get_format(&pf);
1219 assert(surface->format != 0);
1220 surface->image = pixman_image_create_bits(surface->format,
1223 assert(surface->image != NULL);
1225 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1226 #ifdef HOST_WORDS_BIGENDIAN
1227 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1231 DisplaySurface *qemu_create_displaysurface(int width, int height)
1233 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1234 int linesize = width * 4;
1236 trace_displaysurface_create(surface, width, height);
1237 qemu_alloc_display(surface, width, height, linesize,
1238 qemu_default_pixelformat(32), 0);
1242 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1243 int linesize, uint8_t *data,
1246 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1248 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1250 surface->pf = qemu_different_endianness_pixelformat(bpp);
1252 surface->pf = qemu_default_pixelformat(bpp);
1255 surface->format = qemu_pixman_get_format(&surface->pf);
1256 assert(surface->format != 0);
1257 surface->image = pixman_image_create_bits(surface->format,
1259 (void *)data, linesize);
1260 assert(surface->image != NULL);
1262 #ifdef HOST_WORDS_BIGENDIAN
1263 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1269 static DisplaySurface *qemu_create_dummy_surface(void)
1271 static const char msg[] =
1272 "This VM has no graphic display device.";
1273 DisplaySurface *surface = qemu_create_displaysurface(640, 480);
1274 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1275 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1276 pixman_image_t *glyph;
1280 x = (640/FONT_WIDTH - len) / 2;
1281 y = (480/FONT_HEIGHT - 1) / 2;
1282 for (i = 0; i < len; i++) {
1283 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1284 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1285 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1286 qemu_pixman_image_unref(glyph);
1291 void qemu_free_displaysurface(DisplaySurface *surface)
1293 if (surface == NULL) {
1296 trace_displaysurface_free(surface);
1297 qemu_pixman_image_unref(surface->image);
1301 void register_displaychangelistener(DisplayChangeListener *dcl)
1303 static DisplaySurface *dummy;
1306 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1307 dcl->ds = get_alloc_displaystate();
1308 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1309 gui_setup_refresh(dcl->ds);
1314 con = active_console;
1316 if (dcl->ops->dpy_gfx_switch) {
1318 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1321 dummy = qemu_create_dummy_surface();
1323 dcl->ops->dpy_gfx_switch(dcl, dummy);
1328 void update_displaychangelistener(DisplayChangeListener *dcl,
1331 DisplayState *ds = dcl->ds;
1333 dcl->update_interval = interval;
1334 if (!ds->refreshing && ds->update_interval > interval) {
1335 timer_mod(ds->gui_timer, ds->last_update + interval);
1339 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1341 DisplayState *ds = dcl->ds;
1342 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1346 QLIST_REMOVE(dcl, next);
1347 gui_setup_refresh(ds);
1350 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1352 DisplayState *s = con->ds;
1353 DisplayChangeListener *dcl;
1354 int width = surface_width(con->surface);
1355 int height = surface_height(con->surface);
1361 w = MIN(w, width - x);
1362 h = MIN(h, height - y);
1364 if (!qemu_console_is_visible(con)) {
1367 QLIST_FOREACH(dcl, &s->listeners, next) {
1368 if (con != (dcl->con ? dcl->con : active_console)) {
1371 if (dcl->ops->dpy_gfx_update) {
1372 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1377 void dpy_gfx_replace_surface(QemuConsole *con,
1378 DisplaySurface *surface)
1380 DisplayState *s = con->ds;
1381 DisplaySurface *old_surface = con->surface;
1382 DisplayChangeListener *dcl;
1384 con->surface = surface;
1385 QLIST_FOREACH(dcl, &s->listeners, next) {
1386 if (con != (dcl->con ? dcl->con : active_console)) {
1389 if (dcl->ops->dpy_gfx_switch) {
1390 dcl->ops->dpy_gfx_switch(dcl, surface);
1393 qemu_free_displaysurface(old_surface);
1396 void dpy_refresh(DisplayState *s)
1398 DisplayChangeListener *dcl;
1400 QLIST_FOREACH(dcl, &s->listeners, next) {
1401 if (dcl->ops->dpy_refresh) {
1402 dcl->ops->dpy_refresh(dcl);
1407 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1408 int dst_x, int dst_y, int w, int h)
1410 DisplayState *s = con->ds;
1411 DisplayChangeListener *dcl;
1413 if (!qemu_console_is_visible(con)) {
1416 QLIST_FOREACH(dcl, &s->listeners, next) {
1417 if (con != (dcl->con ? dcl->con : active_console)) {
1420 if (dcl->ops->dpy_gfx_copy) {
1421 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1423 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1428 void dpy_text_cursor(QemuConsole *con, int x, int y)
1430 DisplayState *s = con->ds;
1431 DisplayChangeListener *dcl;
1433 if (!qemu_console_is_visible(con)) {
1436 QLIST_FOREACH(dcl, &s->listeners, next) {
1437 if (con != (dcl->con ? dcl->con : active_console)) {
1440 if (dcl->ops->dpy_text_cursor) {
1441 dcl->ops->dpy_text_cursor(dcl, x, y);
1446 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1448 DisplayState *s = con->ds;
1449 DisplayChangeListener *dcl;
1451 if (!qemu_console_is_visible(con)) {
1454 QLIST_FOREACH(dcl, &s->listeners, next) {
1455 if (con != (dcl->con ? dcl->con : active_console)) {
1458 if (dcl->ops->dpy_text_update) {
1459 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1464 void dpy_text_resize(QemuConsole *con, int w, int h)
1466 DisplayState *s = con->ds;
1467 struct DisplayChangeListener *dcl;
1469 if (!qemu_console_is_visible(con)) {
1472 QLIST_FOREACH(dcl, &s->listeners, next) {
1473 if (con != (dcl->con ? dcl->con : active_console)) {
1476 if (dcl->ops->dpy_text_resize) {
1477 dcl->ops->dpy_text_resize(dcl, w, h);
1482 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1484 DisplayState *s = con->ds;
1485 DisplayChangeListener *dcl;
1487 if (!qemu_console_is_visible(con)) {
1490 QLIST_FOREACH(dcl, &s->listeners, next) {
1491 if (con != (dcl->con ? dcl->con : active_console)) {
1494 if (dcl->ops->dpy_mouse_set) {
1495 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1500 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1502 DisplayState *s = con->ds;
1503 DisplayChangeListener *dcl;
1505 if (!qemu_console_is_visible(con)) {
1508 QLIST_FOREACH(dcl, &s->listeners, next) {
1509 if (con != (dcl->con ? dcl->con : active_console)) {
1512 if (dcl->ops->dpy_cursor_define) {
1513 dcl->ops->dpy_cursor_define(dcl, cursor);
1518 bool dpy_cursor_define_supported(QemuConsole *con)
1520 DisplayState *s = con->ds;
1521 DisplayChangeListener *dcl;
1523 QLIST_FOREACH(dcl, &s->listeners, next) {
1524 if (dcl->ops->dpy_cursor_define) {
1531 /***********************************************************/
1532 /* register display */
1534 /* console.c internal use only */
1535 static DisplayState *get_alloc_displaystate(void)
1537 if (!display_state) {
1538 display_state = g_new0(DisplayState, 1);
1540 return display_state;
1544 * Called by main(), after creating QemuConsoles
1545 * and before initializing ui (sdl/vnc/...).
1547 DisplayState *init_displaystate(void)
1549 Error *local_err = NULL;
1553 if (!display_state) {
1554 display_state = g_new0(DisplayState, 1);
1557 for (i = 0; i < nb_consoles; i++) {
1558 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1559 consoles[i]->ds == NULL) {
1560 text_console_do_init(consoles[i]->chr, display_state);
1563 /* Hook up into the qom tree here (not in new_console()), once
1564 * all QemuConsoles are created and the order / numbering
1565 * doesn't change any more */
1566 name = g_strdup_printf("console[%d]", i);
1567 object_property_add_child(container_get(object_get_root(), "/backend"),
1568 name, OBJECT(consoles[i]), &local_err);
1572 return display_state;
1575 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1576 const GraphicHwOps *hw_ops,
1579 Error *local_err = NULL;
1585 ds = get_alloc_displaystate();
1586 trace_console_gfx_new();
1587 s = new_console(ds, GRAPHIC_CONSOLE);
1591 object_property_set_link(OBJECT(s), OBJECT(dev),
1592 "device", &local_err);
1593 object_property_set_int(OBJECT(s), head,
1594 "head", &local_err);
1597 s->surface = qemu_create_displaysurface(width, height);
1601 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1603 if (index >= MAX_CONSOLES) {
1606 return consoles[index];
1609 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1611 Error *local_err = NULL;
1616 for (i = 0; i < nb_consoles; i++) {
1620 obj = object_property_get_link(OBJECT(consoles[i]),
1621 "device", &local_err);
1622 if (DEVICE(obj) != dev) {
1625 h = object_property_get_int(OBJECT(consoles[i]),
1626 "head", &local_err);
1635 bool qemu_console_is_visible(QemuConsole *con)
1637 return (con == active_console) || (con->dcls > 0);
1640 bool qemu_console_is_graphic(QemuConsole *con)
1643 con = active_console;
1645 return con && (con->console_type == GRAPHIC_CONSOLE);
1648 bool qemu_console_is_fixedsize(QemuConsole *con)
1651 con = active_console;
1653 return con && (con->console_type != TEXT_CONSOLE);
1656 int qemu_console_get_index(QemuConsole *con)
1659 con = active_console;
1661 return con ? con->index : -1;
1664 uint32_t qemu_console_get_head(QemuConsole *con)
1667 con = active_console;
1669 return con ? con->head : -1;
1672 int qemu_console_get_width(QemuConsole *con, int fallback)
1675 con = active_console;
1677 return con ? surface_width(con->surface) : fallback;
1680 int qemu_console_get_height(QemuConsole *con, int fallback)
1683 con = active_console;
1685 return con ? surface_height(con->surface) : fallback;
1688 static void text_console_set_echo(CharDriverState *chr, bool echo)
1690 QemuConsole *s = chr->opaque;
1695 static void text_console_update_cursor(void *opaque)
1697 QemuConsole *s = opaque;
1699 s->cursor_visible_phase = !s->cursor_visible_phase;
1700 graphic_hw_invalidate(s);
1701 timer_mod(s->cursor_timer,
1702 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1705 static const GraphicHwOps text_console_ops = {
1706 .invalidate = text_console_invalidate,
1707 .text_update = text_console_update,
1710 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1713 int g_width = 80 * FONT_WIDTH;
1714 int g_height = 24 * FONT_HEIGHT;
1718 chr->chr_write = console_puts;
1720 s->out_fifo.buf = s->out_fifo_buf;
1721 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1722 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1727 s->total_height = DEFAULT_BACKSCROLL;
1731 if (active_console && active_console->surface) {
1732 g_width = surface_width(active_console->surface);
1733 g_height = surface_height(active_console->surface);
1735 s->surface = qemu_create_displaysurface(g_width, g_height);
1739 timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
1741 s->hw_ops = &text_console_ops;
1744 /* Set text attribute defaults */
1745 s->t_attrib_default.bold = 0;
1746 s->t_attrib_default.uline = 0;
1747 s->t_attrib_default.blink = 0;
1748 s->t_attrib_default.invers = 0;
1749 s->t_attrib_default.unvisible = 0;
1750 s->t_attrib_default.fgcol = COLOR_WHITE;
1751 s->t_attrib_default.bgcol = COLOR_BLACK;
1752 /* set current text attributes to default */
1753 s->t_attrib = s->t_attrib_default;
1754 text_console_resize(s);
1760 s->t_attrib.bgcol = COLOR_BLUE;
1761 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1762 console_puts(chr, (uint8_t*)msg, len);
1763 s->t_attrib = s->t_attrib_default;
1766 qemu_chr_be_generic_open(chr);
1771 static CharDriverState *text_console_init(ChardevVC *vc)
1773 CharDriverState *chr;
1776 unsigned height = 0;
1778 chr = g_malloc0(sizeof(CharDriverState));
1780 if (vc->has_width) {
1782 } else if (vc->has_cols) {
1783 width = vc->cols * FONT_WIDTH;
1786 if (vc->has_height) {
1787 height = vc->height;
1788 } else if (vc->has_rows) {
1789 height = vc->rows * FONT_HEIGHT;
1792 trace_console_txt_new(width, height);
1793 if (width == 0 || height == 0) {
1794 s = new_console(NULL, TEXT_CONSOLE);
1796 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1797 s->surface = qemu_create_displaysurface(width, height);
1807 chr->chr_set_echo = text_console_set_echo;
1808 /* console/chardev init sometimes completes elsewhere in a 2nd
1809 * stage, so defer OPENED events until they are fully initialized
1811 chr->explicit_be_open = true;
1813 if (display_state) {
1814 text_console_do_init(chr, display_state);
1819 static VcHandler *vc_handler = text_console_init;
1821 CharDriverState *vc_init(ChardevVC *vc)
1823 return vc_handler(vc);
1826 void register_vc_handler(VcHandler *handler)
1828 vc_handler = handler;
1831 void qemu_console_resize(QemuConsole *s, int width, int height)
1833 DisplaySurface *surface;
1835 assert(s->console_type == GRAPHIC_CONSOLE);
1836 surface = qemu_create_displaysurface(width, height);
1837 dpy_gfx_replace_surface(s, surface);
1840 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1841 int dst_x, int dst_y, int w, int h)
1843 assert(con->console_type == GRAPHIC_CONSOLE);
1844 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1847 DisplaySurface *qemu_console_surface(QemuConsole *console)
1849 return console->surface;
1852 DisplayState *qemu_console_displaystate(QemuConsole *console)
1857 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1861 memset(&pf, 0x00, sizeof(PixelFormat));
1863 pf.bits_per_pixel = bpp;
1864 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1865 pf.depth = bpp == 32 ? 24 : bpp;
1869 pf.rmask = 0x000000FF;
1870 pf.gmask = 0x0000FF00;
1871 pf.bmask = 0x00FF0000;
1883 pf.rmask = 0x0000FF00;
1884 pf.gmask = 0x00FF0000;
1885 pf.bmask = 0xFF000000;
1886 pf.amask = 0x00000000;
1906 PixelFormat qemu_default_pixelformat(int bpp)
1910 memset(&pf, 0x00, sizeof(PixelFormat));
1912 pf.bits_per_pixel = bpp;
1913 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1914 pf.depth = bpp == 32 ? 24 : bpp;
1918 pf.bits_per_pixel = 16;
1919 pf.rmask = 0x00007c00;
1920 pf.gmask = 0x000003E0;
1921 pf.bmask = 0x0000001F;
1933 pf.rmask = 0x0000F800;
1934 pf.gmask = 0x000007E0;
1935 pf.bmask = 0x0000001F;
1947 pf.rmask = 0x00FF0000;
1948 pf.gmask = 0x0000FF00;
1949 pf.bmask = 0x000000FF;
1961 pf.rmask = 0x00FF0000;
1962 pf.gmask = 0x0000FF00;
1963 pf.bmask = 0x000000FF;
1980 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1985 backend->vc = g_new0(ChardevVC, 1);
1987 val = qemu_opt_get_number(opts, "width", 0);
1989 backend->vc->has_width = true;
1990 backend->vc->width = val;
1993 val = qemu_opt_get_number(opts, "height", 0);
1995 backend->vc->has_height = true;
1996 backend->vc->height = val;
1999 val = qemu_opt_get_number(opts, "cols", 0);
2001 backend->vc->has_cols = true;
2002 backend->vc->cols = val;
2005 val = qemu_opt_get_number(opts, "rows", 0);
2007 backend->vc->has_rows = true;
2008 backend->vc->rows = val;
2012 static const TypeInfo qemu_console_info = {
2013 .name = TYPE_QEMU_CONSOLE,
2014 .parent = TYPE_OBJECT,
2015 .instance_size = sizeof(QemuConsole),
2016 .class_size = sizeof(QemuConsoleClass),
2020 static void register_types(void)
2022 type_register_static(&qemu_console_info);
2023 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
2027 type_init(register_types);