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
26 //#define DEBUG_CONSOLE
27 #define DEFAULT_BACKSCROLL 512
28 #define MAX_CONSOLES 12
30 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
31 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
33 typedef struct TextAttributes {
43 typedef struct TextCell {
45 TextAttributes t_attrib;
48 #define MAX_ESC_PARAMS 3
56 typedef struct QEMUFIFO {
59 int count, wptr, rptr;
62 int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
66 l = f->buf_size - f->count;
71 l = f->buf_size - f->wptr;
74 memcpy(f->buf + f->wptr, buf, l);
76 if (f->wptr >= f->buf_size)
85 int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
93 l = f->buf_size - f->rptr;
96 memcpy(buf, f->buf + f->rptr, l);
98 if (f->rptr >= f->buf_size)
107 /* ??? This is mis-named.
108 It is used for both text and graphical consoles. */
110 int text_console; /* true if text console */
112 /* Graphic console state. */
113 vga_hw_update_ptr hw_update;
114 vga_hw_invalidate_ptr hw_invalidate;
115 vga_hw_screen_dump_ptr hw_screen_dump;
118 int g_width, g_height;
122 int backscroll_height;
126 TextAttributes t_attrib_default; /* default text attributes */
127 TextAttributes t_attrib; /* currently active text attributes */
131 int esc_params[MAX_ESC_PARAMS];
134 /* kbd read handler */
135 IOCanRWHandler *fd_can_read;
136 IOReadHandler *fd_read;
138 /* fifo for key pressed */
140 uint8_t out_fifo_buf[16];
141 QEMUTimer *kbd_timer;
144 static TextConsole *active_console;
145 static TextConsole *consoles[MAX_CONSOLES];
146 static int nb_consoles = 0;
148 void vga_hw_update(void)
150 if (active_console->hw_update)
151 active_console->hw_update(active_console->hw);
154 void vga_hw_invalidate(void)
156 if (active_console->hw_invalidate)
157 active_console->hw_invalidate(active_console->hw);
160 void vga_hw_screen_dump(const char *filename)
162 /* There is currently no was of specifying which screen we want to dump,
163 so always dump the dirst one. */
164 if (consoles[0]->hw_screen_dump)
165 consoles[0]->hw_screen_dump(consoles[0]->hw, filename);
168 /* convert a RGBA color to a color index usable in graphic primitives */
169 static unsigned int vga_get_color(DisplayState *ds, unsigned int rgba)
171 unsigned int r, g, b, color;
176 r = (rgba >> 16) & 0xff;
177 g = (rgba >> 8) & 0xff;
179 color = (rgb_to_index[r] * 6 * 6) +
180 (rgb_to_index[g] * 6) +
185 r = (rgba >> 16) & 0xff;
186 g = (rgba >> 8) & 0xff;
188 color = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
191 r = (rgba >> 16) & 0xff;
192 g = (rgba >> 8) & 0xff;
194 color = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
204 static void vga_fill_rect (DisplayState *ds,
205 int posx, int posy, int width, int height, uint32_t color)
210 bpp = (ds->depth + 7) >> 3;
212 ds->linesize * posy + bpp * posx;
213 for (y = 0; y < height; y++) {
217 for (x = 0; x < width; x++) {
218 *((uint8_t *)d) = color;
223 for (x = 0; x < width; x++) {
224 *((uint16_t *)d) = color;
229 for (x = 0; x < width; x++) {
230 *((uint32_t *)d) = color;
239 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
240 static void vga_bitblt(DisplayState *ds, int xs, int ys, int xd, int yd, int w, int h)
246 bpp = (ds->depth + 7) >> 3;
250 ds->linesize * ys + bpp * xs;
252 ds->linesize * yd + bpp * xd;
253 for (y = 0; y < h; y++) {
260 ds->linesize * (ys + h - 1) + bpp * xs;
262 ds->linesize * (yd + h - 1) + bpp * xd;
263 for (y = 0; y < h; y++) {
271 /***********************************************************/
272 /* basic char display */
274 #define FONT_HEIGHT 16
279 #define cbswap_32(__x) \
281 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
282 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
283 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
284 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
286 #ifdef WORDS_BIGENDIAN
289 #define PAT(x) cbswap_32(x)
292 static const uint32_t dmask16[16] = {
311 static const uint32_t dmask4[4] = {
318 static uint32_t color_table[2][8];
331 static const uint32_t color_table_rgb[2][8] = {
333 QEMU_RGB(0x00, 0x00, 0x00), /* black */
334 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
335 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
336 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
337 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
338 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
339 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
340 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
343 QEMU_RGB(0x00, 0x00, 0x00), /* black */
344 QEMU_RGB(0xff, 0x00, 0x00), /* red */
345 QEMU_RGB(0x00, 0xff, 0x00), /* green */
346 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
347 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
348 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
349 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
350 QEMU_RGB(0xff, 0xff, 0xff), /* white */
354 static inline unsigned int col_expand(DisplayState *ds, unsigned int col)
372 static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
374 if (t_attrib->bold) {
379 if (t_attrib->uline) {
384 if (t_attrib->blink) {
389 if (t_attrib->invers) {
394 if (t_attrib->unvisible) {
400 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
404 static void vga_putcharxy(DisplayState *ds, int x, int y, int ch,
405 TextAttributes *t_attrib)
408 const uint8_t *font_ptr;
409 unsigned int font_data, linesize, xorcol, bpp;
411 unsigned int fgcol, bgcol;
414 printf("x: %2i y: %2i", x, y);
415 console_print_text_attributes(t_attrib, ch);
418 if (t_attrib->invers) {
419 bgcol = color_table[t_attrib->bold][t_attrib->fgcol];
420 fgcol = color_table[t_attrib->bold][t_attrib->bgcol];
422 fgcol = color_table[t_attrib->bold][t_attrib->fgcol];
423 bgcol = color_table[t_attrib->bold][t_attrib->bgcol];
426 bpp = (ds->depth + 7) >> 3;
428 ds->linesize * y * FONT_HEIGHT + bpp * x * FONT_WIDTH;
429 linesize = ds->linesize;
430 font_ptr = vgafont16 + FONT_HEIGHT * ch;
431 xorcol = bgcol ^ fgcol;
434 for(i = 0; i < FONT_HEIGHT; i++) {
435 font_data = *font_ptr++;
437 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
440 ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
441 ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
447 for(i = 0; i < FONT_HEIGHT; i++) {
448 font_data = *font_ptr++;
450 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
453 ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
454 ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
455 ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
456 ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
461 for(i = 0; i < FONT_HEIGHT; i++) {
462 font_data = *font_ptr++;
463 if (t_attrib->uline && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
466 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
467 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
468 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
469 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
470 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
471 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
472 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
473 ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
480 static void text_console_resize(TextConsole *s)
482 TextCell *cells, *c, *c1;
483 int w1, x, y, last_width;
485 last_width = s->width;
486 s->width = s->g_width / FONT_WIDTH;
487 s->height = s->g_height / FONT_HEIGHT;
493 cells = qemu_malloc(s->width * s->total_height * sizeof(TextCell));
494 for(y = 0; y < s->total_height; y++) {
495 c = &cells[y * s->width];
497 c1 = &s->cells[y * last_width];
498 for(x = 0; x < w1; x++) {
502 for(x = w1; x < s->width; x++) {
504 c->t_attrib = s->t_attrib_default;
512 static void update_xy(TextConsole *s, int x, int y)
517 if (s == active_console) {
518 y1 = (s->y_base + y) % s->total_height;
519 y2 = y1 - s->y_displayed;
521 y2 += s->total_height;
522 if (y2 < s->height) {
523 c = &s->cells[y1 * s->width + x];
524 vga_putcharxy(s->ds, x, y2, c->ch,
526 dpy_update(s->ds, x * FONT_WIDTH, y2 * FONT_HEIGHT,
527 FONT_WIDTH, FONT_HEIGHT);
532 static void console_show_cursor(TextConsole *s, int show)
537 if (s == active_console) {
538 y1 = (s->y_base + s->y) % s->total_height;
539 y = y1 - s->y_displayed;
541 y += s->total_height;
543 c = &s->cells[y1 * s->width + s->x];
545 TextAttributes t_attrib = s->t_attrib_default;
546 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
547 vga_putcharxy(s->ds, s->x, y, c->ch, &t_attrib);
549 vga_putcharxy(s->ds, s->x, y, c->ch,
552 dpy_update(s->ds, s->x * FONT_WIDTH, y * FONT_HEIGHT,
553 FONT_WIDTH, FONT_HEIGHT);
558 static void console_refresh(TextConsole *s)
563 if (s != active_console)
566 vga_fill_rect(s->ds, 0, 0, s->ds->width, s->ds->height,
567 color_table[0][COLOR_BLACK]);
569 for(y = 0; y < s->height; y++) {
570 c = s->cells + y1 * s->width;
571 for(x = 0; x < s->width; x++) {
572 vga_putcharxy(s->ds, x, y, c->ch,
576 if (++y1 == s->total_height)
579 dpy_update(s->ds, 0, 0, s->ds->width, s->ds->height);
580 console_show_cursor(s, 1);
583 static void console_scroll(int ydelta)
589 if (!s || !s->text_console)
593 for(i = 0; i < ydelta; i++) {
594 if (s->y_displayed == s->y_base)
596 if (++s->y_displayed == s->total_height)
601 i = s->backscroll_height;
602 if (i > s->total_height - s->height)
603 i = s->total_height - s->height;
606 y1 += s->total_height;
607 for(i = 0; i < ydelta; i++) {
608 if (s->y_displayed == y1)
610 if (--s->y_displayed < 0)
611 s->y_displayed = s->total_height - 1;
617 static void console_put_lf(TextConsole *s)
623 if (s->y >= s->height) {
624 s->y = s->height - 1;
626 if (s->y_displayed == s->y_base) {
627 if (++s->y_displayed == s->total_height)
630 if (++s->y_base == s->total_height)
632 if (s->backscroll_height < s->total_height)
633 s->backscroll_height++;
634 y1 = (s->y_base + s->height - 1) % s->total_height;
635 c = &s->cells[y1 * s->width];
636 for(x = 0; x < s->width; x++) {
638 c->t_attrib = s->t_attrib_default;
641 if (s == active_console && s->y_displayed == s->y_base) {
642 vga_bitblt(s->ds, 0, FONT_HEIGHT, 0, 0,
643 s->width * FONT_WIDTH,
644 (s->height - 1) * FONT_HEIGHT);
645 vga_fill_rect(s->ds, 0, (s->height - 1) * FONT_HEIGHT,
646 s->width * FONT_WIDTH, FONT_HEIGHT,
647 color_table[0][s->t_attrib_default.bgcol]);
648 dpy_update(s->ds, 0, 0,
649 s->width * FONT_WIDTH, s->height * FONT_HEIGHT);
654 /* Set console attributes depending on the current escape codes.
655 * NOTE: I know this code is not very efficient (checking every color for it
656 * self) but it is more readable and better maintainable.
658 static void console_handle_escape(TextConsole *s)
662 if (s->nb_esc_params == 0) { /* ESC[m sets all attributes to default */
663 s->t_attrib = s->t_attrib_default;
666 for (i=0; i<s->nb_esc_params; i++) {
667 switch (s->esc_params[i]) {
668 case 0: /* reset all console attributes to default */
669 s->t_attrib = s->t_attrib_default;
672 s->t_attrib.bold = 1;
675 s->t_attrib.uline = 1;
678 s->t_attrib.blink = 1;
681 s->t_attrib.invers = 1;
684 s->t_attrib.unvisible = 1;
687 s->t_attrib.bold = 0;
690 s->t_attrib.uline = 0;
693 s->t_attrib.blink = 0;
696 s->t_attrib.invers = 0;
699 s->t_attrib.unvisible = 0;
701 /* set foreground color */
703 s->t_attrib.fgcol=COLOR_BLACK;
706 s->t_attrib.fgcol=COLOR_RED;
709 s->t_attrib.fgcol=COLOR_GREEN;
712 s->t_attrib.fgcol=COLOR_YELLOW;
715 s->t_attrib.fgcol=COLOR_BLUE;
718 s->t_attrib.fgcol=COLOR_MAGENTA;
721 s->t_attrib.fgcol=COLOR_CYAN;
724 s->t_attrib.fgcol=COLOR_WHITE;
726 /* set background color */
728 s->t_attrib.bgcol=COLOR_BLACK;
731 s->t_attrib.bgcol=COLOR_RED;
734 s->t_attrib.bgcol=COLOR_GREEN;
737 s->t_attrib.bgcol=COLOR_YELLOW;
740 s->t_attrib.bgcol=COLOR_BLUE;
743 s->t_attrib.bgcol=COLOR_MAGENTA;
746 s->t_attrib.bgcol=COLOR_CYAN;
749 s->t_attrib.bgcol=COLOR_WHITE;
755 static void console_putchar(TextConsole *s, int ch)
763 case '\r': /* carriage return */
766 case '\n': /* newline */
769 case '\b': /* backspace */
773 case '\t': /* tabspace */
774 if (s->x + (8 - (s->x % 8)) > s->width) {
778 s->x = s->x + (8 - (s->x % 8));
781 case '\a': /* alert aka. bell */
782 /* TODO: has to be implemented */
784 case 27: /* esc (introducing an escape sequence) */
785 s->state = TTY_STATE_ESC;
788 y1 = (s->y_base + s->y) % s->total_height;
789 c = &s->cells[y1 * s->width + s->x];
791 c->t_attrib = s->t_attrib;
792 update_xy(s, s->x, s->y);
794 if (s->x >= s->width) {
801 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
803 for(i=0;i<MAX_ESC_PARAMS;i++)
804 s->esc_params[i] = 0;
805 s->nb_esc_params = 0;
806 s->state = TTY_STATE_CSI;
808 s->state = TTY_STATE_NORM;
811 case TTY_STATE_CSI: /* handle escape sequence parameters */
812 if (ch >= '0' && ch <= '9') {
813 if (s->nb_esc_params < MAX_ESC_PARAMS) {
814 s->esc_params[s->nb_esc_params] =
815 s->esc_params[s->nb_esc_params] * 10 + ch - '0';
821 s->state = TTY_STATE_NORM;
828 if (s->x < (s->width - 1))
833 y1 = (s->y_base + s->y) % s->total_height;
834 for(x = s->x; x < s->width; x++) {
835 c = &s->cells[y1 * s->width + x];
837 c->t_attrib = s->t_attrib_default;
839 update_xy(s, x, s->y);
845 console_handle_escape(s);
851 void console_select(unsigned int index)
855 if (index >= MAX_CONSOLES)
860 if (s->text_console) {
861 if (s->g_width != s->ds->width ||
862 s->g_height != s->ds->height) {
863 s->g_width = s->ds->width;
864 s->g_height = s->ds->height;
865 text_console_resize(s);
874 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
876 TextConsole *s = chr->opaque;
879 console_show_cursor(s, 0);
880 for(i = 0; i < len; i++) {
881 console_putchar(s, buf[i]);
883 console_show_cursor(s, 1);
887 static void console_chr_add_read_handler(CharDriverState *chr,
888 IOCanRWHandler *fd_can_read,
889 IOReadHandler *fd_read, void *opaque)
891 TextConsole *s = chr->opaque;
892 s->fd_can_read = fd_can_read;
893 s->fd_read = fd_read;
894 s->fd_opaque = opaque;
897 static void console_send_event(CharDriverState *chr, int event)
899 TextConsole *s = chr->opaque;
902 if (event == CHR_EVENT_FOCUS) {
903 for(i = 0; i < nb_consoles; i++) {
904 if (consoles[i] == s) {
912 static void kbd_send_chars(void *opaque)
914 TextConsole *s = opaque;
918 len = s->fd_can_read(s->fd_opaque);
919 if (len > s->out_fifo.count)
920 len = s->out_fifo.count;
922 if (len > sizeof(buf))
924 qemu_fifo_read(&s->out_fifo, buf, len);
925 s->fd_read(s->fd_opaque, buf, len);
927 /* characters are pending: we send them a bit later (XXX:
928 horrible, should change char device API) */
929 if (s->out_fifo.count > 0) {
930 qemu_mod_timer(s->kbd_timer, qemu_get_clock(rt_clock) + 1);
934 /* called when an ascii key is pressed */
935 void kbd_put_keysym(int keysym)
942 if (!s || !s->text_console)
946 case QEMU_KEY_CTRL_UP:
949 case QEMU_KEY_CTRL_DOWN:
952 case QEMU_KEY_CTRL_PAGEUP:
955 case QEMU_KEY_CTRL_PAGEDOWN:
959 /* convert the QEMU keysym to VT100 key string */
961 if (keysym >= 0xe100 && keysym <= 0xe11f) {
966 *q++ = '0' + (c / 10);
967 *q++ = '0' + (c % 10);
969 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
972 *q++ = keysym & 0xff;
977 qemu_fifo_write(&s->out_fifo, buf, q - buf);
984 static TextConsole *new_console(DisplayState *ds, int text)
989 if (nb_consoles >= MAX_CONSOLES)
991 s = qemu_mallocz(sizeof(TextConsole));
995 if (!active_console || (active_console->text_console && !text))
998 s->text_console = text;
1000 consoles[nb_consoles++] = s;
1002 /* HACK: Put graphical consoles before text consoles. */
1003 for (i = nb_consoles; i > 0; i--) {
1004 if (!consoles[i - 1]->text_console)
1006 consoles[i] = consoles[i - 1];
1013 TextConsole *graphic_console_init(DisplayState *ds, vga_hw_update_ptr update,
1014 vga_hw_invalidate_ptr invalidate,
1015 vga_hw_screen_dump_ptr screen_dump,
1020 s = new_console(ds, 0);
1023 s->hw_update = update;
1024 s->hw_invalidate = invalidate;
1025 s->hw_screen_dump = screen_dump;
1030 int is_graphic_console(void)
1032 return !active_console->text_console;
1035 CharDriverState *text_console_init(DisplayState *ds)
1037 CharDriverState *chr;
1040 static int color_inited;
1042 chr = qemu_mallocz(sizeof(CharDriverState));
1045 s = new_console(ds, 1);
1051 chr->chr_write = console_puts;
1052 chr->chr_add_read_handler = console_chr_add_read_handler;
1053 chr->chr_send_event = console_send_event;
1055 s->out_fifo.buf = s->out_fifo_buf;
1056 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1057 s->kbd_timer = qemu_new_timer(rt_clock, kbd_send_chars, s);
1059 if (!color_inited) {
1061 for(j = 0; j < 2; j++) {
1062 for(i = 0; i < 8; i++) {
1063 color_table[j][i] = col_expand(s->ds,
1064 vga_get_color(s->ds, color_table_rgb[j][i]));
1070 s->total_height = DEFAULT_BACKSCROLL;
1073 s->g_width = s->ds->width;
1074 s->g_height = s->ds->height;
1076 /* Set text attribute defaults */
1077 s->t_attrib_default.bold = 0;
1078 s->t_attrib_default.uline = 0;
1079 s->t_attrib_default.blink = 0;
1080 s->t_attrib_default.invers = 0;
1081 s->t_attrib_default.unvisible = 0;
1082 s->t_attrib_default.fgcol = COLOR_WHITE;
1083 s->t_attrib_default.bgcol = COLOR_BLACK;
1085 /* set current text attributes to default */
1086 s->t_attrib = s->t_attrib_default;
1087 text_console_resize(s);