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 void graphic_hw_gl_flushed(QemuConsole
*con
)
301 if (con
->hw_ops
->gl_flushed
) {
302 con
->hw_ops
->gl_flushed(con
->hw
);
306 int qemu_console_get_window_id(QemuConsole
*con
)
308 return con
->window_id
;
311 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
313 con
->window_id
= window_id
;
316 void graphic_hw_invalidate(QemuConsole
*con
)
319 con
= active_console
;
321 if (con
&& con
->hw_ops
->invalidate
) {
322 con
->hw_ops
->invalidate(con
->hw
);
326 static bool ppm_save(int fd
, pixman_image_t
*image
, Error
**errp
)
328 int width
= pixman_image_get_width(image
);
329 int height
= pixman_image_get_height(image
);
330 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
331 g_autofree
char *header
= NULL
;
332 g_autoptr(pixman_image_t
) linebuf
= NULL
;
335 trace_ppm_save(fd
, image
);
337 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
338 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
339 header
, strlen(header
), errp
) < 0) {
343 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
344 for (y
= 0; y
< height
; y
++) {
345 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
346 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
347 (char *)pixman_image_get_data(linebuf
),
348 pixman_image_get_stride(linebuf
), errp
) < 0) {
356 static void graphic_hw_update_bh(void *con
)
358 graphic_hw_update(con
);
361 /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
363 qmp_screendump(const char *filename
, bool has_device
, const char *device
,
364 bool has_head
, int64_t head
, Error
**errp
)
366 g_autoptr(pixman_image_t
) image
= NULL
;
368 DisplaySurface
*surface
;
372 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
379 error_setg(errp
, "'head' must be specified together with 'device'");
382 con
= qemu_console_lookup_by_index(0);
384 error_setg(errp
, "There is no console to take a screendump from");
389 if (qemu_co_queue_empty(&con
->dump_queue
)) {
390 /* Defer the update, it will restart the pending coroutines */
391 aio_bh_schedule_oneshot(qemu_get_aio_context(),
392 graphic_hw_update_bh
, con
);
394 qemu_co_queue_wait(&con
->dump_queue
, NULL
);
397 * All pending coroutines are woken up, while the BQL is held. No
398 * further graphic update are possible until it is released. Take
399 * an image ref before that.
401 surface
= qemu_console_surface(con
);
403 error_setg(errp
, "no surface");
406 image
= pixman_image_ref(surface
->image
);
408 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
410 error_setg(errp
, "failed to open file '%s': %s", filename
,
416 * The image content could potentially be updated as the coroutine
417 * yields and releases the BQL. It could produce corrupted dump, but
418 * it should be otherwise safe.
420 if (!ppm_save(fd
, image
, errp
)) {
421 qemu_unlink(filename
);
425 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
428 con
= active_console
;
430 if (con
&& con
->hw_ops
->text_update
) {
431 con
->hw_ops
->text_update(con
->hw
, chardata
);
435 static void vga_fill_rect(QemuConsole
*con
,
436 int posx
, int posy
, int width
, int height
,
437 pixman_color_t color
)
439 DisplaySurface
*surface
= qemu_console_surface(con
);
440 pixman_rectangle16_t rect
= {
441 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
444 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
448 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
449 static void vga_bitblt(QemuConsole
*con
,
450 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
452 DisplaySurface
*surface
= qemu_console_surface(con
);
454 pixman_image_composite(PIXMAN_OP_SRC
,
455 surface
->image
, NULL
, surface
->image
,
456 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
459 /***********************************************************/
460 /* basic char display */
462 #define FONT_HEIGHT 16
467 #define QEMU_RGB(r, g, b) \
468 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
470 static const pixman_color_t color_table_rgb
[2][8] = {
472 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
473 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
474 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
475 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
476 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
477 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
478 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
479 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
482 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
483 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
484 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
485 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
486 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
487 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
488 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
489 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
493 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
494 TextAttributes
*t_attrib
)
496 static pixman_image_t
*glyphs
[256];
497 DisplaySurface
*surface
= qemu_console_surface(s
);
498 pixman_color_t fgcol
, bgcol
;
500 if (t_attrib
->invers
) {
501 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
502 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
504 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
505 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
509 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
511 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
512 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
515 static void text_console_resize(QemuConsole
*s
)
517 TextCell
*cells
, *c
, *c1
;
518 int w1
, x
, y
, last_width
;
520 last_width
= s
->width
;
521 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
522 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
528 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
529 for(y
= 0; y
< s
->total_height
; y
++) {
530 c
= &cells
[y
* s
->width
];
532 c1
= &s
->cells
[y
* last_width
];
533 for(x
= 0; x
< w1
; x
++) {
537 for(x
= w1
; x
< s
->width
; x
++) {
539 c
->t_attrib
= s
->t_attrib_default
;
547 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
549 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
550 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
551 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
552 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
555 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
557 if (!qemu_console_is_visible(s
)) {
560 if (s
->update_x0
> x
* FONT_WIDTH
)
561 s
->update_x0
= x
* FONT_WIDTH
;
562 if (s
->update_y0
> y
* FONT_HEIGHT
)
563 s
->update_y0
= y
* FONT_HEIGHT
;
564 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
565 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
566 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
567 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
570 static void update_xy(QemuConsole
*s
, int x
, int y
)
575 if (s
->ds
->have_text
) {
576 text_update_xy(s
, x
, y
);
579 y1
= (s
->y_base
+ y
) % s
->total_height
;
580 y2
= y1
- s
->y_displayed
;
582 y2
+= s
->total_height
;
584 if (y2
< s
->height
) {
588 c
= &s
->cells
[y1
* s
->width
+ x
];
589 vga_putcharxy(s
, x
, y2
, c
->ch
,
591 invalidate_xy(s
, x
, y2
);
595 static void console_show_cursor(QemuConsole
*s
, int show
)
601 if (s
->ds
->have_text
) {
602 s
->cursor_invalidate
= 1;
608 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
609 y
= y1
- s
->y_displayed
;
611 y
+= s
->total_height
;
614 c
= &s
->cells
[y1
* s
->width
+ x
];
615 if (show
&& cursor_visible_phase
) {
616 TextAttributes t_attrib
= s
->t_attrib_default
;
617 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
618 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
620 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
622 invalidate_xy(s
, x
, y
);
626 static void console_refresh(QemuConsole
*s
)
628 DisplaySurface
*surface
= qemu_console_surface(s
);
632 if (s
->ds
->have_text
) {
635 s
->text_x
[1] = s
->width
- 1;
636 s
->text_y
[1] = s
->height
- 1;
637 s
->cursor_invalidate
= 1;
640 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
641 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
643 for (y
= 0; y
< s
->height
; y
++) {
644 c
= s
->cells
+ y1
* s
->width
;
645 for (x
= 0; x
< s
->width
; x
++) {
646 vga_putcharxy(s
, x
, y
, c
->ch
,
650 if (++y1
== s
->total_height
) {
654 console_show_cursor(s
, 1);
655 dpy_gfx_update(s
, 0, 0,
656 surface_width(surface
), surface_height(surface
));
659 static void console_scroll(QemuConsole
*s
, int ydelta
)
664 for(i
= 0; i
< ydelta
; i
++) {
665 if (s
->y_displayed
== s
->y_base
)
667 if (++s
->y_displayed
== s
->total_height
)
672 i
= s
->backscroll_height
;
673 if (i
> s
->total_height
- s
->height
)
674 i
= s
->total_height
- s
->height
;
677 y1
+= s
->total_height
;
678 for(i
= 0; i
< ydelta
; i
++) {
679 if (s
->y_displayed
== y1
)
681 if (--s
->y_displayed
< 0)
682 s
->y_displayed
= s
->total_height
- 1;
688 static void console_put_lf(QemuConsole
*s
)
694 if (s
->y
>= s
->height
) {
695 s
->y
= s
->height
- 1;
697 if (s
->y_displayed
== s
->y_base
) {
698 if (++s
->y_displayed
== s
->total_height
)
701 if (++s
->y_base
== s
->total_height
)
703 if (s
->backscroll_height
< s
->total_height
)
704 s
->backscroll_height
++;
705 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
706 c
= &s
->cells
[y1
* s
->width
];
707 for(x
= 0; x
< s
->width
; x
++) {
709 c
->t_attrib
= s
->t_attrib_default
;
712 if (s
->y_displayed
== s
->y_base
) {
713 if (s
->ds
->have_text
) {
716 s
->text_x
[1] = s
->width
- 1;
717 s
->text_y
[1] = s
->height
- 1;
720 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
721 s
->width
* FONT_WIDTH
,
722 (s
->height
- 1) * FONT_HEIGHT
);
723 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
724 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
725 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
728 s
->update_x1
= s
->width
* FONT_WIDTH
;
729 s
->update_y1
= s
->height
* FONT_HEIGHT
;
734 /* Set console attributes depending on the current escape codes.
735 * NOTE: I know this code is not very efficient (checking every color for it
736 * self) but it is more readable and better maintainable.
738 static void console_handle_escape(QemuConsole
*s
)
742 for (i
=0; i
<s
->nb_esc_params
; i
++) {
743 switch (s
->esc_params
[i
]) {
744 case 0: /* reset all console attributes to default */
745 s
->t_attrib
= s
->t_attrib_default
;
748 s
->t_attrib
.bold
= 1;
751 s
->t_attrib
.uline
= 1;
754 s
->t_attrib
.blink
= 1;
757 s
->t_attrib
.invers
= 1;
760 s
->t_attrib
.unvisible
= 1;
763 s
->t_attrib
.bold
= 0;
766 s
->t_attrib
.uline
= 0;
769 s
->t_attrib
.blink
= 0;
772 s
->t_attrib
.invers
= 0;
775 s
->t_attrib
.unvisible
= 0;
777 /* set foreground color */
779 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
782 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
785 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
788 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
791 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
794 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
797 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
800 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
802 /* set background color */
804 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
807 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
810 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
813 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
816 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
819 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
822 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
825 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
831 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
833 int y1
= (s
->y_base
+ y
) % s
->total_height
;
837 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
839 c
->t_attrib
= s
->t_attrib_default
;
843 static void console_put_one(QemuConsole
*s
, int ch
)
847 if (s
->x
>= s
->width
) {
852 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
853 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
855 c
->t_attrib
= s
->t_attrib
;
856 update_xy(s
, s
->x
, s
->y
);
860 static void console_respond_str(QemuConsole
*s
, const char *buf
)
863 console_put_one(s
, *buf
);
868 /* set cursor, checking bounds */
869 static void set_cursor(QemuConsole
*s
, int x
, int y
)
877 if (y
>= s
->height
) {
888 static void console_putchar(QemuConsole
*s
, int ch
)
897 case '\r': /* carriage return */
900 case '\n': /* newline */
903 case '\b': /* backspace */
907 case '\t': /* tabspace */
908 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
912 s
->x
= s
->x
+ (8 - (s
->x
% 8));
915 case '\a': /* alert aka. bell */
916 /* TODO: has to be implemented */
919 /* SI (shift in), character set 0 (ignored) */
922 /* SO (shift out), character set 1 (ignored) */
924 case 27: /* esc (introducing an escape sequence) */
925 s
->state
= TTY_STATE_ESC
;
928 console_put_one(s
, ch
);
932 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
934 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
935 s
->esc_params
[i
] = 0;
936 s
->nb_esc_params
= 0;
937 s
->state
= TTY_STATE_CSI
;
939 s
->state
= TTY_STATE_NORM
;
942 case TTY_STATE_CSI
: /* handle escape sequence parameters */
943 if (ch
>= '0' && ch
<= '9') {
944 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
945 int *param
= &s
->esc_params
[s
->nb_esc_params
];
946 int digit
= (ch
- '0');
948 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
949 *param
* 10 + digit
: INT_MAX
;
952 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
954 if (ch
== ';' || ch
== '?') {
957 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
958 ch
, s
->nb_esc_params
);
959 s
->state
= TTY_STATE_NORM
;
963 if (s
->esc_params
[0] == 0) {
964 s
->esc_params
[0] = 1;
966 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
969 /* move cursor down */
970 if (s
->esc_params
[0] == 0) {
971 s
->esc_params
[0] = 1;
973 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
976 /* move cursor right */
977 if (s
->esc_params
[0] == 0) {
978 s
->esc_params
[0] = 1;
980 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
983 /* move cursor left */
984 if (s
->esc_params
[0] == 0) {
985 s
->esc_params
[0] = 1;
987 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
990 /* move cursor to column */
991 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
995 /* move cursor to row, column */
996 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
999 switch (s
->esc_params
[0]) {
1001 /* clear to end of screen */
1002 for (y
= s
->y
; y
< s
->height
; y
++) {
1003 for (x
= 0; x
< s
->width
; x
++) {
1004 if (y
== s
->y
&& x
< s
->x
) {
1007 console_clear_xy(s
, x
, y
);
1012 /* clear from beginning of screen */
1013 for (y
= 0; y
<= s
->y
; y
++) {
1014 for (x
= 0; x
< s
->width
; x
++) {
1015 if (y
== s
->y
&& x
> s
->x
) {
1018 console_clear_xy(s
, x
, y
);
1023 /* clear entire screen */
1024 for (y
= 0; y
<= s
->height
; y
++) {
1025 for (x
= 0; x
< s
->width
; x
++) {
1026 console_clear_xy(s
, x
, y
);
1033 switch (s
->esc_params
[0]) {
1036 for(x
= s
->x
; x
< s
->width
; x
++) {
1037 console_clear_xy(s
, x
, s
->y
);
1041 /* clear from beginning of line */
1042 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1043 console_clear_xy(s
, x
, s
->y
);
1047 /* clear entire line */
1048 for(x
= 0; x
< s
->width
; x
++) {
1049 console_clear_xy(s
, x
, s
->y
);
1055 console_handle_escape(s
);
1058 switch (s
->esc_params
[0]) {
1060 /* report console status (always succeed)*/
1061 console_respond_str(s
, "\033[0n");
1064 /* report cursor position */
1065 sprintf(response
, "\033[%d;%dR",
1066 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1068 console_respond_str(s
, response
);
1073 /* save cursor position */
1078 /* restore cursor position */
1083 trace_console_putchar_unhandled(ch
);
1091 void console_select(unsigned int index
)
1093 DisplayChangeListener
*dcl
;
1096 trace_console_select(index
);
1097 s
= qemu_console_lookup_by_index(index
);
1099 DisplayState
*ds
= s
->ds
;
1103 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1104 if (dcl
->con
!= NULL
) {
1107 if (dcl
->ops
->dpy_gfx_switch
) {
1108 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1112 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1113 surface_height(s
->surface
));
1116 if (ds
->have_text
) {
1117 dpy_text_resize(s
, s
->width
, s
->height
);
1119 text_console_update_cursor(NULL
);
1125 QemuConsole
*console
;
1127 typedef struct VCChardev VCChardev
;
1129 #define TYPE_CHARDEV_VC "chardev-vc"
1130 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
1133 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1135 VCChardev
*drv
= VC_CHARDEV(chr
);
1136 QemuConsole
*s
= drv
->console
;
1143 s
->update_x0
= s
->width
* FONT_WIDTH
;
1144 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1147 console_show_cursor(s
, 0);
1148 for(i
= 0; i
< len
; i
++) {
1149 console_putchar(s
, buf
[i
]);
1151 console_show_cursor(s
, 1);
1152 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1153 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1154 s
->update_x1
- s
->update_x0
,
1155 s
->update_y1
- s
->update_y0
);
1160 static void kbd_send_chars(void *opaque
)
1162 QemuConsole
*s
= opaque
;
1166 len
= qemu_chr_be_can_write(s
->chr
);
1167 if (len
> s
->out_fifo
.count
)
1168 len
= s
->out_fifo
.count
;
1170 if (len
> sizeof(buf
))
1172 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1173 qemu_chr_be_write(s
->chr
, buf
, len
);
1175 /* characters are pending: we send them a bit later (XXX:
1176 horrible, should change char device API) */
1177 if (s
->out_fifo
.count
> 0) {
1178 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1182 /* called when an ascii key is pressed */
1183 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1185 uint8_t buf
[16], *q
;
1189 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1193 case QEMU_KEY_CTRL_UP
:
1194 console_scroll(s
, -1);
1196 case QEMU_KEY_CTRL_DOWN
:
1197 console_scroll(s
, 1);
1199 case QEMU_KEY_CTRL_PAGEUP
:
1200 console_scroll(s
, -10);
1202 case QEMU_KEY_CTRL_PAGEDOWN
:
1203 console_scroll(s
, 10);
1206 /* convert the QEMU keysym to VT100 key string */
1208 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1211 c
= keysym
- 0xe100;
1213 *q
++ = '0' + (c
/ 10);
1214 *q
++ = '0' + (c
% 10);
1216 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1219 *q
++ = keysym
& 0xff;
1220 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1221 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1227 vc_chr_write(s
->chr
, buf
, q
- buf
);
1230 if (be
&& be
->chr_read
) {
1231 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1238 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1239 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1240 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1241 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1242 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1243 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1244 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1245 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1246 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1247 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1248 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1251 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1252 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1253 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1254 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1255 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1256 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1257 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1258 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1259 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1262 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1266 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1270 kbd_put_keysym_console(s
, keysym
);
1274 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1278 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1279 kbd_put_keysym_console(s
, str
[i
]);
1283 void kbd_put_keysym(int keysym
)
1285 kbd_put_keysym_console(active_console
, keysym
);
1288 static void text_console_invalidate(void *opaque
)
1290 QemuConsole
*s
= (QemuConsole
*) opaque
;
1292 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1293 text_console_resize(s
);
1298 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1300 QemuConsole
*s
= (QemuConsole
*) opaque
;
1303 if (s
->text_x
[0] <= s
->text_x
[1]) {
1304 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1305 chardata
+= s
->text_y
[0] * s
->width
;
1306 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1307 for (j
= 0; j
< s
->width
; j
++, src
++) {
1308 console_write_ch(chardata
++,
1309 ATTR2CHTYPE(s
->cells
[src
].ch
,
1310 s
->cells
[src
].t_attrib
.fgcol
,
1311 s
->cells
[src
].t_attrib
.bgcol
,
1312 s
->cells
[src
].t_attrib
.bold
));
1314 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1315 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1316 s
->text_x
[0] = s
->width
;
1317 s
->text_y
[0] = s
->height
;
1321 if (s
->cursor_invalidate
) {
1322 dpy_text_cursor(s
, s
->x
, s
->y
);
1323 s
->cursor_invalidate
= 0;
1327 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1334 obj
= object_new(TYPE_QEMU_CONSOLE
);
1335 s
= QEMU_CONSOLE(obj
);
1336 qemu_co_queue_init(&s
->dump_queue
);
1338 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1339 (Object
**)&s
->device
,
1340 object_property_allow_set_link
,
1341 OBJ_PROP_LINK_STRONG
);
1342 object_property_add_uint32_ptr(obj
, "head", &s
->head
,
1343 OBJ_PROP_FLAG_READ
);
1345 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1346 (console_type
== GRAPHIC_CONSOLE
))) {
1350 s
->console_type
= console_type
;
1353 if (QTAILQ_EMPTY(&consoles
)) {
1355 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1356 } else if (console_type
!= GRAPHIC_CONSOLE
|| phase_check(PHASE_MACHINE_READY
)) {
1357 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1358 s
->index
= last
->index
+ 1;
1359 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1362 * HACK: Put graphical consoles before text consoles.
1364 * Only do that for coldplugged devices. After initial device
1365 * initialization we will not renumber the consoles any more.
1367 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1369 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1370 c
->console_type
== GRAPHIC_CONSOLE
) {
1371 c
= QTAILQ_NEXT(c
, next
);
1373 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1374 /* have no text consoles */
1375 s
->index
= c
->index
+ 1;
1376 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1378 s
->index
= c
->index
;
1379 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1380 /* renumber text consoles */
1381 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1389 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1391 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1393 trace_displaysurface_create(surface
, width
, height
);
1394 surface
->format
= PIXMAN_x8r8g8b8
;
1395 surface
->image
= pixman_image_create_bits(surface
->format
,
1398 assert(surface
->image
!= NULL
);
1399 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1404 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1405 pixman_format_code_t format
,
1406 int linesize
, uint8_t *data
)
1408 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1410 trace_displaysurface_create_from(surface
, width
, height
, format
);
1411 surface
->format
= format
;
1412 surface
->image
= pixman_image_create_bits(surface
->format
,
1414 (void *)data
, linesize
);
1415 assert(surface
->image
!= NULL
);
1420 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1422 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1424 trace_displaysurface_create_pixman(surface
);
1425 surface
->format
= pixman_image_get_format(image
);
1426 surface
->image
= pixman_image_ref(image
);
1431 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
1434 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1435 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1436 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1437 pixman_image_t
*glyph
;
1441 x
= (w
/ FONT_WIDTH
- len
) / 2;
1442 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1443 for (i
= 0; i
< len
; i
++) {
1444 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1445 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1446 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1447 qemu_pixman_image_unref(glyph
);
1449 surface
->flags
|= QEMU_PLACEHOLDER_FLAG
;
1453 void qemu_free_displaysurface(DisplaySurface
*surface
)
1455 if (surface
== NULL
) {
1458 trace_displaysurface_free(surface
);
1459 qemu_pixman_image_unref(surface
->image
);
1463 bool console_has_gl(QemuConsole
*con
)
1465 return con
->gl
!= NULL
;
1468 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
)
1470 if (dcl
->ops
->dpy_has_dmabuf
) {
1471 return dcl
->ops
->dpy_has_dmabuf(dcl
);
1474 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
1481 static bool dpy_compatible_with(QemuConsole
*con
,
1482 DisplayChangeListener
*dcl
, Error
**errp
)
1486 flags
= con
->hw_ops
->get_flags
? con
->hw_ops
->get_flags(con
->hw
) : 0;
1488 if (flags
& GRAPHIC_FLAGS_GL
&&
1489 !console_has_gl(con
)) {
1490 error_setg(errp
, "The console requires a GL context.");
1495 if (flags
& GRAPHIC_FLAGS_DMABUF
&&
1496 !displaychangelistener_has_dmabuf(dcl
)) {
1497 error_setg(errp
, "The console requires display DMABUF support.");
1504 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1506 static const char nodev
[] =
1507 "This VM has no graphic display device.";
1508 static DisplaySurface
*dummy
;
1513 if (dcl
->ops
->dpy_gl_ctx_create
) {
1514 /* display has opengl support */
1517 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1518 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1525 dpy_compatible_with(dcl
->con
, dcl
, &error_fatal
);
1528 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1529 dcl
->ds
= get_alloc_displaystate();
1530 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1531 gui_setup_refresh(dcl
->ds
);
1536 con
= active_console
;
1538 if (dcl
->ops
->dpy_gfx_switch
) {
1540 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1543 dummy
= qemu_create_placeholder_surface(640, 480, nodev
);
1545 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1548 text_console_update_cursor(NULL
);
1551 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1554 DisplayState
*ds
= dcl
->ds
;
1556 dcl
->update_interval
= interval
;
1557 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1558 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1562 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1564 DisplayState
*ds
= dcl
->ds
;
1565 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1569 QLIST_REMOVE(dcl
, next
);
1571 gui_setup_refresh(ds
);
1574 static void dpy_set_ui_info_timer(void *opaque
)
1576 QemuConsole
*con
= opaque
;
1578 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1581 bool dpy_ui_info_supported(QemuConsole
*con
)
1584 con
= active_console
;
1587 return con
->hw_ops
->ui_info
!= NULL
;
1590 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
1593 con
= active_console
;
1596 return &con
->ui_info
;
1599 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1602 con
= active_console
;
1605 if (!dpy_ui_info_supported(con
)) {
1608 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1609 /* nothing changed -- ignore */
1614 * Typically we get a flood of these as the user resizes the window.
1615 * Wait until the dust has settled (one second without updates), then
1616 * go notify the guest.
1618 con
->ui_info
= *info
;
1619 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1623 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1625 DisplayState
*s
= con
->ds
;
1626 DisplayChangeListener
*dcl
;
1631 width
= surface_width(con
->surface
);
1632 height
= surface_height(con
->surface
);
1638 w
= MIN(w
, width
- x
);
1639 h
= MIN(h
, height
- y
);
1641 if (!qemu_console_is_visible(con
)) {
1644 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1645 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1648 if (dcl
->ops
->dpy_gfx_update
) {
1649 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1654 void dpy_gfx_update_full(QemuConsole
*con
)
1656 if (!con
->surface
) {
1659 dpy_gfx_update(con
, 0, 0,
1660 surface_width(con
->surface
),
1661 surface_height(con
->surface
));
1664 void dpy_gfx_replace_surface(QemuConsole
*con
,
1665 DisplaySurface
*surface
)
1667 static const char placeholder_msg
[] = "Display output is not active.";
1668 DisplayState
*s
= con
->ds
;
1669 DisplaySurface
*old_surface
= con
->surface
;
1670 DisplayChangeListener
*dcl
;
1676 width
= surface_width(old_surface
);
1677 height
= surface_height(old_surface
);
1683 surface
= qemu_create_placeholder_surface(width
, height
, placeholder_msg
);
1686 assert(old_surface
!= surface
);
1688 con
->surface
= surface
;
1689 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1690 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1693 if (dcl
->ops
->dpy_gfx_switch
) {
1694 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1697 qemu_free_displaysurface(old_surface
);
1700 bool dpy_gfx_check_format(QemuConsole
*con
,
1701 pixman_format_code_t format
)
1703 DisplayChangeListener
*dcl
;
1704 DisplayState
*s
= con
->ds
;
1706 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1707 if (dcl
->con
&& dcl
->con
!= con
) {
1708 /* dcl bound to another console -> skip */
1711 if (dcl
->ops
->dpy_gfx_check_format
) {
1712 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1716 /* default is to allow native 32 bpp only */
1717 if (format
!= qemu_default_pixman_format(32, true)) {
1725 static void dpy_refresh(DisplayState
*s
)
1727 DisplayChangeListener
*dcl
;
1729 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1730 if (dcl
->ops
->dpy_refresh
) {
1731 dcl
->ops
->dpy_refresh(dcl
);
1736 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1738 DisplayState
*s
= con
->ds
;
1739 DisplayChangeListener
*dcl
;
1741 if (!qemu_console_is_visible(con
)) {
1744 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1745 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1748 if (dcl
->ops
->dpy_text_cursor
) {
1749 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1754 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1756 DisplayState
*s
= con
->ds
;
1757 DisplayChangeListener
*dcl
;
1759 if (!qemu_console_is_visible(con
)) {
1762 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1763 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1766 if (dcl
->ops
->dpy_text_update
) {
1767 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1772 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1774 DisplayState
*s
= con
->ds
;
1775 DisplayChangeListener
*dcl
;
1777 if (!qemu_console_is_visible(con
)) {
1780 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1781 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1784 if (dcl
->ops
->dpy_text_resize
) {
1785 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1790 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1792 DisplayState
*s
= con
->ds
;
1793 DisplayChangeListener
*dcl
;
1795 if (!qemu_console_is_visible(con
)) {
1798 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1799 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1802 if (dcl
->ops
->dpy_mouse_set
) {
1803 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1808 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1810 DisplayState
*s
= con
->ds
;
1811 DisplayChangeListener
*dcl
;
1813 if (!qemu_console_is_visible(con
)) {
1816 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1817 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1820 if (dcl
->ops
->dpy_cursor_define
) {
1821 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1826 bool dpy_cursor_define_supported(QemuConsole
*con
)
1828 DisplayState
*s
= con
->ds
;
1829 DisplayChangeListener
*dcl
;
1831 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1832 if (dcl
->ops
->dpy_cursor_define
) {
1839 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1840 struct QEMUGLParams
*qparams
)
1843 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1846 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1849 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1852 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1855 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1858 void dpy_gl_scanout_disable(QemuConsole
*con
)
1861 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1864 void dpy_gl_scanout_texture(QemuConsole
*con
,
1865 uint32_t backing_id
,
1866 bool backing_y_0_top
,
1867 uint32_t backing_width
,
1868 uint32_t backing_height
,
1869 uint32_t x
, uint32_t y
,
1870 uint32_t width
, uint32_t height
)
1873 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1875 backing_width
, backing_height
,
1876 x
, y
, width
, height
);
1879 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1883 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1886 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1887 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1891 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1892 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1893 have_hot
, hot_x
, hot_y
);
1897 void dpy_gl_cursor_position(QemuConsole
*con
,
1898 uint32_t pos_x
, uint32_t pos_y
)
1902 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1903 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1907 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1912 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1913 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1917 void dpy_gl_update(QemuConsole
*con
,
1918 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1921 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1924 /***********************************************************/
1925 /* register display */
1927 /* console.c internal use only */
1928 static DisplayState
*get_alloc_displaystate(void)
1930 if (!display_state
) {
1931 display_state
= g_new0(DisplayState
, 1);
1932 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1933 text_console_update_cursor
, NULL
);
1935 return display_state
;
1939 * Called by main(), after creating QemuConsoles
1940 * and before initializing ui (sdl/vnc/...).
1942 DisplayState
*init_displaystate(void)
1947 get_alloc_displaystate();
1948 QTAILQ_FOREACH(con
, &consoles
, next
) {
1949 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1951 text_console_do_init(con
->chr
, display_state
);
1954 /* Hook up into the qom tree here (not in new_console()), once
1955 * all QemuConsoles are created and the order / numbering
1956 * doesn't change any more */
1957 name
= g_strdup_printf("console[%d]", con
->index
);
1958 object_property_add_child(container_get(object_get_root(), "/backend"),
1963 return display_state
;
1966 void graphic_console_set_hwops(QemuConsole
*con
,
1967 const GraphicHwOps
*hw_ops
,
1970 con
->hw_ops
= hw_ops
;
1974 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1975 const GraphicHwOps
*hw_ops
,
1978 static const char noinit
[] =
1979 "Guest has not initialized the display (yet).";
1984 DisplaySurface
*surface
;
1986 ds
= get_alloc_displaystate();
1987 s
= qemu_console_lookup_unused();
1989 trace_console_gfx_reuse(s
->index
);
1991 width
= surface_width(s
->surface
);
1992 height
= surface_height(s
->surface
);
1995 trace_console_gfx_new();
1996 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1997 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1998 dpy_set_ui_info_timer
, s
);
2000 graphic_console_set_hwops(s
, hw_ops
, opaque
);
2002 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
2006 surface
= qemu_create_placeholder_surface(width
, height
, noinit
);
2007 dpy_gfx_replace_surface(s
, surface
);
2011 static const GraphicHwOps unused_ops
= {
2015 void graphic_console_close(QemuConsole
*con
)
2017 static const char unplugged
[] =
2018 "Guest display has been unplugged";
2019 DisplaySurface
*surface
;
2024 width
= surface_width(con
->surface
);
2025 height
= surface_height(con
->surface
);
2028 trace_console_gfx_close(con
->index
);
2029 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
2030 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
2033 dpy_gl_scanout_disable(con
);
2035 surface
= qemu_create_placeholder_surface(width
, height
, unplugged
);
2036 dpy_gfx_replace_surface(con
, surface
);
2039 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
2043 QTAILQ_FOREACH(con
, &consoles
, next
) {
2044 if (con
->index
== index
) {
2051 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
2057 QTAILQ_FOREACH(con
, &consoles
, next
) {
2058 obj
= object_property_get_link(OBJECT(con
),
2059 "device", &error_abort
);
2060 if (DEVICE(obj
) != dev
) {
2063 h
= object_property_get_uint(OBJECT(con
),
2064 "head", &error_abort
);
2073 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
2074 uint32_t head
, Error
**errp
)
2079 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2081 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2082 "Device '%s' not found", device_id
);
2086 con
= qemu_console_lookup_by_device(dev
, head
);
2088 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2096 QemuConsole
*qemu_console_lookup_unused(void)
2101 QTAILQ_FOREACH(con
, &consoles
, next
) {
2102 if (con
->hw_ops
!= &unused_ops
) {
2105 obj
= object_property_get_link(OBJECT(con
),
2106 "device", &error_abort
);
2115 bool qemu_console_is_visible(QemuConsole
*con
)
2117 return (con
== active_console
) || (con
->dcls
> 0);
2120 bool qemu_console_is_graphic(QemuConsole
*con
)
2123 con
= active_console
;
2125 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2128 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2131 con
= active_console
;
2133 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2136 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2138 assert(con
!= NULL
);
2139 return con
->gl_block
;
2142 char *qemu_console_get_label(QemuConsole
*con
)
2144 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2146 return g_strdup(object_get_typename(con
->device
));
2148 return g_strdup("VGA");
2150 if (con
->chr
&& con
->chr
->label
) {
2151 return g_strdup(con
->chr
->label
);
2153 return g_strdup_printf("vc%d", con
->index
);
2157 int qemu_console_get_index(QemuConsole
*con
)
2160 con
= active_console
;
2162 return con
? con
->index
: -1;
2165 uint32_t qemu_console_get_head(QemuConsole
*con
)
2168 con
= active_console
;
2170 return con
? con
->head
: -1;
2173 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2176 con
= active_console
;
2178 return con
? surface_width(con
->surface
) : fallback
;
2181 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2184 con
= active_console
;
2186 return con
? surface_height(con
->surface
) : fallback
;
2189 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2191 VCChardev
*drv
= VC_CHARDEV(chr
);
2192 QemuConsole
*s
= drv
->console
;
2197 static void text_console_update_cursor_timer(void)
2199 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2200 + CONSOLE_CURSOR_PERIOD
/ 2);
2203 static void text_console_update_cursor(void *opaque
)
2208 cursor_visible_phase
= !cursor_visible_phase
;
2210 QTAILQ_FOREACH(s
, &consoles
, next
) {
2211 if (qemu_console_is_graphic(s
) ||
2212 !qemu_console_is_visible(s
)) {
2216 graphic_hw_invalidate(s
);
2220 text_console_update_cursor_timer();
2224 static const GraphicHwOps text_console_ops
= {
2225 .invalidate
= text_console_invalidate
,
2226 .text_update
= text_console_update
,
2229 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2231 VCChardev
*drv
= VC_CHARDEV(chr
);
2232 QemuConsole
*s
= drv
->console
;
2233 int g_width
= 80 * FONT_WIDTH
;
2234 int g_height
= 24 * FONT_HEIGHT
;
2236 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2237 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2238 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2243 s
->total_height
= DEFAULT_BACKSCROLL
;
2247 if (active_console
&& active_console
->surface
) {
2248 g_width
= surface_width(active_console
->surface
);
2249 g_height
= surface_height(active_console
->surface
);
2251 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2254 s
->hw_ops
= &text_console_ops
;
2257 /* Set text attribute defaults */
2258 s
->t_attrib_default
.bold
= 0;
2259 s
->t_attrib_default
.uline
= 0;
2260 s
->t_attrib_default
.blink
= 0;
2261 s
->t_attrib_default
.invers
= 0;
2262 s
->t_attrib_default
.unvisible
= 0;
2263 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2264 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2265 /* set current text attributes to default */
2266 s
->t_attrib
= s
->t_attrib_default
;
2267 text_console_resize(s
);
2272 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2273 msg
= g_strdup_printf("%s console\r\n", chr
->label
);
2274 vc_chr_write(chr
, (uint8_t *)msg
, strlen(msg
));
2276 s
->t_attrib
= s
->t_attrib_default
;
2279 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2282 static void vc_chr_open(Chardev
*chr
,
2283 ChardevBackend
*backend
,
2287 ChardevVC
*vc
= backend
->u
.vc
.data
;
2288 VCChardev
*drv
= VC_CHARDEV(chr
);
2291 unsigned height
= 0;
2293 if (vc
->has_width
) {
2295 } else if (vc
->has_cols
) {
2296 width
= vc
->cols
* FONT_WIDTH
;
2299 if (vc
->has_height
) {
2300 height
= vc
->height
;
2301 } else if (vc
->has_rows
) {
2302 height
= vc
->rows
* FONT_HEIGHT
;
2305 trace_console_txt_new(width
, height
);
2306 if (width
== 0 || height
== 0) {
2307 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2309 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2310 s
->surface
= qemu_create_displaysurface(width
, height
);
2314 error_setg(errp
, "cannot create text console");
2321 if (display_state
) {
2322 text_console_do_init(chr
, display_state
);
2325 /* console/chardev init sometimes completes elsewhere in a 2nd
2326 * stage, so defer OPENED events until they are fully initialized
2331 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2333 DisplaySurface
*surface
;
2335 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2337 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2338 pixman_image_get_width(s
->surface
->image
) == width
&&
2339 pixman_image_get_height(s
->surface
->image
) == height
) {
2343 surface
= qemu_create_displaysurface(width
, height
);
2344 dpy_gfx_replace_surface(s
, surface
);
2347 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2349 return console
->surface
;
2352 PixelFormat
qemu_default_pixelformat(int bpp
)
2354 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2355 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2359 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2361 void qemu_display_register(QemuDisplay
*ui
)
2363 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2364 dpys
[ui
->type
] = ui
;
2367 bool qemu_display_find_default(DisplayOptions
*opts
)
2369 static DisplayType prio
[] = {
2370 #if defined(CONFIG_GTK)
2373 #if defined(CONFIG_SDL)
2376 #if defined(CONFIG_COCOA)
2382 for (i
= 0; i
< (int)ARRAY_SIZE(prio
); i
++) {
2383 if (dpys
[prio
[i
]] == NULL
) {
2384 ui_module_load_one(DisplayType_str(prio
[i
]));
2386 if (dpys
[prio
[i
]] == NULL
) {
2389 opts
->type
= prio
[i
];
2395 void qemu_display_early_init(DisplayOptions
*opts
)
2397 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2398 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2401 if (dpys
[opts
->type
] == NULL
) {
2402 ui_module_load_one(DisplayType_str(opts
->type
));
2404 if (dpys
[opts
->type
] == NULL
) {
2405 error_report("Display '%s' is not available.",
2406 DisplayType_str(opts
->type
));
2409 if (dpys
[opts
->type
]->early_init
) {
2410 dpys
[opts
->type
]->early_init(opts
);
2414 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2416 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2417 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2420 assert(dpys
[opts
->type
] != NULL
);
2421 dpys
[opts
->type
]->init(ds
, opts
);
2424 void qemu_display_help(void)
2428 printf("Available display backend types:\n");
2430 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2432 ui_module_load_one(DisplayType_str(idx
));
2435 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2440 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2445 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2446 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2447 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2449 val
= qemu_opt_get_number(opts
, "width", 0);
2451 vc
->has_width
= true;
2455 val
= qemu_opt_get_number(opts
, "height", 0);
2457 vc
->has_height
= true;
2461 val
= qemu_opt_get_number(opts
, "cols", 0);
2463 vc
->has_cols
= true;
2467 val
= qemu_opt_get_number(opts
, "rows", 0);
2469 vc
->has_rows
= true;
2474 static const TypeInfo qemu_console_info
= {
2475 .name
= TYPE_QEMU_CONSOLE
,
2476 .parent
= TYPE_OBJECT
,
2477 .instance_size
= sizeof(QemuConsole
),
2478 .class_size
= sizeof(QemuConsoleClass
),
2481 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2483 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2485 cc
->parse
= qemu_chr_parse_vc
;
2486 cc
->open
= vc_chr_open
;
2487 cc
->chr_write
= vc_chr_write
;
2488 cc
->chr_set_echo
= vc_chr_set_echo
;
2491 static const TypeInfo char_vc_type_info
= {
2492 .name
= TYPE_CHARDEV_VC
,
2493 .parent
= TYPE_CHARDEV
,
2494 .instance_size
= sizeof(VCChardev
),
2495 .class_init
= char_vc_class_init
,
2498 void qemu_console_early_init(void)
2500 /* set the default vc driver */
2501 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2502 type_register(&char_vc_type_info
);
2506 static void register_types(void)
2508 type_register_static(&qemu_console_info
);
2511 type_init(register_types
);