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 RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
31 #define RGB(r, g, b) RGBA(r, g, b, 0xff)
33 typedef struct TextAttributes {
43 typedef struct TextCell {
45 TextAttributes t_attrib;
48 #define MAX_ESC_PARAMS 3
56 /* ??? This is mis-named.
57 It is used for both text and graphical consoles. */
59 int text_console; /* true if text console */
61 /* Graphic console state. */
62 vga_hw_update_ptr hw_update;
63 vga_hw_invalidate_ptr hw_invalidate;
64 vga_hw_screen_dump_ptr hw_screen_dump;
67 int g_width, g_height;
71 int backscroll_height;
75 TextAttributes t_attrib_default; /* default text attributes */
76 TextAttributes t_attrib; /* currently active text attributes */
80 int esc_params[MAX_ESC_PARAMS];
83 /* kbd read handler */
84 IOReadHandler *fd_read;
88 static TextConsole *active_console;
89 static TextConsole *consoles[MAX_CONSOLES];
90 static int nb_consoles = 0;
92 void vga_hw_update(void)
94 if (active_console->hw_update)
95 active_console->hw_update(active_console->hw);
98 void vga_hw_invalidate(void)
100 if (active_console->hw_invalidate)
101 active_console->hw_invalidate(active_console->hw);
104 void vga_hw_screen_dump(const char *filename)
106 /* There is currently no was of specifying which screen we want to dump,
107 so always dump the dirst one. */
108 if (consoles[0]->hw_screen_dump)
109 consoles[0]->hw_screen_dump(consoles[0]->hw, filename);
112 /* convert a RGBA color to a color index usable in graphic primitives */
113 static unsigned int vga_get_color(DisplayState *ds, unsigned int rgba)
115 unsigned int r, g, b, color;
120 r = (rgba >> 16) & 0xff;
121 g = (rgba >> 8) & 0xff;
123 color = (rgb_to_index[r] * 6 * 6) +
124 (rgb_to_index[g] * 6) +
129 r = (rgba >> 16) & 0xff;
130 g = (rgba >> 8) & 0xff;
132 color = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
135 r = (rgba >> 16) & 0xff;
136 g = (rgba >> 8) & 0xff;
138 color = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
148 static void vga_fill_rect (DisplayState *ds,
149 int posx, int posy, int width, int height, uint32_t color)
154 bpp = (ds->depth + 7) >> 3;
156 ds->linesize * posy + bpp * posx;
157 for (y = 0; y < height; y++) {
161 for (x = 0; x < width; x++) {
162 *((uint8_t *)d) = color;
167 for (x = 0; x < width; x++) {
168 *((uint16_t *)d) = color;
173 for (x = 0; x < width; x++) {
174 *((uint32_t *)d) = color;
183 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
184 static void vga_bitblt(DisplayState *ds, int xs, int ys, int xd, int yd, int w, int h)
190 bpp = (ds->depth + 7) >> 3;
194 ds->linesize * ys + bpp * xs;
196 ds->linesize * yd + bpp * xd;
197 for (y = 0; y < h; y++) {
204 ds->linesize * (ys + h - 1) + bpp * xs;
206 ds->linesize * (yd + h - 1) + bpp * xd;
207 for (y = 0; y < h; y++) {
215 /***********************************************************/
216 /* basic char display */
218 #define FONT_HEIGHT 16
223 #define cbswap_32(__x) \
225 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
226 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
227 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
228 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
230 #ifdef WORDS_BIGENDIAN
233 #define PAT(x) cbswap_32(x)
236 static const uint32_t dmask16[16] = {
255 static const uint32_t dmask4[4] = {
262 static uint32_t color_table[2][8];
275 static const uint32_t color_table_rgb[2][8] = {
277 RGB(0x00, 0x00, 0x00), /* black */
278 RGB(0xaa, 0x00, 0x00), /* red */
279 RGB(0x00, 0xaa, 0x00), /* green */
280 RGB(0xaa, 0xaa, 0x00), /* yellow */
281 RGB(0x00, 0x00, 0xaa), /* blue */
282 RGB(0xaa, 0x00, 0xaa), /* magenta */
283 RGB(0x00, 0xaa, 0xaa), /* cyan */
284 RGB(0xaa, 0xaa, 0xaa), /* white */
287 RGB(0x00, 0x00, 0x00), /* black */
288 RGB(0xff, 0x00, 0x00), /* red */
289 RGB(0x00, 0xff, 0x00), /* green */
290 RGB(0xff, 0xff, 0x00), /* yellow */
291 RGB(0x00, 0x00, 0xff), /* blue */
292 RGB(0xff, 0x00, 0xff), /* magenta */
293 RGB(0x00, 0xff, 0xff), /* cyan */
294 RGB(0xff, 0xff, 0xff), /* white */
298 static inline unsigned int col_expand(DisplayState *ds, unsigned int col)
316 static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
318 if (t_attrib->bold) {
323 if (t_attrib->uline) {
328 if (t_attrib->blink) {
333 if (t_attrib->invers) {
338 if (t_attrib->unvisible) {
344 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
348 static void vga_putcharxy(DisplayState *ds, int x, int y, int ch,
349 TextAttributes *t_attrib)
352 const uint8_t *font_ptr;
353 unsigned int font_data, linesize, xorcol, bpp;
355 unsigned int fgcol, bgcol;
358 printf("x: %2i y: %2i", x, y);
359 console_print_text_attributes(t_attrib, ch);
362 if (t_attrib->invers) {
363 bgcol = color_table[t_attrib->bold][t_attrib->fgcol];
364 fgcol = color_table[t_attrib->bold][t_attrib->bgcol];
366 fgcol = color_table[t_attrib->bold][t_attrib->fgcol];
367 bgcol = color_table[t_attrib->bold][t_attrib->bgcol];
370 bpp = (ds->depth + 7) >> 3;
372 ds->linesize * y * FONT_HEIGHT + bpp * x * FONT_WIDTH;
373 linesize = ds->linesize;
374 font_ptr = vgafont16 + FONT_HEIGHT * ch;
375 xorcol = bgcol ^ fgcol;
378 for(i = 0; i < FONT_HEIGHT; i++) {
379 font_data = *font_ptr++;
381 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
384 ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
385 ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
391 for(i = 0; i < FONT_HEIGHT; i++) {
392 font_data = *font_ptr++;
394 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
397 ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
398 ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
399 ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
400 ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
405 for(i = 0; i < FONT_HEIGHT; i++) {
406 font_data = *font_ptr++;
407 if (t_attrib->uline && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
410 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
411 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
412 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
413 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
414 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
415 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
416 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
417 ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
424 static void text_console_resize(TextConsole *s)
426 TextCell *cells, *c, *c1;
427 int w1, x, y, last_width;
429 last_width = s->width;
430 s->width = s->g_width / FONT_WIDTH;
431 s->height = s->g_height / FONT_HEIGHT;
437 cells = qemu_malloc(s->width * s->total_height * sizeof(TextCell));
438 for(y = 0; y < s->total_height; y++) {
439 c = &cells[y * s->width];
441 c1 = &s->cells[y * last_width];
442 for(x = 0; x < w1; x++) {
446 for(x = w1; x < s->width; x++) {
448 c->t_attrib = s->t_attrib_default;
456 static void update_xy(TextConsole *s, int x, int y)
461 if (s == active_console) {
462 y1 = (s->y_base + y) % s->total_height;
463 y2 = y1 - s->y_displayed;
465 y2 += s->total_height;
466 if (y2 < s->height) {
467 c = &s->cells[y1 * s->width + x];
468 vga_putcharxy(s->ds, x, y2, c->ch,
470 dpy_update(s->ds, x * FONT_WIDTH, y2 * FONT_HEIGHT,
471 FONT_WIDTH, FONT_HEIGHT);
476 static void console_show_cursor(TextConsole *s, int show)
481 if (s == active_console) {
482 y1 = (s->y_base + s->y) % s->total_height;
483 y = y1 - s->y_displayed;
485 y += s->total_height;
487 c = &s->cells[y1 * s->width + s->x];
489 TextAttributes t_attrib = s->t_attrib_default;
490 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
491 vga_putcharxy(s->ds, s->x, y, c->ch, &t_attrib);
493 vga_putcharxy(s->ds, s->x, y, c->ch,
496 dpy_update(s->ds, s->x * FONT_WIDTH, y * FONT_HEIGHT,
497 FONT_WIDTH, FONT_HEIGHT);
502 static void console_refresh(TextConsole *s)
507 if (s != active_console)
510 vga_fill_rect(s->ds, 0, 0, s->ds->width, s->ds->height,
511 color_table[0][COLOR_BLACK]);
513 for(y = 0; y < s->height; y++) {
514 c = s->cells + y1 * s->width;
515 for(x = 0; x < s->width; x++) {
516 vga_putcharxy(s->ds, x, y, c->ch,
520 if (++y1 == s->total_height)
523 dpy_update(s->ds, 0, 0, s->ds->width, s->ds->height);
524 console_show_cursor(s, 1);
527 static void console_scroll(int ydelta)
533 if (!s || !s->text_console)
537 for(i = 0; i < ydelta; i++) {
538 if (s->y_displayed == s->y_base)
540 if (++s->y_displayed == s->total_height)
545 i = s->backscroll_height;
546 if (i > s->total_height - s->height)
547 i = s->total_height - s->height;
550 y1 += s->total_height;
551 for(i = 0; i < ydelta; i++) {
552 if (s->y_displayed == y1)
554 if (--s->y_displayed < 0)
555 s->y_displayed = s->total_height - 1;
561 static void console_put_lf(TextConsole *s)
568 if (s->y >= s->height) {
569 s->y = s->height - 1;
571 if (s->y_displayed == s->y_base) {
572 if (++s->y_displayed == s->total_height)
575 if (++s->y_base == s->total_height)
577 if (s->backscroll_height < s->total_height)
578 s->backscroll_height++;
579 y1 = (s->y_base + s->height - 1) % s->total_height;
580 c = &s->cells[y1 * s->width];
581 for(x = 0; x < s->width; x++) {
583 c->t_attrib = s->t_attrib_default;
586 if (s == active_console && s->y_displayed == s->y_base) {
587 vga_bitblt(s->ds, 0, FONT_HEIGHT, 0, 0,
588 s->width * FONT_WIDTH,
589 (s->height - 1) * FONT_HEIGHT);
590 vga_fill_rect(s->ds, 0, (s->height - 1) * FONT_HEIGHT,
591 s->width * FONT_WIDTH, FONT_HEIGHT,
592 color_table[0][s->t_attrib_default.bgcol]);
593 dpy_update(s->ds, 0, 0,
594 s->width * FONT_WIDTH, s->height * FONT_HEIGHT);
599 /* Set console attributes depending on the current escape codes.
600 * NOTE: I know this code is not very efficient (checking every color for it
601 * self) but it is more readable and better maintainable.
603 static void console_handle_escape(TextConsole *s)
607 if (s->nb_esc_params == 0) { /* ESC[m sets all attributes to default */
608 s->t_attrib = s->t_attrib_default;
611 for (i=0; i<s->nb_esc_params; i++) {
612 switch (s->esc_params[i]) {
613 case 0: /* reset all console attributes to default */
614 s->t_attrib = s->t_attrib_default;
617 s->t_attrib.bold = 1;
620 s->t_attrib.uline = 1;
623 s->t_attrib.blink = 1;
626 s->t_attrib.invers = 1;
629 s->t_attrib.unvisible = 1;
632 s->t_attrib.bold = 0;
635 s->t_attrib.uline = 0;
638 s->t_attrib.blink = 0;
641 s->t_attrib.invers = 0;
644 s->t_attrib.unvisible = 0;
646 /* set foreground color */
648 s->t_attrib.fgcol=COLOR_BLACK;
651 s->t_attrib.fgcol=COLOR_RED;
654 s->t_attrib.fgcol=COLOR_GREEN;
657 s->t_attrib.fgcol=COLOR_YELLOW;
660 s->t_attrib.fgcol=COLOR_BLUE;
663 s->t_attrib.fgcol=COLOR_MAGENTA;
666 s->t_attrib.fgcol=COLOR_CYAN;
669 s->t_attrib.fgcol=COLOR_WHITE;
671 /* set background color */
673 s->t_attrib.bgcol=COLOR_BLACK;
676 s->t_attrib.bgcol=COLOR_RED;
679 s->t_attrib.bgcol=COLOR_GREEN;
682 s->t_attrib.bgcol=COLOR_YELLOW;
685 s->t_attrib.bgcol=COLOR_BLUE;
688 s->t_attrib.bgcol=COLOR_MAGENTA;
691 s->t_attrib.bgcol=COLOR_CYAN;
694 s->t_attrib.bgcol=COLOR_WHITE;
700 static void console_putchar(TextConsole *s, int ch)
708 case '\r': /* carriage return */
711 case '\n': /* newline */
714 case '\b': /* backspace */
716 y1 = (s->y_base + s->y) % s->total_height;
717 c = &s->cells[y1 * s->width + s->x];
719 c->t_attrib = s->t_attrib;
720 update_xy(s, s->x, s->y);
722 case '\t': /* tabspace */
723 if (s->x + (8 - (s->x % 8)) > s->width) {
726 s->x = s->x + (8 - (s->x % 8));
729 case '\a': /* alert aka. bell */
730 /* TODO: has to be implemented */
732 case 27: /* esc (introducing an escape sequence) */
733 s->state = TTY_STATE_ESC;
736 y1 = (s->y_base + s->y) % s->total_height;
737 c = &s->cells[y1 * s->width + s->x];
739 c->t_attrib = s->t_attrib;
740 update_xy(s, s->x, s->y);
742 if (s->x >= s->width)
747 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
749 for(i=0;i<MAX_ESC_PARAMS;i++)
750 s->esc_params[i] = 0;
751 s->nb_esc_params = 0;
752 s->state = TTY_STATE_CSI;
754 s->state = TTY_STATE_NORM;
757 case TTY_STATE_CSI: /* handle escape sequence parameters */
758 if (ch >= '0' && ch <= '9') {
759 if (s->nb_esc_params < MAX_ESC_PARAMS) {
760 s->esc_params[s->nb_esc_params] =
761 s->esc_params[s->nb_esc_params] * 10 + ch - '0';
767 s->state = TTY_STATE_NORM;
774 if (s->x < (s->width - 1))
779 y1 = (s->y_base + s->y) % s->total_height;
780 for(x = s->x; x < s->width; x++) {
781 c = &s->cells[y1 * s->width + x];
783 c->t_attrib = s->t_attrib_default;
785 update_xy(s, x, s->y);
791 console_handle_escape(s);
797 void console_select(unsigned int index)
801 if (index >= MAX_CONSOLES)
806 if (s->text_console) {
807 if (s->g_width != s->ds->width ||
808 s->g_height != s->ds->height) {
809 s->g_width = s->ds->width;
810 s->g_height = s->ds->height;
811 text_console_resize(s);
820 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
822 TextConsole *s = chr->opaque;
825 console_show_cursor(s, 0);
826 for(i = 0; i < len; i++) {
827 console_putchar(s, buf[i]);
829 console_show_cursor(s, 1);
833 static void console_chr_add_read_handler(CharDriverState *chr,
834 IOCanRWHandler *fd_can_read,
835 IOReadHandler *fd_read, void *opaque)
837 TextConsole *s = chr->opaque;
838 s->fd_read = fd_read;
839 s->fd_opaque = opaque;
842 static void console_send_event(CharDriverState *chr, int event)
844 TextConsole *s = chr->opaque;
847 if (event == CHR_EVENT_FOCUS) {
848 for(i = 0; i < nb_consoles; i++) {
849 if (consoles[i] == s) {
857 /* called when an ascii key is pressed */
858 void kbd_put_keysym(int keysym)
865 if (!s || !s->text_console)
869 case QEMU_KEY_CTRL_UP:
872 case QEMU_KEY_CTRL_DOWN:
875 case QEMU_KEY_CTRL_PAGEUP:
878 case QEMU_KEY_CTRL_PAGEDOWN:
883 /* convert the QEMU keysym to VT100 key string */
885 if (keysym >= 0xe100 && keysym <= 0xe11f) {
890 *q++ = '0' + (c / 10);
891 *q++ = '0' + (c % 10);
893 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
896 *q++ = keysym & 0xff;
900 s->fd_read(s->fd_opaque, buf, q - buf);
906 static TextConsole *new_console(DisplayState *ds, int text)
911 if (nb_consoles >= MAX_CONSOLES)
913 s = qemu_mallocz(sizeof(TextConsole));
917 if (!active_console || (active_console->text_console && !text))
920 s->text_console = text;
922 consoles[nb_consoles++] = s;
924 /* HACK: Put graphical consoles before text consoles. */
925 for (i = nb_consoles; i > 0; i--) {
926 if (!consoles[i - 1]->text_console)
928 consoles[i] = consoles[i - 1];
935 TextConsole *graphic_console_init(DisplayState *ds, vga_hw_update_ptr update,
936 vga_hw_invalidate_ptr invalidate,
937 vga_hw_screen_dump_ptr screen_dump,
942 s = new_console(ds, 0);
945 s->hw_update = update;
946 s->hw_invalidate = invalidate;
947 s->hw_screen_dump = screen_dump;
952 int is_graphic_console(void)
954 return !active_console->text_console;
957 CharDriverState *text_console_init(DisplayState *ds)
959 CharDriverState *chr;
962 static int color_inited;
964 chr = qemu_mallocz(sizeof(CharDriverState));
967 s = new_console(ds, 1);
973 chr->chr_write = console_puts;
974 chr->chr_add_read_handler = console_chr_add_read_handler;
975 chr->chr_send_event = console_send_event;
979 for(j = 0; j < 2; j++) {
980 for(i = 0; i < 8; i++) {
981 color_table[j][i] = col_expand(s->ds,
982 vga_get_color(s->ds, color_table_rgb[j][i]));
988 s->total_height = DEFAULT_BACKSCROLL;
991 s->g_width = s->ds->width;
992 s->g_height = s->ds->height;
994 /* Set text attribute defaults */
995 s->t_attrib_default.bold = 0;
996 s->t_attrib_default.uline = 0;
997 s->t_attrib_default.blink = 0;
998 s->t_attrib_default.invers = 0;
999 s->t_attrib_default.unvisible = 0;
1000 s->t_attrib_default.fgcol = COLOR_WHITE;
1001 s->t_attrib_default.bgcol = COLOR_BLACK;
1003 /* set current text attributes to default */
1004 s->t_attrib = s->t_attrib_default;
1005 text_console_resize(s);