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"
25 #include "ui/console.h"
26 #include "qemu/timer.h"
27 #include "qmp-commands.h"
28 #include "sysemu/char.h"
30 //#define DEBUG_CONSOLE
31 #define DEFAULT_BACKSCROLL 512
32 #define MAX_CONSOLES 12
33 #define CONSOLE_CURSOR_PERIOD 500
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
117 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
;
126 int g_width
, g_height
;
128 /* Text console state */
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
;
142 bool cursor_visible_phase
;
143 QEMUTimer
*cursor_timer
;
151 int esc_params
[MAX_ESC_PARAMS
];
154 CharDriverState
*chr
;
155 /* fifo for key pressed */
157 uint8_t out_fifo_buf
[16];
158 QEMUTimer
*kbd_timer
;
161 static DisplayState
*display_state
;
162 static QemuConsole
*active_console
;
163 static QemuConsole
*consoles
[MAX_CONSOLES
];
164 static int nb_consoles
= 0;
166 void vga_hw_update(void)
168 if (active_console
&& active_console
->hw_update
)
169 active_console
->hw_update(active_console
->hw
);
172 void vga_hw_invalidate(void)
174 if (active_console
&& active_console
->hw_invalidate
)
175 active_console
->hw_invalidate(active_console
->hw
);
178 void qmp_screendump(const char *filename
, Error
**errp
)
180 QemuConsole
*previous_active_console
;
183 previous_active_console
= active_console
;
184 cswitch
= previous_active_console
&& previous_active_console
->index
!= 0;
186 /* There is currently no way of specifying which screen we want to dump,
187 so always dump the first one. */
191 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
192 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
, cswitch
, errp
);
194 error_setg(errp
, "device doesn't support screendump");
198 console_select(previous_active_console
->index
);
202 void vga_hw_text_update(console_ch_t
*chardata
)
204 if (active_console
&& active_console
->hw_text_update
)
205 active_console
->hw_text_update(active_console
->hw
, chardata
);
208 static void vga_fill_rect(QemuConsole
*con
,
209 int posx
, int posy
, int width
, int height
,
210 pixman_color_t color
)
212 DisplaySurface
*surface
= qemu_console_surface(con
);
213 pixman_rectangle16_t rect
= {
214 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
217 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
221 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
222 static void vga_bitblt(QemuConsole
*con
,
223 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
225 DisplaySurface
*surface
= qemu_console_surface(con
);
227 pixman_image_composite(PIXMAN_OP_SRC
,
228 surface
->image
, NULL
, surface
->image
,
229 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
232 /***********************************************************/
233 /* basic char display */
235 #define FONT_HEIGHT 16
240 #ifndef CONFIG_CURSES
253 #define QEMU_RGB(r, g, b) \
254 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
256 static const pixman_color_t color_table_rgb
[2][8] = {
258 QEMU_RGB(0x00, 0x00, 0x00), /* black */
259 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
260 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
261 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
262 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
263 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
264 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
265 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
268 QEMU_RGB(0x00, 0x00, 0x00), /* black */
269 QEMU_RGB(0xff, 0x00, 0x00), /* red */
270 QEMU_RGB(0x00, 0xff, 0x00), /* green */
271 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
272 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
273 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
274 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
275 QEMU_RGB(0xff, 0xff, 0xff), /* white */
280 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
282 if (t_attrib
->bold
) {
287 if (t_attrib
->uline
) {
292 if (t_attrib
->blink
) {
297 if (t_attrib
->invers
) {
302 if (t_attrib
->unvisible
) {
308 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
312 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
313 TextAttributes
*t_attrib
)
315 static pixman_image_t
*glyphs
[256];
316 DisplaySurface
*surface
= qemu_console_surface(s
);
317 pixman_color_t fgcol
, bgcol
;
319 if (t_attrib
->invers
) {
320 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
321 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
323 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
324 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
328 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
330 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
331 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
334 static void text_console_resize(QemuConsole
*s
)
336 TextCell
*cells
, *c
, *c1
;
337 int w1
, x
, y
, last_width
;
339 last_width
= s
->width
;
340 s
->width
= s
->g_width
/ FONT_WIDTH
;
341 s
->height
= s
->g_height
/ FONT_HEIGHT
;
347 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
348 for(y
= 0; y
< s
->total_height
; y
++) {
349 c
= &cells
[y
* s
->width
];
351 c1
= &s
->cells
[y
* last_width
];
352 for(x
= 0; x
< w1
; x
++) {
356 for(x
= w1
; x
< s
->width
; x
++) {
358 c
->t_attrib
= s
->t_attrib_default
;
366 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
368 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
369 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
370 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
371 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
374 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
376 if (s
->update_x0
> x
* FONT_WIDTH
)
377 s
->update_x0
= x
* FONT_WIDTH
;
378 if (s
->update_y0
> y
* FONT_HEIGHT
)
379 s
->update_y0
= y
* FONT_HEIGHT
;
380 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
381 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
382 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
383 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
386 static void update_xy(QemuConsole
*s
, int x
, int y
)
391 if (s
!= active_console
) {
395 if (s
->ds
->have_text
) {
396 text_update_xy(s
, x
, y
);
399 if (s
->ds
->have_gfx
) {
400 y1
= (s
->y_base
+ y
) % s
->total_height
;
401 y2
= y1
- s
->y_displayed
;
403 y2
+= s
->total_height
;
404 if (y2
< s
->height
) {
405 c
= &s
->cells
[y1
* s
->width
+ x
];
406 vga_putcharxy(s
, x
, y2
, c
->ch
,
408 invalidate_xy(s
, x
, y2
);
413 static void console_show_cursor(QemuConsole
*s
, int show
)
419 if (s
!= active_console
) {
423 if (s
->ds
->have_text
) {
424 s
->cursor_invalidate
= 1;
427 if (s
->ds
->have_gfx
) {
431 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
432 y
= y1
- s
->y_displayed
;
434 y
+= s
->total_height
;
436 c
= &s
->cells
[y1
* s
->width
+ x
];
437 if (show
&& s
->cursor_visible_phase
) {
438 TextAttributes t_attrib
= s
->t_attrib_default
;
439 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
440 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
442 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
444 invalidate_xy(s
, x
, y
);
449 static void console_refresh(QemuConsole
*s
)
451 DisplaySurface
*surface
= qemu_console_surface(s
);
455 if (s
!= active_console
)
458 if (s
->ds
->have_text
) {
461 s
->text_x
[1] = s
->width
- 1;
462 s
->text_y
[1] = s
->height
- 1;
463 s
->cursor_invalidate
= 1;
466 if (s
->ds
->have_gfx
) {
467 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
468 color_table_rgb
[0][COLOR_BLACK
]);
470 for (y
= 0; y
< s
->height
; y
++) {
471 c
= s
->cells
+ y1
* s
->width
;
472 for (x
= 0; x
< s
->width
; x
++) {
473 vga_putcharxy(s
, x
, y
, c
->ch
,
477 if (++y1
== s
->total_height
) {
481 console_show_cursor(s
, 1);
482 dpy_gfx_update(s
, 0, 0,
483 surface_width(surface
), surface_height(surface
));
487 static void console_scroll(int ydelta
)
493 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
497 for(i
= 0; i
< ydelta
; i
++) {
498 if (s
->y_displayed
== s
->y_base
)
500 if (++s
->y_displayed
== s
->total_height
)
505 i
= s
->backscroll_height
;
506 if (i
> s
->total_height
- s
->height
)
507 i
= s
->total_height
- s
->height
;
510 y1
+= s
->total_height
;
511 for(i
= 0; i
< ydelta
; i
++) {
512 if (s
->y_displayed
== y1
)
514 if (--s
->y_displayed
< 0)
515 s
->y_displayed
= s
->total_height
- 1;
521 static void console_put_lf(QemuConsole
*s
)
527 if (s
->y
>= s
->height
) {
528 s
->y
= s
->height
- 1;
530 if (s
->y_displayed
== s
->y_base
) {
531 if (++s
->y_displayed
== s
->total_height
)
534 if (++s
->y_base
== s
->total_height
)
536 if (s
->backscroll_height
< s
->total_height
)
537 s
->backscroll_height
++;
538 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
539 c
= &s
->cells
[y1
* s
->width
];
540 for(x
= 0; x
< s
->width
; x
++) {
542 c
->t_attrib
= s
->t_attrib_default
;
545 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
546 if (s
->ds
->have_text
) {
549 s
->text_x
[1] = s
->width
- 1;
550 s
->text_y
[1] = s
->height
- 1;
553 if (s
->ds
->have_gfx
) {
554 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
555 s
->width
* FONT_WIDTH
,
556 (s
->height
- 1) * FONT_HEIGHT
);
557 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
558 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
559 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
562 s
->update_x1
= s
->width
* FONT_WIDTH
;
563 s
->update_y1
= s
->height
* FONT_HEIGHT
;
569 /* Set console attributes depending on the current escape codes.
570 * NOTE: I know this code is not very efficient (checking every color for it
571 * self) but it is more readable and better maintainable.
573 static void console_handle_escape(QemuConsole
*s
)
577 for (i
=0; i
<s
->nb_esc_params
; i
++) {
578 switch (s
->esc_params
[i
]) {
579 case 0: /* reset all console attributes to default */
580 s
->t_attrib
= s
->t_attrib_default
;
583 s
->t_attrib
.bold
= 1;
586 s
->t_attrib
.uline
= 1;
589 s
->t_attrib
.blink
= 1;
592 s
->t_attrib
.invers
= 1;
595 s
->t_attrib
.unvisible
= 1;
598 s
->t_attrib
.bold
= 0;
601 s
->t_attrib
.uline
= 0;
604 s
->t_attrib
.blink
= 0;
607 s
->t_attrib
.invers
= 0;
610 s
->t_attrib
.unvisible
= 0;
612 /* set foreground color */
614 s
->t_attrib
.fgcol
=COLOR_BLACK
;
617 s
->t_attrib
.fgcol
=COLOR_RED
;
620 s
->t_attrib
.fgcol
=COLOR_GREEN
;
623 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
626 s
->t_attrib
.fgcol
=COLOR_BLUE
;
629 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
632 s
->t_attrib
.fgcol
=COLOR_CYAN
;
635 s
->t_attrib
.fgcol
=COLOR_WHITE
;
637 /* set background color */
639 s
->t_attrib
.bgcol
=COLOR_BLACK
;
642 s
->t_attrib
.bgcol
=COLOR_RED
;
645 s
->t_attrib
.bgcol
=COLOR_GREEN
;
648 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
651 s
->t_attrib
.bgcol
=COLOR_BLUE
;
654 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
657 s
->t_attrib
.bgcol
=COLOR_CYAN
;
660 s
->t_attrib
.bgcol
=COLOR_WHITE
;
666 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
668 int y1
= (s
->y_base
+ y
) % s
->total_height
;
669 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
671 c
->t_attrib
= s
->t_attrib_default
;
675 /* set cursor, checking bounds */
676 static void set_cursor(QemuConsole
*s
, int x
, int y
)
684 if (y
>= s
->height
) {
695 static void console_putchar(QemuConsole
*s
, int ch
)
704 case '\r': /* carriage return */
707 case '\n': /* newline */
710 case '\b': /* backspace */
714 case '\t': /* tabspace */
715 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
719 s
->x
= s
->x
+ (8 - (s
->x
% 8));
722 case '\a': /* alert aka. bell */
723 /* TODO: has to be implemented */
726 /* SI (shift in), character set 0 (ignored) */
729 /* SO (shift out), character set 1 (ignored) */
731 case 27: /* esc (introducing an escape sequence) */
732 s
->state
= TTY_STATE_ESC
;
735 if (s
->x
>= s
->width
) {
740 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
741 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
743 c
->t_attrib
= s
->t_attrib
;
744 update_xy(s
, s
->x
, s
->y
);
749 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
751 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
752 s
->esc_params
[i
] = 0;
753 s
->nb_esc_params
= 0;
754 s
->state
= TTY_STATE_CSI
;
756 s
->state
= TTY_STATE_NORM
;
759 case TTY_STATE_CSI
: /* handle escape sequence parameters */
760 if (ch
>= '0' && ch
<= '9') {
761 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
762 int *param
= &s
->esc_params
[s
->nb_esc_params
];
763 int digit
= (ch
- '0');
765 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
766 *param
* 10 + digit
: INT_MAX
;
769 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
774 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
775 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
777 s
->state
= TTY_STATE_NORM
;
781 if (s
->esc_params
[0] == 0) {
782 s
->esc_params
[0] = 1;
784 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
787 /* move cursor down */
788 if (s
->esc_params
[0] == 0) {
789 s
->esc_params
[0] = 1;
791 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
794 /* move cursor right */
795 if (s
->esc_params
[0] == 0) {
796 s
->esc_params
[0] = 1;
798 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
801 /* move cursor left */
802 if (s
->esc_params
[0] == 0) {
803 s
->esc_params
[0] = 1;
805 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
808 /* move cursor to column */
809 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
813 /* move cursor to row, column */
814 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
817 switch (s
->esc_params
[0]) {
819 /* clear to end of screen */
820 for (y
= s
->y
; y
< s
->height
; y
++) {
821 for (x
= 0; x
< s
->width
; x
++) {
822 if (y
== s
->y
&& x
< s
->x
) {
825 console_clear_xy(s
, x
, y
);
830 /* clear from beginning of screen */
831 for (y
= 0; y
<= s
->y
; y
++) {
832 for (x
= 0; x
< s
->width
; x
++) {
833 if (y
== s
->y
&& x
> s
->x
) {
836 console_clear_xy(s
, x
, y
);
841 /* clear entire screen */
842 for (y
= 0; y
<= s
->height
; y
++) {
843 for (x
= 0; x
< s
->width
; x
++) {
844 console_clear_xy(s
, x
, y
);
851 switch (s
->esc_params
[0]) {
854 for(x
= s
->x
; x
< s
->width
; x
++) {
855 console_clear_xy(s
, x
, s
->y
);
859 /* clear from beginning of line */
860 for (x
= 0; x
<= s
->x
; x
++) {
861 console_clear_xy(s
, x
, s
->y
);
865 /* clear entire line */
866 for(x
= 0; x
< s
->width
; x
++) {
867 console_clear_xy(s
, x
, s
->y
);
873 console_handle_escape(s
);
876 /* report cursor position */
877 /* TODO: send ESC[row;colR */
880 /* save cursor position */
885 /* restore cursor position */
891 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
900 void console_select(unsigned int index
)
902 DisplaySurface
*surface
;
905 if (index
>= MAX_CONSOLES
)
907 if (active_console
) {
908 surface
= qemu_console_surface(active_console
);
909 active_console
->g_width
= surface_width(surface
);
910 active_console
->g_height
= surface_height(surface
);
914 DisplayState
*ds
= s
->ds
;
916 if (active_console
&& active_console
->cursor_timer
) {
917 qemu_del_timer(active_console
->cursor_timer
);
921 surface
= qemu_create_displaysurface(s
->g_width
, s
->g_height
);
922 dpy_gfx_replace_surface(s
, surface
);
925 dpy_text_resize(s
, s
->width
, s
->height
);
927 if (s
->cursor_timer
) {
928 qemu_mod_timer(s
->cursor_timer
,
929 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
935 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
937 QemuConsole
*s
= chr
->opaque
;
940 s
->update_x0
= s
->width
* FONT_WIDTH
;
941 s
->update_y0
= s
->height
* FONT_HEIGHT
;
944 console_show_cursor(s
, 0);
945 for(i
= 0; i
< len
; i
++) {
946 console_putchar(s
, buf
[i
]);
948 console_show_cursor(s
, 1);
949 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
950 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
951 s
->update_x1
- s
->update_x0
,
952 s
->update_y1
- s
->update_y0
);
957 static void kbd_send_chars(void *opaque
)
959 QemuConsole
*s
= opaque
;
963 len
= qemu_chr_be_can_write(s
->chr
);
964 if (len
> s
->out_fifo
.count
)
965 len
= s
->out_fifo
.count
;
967 if (len
> sizeof(buf
))
969 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
970 qemu_chr_be_write(s
->chr
, buf
, len
);
972 /* characters are pending: we send them a bit later (XXX:
973 horrible, should change char device API) */
974 if (s
->out_fifo
.count
> 0) {
975 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
979 /* called when an ascii key is pressed */
980 void kbd_put_keysym(int keysym
)
987 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
991 case QEMU_KEY_CTRL_UP
:
994 case QEMU_KEY_CTRL_DOWN
:
997 case QEMU_KEY_CTRL_PAGEUP
:
1000 case QEMU_KEY_CTRL_PAGEDOWN
:
1004 /* convert the QEMU keysym to VT100 key string */
1006 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1009 c
= keysym
- 0xe100;
1011 *q
++ = '0' + (c
/ 10);
1012 *q
++ = '0' + (c
% 10);
1014 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1017 *q
++ = keysym
& 0xff;
1018 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1019 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1025 console_puts(s
->chr
, buf
, q
- buf
);
1027 if (s
->chr
->chr_read
) {
1028 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1035 static void text_console_invalidate(void *opaque
)
1037 QemuConsole
*s
= (QemuConsole
*) opaque
;
1038 DisplaySurface
*surface
= qemu_console_surface(s
);
1040 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1041 s
->g_width
= surface_width(surface
);
1042 s
->g_height
= surface_height(surface
);
1043 text_console_resize(s
);
1048 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1050 QemuConsole
*s
= (QemuConsole
*) opaque
;
1053 if (s
->text_x
[0] <= s
->text_x
[1]) {
1054 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1055 chardata
+= s
->text_y
[0] * s
->width
;
1056 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1057 for (j
= 0; j
< s
->width
; j
++, src
++)
1058 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1059 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1060 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1061 (s
->cells
[src
].t_attrib
.bold
<< 21));
1062 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1063 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1064 s
->text_x
[0] = s
->width
;
1065 s
->text_y
[0] = s
->height
;
1069 if (s
->cursor_invalidate
) {
1070 dpy_text_cursor(s
, s
->x
, s
->y
);
1071 s
->cursor_invalidate
= 0;
1075 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1080 if (nb_consoles
>= MAX_CONSOLES
)
1082 s
= g_malloc0(sizeof(QemuConsole
));
1083 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1084 (console_type
== GRAPHIC_CONSOLE
))) {
1088 s
->console_type
= console_type
;
1089 if (console_type
!= GRAPHIC_CONSOLE
) {
1090 s
->index
= nb_consoles
;
1091 consoles
[nb_consoles
++] = s
;
1093 /* HACK: Put graphical consoles before text consoles. */
1094 for (i
= nb_consoles
; i
> 0; i
--) {
1095 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1097 consoles
[i
] = consoles
[i
- 1];
1098 consoles
[i
]->index
= i
;
1107 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1108 int linesize
, PixelFormat pf
, int newflags
)
1112 qemu_pixman_image_unref(surface
->image
);
1113 surface
->image
= NULL
;
1115 surface
->format
= qemu_pixman_get_format(&pf
);
1116 assert(surface
->format
!= 0);
1117 surface
->image
= pixman_image_create_bits(surface
->format
,
1120 assert(surface
->image
!= NULL
);
1122 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1123 #ifdef HOST_WORDS_BIGENDIAN
1124 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1128 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1130 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1131 int linesize
= width
* 4;
1133 trace_displaysurface_create(surface
, width
, height
);
1134 qemu_alloc_display(surface
, width
, height
, linesize
,
1135 qemu_default_pixelformat(32), 0);
1139 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1140 int linesize
, uint8_t *data
,
1143 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1145 trace_displaysurface_create_from(surface
, width
, height
, bpp
, byteswap
);
1147 surface
->pf
= qemu_different_endianness_pixelformat(bpp
);
1149 surface
->pf
= qemu_default_pixelformat(bpp
);
1152 surface
->format
= qemu_pixman_get_format(&surface
->pf
);
1153 assert(surface
->format
!= 0);
1154 surface
->image
= pixman_image_create_bits(surface
->format
,
1156 (void *)data
, linesize
);
1157 assert(surface
->image
!= NULL
);
1159 #ifdef HOST_WORDS_BIGENDIAN
1160 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1166 void qemu_free_displaysurface(DisplaySurface
*surface
)
1168 if (surface
== NULL
) {
1171 trace_displaysurface_free(surface
);
1172 qemu_pixman_image_unref(surface
->image
);
1176 void register_displaychangelistener(DisplayState
*ds
,
1177 DisplayChangeListener
*dcl
)
1179 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1181 QLIST_INSERT_HEAD(&ds
->listeners
, dcl
, next
);
1182 gui_setup_refresh(ds
);
1183 if (dcl
->ops
->dpy_gfx_switch
) {
1184 dcl
->ops
->dpy_gfx_switch(dcl
, ds
->surface
);
1188 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1190 DisplayState
*ds
= dcl
->ds
;
1191 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1192 QLIST_REMOVE(dcl
, next
);
1193 gui_setup_refresh(ds
);
1196 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1198 DisplayState
*s
= con
->ds
;
1199 struct DisplayChangeListener
*dcl
;
1200 int width
= pixman_image_get_width(s
->surface
->image
);
1201 int height
= pixman_image_get_height(s
->surface
->image
);
1207 w
= MIN(w
, width
- x
);
1208 h
= MIN(h
, height
- y
);
1210 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1211 if (dcl
->ops
->dpy_gfx_update
) {
1212 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1217 void dpy_gfx_replace_surface(QemuConsole
*con
,
1218 DisplaySurface
*surface
)
1220 DisplayState
*s
= con
->ds
;
1221 DisplaySurface
*old_surface
= s
->surface
;
1222 struct DisplayChangeListener
*dcl
;
1224 s
->surface
= surface
;
1225 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1226 if (dcl
->ops
->dpy_gfx_switch
) {
1227 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1230 qemu_free_displaysurface(old_surface
);
1233 void dpy_refresh(DisplayState
*s
)
1235 struct DisplayChangeListener
*dcl
;
1236 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1237 if (dcl
->ops
->dpy_refresh
) {
1238 dcl
->ops
->dpy_refresh(dcl
);
1243 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1244 int dst_x
, int dst_y
, int w
, int h
)
1246 DisplayState
*s
= con
->ds
;
1247 struct DisplayChangeListener
*dcl
;
1248 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1249 if (dcl
->ops
->dpy_gfx_copy
) {
1250 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1252 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1257 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1259 DisplayState
*s
= con
->ds
;
1260 struct DisplayChangeListener
*dcl
;
1261 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1262 if (dcl
->ops
->dpy_text_cursor
) {
1263 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1268 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1270 DisplayState
*s
= con
->ds
;
1271 struct DisplayChangeListener
*dcl
;
1272 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1273 if (dcl
->ops
->dpy_text_update
) {
1274 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1279 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1281 DisplayState
*s
= con
->ds
;
1282 struct DisplayChangeListener
*dcl
;
1283 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1284 if (dcl
->ops
->dpy_text_resize
) {
1285 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1290 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1292 DisplayState
*s
= con
->ds
;
1293 struct DisplayChangeListener
*dcl
;
1294 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1295 if (dcl
->ops
->dpy_mouse_set
) {
1296 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1301 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1303 DisplayState
*s
= con
->ds
;
1304 struct DisplayChangeListener
*dcl
;
1305 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1306 if (dcl
->ops
->dpy_cursor_define
) {
1307 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1312 bool dpy_cursor_define_supported(QemuConsole
*con
)
1314 DisplayState
*s
= con
->ds
;
1315 struct DisplayChangeListener
*dcl
;
1316 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1317 if (dcl
->ops
->dpy_cursor_define
) {
1324 static void dumb_display_init(void)
1326 DisplayState
*ds
= g_malloc0(sizeof(DisplayState
));
1330 if (is_fixedsize_console()) {
1331 width
= active_console
->g_width
;
1332 height
= active_console
->g_height
;
1334 ds
->surface
= qemu_create_displaysurface(width
, height
);
1336 register_displaystate(ds
);
1339 /***********************************************************/
1340 /* register display */
1342 void register_displaystate(DisplayState
*ds
)
1352 DisplayState
*get_displaystate(void)
1354 if (!display_state
) {
1355 dumb_display_init ();
1357 return display_state
;
1360 QemuConsole
*graphic_console_init(vga_hw_update_ptr update
,
1361 vga_hw_invalidate_ptr invalidate
,
1362 vga_hw_screen_dump_ptr screen_dump
,
1363 vga_hw_text_update_ptr text_update
,
1369 ds
= (DisplayState
*) g_malloc0(sizeof(DisplayState
));
1370 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1371 s
->hw_update
= update
;
1372 s
->hw_invalidate
= invalidate
;
1373 s
->hw_screen_dump
= screen_dump
;
1374 s
->hw_text_update
= text_update
;
1377 ds
->surface
= qemu_create_displaysurface(640, 480);
1379 register_displaystate(ds
);
1383 int is_graphic_console(void)
1385 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1388 int is_fixedsize_console(void)
1390 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1393 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1395 QemuConsole
*s
= chr
->opaque
;
1400 static void text_console_update_cursor(void *opaque
)
1402 QemuConsole
*s
= opaque
;
1404 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1405 vga_hw_invalidate();
1406 qemu_mod_timer(s
->cursor_timer
,
1407 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
1410 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1416 chr
->chr_write
= console_puts
;
1418 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1419 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1420 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1425 s
->total_height
= DEFAULT_BACKSCROLL
;
1428 if (s
->console_type
== TEXT_CONSOLE
) {
1429 s
->g_width
= surface_width(s
->ds
->surface
);
1430 s
->g_height
= surface_height(s
->ds
->surface
);
1434 qemu_new_timer_ms(rt_clock
, text_console_update_cursor
, s
);
1436 s
->hw_invalidate
= text_console_invalidate
;
1437 s
->hw_text_update
= text_console_update
;
1440 /* Set text attribute defaults */
1441 s
->t_attrib_default
.bold
= 0;
1442 s
->t_attrib_default
.uline
= 0;
1443 s
->t_attrib_default
.blink
= 0;
1444 s
->t_attrib_default
.invers
= 0;
1445 s
->t_attrib_default
.unvisible
= 0;
1446 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1447 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1448 /* set current text attributes to default */
1449 s
->t_attrib
= s
->t_attrib_default
;
1450 text_console_resize(s
);
1456 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1457 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1458 console_puts(chr
, (uint8_t*)msg
, len
);
1459 s
->t_attrib
= s
->t_attrib_default
;
1462 qemu_chr_be_generic_open(chr
);
1467 static CharDriverState
*text_console_init(ChardevVC
*vc
)
1469 CharDriverState
*chr
;
1472 unsigned height
= 0;
1474 chr
= g_malloc0(sizeof(CharDriverState
));
1476 if (vc
->has_width
) {
1478 } else if (vc
->has_cols
) {
1479 width
= vc
->cols
* FONT_WIDTH
;
1482 if (vc
->has_height
) {
1483 height
= vc
->height
;
1484 } else if (vc
->has_rows
) {
1485 height
= vc
->rows
* FONT_HEIGHT
;
1488 if (width
== 0 || height
== 0) {
1489 s
= new_console(NULL
, TEXT_CONSOLE
);
1491 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1501 s
->g_height
= height
;
1503 chr
->chr_set_echo
= text_console_set_echo
;
1507 static VcHandler
*vc_handler
= text_console_init
;
1509 CharDriverState
*vc_init(ChardevVC
*vc
)
1511 return vc_handler(vc
);
1514 void register_vc_handler(VcHandler
*handler
)
1516 vc_handler
= handler
;
1519 void text_consoles_set_display(DisplayState
*ds
)
1523 for (i
= 0; i
< nb_consoles
; i
++) {
1524 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
) {
1525 text_console_do_init(consoles
[i
]->chr
, ds
);
1530 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
1533 s
->g_height
= height
;
1534 if (is_graphic_console()) {
1535 DisplaySurface
*surface
;
1536 surface
= qemu_create_displaysurface(width
, height
);
1537 dpy_gfx_replace_surface(s
, surface
);
1541 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
1542 int dst_x
, int dst_y
, int w
, int h
)
1544 if (is_graphic_console()) {
1545 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1549 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
1551 return console
->ds
->surface
;
1554 DisplayState
*qemu_console_displaystate(QemuConsole
*console
)
1559 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1563 memset(&pf
, 0x00, sizeof(PixelFormat
));
1565 pf
.bits_per_pixel
= bpp
;
1566 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1567 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1571 pf
.rmask
= 0x000000FF;
1572 pf
.gmask
= 0x0000FF00;
1573 pf
.bmask
= 0x00FF0000;
1585 pf
.rmask
= 0x0000FF00;
1586 pf
.gmask
= 0x00FF0000;
1587 pf
.bmask
= 0xFF000000;
1588 pf
.amask
= 0x00000000;
1608 PixelFormat
qemu_default_pixelformat(int bpp
)
1612 memset(&pf
, 0x00, sizeof(PixelFormat
));
1614 pf
.bits_per_pixel
= bpp
;
1615 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1616 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1620 pf
.bits_per_pixel
= 16;
1621 pf
.rmask
= 0x00007c00;
1622 pf
.gmask
= 0x000003E0;
1623 pf
.bmask
= 0x0000001F;
1635 pf
.rmask
= 0x0000F800;
1636 pf
.gmask
= 0x000007E0;
1637 pf
.bmask
= 0x0000001F;
1649 pf
.rmask
= 0x00FF0000;
1650 pf
.gmask
= 0x0000FF00;
1651 pf
.bmask
= 0x000000FF;
1663 pf
.rmask
= 0x00FF0000;
1664 pf
.gmask
= 0x0000FF00;
1665 pf
.bmask
= 0x000000FF;
1682 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
1687 backend
->vc
= g_new0(ChardevVC
, 1);
1689 val
= qemu_opt_get_number(opts
, "width", 0);
1691 backend
->vc
->has_width
= true;
1692 backend
->vc
->width
= val
;
1695 val
= qemu_opt_get_number(opts
, "height", 0);
1697 backend
->vc
->has_height
= true;
1698 backend
->vc
->height
= val
;
1701 val
= qemu_opt_get_number(opts
, "cols", 0);
1703 backend
->vc
->has_cols
= true;
1704 backend
->vc
->cols
= val
;
1707 val
= qemu_opt_get_number(opts
, "rows", 0);
1709 backend
->vc
->has_rows
= true;
1710 backend
->vc
->rows
= val
;
1714 static void register_types(void)
1716 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC
,
1720 type_init(register_types
);