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
;
173 QTAILQ_ENTRY(QemuConsole
) next
;
176 struct DisplayState
{
177 QEMUTimer
*gui_timer
;
178 uint64_t last_update
;
179 uint64_t update_interval
;
184 QLIST_HEAD(, DisplayChangeListener
) listeners
;
187 static DisplayState
*display_state
;
188 static QemuConsole
*active_console
;
189 static QTAILQ_HEAD(, QemuConsole
) consoles
=
190 QTAILQ_HEAD_INITIALIZER(consoles
);
191 static bool cursor_visible_phase
;
192 static QEMUTimer
*cursor_timer
;
194 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
195 static void dpy_refresh(DisplayState
*s
);
196 static DisplayState
*get_alloc_displaystate(void);
197 static void text_console_update_cursor_timer(void);
198 static void text_console_update_cursor(void *opaque
);
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_free(ds
->gui_timer
);
257 ds
->gui_timer
= NULL
;
260 ds
->have_gfx
= have_gfx
;
261 ds
->have_text
= have_text
;
264 void graphic_hw_update_done(QemuConsole
*con
)
267 qemu_co_queue_restart_all(&con
->dump_queue
);
271 void graphic_hw_update(QemuConsole
*con
)
274 con
= con
? con
: active_console
;
278 if (con
->hw_ops
->gfx_update
) {
279 con
->hw_ops
->gfx_update(con
->hw
);
280 async
= con
->hw_ops
->gfx_update_async
;
283 graphic_hw_update_done(con
);
287 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
291 con
->gl_block
= block
;
292 if (con
->hw_ops
->gl_block
) {
293 con
->hw_ops
->gl_block(con
->hw
, block
);
297 int qemu_console_get_window_id(QemuConsole
*con
)
299 return con
->window_id
;
302 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
304 con
->window_id
= window_id
;
307 void graphic_hw_invalidate(QemuConsole
*con
)
310 con
= active_console
;
312 if (con
&& con
->hw_ops
->invalidate
) {
313 con
->hw_ops
->invalidate(con
->hw
);
317 static bool ppm_save(int fd
, pixman_image_t
*image
, Error
**errp
)
319 int width
= pixman_image_get_width(image
);
320 int height
= pixman_image_get_height(image
);
321 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
322 g_autofree
char *header
= NULL
;
323 g_autoptr(pixman_image_t
) linebuf
= NULL
;
326 trace_ppm_save(fd
, image
);
328 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
329 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
330 header
, strlen(header
), errp
) < 0) {
334 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
335 for (y
= 0; y
< height
; y
++) {
336 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
337 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
338 (char *)pixman_image_get_data(linebuf
),
339 pixman_image_get_stride(linebuf
), errp
) < 0) {
347 static void graphic_hw_update_bh(void *con
)
349 graphic_hw_update(con
);
352 /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
354 qmp_screendump(const char *filename
, bool has_device
, const char *device
,
355 bool has_head
, int64_t head
, Error
**errp
)
357 g_autoptr(pixman_image_t
) image
= NULL
;
359 DisplaySurface
*surface
;
363 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
370 error_setg(errp
, "'head' must be specified together with 'device'");
373 con
= qemu_console_lookup_by_index(0);
375 error_setg(errp
, "There is no console to take a screendump from");
380 if (qemu_co_queue_empty(&con
->dump_queue
)) {
381 /* Defer the update, it will restart the pending coroutines */
382 aio_bh_schedule_oneshot(qemu_get_aio_context(),
383 graphic_hw_update_bh
, con
);
385 qemu_co_queue_wait(&con
->dump_queue
, NULL
);
388 * All pending coroutines are woken up, while the BQL is held. No
389 * further graphic update are possible until it is released. Take
390 * an image ref before that.
392 surface
= qemu_console_surface(con
);
394 error_setg(errp
, "no surface");
397 image
= pixman_image_ref(surface
->image
);
399 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
401 error_setg(errp
, "failed to open file '%s': %s", filename
,
407 * The image content could potentially be updated as the coroutine
408 * yields and releases the BQL. It could produce corrupted dump, but
409 * it should be otherwise safe.
411 if (!ppm_save(fd
, image
, errp
)) {
412 qemu_unlink(filename
);
416 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
419 con
= active_console
;
421 if (con
&& con
->hw_ops
->text_update
) {
422 con
->hw_ops
->text_update(con
->hw
, chardata
);
426 static void vga_fill_rect(QemuConsole
*con
,
427 int posx
, int posy
, int width
, int height
,
428 pixman_color_t color
)
430 DisplaySurface
*surface
= qemu_console_surface(con
);
431 pixman_rectangle16_t rect
= {
432 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
435 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
439 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
440 static void vga_bitblt(QemuConsole
*con
,
441 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
443 DisplaySurface
*surface
= qemu_console_surface(con
);
445 pixman_image_composite(PIXMAN_OP_SRC
,
446 surface
->image
, NULL
, surface
->image
,
447 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
450 /***********************************************************/
451 /* basic char display */
453 #define FONT_HEIGHT 16
458 #define QEMU_RGB(r, g, b) \
459 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
461 static const pixman_color_t color_table_rgb
[2][8] = {
463 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
464 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
465 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
466 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
467 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
468 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
469 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
470 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
473 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
474 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
475 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
476 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
477 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
478 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
479 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
480 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
484 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
485 TextAttributes
*t_attrib
)
487 static pixman_image_t
*glyphs
[256];
488 DisplaySurface
*surface
= qemu_console_surface(s
);
489 pixman_color_t fgcol
, bgcol
;
491 if (t_attrib
->invers
) {
492 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
493 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
495 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
496 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
500 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
502 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
503 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
506 static void text_console_resize(QemuConsole
*s
)
508 TextCell
*cells
, *c
, *c1
;
509 int w1
, x
, y
, last_width
;
511 last_width
= s
->width
;
512 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
513 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
519 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
520 for(y
= 0; y
< s
->total_height
; y
++) {
521 c
= &cells
[y
* s
->width
];
523 c1
= &s
->cells
[y
* last_width
];
524 for(x
= 0; x
< w1
; x
++) {
528 for(x
= w1
; x
< s
->width
; x
++) {
530 c
->t_attrib
= s
->t_attrib_default
;
538 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
540 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
541 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
542 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
543 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
546 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
548 if (!qemu_console_is_visible(s
)) {
551 if (s
->update_x0
> x
* FONT_WIDTH
)
552 s
->update_x0
= x
* FONT_WIDTH
;
553 if (s
->update_y0
> y
* FONT_HEIGHT
)
554 s
->update_y0
= y
* FONT_HEIGHT
;
555 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
556 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
557 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
558 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
561 static void update_xy(QemuConsole
*s
, int x
, int y
)
566 if (s
->ds
->have_text
) {
567 text_update_xy(s
, x
, y
);
570 y1
= (s
->y_base
+ y
) % s
->total_height
;
571 y2
= y1
- s
->y_displayed
;
573 y2
+= s
->total_height
;
575 if (y2
< s
->height
) {
579 c
= &s
->cells
[y1
* s
->width
+ x
];
580 vga_putcharxy(s
, x
, y2
, c
->ch
,
582 invalidate_xy(s
, x
, y2
);
586 static void console_show_cursor(QemuConsole
*s
, int show
)
592 if (s
->ds
->have_text
) {
593 s
->cursor_invalidate
= 1;
599 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
600 y
= y1
- s
->y_displayed
;
602 y
+= s
->total_height
;
605 c
= &s
->cells
[y1
* s
->width
+ x
];
606 if (show
&& cursor_visible_phase
) {
607 TextAttributes t_attrib
= s
->t_attrib_default
;
608 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
609 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
611 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
613 invalidate_xy(s
, x
, y
);
617 static void console_refresh(QemuConsole
*s
)
619 DisplaySurface
*surface
= qemu_console_surface(s
);
623 if (s
->ds
->have_text
) {
626 s
->text_x
[1] = s
->width
- 1;
627 s
->text_y
[1] = s
->height
- 1;
628 s
->cursor_invalidate
= 1;
631 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
632 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
634 for (y
= 0; y
< s
->height
; y
++) {
635 c
= s
->cells
+ y1
* s
->width
;
636 for (x
= 0; x
< s
->width
; x
++) {
637 vga_putcharxy(s
, x
, y
, c
->ch
,
641 if (++y1
== s
->total_height
) {
645 console_show_cursor(s
, 1);
646 dpy_gfx_update(s
, 0, 0,
647 surface_width(surface
), surface_height(surface
));
650 static void console_scroll(QemuConsole
*s
, int ydelta
)
655 for(i
= 0; i
< ydelta
; i
++) {
656 if (s
->y_displayed
== s
->y_base
)
658 if (++s
->y_displayed
== s
->total_height
)
663 i
= s
->backscroll_height
;
664 if (i
> s
->total_height
- s
->height
)
665 i
= s
->total_height
- s
->height
;
668 y1
+= s
->total_height
;
669 for(i
= 0; i
< ydelta
; i
++) {
670 if (s
->y_displayed
== y1
)
672 if (--s
->y_displayed
< 0)
673 s
->y_displayed
= s
->total_height
- 1;
679 static void console_put_lf(QemuConsole
*s
)
685 if (s
->y
>= s
->height
) {
686 s
->y
= s
->height
- 1;
688 if (s
->y_displayed
== s
->y_base
) {
689 if (++s
->y_displayed
== s
->total_height
)
692 if (++s
->y_base
== s
->total_height
)
694 if (s
->backscroll_height
< s
->total_height
)
695 s
->backscroll_height
++;
696 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
697 c
= &s
->cells
[y1
* s
->width
];
698 for(x
= 0; x
< s
->width
; x
++) {
700 c
->t_attrib
= s
->t_attrib_default
;
703 if (s
->y_displayed
== s
->y_base
) {
704 if (s
->ds
->have_text
) {
707 s
->text_x
[1] = s
->width
- 1;
708 s
->text_y
[1] = s
->height
- 1;
711 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
712 s
->width
* FONT_WIDTH
,
713 (s
->height
- 1) * FONT_HEIGHT
);
714 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
715 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
716 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
719 s
->update_x1
= s
->width
* FONT_WIDTH
;
720 s
->update_y1
= s
->height
* FONT_HEIGHT
;
725 /* Set console attributes depending on the current escape codes.
726 * NOTE: I know this code is not very efficient (checking every color for it
727 * self) but it is more readable and better maintainable.
729 static void console_handle_escape(QemuConsole
*s
)
733 for (i
=0; i
<s
->nb_esc_params
; i
++) {
734 switch (s
->esc_params
[i
]) {
735 case 0: /* reset all console attributes to default */
736 s
->t_attrib
= s
->t_attrib_default
;
739 s
->t_attrib
.bold
= 1;
742 s
->t_attrib
.uline
= 1;
745 s
->t_attrib
.blink
= 1;
748 s
->t_attrib
.invers
= 1;
751 s
->t_attrib
.unvisible
= 1;
754 s
->t_attrib
.bold
= 0;
757 s
->t_attrib
.uline
= 0;
760 s
->t_attrib
.blink
= 0;
763 s
->t_attrib
.invers
= 0;
766 s
->t_attrib
.unvisible
= 0;
768 /* set foreground color */
770 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
773 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
776 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
779 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
782 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
785 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
788 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
791 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
793 /* set background color */
795 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
798 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
801 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
804 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
807 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
810 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
813 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
816 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
822 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
824 int y1
= (s
->y_base
+ y
) % s
->total_height
;
828 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
830 c
->t_attrib
= s
->t_attrib_default
;
834 static void console_put_one(QemuConsole
*s
, int ch
)
838 if (s
->x
>= s
->width
) {
843 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
844 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
846 c
->t_attrib
= s
->t_attrib
;
847 update_xy(s
, s
->x
, s
->y
);
851 static void console_respond_str(QemuConsole
*s
, const char *buf
)
854 console_put_one(s
, *buf
);
859 /* set cursor, checking bounds */
860 static void set_cursor(QemuConsole
*s
, int x
, int y
)
868 if (y
>= s
->height
) {
879 static void console_putchar(QemuConsole
*s
, int ch
)
888 case '\r': /* carriage return */
891 case '\n': /* newline */
894 case '\b': /* backspace */
898 case '\t': /* tabspace */
899 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
903 s
->x
= s
->x
+ (8 - (s
->x
% 8));
906 case '\a': /* alert aka. bell */
907 /* TODO: has to be implemented */
910 /* SI (shift in), character set 0 (ignored) */
913 /* SO (shift out), character set 1 (ignored) */
915 case 27: /* esc (introducing an escape sequence) */
916 s
->state
= TTY_STATE_ESC
;
919 console_put_one(s
, ch
);
923 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
925 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
926 s
->esc_params
[i
] = 0;
927 s
->nb_esc_params
= 0;
928 s
->state
= TTY_STATE_CSI
;
930 s
->state
= TTY_STATE_NORM
;
933 case TTY_STATE_CSI
: /* handle escape sequence parameters */
934 if (ch
>= '0' && ch
<= '9') {
935 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
936 int *param
= &s
->esc_params
[s
->nb_esc_params
];
937 int digit
= (ch
- '0');
939 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
940 *param
* 10 + digit
: INT_MAX
;
943 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
945 if (ch
== ';' || ch
== '?') {
948 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
949 ch
, s
->nb_esc_params
);
950 s
->state
= TTY_STATE_NORM
;
954 if (s
->esc_params
[0] == 0) {
955 s
->esc_params
[0] = 1;
957 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
960 /* move cursor down */
961 if (s
->esc_params
[0] == 0) {
962 s
->esc_params
[0] = 1;
964 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
967 /* move cursor right */
968 if (s
->esc_params
[0] == 0) {
969 s
->esc_params
[0] = 1;
971 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
974 /* move cursor left */
975 if (s
->esc_params
[0] == 0) {
976 s
->esc_params
[0] = 1;
978 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
981 /* move cursor to column */
982 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
986 /* move cursor to row, column */
987 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
990 switch (s
->esc_params
[0]) {
992 /* clear to end of screen */
993 for (y
= s
->y
; y
< s
->height
; y
++) {
994 for (x
= 0; x
< s
->width
; x
++) {
995 if (y
== s
->y
&& x
< s
->x
) {
998 console_clear_xy(s
, x
, y
);
1003 /* clear from beginning of screen */
1004 for (y
= 0; y
<= s
->y
; y
++) {
1005 for (x
= 0; x
< s
->width
; x
++) {
1006 if (y
== s
->y
&& x
> s
->x
) {
1009 console_clear_xy(s
, x
, y
);
1014 /* clear entire screen */
1015 for (y
= 0; y
<= s
->height
; y
++) {
1016 for (x
= 0; x
< s
->width
; x
++) {
1017 console_clear_xy(s
, x
, y
);
1024 switch (s
->esc_params
[0]) {
1027 for(x
= s
->x
; x
< s
->width
; x
++) {
1028 console_clear_xy(s
, x
, s
->y
);
1032 /* clear from beginning of line */
1033 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1034 console_clear_xy(s
, x
, s
->y
);
1038 /* clear entire line */
1039 for(x
= 0; x
< s
->width
; x
++) {
1040 console_clear_xy(s
, x
, s
->y
);
1046 console_handle_escape(s
);
1049 switch (s
->esc_params
[0]) {
1051 /* report console status (always succeed)*/
1052 console_respond_str(s
, "\033[0n");
1055 /* report cursor position */
1056 sprintf(response
, "\033[%d;%dR",
1057 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1059 console_respond_str(s
, response
);
1064 /* save cursor position */
1069 /* restore cursor position */
1074 trace_console_putchar_unhandled(ch
);
1082 void console_select(unsigned int index
)
1084 DisplayChangeListener
*dcl
;
1087 trace_console_select(index
);
1088 s
= qemu_console_lookup_by_index(index
);
1090 DisplayState
*ds
= s
->ds
;
1094 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1095 if (dcl
->con
!= NULL
) {
1098 if (dcl
->ops
->dpy_gfx_switch
) {
1099 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1103 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1104 surface_height(s
->surface
));
1107 if (ds
->have_text
) {
1108 dpy_text_resize(s
, s
->width
, s
->height
);
1110 text_console_update_cursor(NULL
);
1116 QemuConsole
*console
;
1118 typedef struct VCChardev VCChardev
;
1120 #define TYPE_CHARDEV_VC "chardev-vc"
1121 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
1124 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1126 VCChardev
*drv
= VC_CHARDEV(chr
);
1127 QemuConsole
*s
= drv
->console
;
1134 s
->update_x0
= s
->width
* FONT_WIDTH
;
1135 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1138 console_show_cursor(s
, 0);
1139 for(i
= 0; i
< len
; i
++) {
1140 console_putchar(s
, buf
[i
]);
1142 console_show_cursor(s
, 1);
1143 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1144 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1145 s
->update_x1
- s
->update_x0
,
1146 s
->update_y1
- s
->update_y0
);
1151 static void kbd_send_chars(void *opaque
)
1153 QemuConsole
*s
= opaque
;
1157 len
= qemu_chr_be_can_write(s
->chr
);
1158 if (len
> s
->out_fifo
.count
)
1159 len
= s
->out_fifo
.count
;
1161 if (len
> sizeof(buf
))
1163 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1164 qemu_chr_be_write(s
->chr
, buf
, len
);
1166 /* characters are pending: we send them a bit later (XXX:
1167 horrible, should change char device API) */
1168 if (s
->out_fifo
.count
> 0) {
1169 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1173 /* called when an ascii key is pressed */
1174 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1176 uint8_t buf
[16], *q
;
1180 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1184 case QEMU_KEY_CTRL_UP
:
1185 console_scroll(s
, -1);
1187 case QEMU_KEY_CTRL_DOWN
:
1188 console_scroll(s
, 1);
1190 case QEMU_KEY_CTRL_PAGEUP
:
1191 console_scroll(s
, -10);
1193 case QEMU_KEY_CTRL_PAGEDOWN
:
1194 console_scroll(s
, 10);
1197 /* convert the QEMU keysym to VT100 key string */
1199 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1202 c
= keysym
- 0xe100;
1204 *q
++ = '0' + (c
/ 10);
1205 *q
++ = '0' + (c
% 10);
1207 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1210 *q
++ = keysym
& 0xff;
1211 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1212 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1218 vc_chr_write(s
->chr
, buf
, q
- buf
);
1221 if (be
&& be
->chr_read
) {
1222 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1229 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1230 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1231 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1232 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1233 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1234 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1235 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1236 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1237 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1238 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1239 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1242 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1243 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1244 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1245 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1246 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1247 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1248 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1249 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1250 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1253 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1257 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1261 kbd_put_keysym_console(s
, keysym
);
1265 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1269 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1270 kbd_put_keysym_console(s
, str
[i
]);
1274 void kbd_put_keysym(int keysym
)
1276 kbd_put_keysym_console(active_console
, keysym
);
1279 static void text_console_invalidate(void *opaque
)
1281 QemuConsole
*s
= (QemuConsole
*) opaque
;
1283 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1284 text_console_resize(s
);
1289 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1291 QemuConsole
*s
= (QemuConsole
*) opaque
;
1294 if (s
->text_x
[0] <= s
->text_x
[1]) {
1295 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1296 chardata
+= s
->text_y
[0] * s
->width
;
1297 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1298 for (j
= 0; j
< s
->width
; j
++, src
++) {
1299 console_write_ch(chardata
++,
1300 ATTR2CHTYPE(s
->cells
[src
].ch
,
1301 s
->cells
[src
].t_attrib
.fgcol
,
1302 s
->cells
[src
].t_attrib
.bgcol
,
1303 s
->cells
[src
].t_attrib
.bold
));
1305 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1306 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1307 s
->text_x
[0] = s
->width
;
1308 s
->text_y
[0] = s
->height
;
1312 if (s
->cursor_invalidate
) {
1313 dpy_text_cursor(s
, s
->x
, s
->y
);
1314 s
->cursor_invalidate
= 0;
1318 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1325 obj
= object_new(TYPE_QEMU_CONSOLE
);
1326 s
= QEMU_CONSOLE(obj
);
1327 qemu_co_queue_init(&s
->dump_queue
);
1329 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1330 (Object
**)&s
->device
,
1331 object_property_allow_set_link
,
1332 OBJ_PROP_LINK_STRONG
);
1333 object_property_add_uint32_ptr(obj
, "head", &s
->head
,
1334 OBJ_PROP_FLAG_READ
);
1336 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1337 (console_type
== GRAPHIC_CONSOLE
))) {
1341 s
->console_type
= console_type
;
1344 if (QTAILQ_EMPTY(&consoles
)) {
1346 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1347 } else if (console_type
!= GRAPHIC_CONSOLE
|| phase_check(PHASE_MACHINE_READY
)) {
1348 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1349 s
->index
= last
->index
+ 1;
1350 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1353 * HACK: Put graphical consoles before text consoles.
1355 * Only do that for coldplugged devices. After initial device
1356 * initialization we will not renumber the consoles any more.
1358 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1360 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1361 c
->console_type
== GRAPHIC_CONSOLE
) {
1362 c
= QTAILQ_NEXT(c
, next
);
1364 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1365 /* have no text consoles */
1366 s
->index
= c
->index
+ 1;
1367 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1369 s
->index
= c
->index
;
1370 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1371 /* renumber text consoles */
1372 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1380 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1382 qemu_pixman_image_unref(surface
->image
);
1383 surface
->image
= NULL
;
1385 surface
->format
= PIXMAN_x8r8g8b8
;
1386 surface
->image
= pixman_image_create_bits(surface
->format
,
1389 assert(surface
->image
!= NULL
);
1391 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1394 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1396 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1398 trace_displaysurface_create(surface
, width
, height
);
1399 qemu_alloc_display(surface
, width
, height
);
1403 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1404 pixman_format_code_t format
,
1405 int linesize
, uint8_t *data
)
1407 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1409 trace_displaysurface_create_from(surface
, width
, height
, format
);
1410 surface
->format
= format
;
1411 surface
->image
= pixman_image_create_bits(surface
->format
,
1413 (void *)data
, linesize
);
1414 assert(surface
->image
!= NULL
);
1419 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1421 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1423 trace_displaysurface_create_pixman(surface
);
1424 surface
->format
= pixman_image_get_format(image
);
1425 surface
->image
= pixman_image_ref(image
);
1430 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1433 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1434 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1435 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1436 pixman_image_t
*glyph
;
1440 x
= (w
/ FONT_WIDTH
- len
) / 2;
1441 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1442 for (i
= 0; i
< len
; i
++) {
1443 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1444 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1445 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1446 qemu_pixman_image_unref(glyph
);
1451 void qemu_free_displaysurface(DisplaySurface
*surface
)
1453 if (surface
== NULL
) {
1456 trace_displaysurface_free(surface
);
1457 qemu_pixman_image_unref(surface
->image
);
1461 bool console_has_gl(QemuConsole
*con
)
1463 return con
->gl
!= NULL
;
1466 bool console_has_gl_dmabuf(QemuConsole
*con
)
1468 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1471 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1473 static const char nodev
[] =
1474 "This VM has no graphic display device.";
1475 static DisplaySurface
*dummy
;
1480 if (dcl
->ops
->dpy_gl_ctx_create
) {
1481 /* display has opengl support */
1484 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1485 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1491 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1492 dcl
->ds
= get_alloc_displaystate();
1493 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1494 gui_setup_refresh(dcl
->ds
);
1499 con
= active_console
;
1501 if (dcl
->ops
->dpy_gfx_switch
) {
1503 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1506 dummy
= qemu_create_message_surface(640, 480, nodev
);
1508 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1511 text_console_update_cursor(NULL
);
1514 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1517 DisplayState
*ds
= dcl
->ds
;
1519 dcl
->update_interval
= interval
;
1520 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1521 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1525 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1527 DisplayState
*ds
= dcl
->ds
;
1528 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1532 QLIST_REMOVE(dcl
, next
);
1534 gui_setup_refresh(ds
);
1537 static void dpy_set_ui_info_timer(void *opaque
)
1539 QemuConsole
*con
= opaque
;
1541 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1544 bool dpy_ui_info_supported(QemuConsole
*con
)
1547 con
= active_console
;
1550 return con
->hw_ops
->ui_info
!= NULL
;
1553 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
1556 con
= active_console
;
1559 return &con
->ui_info
;
1562 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1565 con
= active_console
;
1568 if (!dpy_ui_info_supported(con
)) {
1571 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1572 /* nothing changed -- ignore */
1577 * Typically we get a flood of these as the user resizes the window.
1578 * Wait until the dust has settled (one second without updates), then
1579 * go notify the guest.
1581 con
->ui_info
= *info
;
1582 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1586 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1588 DisplayState
*s
= con
->ds
;
1589 DisplayChangeListener
*dcl
;
1594 width
= surface_width(con
->surface
);
1595 height
= surface_height(con
->surface
);
1601 w
= MIN(w
, width
- x
);
1602 h
= MIN(h
, height
- y
);
1604 if (!qemu_console_is_visible(con
)) {
1607 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1608 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1611 if (dcl
->ops
->dpy_gfx_update
) {
1612 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1617 void dpy_gfx_update_full(QemuConsole
*con
)
1619 if (!con
->surface
) {
1622 dpy_gfx_update(con
, 0, 0,
1623 surface_width(con
->surface
),
1624 surface_height(con
->surface
));
1627 void dpy_gfx_replace_surface(QemuConsole
*con
,
1628 DisplaySurface
*surface
)
1630 DisplayState
*s
= con
->ds
;
1631 DisplaySurface
*old_surface
= con
->surface
;
1632 DisplayChangeListener
*dcl
;
1634 assert(old_surface
!= surface
|| surface
== NULL
);
1636 con
->surface
= surface
;
1637 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1638 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1641 if (dcl
->ops
->dpy_gfx_switch
) {
1642 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1645 qemu_free_displaysurface(old_surface
);
1648 bool dpy_gfx_check_format(QemuConsole
*con
,
1649 pixman_format_code_t format
)
1651 DisplayChangeListener
*dcl
;
1652 DisplayState
*s
= con
->ds
;
1654 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1655 if (dcl
->con
&& dcl
->con
!= con
) {
1656 /* dcl bound to another console -> skip */
1659 if (dcl
->ops
->dpy_gfx_check_format
) {
1660 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1664 /* default is to whitelist native 32 bpp only */
1665 if (format
!= qemu_default_pixman_format(32, true)) {
1673 static void dpy_refresh(DisplayState
*s
)
1675 DisplayChangeListener
*dcl
;
1677 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1678 if (dcl
->ops
->dpy_refresh
) {
1679 dcl
->ops
->dpy_refresh(dcl
);
1684 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1686 DisplayState
*s
= con
->ds
;
1687 DisplayChangeListener
*dcl
;
1689 if (!qemu_console_is_visible(con
)) {
1692 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1693 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1696 if (dcl
->ops
->dpy_text_cursor
) {
1697 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1702 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1704 DisplayState
*s
= con
->ds
;
1705 DisplayChangeListener
*dcl
;
1707 if (!qemu_console_is_visible(con
)) {
1710 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1711 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1714 if (dcl
->ops
->dpy_text_update
) {
1715 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1720 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1722 DisplayState
*s
= con
->ds
;
1723 DisplayChangeListener
*dcl
;
1725 if (!qemu_console_is_visible(con
)) {
1728 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1729 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1732 if (dcl
->ops
->dpy_text_resize
) {
1733 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1738 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1740 DisplayState
*s
= con
->ds
;
1741 DisplayChangeListener
*dcl
;
1743 if (!qemu_console_is_visible(con
)) {
1746 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1747 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1750 if (dcl
->ops
->dpy_mouse_set
) {
1751 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1756 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1758 DisplayState
*s
= con
->ds
;
1759 DisplayChangeListener
*dcl
;
1761 if (!qemu_console_is_visible(con
)) {
1764 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1765 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1768 if (dcl
->ops
->dpy_cursor_define
) {
1769 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1774 bool dpy_cursor_define_supported(QemuConsole
*con
)
1776 DisplayState
*s
= con
->ds
;
1777 DisplayChangeListener
*dcl
;
1779 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1780 if (dcl
->ops
->dpy_cursor_define
) {
1787 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1788 struct QEMUGLParams
*qparams
)
1791 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1794 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1797 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1800 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1803 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1806 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1809 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1812 void dpy_gl_scanout_disable(QemuConsole
*con
)
1815 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1816 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1818 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1823 void dpy_gl_scanout_texture(QemuConsole
*con
,
1824 uint32_t backing_id
,
1825 bool backing_y_0_top
,
1826 uint32_t backing_width
,
1827 uint32_t backing_height
,
1828 uint32_t x
, uint32_t y
,
1829 uint32_t width
, uint32_t height
)
1832 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1834 backing_width
, backing_height
,
1835 x
, y
, width
, height
);
1838 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1842 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1845 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1846 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1850 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1851 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1852 have_hot
, hot_x
, hot_y
);
1856 void dpy_gl_cursor_position(QemuConsole
*con
,
1857 uint32_t pos_x
, uint32_t pos_y
)
1861 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1862 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1866 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1871 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1872 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1876 void dpy_gl_update(QemuConsole
*con
,
1877 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1880 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1883 /***********************************************************/
1884 /* register display */
1886 /* console.c internal use only */
1887 static DisplayState
*get_alloc_displaystate(void)
1889 if (!display_state
) {
1890 display_state
= g_new0(DisplayState
, 1);
1891 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1892 text_console_update_cursor
, NULL
);
1894 return display_state
;
1898 * Called by main(), after creating QemuConsoles
1899 * and before initializing ui (sdl/vnc/...).
1901 DisplayState
*init_displaystate(void)
1906 get_alloc_displaystate();
1907 QTAILQ_FOREACH(con
, &consoles
, next
) {
1908 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1910 text_console_do_init(con
->chr
, display_state
);
1913 /* Hook up into the qom tree here (not in new_console()), once
1914 * all QemuConsoles are created and the order / numbering
1915 * doesn't change any more */
1916 name
= g_strdup_printf("console[%d]", con
->index
);
1917 object_property_add_child(container_get(object_get_root(), "/backend"),
1922 return display_state
;
1925 void graphic_console_set_hwops(QemuConsole
*con
,
1926 const GraphicHwOps
*hw_ops
,
1929 con
->hw_ops
= hw_ops
;
1933 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1934 const GraphicHwOps
*hw_ops
,
1937 static const char noinit
[] =
1938 "Guest has not initialized the display (yet).";
1943 DisplaySurface
*surface
;
1945 ds
= get_alloc_displaystate();
1946 s
= qemu_console_lookup_unused();
1948 trace_console_gfx_reuse(s
->index
);
1950 width
= surface_width(s
->surface
);
1951 height
= surface_height(s
->surface
);
1954 trace_console_gfx_new();
1955 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1956 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1957 dpy_set_ui_info_timer
, s
);
1959 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1961 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
1965 surface
= qemu_create_message_surface(width
, height
, noinit
);
1966 dpy_gfx_replace_surface(s
, surface
);
1970 static const GraphicHwOps unused_ops
= {
1974 void graphic_console_close(QemuConsole
*con
)
1976 static const char unplugged
[] =
1977 "Guest display has been unplugged";
1978 DisplaySurface
*surface
;
1983 width
= surface_width(con
->surface
);
1984 height
= surface_height(con
->surface
);
1987 trace_console_gfx_close(con
->index
);
1988 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
1989 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1992 dpy_gl_scanout_disable(con
);
1994 surface
= qemu_create_message_surface(width
, height
, unplugged
);
1995 dpy_gfx_replace_surface(con
, surface
);
1998 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
2002 QTAILQ_FOREACH(con
, &consoles
, next
) {
2003 if (con
->index
== index
) {
2010 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
2016 QTAILQ_FOREACH(con
, &consoles
, next
) {
2017 obj
= object_property_get_link(OBJECT(con
),
2018 "device", &error_abort
);
2019 if (DEVICE(obj
) != dev
) {
2022 h
= object_property_get_uint(OBJECT(con
),
2023 "head", &error_abort
);
2032 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
2033 uint32_t head
, Error
**errp
)
2038 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2040 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2041 "Device '%s' not found", device_id
);
2045 con
= qemu_console_lookup_by_device(dev
, head
);
2047 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2055 QemuConsole
*qemu_console_lookup_unused(void)
2060 QTAILQ_FOREACH(con
, &consoles
, next
) {
2061 if (con
->hw_ops
!= &unused_ops
) {
2064 obj
= object_property_get_link(OBJECT(con
),
2065 "device", &error_abort
);
2074 bool qemu_console_is_visible(QemuConsole
*con
)
2076 return (con
== active_console
) || (con
->dcls
> 0);
2079 bool qemu_console_is_graphic(QemuConsole
*con
)
2082 con
= active_console
;
2084 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2087 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2090 con
= active_console
;
2092 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2095 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2097 assert(con
!= NULL
);
2098 return con
->gl_block
;
2101 char *qemu_console_get_label(QemuConsole
*con
)
2103 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2105 return g_strdup(object_get_typename(con
->device
));
2107 return g_strdup("VGA");
2109 if (con
->chr
&& con
->chr
->label
) {
2110 return g_strdup(con
->chr
->label
);
2112 return g_strdup_printf("vc%d", con
->index
);
2116 int qemu_console_get_index(QemuConsole
*con
)
2119 con
= active_console
;
2121 return con
? con
->index
: -1;
2124 uint32_t qemu_console_get_head(QemuConsole
*con
)
2127 con
= active_console
;
2129 return con
? con
->head
: -1;
2132 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2135 con
= active_console
;
2137 return con
? surface_width(con
->surface
) : fallback
;
2140 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2143 con
= active_console
;
2145 return con
? surface_height(con
->surface
) : fallback
;
2148 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2150 VCChardev
*drv
= VC_CHARDEV(chr
);
2151 QemuConsole
*s
= drv
->console
;
2156 static void text_console_update_cursor_timer(void)
2158 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2159 + CONSOLE_CURSOR_PERIOD
/ 2);
2162 static void text_console_update_cursor(void *opaque
)
2167 cursor_visible_phase
= !cursor_visible_phase
;
2169 QTAILQ_FOREACH(s
, &consoles
, next
) {
2170 if (qemu_console_is_graphic(s
) ||
2171 !qemu_console_is_visible(s
)) {
2175 graphic_hw_invalidate(s
);
2179 text_console_update_cursor_timer();
2183 static const GraphicHwOps text_console_ops
= {
2184 .invalidate
= text_console_invalidate
,
2185 .text_update
= text_console_update
,
2188 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2190 VCChardev
*drv
= VC_CHARDEV(chr
);
2191 QemuConsole
*s
= drv
->console
;
2192 int g_width
= 80 * FONT_WIDTH
;
2193 int g_height
= 24 * FONT_HEIGHT
;
2195 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2196 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2197 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2202 s
->total_height
= DEFAULT_BACKSCROLL
;
2206 if (active_console
&& active_console
->surface
) {
2207 g_width
= surface_width(active_console
->surface
);
2208 g_height
= surface_height(active_console
->surface
);
2210 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2213 s
->hw_ops
= &text_console_ops
;
2216 /* Set text attribute defaults */
2217 s
->t_attrib_default
.bold
= 0;
2218 s
->t_attrib_default
.uline
= 0;
2219 s
->t_attrib_default
.blink
= 0;
2220 s
->t_attrib_default
.invers
= 0;
2221 s
->t_attrib_default
.unvisible
= 0;
2222 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2223 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2224 /* set current text attributes to default */
2225 s
->t_attrib
= s
->t_attrib_default
;
2226 text_console_resize(s
);
2231 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2232 msg
= g_strdup_printf("%s console\r\n", chr
->label
);
2233 vc_chr_write(chr
, (uint8_t *)msg
, strlen(msg
));
2235 s
->t_attrib
= s
->t_attrib_default
;
2238 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2241 static void vc_chr_open(Chardev
*chr
,
2242 ChardevBackend
*backend
,
2246 ChardevVC
*vc
= backend
->u
.vc
.data
;
2247 VCChardev
*drv
= VC_CHARDEV(chr
);
2250 unsigned height
= 0;
2252 if (vc
->has_width
) {
2254 } else if (vc
->has_cols
) {
2255 width
= vc
->cols
* FONT_WIDTH
;
2258 if (vc
->has_height
) {
2259 height
= vc
->height
;
2260 } else if (vc
->has_rows
) {
2261 height
= vc
->rows
* FONT_HEIGHT
;
2264 trace_console_txt_new(width
, height
);
2265 if (width
== 0 || height
== 0) {
2266 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2268 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2269 s
->surface
= qemu_create_displaysurface(width
, height
);
2273 error_setg(errp
, "cannot create text console");
2280 if (display_state
) {
2281 text_console_do_init(chr
, display_state
);
2284 /* console/chardev init sometimes completes elsewhere in a 2nd
2285 * stage, so defer OPENED events until they are fully initialized
2290 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2292 DisplaySurface
*surface
;
2294 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2296 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2297 pixman_image_get_width(s
->surface
->image
) == width
&&
2298 pixman_image_get_height(s
->surface
->image
) == height
) {
2302 surface
= qemu_create_displaysurface(width
, height
);
2303 dpy_gfx_replace_surface(s
, surface
);
2306 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2308 return console
->surface
;
2311 PixelFormat
qemu_default_pixelformat(int bpp
)
2313 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2314 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2318 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2320 void qemu_display_register(QemuDisplay
*ui
)
2322 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2323 dpys
[ui
->type
] = ui
;
2326 bool qemu_display_find_default(DisplayOptions
*opts
)
2328 static DisplayType prio
[] = {
2335 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2336 if (dpys
[prio
[i
]] == NULL
) {
2337 ui_module_load_one(DisplayType_str(prio
[i
]));
2339 if (dpys
[prio
[i
]] == NULL
) {
2342 opts
->type
= prio
[i
];
2348 void qemu_display_early_init(DisplayOptions
*opts
)
2350 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2351 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2354 if (dpys
[opts
->type
] == NULL
) {
2355 ui_module_load_one(DisplayType_str(opts
->type
));
2357 if (dpys
[opts
->type
] == NULL
) {
2358 error_report("Display '%s' is not available.",
2359 DisplayType_str(opts
->type
));
2362 if (dpys
[opts
->type
]->early_init
) {
2363 dpys
[opts
->type
]->early_init(opts
);
2367 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2369 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2370 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2373 assert(dpys
[opts
->type
] != NULL
);
2374 dpys
[opts
->type
]->init(ds
, opts
);
2377 void qemu_display_help(void)
2381 printf("Available display backend types:\n");
2383 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2385 ui_module_load_one(DisplayType_str(idx
));
2388 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2393 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2398 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2399 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2400 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2402 val
= qemu_opt_get_number(opts
, "width", 0);
2404 vc
->has_width
= true;
2408 val
= qemu_opt_get_number(opts
, "height", 0);
2410 vc
->has_height
= true;
2414 val
= qemu_opt_get_number(opts
, "cols", 0);
2416 vc
->has_cols
= true;
2420 val
= qemu_opt_get_number(opts
, "rows", 0);
2422 vc
->has_rows
= true;
2427 static const TypeInfo qemu_console_info
= {
2428 .name
= TYPE_QEMU_CONSOLE
,
2429 .parent
= TYPE_OBJECT
,
2430 .instance_size
= sizeof(QemuConsole
),
2431 .class_size
= sizeof(QemuConsoleClass
),
2434 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2436 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2438 cc
->parse
= qemu_chr_parse_vc
;
2439 cc
->open
= vc_chr_open
;
2440 cc
->chr_write
= vc_chr_write
;
2441 cc
->chr_set_echo
= vc_chr_set_echo
;
2444 static const TypeInfo char_vc_type_info
= {
2445 .name
= TYPE_CHARDEV_VC
,
2446 .parent
= TYPE_CHARDEV
,
2447 .instance_size
= sizeof(VCChardev
),
2448 .class_init
= char_vc_class_init
,
2451 void qemu_console_early_init(void)
2453 /* set the default vc driver */
2454 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2455 type_register(&char_vc_type_info
);
2459 static void register_types(void)
2461 type_register_static(&qemu_console_info
);
2464 type_init(register_types
);