2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
26 #include "qemu-timer.h"
28 //#define DEBUG_CONSOLE
29 #define DEFAULT_BACKSCROLL 512
30 #define MAX_CONSOLES 12
32 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
33 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
35 typedef struct TextAttributes
{
45 typedef struct TextCell
{
47 TextAttributes t_attrib
;
50 #define MAX_ESC_PARAMS 3
58 typedef struct QEMUFIFO
{
61 int count
, wptr
, rptr
;
64 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
68 l
= f
->buf_size
- f
->count
;
73 l
= f
->buf_size
- f
->wptr
;
76 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
78 if (f
->wptr
>= f
->buf_size
)
87 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
95 l
= f
->buf_size
- f
->rptr
;
98 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
100 if (f
->rptr
>= f
->buf_size
)
112 TEXT_CONSOLE_FIXED_SIZE
115 /* ??? This is mis-named.
116 It is used for both text and graphical consoles. */
118 console_type_t console_type
;
120 /* Graphic console state. */
121 vga_hw_update_ptr hw_update
;
122 vga_hw_invalidate_ptr hw_invalidate
;
123 vga_hw_screen_dump_ptr hw_screen_dump
;
124 vga_hw_text_update_ptr hw_text_update
;
127 int g_width
, g_height
;
131 int backscroll_height
;
133 int x_saved
, y_saved
;
136 TextAttributes t_attrib_default
; /* default text attributes */
137 TextAttributes t_attrib
; /* currently active text attributes */
139 int text_x
[2], text_y
[2], cursor_invalidate
;
148 int esc_params
[MAX_ESC_PARAMS
];
151 CharDriverState
*chr
;
152 /* fifo for key pressed */
154 uint8_t out_fifo_buf
[16];
155 QEMUTimer
*kbd_timer
;
158 static DisplayState
*display_state
;
159 static TextConsole
*active_console
;
160 static TextConsole
*consoles
[MAX_CONSOLES
];
161 static int nb_consoles
= 0;
163 void vga_hw_update(void)
165 if (active_console
&& active_console
->hw_update
)
166 active_console
->hw_update(active_console
->hw
);
169 void vga_hw_invalidate(void)
171 if (active_console
&& active_console
->hw_invalidate
)
172 active_console
->hw_invalidate(active_console
->hw
);
175 void vga_hw_screen_dump(const char *filename
)
177 TextConsole
*previous_active_console
;
179 previous_active_console
= active_console
;
180 active_console
= consoles
[0];
181 /* There is currently no way of specifying which screen we want to dump,
182 so always dump the first one. */
183 if (consoles
[0]->hw_screen_dump
)
184 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
185 active_console
= previous_active_console
;
188 void vga_hw_text_update(console_ch_t
*chardata
)
190 if (active_console
&& active_console
->hw_text_update
)
191 active_console
->hw_text_update(active_console
->hw
, chardata
);
194 /* convert a RGBA color to a color index usable in graphic primitives */
195 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
197 unsigned int r
, g
, b
, color
;
199 switch(ds_get_bits_per_pixel(ds
)) {
202 r
= (rgba
>> 16) & 0xff;
203 g
= (rgba
>> 8) & 0xff;
205 color
= (rgb_to_index
[r
] * 6 * 6) +
206 (rgb_to_index
[g
] * 6) +
211 r
= (rgba
>> 16) & 0xff;
212 g
= (rgba
>> 8) & 0xff;
214 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
217 r
= (rgba
>> 16) & 0xff;
218 g
= (rgba
>> 8) & 0xff;
220 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
230 static void vga_fill_rect (DisplayState
*ds
,
231 int posx
, int posy
, int width
, int height
, uint32_t color
)
236 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
237 d1
= ds_get_data(ds
) +
238 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
239 for (y
= 0; y
< height
; y
++) {
243 for (x
= 0; x
< width
; x
++) {
244 *((uint8_t *)d
) = color
;
249 for (x
= 0; x
< width
; x
++) {
250 *((uint16_t *)d
) = color
;
255 for (x
= 0; x
< width
; x
++) {
256 *((uint32_t *)d
) = color
;
261 d1
+= ds_get_linesize(ds
);
265 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
266 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
272 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
275 s
= ds_get_data(ds
) +
276 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
277 d
= ds_get_data(ds
) +
278 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
279 for (y
= 0; y
< h
; y
++) {
281 d
+= ds_get_linesize(ds
);
282 s
+= ds_get_linesize(ds
);
285 s
= ds_get_data(ds
) +
286 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
287 d
= ds_get_data(ds
) +
288 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
289 for (y
= 0; y
< h
; y
++) {
291 d
-= ds_get_linesize(ds
);
292 s
-= ds_get_linesize(ds
);
297 /***********************************************************/
298 /* basic char display */
300 #define FONT_HEIGHT 16
305 #define cbswap_32(__x) \
307 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
308 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
309 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
310 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
312 #ifdef HOST_WORDS_BIGENDIAN
315 #define PAT(x) cbswap_32(x)
318 static const uint32_t dmask16
[16] = {
337 static const uint32_t dmask4
[4] = {
344 static uint32_t color_table
[2][8];
357 static const uint32_t color_table_rgb
[2][8] = {
359 QEMU_RGB(0x00, 0x00, 0x00), /* black */
360 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
361 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
362 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
363 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
364 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
365 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
366 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
369 QEMU_RGB(0x00, 0x00, 0x00), /* black */
370 QEMU_RGB(0xff, 0x00, 0x00), /* red */
371 QEMU_RGB(0x00, 0xff, 0x00), /* green */
372 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
373 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
374 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
375 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
376 QEMU_RGB(0xff, 0xff, 0xff), /* white */
380 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
382 switch(ds_get_bits_per_pixel(ds
)) {
398 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
400 if (t_attrib
->bold
) {
405 if (t_attrib
->uline
) {
410 if (t_attrib
->blink
) {
415 if (t_attrib
->invers
) {
420 if (t_attrib
->unvisible
) {
426 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
430 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
431 TextAttributes
*t_attrib
)
434 const uint8_t *font_ptr
;
435 unsigned int font_data
, linesize
, xorcol
, bpp
;
437 unsigned int fgcol
, bgcol
;
440 printf("x: %2i y: %2i", x
, y
);
441 console_print_text_attributes(t_attrib
, ch
);
444 if (t_attrib
->invers
) {
445 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
446 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
448 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
449 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
452 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
453 d
= ds_get_data(ds
) +
454 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
455 linesize
= ds_get_linesize(ds
);
456 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
457 xorcol
= bgcol
^ fgcol
;
458 switch(ds_get_bits_per_pixel(ds
)) {
460 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
461 font_data
= *font_ptr
++;
463 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
466 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
467 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
473 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
474 font_data
= *font_ptr
++;
476 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
479 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
480 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
481 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
482 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
487 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
488 font_data
= *font_ptr
++;
489 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
492 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
493 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
494 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
495 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
496 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
497 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
498 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
499 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
506 static void text_console_resize(TextConsole
*s
)
508 TextCell
*cells
, *c
, *c1
;
509 int w1
, x
, y
, last_width
;
511 last_width
= s
->width
;
512 s
->width
= s
->g_width
/ FONT_WIDTH
;
513 s
->height
= s
->g_height
/ FONT_HEIGHT
;
519 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
520 for(y
= 0; y
< s
->total_height
; y
++) {
521 c
= &cells
[y
* s
->width
];
523 c1
= &s
->cells
[y
* last_width
];
524 for(x
= 0; x
< w1
; x
++) {
528 for(x
= w1
; x
< s
->width
; x
++) {
530 c
->t_attrib
= s
->t_attrib_default
;
538 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
540 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
541 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
542 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
543 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
546 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
548 if (s
->update_x0
> x
* FONT_WIDTH
)
549 s
->update_x0
= x
* FONT_WIDTH
;
550 if (s
->update_y0
> y
* FONT_HEIGHT
)
551 s
->update_y0
= y
* FONT_HEIGHT
;
552 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
553 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
554 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
555 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
558 static void update_xy(TextConsole
*s
, int x
, int y
)
563 if (s
== active_console
) {
564 if (!ds_get_bits_per_pixel(s
->ds
)) {
565 text_update_xy(s
, x
, y
);
569 y1
= (s
->y_base
+ y
) % s
->total_height
;
570 y2
= y1
- s
->y_displayed
;
572 y2
+= s
->total_height
;
573 if (y2
< s
->height
) {
574 c
= &s
->cells
[y1
* s
->width
+ x
];
575 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
577 invalidate_xy(s
, x
, y2
);
582 static void console_show_cursor(TextConsole
*s
, int show
)
587 if (s
== active_console
) {
590 if (!ds_get_bits_per_pixel(s
->ds
)) {
591 s
->cursor_invalidate
= 1;
598 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
599 y
= y1
- s
->y_displayed
;
601 y
+= s
->total_height
;
603 c
= &s
->cells
[y1
* s
->width
+ x
];
605 TextAttributes t_attrib
= s
->t_attrib_default
;
606 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
607 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
609 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
611 invalidate_xy(s
, x
, y
);
616 static void console_refresh(TextConsole
*s
)
621 if (s
!= active_console
)
623 if (!ds_get_bits_per_pixel(s
->ds
)) {
626 s
->text_x
[1] = s
->width
- 1;
627 s
->text_y
[1] = s
->height
- 1;
628 s
->cursor_invalidate
= 1;
632 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
633 color_table
[0][COLOR_BLACK
]);
635 for(y
= 0; y
< s
->height
; y
++) {
636 c
= s
->cells
+ y1
* s
->width
;
637 for(x
= 0; x
< s
->width
; x
++) {
638 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
642 if (++y1
== s
->total_height
)
645 console_show_cursor(s
, 1);
646 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
649 static void console_scroll(int ydelta
)
655 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
659 for(i
= 0; i
< ydelta
; i
++) {
660 if (s
->y_displayed
== s
->y_base
)
662 if (++s
->y_displayed
== s
->total_height
)
667 i
= s
->backscroll_height
;
668 if (i
> s
->total_height
- s
->height
)
669 i
= s
->total_height
- s
->height
;
672 y1
+= s
->total_height
;
673 for(i
= 0; i
< ydelta
; i
++) {
674 if (s
->y_displayed
== y1
)
676 if (--s
->y_displayed
< 0)
677 s
->y_displayed
= s
->total_height
- 1;
683 static void console_put_lf(TextConsole
*s
)
689 if (s
->y
>= s
->height
) {
690 s
->y
= s
->height
- 1;
692 if (s
->y_displayed
== s
->y_base
) {
693 if (++s
->y_displayed
== s
->total_height
)
696 if (++s
->y_base
== s
->total_height
)
698 if (s
->backscroll_height
< s
->total_height
)
699 s
->backscroll_height
++;
700 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
701 c
= &s
->cells
[y1
* s
->width
];
702 for(x
= 0; x
< s
->width
; x
++) {
704 c
->t_attrib
= s
->t_attrib_default
;
707 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
708 if (!ds_get_bits_per_pixel(s
->ds
)) {
711 s
->text_x
[1] = s
->width
- 1;
712 s
->text_y
[1] = s
->height
- 1;
716 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
717 s
->width
* FONT_WIDTH
,
718 (s
->height
- 1) * FONT_HEIGHT
);
719 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
720 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
721 color_table
[0][s
->t_attrib_default
.bgcol
]);
724 s
->update_x1
= s
->width
* FONT_WIDTH
;
725 s
->update_y1
= s
->height
* FONT_HEIGHT
;
730 /* Set console attributes depending on the current escape codes.
731 * NOTE: I know this code is not very efficient (checking every color for it
732 * self) but it is more readable and better maintainable.
734 static void console_handle_escape(TextConsole
*s
)
738 for (i
=0; i
<s
->nb_esc_params
; i
++) {
739 switch (s
->esc_params
[i
]) {
740 case 0: /* reset all console attributes to default */
741 s
->t_attrib
= s
->t_attrib_default
;
744 s
->t_attrib
.bold
= 1;
747 s
->t_attrib
.uline
= 1;
750 s
->t_attrib
.blink
= 1;
753 s
->t_attrib
.invers
= 1;
756 s
->t_attrib
.unvisible
= 1;
759 s
->t_attrib
.bold
= 0;
762 s
->t_attrib
.uline
= 0;
765 s
->t_attrib
.blink
= 0;
768 s
->t_attrib
.invers
= 0;
771 s
->t_attrib
.unvisible
= 0;
773 /* set foreground color */
775 s
->t_attrib
.fgcol
=COLOR_BLACK
;
778 s
->t_attrib
.fgcol
=COLOR_RED
;
781 s
->t_attrib
.fgcol
=COLOR_GREEN
;
784 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
787 s
->t_attrib
.fgcol
=COLOR_BLUE
;
790 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
793 s
->t_attrib
.fgcol
=COLOR_CYAN
;
796 s
->t_attrib
.fgcol
=COLOR_WHITE
;
798 /* set background color */
800 s
->t_attrib
.bgcol
=COLOR_BLACK
;
803 s
->t_attrib
.bgcol
=COLOR_RED
;
806 s
->t_attrib
.bgcol
=COLOR_GREEN
;
809 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
812 s
->t_attrib
.bgcol
=COLOR_BLUE
;
815 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
818 s
->t_attrib
.bgcol
=COLOR_CYAN
;
821 s
->t_attrib
.bgcol
=COLOR_WHITE
;
827 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
829 int y1
= (s
->y_base
+ y
) % s
->total_height
;
830 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
832 c
->t_attrib
= s
->t_attrib_default
;
836 static void console_putchar(TextConsole
*s
, int ch
)
845 case '\r': /* carriage return */
848 case '\n': /* newline */
851 case '\b': /* backspace */
855 case '\t': /* tabspace */
856 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
860 s
->x
= s
->x
+ (8 - (s
->x
% 8));
863 case '\a': /* alert aka. bell */
864 /* TODO: has to be implemented */
867 /* SI (shift in), character set 0 (ignored) */
870 /* SO (shift out), character set 1 (ignored) */
872 case 27: /* esc (introducing an escape sequence) */
873 s
->state
= TTY_STATE_ESC
;
876 if (s
->x
>= s
->width
) {
881 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
882 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
884 c
->t_attrib
= s
->t_attrib
;
885 update_xy(s
, s
->x
, s
->y
);
890 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
892 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
893 s
->esc_params
[i
] = 0;
894 s
->nb_esc_params
= 0;
895 s
->state
= TTY_STATE_CSI
;
897 s
->state
= TTY_STATE_NORM
;
900 case TTY_STATE_CSI
: /* handle escape sequence parameters */
901 if (ch
>= '0' && ch
<= '9') {
902 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
903 s
->esc_params
[s
->nb_esc_params
] =
904 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
911 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
912 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
914 s
->state
= TTY_STATE_NORM
;
918 if (s
->esc_params
[0] == 0) {
919 s
->esc_params
[0] = 1;
921 s
->y
-= s
->esc_params
[0];
927 /* move cursor down */
928 if (s
->esc_params
[0] == 0) {
929 s
->esc_params
[0] = 1;
931 s
->y
+= s
->esc_params
[0];
932 if (s
->y
>= s
->height
) {
933 s
->y
= s
->height
- 1;
937 /* move cursor right */
938 if (s
->esc_params
[0] == 0) {
939 s
->esc_params
[0] = 1;
941 s
->x
+= s
->esc_params
[0];
942 if (s
->x
>= s
->width
) {
947 /* move cursor left */
948 if (s
->esc_params
[0] == 0) {
949 s
->esc_params
[0] = 1;
951 s
->x
-= s
->esc_params
[0];
957 /* move cursor to column */
958 s
->x
= s
->esc_params
[0] - 1;
965 /* move cursor to row, column */
966 s
->x
= s
->esc_params
[1] - 1;
970 s
->y
= s
->esc_params
[0] - 1;
976 switch (s
->esc_params
[0]) {
978 /* clear to end of screen */
979 for (y
= s
->y
; y
< s
->height
; y
++) {
980 for (x
= 0; x
< s
->width
; x
++) {
981 if (y
== s
->y
&& x
< s
->x
) {
984 console_clear_xy(s
, x
, y
);
989 /* clear from beginning of screen */
990 for (y
= 0; y
<= s
->y
; y
++) {
991 for (x
= 0; x
< s
->width
; x
++) {
992 if (y
== s
->y
&& x
> s
->x
) {
995 console_clear_xy(s
, x
, y
);
1000 /* clear entire screen */
1001 for (y
= 0; y
<= s
->height
; y
++) {
1002 for (x
= 0; x
< s
->width
; x
++) {
1003 console_clear_xy(s
, x
, y
);
1009 switch (s
->esc_params
[0]) {
1012 for(x
= s
->x
; x
< s
->width
; x
++) {
1013 console_clear_xy(s
, x
, s
->y
);
1017 /* clear from beginning of line */
1018 for (x
= 0; x
<= s
->x
; x
++) {
1019 console_clear_xy(s
, x
, s
->y
);
1023 /* clear entire line */
1024 for(x
= 0; x
< s
->width
; x
++) {
1025 console_clear_xy(s
, x
, s
->y
);
1031 console_handle_escape(s
);
1034 /* report cursor position */
1035 /* TODO: send ESC[row;colR */
1038 /* save cursor position */
1043 /* restore cursor position */
1048 #ifdef DEBUG_CONSOLE
1049 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1058 void console_select(unsigned int index
)
1062 if (index
>= MAX_CONSOLES
)
1064 if (active_console
) {
1065 active_console
->g_width
= ds_get_width(active_console
->ds
);
1066 active_console
->g_height
= ds_get_height(active_console
->ds
);
1068 s
= consoles
[index
];
1070 DisplayState
*ds
= s
->ds
;
1072 if (ds_get_bits_per_pixel(s
->ds
)) {
1073 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1075 s
->ds
->surface
->width
= s
->width
;
1076 s
->ds
->surface
->height
= s
->height
;
1079 vga_hw_invalidate();
1083 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1085 TextConsole
*s
= chr
->opaque
;
1088 s
->update_x0
= s
->width
* FONT_WIDTH
;
1089 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1092 console_show_cursor(s
, 0);
1093 for(i
= 0; i
< len
; i
++) {
1094 console_putchar(s
, buf
[i
]);
1096 console_show_cursor(s
, 1);
1097 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1098 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1099 s
->update_x1
- s
->update_x0
,
1100 s
->update_y1
- s
->update_y0
);
1105 static void console_send_event(CharDriverState
*chr
, int event
)
1107 TextConsole
*s
= chr
->opaque
;
1110 if (event
== CHR_EVENT_FOCUS
) {
1111 for(i
= 0; i
< nb_consoles
; i
++) {
1112 if (consoles
[i
] == s
) {
1120 static void kbd_send_chars(void *opaque
)
1122 TextConsole
*s
= opaque
;
1126 len
= qemu_chr_can_read(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_read(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
= qemu_mallocz(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 consoles
[nb_consoles
++] = s
;
1265 /* HACK: Put graphical consoles before text consoles. */
1266 for (i
= nb_consoles
; i
> 0; i
--) {
1267 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1269 consoles
[i
] = consoles
[i
- 1];
1277 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1279 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1281 int linesize
= width
* 4;
1282 qemu_alloc_display(surface
, width
, height
, linesize
,
1283 qemu_default_pixelformat(32), 0);
1287 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1288 int width
, int height
)
1290 int linesize
= width
* 4;
1291 qemu_alloc_display(surface
, width
, height
, linesize
,
1292 qemu_default_pixelformat(32), 0);
1296 void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1297 int linesize
, PixelFormat pf
, int newflags
)
1300 surface
->width
= width
;
1301 surface
->height
= height
;
1302 surface
->linesize
= linesize
;
1304 if (surface
->flags
& QEMU_ALLOCATED_FLAG
) {
1305 data
= qemu_realloc(surface
->data
,
1306 surface
->linesize
* surface
->height
);
1308 data
= qemu_malloc(surface
->linesize
* surface
->height
);
1310 surface
->data
= (uint8_t *)data
;
1311 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1312 #ifdef HOST_WORDS_BIGENDIAN
1313 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1317 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1318 int linesize
, uint8_t *data
)
1320 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1322 surface
->width
= width
;
1323 surface
->height
= height
;
1324 surface
->linesize
= linesize
;
1325 surface
->pf
= qemu_default_pixelformat(bpp
);
1326 #ifdef HOST_WORDS_BIGENDIAN
1327 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1329 surface
->data
= data
;
1334 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1336 if (surface
== NULL
)
1338 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1339 qemu_free(surface
->data
);
1343 static struct DisplayAllocator default_allocator
= {
1344 defaultallocator_create_displaysurface
,
1345 defaultallocator_resize_displaysurface
,
1346 defaultallocator_free_displaysurface
1349 static void dumb_display_init(void)
1351 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
1352 ds
->allocator
= &default_allocator
;
1353 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1354 register_displaystate(ds
);
1357 /***********************************************************/
1358 /* register display */
1360 void register_displaystate(DisplayState
*ds
)
1370 DisplayState
*get_displaystate(void)
1372 if (!display_state
) {
1373 dumb_display_init ();
1375 return display_state
;
1378 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1380 if(ds
->allocator
== &default_allocator
) {
1381 DisplaySurface
*surf
;
1382 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1383 defaultallocator_free_displaysurface(ds
->surface
);
1387 return ds
->allocator
;
1390 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1391 vga_hw_invalidate_ptr invalidate
,
1392 vga_hw_screen_dump_ptr screen_dump
,
1393 vga_hw_text_update_ptr text_update
,
1399 ds
= (DisplayState
*) qemu_mallocz(sizeof(DisplayState
));
1400 ds
->allocator
= &default_allocator
;
1401 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1403 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1405 qemu_free_displaysurface(ds
);
1409 s
->hw_update
= update
;
1410 s
->hw_invalidate
= invalidate
;
1411 s
->hw_screen_dump
= screen_dump
;
1412 s
->hw_text_update
= text_update
;
1415 register_displaystate(ds
);
1419 int is_graphic_console(void)
1421 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1424 int is_fixedsize_console(void)
1426 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1429 void console_color_init(DisplayState
*ds
)
1432 for (j
= 0; j
< 2; j
++) {
1433 for (i
= 0; i
< 8; i
++) {
1434 color_table
[j
][i
] = col_expand(ds
,
1435 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1440 static int n_text_consoles
;
1441 static CharDriverState
*text_consoles
[128];
1443 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1445 TextConsole
*s
= chr
->opaque
;
1450 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1453 static int color_inited
;
1457 chr
->chr_write
= console_puts
;
1458 chr
->chr_send_event
= console_send_event
;
1460 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1461 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1462 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1465 if (!color_inited
) {
1467 console_color_init(s
->ds
);
1471 s
->total_height
= DEFAULT_BACKSCROLL
;
1474 if (s
->console_type
== TEXT_CONSOLE
) {
1475 s
->g_width
= ds_get_width(s
->ds
);
1476 s
->g_height
= ds_get_height(s
->ds
);
1479 s
->hw_invalidate
= text_console_invalidate
;
1480 s
->hw_text_update
= text_console_update
;
1483 /* Set text attribute defaults */
1484 s
->t_attrib_default
.bold
= 0;
1485 s
->t_attrib_default
.uline
= 0;
1486 s
->t_attrib_default
.blink
= 0;
1487 s
->t_attrib_default
.invers
= 0;
1488 s
->t_attrib_default
.unvisible
= 0;
1489 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1490 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1491 /* set current text attributes to default */
1492 s
->t_attrib
= s
->t_attrib_default
;
1493 text_console_resize(s
);
1499 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1500 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1501 console_puts(chr
, (uint8_t*)msg
, len
);
1502 s
->t_attrib
= s
->t_attrib_default
;
1505 qemu_chr_generic_open(chr
);
1510 CharDriverState
*text_console_init(QemuOpts
*opts
)
1512 CharDriverState
*chr
;
1517 chr
= qemu_mallocz(sizeof(CharDriverState
));
1519 if (n_text_consoles
== 128) {
1520 fprintf(stderr
, "Too many text consoles\n");
1523 text_consoles
[n_text_consoles
] = chr
;
1526 width
= qemu_opt_get_number(opts
, "width", 0);
1528 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1530 height
= qemu_opt_get_number(opts
, "height", 0);
1532 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1534 if (width
== 0 || height
== 0) {
1535 s
= new_console(NULL
, TEXT_CONSOLE
);
1537 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1547 s
->g_height
= height
;
1549 chr
->chr_set_echo
= text_console_set_echo
;
1553 void text_consoles_set_display(DisplayState
*ds
)
1557 for (i
= 0; i
< n_text_consoles
; i
++) {
1558 text_console_do_init(text_consoles
[i
], ds
);
1561 n_text_consoles
= 0;
1564 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1566 TextConsole
*s
= get_graphic_console(ds
);
1570 s
->g_height
= height
;
1571 if (is_graphic_console()) {
1572 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1577 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1578 int dst_x
, int dst_y
, int w
, int h
)
1580 if (is_graphic_console()) {
1581 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1585 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1589 memset(&pf
, 0x00, sizeof(PixelFormat
));
1591 pf
.bits_per_pixel
= bpp
;
1592 pf
.bytes_per_pixel
= bpp
/ 8;
1593 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1597 pf
.rmask
= 0x000000FF;
1598 pf
.gmask
= 0x0000FF00;
1599 pf
.bmask
= 0x00FF0000;
1611 pf
.rmask
= 0x0000FF00;
1612 pf
.gmask
= 0x00FF0000;
1613 pf
.bmask
= 0xFF000000;
1614 pf
.amask
= 0x00000000;
1634 PixelFormat
qemu_default_pixelformat(int bpp
)
1638 memset(&pf
, 0x00, sizeof(PixelFormat
));
1640 pf
.bits_per_pixel
= bpp
;
1641 pf
.bytes_per_pixel
= bpp
/ 8;
1642 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1646 pf
.bits_per_pixel
= 16;
1647 pf
.bytes_per_pixel
= 2;
1648 pf
.rmask
= 0x00007c00;
1649 pf
.gmask
= 0x000003E0;
1650 pf
.bmask
= 0x0000001F;
1662 pf
.rmask
= 0x0000F800;
1663 pf
.gmask
= 0x000007E0;
1664 pf
.bmask
= 0x0000001F;
1676 pf
.rmask
= 0x00FF0000;
1677 pf
.gmask
= 0x0000FF00;
1678 pf
.bmask
= 0x000000FF;
1689 pf
.rmask
= 0x00FF0000;
1690 pf
.gmask
= 0x0000FF00;
1691 pf
.bmask
= 0x000000FF;