2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
26 #include "qemu-timer.h"
28 //#define DEBUG_CONSOLE
29 #define DEFAULT_BACKSCROLL 512
30 #define MAX_CONSOLES 12
32 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
33 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
35 typedef struct TextAttributes
{
45 typedef struct TextCell
{
47 TextAttributes t_attrib
;
50 #define MAX_ESC_PARAMS 3
58 typedef struct QEMUFIFO
{
61 int count
, wptr
, rptr
;
64 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
68 l
= f
->buf_size
- f
->count
;
73 l
= f
->buf_size
- f
->wptr
;
76 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
78 if (f
->wptr
>= f
->buf_size
)
87 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
95 l
= f
->buf_size
- f
->rptr
;
98 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
100 if (f
->rptr
>= f
->buf_size
)
112 TEXT_CONSOLE_FIXED_SIZE
115 /* ??? This is mis-named.
116 It is used for both text and graphical consoles. */
119 console_type_t console_type
;
121 /* Graphic console state. */
122 vga_hw_update_ptr hw_update
;
123 vga_hw_invalidate_ptr hw_invalidate
;
124 vga_hw_screen_dump_ptr hw_screen_dump
;
125 vga_hw_text_update_ptr hw_text_update
;
128 int g_width
, g_height
;
132 int backscroll_height
;
134 int x_saved
, y_saved
;
137 TextAttributes t_attrib_default
; /* default text attributes */
138 TextAttributes t_attrib
; /* currently active text attributes */
140 int text_x
[2], text_y
[2], cursor_invalidate
;
149 int esc_params
[MAX_ESC_PARAMS
];
152 CharDriverState
*chr
;
153 /* fifo for key pressed */
155 uint8_t out_fifo_buf
[16];
156 QEMUTimer
*kbd_timer
;
159 static DisplayState
*display_state
;
160 static TextConsole
*active_console
;
161 static TextConsole
*consoles
[MAX_CONSOLES
];
162 static int nb_consoles
= 0;
164 void vga_hw_update(void)
166 if (active_console
&& active_console
->hw_update
)
167 active_console
->hw_update(active_console
->hw
);
170 void vga_hw_invalidate(void)
172 if (active_console
&& active_console
->hw_invalidate
)
173 active_console
->hw_invalidate(active_console
->hw
);
176 void vga_hw_screen_dump(const char *filename
)
178 TextConsole
*previous_active_console
;
180 previous_active_console
= active_console
;
182 /* There is currently no way of specifying which screen we want to dump,
183 so always dump the first one. */
185 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
186 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
);
188 error_report("screen dump not implemented");
191 if (previous_active_console
) {
192 console_select(previous_active_console
->index
);
196 void vga_hw_text_update(console_ch_t
*chardata
)
198 if (active_console
&& active_console
->hw_text_update
)
199 active_console
->hw_text_update(active_console
->hw
, chardata
);
202 /* convert a RGBA color to a color index usable in graphic primitives */
203 static unsigned int vga_get_color(DisplayState
*ds
, unsigned int rgba
)
205 unsigned int r
, g
, b
, color
;
207 switch(ds_get_bits_per_pixel(ds
)) {
210 r
= (rgba
>> 16) & 0xff;
211 g
= (rgba
>> 8) & 0xff;
213 color
= (rgb_to_index
[r
] * 6 * 6) +
214 (rgb_to_index
[g
] * 6) +
219 r
= (rgba
>> 16) & 0xff;
220 g
= (rgba
>> 8) & 0xff;
222 color
= ((r
>> 3) << 10) | ((g
>> 3) << 5) | (b
>> 3);
225 r
= (rgba
>> 16) & 0xff;
226 g
= (rgba
>> 8) & 0xff;
228 color
= ((r
>> 3) << 11) | ((g
>> 2) << 5) | (b
>> 3);
238 static void vga_fill_rect (DisplayState
*ds
,
239 int posx
, int posy
, int width
, int height
, uint32_t color
)
244 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
245 d1
= ds_get_data(ds
) +
246 ds_get_linesize(ds
) * posy
+ bpp
* posx
;
247 for (y
= 0; y
< height
; y
++) {
251 for (x
= 0; x
< width
; x
++) {
252 *((uint8_t *)d
) = color
;
257 for (x
= 0; x
< width
; x
++) {
258 *((uint16_t *)d
) = color
;
263 for (x
= 0; x
< width
; x
++) {
264 *((uint32_t *)d
) = color
;
269 d1
+= ds_get_linesize(ds
);
273 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
274 static void vga_bitblt(DisplayState
*ds
, int xs
, int ys
, int xd
, int yd
, int w
, int h
)
280 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
283 s
= ds_get_data(ds
) +
284 ds_get_linesize(ds
) * ys
+ bpp
* xs
;
285 d
= ds_get_data(ds
) +
286 ds_get_linesize(ds
) * yd
+ bpp
* xd
;
287 for (y
= 0; y
< h
; y
++) {
289 d
+= ds_get_linesize(ds
);
290 s
+= ds_get_linesize(ds
);
293 s
= ds_get_data(ds
) +
294 ds_get_linesize(ds
) * (ys
+ h
- 1) + bpp
* xs
;
295 d
= ds_get_data(ds
) +
296 ds_get_linesize(ds
) * (yd
+ h
- 1) + bpp
* xd
;
297 for (y
= 0; y
< h
; y
++) {
299 d
-= ds_get_linesize(ds
);
300 s
-= ds_get_linesize(ds
);
305 /***********************************************************/
306 /* basic char display */
308 #define FONT_HEIGHT 16
313 #define cbswap_32(__x) \
315 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
316 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
317 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
318 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
320 #ifdef HOST_WORDS_BIGENDIAN
323 #define PAT(x) cbswap_32(x)
326 static const uint32_t dmask16
[16] = {
345 static const uint32_t dmask4
[4] = {
352 static uint32_t color_table
[2][8];
354 #ifndef CONFIG_CURSES
367 static const uint32_t color_table_rgb
[2][8] = {
369 QEMU_RGB(0x00, 0x00, 0x00), /* black */
370 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
371 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
372 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
373 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
374 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
375 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
376 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
379 QEMU_RGB(0x00, 0x00, 0x00), /* black */
380 QEMU_RGB(0xff, 0x00, 0x00), /* red */
381 QEMU_RGB(0x00, 0xff, 0x00), /* green */
382 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
383 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
384 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
385 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
386 QEMU_RGB(0xff, 0xff, 0xff), /* white */
390 static inline unsigned int col_expand(DisplayState
*ds
, unsigned int col
)
392 switch(ds_get_bits_per_pixel(ds
)) {
408 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
410 if (t_attrib
->bold
) {
415 if (t_attrib
->uline
) {
420 if (t_attrib
->blink
) {
425 if (t_attrib
->invers
) {
430 if (t_attrib
->unvisible
) {
436 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
440 static void vga_putcharxy(DisplayState
*ds
, int x
, int y
, int ch
,
441 TextAttributes
*t_attrib
)
444 const uint8_t *font_ptr
;
445 unsigned int font_data
, linesize
, xorcol
, bpp
;
447 unsigned int fgcol
, bgcol
;
450 printf("x: %2i y: %2i", x
, y
);
451 console_print_text_attributes(t_attrib
, ch
);
454 if (t_attrib
->invers
) {
455 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
456 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
458 fgcol
= color_table
[t_attrib
->bold
][t_attrib
->fgcol
];
459 bgcol
= color_table
[t_attrib
->bold
][t_attrib
->bgcol
];
462 bpp
= (ds_get_bits_per_pixel(ds
) + 7) >> 3;
463 d
= ds_get_data(ds
) +
464 ds_get_linesize(ds
) * y
* FONT_HEIGHT
+ bpp
* x
* FONT_WIDTH
;
465 linesize
= ds_get_linesize(ds
);
466 font_ptr
= vgafont16
+ FONT_HEIGHT
* ch
;
467 xorcol
= bgcol
^ fgcol
;
468 switch(ds_get_bits_per_pixel(ds
)) {
470 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
471 font_data
= *font_ptr
++;
473 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
476 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
477 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
483 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
484 font_data
= *font_ptr
++;
486 && ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
489 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
490 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
491 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
492 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
497 for(i
= 0; i
< FONT_HEIGHT
; i
++) {
498 font_data
= *font_ptr
++;
499 if (t_attrib
->uline
&& ((i
== FONT_HEIGHT
- 2) || (i
== FONT_HEIGHT
- 3))) {
502 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
503 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
504 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
505 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
506 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
507 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
508 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
509 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
516 static void text_console_resize(TextConsole
*s
)
518 TextCell
*cells
, *c
, *c1
;
519 int w1
, x
, y
, last_width
;
521 last_width
= s
->width
;
522 s
->width
= s
->g_width
/ FONT_WIDTH
;
523 s
->height
= s
->g_height
/ FONT_HEIGHT
;
529 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
530 for(y
= 0; y
< s
->total_height
; y
++) {
531 c
= &cells
[y
* s
->width
];
533 c1
= &s
->cells
[y
* last_width
];
534 for(x
= 0; x
< w1
; x
++) {
538 for(x
= w1
; x
< s
->width
; x
++) {
540 c
->t_attrib
= s
->t_attrib_default
;
548 static inline void text_update_xy(TextConsole
*s
, int x
, int y
)
550 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
551 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
552 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
553 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
556 static void invalidate_xy(TextConsole
*s
, int x
, int y
)
558 if (s
->update_x0
> x
* FONT_WIDTH
)
559 s
->update_x0
= x
* FONT_WIDTH
;
560 if (s
->update_y0
> y
* FONT_HEIGHT
)
561 s
->update_y0
= y
* FONT_HEIGHT
;
562 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
563 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
564 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
565 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
568 static void update_xy(TextConsole
*s
, int x
, int y
)
573 if (s
== active_console
) {
574 if (!ds_get_bits_per_pixel(s
->ds
)) {
575 text_update_xy(s
, x
, y
);
579 y1
= (s
->y_base
+ y
) % s
->total_height
;
580 y2
= y1
- s
->y_displayed
;
582 y2
+= s
->total_height
;
583 if (y2
< s
->height
) {
584 c
= &s
->cells
[y1
* s
->width
+ x
];
585 vga_putcharxy(s
->ds
, x
, y2
, c
->ch
,
587 invalidate_xy(s
, x
, y2
);
592 static void console_show_cursor(TextConsole
*s
, int show
)
597 if (s
== active_console
) {
600 if (!ds_get_bits_per_pixel(s
->ds
)) {
601 s
->cursor_invalidate
= 1;
608 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
609 y
= y1
- s
->y_displayed
;
611 y
+= s
->total_height
;
613 c
= &s
->cells
[y1
* s
->width
+ x
];
615 TextAttributes t_attrib
= s
->t_attrib_default
;
616 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
617 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &t_attrib
);
619 vga_putcharxy(s
->ds
, x
, y
, c
->ch
, &(c
->t_attrib
));
621 invalidate_xy(s
, x
, y
);
626 static void console_refresh(TextConsole
*s
)
631 if (s
!= active_console
)
633 if (!ds_get_bits_per_pixel(s
->ds
)) {
636 s
->text_x
[1] = s
->width
- 1;
637 s
->text_y
[1] = s
->height
- 1;
638 s
->cursor_invalidate
= 1;
642 vga_fill_rect(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
),
643 color_table
[0][COLOR_BLACK
]);
645 for(y
= 0; y
< s
->height
; y
++) {
646 c
= s
->cells
+ y1
* s
->width
;
647 for(x
= 0; x
< s
->width
; x
++) {
648 vga_putcharxy(s
->ds
, x
, y
, c
->ch
,
652 if (++y1
== s
->total_height
)
655 console_show_cursor(s
, 1);
656 dpy_update(s
->ds
, 0, 0, ds_get_width(s
->ds
), ds_get_height(s
->ds
));
659 static void console_scroll(int ydelta
)
665 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
669 for(i
= 0; i
< ydelta
; i
++) {
670 if (s
->y_displayed
== s
->y_base
)
672 if (++s
->y_displayed
== s
->total_height
)
677 i
= s
->backscroll_height
;
678 if (i
> s
->total_height
- s
->height
)
679 i
= s
->total_height
- s
->height
;
682 y1
+= s
->total_height
;
683 for(i
= 0; i
< ydelta
; i
++) {
684 if (s
->y_displayed
== y1
)
686 if (--s
->y_displayed
< 0)
687 s
->y_displayed
= s
->total_height
- 1;
693 static void console_put_lf(TextConsole
*s
)
699 if (s
->y
>= s
->height
) {
700 s
->y
= s
->height
- 1;
702 if (s
->y_displayed
== s
->y_base
) {
703 if (++s
->y_displayed
== s
->total_height
)
706 if (++s
->y_base
== s
->total_height
)
708 if (s
->backscroll_height
< s
->total_height
)
709 s
->backscroll_height
++;
710 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
711 c
= &s
->cells
[y1
* s
->width
];
712 for(x
= 0; x
< s
->width
; x
++) {
714 c
->t_attrib
= s
->t_attrib_default
;
717 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
718 if (!ds_get_bits_per_pixel(s
->ds
)) {
721 s
->text_x
[1] = s
->width
- 1;
722 s
->text_y
[1] = s
->height
- 1;
726 vga_bitblt(s
->ds
, 0, FONT_HEIGHT
, 0, 0,
727 s
->width
* FONT_WIDTH
,
728 (s
->height
- 1) * FONT_HEIGHT
);
729 vga_fill_rect(s
->ds
, 0, (s
->height
- 1) * FONT_HEIGHT
,
730 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
731 color_table
[0][s
->t_attrib_default
.bgcol
]);
734 s
->update_x1
= s
->width
* FONT_WIDTH
;
735 s
->update_y1
= s
->height
* FONT_HEIGHT
;
740 /* Set console attributes depending on the current escape codes.
741 * NOTE: I know this code is not very efficient (checking every color for it
742 * self) but it is more readable and better maintainable.
744 static void console_handle_escape(TextConsole
*s
)
748 for (i
=0; i
<s
->nb_esc_params
; i
++) {
749 switch (s
->esc_params
[i
]) {
750 case 0: /* reset all console attributes to default */
751 s
->t_attrib
= s
->t_attrib_default
;
754 s
->t_attrib
.bold
= 1;
757 s
->t_attrib
.uline
= 1;
760 s
->t_attrib
.blink
= 1;
763 s
->t_attrib
.invers
= 1;
766 s
->t_attrib
.unvisible
= 1;
769 s
->t_attrib
.bold
= 0;
772 s
->t_attrib
.uline
= 0;
775 s
->t_attrib
.blink
= 0;
778 s
->t_attrib
.invers
= 0;
781 s
->t_attrib
.unvisible
= 0;
783 /* set foreground color */
785 s
->t_attrib
.fgcol
=COLOR_BLACK
;
788 s
->t_attrib
.fgcol
=COLOR_RED
;
791 s
->t_attrib
.fgcol
=COLOR_GREEN
;
794 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
797 s
->t_attrib
.fgcol
=COLOR_BLUE
;
800 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
803 s
->t_attrib
.fgcol
=COLOR_CYAN
;
806 s
->t_attrib
.fgcol
=COLOR_WHITE
;
808 /* set background color */
810 s
->t_attrib
.bgcol
=COLOR_BLACK
;
813 s
->t_attrib
.bgcol
=COLOR_RED
;
816 s
->t_attrib
.bgcol
=COLOR_GREEN
;
819 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
822 s
->t_attrib
.bgcol
=COLOR_BLUE
;
825 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
828 s
->t_attrib
.bgcol
=COLOR_CYAN
;
831 s
->t_attrib
.bgcol
=COLOR_WHITE
;
837 static void console_clear_xy(TextConsole
*s
, int x
, int y
)
839 int y1
= (s
->y_base
+ y
) % s
->total_height
;
840 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
842 c
->t_attrib
= s
->t_attrib_default
;
846 static void console_putchar(TextConsole
*s
, int ch
)
855 case '\r': /* carriage return */
858 case '\n': /* newline */
861 case '\b': /* backspace */
865 case '\t': /* tabspace */
866 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
870 s
->x
= s
->x
+ (8 - (s
->x
% 8));
873 case '\a': /* alert aka. bell */
874 /* TODO: has to be implemented */
877 /* SI (shift in), character set 0 (ignored) */
880 /* SO (shift out), character set 1 (ignored) */
882 case 27: /* esc (introducing an escape sequence) */
883 s
->state
= TTY_STATE_ESC
;
886 if (s
->x
>= s
->width
) {
891 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
892 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
894 c
->t_attrib
= s
->t_attrib
;
895 update_xy(s
, s
->x
, s
->y
);
900 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
902 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
903 s
->esc_params
[i
] = 0;
904 s
->nb_esc_params
= 0;
905 s
->state
= TTY_STATE_CSI
;
907 s
->state
= TTY_STATE_NORM
;
910 case TTY_STATE_CSI
: /* handle escape sequence parameters */
911 if (ch
>= '0' && ch
<= '9') {
912 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
913 s
->esc_params
[s
->nb_esc_params
] =
914 s
->esc_params
[s
->nb_esc_params
] * 10 + ch
- '0';
921 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
922 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
924 s
->state
= TTY_STATE_NORM
;
928 if (s
->esc_params
[0] == 0) {
929 s
->esc_params
[0] = 1;
931 s
->y
-= s
->esc_params
[0];
937 /* move cursor down */
938 if (s
->esc_params
[0] == 0) {
939 s
->esc_params
[0] = 1;
941 s
->y
+= s
->esc_params
[0];
942 if (s
->y
>= s
->height
) {
943 s
->y
= s
->height
- 1;
947 /* move cursor right */
948 if (s
->esc_params
[0] == 0) {
949 s
->esc_params
[0] = 1;
951 s
->x
+= s
->esc_params
[0];
952 if (s
->x
>= s
->width
) {
957 /* move cursor left */
958 if (s
->esc_params
[0] == 0) {
959 s
->esc_params
[0] = 1;
961 s
->x
-= s
->esc_params
[0];
967 /* move cursor to column */
968 s
->x
= s
->esc_params
[0] - 1;
975 /* move cursor to row, column */
976 s
->x
= s
->esc_params
[1] - 1;
980 s
->y
= s
->esc_params
[0] - 1;
986 switch (s
->esc_params
[0]) {
988 /* clear to end of screen */
989 for (y
= s
->y
; y
< s
->height
; y
++) {
990 for (x
= 0; x
< s
->width
; x
++) {
991 if (y
== s
->y
&& x
< s
->x
) {
994 console_clear_xy(s
, x
, y
);
999 /* clear from beginning of screen */
1000 for (y
= 0; y
<= s
->y
; y
++) {
1001 for (x
= 0; x
< s
->width
; x
++) {
1002 if (y
== s
->y
&& x
> s
->x
) {
1005 console_clear_xy(s
, x
, y
);
1010 /* clear entire screen */
1011 for (y
= 0; y
<= s
->height
; y
++) {
1012 for (x
= 0; x
< s
->width
; x
++) {
1013 console_clear_xy(s
, x
, y
);
1020 switch (s
->esc_params
[0]) {
1023 for(x
= s
->x
; x
< s
->width
; x
++) {
1024 console_clear_xy(s
, x
, s
->y
);
1028 /* clear from beginning of line */
1029 for (x
= 0; x
<= s
->x
; x
++) {
1030 console_clear_xy(s
, x
, s
->y
);
1034 /* clear entire line */
1035 for(x
= 0; x
< s
->width
; x
++) {
1036 console_clear_xy(s
, x
, s
->y
);
1042 console_handle_escape(s
);
1045 /* report cursor position */
1046 /* TODO: send ESC[row;colR */
1049 /* save cursor position */
1054 /* restore cursor position */
1059 #ifdef DEBUG_CONSOLE
1060 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
1069 void console_select(unsigned int index
)
1073 if (index
>= MAX_CONSOLES
)
1075 if (active_console
) {
1076 active_console
->g_width
= ds_get_width(active_console
->ds
);
1077 active_console
->g_height
= ds_get_height(active_console
->ds
);
1079 s
= consoles
[index
];
1081 DisplayState
*ds
= s
->ds
;
1083 if (ds_get_bits_per_pixel(s
->ds
)) {
1084 ds
->surface
= qemu_resize_displaysurface(ds
, s
->g_width
, s
->g_height
);
1086 s
->ds
->surface
->width
= s
->width
;
1087 s
->ds
->surface
->height
= s
->height
;
1090 vga_hw_invalidate();
1094 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1096 TextConsole
*s
= chr
->opaque
;
1099 s
->update_x0
= s
->width
* FONT_WIDTH
;
1100 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1103 console_show_cursor(s
, 0);
1104 for(i
= 0; i
< len
; i
++) {
1105 console_putchar(s
, buf
[i
]);
1107 console_show_cursor(s
, 1);
1108 if (ds_get_bits_per_pixel(s
->ds
) && s
->update_x0
< s
->update_x1
) {
1109 dpy_update(s
->ds
, s
->update_x0
, s
->update_y0
,
1110 s
->update_x1
- s
->update_x0
,
1111 s
->update_y1
- s
->update_y0
);
1116 static void kbd_send_chars(void *opaque
)
1118 TextConsole
*s
= opaque
;
1122 len
= qemu_chr_be_can_write(s
->chr
);
1123 if (len
> s
->out_fifo
.count
)
1124 len
= s
->out_fifo
.count
;
1126 if (len
> sizeof(buf
))
1128 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1129 qemu_chr_be_write(s
->chr
, buf
, len
);
1131 /* characters are pending: we send them a bit later (XXX:
1132 horrible, should change char device API) */
1133 if (s
->out_fifo
.count
> 0) {
1134 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
1138 /* called when an ascii key is pressed */
1139 void kbd_put_keysym(int keysym
)
1142 uint8_t buf
[16], *q
;
1146 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1150 case QEMU_KEY_CTRL_UP
:
1153 case QEMU_KEY_CTRL_DOWN
:
1156 case QEMU_KEY_CTRL_PAGEUP
:
1157 console_scroll(-10);
1159 case QEMU_KEY_CTRL_PAGEDOWN
:
1163 /* convert the QEMU keysym to VT100 key string */
1165 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1168 c
= keysym
- 0xe100;
1170 *q
++ = '0' + (c
/ 10);
1171 *q
++ = '0' + (c
% 10);
1173 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1176 *q
++ = keysym
& 0xff;
1177 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1178 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1184 console_puts(s
->chr
, buf
, q
- buf
);
1186 if (s
->chr
->chr_read
) {
1187 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1194 static void text_console_invalidate(void *opaque
)
1196 TextConsole
*s
= (TextConsole
*) opaque
;
1197 if (!ds_get_bits_per_pixel(s
->ds
) && s
->console_type
== TEXT_CONSOLE
) {
1198 s
->g_width
= ds_get_width(s
->ds
);
1199 s
->g_height
= ds_get_height(s
->ds
);
1200 text_console_resize(s
);
1205 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1207 TextConsole
*s
= (TextConsole
*) opaque
;
1210 if (s
->text_x
[0] <= s
->text_x
[1]) {
1211 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1212 chardata
+= s
->text_y
[0] * s
->width
;
1213 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1214 for (j
= 0; j
< s
->width
; j
++, src
++)
1215 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1216 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1217 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1218 (s
->cells
[src
].t_attrib
.bold
<< 21));
1219 dpy_update(s
->ds
, s
->text_x
[0], s
->text_y
[0],
1220 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1221 s
->text_x
[0] = s
->width
;
1222 s
->text_y
[0] = s
->height
;
1226 if (s
->cursor_invalidate
) {
1227 dpy_cursor(s
->ds
, s
->x
, s
->y
);
1228 s
->cursor_invalidate
= 0;
1232 static TextConsole
*get_graphic_console(DisplayState
*ds
)
1236 for (i
= 0; i
< nb_consoles
; i
++) {
1238 if (s
->console_type
== GRAPHIC_CONSOLE
&& s
->ds
== ds
)
1244 static TextConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1249 if (nb_consoles
>= MAX_CONSOLES
)
1251 s
= g_malloc0(sizeof(TextConsole
));
1252 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1253 (console_type
== GRAPHIC_CONSOLE
))) {
1257 s
->console_type
= console_type
;
1258 if (console_type
!= GRAPHIC_CONSOLE
) {
1259 s
->index
= nb_consoles
;
1260 consoles
[nb_consoles
++] = s
;
1262 /* HACK: Put graphical consoles before text consoles. */
1263 for (i
= nb_consoles
; i
> 0; i
--) {
1264 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1266 consoles
[i
] = consoles
[i
- 1];
1267 consoles
[i
]->index
= i
;
1276 static DisplaySurface
* defaultallocator_create_displaysurface(int width
, int height
)
1278 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1280 int linesize
= width
* 4;
1281 qemu_alloc_display(surface
, width
, height
, linesize
,
1282 qemu_default_pixelformat(32), 0);
1286 static DisplaySurface
* defaultallocator_resize_displaysurface(DisplaySurface
*surface
,
1287 int width
, int height
)
1289 int linesize
= width
* 4;
1290 qemu_alloc_display(surface
, width
, height
, linesize
,
1291 qemu_default_pixelformat(32), 0);
1295 void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1296 int linesize
, PixelFormat pf
, int newflags
)
1299 surface
->width
= width
;
1300 surface
->height
= height
;
1301 surface
->linesize
= linesize
;
1303 if (surface
->flags
& QEMU_ALLOCATED_FLAG
) {
1304 data
= g_realloc(surface
->data
,
1305 surface
->linesize
* surface
->height
);
1307 data
= g_malloc(surface
->linesize
* surface
->height
);
1309 surface
->data
= (uint8_t *)data
;
1310 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1311 #ifdef HOST_WORDS_BIGENDIAN
1312 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1316 DisplaySurface
* qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1317 int linesize
, uint8_t *data
)
1319 DisplaySurface
*surface
= (DisplaySurface
*) g_malloc0(sizeof(DisplaySurface
));
1321 surface
->width
= width
;
1322 surface
->height
= height
;
1323 surface
->linesize
= linesize
;
1324 surface
->pf
= qemu_default_pixelformat(bpp
);
1325 #ifdef HOST_WORDS_BIGENDIAN
1326 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1328 surface
->data
= data
;
1333 static void defaultallocator_free_displaysurface(DisplaySurface
*surface
)
1335 if (surface
== NULL
)
1337 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
1338 g_free(surface
->data
);
1342 static struct DisplayAllocator default_allocator
= {
1343 defaultallocator_create_displaysurface
,
1344 defaultallocator_resize_displaysurface
,
1345 defaultallocator_free_displaysurface
1348 static void dumb_display_init(void)
1350 DisplayState
*ds
= g_malloc0(sizeof(DisplayState
));
1354 ds
->allocator
= &default_allocator
;
1355 if (is_fixedsize_console()) {
1356 width
= active_console
->g_width
;
1357 height
= active_console
->g_height
;
1359 ds
->surface
= qemu_create_displaysurface(ds
, width
, height
);
1360 register_displaystate(ds
);
1363 /***********************************************************/
1364 /* register display */
1366 void register_displaystate(DisplayState
*ds
)
1376 DisplayState
*get_displaystate(void)
1378 if (!display_state
) {
1379 dumb_display_init ();
1381 return display_state
;
1384 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
1386 if(ds
->allocator
== &default_allocator
) {
1387 DisplaySurface
*surf
;
1388 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
1389 defaultallocator_free_displaysurface(ds
->surface
);
1393 return ds
->allocator
;
1396 DisplayState
*graphic_console_init(vga_hw_update_ptr update
,
1397 vga_hw_invalidate_ptr invalidate
,
1398 vga_hw_screen_dump_ptr screen_dump
,
1399 vga_hw_text_update_ptr text_update
,
1405 ds
= (DisplayState
*) g_malloc0(sizeof(DisplayState
));
1406 ds
->allocator
= &default_allocator
;
1407 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
1409 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1411 qemu_free_displaysurface(ds
);
1415 s
->hw_update
= update
;
1416 s
->hw_invalidate
= invalidate
;
1417 s
->hw_screen_dump
= screen_dump
;
1418 s
->hw_text_update
= text_update
;
1421 register_displaystate(ds
);
1425 int is_graphic_console(void)
1427 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1430 int is_fixedsize_console(void)
1432 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1435 void console_color_init(DisplayState
*ds
)
1438 for (j
= 0; j
< 2; j
++) {
1439 for (i
= 0; i
< 8; i
++) {
1440 color_table
[j
][i
] = col_expand(ds
,
1441 vga_get_color(ds
, color_table_rgb
[j
][i
]));
1446 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1448 TextConsole
*s
= chr
->opaque
;
1453 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1456 static int color_inited
;
1460 chr
->chr_write
= console_puts
;
1462 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1463 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1464 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1467 if (!color_inited
) {
1469 console_color_init(s
->ds
);
1473 s
->total_height
= DEFAULT_BACKSCROLL
;
1476 if (s
->console_type
== TEXT_CONSOLE
) {
1477 s
->g_width
= ds_get_width(s
->ds
);
1478 s
->g_height
= ds_get_height(s
->ds
);
1481 s
->hw_invalidate
= text_console_invalidate
;
1482 s
->hw_text_update
= text_console_update
;
1485 /* Set text attribute defaults */
1486 s
->t_attrib_default
.bold
= 0;
1487 s
->t_attrib_default
.uline
= 0;
1488 s
->t_attrib_default
.blink
= 0;
1489 s
->t_attrib_default
.invers
= 0;
1490 s
->t_attrib_default
.unvisible
= 0;
1491 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1492 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1493 /* set current text attributes to default */
1494 s
->t_attrib
= s
->t_attrib_default
;
1495 text_console_resize(s
);
1501 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1502 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1503 console_puts(chr
, (uint8_t*)msg
, len
);
1504 s
->t_attrib
= s
->t_attrib_default
;
1507 qemu_chr_generic_open(chr
);
1512 CharDriverState
*text_console_init(QemuOpts
*opts
)
1514 CharDriverState
*chr
;
1519 chr
= g_malloc0(sizeof(CharDriverState
));
1521 width
= qemu_opt_get_number(opts
, "width", 0);
1523 width
= qemu_opt_get_number(opts
, "cols", 0) * FONT_WIDTH
;
1525 height
= qemu_opt_get_number(opts
, "height", 0);
1527 height
= qemu_opt_get_number(opts
, "rows", 0) * FONT_HEIGHT
;
1529 if (width
== 0 || height
== 0) {
1530 s
= new_console(NULL
, TEXT_CONSOLE
);
1532 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1542 s
->g_height
= height
;
1544 chr
->chr_set_echo
= text_console_set_echo
;
1548 void text_consoles_set_display(DisplayState
*ds
)
1552 for (i
= 0; i
< nb_consoles
; i
++) {
1553 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
) {
1554 text_console_do_init(consoles
[i
]->chr
, ds
);
1559 void qemu_console_resize(DisplayState
*ds
, int width
, int height
)
1561 TextConsole
*s
= get_graphic_console(ds
);
1565 s
->g_height
= height
;
1566 if (is_graphic_console()) {
1567 ds
->surface
= qemu_resize_displaysurface(ds
, width
, height
);
1572 void qemu_console_copy(DisplayState
*ds
, int src_x
, int src_y
,
1573 int dst_x
, int dst_y
, int w
, int h
)
1575 if (is_graphic_console()) {
1576 dpy_copy(ds
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1580 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1584 memset(&pf
, 0x00, sizeof(PixelFormat
));
1586 pf
.bits_per_pixel
= bpp
;
1587 pf
.bytes_per_pixel
= bpp
/ 8;
1588 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1592 pf
.rmask
= 0x000000FF;
1593 pf
.gmask
= 0x0000FF00;
1594 pf
.bmask
= 0x00FF0000;
1606 pf
.rmask
= 0x0000FF00;
1607 pf
.gmask
= 0x00FF0000;
1608 pf
.bmask
= 0xFF000000;
1609 pf
.amask
= 0x00000000;
1629 PixelFormat
qemu_default_pixelformat(int bpp
)
1633 memset(&pf
, 0x00, sizeof(PixelFormat
));
1635 pf
.bits_per_pixel
= bpp
;
1636 pf
.bytes_per_pixel
= bpp
/ 8;
1637 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1641 pf
.bits_per_pixel
= 16;
1642 pf
.bytes_per_pixel
= 2;
1643 pf
.rmask
= 0x00007c00;
1644 pf
.gmask
= 0x000003E0;
1645 pf
.bmask
= 0x0000001F;
1657 pf
.rmask
= 0x0000F800;
1658 pf
.gmask
= 0x000007E0;
1659 pf
.bmask
= 0x0000001F;
1671 pf
.rmask
= 0x00FF0000;
1672 pf
.gmask
= 0x0000FF00;
1673 pf
.bmask
= 0x000000FF;
1685 pf
.rmask
= 0x00FF0000;
1686 pf
.gmask
= 0x0000FF00;
1687 pf
.bmask
= 0x000000FF;