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
;
181 previous_active_console
= active_console
;
182 cswitch
= previous_active_console
&& previous_active_console
->index
!= 0;
184 /* There is currently no way of specifying which screen we want to dump,
185 so always dump the first one. */
189 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
190 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
, cswitch
);
192 error_report("screen dump not implemented");
196 console_select(previous_active_console
->index
);
200 void vga_hw_text_update(console_ch_t
*chardata
)
202 if (active_console
&& active_console
->hw_text_update
)
203 active_console
->hw_text_update(active_console
->hw
, chardata
);
206 /* convert a RGBA color to a color index usable in graphic primitives */
207 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
209 unsigned int r
, g
, b
, color
;
211 switch(ds_get_bits_per_pixel(ds
)) {
214 r
= (rgba
>> 16) & 0xff;
215 g
= (rgba
>> 8) & 0xff;
217 color
= (rgb_to_index
[r
] * 6 * 6) +
218 (rgb_to_index
[g
] * 6) +
223 r
= (rgba
>> 16) & 0xff;
224 g
= (rgba
>> 8) & 0xff;
226 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
229 r
= (rgba
>> 16) & 0xff;
230 g
= (rgba
>> 8) & 0xff;
232 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
242 static void vga_fill_rect (DisplayState
*ds
,
243 int posx
, int posy
, int width
, int height
, uint32_t color
)
248 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
249 d1
= ds_get_data(ds
) +
250 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
251 for (y
= 0; y
< height
; y
++) {
255 for (x
= 0; x
< width
; x
++) {
256 *((uint8_t *)d
) = color
;
261 for (x
= 0; x
< width
; x
++) {
262 *((uint16_t *)d
) = color
;
267 for (x
= 0; x
< width
; x
++) {
268 *((uint32_t *)d
) = color
;
273 d1
+= ds_get_linesize(ds
);
277 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
278 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
284 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
287 s
= ds_get_data(ds
) +
288 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
289 d
= ds_get_data(ds
) +
290 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
291 for (y
= 0; y
< h
; y
++) {
293 d
+= ds_get_linesize(ds
);
294 s
+= ds_get_linesize(ds
);
297 s
= ds_get_data(ds
) +
298 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
299 d
= ds_get_data(ds
) +
300 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
301 for (y
= 0; y
< h
; y
++) {
303 d
-= ds_get_linesize(ds
);
304 s
-= ds_get_linesize(ds
);
309 /***********************************************************/
310 /* basic char display */
312 #define FONT_HEIGHT 16
317 #define cbswap_32(__x) \
319 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
320 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
321 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
322 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
324 #ifdef HOST_WORDS_BIGENDIAN
327 #define PAT(x) cbswap_32(x)
330 static const uint32_t dmask16
[16] = {
349 static const uint32_t dmask4
[4] = {
356 static uint32_t color_table
[2][8];
358 #ifndef CONFIG_CURSES
371 static const uint32_t color_table_rgb
[2][8] = {
373 QEMU_RGB(0x00, 0x00, 0x00), /* black */
374 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
375 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
376 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
377 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
378 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
379 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
380 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
383 QEMU_RGB(0x00, 0x00, 0x00), /* black */
384 QEMU_RGB(0xff, 0x00, 0x00), /* red */
385 QEMU_RGB(0x00, 0xff, 0x00), /* green */
386 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
387 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
388 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
389 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
390 QEMU_RGB(0xff, 0xff, 0xff), /* white */
394 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
396 switch(ds_get_bits_per_pixel(ds
)) {
412 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
414 if (t_attrib
->bold
) {
419 if (t_attrib
->uline
) {
424 if (t_attrib
->blink
) {
429 if (t_attrib
->invers
) {
434 if (t_attrib
->unvisible
) {
440 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
444 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
445 TextAttributes
*t_attrib
)
448 const uint8_t *font_ptr
;
449 unsigned int font_data
, linesize
, xorcol
, bpp
;
451 unsigned int fgcol
, bgcol
;
454 printf("x: %2i y: %2i", x
, y
);
455 console_print_text_attributes(t_attrib
, ch
);
458 if (t_attrib
->invers
) {
459 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
460 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
462 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
463 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
466 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
467 d
= ds_get_data(ds
) +
468 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
469 linesize
= ds_get_linesize(ds
);
470 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
471 xorcol
= bgcol
^ fgcol
;
472 switch(ds_get_bits_per_pixel(ds
)) {
474 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
475 font_data
= *font_ptr
++;
477 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
480 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
481 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
487 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
488 font_data
= *font_ptr
++;
490 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
493 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
494 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
495 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
496 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
501 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
502 font_data
= *font_ptr
++;
503 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
506 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
507 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
508 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
509 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
510 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
511 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
512 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
513 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
520 static void text_console_resize(TextConsole
*s
)
522 TextCell
*cells
, *c
, *c1
;
523 int w1
, x
, y
, last_width
;
525 last_width
= s
->width
;
526 s
->width
= s
->g_width
/ FONT_WIDTH
;
527 s
->height
= s
->g_height
/ FONT_HEIGHT
;
533 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
534 for(y
= 0; y
< s
->total_height
; y
++) {
535 c
= &cells
[y
* s
->width
];
537 c1
= &s
->cells
[y
* last_width
];
538 for(x
= 0; x
< w1
; x
++) {
542 for(x
= w1
; x
< s
->width
; x
++) {
544 c
->t_attrib
= s
->t_attrib_default
;
552 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
554 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
555 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
556 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
557 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
560 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
562 if (s
->update_x0
> x
* FONT_WIDTH
)
563 s
->update_x0
= x
* FONT_WIDTH
;
564 if (s
->update_y0
> y
* FONT_HEIGHT
)
565 s
->update_y0
= y
* FONT_HEIGHT
;
566 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
567 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
568 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
569 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
572 static void update_xy(TextConsole
*s
, int x
, int y
)
577 if (s
== active_console
) {
578 if (!ds_get_bits_per_pixel(s
->ds
)) {
579 text_update_xy(s
, x
, y
);
583 y1
= (s
->y_base
+ y
) % s
->total_height
;
584 y2
= y1
- s
->y_displayed
;
586 y2
+= s
->total_height
;
587 if (y2
< s
->height
) {
588 c
= &s
->cells
[y1
* s
->width
+ x
];
589 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
591 invalidate_xy(s
, x
, y2
);
596 static void console_show_cursor(TextConsole
*s
, int show
)
601 if (s
== active_console
) {
604 if (!ds_get_bits_per_pixel(s
->ds
)) {
605 s
->cursor_invalidate
= 1;
612 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
613 y
= y1
- s
->y_displayed
;
615 y
+= s
->total_height
;
617 c
= &s
->cells
[y1
* s
->width
+ x
];
619 TextAttributes t_attrib
= s
->t_attrib_default
;
620 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
621 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
623 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
625 invalidate_xy(s
, x
, y
);
630 static void console_refresh(TextConsole
*s
)
635 if (s
!= active_console
)
637 if (!ds_get_bits_per_pixel(s
->ds
)) {
640 s
->text_x
[1] = s
->width
- 1;
641 s
->text_y
[1] = s
->height
- 1;
642 s
->cursor_invalidate
= 1;
646 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
647 color_table
[0][COLOR_BLACK
]);
649 for(y
= 0; y
< s
->height
; y
++) {
650 c
= s
->cells
+ y1
* s
->width
;
651 for(x
= 0; x
< s
->width
; x
++) {
652 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
656 if (++y1
== s
->total_height
)
659 console_show_cursor(s
, 1);
660 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
663 static void console_scroll(int ydelta
)
669 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
673 for(i
= 0; i
< ydelta
; i
++) {
674 if (s
->y_displayed
== s
->y_base
)
676 if (++s
->y_displayed
== s
->total_height
)
681 i
= s
->backscroll_height
;
682 if (i
> s
->total_height
- s
->height
)
683 i
= s
->total_height
- s
->height
;
686 y1
+= s
->total_height
;
687 for(i
= 0; i
< ydelta
; i
++) {
688 if (s
->y_displayed
== y1
)
690 if (--s
->y_displayed
< 0)
691 s
->y_displayed
= s
->total_height
- 1;
697 static void console_put_lf(TextConsole
*s
)
703 if (s
->y
>= s
->height
) {
704 s
->y
= s
->height
- 1;
706 if (s
->y_displayed
== s
->y_base
) {
707 if (++s
->y_displayed
== s
->total_height
)
710 if (++s
->y_base
== s
->total_height
)
712 if (s
->backscroll_height
< s
->total_height
)
713 s
->backscroll_height
++;
714 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
715 c
= &s
->cells
[y1
* s
->width
];
716 for(x
= 0; x
< s
->width
; x
++) {
718 c
->t_attrib
= s
->t_attrib_default
;
721 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
722 if (!ds_get_bits_per_pixel(s
->ds
)) {
725 s
->text_x
[1] = s
->width
- 1;
726 s
->text_y
[1] = s
->height
- 1;
730 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
731 s
->width
* FONT_WIDTH
,
732 (s
->height
- 1) * FONT_HEIGHT
);
733 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
734 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
735 color_table
[0][s
->t_attrib_default
.bgcol
]);
738 s
->update_x1
= s
->width
* FONT_WIDTH
;
739 s
->update_y1
= s
->height
* FONT_HEIGHT
;
744 /* Set console attributes depending on the current escape codes.
745 * NOTE: I know this code is not very efficient (checking every color for it
746 * self) but it is more readable and better maintainable.
748 static void console_handle_escape(TextConsole
*s
)
752 for (i
=0; i
<s
->nb_esc_params
; i
++) {
753 switch (s
->esc_params
[i
]) {
754 case 0: /* reset all console attributes to default */
755 s
->t_attrib
= s
->t_attrib_default
;
758 s
->t_attrib
.bold
= 1;
761 s
->t_attrib
.uline
= 1;
764 s
->t_attrib
.blink
= 1;
767 s
->t_attrib
.invers
= 1;
770 s
->t_attrib
.unvisible
= 1;
773 s
->t_attrib
.bold
= 0;
776 s
->t_attrib
.uline
= 0;
779 s
->t_attrib
.blink
= 0;
782 s
->t_attrib
.invers
= 0;
785 s
->t_attrib
.unvisible
= 0;
787 /* set foreground color */
789 s
->t_attrib
.fgcol
=COLOR_BLACK
;
792 s
->t_attrib
.fgcol
=COLOR_RED
;
795 s
->t_attrib
.fgcol
=COLOR_GREEN
;
798 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
801 s
->t_attrib
.fgcol
=COLOR_BLUE
;
804 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
807 s
->t_attrib
.fgcol
=COLOR_CYAN
;
810 s
->t_attrib
.fgcol
=COLOR_WHITE
;
812 /* set background color */
814 s
->t_attrib
.bgcol
=COLOR_BLACK
;
817 s
->t_attrib
.bgcol
=COLOR_RED
;
820 s
->t_attrib
.bgcol
=COLOR_GREEN
;
823 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
826 s
->t_attrib
.bgcol
=COLOR_BLUE
;
829 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
832 s
->t_attrib
.bgcol
=COLOR_CYAN
;
835 s
->t_attrib
.bgcol
=COLOR_WHITE
;
841 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
843 int y1
= (s
->y_base
+ y
) % s
->total_height
;
844 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
846 c
->t_attrib
= s
->t_attrib_default
;
850 static void console_putchar(TextConsole
*s
, int ch
)
859 case '\r': /* carriage return */
862 case '\n': /* newline */
865 case '\b': /* backspace */
869 case '\t': /* tabspace */
870 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
874 s
->x
= s
->x
+ (8 - (s
->x
% 8));
877 case '\a': /* alert aka. bell */
878 /* TODO: has to be implemented */
881 /* SI (shift in), character set 0 (ignored) */
884 /* SO (shift out), character set 1 (ignored) */
886 case 27: /* esc (introducing an escape sequence) */
887 s
->state
= TTY_STATE_ESC
;
890 if (s
->x
>= s
->width
) {
895 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
896 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
898 c
->t_attrib
= s
->t_attrib
;
899 update_xy(s
, s
->x
, s
->y
);
904 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
906 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
907 s
->esc_params
[i
] = 0;
908 s
->nb_esc_params
= 0;
909 s
->state
= TTY_STATE_CSI
;
911 s
->state
= TTY_STATE_NORM
;
914 case TTY_STATE_CSI
: /* handle escape sequence parameters */
915 if (ch
>= '0' && ch
<= '9') {
916 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
917 s
->esc_params
[s
->nb_esc_params
] =
918 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
925 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
926 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
928 s
->state
= TTY_STATE_NORM
;
932 if (s
->esc_params
[0] == 0) {
933 s
->esc_params
[0] = 1;
935 s
->y
-= s
->esc_params
[0];
941 /* move cursor down */
942 if (s
->esc_params
[0] == 0) {
943 s
->esc_params
[0] = 1;
945 s
->y
+= s
->esc_params
[0];
946 if (s
->y
>= s
->height
) {
947 s
->y
= s
->height
- 1;
951 /* move cursor right */
952 if (s
->esc_params
[0] == 0) {
953 s
->esc_params
[0] = 1;
955 s
->x
+= s
->esc_params
[0];
956 if (s
->x
>= s
->width
) {
961 /* move cursor left */
962 if (s
->esc_params
[0] == 0) {
963 s
->esc_params
[0] = 1;
965 s
->x
-= s
->esc_params
[0];
971 /* move cursor to column */
972 s
->x
= s
->esc_params
[0] - 1;
979 /* move cursor to row, column */
980 s
->x
= s
->esc_params
[1] - 1;
984 s
->y
= s
->esc_params
[0] - 1;
990 switch (s
->esc_params
[0]) {
992 /* clear to end of screen */
993 for (y
= s
->y
; y
< s
->height
; y
++) {
994 for (x
= 0; x
< s
->width
; x
++) {
995 if (y
== s
->y
&& x
< s
->x
) {
998 console_clear_xy(s
, x
, y
);
1003 /* clear from beginning of screen */
1004 for (y
= 0; y
<= s
->y
; y
++) {
1005 for (x
= 0; x
< s
->width
; x
++) {
1006 if (y
== s
->y
&& x
> s
->x
) {
1009 console_clear_xy(s
, x
, y
);
1014 /* clear entire screen */
1015 for (y
= 0; y
<= s
->height
; y
++) {
1016 for (x
= 0; x
< s
->width
; x
++) {
1017 console_clear_xy(s
, x
, y
);
1024 switch (s
->esc_params
[0]) {
1027 for(x
= s
->x
; x
< s
->width
; x
++) {
1028 console_clear_xy(s
, x
, s
->y
);
1032 /* clear from beginning of line */
1033 for (x
= 0; x
<= s
->x
; x
++) {
1034 console_clear_xy(s
, x
, s
->y
);
1038 /* clear entire line */
1039 for(x
= 0; x
< s
->width
; x
++) {
1040 console_clear_xy(s
, x
, s
->y
);
1046 console_handle_escape(s
);
1049 /* report cursor position */
1050 /* TODO: send ESC[row;colR */
1053 /* save cursor position */
1058 /* restore cursor position */
1063 #ifdef DEBUG_CONSOLE
1064 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1073 void console_select(unsigned int index
)
1077 if (index
>= MAX_CONSOLES
)
1079 if (active_console
) {
1080 active_console
->g_width
= ds_get_width(active_console
->ds
);
1081 active_console
->g_height
= ds_get_height(active_console
->ds
);
1083 s
= consoles
[index
];
1085 DisplayState
*ds
= s
->ds
;
1087 if (ds_get_bits_per_pixel(s
->ds
)) {
1088 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1090 s
->ds
->surface
->width
= s
->width
;
1091 s
->ds
->surface
->height
= s
->height
;
1094 vga_hw_invalidate();
1098 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1100 TextConsole
*s
= chr
->opaque
;
1103 s
->update_x0
= s
->width
* FONT_WIDTH
;
1104 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1107 console_show_cursor(s
, 0);
1108 for(i
= 0; i
< len
; i
++) {
1109 console_putchar(s
, buf
[i
]);
1111 console_show_cursor(s
, 1);
1112 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1113 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1114 s
->update_x1
- s
->update_x0
,
1115 s
->update_y1
- s
->update_y0
);
1120 static void kbd_send_chars(void *opaque
)
1122 TextConsole
*s
= opaque
;
1126 len
= qemu_chr_be_can_write(s
->chr
);
1127 if (len
> s
->out_fifo
.count
)
1128 len
= s
->out_fifo
.count
;
1130 if (len
> sizeof(buf
))
1132 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1133 qemu_chr_be_write(s
->chr
, buf
, len
);
1135 /* characters are pending: we send them a bit later (XXX:
1136 horrible, should change char device API) */
1137 if (s
->out_fifo
.count
> 0) {
1138 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
1142 /* called when an ascii key is pressed */
1143 void kbd_put_keysym(int keysym
)
1146 uint8_t buf
[16], *q
;
1150 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1154 case QEMU_KEY_CTRL_UP
:
1157 case QEMU_KEY_CTRL_DOWN
:
1160 case QEMU_KEY_CTRL_PAGEUP
:
1161 console_scroll(-10);
1163 case QEMU_KEY_CTRL_PAGEDOWN
:
1167 /* convert the QEMU keysym to VT100 key string */
1169 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1172 c
= keysym
- 0xe100;
1174 *q
++ = '0' + (c
/ 10);
1175 *q
++ = '0' + (c
% 10);
1177 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1180 *q
++ = keysym
& 0xff;
1181 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1182 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1188 console_puts(s
->chr
, buf
, q
- buf
);
1190 if (s
->chr
->chr_read
) {
1191 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1198 static void text_console_invalidate(void *opaque
)
1200 TextConsole
*s
= (TextConsole
*) opaque
;
1201 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1202 s
->g_width
= ds_get_width(s
->ds
);
1203 s
->g_height
= ds_get_height(s
->ds
);
1204 text_console_resize(s
);
1209 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1211 TextConsole
*s
= (TextConsole
*) opaque
;
1214 if (s
->text_x
[0] <= s
->text_x
[1]) {
1215 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1216 chardata
+= s
->text_y
[0] * s
->width
;
1217 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1218 for (j
= 0; j
< s
->width
; j
++, src
++)
1219 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1220 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1221 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1222 (s
->cells
[src
].t_attrib
.bold
<< 21));
1223 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1224 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1225 s
->text_x
[0] = s
->width
;
1226 s
->text_y
[0] = s
->height
;
1230 if (s
->cursor_invalidate
) {
1231 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1232 s
->cursor_invalidate
= 0;
1236 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1240 for (i
= 0; i
< nb_consoles
; i
++) {
1242 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1248 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1253 if (nb_consoles
>= MAX_CONSOLES
)
1255 s
= g_malloc0(sizeof(TextConsole
));
1256 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1257 (console_type
== GRAPHIC_CONSOLE
))) {
1261 s
->console_type
= console_type
;
1262 if (console_type
!= GRAPHIC_CONSOLE
) {
1263 s
->index
= nb_consoles
;
1264 consoles
[nb_consoles
++] = s
;
1266 /* HACK: Put graphical consoles before text consoles. */
1267 for (i
= nb_consoles
; i
> 0; i
--) {
1268 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1270 consoles
[i
] = consoles
[i
- 1];
1271 consoles
[i
]->index
= i
;
1280 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1282 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1284 int linesize
= width
* 4;
1285 qemu_alloc_display(surface
, width
, height
, linesize
,
1286 qemu_default_pixelformat(32), 0);
1290 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1291 int width
, int height
)
1293 int linesize
= width
* 4;
1294 qemu_alloc_display(surface
, width
, height
, linesize
,
1295 qemu_default_pixelformat(32), 0);
1299 void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1300 int linesize
, PixelFormat pf
, int newflags
)
1303 surface
->width
= width
;
1304 surface
->height
= height
;
1305 surface
->linesize
= linesize
;
1307 if (surface
->flags
& QEMU_ALLOCATED_FLAG
) {
1308 data
= g_realloc(surface
->data
,
1309 surface
->linesize
* surface
->height
);
1311 data
= g_malloc(surface
->linesize
* surface
->height
);
1313 surface
->data
= (uint8_t *)data
;
1314 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1315 #ifdef HOST_WORDS_BIGENDIAN
1316 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1320 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1321 int linesize
, uint8_t *data
)
1323 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1325 surface
->width
= width
;
1326 surface
->height
= height
;
1327 surface
->linesize
= linesize
;
1328 surface
->pf
= qemu_default_pixelformat(bpp
);
1329 #ifdef HOST_WORDS_BIGENDIAN
1330 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1332 surface
->data
= data
;
1337 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1339 if (surface
== NULL
)
1341 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1342 g_free(surface
->data
);
1346 static struct DisplayAllocator default_allocator
= {
1347 defaultallocator_create_displaysurface
,
1348 defaultallocator_resize_displaysurface
,
1349 defaultallocator_free_displaysurface
1352 static void dumb_display_init(void)
1354 DisplayState
*ds
= g_malloc0(sizeof(DisplayState
));
1358 ds
->allocator
= &default_allocator
;
1359 if (is_fixedsize_console()) {
1360 width
= active_console
->g_width
;
1361 height
= active_console
->g_height
;
1363 ds
->surface
= qemu_create_displaysurface(ds
, width
, height
);
1364 register_displaystate(ds
);
1367 /***********************************************************/
1368 /* register display */
1370 void register_displaystate(DisplayState
*ds
)
1380 DisplayState
*get_displaystate(void)
1382 if (!display_state
) {
1383 dumb_display_init ();
1385 return display_state
;
1388 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1390 if(ds
->allocator
== &default_allocator
) {
1391 DisplaySurface
*surf
;
1392 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1393 defaultallocator_free_displaysurface(ds
->surface
);
1397 return ds
->allocator
;
1400 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1401 vga_hw_invalidate_ptr invalidate
,
1402 vga_hw_screen_dump_ptr screen_dump
,
1403 vga_hw_text_update_ptr text_update
,
1409 ds
= (DisplayState
*) g_malloc0(sizeof(DisplayState
));
1410 ds
->allocator
= &default_allocator
;
1411 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1413 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1415 qemu_free_displaysurface(ds
);
1419 s
->hw_update
= update
;
1420 s
->hw_invalidate
= invalidate
;
1421 s
->hw_screen_dump
= screen_dump
;
1422 s
->hw_text_update
= text_update
;
1425 register_displaystate(ds
);
1429 int is_graphic_console(void)
1431 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1434 int is_fixedsize_console(void)
1436 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1439 void console_color_init(DisplayState
*ds
)
1442 for (j
= 0; j
< 2; j
++) {
1443 for (i
= 0; i
< 8; i
++) {
1444 color_table
[j
][i
] = col_expand(ds
,
1445 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1450 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1452 TextConsole
*s
= chr
->opaque
;
1457 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1460 static int color_inited
;
1464 chr
->chr_write
= console_puts
;
1466 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1467 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1468 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1471 if (!color_inited
) {
1473 console_color_init(s
->ds
);
1477 s
->total_height
= DEFAULT_BACKSCROLL
;
1480 if (s
->console_type
== TEXT_CONSOLE
) {
1481 s
->g_width
= ds_get_width(s
->ds
);
1482 s
->g_height
= ds_get_height(s
->ds
);
1485 s
->hw_invalidate
= text_console_invalidate
;
1486 s
->hw_text_update
= text_console_update
;
1489 /* Set text attribute defaults */
1490 s
->t_attrib_default
.bold
= 0;
1491 s
->t_attrib_default
.uline
= 0;
1492 s
->t_attrib_default
.blink
= 0;
1493 s
->t_attrib_default
.invers
= 0;
1494 s
->t_attrib_default
.unvisible
= 0;
1495 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1496 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1497 /* set current text attributes to default */
1498 s
->t_attrib
= s
->t_attrib_default
;
1499 text_console_resize(s
);
1505 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1506 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1507 console_puts(chr
, (uint8_t*)msg
, len
);
1508 s
->t_attrib
= s
->t_attrib_default
;
1511 qemu_chr_generic_open(chr
);
1516 CharDriverState
*text_console_init(QemuOpts
*opts
)
1518 CharDriverState
*chr
;
1523 chr
= g_malloc0(sizeof(CharDriverState
));
1525 width
= qemu_opt_get_number(opts
, "width", 0);
1527 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1529 height
= qemu_opt_get_number(opts
, "height", 0);
1531 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1533 if (width
== 0 || height
== 0) {
1534 s
= new_console(NULL
, TEXT_CONSOLE
);
1536 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1546 s
->g_height
= height
;
1548 chr
->chr_set_echo
= text_console_set_echo
;
1552 void text_consoles_set_display(DisplayState
*ds
)
1556 for (i
= 0; i
< nb_consoles
; i
++) {
1557 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
) {
1558 text_console_do_init(consoles
[i
]->chr
, ds
);
1563 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1565 TextConsole
*s
= get_graphic_console(ds
);
1569 s
->g_height
= height
;
1570 if (is_graphic_console()) {
1571 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1576 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1577 int dst_x
, int dst_y
, int w
, int h
)
1579 if (is_graphic_console()) {
1580 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1584 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1588 memset(&pf
, 0x00, sizeof(PixelFormat
));
1590 pf
.bits_per_pixel
= bpp
;
1591 pf
.bytes_per_pixel
= bpp
/ 8;
1592 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1596 pf
.rmask
= 0x000000FF;
1597 pf
.gmask
= 0x0000FF00;
1598 pf
.bmask
= 0x00FF0000;
1610 pf
.rmask
= 0x0000FF00;
1611 pf
.gmask
= 0x00FF0000;
1612 pf
.bmask
= 0xFF000000;
1613 pf
.amask
= 0x00000000;
1633 PixelFormat
qemu_default_pixelformat(int bpp
)
1637 memset(&pf
, 0x00, sizeof(PixelFormat
));
1639 pf
.bits_per_pixel
= bpp
;
1640 pf
.bytes_per_pixel
= bpp
/ 8;
1641 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1645 pf
.bits_per_pixel
= 16;
1646 pf
.bytes_per_pixel
= 2;
1647 pf
.rmask
= 0x00007c00;
1648 pf
.gmask
= 0x000003E0;
1649 pf
.bmask
= 0x0000001F;
1661 pf
.rmask
= 0x0000F800;
1662 pf
.gmask
= 0x000007E0;
1663 pf
.bmask
= 0x0000001F;
1675 pf
.rmask
= 0x00FF0000;
1676 pf
.gmask
= 0x0000FF00;
1677 pf
.bmask
= 0x000000FF;
1689 pf
.rmask
= 0x00FF0000;
1690 pf
.gmask
= 0x0000FF00;
1691 pf
.bmask
= 0x000000FF;