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
58 int text_console
; /* true if text console */
60 int g_width
, g_height
;
64 int backscroll_height
;
68 TextAttributes t_attrib_default
; /* default text attributes */
69 TextAttributes t_attrib
; /* currently active text attributes */
73 int esc_params
[MAX_ESC_PARAMS
];
76 /* kbd read handler */
77 IOReadHandler
*fd_read
;
81 static TextConsole
*active_console
;
82 static TextConsole
*consoles
[MAX_CONSOLES
];
83 static int nb_consoles
= 0;
85 /* convert a RGBA color to a color index usable in graphic primitives */
86 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
88 unsigned int r
, g
, b
, color
;
93 r
= (rgba
>> 16) & 0xff;
94 g
= (rgba
>> 8) & 0xff;
96 color
= (rgb_to_index
[r
] * 6 * 6) +
97 (rgb_to_index
[g
] * 6) +
102 r
= (rgba
>> 16) & 0xff;
103 g
= (rgba
>> 8) & 0xff;
105 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
108 r
= (rgba
>> 16) & 0xff;
109 g
= (rgba
>> 8) & 0xff;
111 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
121 static void vga_fill_rect (DisplayState
*ds
,
122 int posx
, int posy
, int width
, int height
, uint32_t color
)
127 bpp
= (ds
->depth
+ 7) >> 3;
129 ds
->linesize
* posy
+ bpp
* posx
;
130 for (y
= 0; y
< height
; y
++) {
134 for (x
= 0; x
< width
; x
++) {
135 *((uint8_t *)d
) = color
;
140 for (x
= 0; x
< width
; x
++) {
141 *((uint16_t *)d
) = color
;
146 for (x
= 0; x
< width
; x
++) {
147 *((uint32_t *)d
) = color
;
156 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
157 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
163 bpp
= (ds
->depth
+ 7) >> 3;
167 ds
->linesize
* ys
+ bpp
* xs
;
169 ds
->linesize
* yd
+ bpp
* xd
;
170 for (y
= 0; y
< h
; y
++) {
177 ds
->linesize
* (ys
+ h
- 1) + bpp
* xs
;
179 ds
->linesize
* (yd
+ h
- 1) + bpp
* xd
;
180 for (y
= 0; y
< h
; y
++) {
188 /***********************************************************/
189 /* basic char display */
191 #define FONT_HEIGHT 16
196 #define cbswap_32(__x) \
198 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
199 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
200 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
201 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
203 #ifdef WORDS_BIGENDIAN
206 #define PAT(x) cbswap_32(x)
209 static const uint32_t dmask16
[16] = {
228 static const uint32_t dmask4
[4] = {
235 static uint32_t color_table
[2][8];
248 static const uint32_t color_table_rgb
[2][8] = {
250 RGB(0x00, 0x00, 0x00), /* black */
251 RGB(0xaa, 0x00, 0x00), /* red */
252 RGB(0x00, 0xaa, 0x00), /* green */
253 RGB(0xaa, 0xaa, 0x00), /* yellow */
254 RGB(0x00, 0x00, 0xaa), /* blue */
255 RGB(0xaa, 0x00, 0xaa), /* magenta */
256 RGB(0x00, 0xaa, 0xaa), /* cyan */
257 RGB(0xaa, 0xaa, 0xaa), /* white */
260 RGB(0x00, 0x00, 0x00), /* black */
261 RGB(0xff, 0x00, 0x00), /* red */
262 RGB(0x00, 0xff, 0x00), /* green */
263 RGB(0xff, 0xff, 0x00), /* yellow */
264 RGB(0x00, 0x00, 0xff), /* blue */
265 RGB(0xff, 0x00, 0xff), /* magenta */
266 RGB(0x00, 0xff, 0xff), /* cyan */
267 RGB(0xff, 0xff, 0xff), /* white */
271 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
289 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
291 if (t_attrib
->bold
) {
296 if (t_attrib
->uline
) {
301 if (t_attrib
->blink
) {
306 if (t_attrib
->invers
) {
311 if (t_attrib
->unvisible
) {
317 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
321 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
322 TextAttributes
*t_attrib
)
325 const uint8_t *font_ptr
;
326 unsigned int font_data
, linesize
, xorcol
, bpp
;
328 unsigned int fgcol
, bgcol
;
331 printf("x: %2i y: %2i", x
, y
);
332 console_print_text_attributes(t_attrib
, ch
);
335 if (t_attrib
->invers
) {
336 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
337 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
339 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
340 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
343 bpp
= (ds
->depth
+ 7) >> 3;
345 ds
->linesize
* y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
346 linesize
= ds
->linesize
;
347 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
348 xorcol
= bgcol
^ fgcol
;
351 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
352 font_data
= *font_ptr
++;
354 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
357 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
358 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
364 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
365 font_data
= *font_ptr
++;
367 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
370 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
371 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
372 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
373 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
378 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
379 font_data
= *font_ptr
++;
380 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
383 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
384 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
385 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
386 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
387 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
388 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
389 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
390 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
397 static void text_console_resize(TextConsole
*s
)
399 TextCell
*cells
, *c
, *c1
;
400 int w1
, x
, y
, last_width
;
402 last_width
= s
->width
;
403 s
->width
= s
->g_width
/ FONT_WIDTH
;
404 s
->height
= s
->g_height
/ FONT_HEIGHT
;
410 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
411 for(y
= 0; y
< s
->total_height
; y
++) {
412 c
= &cells
[y
* s
->width
];
414 c1
= &s
->cells
[y
* last_width
];
415 for(x
= 0; x
< w1
; x
++) {
419 for(x
= w1
; x
< s
->width
; x
++) {
421 c
->t_attrib
= s
->t_attrib_default
;
429 static void update_xy(TextConsole
*s
, int x
, int y
)
434 if (s
== active_console
) {
435 y1
= (s
->y_base
+ y
) % s
->total_height
;
436 y2
= y1
- s
->y_displayed
;
438 y2
+= s
->total_height
;
439 if (y2
< s
->height
) {
440 c
= &s
->cells
[y1
* s
->width
+ x
];
441 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
443 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
444 FONT_WIDTH
, FONT_HEIGHT
);
449 static void console_show_cursor(TextConsole
*s
, int show
)
454 if (s
== active_console
) {
455 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
456 y
= y1
- s
->y_displayed
;
458 y
+= s
->total_height
;
460 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
462 TextAttributes t_attrib
= s
->t_attrib_default
;
463 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
464 vga_putcharxy(s
->ds
, s
->x
, y
, c
->ch
, &t_attrib
);
466 vga_putcharxy(s
->ds
, s
->x
, y
, c
->ch
,
469 dpy_update(s
->ds
, s
->x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
470 FONT_WIDTH
, FONT_HEIGHT
);
475 static void console_refresh(TextConsole
*s
)
480 if (s
!= active_console
)
483 vga_fill_rect(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
,
484 color_table
[0][COLOR_BLACK
]);
486 for(y
= 0; y
< s
->height
; y
++) {
487 c
= s
->cells
+ y1
* s
->width
;
488 for(x
= 0; x
< s
->width
; x
++) {
489 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
493 if (++y1
== s
->total_height
)
496 dpy_update(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
);
497 console_show_cursor(s
, 1);
500 static void console_scroll(int ydelta
)
506 if (!s
|| !s
->text_console
)
510 for(i
= 0; i
< ydelta
; i
++) {
511 if (s
->y_displayed
== s
->y_base
)
513 if (++s
->y_displayed
== s
->total_height
)
518 i
= s
->backscroll_height
;
519 if (i
> s
->total_height
- s
->height
)
520 i
= s
->total_height
- s
->height
;
523 y1
+= s
->total_height
;
524 for(i
= 0; i
< ydelta
; i
++) {
525 if (s
->y_displayed
== y1
)
527 if (--s
->y_displayed
< 0)
528 s
->y_displayed
= s
->total_height
- 1;
534 static void console_put_lf(TextConsole
*s
)
541 if (s
->y
>= s
->height
) {
542 s
->y
= s
->height
- 1;
544 if (s
->y_displayed
== s
->y_base
) {
545 if (++s
->y_displayed
== s
->total_height
)
548 if (++s
->y_base
== s
->total_height
)
550 if (s
->backscroll_height
< s
->total_height
)
551 s
->backscroll_height
++;
552 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
553 c
= &s
->cells
[y1
* s
->width
];
554 for(x
= 0; x
< s
->width
; x
++) {
556 c
->t_attrib
= s
->t_attrib_default
;
559 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
560 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
561 s
->width
* FONT_WIDTH
,
562 (s
->height
- 1) * FONT_HEIGHT
);
563 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
564 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
565 color_table
[0][s
->t_attrib_default
.bgcol
]);
566 dpy_update(s
->ds
, 0, 0,
567 s
->width
* FONT_WIDTH
, s
->height
* FONT_HEIGHT
);
572 /* Set console attributes depending on the current escape codes.
573 * NOTE: I know this code is not very efficient (checking every color for it
574 * self) but it is more readable and better maintainable.
576 static void console_handle_escape(TextConsole
*s
)
580 if (s
->nb_esc_params
== 0) { /* ESC[m sets all attributes to default */
581 s
->t_attrib
= s
->t_attrib_default
;
584 for (i
=0; i
<s
->nb_esc_params
; i
++) {
585 switch (s
->esc_params
[i
]) {
586 case 0: /* reset all console attributes to default */
587 s
->t_attrib
= s
->t_attrib_default
;
590 s
->t_attrib
.bold
= 1;
593 s
->t_attrib
.uline
= 1;
596 s
->t_attrib
.blink
= 1;
599 s
->t_attrib
.invers
= 1;
602 s
->t_attrib
.unvisible
= 1;
605 s
->t_attrib
.bold
= 0;
608 s
->t_attrib
.uline
= 0;
611 s
->t_attrib
.blink
= 0;
614 s
->t_attrib
.invers
= 0;
617 s
->t_attrib
.unvisible
= 0;
619 /* set foreground color */
621 s
->t_attrib
.fgcol
=COLOR_BLACK
;
624 s
->t_attrib
.fgcol
=COLOR_RED
;
627 s
->t_attrib
.fgcol
=COLOR_GREEN
;
630 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
633 s
->t_attrib
.fgcol
=COLOR_BLUE
;
636 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
639 s
->t_attrib
.fgcol
=COLOR_CYAN
;
642 s
->t_attrib
.fgcol
=COLOR_WHITE
;
644 /* set background color */
646 s
->t_attrib
.bgcol
=COLOR_BLACK
;
649 s
->t_attrib
.bgcol
=COLOR_RED
;
652 s
->t_attrib
.bgcol
=COLOR_GREEN
;
655 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
658 s
->t_attrib
.bgcol
=COLOR_BLUE
;
661 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
664 s
->t_attrib
.bgcol
=COLOR_CYAN
;
667 s
->t_attrib
.bgcol
=COLOR_WHITE
;
673 static void console_putchar(TextConsole
*s
, int ch
)
681 case '\r': /* carriage return */
684 case '\n': /* newline */
687 case '\b': /* backspace */
689 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
690 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
692 c
->t_attrib
= s
->t_attrib
;
693 update_xy(s
, s
->x
, s
->y
);
695 case '\t': /* tabspace */
696 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
699 s
->x
= s
->x
+ (8 - (s
->x
% 8));
702 case '\a': /* alert aka. bell */
703 /* TODO: has to be implemented */
705 case 27: /* esc (introducing an escape sequence) */
706 s
->state
= TTY_STATE_ESC
;
709 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
710 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
712 c
->t_attrib
= s
->t_attrib
;
713 update_xy(s
, s
->x
, s
->y
);
715 if (s
->x
>= s
->width
)
720 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
722 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
723 s
->esc_params
[i
] = 0;
724 s
->nb_esc_params
= 0;
725 s
->state
= TTY_STATE_CSI
;
727 s
->state
= TTY_STATE_NORM
;
730 case TTY_STATE_CSI
: /* handle escape sequence parameters */
731 if (ch
>= '0' && ch
<= '9') {
732 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
733 s
->esc_params
[s
->nb_esc_params
] =
734 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
740 s
->state
= TTY_STATE_NORM
;
747 if (s
->x
< (s
->width
- 1))
752 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
753 for(x
= s
->x
; x
< s
->width
; x
++) {
754 c
= &s
->cells
[y1
* s
->width
+ x
];
756 c
->t_attrib
= s
->t_attrib_default
;
758 update_xy(s
, x
, s
->y
);
764 console_handle_escape(s
);
770 void console_select(unsigned int index
)
774 if (index
>= MAX_CONSOLES
)
779 if (s
->text_console
) {
780 if (s
->g_width
!= s
->ds
->width
||
781 s
->g_height
!= s
->ds
->height
) {
782 s
->g_width
= s
->ds
->width
;
783 s
->g_height
= s
->ds
->height
;
784 text_console_resize(s
);
791 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
793 TextConsole
*s
= chr
->opaque
;
796 console_show_cursor(s
, 0);
797 for(i
= 0; i
< len
; i
++) {
798 console_putchar(s
, buf
[i
]);
800 console_show_cursor(s
, 1);
804 static void console_chr_add_read_handler(CharDriverState
*chr
,
805 IOCanRWHandler
*fd_can_read
,
806 IOReadHandler
*fd_read
, void *opaque
)
808 TextConsole
*s
= chr
->opaque
;
809 s
->fd_read
= fd_read
;
810 s
->fd_opaque
= opaque
;
813 static void console_send_event(CharDriverState
*chr
, int event
)
815 TextConsole
*s
= chr
->opaque
;
818 if (event
== CHR_EVENT_FOCUS
) {
819 for(i
= 0; i
< nb_consoles
; i
++) {
820 if (consoles
[i
] == s
) {
828 /* called when an ascii key is pressed */
829 void kbd_put_keysym(int keysym
)
836 if (!s
|| !s
->text_console
)
840 case QEMU_KEY_CTRL_UP
:
843 case QEMU_KEY_CTRL_DOWN
:
846 case QEMU_KEY_CTRL_PAGEUP
:
849 case QEMU_KEY_CTRL_PAGEDOWN
:
854 /* convert the QEMU keysym to VT100 key string */
856 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
861 *q
++ = '0' + (c
/ 10);
862 *q
++ = '0' + (c
% 10);
864 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
867 *q
++ = keysym
& 0xff;
871 s
->fd_read(s
->fd_opaque
, buf
, q
- buf
);
877 TextConsole
*graphic_console_init(DisplayState
*ds
)
881 if (nb_consoles
>= MAX_CONSOLES
)
883 s
= qemu_mallocz(sizeof(TextConsole
));
890 consoles
[nb_consoles
++] = s
;
894 int is_active_console(TextConsole
*s
)
896 return s
== active_console
;
899 CharDriverState
*text_console_init(DisplayState
*ds
)
901 CharDriverState
*chr
;
904 static int color_inited
;
906 chr
= qemu_mallocz(sizeof(CharDriverState
));
909 s
= graphic_console_init(ds
);
916 chr
->chr_write
= console_puts
;
917 chr
->chr_add_read_handler
= console_chr_add_read_handler
;
918 chr
->chr_send_event
= console_send_event
;
922 for(j
= 0; j
< 2; j
++) {
923 for(i
= 0; i
< 8; i
++) {
924 color_table
[j
][i
] = col_expand(s
->ds
,
925 vga_get_color(s
->ds
, color_table_rgb
[j
][i
]));
931 s
->total_height
= DEFAULT_BACKSCROLL
;
934 s
->g_width
= s
->ds
->width
;
935 s
->g_height
= s
->ds
->height
;
937 /* Set text attribute defaults */
938 s
->t_attrib_default
.bold
= 0;
939 s
->t_attrib_default
.uline
= 0;
940 s
->t_attrib_default
.blink
= 0;
941 s
->t_attrib_default
.invers
= 0;
942 s
->t_attrib_default
.unvisible
= 0;
943 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
944 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
946 /* set current text attributes to default */
947 s
->t_attrib
= s
->t_attrib_default
;
948 text_console_resize(s
);