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
;
126 int g_width
, g_height
;
130 int backscroll_height
;
132 int x_saved
, y_saved
;
135 TextAttributes t_attrib_default
; /* default text attributes */
136 TextAttributes t_attrib
; /* currently active text attributes */
140 int esc_params
[MAX_ESC_PARAMS
];
143 CharDriverState
*chr
;
144 /* fifo for key pressed */
146 uint8_t out_fifo_buf
[16];
147 QEMUTimer
*kbd_timer
;
150 static TextConsole
*active_console
;
151 static TextConsole
*consoles
[MAX_CONSOLES
];
152 static int nb_consoles
= 0;
154 void vga_hw_update(void)
156 if (active_console
&& active_console
->hw_update
)
157 active_console
->hw_update(active_console
->hw
);
160 void vga_hw_invalidate(void)
162 if (active_console
->hw_invalidate
)
163 active_console
->hw_invalidate(active_console
->hw
);
166 void vga_hw_screen_dump(const char *filename
)
168 /* There is currently no was of specifying which screen we want to dump,
169 so always dump the dirst one. */
170 if (consoles
[0]->hw_screen_dump
)
171 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
174 /* convert a RGBA color to a color index usable in graphic primitives */
175 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
177 unsigned int r
, g
, b
, color
;
182 r
= (rgba
>> 16) & 0xff;
183 g
= (rgba
>> 8) & 0xff;
185 color
= (rgb_to_index
[r
] * 6 * 6) +
186 (rgb_to_index
[g
] * 6) +
191 r
= (rgba
>> 16) & 0xff;
192 g
= (rgba
>> 8) & 0xff;
194 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
197 r
= (rgba
>> 16) & 0xff;
198 g
= (rgba
>> 8) & 0xff;
200 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
210 static void vga_fill_rect (DisplayState
*ds
,
211 int posx
, int posy
, int width
, int height
, uint32_t color
)
216 bpp
= (ds
->depth
+ 7) >> 3;
218 ds
->linesize
* posy
+ bpp
* posx
;
219 for (y
= 0; y
< height
; y
++) {
223 for (x
= 0; x
< width
; x
++) {
224 *((uint8_t *)d
) = color
;
229 for (x
= 0; x
< width
; x
++) {
230 *((uint16_t *)d
) = color
;
235 for (x
= 0; x
< width
; x
++) {
236 *((uint32_t *)d
) = color
;
245 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
246 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
252 bpp
= (ds
->depth
+ 7) >> 3;
256 ds
->linesize
* ys
+ bpp
* xs
;
258 ds
->linesize
* yd
+ bpp
* xd
;
259 for (y
= 0; y
< h
; y
++) {
266 ds
->linesize
* (ys
+ h
- 1) + bpp
* xs
;
268 ds
->linesize
* (yd
+ h
- 1) + bpp
* xd
;
269 for (y
= 0; y
< h
; y
++) {
277 /***********************************************************/
278 /* basic char display */
280 #define FONT_HEIGHT 16
285 #define cbswap_32(__x) \
287 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
288 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
289 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
290 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
292 #ifdef WORDS_BIGENDIAN
295 #define PAT(x) cbswap_32(x)
298 static const uint32_t dmask16
[16] = {
317 static const uint32_t dmask4
[4] = {
324 static uint32_t color_table
[2][8];
337 static const uint32_t color_table_rgb
[2][8] = {
339 QEMU_RGB(0x00, 0x00, 0x00), /* black */
340 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
341 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
342 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
343 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
344 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
345 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
346 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
349 QEMU_RGB(0x00, 0x00, 0x00), /* black */
350 QEMU_RGB(0xff, 0x00, 0x00), /* red */
351 QEMU_RGB(0x00, 0xff, 0x00), /* green */
352 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
353 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
354 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
355 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
356 QEMU_RGB(0xff, 0xff, 0xff), /* white */
360 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
378 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
380 if (t_attrib
->bold
) {
385 if (t_attrib
->uline
) {
390 if (t_attrib
->blink
) {
395 if (t_attrib
->invers
) {
400 if (t_attrib
->unvisible
) {
406 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
410 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
411 TextAttributes
*t_attrib
)
414 const uint8_t *font_ptr
;
415 unsigned int font_data
, linesize
, xorcol
, bpp
;
417 unsigned int fgcol
, bgcol
;
420 printf("x: %2i y: %2i", x
, y
);
421 console_print_text_attributes(t_attrib
, ch
);
424 if (t_attrib
->invers
) {
425 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
426 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
428 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
429 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
432 bpp
= (ds
->depth
+ 7) >> 3;
434 ds
->linesize
* y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
435 linesize
= ds
->linesize
;
436 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
437 xorcol
= bgcol
^ fgcol
;
440 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
441 font_data
= *font_ptr
++;
443 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
446 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
447 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
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] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
460 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
461 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
462 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
467 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
468 font_data
= *font_ptr
++;
469 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
472 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
473 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
474 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
475 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
476 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
477 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
478 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
479 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
486 static void text_console_resize(TextConsole
*s
)
488 TextCell
*cells
, *c
, *c1
;
489 int w1
, x
, y
, last_width
;
491 last_width
= s
->width
;
492 s
->width
= s
->g_width
/ FONT_WIDTH
;
493 s
->height
= s
->g_height
/ FONT_HEIGHT
;
499 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
500 for(y
= 0; y
< s
->total_height
; y
++) {
501 c
= &cells
[y
* s
->width
];
503 c1
= &s
->cells
[y
* last_width
];
504 for(x
= 0; x
< w1
; x
++) {
508 for(x
= w1
; x
< s
->width
; x
++) {
510 c
->t_attrib
= s
->t_attrib_default
;
518 static void update_xy(TextConsole
*s
, int x
, int y
)
523 if (s
== active_console
) {
524 y1
= (s
->y_base
+ y
) % s
->total_height
;
525 y2
= y1
- s
->y_displayed
;
527 y2
+= s
->total_height
;
528 if (y2
< s
->height
) {
529 c
= &s
->cells
[y1
* s
->width
+ x
];
530 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
532 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
533 FONT_WIDTH
, FONT_HEIGHT
);
538 static void console_show_cursor(TextConsole
*s
, int show
)
543 if (s
== active_console
) {
548 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
549 y
= y1
- s
->y_displayed
;
551 y
+= s
->total_height
;
553 c
= &s
->cells
[y1
* s
->width
+ x
];
555 TextAttributes t_attrib
= s
->t_attrib_default
;
556 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
557 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
559 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
561 dpy_update(s
->ds
, x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
562 FONT_WIDTH
, FONT_HEIGHT
);
567 static void console_refresh(TextConsole
*s
)
572 if (s
!= active_console
)
575 vga_fill_rect(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
,
576 color_table
[0][COLOR_BLACK
]);
578 for(y
= 0; y
< s
->height
; y
++) {
579 c
= s
->cells
+ y1
* s
->width
;
580 for(x
= 0; x
< s
->width
; x
++) {
581 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
585 if (++y1
== s
->total_height
)
588 dpy_update(s
->ds
, 0, 0, s
->ds
->width
, s
->ds
->height
);
589 console_show_cursor(s
, 1);
592 static void console_scroll(int ydelta
)
598 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
602 for(i
= 0; i
< ydelta
; i
++) {
603 if (s
->y_displayed
== s
->y_base
)
605 if (++s
->y_displayed
== s
->total_height
)
610 i
= s
->backscroll_height
;
611 if (i
> s
->total_height
- s
->height
)
612 i
= s
->total_height
- s
->height
;
615 y1
+= s
->total_height
;
616 for(i
= 0; i
< ydelta
; i
++) {
617 if (s
->y_displayed
== y1
)
619 if (--s
->y_displayed
< 0)
620 s
->y_displayed
= s
->total_height
- 1;
626 static void console_put_lf(TextConsole
*s
)
632 if (s
->y
>= s
->height
) {
633 s
->y
= s
->height
- 1;
635 if (s
->y_displayed
== s
->y_base
) {
636 if (++s
->y_displayed
== s
->total_height
)
639 if (++s
->y_base
== s
->total_height
)
641 if (s
->backscroll_height
< s
->total_height
)
642 s
->backscroll_height
++;
643 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
644 c
= &s
->cells
[y1
* s
->width
];
645 for(x
= 0; x
< s
->width
; x
++) {
647 c
->t_attrib
= s
->t_attrib_default
;
650 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
651 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
652 s
->width
* FONT_WIDTH
,
653 (s
->height
- 1) * FONT_HEIGHT
);
654 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
655 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
656 color_table
[0][s
->t_attrib_default
.bgcol
]);
657 dpy_update(s
->ds
, 0, 0,
658 s
->width
* FONT_WIDTH
, s
->height
* FONT_HEIGHT
);
663 /* Set console attributes depending on the current escape codes.
664 * NOTE: I know this code is not very efficient (checking every color for it
665 * self) but it is more readable and better maintainable.
667 static void console_handle_escape(TextConsole
*s
)
671 for (i
=0; i
<s
->nb_esc_params
; i
++) {
672 switch (s
->esc_params
[i
]) {
673 case 0: /* reset all console attributes to default */
674 s
->t_attrib
= s
->t_attrib_default
;
677 s
->t_attrib
.bold
= 1;
680 s
->t_attrib
.uline
= 1;
683 s
->t_attrib
.blink
= 1;
686 s
->t_attrib
.invers
= 1;
689 s
->t_attrib
.unvisible
= 1;
692 s
->t_attrib
.bold
= 0;
695 s
->t_attrib
.uline
= 0;
698 s
->t_attrib
.blink
= 0;
701 s
->t_attrib
.invers
= 0;
704 s
->t_attrib
.unvisible
= 0;
706 /* set foreground color */
708 s
->t_attrib
.fgcol
=COLOR_BLACK
;
711 s
->t_attrib
.fgcol
=COLOR_RED
;
714 s
->t_attrib
.fgcol
=COLOR_GREEN
;
717 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
720 s
->t_attrib
.fgcol
=COLOR_BLUE
;
723 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
726 s
->t_attrib
.fgcol
=COLOR_CYAN
;
729 s
->t_attrib
.fgcol
=COLOR_WHITE
;
731 /* set background color */
733 s
->t_attrib
.bgcol
=COLOR_BLACK
;
736 s
->t_attrib
.bgcol
=COLOR_RED
;
739 s
->t_attrib
.bgcol
=COLOR_GREEN
;
742 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
745 s
->t_attrib
.bgcol
=COLOR_BLUE
;
748 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
751 s
->t_attrib
.bgcol
=COLOR_CYAN
;
754 s
->t_attrib
.bgcol
=COLOR_WHITE
;
760 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
762 int y1
= (s
->y_base
+ y
) % s
->total_height
;
763 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
765 c
->t_attrib
= s
->t_attrib_default
;
770 static void console_putchar(TextConsole
*s
, int ch
)
779 case '\r': /* carriage return */
782 case '\n': /* newline */
785 case '\b': /* backspace */
789 case '\t': /* tabspace */
790 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
794 s
->x
= s
->x
+ (8 - (s
->x
% 8));
797 case '\a': /* alert aka. bell */
798 /* TODO: has to be implemented */
801 /* SI (shift in), character set 0 (ignored) */
804 /* SO (shift out), character set 1 (ignored) */
806 case 27: /* esc (introducing an escape sequence) */
807 s
->state
= TTY_STATE_ESC
;
810 if (s
->x
>= s
->width
) {
815 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
816 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
818 c
->t_attrib
= s
->t_attrib
;
819 update_xy(s
, s
->x
, s
->y
);
824 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
826 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
827 s
->esc_params
[i
] = 0;
828 s
->nb_esc_params
= 0;
829 s
->state
= TTY_STATE_CSI
;
831 s
->state
= TTY_STATE_NORM
;
834 case TTY_STATE_CSI
: /* handle escape sequence parameters */
835 if (ch
>= '0' && ch
<= '9') {
836 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
837 s
->esc_params
[s
->nb_esc_params
] =
838 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
845 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
846 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
848 s
->state
= TTY_STATE_NORM
;
852 if (s
->esc_params
[0] == 0) {
853 s
->esc_params
[0] = 1;
855 s
->y
-= s
->esc_params
[0];
861 /* move cursor down */
862 if (s
->esc_params
[0] == 0) {
863 s
->esc_params
[0] = 1;
865 s
->y
+= s
->esc_params
[0];
866 if (s
->y
>= s
->height
) {
867 s
->y
= s
->height
- 1;
871 /* move cursor right */
872 if (s
->esc_params
[0] == 0) {
873 s
->esc_params
[0] = 1;
875 s
->x
+= s
->esc_params
[0];
876 if (s
->x
>= s
->width
) {
881 /* move cursor left */
882 if (s
->esc_params
[0] == 0) {
883 s
->esc_params
[0] = 1;
885 s
->x
-= s
->esc_params
[0];
891 /* move cursor to column */
892 s
->x
= s
->esc_params
[0] - 1;
899 /* move cursor to row, column */
900 s
->x
= s
->esc_params
[1] - 1;
904 s
->y
= s
->esc_params
[0] - 1;
910 switch (s
->esc_params
[0]) {
912 /* clear to end of screen */
913 for (y
= s
->y
; y
< s
->height
; y
++) {
914 for (x
= 0; x
< s
->width
; x
++) {
915 if (y
== s
->y
&& x
< s
->x
) {
918 console_clear_xy(s
, x
, y
);
923 /* clear from beginning of screen */
924 for (y
= 0; y
<= s
->y
; y
++) {
925 for (x
= 0; x
< s
->width
; x
++) {
926 if (y
== s
->y
&& x
> s
->x
) {
929 console_clear_xy(s
, x
, y
);
934 /* clear entire screen */
935 for (y
= 0; y
<= s
->height
; y
++) {
936 for (x
= 0; x
< s
->width
; x
++) {
937 console_clear_xy(s
, x
, y
);
943 switch (s
->esc_params
[0]) {
946 for(x
= s
->x
; x
< s
->width
; x
++) {
947 console_clear_xy(s
, x
, s
->y
);
951 /* clear from beginning of line */
952 for (x
= 0; x
<= s
->x
; x
++) {
953 console_clear_xy(s
, x
, s
->y
);
957 /* clear entire line */
958 for(x
= 0; x
< s
->width
; x
++) {
959 console_clear_xy(s
, x
, s
->y
);
965 console_handle_escape(s
);
968 /* report cursor position */
969 /* TODO: send ESC[row;colR */
972 /* save cursor position */
977 /* restore cursor position */
983 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
992 void console_select(unsigned int index
)
996 if (index
>= MAX_CONSOLES
)
1001 if (s
->console_type
!= GRAPHIC_CONSOLE
) {
1002 if (s
->g_width
!= s
->ds
->width
||
1003 s
->g_height
!= s
->ds
->height
) {
1004 if (s
->console_type
== TEXT_CONSOLE_FIXED_SIZE
) {
1005 dpy_resize(s
->ds
, s
->g_width
, s
->g_height
);
1007 s
->g_width
= s
->ds
->width
;
1008 s
->g_height
= s
->ds
->height
;
1009 text_console_resize(s
);
1014 vga_hw_invalidate();
1019 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1021 TextConsole
*s
= chr
->opaque
;
1024 console_show_cursor(s
, 0);
1025 for(i
= 0; i
< len
; i
++) {
1026 console_putchar(s
, buf
[i
]);
1028 console_show_cursor(s
, 1);
1032 static void console_send_event(CharDriverState
*chr
, int event
)
1034 TextConsole
*s
= chr
->opaque
;
1037 if (event
== CHR_EVENT_FOCUS
) {
1038 for(i
= 0; i
< nb_consoles
; i
++) {
1039 if (consoles
[i
] == s
) {
1047 static void kbd_send_chars(void *opaque
)
1049 TextConsole
*s
= opaque
;
1053 len
= qemu_chr_can_read(s
->chr
);
1054 if (len
> s
->out_fifo
.count
)
1055 len
= s
->out_fifo
.count
;
1057 if (len
> sizeof(buf
))
1059 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1060 qemu_chr_read(s
->chr
, buf
, len
);
1062 /* characters are pending: we send them a bit later (XXX:
1063 horrible, should change char device API) */
1064 if (s
->out_fifo
.count
> 0) {
1065 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1069 /* called when an ascii key is pressed */
1070 void kbd_put_keysym(int keysym
)
1073 uint8_t buf
[16], *q
;
1077 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1081 case QEMU_KEY_CTRL_UP
:
1084 case QEMU_KEY_CTRL_DOWN
:
1087 case QEMU_KEY_CTRL_PAGEUP
:
1088 console_scroll(-10);
1090 case QEMU_KEY_CTRL_PAGEDOWN
:
1094 /* convert the QEMU keysym to VT100 key string */
1096 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1099 c
= keysym
- 0xe100;
1101 *q
++ = '0' + (c
/ 10);
1102 *q
++ = '0' + (c
% 10);
1104 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1107 *q
++ = keysym
& 0xff;
1111 if (s
->chr
->chr_read
) {
1112 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1119 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1124 if (nb_consoles
>= MAX_CONSOLES
)
1126 s
= qemu_mallocz(sizeof(TextConsole
));
1130 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1131 (console_type
== GRAPHIC_CONSOLE
))) {
1135 s
->console_type
= console_type
;
1136 if (console_type
!= GRAPHIC_CONSOLE
) {
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]->console_type
== GRAPHIC_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
, GRAPHIC_CONSOLE
);
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
->console_type
== GRAPHIC_CONSOLE
;
1172 void console_color_init(DisplayState
*ds
)
1175 for (j
= 0; j
< 2; j
++) {
1176 for (i
= 0; i
< 8; i
++) {
1177 color_table
[j
][i
] = col_expand(ds
,
1178 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1183 CharDriverState
*text_console_init(DisplayState
*ds
, const char *p
)
1185 CharDriverState
*chr
;
1189 static int color_inited
;
1191 chr
= qemu_mallocz(sizeof(CharDriverState
));
1194 s
= new_console(ds
, (p
== 0) ? TEXT_CONSOLE
: TEXT_CONSOLE_FIXED_SIZE
);
1200 chr
->chr_write
= console_puts
;
1201 chr
->chr_send_event
= console_send_event
;
1204 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1205 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1206 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1208 if (!color_inited
) {
1210 console_color_init(s
->ds
);
1214 s
->total_height
= DEFAULT_BACKSCROLL
;
1217 width
= s
->ds
->width
;
1218 height
= s
->ds
->height
;
1220 width
= strtoul(p
, (char **)&p
, 10);
1223 width
*= FONT_WIDTH
;
1227 height
= strtoul(p
, (char **)&p
, 10);
1230 height
*= FONT_HEIGHT
;
1235 s
->g_height
= height
;
1237 /* Set text attribute defaults */
1238 s
->t_attrib_default
.bold
= 0;
1239 s
->t_attrib_default
.uline
= 0;
1240 s
->t_attrib_default
.blink
= 0;
1241 s
->t_attrib_default
.invers
= 0;
1242 s
->t_attrib_default
.unvisible
= 0;
1243 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1244 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1246 /* set current text attributes to default */
1247 s
->t_attrib
= s
->t_attrib_default
;
1248 text_console_resize(s
);
1250 qemu_chr_reset(chr
);