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
24 #include "qemu-common.h"
26 #include "qemu-timer.h"
28 //#define DEBUG_CONSOLE
29 #define DEFAULT_BACKSCROLL 512
30 #define MAX_CONSOLES 12
32 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
33 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
35 typedef struct TextAttributes
{
45 typedef struct TextCell
{
47 TextAttributes t_attrib
;
50 #define MAX_ESC_PARAMS 3
58 typedef struct QEMUFIFO
{
61 int count
, wptr
, rptr
;
64 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
68 l
= f
->buf_size
- f
->count
;
73 l
= f
->buf_size
- f
->wptr
;
76 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
78 if (f
->wptr
>= f
->buf_size
)
87 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
95 l
= f
->buf_size
- f
->rptr
;
98 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
100 if (f
->rptr
>= f
->buf_size
)
112 TEXT_CONSOLE_FIXED_SIZE
115 /* ??? This is mis-named.
116 It is used for both text and graphical consoles. */
118 console_type_t console_type
;
120 /* Graphic console state. */
121 vga_hw_update_ptr hw_update
;
122 vga_hw_invalidate_ptr hw_invalidate
;
123 vga_hw_screen_dump_ptr hw_screen_dump
;
124 vga_hw_text_update_ptr hw_text_update
;
127 int g_width
, g_height
;
131 int backscroll_height
;
133 int x_saved
, y_saved
;
136 TextAttributes t_attrib_default
; /* default text attributes */
137 TextAttributes t_attrib
; /* currently active text attributes */
139 int text_x
[2], text_y
[2], cursor_invalidate
;
142 int esc_params
[MAX_ESC_PARAMS
];
145 CharDriverState
*chr
;
146 /* fifo for key pressed */
148 uint8_t out_fifo_buf
[16];
149 QEMUTimer
*kbd_timer
;
152 static TextConsole
*active_console
;
153 static TextConsole
*consoles
[MAX_CONSOLES
];
154 static int nb_consoles
= 0;
156 void vga_hw_update(void)
158 if (active_console
&& active_console
->hw_update
)
159 active_console
->hw_update(active_console
->hw
);
162 void vga_hw_invalidate(void)
164 if (active_console
->hw_invalidate
)
165 active_console
->hw_invalidate(active_console
->hw
);
168 void vga_hw_screen_dump(const char *filename
)
170 /* There is currently no was of specifying which screen we want to dump,
171 so always dump the dirst one. */
172 if (consoles
[0]->hw_screen_dump
)
173 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
176 void vga_hw_text_update(console_ch_t
*chardata
)
178 if (active_console
&& active_console
->hw_text_update
)
179 active_console
->hw_text_update(active_console
->hw
, chardata
);
182 /* convert a RGBA color to a color index usable in graphic primitives */
183 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
185 unsigned int r
, g
, b
, color
;
190 r
= (rgba
>> 16) & 0xff;
191 g
= (rgba
>> 8) & 0xff;
193 color
= (rgb_to_index
[r
] * 6 * 6) +
194 (rgb_to_index
[g
] * 6) +
199 r
= (rgba
>> 16) & 0xff;
200 g
= (rgba
>> 8) & 0xff;
202 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
205 r
= (rgba
>> 16) & 0xff;
206 g
= (rgba
>> 8) & 0xff;
208 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
218 static void vga_fill_rect (DisplayState
*ds
,
219 int posx
, int posy
, int width
, int height
, uint32_t color
)
224 bpp
= (ds
->depth
+ 7) >> 3;
226 ds
->linesize
* posy
+ bpp
* posx
;
227 for (y
= 0; y
< height
; y
++) {
231 for (x
= 0; x
< width
; x
++) {
232 *((uint8_t *)d
) = color
;
237 for (x
= 0; x
< width
; x
++) {
238 *((uint16_t *)d
) = color
;
243 for (x
= 0; x
< width
; x
++) {
244 *((uint32_t *)d
) = color
;
253 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
254 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
260 bpp
= (ds
->depth
+ 7) >> 3;
264 ds
->linesize
* ys
+ bpp
* xs
;
266 ds
->linesize
* yd
+ bpp
* xd
;
267 for (y
= 0; y
< h
; y
++) {
274 ds
->linesize
* (ys
+ h
- 1) + bpp
* xs
;
276 ds
->linesize
* (yd
+ h
- 1) + bpp
* xd
;
277 for (y
= 0; y
< h
; y
++) {
285 /***********************************************************/
286 /* basic char display */
288 #define FONT_HEIGHT 16
293 #define cbswap_32(__x) \
295 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
296 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
297 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
298 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
300 #ifdef WORDS_BIGENDIAN
303 #define PAT(x) cbswap_32(x)
306 static const uint32_t dmask16
[16] = {
325 static const uint32_t dmask4
[4] = {
332 static uint32_t color_table
[2][8];
345 static const uint32_t color_table_rgb
[2][8] = {
347 QEMU_RGB(0x00, 0x00, 0x00), /* black */
348 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
349 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
350 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
351 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
352 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
353 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
354 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
357 QEMU_RGB(0x00, 0x00, 0x00), /* black */
358 QEMU_RGB(0xff, 0x00, 0x00), /* red */
359 QEMU_RGB(0x00, 0xff, 0x00), /* green */
360 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
361 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
362 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
363 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
364 QEMU_RGB(0xff, 0xff, 0xff), /* white */
368 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
386 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
388 if (t_attrib
->bold
) {
393 if (t_attrib
->uline
) {
398 if (t_attrib
->blink
) {
403 if (t_attrib
->invers
) {
408 if (t_attrib
->unvisible
) {
414 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
418 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
419 TextAttributes
*t_attrib
)
422 const uint8_t *font_ptr
;
423 unsigned int font_data
, linesize
, xorcol
, bpp
;
425 unsigned int fgcol
, bgcol
;
428 printf("x: %2i y: %2i", x
, y
);
429 console_print_text_attributes(t_attrib
, ch
);
432 if (t_attrib
->invers
) {
433 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
434 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
436 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
437 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
440 bpp
= (ds
->depth
+ 7) >> 3;
442 ds
->linesize
* y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
443 linesize
= ds
->linesize
;
444 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
445 xorcol
= bgcol
^ fgcol
;
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] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
455 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
461 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
462 font_data
= *font_ptr
++;
464 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
467 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
468 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
469 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
470 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
475 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
476 font_data
= *font_ptr
++;
477 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
480 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
481 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
482 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
483 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
484 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
485 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
486 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
487 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
494 static void text_console_resize(TextConsole
*s
)
496 TextCell
*cells
, *c
, *c1
;
497 int w1
, x
, y
, last_width
;
499 last_width
= s
->width
;
500 s
->width
= s
->g_width
/ FONT_WIDTH
;
501 s
->height
= s
->g_height
/ FONT_HEIGHT
;
507 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
508 for(y
= 0; y
< s
->total_height
; y
++) {
509 c
= &cells
[y
* s
->width
];
511 c1
= &s
->cells
[y
* last_width
];
512 for(x
= 0; x
< w1
; x
++) {
516 for(x
= w1
; x
< s
->width
; x
++) {
518 c
->t_attrib
= s
->t_attrib_default
;
526 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
528 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
529 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
530 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
531 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
534 static void update_xy(TextConsole
*s
, int x
, int y
)
539 if (s
== active_console
) {
541 text_update_xy(s
, x
, y
);
545 y1
= (s
->y_base
+ y
) % s
->total_height
;
546 y2
= y1
- s
->y_displayed
;
548 y2
+= s
->total_height
;
549 if (y2
< s
->height
) {
550 c
= &s
->cells
[y1
* s
->width
+ x
];
551 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
553 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
554 FONT_WIDTH
, FONT_HEIGHT
);
559 static void console_show_cursor(TextConsole
*s
, int show
)
564 if (s
== active_console
) {
568 s
->cursor_invalidate
= 1;
575 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
576 y
= y1
- s
->y_displayed
;
578 y
+= s
->total_height
;
580 c
= &s
->cells
[y1
* s
->width
+ x
];
582 TextAttributes t_attrib
= s
->t_attrib_default
;
583 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
584 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
586 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
588 dpy_update(s
->ds
, x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
589 FONT_WIDTH
, FONT_HEIGHT
);
594 static void console_refresh(TextConsole
*s
)
599 if (s
!= active_console
)
604 s
->text_x
[1] = s
->width
- 1;
605 s
->text_y
[1] = s
->height
- 1;
606 s
->cursor_invalidate
= 1;
610 vga_fill_rect(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
,
611 color_table
[0][COLOR_BLACK
]);
613 for(y
= 0; y
< s
->height
; y
++) {
614 c
= s
->cells
+ y1
* s
->width
;
615 for(x
= 0; x
< s
->width
; x
++) {
616 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
620 if (++y1
== s
->total_height
)
623 dpy_update(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
);
624 console_show_cursor(s
, 1);
627 static void console_scroll(int ydelta
)
633 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
637 for(i
= 0; i
< ydelta
; i
++) {
638 if (s
->y_displayed
== s
->y_base
)
640 if (++s
->y_displayed
== s
->total_height
)
645 i
= s
->backscroll_height
;
646 if (i
> s
->total_height
- s
->height
)
647 i
= s
->total_height
- s
->height
;
650 y1
+= s
->total_height
;
651 for(i
= 0; i
< ydelta
; i
++) {
652 if (s
->y_displayed
== y1
)
654 if (--s
->y_displayed
< 0)
655 s
->y_displayed
= s
->total_height
- 1;
661 static void console_put_lf(TextConsole
*s
)
667 if (s
->y
>= s
->height
) {
668 s
->y
= s
->height
- 1;
670 if (s
->y_displayed
== s
->y_base
) {
671 if (++s
->y_displayed
== s
->total_height
)
674 if (++s
->y_base
== s
->total_height
)
676 if (s
->backscroll_height
< s
->total_height
)
677 s
->backscroll_height
++;
678 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
679 c
= &s
->cells
[y1
* s
->width
];
680 for(x
= 0; x
< s
->width
; x
++) {
682 c
->t_attrib
= s
->t_attrib_default
;
685 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
689 s
->text_x
[1] = s
->width
- 1;
690 s
->text_y
[1] = s
->height
- 1;
694 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
695 s
->width
* FONT_WIDTH
,
696 (s
->height
- 1) * FONT_HEIGHT
);
697 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
698 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
699 color_table
[0][s
->t_attrib_default
.bgcol
]);
700 dpy_update(s
->ds
, 0, 0,
701 s
->width
* FONT_WIDTH
, s
->height
* FONT_HEIGHT
);
706 /* Set console attributes depending on the current escape codes.
707 * NOTE: I know this code is not very efficient (checking every color for it
708 * self) but it is more readable and better maintainable.
710 static void console_handle_escape(TextConsole
*s
)
714 for (i
=0; i
<s
->nb_esc_params
; i
++) {
715 switch (s
->esc_params
[i
]) {
716 case 0: /* reset all console attributes to default */
717 s
->t_attrib
= s
->t_attrib_default
;
720 s
->t_attrib
.bold
= 1;
723 s
->t_attrib
.uline
= 1;
726 s
->t_attrib
.blink
= 1;
729 s
->t_attrib
.invers
= 1;
732 s
->t_attrib
.unvisible
= 1;
735 s
->t_attrib
.bold
= 0;
738 s
->t_attrib
.uline
= 0;
741 s
->t_attrib
.blink
= 0;
744 s
->t_attrib
.invers
= 0;
747 s
->t_attrib
.unvisible
= 0;
749 /* set foreground color */
751 s
->t_attrib
.fgcol
=COLOR_BLACK
;
754 s
->t_attrib
.fgcol
=COLOR_RED
;
757 s
->t_attrib
.fgcol
=COLOR_GREEN
;
760 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
763 s
->t_attrib
.fgcol
=COLOR_BLUE
;
766 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
769 s
->t_attrib
.fgcol
=COLOR_CYAN
;
772 s
->t_attrib
.fgcol
=COLOR_WHITE
;
774 /* set background color */
776 s
->t_attrib
.bgcol
=COLOR_BLACK
;
779 s
->t_attrib
.bgcol
=COLOR_RED
;
782 s
->t_attrib
.bgcol
=COLOR_GREEN
;
785 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
788 s
->t_attrib
.bgcol
=COLOR_BLUE
;
791 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
794 s
->t_attrib
.bgcol
=COLOR_CYAN
;
797 s
->t_attrib
.bgcol
=COLOR_WHITE
;
803 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
805 int y1
= (s
->y_base
+ y
) % s
->total_height
;
806 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
808 c
->t_attrib
= s
->t_attrib_default
;
813 static void console_putchar(TextConsole
*s
, int ch
)
822 case '\r': /* carriage return */
825 case '\n': /* newline */
828 case '\b': /* backspace */
832 case '\t': /* tabspace */
833 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
837 s
->x
= s
->x
+ (8 - (s
->x
% 8));
840 case '\a': /* alert aka. bell */
841 /* TODO: has to be implemented */
844 /* SI (shift in), character set 0 (ignored) */
847 /* SO (shift out), character set 1 (ignored) */
849 case 27: /* esc (introducing an escape sequence) */
850 s
->state
= TTY_STATE_ESC
;
853 if (s
->x
>= s
->width
) {
858 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
859 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
861 c
->t_attrib
= s
->t_attrib
;
862 update_xy(s
, s
->x
, s
->y
);
867 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
869 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
870 s
->esc_params
[i
] = 0;
871 s
->nb_esc_params
= 0;
872 s
->state
= TTY_STATE_CSI
;
874 s
->state
= TTY_STATE_NORM
;
877 case TTY_STATE_CSI
: /* handle escape sequence parameters */
878 if (ch
>= '0' && ch
<= '9') {
879 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
880 s
->esc_params
[s
->nb_esc_params
] =
881 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
888 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
889 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
891 s
->state
= TTY_STATE_NORM
;
895 if (s
->esc_params
[0] == 0) {
896 s
->esc_params
[0] = 1;
898 s
->y
-= s
->esc_params
[0];
904 /* move cursor down */
905 if (s
->esc_params
[0] == 0) {
906 s
->esc_params
[0] = 1;
908 s
->y
+= s
->esc_params
[0];
909 if (s
->y
>= s
->height
) {
910 s
->y
= s
->height
- 1;
914 /* move cursor right */
915 if (s
->esc_params
[0] == 0) {
916 s
->esc_params
[0] = 1;
918 s
->x
+= s
->esc_params
[0];
919 if (s
->x
>= s
->width
) {
924 /* move cursor left */
925 if (s
->esc_params
[0] == 0) {
926 s
->esc_params
[0] = 1;
928 s
->x
-= s
->esc_params
[0];
934 /* move cursor to column */
935 s
->x
= s
->esc_params
[0] - 1;
942 /* move cursor to row, column */
943 s
->x
= s
->esc_params
[1] - 1;
947 s
->y
= s
->esc_params
[0] - 1;
953 switch (s
->esc_params
[0]) {
955 /* clear to end of screen */
956 for (y
= s
->y
; y
< s
->height
; y
++) {
957 for (x
= 0; x
< s
->width
; x
++) {
958 if (y
== s
->y
&& x
< s
->x
) {
961 console_clear_xy(s
, x
, y
);
966 /* clear from beginning of screen */
967 for (y
= 0; y
<= s
->y
; y
++) {
968 for (x
= 0; x
< s
->width
; x
++) {
969 if (y
== s
->y
&& x
> s
->x
) {
972 console_clear_xy(s
, x
, y
);
977 /* clear entire screen */
978 for (y
= 0; y
<= s
->height
; y
++) {
979 for (x
= 0; x
< s
->width
; x
++) {
980 console_clear_xy(s
, x
, y
);
986 switch (s
->esc_params
[0]) {
989 for(x
= s
->x
; x
< s
->width
; x
++) {
990 console_clear_xy(s
, x
, s
->y
);
994 /* clear from beginning of line */
995 for (x
= 0; x
<= s
->x
; x
++) {
996 console_clear_xy(s
, x
, s
->y
);
1000 /* clear entire line */
1001 for(x
= 0; x
< s
->width
; x
++) {
1002 console_clear_xy(s
, x
, s
->y
);
1008 console_handle_escape(s
);
1011 /* report cursor position */
1012 /* TODO: send ESC[row;colR */
1015 /* save cursor position */
1020 /* restore cursor position */
1025 #ifdef DEBUG_CONSOLE
1026 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1035 void console_select(unsigned int index
)
1039 if (index
>= MAX_CONSOLES
)
1041 s
= consoles
[index
];
1044 vga_hw_invalidate();
1048 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1050 TextConsole
*s
= chr
->opaque
;
1053 console_show_cursor(s
, 0);
1054 for(i
= 0; i
< len
; i
++) {
1055 console_putchar(s
, buf
[i
]);
1057 console_show_cursor(s
, 1);
1061 static void console_send_event(CharDriverState
*chr
, int event
)
1063 TextConsole
*s
= chr
->opaque
;
1066 if (event
== CHR_EVENT_FOCUS
) {
1067 for(i
= 0; i
< nb_consoles
; i
++) {
1068 if (consoles
[i
] == s
) {
1076 static void kbd_send_chars(void *opaque
)
1078 TextConsole
*s
= opaque
;
1082 len
= qemu_chr_can_read(s
->chr
);
1083 if (len
> s
->out_fifo
.count
)
1084 len
= s
->out_fifo
.count
;
1086 if (len
> sizeof(buf
))
1088 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1089 qemu_chr_read(s
->chr
, buf
, len
);
1091 /* characters are pending: we send them a bit later (XXX:
1092 horrible, should change char device API) */
1093 if (s
->out_fifo
.count
> 0) {
1094 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1098 /* called when an ascii key is pressed */
1099 void kbd_put_keysym(int keysym
)
1102 uint8_t buf
[16], *q
;
1106 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1110 case QEMU_KEY_CTRL_UP
:
1113 case QEMU_KEY_CTRL_DOWN
:
1116 case QEMU_KEY_CTRL_PAGEUP
:
1117 console_scroll(-10);
1119 case QEMU_KEY_CTRL_PAGEDOWN
:
1123 /* convert the QEMU keysym to VT100 key string */
1125 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1128 c
= keysym
- 0xe100;
1130 *q
++ = '0' + (c
/ 10);
1131 *q
++ = '0' + (c
% 10);
1133 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1136 *q
++ = keysym
& 0xff;
1140 if (s
->chr
->chr_read
) {
1141 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1148 static void text_console_invalidate(void *opaque
)
1150 TextConsole
*s
= (TextConsole
*) opaque
;
1152 if (s
->console_type
!= GRAPHIC_CONSOLE
) {
1153 if (s
->g_width
!= s
->ds
->width
||
1154 s
->g_height
!= s
->ds
->height
) {
1155 if (s
->console_type
== TEXT_CONSOLE_FIXED_SIZE
)
1156 dpy_resize(s
->ds
, s
->g_width
, s
->g_height
);
1158 s
->g_width
= s
->ds
->width
;
1159 s
->g_height
= s
->ds
->height
;
1160 text_console_resize(s
);
1167 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1169 TextConsole
*s
= (TextConsole
*) opaque
;
1172 if (s
->text_x
[0] <= s
->text_x
[1]) {
1173 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1174 chardata
+= s
->text_y
[0] * s
->width
;
1175 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1176 for (j
= 0; j
< s
->width
; j
++, src
++)
1177 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1178 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1179 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1180 (s
->cells
[src
].t_attrib
.bold
<< 21));
1181 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1182 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1183 s
->text_x
[0] = s
->width
;
1184 s
->text_y
[0] = s
->height
;
1188 if (s
->cursor_invalidate
) {
1189 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1190 s
->cursor_invalidate
= 0;
1194 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1199 if (nb_consoles
>= MAX_CONSOLES
)
1201 s
= qemu_mallocz(sizeof(TextConsole
));
1205 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1206 (console_type
== GRAPHIC_CONSOLE
))) {
1210 s
->console_type
= console_type
;
1211 if (console_type
!= GRAPHIC_CONSOLE
) {
1212 consoles
[nb_consoles
++] = s
;
1214 /* HACK: Put graphical consoles before text consoles. */
1215 for (i
= nb_consoles
; i
> 0; i
--) {
1216 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1218 consoles
[i
] = consoles
[i
- 1];
1225 TextConsole
*graphic_console_init(DisplayState
*ds
, vga_hw_update_ptr update
,
1226 vga_hw_invalidate_ptr invalidate
,
1227 vga_hw_screen_dump_ptr screen_dump
,
1228 vga_hw_text_update_ptr text_update
,
1233 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1236 s
->hw_update
= update
;
1237 s
->hw_invalidate
= invalidate
;
1238 s
->hw_screen_dump
= screen_dump
;
1239 s
->hw_text_update
= text_update
;
1244 int is_graphic_console(void)
1246 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1249 void console_color_init(DisplayState
*ds
)
1252 for (j
= 0; j
< 2; j
++) {
1253 for (i
= 0; i
< 8; i
++) {
1254 color_table
[j
][i
] = col_expand(ds
,
1255 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1260 CharDriverState
*text_console_init(DisplayState
*ds
, const char *p
)
1262 CharDriverState
*chr
;
1266 static int color_inited
;
1268 chr
= qemu_mallocz(sizeof(CharDriverState
));
1271 s
= new_console(ds
, (p
== 0) ? TEXT_CONSOLE
: TEXT_CONSOLE_FIXED_SIZE
);
1277 chr
->chr_write
= console_puts
;
1278 chr
->chr_send_event
= console_send_event
;
1281 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1282 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1283 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1285 if (!color_inited
) {
1287 console_color_init(s
->ds
);
1291 s
->total_height
= DEFAULT_BACKSCROLL
;
1294 width
= s
->ds
->width
;
1295 height
= s
->ds
->height
;
1297 width
= strtoul(p
, (char **)&p
, 10);
1300 width
*= FONT_WIDTH
;
1304 height
= strtoul(p
, (char **)&p
, 10);
1307 height
*= FONT_HEIGHT
;
1312 s
->g_height
= height
;
1314 s
->hw_invalidate
= text_console_invalidate
;
1315 s
->hw_text_update
= text_console_update
;
1318 /* Set text attribute defaults */
1319 s
->t_attrib_default
.bold
= 0;
1320 s
->t_attrib_default
.uline
= 0;
1321 s
->t_attrib_default
.blink
= 0;
1322 s
->t_attrib_default
.invers
= 0;
1323 s
->t_attrib_default
.unvisible
= 0;
1324 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1325 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1327 /* set current text attributes to default */
1328 s
->t_attrib
= s
->t_attrib_default
;
1329 text_console_resize(s
);
1331 qemu_chr_reset(chr
);