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 /* kbd read handler */
136 IOCanRWHandler
*fd_can_read
;
137 IOReadHandler
*fd_read
;
139 /* fifo for key pressed */
141 uint8_t out_fifo_buf
[16];
142 QEMUTimer
*kbd_timer
;
145 static TextConsole
*active_console
;
146 static TextConsole
*consoles
[MAX_CONSOLES
];
147 static int nb_consoles
= 0;
149 void vga_hw_update(void)
151 if (active_console
&& active_console
->hw_update
)
152 active_console
->hw_update(active_console
->hw
);
155 void vga_hw_invalidate(void)
157 if (active_console
->hw_invalidate
)
158 active_console
->hw_invalidate(active_console
->hw
);
161 void vga_hw_screen_dump(const char *filename
)
163 /* There is currently no was of specifying which screen we want to dump,
164 so always dump the dirst one. */
165 if (consoles
[0]->hw_screen_dump
)
166 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
169 /* convert a RGBA color to a color index usable in graphic primitives */
170 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
172 unsigned int r
, g
, b
, color
;
177 r
= (rgba
>> 16) & 0xff;
178 g
= (rgba
>> 8) & 0xff;
180 color
= (rgb_to_index
[r
] * 6 * 6) +
181 (rgb_to_index
[g
] * 6) +
186 r
= (rgba
>> 16) & 0xff;
187 g
= (rgba
>> 8) & 0xff;
189 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
192 r
= (rgba
>> 16) & 0xff;
193 g
= (rgba
>> 8) & 0xff;
195 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
205 static void vga_fill_rect (DisplayState
*ds
,
206 int posx
, int posy
, int width
, int height
, uint32_t color
)
211 bpp
= (ds
->depth
+ 7) >> 3;
213 ds
->linesize
* posy
+ bpp
* posx
;
214 for (y
= 0; y
< height
; y
++) {
218 for (x
= 0; x
< width
; x
++) {
219 *((uint8_t *)d
) = color
;
224 for (x
= 0; x
< width
; x
++) {
225 *((uint16_t *)d
) = color
;
230 for (x
= 0; x
< width
; x
++) {
231 *((uint32_t *)d
) = color
;
240 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
241 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
247 bpp
= (ds
->depth
+ 7) >> 3;
251 ds
->linesize
* ys
+ bpp
* xs
;
253 ds
->linesize
* yd
+ bpp
* xd
;
254 for (y
= 0; y
< h
; y
++) {
261 ds
->linesize
* (ys
+ h
- 1) + bpp
* xs
;
263 ds
->linesize
* (yd
+ h
- 1) + bpp
* xd
;
264 for (y
= 0; y
< h
; y
++) {
272 /***********************************************************/
273 /* basic char display */
275 #define FONT_HEIGHT 16
280 #define cbswap_32(__x) \
282 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
283 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
284 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
285 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
287 #ifdef WORDS_BIGENDIAN
290 #define PAT(x) cbswap_32(x)
293 static const uint32_t dmask16
[16] = {
312 static const uint32_t dmask4
[4] = {
319 static uint32_t color_table
[2][8];
332 static const uint32_t color_table_rgb
[2][8] = {
334 QEMU_RGB(0x00, 0x00, 0x00), /* black */
335 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
336 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
337 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
338 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
339 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
340 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
341 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
344 QEMU_RGB(0x00, 0x00, 0x00), /* black */
345 QEMU_RGB(0xff, 0x00, 0x00), /* red */
346 QEMU_RGB(0x00, 0xff, 0x00), /* green */
347 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
348 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
349 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
350 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
351 QEMU_RGB(0xff, 0xff, 0xff), /* white */
355 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
373 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
375 if (t_attrib
->bold
) {
380 if (t_attrib
->uline
) {
385 if (t_attrib
->blink
) {
390 if (t_attrib
->invers
) {
395 if (t_attrib
->unvisible
) {
401 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
405 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
406 TextAttributes
*t_attrib
)
409 const uint8_t *font_ptr
;
410 unsigned int font_data
, linesize
, xorcol
, bpp
;
412 unsigned int fgcol
, bgcol
;
415 printf("x: %2i y: %2i", x
, y
);
416 console_print_text_attributes(t_attrib
, ch
);
419 if (t_attrib
->invers
) {
420 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
421 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
423 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
424 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
427 bpp
= (ds
->depth
+ 7) >> 3;
429 ds
->linesize
* y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
430 linesize
= ds
->linesize
;
431 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
432 xorcol
= bgcol
^ fgcol
;
435 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
436 font_data
= *font_ptr
++;
438 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
441 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
442 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
448 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
449 font_data
= *font_ptr
++;
451 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
454 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
455 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
456 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
457 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
462 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
463 font_data
= *font_ptr
++;
464 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
467 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
468 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
469 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
470 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
471 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
472 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
473 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
474 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
481 static void text_console_resize(TextConsole
*s
)
483 TextCell
*cells
, *c
, *c1
;
484 int w1
, x
, y
, last_width
;
486 last_width
= s
->width
;
487 s
->width
= s
->g_width
/ FONT_WIDTH
;
488 s
->height
= s
->g_height
/ FONT_HEIGHT
;
494 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
495 for(y
= 0; y
< s
->total_height
; y
++) {
496 c
= &cells
[y
* s
->width
];
498 c1
= &s
->cells
[y
* last_width
];
499 for(x
= 0; x
< w1
; x
++) {
503 for(x
= w1
; x
< s
->width
; x
++) {
505 c
->t_attrib
= s
->t_attrib_default
;
513 static void update_xy(TextConsole
*s
, int x
, int y
)
518 if (s
== active_console
) {
519 y1
= (s
->y_base
+ y
) % s
->total_height
;
520 y2
= y1
- s
->y_displayed
;
522 y2
+= s
->total_height
;
523 if (y2
< s
->height
) {
524 c
= &s
->cells
[y1
* s
->width
+ x
];
525 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
527 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
528 FONT_WIDTH
, FONT_HEIGHT
);
533 static void console_show_cursor(TextConsole
*s
, int show
)
538 if (s
== active_console
) {
539 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
540 y
= y1
- s
->y_displayed
;
542 y
+= s
->total_height
;
544 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
546 TextAttributes t_attrib
= s
->t_attrib_default
;
547 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
548 vga_putcharxy(s
->ds
, s
->x
, y
, c
->ch
, &t_attrib
);
550 vga_putcharxy(s
->ds
, s
->x
, y
, c
->ch
,
553 dpy_update(s
->ds
, s
->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
- 1) {
805 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
806 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
808 c
->t_attrib
= s
->t_attrib
;
809 update_xy(s
, s
->x
, s
->y
);
811 #if 0 /* line wrap disabled */
812 if (s
->x
>= s
->width
) {
820 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
822 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
823 s
->esc_params
[i
] = 0;
824 s
->nb_esc_params
= 0;
825 s
->state
= TTY_STATE_CSI
;
827 s
->state
= TTY_STATE_NORM
;
830 case TTY_STATE_CSI
: /* handle escape sequence parameters */
831 if (ch
>= '0' && ch
<= '9') {
832 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
833 s
->esc_params
[s
->nb_esc_params
] =
834 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
841 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
842 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
844 s
->state
= TTY_STATE_NORM
;
848 if (s
->esc_params
[0] == 0) {
849 s
->esc_params
[0] = 1;
851 s
->y
-= s
->esc_params
[0];
857 /* move cursor down */
858 if (s
->esc_params
[0] == 0) {
859 s
->esc_params
[0] = 1;
861 s
->y
+= s
->esc_params
[0];
862 if (s
->y
>= s
->height
) {
863 s
->y
= s
->height
- 1;
867 /* move cursor right */
868 if (s
->esc_params
[0] == 0) {
869 s
->esc_params
[0] = 1;
871 s
->x
+= s
->esc_params
[0];
872 if (s
->x
>= s
->width
) {
877 /* move cursor left */
878 if (s
->esc_params
[0] == 0) {
879 s
->esc_params
[0] = 1;
881 s
->x
-= s
->esc_params
[0];
887 /* move cursor to column */
888 s
->x
= s
->esc_params
[0] - 1;
895 /* move cursor to row, column */
896 s
->x
= s
->esc_params
[1] - 1;
900 s
->y
= s
->esc_params
[0] - 1;
906 switch (s
->esc_params
[0]) {
908 /* clear to end of screen */
909 for (y
= s
->y
; y
< s
->height
; y
++) {
910 for (x
= 0; x
< s
->width
; x
++) {
911 if (y
== s
->y
&& x
< s
->x
) {
914 console_clear_xy(s
, x
, y
);
919 /* clear from beginning of screen */
920 for (y
= 0; y
<= s
->y
; y
++) {
921 for (x
= 0; x
< s
->width
; x
++) {
922 if (y
== s
->y
&& x
> s
->x
) {
925 console_clear_xy(s
, x
, y
);
930 /* clear entire screen */
931 for (y
= 0; y
<= s
->height
; y
++) {
932 for (x
= 0; x
< s
->width
; x
++) {
933 console_clear_xy(s
, x
, y
);
939 switch (s
->esc_params
[0]) {
942 for(x
= s
->x
; x
< s
->width
; x
++) {
943 console_clear_xy(s
, x
, s
->y
);
947 /* clear from beginning of line */
948 for (x
= 0; x
<= s
->x
; x
++) {
949 console_clear_xy(s
, x
, s
->y
);
953 /* clear entire line */
954 for(x
= 0; x
< s
->width
; x
++) {
955 console_clear_xy(s
, x
, s
->y
);
961 console_handle_escape(s
);
964 /* report cursor position */
965 /* TODO: send ESC[row;colR */
968 /* save cursor position */
973 /* restore cursor position */
979 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
988 void console_select(unsigned int index
)
992 if (index
>= MAX_CONSOLES
)
997 if (s
->text_console
) {
998 if (s
->g_width
!= s
->ds
->width
||
999 s
->g_height
!= s
->ds
->height
) {
1000 s
->g_width
= s
->ds
->width
;
1001 s
->g_height
= s
->ds
->height
;
1002 text_console_resize(s
);
1006 vga_hw_invalidate();
1011 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1013 TextConsole
*s
= chr
->opaque
;
1016 console_show_cursor(s
, 0);
1017 for(i
= 0; i
< len
; i
++) {
1018 console_putchar(s
, buf
[i
]);
1020 console_show_cursor(s
, 1);
1024 static void console_chr_add_read_handler(CharDriverState
*chr
,
1025 IOCanRWHandler
*fd_can_read
,
1026 IOReadHandler
*fd_read
, void *opaque
)
1028 TextConsole
*s
= chr
->opaque
;
1029 s
->fd_can_read
= fd_can_read
;
1030 s
->fd_read
= fd_read
;
1031 s
->fd_opaque
= opaque
;
1034 static void console_send_event(CharDriverState
*chr
, int event
)
1036 TextConsole
*s
= chr
->opaque
;
1039 if (event
== CHR_EVENT_FOCUS
) {
1040 for(i
= 0; i
< nb_consoles
; i
++) {
1041 if (consoles
[i
] == s
) {
1049 static void kbd_send_chars(void *opaque
)
1051 TextConsole
*s
= opaque
;
1055 len
= s
->fd_can_read(s
->fd_opaque
);
1056 if (len
> s
->out_fifo
.count
)
1057 len
= s
->out_fifo
.count
;
1059 if (len
> sizeof(buf
))
1061 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1062 s
->fd_read(s
->fd_opaque
, buf
, len
);
1064 /* characters are pending: we send them a bit later (XXX:
1065 horrible, should change char device API) */
1066 if (s
->out_fifo
.count
> 0) {
1067 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1071 /* called when an ascii key is pressed */
1072 void kbd_put_keysym(int keysym
)
1075 uint8_t buf
[16], *q
;
1079 if (!s
|| !s
->text_console
)
1083 case QEMU_KEY_CTRL_UP
:
1086 case QEMU_KEY_CTRL_DOWN
:
1089 case QEMU_KEY_CTRL_PAGEUP
:
1090 console_scroll(-10);
1092 case QEMU_KEY_CTRL_PAGEDOWN
:
1096 /* convert the QEMU keysym to VT100 key string */
1098 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1101 c
= keysym
- 0xe100;
1103 *q
++ = '0' + (c
/ 10);
1104 *q
++ = '0' + (c
% 10);
1106 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1109 *q
++ = keysym
& 0xff;
1114 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1121 static TextConsole
*new_console(DisplayState
*ds
, int text
)
1126 if (nb_consoles
>= MAX_CONSOLES
)
1128 s
= qemu_mallocz(sizeof(TextConsole
));
1132 if (!active_console
|| (active_console
->text_console
&& !text
))
1135 s
->text_console
= text
;
1137 consoles
[nb_consoles
++] = s
;
1139 /* HACK: Put graphical consoles before text consoles. */
1140 for (i
= nb_consoles
; i
> 0; i
--) {
1141 if (!consoles
[i
- 1]->text_console
)
1143 consoles
[i
] = consoles
[i
- 1];
1150 TextConsole
*graphic_console_init(DisplayState
*ds
, vga_hw_update_ptr update
,
1151 vga_hw_invalidate_ptr invalidate
,
1152 vga_hw_screen_dump_ptr screen_dump
,
1157 s
= new_console(ds
, 0);
1160 s
->hw_update
= update
;
1161 s
->hw_invalidate
= invalidate
;
1162 s
->hw_screen_dump
= screen_dump
;
1167 int is_graphic_console(void)
1169 return !active_console
->text_console
;
1172 CharDriverState
*text_console_init(DisplayState
*ds
)
1174 CharDriverState
*chr
;
1177 static int color_inited
;
1179 chr
= qemu_mallocz(sizeof(CharDriverState
));
1182 s
= new_console(ds
, 1);
1188 chr
->chr_write
= console_puts
;
1189 chr
->chr_add_read_handler
= console_chr_add_read_handler
;
1190 chr
->chr_send_event
= console_send_event
;
1192 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1193 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1194 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1196 if (!color_inited
) {
1198 for(j
= 0; j
< 2; j
++) {
1199 for(i
= 0; i
< 8; i
++) {
1200 color_table
[j
][i
] = col_expand(s
->ds
,
1201 vga_get_color(s
->ds
, color_table_rgb
[j
][i
]));
1207 s
->total_height
= DEFAULT_BACKSCROLL
;
1210 s
->g_width
= s
->ds
->width
;
1211 s
->g_height
= s
->ds
->height
;
1213 /* Set text attribute defaults */
1214 s
->t_attrib_default
.bold
= 0;
1215 s
->t_attrib_default
.uline
= 0;
1216 s
->t_attrib_default
.blink
= 0;
1217 s
->t_attrib_default
.invers
= 0;
1218 s
->t_attrib_default
.unvisible
= 0;
1219 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1220 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1222 /* set current text attributes to default */
1223 s
->t_attrib
= s
->t_attrib_default
;
1224 text_console_resize(s
);
1226 qemu_chr_reset(chr
);