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
) {
540 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
541 y
= y1
- s
->y_displayed
;
543 y
+= s
->total_height
;
545 c
= &s
->cells
[y1
* s
->width
+ x
];
547 TextAttributes t_attrib
= s
->t_attrib_default
;
548 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
549 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
551 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
553 dpy_update(s
->ds
, x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
554 FONT_WIDTH
, FONT_HEIGHT
);
559 static void console_refresh(TextConsole
*s
)
564 if (s
!= active_console
)
567 vga_fill_rect(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
,
568 color_table
[0][COLOR_BLACK
]);
570 for(y
= 0; y
< s
->height
; y
++) {
571 c
= s
->cells
+ y1
* s
->width
;
572 for(x
= 0; x
< s
->width
; x
++) {
573 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
577 if (++y1
== s
->total_height
)
580 dpy_update(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
);
581 console_show_cursor(s
, 1);
584 static void console_scroll(int ydelta
)
590 if (!s
|| !s
->text_console
)
594 for(i
= 0; i
< ydelta
; i
++) {
595 if (s
->y_displayed
== s
->y_base
)
597 if (++s
->y_displayed
== s
->total_height
)
602 i
= s
->backscroll_height
;
603 if (i
> s
->total_height
- s
->height
)
604 i
= s
->total_height
- s
->height
;
607 y1
+= s
->total_height
;
608 for(i
= 0; i
< ydelta
; i
++) {
609 if (s
->y_displayed
== y1
)
611 if (--s
->y_displayed
< 0)
612 s
->y_displayed
= s
->total_height
- 1;
618 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 for (i
=0; i
<s
->nb_esc_params
; i
++) {
664 switch (s
->esc_params
[i
]) {
665 case 0: /* reset all console attributes to default */
666 s
->t_attrib
= s
->t_attrib_default
;
669 s
->t_attrib
.bold
= 1;
672 s
->t_attrib
.uline
= 1;
675 s
->t_attrib
.blink
= 1;
678 s
->t_attrib
.invers
= 1;
681 s
->t_attrib
.unvisible
= 1;
684 s
->t_attrib
.bold
= 0;
687 s
->t_attrib
.uline
= 0;
690 s
->t_attrib
.blink
= 0;
693 s
->t_attrib
.invers
= 0;
696 s
->t_attrib
.unvisible
= 0;
698 /* set foreground color */
700 s
->t_attrib
.fgcol
=COLOR_BLACK
;
703 s
->t_attrib
.fgcol
=COLOR_RED
;
706 s
->t_attrib
.fgcol
=COLOR_GREEN
;
709 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
712 s
->t_attrib
.fgcol
=COLOR_BLUE
;
715 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
718 s
->t_attrib
.fgcol
=COLOR_CYAN
;
721 s
->t_attrib
.fgcol
=COLOR_WHITE
;
723 /* set background color */
725 s
->t_attrib
.bgcol
=COLOR_BLACK
;
728 s
->t_attrib
.bgcol
=COLOR_RED
;
731 s
->t_attrib
.bgcol
=COLOR_GREEN
;
734 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
737 s
->t_attrib
.bgcol
=COLOR_BLUE
;
740 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
743 s
->t_attrib
.bgcol
=COLOR_CYAN
;
746 s
->t_attrib
.bgcol
=COLOR_WHITE
;
752 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
754 int y1
= (s
->y_base
+ y
) % s
->total_height
;
755 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
757 c
->t_attrib
= s
->t_attrib_default
;
762 static void console_putchar(TextConsole
*s
, int ch
)
771 case '\r': /* carriage return */
774 case '\n': /* newline */
777 case '\b': /* backspace */
781 case '\t': /* tabspace */
782 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
786 s
->x
= s
->x
+ (8 - (s
->x
% 8));
789 case '\a': /* alert aka. bell */
790 /* TODO: has to be implemented */
793 /* SI (shift in), character set 0 (ignored) */
796 /* SO (shift out), character set 1 (ignored) */
798 case 27: /* esc (introducing an escape sequence) */
799 s
->state
= TTY_STATE_ESC
;
802 if (s
->x
>= s
->width
) {
807 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
808 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
810 c
->t_attrib
= s
->t_attrib
;
811 update_xy(s
, s
->x
, s
->y
);
816 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
818 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
819 s
->esc_params
[i
] = 0;
820 s
->nb_esc_params
= 0;
821 s
->state
= TTY_STATE_CSI
;
823 s
->state
= TTY_STATE_NORM
;
826 case TTY_STATE_CSI
: /* handle escape sequence parameters */
827 if (ch
>= '0' && ch
<= '9') {
828 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
829 s
->esc_params
[s
->nb_esc_params
] =
830 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
837 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
838 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
840 s
->state
= TTY_STATE_NORM
;
844 if (s
->esc_params
[0] == 0) {
845 s
->esc_params
[0] = 1;
847 s
->y
-= s
->esc_params
[0];
853 /* move cursor down */
854 if (s
->esc_params
[0] == 0) {
855 s
->esc_params
[0] = 1;
857 s
->y
+= s
->esc_params
[0];
858 if (s
->y
>= s
->height
) {
859 s
->y
= s
->height
- 1;
863 /* move cursor right */
864 if (s
->esc_params
[0] == 0) {
865 s
->esc_params
[0] = 1;
867 s
->x
+= s
->esc_params
[0];
868 if (s
->x
>= s
->width
) {
873 /* move cursor left */
874 if (s
->esc_params
[0] == 0) {
875 s
->esc_params
[0] = 1;
877 s
->x
-= s
->esc_params
[0];
883 /* move cursor to column */
884 s
->x
= s
->esc_params
[0] - 1;
891 /* move cursor to row, column */
892 s
->x
= s
->esc_params
[1] - 1;
896 s
->y
= s
->esc_params
[0] - 1;
902 switch (s
->esc_params
[0]) {
904 /* clear to end of screen */
905 for (y
= s
->y
; y
< s
->height
; y
++) {
906 for (x
= 0; x
< s
->width
; x
++) {
907 if (y
== s
->y
&& x
< s
->x
) {
910 console_clear_xy(s
, x
, y
);
915 /* clear from beginning of screen */
916 for (y
= 0; y
<= s
->y
; y
++) {
917 for (x
= 0; x
< s
->width
; x
++) {
918 if (y
== s
->y
&& x
> s
->x
) {
921 console_clear_xy(s
, x
, y
);
926 /* clear entire screen */
927 for (y
= 0; y
<= s
->height
; y
++) {
928 for (x
= 0; x
< s
->width
; x
++) {
929 console_clear_xy(s
, x
, y
);
935 switch (s
->esc_params
[0]) {
938 for(x
= s
->x
; x
< s
->width
; x
++) {
939 console_clear_xy(s
, x
, s
->y
);
943 /* clear from beginning of line */
944 for (x
= 0; x
<= s
->x
; x
++) {
945 console_clear_xy(s
, x
, s
->y
);
949 /* clear entire line */
950 for(x
= 0; x
< s
->width
; x
++) {
951 console_clear_xy(s
, x
, s
->y
);
957 console_handle_escape(s
);
960 /* report cursor position */
961 /* TODO: send ESC[row;colR */
964 /* save cursor position */
969 /* restore cursor position */
975 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
984 void console_select(unsigned int index
)
988 if (index
>= MAX_CONSOLES
)
993 if (s
->text_console
) {
994 if (s
->g_width
!= s
->ds
->width
||
995 s
->g_height
!= s
->ds
->height
) {
996 s
->g_width
= s
->ds
->width
;
997 s
->g_height
= s
->ds
->height
;
998 text_console_resize(s
);
1002 vga_hw_invalidate();
1007 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1009 TextConsole
*s
= chr
->opaque
;
1012 console_show_cursor(s
, 0);
1013 for(i
= 0; i
< len
; i
++) {
1014 console_putchar(s
, buf
[i
]);
1016 console_show_cursor(s
, 1);
1020 static void console_send_event(CharDriverState
*chr
, int event
)
1022 TextConsole
*s
= chr
->opaque
;
1025 if (event
== CHR_EVENT_FOCUS
) {
1026 for(i
= 0; i
< nb_consoles
; i
++) {
1027 if (consoles
[i
] == s
) {
1035 static void kbd_send_chars(void *opaque
)
1037 TextConsole
*s
= opaque
;
1041 len
= qemu_chr_can_read(s
->chr
);
1042 if (len
> s
->out_fifo
.count
)
1043 len
= s
->out_fifo
.count
;
1045 if (len
> sizeof(buf
))
1047 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1048 qemu_chr_read(s
->chr
, buf
, len
);
1050 /* characters are pending: we send them a bit later (XXX:
1051 horrible, should change char device API) */
1052 if (s
->out_fifo
.count
> 0) {
1053 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1057 /* called when an ascii key is pressed */
1058 void kbd_put_keysym(int keysym
)
1061 uint8_t buf
[16], *q
;
1065 if (!s
|| !s
->text_console
)
1069 case QEMU_KEY_CTRL_UP
:
1072 case QEMU_KEY_CTRL_DOWN
:
1075 case QEMU_KEY_CTRL_PAGEUP
:
1076 console_scroll(-10);
1078 case QEMU_KEY_CTRL_PAGEDOWN
:
1082 /* convert the QEMU keysym to VT100 key string */
1084 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1087 c
= keysym
- 0xe100;
1089 *q
++ = '0' + (c
/ 10);
1090 *q
++ = '0' + (c
% 10);
1092 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1095 *q
++ = keysym
& 0xff;
1099 if (s
->chr
->chr_read
) {
1100 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1107 static TextConsole
*new_console(DisplayState
*ds
, int text
)
1112 if (nb_consoles
>= MAX_CONSOLES
)
1114 s
= qemu_mallocz(sizeof(TextConsole
));
1118 if (!active_console
|| (active_console
->text_console
&& !text
))
1121 s
->text_console
= text
;
1123 consoles
[nb_consoles
++] = s
;
1125 /* HACK: Put graphical consoles before text consoles. */
1126 for (i
= nb_consoles
; i
> 0; i
--) {
1127 if (!consoles
[i
- 1]->text_console
)
1129 consoles
[i
] = consoles
[i
- 1];
1136 TextConsole
*graphic_console_init(DisplayState
*ds
, vga_hw_update_ptr update
,
1137 vga_hw_invalidate_ptr invalidate
,
1138 vga_hw_screen_dump_ptr screen_dump
,
1143 s
= new_console(ds
, 0);
1146 s
->hw_update
= update
;
1147 s
->hw_invalidate
= invalidate
;
1148 s
->hw_screen_dump
= screen_dump
;
1153 int is_graphic_console(void)
1155 return !active_console
->text_console
;
1158 CharDriverState
*text_console_init(DisplayState
*ds
)
1160 CharDriverState
*chr
;
1163 static int color_inited
;
1165 chr
= qemu_mallocz(sizeof(CharDriverState
));
1168 s
= new_console(ds
, 1);
1174 chr
->chr_write
= console_puts
;
1175 chr
->chr_send_event
= console_send_event
;
1178 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1179 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1180 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1182 if (!color_inited
) {
1184 for(j
= 0; j
< 2; j
++) {
1185 for(i
= 0; i
< 8; i
++) {
1186 color_table
[j
][i
] = col_expand(s
->ds
,
1187 vga_get_color(s
->ds
, color_table_rgb
[j
][i
]));
1193 s
->total_height
= DEFAULT_BACKSCROLL
;
1196 s
->g_width
= s
->ds
->width
;
1197 s
->g_height
= s
->ds
->height
;
1199 /* Set text attribute defaults */
1200 s
->t_attrib_default
.bold
= 0;
1201 s
->t_attrib_default
.uline
= 0;
1202 s
->t_attrib_default
.blink
= 0;
1203 s
->t_attrib_default
.invers
= 0;
1204 s
->t_attrib_default
.unvisible
= 0;
1205 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1206 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1208 /* set current text attributes to default */
1209 s
->t_attrib
= s
->t_attrib_default
;
1210 text_console_resize(s
);
1212 qemu_chr_reset(chr
);