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
;
147 int esc_params
[MAX_ESC_PARAMS
];
150 CharDriverState
*chr
;
151 /* fifo for key pressed */
153 uint8_t out_fifo_buf
[16];
154 QEMUTimer
*kbd_timer
;
157 static DisplayState
*display_state
;
158 static TextConsole
*active_console
;
159 static TextConsole
*consoles
[MAX_CONSOLES
];
160 static int nb_consoles
= 0;
162 void vga_hw_update(void)
164 if (active_console
&& active_console
->hw_update
)
165 active_console
->hw_update(active_console
->hw
);
168 void vga_hw_invalidate(void)
170 if (active_console
->hw_invalidate
)
171 active_console
->hw_invalidate(active_console
->hw
);
174 void vga_hw_screen_dump(const char *filename
)
176 TextConsole
*previous_active_console
;
178 previous_active_console
= active_console
;
179 active_console
= consoles
[0];
180 /* There is currently no way of specifying which screen we want to dump,
181 so always dump the first one. */
182 if (consoles
[0]->hw_screen_dump
)
183 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
184 active_console
= previous_active_console
;
187 void vga_hw_text_update(console_ch_t
*chardata
)
189 if (active_console
&& active_console
->hw_text_update
)
190 active_console
->hw_text_update(active_console
->hw
, chardata
);
193 /* convert a RGBA color to a color index usable in graphic primitives */
194 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
196 unsigned int r
, g
, b
, color
;
198 switch(ds_get_bits_per_pixel(ds
)) {
201 r
= (rgba
>> 16) & 0xff;
202 g
= (rgba
>> 8) & 0xff;
204 color
= (rgb_to_index
[r
] * 6 * 6) +
205 (rgb_to_index
[g
] * 6) +
210 r
= (rgba
>> 16) & 0xff;
211 g
= (rgba
>> 8) & 0xff;
213 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
216 r
= (rgba
>> 16) & 0xff;
217 g
= (rgba
>> 8) & 0xff;
219 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
229 static void vga_fill_rect (DisplayState
*ds
,
230 int posx
, int posy
, int width
, int height
, uint32_t color
)
235 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
236 d1
= ds_get_data(ds
) +
237 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
238 for (y
= 0; y
< height
; y
++) {
242 for (x
= 0; x
< width
; x
++) {
243 *((uint8_t *)d
) = color
;
248 for (x
= 0; x
< width
; x
++) {
249 *((uint16_t *)d
) = color
;
254 for (x
= 0; x
< width
; x
++) {
255 *((uint32_t *)d
) = color
;
260 d1
+= ds_get_linesize(ds
);
264 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
265 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
271 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
274 s
= ds_get_data(ds
) +
275 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
276 d
= ds_get_data(ds
) +
277 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
278 for (y
= 0; y
< h
; y
++) {
280 d
+= ds_get_linesize(ds
);
281 s
+= ds_get_linesize(ds
);
284 s
= ds_get_data(ds
) +
285 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
286 d
= ds_get_data(ds
) +
287 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
288 for (y
= 0; y
< h
; y
++) {
290 d
-= ds_get_linesize(ds
);
291 s
-= ds_get_linesize(ds
);
296 /***********************************************************/
297 /* basic char display */
299 #define FONT_HEIGHT 16
304 #define cbswap_32(__x) \
306 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
307 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
308 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
309 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
311 #ifdef HOST_WORDS_BIGENDIAN
314 #define PAT(x) cbswap_32(x)
317 static const uint32_t dmask16
[16] = {
336 static const uint32_t dmask4
[4] = {
343 static uint32_t color_table
[2][8];
356 static const uint32_t color_table_rgb
[2][8] = {
358 QEMU_RGB(0x00, 0x00, 0x00), /* black */
359 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
360 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
361 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
362 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
363 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
364 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
365 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
368 QEMU_RGB(0x00, 0x00, 0x00), /* black */
369 QEMU_RGB(0xff, 0x00, 0x00), /* red */
370 QEMU_RGB(0x00, 0xff, 0x00), /* green */
371 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
372 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
373 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
374 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
375 QEMU_RGB(0xff, 0xff, 0xff), /* white */
379 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
381 switch(ds_get_bits_per_pixel(ds
)) {
397 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
399 if (t_attrib
->bold
) {
404 if (t_attrib
->uline
) {
409 if (t_attrib
->blink
) {
414 if (t_attrib
->invers
) {
419 if (t_attrib
->unvisible
) {
425 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
429 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
430 TextAttributes
*t_attrib
)
433 const uint8_t *font_ptr
;
434 unsigned int font_data
, linesize
, xorcol
, bpp
;
436 unsigned int fgcol
, bgcol
;
439 printf("x: %2i y: %2i", x
, y
);
440 console_print_text_attributes(t_attrib
, ch
);
443 if (t_attrib
->invers
) {
444 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
445 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
447 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
448 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
451 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
452 d
= ds_get_data(ds
) +
453 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
454 linesize
= ds_get_linesize(ds
);
455 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
456 xorcol
= bgcol
^ fgcol
;
457 switch(ds_get_bits_per_pixel(ds
)) {
459 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
460 font_data
= *font_ptr
++;
462 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
465 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
466 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
472 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
473 font_data
= *font_ptr
++;
475 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
478 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
479 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
480 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
481 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
486 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
487 font_data
= *font_ptr
++;
488 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
491 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
492 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
493 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
494 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
495 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
496 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
497 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
498 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
505 static void text_console_resize(TextConsole
*s
)
507 TextCell
*cells
, *c
, *c1
;
508 int w1
, x
, y
, last_width
;
510 last_width
= s
->width
;
511 s
->width
= s
->g_width
/ FONT_WIDTH
;
512 s
->height
= s
->g_height
/ FONT_HEIGHT
;
518 cells
= qemu_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
519 for(y
= 0; y
< s
->total_height
; y
++) {
520 c
= &cells
[y
* s
->width
];
522 c1
= &s
->cells
[y
* last_width
];
523 for(x
= 0; x
< w1
; x
++) {
527 for(x
= w1
; x
< s
->width
; x
++) {
529 c
->t_attrib
= s
->t_attrib_default
;
537 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
539 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
540 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
541 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
542 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
545 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
547 if (s
->update_x0
> x
* FONT_WIDTH
)
548 s
->update_x0
= x
* FONT_WIDTH
;
549 if (s
->update_y0
> y
* FONT_HEIGHT
)
550 s
->update_y0
= y
* FONT_HEIGHT
;
551 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
552 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
553 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
554 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
557 static void update_xy(TextConsole
*s
, int x
, int y
)
562 if (s
== active_console
) {
563 if (!ds_get_bits_per_pixel(s
->ds
)) {
564 text_update_xy(s
, x
, y
);
568 y1
= (s
->y_base
+ y
) % s
->total_height
;
569 y2
= y1
- s
->y_displayed
;
571 y2
+= s
->total_height
;
572 if (y2
< s
->height
) {
573 c
= &s
->cells
[y1
* s
->width
+ x
];
574 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
576 invalidate_xy(s
, x
, y2
);
581 static void console_show_cursor(TextConsole
*s
, int show
)
586 if (s
== active_console
) {
589 if (!ds_get_bits_per_pixel(s
->ds
)) {
590 s
->cursor_invalidate
= 1;
597 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
598 y
= y1
- s
->y_displayed
;
600 y
+= s
->total_height
;
602 c
= &s
->cells
[y1
* s
->width
+ x
];
604 TextAttributes t_attrib
= s
->t_attrib_default
;
605 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
606 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
608 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
610 invalidate_xy(s
, x
, y
);
615 static void console_refresh(TextConsole
*s
)
620 if (s
!= active_console
)
622 if (!ds_get_bits_per_pixel(s
->ds
)) {
625 s
->text_x
[1] = s
->width
- 1;
626 s
->text_y
[1] = s
->height
- 1;
627 s
->cursor_invalidate
= 1;
631 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
632 color_table
[0][COLOR_BLACK
]);
634 for(y
= 0; y
< s
->height
; y
++) {
635 c
= s
->cells
+ y1
* s
->width
;
636 for(x
= 0; x
< s
->width
; x
++) {
637 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
641 if (++y1
== s
->total_height
)
644 console_show_cursor(s
, 1);
645 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
648 static void console_scroll(int ydelta
)
654 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
658 for(i
= 0; i
< ydelta
; i
++) {
659 if (s
->y_displayed
== s
->y_base
)
661 if (++s
->y_displayed
== s
->total_height
)
666 i
= s
->backscroll_height
;
667 if (i
> s
->total_height
- s
->height
)
668 i
= s
->total_height
- s
->height
;
671 y1
+= s
->total_height
;
672 for(i
= 0; i
< ydelta
; i
++) {
673 if (s
->y_displayed
== y1
)
675 if (--s
->y_displayed
< 0)
676 s
->y_displayed
= s
->total_height
- 1;
682 static void console_put_lf(TextConsole
*s
)
688 if (s
->y
>= s
->height
) {
689 s
->y
= s
->height
- 1;
691 if (s
->y_displayed
== s
->y_base
) {
692 if (++s
->y_displayed
== s
->total_height
)
695 if (++s
->y_base
== s
->total_height
)
697 if (s
->backscroll_height
< s
->total_height
)
698 s
->backscroll_height
++;
699 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
700 c
= &s
->cells
[y1
* s
->width
];
701 for(x
= 0; x
< s
->width
; x
++) {
703 c
->t_attrib
= s
->t_attrib_default
;
706 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
707 if (!ds_get_bits_per_pixel(s
->ds
)) {
710 s
->text_x
[1] = s
->width
- 1;
711 s
->text_y
[1] = s
->height
- 1;
715 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
716 s
->width
* FONT_WIDTH
,
717 (s
->height
- 1) * FONT_HEIGHT
);
718 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
719 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
720 color_table
[0][s
->t_attrib_default
.bgcol
]);
723 s
->update_x1
= s
->width
* FONT_WIDTH
;
724 s
->update_y1
= s
->height
* FONT_HEIGHT
;
729 /* Set console attributes depending on the current escape codes.
730 * NOTE: I know this code is not very efficient (checking every color for it
731 * self) but it is more readable and better maintainable.
733 static void console_handle_escape(TextConsole
*s
)
737 for (i
=0; i
<s
->nb_esc_params
; i
++) {
738 switch (s
->esc_params
[i
]) {
739 case 0: /* reset all console attributes to default */
740 s
->t_attrib
= s
->t_attrib_default
;
743 s
->t_attrib
.bold
= 1;
746 s
->t_attrib
.uline
= 1;
749 s
->t_attrib
.blink
= 1;
752 s
->t_attrib
.invers
= 1;
755 s
->t_attrib
.unvisible
= 1;
758 s
->t_attrib
.bold
= 0;
761 s
->t_attrib
.uline
= 0;
764 s
->t_attrib
.blink
= 0;
767 s
->t_attrib
.invers
= 0;
770 s
->t_attrib
.unvisible
= 0;
772 /* set foreground color */
774 s
->t_attrib
.fgcol
=COLOR_BLACK
;
777 s
->t_attrib
.fgcol
=COLOR_RED
;
780 s
->t_attrib
.fgcol
=COLOR_GREEN
;
783 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
786 s
->t_attrib
.fgcol
=COLOR_BLUE
;
789 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
792 s
->t_attrib
.fgcol
=COLOR_CYAN
;
795 s
->t_attrib
.fgcol
=COLOR_WHITE
;
797 /* set background color */
799 s
->t_attrib
.bgcol
=COLOR_BLACK
;
802 s
->t_attrib
.bgcol
=COLOR_RED
;
805 s
->t_attrib
.bgcol
=COLOR_GREEN
;
808 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
811 s
->t_attrib
.bgcol
=COLOR_BLUE
;
814 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
817 s
->t_attrib
.bgcol
=COLOR_CYAN
;
820 s
->t_attrib
.bgcol
=COLOR_WHITE
;
826 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
828 int y1
= (s
->y_base
+ y
) % s
->total_height
;
829 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
831 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 active_console
->g_width
= ds_get_width(active_console
->ds
);
1065 active_console
->g_height
= ds_get_height(active_console
->ds
);
1066 s
= consoles
[index
];
1068 DisplayState
*ds
= s
->ds
;
1070 if (ds_get_bits_per_pixel(s
->ds
)) {
1071 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1073 s
->ds
->surface
->width
= s
->width
;
1074 s
->ds
->surface
->height
= s
->height
;
1077 vga_hw_invalidate();
1081 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1083 TextConsole
*s
= chr
->opaque
;
1086 s
->update_x0
= s
->width
* FONT_WIDTH
;
1087 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1090 console_show_cursor(s
, 0);
1091 for(i
= 0; i
< len
; i
++) {
1092 console_putchar(s
, buf
[i
]);
1094 console_show_cursor(s
, 1);
1095 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1096 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1097 s
->update_x1
- s
->update_x0
,
1098 s
->update_y1
- s
->update_y0
);
1103 static void console_send_event(CharDriverState
*chr
, int event
)
1105 TextConsole
*s
= chr
->opaque
;
1108 if (event
== CHR_EVENT_FOCUS
) {
1109 for(i
= 0; i
< nb_consoles
; i
++) {
1110 if (consoles
[i
] == s
) {
1118 static void kbd_send_chars(void *opaque
)
1120 TextConsole
*s
= opaque
;
1124 len
= qemu_chr_can_read(s
->chr
);
1125 if (len
> s
->out_fifo
.count
)
1126 len
= s
->out_fifo
.count
;
1128 if (len
> sizeof(buf
))
1130 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1131 qemu_chr_read(s
->chr
, buf
, len
);
1133 /* characters are pending: we send them a bit later (XXX:
1134 horrible, should change char device API) */
1135 if (s
->out_fifo
.count
> 0) {
1136 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock(rt_clock
) + 1);
1140 /* called when an ascii key is pressed */
1141 void kbd_put_keysym(int keysym
)
1144 uint8_t buf
[16], *q
;
1148 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1152 case QEMU_KEY_CTRL_UP
:
1155 case QEMU_KEY_CTRL_DOWN
:
1158 case QEMU_KEY_CTRL_PAGEUP
:
1159 console_scroll(-10);
1161 case QEMU_KEY_CTRL_PAGEDOWN
:
1165 /* convert the QEMU keysym to VT100 key string */
1167 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1170 c
= keysym
- 0xe100;
1172 *q
++ = '0' + (c
/ 10);
1173 *q
++ = '0' + (c
% 10);
1175 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1178 *q
++ = keysym
& 0xff;
1182 if (s
->chr
->chr_read
) {
1183 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1190 static void text_console_invalidate(void *opaque
)
1192 TextConsole
*s
= (TextConsole
*) opaque
;
1193 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1194 s
->g_width
= ds_get_width(s
->ds
);
1195 s
->g_height
= ds_get_height(s
->ds
);
1196 text_console_resize(s
);
1201 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1203 TextConsole
*s
= (TextConsole
*) opaque
;
1206 if (s
->text_x
[0] <= s
->text_x
[1]) {
1207 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1208 chardata
+= s
->text_y
[0] * s
->width
;
1209 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1210 for (j
= 0; j
< s
->width
; j
++, src
++)
1211 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1212 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1213 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1214 (s
->cells
[src
].t_attrib
.bold
<< 21));
1215 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1216 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1217 s
->text_x
[0] = s
->width
;
1218 s
->text_y
[0] = s
->height
;
1222 if (s
->cursor_invalidate
) {
1223 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1224 s
->cursor_invalidate
= 0;
1228 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1232 for (i
= 0; i
< nb_consoles
; i
++) {
1234 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1240 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1245 if (nb_consoles
>= MAX_CONSOLES
)
1247 s
= qemu_mallocz(sizeof(TextConsole
));
1248 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1249 (console_type
== GRAPHIC_CONSOLE
))) {
1253 s
->console_type
= console_type
;
1254 if (console_type
!= GRAPHIC_CONSOLE
) {
1255 consoles
[nb_consoles
++] = s
;
1257 /* HACK: Put graphical consoles before text consoles. */
1258 for (i
= nb_consoles
; i
> 0; i
--) {
1259 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1261 consoles
[i
] = consoles
[i
- 1];
1269 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1271 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1273 surface
->width
= width
;
1274 surface
->height
= height
;
1275 surface
->linesize
= width
* 4;
1276 surface
->pf
= qemu_default_pixelformat(32);
1277 #ifdef HOST_WORDS_BIGENDIAN
1278 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
1280 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1282 surface
->data
= (uint8_t*) qemu_mallocz(surface
->linesize
* surface
->height
);
1287 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1288 int width
, int height
)
1290 surface
->width
= width
;
1291 surface
->height
= height
;
1292 surface
->linesize
= width
* 4;
1293 surface
->pf
= qemu_default_pixelformat(32);
1294 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1295 surface
->data
= (uint8_t*) qemu_realloc(surface
->data
, surface
->linesize
* surface
->height
);
1297 surface
->data
= (uint8_t*) qemu_malloc(surface
->linesize
* surface
->height
);
1298 #ifdef HOST_WORDS_BIGENDIAN
1299 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
1301 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1307 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1308 int linesize
, uint8_t *data
)
1310 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
1312 surface
->width
= width
;
1313 surface
->height
= height
;
1314 surface
->linesize
= linesize
;
1315 surface
->pf
= qemu_default_pixelformat(bpp
);
1316 #ifdef HOST_WORDS_BIGENDIAN
1317 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1319 surface
->data
= data
;
1324 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1326 if (surface
== NULL
)
1328 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1329 qemu_free(surface
->data
);
1333 static struct DisplayAllocator default_allocator
= {
1334 defaultallocator_create_displaysurface
,
1335 defaultallocator_resize_displaysurface
,
1336 defaultallocator_free_displaysurface
1339 static void dumb_display_init(void)
1341 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
1342 ds
->allocator
= &default_allocator
;
1343 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1344 register_displaystate(ds
);
1347 /***********************************************************/
1348 /* register display */
1350 void register_displaystate(DisplayState
*ds
)
1360 DisplayState
*get_displaystate(void)
1362 if (!display_state
) {
1363 dumb_display_init ();
1365 return display_state
;
1368 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1370 if(ds
->allocator
== &default_allocator
) {
1371 DisplaySurface
*surf
;
1372 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1373 defaultallocator_free_displaysurface(ds
->surface
);
1377 return ds
->allocator
;
1380 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1381 vga_hw_invalidate_ptr invalidate
,
1382 vga_hw_screen_dump_ptr screen_dump
,
1383 vga_hw_text_update_ptr text_update
,
1389 ds
= (DisplayState
*) qemu_mallocz(sizeof(DisplayState
));
1390 ds
->allocator
= &default_allocator
;
1391 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1393 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1395 qemu_free_displaysurface(ds
);
1399 s
->hw_update
= update
;
1400 s
->hw_invalidate
= invalidate
;
1401 s
->hw_screen_dump
= screen_dump
;
1402 s
->hw_text_update
= text_update
;
1405 register_displaystate(ds
);
1409 int is_graphic_console(void)
1411 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1414 int is_fixedsize_console(void)
1416 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1419 void console_color_init(DisplayState
*ds
)
1422 for (j
= 0; j
< 2; j
++) {
1423 for (i
= 0; i
< 8; i
++) {
1424 color_table
[j
][i
] = col_expand(ds
,
1425 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1430 static int n_text_consoles
;
1431 static CharDriverState
*text_consoles
[128];
1432 static QemuOpts
*text_console_opts
[128];
1434 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
, QemuOpts
*opts
)
1439 static int color_inited
;
1441 width
= qemu_opt_get_number(opts
, "width", 0);
1443 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1445 height
= qemu_opt_get_number(opts
, "height", 0);
1447 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1449 if (width
== 0 || height
== 0) {
1450 s
= new_console(ds
, TEXT_CONSOLE
);
1451 width
= ds_get_width(s
->ds
);
1452 height
= ds_get_height(s
->ds
);
1454 s
= new_console(ds
, TEXT_CONSOLE_FIXED_SIZE
);
1462 chr
->chr_write
= console_puts
;
1463 chr
->chr_send_event
= console_send_event
;
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(rt_clock
, kbd_send_chars
, s
);
1471 if (!color_inited
) {
1473 console_color_init(s
->ds
);
1477 s
->total_height
= DEFAULT_BACKSCROLL
;
1481 s
->g_height
= height
;
1483 s
->hw_invalidate
= text_console_invalidate
;
1484 s
->hw_text_update
= text_console_update
;
1487 /* Set text attribute defaults */
1488 s
->t_attrib_default
.bold
= 0;
1489 s
->t_attrib_default
.uline
= 0;
1490 s
->t_attrib_default
.blink
= 0;
1491 s
->t_attrib_default
.invers
= 0;
1492 s
->t_attrib_default
.unvisible
= 0;
1493 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1494 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1495 /* set current text attributes to default */
1496 s
->t_attrib
= s
->t_attrib_default
;
1497 text_console_resize(s
);
1503 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1504 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1505 console_puts(chr
, (uint8_t*)msg
, len
);
1506 s
->t_attrib
= s
->t_attrib_default
;
1509 qemu_chr_generic_open(chr
);
1514 CharDriverState
*text_console_init(QemuOpts
*opts
)
1516 CharDriverState
*chr
;
1518 chr
= qemu_mallocz(sizeof(CharDriverState
));
1520 if (n_text_consoles
== 128) {
1521 fprintf(stderr
, "Too many text consoles\n");
1524 text_consoles
[n_text_consoles
] = chr
;
1525 text_console_opts
[n_text_consoles
] = opts
;
1531 void text_consoles_set_display(DisplayState
*ds
)
1535 for (i
= 0; i
< n_text_consoles
; i
++) {
1536 text_console_do_init(text_consoles
[i
], ds
, text_console_opts
[i
]);
1537 qemu_opts_del(text_console_opts
[i
]);
1538 text_console_opts
[i
] = NULL
;
1541 n_text_consoles
= 0;
1544 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1546 TextConsole
*s
= get_graphic_console(ds
);
1550 s
->g_height
= height
;
1551 if (is_graphic_console()) {
1552 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1557 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1558 int dst_x
, int dst_y
, int w
, int h
)
1560 if (is_graphic_console()) {
1561 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1565 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1569 memset(&pf
, 0x00, sizeof(PixelFormat
));
1571 pf
.bits_per_pixel
= bpp
;
1572 pf
.bytes_per_pixel
= bpp
/ 8;
1573 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1577 pf
.rmask
= 0x000000FF;
1578 pf
.gmask
= 0x0000FF00;
1579 pf
.bmask
= 0x00FF0000;
1591 pf
.rmask
= 0x0000FF00;
1592 pf
.gmask
= 0x00FF0000;
1593 pf
.bmask
= 0xFF000000;
1594 pf
.amask
= 0x00000000;
1614 PixelFormat
qemu_default_pixelformat(int bpp
)
1618 memset(&pf
, 0x00, sizeof(PixelFormat
));
1620 pf
.bits_per_pixel
= bpp
;
1621 pf
.bytes_per_pixel
= bpp
/ 8;
1622 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1626 pf
.rmask
= 0x0000F800;
1627 pf
.gmask
= 0x000007E0;
1628 pf
.bmask
= 0x0000001F;
1640 pf
.rmask
= 0x00FF0000;
1641 pf
.gmask
= 0x0000FF00;
1642 pf
.bmask
= 0x000000FF;
1653 pf
.rmask
= 0x00FF0000;
1654 pf
.gmask
= 0x0000FF00;
1655 pf
.bmask
= 0x000000FF;