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
31 #define DEFAULT_MONITOR_SIZE "800x600"
33 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
34 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
36 typedef struct TextAttributes
{
46 typedef struct TextCell
{
48 TextAttributes t_attrib
;
51 #define MAX_ESC_PARAMS 3
59 typedef struct QEMUFIFO
{
62 int count
, wptr
, rptr
;
65 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
69 l
= f
->buf_size
- f
->count
;
74 l
= f
->buf_size
- f
->wptr
;
77 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
79 if (f
->wptr
>= f
->buf_size
)
88 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
96 l
= f
->buf_size
- f
->rptr
;
99 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
101 if (f
->rptr
>= f
->buf_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 TextConsole
*previous_active_console
;
172 previous_active_console
= active_console
;
173 active_console
= consoles
[0];
174 /* There is currently no way of specifying which screen we want to dump,
175 so always dump the first one. */
176 if (consoles
[0]->hw_screen_dump
)
177 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
178 active_console
= previous_active_console
;
181 void vga_hw_text_update(console_ch_t
*chardata
)
183 if (active_console
&& active_console
->hw_text_update
)
184 active_console
->hw_text_update(active_console
->hw
, chardata
);
187 /* convert a RGBA color to a color index usable in graphic primitives */
188 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
190 unsigned int r
, g
, b
, color
;
195 r
= (rgba
>> 16) & 0xff;
196 g
= (rgba
>> 8) & 0xff;
198 color
= (rgb_to_index
[r
] * 6 * 6) +
199 (rgb_to_index
[g
] * 6) +
204 r
= (rgba
>> 16) & 0xff;
205 g
= (rgba
>> 8) & 0xff;
207 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
210 r
= (rgba
>> 16) & 0xff;
211 g
= (rgba
>> 8) & 0xff;
213 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
223 static void vga_fill_rect (DisplayState
*ds
,
224 int posx
, int posy
, int width
, int height
, uint32_t color
)
229 bpp
= (ds
->depth
+ 7) >> 3;
231 ds
->linesize
* posy
+ bpp
* posx
;
232 for (y
= 0; y
< height
; y
++) {
236 for (x
= 0; x
< width
; x
++) {
237 *((uint8_t *)d
) = color
;
242 for (x
= 0; x
< width
; x
++) {
243 *((uint16_t *)d
) = color
;
248 for (x
= 0; x
< width
; x
++) {
249 *((uint32_t *)d
) = color
;
258 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
259 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
265 bpp
= (ds
->depth
+ 7) >> 3;
269 ds
->linesize
* ys
+ bpp
* xs
;
271 ds
->linesize
* yd
+ bpp
* xd
;
272 for (y
= 0; y
< h
; y
++) {
279 ds
->linesize
* (ys
+ h
- 1) + bpp
* xs
;
281 ds
->linesize
* (yd
+ h
- 1) + bpp
* xd
;
282 for (y
= 0; y
< h
; y
++) {
290 /***********************************************************/
291 /* basic char display */
293 #define FONT_HEIGHT 16
298 #define cbswap_32(__x) \
300 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
301 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
302 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
303 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
305 #ifdef WORDS_BIGENDIAN
308 #define PAT(x) cbswap_32(x)
311 static const uint32_t dmask16
[16] = {
330 static const uint32_t dmask4
[4] = {
337 static uint32_t color_table
[2][8];
350 static const uint32_t color_table_rgb
[2][8] = {
352 QEMU_RGB(0x00, 0x00, 0x00), /* black */
353 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
354 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
355 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
356 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
357 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
358 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
359 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
362 QEMU_RGB(0x00, 0x00, 0x00), /* black */
363 QEMU_RGB(0xff, 0x00, 0x00), /* red */
364 QEMU_RGB(0x00, 0xff, 0x00), /* green */
365 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
366 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
367 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
368 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
369 QEMU_RGB(0xff, 0xff, 0xff), /* white */
373 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
391 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
393 if (t_attrib
->bold
) {
398 if (t_attrib
->uline
) {
403 if (t_attrib
->blink
) {
408 if (t_attrib
->invers
) {
413 if (t_attrib
->unvisible
) {
419 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
423 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
424 TextAttributes
*t_attrib
)
427 const uint8_t *font_ptr
;
428 unsigned int font_data
, linesize
, xorcol
, bpp
;
430 unsigned int fgcol
, bgcol
;
433 printf("x: %2i y: %2i", x
, y
);
434 console_print_text_attributes(t_attrib
, ch
);
437 if (t_attrib
->invers
) {
438 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
439 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
441 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
442 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
445 bpp
= (ds
->depth
+ 7) >> 3;
447 ds
->linesize
* y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
448 linesize
= ds
->linesize
;
449 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
450 xorcol
= bgcol
^ fgcol
;
453 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
454 font_data
= *font_ptr
++;
456 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
459 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
460 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
466 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
467 font_data
= *font_ptr
++;
469 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
472 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
473 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
474 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
475 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
480 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
481 font_data
= *font_ptr
++;
482 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
485 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
486 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
487 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
488 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
489 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
490 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
491 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
492 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
499 static void text_console_resize(TextConsole
*s
)
501 TextCell
*cells
, *c
, *c1
;
502 int w1
, x
, y
, last_width
;
504 last_width
= s
->width
;
505 s
->width
= s
->g_width
/ FONT_WIDTH
;
506 s
->height
= s
->g_height
/ FONT_HEIGHT
;
512 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
513 for(y
= 0; y
< s
->total_height
; y
++) {
514 c
= &cells
[y
* s
->width
];
516 c1
= &s
->cells
[y
* last_width
];
517 for(x
= 0; x
< w1
; x
++) {
521 for(x
= w1
; x
< s
->width
; x
++) {
523 c
->t_attrib
= s
->t_attrib_default
;
531 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
533 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
534 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
535 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
536 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
539 static void update_xy(TextConsole
*s
, int x
, int y
)
544 if (s
== active_console
) {
546 text_update_xy(s
, x
, y
);
550 y1
= (s
->y_base
+ y
) % s
->total_height
;
551 y2
= y1
- s
->y_displayed
;
553 y2
+= s
->total_height
;
554 if (y2
< s
->height
) {
555 c
= &s
->cells
[y1
* s
->width
+ x
];
556 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
558 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
559 FONT_WIDTH
, FONT_HEIGHT
);
564 static void console_show_cursor(TextConsole
*s
, int show
)
569 if (s
== active_console
) {
573 s
->cursor_invalidate
= 1;
580 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
581 y
= y1
- s
->y_displayed
;
583 y
+= s
->total_height
;
585 c
= &s
->cells
[y1
* s
->width
+ x
];
587 TextAttributes t_attrib
= s
->t_attrib_default
;
588 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
589 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
591 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
593 dpy_update(s
->ds
, x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
594 FONT_WIDTH
, FONT_HEIGHT
);
599 static void console_refresh(TextConsole
*s
)
604 if (s
!= active_console
)
609 s
->text_x
[1] = s
->width
- 1;
610 s
->text_y
[1] = s
->height
- 1;
611 s
->cursor_invalidate
= 1;
615 vga_fill_rect(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
,
616 color_table
[0][COLOR_BLACK
]);
618 for(y
= 0; y
< s
->height
; y
++) {
619 c
= s
->cells
+ y1
* s
->width
;
620 for(x
= 0; x
< s
->width
; x
++) {
621 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
625 if (++y1
== s
->total_height
)
628 dpy_update(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
);
629 console_show_cursor(s
, 1);
632 static void console_scroll(int ydelta
)
638 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
642 for(i
= 0; i
< ydelta
; i
++) {
643 if (s
->y_displayed
== s
->y_base
)
645 if (++s
->y_displayed
== s
->total_height
)
650 i
= s
->backscroll_height
;
651 if (i
> s
->total_height
- s
->height
)
652 i
= s
->total_height
- s
->height
;
655 y1
+= s
->total_height
;
656 for(i
= 0; i
< ydelta
; i
++) {
657 if (s
->y_displayed
== y1
)
659 if (--s
->y_displayed
< 0)
660 s
->y_displayed
= s
->total_height
- 1;
666 static void console_put_lf(TextConsole
*s
)
672 if (s
->y
>= s
->height
) {
673 s
->y
= s
->height
- 1;
675 if (s
->y_displayed
== s
->y_base
) {
676 if (++s
->y_displayed
== s
->total_height
)
679 if (++s
->y_base
== s
->total_height
)
681 if (s
->backscroll_height
< s
->total_height
)
682 s
->backscroll_height
++;
683 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
684 c
= &s
->cells
[y1
* s
->width
];
685 for(x
= 0; x
< s
->width
; x
++) {
687 c
->t_attrib
= s
->t_attrib_default
;
690 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
694 s
->text_x
[1] = s
->width
- 1;
695 s
->text_y
[1] = s
->height
- 1;
699 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
700 s
->width
* FONT_WIDTH
,
701 (s
->height
- 1) * FONT_HEIGHT
);
702 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
703 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
704 color_table
[0][s
->t_attrib_default
.bgcol
]);
705 dpy_update(s
->ds
, 0, 0,
706 s
->width
* FONT_WIDTH
, s
->height
* FONT_HEIGHT
);
711 /* Set console attributes depending on the current escape codes.
712 * NOTE: I know this code is not very efficient (checking every color for it
713 * self) but it is more readable and better maintainable.
715 static void console_handle_escape(TextConsole
*s
)
719 for (i
=0; i
<s
->nb_esc_params
; i
++) {
720 switch (s
->esc_params
[i
]) {
721 case 0: /* reset all console attributes to default */
722 s
->t_attrib
= s
->t_attrib_default
;
725 s
->t_attrib
.bold
= 1;
728 s
->t_attrib
.uline
= 1;
731 s
->t_attrib
.blink
= 1;
734 s
->t_attrib
.invers
= 1;
737 s
->t_attrib
.unvisible
= 1;
740 s
->t_attrib
.bold
= 0;
743 s
->t_attrib
.uline
= 0;
746 s
->t_attrib
.blink
= 0;
749 s
->t_attrib
.invers
= 0;
752 s
->t_attrib
.unvisible
= 0;
754 /* set foreground color */
756 s
->t_attrib
.fgcol
=COLOR_BLACK
;
759 s
->t_attrib
.fgcol
=COLOR_RED
;
762 s
->t_attrib
.fgcol
=COLOR_GREEN
;
765 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
768 s
->t_attrib
.fgcol
=COLOR_BLUE
;
771 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
774 s
->t_attrib
.fgcol
=COLOR_CYAN
;
777 s
->t_attrib
.fgcol
=COLOR_WHITE
;
779 /* set background color */
781 s
->t_attrib
.bgcol
=COLOR_BLACK
;
784 s
->t_attrib
.bgcol
=COLOR_RED
;
787 s
->t_attrib
.bgcol
=COLOR_GREEN
;
790 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
793 s
->t_attrib
.bgcol
=COLOR_BLUE
;
796 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
799 s
->t_attrib
.bgcol
=COLOR_CYAN
;
802 s
->t_attrib
.bgcol
=COLOR_WHITE
;
808 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
810 int y1
= (s
->y_base
+ y
) % s
->total_height
;
811 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
813 c
->t_attrib
= s
->t_attrib_default
;
818 static void console_putchar(TextConsole
*s
, int ch
)
827 case '\r': /* carriage return */
830 case '\n': /* newline */
833 case '\b': /* backspace */
837 case '\t': /* tabspace */
838 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
842 s
->x
= s
->x
+ (8 - (s
->x
% 8));
845 case '\a': /* alert aka. bell */
846 /* TODO: has to be implemented */
849 /* SI (shift in), character set 0 (ignored) */
852 /* SO (shift out), character set 1 (ignored) */
854 case 27: /* esc (introducing an escape sequence) */
855 s
->state
= TTY_STATE_ESC
;
858 if (s
->x
>= s
->width
) {
863 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
864 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
866 c
->t_attrib
= s
->t_attrib
;
867 update_xy(s
, s
->x
, s
->y
);
872 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
874 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
875 s
->esc_params
[i
] = 0;
876 s
->nb_esc_params
= 0;
877 s
->state
= TTY_STATE_CSI
;
879 s
->state
= TTY_STATE_NORM
;
882 case TTY_STATE_CSI
: /* handle escape sequence parameters */
883 if (ch
>= '0' && ch
<= '9') {
884 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
885 s
->esc_params
[s
->nb_esc_params
] =
886 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
893 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
894 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
896 s
->state
= TTY_STATE_NORM
;
900 if (s
->esc_params
[0] == 0) {
901 s
->esc_params
[0] = 1;
903 s
->y
-= s
->esc_params
[0];
909 /* move cursor down */
910 if (s
->esc_params
[0] == 0) {
911 s
->esc_params
[0] = 1;
913 s
->y
+= s
->esc_params
[0];
914 if (s
->y
>= s
->height
) {
915 s
->y
= s
->height
- 1;
919 /* move cursor right */
920 if (s
->esc_params
[0] == 0) {
921 s
->esc_params
[0] = 1;
923 s
->x
+= s
->esc_params
[0];
924 if (s
->x
>= s
->width
) {
929 /* move cursor left */
930 if (s
->esc_params
[0] == 0) {
931 s
->esc_params
[0] = 1;
933 s
->x
-= s
->esc_params
[0];
939 /* move cursor to column */
940 s
->x
= s
->esc_params
[0] - 1;
947 /* move cursor to row, column */
948 s
->x
= s
->esc_params
[1] - 1;
952 s
->y
= s
->esc_params
[0] - 1;
958 switch (s
->esc_params
[0]) {
960 /* clear to end of screen */
961 for (y
= s
->y
; y
< s
->height
; y
++) {
962 for (x
= 0; x
< s
->width
; x
++) {
963 if (y
== s
->y
&& x
< s
->x
) {
966 console_clear_xy(s
, x
, y
);
971 /* clear from beginning of screen */
972 for (y
= 0; y
<= s
->y
; y
++) {
973 for (x
= 0; x
< s
->width
; x
++) {
974 if (y
== s
->y
&& x
> s
->x
) {
977 console_clear_xy(s
, x
, y
);
982 /* clear entire screen */
983 for (y
= 0; y
<= s
->height
; y
++) {
984 for (x
= 0; x
< s
->width
; x
++) {
985 console_clear_xy(s
, x
, y
);
991 switch (s
->esc_params
[0]) {
994 for(x
= s
->x
; x
< s
->width
; x
++) {
995 console_clear_xy(s
, x
, s
->y
);
999 /* clear from beginning of line */
1000 for (x
= 0; x
<= s
->x
; x
++) {
1001 console_clear_xy(s
, x
, s
->y
);
1005 /* clear entire line */
1006 for(x
= 0; x
< s
->width
; x
++) {
1007 console_clear_xy(s
, x
, s
->y
);
1013 console_handle_escape(s
);
1016 /* report cursor position */
1017 /* TODO: send ESC[row;colR */
1020 /* save cursor position */
1025 /* restore cursor position */
1030 #ifdef DEBUG_CONSOLE
1031 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1040 void console_select(unsigned int index
)
1044 if (index
>= MAX_CONSOLES
)
1046 s
= consoles
[index
];
1049 if (s
->g_width
&& s
->g_height
1050 && (s
->g_width
!= s
->ds
->width
|| s
->g_height
!= s
->ds
->height
))
1051 dpy_resize(s
->ds
, s
->g_width
, s
->g_height
);
1052 vga_hw_invalidate();
1056 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1058 TextConsole
*s
= chr
->opaque
;
1061 console_show_cursor(s
, 0);
1062 for(i
= 0; i
< len
; i
++) {
1063 console_putchar(s
, buf
[i
]);
1065 console_show_cursor(s
, 1);
1069 static void console_send_event(CharDriverState
*chr
, int event
)
1071 TextConsole
*s
= chr
->opaque
;
1074 if (event
== CHR_EVENT_FOCUS
) {
1075 for(i
= 0; i
< nb_consoles
; i
++) {
1076 if (consoles
[i
] == s
) {
1084 static void kbd_send_chars(void *opaque
)
1086 TextConsole
*s
= opaque
;
1090 len
= qemu_chr_can_read(s
->chr
);
1091 if (len
> s
->out_fifo
.count
)
1092 len
= s
->out_fifo
.count
;
1094 if (len
> sizeof(buf
))
1096 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1097 qemu_chr_read(s
->chr
, buf
, len
);
1099 /* characters are pending: we send them a bit later (XXX:
1100 horrible, should change char device API) */
1101 if (s
->out_fifo
.count
> 0) {
1102 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1106 /* called when an ascii key is pressed */
1107 void kbd_put_keysym(int keysym
)
1110 uint8_t buf
[16], *q
;
1114 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1118 case QEMU_KEY_CTRL_UP
:
1121 case QEMU_KEY_CTRL_DOWN
:
1124 case QEMU_KEY_CTRL_PAGEUP
:
1125 console_scroll(-10);
1127 case QEMU_KEY_CTRL_PAGEDOWN
:
1131 /* convert the QEMU keysym to VT100 key string */
1133 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1136 c
= keysym
- 0xe100;
1138 *q
++ = '0' + (c
/ 10);
1139 *q
++ = '0' + (c
% 10);
1141 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1144 *q
++ = keysym
& 0xff;
1148 if (s
->chr
->chr_read
) {
1149 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1156 static void text_console_invalidate(void *opaque
)
1158 TextConsole
*s
= (TextConsole
*) opaque
;
1163 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1165 TextConsole
*s
= (TextConsole
*) opaque
;
1168 if (s
->text_x
[0] <= s
->text_x
[1]) {
1169 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1170 chardata
+= s
->text_y
[0] * s
->width
;
1171 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1172 for (j
= 0; j
< s
->width
; j
++, src
++)
1173 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1174 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1175 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1176 (s
->cells
[src
].t_attrib
.bold
<< 21));
1177 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1178 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1179 s
->text_x
[0] = s
->width
;
1180 s
->text_y
[0] = s
->height
;
1184 if (s
->cursor_invalidate
) {
1185 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1186 s
->cursor_invalidate
= 0;
1190 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1195 if (nb_consoles
>= MAX_CONSOLES
)
1197 s
= qemu_mallocz(sizeof(TextConsole
));
1201 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1202 (console_type
== GRAPHIC_CONSOLE
))) {
1206 s
->console_type
= console_type
;
1207 if (console_type
!= GRAPHIC_CONSOLE
) {
1208 consoles
[nb_consoles
++] = s
;
1210 /* HACK: Put graphical consoles before text consoles. */
1211 for (i
= nb_consoles
; i
> 0; i
--) {
1212 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1214 consoles
[i
] = consoles
[i
- 1];
1221 TextConsole
*graphic_console_init(DisplayState
*ds
, vga_hw_update_ptr update
,
1222 vga_hw_invalidate_ptr invalidate
,
1223 vga_hw_screen_dump_ptr screen_dump
,
1224 vga_hw_text_update_ptr text_update
,
1229 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1232 s
->hw_update
= update
;
1233 s
->hw_invalidate
= invalidate
;
1234 s
->hw_screen_dump
= screen_dump
;
1235 s
->hw_text_update
= text_update
;
1240 int is_graphic_console(void)
1242 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1245 void console_color_init(DisplayState
*ds
)
1248 for (j
= 0; j
< 2; j
++) {
1249 for (i
= 0; i
< 8; i
++) {
1250 color_table
[j
][i
] = col_expand(ds
,
1251 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1256 CharDriverState
*text_console_init(DisplayState
*ds
, const char *p
)
1258 CharDriverState
*chr
;
1262 static int color_inited
;
1264 chr
= qemu_mallocz(sizeof(CharDriverState
));
1267 s
= new_console(ds
, TEXT_CONSOLE
);
1273 p
= DEFAULT_MONITOR_SIZE
;
1276 chr
->chr_write
= console_puts
;
1277 chr
->chr_send_event
= console_send_event
;
1280 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1281 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1282 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1284 if (!color_inited
) {
1286 console_color_init(s
->ds
);
1290 s
->total_height
= DEFAULT_BACKSCROLL
;
1293 width
= s
->ds
->width
;
1294 height
= s
->ds
->height
;
1296 width
= strtoul(p
, (char **)&p
, 10);
1299 width
*= FONT_WIDTH
;
1303 height
= strtoul(p
, (char **)&p
, 10);
1306 height
*= FONT_HEIGHT
;
1311 s
->g_height
= height
;
1313 s
->hw_invalidate
= text_console_invalidate
;
1314 s
->hw_text_update
= text_console_update
;
1317 /* Set text attribute defaults */
1318 s
->t_attrib_default
.bold
= 0;
1319 s
->t_attrib_default
.uline
= 0;
1320 s
->t_attrib_default
.blink
= 0;
1321 s
->t_attrib_default
.invers
= 0;
1322 s
->t_attrib_default
.unvisible
= 0;
1323 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1324 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1326 /* set current text attributes to default */
1327 s
->t_attrib
= s
->t_attrib_default
;
1328 text_console_resize(s
);
1330 qemu_chr_reset(chr
);
1335 void qemu_console_resize(QEMUConsole
*console
, int width
, int height
)
1337 if (console
->g_width
!= width
|| console
->g_height
!= height
1338 || !console
->ds
->data
) {
1339 console
->g_width
= width
;
1340 console
->g_height
= height
;
1341 if (active_console
== console
) {
1342 dpy_resize(console
->ds
, width
, height
);