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/coroutine.h"
31 #include "qemu/error-report.h"
32 #include "qemu/fifo8.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/module.h"
35 #include "qemu/option.h"
36 #include "qemu/timer.h"
37 #include "chardev/char.h"
39 #include "exec/memory.h"
40 #include "io/channel-file.h"
41 #include "qom/object.h"
46 #define DEFAULT_BACKSCROLL 512
47 #define CONSOLE_CURSOR_PERIOD 500
49 typedef struct TextAttributes
{
59 typedef struct TextCell
{
61 TextAttributes t_attrib
;
64 #define MAX_ESC_PARAMS 3
75 TEXT_CONSOLE_FIXED_SIZE
82 console_type_t console_type
;
84 DisplaySurface
*surface
;
85 DisplayScanout scanout
;
89 QEMUTimer
*gl_unblock_timer
;
92 /* Graphic console state. */
98 int cursor_x
, cursor_y
, cursor_on
;
99 const GraphicHwOps
*hw_ops
;
102 /* Text console state */
106 int backscroll_height
;
108 int x_saved
, y_saved
;
111 TextAttributes t_attrib_default
; /* default text attributes */
112 TextAttributes t_attrib
; /* currently active text attributes */
114 int text_x
[2], text_y
[2], cursor_invalidate
;
123 int esc_params
[MAX_ESC_PARAMS
];
127 /* fifo for key pressed */
131 QTAILQ_ENTRY(QemuConsole
) next
;
134 struct DisplayState
{
135 QEMUTimer
*gui_timer
;
136 uint64_t last_update
;
137 uint64_t update_interval
;
142 QLIST_HEAD(, DisplayChangeListener
) listeners
;
145 static DisplayState
*display_state
;
146 static QemuConsole
*active_console
;
147 static QTAILQ_HEAD(, QemuConsole
) consoles
=
148 QTAILQ_HEAD_INITIALIZER(consoles
);
149 static bool cursor_visible_phase
;
150 static QEMUTimer
*cursor_timer
;
152 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
153 static void dpy_refresh(DisplayState
*s
);
154 static DisplayState
*get_alloc_displaystate(void);
155 static void text_console_update_cursor_timer(void);
156 static void text_console_update_cursor(void *opaque
);
157 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
);
158 static bool console_compatible_with(QemuConsole
*con
,
159 DisplayChangeListener
*dcl
, Error
**errp
);
161 static void gui_update(void *opaque
)
163 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
164 uint64_t dcl_interval
;
165 DisplayState
*ds
= opaque
;
166 DisplayChangeListener
*dcl
;
168 ds
->refreshing
= true;
170 ds
->refreshing
= false;
172 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
173 dcl_interval
= dcl
->update_interval
?
174 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
175 if (interval
> dcl_interval
) {
176 interval
= dcl_interval
;
179 if (ds
->update_interval
!= interval
) {
180 ds
->update_interval
= interval
;
181 trace_console_refresh(interval
);
183 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
184 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
187 static void gui_setup_refresh(DisplayState
*ds
)
189 DisplayChangeListener
*dcl
;
190 bool need_timer
= false;
191 bool have_gfx
= false;
192 bool have_text
= false;
194 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
195 if (dcl
->ops
->dpy_refresh
!= NULL
) {
198 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
201 if (dcl
->ops
->dpy_text_update
!= NULL
) {
206 if (need_timer
&& ds
->gui_timer
== NULL
) {
207 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
208 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
210 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
211 timer_free(ds
->gui_timer
);
212 ds
->gui_timer
= NULL
;
215 ds
->have_gfx
= have_gfx
;
216 ds
->have_text
= have_text
;
219 void graphic_hw_update_done(QemuConsole
*con
)
222 qemu_co_enter_all(&con
->dump_queue
, NULL
);
226 void graphic_hw_update(QemuConsole
*con
)
229 con
= con
? con
: active_console
;
233 if (con
->hw_ops
->gfx_update
) {
234 con
->hw_ops
->gfx_update(con
->hw
);
235 async
= con
->hw_ops
->gfx_update_async
;
238 graphic_hw_update_done(con
);
242 static void graphic_hw_gl_unblock_timer(void *opaque
)
244 warn_report("console: no gl-unblock within one second");
247 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
257 assert(con
->gl_block
>= 0);
258 if (!con
->hw_ops
->gl_block
) {
261 if ((block
&& con
->gl_block
!= 1) || (!block
&& con
->gl_block
!= 0)) {
264 con
->hw_ops
->gl_block(con
->hw
, block
);
267 timeout
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
268 timeout
+= 1000; /* one sec */
269 timer_mod(con
->gl_unblock_timer
, timeout
);
271 timer_del(con
->gl_unblock_timer
);
275 int qemu_console_get_window_id(QemuConsole
*con
)
277 return con
->window_id
;
280 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
282 con
->window_id
= window_id
;
285 void graphic_hw_invalidate(QemuConsole
*con
)
288 con
= active_console
;
290 if (con
&& con
->hw_ops
->invalidate
) {
291 con
->hw_ops
->invalidate(con
->hw
);
297 * png_save: Take a screenshot as PNG
299 * Saves screendump as a PNG file
301 * Returns true for success or false for error.
303 * @fd: File descriptor for PNG file.
304 * @image: Image data in pixman format.
305 * @errp: Pointer to an error.
307 static bool png_save(int fd
, pixman_image_t
*image
, Error
**errp
)
309 int width
= pixman_image_get_width(image
);
310 int height
= pixman_image_get_height(image
);
313 g_autoptr(pixman_image_t
) linebuf
=
314 qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
315 uint8_t *buf
= (uint8_t *)pixman_image_get_data(linebuf
);
316 FILE *f
= fdopen(fd
, "wb");
319 error_setg_errno(errp
, errno
,
320 "Failed to create file from file descriptor");
324 png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
,
327 error_setg(errp
, "PNG creation failed. Unable to write struct");
332 info_ptr
= png_create_info_struct(png_ptr
);
335 error_setg(errp
, "PNG creation failed. Unable to write info");
337 png_destroy_write_struct(&png_ptr
, &info_ptr
);
341 png_init_io(png_ptr
, f
);
343 png_set_IHDR(png_ptr
, info_ptr
, width
, height
, 8,
344 PNG_COLOR_TYPE_RGB
, PNG_INTERLACE_NONE
,
345 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
347 png_write_info(png_ptr
, info_ptr
);
349 for (y
= 0; y
< height
; ++y
) {
350 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
351 png_write_row(png_ptr
, buf
);
354 png_write_end(png_ptr
, NULL
);
356 png_destroy_write_struct(&png_ptr
, &info_ptr
);
358 if (fclose(f
) != 0) {
359 error_setg_errno(errp
, errno
,
360 "PNG creation failed. Unable to close file");
367 #else /* no png support */
369 static bool png_save(int fd
, pixman_image_t
*image
, Error
**errp
)
371 error_setg(errp
, "Enable PNG support with libpng for screendump");
375 #endif /* CONFIG_PNG */
377 static bool ppm_save(int fd
, pixman_image_t
*image
, Error
**errp
)
379 int width
= pixman_image_get_width(image
);
380 int height
= pixman_image_get_height(image
);
381 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
382 g_autofree
char *header
= NULL
;
383 g_autoptr(pixman_image_t
) linebuf
= NULL
;
386 trace_ppm_save(fd
, image
);
388 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
389 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
390 header
, strlen(header
), errp
) < 0) {
394 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
395 for (y
= 0; y
< height
; y
++) {
396 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
397 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
398 (char *)pixman_image_get_data(linebuf
),
399 pixman_image_get_stride(linebuf
), errp
) < 0) {
407 static void graphic_hw_update_bh(void *con
)
409 graphic_hw_update(con
);
412 /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
414 qmp_screendump(const char *filename
, const char *device
,
415 bool has_head
, int64_t head
,
416 bool has_format
, ImageFormat format
, Error
**errp
)
418 g_autoptr(pixman_image_t
) image
= NULL
;
420 DisplaySurface
*surface
;
424 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
431 error_setg(errp
, "'head' must be specified together with 'device'");
434 con
= qemu_console_lookup_by_index(0);
436 error_setg(errp
, "There is no console to take a screendump from");
441 if (qemu_co_queue_empty(&con
->dump_queue
)) {
442 /* Defer the update, it will restart the pending coroutines */
443 aio_bh_schedule_oneshot(qemu_get_aio_context(),
444 graphic_hw_update_bh
, con
);
446 qemu_co_queue_wait(&con
->dump_queue
, NULL
);
449 * All pending coroutines are woken up, while the BQL is held. No
450 * further graphic update are possible until it is released. Take
451 * an image ref before that.
453 surface
= qemu_console_surface(con
);
455 error_setg(errp
, "no surface");
458 image
= pixman_image_ref(surface
->image
);
460 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
462 error_setg(errp
, "failed to open file '%s': %s", filename
,
468 * The image content could potentially be updated as the coroutine
469 * yields and releases the BQL. It could produce corrupted dump, but
470 * it should be otherwise safe.
472 if (has_format
&& format
== IMAGE_FORMAT_PNG
) {
473 /* PNG format specified for screendump */
474 if (!png_save(fd
, image
, errp
)) {
475 qemu_unlink(filename
);
478 /* PPM format specified/default for screendump */
479 if (!ppm_save(fd
, image
, errp
)) {
480 qemu_unlink(filename
);
485 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
488 con
= active_console
;
490 if (con
&& con
->hw_ops
->text_update
) {
491 con
->hw_ops
->text_update(con
->hw
, chardata
);
495 static void vga_fill_rect(QemuConsole
*con
,
496 int posx
, int posy
, int width
, int height
,
497 pixman_color_t color
)
499 DisplaySurface
*surface
= qemu_console_surface(con
);
500 pixman_rectangle16_t rect
= {
501 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
504 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
508 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
509 static void vga_bitblt(QemuConsole
*con
,
510 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
512 DisplaySurface
*surface
= qemu_console_surface(con
);
514 pixman_image_composite(PIXMAN_OP_SRC
,
515 surface
->image
, NULL
, surface
->image
,
516 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
519 /***********************************************************/
520 /* basic char display */
522 #define FONT_HEIGHT 16
527 #define QEMU_RGB(r, g, b) \
528 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
530 static const pixman_color_t color_table_rgb
[2][8] = {
532 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
533 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
534 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
535 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
536 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
537 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
538 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
539 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
542 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
543 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
544 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
545 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
546 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
547 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
548 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
549 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
553 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
554 TextAttributes
*t_attrib
)
556 static pixman_image_t
*glyphs
[256];
557 DisplaySurface
*surface
= qemu_console_surface(s
);
558 pixman_color_t fgcol
, bgcol
;
560 if (t_attrib
->invers
) {
561 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
562 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
564 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
565 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
569 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
571 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
572 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
575 static void text_console_resize(QemuConsole
*s
)
577 TextCell
*cells
, *c
, *c1
;
578 int w1
, x
, y
, last_width
;
580 assert(s
->scanout
.kind
== SCANOUT_SURFACE
);
582 last_width
= s
->width
;
583 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
584 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
590 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
591 for(y
= 0; y
< s
->total_height
; y
++) {
592 c
= &cells
[y
* s
->width
];
594 c1
= &s
->cells
[y
* last_width
];
595 for(x
= 0; x
< w1
; x
++) {
599 for(x
= w1
; x
< s
->width
; x
++) {
601 c
->t_attrib
= s
->t_attrib_default
;
609 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
611 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
612 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
613 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
614 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
617 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
619 if (!qemu_console_is_visible(s
)) {
622 if (s
->update_x0
> x
* FONT_WIDTH
)
623 s
->update_x0
= x
* FONT_WIDTH
;
624 if (s
->update_y0
> y
* FONT_HEIGHT
)
625 s
->update_y0
= y
* FONT_HEIGHT
;
626 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
627 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
628 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
629 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
632 static void update_xy(QemuConsole
*s
, int x
, int y
)
637 if (s
->ds
->have_text
) {
638 text_update_xy(s
, x
, y
);
641 y1
= (s
->y_base
+ y
) % s
->total_height
;
642 y2
= y1
- s
->y_displayed
;
644 y2
+= s
->total_height
;
646 if (y2
< s
->height
) {
650 c
= &s
->cells
[y1
* s
->width
+ x
];
651 vga_putcharxy(s
, x
, y2
, c
->ch
,
653 invalidate_xy(s
, x
, y2
);
657 static void console_show_cursor(QemuConsole
*s
, int show
)
663 if (s
->ds
->have_text
) {
664 s
->cursor_invalidate
= 1;
670 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
671 y
= y1
- s
->y_displayed
;
673 y
+= s
->total_height
;
676 c
= &s
->cells
[y1
* s
->width
+ x
];
677 if (show
&& cursor_visible_phase
) {
678 TextAttributes t_attrib
= s
->t_attrib_default
;
679 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
680 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
682 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
684 invalidate_xy(s
, x
, y
);
688 static void console_refresh(QemuConsole
*s
)
690 DisplaySurface
*surface
= qemu_console_surface(s
);
694 if (s
->ds
->have_text
) {
697 s
->text_x
[1] = s
->width
- 1;
698 s
->text_y
[1] = s
->height
- 1;
699 s
->cursor_invalidate
= 1;
702 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
703 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
705 for (y
= 0; y
< s
->height
; y
++) {
706 c
= s
->cells
+ y1
* s
->width
;
707 for (x
= 0; x
< s
->width
; x
++) {
708 vga_putcharxy(s
, x
, y
, c
->ch
,
712 if (++y1
== s
->total_height
) {
716 console_show_cursor(s
, 1);
717 dpy_gfx_update(s
, 0, 0,
718 surface_width(surface
), surface_height(surface
));
721 static void console_scroll(QemuConsole
*s
, int ydelta
)
726 for(i
= 0; i
< ydelta
; i
++) {
727 if (s
->y_displayed
== s
->y_base
)
729 if (++s
->y_displayed
== s
->total_height
)
734 i
= s
->backscroll_height
;
735 if (i
> s
->total_height
- s
->height
)
736 i
= s
->total_height
- s
->height
;
739 y1
+= s
->total_height
;
740 for(i
= 0; i
< ydelta
; i
++) {
741 if (s
->y_displayed
== y1
)
743 if (--s
->y_displayed
< 0)
744 s
->y_displayed
= s
->total_height
- 1;
750 static void console_put_lf(QemuConsole
*s
)
756 if (s
->y
>= s
->height
) {
757 s
->y
= s
->height
- 1;
759 if (s
->y_displayed
== s
->y_base
) {
760 if (++s
->y_displayed
== s
->total_height
)
763 if (++s
->y_base
== s
->total_height
)
765 if (s
->backscroll_height
< s
->total_height
)
766 s
->backscroll_height
++;
767 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
768 c
= &s
->cells
[y1
* s
->width
];
769 for(x
= 0; x
< s
->width
; x
++) {
771 c
->t_attrib
= s
->t_attrib_default
;
774 if (s
->y_displayed
== s
->y_base
) {
775 if (s
->ds
->have_text
) {
778 s
->text_x
[1] = s
->width
- 1;
779 s
->text_y
[1] = s
->height
- 1;
782 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
783 s
->width
* FONT_WIDTH
,
784 (s
->height
- 1) * FONT_HEIGHT
);
785 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
786 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
787 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
790 s
->update_x1
= s
->width
* FONT_WIDTH
;
791 s
->update_y1
= s
->height
* FONT_HEIGHT
;
796 /* Set console attributes depending on the current escape codes.
797 * NOTE: I know this code is not very efficient (checking every color for it
798 * self) but it is more readable and better maintainable.
800 static void console_handle_escape(QemuConsole
*s
)
804 for (i
=0; i
<s
->nb_esc_params
; i
++) {
805 switch (s
->esc_params
[i
]) {
806 case 0: /* reset all console attributes to default */
807 s
->t_attrib
= s
->t_attrib_default
;
810 s
->t_attrib
.bold
= 1;
813 s
->t_attrib
.uline
= 1;
816 s
->t_attrib
.blink
= 1;
819 s
->t_attrib
.invers
= 1;
822 s
->t_attrib
.unvisible
= 1;
825 s
->t_attrib
.bold
= 0;
828 s
->t_attrib
.uline
= 0;
831 s
->t_attrib
.blink
= 0;
834 s
->t_attrib
.invers
= 0;
837 s
->t_attrib
.unvisible
= 0;
839 /* set foreground color */
841 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
844 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
847 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
850 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
853 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
856 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
859 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
862 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
864 /* set background color */
866 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
869 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
872 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
875 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
878 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
881 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
884 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
887 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
893 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
895 int y1
= (s
->y_base
+ y
) % s
->total_height
;
899 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
901 c
->t_attrib
= s
->t_attrib_default
;
905 static void console_put_one(QemuConsole
*s
, int ch
)
909 if (s
->x
>= s
->width
) {
914 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
915 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
917 c
->t_attrib
= s
->t_attrib
;
918 update_xy(s
, s
->x
, s
->y
);
922 static void console_respond_str(QemuConsole
*s
, const char *buf
)
925 console_put_one(s
, *buf
);
930 /* set cursor, checking bounds */
931 static void set_cursor(QemuConsole
*s
, int x
, int y
)
939 if (y
>= s
->height
) {
950 static void console_putchar(QemuConsole
*s
, int ch
)
959 case '\r': /* carriage return */
962 case '\n': /* newline */
965 case '\b': /* backspace */
969 case '\t': /* tabspace */
970 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
974 s
->x
= s
->x
+ (8 - (s
->x
% 8));
977 case '\a': /* alert aka. bell */
978 /* TODO: has to be implemented */
981 /* SI (shift in), character set 0 (ignored) */
984 /* SO (shift out), character set 1 (ignored) */
986 case 27: /* esc (introducing an escape sequence) */
987 s
->state
= TTY_STATE_ESC
;
990 console_put_one(s
, ch
);
994 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
996 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
997 s
->esc_params
[i
] = 0;
998 s
->nb_esc_params
= 0;
999 s
->state
= TTY_STATE_CSI
;
1001 s
->state
= TTY_STATE_NORM
;
1004 case TTY_STATE_CSI
: /* handle escape sequence parameters */
1005 if (ch
>= '0' && ch
<= '9') {
1006 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
1007 int *param
= &s
->esc_params
[s
->nb_esc_params
];
1008 int digit
= (ch
- '0');
1010 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
1011 *param
* 10 + digit
: INT_MAX
;
1014 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
1016 if (ch
== ';' || ch
== '?') {
1019 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
1020 ch
, s
->nb_esc_params
);
1021 s
->state
= TTY_STATE_NORM
;
1024 /* move cursor up */
1025 if (s
->esc_params
[0] == 0) {
1026 s
->esc_params
[0] = 1;
1028 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
1031 /* move cursor down */
1032 if (s
->esc_params
[0] == 0) {
1033 s
->esc_params
[0] = 1;
1035 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
1038 /* move cursor right */
1039 if (s
->esc_params
[0] == 0) {
1040 s
->esc_params
[0] = 1;
1042 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
1045 /* move cursor left */
1046 if (s
->esc_params
[0] == 0) {
1047 s
->esc_params
[0] = 1;
1049 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
1052 /* move cursor to column */
1053 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
1057 /* move cursor to row, column */
1058 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
1061 switch (s
->esc_params
[0]) {
1063 /* clear to end of screen */
1064 for (y
= s
->y
; y
< s
->height
; y
++) {
1065 for (x
= 0; x
< s
->width
; x
++) {
1066 if (y
== s
->y
&& x
< s
->x
) {
1069 console_clear_xy(s
, x
, y
);
1074 /* clear from beginning of screen */
1075 for (y
= 0; y
<= s
->y
; y
++) {
1076 for (x
= 0; x
< s
->width
; x
++) {
1077 if (y
== s
->y
&& x
> s
->x
) {
1080 console_clear_xy(s
, x
, y
);
1085 /* clear entire screen */
1086 for (y
= 0; y
<= s
->height
; y
++) {
1087 for (x
= 0; x
< s
->width
; x
++) {
1088 console_clear_xy(s
, x
, y
);
1095 switch (s
->esc_params
[0]) {
1098 for(x
= s
->x
; x
< s
->width
; x
++) {
1099 console_clear_xy(s
, x
, s
->y
);
1103 /* clear from beginning of line */
1104 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1105 console_clear_xy(s
, x
, s
->y
);
1109 /* clear entire line */
1110 for(x
= 0; x
< s
->width
; x
++) {
1111 console_clear_xy(s
, x
, s
->y
);
1117 console_handle_escape(s
);
1120 switch (s
->esc_params
[0]) {
1122 /* report console status (always succeed)*/
1123 console_respond_str(s
, "\033[0n");
1126 /* report cursor position */
1127 sprintf(response
, "\033[%d;%dR",
1128 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1130 console_respond_str(s
, response
);
1135 /* save cursor position */
1140 /* restore cursor position */
1145 trace_console_putchar_unhandled(ch
);
1153 static void displaychangelistener_gfx_switch(DisplayChangeListener
*dcl
,
1154 struct DisplaySurface
*new_surface
,
1157 if (dcl
->ops
->dpy_gfx_switch
) {
1158 dcl
->ops
->dpy_gfx_switch(dcl
, new_surface
);
1161 if (update
&& dcl
->ops
->dpy_gfx_update
) {
1162 dcl
->ops
->dpy_gfx_update(dcl
, 0, 0,
1163 surface_width(new_surface
),
1164 surface_height(new_surface
));
1168 static void dpy_gfx_create_texture(QemuConsole
*con
, DisplaySurface
*surface
)
1170 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_create_texture
) {
1171 con
->gl
->ops
->dpy_gl_ctx_create_texture(con
->gl
, surface
);
1175 static void dpy_gfx_destroy_texture(QemuConsole
*con
, DisplaySurface
*surface
)
1177 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_destroy_texture
) {
1178 con
->gl
->ops
->dpy_gl_ctx_destroy_texture(con
->gl
, surface
);
1182 static void dpy_gfx_update_texture(QemuConsole
*con
, DisplaySurface
*surface
,
1183 int x
, int y
, int w
, int h
)
1185 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_update_texture
) {
1186 con
->gl
->ops
->dpy_gl_ctx_update_texture(con
->gl
, surface
, x
, y
, w
, h
);
1190 static void displaychangelistener_display_console(DisplayChangeListener
*dcl
,
1194 static const char nodev
[] =
1195 "This VM has no graphic display device.";
1196 static DisplaySurface
*dummy
;
1198 if (!con
|| !console_compatible_with(con
, dcl
, errp
)) {
1200 dummy
= qemu_create_placeholder_surface(640, 480, nodev
);
1203 dpy_gfx_create_texture(con
, dummy
);
1205 displaychangelistener_gfx_switch(dcl
, dummy
, TRUE
);
1209 dpy_gfx_create_texture(con
, con
->surface
);
1210 displaychangelistener_gfx_switch(dcl
, con
->surface
,
1211 con
->scanout
.kind
== SCANOUT_SURFACE
);
1213 if (con
->scanout
.kind
== SCANOUT_DMABUF
&&
1214 displaychangelistener_has_dmabuf(dcl
)) {
1215 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, con
->scanout
.dmabuf
);
1216 } else if (con
->scanout
.kind
== SCANOUT_TEXTURE
&&
1217 dcl
->ops
->dpy_gl_scanout_texture
) {
1218 dcl
->ops
->dpy_gl_scanout_texture(dcl
,
1219 con
->scanout
.texture
.backing_id
,
1220 con
->scanout
.texture
.backing_y_0_top
,
1221 con
->scanout
.texture
.backing_width
,
1222 con
->scanout
.texture
.backing_height
,
1223 con
->scanout
.texture
.x
,
1224 con
->scanout
.texture
.y
,
1225 con
->scanout
.texture
.width
,
1226 con
->scanout
.texture
.height
);
1230 void console_select(unsigned int index
)
1232 DisplayChangeListener
*dcl
;
1235 trace_console_select(index
);
1236 s
= qemu_console_lookup_by_index(index
);
1238 DisplayState
*ds
= s
->ds
;
1242 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1243 if (dcl
->con
!= NULL
) {
1246 displaychangelistener_display_console(dcl
, s
, NULL
);
1249 if (ds
->have_text
) {
1250 dpy_text_resize(s
, s
->width
, s
->height
);
1252 text_console_update_cursor(NULL
);
1258 QemuConsole
*console
;
1260 typedef struct VCChardev VCChardev
;
1262 #define TYPE_CHARDEV_VC "chardev-vc"
1263 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
1266 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1268 VCChardev
*drv
= VC_CHARDEV(chr
);
1269 QemuConsole
*s
= drv
->console
;
1276 s
->update_x0
= s
->width
* FONT_WIDTH
;
1277 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1280 console_show_cursor(s
, 0);
1281 for(i
= 0; i
< len
; i
++) {
1282 console_putchar(s
, buf
[i
]);
1284 console_show_cursor(s
, 1);
1285 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1286 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1287 s
->update_x1
- s
->update_x0
,
1288 s
->update_y1
- s
->update_y0
);
1293 static void kbd_send_chars(QemuConsole
*s
)
1295 uint32_t len
, avail
;
1297 len
= qemu_chr_be_can_write(s
->chr
);
1298 avail
= fifo8_num_used(&s
->out_fifo
);
1299 while (len
> 0 && avail
> 0) {
1303 buf
= fifo8_pop_buf(&s
->out_fifo
, MIN(len
, avail
), &size
);
1304 qemu_chr_be_write(s
->chr
, buf
, size
);
1305 len
= qemu_chr_be_can_write(s
->chr
);
1310 /* called when an ascii key is pressed */
1311 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1313 uint8_t buf
[16], *q
;
1317 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1321 case QEMU_KEY_CTRL_UP
:
1322 console_scroll(s
, -1);
1324 case QEMU_KEY_CTRL_DOWN
:
1325 console_scroll(s
, 1);
1327 case QEMU_KEY_CTRL_PAGEUP
:
1328 console_scroll(s
, -10);
1330 case QEMU_KEY_CTRL_PAGEDOWN
:
1331 console_scroll(s
, 10);
1334 /* convert the QEMU keysym to VT100 key string */
1336 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1339 c
= keysym
- 0xe100;
1341 *q
++ = '0' + (c
/ 10);
1342 *q
++ = '0' + (c
% 10);
1344 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1347 *q
++ = keysym
& 0xff;
1348 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1349 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1355 vc_chr_write(s
->chr
, buf
, q
- buf
);
1357 num_free
= fifo8_num_free(&s
->out_fifo
);
1358 fifo8_push_all(&s
->out_fifo
, buf
, MIN(num_free
, q
- buf
));
1364 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1365 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1366 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1367 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1368 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1369 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1370 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1371 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1372 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1373 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1374 [Q_KEY_CODE_TAB
] = QEMU_KEY_TAB
,
1375 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1378 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1379 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1380 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1381 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1382 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1383 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1384 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1385 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1386 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1389 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1393 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1397 kbd_put_keysym_console(s
, keysym
);
1401 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1405 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1406 kbd_put_keysym_console(s
, str
[i
]);
1410 void kbd_put_keysym(int keysym
)
1412 kbd_put_keysym_console(active_console
, keysym
);
1415 static void text_console_invalidate(void *opaque
)
1417 QemuConsole
*s
= (QemuConsole
*) opaque
;
1419 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1420 text_console_resize(s
);
1425 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1427 QemuConsole
*s
= (QemuConsole
*) opaque
;
1430 if (s
->text_x
[0] <= s
->text_x
[1]) {
1431 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1432 chardata
+= s
->text_y
[0] * s
->width
;
1433 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1434 for (j
= 0; j
< s
->width
; j
++, src
++) {
1435 console_write_ch(chardata
++,
1436 ATTR2CHTYPE(s
->cells
[src
].ch
,
1437 s
->cells
[src
].t_attrib
.fgcol
,
1438 s
->cells
[src
].t_attrib
.bgcol
,
1439 s
->cells
[src
].t_attrib
.bold
));
1441 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1442 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1443 s
->text_x
[0] = s
->width
;
1444 s
->text_y
[0] = s
->height
;
1448 if (s
->cursor_invalidate
) {
1449 dpy_text_cursor(s
, s
->x
, s
->y
);
1450 s
->cursor_invalidate
= 0;
1454 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1461 obj
= object_new(TYPE_QEMU_CONSOLE
);
1462 s
= QEMU_CONSOLE(obj
);
1463 qemu_co_queue_init(&s
->dump_queue
);
1465 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1466 (Object
**)&s
->device
,
1467 object_property_allow_set_link
,
1468 OBJ_PROP_LINK_STRONG
);
1469 object_property_add_uint32_ptr(obj
, "head", &s
->head
,
1470 OBJ_PROP_FLAG_READ
);
1472 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1473 (console_type
== GRAPHIC_CONSOLE
))) {
1477 s
->console_type
= console_type
;
1480 if (QTAILQ_EMPTY(&consoles
)) {
1482 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1483 } else if (console_type
!= GRAPHIC_CONSOLE
|| phase_check(PHASE_MACHINE_READY
)) {
1484 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1485 s
->index
= last
->index
+ 1;
1486 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1489 * HACK: Put graphical consoles before text consoles.
1491 * Only do that for coldplugged devices. After initial device
1492 * initialization we will not renumber the consoles any more.
1494 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1496 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1497 c
->console_type
== GRAPHIC_CONSOLE
) {
1498 c
= QTAILQ_NEXT(c
, next
);
1500 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1501 /* have no text consoles */
1502 s
->index
= c
->index
+ 1;
1503 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1505 s
->index
= c
->index
;
1506 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1507 /* renumber text consoles */
1508 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1516 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1518 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1520 trace_displaysurface_create(surface
, width
, height
);
1521 surface
->format
= PIXMAN_x8r8g8b8
;
1522 surface
->image
= pixman_image_create_bits(surface
->format
,
1525 assert(surface
->image
!= NULL
);
1526 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1531 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1532 pixman_format_code_t format
,
1533 int linesize
, uint8_t *data
)
1535 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1537 trace_displaysurface_create_from(surface
, width
, height
, format
);
1538 surface
->format
= format
;
1539 surface
->image
= pixman_image_create_bits(surface
->format
,
1541 (void *)data
, linesize
);
1542 assert(surface
->image
!= NULL
);
1547 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1549 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1551 trace_displaysurface_create_pixman(surface
);
1552 surface
->format
= pixman_image_get_format(image
);
1553 surface
->image
= pixman_image_ref(image
);
1558 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
1561 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1562 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1563 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1564 pixman_image_t
*glyph
;
1568 x
= (w
/ FONT_WIDTH
- len
) / 2;
1569 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1570 for (i
= 0; i
< len
; i
++) {
1571 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1572 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1573 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1574 qemu_pixman_image_unref(glyph
);
1576 surface
->flags
|= QEMU_PLACEHOLDER_FLAG
;
1580 void qemu_free_displaysurface(DisplaySurface
*surface
)
1582 if (surface
== NULL
) {
1585 trace_displaysurface_free(surface
);
1586 qemu_pixman_image_unref(surface
->image
);
1590 bool console_has_gl(QemuConsole
*con
)
1592 return con
->gl
!= NULL
;
1595 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
)
1597 if (dcl
->ops
->dpy_has_dmabuf
) {
1598 return dcl
->ops
->dpy_has_dmabuf(dcl
);
1601 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
1608 static bool console_compatible_with(QemuConsole
*con
,
1609 DisplayChangeListener
*dcl
, Error
**errp
)
1613 flags
= con
->hw_ops
->get_flags
? con
->hw_ops
->get_flags(con
->hw
) : 0;
1615 if (console_has_gl(con
) &&
1616 !con
->gl
->ops
->dpy_gl_ctx_is_compatible_dcl(con
->gl
, dcl
)) {
1617 error_setg(errp
, "Display %s is incompatible with the GL context",
1618 dcl
->ops
->dpy_name
);
1622 if (flags
& GRAPHIC_FLAGS_GL
&&
1623 !console_has_gl(con
)) {
1624 error_setg(errp
, "The console requires a GL context.");
1629 if (flags
& GRAPHIC_FLAGS_DMABUF
&&
1630 !displaychangelistener_has_dmabuf(dcl
)) {
1631 error_setg(errp
, "The console requires display DMABUF support.");
1638 void console_handle_touch_event(QemuConsole
*con
,
1639 struct touch_slot touch_slots
[INPUT_EVENT_SLOTS_MAX
],
1641 int width
, int height
,
1643 InputMultiTouchType type
,
1646 struct touch_slot
*slot
;
1647 bool needs_sync
= false;
1651 if (num_slot
>= INPUT_EVENT_SLOTS_MAX
) {
1653 "Unexpected touch slot number: % " PRId64
" >= %d",
1654 num_slot
, INPUT_EVENT_SLOTS_MAX
);
1658 slot
= &touch_slots
[num_slot
];
1662 if (type
== INPUT_MULTI_TOUCH_TYPE_BEGIN
) {
1663 slot
->tracking_id
= num_slot
;
1666 for (i
= 0; i
< INPUT_EVENT_SLOTS_MAX
; ++i
) {
1667 if (i
== num_slot
) {
1670 update
= INPUT_MULTI_TOUCH_TYPE_UPDATE
;
1673 slot
= &touch_slots
[i
];
1675 if (slot
->tracking_id
== -1) {
1679 if (update
== INPUT_MULTI_TOUCH_TYPE_END
) {
1680 slot
->tracking_id
= -1;
1681 qemu_input_queue_mtt(con
, update
, i
, slot
->tracking_id
);
1684 qemu_input_queue_mtt(con
, update
, i
, slot
->tracking_id
);
1685 qemu_input_queue_btn(con
, INPUT_BUTTON_TOUCH
, true);
1686 qemu_input_queue_mtt_abs(con
,
1687 INPUT_AXIS_X
, (int) slot
->x
,
1689 i
, slot
->tracking_id
);
1690 qemu_input_queue_mtt_abs(con
,
1691 INPUT_AXIS_Y
, (int) slot
->y
,
1693 i
, slot
->tracking_id
);
1699 qemu_input_event_sync();
1703 void qemu_console_set_display_gl_ctx(QemuConsole
*con
, DisplayGLCtx
*gl
)
1705 /* display has opengl support */
1708 error_report("The console already has an OpenGL context.");
1714 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1720 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1721 dcl
->ds
= get_alloc_displaystate();
1722 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1723 gui_setup_refresh(dcl
->ds
);
1728 con
= active_console
;
1730 displaychangelistener_display_console(dcl
, con
, dcl
->con
? &error_fatal
: NULL
);
1731 if (con
&& con
->cursor
&& dcl
->ops
->dpy_cursor_define
) {
1732 dcl
->ops
->dpy_cursor_define(dcl
, con
->cursor
);
1734 if (con
&& dcl
->ops
->dpy_mouse_set
) {
1735 dcl
->ops
->dpy_mouse_set(dcl
, con
->cursor_x
, con
->cursor_y
, con
->cursor_on
);
1737 text_console_update_cursor(NULL
);
1740 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1743 DisplayState
*ds
= dcl
->ds
;
1745 dcl
->update_interval
= interval
;
1746 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1747 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1751 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1753 DisplayState
*ds
= dcl
->ds
;
1754 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1758 QLIST_REMOVE(dcl
, next
);
1760 gui_setup_refresh(ds
);
1763 static void dpy_set_ui_info_timer(void *opaque
)
1765 QemuConsole
*con
= opaque
;
1767 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1770 bool dpy_ui_info_supported(QemuConsole
*con
)
1773 con
= active_console
;
1776 return con
->hw_ops
->ui_info
!= NULL
;
1779 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
1782 con
= active_console
;
1785 return &con
->ui_info
;
1788 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
, bool delay
)
1791 con
= active_console
;
1794 if (!dpy_ui_info_supported(con
)) {
1797 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1798 /* nothing changed -- ignore */
1803 * Typically we get a flood of these as the user resizes the window.
1804 * Wait until the dust has settled (one second without updates), then
1805 * go notify the guest.
1807 con
->ui_info
= *info
;
1808 timer_mod(con
->ui_timer
,
1809 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + (delay
? 1000 : 0));
1813 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1815 DisplayState
*s
= con
->ds
;
1816 DisplayChangeListener
*dcl
;
1817 int width
= qemu_console_get_width(con
, x
+ w
);
1818 int height
= qemu_console_get_height(con
, y
+ h
);
1824 w
= MIN(w
, width
- x
);
1825 h
= MIN(h
, height
- y
);
1827 if (!qemu_console_is_visible(con
)) {
1830 dpy_gfx_update_texture(con
, con
->surface
, x
, y
, w
, h
);
1831 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1832 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1835 if (dcl
->ops
->dpy_gfx_update
) {
1836 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1841 void dpy_gfx_update_full(QemuConsole
*con
)
1843 int w
= qemu_console_get_width(con
, 0);
1844 int h
= qemu_console_get_height(con
, 0);
1846 dpy_gfx_update(con
, 0, 0, w
, h
);
1849 void dpy_gfx_replace_surface(QemuConsole
*con
,
1850 DisplaySurface
*surface
)
1852 static const char placeholder_msg
[] = "Display output is not active.";
1853 DisplayState
*s
= con
->ds
;
1854 DisplaySurface
*old_surface
= con
->surface
;
1855 DisplayChangeListener
*dcl
;
1861 width
= surface_width(old_surface
);
1862 height
= surface_height(old_surface
);
1868 surface
= qemu_create_placeholder_surface(width
, height
, placeholder_msg
);
1871 assert(old_surface
!= surface
);
1873 con
->scanout
.kind
= SCANOUT_SURFACE
;
1874 con
->surface
= surface
;
1875 dpy_gfx_create_texture(con
, surface
);
1876 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1877 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1880 displaychangelistener_gfx_switch(dcl
, surface
, FALSE
);
1882 dpy_gfx_destroy_texture(con
, old_surface
);
1883 qemu_free_displaysurface(old_surface
);
1886 bool dpy_gfx_check_format(QemuConsole
*con
,
1887 pixman_format_code_t format
)
1889 DisplayChangeListener
*dcl
;
1890 DisplayState
*s
= con
->ds
;
1892 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1893 if (dcl
->con
&& dcl
->con
!= con
) {
1894 /* dcl bound to another console -> skip */
1897 if (dcl
->ops
->dpy_gfx_check_format
) {
1898 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1902 /* default is to allow native 32 bpp only */
1903 if (format
!= qemu_default_pixman_format(32, true)) {
1911 static void dpy_refresh(DisplayState
*s
)
1913 DisplayChangeListener
*dcl
;
1915 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1916 if (dcl
->ops
->dpy_refresh
) {
1917 dcl
->ops
->dpy_refresh(dcl
);
1922 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1924 DisplayState
*s
= con
->ds
;
1925 DisplayChangeListener
*dcl
;
1927 if (!qemu_console_is_visible(con
)) {
1930 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1931 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1934 if (dcl
->ops
->dpy_text_cursor
) {
1935 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1940 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1942 DisplayState
*s
= con
->ds
;
1943 DisplayChangeListener
*dcl
;
1945 if (!qemu_console_is_visible(con
)) {
1948 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1949 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1952 if (dcl
->ops
->dpy_text_update
) {
1953 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1958 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1960 DisplayState
*s
= con
->ds
;
1961 DisplayChangeListener
*dcl
;
1963 if (!qemu_console_is_visible(con
)) {
1966 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1967 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1970 if (dcl
->ops
->dpy_text_resize
) {
1971 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1976 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1978 DisplayState
*s
= con
->ds
;
1979 DisplayChangeListener
*dcl
;
1983 con
->cursor_on
= on
;
1984 if (!qemu_console_is_visible(con
)) {
1987 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1988 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1991 if (dcl
->ops
->dpy_mouse_set
) {
1992 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1997 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1999 DisplayState
*s
= con
->ds
;
2000 DisplayChangeListener
*dcl
;
2002 cursor_unref(con
->cursor
);
2003 con
->cursor
= cursor_ref(cursor
);
2004 if (!qemu_console_is_visible(con
)) {
2007 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2008 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2011 if (dcl
->ops
->dpy_cursor_define
) {
2012 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
2017 bool dpy_cursor_define_supported(QemuConsole
*con
)
2019 DisplayState
*s
= con
->ds
;
2020 DisplayChangeListener
*dcl
;
2022 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2023 if (dcl
->ops
->dpy_cursor_define
) {
2030 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
2031 struct QEMUGLParams
*qparams
)
2034 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
2037 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
2040 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
2043 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
2046 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
2049 void dpy_gl_scanout_disable(QemuConsole
*con
)
2051 DisplayState
*s
= con
->ds
;
2052 DisplayChangeListener
*dcl
;
2054 if (con
->scanout
.kind
!= SCANOUT_SURFACE
) {
2055 con
->scanout
.kind
= SCANOUT_NONE
;
2057 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2058 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2061 if (dcl
->ops
->dpy_gl_scanout_disable
) {
2062 dcl
->ops
->dpy_gl_scanout_disable(dcl
);
2067 void dpy_gl_scanout_texture(QemuConsole
*con
,
2068 uint32_t backing_id
,
2069 bool backing_y_0_top
,
2070 uint32_t backing_width
,
2071 uint32_t backing_height
,
2072 uint32_t x
, uint32_t y
,
2073 uint32_t width
, uint32_t height
)
2075 DisplayState
*s
= con
->ds
;
2076 DisplayChangeListener
*dcl
;
2078 con
->scanout
.kind
= SCANOUT_TEXTURE
;
2079 con
->scanout
.texture
= (ScanoutTexture
) {
2080 backing_id
, backing_y_0_top
, backing_width
, backing_height
,
2083 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2084 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2087 if (dcl
->ops
->dpy_gl_scanout_texture
) {
2088 dcl
->ops
->dpy_gl_scanout_texture(dcl
, backing_id
,
2090 backing_width
, backing_height
,
2091 x
, y
, width
, height
);
2096 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
2099 DisplayState
*s
= con
->ds
;
2100 DisplayChangeListener
*dcl
;
2102 con
->scanout
.kind
= SCANOUT_DMABUF
;
2103 con
->scanout
.dmabuf
= dmabuf
;
2104 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2105 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2108 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
2109 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, dmabuf
);
2114 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
2115 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
2117 DisplayState
*s
= con
->ds
;
2118 DisplayChangeListener
*dcl
;
2120 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2121 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2124 if (dcl
->ops
->dpy_gl_cursor_dmabuf
) {
2125 dcl
->ops
->dpy_gl_cursor_dmabuf(dcl
, dmabuf
,
2126 have_hot
, hot_x
, hot_y
);
2131 void dpy_gl_cursor_position(QemuConsole
*con
,
2132 uint32_t pos_x
, uint32_t pos_y
)
2134 DisplayState
*s
= con
->ds
;
2135 DisplayChangeListener
*dcl
;
2137 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2138 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2141 if (dcl
->ops
->dpy_gl_cursor_position
) {
2142 dcl
->ops
->dpy_gl_cursor_position(dcl
, pos_x
, pos_y
);
2147 void dpy_gl_release_dmabuf(QemuConsole
*con
,
2150 DisplayState
*s
= con
->ds
;
2151 DisplayChangeListener
*dcl
;
2153 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2154 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2157 if (dcl
->ops
->dpy_gl_release_dmabuf
) {
2158 dcl
->ops
->dpy_gl_release_dmabuf(dcl
, dmabuf
);
2163 void dpy_gl_update(QemuConsole
*con
,
2164 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
2166 DisplayState
*s
= con
->ds
;
2167 DisplayChangeListener
*dcl
;
2171 graphic_hw_gl_block(con
, true);
2172 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2173 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2176 if (dcl
->ops
->dpy_gl_update
) {
2177 dcl
->ops
->dpy_gl_update(dcl
, x
, y
, w
, h
);
2180 graphic_hw_gl_block(con
, false);
2183 /***********************************************************/
2184 /* register display */
2186 /* console.c internal use only */
2187 static DisplayState
*get_alloc_displaystate(void)
2189 if (!display_state
) {
2190 display_state
= g_new0(DisplayState
, 1);
2191 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2192 text_console_update_cursor
, NULL
);
2194 return display_state
;
2198 * Called by main(), after creating QemuConsoles
2199 * and before initializing ui (sdl/vnc/...).
2201 DisplayState
*init_displaystate(void)
2206 get_alloc_displaystate();
2207 QTAILQ_FOREACH(con
, &consoles
, next
) {
2208 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
2210 text_console_do_init(con
->chr
, display_state
);
2213 /* Hook up into the qom tree here (not in new_console()), once
2214 * all QemuConsoles are created and the order / numbering
2215 * doesn't change any more */
2216 name
= g_strdup_printf("console[%d]", con
->index
);
2217 object_property_add_child(container_get(object_get_root(), "/backend"),
2222 return display_state
;
2225 void graphic_console_set_hwops(QemuConsole
*con
,
2226 const GraphicHwOps
*hw_ops
,
2229 con
->hw_ops
= hw_ops
;
2233 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
2234 const GraphicHwOps
*hw_ops
,
2237 static const char noinit
[] =
2238 "Guest has not initialized the display (yet).";
2243 DisplaySurface
*surface
;
2245 ds
= get_alloc_displaystate();
2246 s
= qemu_console_lookup_unused();
2248 trace_console_gfx_reuse(s
->index
);
2249 width
= qemu_console_get_width(s
, 0);
2250 height
= qemu_console_get_height(s
, 0);
2252 trace_console_gfx_new();
2253 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
2254 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2255 dpy_set_ui_info_timer
, s
);
2257 graphic_console_set_hwops(s
, hw_ops
, opaque
);
2259 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
2263 surface
= qemu_create_placeholder_surface(width
, height
, noinit
);
2264 dpy_gfx_replace_surface(s
, surface
);
2265 s
->gl_unblock_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2266 graphic_hw_gl_unblock_timer
, s
);
2270 static const GraphicHwOps unused_ops
= {
2274 void graphic_console_close(QemuConsole
*con
)
2276 static const char unplugged
[] =
2277 "Guest display has been unplugged";
2278 DisplaySurface
*surface
;
2279 int width
= qemu_console_get_width(con
, 640);
2280 int height
= qemu_console_get_height(con
, 480);
2282 trace_console_gfx_close(con
->index
);
2283 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
2284 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
2287 dpy_gl_scanout_disable(con
);
2289 surface
= qemu_create_placeholder_surface(width
, height
, unplugged
);
2290 dpy_gfx_replace_surface(con
, surface
);
2293 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
2297 QTAILQ_FOREACH(con
, &consoles
, next
) {
2298 if (con
->index
== index
) {
2305 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
2311 QTAILQ_FOREACH(con
, &consoles
, next
) {
2312 obj
= object_property_get_link(OBJECT(con
),
2313 "device", &error_abort
);
2314 if (DEVICE(obj
) != dev
) {
2317 h
= object_property_get_uint(OBJECT(con
),
2318 "head", &error_abort
);
2327 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
2328 uint32_t head
, Error
**errp
)
2333 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2335 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2336 "Device '%s' not found", device_id
);
2340 con
= qemu_console_lookup_by_device(dev
, head
);
2342 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2350 QemuConsole
*qemu_console_lookup_unused(void)
2355 QTAILQ_FOREACH(con
, &consoles
, next
) {
2356 if (con
->hw_ops
!= &unused_ops
) {
2359 obj
= object_property_get_link(OBJECT(con
),
2360 "device", &error_abort
);
2369 QEMUCursor
*qemu_console_get_cursor(QemuConsole
*con
)
2372 con
= active_console
;
2374 return con
? con
->cursor
: NULL
;
2377 bool qemu_console_is_visible(QemuConsole
*con
)
2379 return (con
== active_console
) || (con
->dcls
> 0);
2382 bool qemu_console_is_graphic(QemuConsole
*con
)
2385 con
= active_console
;
2387 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2390 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2393 con
= active_console
;
2395 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2398 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2400 assert(con
!= NULL
);
2401 return con
->gl_block
;
2404 bool qemu_console_is_multihead(DeviceState
*dev
)
2408 uint32_t f
= 0xffffffff;
2411 QTAILQ_FOREACH(con
, &consoles
, next
) {
2412 obj
= object_property_get_link(OBJECT(con
),
2413 "device", &error_abort
);
2414 if (DEVICE(obj
) != dev
) {
2418 h
= object_property_get_uint(OBJECT(con
),
2419 "head", &error_abort
);
2420 if (f
== 0xffffffff) {
2422 } else if (h
!= f
) {
2429 char *qemu_console_get_label(QemuConsole
*con
)
2431 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2436 dev
= DEVICE(con
->device
);
2437 multihead
= qemu_console_is_multihead(dev
);
2439 return g_strdup_printf("%s.%d", dev
->id
?
2441 object_get_typename(con
->device
),
2444 return g_strdup_printf("%s", dev
->id
?
2446 object_get_typename(con
->device
));
2449 return g_strdup("VGA");
2451 if (con
->chr
&& con
->chr
->label
) {
2452 return g_strdup(con
->chr
->label
);
2454 return g_strdup_printf("vc%d", con
->index
);
2458 int qemu_console_get_index(QemuConsole
*con
)
2461 con
= active_console
;
2463 return con
? con
->index
: -1;
2466 uint32_t qemu_console_get_head(QemuConsole
*con
)
2469 con
= active_console
;
2471 return con
? con
->head
: -1;
2474 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2477 con
= active_console
;
2482 switch (con
->scanout
.kind
) {
2483 case SCANOUT_DMABUF
:
2484 return con
->scanout
.dmabuf
->width
;
2485 case SCANOUT_TEXTURE
:
2486 return con
->scanout
.texture
.width
;
2487 case SCANOUT_SURFACE
:
2488 return surface_width(con
->surface
);
2494 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2497 con
= active_console
;
2502 switch (con
->scanout
.kind
) {
2503 case SCANOUT_DMABUF
:
2504 return con
->scanout
.dmabuf
->height
;
2505 case SCANOUT_TEXTURE
:
2506 return con
->scanout
.texture
.height
;
2507 case SCANOUT_SURFACE
:
2508 return surface_height(con
->surface
);
2514 static void vc_chr_accept_input(Chardev
*chr
)
2516 VCChardev
*drv
= VC_CHARDEV(chr
);
2517 QemuConsole
*s
= drv
->console
;
2522 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2524 VCChardev
*drv
= VC_CHARDEV(chr
);
2525 QemuConsole
*s
= drv
->console
;
2530 static void text_console_update_cursor_timer(void)
2532 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2533 + CONSOLE_CURSOR_PERIOD
/ 2);
2536 static void text_console_update_cursor(void *opaque
)
2541 cursor_visible_phase
= !cursor_visible_phase
;
2543 QTAILQ_FOREACH(s
, &consoles
, next
) {
2544 if (qemu_console_is_graphic(s
) ||
2545 !qemu_console_is_visible(s
)) {
2549 graphic_hw_invalidate(s
);
2553 text_console_update_cursor_timer();
2557 static const GraphicHwOps text_console_ops
= {
2558 .invalidate
= text_console_invalidate
,
2559 .text_update
= text_console_update
,
2562 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2564 VCChardev
*drv
= VC_CHARDEV(chr
);
2565 QemuConsole
*s
= drv
->console
;
2566 int g_width
= 80 * FONT_WIDTH
;
2567 int g_height
= 24 * FONT_HEIGHT
;
2569 fifo8_create(&s
->out_fifo
, 16);
2574 s
->total_height
= DEFAULT_BACKSCROLL
;
2577 if (s
->scanout
.kind
!= SCANOUT_SURFACE
) {
2578 if (active_console
&& active_console
->scanout
.kind
== SCANOUT_SURFACE
) {
2579 g_width
= qemu_console_get_width(active_console
, g_width
);
2580 g_height
= qemu_console_get_height(active_console
, g_height
);
2582 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2583 s
->scanout
.kind
= SCANOUT_SURFACE
;
2586 s
->hw_ops
= &text_console_ops
;
2589 /* Set text attribute defaults */
2590 s
->t_attrib_default
.bold
= 0;
2591 s
->t_attrib_default
.uline
= 0;
2592 s
->t_attrib_default
.blink
= 0;
2593 s
->t_attrib_default
.invers
= 0;
2594 s
->t_attrib_default
.unvisible
= 0;
2595 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2596 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2597 /* set current text attributes to default */
2598 s
->t_attrib
= s
->t_attrib_default
;
2599 text_console_resize(s
);
2604 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2605 msg
= g_strdup_printf("%s console\r\n", chr
->label
);
2606 vc_chr_write(chr
, (uint8_t *)msg
, strlen(msg
));
2608 s
->t_attrib
= s
->t_attrib_default
;
2611 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2614 static void vc_chr_open(Chardev
*chr
,
2615 ChardevBackend
*backend
,
2619 ChardevVC
*vc
= backend
->u
.vc
.data
;
2620 VCChardev
*drv
= VC_CHARDEV(chr
);
2623 unsigned height
= 0;
2625 if (vc
->has_width
) {
2627 } else if (vc
->has_cols
) {
2628 width
= vc
->cols
* FONT_WIDTH
;
2631 if (vc
->has_height
) {
2632 height
= vc
->height
;
2633 } else if (vc
->has_rows
) {
2634 height
= vc
->rows
* FONT_HEIGHT
;
2637 trace_console_txt_new(width
, height
);
2638 if (width
== 0 || height
== 0) {
2639 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2641 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2642 s
->scanout
.kind
= SCANOUT_SURFACE
;
2643 s
->surface
= qemu_create_displaysurface(width
, height
);
2647 error_setg(errp
, "cannot create text console");
2654 if (display_state
) {
2655 text_console_do_init(chr
, display_state
);
2658 /* console/chardev init sometimes completes elsewhere in a 2nd
2659 * stage, so defer OPENED events until they are fully initialized
2664 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2666 DisplaySurface
*surface
= qemu_console_surface(s
);
2668 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2670 if ((s
->scanout
.kind
!= SCANOUT_SURFACE
||
2671 (surface
&& surface
->flags
& QEMU_ALLOCATED_FLAG
)) &&
2672 qemu_console_get_width(s
, -1) == width
&&
2673 qemu_console_get_height(s
, -1) == height
) {
2677 surface
= qemu_create_displaysurface(width
, height
);
2678 dpy_gfx_replace_surface(s
, surface
);
2681 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2683 switch (console
->scanout
.kind
) {
2684 case SCANOUT_SURFACE
:
2685 return console
->surface
;
2691 PixelFormat
qemu_default_pixelformat(int bpp
)
2693 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2694 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2698 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2700 void qemu_display_register(QemuDisplay
*ui
)
2702 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2703 dpys
[ui
->type
] = ui
;
2706 bool qemu_display_find_default(DisplayOptions
*opts
)
2708 static DisplayType prio
[] = {
2709 #if defined(CONFIG_GTK)
2712 #if defined(CONFIG_SDL)
2715 #if defined(CONFIG_COCOA)
2721 for (i
= 0; i
< (int)ARRAY_SIZE(prio
); i
++) {
2722 if (dpys
[prio
[i
]] == NULL
) {
2723 Error
*local_err
= NULL
;
2724 int rv
= ui_module_load(DisplayType_str(prio
[i
]), &local_err
);
2726 error_report_err(local_err
);
2729 if (dpys
[prio
[i
]] == NULL
) {
2732 opts
->type
= prio
[i
];
2738 void qemu_display_early_init(DisplayOptions
*opts
)
2740 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2741 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2744 if (dpys
[opts
->type
] == NULL
) {
2745 Error
*local_err
= NULL
;
2746 int rv
= ui_module_load(DisplayType_str(opts
->type
), &local_err
);
2748 error_report_err(local_err
);
2751 if (dpys
[opts
->type
] == NULL
) {
2752 error_report("Display '%s' is not available.",
2753 DisplayType_str(opts
->type
));
2756 if (dpys
[opts
->type
]->early_init
) {
2757 dpys
[opts
->type
]->early_init(opts
);
2761 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2763 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2764 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2767 assert(dpys
[opts
->type
] != NULL
);
2768 dpys
[opts
->type
]->init(ds
, opts
);
2771 void qemu_display_help(void)
2775 printf("Available display backend types:\n");
2777 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2779 Error
*local_err
= NULL
;
2780 int rv
= ui_module_load(DisplayType_str(idx
), &local_err
);
2782 error_report_err(local_err
);
2786 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2791 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2796 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2797 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2798 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2800 val
= qemu_opt_get_number(opts
, "width", 0);
2802 vc
->has_width
= true;
2806 val
= qemu_opt_get_number(opts
, "height", 0);
2808 vc
->has_height
= true;
2812 val
= qemu_opt_get_number(opts
, "cols", 0);
2814 vc
->has_cols
= true;
2818 val
= qemu_opt_get_number(opts
, "rows", 0);
2820 vc
->has_rows
= true;
2825 static const TypeInfo qemu_console_info
= {
2826 .name
= TYPE_QEMU_CONSOLE
,
2827 .parent
= TYPE_OBJECT
,
2828 .instance_size
= sizeof(QemuConsole
),
2829 .class_size
= sizeof(QemuConsoleClass
),
2832 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2834 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2836 cc
->parse
= qemu_chr_parse_vc
;
2837 cc
->open
= vc_chr_open
;
2838 cc
->chr_write
= vc_chr_write
;
2839 cc
->chr_accept_input
= vc_chr_accept_input
;
2840 cc
->chr_set_echo
= vc_chr_set_echo
;
2843 static const TypeInfo char_vc_type_info
= {
2844 .name
= TYPE_CHARDEV_VC
,
2845 .parent
= TYPE_CHARDEV
,
2846 .instance_size
= sizeof(VCChardev
),
2847 .class_init
= char_vc_class_init
,
2850 void qemu_console_early_init(void)
2852 /* set the default vc driver */
2853 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2854 type_register(&char_vc_type_info
);
2858 static void register_types(void)
2860 type_register_static(&qemu_console_info
);
2863 type_init(register_types
);