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
)
113 TEXT_CONSOLE_FIXED_SIZE
116 /* ??? This is mis-named.
117 It is used for both text and graphical consoles. */
119 console_type_t console_type
;
121 /* Graphic console state. */
122 vga_hw_update_ptr hw_update
;
123 vga_hw_invalidate_ptr hw_invalidate
;
124 vga_hw_screen_dump_ptr hw_screen_dump
;
125 vga_hw_text_update_ptr hw_text_update
;
128 int g_width
, g_height
;
132 int backscroll_height
;
134 int x_saved
, y_saved
;
137 TextAttributes t_attrib_default
; /* default text attributes */
138 TextAttributes t_attrib
; /* currently active text attributes */
140 int text_x
[2], text_y
[2], cursor_invalidate
;
143 int esc_params
[MAX_ESC_PARAMS
];
146 CharDriverState
*chr
;
147 /* fifo for key pressed */
149 uint8_t out_fifo_buf
[16];
150 QEMUTimer
*kbd_timer
;
153 static TextConsole
*active_console
;
154 static TextConsole
*consoles
[MAX_CONSOLES
];
155 static int nb_consoles
= 0;
157 void vga_hw_update(void)
159 if (active_console
&& active_console
->hw_update
)
160 active_console
->hw_update(active_console
->hw
);
163 void vga_hw_invalidate(void)
165 if (active_console
->hw_invalidate
)
166 active_console
->hw_invalidate(active_console
->hw
);
169 void vga_hw_screen_dump(const char *filename
)
171 TextConsole
*previous_active_console
;
173 previous_active_console
= active_console
;
174 active_console
= consoles
[0];
175 /* There is currently no way of specifying which screen we want to dump,
176 so always dump the first one. */
177 if (consoles
[0]->hw_screen_dump
)
178 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
179 active_console
= previous_active_console
;
182 void vga_hw_text_update(console_ch_t
*chardata
)
184 if (active_console
&& active_console
->hw_text_update
)
185 active_console
->hw_text_update(active_console
->hw
, chardata
);
188 /* convert a RGBA color to a color index usable in graphic primitives */
189 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
191 unsigned int r
, g
, b
, color
;
193 switch(ds_get_bits_per_pixel(ds
)) {
196 r
= (rgba
>> 16) & 0xff;
197 g
= (rgba
>> 8) & 0xff;
199 color
= (rgb_to_index
[r
] * 6 * 6) +
200 (rgb_to_index
[g
] * 6) +
205 r
= (rgba
>> 16) & 0xff;
206 g
= (rgba
>> 8) & 0xff;
208 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
211 r
= (rgba
>> 16) & 0xff;
212 g
= (rgba
>> 8) & 0xff;
214 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
224 static void vga_fill_rect (DisplayState
*ds
,
225 int posx
, int posy
, int width
, int height
, uint32_t color
)
230 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
231 d1
= ds_get_data(ds
) +
232 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
233 for (y
= 0; y
< height
; y
++) {
237 for (x
= 0; x
< width
; x
++) {
238 *((uint8_t *)d
) = color
;
243 for (x
= 0; x
< width
; x
++) {
244 *((uint16_t *)d
) = color
;
249 for (x
= 0; x
< width
; x
++) {
250 *((uint32_t *)d
) = color
;
255 d1
+= ds_get_linesize(ds
);
259 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
260 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
266 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
269 s
= ds_get_data(ds
) +
270 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
271 d
= ds_get_data(ds
) +
272 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
273 for (y
= 0; y
< h
; y
++) {
275 d
+= ds_get_linesize(ds
);
276 s
+= ds_get_linesize(ds
);
279 s
= ds_get_data(ds
) +
280 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
281 d
= ds_get_data(ds
) +
282 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
283 for (y
= 0; y
< h
; y
++) {
285 d
-= ds_get_linesize(ds
);
286 s
-= ds_get_linesize(ds
);
291 /***********************************************************/
292 /* basic char display */
294 #define FONT_HEIGHT 16
299 #define cbswap_32(__x) \
301 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
302 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
303 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
304 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
306 #ifdef WORDS_BIGENDIAN
309 #define PAT(x) cbswap_32(x)
312 static const uint32_t dmask16
[16] = {
331 static const uint32_t dmask4
[4] = {
338 static uint32_t color_table
[2][8];
351 static const uint32_t color_table_rgb
[2][8] = {
353 QEMU_RGB(0x00, 0x00, 0x00), /* black */
354 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
355 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
356 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
357 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
358 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
359 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
360 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
363 QEMU_RGB(0x00, 0x00, 0x00), /* black */
364 QEMU_RGB(0xff, 0x00, 0x00), /* red */
365 QEMU_RGB(0x00, 0xff, 0x00), /* green */
366 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
367 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
368 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
369 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
370 QEMU_RGB(0xff, 0xff, 0xff), /* white */
374 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
376 switch(ds_get_bits_per_pixel(ds
)) {
392 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
394 if (t_attrib
->bold
) {
399 if (t_attrib
->uline
) {
404 if (t_attrib
->blink
) {
409 if (t_attrib
->invers
) {
414 if (t_attrib
->unvisible
) {
420 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
424 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
425 TextAttributes
*t_attrib
)
428 const uint8_t *font_ptr
;
429 unsigned int font_data
, linesize
, xorcol
, bpp
;
431 unsigned int fgcol
, bgcol
;
434 printf("x: %2i y: %2i", x
, y
);
435 console_print_text_attributes(t_attrib
, ch
);
438 if (t_attrib
->invers
) {
439 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
440 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
442 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
443 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
446 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
447 d
= ds_get_data(ds
) +
448 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
449 linesize
= ds_get_linesize(ds
);
450 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
451 xorcol
= bgcol
^ fgcol
;
452 switch(ds_get_bits_per_pixel(ds
)) {
454 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
455 font_data
= *font_ptr
++;
457 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
460 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
461 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
467 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
468 font_data
= *font_ptr
++;
470 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
473 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
474 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
475 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
476 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
481 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
482 font_data
= *font_ptr
++;
483 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
486 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
487 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
488 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
489 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
490 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
491 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
492 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
493 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
500 static void text_console_resize(TextConsole
*s
)
502 TextCell
*cells
, *c
, *c1
;
503 int w1
, x
, y
, last_width
;
505 last_width
= s
->width
;
506 s
->width
= s
->g_width
/ FONT_WIDTH
;
507 s
->height
= s
->g_height
/ FONT_HEIGHT
;
513 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
514 for(y
= 0; y
< s
->total_height
; y
++) {
515 c
= &cells
[y
* s
->width
];
517 c1
= &s
->cells
[y
* last_width
];
518 for(x
= 0; x
< w1
; x
++) {
522 for(x
= w1
; x
< s
->width
; x
++) {
524 c
->t_attrib
= s
->t_attrib_default
;
532 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
534 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
535 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
536 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
537 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
540 static void update_xy(TextConsole
*s
, int x
, int y
)
545 if (s
== active_console
) {
546 if (!ds_get_bits_per_pixel(s
->ds
)) {
547 text_update_xy(s
, x
, y
);
551 y1
= (s
->y_base
+ y
) % s
->total_height
;
552 y2
= y1
- s
->y_displayed
;
554 y2
+= s
->total_height
;
555 if (y2
< s
->height
) {
556 c
= &s
->cells
[y1
* s
->width
+ x
];
557 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
559 dpy_update(s
->ds
, x
* FONT_WIDTH
, y2
* FONT_HEIGHT
,
560 FONT_WIDTH
, FONT_HEIGHT
);
565 static void console_show_cursor(TextConsole
*s
, int show
)
570 if (s
== active_console
) {
573 if (!ds_get_bits_per_pixel(s
->ds
)) {
574 s
->cursor_invalidate
= 1;
581 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
582 y
= y1
- s
->y_displayed
;
584 y
+= s
->total_height
;
586 c
= &s
->cells
[y1
* s
->width
+ x
];
588 TextAttributes t_attrib
= s
->t_attrib_default
;
589 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
590 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
592 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
594 dpy_update(s
->ds
, x
* FONT_WIDTH
, y
* FONT_HEIGHT
,
595 FONT_WIDTH
, FONT_HEIGHT
);
600 static void console_refresh(TextConsole
*s
)
605 if (s
!= active_console
)
607 if (!ds_get_bits_per_pixel(s
->ds
)) {
610 s
->text_x
[1] = s
->width
- 1;
611 s
->text_y
[1] = s
->height
- 1;
612 s
->cursor_invalidate
= 1;
616 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
617 color_table
[0][COLOR_BLACK
]);
619 for(y
= 0; y
< s
->height
; y
++) {
620 c
= s
->cells
+ y1
* s
->width
;
621 for(x
= 0; x
< s
->width
; x
++) {
622 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
626 if (++y1
== s
->total_height
)
629 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
630 console_show_cursor(s
, 1);
633 static void console_scroll(int ydelta
)
639 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
643 for(i
= 0; i
< ydelta
; i
++) {
644 if (s
->y_displayed
== s
->y_base
)
646 if (++s
->y_displayed
== s
->total_height
)
651 i
= s
->backscroll_height
;
652 if (i
> s
->total_height
- s
->height
)
653 i
= s
->total_height
- s
->height
;
656 y1
+= s
->total_height
;
657 for(i
= 0; i
< ydelta
; i
++) {
658 if (s
->y_displayed
== y1
)
660 if (--s
->y_displayed
< 0)
661 s
->y_displayed
= s
->total_height
- 1;
667 static void console_put_lf(TextConsole
*s
)
673 if (s
->y
>= s
->height
) {
674 s
->y
= s
->height
- 1;
676 if (s
->y_displayed
== s
->y_base
) {
677 if (++s
->y_displayed
== s
->total_height
)
680 if (++s
->y_base
== s
->total_height
)
682 if (s
->backscroll_height
< s
->total_height
)
683 s
->backscroll_height
++;
684 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
685 c
= &s
->cells
[y1
* s
->width
];
686 for(x
= 0; x
< s
->width
; x
++) {
688 c
->t_attrib
= s
->t_attrib_default
;
691 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
692 if (!ds_get_bits_per_pixel(s
->ds
)) {
695 s
->text_x
[1] = s
->width
- 1;
696 s
->text_y
[1] = s
->height
- 1;
700 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
701 s
->width
* FONT_WIDTH
,
702 (s
->height
- 1) * FONT_HEIGHT
);
703 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
704 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
705 color_table
[0][s
->t_attrib_default
.bgcol
]);
706 dpy_update(s
->ds
, 0, 0,
707 s
->width
* FONT_WIDTH
, s
->height
* FONT_HEIGHT
);
712 /* Set console attributes depending on the current escape codes.
713 * NOTE: I know this code is not very efficient (checking every color for it
714 * self) but it is more readable and better maintainable.
716 static void console_handle_escape(TextConsole
*s
)
720 for (i
=0; i
<s
->nb_esc_params
; i
++) {
721 switch (s
->esc_params
[i
]) {
722 case 0: /* reset all console attributes to default */
723 s
->t_attrib
= s
->t_attrib_default
;
726 s
->t_attrib
.bold
= 1;
729 s
->t_attrib
.uline
= 1;
732 s
->t_attrib
.blink
= 1;
735 s
->t_attrib
.invers
= 1;
738 s
->t_attrib
.unvisible
= 1;
741 s
->t_attrib
.bold
= 0;
744 s
->t_attrib
.uline
= 0;
747 s
->t_attrib
.blink
= 0;
750 s
->t_attrib
.invers
= 0;
753 s
->t_attrib
.unvisible
= 0;
755 /* set foreground color */
757 s
->t_attrib
.fgcol
=COLOR_BLACK
;
760 s
->t_attrib
.fgcol
=COLOR_RED
;
763 s
->t_attrib
.fgcol
=COLOR_GREEN
;
766 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
769 s
->t_attrib
.fgcol
=COLOR_BLUE
;
772 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
775 s
->t_attrib
.fgcol
=COLOR_CYAN
;
778 s
->t_attrib
.fgcol
=COLOR_WHITE
;
780 /* set background color */
782 s
->t_attrib
.bgcol
=COLOR_BLACK
;
785 s
->t_attrib
.bgcol
=COLOR_RED
;
788 s
->t_attrib
.bgcol
=COLOR_GREEN
;
791 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
794 s
->t_attrib
.bgcol
=COLOR_BLUE
;
797 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
800 s
->t_attrib
.bgcol
=COLOR_CYAN
;
803 s
->t_attrib
.bgcol
=COLOR_WHITE
;
809 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
811 int y1
= (s
->y_base
+ y
) % s
->total_height
;
812 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
814 c
->t_attrib
= s
->t_attrib_default
;
819 static void console_putchar(TextConsole
*s
, int ch
)
828 case '\r': /* carriage return */
831 case '\n': /* newline */
834 case '\b': /* backspace */
838 case '\t': /* tabspace */
839 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
843 s
->x
= s
->x
+ (8 - (s
->x
% 8));
846 case '\a': /* alert aka. bell */
847 /* TODO: has to be implemented */
850 /* SI (shift in), character set 0 (ignored) */
853 /* SO (shift out), character set 1 (ignored) */
855 case 27: /* esc (introducing an escape sequence) */
856 s
->state
= TTY_STATE_ESC
;
859 if (s
->x
>= s
->width
) {
864 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
865 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
867 c
->t_attrib
= s
->t_attrib
;
868 update_xy(s
, s
->x
, s
->y
);
873 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
875 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
876 s
->esc_params
[i
] = 0;
877 s
->nb_esc_params
= 0;
878 s
->state
= TTY_STATE_CSI
;
880 s
->state
= TTY_STATE_NORM
;
883 case TTY_STATE_CSI
: /* handle escape sequence parameters */
884 if (ch
>= '0' && ch
<= '9') {
885 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
886 s
->esc_params
[s
->nb_esc_params
] =
887 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
894 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
895 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
897 s
->state
= TTY_STATE_NORM
;
901 if (s
->esc_params
[0] == 0) {
902 s
->esc_params
[0] = 1;
904 s
->y
-= s
->esc_params
[0];
910 /* move cursor down */
911 if (s
->esc_params
[0] == 0) {
912 s
->esc_params
[0] = 1;
914 s
->y
+= s
->esc_params
[0];
915 if (s
->y
>= s
->height
) {
916 s
->y
= s
->height
- 1;
920 /* move cursor right */
921 if (s
->esc_params
[0] == 0) {
922 s
->esc_params
[0] = 1;
924 s
->x
+= s
->esc_params
[0];
925 if (s
->x
>= s
->width
) {
930 /* move cursor left */
931 if (s
->esc_params
[0] == 0) {
932 s
->esc_params
[0] = 1;
934 s
->x
-= s
->esc_params
[0];
940 /* move cursor to column */
941 s
->x
= s
->esc_params
[0] - 1;
948 /* move cursor to row, column */
949 s
->x
= s
->esc_params
[1] - 1;
953 s
->y
= s
->esc_params
[0] - 1;
959 switch (s
->esc_params
[0]) {
961 /* clear to end of screen */
962 for (y
= s
->y
; y
< s
->height
; y
++) {
963 for (x
= 0; x
< s
->width
; x
++) {
964 if (y
== s
->y
&& x
< s
->x
) {
967 console_clear_xy(s
, x
, y
);
972 /* clear from beginning of screen */
973 for (y
= 0; y
<= s
->y
; y
++) {
974 for (x
= 0; x
< s
->width
; x
++) {
975 if (y
== s
->y
&& x
> s
->x
) {
978 console_clear_xy(s
, x
, y
);
983 /* clear entire screen */
984 for (y
= 0; y
<= s
->height
; y
++) {
985 for (x
= 0; x
< s
->width
; x
++) {
986 console_clear_xy(s
, x
, y
);
992 switch (s
->esc_params
[0]) {
995 for(x
= s
->x
; x
< s
->width
; x
++) {
996 console_clear_xy(s
, x
, s
->y
);
1000 /* clear from beginning of line */
1001 for (x
= 0; x
<= s
->x
; x
++) {
1002 console_clear_xy(s
, x
, s
->y
);
1006 /* clear entire line */
1007 for(x
= 0; x
< s
->width
; x
++) {
1008 console_clear_xy(s
, x
, s
->y
);
1014 console_handle_escape(s
);
1017 /* report cursor position */
1018 /* TODO: send ESC[row;colR */
1021 /* save cursor position */
1026 /* restore cursor position */
1031 #ifdef DEBUG_CONSOLE
1032 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1041 void console_select(unsigned int index
)
1045 if (index
>= MAX_CONSOLES
)
1047 active_console
->g_width
= ds_get_width(active_console
->ds
);
1048 active_console
->g_height
= ds_get_height(active_console
->ds
);
1049 s
= consoles
[index
];
1051 DisplayState
*ds
= s
->ds
;
1053 ds
->surface
= qemu_resize_displaysurface(ds
->surface
, s
->g_width
,
1054 s
->g_height
, 32, 4 * s
->g_width
);
1056 vga_hw_invalidate();
1060 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1062 TextConsole
*s
= chr
->opaque
;
1065 console_show_cursor(s
, 0);
1066 for(i
= 0; i
< len
; i
++) {
1067 console_putchar(s
, buf
[i
]);
1069 console_show_cursor(s
, 1);
1073 static void console_send_event(CharDriverState
*chr
, int event
)
1075 TextConsole
*s
= chr
->opaque
;
1078 if (event
== CHR_EVENT_FOCUS
) {
1079 for(i
= 0; i
< nb_consoles
; i
++) {
1080 if (consoles
[i
] == s
) {
1088 static void kbd_send_chars(void *opaque
)
1090 TextConsole
*s
= opaque
;
1094 len
= qemu_chr_can_read(s
->chr
);
1095 if (len
> s
->out_fifo
.count
)
1096 len
= s
->out_fifo
.count
;
1098 if (len
> sizeof(buf
))
1100 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1101 qemu_chr_read(s
->chr
, buf
, len
);
1103 /* characters are pending: we send them a bit later (XXX:
1104 horrible, should change char device API) */
1105 if (s
->out_fifo
.count
> 0) {
1106 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1110 /* called when an ascii key is pressed */
1111 void kbd_put_keysym(int keysym
)
1114 uint8_t buf
[16], *q
;
1118 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1122 case QEMU_KEY_CTRL_UP
:
1125 case QEMU_KEY_CTRL_DOWN
:
1128 case QEMU_KEY_CTRL_PAGEUP
:
1129 console_scroll(-10);
1131 case QEMU_KEY_CTRL_PAGEDOWN
:
1135 /* convert the QEMU keysym to VT100 key string */
1137 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1140 c
= keysym
- 0xe100;
1142 *q
++ = '0' + (c
/ 10);
1143 *q
++ = '0' + (c
% 10);
1145 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1148 *q
++ = keysym
& 0xff;
1152 if (s
->chr
->chr_read
) {
1153 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1160 static void text_console_invalidate(void *opaque
)
1162 TextConsole
*s
= (TextConsole
*) opaque
;
1166 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1168 TextConsole
*s
= (TextConsole
*) opaque
;
1171 if (s
->text_x
[0] <= s
->text_x
[1]) {
1172 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1173 chardata
+= s
->text_y
[0] * s
->width
;
1174 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1175 for (j
= 0; j
< s
->width
; j
++, src
++)
1176 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1177 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1178 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1179 (s
->cells
[src
].t_attrib
.bold
<< 21));
1180 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1181 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1182 s
->text_x
[0] = s
->width
;
1183 s
->text_y
[0] = s
->height
;
1187 if (s
->cursor_invalidate
) {
1188 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1189 s
->cursor_invalidate
= 0;
1193 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1197 for (i
= 0; i
< nb_consoles
; i
++) {
1199 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1205 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1210 if (nb_consoles
>= MAX_CONSOLES
)
1212 s
= qemu_mallocz(sizeof(TextConsole
));
1216 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1217 (console_type
== GRAPHIC_CONSOLE
))) {
1221 s
->console_type
= console_type
;
1222 if (console_type
!= GRAPHIC_CONSOLE
) {
1223 consoles
[nb_consoles
++] = s
;
1225 /* HACK: Put graphical consoles before text consoles. */
1226 for (i
= nb_consoles
; i
> 0; i
--) {
1227 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1229 consoles
[i
] = consoles
[i
- 1];
1237 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1238 vga_hw_invalidate_ptr invalidate
,
1239 vga_hw_screen_dump_ptr screen_dump
,
1240 vga_hw_text_update_ptr text_update
,
1246 ds
= (DisplayState
*) qemu_mallocz(sizeof(DisplayState
));
1249 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
1251 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1253 qemu_free_displaysurface(ds
->surface
);
1257 s
->hw_update
= update
;
1258 s
->hw_invalidate
= invalidate
;
1259 s
->hw_screen_dump
= screen_dump
;
1260 s
->hw_text_update
= text_update
;
1263 register_displaystate(ds
);
1267 int is_graphic_console(void)
1269 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1272 int is_fixedsize_console(void)
1274 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1277 void console_color_init(DisplayState
*ds
)
1280 for (j
= 0; j
< 2; j
++) {
1281 for (i
= 0; i
< 8; i
++) {
1282 color_table
[j
][i
] = col_expand(ds
,
1283 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1288 static int n_text_consoles
;
1289 static CharDriverState
*text_consoles
[128];
1290 static char *text_console_strs
[128];
1292 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
, const char *p
)
1297 static int color_inited
;
1299 s
= new_console(ds
, (p
== 0) ? TEXT_CONSOLE
: TEXT_CONSOLE_FIXED_SIZE
);
1305 chr
->chr_write
= console_puts
;
1306 chr
->chr_send_event
= console_send_event
;
1309 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1310 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1311 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1314 if (!color_inited
) {
1316 console_color_init(s
->ds
);
1320 s
->total_height
= DEFAULT_BACKSCROLL
;
1323 width
= ds_get_width(s
->ds
);
1324 height
= ds_get_height(s
->ds
);
1326 width
= strtoul(p
, (char **)&p
, 10);
1329 width
*= FONT_WIDTH
;
1333 height
= strtoul(p
, (char **)&p
, 10);
1336 height
*= FONT_HEIGHT
;
1341 s
->g_height
= height
;
1343 s
->hw_invalidate
= text_console_invalidate
;
1344 s
->hw_text_update
= text_console_update
;
1347 /* Set text attribute defaults */
1348 s
->t_attrib_default
.bold
= 0;
1349 s
->t_attrib_default
.uline
= 0;
1350 s
->t_attrib_default
.blink
= 0;
1351 s
->t_attrib_default
.invers
= 0;
1352 s
->t_attrib_default
.unvisible
= 0;
1353 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1354 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1355 /* set current text attributes to default */
1356 s
->t_attrib
= s
->t_attrib_default
;
1357 text_console_resize(s
);
1359 qemu_chr_reset(chr
);
1364 CharDriverState
*text_console_init(const char *p
)
1366 CharDriverState
*chr
;
1368 chr
= qemu_mallocz(sizeof(CharDriverState
));
1372 if (n_text_consoles
== 128) {
1373 fprintf(stderr
, "Too many text consoles\n");
1376 text_consoles
[n_text_consoles
] = chr
;
1377 text_console_strs
[n_text_consoles
] = p
? qemu_strdup(p
) : NULL
;
1383 void text_consoles_set_display(DisplayState
*ds
)
1387 for (i
= 0; i
< n_text_consoles
; i
++) {
1388 text_console_do_init(text_consoles
[i
], ds
, text_console_strs
[i
]);
1389 qemu_free(text_console_strs
[i
]);
1392 n_text_consoles
= 0;
1395 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1397 TextConsole
*s
= get_graphic_console(ds
);
1399 s
->g_height
= height
;
1400 if (is_graphic_console()) {
1401 ds
->surface
= qemu_resize_displaysurface(ds
->surface
, width
, height
, 32, 4 * width
);
1406 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1407 int dst_x
, int dst_y
, int w
, int h
)
1409 if (is_graphic_console()) {
1410 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1414 static PixelFormat
qemu_default_pixelformat(int bpp
)
1418 memset(&pf
, 0x00, sizeof(PixelFormat
));
1420 pf
.bits_per_pixel
= bpp
;
1421 pf
.bytes_per_pixel
= bpp
/ 8;
1422 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1426 pf
.rmask
= 0x000000E0;
1427 pf
.gmask
= 0x0000001C;
1428 pf
.bmask
= 0x00000003;
1437 pf
.rmask
= 0x0000F800;
1438 pf
.gmask
= 0x000007E0;
1439 pf
.bmask
= 0x0000001F;
1449 pf
.rmask
= 0x00FF0000;
1450 pf
.gmask
= 0x0000FF00;
1451 pf
.bmask
= 0x000000FF;
1465 DisplaySurface
* qemu_create_displaysurface(int width
, int height
, int bpp
, int linesize
)
1467 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1468 if (surface
== NULL
) {
1469 fprintf(stderr
, "qemu_create_displaysurface: malloc failed\n");
1473 surface
->width
= width
;
1474 surface
->height
= height
;
1475 surface
->linesize
= linesize
;
1476 surface
->pf
= qemu_default_pixelformat(bpp
);
1477 #ifdef WORDS_BIGENDIAN
1478 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
1480 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1482 surface
->data
= (uint8_t*) qemu_mallocz(surface
->linesize
* surface
->height
);
1483 if (surface
->data
== NULL
) {
1484 fprintf(stderr
, "qemu_create_displaysurface: malloc failed\n");
1491 DisplaySurface
* qemu_resize_displaysurface(DisplaySurface
*surface
,
1492 int width
, int height
, int bpp
, int linesize
)
1494 surface
->width
= width
;
1495 surface
->height
= height
;
1496 surface
->linesize
= linesize
;
1497 surface
->pf
= qemu_default_pixelformat(bpp
);
1498 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1499 surface
->data
= (uint8_t*) qemu_realloc(surface
->data
, surface
->linesize
* surface
->height
);
1501 surface
->data
= (uint8_t*) qemu_malloc(surface
->linesize
* surface
->height
);
1502 if (surface
->data
== NULL
) {
1503 fprintf(stderr
, "qemu_resize_displaysurface: malloc failed\n");
1506 #ifdef WORDS_BIGENDIAN
1507 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
1509 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1515 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1516 int linesize
, uint8_t *data
)
1518 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1519 if (surface
== NULL
) {
1520 fprintf(stderr
, "qemu_create_displaysurface_from: malloc failed\n");
1524 surface
->width
= width
;
1525 surface
->height
= height
;
1526 surface
->linesize
= linesize
;
1527 surface
->pf
= qemu_default_pixelformat(bpp
);
1528 #ifdef WORDS_BIGENDIAN
1529 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1531 surface
->data
= data
;
1536 void qemu_free_displaysurface(DisplaySurface
*surface
)
1538 if (surface
== NULL
)
1540 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1541 qemu_free(surface
->data
);