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 CONSOLE_CURSOR_PERIOD 500
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. */
120 console_type_t console_type
;
122 /* Graphic console state. */
123 vga_hw_update_ptr hw_update
;
124 vga_hw_invalidate_ptr hw_invalidate
;
125 vga_hw_screen_dump_ptr hw_screen_dump
;
126 vga_hw_text_update_ptr hw_text_update
;
129 int g_width
, g_height
;
133 int backscroll_height
;
135 int x_saved
, y_saved
;
138 TextAttributes t_attrib_default
; /* default text attributes */
139 TextAttributes t_attrib
; /* currently active text attributes */
141 int text_x
[2], text_y
[2], cursor_invalidate
;
143 bool cursor_visible_phase
;
144 QEMUTimer
*cursor_timer
;
152 int esc_params
[MAX_ESC_PARAMS
];
155 CharDriverState
*chr
;
156 /* fifo for key pressed */
158 uint8_t out_fifo_buf
[16];
159 QEMUTimer
*kbd_timer
;
162 static DisplayState
*display_state
;
163 static TextConsole
*active_console
;
164 static TextConsole
*consoles
[MAX_CONSOLES
];
165 static int nb_consoles
= 0;
167 void vga_hw_update(void)
169 if (active_console
&& active_console
->hw_update
)
170 active_console
->hw_update(active_console
->hw
);
173 void vga_hw_invalidate(void)
175 if (active_console
&& active_console
->hw_invalidate
)
176 active_console
->hw_invalidate(active_console
->hw
);
179 void vga_hw_screen_dump(const char *filename
)
181 TextConsole
*previous_active_console
;
184 previous_active_console
= active_console
;
185 cswitch
= previous_active_console
&& previous_active_console
->index
!= 0;
187 /* There is currently no way of specifying which screen we want to dump,
188 so always dump the first one. */
192 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
193 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
, cswitch
);
195 error_report("screen dump not implemented");
199 console_select(previous_active_console
->index
);
203 void vga_hw_text_update(console_ch_t
*chardata
)
205 if (active_console
&& active_console
->hw_text_update
)
206 active_console
->hw_text_update(active_console
->hw
, chardata
);
209 /* convert a RGBA color to a color index usable in graphic primitives */
210 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
212 unsigned int r
, g
, b
, color
;
214 switch(ds_get_bits_per_pixel(ds
)) {
217 r
= (rgba
>> 16) & 0xff;
218 g
= (rgba
>> 8) & 0xff;
220 color
= (rgb_to_index
[r
] * 6 * 6) +
221 (rgb_to_index
[g
] * 6) +
226 r
= (rgba
>> 16) & 0xff;
227 g
= (rgba
>> 8) & 0xff;
229 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
232 r
= (rgba
>> 16) & 0xff;
233 g
= (rgba
>> 8) & 0xff;
235 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
245 static void vga_fill_rect (DisplayState
*ds
,
246 int posx
, int posy
, int width
, int height
, uint32_t color
)
251 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
252 d1
= ds_get_data(ds
) +
253 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
254 for (y
= 0; y
< height
; y
++) {
258 for (x
= 0; x
< width
; x
++) {
259 *((uint8_t *)d
) = color
;
264 for (x
= 0; x
< width
; x
++) {
265 *((uint16_t *)d
) = color
;
270 for (x
= 0; x
< width
; x
++) {
271 *((uint32_t *)d
) = color
;
276 d1
+= ds_get_linesize(ds
);
280 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
281 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
287 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
290 s
= ds_get_data(ds
) +
291 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
292 d
= ds_get_data(ds
) +
293 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
294 for (y
= 0; y
< h
; y
++) {
296 d
+= ds_get_linesize(ds
);
297 s
+= ds_get_linesize(ds
);
300 s
= ds_get_data(ds
) +
301 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
302 d
= ds_get_data(ds
) +
303 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
304 for (y
= 0; y
< h
; y
++) {
306 d
-= ds_get_linesize(ds
);
307 s
-= ds_get_linesize(ds
);
312 /***********************************************************/
313 /* basic char display */
315 #define FONT_HEIGHT 16
320 #define cbswap_32(__x) \
322 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
323 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
324 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
325 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
327 #ifdef HOST_WORDS_BIGENDIAN
330 #define PAT(x) cbswap_32(x)
333 static const uint32_t dmask16
[16] = {
352 static const uint32_t dmask4
[4] = {
359 static uint32_t color_table
[2][8];
361 #ifndef CONFIG_CURSES
374 static const uint32_t color_table_rgb
[2][8] = {
376 QEMU_RGB(0x00, 0x00, 0x00), /* black */
377 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
378 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
379 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
380 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
381 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
382 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
383 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
386 QEMU_RGB(0x00, 0x00, 0x00), /* black */
387 QEMU_RGB(0xff, 0x00, 0x00), /* red */
388 QEMU_RGB(0x00, 0xff, 0x00), /* green */
389 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
390 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
391 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
392 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
393 QEMU_RGB(0xff, 0xff, 0xff), /* white */
397 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
399 switch(ds_get_bits_per_pixel(ds
)) {
415 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
417 if (t_attrib
->bold
) {
422 if (t_attrib
->uline
) {
427 if (t_attrib
->blink
) {
432 if (t_attrib
->invers
) {
437 if (t_attrib
->unvisible
) {
443 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
447 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
448 TextAttributes
*t_attrib
)
451 const uint8_t *font_ptr
;
452 unsigned int font_data
, linesize
, xorcol
, bpp
;
454 unsigned int fgcol
, bgcol
;
457 printf("x: %2i y: %2i", x
, y
);
458 console_print_text_attributes(t_attrib
, ch
);
461 if (t_attrib
->invers
) {
462 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
463 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
465 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
466 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
469 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
470 d
= ds_get_data(ds
) +
471 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
472 linesize
= ds_get_linesize(ds
);
473 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
474 xorcol
= bgcol
^ fgcol
;
475 switch(ds_get_bits_per_pixel(ds
)) {
477 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
478 font_data
= *font_ptr
++;
480 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
483 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
484 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
490 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
491 font_data
= *font_ptr
++;
493 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
496 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
497 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
498 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
499 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
504 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
505 font_data
= *font_ptr
++;
506 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
509 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
510 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
511 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
512 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
513 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
514 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
515 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
516 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
523 static void text_console_resize(TextConsole
*s
)
525 TextCell
*cells
, *c
, *c1
;
526 int w1
, x
, y
, last_width
;
528 last_width
= s
->width
;
529 s
->width
= s
->g_width
/ FONT_WIDTH
;
530 s
->height
= s
->g_height
/ FONT_HEIGHT
;
536 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
537 for(y
= 0; y
< s
->total_height
; y
++) {
538 c
= &cells
[y
* s
->width
];
540 c1
= &s
->cells
[y
* last_width
];
541 for(x
= 0; x
< w1
; x
++) {
545 for(x
= w1
; x
< s
->width
; x
++) {
547 c
->t_attrib
= s
->t_attrib_default
;
555 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
557 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
558 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
559 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
560 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
563 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
565 if (s
->update_x0
> x
* FONT_WIDTH
)
566 s
->update_x0
= x
* FONT_WIDTH
;
567 if (s
->update_y0
> y
* FONT_HEIGHT
)
568 s
->update_y0
= y
* FONT_HEIGHT
;
569 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
570 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
571 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
572 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
575 static void update_xy(TextConsole
*s
, int x
, int y
)
580 if (s
== active_console
) {
581 if (!ds_get_bits_per_pixel(s
->ds
)) {
582 text_update_xy(s
, x
, y
);
586 y1
= (s
->y_base
+ y
) % s
->total_height
;
587 y2
= y1
- s
->y_displayed
;
589 y2
+= s
->total_height
;
590 if (y2
< s
->height
) {
591 c
= &s
->cells
[y1
* s
->width
+ x
];
592 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
594 invalidate_xy(s
, x
, y2
);
599 static void console_show_cursor(TextConsole
*s
, int show
)
604 if (s
== active_console
) {
607 if (!ds_get_bits_per_pixel(s
->ds
)) {
608 s
->cursor_invalidate
= 1;
615 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
616 y
= y1
- s
->y_displayed
;
618 y
+= s
->total_height
;
620 c
= &s
->cells
[y1
* s
->width
+ x
];
621 if (show
&& s
->cursor_visible_phase
) {
622 TextAttributes t_attrib
= s
->t_attrib_default
;
623 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
624 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
626 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
628 invalidate_xy(s
, x
, y
);
633 static void console_refresh(TextConsole
*s
)
638 if (s
!= active_console
)
640 if (!ds_get_bits_per_pixel(s
->ds
)) {
643 s
->text_x
[1] = s
->width
- 1;
644 s
->text_y
[1] = s
->height
- 1;
645 s
->cursor_invalidate
= 1;
649 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
650 color_table
[0][COLOR_BLACK
]);
652 for(y
= 0; y
< s
->height
; y
++) {
653 c
= s
->cells
+ y1
* s
->width
;
654 for(x
= 0; x
< s
->width
; x
++) {
655 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
659 if (++y1
== s
->total_height
)
662 console_show_cursor(s
, 1);
663 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
666 static void console_scroll(int ydelta
)
672 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
676 for(i
= 0; i
< ydelta
; i
++) {
677 if (s
->y_displayed
== s
->y_base
)
679 if (++s
->y_displayed
== s
->total_height
)
684 i
= s
->backscroll_height
;
685 if (i
> s
->total_height
- s
->height
)
686 i
= s
->total_height
- s
->height
;
689 y1
+= s
->total_height
;
690 for(i
= 0; i
< ydelta
; i
++) {
691 if (s
->y_displayed
== y1
)
693 if (--s
->y_displayed
< 0)
694 s
->y_displayed
= s
->total_height
- 1;
700 static void console_put_lf(TextConsole
*s
)
706 if (s
->y
>= s
->height
) {
707 s
->y
= s
->height
- 1;
709 if (s
->y_displayed
== s
->y_base
) {
710 if (++s
->y_displayed
== s
->total_height
)
713 if (++s
->y_base
== s
->total_height
)
715 if (s
->backscroll_height
< s
->total_height
)
716 s
->backscroll_height
++;
717 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
718 c
= &s
->cells
[y1
* s
->width
];
719 for(x
= 0; x
< s
->width
; x
++) {
721 c
->t_attrib
= s
->t_attrib_default
;
724 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
725 if (!ds_get_bits_per_pixel(s
->ds
)) {
728 s
->text_x
[1] = s
->width
- 1;
729 s
->text_y
[1] = s
->height
- 1;
733 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
734 s
->width
* FONT_WIDTH
,
735 (s
->height
- 1) * FONT_HEIGHT
);
736 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
737 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
738 color_table
[0][s
->t_attrib_default
.bgcol
]);
741 s
->update_x1
= s
->width
* FONT_WIDTH
;
742 s
->update_y1
= s
->height
* FONT_HEIGHT
;
747 /* Set console attributes depending on the current escape codes.
748 * NOTE: I know this code is not very efficient (checking every color for it
749 * self) but it is more readable and better maintainable.
751 static void console_handle_escape(TextConsole
*s
)
755 for (i
=0; i
<s
->nb_esc_params
; i
++) {
756 switch (s
->esc_params
[i
]) {
757 case 0: /* reset all console attributes to default */
758 s
->t_attrib
= s
->t_attrib_default
;
761 s
->t_attrib
.bold
= 1;
764 s
->t_attrib
.uline
= 1;
767 s
->t_attrib
.blink
= 1;
770 s
->t_attrib
.invers
= 1;
773 s
->t_attrib
.unvisible
= 1;
776 s
->t_attrib
.bold
= 0;
779 s
->t_attrib
.uline
= 0;
782 s
->t_attrib
.blink
= 0;
785 s
->t_attrib
.invers
= 0;
788 s
->t_attrib
.unvisible
= 0;
790 /* set foreground color */
792 s
->t_attrib
.fgcol
=COLOR_BLACK
;
795 s
->t_attrib
.fgcol
=COLOR_RED
;
798 s
->t_attrib
.fgcol
=COLOR_GREEN
;
801 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
804 s
->t_attrib
.fgcol
=COLOR_BLUE
;
807 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
810 s
->t_attrib
.fgcol
=COLOR_CYAN
;
813 s
->t_attrib
.fgcol
=COLOR_WHITE
;
815 /* set background color */
817 s
->t_attrib
.bgcol
=COLOR_BLACK
;
820 s
->t_attrib
.bgcol
=COLOR_RED
;
823 s
->t_attrib
.bgcol
=COLOR_GREEN
;
826 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
829 s
->t_attrib
.bgcol
=COLOR_BLUE
;
832 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
835 s
->t_attrib
.bgcol
=COLOR_CYAN
;
838 s
->t_attrib
.bgcol
=COLOR_WHITE
;
844 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
846 int y1
= (s
->y_base
+ y
) % s
->total_height
;
847 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
849 c
->t_attrib
= s
->t_attrib_default
;
853 /* set cursor, checking bounds */
854 static void set_cursor(TextConsole
*s
, int x
, int y
)
862 if (y
>= s
->height
) {
873 static void console_putchar(TextConsole
*s
, int ch
)
882 case '\r': /* carriage return */
885 case '\n': /* newline */
888 case '\b': /* backspace */
892 case '\t': /* tabspace */
893 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
897 s
->x
= s
->x
+ (8 - (s
->x
% 8));
900 case '\a': /* alert aka. bell */
901 /* TODO: has to be implemented */
904 /* SI (shift in), character set 0 (ignored) */
907 /* SO (shift out), character set 1 (ignored) */
909 case 27: /* esc (introducing an escape sequence) */
910 s
->state
= TTY_STATE_ESC
;
913 if (s
->x
>= s
->width
) {
918 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
919 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
921 c
->t_attrib
= s
->t_attrib
;
922 update_xy(s
, s
->x
, s
->y
);
927 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
929 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
930 s
->esc_params
[i
] = 0;
931 s
->nb_esc_params
= 0;
932 s
->state
= TTY_STATE_CSI
;
934 s
->state
= TTY_STATE_NORM
;
937 case TTY_STATE_CSI
: /* handle escape sequence parameters */
938 if (ch
>= '0' && ch
<= '9') {
939 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
940 int *param
= &s
->esc_params
[s
->nb_esc_params
];
941 int digit
= (ch
- '0');
943 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
944 *param
* 10 + digit
: INT_MAX
;
947 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
952 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
953 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
955 s
->state
= TTY_STATE_NORM
;
959 if (s
->esc_params
[0] == 0) {
960 s
->esc_params
[0] = 1;
962 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
965 /* move cursor down */
966 if (s
->esc_params
[0] == 0) {
967 s
->esc_params
[0] = 1;
969 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
972 /* move cursor right */
973 if (s
->esc_params
[0] == 0) {
974 s
->esc_params
[0] = 1;
976 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
979 /* move cursor left */
980 if (s
->esc_params
[0] == 0) {
981 s
->esc_params
[0] = 1;
983 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
986 /* move cursor to column */
987 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
991 /* move cursor to row, column */
992 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
995 switch (s
->esc_params
[0]) {
997 /* clear to end of screen */
998 for (y
= s
->y
; y
< s
->height
; y
++) {
999 for (x
= 0; x
< s
->width
; x
++) {
1000 if (y
== s
->y
&& x
< s
->x
) {
1003 console_clear_xy(s
, x
, y
);
1008 /* clear from beginning of screen */
1009 for (y
= 0; y
<= s
->y
; y
++) {
1010 for (x
= 0; x
< s
->width
; x
++) {
1011 if (y
== s
->y
&& x
> s
->x
) {
1014 console_clear_xy(s
, x
, y
);
1019 /* clear entire screen */
1020 for (y
= 0; y
<= s
->height
; y
++) {
1021 for (x
= 0; x
< s
->width
; x
++) {
1022 console_clear_xy(s
, x
, y
);
1029 switch (s
->esc_params
[0]) {
1032 for(x
= s
->x
; x
< s
->width
; x
++) {
1033 console_clear_xy(s
, x
, s
->y
);
1037 /* clear from beginning of line */
1038 for (x
= 0; x
<= s
->x
; x
++) {
1039 console_clear_xy(s
, x
, s
->y
);
1043 /* clear entire line */
1044 for(x
= 0; x
< s
->width
; x
++) {
1045 console_clear_xy(s
, x
, s
->y
);
1051 console_handle_escape(s
);
1054 /* report cursor position */
1055 /* TODO: send ESC[row;colR */
1058 /* save cursor position */
1063 /* restore cursor position */
1068 #ifdef DEBUG_CONSOLE
1069 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1078 void console_select(unsigned int index
)
1082 if (index
>= MAX_CONSOLES
)
1084 if (active_console
) {
1085 active_console
->g_width
= ds_get_width(active_console
->ds
);
1086 active_console
->g_height
= ds_get_height(active_console
->ds
);
1088 s
= consoles
[index
];
1090 DisplayState
*ds
= s
->ds
;
1092 if (active_console
&& active_console
->cursor_timer
) {
1093 qemu_del_timer(active_console
->cursor_timer
);
1096 if (ds_get_bits_per_pixel(s
->ds
)) {
1097 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1099 s
->ds
->surface
->width
= s
->width
;
1100 s
->ds
->surface
->height
= s
->height
;
1102 if (s
->cursor_timer
) {
1103 qemu_mod_timer(s
->cursor_timer
,
1104 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
1107 vga_hw_invalidate();
1111 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1113 TextConsole
*s
= chr
->opaque
;
1116 s
->update_x0
= s
->width
* FONT_WIDTH
;
1117 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1120 console_show_cursor(s
, 0);
1121 for(i
= 0; i
< len
; i
++) {
1122 console_putchar(s
, buf
[i
]);
1124 console_show_cursor(s
, 1);
1125 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1126 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1127 s
->update_x1
- s
->update_x0
,
1128 s
->update_y1
- s
->update_y0
);
1133 static void kbd_send_chars(void *opaque
)
1135 TextConsole
*s
= opaque
;
1139 len
= qemu_chr_be_can_write(s
->chr
);
1140 if (len
> s
->out_fifo
.count
)
1141 len
= s
->out_fifo
.count
;
1143 if (len
> sizeof(buf
))
1145 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1146 qemu_chr_be_write(s
->chr
, buf
, len
);
1148 /* characters are pending: we send them a bit later (XXX:
1149 horrible, should change char device API) */
1150 if (s
->out_fifo
.count
> 0) {
1151 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
1155 /* called when an ascii key is pressed */
1156 void kbd_put_keysym(int keysym
)
1159 uint8_t buf
[16], *q
;
1163 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1167 case QEMU_KEY_CTRL_UP
:
1170 case QEMU_KEY_CTRL_DOWN
:
1173 case QEMU_KEY_CTRL_PAGEUP
:
1174 console_scroll(-10);
1176 case QEMU_KEY_CTRL_PAGEDOWN
:
1180 /* convert the QEMU keysym to VT100 key string */
1182 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1185 c
= keysym
- 0xe100;
1187 *q
++ = '0' + (c
/ 10);
1188 *q
++ = '0' + (c
% 10);
1190 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1193 *q
++ = keysym
& 0xff;
1194 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1195 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1201 console_puts(s
->chr
, buf
, q
- buf
);
1203 if (s
->chr
->chr_read
) {
1204 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1211 static void text_console_invalidate(void *opaque
)
1213 TextConsole
*s
= (TextConsole
*) opaque
;
1214 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1215 s
->g_width
= ds_get_width(s
->ds
);
1216 s
->g_height
= ds_get_height(s
->ds
);
1217 text_console_resize(s
);
1222 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1224 TextConsole
*s
= (TextConsole
*) opaque
;
1227 if (s
->text_x
[0] <= s
->text_x
[1]) {
1228 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1229 chardata
+= s
->text_y
[0] * s
->width
;
1230 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1231 for (j
= 0; j
< s
->width
; j
++, src
++)
1232 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1233 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1234 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1235 (s
->cells
[src
].t_attrib
.bold
<< 21));
1236 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1237 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1238 s
->text_x
[0] = s
->width
;
1239 s
->text_y
[0] = s
->height
;
1243 if (s
->cursor_invalidate
) {
1244 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1245 s
->cursor_invalidate
= 0;
1249 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1253 for (i
= 0; i
< nb_consoles
; i
++) {
1255 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1261 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1266 if (nb_consoles
>= MAX_CONSOLES
)
1268 s
= g_malloc0(sizeof(TextConsole
));
1269 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1270 (console_type
== GRAPHIC_CONSOLE
))) {
1274 s
->console_type
= console_type
;
1275 if (console_type
!= GRAPHIC_CONSOLE
) {
1276 s
->index
= nb_consoles
;
1277 consoles
[nb_consoles
++] = s
;
1279 /* HACK: Put graphical consoles before text consoles. */
1280 for (i
= nb_consoles
; i
> 0; i
--) {
1281 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1283 consoles
[i
] = consoles
[i
- 1];
1284 consoles
[i
]->index
= i
;
1293 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1295 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1297 int linesize
= width
* 4;
1298 qemu_alloc_display(surface
, width
, height
, linesize
,
1299 qemu_default_pixelformat(32), 0);
1303 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1304 int width
, int height
)
1306 int linesize
= width
* 4;
1307 qemu_alloc_display(surface
, width
, height
, linesize
,
1308 qemu_default_pixelformat(32), 0);
1312 void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1313 int linesize
, PixelFormat pf
, int newflags
)
1316 surface
->width
= width
;
1317 surface
->height
= height
;
1318 surface
->linesize
= linesize
;
1320 if (surface
->flags
& QEMU_ALLOCATED_FLAG
) {
1321 data
= g_realloc(surface
->data
,
1322 surface
->linesize
* surface
->height
);
1324 data
= g_malloc(surface
->linesize
* surface
->height
);
1326 surface
->data
= (uint8_t *)data
;
1327 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1328 #ifdef HOST_WORDS_BIGENDIAN
1329 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1333 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1334 int linesize
, uint8_t *data
)
1336 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1338 surface
->width
= width
;
1339 surface
->height
= height
;
1340 surface
->linesize
= linesize
;
1341 surface
->pf
= qemu_default_pixelformat(bpp
);
1342 #ifdef HOST_WORDS_BIGENDIAN
1343 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1345 surface
->data
= data
;
1350 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1352 if (surface
== NULL
)
1354 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1355 g_free(surface
->data
);
1359 static struct DisplayAllocator default_allocator
= {
1360 defaultallocator_create_displaysurface
,
1361 defaultallocator_resize_displaysurface
,
1362 defaultallocator_free_displaysurface
1365 static void dumb_display_init(void)
1367 DisplayState
*ds
= g_malloc0(sizeof(DisplayState
));
1371 ds
->allocator
= &default_allocator
;
1372 if (is_fixedsize_console()) {
1373 width
= active_console
->g_width
;
1374 height
= active_console
->g_height
;
1376 ds
->surface
= qemu_create_displaysurface(ds
, width
, height
);
1377 register_displaystate(ds
);
1380 /***********************************************************/
1381 /* register display */
1383 void register_displaystate(DisplayState
*ds
)
1393 DisplayState
*get_displaystate(void)
1395 if (!display_state
) {
1396 dumb_display_init ();
1398 return display_state
;
1401 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1403 if(ds
->allocator
== &default_allocator
) {
1404 DisplaySurface
*surf
;
1405 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1406 defaultallocator_free_displaysurface(ds
->surface
);
1410 return ds
->allocator
;
1413 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1414 vga_hw_invalidate_ptr invalidate
,
1415 vga_hw_screen_dump_ptr screen_dump
,
1416 vga_hw_text_update_ptr text_update
,
1422 ds
= (DisplayState
*) g_malloc0(sizeof(DisplayState
));
1423 ds
->allocator
= &default_allocator
;
1424 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1426 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1428 qemu_free_displaysurface(ds
);
1432 s
->hw_update
= update
;
1433 s
->hw_invalidate
= invalidate
;
1434 s
->hw_screen_dump
= screen_dump
;
1435 s
->hw_text_update
= text_update
;
1438 register_displaystate(ds
);
1442 int is_graphic_console(void)
1444 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1447 int is_fixedsize_console(void)
1449 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1452 void console_color_init(DisplayState
*ds
)
1455 for (j
= 0; j
< 2; j
++) {
1456 for (i
= 0; i
< 8; i
++) {
1457 color_table
[j
][i
] = col_expand(ds
,
1458 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1463 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1465 TextConsole
*s
= chr
->opaque
;
1470 static void text_console_update_cursor(void *opaque
)
1472 TextConsole
*s
= opaque
;
1474 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1475 vga_hw_invalidate();
1476 qemu_mod_timer(s
->cursor_timer
,
1477 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
1480 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1483 static int color_inited
;
1487 chr
->chr_write
= console_puts
;
1489 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1490 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1491 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1494 if (!color_inited
) {
1496 console_color_init(s
->ds
);
1500 s
->total_height
= DEFAULT_BACKSCROLL
;
1503 if (s
->console_type
== TEXT_CONSOLE
) {
1504 s
->g_width
= ds_get_width(s
->ds
);
1505 s
->g_height
= ds_get_height(s
->ds
);
1509 qemu_new_timer_ms(rt_clock
, text_console_update_cursor
, s
);
1511 s
->hw_invalidate
= text_console_invalidate
;
1512 s
->hw_text_update
= text_console_update
;
1515 /* Set text attribute defaults */
1516 s
->t_attrib_default
.bold
= 0;
1517 s
->t_attrib_default
.uline
= 0;
1518 s
->t_attrib_default
.blink
= 0;
1519 s
->t_attrib_default
.invers
= 0;
1520 s
->t_attrib_default
.unvisible
= 0;
1521 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1522 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1523 /* set current text attributes to default */
1524 s
->t_attrib
= s
->t_attrib_default
;
1525 text_console_resize(s
);
1531 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1532 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1533 console_puts(chr
, (uint8_t*)msg
, len
);
1534 s
->t_attrib
= s
->t_attrib_default
;
1537 qemu_chr_generic_open(chr
);
1542 CharDriverState
*text_console_init(QemuOpts
*opts
)
1544 CharDriverState
*chr
;
1549 chr
= g_malloc0(sizeof(CharDriverState
));
1551 width
= qemu_opt_get_number(opts
, "width", 0);
1553 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1555 height
= qemu_opt_get_number(opts
, "height", 0);
1557 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1559 if (width
== 0 || height
== 0) {
1560 s
= new_console(NULL
, TEXT_CONSOLE
);
1562 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1572 s
->g_height
= height
;
1574 chr
->chr_set_echo
= text_console_set_echo
;
1578 void text_consoles_set_display(DisplayState
*ds
)
1582 for (i
= 0; i
< nb_consoles
; i
++) {
1583 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
) {
1584 text_console_do_init(consoles
[i
]->chr
, ds
);
1589 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1591 TextConsole
*s
= get_graphic_console(ds
);
1595 s
->g_height
= height
;
1596 if (is_graphic_console()) {
1597 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1602 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1603 int dst_x
, int dst_y
, int w
, int h
)
1605 if (is_graphic_console()) {
1606 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1610 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1614 memset(&pf
, 0x00, sizeof(PixelFormat
));
1616 pf
.bits_per_pixel
= bpp
;
1617 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1618 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1622 pf
.rmask
= 0x000000FF;
1623 pf
.gmask
= 0x0000FF00;
1624 pf
.bmask
= 0x00FF0000;
1636 pf
.rmask
= 0x0000FF00;
1637 pf
.gmask
= 0x00FF0000;
1638 pf
.bmask
= 0xFF000000;
1639 pf
.amask
= 0x00000000;
1659 PixelFormat
qemu_default_pixelformat(int bpp
)
1663 memset(&pf
, 0x00, sizeof(PixelFormat
));
1665 pf
.bits_per_pixel
= bpp
;
1666 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1667 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1671 pf
.bits_per_pixel
= 16;
1672 pf
.rmask
= 0x00007c00;
1673 pf
.gmask
= 0x000003E0;
1674 pf
.bmask
= 0x0000001F;
1686 pf
.rmask
= 0x0000F800;
1687 pf
.gmask
= 0x000007E0;
1688 pf
.bmask
= 0x0000001F;
1700 pf
.rmask
= 0x00FF0000;
1701 pf
.gmask
= 0x0000FF00;
1702 pf
.bmask
= 0x000000FF;
1714 pf
.rmask
= 0x00FF0000;
1715 pf
.gmask
= 0x0000FF00;
1716 pf
.bmask
= 0x000000FF;