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 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
168 void vga_hw_update(void)
170 if (active_console
&& active_console
->hw_update
)
171 active_console
->hw_update(active_console
->hw
);
174 void vga_hw_invalidate(void)
176 if (active_console
&& active_console
->hw_invalidate
)
177 active_console
->hw_invalidate(active_console
->hw
);
180 void qmp_screendump(const char *filename
, Error
**errp
)
182 QemuConsole
*previous_active_console
;
185 previous_active_console
= active_console
;
186 cswitch
= previous_active_console
&& previous_active_console
->index
!= 0;
188 /* There is currently no way of specifying which screen we want to dump,
189 so always dump the first one. */
193 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
194 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
, cswitch
, errp
);
196 error_setg(errp
, "device doesn't support screendump");
200 console_select(previous_active_console
->index
);
204 void vga_hw_text_update(console_ch_t
*chardata
)
206 if (active_console
&& active_console
->hw_text_update
)
207 active_console
->hw_text_update(active_console
->hw
, chardata
);
210 static void vga_fill_rect(QemuConsole
*con
,
211 int posx
, int posy
, int width
, int height
,
212 pixman_color_t color
)
214 DisplaySurface
*surface
= qemu_console_surface(con
);
215 pixman_rectangle16_t rect
= {
216 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
219 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
223 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
224 static void vga_bitblt(QemuConsole
*con
,
225 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
227 DisplaySurface
*surface
= qemu_console_surface(con
);
229 pixman_image_composite(PIXMAN_OP_SRC
,
230 surface
->image
, NULL
, surface
->image
,
231 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
234 /***********************************************************/
235 /* basic char display */
237 #define FONT_HEIGHT 16
242 #ifndef CONFIG_CURSES
255 #define QEMU_RGB(r, g, b) \
256 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
258 static const pixman_color_t color_table_rgb
[2][8] = {
260 QEMU_RGB(0x00, 0x00, 0x00), /* black */
261 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
262 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
263 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
264 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
265 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
266 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
267 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
270 QEMU_RGB(0x00, 0x00, 0x00), /* black */
271 QEMU_RGB(0xff, 0x00, 0x00), /* red */
272 QEMU_RGB(0x00, 0xff, 0x00), /* green */
273 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
274 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
275 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
276 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
277 QEMU_RGB(0xff, 0xff, 0xff), /* white */
282 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
284 if (t_attrib
->bold
) {
289 if (t_attrib
->uline
) {
294 if (t_attrib
->blink
) {
299 if (t_attrib
->invers
) {
304 if (t_attrib
->unvisible
) {
310 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
314 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
315 TextAttributes
*t_attrib
)
317 static pixman_image_t
*glyphs
[256];
318 DisplaySurface
*surface
= qemu_console_surface(s
);
319 pixman_color_t fgcol
, bgcol
;
321 if (t_attrib
->invers
) {
322 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
323 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
325 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
326 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
330 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
332 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
333 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
336 static void text_console_resize(QemuConsole
*s
)
338 TextCell
*cells
, *c
, *c1
;
339 int w1
, x
, y
, last_width
;
341 last_width
= s
->width
;
342 s
->width
= s
->g_width
/ FONT_WIDTH
;
343 s
->height
= s
->g_height
/ FONT_HEIGHT
;
349 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
350 for(y
= 0; y
< s
->total_height
; y
++) {
351 c
= &cells
[y
* s
->width
];
353 c1
= &s
->cells
[y
* last_width
];
354 for(x
= 0; x
< w1
; x
++) {
358 for(x
= w1
; x
< s
->width
; x
++) {
360 c
->t_attrib
= s
->t_attrib_default
;
368 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
370 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
371 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
372 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
373 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
376 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
378 if (s
->update_x0
> x
* FONT_WIDTH
)
379 s
->update_x0
= x
* FONT_WIDTH
;
380 if (s
->update_y0
> y
* FONT_HEIGHT
)
381 s
->update_y0
= y
* FONT_HEIGHT
;
382 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
383 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
384 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
385 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
388 static void update_xy(QemuConsole
*s
, int x
, int y
)
393 if (s
!= active_console
) {
397 if (s
->ds
->have_text
) {
398 text_update_xy(s
, x
, y
);
401 if (s
->ds
->have_gfx
) {
402 y1
= (s
->y_base
+ y
) % s
->total_height
;
403 y2
= y1
- s
->y_displayed
;
405 y2
+= s
->total_height
;
406 if (y2
< s
->height
) {
407 c
= &s
->cells
[y1
* s
->width
+ x
];
408 vga_putcharxy(s
, x
, y2
, c
->ch
,
410 invalidate_xy(s
, x
, y2
);
415 static void console_show_cursor(QemuConsole
*s
, int show
)
421 if (s
!= active_console
) {
425 if (s
->ds
->have_text
) {
426 s
->cursor_invalidate
= 1;
429 if (s
->ds
->have_gfx
) {
433 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
434 y
= y1
- s
->y_displayed
;
436 y
+= s
->total_height
;
438 c
= &s
->cells
[y1
* s
->width
+ x
];
439 if (show
&& s
->cursor_visible_phase
) {
440 TextAttributes t_attrib
= s
->t_attrib_default
;
441 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
442 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
444 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
446 invalidate_xy(s
, x
, y
);
451 static void console_refresh(QemuConsole
*s
)
453 DisplaySurface
*surface
= qemu_console_surface(s
);
457 if (s
!= active_console
)
460 if (s
->ds
->have_text
) {
463 s
->text_x
[1] = s
->width
- 1;
464 s
->text_y
[1] = s
->height
- 1;
465 s
->cursor_invalidate
= 1;
468 if (s
->ds
->have_gfx
) {
469 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
470 color_table_rgb
[0][COLOR_BLACK
]);
472 for (y
= 0; y
< s
->height
; y
++) {
473 c
= s
->cells
+ y1
* s
->width
;
474 for (x
= 0; x
< s
->width
; x
++) {
475 vga_putcharxy(s
, x
, y
, c
->ch
,
479 if (++y1
== s
->total_height
) {
483 console_show_cursor(s
, 1);
484 dpy_gfx_update(s
, 0, 0,
485 surface_width(surface
), surface_height(surface
));
489 static void console_scroll(int ydelta
)
495 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
499 for(i
= 0; i
< ydelta
; i
++) {
500 if (s
->y_displayed
== s
->y_base
)
502 if (++s
->y_displayed
== s
->total_height
)
507 i
= s
->backscroll_height
;
508 if (i
> s
->total_height
- s
->height
)
509 i
= s
->total_height
- s
->height
;
512 y1
+= s
->total_height
;
513 for(i
= 0; i
< ydelta
; i
++) {
514 if (s
->y_displayed
== y1
)
516 if (--s
->y_displayed
< 0)
517 s
->y_displayed
= s
->total_height
- 1;
523 static void console_put_lf(QemuConsole
*s
)
529 if (s
->y
>= s
->height
) {
530 s
->y
= s
->height
- 1;
532 if (s
->y_displayed
== s
->y_base
) {
533 if (++s
->y_displayed
== s
->total_height
)
536 if (++s
->y_base
== s
->total_height
)
538 if (s
->backscroll_height
< s
->total_height
)
539 s
->backscroll_height
++;
540 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
541 c
= &s
->cells
[y1
* s
->width
];
542 for(x
= 0; x
< s
->width
; x
++) {
544 c
->t_attrib
= s
->t_attrib_default
;
547 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
548 if (s
->ds
->have_text
) {
551 s
->text_x
[1] = s
->width
- 1;
552 s
->text_y
[1] = s
->height
- 1;
555 if (s
->ds
->have_gfx
) {
556 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
557 s
->width
* FONT_WIDTH
,
558 (s
->height
- 1) * FONT_HEIGHT
);
559 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
560 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
561 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
564 s
->update_x1
= s
->width
* FONT_WIDTH
;
565 s
->update_y1
= s
->height
* FONT_HEIGHT
;
571 /* Set console attributes depending on the current escape codes.
572 * NOTE: I know this code is not very efficient (checking every color for it
573 * self) but it is more readable and better maintainable.
575 static void console_handle_escape(QemuConsole
*s
)
579 for (i
=0; i
<s
->nb_esc_params
; i
++) {
580 switch (s
->esc_params
[i
]) {
581 case 0: /* reset all console attributes to default */
582 s
->t_attrib
= s
->t_attrib_default
;
585 s
->t_attrib
.bold
= 1;
588 s
->t_attrib
.uline
= 1;
591 s
->t_attrib
.blink
= 1;
594 s
->t_attrib
.invers
= 1;
597 s
->t_attrib
.unvisible
= 1;
600 s
->t_attrib
.bold
= 0;
603 s
->t_attrib
.uline
= 0;
606 s
->t_attrib
.blink
= 0;
609 s
->t_attrib
.invers
= 0;
612 s
->t_attrib
.unvisible
= 0;
614 /* set foreground color */
616 s
->t_attrib
.fgcol
=COLOR_BLACK
;
619 s
->t_attrib
.fgcol
=COLOR_RED
;
622 s
->t_attrib
.fgcol
=COLOR_GREEN
;
625 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
628 s
->t_attrib
.fgcol
=COLOR_BLUE
;
631 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
634 s
->t_attrib
.fgcol
=COLOR_CYAN
;
637 s
->t_attrib
.fgcol
=COLOR_WHITE
;
639 /* set background color */
641 s
->t_attrib
.bgcol
=COLOR_BLACK
;
644 s
->t_attrib
.bgcol
=COLOR_RED
;
647 s
->t_attrib
.bgcol
=COLOR_GREEN
;
650 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
653 s
->t_attrib
.bgcol
=COLOR_BLUE
;
656 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
659 s
->t_attrib
.bgcol
=COLOR_CYAN
;
662 s
->t_attrib
.bgcol
=COLOR_WHITE
;
668 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
670 int y1
= (s
->y_base
+ y
) % s
->total_height
;
671 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
673 c
->t_attrib
= s
->t_attrib_default
;
677 /* set cursor, checking bounds */
678 static void set_cursor(QemuConsole
*s
, int x
, int y
)
686 if (y
>= s
->height
) {
697 static void console_putchar(QemuConsole
*s
, int ch
)
706 case '\r': /* carriage return */
709 case '\n': /* newline */
712 case '\b': /* backspace */
716 case '\t': /* tabspace */
717 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
721 s
->x
= s
->x
+ (8 - (s
->x
% 8));
724 case '\a': /* alert aka. bell */
725 /* TODO: has to be implemented */
728 /* SI (shift in), character set 0 (ignored) */
731 /* SO (shift out), character set 1 (ignored) */
733 case 27: /* esc (introducing an escape sequence) */
734 s
->state
= TTY_STATE_ESC
;
737 if (s
->x
>= s
->width
) {
742 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
743 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
745 c
->t_attrib
= s
->t_attrib
;
746 update_xy(s
, s
->x
, s
->y
);
751 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
753 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
754 s
->esc_params
[i
] = 0;
755 s
->nb_esc_params
= 0;
756 s
->state
= TTY_STATE_CSI
;
758 s
->state
= TTY_STATE_NORM
;
761 case TTY_STATE_CSI
: /* handle escape sequence parameters */
762 if (ch
>= '0' && ch
<= '9') {
763 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
764 int *param
= &s
->esc_params
[s
->nb_esc_params
];
765 int digit
= (ch
- '0');
767 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
768 *param
* 10 + digit
: INT_MAX
;
771 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
776 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
777 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
779 s
->state
= TTY_STATE_NORM
;
783 if (s
->esc_params
[0] == 0) {
784 s
->esc_params
[0] = 1;
786 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
789 /* move cursor down */
790 if (s
->esc_params
[0] == 0) {
791 s
->esc_params
[0] = 1;
793 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
796 /* move cursor right */
797 if (s
->esc_params
[0] == 0) {
798 s
->esc_params
[0] = 1;
800 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
803 /* move cursor left */
804 if (s
->esc_params
[0] == 0) {
805 s
->esc_params
[0] = 1;
807 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
810 /* move cursor to column */
811 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
815 /* move cursor to row, column */
816 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
819 switch (s
->esc_params
[0]) {
821 /* clear to end of screen */
822 for (y
= s
->y
; y
< s
->height
; y
++) {
823 for (x
= 0; x
< s
->width
; x
++) {
824 if (y
== s
->y
&& x
< s
->x
) {
827 console_clear_xy(s
, x
, y
);
832 /* clear from beginning of screen */
833 for (y
= 0; y
<= s
->y
; y
++) {
834 for (x
= 0; x
< s
->width
; x
++) {
835 if (y
== s
->y
&& x
> s
->x
) {
838 console_clear_xy(s
, x
, y
);
843 /* clear entire screen */
844 for (y
= 0; y
<= s
->height
; y
++) {
845 for (x
= 0; x
< s
->width
; x
++) {
846 console_clear_xy(s
, x
, y
);
853 switch (s
->esc_params
[0]) {
856 for(x
= s
->x
; x
< s
->width
; x
++) {
857 console_clear_xy(s
, x
, s
->y
);
861 /* clear from beginning of line */
862 for (x
= 0; x
<= s
->x
; x
++) {
863 console_clear_xy(s
, x
, s
->y
);
867 /* clear entire line */
868 for(x
= 0; x
< s
->width
; x
++) {
869 console_clear_xy(s
, x
, s
->y
);
875 console_handle_escape(s
);
878 /* report cursor position */
879 /* TODO: send ESC[row;colR */
882 /* save cursor position */
887 /* restore cursor position */
893 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
902 void console_select(unsigned int index
)
904 DisplaySurface
*surface
;
907 if (index
>= MAX_CONSOLES
)
910 trace_console_select(index
);
911 if (active_console
) {
912 surface
= qemu_console_surface(active_console
);
913 active_console
->g_width
= surface_width(surface
);
914 active_console
->g_height
= surface_height(surface
);
918 DisplayState
*ds
= s
->ds
;
920 if (active_console
&& active_console
->cursor_timer
) {
921 qemu_del_timer(active_console
->cursor_timer
);
925 surface
= qemu_create_displaysurface(s
->g_width
, s
->g_height
);
926 dpy_gfx_replace_surface(s
, surface
);
929 dpy_text_resize(s
, s
->width
, s
->height
);
931 if (s
->cursor_timer
) {
932 qemu_mod_timer(s
->cursor_timer
,
933 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
939 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
941 QemuConsole
*s
= chr
->opaque
;
944 s
->update_x0
= s
->width
* FONT_WIDTH
;
945 s
->update_y0
= s
->height
* FONT_HEIGHT
;
948 console_show_cursor(s
, 0);
949 for(i
= 0; i
< len
; i
++) {
950 console_putchar(s
, buf
[i
]);
952 console_show_cursor(s
, 1);
953 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
954 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
955 s
->update_x1
- s
->update_x0
,
956 s
->update_y1
- s
->update_y0
);
961 static void kbd_send_chars(void *opaque
)
963 QemuConsole
*s
= opaque
;
967 len
= qemu_chr_be_can_write(s
->chr
);
968 if (len
> s
->out_fifo
.count
)
969 len
= s
->out_fifo
.count
;
971 if (len
> sizeof(buf
))
973 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
974 qemu_chr_be_write(s
->chr
, buf
, len
);
976 /* characters are pending: we send them a bit later (XXX:
977 horrible, should change char device API) */
978 if (s
->out_fifo
.count
> 0) {
979 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
983 /* called when an ascii key is pressed */
984 void kbd_put_keysym(int keysym
)
991 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
995 case QEMU_KEY_CTRL_UP
:
998 case QEMU_KEY_CTRL_DOWN
:
1001 case QEMU_KEY_CTRL_PAGEUP
:
1002 console_scroll(-10);
1004 case QEMU_KEY_CTRL_PAGEDOWN
:
1008 /* convert the QEMU keysym to VT100 key string */
1010 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1013 c
= keysym
- 0xe100;
1015 *q
++ = '0' + (c
/ 10);
1016 *q
++ = '0' + (c
% 10);
1018 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1021 *q
++ = keysym
& 0xff;
1022 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1023 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1029 console_puts(s
->chr
, buf
, q
- buf
);
1031 if (s
->chr
->chr_read
) {
1032 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1039 static void text_console_invalidate(void *opaque
)
1041 QemuConsole
*s
= (QemuConsole
*) opaque
;
1042 DisplaySurface
*surface
= qemu_console_surface(s
);
1044 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1045 s
->g_width
= surface_width(surface
);
1046 s
->g_height
= surface_height(surface
);
1047 text_console_resize(s
);
1052 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1054 QemuConsole
*s
= (QemuConsole
*) opaque
;
1057 if (s
->text_x
[0] <= s
->text_x
[1]) {
1058 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1059 chardata
+= s
->text_y
[0] * s
->width
;
1060 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1061 for (j
= 0; j
< s
->width
; j
++, src
++)
1062 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1063 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1064 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1065 (s
->cells
[src
].t_attrib
.bold
<< 21));
1066 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1067 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1068 s
->text_x
[0] = s
->width
;
1069 s
->text_y
[0] = s
->height
;
1073 if (s
->cursor_invalidate
) {
1074 dpy_text_cursor(s
, s
->x
, s
->y
);
1075 s
->cursor_invalidate
= 0;
1079 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1084 if (nb_consoles
>= MAX_CONSOLES
)
1086 s
= g_malloc0(sizeof(QemuConsole
));
1087 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1088 (console_type
== GRAPHIC_CONSOLE
))) {
1092 s
->console_type
= console_type
;
1093 if (console_type
!= GRAPHIC_CONSOLE
) {
1094 s
->index
= nb_consoles
;
1095 consoles
[nb_consoles
++] = s
;
1097 /* HACK: Put graphical consoles before text consoles. */
1098 for (i
= nb_consoles
; i
> 0; i
--) {
1099 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1101 consoles
[i
] = consoles
[i
- 1];
1102 consoles
[i
]->index
= i
;
1111 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1112 int linesize
, PixelFormat pf
, int newflags
)
1116 qemu_pixman_image_unref(surface
->image
);
1117 surface
->image
= NULL
;
1119 surface
->format
= qemu_pixman_get_format(&pf
);
1120 assert(surface
->format
!= 0);
1121 surface
->image
= pixman_image_create_bits(surface
->format
,
1124 assert(surface
->image
!= NULL
);
1126 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1127 #ifdef HOST_WORDS_BIGENDIAN
1128 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1132 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1134 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1135 int linesize
= width
* 4;
1137 trace_displaysurface_create(surface
, width
, height
);
1138 qemu_alloc_display(surface
, width
, height
, linesize
,
1139 qemu_default_pixelformat(32), 0);
1143 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1144 int linesize
, uint8_t *data
,
1147 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1149 trace_displaysurface_create_from(surface
, width
, height
, bpp
, byteswap
);
1151 surface
->pf
= qemu_different_endianness_pixelformat(bpp
);
1153 surface
->pf
= qemu_default_pixelformat(bpp
);
1156 surface
->format
= qemu_pixman_get_format(&surface
->pf
);
1157 assert(surface
->format
!= 0);
1158 surface
->image
= pixman_image_create_bits(surface
->format
,
1160 (void *)data
, linesize
);
1161 assert(surface
->image
!= NULL
);
1163 #ifdef HOST_WORDS_BIGENDIAN
1164 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1170 void qemu_free_displaysurface(DisplaySurface
*surface
)
1172 if (surface
== NULL
) {
1175 trace_displaysurface_free(surface
);
1176 qemu_pixman_image_unref(surface
->image
);
1180 void register_displaychangelistener(DisplayState
*ds
,
1181 DisplayChangeListener
*dcl
)
1183 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1185 QLIST_INSERT_HEAD(&ds
->listeners
, dcl
, next
);
1186 gui_setup_refresh(ds
);
1187 if (dcl
->ops
->dpy_gfx_switch
) {
1188 dcl
->ops
->dpy_gfx_switch(dcl
, ds
->surface
);
1192 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1194 DisplayState
*ds
= dcl
->ds
;
1195 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1196 QLIST_REMOVE(dcl
, next
);
1197 gui_setup_refresh(ds
);
1200 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1202 DisplayState
*s
= con
->ds
;
1203 struct DisplayChangeListener
*dcl
;
1204 int width
= pixman_image_get_width(s
->surface
->image
);
1205 int height
= pixman_image_get_height(s
->surface
->image
);
1211 w
= MIN(w
, width
- x
);
1212 h
= MIN(h
, height
- y
);
1214 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1215 if (dcl
->ops
->dpy_gfx_update
) {
1216 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1221 void dpy_gfx_replace_surface(QemuConsole
*con
,
1222 DisplaySurface
*surface
)
1224 DisplayState
*s
= con
->ds
;
1225 DisplaySurface
*old_surface
= s
->surface
;
1226 struct DisplayChangeListener
*dcl
;
1228 s
->surface
= surface
;
1229 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1230 if (dcl
->ops
->dpy_gfx_switch
) {
1231 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1234 qemu_free_displaysurface(old_surface
);
1237 void dpy_refresh(DisplayState
*s
)
1239 struct DisplayChangeListener
*dcl
;
1240 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1241 if (dcl
->ops
->dpy_refresh
) {
1242 dcl
->ops
->dpy_refresh(dcl
);
1247 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1248 int dst_x
, int dst_y
, int w
, int h
)
1250 DisplayState
*s
= con
->ds
;
1251 struct DisplayChangeListener
*dcl
;
1252 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1253 if (dcl
->ops
->dpy_gfx_copy
) {
1254 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1256 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1261 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1263 DisplayState
*s
= con
->ds
;
1264 struct DisplayChangeListener
*dcl
;
1265 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1266 if (dcl
->ops
->dpy_text_cursor
) {
1267 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1272 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1274 DisplayState
*s
= con
->ds
;
1275 struct DisplayChangeListener
*dcl
;
1276 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1277 if (dcl
->ops
->dpy_text_update
) {
1278 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1283 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1285 DisplayState
*s
= con
->ds
;
1286 struct DisplayChangeListener
*dcl
;
1287 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1288 if (dcl
->ops
->dpy_text_resize
) {
1289 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1294 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1296 DisplayState
*s
= con
->ds
;
1297 struct DisplayChangeListener
*dcl
;
1298 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1299 if (dcl
->ops
->dpy_mouse_set
) {
1300 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1305 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1307 DisplayState
*s
= con
->ds
;
1308 struct DisplayChangeListener
*dcl
;
1309 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1310 if (dcl
->ops
->dpy_cursor_define
) {
1311 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1316 bool dpy_cursor_define_supported(QemuConsole
*con
)
1318 DisplayState
*s
= con
->ds
;
1319 struct DisplayChangeListener
*dcl
;
1320 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1321 if (dcl
->ops
->dpy_cursor_define
) {
1328 /***********************************************************/
1329 /* register display */
1331 /* console.c internal use only */
1332 static DisplayState
*get_alloc_displaystate(void)
1334 if (!display_state
) {
1335 display_state
= g_new0(DisplayState
, 1);
1337 return display_state
;
1341 * Called by main(), after creating QemuConsoles
1342 * and before initializing ui (sdl/vnc/...).
1344 DisplayState
*init_displaystate(void)
1348 if (!display_state
) {
1349 display_state
= g_new0(DisplayState
, 1);
1352 for (i
= 0; i
< nb_consoles
; i
++) {
1353 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1354 consoles
[i
]->ds
== NULL
) {
1355 text_console_do_init(consoles
[i
]->chr
, display_state
);
1359 return display_state
;
1362 QemuConsole
*graphic_console_init(vga_hw_update_ptr update
,
1363 vga_hw_invalidate_ptr invalidate
,
1364 vga_hw_screen_dump_ptr screen_dump
,
1365 vga_hw_text_update_ptr text_update
,
1373 ds
= get_alloc_displaystate();
1374 trace_console_gfx_new();
1375 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1376 s
->hw_update
= update
;
1377 s
->hw_invalidate
= invalidate
;
1378 s
->hw_screen_dump
= screen_dump
;
1379 s
->hw_text_update
= text_update
;
1383 ds
->surface
= qemu_create_displaysurface(width
, height
);
1388 int is_graphic_console(void)
1390 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1393 int is_fixedsize_console(void)
1395 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1398 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1400 QemuConsole
*s
= chr
->opaque
;
1405 static void text_console_update_cursor(void *opaque
)
1407 QemuConsole
*s
= opaque
;
1409 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1410 vga_hw_invalidate();
1411 qemu_mod_timer(s
->cursor_timer
,
1412 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
1415 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1421 chr
->chr_write
= console_puts
;
1423 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1424 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1425 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1430 s
->total_height
= DEFAULT_BACKSCROLL
;
1433 if (s
->console_type
== TEXT_CONSOLE
) {
1434 s
->g_width
= surface_width(s
->ds
->surface
);
1435 s
->g_height
= surface_height(s
->ds
->surface
);
1439 qemu_new_timer_ms(rt_clock
, text_console_update_cursor
, s
);
1441 s
->hw_invalidate
= text_console_invalidate
;
1442 s
->hw_text_update
= text_console_update
;
1445 /* Set text attribute defaults */
1446 s
->t_attrib_default
.bold
= 0;
1447 s
->t_attrib_default
.uline
= 0;
1448 s
->t_attrib_default
.blink
= 0;
1449 s
->t_attrib_default
.invers
= 0;
1450 s
->t_attrib_default
.unvisible
= 0;
1451 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1452 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1453 /* set current text attributes to default */
1454 s
->t_attrib
= s
->t_attrib_default
;
1455 text_console_resize(s
);
1461 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1462 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1463 console_puts(chr
, (uint8_t*)msg
, len
);
1464 s
->t_attrib
= s
->t_attrib_default
;
1467 qemu_chr_be_generic_open(chr
);
1472 static CharDriverState
*text_console_init(ChardevVC
*vc
)
1474 CharDriverState
*chr
;
1477 unsigned height
= 0;
1479 chr
= g_malloc0(sizeof(CharDriverState
));
1481 if (vc
->has_width
) {
1483 } else if (vc
->has_cols
) {
1484 width
= vc
->cols
* FONT_WIDTH
;
1487 if (vc
->has_height
) {
1488 height
= vc
->height
;
1489 } else if (vc
->has_rows
) {
1490 height
= vc
->rows
* FONT_HEIGHT
;
1493 trace_console_txt_new(width
, height
);
1494 if (width
== 0 || height
== 0) {
1495 s
= new_console(NULL
, TEXT_CONSOLE
);
1497 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1507 s
->g_height
= height
;
1509 chr
->chr_set_echo
= text_console_set_echo
;
1511 if (display_state
) {
1512 text_console_do_init(chr
, display_state
);
1517 static VcHandler
*vc_handler
= text_console_init
;
1519 CharDriverState
*vc_init(ChardevVC
*vc
)
1521 return vc_handler(vc
);
1524 void register_vc_handler(VcHandler
*handler
)
1526 vc_handler
= handler
;
1529 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
1532 s
->g_height
= height
;
1533 if (is_graphic_console()) {
1534 DisplaySurface
*surface
;
1535 surface
= qemu_create_displaysurface(width
, height
);
1536 dpy_gfx_replace_surface(s
, surface
);
1540 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
1541 int dst_x
, int dst_y
, int w
, int h
)
1543 if (is_graphic_console()) {
1544 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1548 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
1550 return console
->ds
->surface
;
1553 DisplayState
*qemu_console_displaystate(QemuConsole
*console
)
1558 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1562 memset(&pf
, 0x00, sizeof(PixelFormat
));
1564 pf
.bits_per_pixel
= bpp
;
1565 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1566 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1570 pf
.rmask
= 0x000000FF;
1571 pf
.gmask
= 0x0000FF00;
1572 pf
.bmask
= 0x00FF0000;
1584 pf
.rmask
= 0x0000FF00;
1585 pf
.gmask
= 0x00FF0000;
1586 pf
.bmask
= 0xFF000000;
1587 pf
.amask
= 0x00000000;
1607 PixelFormat
qemu_default_pixelformat(int bpp
)
1611 memset(&pf
, 0x00, sizeof(PixelFormat
));
1613 pf
.bits_per_pixel
= bpp
;
1614 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1615 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1619 pf
.bits_per_pixel
= 16;
1620 pf
.rmask
= 0x00007c00;
1621 pf
.gmask
= 0x000003E0;
1622 pf
.bmask
= 0x0000001F;
1634 pf
.rmask
= 0x0000F800;
1635 pf
.gmask
= 0x000007E0;
1636 pf
.bmask
= 0x0000001F;
1648 pf
.rmask
= 0x00FF0000;
1649 pf
.gmask
= 0x0000FF00;
1650 pf
.bmask
= 0x000000FF;
1662 pf
.rmask
= 0x00FF0000;
1663 pf
.gmask
= 0x0000FF00;
1664 pf
.bmask
= 0x000000FF;
1681 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
1686 backend
->vc
= g_new0(ChardevVC
, 1);
1688 val
= qemu_opt_get_number(opts
, "width", 0);
1690 backend
->vc
->has_width
= true;
1691 backend
->vc
->width
= val
;
1694 val
= qemu_opt_get_number(opts
, "height", 0);
1696 backend
->vc
->has_height
= true;
1697 backend
->vc
->height
= val
;
1700 val
= qemu_opt_get_number(opts
, "cols", 0);
1702 backend
->vc
->has_cols
= true;
1703 backend
->vc
->cols
= val
;
1706 val
= qemu_opt_get_number(opts
, "rows", 0);
1708 backend
->vc
->has_rows
= true;
1709 backend
->vc
->rows
= val
;
1713 static void register_types(void)
1715 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC
,
1719 type_init(register_types
);