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
;
148 int esc_params
[MAX_ESC_PARAMS
];
151 CharDriverState
*chr
;
152 /* fifo for key pressed */
154 uint8_t out_fifo_buf
[16];
155 QEMUTimer
*kbd_timer
;
158 static DisplayState
*display_state
;
159 static TextConsole
*active_console
;
160 static TextConsole
*consoles
[MAX_CONSOLES
];
161 static int nb_consoles
= 0;
163 void vga_hw_update(void)
165 if (active_console
&& active_console
->hw_update
)
166 active_console
->hw_update(active_console
->hw
);
169 void vga_hw_invalidate(void)
171 if (active_console
&& active_console
->hw_invalidate
)
172 active_console
->hw_invalidate(active_console
->hw
);
175 void vga_hw_screen_dump(const char *filename
)
177 TextConsole
*previous_active_console
;
179 previous_active_console
= active_console
;
180 active_console
= consoles
[0];
181 /* There is currently no way of specifying which screen we want to dump,
182 so always dump the first one. */
183 if (consoles
[0] && consoles
[0]->hw_screen_dump
)
184 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
185 active_console
= previous_active_console
;
188 void vga_hw_text_update(console_ch_t
*chardata
)
190 if (active_console
&& active_console
->hw_text_update
)
191 active_console
->hw_text_update(active_console
->hw
, chardata
);
194 /* convert a RGBA color to a color index usable in graphic primitives */
195 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
197 unsigned int r
, g
, b
, color
;
199 switch(ds_get_bits_per_pixel(ds
)) {
202 r
= (rgba
>> 16) & 0xff;
203 g
= (rgba
>> 8) & 0xff;
205 color
= (rgb_to_index
[r
] * 6 * 6) +
206 (rgb_to_index
[g
] * 6) +
211 r
= (rgba
>> 16) & 0xff;
212 g
= (rgba
>> 8) & 0xff;
214 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
217 r
= (rgba
>> 16) & 0xff;
218 g
= (rgba
>> 8) & 0xff;
220 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
230 static void vga_fill_rect (DisplayState
*ds
,
231 int posx
, int posy
, int width
, int height
, uint32_t color
)
236 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
237 d1
= ds_get_data(ds
) +
238 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
239 for (y
= 0; y
< height
; y
++) {
243 for (x
= 0; x
< width
; x
++) {
244 *((uint8_t *)d
) = color
;
249 for (x
= 0; x
< width
; x
++) {
250 *((uint16_t *)d
) = color
;
255 for (x
= 0; x
< width
; x
++) {
256 *((uint32_t *)d
) = color
;
261 d1
+= ds_get_linesize(ds
);
265 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
266 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
272 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
275 s
= ds_get_data(ds
) +
276 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
277 d
= ds_get_data(ds
) +
278 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
279 for (y
= 0; y
< h
; y
++) {
281 d
+= ds_get_linesize(ds
);
282 s
+= ds_get_linesize(ds
);
285 s
= ds_get_data(ds
) +
286 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
287 d
= ds_get_data(ds
) +
288 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
289 for (y
= 0; y
< h
; y
++) {
291 d
-= ds_get_linesize(ds
);
292 s
-= ds_get_linesize(ds
);
297 /***********************************************************/
298 /* basic char display */
300 #define FONT_HEIGHT 16
305 #define cbswap_32(__x) \
307 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
308 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
309 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
310 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
312 #ifdef HOST_WORDS_BIGENDIAN
315 #define PAT(x) cbswap_32(x)
318 static const uint32_t dmask16
[16] = {
337 static const uint32_t dmask4
[4] = {
344 static uint32_t color_table
[2][8];
346 #ifndef CONFIG_CURSES
359 static const uint32_t color_table_rgb
[2][8] = {
361 QEMU_RGB(0x00, 0x00, 0x00), /* black */
362 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
363 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
364 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
365 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
366 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
367 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
368 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
371 QEMU_RGB(0x00, 0x00, 0x00), /* black */
372 QEMU_RGB(0xff, 0x00, 0x00), /* red */
373 QEMU_RGB(0x00, 0xff, 0x00), /* green */
374 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
375 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
376 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
377 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
378 QEMU_RGB(0xff, 0xff, 0xff), /* white */
382 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
384 switch(ds_get_bits_per_pixel(ds
)) {
400 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
402 if (t_attrib
->bold
) {
407 if (t_attrib
->uline
) {
412 if (t_attrib
->blink
) {
417 if (t_attrib
->invers
) {
422 if (t_attrib
->unvisible
) {
428 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
432 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
433 TextAttributes
*t_attrib
)
436 const uint8_t *font_ptr
;
437 unsigned int font_data
, linesize
, xorcol
, bpp
;
439 unsigned int fgcol
, bgcol
;
442 printf("x: %2i y: %2i", x
, y
);
443 console_print_text_attributes(t_attrib
, ch
);
446 if (t_attrib
->invers
) {
447 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
448 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
450 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
451 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
454 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
455 d
= ds_get_data(ds
) +
456 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
457 linesize
= ds_get_linesize(ds
);
458 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
459 xorcol
= bgcol
^ fgcol
;
460 switch(ds_get_bits_per_pixel(ds
)) {
462 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
463 font_data
= *font_ptr
++;
465 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
468 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
469 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
475 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
476 font_data
= *font_ptr
++;
478 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
481 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
482 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
483 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
484 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
489 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
490 font_data
= *font_ptr
++;
491 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
494 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
495 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
496 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
497 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
498 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
499 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
500 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
501 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
508 static void text_console_resize(TextConsole
*s
)
510 TextCell
*cells
, *c
, *c1
;
511 int w1
, x
, y
, last_width
;
513 last_width
= s
->width
;
514 s
->width
= s
->g_width
/ FONT_WIDTH
;
515 s
->height
= s
->g_height
/ FONT_HEIGHT
;
521 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
522 for(y
= 0; y
< s
->total_height
; y
++) {
523 c
= &cells
[y
* s
->width
];
525 c1
= &s
->cells
[y
* last_width
];
526 for(x
= 0; x
< w1
; x
++) {
530 for(x
= w1
; x
< s
->width
; x
++) {
532 c
->t_attrib
= s
->t_attrib_default
;
540 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
542 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
543 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
544 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
545 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
548 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
550 if (s
->update_x0
> x
* FONT_WIDTH
)
551 s
->update_x0
= x
* FONT_WIDTH
;
552 if (s
->update_y0
> y
* FONT_HEIGHT
)
553 s
->update_y0
= y
* FONT_HEIGHT
;
554 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
555 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
556 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
557 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
560 static void update_xy(TextConsole
*s
, int x
, int y
)
565 if (s
== active_console
) {
566 if (!ds_get_bits_per_pixel(s
->ds
)) {
567 text_update_xy(s
, x
, y
);
571 y1
= (s
->y_base
+ y
) % s
->total_height
;
572 y2
= y1
- s
->y_displayed
;
574 y2
+= s
->total_height
;
575 if (y2
< s
->height
) {
576 c
= &s
->cells
[y1
* s
->width
+ x
];
577 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
579 invalidate_xy(s
, x
, y2
);
584 static void console_show_cursor(TextConsole
*s
, int show
)
589 if (s
== active_console
) {
592 if (!ds_get_bits_per_pixel(s
->ds
)) {
593 s
->cursor_invalidate
= 1;
600 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
601 y
= y1
- s
->y_displayed
;
603 y
+= s
->total_height
;
605 c
= &s
->cells
[y1
* s
->width
+ x
];
607 TextAttributes t_attrib
= s
->t_attrib_default
;
608 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
609 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
611 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
613 invalidate_xy(s
, x
, y
);
618 static void console_refresh(TextConsole
*s
)
623 if (s
!= active_console
)
625 if (!ds_get_bits_per_pixel(s
->ds
)) {
628 s
->text_x
[1] = s
->width
- 1;
629 s
->text_y
[1] = s
->height
- 1;
630 s
->cursor_invalidate
= 1;
634 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
635 color_table
[0][COLOR_BLACK
]);
637 for(y
= 0; y
< s
->height
; y
++) {
638 c
= s
->cells
+ y1
* s
->width
;
639 for(x
= 0; x
< s
->width
; x
++) {
640 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
644 if (++y1
== s
->total_height
)
647 console_show_cursor(s
, 1);
648 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
651 static void console_scroll(int ydelta
)
657 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
661 for(i
= 0; i
< ydelta
; i
++) {
662 if (s
->y_displayed
== s
->y_base
)
664 if (++s
->y_displayed
== s
->total_height
)
669 i
= s
->backscroll_height
;
670 if (i
> s
->total_height
- s
->height
)
671 i
= s
->total_height
- s
->height
;
674 y1
+= s
->total_height
;
675 for(i
= 0; i
< ydelta
; i
++) {
676 if (s
->y_displayed
== y1
)
678 if (--s
->y_displayed
< 0)
679 s
->y_displayed
= s
->total_height
- 1;
685 static void console_put_lf(TextConsole
*s
)
691 if (s
->y
>= s
->height
) {
692 s
->y
= s
->height
- 1;
694 if (s
->y_displayed
== s
->y_base
) {
695 if (++s
->y_displayed
== s
->total_height
)
698 if (++s
->y_base
== s
->total_height
)
700 if (s
->backscroll_height
< s
->total_height
)
701 s
->backscroll_height
++;
702 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
703 c
= &s
->cells
[y1
* s
->width
];
704 for(x
= 0; x
< s
->width
; x
++) {
706 c
->t_attrib
= s
->t_attrib_default
;
709 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
710 if (!ds_get_bits_per_pixel(s
->ds
)) {
713 s
->text_x
[1] = s
->width
- 1;
714 s
->text_y
[1] = s
->height
- 1;
718 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
719 s
->width
* FONT_WIDTH
,
720 (s
->height
- 1) * FONT_HEIGHT
);
721 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
722 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
723 color_table
[0][s
->t_attrib_default
.bgcol
]);
726 s
->update_x1
= s
->width
* FONT_WIDTH
;
727 s
->update_y1
= s
->height
* FONT_HEIGHT
;
732 /* Set console attributes depending on the current escape codes.
733 * NOTE: I know this code is not very efficient (checking every color for it
734 * self) but it is more readable and better maintainable.
736 static void console_handle_escape(TextConsole
*s
)
740 for (i
=0; i
<s
->nb_esc_params
; i
++) {
741 switch (s
->esc_params
[i
]) {
742 case 0: /* reset all console attributes to default */
743 s
->t_attrib
= s
->t_attrib_default
;
746 s
->t_attrib
.bold
= 1;
749 s
->t_attrib
.uline
= 1;
752 s
->t_attrib
.blink
= 1;
755 s
->t_attrib
.invers
= 1;
758 s
->t_attrib
.unvisible
= 1;
761 s
->t_attrib
.bold
= 0;
764 s
->t_attrib
.uline
= 0;
767 s
->t_attrib
.blink
= 0;
770 s
->t_attrib
.invers
= 0;
773 s
->t_attrib
.unvisible
= 0;
775 /* set foreground color */
777 s
->t_attrib
.fgcol
=COLOR_BLACK
;
780 s
->t_attrib
.fgcol
=COLOR_RED
;
783 s
->t_attrib
.fgcol
=COLOR_GREEN
;
786 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
789 s
->t_attrib
.fgcol
=COLOR_BLUE
;
792 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
795 s
->t_attrib
.fgcol
=COLOR_CYAN
;
798 s
->t_attrib
.fgcol
=COLOR_WHITE
;
800 /* set background color */
802 s
->t_attrib
.bgcol
=COLOR_BLACK
;
805 s
->t_attrib
.bgcol
=COLOR_RED
;
808 s
->t_attrib
.bgcol
=COLOR_GREEN
;
811 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
814 s
->t_attrib
.bgcol
=COLOR_BLUE
;
817 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
820 s
->t_attrib
.bgcol
=COLOR_CYAN
;
823 s
->t_attrib
.bgcol
=COLOR_WHITE
;
829 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
831 int y1
= (s
->y_base
+ y
) % s
->total_height
;
832 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
834 c
->t_attrib
= s
->t_attrib_default
;
838 static void console_putchar(TextConsole
*s
, int ch
)
847 case '\r': /* carriage return */
850 case '\n': /* newline */
853 case '\b': /* backspace */
857 case '\t': /* tabspace */
858 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
862 s
->x
= s
->x
+ (8 - (s
->x
% 8));
865 case '\a': /* alert aka. bell */
866 /* TODO: has to be implemented */
869 /* SI (shift in), character set 0 (ignored) */
872 /* SO (shift out), character set 1 (ignored) */
874 case 27: /* esc (introducing an escape sequence) */
875 s
->state
= TTY_STATE_ESC
;
878 if (s
->x
>= s
->width
) {
883 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
884 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
886 c
->t_attrib
= s
->t_attrib
;
887 update_xy(s
, s
->x
, s
->y
);
892 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
894 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
895 s
->esc_params
[i
] = 0;
896 s
->nb_esc_params
= 0;
897 s
->state
= TTY_STATE_CSI
;
899 s
->state
= TTY_STATE_NORM
;
902 case TTY_STATE_CSI
: /* handle escape sequence parameters */
903 if (ch
>= '0' && ch
<= '9') {
904 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
905 s
->esc_params
[s
->nb_esc_params
] =
906 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
913 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
914 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
916 s
->state
= TTY_STATE_NORM
;
920 if (s
->esc_params
[0] == 0) {
921 s
->esc_params
[0] = 1;
923 s
->y
-= s
->esc_params
[0];
929 /* move cursor down */
930 if (s
->esc_params
[0] == 0) {
931 s
->esc_params
[0] = 1;
933 s
->y
+= s
->esc_params
[0];
934 if (s
->y
>= s
->height
) {
935 s
->y
= s
->height
- 1;
939 /* move cursor right */
940 if (s
->esc_params
[0] == 0) {
941 s
->esc_params
[0] = 1;
943 s
->x
+= s
->esc_params
[0];
944 if (s
->x
>= s
->width
) {
949 /* move cursor left */
950 if (s
->esc_params
[0] == 0) {
951 s
->esc_params
[0] = 1;
953 s
->x
-= s
->esc_params
[0];
959 /* move cursor to column */
960 s
->x
= s
->esc_params
[0] - 1;
967 /* move cursor to row, column */
968 s
->x
= s
->esc_params
[1] - 1;
972 s
->y
= s
->esc_params
[0] - 1;
978 switch (s
->esc_params
[0]) {
980 /* clear to end of screen */
981 for (y
= s
->y
; y
< s
->height
; y
++) {
982 for (x
= 0; x
< s
->width
; x
++) {
983 if (y
== s
->y
&& x
< s
->x
) {
986 console_clear_xy(s
, x
, y
);
991 /* clear from beginning of screen */
992 for (y
= 0; y
<= s
->y
; y
++) {
993 for (x
= 0; x
< s
->width
; x
++) {
994 if (y
== s
->y
&& x
> s
->x
) {
997 console_clear_xy(s
, x
, y
);
1002 /* clear entire screen */
1003 for (y
= 0; y
<= s
->height
; y
++) {
1004 for (x
= 0; x
< s
->width
; x
++) {
1005 console_clear_xy(s
, x
, y
);
1011 switch (s
->esc_params
[0]) {
1014 for(x
= s
->x
; x
< s
->width
; x
++) {
1015 console_clear_xy(s
, x
, s
->y
);
1019 /* clear from beginning of line */
1020 for (x
= 0; x
<= s
->x
; x
++) {
1021 console_clear_xy(s
, x
, s
->y
);
1025 /* clear entire line */
1026 for(x
= 0; x
< s
->width
; x
++) {
1027 console_clear_xy(s
, x
, s
->y
);
1033 console_handle_escape(s
);
1036 /* report cursor position */
1037 /* TODO: send ESC[row;colR */
1040 /* save cursor position */
1045 /* restore cursor position */
1050 #ifdef DEBUG_CONSOLE
1051 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1060 void console_select(unsigned int index
)
1064 if (index
>= MAX_CONSOLES
)
1066 if (active_console
) {
1067 active_console
->g_width
= ds_get_width(active_console
->ds
);
1068 active_console
->g_height
= ds_get_height(active_console
->ds
);
1070 s
= consoles
[index
];
1072 DisplayState
*ds
= s
->ds
;
1074 if (ds_get_bits_per_pixel(s
->ds
)) {
1075 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1077 s
->ds
->surface
->width
= s
->width
;
1078 s
->ds
->surface
->height
= s
->height
;
1081 vga_hw_invalidate();
1085 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1087 TextConsole
*s
= chr
->opaque
;
1090 s
->update_x0
= s
->width
* FONT_WIDTH
;
1091 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1094 console_show_cursor(s
, 0);
1095 for(i
= 0; i
< len
; i
++) {
1096 console_putchar(s
, buf
[i
]);
1098 console_show_cursor(s
, 1);
1099 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1100 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1101 s
->update_x1
- s
->update_x0
,
1102 s
->update_y1
- s
->update_y0
);
1107 static void kbd_send_chars(void *opaque
)
1109 TextConsole
*s
= opaque
;
1113 len
= qemu_chr_be_can_write(s
->chr
);
1114 if (len
> s
->out_fifo
.count
)
1115 len
= s
->out_fifo
.count
;
1117 if (len
> sizeof(buf
))
1119 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1120 qemu_chr_be_write(s
->chr
, buf
, len
);
1122 /* characters are pending: we send them a bit later (XXX:
1123 horrible, should change char device API) */
1124 if (s
->out_fifo
.count
> 0) {
1125 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
1129 /* called when an ascii key is pressed */
1130 void kbd_put_keysym(int keysym
)
1133 uint8_t buf
[16], *q
;
1137 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1141 case QEMU_KEY_CTRL_UP
:
1144 case QEMU_KEY_CTRL_DOWN
:
1147 case QEMU_KEY_CTRL_PAGEUP
:
1148 console_scroll(-10);
1150 case QEMU_KEY_CTRL_PAGEDOWN
:
1154 /* convert the QEMU keysym to VT100 key string */
1156 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1159 c
= keysym
- 0xe100;
1161 *q
++ = '0' + (c
/ 10);
1162 *q
++ = '0' + (c
% 10);
1164 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1167 *q
++ = keysym
& 0xff;
1168 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1169 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1175 console_puts(s
->chr
, buf
, q
- buf
);
1177 if (s
->chr
->chr_read
) {
1178 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1185 static void text_console_invalidate(void *opaque
)
1187 TextConsole
*s
= (TextConsole
*) opaque
;
1188 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1189 s
->g_width
= ds_get_width(s
->ds
);
1190 s
->g_height
= ds_get_height(s
->ds
);
1191 text_console_resize(s
);
1196 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1198 TextConsole
*s
= (TextConsole
*) opaque
;
1201 if (s
->text_x
[0] <= s
->text_x
[1]) {
1202 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1203 chardata
+= s
->text_y
[0] * s
->width
;
1204 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1205 for (j
= 0; j
< s
->width
; j
++, src
++)
1206 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1207 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1208 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1209 (s
->cells
[src
].t_attrib
.bold
<< 21));
1210 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1211 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1212 s
->text_x
[0] = s
->width
;
1213 s
->text_y
[0] = s
->height
;
1217 if (s
->cursor_invalidate
) {
1218 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1219 s
->cursor_invalidate
= 0;
1223 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1227 for (i
= 0; i
< nb_consoles
; i
++) {
1229 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1235 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1240 if (nb_consoles
>= MAX_CONSOLES
)
1242 s
= g_malloc0(sizeof(TextConsole
));
1243 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1244 (console_type
== GRAPHIC_CONSOLE
))) {
1248 s
->console_type
= console_type
;
1249 if (console_type
!= GRAPHIC_CONSOLE
) {
1250 consoles
[nb_consoles
++] = s
;
1252 /* HACK: Put graphical consoles before text consoles. */
1253 for (i
= nb_consoles
; i
> 0; i
--) {
1254 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1256 consoles
[i
] = consoles
[i
- 1];
1264 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1266 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1268 int linesize
= width
* 4;
1269 qemu_alloc_display(surface
, width
, height
, linesize
,
1270 qemu_default_pixelformat(32), 0);
1274 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1275 int width
, int height
)
1277 int linesize
= width
* 4;
1278 qemu_alloc_display(surface
, width
, height
, linesize
,
1279 qemu_default_pixelformat(32), 0);
1283 void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1284 int linesize
, PixelFormat pf
, int newflags
)
1287 surface
->width
= width
;
1288 surface
->height
= height
;
1289 surface
->linesize
= linesize
;
1291 if (surface
->flags
& QEMU_ALLOCATED_FLAG
) {
1292 data
= g_realloc(surface
->data
,
1293 surface
->linesize
* surface
->height
);
1295 data
= g_malloc(surface
->linesize
* surface
->height
);
1297 surface
->data
= (uint8_t *)data
;
1298 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1299 #ifdef HOST_WORDS_BIGENDIAN
1300 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1304 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1305 int linesize
, uint8_t *data
)
1307 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1309 surface
->width
= width
;
1310 surface
->height
= height
;
1311 surface
->linesize
= linesize
;
1312 surface
->pf
= qemu_default_pixelformat(bpp
);
1313 #ifdef HOST_WORDS_BIGENDIAN
1314 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1316 surface
->data
= data
;
1321 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1323 if (surface
== NULL
)
1325 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1326 g_free(surface
->data
);
1330 static struct DisplayAllocator default_allocator
= {
1331 defaultallocator_create_displaysurface
,
1332 defaultallocator_resize_displaysurface
,
1333 defaultallocator_free_displaysurface
1336 static void dumb_display_init(void)
1338 DisplayState
*ds
= g_malloc0(sizeof(DisplayState
));
1342 ds
->allocator
= &default_allocator
;
1343 if (is_fixedsize_console()) {
1344 width
= active_console
->g_width
;
1345 height
= active_console
->g_height
;
1347 ds
->surface
= qemu_create_displaysurface(ds
, width
, height
);
1348 register_displaystate(ds
);
1351 /***********************************************************/
1352 /* register display */
1354 void register_displaystate(DisplayState
*ds
)
1364 DisplayState
*get_displaystate(void)
1366 if (!display_state
) {
1367 dumb_display_init ();
1369 return display_state
;
1372 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1374 if(ds
->allocator
== &default_allocator
) {
1375 DisplaySurface
*surf
;
1376 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1377 defaultallocator_free_displaysurface(ds
->surface
);
1381 return ds
->allocator
;
1384 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1385 vga_hw_invalidate_ptr invalidate
,
1386 vga_hw_screen_dump_ptr screen_dump
,
1387 vga_hw_text_update_ptr text_update
,
1393 ds
= (DisplayState
*) g_malloc0(sizeof(DisplayState
));
1394 ds
->allocator
= &default_allocator
;
1395 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1397 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1399 qemu_free_displaysurface(ds
);
1403 s
->hw_update
= update
;
1404 s
->hw_invalidate
= invalidate
;
1405 s
->hw_screen_dump
= screen_dump
;
1406 s
->hw_text_update
= text_update
;
1409 register_displaystate(ds
);
1413 int is_graphic_console(void)
1415 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1418 int is_fixedsize_console(void)
1420 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1423 void console_color_init(DisplayState
*ds
)
1426 for (j
= 0; j
< 2; j
++) {
1427 for (i
= 0; i
< 8; i
++) {
1428 color_table
[j
][i
] = col_expand(ds
,
1429 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1434 static int n_text_consoles
;
1435 static CharDriverState
*text_consoles
[128];
1437 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1439 TextConsole
*s
= chr
->opaque
;
1444 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1447 static int color_inited
;
1451 chr
->chr_write
= console_puts
;
1453 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1454 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1455 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1458 if (!color_inited
) {
1460 console_color_init(s
->ds
);
1464 s
->total_height
= DEFAULT_BACKSCROLL
;
1467 if (s
->console_type
== TEXT_CONSOLE
) {
1468 s
->g_width
= ds_get_width(s
->ds
);
1469 s
->g_height
= ds_get_height(s
->ds
);
1472 s
->hw_invalidate
= text_console_invalidate
;
1473 s
->hw_text_update
= text_console_update
;
1476 /* Set text attribute defaults */
1477 s
->t_attrib_default
.bold
= 0;
1478 s
->t_attrib_default
.uline
= 0;
1479 s
->t_attrib_default
.blink
= 0;
1480 s
->t_attrib_default
.invers
= 0;
1481 s
->t_attrib_default
.unvisible
= 0;
1482 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1483 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1484 /* set current text attributes to default */
1485 s
->t_attrib
= s
->t_attrib_default
;
1486 text_console_resize(s
);
1492 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1493 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1494 console_puts(chr
, (uint8_t*)msg
, len
);
1495 s
->t_attrib
= s
->t_attrib_default
;
1498 qemu_chr_generic_open(chr
);
1503 int text_console_init(QemuOpts
*opts
, CharDriverState
**_chr
)
1505 CharDriverState
*chr
;
1510 chr
= g_malloc0(sizeof(CharDriverState
));
1512 if (n_text_consoles
== 128) {
1513 fprintf(stderr
, "Too many text consoles\n");
1516 text_consoles
[n_text_consoles
] = chr
;
1519 width
= qemu_opt_get_number(opts
, "width", 0);
1521 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1523 height
= qemu_opt_get_number(opts
, "height", 0);
1525 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1527 if (width
== 0 || height
== 0) {
1528 s
= new_console(NULL
, TEXT_CONSOLE
);
1530 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1540 s
->g_height
= height
;
1542 chr
->chr_set_echo
= text_console_set_echo
;
1548 void text_consoles_set_display(DisplayState
*ds
)
1552 for (i
= 0; i
< n_text_consoles
; i
++) {
1553 text_console_do_init(text_consoles
[i
], ds
);
1556 n_text_consoles
= 0;
1559 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1561 TextConsole
*s
= get_graphic_console(ds
);
1565 s
->g_height
= height
;
1566 if (is_graphic_console()) {
1567 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1572 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1573 int dst_x
, int dst_y
, int w
, int h
)
1575 if (is_graphic_console()) {
1576 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1580 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1584 memset(&pf
, 0x00, sizeof(PixelFormat
));
1586 pf
.bits_per_pixel
= bpp
;
1587 pf
.bytes_per_pixel
= bpp
/ 8;
1588 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1592 pf
.rmask
= 0x000000FF;
1593 pf
.gmask
= 0x0000FF00;
1594 pf
.bmask
= 0x00FF0000;
1606 pf
.rmask
= 0x0000FF00;
1607 pf
.gmask
= 0x00FF0000;
1608 pf
.bmask
= 0xFF000000;
1609 pf
.amask
= 0x00000000;
1629 PixelFormat
qemu_default_pixelformat(int bpp
)
1633 memset(&pf
, 0x00, sizeof(PixelFormat
));
1635 pf
.bits_per_pixel
= bpp
;
1636 pf
.bytes_per_pixel
= bpp
/ 8;
1637 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1641 pf
.bits_per_pixel
= 16;
1642 pf
.bytes_per_pixel
= 2;
1643 pf
.rmask
= 0x00007c00;
1644 pf
.gmask
= 0x000003E0;
1645 pf
.bmask
= 0x0000001F;
1657 pf
.rmask
= 0x0000F800;
1658 pf
.gmask
= 0x000007E0;
1659 pf
.bmask
= 0x0000001F;
1671 pf
.rmask
= 0x00FF0000;
1672 pf
.gmask
= 0x0000FF00;
1673 pf
.bmask
= 0x000000FF;
1684 pf
.rmask
= 0x00FF0000;
1685 pf
.gmask
= 0x0000FF00;
1686 pf
.bmask
= 0x000000FF;