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)
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 (s == active_console && s->y_displayed == s->y_base) {
643 vga_bitblt(s->ds, 0, FONT_HEIGHT, 0, 0,
644 s->width * FONT_WIDTH,
645 (s->height - 1) * FONT_HEIGHT);
646 vga_fill_rect(s->ds, 0, (s->height - 1) * FONT_HEIGHT,
647 s->width * FONT_WIDTH, FONT_HEIGHT,
648 color_table[0][s->t_attrib_default.bgcol]);
649 dpy_update(s->ds, 0, 0,
650 s->width * FONT_WIDTH, s->height * FONT_HEIGHT);
655 /* Set console attributes depending on the current escape codes.
656 * NOTE: I know this code is not very efficient (checking every color for it
657 * self) but it is more readable and better maintainable.
659 static void console_handle_escape(TextConsole *s)
663 if (s->nb_esc_params == 0) { /* ESC[m sets all attributes to default */
664 s->t_attrib = s->t_attrib_default;
667 for (i=0; i<s->nb_esc_params; i++) {
668 switch (s->esc_params[i]) {
669 case 0: /* reset all console attributes to default */
670 s->t_attrib = s->t_attrib_default;
673 s->t_attrib.bold = 1;
676 s->t_attrib.uline = 1;
679 s->t_attrib.blink = 1;
682 s->t_attrib.invers = 1;
685 s->t_attrib.unvisible = 1;
688 s->t_attrib.bold = 0;
691 s->t_attrib.uline = 0;
694 s->t_attrib.blink = 0;
697 s->t_attrib.invers = 0;
700 s->t_attrib.unvisible = 0;
702 /* set foreground color */
704 s->t_attrib.fgcol=COLOR_BLACK;
707 s->t_attrib.fgcol=COLOR_RED;
710 s->t_attrib.fgcol=COLOR_GREEN;
713 s->t_attrib.fgcol=COLOR_YELLOW;
716 s->t_attrib.fgcol=COLOR_BLUE;
719 s->t_attrib.fgcol=COLOR_MAGENTA;
722 s->t_attrib.fgcol=COLOR_CYAN;
725 s->t_attrib.fgcol=COLOR_WHITE;
727 /* set background color */
729 s->t_attrib.bgcol=COLOR_BLACK;
732 s->t_attrib.bgcol=COLOR_RED;
735 s->t_attrib.bgcol=COLOR_GREEN;
738 s->t_attrib.bgcol=COLOR_YELLOW;
741 s->t_attrib.bgcol=COLOR_BLUE;
744 s->t_attrib.bgcol=COLOR_MAGENTA;
747 s->t_attrib.bgcol=COLOR_CYAN;
750 s->t_attrib.bgcol=COLOR_WHITE;
756 static void console_putchar(TextConsole *s, int ch)
764 case '\r': /* carriage return */
767 case '\n': /* newline */
770 case '\b': /* backspace */
774 case '\t': /* tabspace */
775 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)
799 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
801 for(i=0;i<MAX_ESC_PARAMS;i++)
802 s->esc_params[i] = 0;
803 s->nb_esc_params = 0;
804 s->state = TTY_STATE_CSI;
806 s->state = TTY_STATE_NORM;
809 case TTY_STATE_CSI: /* handle escape sequence parameters */
810 if (ch >= '0' && ch <= '9') {
811 if (s->nb_esc_params < MAX_ESC_PARAMS) {
812 s->esc_params[s->nb_esc_params] =
813 s->esc_params[s->nb_esc_params] * 10 + ch - '0';
819 s->state = TTY_STATE_NORM;
826 if (s->x < (s->width - 1))
831 y1 = (s->y_base + s->y) % s->total_height;
832 for(x = s->x; x < s->width; x++) {
833 c = &s->cells[y1 * s->width + x];
835 c->t_attrib = s->t_attrib_default;
837 update_xy(s, x, s->y);
843 console_handle_escape(s);
849 void console_select(unsigned int index)
853 if (index >= MAX_CONSOLES)
858 if (s->text_console) {
859 if (s->g_width != s->ds->width ||
860 s->g_height != s->ds->height) {
861 s->g_width = s->ds->width;
862 s->g_height = s->ds->height;
863 text_console_resize(s);
872 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
874 TextConsole *s = chr->opaque;
877 console_show_cursor(s, 0);
878 for(i = 0; i < len; i++) {
879 console_putchar(s, buf[i]);
881 console_show_cursor(s, 1);
885 static void console_chr_add_read_handler(CharDriverState *chr,
886 IOCanRWHandler *fd_can_read,
887 IOReadHandler *fd_read, void *opaque)
889 TextConsole *s = chr->opaque;
890 s->fd_can_read = fd_can_read;
891 s->fd_read = fd_read;
892 s->fd_opaque = opaque;
895 static void console_send_event(CharDriverState *chr, int event)
897 TextConsole *s = chr->opaque;
900 if (event == CHR_EVENT_FOCUS) {
901 for(i = 0; i < nb_consoles; i++) {
902 if (consoles[i] == s) {
910 static void kbd_send_chars(void *opaque)
912 TextConsole *s = opaque;
916 len = s->fd_can_read(s->fd_opaque);
917 if (len > s->out_fifo.count)
918 len = s->out_fifo.count;
920 if (len > sizeof(buf))
922 qemu_fifo_read(&s->out_fifo, buf, len);
923 s->fd_read(s->fd_opaque, buf, len);
925 /* characters are pending: we send them a bit later (XXX:
926 horrible, should change char device API) */
927 if (s->out_fifo.count > 0) {
928 qemu_mod_timer(s->kbd_timer, qemu_get_clock(rt_clock) + 1);
932 /* called when an ascii key is pressed */
933 void kbd_put_keysym(int keysym)
940 if (!s || !s->text_console)
944 case QEMU_KEY_CTRL_UP:
947 case QEMU_KEY_CTRL_DOWN:
950 case QEMU_KEY_CTRL_PAGEUP:
953 case QEMU_KEY_CTRL_PAGEDOWN:
957 /* convert the QEMU keysym to VT100 key string */
959 if (keysym >= 0xe100 && keysym <= 0xe11f) {
964 *q++ = '0' + (c / 10);
965 *q++ = '0' + (c % 10);
967 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
970 *q++ = keysym & 0xff;
975 qemu_fifo_write(&s->out_fifo, buf, q - buf);
982 static TextConsole *new_console(DisplayState *ds, int text)
987 if (nb_consoles >= MAX_CONSOLES)
989 s = qemu_mallocz(sizeof(TextConsole));
993 if (!active_console || (active_console->text_console && !text))
996 s->text_console = text;
998 consoles[nb_consoles++] = s;
1000 /* HACK: Put graphical consoles before text consoles. */
1001 for (i = nb_consoles; i > 0; i--) {
1002 if (!consoles[i - 1]->text_console)
1004 consoles[i] = consoles[i - 1];
1011 TextConsole *graphic_console_init(DisplayState *ds, vga_hw_update_ptr update,
1012 vga_hw_invalidate_ptr invalidate,
1013 vga_hw_screen_dump_ptr screen_dump,
1018 s = new_console(ds, 0);
1021 s->hw_update = update;
1022 s->hw_invalidate = invalidate;
1023 s->hw_screen_dump = screen_dump;
1028 int is_graphic_console(void)
1030 return !active_console->text_console;
1033 CharDriverState *text_console_init(DisplayState *ds)
1035 CharDriverState *chr;
1038 static int color_inited;
1040 chr = qemu_mallocz(sizeof(CharDriverState));
1043 s = new_console(ds, 1);
1049 chr->chr_write = console_puts;
1050 chr->chr_add_read_handler = console_chr_add_read_handler;
1051 chr->chr_send_event = console_send_event;
1053 s->out_fifo.buf = s->out_fifo_buf;
1054 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1055 s->kbd_timer = qemu_new_timer(rt_clock, kbd_send_chars, s);
1057 if (!color_inited) {
1059 for(j = 0; j < 2; j++) {
1060 for(i = 0; i < 8; i++) {
1061 color_table[j][i] = col_expand(s->ds,
1062 vga_get_color(s->ds, color_table_rgb[j][i]));
1068 s->total_height = DEFAULT_BACKSCROLL;
1071 s->g_width = s->ds->width;
1072 s->g_height = s->ds->height;
1074 /* Set text attribute defaults */
1075 s->t_attrib_default.bold = 0;
1076 s->t_attrib_default.uline = 0;
1077 s->t_attrib_default.blink = 0;
1078 s->t_attrib_default.invers = 0;
1079 s->t_attrib_default.unvisible = 0;
1080 s->t_attrib_default.fgcol = COLOR_WHITE;
1081 s->t_attrib_default.bgcol = COLOR_BLACK;
1083 /* set current text attributes to default */
1084 s->t_attrib = s->t_attrib_default;
1085 text_console_resize(s);