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. */
119 console_type_t console_type
;
121 /* Graphic console state. */
122 vga_hw_update_ptr hw_update
;
123 vga_hw_invalidate_ptr hw_invalidate
;
124 vga_hw_screen_dump_ptr hw_screen_dump
;
125 vga_hw_text_update_ptr hw_text_update
;
128 int g_width
, g_height
;
132 int backscroll_height
;
134 int x_saved
, y_saved
;
137 TextAttributes t_attrib_default
; /* default text attributes */
138 TextAttributes t_attrib
; /* currently active text attributes */
140 int text_x
[2], text_y
[2], cursor_invalidate
;
149 int esc_params
[MAX_ESC_PARAMS
];
152 CharDriverState
*chr
;
153 /* fifo for key pressed */
155 uint8_t out_fifo_buf
[16];
156 QEMUTimer
*kbd_timer
;
159 static DisplayState
*display_state
;
160 static TextConsole
*active_console
;
161 static TextConsole
*consoles
[MAX_CONSOLES
];
162 static int nb_consoles
= 0;
164 void vga_hw_update(void)
166 if (active_console
&& active_console
->hw_update
)
167 active_console
->hw_update(active_console
->hw
);
170 void vga_hw_invalidate(void)
172 if (active_console
&& active_console
->hw_invalidate
)
173 active_console
->hw_invalidate(active_console
->hw
);
176 void vga_hw_screen_dump(const char *filename
)
178 TextConsole
*previous_active_console
;
180 previous_active_console
= active_console
;
182 /* There is currently no way of specifying which screen we want to dump,
183 so always dump the first one. */
185 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
186 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
189 if (previous_active_console
) {
190 console_select(previous_active_console
->index
);
194 void vga_hw_text_update(console_ch_t
*chardata
)
196 if (active_console
&& active_console
->hw_text_update
)
197 active_console
->hw_text_update(active_console
->hw
, chardata
);
200 /* convert a RGBA color to a color index usable in graphic primitives */
201 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
203 unsigned int r
, g
, b
, color
;
205 switch(ds_get_bits_per_pixel(ds
)) {
208 r
= (rgba
>> 16) & 0xff;
209 g
= (rgba
>> 8) & 0xff;
211 color
= (rgb_to_index
[r
] * 6 * 6) +
212 (rgb_to_index
[g
] * 6) +
217 r
= (rgba
>> 16) & 0xff;
218 g
= (rgba
>> 8) & 0xff;
220 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
223 r
= (rgba
>> 16) & 0xff;
224 g
= (rgba
>> 8) & 0xff;
226 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
236 static void vga_fill_rect (DisplayState
*ds
,
237 int posx
, int posy
, int width
, int height
, uint32_t color
)
242 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
243 d1
= ds_get_data(ds
) +
244 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
245 for (y
= 0; y
< height
; y
++) {
249 for (x
= 0; x
< width
; x
++) {
250 *((uint8_t *)d
) = color
;
255 for (x
= 0; x
< width
; x
++) {
256 *((uint16_t *)d
) = color
;
261 for (x
= 0; x
< width
; x
++) {
262 *((uint32_t *)d
) = color
;
267 d1
+= ds_get_linesize(ds
);
271 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
272 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
278 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
281 s
= ds_get_data(ds
) +
282 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
283 d
= ds_get_data(ds
) +
284 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
285 for (y
= 0; y
< h
; y
++) {
287 d
+= ds_get_linesize(ds
);
288 s
+= ds_get_linesize(ds
);
291 s
= ds_get_data(ds
) +
292 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
293 d
= ds_get_data(ds
) +
294 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
295 for (y
= 0; y
< h
; y
++) {
297 d
-= ds_get_linesize(ds
);
298 s
-= ds_get_linesize(ds
);
303 /***********************************************************/
304 /* basic char display */
306 #define FONT_HEIGHT 16
311 #define cbswap_32(__x) \
313 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
314 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
315 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
316 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
318 #ifdef HOST_WORDS_BIGENDIAN
321 #define PAT(x) cbswap_32(x)
324 static const uint32_t dmask16
[16] = {
343 static const uint32_t dmask4
[4] = {
350 static uint32_t color_table
[2][8];
352 #ifndef CONFIG_CURSES
365 static const uint32_t color_table_rgb
[2][8] = {
367 QEMU_RGB(0x00, 0x00, 0x00), /* black */
368 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
369 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
370 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
371 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
372 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
373 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
374 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
377 QEMU_RGB(0x00, 0x00, 0x00), /* black */
378 QEMU_RGB(0xff, 0x00, 0x00), /* red */
379 QEMU_RGB(0x00, 0xff, 0x00), /* green */
380 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
381 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
382 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
383 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
384 QEMU_RGB(0xff, 0xff, 0xff), /* white */
388 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
390 switch(ds_get_bits_per_pixel(ds
)) {
406 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
408 if (t_attrib
->bold
) {
413 if (t_attrib
->uline
) {
418 if (t_attrib
->blink
) {
423 if (t_attrib
->invers
) {
428 if (t_attrib
->unvisible
) {
434 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
438 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
439 TextAttributes
*t_attrib
)
442 const uint8_t *font_ptr
;
443 unsigned int font_data
, linesize
, xorcol
, bpp
;
445 unsigned int fgcol
, bgcol
;
448 printf("x: %2i y: %2i", x
, y
);
449 console_print_text_attributes(t_attrib
, ch
);
452 if (t_attrib
->invers
) {
453 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
454 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
456 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
457 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
460 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
461 d
= ds_get_data(ds
) +
462 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
463 linesize
= ds_get_linesize(ds
);
464 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
465 xorcol
= bgcol
^ fgcol
;
466 switch(ds_get_bits_per_pixel(ds
)) {
468 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
469 font_data
= *font_ptr
++;
471 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
474 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
475 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
481 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
482 font_data
= *font_ptr
++;
484 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
487 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
488 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
489 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
490 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
495 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
496 font_data
= *font_ptr
++;
497 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
500 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
501 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
502 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
503 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
504 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
505 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
506 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
507 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
514 static void text_console_resize(TextConsole
*s
)
516 TextCell
*cells
, *c
, *c1
;
517 int w1
, x
, y
, last_width
;
519 last_width
= s
->width
;
520 s
->width
= s
->g_width
/ FONT_WIDTH
;
521 s
->height
= s
->g_height
/ FONT_HEIGHT
;
527 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
528 for(y
= 0; y
< s
->total_height
; y
++) {
529 c
= &cells
[y
* s
->width
];
531 c1
= &s
->cells
[y
* last_width
];
532 for(x
= 0; x
< w1
; x
++) {
536 for(x
= w1
; x
< s
->width
; x
++) {
538 c
->t_attrib
= s
->t_attrib_default
;
546 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
548 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
549 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
550 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
551 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
554 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
556 if (s
->update_x0
> x
* FONT_WIDTH
)
557 s
->update_x0
= x
* FONT_WIDTH
;
558 if (s
->update_y0
> y
* FONT_HEIGHT
)
559 s
->update_y0
= y
* FONT_HEIGHT
;
560 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
561 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
562 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
563 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
566 static void update_xy(TextConsole
*s
, int x
, int y
)
571 if (s
== active_console
) {
572 if (!ds_get_bits_per_pixel(s
->ds
)) {
573 text_update_xy(s
, x
, y
);
577 y1
= (s
->y_base
+ y
) % s
->total_height
;
578 y2
= y1
- s
->y_displayed
;
580 y2
+= s
->total_height
;
581 if (y2
< s
->height
) {
582 c
= &s
->cells
[y1
* s
->width
+ x
];
583 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
585 invalidate_xy(s
, x
, y2
);
590 static void console_show_cursor(TextConsole
*s
, int show
)
595 if (s
== active_console
) {
598 if (!ds_get_bits_per_pixel(s
->ds
)) {
599 s
->cursor_invalidate
= 1;
606 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
607 y
= y1
- s
->y_displayed
;
609 y
+= s
->total_height
;
611 c
= &s
->cells
[y1
* s
->width
+ x
];
613 TextAttributes t_attrib
= s
->t_attrib_default
;
614 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
615 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
617 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
619 invalidate_xy(s
, x
, y
);
624 static void console_refresh(TextConsole
*s
)
629 if (s
!= active_console
)
631 if (!ds_get_bits_per_pixel(s
->ds
)) {
634 s
->text_x
[1] = s
->width
- 1;
635 s
->text_y
[1] = s
->height
- 1;
636 s
->cursor_invalidate
= 1;
640 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
641 color_table
[0][COLOR_BLACK
]);
643 for(y
= 0; y
< s
->height
; y
++) {
644 c
= s
->cells
+ y1
* s
->width
;
645 for(x
= 0; x
< s
->width
; x
++) {
646 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
650 if (++y1
== s
->total_height
)
653 console_show_cursor(s
, 1);
654 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
657 static void console_scroll(int ydelta
)
663 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
667 for(i
= 0; i
< ydelta
; i
++) {
668 if (s
->y_displayed
== s
->y_base
)
670 if (++s
->y_displayed
== s
->total_height
)
675 i
= s
->backscroll_height
;
676 if (i
> s
->total_height
- s
->height
)
677 i
= s
->total_height
- s
->height
;
680 y1
+= s
->total_height
;
681 for(i
= 0; i
< ydelta
; i
++) {
682 if (s
->y_displayed
== y1
)
684 if (--s
->y_displayed
< 0)
685 s
->y_displayed
= s
->total_height
- 1;
691 static void console_put_lf(TextConsole
*s
)
697 if (s
->y
>= s
->height
) {
698 s
->y
= s
->height
- 1;
700 if (s
->y_displayed
== s
->y_base
) {
701 if (++s
->y_displayed
== s
->total_height
)
704 if (++s
->y_base
== s
->total_height
)
706 if (s
->backscroll_height
< s
->total_height
)
707 s
->backscroll_height
++;
708 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
709 c
= &s
->cells
[y1
* s
->width
];
710 for(x
= 0; x
< s
->width
; x
++) {
712 c
->t_attrib
= s
->t_attrib_default
;
715 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
716 if (!ds_get_bits_per_pixel(s
->ds
)) {
719 s
->text_x
[1] = s
->width
- 1;
720 s
->text_y
[1] = s
->height
- 1;
724 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
725 s
->width
* FONT_WIDTH
,
726 (s
->height
- 1) * FONT_HEIGHT
);
727 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
728 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
729 color_table
[0][s
->t_attrib_default
.bgcol
]);
732 s
->update_x1
= s
->width
* FONT_WIDTH
;
733 s
->update_y1
= s
->height
* FONT_HEIGHT
;
738 /* Set console attributes depending on the current escape codes.
739 * NOTE: I know this code is not very efficient (checking every color for it
740 * self) but it is more readable and better maintainable.
742 static void console_handle_escape(TextConsole
*s
)
746 for (i
=0; i
<s
->nb_esc_params
; i
++) {
747 switch (s
->esc_params
[i
]) {
748 case 0: /* reset all console attributes to default */
749 s
->t_attrib
= s
->t_attrib_default
;
752 s
->t_attrib
.bold
= 1;
755 s
->t_attrib
.uline
= 1;
758 s
->t_attrib
.blink
= 1;
761 s
->t_attrib
.invers
= 1;
764 s
->t_attrib
.unvisible
= 1;
767 s
->t_attrib
.bold
= 0;
770 s
->t_attrib
.uline
= 0;
773 s
->t_attrib
.blink
= 0;
776 s
->t_attrib
.invers
= 0;
779 s
->t_attrib
.unvisible
= 0;
781 /* set foreground color */
783 s
->t_attrib
.fgcol
=COLOR_BLACK
;
786 s
->t_attrib
.fgcol
=COLOR_RED
;
789 s
->t_attrib
.fgcol
=COLOR_GREEN
;
792 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
795 s
->t_attrib
.fgcol
=COLOR_BLUE
;
798 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
801 s
->t_attrib
.fgcol
=COLOR_CYAN
;
804 s
->t_attrib
.fgcol
=COLOR_WHITE
;
806 /* set background color */
808 s
->t_attrib
.bgcol
=COLOR_BLACK
;
811 s
->t_attrib
.bgcol
=COLOR_RED
;
814 s
->t_attrib
.bgcol
=COLOR_GREEN
;
817 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
820 s
->t_attrib
.bgcol
=COLOR_BLUE
;
823 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
826 s
->t_attrib
.bgcol
=COLOR_CYAN
;
829 s
->t_attrib
.bgcol
=COLOR_WHITE
;
835 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
837 int y1
= (s
->y_base
+ y
) % s
->total_height
;
838 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
840 c
->t_attrib
= s
->t_attrib_default
;
844 static void console_putchar(TextConsole
*s
, int ch
)
853 case '\r': /* carriage return */
856 case '\n': /* newline */
859 case '\b': /* backspace */
863 case '\t': /* tabspace */
864 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
868 s
->x
= s
->x
+ (8 - (s
->x
% 8));
871 case '\a': /* alert aka. bell */
872 /* TODO: has to be implemented */
875 /* SI (shift in), character set 0 (ignored) */
878 /* SO (shift out), character set 1 (ignored) */
880 case 27: /* esc (introducing an escape sequence) */
881 s
->state
= TTY_STATE_ESC
;
884 if (s
->x
>= s
->width
) {
889 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
890 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
892 c
->t_attrib
= s
->t_attrib
;
893 update_xy(s
, s
->x
, s
->y
);
898 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
900 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
901 s
->esc_params
[i
] = 0;
902 s
->nb_esc_params
= 0;
903 s
->state
= TTY_STATE_CSI
;
905 s
->state
= TTY_STATE_NORM
;
908 case TTY_STATE_CSI
: /* handle escape sequence parameters */
909 if (ch
>= '0' && ch
<= '9') {
910 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
911 s
->esc_params
[s
->nb_esc_params
] =
912 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
919 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
920 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
922 s
->state
= TTY_STATE_NORM
;
926 if (s
->esc_params
[0] == 0) {
927 s
->esc_params
[0] = 1;
929 s
->y
-= s
->esc_params
[0];
935 /* move cursor down */
936 if (s
->esc_params
[0] == 0) {
937 s
->esc_params
[0] = 1;
939 s
->y
+= s
->esc_params
[0];
940 if (s
->y
>= s
->height
) {
941 s
->y
= s
->height
- 1;
945 /* move cursor right */
946 if (s
->esc_params
[0] == 0) {
947 s
->esc_params
[0] = 1;
949 s
->x
+= s
->esc_params
[0];
950 if (s
->x
>= s
->width
) {
955 /* move cursor left */
956 if (s
->esc_params
[0] == 0) {
957 s
->esc_params
[0] = 1;
959 s
->x
-= s
->esc_params
[0];
965 /* move cursor to column */
966 s
->x
= s
->esc_params
[0] - 1;
973 /* move cursor to row, column */
974 s
->x
= s
->esc_params
[1] - 1;
978 s
->y
= s
->esc_params
[0] - 1;
984 switch (s
->esc_params
[0]) {
986 /* clear to end of screen */
987 for (y
= s
->y
; y
< s
->height
; y
++) {
988 for (x
= 0; x
< s
->width
; x
++) {
989 if (y
== s
->y
&& x
< s
->x
) {
992 console_clear_xy(s
, x
, y
);
997 /* clear from beginning of screen */
998 for (y
= 0; y
<= s
->y
; 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 entire screen */
1009 for (y
= 0; y
<= s
->height
; y
++) {
1010 for (x
= 0; x
< s
->width
; x
++) {
1011 console_clear_xy(s
, x
, y
);
1018 switch (s
->esc_params
[0]) {
1021 for(x
= s
->x
; x
< s
->width
; x
++) {
1022 console_clear_xy(s
, x
, s
->y
);
1026 /* clear from beginning of line */
1027 for (x
= 0; x
<= s
->x
; x
++) {
1028 console_clear_xy(s
, x
, s
->y
);
1032 /* clear entire line */
1033 for(x
= 0; x
< s
->width
; x
++) {
1034 console_clear_xy(s
, x
, s
->y
);
1040 console_handle_escape(s
);
1043 /* report cursor position */
1044 /* TODO: send ESC[row;colR */
1047 /* save cursor position */
1052 /* restore cursor position */
1057 #ifdef DEBUG_CONSOLE
1058 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1067 void console_select(unsigned int index
)
1071 if (index
>= MAX_CONSOLES
)
1073 if (active_console
) {
1074 active_console
->g_width
= ds_get_width(active_console
->ds
);
1075 active_console
->g_height
= ds_get_height(active_console
->ds
);
1077 s
= consoles
[index
];
1079 DisplayState
*ds
= s
->ds
;
1081 if (ds_get_bits_per_pixel(s
->ds
)) {
1082 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1084 s
->ds
->surface
->width
= s
->width
;
1085 s
->ds
->surface
->height
= s
->height
;
1088 vga_hw_invalidate();
1092 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1094 TextConsole
*s
= chr
->opaque
;
1097 s
->update_x0
= s
->width
* FONT_WIDTH
;
1098 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1101 console_show_cursor(s
, 0);
1102 for(i
= 0; i
< len
; i
++) {
1103 console_putchar(s
, buf
[i
]);
1105 console_show_cursor(s
, 1);
1106 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1107 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1108 s
->update_x1
- s
->update_x0
,
1109 s
->update_y1
- s
->update_y0
);
1114 static void kbd_send_chars(void *opaque
)
1116 TextConsole
*s
= opaque
;
1120 len
= qemu_chr_be_can_write(s
->chr
);
1121 if (len
> s
->out_fifo
.count
)
1122 len
= s
->out_fifo
.count
;
1124 if (len
> sizeof(buf
))
1126 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1127 qemu_chr_be_write(s
->chr
, buf
, len
);
1129 /* characters are pending: we send them a bit later (XXX:
1130 horrible, should change char device API) */
1131 if (s
->out_fifo
.count
> 0) {
1132 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
1136 /* called when an ascii key is pressed */
1137 void kbd_put_keysym(int keysym
)
1140 uint8_t buf
[16], *q
;
1144 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1148 case QEMU_KEY_CTRL_UP
:
1151 case QEMU_KEY_CTRL_DOWN
:
1154 case QEMU_KEY_CTRL_PAGEUP
:
1155 console_scroll(-10);
1157 case QEMU_KEY_CTRL_PAGEDOWN
:
1161 /* convert the QEMU keysym to VT100 key string */
1163 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1166 c
= keysym
- 0xe100;
1168 *q
++ = '0' + (c
/ 10);
1169 *q
++ = '0' + (c
% 10);
1171 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1174 *q
++ = keysym
& 0xff;
1175 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1176 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1182 console_puts(s
->chr
, buf
, q
- buf
);
1184 if (s
->chr
->chr_read
) {
1185 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1192 static void text_console_invalidate(void *opaque
)
1194 TextConsole
*s
= (TextConsole
*) opaque
;
1195 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1196 s
->g_width
= ds_get_width(s
->ds
);
1197 s
->g_height
= ds_get_height(s
->ds
);
1198 text_console_resize(s
);
1203 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1205 TextConsole
*s
= (TextConsole
*) opaque
;
1208 if (s
->text_x
[0] <= s
->text_x
[1]) {
1209 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1210 chardata
+= s
->text_y
[0] * s
->width
;
1211 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1212 for (j
= 0; j
< s
->width
; j
++, src
++)
1213 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1214 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1215 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1216 (s
->cells
[src
].t_attrib
.bold
<< 21));
1217 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1218 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1219 s
->text_x
[0] = s
->width
;
1220 s
->text_y
[0] = s
->height
;
1224 if (s
->cursor_invalidate
) {
1225 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1226 s
->cursor_invalidate
= 0;
1230 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1234 for (i
= 0; i
< nb_consoles
; i
++) {
1236 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1242 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1247 if (nb_consoles
>= MAX_CONSOLES
)
1249 s
= g_malloc0(sizeof(TextConsole
));
1250 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1251 (console_type
== GRAPHIC_CONSOLE
))) {
1255 s
->console_type
= console_type
;
1256 if (console_type
!= GRAPHIC_CONSOLE
) {
1257 s
->index
= nb_consoles
;
1258 consoles
[nb_consoles
++] = s
;
1260 /* HACK: Put graphical consoles before text consoles. */
1261 for (i
= nb_consoles
; i
> 0; i
--) {
1262 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1264 consoles
[i
] = consoles
[i
- 1];
1265 consoles
[i
]->index
= i
;
1274 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1276 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1278 int linesize
= width
* 4;
1279 qemu_alloc_display(surface
, width
, height
, linesize
,
1280 qemu_default_pixelformat(32), 0);
1284 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1285 int width
, int height
)
1287 int linesize
= width
* 4;
1288 qemu_alloc_display(surface
, width
, height
, linesize
,
1289 qemu_default_pixelformat(32), 0);
1293 void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1294 int linesize
, PixelFormat pf
, int newflags
)
1297 surface
->width
= width
;
1298 surface
->height
= height
;
1299 surface
->linesize
= linesize
;
1301 if (surface
->flags
& QEMU_ALLOCATED_FLAG
) {
1302 data
= g_realloc(surface
->data
,
1303 surface
->linesize
* surface
->height
);
1305 data
= g_malloc(surface
->linesize
* surface
->height
);
1307 surface
->data
= (uint8_t *)data
;
1308 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1309 #ifdef HOST_WORDS_BIGENDIAN
1310 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1314 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1315 int linesize
, uint8_t *data
)
1317 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1319 surface
->width
= width
;
1320 surface
->height
= height
;
1321 surface
->linesize
= linesize
;
1322 surface
->pf
= qemu_default_pixelformat(bpp
);
1323 #ifdef HOST_WORDS_BIGENDIAN
1324 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1326 surface
->data
= data
;
1331 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1333 if (surface
== NULL
)
1335 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1336 g_free(surface
->data
);
1340 static struct DisplayAllocator default_allocator
= {
1341 defaultallocator_create_displaysurface
,
1342 defaultallocator_resize_displaysurface
,
1343 defaultallocator_free_displaysurface
1346 static void dumb_display_init(void)
1348 DisplayState
*ds
= g_malloc0(sizeof(DisplayState
));
1352 ds
->allocator
= &default_allocator
;
1353 if (is_fixedsize_console()) {
1354 width
= active_console
->g_width
;
1355 height
= active_console
->g_height
;
1357 ds
->surface
= qemu_create_displaysurface(ds
, width
, height
);
1358 register_displaystate(ds
);
1361 /***********************************************************/
1362 /* register display */
1364 void register_displaystate(DisplayState
*ds
)
1374 DisplayState
*get_displaystate(void)
1376 if (!display_state
) {
1377 dumb_display_init ();
1379 return display_state
;
1382 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1384 if(ds
->allocator
== &default_allocator
) {
1385 DisplaySurface
*surf
;
1386 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1387 defaultallocator_free_displaysurface(ds
->surface
);
1391 return ds
->allocator
;
1394 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1395 vga_hw_invalidate_ptr invalidate
,
1396 vga_hw_screen_dump_ptr screen_dump
,
1397 vga_hw_text_update_ptr text_update
,
1403 ds
= (DisplayState
*) g_malloc0(sizeof(DisplayState
));
1404 ds
->allocator
= &default_allocator
;
1405 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1407 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1409 qemu_free_displaysurface(ds
);
1413 s
->hw_update
= update
;
1414 s
->hw_invalidate
= invalidate
;
1415 s
->hw_screen_dump
= screen_dump
;
1416 s
->hw_text_update
= text_update
;
1419 register_displaystate(ds
);
1423 int is_graphic_console(void)
1425 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1428 int is_fixedsize_console(void)
1430 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1433 void console_color_init(DisplayState
*ds
)
1436 for (j
= 0; j
< 2; j
++) {
1437 for (i
= 0; i
< 8; i
++) {
1438 color_table
[j
][i
] = col_expand(ds
,
1439 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1444 static int n_text_consoles
;
1445 static CharDriverState
*text_consoles
[128];
1447 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1449 TextConsole
*s
= chr
->opaque
;
1454 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1457 static int color_inited
;
1461 chr
->chr_write
= console_puts
;
1463 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1464 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1465 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1468 if (!color_inited
) {
1470 console_color_init(s
->ds
);
1474 s
->total_height
= DEFAULT_BACKSCROLL
;
1477 if (s
->console_type
== TEXT_CONSOLE
) {
1478 s
->g_width
= ds_get_width(s
->ds
);
1479 s
->g_height
= ds_get_height(s
->ds
);
1482 s
->hw_invalidate
= text_console_invalidate
;
1483 s
->hw_text_update
= text_console_update
;
1486 /* Set text attribute defaults */
1487 s
->t_attrib_default
.bold
= 0;
1488 s
->t_attrib_default
.uline
= 0;
1489 s
->t_attrib_default
.blink
= 0;
1490 s
->t_attrib_default
.invers
= 0;
1491 s
->t_attrib_default
.unvisible
= 0;
1492 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1493 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1494 /* set current text attributes to default */
1495 s
->t_attrib
= s
->t_attrib_default
;
1496 text_console_resize(s
);
1502 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1503 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1504 console_puts(chr
, (uint8_t*)msg
, len
);
1505 s
->t_attrib
= s
->t_attrib_default
;
1508 qemu_chr_generic_open(chr
);
1513 int text_console_init(QemuOpts
*opts
, CharDriverState
**_chr
)
1515 CharDriverState
*chr
;
1520 chr
= g_malloc0(sizeof(CharDriverState
));
1522 if (n_text_consoles
== 128) {
1523 fprintf(stderr
, "Too many text consoles\n");
1526 text_consoles
[n_text_consoles
] = chr
;
1529 width
= qemu_opt_get_number(opts
, "width", 0);
1531 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1533 height
= qemu_opt_get_number(opts
, "height", 0);
1535 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1537 if (width
== 0 || height
== 0) {
1538 s
= new_console(NULL
, TEXT_CONSOLE
);
1540 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1550 s
->g_height
= height
;
1552 chr
->chr_set_echo
= text_console_set_echo
;
1558 void text_consoles_set_display(DisplayState
*ds
)
1562 for (i
= 0; i
< n_text_consoles
; i
++) {
1563 text_console_do_init(text_consoles
[i
], ds
);
1566 n_text_consoles
= 0;
1569 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1571 TextConsole
*s
= get_graphic_console(ds
);
1575 s
->g_height
= height
;
1576 if (is_graphic_console()) {
1577 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1582 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1583 int dst_x
, int dst_y
, int w
, int h
)
1585 if (is_graphic_console()) {
1586 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1590 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1594 memset(&pf
, 0x00, sizeof(PixelFormat
));
1596 pf
.bits_per_pixel
= bpp
;
1597 pf
.bytes_per_pixel
= bpp
/ 8;
1598 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1602 pf
.rmask
= 0x000000FF;
1603 pf
.gmask
= 0x0000FF00;
1604 pf
.bmask
= 0x00FF0000;
1616 pf
.rmask
= 0x0000FF00;
1617 pf
.gmask
= 0x00FF0000;
1618 pf
.bmask
= 0xFF000000;
1619 pf
.amask
= 0x00000000;
1639 PixelFormat
qemu_default_pixelformat(int bpp
)
1643 memset(&pf
, 0x00, sizeof(PixelFormat
));
1645 pf
.bits_per_pixel
= bpp
;
1646 pf
.bytes_per_pixel
= bpp
/ 8;
1647 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1651 pf
.bits_per_pixel
= 16;
1652 pf
.bytes_per_pixel
= 2;
1653 pf
.rmask
= 0x00007c00;
1654 pf
.gmask
= 0x000003E0;
1655 pf
.bmask
= 0x0000001F;
1667 pf
.rmask
= 0x0000F800;
1668 pf
.gmask
= 0x000007E0;
1669 pf
.bmask
= 0x0000001F;
1681 pf
.rmask
= 0x00FF0000;
1682 pf
.gmask
= 0x0000FF00;
1683 pf
.bmask
= 0x000000FF;
1695 pf
.rmask
= 0x00FF0000;
1696 pf
.gmask
= 0x0000FF00;
1697 pf
.bmask
= 0x000000FF;