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
;
124 int x_saved
, y_saved
;
127 TextAttributes t_attrib_default
; /* default text attributes */
128 TextAttributes t_attrib
; /* currently active text attributes */
132 int esc_params
[MAX_ESC_PARAMS
];
135 CharDriverState
*chr
;
136 /* fifo for key pressed */
138 uint8_t out_fifo_buf
[16];
139 QEMUTimer
*kbd_timer
;
142 static TextConsole
*active_console
;
143 static TextConsole
*consoles
[MAX_CONSOLES
];
144 static int nb_consoles
= 0;
146 void vga_hw_update(void)
148 if (active_console
&& active_console
->hw_update
)
149 active_console
->hw_update(active_console
->hw
);
152 void vga_hw_invalidate(void)
154 if (active_console
->hw_invalidate
)
155 active_console
->hw_invalidate(active_console
->hw
);
158 void vga_hw_screen_dump(const char *filename
)
160 /* There is currently no was of specifying which screen we want to dump,
161 so always dump the dirst one. */
162 if (consoles
[0]->hw_screen_dump
)
163 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
166 /* convert a RGBA color to a color index usable in graphic primitives */
167 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
169 unsigned int r
, g
, b
, color
;
174 r
= (rgba
>> 16) & 0xff;
175 g
= (rgba
>> 8) & 0xff;
177 color
= (rgb_to_index
[r
] * 6 * 6) +
178 (rgb_to_index
[g
] * 6) +
183 r
= (rgba
>> 16) & 0xff;
184 g
= (rgba
>> 8) & 0xff;
186 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
189 r
= (rgba
>> 16) & 0xff;
190 g
= (rgba
>> 8) & 0xff;
192 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
202 static void vga_fill_rect (DisplayState
*ds
,
203 int posx
, int posy
, int width
, int height
, uint32_t color
)
208 bpp
= (ds
->depth
+ 7) >> 3;
210 ds
->linesize
* posy
+ bpp
* posx
;
211 for (y
= 0; y
< height
; y
++) {
215 for (x
= 0; x
< width
; x
++) {
216 *((uint8_t *)d
) = color
;
221 for (x
= 0; x
< width
; x
++) {
222 *((uint16_t *)d
) = color
;
227 for (x
= 0; x
< width
; x
++) {
228 *((uint32_t *)d
) = color
;
237 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
238 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
244 bpp
= (ds
->depth
+ 7) >> 3;
248 ds
->linesize
* ys
+ bpp
* xs
;
250 ds
->linesize
* yd
+ bpp
* xd
;
251 for (y
= 0; y
< h
; y
++) {
258 ds
->linesize
* (ys
+ h
- 1) + bpp
* xs
;
260 ds
->linesize
* (yd
+ h
- 1) + bpp
* xd
;
261 for (y
= 0; y
< h
; y
++) {
269 /***********************************************************/
270 /* basic char display */
272 #define FONT_HEIGHT 16
277 #define cbswap_32(__x) \
279 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
280 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
281 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
282 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
284 #ifdef WORDS_BIGENDIAN
287 #define PAT(x) cbswap_32(x)
290 static const uint32_t dmask16
[16] = {
309 static const uint32_t dmask4
[4] = {
316 static uint32_t color_table
[2][8];
329 static const uint32_t color_table_rgb
[2][8] = {
331 QEMU_RGB(0x00, 0x00, 0x00), /* black */
332 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
333 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
334 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
335 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
336 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
337 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
338 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
341 QEMU_RGB(0x00, 0x00, 0x00), /* black */
342 QEMU_RGB(0xff, 0x00, 0x00), /* red */
343 QEMU_RGB(0x00, 0xff, 0x00), /* green */
344 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
345 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
346 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
347 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
348 QEMU_RGB(0xff, 0xff, 0xff), /* white */
352 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
370 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
372 if (t_attrib
->bold
) {
377 if (t_attrib
->uline
) {
382 if (t_attrib
->blink
) {
387 if (t_attrib
->invers
) {
392 if (t_attrib
->unvisible
) {
398 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
402 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
403 TextAttributes
*t_attrib
)
406 const uint8_t *font_ptr
;
407 unsigned int font_data
, linesize
, xorcol
, bpp
;
409 unsigned int fgcol
, bgcol
;
412 printf("x: %2i y: %2i", x
, y
);
413 console_print_text_attributes(t_attrib
, ch
);
416 if (t_attrib
->invers
) {
417 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
418 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
420 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
421 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
424 bpp
= (ds
->depth
+ 7) >> 3;
426 ds
->linesize
* y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
427 linesize
= ds
->linesize
;
428 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
429 xorcol
= bgcol
^ fgcol
;
432 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
433 font_data
= *font_ptr
++;
435 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
438 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
439 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
445 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
446 font_data
= *font_ptr
++;
448 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
451 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
452 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
453 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
454 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
459 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
460 font_data
= *font_ptr
++;
461 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
464 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
465 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
466 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
467 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
468 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
469 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
470 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
471 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
478 static void text_console_resize(TextConsole
*s
)
480 TextCell
*cells
, *c
, *c1
;
481 int w1
, x
, y
, last_width
;
483 last_width
= s
->width
;
484 s
->width
= s
->g_width
/ FONT_WIDTH
;
485 s
->height
= s
->g_height
/ FONT_HEIGHT
;
491 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
492 for(y
= 0; y
< s
->total_height
; y
++) {
493 c
= &cells
[y
* s
->width
];
495 c1
= &s
->cells
[y
* last_width
];
496 for(x
= 0; x
< w1
; x
++) {
500 for(x
= w1
; x
< s
->width
; x
++) {
502 c
->t_attrib
= s
->t_attrib_default
;
510 static void update_xy(TextConsole
*s
, int x
, int y
)
515 if (s
== active_console
) {
516 y1
= (s
->y_base
+ y
) % s
->total_height
;
517 y2
= y1
- s
->y_displayed
;
519 y2
+= s
->total_height
;
520 if (y2
< s
->height
) {
521 c
= &s
->cells
[y1
* s
->width
+ x
];
522 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
524 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
525 FONT_WIDTH
, FONT_HEIGHT
);
530 static void console_show_cursor(TextConsole
*s
, int show
)
535 if (s
== active_console
) {
536 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
537 y
= y1
- s
->y_displayed
;
539 y
+= s
->total_height
;
541 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
543 TextAttributes t_attrib
= s
->t_attrib_default
;
544 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
545 vga_putcharxy(s
->ds
, s
->x
, y
, c
->ch
, &t_attrib
);
547 vga_putcharxy(s
->ds
, s
->x
, y
, c
->ch
,
550 dpy_update(s
->ds
, s
->x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
551 FONT_WIDTH
, FONT_HEIGHT
);
556 static void console_refresh(TextConsole
*s
)
561 if (s
!= active_console
)
564 vga_fill_rect(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
,
565 color_table
[0][COLOR_BLACK
]);
567 for(y
= 0; y
< s
->height
; y
++) {
568 c
= s
->cells
+ y1
* s
->width
;
569 for(x
= 0; x
< s
->width
; x
++) {
570 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
574 if (++y1
== s
->total_height
)
577 dpy_update(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
);
578 console_show_cursor(s
, 1);
581 static void console_scroll(int ydelta
)
587 if (!s
|| !s
->text_console
)
591 for(i
= 0; i
< ydelta
; i
++) {
592 if (s
->y_displayed
== s
->y_base
)
594 if (++s
->y_displayed
== s
->total_height
)
599 i
= s
->backscroll_height
;
600 if (i
> s
->total_height
- s
->height
)
601 i
= s
->total_height
- s
->height
;
604 y1
+= s
->total_height
;
605 for(i
= 0; i
< ydelta
; i
++) {
606 if (s
->y_displayed
== y1
)
608 if (--s
->y_displayed
< 0)
609 s
->y_displayed
= s
->total_height
- 1;
615 static void console_put_lf(TextConsole
*s
)
621 if (s
->y
>= s
->height
) {
622 s
->y
= s
->height
- 1;
624 if (s
->y_displayed
== s
->y_base
) {
625 if (++s
->y_displayed
== s
->total_height
)
628 if (++s
->y_base
== s
->total_height
)
630 if (s
->backscroll_height
< s
->total_height
)
631 s
->backscroll_height
++;
632 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
633 c
= &s
->cells
[y1
* s
->width
];
634 for(x
= 0; x
< s
->width
; x
++) {
636 c
->t_attrib
= s
->t_attrib_default
;
639 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
640 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
641 s
->width
* FONT_WIDTH
,
642 (s
->height
- 1) * FONT_HEIGHT
);
643 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
644 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
645 color_table
[0][s
->t_attrib_default
.bgcol
]);
646 dpy_update(s
->ds
, 0, 0,
647 s
->width
* FONT_WIDTH
, s
->height
* FONT_HEIGHT
);
652 /* Set console attributes depending on the current escape codes.
653 * NOTE: I know this code is not very efficient (checking every color for it
654 * self) but it is more readable and better maintainable.
656 static void console_handle_escape(TextConsole
*s
)
660 for (i
=0; i
<s
->nb_esc_params
; i
++) {
661 switch (s
->esc_params
[i
]) {
662 case 0: /* reset all console attributes to default */
663 s
->t_attrib
= s
->t_attrib_default
;
666 s
->t_attrib
.bold
= 1;
669 s
->t_attrib
.uline
= 1;
672 s
->t_attrib
.blink
= 1;
675 s
->t_attrib
.invers
= 1;
678 s
->t_attrib
.unvisible
= 1;
681 s
->t_attrib
.bold
= 0;
684 s
->t_attrib
.uline
= 0;
687 s
->t_attrib
.blink
= 0;
690 s
->t_attrib
.invers
= 0;
693 s
->t_attrib
.unvisible
= 0;
695 /* set foreground color */
697 s
->t_attrib
.fgcol
=COLOR_BLACK
;
700 s
->t_attrib
.fgcol
=COLOR_RED
;
703 s
->t_attrib
.fgcol
=COLOR_GREEN
;
706 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
709 s
->t_attrib
.fgcol
=COLOR_BLUE
;
712 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
715 s
->t_attrib
.fgcol
=COLOR_CYAN
;
718 s
->t_attrib
.fgcol
=COLOR_WHITE
;
720 /* set background color */
722 s
->t_attrib
.bgcol
=COLOR_BLACK
;
725 s
->t_attrib
.bgcol
=COLOR_RED
;
728 s
->t_attrib
.bgcol
=COLOR_GREEN
;
731 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
734 s
->t_attrib
.bgcol
=COLOR_BLUE
;
737 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
740 s
->t_attrib
.bgcol
=COLOR_CYAN
;
743 s
->t_attrib
.bgcol
=COLOR_WHITE
;
749 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
751 int y1
= (s
->y_base
+ y
) % s
->total_height
;
752 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
754 c
->t_attrib
= s
->t_attrib_default
;
759 static void console_putchar(TextConsole
*s
, int ch
)
768 case '\r': /* carriage return */
771 case '\n': /* newline */
774 case '\b': /* backspace */
778 case '\t': /* tabspace */
779 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
783 s
->x
= s
->x
+ (8 - (s
->x
% 8));
786 case '\a': /* alert aka. bell */
787 /* TODO: has to be implemented */
790 /* SI (shift in), character set 0 (ignored) */
793 /* SO (shift out), character set 1 (ignored) */
795 case 27: /* esc (introducing an escape sequence) */
796 s
->state
= TTY_STATE_ESC
;
799 if (s
->x
>= s
->width
- 1) {
802 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
803 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
805 c
->t_attrib
= s
->t_attrib
;
806 update_xy(s
, s
->x
, s
->y
);
808 #if 0 /* line wrap disabled */
809 if (s
->x
>= s
->width
) {
817 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
819 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
820 s
->esc_params
[i
] = 0;
821 s
->nb_esc_params
= 0;
822 s
->state
= TTY_STATE_CSI
;
824 s
->state
= TTY_STATE_NORM
;
827 case TTY_STATE_CSI
: /* handle escape sequence parameters */
828 if (ch
>= '0' && ch
<= '9') {
829 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
830 s
->esc_params
[s
->nb_esc_params
] =
831 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
838 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
839 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
841 s
->state
= TTY_STATE_NORM
;
845 if (s
->esc_params
[0] == 0) {
846 s
->esc_params
[0] = 1;
848 s
->y
-= s
->esc_params
[0];
854 /* move cursor down */
855 if (s
->esc_params
[0] == 0) {
856 s
->esc_params
[0] = 1;
858 s
->y
+= s
->esc_params
[0];
859 if (s
->y
>= s
->height
) {
860 s
->y
= s
->height
- 1;
864 /* move cursor right */
865 if (s
->esc_params
[0] == 0) {
866 s
->esc_params
[0] = 1;
868 s
->x
+= s
->esc_params
[0];
869 if (s
->x
>= s
->width
) {
874 /* move cursor left */
875 if (s
->esc_params
[0] == 0) {
876 s
->esc_params
[0] = 1;
878 s
->x
-= s
->esc_params
[0];
884 /* move cursor to column */
885 s
->x
= s
->esc_params
[0] - 1;
892 /* move cursor to row, column */
893 s
->x
= s
->esc_params
[1] - 1;
897 s
->y
= s
->esc_params
[0] - 1;
903 switch (s
->esc_params
[0]) {
905 /* clear to end of screen */
906 for (y
= s
->y
; y
< s
->height
; y
++) {
907 for (x
= 0; x
< s
->width
; x
++) {
908 if (y
== s
->y
&& x
< s
->x
) {
911 console_clear_xy(s
, x
, y
);
916 /* clear from beginning of screen */
917 for (y
= 0; y
<= s
->y
; y
++) {
918 for (x
= 0; x
< s
->width
; x
++) {
919 if (y
== s
->y
&& x
> s
->x
) {
922 console_clear_xy(s
, x
, y
);
927 /* clear entire screen */
928 for (y
= 0; y
<= s
->height
; y
++) {
929 for (x
= 0; x
< s
->width
; x
++) {
930 console_clear_xy(s
, x
, y
);
936 switch (s
->esc_params
[0]) {
939 for(x
= s
->x
; x
< s
->width
; x
++) {
940 console_clear_xy(s
, x
, s
->y
);
944 /* clear from beginning of line */
945 for (x
= 0; x
<= s
->x
; x
++) {
946 console_clear_xy(s
, x
, s
->y
);
950 /* clear entire line */
951 for(x
= 0; x
< s
->width
; x
++) {
952 console_clear_xy(s
, x
, s
->y
);
958 console_handle_escape(s
);
961 /* report cursor position */
962 /* TODO: send ESC[row;colR */
965 /* save cursor position */
970 /* restore cursor position */
976 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
985 void console_select(unsigned int index
)
989 if (index
>= MAX_CONSOLES
)
994 if (s
->text_console
) {
995 if (s
->g_width
!= s
->ds
->width
||
996 s
->g_height
!= s
->ds
->height
) {
997 s
->g_width
= s
->ds
->width
;
998 s
->g_height
= s
->ds
->height
;
999 text_console_resize(s
);
1003 vga_hw_invalidate();
1008 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1010 TextConsole
*s
= chr
->opaque
;
1013 console_show_cursor(s
, 0);
1014 for(i
= 0; i
< len
; i
++) {
1015 console_putchar(s
, buf
[i
]);
1017 console_show_cursor(s
, 1);
1021 static void console_send_event(CharDriverState
*chr
, int event
)
1023 TextConsole
*s
= chr
->opaque
;
1026 if (event
== CHR_EVENT_FOCUS
) {
1027 for(i
= 0; i
< nb_consoles
; i
++) {
1028 if (consoles
[i
] == s
) {
1036 static void kbd_send_chars(void *opaque
)
1038 TextConsole
*s
= opaque
;
1042 len
= qemu_chr_can_read(s
->chr
);
1043 if (len
> s
->out_fifo
.count
)
1044 len
= s
->out_fifo
.count
;
1046 if (len
> sizeof(buf
))
1048 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1049 qemu_chr_read(s
->chr
, buf
, len
);
1051 /* characters are pending: we send them a bit later (XXX:
1052 horrible, should change char device API) */
1053 if (s
->out_fifo
.count
> 0) {
1054 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1058 /* called when an ascii key is pressed */
1059 void kbd_put_keysym(int keysym
)
1062 uint8_t buf
[16], *q
;
1066 if (!s
|| !s
->text_console
)
1070 case QEMU_KEY_CTRL_UP
:
1073 case QEMU_KEY_CTRL_DOWN
:
1076 case QEMU_KEY_CTRL_PAGEUP
:
1077 console_scroll(-10);
1079 case QEMU_KEY_CTRL_PAGEDOWN
:
1083 /* convert the QEMU keysym to VT100 key string */
1085 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1088 c
= keysym
- 0xe100;
1090 *q
++ = '0' + (c
/ 10);
1091 *q
++ = '0' + (c
% 10);
1093 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1096 *q
++ = keysym
& 0xff;
1100 if (s
->chr
->chr_read
) {
1101 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1108 static TextConsole
*new_console(DisplayState
*ds
, int text
)
1113 if (nb_consoles
>= MAX_CONSOLES
)
1115 s
= qemu_mallocz(sizeof(TextConsole
));
1119 if (!active_console
|| (active_console
->text_console
&& !text
))
1122 s
->text_console
= text
;
1124 consoles
[nb_consoles
++] = s
;
1126 /* HACK: Put graphical consoles before text consoles. */
1127 for (i
= nb_consoles
; i
> 0; i
--) {
1128 if (!consoles
[i
- 1]->text_console
)
1130 consoles
[i
] = consoles
[i
- 1];
1137 TextConsole
*graphic_console_init(DisplayState
*ds
, vga_hw_update_ptr update
,
1138 vga_hw_invalidate_ptr invalidate
,
1139 vga_hw_screen_dump_ptr screen_dump
,
1144 s
= new_console(ds
, 0);
1147 s
->hw_update
= update
;
1148 s
->hw_invalidate
= invalidate
;
1149 s
->hw_screen_dump
= screen_dump
;
1154 int is_graphic_console(void)
1156 return !active_console
->text_console
;
1159 CharDriverState
*text_console_init(DisplayState
*ds
)
1161 CharDriverState
*chr
;
1164 static int color_inited
;
1166 chr
= qemu_mallocz(sizeof(CharDriverState
));
1169 s
= new_console(ds
, 1);
1175 chr
->chr_write
= console_puts
;
1176 chr
->chr_send_event
= console_send_event
;
1179 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1180 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1181 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1183 if (!color_inited
) {
1185 for(j
= 0; j
< 2; j
++) {
1186 for(i
= 0; i
< 8; i
++) {
1187 color_table
[j
][i
] = col_expand(s
->ds
,
1188 vga_get_color(s
->ds
, color_table_rgb
[j
][i
]));
1194 s
->total_height
= DEFAULT_BACKSCROLL
;
1197 s
->g_width
= s
->ds
->width
;
1198 s
->g_height
= s
->ds
->height
;
1200 /* Set text attribute defaults */
1201 s
->t_attrib_default
.bold
= 0;
1202 s
->t_attrib_default
.uline
= 0;
1203 s
->t_attrib_default
.blink
= 0;
1204 s
->t_attrib_default
.invers
= 0;
1205 s
->t_attrib_default
.unvisible
= 0;
1206 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1207 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1209 /* set current text attributes to default */
1210 s
->t_attrib
= s
->t_attrib_default
;
1211 text_console_resize(s
);
1213 qemu_chr_reset(chr
);