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
;
147 int esc_params
[MAX_ESC_PARAMS
];
150 CharDriverState
*chr
;
151 /* fifo for key pressed */
153 uint8_t out_fifo_buf
[16];
154 QEMUTimer
*kbd_timer
;
157 static TextConsole
*active_console
;
158 static TextConsole
*consoles
[MAX_CONSOLES
];
159 static int nb_consoles
= 0;
161 void vga_hw_update(void)
163 if (active_console
&& active_console
->hw_update
)
164 active_console
->hw_update(active_console
->hw
);
167 void vga_hw_invalidate(void)
169 if (active_console
->hw_invalidate
)
170 active_console
->hw_invalidate(active_console
->hw
);
173 void vga_hw_screen_dump(const char *filename
)
175 TextConsole
*previous_active_console
;
177 previous_active_console
= active_console
;
178 active_console
= consoles
[0];
179 /* There is currently no way of specifying which screen we want to dump,
180 so always dump the first one. */
181 if (consoles
[0]->hw_screen_dump
)
182 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
183 active_console
= previous_active_console
;
186 void vga_hw_text_update(console_ch_t
*chardata
)
188 if (active_console
&& active_console
->hw_text_update
)
189 active_console
->hw_text_update(active_console
->hw
, chardata
);
192 /* convert a RGBA color to a color index usable in graphic primitives */
193 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
195 unsigned int r
, g
, b
, color
;
197 switch(ds_get_bits_per_pixel(ds
)) {
200 r
= (rgba
>> 16) & 0xff;
201 g
= (rgba
>> 8) & 0xff;
203 color
= (rgb_to_index
[r
] * 6 * 6) +
204 (rgb_to_index
[g
] * 6) +
209 r
= (rgba
>> 16) & 0xff;
210 g
= (rgba
>> 8) & 0xff;
212 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
215 r
= (rgba
>> 16) & 0xff;
216 g
= (rgba
>> 8) & 0xff;
218 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
228 static void vga_fill_rect (DisplayState
*ds
,
229 int posx
, int posy
, int width
, int height
, uint32_t color
)
234 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
235 d1
= ds_get_data(ds
) +
236 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
237 for (y
= 0; y
< height
; y
++) {
241 for (x
= 0; x
< width
; x
++) {
242 *((uint8_t *)d
) = color
;
247 for (x
= 0; x
< width
; x
++) {
248 *((uint16_t *)d
) = color
;
253 for (x
= 0; x
< width
; x
++) {
254 *((uint32_t *)d
) = color
;
259 d1
+= ds_get_linesize(ds
);
263 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
264 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
270 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
273 s
= ds_get_data(ds
) +
274 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
275 d
= ds_get_data(ds
) +
276 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
277 for (y
= 0; y
< h
; y
++) {
279 d
+= ds_get_linesize(ds
);
280 s
+= ds_get_linesize(ds
);
283 s
= ds_get_data(ds
) +
284 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
285 d
= ds_get_data(ds
) +
286 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
287 for (y
= 0; y
< h
; y
++) {
289 d
-= ds_get_linesize(ds
);
290 s
-= ds_get_linesize(ds
);
295 /***********************************************************/
296 /* basic char display */
298 #define FONT_HEIGHT 16
303 #define cbswap_32(__x) \
305 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
306 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
307 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
308 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
310 #ifdef HOST_WORDS_BIGENDIAN
313 #define PAT(x) cbswap_32(x)
316 static const uint32_t dmask16
[16] = {
335 static const uint32_t dmask4
[4] = {
342 static uint32_t color_table
[2][8];
355 static const uint32_t color_table_rgb
[2][8] = {
357 QEMU_RGB(0x00, 0x00, 0x00), /* black */
358 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
359 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
360 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
361 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
362 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
363 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
364 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
367 QEMU_RGB(0x00, 0x00, 0x00), /* black */
368 QEMU_RGB(0xff, 0x00, 0x00), /* red */
369 QEMU_RGB(0x00, 0xff, 0x00), /* green */
370 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
371 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
372 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
373 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
374 QEMU_RGB(0xff, 0xff, 0xff), /* white */
378 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
380 switch(ds_get_bits_per_pixel(ds
)) {
396 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
398 if (t_attrib
->bold
) {
403 if (t_attrib
->uline
) {
408 if (t_attrib
->blink
) {
413 if (t_attrib
->invers
) {
418 if (t_attrib
->unvisible
) {
424 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
428 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
429 TextAttributes
*t_attrib
)
432 const uint8_t *font_ptr
;
433 unsigned int font_data
, linesize
, xorcol
, bpp
;
435 unsigned int fgcol
, bgcol
;
438 printf("x: %2i y: %2i", x
, y
);
439 console_print_text_attributes(t_attrib
, ch
);
442 if (t_attrib
->invers
) {
443 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
444 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
446 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
447 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
450 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
451 d
= ds_get_data(ds
) +
452 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
453 linesize
= ds_get_linesize(ds
);
454 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
455 xorcol
= bgcol
^ fgcol
;
456 switch(ds_get_bits_per_pixel(ds
)) {
458 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
459 font_data
= *font_ptr
++;
461 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
464 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
465 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
471 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
472 font_data
= *font_ptr
++;
474 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
477 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
478 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
479 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
480 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
485 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
486 font_data
= *font_ptr
++;
487 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
490 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
491 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
492 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
493 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
494 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
495 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
496 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
497 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
504 static void text_console_resize(TextConsole
*s
)
506 TextCell
*cells
, *c
, *c1
;
507 int w1
, x
, y
, last_width
;
509 last_width
= s
->width
;
510 s
->width
= s
->g_width
/ FONT_WIDTH
;
511 s
->height
= s
->g_height
/ FONT_HEIGHT
;
517 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
518 for(y
= 0; y
< s
->total_height
; y
++) {
519 c
= &cells
[y
* s
->width
];
521 c1
= &s
->cells
[y
* last_width
];
522 for(x
= 0; x
< w1
; x
++) {
526 for(x
= w1
; x
< s
->width
; x
++) {
528 c
->t_attrib
= s
->t_attrib_default
;
536 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
538 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
539 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
540 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
541 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
544 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
546 if (s
->update_x0
> x
* FONT_WIDTH
)
547 s
->update_x0
= x
* FONT_WIDTH
;
548 if (s
->update_y0
> y
* FONT_HEIGHT
)
549 s
->update_y0
= y
* FONT_HEIGHT
;
550 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
551 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
552 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
553 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
556 static void update_xy(TextConsole
*s
, int x
, int y
)
561 if (s
== active_console
) {
562 if (!ds_get_bits_per_pixel(s
->ds
)) {
563 text_update_xy(s
, x
, y
);
567 y1
= (s
->y_base
+ y
) % s
->total_height
;
568 y2
= y1
- s
->y_displayed
;
570 y2
+= s
->total_height
;
571 if (y2
< s
->height
) {
572 c
= &s
->cells
[y1
* s
->width
+ x
];
573 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
575 invalidate_xy(s
, x
, y2
);
580 static void console_show_cursor(TextConsole
*s
, int show
)
585 if (s
== active_console
) {
588 if (!ds_get_bits_per_pixel(s
->ds
)) {
589 s
->cursor_invalidate
= 1;
596 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
597 y
= y1
- s
->y_displayed
;
599 y
+= s
->total_height
;
601 c
= &s
->cells
[y1
* s
->width
+ x
];
603 TextAttributes t_attrib
= s
->t_attrib_default
;
604 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
605 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
607 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
609 invalidate_xy(s
, x
, y
);
614 static void console_refresh(TextConsole
*s
)
619 if (s
!= active_console
)
621 if (!ds_get_bits_per_pixel(s
->ds
)) {
624 s
->text_x
[1] = s
->width
- 1;
625 s
->text_y
[1] = s
->height
- 1;
626 s
->cursor_invalidate
= 1;
630 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
631 color_table
[0][COLOR_BLACK
]);
633 for(y
= 0; y
< s
->height
; y
++) {
634 c
= s
->cells
+ y1
* s
->width
;
635 for(x
= 0; x
< s
->width
; x
++) {
636 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
640 if (++y1
== s
->total_height
)
643 console_show_cursor(s
, 1);
644 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
647 static void console_scroll(int ydelta
)
653 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
657 for(i
= 0; i
< ydelta
; i
++) {
658 if (s
->y_displayed
== s
->y_base
)
660 if (++s
->y_displayed
== s
->total_height
)
665 i
= s
->backscroll_height
;
666 if (i
> s
->total_height
- s
->height
)
667 i
= s
->total_height
- s
->height
;
670 y1
+= s
->total_height
;
671 for(i
= 0; i
< ydelta
; i
++) {
672 if (s
->y_displayed
== y1
)
674 if (--s
->y_displayed
< 0)
675 s
->y_displayed
= s
->total_height
- 1;
681 static void console_put_lf(TextConsole
*s
)
687 if (s
->y
>= s
->height
) {
688 s
->y
= s
->height
- 1;
690 if (s
->y_displayed
== s
->y_base
) {
691 if (++s
->y_displayed
== s
->total_height
)
694 if (++s
->y_base
== s
->total_height
)
696 if (s
->backscroll_height
< s
->total_height
)
697 s
->backscroll_height
++;
698 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
699 c
= &s
->cells
[y1
* s
->width
];
700 for(x
= 0; x
< s
->width
; x
++) {
702 c
->t_attrib
= s
->t_attrib_default
;
705 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
706 if (!ds_get_bits_per_pixel(s
->ds
)) {
709 s
->text_x
[1] = s
->width
- 1;
710 s
->text_y
[1] = s
->height
- 1;
714 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
715 s
->width
* FONT_WIDTH
,
716 (s
->height
- 1) * FONT_HEIGHT
);
717 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
718 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
719 color_table
[0][s
->t_attrib_default
.bgcol
]);
722 s
->update_x1
= s
->width
* FONT_WIDTH
;
723 s
->update_y1
= s
->height
* FONT_HEIGHT
;
728 /* Set console attributes depending on the current escape codes.
729 * NOTE: I know this code is not very efficient (checking every color for it
730 * self) but it is more readable and better maintainable.
732 static void console_handle_escape(TextConsole
*s
)
736 for (i
=0; i
<s
->nb_esc_params
; i
++) {
737 switch (s
->esc_params
[i
]) {
738 case 0: /* reset all console attributes to default */
739 s
->t_attrib
= s
->t_attrib_default
;
742 s
->t_attrib
.bold
= 1;
745 s
->t_attrib
.uline
= 1;
748 s
->t_attrib
.blink
= 1;
751 s
->t_attrib
.invers
= 1;
754 s
->t_attrib
.unvisible
= 1;
757 s
->t_attrib
.bold
= 0;
760 s
->t_attrib
.uline
= 0;
763 s
->t_attrib
.blink
= 0;
766 s
->t_attrib
.invers
= 0;
769 s
->t_attrib
.unvisible
= 0;
771 /* set foreground color */
773 s
->t_attrib
.fgcol
=COLOR_BLACK
;
776 s
->t_attrib
.fgcol
=COLOR_RED
;
779 s
->t_attrib
.fgcol
=COLOR_GREEN
;
782 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
785 s
->t_attrib
.fgcol
=COLOR_BLUE
;
788 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
791 s
->t_attrib
.fgcol
=COLOR_CYAN
;
794 s
->t_attrib
.fgcol
=COLOR_WHITE
;
796 /* set background color */
798 s
->t_attrib
.bgcol
=COLOR_BLACK
;
801 s
->t_attrib
.bgcol
=COLOR_RED
;
804 s
->t_attrib
.bgcol
=COLOR_GREEN
;
807 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
810 s
->t_attrib
.bgcol
=COLOR_BLUE
;
813 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
816 s
->t_attrib
.bgcol
=COLOR_CYAN
;
819 s
->t_attrib
.bgcol
=COLOR_WHITE
;
825 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
827 int y1
= (s
->y_base
+ y
) % s
->total_height
;
828 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
830 c
->t_attrib
= s
->t_attrib_default
;
835 static void console_putchar(TextConsole
*s
, int ch
)
844 case '\r': /* carriage return */
847 case '\n': /* newline */
850 case '\b': /* backspace */
854 case '\t': /* tabspace */
855 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
859 s
->x
= s
->x
+ (8 - (s
->x
% 8));
862 case '\a': /* alert aka. bell */
863 /* TODO: has to be implemented */
866 /* SI (shift in), character set 0 (ignored) */
869 /* SO (shift out), character set 1 (ignored) */
871 case 27: /* esc (introducing an escape sequence) */
872 s
->state
= TTY_STATE_ESC
;
875 if (s
->x
>= s
->width
) {
880 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
881 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
883 c
->t_attrib
= s
->t_attrib
;
884 update_xy(s
, s
->x
, s
->y
);
889 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
891 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
892 s
->esc_params
[i
] = 0;
893 s
->nb_esc_params
= 0;
894 s
->state
= TTY_STATE_CSI
;
896 s
->state
= TTY_STATE_NORM
;
899 case TTY_STATE_CSI
: /* handle escape sequence parameters */
900 if (ch
>= '0' && ch
<= '9') {
901 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
902 s
->esc_params
[s
->nb_esc_params
] =
903 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
910 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
911 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
913 s
->state
= TTY_STATE_NORM
;
917 if (s
->esc_params
[0] == 0) {
918 s
->esc_params
[0] = 1;
920 s
->y
-= s
->esc_params
[0];
926 /* move cursor down */
927 if (s
->esc_params
[0] == 0) {
928 s
->esc_params
[0] = 1;
930 s
->y
+= s
->esc_params
[0];
931 if (s
->y
>= s
->height
) {
932 s
->y
= s
->height
- 1;
936 /* move cursor right */
937 if (s
->esc_params
[0] == 0) {
938 s
->esc_params
[0] = 1;
940 s
->x
+= s
->esc_params
[0];
941 if (s
->x
>= s
->width
) {
946 /* move cursor left */
947 if (s
->esc_params
[0] == 0) {
948 s
->esc_params
[0] = 1;
950 s
->x
-= s
->esc_params
[0];
956 /* move cursor to column */
957 s
->x
= s
->esc_params
[0] - 1;
964 /* move cursor to row, column */
965 s
->x
= s
->esc_params
[1] - 1;
969 s
->y
= s
->esc_params
[0] - 1;
975 switch (s
->esc_params
[0]) {
977 /* clear to end of screen */
978 for (y
= s
->y
; y
< s
->height
; y
++) {
979 for (x
= 0; x
< s
->width
; x
++) {
980 if (y
== s
->y
&& x
< s
->x
) {
983 console_clear_xy(s
, x
, y
);
988 /* clear from beginning of screen */
989 for (y
= 0; y
<= s
->y
; y
++) {
990 for (x
= 0; x
< s
->width
; x
++) {
991 if (y
== s
->y
&& x
> s
->x
) {
994 console_clear_xy(s
, x
, y
);
999 /* clear entire screen */
1000 for (y
= 0; y
<= s
->height
; y
++) {
1001 for (x
= 0; x
< s
->width
; x
++) {
1002 console_clear_xy(s
, x
, y
);
1008 switch (s
->esc_params
[0]) {
1011 for(x
= s
->x
; x
< s
->width
; x
++) {
1012 console_clear_xy(s
, x
, s
->y
);
1016 /* clear from beginning of line */
1017 for (x
= 0; x
<= s
->x
; x
++) {
1018 console_clear_xy(s
, x
, s
->y
);
1022 /* clear entire line */
1023 for(x
= 0; x
< s
->width
; x
++) {
1024 console_clear_xy(s
, x
, s
->y
);
1030 console_handle_escape(s
);
1033 /* report cursor position */
1034 /* TODO: send ESC[row;colR */
1037 /* save cursor position */
1042 /* restore cursor position */
1047 #ifdef DEBUG_CONSOLE
1048 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1057 void console_select(unsigned int index
)
1061 if (index
>= MAX_CONSOLES
)
1063 active_console
->g_width
= ds_get_width(active_console
->ds
);
1064 active_console
->g_height
= ds_get_height(active_console
->ds
);
1065 s
= consoles
[index
];
1067 DisplayState
*ds
= s
->ds
;
1069 if (ds_get_bits_per_pixel(s
->ds
)) {
1070 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1072 s
->ds
->surface
->width
= s
->width
;
1073 s
->ds
->surface
->height
= s
->height
;
1076 vga_hw_invalidate();
1080 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1082 TextConsole
*s
= chr
->opaque
;
1085 s
->update_x0
= s
->width
* FONT_WIDTH
;
1086 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1089 console_show_cursor(s
, 0);
1090 for(i
= 0; i
< len
; i
++) {
1091 console_putchar(s
, buf
[i
]);
1093 console_show_cursor(s
, 1);
1094 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1095 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1096 s
->update_x1
- s
->update_x0
,
1097 s
->update_y1
- s
->update_y0
);
1102 static void console_send_event(CharDriverState
*chr
, int event
)
1104 TextConsole
*s
= chr
->opaque
;
1107 if (event
== CHR_EVENT_FOCUS
) {
1108 for(i
= 0; i
< nb_consoles
; i
++) {
1109 if (consoles
[i
] == s
) {
1117 static void kbd_send_chars(void *opaque
)
1119 TextConsole
*s
= opaque
;
1123 len
= qemu_chr_can_read(s
->chr
);
1124 if (len
> s
->out_fifo
.count
)
1125 len
= s
->out_fifo
.count
;
1127 if (len
> sizeof(buf
))
1129 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1130 qemu_chr_read(s
->chr
, buf
, len
);
1132 /* characters are pending: we send them a bit later (XXX:
1133 horrible, should change char device API) */
1134 if (s
->out_fifo
.count
> 0) {
1135 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1139 /* called when an ascii key is pressed */
1140 void kbd_put_keysym(int keysym
)
1143 uint8_t buf
[16], *q
;
1147 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1151 case QEMU_KEY_CTRL_UP
:
1154 case QEMU_KEY_CTRL_DOWN
:
1157 case QEMU_KEY_CTRL_PAGEUP
:
1158 console_scroll(-10);
1160 case QEMU_KEY_CTRL_PAGEDOWN
:
1164 /* convert the QEMU keysym to VT100 key string */
1166 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1169 c
= keysym
- 0xe100;
1171 *q
++ = '0' + (c
/ 10);
1172 *q
++ = '0' + (c
% 10);
1174 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1177 *q
++ = keysym
& 0xff;
1181 if (s
->chr
->chr_read
) {
1182 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1189 static void text_console_invalidate(void *opaque
)
1191 TextConsole
*s
= (TextConsole
*) opaque
;
1192 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1193 s
->g_width
= ds_get_width(s
->ds
);
1194 s
->g_height
= ds_get_height(s
->ds
);
1195 text_console_resize(s
);
1200 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1202 TextConsole
*s
= (TextConsole
*) opaque
;
1205 if (s
->text_x
[0] <= s
->text_x
[1]) {
1206 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1207 chardata
+= s
->text_y
[0] * s
->width
;
1208 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1209 for (j
= 0; j
< s
->width
; j
++, src
++)
1210 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1211 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1212 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1213 (s
->cells
[src
].t_attrib
.bold
<< 21));
1214 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1215 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1216 s
->text_x
[0] = s
->width
;
1217 s
->text_y
[0] = s
->height
;
1221 if (s
->cursor_invalidate
) {
1222 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1223 s
->cursor_invalidate
= 0;
1227 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1231 for (i
= 0; i
< nb_consoles
; i
++) {
1233 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1239 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1244 if (nb_consoles
>= MAX_CONSOLES
)
1246 s
= qemu_mallocz(sizeof(TextConsole
));
1247 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1248 (console_type
== GRAPHIC_CONSOLE
))) {
1252 s
->console_type
= console_type
;
1253 if (console_type
!= GRAPHIC_CONSOLE
) {
1254 consoles
[nb_consoles
++] = s
;
1256 /* HACK: Put graphical consoles before text consoles. */
1257 for (i
= nb_consoles
; i
> 0; i
--) {
1258 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1260 consoles
[i
] = consoles
[i
- 1];
1268 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1269 vga_hw_invalidate_ptr invalidate
,
1270 vga_hw_screen_dump_ptr screen_dump
,
1271 vga_hw_text_update_ptr text_update
,
1277 ds
= (DisplayState
*) qemu_mallocz(sizeof(DisplayState
));
1278 ds
->allocator
= &default_allocator
;
1279 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1281 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1283 qemu_free_displaysurface(ds
);
1287 s
->hw_update
= update
;
1288 s
->hw_invalidate
= invalidate
;
1289 s
->hw_screen_dump
= screen_dump
;
1290 s
->hw_text_update
= text_update
;
1293 register_displaystate(ds
);
1297 int is_graphic_console(void)
1299 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1302 int is_fixedsize_console(void)
1304 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1307 void console_color_init(DisplayState
*ds
)
1310 for (j
= 0; j
< 2; j
++) {
1311 for (i
= 0; i
< 8; i
++) {
1312 color_table
[j
][i
] = col_expand(ds
,
1313 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1318 static int n_text_consoles
;
1319 static CharDriverState
*text_consoles
[128];
1320 static QemuOpts
*text_console_opts
[128];
1322 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
, QemuOpts
*opts
)
1327 static int color_inited
;
1329 width
= qemu_opt_get_number(opts
, "width", 0);
1331 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1333 height
= qemu_opt_get_number(opts
, "height", 0);
1335 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1337 if (width
== 0 || height
== 0) {
1338 s
= new_console(ds
, TEXT_CONSOLE
);
1339 width
= ds_get_width(s
->ds
);
1340 height
= ds_get_height(s
->ds
);
1342 s
= new_console(ds
, TEXT_CONSOLE_FIXED_SIZE
);
1350 chr
->chr_write
= console_puts
;
1351 chr
->chr_send_event
= console_send_event
;
1354 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1355 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1356 s
->kbd_timer
= qemu_new_timer(rt_clock
, kbd_send_chars
, s
);
1359 if (!color_inited
) {
1361 console_color_init(s
->ds
);
1365 s
->total_height
= DEFAULT_BACKSCROLL
;
1369 s
->g_height
= height
;
1371 s
->hw_invalidate
= text_console_invalidate
;
1372 s
->hw_text_update
= text_console_update
;
1375 /* Set text attribute defaults */
1376 s
->t_attrib_default
.bold
= 0;
1377 s
->t_attrib_default
.uline
= 0;
1378 s
->t_attrib_default
.blink
= 0;
1379 s
->t_attrib_default
.invers
= 0;
1380 s
->t_attrib_default
.unvisible
= 0;
1381 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1382 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1383 /* set current text attributes to default */
1384 s
->t_attrib
= s
->t_attrib_default
;
1385 text_console_resize(s
);
1387 qemu_chr_reset(chr
);
1392 CharDriverState
*text_console_init(QemuOpts
*opts
)
1394 CharDriverState
*chr
;
1396 chr
= qemu_mallocz(sizeof(CharDriverState
));
1398 if (n_text_consoles
== 128) {
1399 fprintf(stderr
, "Too many text consoles\n");
1402 text_consoles
[n_text_consoles
] = chr
;
1403 text_console_opts
[n_text_consoles
] = opts
;
1409 void text_consoles_set_display(DisplayState
*ds
)
1413 for (i
= 0; i
< n_text_consoles
; i
++) {
1414 text_console_do_init(text_consoles
[i
], ds
, text_console_opts
[i
]);
1415 qemu_opts_del(text_console_opts
[i
]);
1416 text_console_opts
[i
] = NULL
;
1419 n_text_consoles
= 0;
1422 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1424 TextConsole
*s
= get_graphic_console(ds
);
1428 s
->g_height
= height
;
1429 if (is_graphic_console()) {
1430 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1435 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1436 int dst_x
, int dst_y
, int w
, int h
)
1438 if (is_graphic_console()) {
1439 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1443 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1447 memset(&pf
, 0x00, sizeof(PixelFormat
));
1449 pf
.bits_per_pixel
= bpp
;
1450 pf
.bytes_per_pixel
= bpp
/ 8;
1451 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1455 pf
.rmask
= 0x000000FF;
1456 pf
.gmask
= 0x0000FF00;
1457 pf
.bmask
= 0x00FF0000;
1469 pf
.rmask
= 0x0000FF00;
1470 pf
.gmask
= 0x00FF0000;
1471 pf
.bmask
= 0xFF000000;
1472 pf
.amask
= 0x00000000;
1492 PixelFormat
qemu_default_pixelformat(int bpp
)
1496 memset(&pf
, 0x00, sizeof(PixelFormat
));
1498 pf
.bits_per_pixel
= bpp
;
1499 pf
.bytes_per_pixel
= bpp
/ 8;
1500 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1504 pf
.rmask
= 0x0000F800;
1505 pf
.gmask
= 0x000007E0;
1506 pf
.bmask
= 0x0000001F;
1518 pf
.rmask
= 0x00FF0000;
1519 pf
.gmask
= 0x0000FF00;
1520 pf
.bmask
= 0x000000FF;
1531 pf
.rmask
= 0x00FF0000;
1532 pf
.gmask
= 0x0000FF00;
1533 pf
.bmask
= 0x000000FF;
1553 DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1555 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1557 surface
->width
= width
;
1558 surface
->height
= height
;
1559 surface
->linesize
= width
* 4;
1560 surface
->pf
= qemu_default_pixelformat(32);
1561 #ifdef HOST_WORDS_BIGENDIAN
1562 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
1564 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1566 surface
->data
= (uint8_t*) qemu_mallocz(surface
->linesize
* surface
->height
);
1571 DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1572 int width
, int height
)
1574 surface
->width
= width
;
1575 surface
->height
= height
;
1576 surface
->linesize
= width
* 4;
1577 surface
->pf
= qemu_default_pixelformat(32);
1578 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1579 surface
->data
= (uint8_t*) qemu_realloc(surface
->data
, surface
->linesize
* surface
->height
);
1581 surface
->data
= (uint8_t*) qemu_malloc(surface
->linesize
* surface
->height
);
1582 #ifdef HOST_WORDS_BIGENDIAN
1583 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
1585 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1591 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1592 int linesize
, uint8_t *data
)
1594 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1596 surface
->width
= width
;
1597 surface
->height
= height
;
1598 surface
->linesize
= linesize
;
1599 surface
->pf
= qemu_default_pixelformat(bpp
);
1600 #ifdef HOST_WORDS_BIGENDIAN
1601 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1603 surface
->data
= data
;
1608 void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1610 if (surface
== NULL
)
1612 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1613 qemu_free(surface
->data
);