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
25 #include "qemu/osdep.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-ui.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qemu/timer.h"
33 #include "chardev/char-fe.h"
35 #include "exec/memory.h"
36 #include "io/channel-file.h"
37 #include "qom/object.h"
39 #define DEFAULT_BACKSCROLL 512
40 #define CONSOLE_CURSOR_PERIOD 500
42 typedef struct TextAttributes
{
52 typedef struct TextCell
{
54 TextAttributes t_attrib
;
57 #define MAX_ESC_PARAMS 3
65 typedef struct QEMUFIFO
{
68 int count
, wptr
, rptr
;
71 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
75 l
= f
->buf_size
- f
->count
;
80 l
= f
->buf_size
- f
->wptr
;
83 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
85 if (f
->wptr
>= f
->buf_size
)
94 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
102 l
= f
->buf_size
- f
->rptr
;
105 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
107 if (f
->rptr
>= f
->buf_size
)
119 TEXT_CONSOLE_FIXED_SIZE
126 console_type_t console_type
;
128 DisplaySurface
*surface
;
130 DisplayChangeListener
*gl
;
134 /* Graphic console state. */
139 const GraphicHwOps
*hw_ops
;
142 /* Text console state */
146 int backscroll_height
;
148 int x_saved
, y_saved
;
151 TextAttributes t_attrib_default
; /* default text attributes */
152 TextAttributes t_attrib
; /* currently active text attributes */
154 int text_x
[2], text_y
[2], cursor_invalidate
;
163 int esc_params
[MAX_ESC_PARAMS
];
167 /* fifo for key pressed */
169 uint8_t out_fifo_buf
[16];
170 QEMUTimer
*kbd_timer
;
172 QTAILQ_ENTRY(QemuConsole
) next
;
175 struct DisplayState
{
176 QEMUTimer
*gui_timer
;
177 uint64_t last_update
;
178 uint64_t update_interval
;
183 QLIST_HEAD(, DisplayChangeListener
) listeners
;
186 static DisplayState
*display_state
;
187 static QemuConsole
*active_console
;
188 static QTAILQ_HEAD(, QemuConsole
) consoles
=
189 QTAILQ_HEAD_INITIALIZER(consoles
);
190 static bool cursor_visible_phase
;
191 static QEMUTimer
*cursor_timer
;
193 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
194 static void dpy_refresh(DisplayState
*s
);
195 static DisplayState
*get_alloc_displaystate(void);
196 static void text_console_update_cursor_timer(void);
197 static void text_console_update_cursor(void *opaque
);
198 static bool ppm_save(int fd
, DisplaySurface
*ds
, Error
**errp
);
200 static void gui_update(void *opaque
)
202 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
203 uint64_t dcl_interval
;
204 DisplayState
*ds
= opaque
;
205 DisplayChangeListener
*dcl
;
208 ds
->refreshing
= true;
210 ds
->refreshing
= false;
212 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
213 dcl_interval
= dcl
->update_interval
?
214 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
215 if (interval
> dcl_interval
) {
216 interval
= dcl_interval
;
219 if (ds
->update_interval
!= interval
) {
220 ds
->update_interval
= interval
;
221 QTAILQ_FOREACH(con
, &consoles
, next
) {
222 if (con
->hw_ops
->update_interval
) {
223 con
->hw_ops
->update_interval(con
->hw
, interval
);
226 trace_console_refresh(interval
);
228 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
229 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
232 static void gui_setup_refresh(DisplayState
*ds
)
234 DisplayChangeListener
*dcl
;
235 bool need_timer
= false;
236 bool have_gfx
= false;
237 bool have_text
= false;
239 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
240 if (dcl
->ops
->dpy_refresh
!= NULL
) {
243 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
246 if (dcl
->ops
->dpy_text_update
!= NULL
) {
251 if (need_timer
&& ds
->gui_timer
== NULL
) {
252 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
253 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
255 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
256 timer_del(ds
->gui_timer
);
257 timer_free(ds
->gui_timer
);
258 ds
->gui_timer
= NULL
;
261 ds
->have_gfx
= have_gfx
;
262 ds
->have_text
= have_text
;
265 void graphic_hw_update_done(QemuConsole
*con
)
269 void graphic_hw_update(QemuConsole
*con
)
273 con
= active_console
;
275 if (con
&& con
->hw_ops
->gfx_update
) {
276 con
->hw_ops
->gfx_update(con
->hw
);
277 async
= con
->hw_ops
->gfx_update_async
;
280 graphic_hw_update_done(con
);
284 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
288 con
->gl_block
= block
;
289 if (con
->hw_ops
->gl_block
) {
290 con
->hw_ops
->gl_block(con
->hw
, block
);
294 int qemu_console_get_window_id(QemuConsole
*con
)
296 return con
->window_id
;
299 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
301 con
->window_id
= window_id
;
304 void graphic_hw_invalidate(QemuConsole
*con
)
307 con
= active_console
;
309 if (con
&& con
->hw_ops
->invalidate
) {
310 con
->hw_ops
->invalidate(con
->hw
);
314 static bool ppm_save(int fd
, DisplaySurface
*ds
, Error
**errp
)
316 int width
= pixman_image_get_width(ds
->image
);
317 int height
= pixman_image_get_height(ds
->image
);
318 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
319 g_autofree
char *header
= NULL
;
320 g_autoptr(pixman_image_t
) linebuf
= NULL
;
323 trace_ppm_save(fd
, ds
);
325 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
326 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
327 header
, strlen(header
), errp
) < 0) {
331 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
332 for (y
= 0; y
< height
; y
++) {
333 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
334 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
335 (char *)pixman_image_get_data(linebuf
),
336 pixman_image_get_stride(linebuf
), errp
) < 0) {
344 void qmp_screendump(const char *filename
, bool has_device
, const char *device
,
345 bool has_head
, int64_t head
, Error
**errp
)
348 DisplaySurface
*surface
;
352 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
359 error_setg(errp
, "'head' must be specified together with 'device'");
362 con
= qemu_console_lookup_by_index(0);
364 error_setg(errp
, "There is no console to take a screendump from");
369 graphic_hw_update(con
);
370 surface
= qemu_console_surface(con
);
372 error_setg(errp
, "no surface");
376 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
378 error_setg(errp
, "failed to open file '%s': %s", filename
,
383 if (!ppm_save(fd
, surface
, errp
)) {
384 qemu_unlink(filename
);
388 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
391 con
= active_console
;
393 if (con
&& con
->hw_ops
->text_update
) {
394 con
->hw_ops
->text_update(con
->hw
, chardata
);
398 static void vga_fill_rect(QemuConsole
*con
,
399 int posx
, int posy
, int width
, int height
,
400 pixman_color_t color
)
402 DisplaySurface
*surface
= qemu_console_surface(con
);
403 pixman_rectangle16_t rect
= {
404 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
407 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
411 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
412 static void vga_bitblt(QemuConsole
*con
,
413 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
415 DisplaySurface
*surface
= qemu_console_surface(con
);
417 pixman_image_composite(PIXMAN_OP_SRC
,
418 surface
->image
, NULL
, surface
->image
,
419 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
422 /***********************************************************/
423 /* basic char display */
425 #define FONT_HEIGHT 16
430 #define QEMU_RGB(r, g, b) \
431 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
433 static const pixman_color_t color_table_rgb
[2][8] = {
435 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
436 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
437 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
438 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
439 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
440 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
441 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
442 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
445 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
446 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
447 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
448 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
449 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
450 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
451 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
452 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
456 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
457 TextAttributes
*t_attrib
)
459 static pixman_image_t
*glyphs
[256];
460 DisplaySurface
*surface
= qemu_console_surface(s
);
461 pixman_color_t fgcol
, bgcol
;
463 if (t_attrib
->invers
) {
464 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
465 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
467 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
468 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
472 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
474 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
475 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
478 static void text_console_resize(QemuConsole
*s
)
480 TextCell
*cells
, *c
, *c1
;
481 int w1
, x
, y
, last_width
;
483 last_width
= s
->width
;
484 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
485 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
491 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
492 for(y
= 0; y
< s
->total_height
; y
++) {
493 c
= &cells
[y
* s
->width
];
495 c1
= &s
->cells
[y
* last_width
];
496 for(x
= 0; x
< w1
; x
++) {
500 for(x
= w1
; x
< s
->width
; x
++) {
502 c
->t_attrib
= s
->t_attrib_default
;
510 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
512 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
513 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
514 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
515 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
518 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
520 if (!qemu_console_is_visible(s
)) {
523 if (s
->update_x0
> x
* FONT_WIDTH
)
524 s
->update_x0
= x
* FONT_WIDTH
;
525 if (s
->update_y0
> y
* FONT_HEIGHT
)
526 s
->update_y0
= y
* FONT_HEIGHT
;
527 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
528 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
529 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
530 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
533 static void update_xy(QemuConsole
*s
, int x
, int y
)
538 if (s
->ds
->have_text
) {
539 text_update_xy(s
, x
, y
);
542 y1
= (s
->y_base
+ y
) % s
->total_height
;
543 y2
= y1
- s
->y_displayed
;
545 y2
+= s
->total_height
;
547 if (y2
< s
->height
) {
551 c
= &s
->cells
[y1
* s
->width
+ x
];
552 vga_putcharxy(s
, x
, y2
, c
->ch
,
554 invalidate_xy(s
, x
, y2
);
558 static void console_show_cursor(QemuConsole
*s
, int show
)
564 if (s
->ds
->have_text
) {
565 s
->cursor_invalidate
= 1;
571 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
572 y
= y1
- s
->y_displayed
;
574 y
+= s
->total_height
;
577 c
= &s
->cells
[y1
* s
->width
+ x
];
578 if (show
&& cursor_visible_phase
) {
579 TextAttributes t_attrib
= s
->t_attrib_default
;
580 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
581 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
583 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
585 invalidate_xy(s
, x
, y
);
589 static void console_refresh(QemuConsole
*s
)
591 DisplaySurface
*surface
= qemu_console_surface(s
);
595 if (s
->ds
->have_text
) {
598 s
->text_x
[1] = s
->width
- 1;
599 s
->text_y
[1] = s
->height
- 1;
600 s
->cursor_invalidate
= 1;
603 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
604 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
606 for (y
= 0; y
< s
->height
; y
++) {
607 c
= s
->cells
+ y1
* s
->width
;
608 for (x
= 0; x
< s
->width
; x
++) {
609 vga_putcharxy(s
, x
, y
, c
->ch
,
613 if (++y1
== s
->total_height
) {
617 console_show_cursor(s
, 1);
618 dpy_gfx_update(s
, 0, 0,
619 surface_width(surface
), surface_height(surface
));
622 static void console_scroll(QemuConsole
*s
, int ydelta
)
627 for(i
= 0; i
< ydelta
; i
++) {
628 if (s
->y_displayed
== s
->y_base
)
630 if (++s
->y_displayed
== s
->total_height
)
635 i
= s
->backscroll_height
;
636 if (i
> s
->total_height
- s
->height
)
637 i
= s
->total_height
- s
->height
;
640 y1
+= s
->total_height
;
641 for(i
= 0; i
< ydelta
; i
++) {
642 if (s
->y_displayed
== y1
)
644 if (--s
->y_displayed
< 0)
645 s
->y_displayed
= s
->total_height
- 1;
651 static void console_put_lf(QemuConsole
*s
)
657 if (s
->y
>= s
->height
) {
658 s
->y
= s
->height
- 1;
660 if (s
->y_displayed
== s
->y_base
) {
661 if (++s
->y_displayed
== s
->total_height
)
664 if (++s
->y_base
== s
->total_height
)
666 if (s
->backscroll_height
< s
->total_height
)
667 s
->backscroll_height
++;
668 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
669 c
= &s
->cells
[y1
* s
->width
];
670 for(x
= 0; x
< s
->width
; x
++) {
672 c
->t_attrib
= s
->t_attrib_default
;
675 if (s
->y_displayed
== s
->y_base
) {
676 if (s
->ds
->have_text
) {
679 s
->text_x
[1] = s
->width
- 1;
680 s
->text_y
[1] = s
->height
- 1;
683 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
684 s
->width
* FONT_WIDTH
,
685 (s
->height
- 1) * FONT_HEIGHT
);
686 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
687 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
688 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
691 s
->update_x1
= s
->width
* FONT_WIDTH
;
692 s
->update_y1
= s
->height
* FONT_HEIGHT
;
697 /* Set console attributes depending on the current escape codes.
698 * NOTE: I know this code is not very efficient (checking every color for it
699 * self) but it is more readable and better maintainable.
701 static void console_handle_escape(QemuConsole
*s
)
705 for (i
=0; i
<s
->nb_esc_params
; i
++) {
706 switch (s
->esc_params
[i
]) {
707 case 0: /* reset all console attributes to default */
708 s
->t_attrib
= s
->t_attrib_default
;
711 s
->t_attrib
.bold
= 1;
714 s
->t_attrib
.uline
= 1;
717 s
->t_attrib
.blink
= 1;
720 s
->t_attrib
.invers
= 1;
723 s
->t_attrib
.unvisible
= 1;
726 s
->t_attrib
.bold
= 0;
729 s
->t_attrib
.uline
= 0;
732 s
->t_attrib
.blink
= 0;
735 s
->t_attrib
.invers
= 0;
738 s
->t_attrib
.unvisible
= 0;
740 /* set foreground color */
742 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
745 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
748 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
751 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
754 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
757 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
760 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
763 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
765 /* set background color */
767 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
770 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
773 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
776 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
779 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
782 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
785 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
788 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
794 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
796 int y1
= (s
->y_base
+ y
) % s
->total_height
;
800 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
802 c
->t_attrib
= s
->t_attrib_default
;
806 static void console_put_one(QemuConsole
*s
, int ch
)
810 if (s
->x
>= s
->width
) {
815 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
816 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
818 c
->t_attrib
= s
->t_attrib
;
819 update_xy(s
, s
->x
, s
->y
);
823 static void console_respond_str(QemuConsole
*s
, const char *buf
)
826 console_put_one(s
, *buf
);
831 /* set cursor, checking bounds */
832 static void set_cursor(QemuConsole
*s
, int x
, int y
)
840 if (y
>= s
->height
) {
851 static void console_putchar(QemuConsole
*s
, int ch
)
860 case '\r': /* carriage return */
863 case '\n': /* newline */
866 case '\b': /* backspace */
870 case '\t': /* tabspace */
871 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
875 s
->x
= s
->x
+ (8 - (s
->x
% 8));
878 case '\a': /* alert aka. bell */
879 /* TODO: has to be implemented */
882 /* SI (shift in), character set 0 (ignored) */
885 /* SO (shift out), character set 1 (ignored) */
887 case 27: /* esc (introducing an escape sequence) */
888 s
->state
= TTY_STATE_ESC
;
891 console_put_one(s
, ch
);
895 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
897 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
898 s
->esc_params
[i
] = 0;
899 s
->nb_esc_params
= 0;
900 s
->state
= TTY_STATE_CSI
;
902 s
->state
= TTY_STATE_NORM
;
905 case TTY_STATE_CSI
: /* handle escape sequence parameters */
906 if (ch
>= '0' && ch
<= '9') {
907 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
908 int *param
= &s
->esc_params
[s
->nb_esc_params
];
909 int digit
= (ch
- '0');
911 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
912 *param
* 10 + digit
: INT_MAX
;
915 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
917 if (ch
== ';' || ch
== '?') {
920 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
921 ch
, s
->nb_esc_params
);
922 s
->state
= TTY_STATE_NORM
;
926 if (s
->esc_params
[0] == 0) {
927 s
->esc_params
[0] = 1;
929 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
932 /* move cursor down */
933 if (s
->esc_params
[0] == 0) {
934 s
->esc_params
[0] = 1;
936 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
939 /* move cursor right */
940 if (s
->esc_params
[0] == 0) {
941 s
->esc_params
[0] = 1;
943 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
946 /* move cursor left */
947 if (s
->esc_params
[0] == 0) {
948 s
->esc_params
[0] = 1;
950 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
953 /* move cursor to column */
954 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
958 /* move cursor to row, column */
959 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
962 switch (s
->esc_params
[0]) {
964 /* clear to end of screen */
965 for (y
= s
->y
; y
< s
->height
; y
++) {
966 for (x
= 0; x
< s
->width
; x
++) {
967 if (y
== s
->y
&& x
< s
->x
) {
970 console_clear_xy(s
, x
, y
);
975 /* clear from beginning of screen */
976 for (y
= 0; y
<= s
->y
; y
++) {
977 for (x
= 0; x
< s
->width
; x
++) {
978 if (y
== s
->y
&& x
> s
->x
) {
981 console_clear_xy(s
, x
, y
);
986 /* clear entire screen */
987 for (y
= 0; y
<= s
->height
; y
++) {
988 for (x
= 0; x
< s
->width
; x
++) {
989 console_clear_xy(s
, x
, y
);
996 switch (s
->esc_params
[0]) {
999 for(x
= s
->x
; x
< s
->width
; x
++) {
1000 console_clear_xy(s
, x
, s
->y
);
1004 /* clear from beginning of line */
1005 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1006 console_clear_xy(s
, x
, s
->y
);
1010 /* clear entire line */
1011 for(x
= 0; x
< s
->width
; x
++) {
1012 console_clear_xy(s
, x
, s
->y
);
1018 console_handle_escape(s
);
1021 switch (s
->esc_params
[0]) {
1023 /* report console status (always succeed)*/
1024 console_respond_str(s
, "\033[0n");
1027 /* report cursor position */
1028 sprintf(response
, "\033[%d;%dR",
1029 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1031 console_respond_str(s
, response
);
1036 /* save cursor position */
1041 /* restore cursor position */
1046 trace_console_putchar_unhandled(ch
);
1054 void console_select(unsigned int index
)
1056 DisplayChangeListener
*dcl
;
1059 trace_console_select(index
);
1060 s
= qemu_console_lookup_by_index(index
);
1062 DisplayState
*ds
= s
->ds
;
1066 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1067 if (dcl
->con
!= NULL
) {
1070 if (dcl
->ops
->dpy_gfx_switch
) {
1071 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1075 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1076 surface_height(s
->surface
));
1079 if (ds
->have_text
) {
1080 dpy_text_resize(s
, s
->width
, s
->height
);
1082 text_console_update_cursor(NULL
);
1088 QemuConsole
*console
;
1090 typedef struct VCChardev VCChardev
;
1092 #define TYPE_CHARDEV_VC "chardev-vc"
1093 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
1096 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1098 VCChardev
*drv
= VC_CHARDEV(chr
);
1099 QemuConsole
*s
= drv
->console
;
1106 s
->update_x0
= s
->width
* FONT_WIDTH
;
1107 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1110 console_show_cursor(s
, 0);
1111 for(i
= 0; i
< len
; i
++) {
1112 console_putchar(s
, buf
[i
]);
1114 console_show_cursor(s
, 1);
1115 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1116 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1117 s
->update_x1
- s
->update_x0
,
1118 s
->update_y1
- s
->update_y0
);
1123 static void kbd_send_chars(void *opaque
)
1125 QemuConsole
*s
= opaque
;
1129 len
= qemu_chr_be_can_write(s
->chr
);
1130 if (len
> s
->out_fifo
.count
)
1131 len
= s
->out_fifo
.count
;
1133 if (len
> sizeof(buf
))
1135 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1136 qemu_chr_be_write(s
->chr
, buf
, len
);
1138 /* characters are pending: we send them a bit later (XXX:
1139 horrible, should change char device API) */
1140 if (s
->out_fifo
.count
> 0) {
1141 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1145 /* called when an ascii key is pressed */
1146 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1148 uint8_t buf
[16], *q
;
1152 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1156 case QEMU_KEY_CTRL_UP
:
1157 console_scroll(s
, -1);
1159 case QEMU_KEY_CTRL_DOWN
:
1160 console_scroll(s
, 1);
1162 case QEMU_KEY_CTRL_PAGEUP
:
1163 console_scroll(s
, -10);
1165 case QEMU_KEY_CTRL_PAGEDOWN
:
1166 console_scroll(s
, 10);
1169 /* convert the QEMU keysym to VT100 key string */
1171 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1174 c
= keysym
- 0xe100;
1176 *q
++ = '0' + (c
/ 10);
1177 *q
++ = '0' + (c
% 10);
1179 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1182 *q
++ = keysym
& 0xff;
1183 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1184 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1190 vc_chr_write(s
->chr
, buf
, q
- buf
);
1193 if (be
&& be
->chr_read
) {
1194 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1201 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1202 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1203 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1204 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1205 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1206 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1207 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1208 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1209 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1210 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1211 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1214 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1215 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1216 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1217 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1218 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1219 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1220 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1221 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1222 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1225 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1229 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1233 kbd_put_keysym_console(s
, keysym
);
1237 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1241 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1242 kbd_put_keysym_console(s
, str
[i
]);
1246 void kbd_put_keysym(int keysym
)
1248 kbd_put_keysym_console(active_console
, keysym
);
1251 static void text_console_invalidate(void *opaque
)
1253 QemuConsole
*s
= (QemuConsole
*) opaque
;
1255 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1256 text_console_resize(s
);
1261 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1263 QemuConsole
*s
= (QemuConsole
*) opaque
;
1266 if (s
->text_x
[0] <= s
->text_x
[1]) {
1267 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1268 chardata
+= s
->text_y
[0] * s
->width
;
1269 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1270 for (j
= 0; j
< s
->width
; j
++, src
++) {
1271 console_write_ch(chardata
++,
1272 ATTR2CHTYPE(s
->cells
[src
].ch
,
1273 s
->cells
[src
].t_attrib
.fgcol
,
1274 s
->cells
[src
].t_attrib
.bgcol
,
1275 s
->cells
[src
].t_attrib
.bold
));
1277 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1278 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1279 s
->text_x
[0] = s
->width
;
1280 s
->text_y
[0] = s
->height
;
1284 if (s
->cursor_invalidate
) {
1285 dpy_text_cursor(s
, s
->x
, s
->y
);
1286 s
->cursor_invalidate
= 0;
1290 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1297 obj
= object_new(TYPE_QEMU_CONSOLE
);
1298 s
= QEMU_CONSOLE(obj
);
1300 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1301 (Object
**)&s
->device
,
1302 object_property_allow_set_link
,
1303 OBJ_PROP_LINK_STRONG
);
1304 object_property_add_uint32_ptr(obj
, "head", &s
->head
,
1305 OBJ_PROP_FLAG_READ
);
1307 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1308 (console_type
== GRAPHIC_CONSOLE
))) {
1312 s
->console_type
= console_type
;
1314 if (QTAILQ_EMPTY(&consoles
)) {
1316 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1317 } else if (console_type
!= GRAPHIC_CONSOLE
|| qdev_hotplug
) {
1318 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1319 s
->index
= last
->index
+ 1;
1320 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1323 * HACK: Put graphical consoles before text consoles.
1325 * Only do that for coldplugged devices. After initial device
1326 * initialization we will not renumber the consoles any more.
1328 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1330 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1331 c
->console_type
== GRAPHIC_CONSOLE
) {
1332 c
= QTAILQ_NEXT(c
, next
);
1334 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1335 /* have no text consoles */
1336 s
->index
= c
->index
+ 1;
1337 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1339 s
->index
= c
->index
;
1340 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1341 /* renumber text consoles */
1342 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1350 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1352 qemu_pixman_image_unref(surface
->image
);
1353 surface
->image
= NULL
;
1355 surface
->format
= PIXMAN_x8r8g8b8
;
1356 surface
->image
= pixman_image_create_bits(surface
->format
,
1359 assert(surface
->image
!= NULL
);
1361 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1364 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1366 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1368 trace_displaysurface_create(surface
, width
, height
);
1369 qemu_alloc_display(surface
, width
, height
);
1373 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1374 pixman_format_code_t format
,
1375 int linesize
, uint8_t *data
)
1377 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1379 trace_displaysurface_create_from(surface
, width
, height
, format
);
1380 surface
->format
= format
;
1381 surface
->image
= pixman_image_create_bits(surface
->format
,
1383 (void *)data
, linesize
);
1384 assert(surface
->image
!= NULL
);
1389 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1391 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1393 trace_displaysurface_create_pixman(surface
);
1394 surface
->format
= pixman_image_get_format(image
);
1395 surface
->image
= pixman_image_ref(image
);
1400 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1403 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1404 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1405 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1406 pixman_image_t
*glyph
;
1410 x
= (w
/ FONT_WIDTH
- len
) / 2;
1411 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1412 for (i
= 0; i
< len
; i
++) {
1413 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1414 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1415 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1416 qemu_pixman_image_unref(glyph
);
1421 void qemu_free_displaysurface(DisplaySurface
*surface
)
1423 if (surface
== NULL
) {
1426 trace_displaysurface_free(surface
);
1427 qemu_pixman_image_unref(surface
->image
);
1431 bool console_has_gl(QemuConsole
*con
)
1433 return con
->gl
!= NULL
;
1436 bool console_has_gl_dmabuf(QemuConsole
*con
)
1438 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1441 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1443 static const char nodev
[] =
1444 "This VM has no graphic display device.";
1445 static DisplaySurface
*dummy
;
1450 if (dcl
->ops
->dpy_gl_ctx_create
) {
1451 /* display has opengl support */
1454 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1455 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1461 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1462 dcl
->ds
= get_alloc_displaystate();
1463 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1464 gui_setup_refresh(dcl
->ds
);
1469 con
= active_console
;
1471 if (dcl
->ops
->dpy_gfx_switch
) {
1473 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1476 dummy
= qemu_create_message_surface(640, 480, nodev
);
1478 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1481 text_console_update_cursor(NULL
);
1484 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1487 DisplayState
*ds
= dcl
->ds
;
1489 dcl
->update_interval
= interval
;
1490 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1491 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1495 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1497 DisplayState
*ds
= dcl
->ds
;
1498 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1502 QLIST_REMOVE(dcl
, next
);
1504 gui_setup_refresh(ds
);
1507 static void dpy_set_ui_info_timer(void *opaque
)
1509 QemuConsole
*con
= opaque
;
1511 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1514 bool dpy_ui_info_supported(QemuConsole
*con
)
1516 return con
->hw_ops
->ui_info
!= NULL
;
1519 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
1521 assert(con
!= NULL
);
1523 return &con
->ui_info
;
1526 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1528 assert(con
!= NULL
);
1530 if (!dpy_ui_info_supported(con
)) {
1533 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1534 /* nothing changed -- ignore */
1539 * Typically we get a flood of these as the user resizes the window.
1540 * Wait until the dust has settled (one second without updates), then
1541 * go notify the guest.
1543 con
->ui_info
= *info
;
1544 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1548 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1550 DisplayState
*s
= con
->ds
;
1551 DisplayChangeListener
*dcl
;
1556 width
= surface_width(con
->surface
);
1557 height
= surface_height(con
->surface
);
1563 w
= MIN(w
, width
- x
);
1564 h
= MIN(h
, height
- y
);
1566 if (!qemu_console_is_visible(con
)) {
1569 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1570 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1573 if (dcl
->ops
->dpy_gfx_update
) {
1574 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1579 void dpy_gfx_update_full(QemuConsole
*con
)
1581 if (!con
->surface
) {
1584 dpy_gfx_update(con
, 0, 0,
1585 surface_width(con
->surface
),
1586 surface_height(con
->surface
));
1589 void dpy_gfx_replace_surface(QemuConsole
*con
,
1590 DisplaySurface
*surface
)
1592 DisplayState
*s
= con
->ds
;
1593 DisplaySurface
*old_surface
= con
->surface
;
1594 DisplayChangeListener
*dcl
;
1596 assert(old_surface
!= surface
|| surface
== NULL
);
1598 con
->surface
= surface
;
1599 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1600 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1603 if (dcl
->ops
->dpy_gfx_switch
) {
1604 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1607 qemu_free_displaysurface(old_surface
);
1610 bool dpy_gfx_check_format(QemuConsole
*con
,
1611 pixman_format_code_t format
)
1613 DisplayChangeListener
*dcl
;
1614 DisplayState
*s
= con
->ds
;
1616 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1617 if (dcl
->con
&& dcl
->con
!= con
) {
1618 /* dcl bound to another console -> skip */
1621 if (dcl
->ops
->dpy_gfx_check_format
) {
1622 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1626 /* default is to whitelist native 32 bpp only */
1627 if (format
!= qemu_default_pixman_format(32, true)) {
1635 static void dpy_refresh(DisplayState
*s
)
1637 DisplayChangeListener
*dcl
;
1639 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1640 if (dcl
->ops
->dpy_refresh
) {
1641 dcl
->ops
->dpy_refresh(dcl
);
1646 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1648 DisplayState
*s
= con
->ds
;
1649 DisplayChangeListener
*dcl
;
1651 if (!qemu_console_is_visible(con
)) {
1654 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1655 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1658 if (dcl
->ops
->dpy_text_cursor
) {
1659 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1664 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1666 DisplayState
*s
= con
->ds
;
1667 DisplayChangeListener
*dcl
;
1669 if (!qemu_console_is_visible(con
)) {
1672 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1673 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1676 if (dcl
->ops
->dpy_text_update
) {
1677 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1682 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1684 DisplayState
*s
= con
->ds
;
1685 DisplayChangeListener
*dcl
;
1687 if (!qemu_console_is_visible(con
)) {
1690 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1691 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1694 if (dcl
->ops
->dpy_text_resize
) {
1695 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1700 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1702 DisplayState
*s
= con
->ds
;
1703 DisplayChangeListener
*dcl
;
1705 if (!qemu_console_is_visible(con
)) {
1708 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1709 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1712 if (dcl
->ops
->dpy_mouse_set
) {
1713 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1718 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1720 DisplayState
*s
= con
->ds
;
1721 DisplayChangeListener
*dcl
;
1723 if (!qemu_console_is_visible(con
)) {
1726 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1727 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1730 if (dcl
->ops
->dpy_cursor_define
) {
1731 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1736 bool dpy_cursor_define_supported(QemuConsole
*con
)
1738 DisplayState
*s
= con
->ds
;
1739 DisplayChangeListener
*dcl
;
1741 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1742 if (dcl
->ops
->dpy_cursor_define
) {
1749 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1750 struct QEMUGLParams
*qparams
)
1753 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1756 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1759 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1762 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1765 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1768 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1771 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1774 void dpy_gl_scanout_disable(QemuConsole
*con
)
1777 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1778 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1780 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1785 void dpy_gl_scanout_texture(QemuConsole
*con
,
1786 uint32_t backing_id
,
1787 bool backing_y_0_top
,
1788 uint32_t backing_width
,
1789 uint32_t backing_height
,
1790 uint32_t x
, uint32_t y
,
1791 uint32_t width
, uint32_t height
)
1794 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1796 backing_width
, backing_height
,
1797 x
, y
, width
, height
);
1800 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1804 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1807 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1808 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1812 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1813 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1814 have_hot
, hot_x
, hot_y
);
1818 void dpy_gl_cursor_position(QemuConsole
*con
,
1819 uint32_t pos_x
, uint32_t pos_y
)
1823 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1824 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1828 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1833 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1834 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1838 void dpy_gl_update(QemuConsole
*con
,
1839 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1842 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1845 /***********************************************************/
1846 /* register display */
1848 /* console.c internal use only */
1849 static DisplayState
*get_alloc_displaystate(void)
1851 if (!display_state
) {
1852 display_state
= g_new0(DisplayState
, 1);
1853 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1854 text_console_update_cursor
, NULL
);
1856 return display_state
;
1860 * Called by main(), after creating QemuConsoles
1861 * and before initializing ui (sdl/vnc/...).
1863 DisplayState
*init_displaystate(void)
1868 get_alloc_displaystate();
1869 QTAILQ_FOREACH(con
, &consoles
, next
) {
1870 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1872 text_console_do_init(con
->chr
, display_state
);
1875 /* Hook up into the qom tree here (not in new_console()), once
1876 * all QemuConsoles are created and the order / numbering
1877 * doesn't change any more */
1878 name
= g_strdup_printf("console[%d]", con
->index
);
1879 object_property_add_child(container_get(object_get_root(), "/backend"),
1884 return display_state
;
1887 void graphic_console_set_hwops(QemuConsole
*con
,
1888 const GraphicHwOps
*hw_ops
,
1891 con
->hw_ops
= hw_ops
;
1895 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1896 const GraphicHwOps
*hw_ops
,
1899 static const char noinit
[] =
1900 "Guest has not initialized the display (yet).";
1905 DisplaySurface
*surface
;
1907 ds
= get_alloc_displaystate();
1908 s
= qemu_console_lookup_unused();
1910 trace_console_gfx_reuse(s
->index
);
1912 width
= surface_width(s
->surface
);
1913 height
= surface_height(s
->surface
);
1916 trace_console_gfx_new();
1917 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1918 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1919 dpy_set_ui_info_timer
, s
);
1921 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1923 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
1927 surface
= qemu_create_message_surface(width
, height
, noinit
);
1928 dpy_gfx_replace_surface(s
, surface
);
1932 static const GraphicHwOps unused_ops
= {
1936 void graphic_console_close(QemuConsole
*con
)
1938 static const char unplugged
[] =
1939 "Guest display has been unplugged";
1940 DisplaySurface
*surface
;
1945 width
= surface_width(con
->surface
);
1946 height
= surface_height(con
->surface
);
1949 trace_console_gfx_close(con
->index
);
1950 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
1951 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1954 dpy_gl_scanout_disable(con
);
1956 surface
= qemu_create_message_surface(width
, height
, unplugged
);
1957 dpy_gfx_replace_surface(con
, surface
);
1960 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1964 QTAILQ_FOREACH(con
, &consoles
, next
) {
1965 if (con
->index
== index
) {
1972 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1978 QTAILQ_FOREACH(con
, &consoles
, next
) {
1979 obj
= object_property_get_link(OBJECT(con
),
1980 "device", &error_abort
);
1981 if (DEVICE(obj
) != dev
) {
1984 h
= object_property_get_uint(OBJECT(con
),
1985 "head", &error_abort
);
1994 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1995 uint32_t head
, Error
**errp
)
2000 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2002 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2003 "Device '%s' not found", device_id
);
2007 con
= qemu_console_lookup_by_device(dev
, head
);
2009 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2017 QemuConsole
*qemu_console_lookup_unused(void)
2022 QTAILQ_FOREACH(con
, &consoles
, next
) {
2023 if (con
->hw_ops
!= &unused_ops
) {
2026 obj
= object_property_get_link(OBJECT(con
),
2027 "device", &error_abort
);
2036 bool qemu_console_is_visible(QemuConsole
*con
)
2038 return (con
== active_console
) || (con
->dcls
> 0);
2041 bool qemu_console_is_graphic(QemuConsole
*con
)
2044 con
= active_console
;
2046 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2049 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2052 con
= active_console
;
2054 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2057 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2059 assert(con
!= NULL
);
2060 return con
->gl_block
;
2063 char *qemu_console_get_label(QemuConsole
*con
)
2065 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2067 return g_strdup(object_get_typename(con
->device
));
2069 return g_strdup("VGA");
2071 if (con
->chr
&& con
->chr
->label
) {
2072 return g_strdup(con
->chr
->label
);
2074 return g_strdup_printf("vc%d", con
->index
);
2078 int qemu_console_get_index(QemuConsole
*con
)
2081 con
= active_console
;
2083 return con
? con
->index
: -1;
2086 uint32_t qemu_console_get_head(QemuConsole
*con
)
2089 con
= active_console
;
2091 return con
? con
->head
: -1;
2094 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
2096 assert(con
!= NULL
);
2097 return &con
->ui_info
;
2100 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2103 con
= active_console
;
2105 return con
? surface_width(con
->surface
) : fallback
;
2108 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2111 con
= active_console
;
2113 return con
? surface_height(con
->surface
) : fallback
;
2116 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2118 VCChardev
*drv
= VC_CHARDEV(chr
);
2119 QemuConsole
*s
= drv
->console
;
2124 static void text_console_update_cursor_timer(void)
2126 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2127 + CONSOLE_CURSOR_PERIOD
/ 2);
2130 static void text_console_update_cursor(void *opaque
)
2135 cursor_visible_phase
= !cursor_visible_phase
;
2137 QTAILQ_FOREACH(s
, &consoles
, next
) {
2138 if (qemu_console_is_graphic(s
) ||
2139 !qemu_console_is_visible(s
)) {
2143 graphic_hw_invalidate(s
);
2147 text_console_update_cursor_timer();
2151 static const GraphicHwOps text_console_ops
= {
2152 .invalidate
= text_console_invalidate
,
2153 .text_update
= text_console_update
,
2156 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2158 VCChardev
*drv
= VC_CHARDEV(chr
);
2159 QemuConsole
*s
= drv
->console
;
2160 int g_width
= 80 * FONT_WIDTH
;
2161 int g_height
= 24 * FONT_HEIGHT
;
2163 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2164 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2165 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2170 s
->total_height
= DEFAULT_BACKSCROLL
;
2174 if (active_console
&& active_console
->surface
) {
2175 g_width
= surface_width(active_console
->surface
);
2176 g_height
= surface_height(active_console
->surface
);
2178 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2181 s
->hw_ops
= &text_console_ops
;
2184 /* Set text attribute defaults */
2185 s
->t_attrib_default
.bold
= 0;
2186 s
->t_attrib_default
.uline
= 0;
2187 s
->t_attrib_default
.blink
= 0;
2188 s
->t_attrib_default
.invers
= 0;
2189 s
->t_attrib_default
.unvisible
= 0;
2190 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2191 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2192 /* set current text attributes to default */
2193 s
->t_attrib
= s
->t_attrib_default
;
2194 text_console_resize(s
);
2199 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2200 msg
= g_strdup_printf("%s console\r\n", chr
->label
);
2201 vc_chr_write(chr
, (uint8_t *)msg
, strlen(msg
));
2203 s
->t_attrib
= s
->t_attrib_default
;
2206 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2209 static void vc_chr_open(Chardev
*chr
,
2210 ChardevBackend
*backend
,
2214 ChardevVC
*vc
= backend
->u
.vc
.data
;
2215 VCChardev
*drv
= VC_CHARDEV(chr
);
2218 unsigned height
= 0;
2220 if (vc
->has_width
) {
2222 } else if (vc
->has_cols
) {
2223 width
= vc
->cols
* FONT_WIDTH
;
2226 if (vc
->has_height
) {
2227 height
= vc
->height
;
2228 } else if (vc
->has_rows
) {
2229 height
= vc
->rows
* FONT_HEIGHT
;
2232 trace_console_txt_new(width
, height
);
2233 if (width
== 0 || height
== 0) {
2234 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2236 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2237 s
->surface
= qemu_create_displaysurface(width
, height
);
2241 error_setg(errp
, "cannot create text console");
2248 if (display_state
) {
2249 text_console_do_init(chr
, display_state
);
2252 /* console/chardev init sometimes completes elsewhere in a 2nd
2253 * stage, so defer OPENED events until they are fully initialized
2258 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2260 DisplaySurface
*surface
;
2262 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2264 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2265 pixman_image_get_width(s
->surface
->image
) == width
&&
2266 pixman_image_get_height(s
->surface
->image
) == height
) {
2270 surface
= qemu_create_displaysurface(width
, height
);
2271 dpy_gfx_replace_surface(s
, surface
);
2274 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2276 return console
->surface
;
2279 PixelFormat
qemu_default_pixelformat(int bpp
)
2281 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2282 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2286 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2288 void qemu_display_register(QemuDisplay
*ui
)
2290 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2291 dpys
[ui
->type
] = ui
;
2294 bool qemu_display_find_default(DisplayOptions
*opts
)
2296 static DisplayType prio
[] = {
2303 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2304 if (dpys
[prio
[i
]] == NULL
) {
2305 ui_module_load_one(DisplayType_str(prio
[i
]));
2307 if (dpys
[prio
[i
]] == NULL
) {
2310 opts
->type
= prio
[i
];
2316 void qemu_display_early_init(DisplayOptions
*opts
)
2318 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2319 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2322 if (dpys
[opts
->type
] == NULL
) {
2323 ui_module_load_one(DisplayType_str(opts
->type
));
2325 if (dpys
[opts
->type
] == NULL
) {
2326 error_report("Display '%s' is not available.",
2327 DisplayType_str(opts
->type
));
2330 if (dpys
[opts
->type
]->early_init
) {
2331 dpys
[opts
->type
]->early_init(opts
);
2335 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2337 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2338 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2341 assert(dpys
[opts
->type
] != NULL
);
2342 dpys
[opts
->type
]->init(ds
, opts
);
2345 void qemu_display_help(void)
2349 printf("Available display backend types:\n");
2351 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2353 ui_module_load_one(DisplayType_str(idx
));
2356 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2361 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2366 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2367 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2368 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2370 val
= qemu_opt_get_number(opts
, "width", 0);
2372 vc
->has_width
= true;
2376 val
= qemu_opt_get_number(opts
, "height", 0);
2378 vc
->has_height
= true;
2382 val
= qemu_opt_get_number(opts
, "cols", 0);
2384 vc
->has_cols
= true;
2388 val
= qemu_opt_get_number(opts
, "rows", 0);
2390 vc
->has_rows
= true;
2395 static const TypeInfo qemu_console_info
= {
2396 .name
= TYPE_QEMU_CONSOLE
,
2397 .parent
= TYPE_OBJECT
,
2398 .instance_size
= sizeof(QemuConsole
),
2399 .class_size
= sizeof(QemuConsoleClass
),
2402 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2404 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2406 cc
->parse
= qemu_chr_parse_vc
;
2407 cc
->open
= vc_chr_open
;
2408 cc
->chr_write
= vc_chr_write
;
2409 cc
->chr_set_echo
= vc_chr_set_echo
;
2412 static const TypeInfo char_vc_type_info
= {
2413 .name
= TYPE_CHARDEV_VC
,
2414 .parent
= TYPE_CHARDEV
,
2415 .instance_size
= sizeof(VCChardev
),
2416 .class_init
= char_vc_class_init
,
2419 void qemu_console_early_init(void)
2421 /* set the default vc driver */
2422 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2423 type_register(&char_vc_type_info
);
2427 static void register_types(void)
2429 type_register_static(&qemu_console_info
);
2432 type_init(register_types
);