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_screen_dump_ptr hw_screen_dump
;
125 graphic_hw_text_update_ptr hw_text_update
;
127 int g_width
, g_height
;
129 /* Text console state */
133 int backscroll_height
;
135 int x_saved
, y_saved
;
138 TextAttributes t_attrib_default
; /* default text attributes */
139 TextAttributes t_attrib
; /* currently active text attributes */
141 int text_x
[2], text_y
[2], cursor_invalidate
;
143 bool cursor_visible_phase
;
144 QEMUTimer
*cursor_timer
;
152 int esc_params
[MAX_ESC_PARAMS
];
155 CharDriverState
*chr
;
156 /* fifo for key pressed */
158 uint8_t out_fifo_buf
[16];
159 QEMUTimer
*kbd_timer
;
162 static DisplayState
*display_state
;
163 static QemuConsole
*active_console
;
164 static QemuConsole
*consoles
[MAX_CONSOLES
];
165 static int nb_consoles
= 0;
167 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
168 static void dpy_gfx_switch_surface(DisplayState
*ds
,
169 DisplaySurface
*surface
);
171 void graphic_hw_update(QemuConsole
*con
)
174 con
= active_console
;
176 if (con
&& con
->hw_update
) {
177 con
->hw_update(con
->hw
);
181 void graphic_hw_invalidate(QemuConsole
*con
)
184 con
= active_console
;
186 if (con
&& con
->hw_invalidate
) {
187 con
->hw_invalidate(con
->hw
);
191 void qmp_screendump(const char *filename
, Error
**errp
)
193 QemuConsole
*previous_active_console
;
196 previous_active_console
= active_console
;
197 cswitch
= previous_active_console
&& previous_active_console
->index
!= 0;
199 /* There is currently no way of specifying which screen we want to dump,
200 so always dump the first one. */
204 if (consoles
[0] && consoles
[0]->hw_screen_dump
) {
205 consoles
[0]->hw_screen_dump(consoles
[0]->hw
, filename
, cswitch
, errp
);
207 error_setg(errp
, "device doesn't support screendump");
211 console_select(previous_active_console
->index
);
215 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
218 con
= active_console
;
220 if (con
&& con
->hw_text_update
)
221 con
->hw_text_update(con
->hw
, chardata
);
224 static void vga_fill_rect(QemuConsole
*con
,
225 int posx
, int posy
, int width
, int height
,
226 pixman_color_t color
)
228 DisplaySurface
*surface
= qemu_console_surface(con
);
229 pixman_rectangle16_t rect
= {
230 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
233 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
237 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
238 static void vga_bitblt(QemuConsole
*con
,
239 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
241 DisplaySurface
*surface
= qemu_console_surface(con
);
243 pixman_image_composite(PIXMAN_OP_SRC
,
244 surface
->image
, NULL
, surface
->image
,
245 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
248 /***********************************************************/
249 /* basic char display */
251 #define FONT_HEIGHT 16
256 #ifndef CONFIG_CURSES
269 #define QEMU_RGB(r, g, b) \
270 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
272 static const pixman_color_t color_table_rgb
[2][8] = {
274 QEMU_RGB(0x00, 0x00, 0x00), /* black */
275 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
276 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
277 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
278 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
279 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
280 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
281 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
284 QEMU_RGB(0x00, 0x00, 0x00), /* black */
285 QEMU_RGB(0xff, 0x00, 0x00), /* red */
286 QEMU_RGB(0x00, 0xff, 0x00), /* green */
287 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
288 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
289 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
290 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
291 QEMU_RGB(0xff, 0xff, 0xff), /* white */
296 static void console_print_text_attributes(TextAttributes
*t_attrib
, char ch
)
298 if (t_attrib
->bold
) {
303 if (t_attrib
->uline
) {
308 if (t_attrib
->blink
) {
313 if (t_attrib
->invers
) {
318 if (t_attrib
->unvisible
) {
324 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib
->fgcol
, t_attrib
->bgcol
, ch
, ch
);
328 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
329 TextAttributes
*t_attrib
)
331 static pixman_image_t
*glyphs
[256];
332 DisplaySurface
*surface
= qemu_console_surface(s
);
333 pixman_color_t fgcol
, bgcol
;
335 if (t_attrib
->invers
) {
336 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
337 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
339 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
340 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
344 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
346 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
347 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
350 static void text_console_resize(QemuConsole
*s
)
352 TextCell
*cells
, *c
, *c1
;
353 int w1
, x
, y
, last_width
;
355 last_width
= s
->width
;
356 s
->width
= s
->g_width
/ FONT_WIDTH
;
357 s
->height
= s
->g_height
/ FONT_HEIGHT
;
363 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
364 for(y
= 0; y
< s
->total_height
; y
++) {
365 c
= &cells
[y
* s
->width
];
367 c1
= &s
->cells
[y
* last_width
];
368 for(x
= 0; x
< w1
; x
++) {
372 for(x
= w1
; x
< s
->width
; x
++) {
374 c
->t_attrib
= s
->t_attrib_default
;
382 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
384 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
385 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
386 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
387 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
390 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
392 if (s
->update_x0
> x
* FONT_WIDTH
)
393 s
->update_x0
= x
* FONT_WIDTH
;
394 if (s
->update_y0
> y
* FONT_HEIGHT
)
395 s
->update_y0
= y
* FONT_HEIGHT
;
396 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
397 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
398 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
399 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
402 static void update_xy(QemuConsole
*s
, int x
, int y
)
407 if (s
!= active_console
) {
411 if (s
->ds
->have_text
) {
412 text_update_xy(s
, x
, y
);
415 if (s
->ds
->have_gfx
) {
416 y1
= (s
->y_base
+ y
) % s
->total_height
;
417 y2
= y1
- s
->y_displayed
;
419 y2
+= s
->total_height
;
420 if (y2
< s
->height
) {
421 c
= &s
->cells
[y1
* s
->width
+ x
];
422 vga_putcharxy(s
, x
, y2
, c
->ch
,
424 invalidate_xy(s
, x
, y2
);
429 static void console_show_cursor(QemuConsole
*s
, int show
)
435 if (s
!= active_console
) {
439 if (s
->ds
->have_text
) {
440 s
->cursor_invalidate
= 1;
443 if (s
->ds
->have_gfx
) {
447 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
448 y
= y1
- s
->y_displayed
;
450 y
+= s
->total_height
;
452 c
= &s
->cells
[y1
* s
->width
+ x
];
453 if (show
&& s
->cursor_visible_phase
) {
454 TextAttributes t_attrib
= s
->t_attrib_default
;
455 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
456 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
458 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
460 invalidate_xy(s
, x
, y
);
465 static void console_refresh(QemuConsole
*s
)
467 DisplaySurface
*surface
= qemu_console_surface(s
);
471 if (s
!= active_console
)
474 if (s
->ds
->have_text
) {
477 s
->text_x
[1] = s
->width
- 1;
478 s
->text_y
[1] = s
->height
- 1;
479 s
->cursor_invalidate
= 1;
482 if (s
->ds
->have_gfx
) {
483 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
484 color_table_rgb
[0][COLOR_BLACK
]);
486 for (y
= 0; y
< s
->height
; y
++) {
487 c
= s
->cells
+ y1
* s
->width
;
488 for (x
= 0; x
< s
->width
; x
++) {
489 vga_putcharxy(s
, x
, y
, c
->ch
,
493 if (++y1
== s
->total_height
) {
497 console_show_cursor(s
, 1);
498 dpy_gfx_update(s
, 0, 0,
499 surface_width(surface
), surface_height(surface
));
503 static void console_scroll(int ydelta
)
509 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
513 for(i
= 0; i
< ydelta
; i
++) {
514 if (s
->y_displayed
== s
->y_base
)
516 if (++s
->y_displayed
== s
->total_height
)
521 i
= s
->backscroll_height
;
522 if (i
> s
->total_height
- s
->height
)
523 i
= s
->total_height
- s
->height
;
526 y1
+= s
->total_height
;
527 for(i
= 0; i
< ydelta
; i
++) {
528 if (s
->y_displayed
== y1
)
530 if (--s
->y_displayed
< 0)
531 s
->y_displayed
= s
->total_height
- 1;
537 static void console_put_lf(QemuConsole
*s
)
543 if (s
->y
>= s
->height
) {
544 s
->y
= s
->height
- 1;
546 if (s
->y_displayed
== s
->y_base
) {
547 if (++s
->y_displayed
== s
->total_height
)
550 if (++s
->y_base
== s
->total_height
)
552 if (s
->backscroll_height
< s
->total_height
)
553 s
->backscroll_height
++;
554 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
555 c
= &s
->cells
[y1
* s
->width
];
556 for(x
= 0; x
< s
->width
; x
++) {
558 c
->t_attrib
= s
->t_attrib_default
;
561 if (s
== active_console
&& s
->y_displayed
== s
->y_base
) {
562 if (s
->ds
->have_text
) {
565 s
->text_x
[1] = s
->width
- 1;
566 s
->text_y
[1] = s
->height
- 1;
569 if (s
->ds
->have_gfx
) {
570 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
571 s
->width
* FONT_WIDTH
,
572 (s
->height
- 1) * FONT_HEIGHT
);
573 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
574 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
575 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
578 s
->update_x1
= s
->width
* FONT_WIDTH
;
579 s
->update_y1
= s
->height
* FONT_HEIGHT
;
585 /* Set console attributes depending on the current escape codes.
586 * NOTE: I know this code is not very efficient (checking every color for it
587 * self) but it is more readable and better maintainable.
589 static void console_handle_escape(QemuConsole
*s
)
593 for (i
=0; i
<s
->nb_esc_params
; i
++) {
594 switch (s
->esc_params
[i
]) {
595 case 0: /* reset all console attributes to default */
596 s
->t_attrib
= s
->t_attrib_default
;
599 s
->t_attrib
.bold
= 1;
602 s
->t_attrib
.uline
= 1;
605 s
->t_attrib
.blink
= 1;
608 s
->t_attrib
.invers
= 1;
611 s
->t_attrib
.unvisible
= 1;
614 s
->t_attrib
.bold
= 0;
617 s
->t_attrib
.uline
= 0;
620 s
->t_attrib
.blink
= 0;
623 s
->t_attrib
.invers
= 0;
626 s
->t_attrib
.unvisible
= 0;
628 /* set foreground color */
630 s
->t_attrib
.fgcol
=COLOR_BLACK
;
633 s
->t_attrib
.fgcol
=COLOR_RED
;
636 s
->t_attrib
.fgcol
=COLOR_GREEN
;
639 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
642 s
->t_attrib
.fgcol
=COLOR_BLUE
;
645 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
648 s
->t_attrib
.fgcol
=COLOR_CYAN
;
651 s
->t_attrib
.fgcol
=COLOR_WHITE
;
653 /* set background color */
655 s
->t_attrib
.bgcol
=COLOR_BLACK
;
658 s
->t_attrib
.bgcol
=COLOR_RED
;
661 s
->t_attrib
.bgcol
=COLOR_GREEN
;
664 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
667 s
->t_attrib
.bgcol
=COLOR_BLUE
;
670 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
673 s
->t_attrib
.bgcol
=COLOR_CYAN
;
676 s
->t_attrib
.bgcol
=COLOR_WHITE
;
682 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
684 int y1
= (s
->y_base
+ y
) % s
->total_height
;
685 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
687 c
->t_attrib
= s
->t_attrib_default
;
691 /* set cursor, checking bounds */
692 static void set_cursor(QemuConsole
*s
, int x
, int y
)
700 if (y
>= s
->height
) {
711 static void console_putchar(QemuConsole
*s
, int ch
)
720 case '\r': /* carriage return */
723 case '\n': /* newline */
726 case '\b': /* backspace */
730 case '\t': /* tabspace */
731 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
735 s
->x
= s
->x
+ (8 - (s
->x
% 8));
738 case '\a': /* alert aka. bell */
739 /* TODO: has to be implemented */
742 /* SI (shift in), character set 0 (ignored) */
745 /* SO (shift out), character set 1 (ignored) */
747 case 27: /* esc (introducing an escape sequence) */
748 s
->state
= TTY_STATE_ESC
;
751 if (s
->x
>= s
->width
) {
756 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
757 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
759 c
->t_attrib
= s
->t_attrib
;
760 update_xy(s
, s
->x
, s
->y
);
765 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
767 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
768 s
->esc_params
[i
] = 0;
769 s
->nb_esc_params
= 0;
770 s
->state
= TTY_STATE_CSI
;
772 s
->state
= TTY_STATE_NORM
;
775 case TTY_STATE_CSI
: /* handle escape sequence parameters */
776 if (ch
>= '0' && ch
<= '9') {
777 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
778 int *param
= &s
->esc_params
[s
->nb_esc_params
];
779 int digit
= (ch
- '0');
781 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
782 *param
* 10 + digit
: INT_MAX
;
785 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
790 fprintf(stderr
, "escape sequence CSI%d;%d%c, %d parameters\n",
791 s
->esc_params
[0], s
->esc_params
[1], ch
, s
->nb_esc_params
);
793 s
->state
= TTY_STATE_NORM
;
797 if (s
->esc_params
[0] == 0) {
798 s
->esc_params
[0] = 1;
800 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
803 /* move cursor down */
804 if (s
->esc_params
[0] == 0) {
805 s
->esc_params
[0] = 1;
807 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
810 /* move cursor right */
811 if (s
->esc_params
[0] == 0) {
812 s
->esc_params
[0] = 1;
814 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
817 /* move cursor left */
818 if (s
->esc_params
[0] == 0) {
819 s
->esc_params
[0] = 1;
821 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
824 /* move cursor to column */
825 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
829 /* move cursor to row, column */
830 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
833 switch (s
->esc_params
[0]) {
835 /* clear to end of screen */
836 for (y
= s
->y
; y
< s
->height
; y
++) {
837 for (x
= 0; x
< s
->width
; x
++) {
838 if (y
== s
->y
&& x
< s
->x
) {
841 console_clear_xy(s
, x
, y
);
846 /* clear from beginning of screen */
847 for (y
= 0; y
<= s
->y
; y
++) {
848 for (x
= 0; x
< s
->width
; x
++) {
849 if (y
== s
->y
&& x
> s
->x
) {
852 console_clear_xy(s
, x
, y
);
857 /* clear entire screen */
858 for (y
= 0; y
<= s
->height
; y
++) {
859 for (x
= 0; x
< s
->width
; x
++) {
860 console_clear_xy(s
, x
, y
);
867 switch (s
->esc_params
[0]) {
870 for(x
= s
->x
; x
< s
->width
; x
++) {
871 console_clear_xy(s
, x
, s
->y
);
875 /* clear from beginning of line */
876 for (x
= 0; x
<= s
->x
; x
++) {
877 console_clear_xy(s
, x
, s
->y
);
881 /* clear entire line */
882 for(x
= 0; x
< s
->width
; x
++) {
883 console_clear_xy(s
, x
, s
->y
);
889 console_handle_escape(s
);
892 /* report cursor position */
893 /* TODO: send ESC[row;colR */
896 /* save cursor position */
901 /* restore cursor position */
907 fprintf(stderr
, "unhandled escape character '%c'\n", ch
);
916 void console_select(unsigned int index
)
918 DisplaySurface
*surface
;
921 if (index
>= MAX_CONSOLES
)
924 trace_console_select(index
);
925 if (active_console
) {
926 surface
= qemu_console_surface(active_console
);
927 active_console
->g_width
= surface_width(surface
);
928 active_console
->g_height
= surface_height(surface
);
932 DisplayState
*ds
= s
->ds
;
934 if (active_console
&& active_console
->cursor_timer
) {
935 qemu_del_timer(active_console
->cursor_timer
);
939 dpy_gfx_switch_surface(ds
, s
->surface
);
940 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
941 surface_height(s
->surface
));
944 dpy_text_resize(s
, s
->width
, s
->height
);
946 if (s
->cursor_timer
) {
947 qemu_mod_timer(s
->cursor_timer
,
948 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
953 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
955 QemuConsole
*s
= chr
->opaque
;
958 s
->update_x0
= s
->width
* FONT_WIDTH
;
959 s
->update_y0
= s
->height
* FONT_HEIGHT
;
962 console_show_cursor(s
, 0);
963 for(i
= 0; i
< len
; i
++) {
964 console_putchar(s
, buf
[i
]);
966 console_show_cursor(s
, 1);
967 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
968 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
969 s
->update_x1
- s
->update_x0
,
970 s
->update_y1
- s
->update_y0
);
975 static void kbd_send_chars(void *opaque
)
977 QemuConsole
*s
= opaque
;
981 len
= qemu_chr_be_can_write(s
->chr
);
982 if (len
> s
->out_fifo
.count
)
983 len
= s
->out_fifo
.count
;
985 if (len
> sizeof(buf
))
987 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
988 qemu_chr_be_write(s
->chr
, buf
, len
);
990 /* characters are pending: we send them a bit later (XXX:
991 horrible, should change char device API) */
992 if (s
->out_fifo
.count
> 0) {
993 qemu_mod_timer(s
->kbd_timer
, qemu_get_clock_ms(rt_clock
) + 1);
997 /* called when an ascii key is pressed */
998 void kbd_put_keysym(int keysym
)
1001 uint8_t buf
[16], *q
;
1005 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1009 case QEMU_KEY_CTRL_UP
:
1012 case QEMU_KEY_CTRL_DOWN
:
1015 case QEMU_KEY_CTRL_PAGEUP
:
1016 console_scroll(-10);
1018 case QEMU_KEY_CTRL_PAGEDOWN
:
1022 /* convert the QEMU keysym to VT100 key string */
1024 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1027 c
= keysym
- 0xe100;
1029 *q
++ = '0' + (c
/ 10);
1030 *q
++ = '0' + (c
% 10);
1032 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1035 *q
++ = keysym
& 0xff;
1036 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1037 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1043 console_puts(s
->chr
, buf
, q
- buf
);
1045 if (s
->chr
->chr_read
) {
1046 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1053 static void text_console_invalidate(void *opaque
)
1055 QemuConsole
*s
= (QemuConsole
*) opaque
;
1056 DisplaySurface
*surface
= qemu_console_surface(s
);
1058 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1059 s
->g_width
= surface_width(surface
);
1060 s
->g_height
= surface_height(surface
);
1061 text_console_resize(s
);
1066 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1068 QemuConsole
*s
= (QemuConsole
*) opaque
;
1071 if (s
->text_x
[0] <= s
->text_x
[1]) {
1072 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1073 chardata
+= s
->text_y
[0] * s
->width
;
1074 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1075 for (j
= 0; j
< s
->width
; j
++, src
++)
1076 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1077 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1078 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1079 (s
->cells
[src
].t_attrib
.bold
<< 21));
1080 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1081 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1082 s
->text_x
[0] = s
->width
;
1083 s
->text_y
[0] = s
->height
;
1087 if (s
->cursor_invalidate
) {
1088 dpy_text_cursor(s
, s
->x
, s
->y
);
1089 s
->cursor_invalidate
= 0;
1093 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
)
1098 if (nb_consoles
>= MAX_CONSOLES
)
1100 s
= g_malloc0(sizeof(QemuConsole
));
1101 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1102 (console_type
== GRAPHIC_CONSOLE
))) {
1106 s
->console_type
= console_type
;
1107 if (console_type
!= GRAPHIC_CONSOLE
) {
1108 s
->index
= nb_consoles
;
1109 consoles
[nb_consoles
++] = s
;
1111 /* HACK: Put graphical consoles before text consoles. */
1112 for (i
= nb_consoles
; i
> 0; i
--) {
1113 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1115 consoles
[i
] = consoles
[i
- 1];
1116 consoles
[i
]->index
= i
;
1125 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
,
1126 int linesize
, PixelFormat pf
, int newflags
)
1130 qemu_pixman_image_unref(surface
->image
);
1131 surface
->image
= NULL
;
1133 surface
->format
= qemu_pixman_get_format(&pf
);
1134 assert(surface
->format
!= 0);
1135 surface
->image
= pixman_image_create_bits(surface
->format
,
1138 assert(surface
->image
!= NULL
);
1140 surface
->flags
= newflags
| QEMU_ALLOCATED_FLAG
;
1141 #ifdef HOST_WORDS_BIGENDIAN
1142 surface
->flags
|= QEMU_BIG_ENDIAN_FLAG
;
1146 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1148 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1149 int linesize
= width
* 4;
1151 trace_displaysurface_create(surface
, width
, height
);
1152 qemu_alloc_display(surface
, width
, height
, linesize
,
1153 qemu_default_pixelformat(32), 0);
1157 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
, int bpp
,
1158 int linesize
, uint8_t *data
,
1161 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1163 trace_displaysurface_create_from(surface
, width
, height
, bpp
, byteswap
);
1165 surface
->pf
= qemu_different_endianness_pixelformat(bpp
);
1167 surface
->pf
= qemu_default_pixelformat(bpp
);
1170 surface
->format
= qemu_pixman_get_format(&surface
->pf
);
1171 assert(surface
->format
!= 0);
1172 surface
->image
= pixman_image_create_bits(surface
->format
,
1174 (void *)data
, linesize
);
1175 assert(surface
->image
!= NULL
);
1177 #ifdef HOST_WORDS_BIGENDIAN
1178 surface
->flags
= QEMU_BIG_ENDIAN_FLAG
;
1184 void qemu_free_displaysurface(DisplaySurface
*surface
)
1186 if (surface
== NULL
) {
1189 trace_displaysurface_free(surface
);
1190 qemu_pixman_image_unref(surface
->image
);
1194 void register_displaychangelistener(DisplayState
*ds
,
1195 DisplayChangeListener
*dcl
)
1197 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1199 QLIST_INSERT_HEAD(&ds
->listeners
, dcl
, next
);
1200 gui_setup_refresh(ds
);
1201 if (dcl
->ops
->dpy_gfx_switch
&& active_console
) {
1202 dcl
->ops
->dpy_gfx_switch(dcl
, active_console
->surface
);
1206 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1208 DisplayState
*ds
= dcl
->ds
;
1209 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1210 QLIST_REMOVE(dcl
, next
);
1211 gui_setup_refresh(ds
);
1214 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1216 DisplayState
*s
= con
->ds
;
1217 struct DisplayChangeListener
*dcl
;
1218 int width
= surface_width(con
->surface
);
1219 int height
= surface_height(con
->surface
);
1225 w
= MIN(w
, width
- x
);
1226 h
= MIN(h
, height
- y
);
1228 if (con
!= active_console
) {
1231 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1232 if (dcl
->ops
->dpy_gfx_update
) {
1233 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1238 static void dpy_gfx_switch_surface(DisplayState
*ds
,
1239 DisplaySurface
*surface
)
1241 struct DisplayChangeListener
*dcl
;
1243 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1244 if (dcl
->ops
->dpy_gfx_switch
) {
1245 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1250 void dpy_gfx_replace_surface(QemuConsole
*con
,
1251 DisplaySurface
*surface
)
1253 DisplayState
*s
= con
->ds
;
1254 DisplaySurface
*old_surface
= con
->surface
;
1256 con
->surface
= surface
;
1257 if (con
== active_console
) {
1258 dpy_gfx_switch_surface(s
, surface
);
1260 qemu_free_displaysurface(old_surface
);
1263 void dpy_refresh(DisplayState
*s
)
1265 struct DisplayChangeListener
*dcl
;
1266 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1267 if (dcl
->ops
->dpy_refresh
) {
1268 dcl
->ops
->dpy_refresh(dcl
);
1273 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1274 int dst_x
, int dst_y
, int w
, int h
)
1276 DisplayState
*s
= con
->ds
;
1277 struct DisplayChangeListener
*dcl
;
1279 if (con
!= active_console
) {
1282 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1283 if (dcl
->ops
->dpy_gfx_copy
) {
1284 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1286 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1291 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1293 DisplayState
*s
= con
->ds
;
1294 struct DisplayChangeListener
*dcl
;
1296 if (con
!= active_console
) {
1299 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1300 if (dcl
->ops
->dpy_text_cursor
) {
1301 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1306 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1308 DisplayState
*s
= con
->ds
;
1309 struct DisplayChangeListener
*dcl
;
1311 if (con
!= active_console
) {
1314 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1315 if (dcl
->ops
->dpy_text_update
) {
1316 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1321 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1323 DisplayState
*s
= con
->ds
;
1324 struct DisplayChangeListener
*dcl
;
1326 if (con
!= active_console
) {
1329 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1330 if (dcl
->ops
->dpy_text_resize
) {
1331 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1336 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1338 DisplayState
*s
= con
->ds
;
1339 struct DisplayChangeListener
*dcl
;
1341 if (con
!= active_console
) {
1344 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1345 if (dcl
->ops
->dpy_mouse_set
) {
1346 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1351 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1353 DisplayState
*s
= con
->ds
;
1354 struct DisplayChangeListener
*dcl
;
1356 if (con
!= active_console
) {
1359 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1360 if (dcl
->ops
->dpy_cursor_define
) {
1361 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1366 bool dpy_cursor_define_supported(QemuConsole
*con
)
1368 DisplayState
*s
= con
->ds
;
1369 struct DisplayChangeListener
*dcl
;
1370 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1371 if (dcl
->ops
->dpy_cursor_define
) {
1378 /***********************************************************/
1379 /* register display */
1381 /* console.c internal use only */
1382 static DisplayState
*get_alloc_displaystate(void)
1384 if (!display_state
) {
1385 display_state
= g_new0(DisplayState
, 1);
1387 return display_state
;
1391 * Called by main(), after creating QemuConsoles
1392 * and before initializing ui (sdl/vnc/...).
1394 DisplayState
*init_displaystate(void)
1398 if (!display_state
) {
1399 display_state
= g_new0(DisplayState
, 1);
1402 for (i
= 0; i
< nb_consoles
; i
++) {
1403 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1404 consoles
[i
]->ds
== NULL
) {
1405 text_console_do_init(consoles
[i
]->chr
, display_state
);
1409 return display_state
;
1412 QemuConsole
*graphic_console_init(graphic_hw_update_ptr update
,
1413 graphic_hw_invalidate_ptr invalidate
,
1414 graphic_hw_screen_dump_ptr screen_dump
,
1415 graphic_hw_text_update_ptr text_update
,
1423 ds
= get_alloc_displaystate();
1424 trace_console_gfx_new();
1425 s
= new_console(ds
, GRAPHIC_CONSOLE
);
1426 s
->hw_update
= update
;
1427 s
->hw_invalidate
= invalidate
;
1428 s
->hw_screen_dump
= screen_dump
;
1429 s
->hw_text_update
= text_update
;
1432 s
->surface
= qemu_create_displaysurface(width
, height
);
1436 int is_graphic_console(void)
1438 return active_console
&& active_console
->console_type
== GRAPHIC_CONSOLE
;
1441 int is_fixedsize_console(void)
1443 return active_console
&& active_console
->console_type
!= TEXT_CONSOLE
;
1446 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1448 QemuConsole
*s
= chr
->opaque
;
1453 static void text_console_update_cursor(void *opaque
)
1455 QemuConsole
*s
= opaque
;
1457 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1458 graphic_hw_invalidate(s
);
1459 qemu_mod_timer(s
->cursor_timer
,
1460 qemu_get_clock_ms(rt_clock
) + CONSOLE_CURSOR_PERIOD
/ 2);
1463 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1469 chr
->chr_write
= console_puts
;
1471 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1472 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1473 s
->kbd_timer
= qemu_new_timer_ms(rt_clock
, kbd_send_chars
, s
);
1478 s
->total_height
= DEFAULT_BACKSCROLL
;
1481 if (s
->console_type
== TEXT_CONSOLE
) {
1482 if (active_console
&& active_console
->surface
) {
1483 s
->g_width
= surface_width(active_console
->surface
);
1484 s
->g_height
= surface_height(active_console
->surface
);
1486 s
->g_width
= 80 * FONT_WIDTH
;
1487 s
->g_height
= 24 * FONT_HEIGHT
;
1490 s
->surface
= qemu_create_displaysurface(s
->g_width
, s
->g_height
);
1493 qemu_new_timer_ms(rt_clock
, text_console_update_cursor
, s
);
1495 s
->hw_invalidate
= text_console_invalidate
;
1496 s
->hw_text_update
= text_console_update
;
1499 /* Set text attribute defaults */
1500 s
->t_attrib_default
.bold
= 0;
1501 s
->t_attrib_default
.uline
= 0;
1502 s
->t_attrib_default
.blink
= 0;
1503 s
->t_attrib_default
.invers
= 0;
1504 s
->t_attrib_default
.unvisible
= 0;
1505 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1506 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1507 /* set current text attributes to default */
1508 s
->t_attrib
= s
->t_attrib_default
;
1509 text_console_resize(s
);
1515 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1516 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1517 console_puts(chr
, (uint8_t*)msg
, len
);
1518 s
->t_attrib
= s
->t_attrib_default
;
1521 qemu_chr_be_generic_open(chr
);
1526 static CharDriverState
*text_console_init(ChardevVC
*vc
)
1528 CharDriverState
*chr
;
1531 unsigned height
= 0;
1533 chr
= g_malloc0(sizeof(CharDriverState
));
1535 if (vc
->has_width
) {
1537 } else if (vc
->has_cols
) {
1538 width
= vc
->cols
* FONT_WIDTH
;
1541 if (vc
->has_height
) {
1542 height
= vc
->height
;
1543 } else if (vc
->has_rows
) {
1544 height
= vc
->rows
* FONT_HEIGHT
;
1547 trace_console_txt_new(width
, height
);
1548 if (width
== 0 || height
== 0) {
1549 s
= new_console(NULL
, TEXT_CONSOLE
);
1551 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
);
1561 s
->g_height
= height
;
1563 chr
->chr_set_echo
= text_console_set_echo
;
1565 if (display_state
) {
1566 text_console_do_init(chr
, display_state
);
1571 static VcHandler
*vc_handler
= text_console_init
;
1573 CharDriverState
*vc_init(ChardevVC
*vc
)
1575 return vc_handler(vc
);
1578 void register_vc_handler(VcHandler
*handler
)
1580 vc_handler
= handler
;
1583 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
1585 DisplaySurface
*surface
;
1587 assert(s
->console_type
== GRAPHIC_CONSOLE
);
1589 s
->g_height
= height
;
1590 surface
= qemu_create_displaysurface(width
, height
);
1591 dpy_gfx_replace_surface(s
, surface
);
1594 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
1595 int dst_x
, int dst_y
, int w
, int h
)
1597 assert(con
->console_type
== GRAPHIC_CONSOLE
);
1598 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1601 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
1603 return console
->surface
;
1606 DisplayState
*qemu_console_displaystate(QemuConsole
*console
)
1611 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1615 memset(&pf
, 0x00, sizeof(PixelFormat
));
1617 pf
.bits_per_pixel
= bpp
;
1618 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1619 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1623 pf
.rmask
= 0x000000FF;
1624 pf
.gmask
= 0x0000FF00;
1625 pf
.bmask
= 0x00FF0000;
1637 pf
.rmask
= 0x0000FF00;
1638 pf
.gmask
= 0x00FF0000;
1639 pf
.bmask
= 0xFF000000;
1640 pf
.amask
= 0x00000000;
1660 PixelFormat
qemu_default_pixelformat(int bpp
)
1664 memset(&pf
, 0x00, sizeof(PixelFormat
));
1666 pf
.bits_per_pixel
= bpp
;
1667 pf
.bytes_per_pixel
= DIV_ROUND_UP(bpp
, 8);
1668 pf
.depth
= bpp
== 32 ? 24 : bpp
;
1672 pf
.bits_per_pixel
= 16;
1673 pf
.rmask
= 0x00007c00;
1674 pf
.gmask
= 0x000003E0;
1675 pf
.bmask
= 0x0000001F;
1687 pf
.rmask
= 0x0000F800;
1688 pf
.gmask
= 0x000007E0;
1689 pf
.bmask
= 0x0000001F;
1701 pf
.rmask
= 0x00FF0000;
1702 pf
.gmask
= 0x0000FF00;
1703 pf
.bmask
= 0x000000FF;
1715 pf
.rmask
= 0x00FF0000;
1716 pf
.gmask
= 0x0000FF00;
1717 pf
.bmask
= 0x000000FF;
1734 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
1739 backend
->vc
= g_new0(ChardevVC
, 1);
1741 val
= qemu_opt_get_number(opts
, "width", 0);
1743 backend
->vc
->has_width
= true;
1744 backend
->vc
->width
= val
;
1747 val
= qemu_opt_get_number(opts
, "height", 0);
1749 backend
->vc
->has_height
= true;
1750 backend
->vc
->height
= val
;
1753 val
= qemu_opt_get_number(opts
, "cols", 0);
1755 backend
->vc
->has_cols
= true;
1756 backend
->vc
->cols
= val
;
1759 val
= qemu_opt_get_number(opts
, "rows", 0);
1761 backend
->vc
->has_rows
= true;
1762 backend
->vc
->rows
= val
;
1766 static void register_types(void)
1768 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC
,
1772 type_init(register_types
);