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
;
119 DisplaySurface
*surface
;
121 /* Graphic console state. */
122 graphic_hw_update_ptr hw_update
;
123 graphic_hw_invalidate_ptr hw_invalidate
;
124 graphic_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
);
167 static void dpy_gfx_switch_surface(DisplayState
*ds
,
168 DisplaySurface
*surface
);
170 void graphic_hw_update(QemuConsole
*con
)
173 con
= active_console
;
175 if (con
&& con
->hw_update
) {
176 con
->hw_update(con
->hw
);
180 void graphic_hw_invalidate(QemuConsole
*con
)
183 con
= active_console
;
185 if (con
&& con
->hw_invalidate
) {
186 con
->hw_invalidate(con
->hw
);
190 static void ppm_save(const char *filename
, struct DisplaySurface
*ds
,
193 int width
= pixman_image_get_width(ds
->image
);
194 int height
= pixman_image_get_height(ds
->image
);
198 pixman_image_t
*linebuf
;
200 trace_ppm_save(filename
, ds
);
201 f
= fopen(filename
, "wb");
203 error_setg(errp
, "failed to open file '%s': %s", filename
,
207 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
212 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
213 for (y
= 0; y
< height
; y
++) {
214 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
216 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
217 pixman_image_get_stride(linebuf
), f
);
225 qemu_pixman_image_unref(linebuf
);
230 error_setg(errp
, "failed to write to file '%s': %s", filename
,
236 void qmp_screendump(const char *filename
, Error
**errp
)
238 QemuConsole
*con
= consoles
[0];
239 DisplaySurface
*surface
;
242 error_setg(errp
, "There is no QemuConsole I can screendump from.");
246 graphic_hw_update(con
);
247 surface
= qemu_console_surface(con
);
248 ppm_save(filename
, surface
, errp
);
251 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
254 con
= active_console
;
256 if (con
&& con
->hw_text_update
)
257 con
->hw_text_update(con
->hw
, chardata
);
260 static void vga_fill_rect(QemuConsole
*con
,
261 int posx
, int posy
, int width
, int height
,
262 pixman_color_t color
)
264 DisplaySurface
*surface
= qemu_console_surface(con
);
265 pixman_rectangle16_t rect
= {
266 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
269 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
273 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
274 static void vga_bitblt(QemuConsole
*con
,
275 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
277 DisplaySurface
*surface
= qemu_console_surface(con
);
279 pixman_image_composite(PIXMAN_OP_SRC
,
280 surface
->image
, NULL
, surface
->image
,
281 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
284 /***********************************************************/
285 /* basic char display */
287 #define FONT_HEIGHT 16
292 #ifndef CONFIG_CURSES
305 #define QEMU_RGB(r, g, b) \
306 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
308 static const pixman_color_t color_table_rgb
[2][8] = {
310 QEMU_RGB(0x00, 0x00, 0x00), /* black */
311 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
312 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
313 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
314 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
315 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
316 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
317 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
320 QEMU_RGB(0x00, 0x00, 0x00), /* black */
321 QEMU_RGB(0xff, 0x00, 0x00), /* red */
322 QEMU_RGB(0x00, 0xff, 0x00), /* green */
323 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
324 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
325 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
326 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
327 QEMU_RGB(0xff, 0xff, 0xff), /* white */
332 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
334 if (t_attrib
->bold
) {
339 if (t_attrib
->uline
) {
344 if (t_attrib
->blink
) {
349 if (t_attrib
->invers
) {
354 if (t_attrib
->unvisible
) {
360 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
364 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
365 TextAttributes
*t_attrib
)
367 static pixman_image_t
*glyphs
[256];
368 DisplaySurface
*surface
= qemu_console_surface(s
);
369 pixman_color_t fgcol
, bgcol
;
371 if (t_attrib
->invers
) {
372 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
373 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
375 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
376 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
380 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
382 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
383 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
386 static void text_console_resize(QemuConsole
*s
)
388 TextCell
*cells
, *c
, *c1
;
389 int w1
, x
, y
, last_width
;
391 last_width
= s
->width
;
392 s
->width
= s
->g_width
/ FONT_WIDTH
;
393 s
->height
= s
->g_height
/ FONT_HEIGHT
;
399 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
400 for(y
= 0; y
< s
->total_height
; y
++) {
401 c
= &cells
[y
* s
->width
];
403 c1
= &s
->cells
[y
* last_width
];
404 for(x
= 0; x
< w1
; x
++) {
408 for(x
= w1
; x
< s
->width
; x
++) {
410 c
->t_attrib
= s
->t_attrib_default
;
418 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
420 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
421 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
422 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
423 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
426 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
428 if (s
->update_x0
> x
* FONT_WIDTH
)
429 s
->update_x0
= x
* FONT_WIDTH
;
430 if (s
->update_y0
> y
* FONT_HEIGHT
)
431 s
->update_y0
= y
* FONT_HEIGHT
;
432 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
433 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
434 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
435 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
438 static void update_xy(QemuConsole
*s
, int x
, int y
)
443 if (s
!= active_console
) {
447 if (s
->ds
->have_text
) {
448 text_update_xy(s
, x
, y
);
451 if (s
->ds
->have_gfx
) {
452 y1
= (s
->y_base
+ y
) % s
->total_height
;
453 y2
= y1
- s
->y_displayed
;
455 y2
+= s
->total_height
;
456 if (y2
< s
->height
) {
457 c
= &s
->cells
[y1
* s
->width
+ x
];
458 vga_putcharxy(s
, x
, y2
, c
->ch
,
460 invalidate_xy(s
, x
, y2
);
465 static void console_show_cursor(QemuConsole
*s
, int show
)
471 if (s
!= active_console
) {
475 if (s
->ds
->have_text
) {
476 s
->cursor_invalidate
= 1;
479 if (s
->ds
->have_gfx
) {
483 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
484 y
= y1
- s
->y_displayed
;
486 y
+= s
->total_height
;
488 c
= &s
->cells
[y1
* s
->width
+ x
];
489 if (show
&& s
->cursor_visible_phase
) {
490 TextAttributes t_attrib
= s
->t_attrib_default
;
491 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
492 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
494 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
496 invalidate_xy(s
, x
, y
);
501 static void console_refresh(QemuConsole
*s
)
503 DisplaySurface
*surface
= qemu_console_surface(s
);
507 if (s
!= active_console
)
510 if (s
->ds
->have_text
) {
513 s
->text_x
[1] = s
->width
- 1;
514 s
->text_y
[1] = s
->height
- 1;
515 s
->cursor_invalidate
= 1;
518 if (s
->ds
->have_gfx
) {
519 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
520 color_table_rgb
[0][COLOR_BLACK
]);
522 for (y
= 0; y
< s
->height
; y
++) {
523 c
= s
->cells
+ y1
* s
->width
;
524 for (x
= 0; x
< s
->width
; x
++) {
525 vga_putcharxy(s
, x
, y
, c
->ch
,
529 if (++y1
== s
->total_height
) {
533 console_show_cursor(s
, 1);
534 dpy_gfx_update(s
, 0, 0,
535 surface_width(surface
), surface_height(surface
));
539 static void console_scroll(int ydelta
)
545 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
549 for(i
= 0; i
< ydelta
; i
++) {
550 if (s
->y_displayed
== s
->y_base
)
552 if (++s
->y_displayed
== s
->total_height
)
557 i
= s
->backscroll_height
;
558 if (i
> s
->total_height
- s
->height
)
559 i
= s
->total_height
- s
->height
;
562 y1
+= s
->total_height
;
563 for(i
= 0; i
< ydelta
; i
++) {
564 if (s
->y_displayed
== y1
)
566 if (--s
->y_displayed
< 0)
567 s
->y_displayed
= s
->total_height
- 1;
573 static void console_put_lf(QemuConsole
*s
)
579 if (s
->y
>= s
->height
) {
580 s
->y
= s
->height
- 1;
582 if (s
->y_displayed
== s
->y_base
) {
583 if (++s
->y_displayed
== s
->total_height
)
586 if (++s
->y_base
== s
->total_height
)
588 if (s
->backscroll_height
< s
->total_height
)
589 s
->backscroll_height
++;
590 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
591 c
= &s
->cells
[y1
* s
->width
];
592 for(x
= 0; x
< s
->width
; x
++) {
594 c
->t_attrib
= s
->t_attrib_default
;
597 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
598 if (s
->ds
->have_text
) {
601 s
->text_x
[1] = s
->width
- 1;
602 s
->text_y
[1] = s
->height
- 1;
605 if (s
->ds
->have_gfx
) {
606 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
607 s
->width
* FONT_WIDTH
,
608 (s
->height
- 1) * FONT_HEIGHT
);
609 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
610 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
611 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
614 s
->update_x1
= s
->width
* FONT_WIDTH
;
615 s
->update_y1
= s
->height
* FONT_HEIGHT
;
621 /* Set console attributes depending on the current escape codes.
622 * NOTE: I know this code is not very efficient (checking every color for it
623 * self) but it is more readable and better maintainable.
625 static void console_handle_escape(QemuConsole
*s
)
629 for (i
=0; i
<s
->nb_esc_params
; i
++) {
630 switch (s
->esc_params
[i
]) {
631 case 0: /* reset all console attributes to default */
632 s
->t_attrib
= s
->t_attrib_default
;
635 s
->t_attrib
.bold
= 1;
638 s
->t_attrib
.uline
= 1;
641 s
->t_attrib
.blink
= 1;
644 s
->t_attrib
.invers
= 1;
647 s
->t_attrib
.unvisible
= 1;
650 s
->t_attrib
.bold
= 0;
653 s
->t_attrib
.uline
= 0;
656 s
->t_attrib
.blink
= 0;
659 s
->t_attrib
.invers
= 0;
662 s
->t_attrib
.unvisible
= 0;
664 /* set foreground color */
666 s
->t_attrib
.fgcol
=COLOR_BLACK
;
669 s
->t_attrib
.fgcol
=COLOR_RED
;
672 s
->t_attrib
.fgcol
=COLOR_GREEN
;
675 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
678 s
->t_attrib
.fgcol
=COLOR_BLUE
;
681 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
684 s
->t_attrib
.fgcol
=COLOR_CYAN
;
687 s
->t_attrib
.fgcol
=COLOR_WHITE
;
689 /* set background color */
691 s
->t_attrib
.bgcol
=COLOR_BLACK
;
694 s
->t_attrib
.bgcol
=COLOR_RED
;
697 s
->t_attrib
.bgcol
=COLOR_GREEN
;
700 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
703 s
->t_attrib
.bgcol
=COLOR_BLUE
;
706 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
709 s
->t_attrib
.bgcol
=COLOR_CYAN
;
712 s
->t_attrib
.bgcol
=COLOR_WHITE
;
718 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
720 int y1
= (s
->y_base
+ y
) % s
->total_height
;
721 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
723 c
->t_attrib
= s
->t_attrib_default
;
727 /* set cursor, checking bounds */
728 static void set_cursor(QemuConsole
*s
, int x
, int y
)
736 if (y
>= s
->height
) {
747 static void console_putchar(QemuConsole
*s
, int ch
)
756 case '\r': /* carriage return */
759 case '\n': /* newline */
762 case '\b': /* backspace */
766 case '\t': /* tabspace */
767 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
771 s
->x
= s
->x
+ (8 - (s
->x
% 8));
774 case '\a': /* alert aka. bell */
775 /* TODO: has to be implemented */
778 /* SI (shift in), character set 0 (ignored) */
781 /* SO (shift out), character set 1 (ignored) */
783 case 27: /* esc (introducing an escape sequence) */
784 s
->state
= TTY_STATE_ESC
;
787 if (s
->x
>= s
->width
) {
792 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
793 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
795 c
->t_attrib
= s
->t_attrib
;
796 update_xy(s
, s
->x
, s
->y
);
801 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
803 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
804 s
->esc_params
[i
] = 0;
805 s
->nb_esc_params
= 0;
806 s
->state
= TTY_STATE_CSI
;
808 s
->state
= TTY_STATE_NORM
;
811 case TTY_STATE_CSI
: /* handle escape sequence parameters */
812 if (ch
>= '0' && ch
<= '9') {
813 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
814 int *param
= &s
->esc_params
[s
->nb_esc_params
];
815 int digit
= (ch
- '0');
817 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
818 *param
* 10 + digit
: INT_MAX
;
821 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
826 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
827 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
829 s
->state
= TTY_STATE_NORM
;
833 if (s
->esc_params
[0] == 0) {
834 s
->esc_params
[0] = 1;
836 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
839 /* move cursor down */
840 if (s
->esc_params
[0] == 0) {
841 s
->esc_params
[0] = 1;
843 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
846 /* move cursor right */
847 if (s
->esc_params
[0] == 0) {
848 s
->esc_params
[0] = 1;
850 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
853 /* move cursor left */
854 if (s
->esc_params
[0] == 0) {
855 s
->esc_params
[0] = 1;
857 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
860 /* move cursor to column */
861 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
865 /* move cursor to row, column */
866 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
869 switch (s
->esc_params
[0]) {
871 /* clear to end of screen */
872 for (y
= s
->y
; y
< s
->height
; y
++) {
873 for (x
= 0; x
< s
->width
; x
++) {
874 if (y
== s
->y
&& x
< s
->x
) {
877 console_clear_xy(s
, x
, y
);
882 /* clear from beginning of screen */
883 for (y
= 0; y
<= s
->y
; y
++) {
884 for (x
= 0; x
< s
->width
; x
++) {
885 if (y
== s
->y
&& x
> s
->x
) {
888 console_clear_xy(s
, x
, y
);
893 /* clear entire screen */
894 for (y
= 0; y
<= s
->height
; y
++) {
895 for (x
= 0; x
< s
->width
; x
++) {
896 console_clear_xy(s
, x
, y
);
903 switch (s
->esc_params
[0]) {
906 for(x
= s
->x
; x
< s
->width
; x
++) {
907 console_clear_xy(s
, x
, s
->y
);
911 /* clear from beginning of line */
912 for (x
= 0; x
<= s
->x
; x
++) {
913 console_clear_xy(s
, x
, s
->y
);
917 /* clear entire line */
918 for(x
= 0; x
< s
->width
; x
++) {
919 console_clear_xy(s
, x
, s
->y
);
925 console_handle_escape(s
);
928 /* report cursor position */
929 /* TODO: send ESC[row;colR */
932 /* save cursor position */
937 /* restore cursor position */
943 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
952 void console_select(unsigned int index
)
954 DisplaySurface
*surface
;
957 if (index
>= MAX_CONSOLES
)
960 trace_console_select(index
);
961 if (active_console
) {
962 surface
= qemu_console_surface(active_console
);
963 active_console
->g_width
= surface_width(surface
);
964 active_console
->g_height
= surface_height(surface
);
968 DisplayState
*ds
= s
->ds
;
970 if (active_console
&& active_console
->cursor_timer
) {
971 qemu_del_timer(active_console
->cursor_timer
);
975 dpy_gfx_switch_surface(ds
, s
->surface
);
976 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
977 surface_height(s
->surface
));
980 dpy_text_resize(s
, s
->width
, s
->height
);
982 if (s
->cursor_timer
) {
983 qemu_mod_timer(s
->cursor_timer
,
984 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
989 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
991 QemuConsole
*s
= chr
->opaque
;
994 s
->update_x0
= s
->width
* FONT_WIDTH
;
995 s
->update_y0
= s
->height
* FONT_HEIGHT
;
998 console_show_cursor(s
, 0);
999 for(i
= 0; i
< len
; i
++) {
1000 console_putchar(s
, buf
[i
]);
1002 console_show_cursor(s
, 1);
1003 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1004 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1005 s
->update_x1
- s
->update_x0
,
1006 s
->update_y1
- s
->update_y0
);
1011 static void kbd_send_chars(void *opaque
)
1013 QemuConsole
*s
= opaque
;
1017 len
= qemu_chr_be_can_write(s
->chr
);
1018 if (len
> s
->out_fifo
.count
)
1019 len
= s
->out_fifo
.count
;
1021 if (len
> sizeof(buf
))
1023 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1024 qemu_chr_be_write(s
->chr
, buf
, len
);
1026 /* characters are pending: we send them a bit later (XXX:
1027 horrible, should change char device API) */
1028 if (s
->out_fifo
.count
> 0) {
1029 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
1033 /* called when an ascii key is pressed */
1034 void kbd_put_keysym(int keysym
)
1037 uint8_t buf
[16], *q
;
1041 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1045 case QEMU_KEY_CTRL_UP
:
1048 case QEMU_KEY_CTRL_DOWN
:
1051 case QEMU_KEY_CTRL_PAGEUP
:
1052 console_scroll(-10);
1054 case QEMU_KEY_CTRL_PAGEDOWN
:
1058 /* convert the QEMU keysym to VT100 key string */
1060 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1063 c
= keysym
- 0xe100;
1065 *q
++ = '0' + (c
/ 10);
1066 *q
++ = '0' + (c
% 10);
1068 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1071 *q
++ = keysym
& 0xff;
1072 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1073 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1079 console_puts(s
->chr
, buf
, q
- buf
);
1081 if (s
->chr
->chr_read
) {
1082 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1089 static void text_console_invalidate(void *opaque
)
1091 QemuConsole
*s
= (QemuConsole
*) opaque
;
1092 DisplaySurface
*surface
= qemu_console_surface(s
);
1094 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1095 s
->g_width
= surface_width(surface
);
1096 s
->g_height
= surface_height(surface
);
1097 text_console_resize(s
);
1102 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1104 QemuConsole
*s
= (QemuConsole
*) opaque
;
1107 if (s
->text_x
[0] <= s
->text_x
[1]) {
1108 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1109 chardata
+= s
->text_y
[0] * s
->width
;
1110 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1111 for (j
= 0; j
< s
->width
; j
++, src
++)
1112 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1113 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1114 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1115 (s
->cells
[src
].t_attrib
.bold
<< 21));
1116 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1117 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1118 s
->text_x
[0] = s
->width
;
1119 s
->text_y
[0] = s
->height
;
1123 if (s
->cursor_invalidate
) {
1124 dpy_text_cursor(s
, s
->x
, s
->y
);
1125 s
->cursor_invalidate
= 0;
1129 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1134 if (nb_consoles
>= MAX_CONSOLES
)
1136 s
= g_malloc0(sizeof(QemuConsole
));
1137 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1138 (console_type
== GRAPHIC_CONSOLE
))) {
1142 s
->console_type
= console_type
;
1143 if (console_type
!= GRAPHIC_CONSOLE
) {
1144 s
->index
= nb_consoles
;
1145 consoles
[nb_consoles
++] = s
;
1147 /* HACK: Put graphical consoles before text consoles. */
1148 for (i
= nb_consoles
; i
> 0; i
--) {
1149 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1151 consoles
[i
] = consoles
[i
- 1];
1152 consoles
[i
]->index
= i
;
1161 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1162 int linesize
, PixelFormat pf
, int newflags
)
1166 qemu_pixman_image_unref(surface
->image
);
1167 surface
->image
= NULL
;
1169 surface
->format
= qemu_pixman_get_format(&pf
);
1170 assert(surface
->format
!= 0);
1171 surface
->image
= pixman_image_create_bits(surface
->format
,
1174 assert(surface
->image
!= NULL
);
1176 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1177 #ifdef HOST_WORDS_BIGENDIAN
1178 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1182 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1184 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1185 int linesize
= width
* 4;
1187 trace_displaysurface_create(surface
, width
, height
);
1188 qemu_alloc_display(surface
, width
, height
, linesize
,
1189 qemu_default_pixelformat(32), 0);
1193 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1194 int linesize
, uint8_t *data
,
1197 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1199 trace_displaysurface_create_from(surface
, width
, height
, bpp
, byteswap
);
1201 surface
->pf
= qemu_different_endianness_pixelformat(bpp
);
1203 surface
->pf
= qemu_default_pixelformat(bpp
);
1206 surface
->format
= qemu_pixman_get_format(&surface
->pf
);
1207 assert(surface
->format
!= 0);
1208 surface
->image
= pixman_image_create_bits(surface
->format
,
1210 (void *)data
, linesize
);
1211 assert(surface
->image
!= NULL
);
1213 #ifdef HOST_WORDS_BIGENDIAN
1214 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1220 void qemu_free_displaysurface(DisplaySurface
*surface
)
1222 if (surface
== NULL
) {
1225 trace_displaysurface_free(surface
);
1226 qemu_pixman_image_unref(surface
->image
);
1230 void register_displaychangelistener(DisplayState
*ds
,
1231 DisplayChangeListener
*dcl
)
1233 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1235 QLIST_INSERT_HEAD(&ds
->listeners
, dcl
, next
);
1236 gui_setup_refresh(ds
);
1237 if (dcl
->ops
->dpy_gfx_switch
&& active_console
) {
1238 dcl
->ops
->dpy_gfx_switch(dcl
, active_console
->surface
);
1242 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1244 DisplayState
*ds
= dcl
->ds
;
1245 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1246 QLIST_REMOVE(dcl
, next
);
1247 gui_setup_refresh(ds
);
1250 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1252 DisplayState
*s
= con
->ds
;
1253 struct DisplayChangeListener
*dcl
;
1254 int width
= surface_width(con
->surface
);
1255 int height
= surface_height(con
->surface
);
1261 w
= MIN(w
, width
- x
);
1262 h
= MIN(h
, height
- y
);
1264 if (con
!= active_console
) {
1267 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1268 if (dcl
->ops
->dpy_gfx_update
) {
1269 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1274 static void dpy_gfx_switch_surface(DisplayState
*ds
,
1275 DisplaySurface
*surface
)
1277 struct DisplayChangeListener
*dcl
;
1279 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1280 if (dcl
->ops
->dpy_gfx_switch
) {
1281 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1286 void dpy_gfx_replace_surface(QemuConsole
*con
,
1287 DisplaySurface
*surface
)
1289 DisplayState
*s
= con
->ds
;
1290 DisplaySurface
*old_surface
= con
->surface
;
1292 con
->surface
= surface
;
1293 if (con
== active_console
) {
1294 dpy_gfx_switch_surface(s
, surface
);
1296 qemu_free_displaysurface(old_surface
);
1299 void dpy_refresh(DisplayState
*s
)
1301 struct DisplayChangeListener
*dcl
;
1302 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1303 if (dcl
->ops
->dpy_refresh
) {
1304 dcl
->ops
->dpy_refresh(dcl
);
1309 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1310 int dst_x
, int dst_y
, int w
, int h
)
1312 DisplayState
*s
= con
->ds
;
1313 struct DisplayChangeListener
*dcl
;
1315 if (con
!= active_console
) {
1318 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1319 if (dcl
->ops
->dpy_gfx_copy
) {
1320 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1322 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1327 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1329 DisplayState
*s
= con
->ds
;
1330 struct DisplayChangeListener
*dcl
;
1332 if (con
!= active_console
) {
1335 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1336 if (dcl
->ops
->dpy_text_cursor
) {
1337 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1342 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1344 DisplayState
*s
= con
->ds
;
1345 struct DisplayChangeListener
*dcl
;
1347 if (con
!= active_console
) {
1350 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1351 if (dcl
->ops
->dpy_text_update
) {
1352 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1357 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1359 DisplayState
*s
= con
->ds
;
1360 struct DisplayChangeListener
*dcl
;
1362 if (con
!= active_console
) {
1365 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1366 if (dcl
->ops
->dpy_text_resize
) {
1367 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1372 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1374 DisplayState
*s
= con
->ds
;
1375 struct DisplayChangeListener
*dcl
;
1377 if (con
!= active_console
) {
1380 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1381 if (dcl
->ops
->dpy_mouse_set
) {
1382 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1387 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1389 DisplayState
*s
= con
->ds
;
1390 struct DisplayChangeListener
*dcl
;
1392 if (con
!= active_console
) {
1395 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1396 if (dcl
->ops
->dpy_cursor_define
) {
1397 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1402 bool dpy_cursor_define_supported(QemuConsole
*con
)
1404 DisplayState
*s
= con
->ds
;
1405 struct DisplayChangeListener
*dcl
;
1406 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1407 if (dcl
->ops
->dpy_cursor_define
) {
1414 /***********************************************************/
1415 /* register display */
1417 /* console.c internal use only */
1418 static DisplayState
*get_alloc_displaystate(void)
1420 if (!display_state
) {
1421 display_state
= g_new0(DisplayState
, 1);
1423 return display_state
;
1427 * Called by main(), after creating QemuConsoles
1428 * and before initializing ui (sdl/vnc/...).
1430 DisplayState
*init_displaystate(void)
1434 if (!display_state
) {
1435 display_state
= g_new0(DisplayState
, 1);
1438 for (i
= 0; i
< nb_consoles
; i
++) {
1439 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1440 consoles
[i
]->ds
== NULL
) {
1441 text_console_do_init(consoles
[i
]->chr
, display_state
);
1445 return display_state
;
1448 QemuConsole
*graphic_console_init(graphic_hw_update_ptr update
,
1449 graphic_hw_invalidate_ptr invalidate
,
1450 graphic_hw_text_update_ptr text_update
,
1458 ds
= get_alloc_displaystate();
1459 trace_console_gfx_new();
1460 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1461 s
->hw_update
= update
;
1462 s
->hw_invalidate
= invalidate
;
1463 s
->hw_text_update
= text_update
;
1466 s
->surface
= qemu_create_displaysurface(width
, height
);
1470 int is_graphic_console(void)
1472 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1475 int is_fixedsize_console(void)
1477 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1480 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1482 QemuConsole
*s
= chr
->opaque
;
1487 static void text_console_update_cursor(void *opaque
)
1489 QemuConsole
*s
= opaque
;
1491 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1492 graphic_hw_invalidate(s
);
1493 qemu_mod_timer(s
->cursor_timer
,
1494 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
1497 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1503 chr
->chr_write
= console_puts
;
1505 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1506 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1507 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1512 s
->total_height
= DEFAULT_BACKSCROLL
;
1515 if (s
->console_type
== TEXT_CONSOLE
) {
1516 if (active_console
&& active_console
->surface
) {
1517 s
->g_width
= surface_width(active_console
->surface
);
1518 s
->g_height
= surface_height(active_console
->surface
);
1520 s
->g_width
= 80 * FONT_WIDTH
;
1521 s
->g_height
= 24 * FONT_HEIGHT
;
1524 s
->surface
= qemu_create_displaysurface(s
->g_width
, s
->g_height
);
1527 qemu_new_timer_ms(rt_clock
, text_console_update_cursor
, s
);
1529 s
->hw_invalidate
= text_console_invalidate
;
1530 s
->hw_text_update
= text_console_update
;
1533 /* Set text attribute defaults */
1534 s
->t_attrib_default
.bold
= 0;
1535 s
->t_attrib_default
.uline
= 0;
1536 s
->t_attrib_default
.blink
= 0;
1537 s
->t_attrib_default
.invers
= 0;
1538 s
->t_attrib_default
.unvisible
= 0;
1539 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1540 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1541 /* set current text attributes to default */
1542 s
->t_attrib
= s
->t_attrib_default
;
1543 text_console_resize(s
);
1549 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1550 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1551 console_puts(chr
, (uint8_t*)msg
, len
);
1552 s
->t_attrib
= s
->t_attrib_default
;
1555 qemu_chr_be_generic_open(chr
);
1560 static CharDriverState
*text_console_init(ChardevVC
*vc
)
1562 CharDriverState
*chr
;
1565 unsigned height
= 0;
1567 chr
= g_malloc0(sizeof(CharDriverState
));
1569 if (vc
->has_width
) {
1571 } else if (vc
->has_cols
) {
1572 width
= vc
->cols
* FONT_WIDTH
;
1575 if (vc
->has_height
) {
1576 height
= vc
->height
;
1577 } else if (vc
->has_rows
) {
1578 height
= vc
->rows
* FONT_HEIGHT
;
1581 trace_console_txt_new(width
, height
);
1582 if (width
== 0 || height
== 0) {
1583 s
= new_console(NULL
, TEXT_CONSOLE
);
1585 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1595 s
->g_height
= height
;
1597 chr
->chr_set_echo
= text_console_set_echo
;
1599 if (display_state
) {
1600 text_console_do_init(chr
, display_state
);
1605 static VcHandler
*vc_handler
= text_console_init
;
1607 CharDriverState
*vc_init(ChardevVC
*vc
)
1609 return vc_handler(vc
);
1612 void register_vc_handler(VcHandler
*handler
)
1614 vc_handler
= handler
;
1617 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
1619 DisplaySurface
*surface
;
1621 assert(s
->console_type
== GRAPHIC_CONSOLE
);
1623 s
->g_height
= height
;
1624 surface
= qemu_create_displaysurface(width
, height
);
1625 dpy_gfx_replace_surface(s
, surface
);
1628 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
1629 int dst_x
, int dst_y
, int w
, int h
)
1631 assert(con
->console_type
== GRAPHIC_CONSOLE
);
1632 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1635 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
1637 return console
->surface
;
1640 DisplayState
*qemu_console_displaystate(QemuConsole
*console
)
1645 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1649 memset(&pf
, 0x00, sizeof(PixelFormat
));
1651 pf
.bits_per_pixel
= bpp
;
1652 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1653 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1657 pf
.rmask
= 0x000000FF;
1658 pf
.gmask
= 0x0000FF00;
1659 pf
.bmask
= 0x00FF0000;
1671 pf
.rmask
= 0x0000FF00;
1672 pf
.gmask
= 0x00FF0000;
1673 pf
.bmask
= 0xFF000000;
1674 pf
.amask
= 0x00000000;
1694 PixelFormat
qemu_default_pixelformat(int bpp
)
1698 memset(&pf
, 0x00, sizeof(PixelFormat
));
1700 pf
.bits_per_pixel
= bpp
;
1701 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1702 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1706 pf
.bits_per_pixel
= 16;
1707 pf
.rmask
= 0x00007c00;
1708 pf
.gmask
= 0x000003E0;
1709 pf
.bmask
= 0x0000001F;
1721 pf
.rmask
= 0x0000F800;
1722 pf
.gmask
= 0x000007E0;
1723 pf
.bmask
= 0x0000001F;
1735 pf
.rmask
= 0x00FF0000;
1736 pf
.gmask
= 0x0000FF00;
1737 pf
.bmask
= 0x000000FF;
1749 pf
.rmask
= 0x00FF0000;
1750 pf
.gmask
= 0x0000FF00;
1751 pf
.bmask
= 0x000000FF;
1768 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
1773 backend
->vc
= g_new0(ChardevVC
, 1);
1775 val
= qemu_opt_get_number(opts
, "width", 0);
1777 backend
->vc
->has_width
= true;
1778 backend
->vc
->width
= val
;
1781 val
= qemu_opt_get_number(opts
, "height", 0);
1783 backend
->vc
->has_height
= true;
1784 backend
->vc
->height
= val
;
1787 val
= qemu_opt_get_number(opts
, "cols", 0);
1789 backend
->vc
->has_cols
= true;
1790 backend
->vc
->cols
= val
;
1793 val
= qemu_opt_get_number(opts
, "rows", 0);
1795 backend
->vc
->has_rows
= true;
1796 backend
->vc
->rows
= val
;
1800 static void register_types(void)
1802 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC
,
1806 type_init(register_types
);