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/fifo8.h"
31 #include "qemu/main-loop.h"
32 #include "qemu/module.h"
33 #include "qemu/option.h"
34 #include "qemu/timer.h"
35 #include "chardev/char.h"
37 #include "exec/memory.h"
38 #include "io/channel-file.h"
39 #include "qom/object.h"
44 #define DEFAULT_BACKSCROLL 512
45 #define CONSOLE_CURSOR_PERIOD 500
47 typedef struct TextAttributes
{
57 typedef struct TextCell
{
59 TextAttributes t_attrib
;
62 #define MAX_ESC_PARAMS 3
73 TEXT_CONSOLE_FIXED_SIZE
80 console_type_t console_type
;
82 DisplaySurface
*surface
;
83 DisplayScanout scanout
;
87 QEMUTimer
*gl_unblock_timer
;
90 /* Graphic console state. */
95 const GraphicHwOps
*hw_ops
;
98 /* Text console state */
102 int backscroll_height
;
104 int x_saved
, y_saved
;
107 TextAttributes t_attrib_default
; /* default text attributes */
108 TextAttributes t_attrib
; /* currently active text attributes */
110 int text_x
[2], text_y
[2], cursor_invalidate
;
119 int esc_params
[MAX_ESC_PARAMS
];
123 /* fifo for key pressed */
127 QTAILQ_ENTRY(QemuConsole
) next
;
130 struct DisplayState
{
131 QEMUTimer
*gui_timer
;
132 uint64_t last_update
;
133 uint64_t update_interval
;
138 QLIST_HEAD(, DisplayChangeListener
) listeners
;
141 static DisplayState
*display_state
;
142 static QemuConsole
*active_console
;
143 static QTAILQ_HEAD(, QemuConsole
) consoles
=
144 QTAILQ_HEAD_INITIALIZER(consoles
);
145 static bool cursor_visible_phase
;
146 static QEMUTimer
*cursor_timer
;
148 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
149 static void dpy_refresh(DisplayState
*s
);
150 static DisplayState
*get_alloc_displaystate(void);
151 static void text_console_update_cursor_timer(void);
152 static void text_console_update_cursor(void *opaque
);
153 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
);
154 static bool console_compatible_with(QemuConsole
*con
,
155 DisplayChangeListener
*dcl
, Error
**errp
);
157 static void gui_update(void *opaque
)
159 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
160 uint64_t dcl_interval
;
161 DisplayState
*ds
= opaque
;
162 DisplayChangeListener
*dcl
;
165 ds
->refreshing
= true;
167 ds
->refreshing
= false;
169 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
170 dcl_interval
= dcl
->update_interval
?
171 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
172 if (interval
> dcl_interval
) {
173 interval
= dcl_interval
;
176 if (ds
->update_interval
!= interval
) {
177 ds
->update_interval
= interval
;
178 QTAILQ_FOREACH(con
, &consoles
, next
) {
179 if (con
->hw_ops
->update_interval
) {
180 con
->hw_ops
->update_interval(con
->hw
, interval
);
183 trace_console_refresh(interval
);
185 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
186 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
189 static void gui_setup_refresh(DisplayState
*ds
)
191 DisplayChangeListener
*dcl
;
192 bool need_timer
= false;
193 bool have_gfx
= false;
194 bool have_text
= false;
196 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
197 if (dcl
->ops
->dpy_refresh
!= NULL
) {
200 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
203 if (dcl
->ops
->dpy_text_update
!= NULL
) {
208 if (need_timer
&& ds
->gui_timer
== NULL
) {
209 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
210 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
212 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
213 timer_free(ds
->gui_timer
);
214 ds
->gui_timer
= NULL
;
217 ds
->have_gfx
= have_gfx
;
218 ds
->have_text
= have_text
;
221 void graphic_hw_update_done(QemuConsole
*con
)
224 qemu_co_queue_restart_all(&con
->dump_queue
);
228 void graphic_hw_update(QemuConsole
*con
)
231 con
= con
? con
: active_console
;
235 if (con
->hw_ops
->gfx_update
) {
236 con
->hw_ops
->gfx_update(con
->hw
);
237 async
= con
->hw_ops
->gfx_update_async
;
240 graphic_hw_update_done(con
);
244 static void graphic_hw_gl_unblock_timer(void *opaque
)
246 warn_report("console: no gl-unblock within one second");
249 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
259 assert(con
->gl_block
>= 0);
260 if (!con
->hw_ops
->gl_block
) {
263 if ((block
&& con
->gl_block
!= 1) || (!block
&& con
->gl_block
!= 0)) {
266 con
->hw_ops
->gl_block(con
->hw
, block
);
269 timeout
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
270 timeout
+= 1000; /* one sec */
271 timer_mod(con
->gl_unblock_timer
, timeout
);
273 timer_del(con
->gl_unblock_timer
);
277 int qemu_console_get_window_id(QemuConsole
*con
)
279 return con
->window_id
;
282 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
284 con
->window_id
= window_id
;
287 void graphic_hw_invalidate(QemuConsole
*con
)
290 con
= active_console
;
292 if (con
&& con
->hw_ops
->invalidate
) {
293 con
->hw_ops
->invalidate(con
->hw
);
299 * png_save: Take a screenshot as PNG
301 * Saves screendump as a PNG file
303 * Returns true for success or false for error.
305 * @fd: File descriptor for PNG file.
306 * @image: Image data in pixman format.
307 * @errp: Pointer to an error.
309 static bool png_save(int fd
, pixman_image_t
*image
, Error
**errp
)
311 int width
= pixman_image_get_width(image
);
312 int height
= pixman_image_get_height(image
);
313 g_autofree png_struct
*png_ptr
= NULL
;
314 g_autofree png_info
*info_ptr
= NULL
;
315 g_autoptr(pixman_image_t
) linebuf
=
316 qemu_pixman_linebuf_create(PIXMAN_a8r8g8b8
, width
);
317 uint8_t *buf
= (uint8_t *)pixman_image_get_data(linebuf
);
318 FILE *f
= fdopen(fd
, "wb");
321 error_setg_errno(errp
, errno
,
322 "Failed to create file from file descriptor");
326 png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
,
329 error_setg(errp
, "PNG creation failed. Unable to write struct");
334 info_ptr
= png_create_info_struct(png_ptr
);
337 error_setg(errp
, "PNG creation failed. Unable to write info");
339 png_destroy_write_struct(&png_ptr
, &info_ptr
);
343 png_init_io(png_ptr
, f
);
345 png_set_IHDR(png_ptr
, info_ptr
, width
, height
, 8,
346 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
347 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
349 png_write_info(png_ptr
, info_ptr
);
351 for (y
= 0; y
< height
; ++y
) {
352 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
353 png_write_row(png_ptr
, buf
);
355 qemu_pixman_image_unref(linebuf
);
357 png_write_end(png_ptr
, NULL
);
359 png_destroy_write_struct(&png_ptr
, &info_ptr
);
361 if (fclose(f
) != 0) {
362 error_setg_errno(errp
, errno
,
363 "PNG creation failed. Unable to close file");
370 #else /* no png support */
372 static bool png_save(int fd
, pixman_image_t
*image
, Error
**errp
)
374 error_setg(errp
, "Enable PNG support with libpng for screendump");
378 #endif /* CONFIG_PNG */
380 static bool ppm_save(int fd
, pixman_image_t
*image
, Error
**errp
)
382 int width
= pixman_image_get_width(image
);
383 int height
= pixman_image_get_height(image
);
384 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
385 g_autofree
char *header
= NULL
;
386 g_autoptr(pixman_image_t
) linebuf
= NULL
;
389 trace_ppm_save(fd
, image
);
391 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
392 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
393 header
, strlen(header
), errp
) < 0) {
397 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
398 for (y
= 0; y
< height
; y
++) {
399 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
400 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
401 (char *)pixman_image_get_data(linebuf
),
402 pixman_image_get_stride(linebuf
), errp
) < 0) {
410 static void graphic_hw_update_bh(void *con
)
412 graphic_hw_update(con
);
415 /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
417 qmp_screendump(const char *filename
, bool has_device
, const char *device
,
418 bool has_head
, int64_t head
,
419 bool has_format
, ImageFormat format
, Error
**errp
)
421 g_autoptr(pixman_image_t
) image
= NULL
;
423 DisplaySurface
*surface
;
427 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
434 error_setg(errp
, "'head' must be specified together with 'device'");
437 con
= qemu_console_lookup_by_index(0);
439 error_setg(errp
, "There is no console to take a screendump from");
444 if (qemu_co_queue_empty(&con
->dump_queue
)) {
445 /* Defer the update, it will restart the pending coroutines */
446 aio_bh_schedule_oneshot(qemu_get_aio_context(),
447 graphic_hw_update_bh
, con
);
449 qemu_co_queue_wait(&con
->dump_queue
, NULL
);
452 * All pending coroutines are woken up, while the BQL is held. No
453 * further graphic update are possible until it is released. Take
454 * an image ref before that.
456 surface
= qemu_console_surface(con
);
458 error_setg(errp
, "no surface");
461 image
= pixman_image_ref(surface
->image
);
463 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
465 error_setg(errp
, "failed to open file '%s': %s", filename
,
471 * The image content could potentially be updated as the coroutine
472 * yields and releases the BQL. It could produce corrupted dump, but
473 * it should be otherwise safe.
475 if (has_format
&& format
== IMAGE_FORMAT_PNG
) {
476 /* PNG format specified for screendump */
477 if (!png_save(fd
, image
, errp
)) {
478 qemu_unlink(filename
);
481 /* PPM format specified/default for screendump */
482 if (!ppm_save(fd
, image
, errp
)) {
483 qemu_unlink(filename
);
488 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
491 con
= active_console
;
493 if (con
&& con
->hw_ops
->text_update
) {
494 con
->hw_ops
->text_update(con
->hw
, chardata
);
498 static void vga_fill_rect(QemuConsole
*con
,
499 int posx
, int posy
, int width
, int height
,
500 pixman_color_t color
)
502 DisplaySurface
*surface
= qemu_console_surface(con
);
503 pixman_rectangle16_t rect
= {
504 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
507 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
511 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
512 static void vga_bitblt(QemuConsole
*con
,
513 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
515 DisplaySurface
*surface
= qemu_console_surface(con
);
517 pixman_image_composite(PIXMAN_OP_SRC
,
518 surface
->image
, NULL
, surface
->image
,
519 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
522 /***********************************************************/
523 /* basic char display */
525 #define FONT_HEIGHT 16
530 #define QEMU_RGB(r, g, b) \
531 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
533 static const pixman_color_t color_table_rgb
[2][8] = {
535 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
536 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
537 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
538 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
539 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
540 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
541 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
542 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
545 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
546 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
547 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
548 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
549 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
550 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
551 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
552 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
556 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
557 TextAttributes
*t_attrib
)
559 static pixman_image_t
*glyphs
[256];
560 DisplaySurface
*surface
= qemu_console_surface(s
);
561 pixman_color_t fgcol
, bgcol
;
563 if (t_attrib
->invers
) {
564 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
565 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
567 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
568 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
572 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
574 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
575 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
578 static void text_console_resize(QemuConsole
*s
)
580 TextCell
*cells
, *c
, *c1
;
581 int w1
, x
, y
, last_width
;
583 assert(s
->scanout
.kind
== SCANOUT_SURFACE
);
585 last_width
= s
->width
;
586 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
587 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
593 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
594 for(y
= 0; y
< s
->total_height
; y
++) {
595 c
= &cells
[y
* s
->width
];
597 c1
= &s
->cells
[y
* last_width
];
598 for(x
= 0; x
< w1
; x
++) {
602 for(x
= w1
; x
< s
->width
; x
++) {
604 c
->t_attrib
= s
->t_attrib_default
;
612 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
614 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
615 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
616 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
617 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
620 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
622 if (!qemu_console_is_visible(s
)) {
625 if (s
->update_x0
> x
* FONT_WIDTH
)
626 s
->update_x0
= x
* FONT_WIDTH
;
627 if (s
->update_y0
> y
* FONT_HEIGHT
)
628 s
->update_y0
= y
* FONT_HEIGHT
;
629 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
630 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
631 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
632 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
635 static void update_xy(QemuConsole
*s
, int x
, int y
)
640 if (s
->ds
->have_text
) {
641 text_update_xy(s
, x
, y
);
644 y1
= (s
->y_base
+ y
) % s
->total_height
;
645 y2
= y1
- s
->y_displayed
;
647 y2
+= s
->total_height
;
649 if (y2
< s
->height
) {
653 c
= &s
->cells
[y1
* s
->width
+ x
];
654 vga_putcharxy(s
, x
, y2
, c
->ch
,
656 invalidate_xy(s
, x
, y2
);
660 static void console_show_cursor(QemuConsole
*s
, int show
)
666 if (s
->ds
->have_text
) {
667 s
->cursor_invalidate
= 1;
673 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
674 y
= y1
- s
->y_displayed
;
676 y
+= s
->total_height
;
679 c
= &s
->cells
[y1
* s
->width
+ x
];
680 if (show
&& cursor_visible_phase
) {
681 TextAttributes t_attrib
= s
->t_attrib_default
;
682 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
683 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
685 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
687 invalidate_xy(s
, x
, y
);
691 static void console_refresh(QemuConsole
*s
)
693 DisplaySurface
*surface
= qemu_console_surface(s
);
697 if (s
->ds
->have_text
) {
700 s
->text_x
[1] = s
->width
- 1;
701 s
->text_y
[1] = s
->height
- 1;
702 s
->cursor_invalidate
= 1;
705 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
706 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
708 for (y
= 0; y
< s
->height
; y
++) {
709 c
= s
->cells
+ y1
* s
->width
;
710 for (x
= 0; x
< s
->width
; x
++) {
711 vga_putcharxy(s
, x
, y
, c
->ch
,
715 if (++y1
== s
->total_height
) {
719 console_show_cursor(s
, 1);
720 dpy_gfx_update(s
, 0, 0,
721 surface_width(surface
), surface_height(surface
));
724 static void console_scroll(QemuConsole
*s
, int ydelta
)
729 for(i
= 0; i
< ydelta
; i
++) {
730 if (s
->y_displayed
== s
->y_base
)
732 if (++s
->y_displayed
== s
->total_height
)
737 i
= s
->backscroll_height
;
738 if (i
> s
->total_height
- s
->height
)
739 i
= s
->total_height
- s
->height
;
742 y1
+= s
->total_height
;
743 for(i
= 0; i
< ydelta
; i
++) {
744 if (s
->y_displayed
== y1
)
746 if (--s
->y_displayed
< 0)
747 s
->y_displayed
= s
->total_height
- 1;
753 static void console_put_lf(QemuConsole
*s
)
759 if (s
->y
>= s
->height
) {
760 s
->y
= s
->height
- 1;
762 if (s
->y_displayed
== s
->y_base
) {
763 if (++s
->y_displayed
== s
->total_height
)
766 if (++s
->y_base
== s
->total_height
)
768 if (s
->backscroll_height
< s
->total_height
)
769 s
->backscroll_height
++;
770 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
771 c
= &s
->cells
[y1
* s
->width
];
772 for(x
= 0; x
< s
->width
; x
++) {
774 c
->t_attrib
= s
->t_attrib_default
;
777 if (s
->y_displayed
== s
->y_base
) {
778 if (s
->ds
->have_text
) {
781 s
->text_x
[1] = s
->width
- 1;
782 s
->text_y
[1] = s
->height
- 1;
785 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
786 s
->width
* FONT_WIDTH
,
787 (s
->height
- 1) * FONT_HEIGHT
);
788 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
789 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
790 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
793 s
->update_x1
= s
->width
* FONT_WIDTH
;
794 s
->update_y1
= s
->height
* FONT_HEIGHT
;
799 /* Set console attributes depending on the current escape codes.
800 * NOTE: I know this code is not very efficient (checking every color for it
801 * self) but it is more readable and better maintainable.
803 static void console_handle_escape(QemuConsole
*s
)
807 for (i
=0; i
<s
->nb_esc_params
; i
++) {
808 switch (s
->esc_params
[i
]) {
809 case 0: /* reset all console attributes to default */
810 s
->t_attrib
= s
->t_attrib_default
;
813 s
->t_attrib
.bold
= 1;
816 s
->t_attrib
.uline
= 1;
819 s
->t_attrib
.blink
= 1;
822 s
->t_attrib
.invers
= 1;
825 s
->t_attrib
.unvisible
= 1;
828 s
->t_attrib
.bold
= 0;
831 s
->t_attrib
.uline
= 0;
834 s
->t_attrib
.blink
= 0;
837 s
->t_attrib
.invers
= 0;
840 s
->t_attrib
.unvisible
= 0;
842 /* set foreground color */
844 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
847 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
850 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
853 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
856 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
859 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
862 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
865 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
867 /* set background color */
869 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
872 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
875 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
878 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
881 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
884 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
887 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
890 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
896 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
898 int y1
= (s
->y_base
+ y
) % s
->total_height
;
902 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
904 c
->t_attrib
= s
->t_attrib_default
;
908 static void console_put_one(QemuConsole
*s
, int ch
)
912 if (s
->x
>= s
->width
) {
917 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
918 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
920 c
->t_attrib
= s
->t_attrib
;
921 update_xy(s
, s
->x
, s
->y
);
925 static void console_respond_str(QemuConsole
*s
, const char *buf
)
928 console_put_one(s
, *buf
);
933 /* set cursor, checking bounds */
934 static void set_cursor(QemuConsole
*s
, int x
, int y
)
942 if (y
>= s
->height
) {
953 static void console_putchar(QemuConsole
*s
, int ch
)
962 case '\r': /* carriage return */
965 case '\n': /* newline */
968 case '\b': /* backspace */
972 case '\t': /* tabspace */
973 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
977 s
->x
= s
->x
+ (8 - (s
->x
% 8));
980 case '\a': /* alert aka. bell */
981 /* TODO: has to be implemented */
984 /* SI (shift in), character set 0 (ignored) */
987 /* SO (shift out), character set 1 (ignored) */
989 case 27: /* esc (introducing an escape sequence) */
990 s
->state
= TTY_STATE_ESC
;
993 console_put_one(s
, ch
);
997 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
999 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
1000 s
->esc_params
[i
] = 0;
1001 s
->nb_esc_params
= 0;
1002 s
->state
= TTY_STATE_CSI
;
1004 s
->state
= TTY_STATE_NORM
;
1007 case TTY_STATE_CSI
: /* handle escape sequence parameters */
1008 if (ch
>= '0' && ch
<= '9') {
1009 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
1010 int *param
= &s
->esc_params
[s
->nb_esc_params
];
1011 int digit
= (ch
- '0');
1013 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
1014 *param
* 10 + digit
: INT_MAX
;
1017 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
1019 if (ch
== ';' || ch
== '?') {
1022 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
1023 ch
, s
->nb_esc_params
);
1024 s
->state
= TTY_STATE_NORM
;
1027 /* move cursor up */
1028 if (s
->esc_params
[0] == 0) {
1029 s
->esc_params
[0] = 1;
1031 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
1034 /* move cursor down */
1035 if (s
->esc_params
[0] == 0) {
1036 s
->esc_params
[0] = 1;
1038 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
1041 /* move cursor right */
1042 if (s
->esc_params
[0] == 0) {
1043 s
->esc_params
[0] = 1;
1045 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
1048 /* move cursor left */
1049 if (s
->esc_params
[0] == 0) {
1050 s
->esc_params
[0] = 1;
1052 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
1055 /* move cursor to column */
1056 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
1060 /* move cursor to row, column */
1061 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
1064 switch (s
->esc_params
[0]) {
1066 /* clear to end of screen */
1067 for (y
= s
->y
; y
< s
->height
; y
++) {
1068 for (x
= 0; x
< s
->width
; x
++) {
1069 if (y
== s
->y
&& x
< s
->x
) {
1072 console_clear_xy(s
, x
, y
);
1077 /* clear from beginning of screen */
1078 for (y
= 0; y
<= s
->y
; y
++) {
1079 for (x
= 0; x
< s
->width
; x
++) {
1080 if (y
== s
->y
&& x
> s
->x
) {
1083 console_clear_xy(s
, x
, y
);
1088 /* clear entire screen */
1089 for (y
= 0; y
<= s
->height
; y
++) {
1090 for (x
= 0; x
< s
->width
; x
++) {
1091 console_clear_xy(s
, x
, y
);
1098 switch (s
->esc_params
[0]) {
1101 for(x
= s
->x
; x
< s
->width
; x
++) {
1102 console_clear_xy(s
, x
, s
->y
);
1106 /* clear from beginning of line */
1107 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1108 console_clear_xy(s
, x
, s
->y
);
1112 /* clear entire line */
1113 for(x
= 0; x
< s
->width
; x
++) {
1114 console_clear_xy(s
, x
, s
->y
);
1120 console_handle_escape(s
);
1123 switch (s
->esc_params
[0]) {
1125 /* report console status (always succeed)*/
1126 console_respond_str(s
, "\033[0n");
1129 /* report cursor position */
1130 sprintf(response
, "\033[%d;%dR",
1131 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1133 console_respond_str(s
, response
);
1138 /* save cursor position */
1143 /* restore cursor position */
1148 trace_console_putchar_unhandled(ch
);
1156 static void displaychangelistener_gfx_switch(DisplayChangeListener
*dcl
,
1157 struct DisplaySurface
*new_surface
,
1160 if (dcl
->ops
->dpy_gfx_switch
) {
1161 dcl
->ops
->dpy_gfx_switch(dcl
, new_surface
);
1164 if (update
&& dcl
->ops
->dpy_gfx_update
) {
1165 dcl
->ops
->dpy_gfx_update(dcl
, 0, 0,
1166 surface_width(new_surface
),
1167 surface_height(new_surface
));
1171 static void dpy_gfx_create_texture(QemuConsole
*con
, DisplaySurface
*surface
)
1173 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_create_texture
) {
1174 con
->gl
->ops
->dpy_gl_ctx_create_texture(con
->gl
, surface
);
1178 static void dpy_gfx_destroy_texture(QemuConsole
*con
, DisplaySurface
*surface
)
1180 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_destroy_texture
) {
1181 con
->gl
->ops
->dpy_gl_ctx_destroy_texture(con
->gl
, surface
);
1185 static void dpy_gfx_update_texture(QemuConsole
*con
, DisplaySurface
*surface
,
1186 int x
, int y
, int w
, int h
)
1188 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_update_texture
) {
1189 con
->gl
->ops
->dpy_gl_ctx_update_texture(con
->gl
, surface
, x
, y
, w
, h
);
1193 static void displaychangelistener_display_console(DisplayChangeListener
*dcl
,
1197 static const char nodev
[] =
1198 "This VM has no graphic display device.";
1199 static DisplaySurface
*dummy
;
1201 if (!con
|| !console_compatible_with(con
, dcl
, errp
)) {
1203 dummy
= qemu_create_placeholder_surface(640, 480, nodev
);
1206 dpy_gfx_create_texture(con
, dummy
);
1208 displaychangelistener_gfx_switch(dcl
, dummy
, TRUE
);
1212 dpy_gfx_create_texture(con
, con
->surface
);
1213 displaychangelistener_gfx_switch(dcl
, con
->surface
,
1214 con
->scanout
.kind
== SCANOUT_SURFACE
);
1216 if (con
->scanout
.kind
== SCANOUT_DMABUF
&&
1217 displaychangelistener_has_dmabuf(dcl
)) {
1218 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, con
->scanout
.dmabuf
);
1219 } else if (con
->scanout
.kind
== SCANOUT_TEXTURE
&&
1220 dcl
->ops
->dpy_gl_scanout_texture
) {
1221 dcl
->ops
->dpy_gl_scanout_texture(dcl
,
1222 con
->scanout
.texture
.backing_id
,
1223 con
->scanout
.texture
.backing_y_0_top
,
1224 con
->scanout
.texture
.backing_width
,
1225 con
->scanout
.texture
.backing_height
,
1226 con
->scanout
.texture
.x
,
1227 con
->scanout
.texture
.y
,
1228 con
->scanout
.texture
.width
,
1229 con
->scanout
.texture
.height
);
1233 void console_select(unsigned int index
)
1235 DisplayChangeListener
*dcl
;
1238 trace_console_select(index
);
1239 s
= qemu_console_lookup_by_index(index
);
1241 DisplayState
*ds
= s
->ds
;
1245 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1246 if (dcl
->con
!= NULL
) {
1249 displaychangelistener_display_console(dcl
, s
, NULL
);
1252 if (ds
->have_text
) {
1253 dpy_text_resize(s
, s
->width
, s
->height
);
1255 text_console_update_cursor(NULL
);
1261 QemuConsole
*console
;
1263 typedef struct VCChardev VCChardev
;
1265 #define TYPE_CHARDEV_VC "chardev-vc"
1266 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
1269 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1271 VCChardev
*drv
= VC_CHARDEV(chr
);
1272 QemuConsole
*s
= drv
->console
;
1279 s
->update_x0
= s
->width
* FONT_WIDTH
;
1280 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1283 console_show_cursor(s
, 0);
1284 for(i
= 0; i
< len
; i
++) {
1285 console_putchar(s
, buf
[i
]);
1287 console_show_cursor(s
, 1);
1288 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1289 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1290 s
->update_x1
- s
->update_x0
,
1291 s
->update_y1
- s
->update_y0
);
1296 static void kbd_send_chars(QemuConsole
*s
)
1298 uint32_t len
, avail
;
1300 len
= qemu_chr_be_can_write(s
->chr
);
1301 avail
= fifo8_num_used(&s
->out_fifo
);
1302 while (len
> 0 && avail
> 0) {
1306 buf
= fifo8_pop_buf(&s
->out_fifo
, MIN(len
, avail
), &size
);
1307 qemu_chr_be_write(s
->chr
, (uint8_t *)buf
, size
);
1308 len
= qemu_chr_be_can_write(s
->chr
);
1313 /* called when an ascii key is pressed */
1314 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1316 uint8_t buf
[16], *q
;
1320 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1324 case QEMU_KEY_CTRL_UP
:
1325 console_scroll(s
, -1);
1327 case QEMU_KEY_CTRL_DOWN
:
1328 console_scroll(s
, 1);
1330 case QEMU_KEY_CTRL_PAGEUP
:
1331 console_scroll(s
, -10);
1333 case QEMU_KEY_CTRL_PAGEDOWN
:
1334 console_scroll(s
, 10);
1337 /* convert the QEMU keysym to VT100 key string */
1339 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1342 c
= keysym
- 0xe100;
1344 *q
++ = '0' + (c
/ 10);
1345 *q
++ = '0' + (c
% 10);
1347 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1350 *q
++ = keysym
& 0xff;
1351 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1352 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1358 vc_chr_write(s
->chr
, buf
, q
- buf
);
1360 num_free
= fifo8_num_free(&s
->out_fifo
);
1361 fifo8_push_all(&s
->out_fifo
, buf
, MIN(num_free
, q
- buf
));
1367 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1368 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1369 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1370 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1371 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1372 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1373 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1374 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1375 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1376 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1377 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1380 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1381 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1382 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1383 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1384 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1385 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1386 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1387 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1388 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1391 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1395 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1399 kbd_put_keysym_console(s
, keysym
);
1403 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1407 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1408 kbd_put_keysym_console(s
, str
[i
]);
1412 void kbd_put_keysym(int keysym
)
1414 kbd_put_keysym_console(active_console
, keysym
);
1417 static void text_console_invalidate(void *opaque
)
1419 QemuConsole
*s
= (QemuConsole
*) opaque
;
1421 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1422 text_console_resize(s
);
1427 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1429 QemuConsole
*s
= (QemuConsole
*) opaque
;
1432 if (s
->text_x
[0] <= s
->text_x
[1]) {
1433 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1434 chardata
+= s
->text_y
[0] * s
->width
;
1435 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1436 for (j
= 0; j
< s
->width
; j
++, src
++) {
1437 console_write_ch(chardata
++,
1438 ATTR2CHTYPE(s
->cells
[src
].ch
,
1439 s
->cells
[src
].t_attrib
.fgcol
,
1440 s
->cells
[src
].t_attrib
.bgcol
,
1441 s
->cells
[src
].t_attrib
.bold
));
1443 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1444 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1445 s
->text_x
[0] = s
->width
;
1446 s
->text_y
[0] = s
->height
;
1450 if (s
->cursor_invalidate
) {
1451 dpy_text_cursor(s
, s
->x
, s
->y
);
1452 s
->cursor_invalidate
= 0;
1456 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1463 obj
= object_new(TYPE_QEMU_CONSOLE
);
1464 s
= QEMU_CONSOLE(obj
);
1465 qemu_co_queue_init(&s
->dump_queue
);
1467 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1468 (Object
**)&s
->device
,
1469 object_property_allow_set_link
,
1470 OBJ_PROP_LINK_STRONG
);
1471 object_property_add_uint32_ptr(obj
, "head", &s
->head
,
1472 OBJ_PROP_FLAG_READ
);
1474 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1475 (console_type
== GRAPHIC_CONSOLE
))) {
1479 s
->console_type
= console_type
;
1482 if (QTAILQ_EMPTY(&consoles
)) {
1484 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1485 } else if (console_type
!= GRAPHIC_CONSOLE
|| phase_check(PHASE_MACHINE_READY
)) {
1486 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1487 s
->index
= last
->index
+ 1;
1488 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1491 * HACK: Put graphical consoles before text consoles.
1493 * Only do that for coldplugged devices. After initial device
1494 * initialization we will not renumber the consoles any more.
1496 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1498 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1499 c
->console_type
== GRAPHIC_CONSOLE
) {
1500 c
= QTAILQ_NEXT(c
, next
);
1502 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1503 /* have no text consoles */
1504 s
->index
= c
->index
+ 1;
1505 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1507 s
->index
= c
->index
;
1508 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1509 /* renumber text consoles */
1510 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1518 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1520 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1522 trace_displaysurface_create(surface
, width
, height
);
1523 surface
->format
= PIXMAN_x8r8g8b8
;
1524 surface
->image
= pixman_image_create_bits(surface
->format
,
1527 assert(surface
->image
!= NULL
);
1528 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1533 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1534 pixman_format_code_t format
,
1535 int linesize
, uint8_t *data
)
1537 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1539 trace_displaysurface_create_from(surface
, width
, height
, format
);
1540 surface
->format
= format
;
1541 surface
->image
= pixman_image_create_bits(surface
->format
,
1543 (void *)data
, linesize
);
1544 assert(surface
->image
!= NULL
);
1549 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1551 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1553 trace_displaysurface_create_pixman(surface
);
1554 surface
->format
= pixman_image_get_format(image
);
1555 surface
->image
= pixman_image_ref(image
);
1560 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
1563 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1564 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1565 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1566 pixman_image_t
*glyph
;
1570 x
= (w
/ FONT_WIDTH
- len
) / 2;
1571 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1572 for (i
= 0; i
< len
; i
++) {
1573 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1574 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1575 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1576 qemu_pixman_image_unref(glyph
);
1578 surface
->flags
|= QEMU_PLACEHOLDER_FLAG
;
1582 void qemu_free_displaysurface(DisplaySurface
*surface
)
1584 if (surface
== NULL
) {
1587 trace_displaysurface_free(surface
);
1588 qemu_pixman_image_unref(surface
->image
);
1592 bool console_has_gl(QemuConsole
*con
)
1594 return con
->gl
!= NULL
;
1597 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
)
1599 if (dcl
->ops
->dpy_has_dmabuf
) {
1600 return dcl
->ops
->dpy_has_dmabuf(dcl
);
1603 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
1610 static bool console_compatible_with(QemuConsole
*con
,
1611 DisplayChangeListener
*dcl
, Error
**errp
)
1615 flags
= con
->hw_ops
->get_flags
? con
->hw_ops
->get_flags(con
->hw
) : 0;
1617 if (console_has_gl(con
) &&
1618 !con
->gl
->ops
->dpy_gl_ctx_is_compatible_dcl(con
->gl
, dcl
)) {
1619 error_setg(errp
, "Display %s is incompatible with the GL context",
1620 dcl
->ops
->dpy_name
);
1624 if (flags
& GRAPHIC_FLAGS_GL
&&
1625 !console_has_gl(con
)) {
1626 error_setg(errp
, "The console requires a GL context.");
1631 if (flags
& GRAPHIC_FLAGS_DMABUF
&&
1632 !displaychangelistener_has_dmabuf(dcl
)) {
1633 error_setg(errp
, "The console requires display DMABUF support.");
1640 void qemu_console_set_display_gl_ctx(QemuConsole
*con
, DisplayGLCtx
*gl
)
1642 /* display has opengl support */
1645 error_report("The console already has an OpenGL context.");
1651 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1657 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1658 dcl
->ds
= get_alloc_displaystate();
1659 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1660 gui_setup_refresh(dcl
->ds
);
1665 con
= active_console
;
1667 displaychangelistener_display_console(dcl
, con
, dcl
->con
? &error_fatal
: NULL
);
1668 text_console_update_cursor(NULL
);
1671 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1674 DisplayState
*ds
= dcl
->ds
;
1676 dcl
->update_interval
= interval
;
1677 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1678 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1682 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1684 DisplayState
*ds
= dcl
->ds
;
1685 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1689 QLIST_REMOVE(dcl
, next
);
1691 gui_setup_refresh(ds
);
1694 static void dpy_set_ui_info_timer(void *opaque
)
1696 QemuConsole
*con
= opaque
;
1698 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1701 bool dpy_ui_info_supported(QemuConsole
*con
)
1704 con
= active_console
;
1707 return con
->hw_ops
->ui_info
!= NULL
;
1710 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
1713 con
= active_console
;
1716 return &con
->ui_info
;
1719 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
, bool delay
)
1722 con
= active_console
;
1725 if (!dpy_ui_info_supported(con
)) {
1728 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1729 /* nothing changed -- ignore */
1734 * Typically we get a flood of these as the user resizes the window.
1735 * Wait until the dust has settled (one second without updates), then
1736 * go notify the guest.
1738 con
->ui_info
= *info
;
1739 timer_mod(con
->ui_timer
,
1740 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + (delay
? 1000 : 0));
1744 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1746 DisplayState
*s
= con
->ds
;
1747 DisplayChangeListener
*dcl
;
1748 int width
= qemu_console_get_width(con
, x
+ w
);
1749 int height
= qemu_console_get_height(con
, y
+ h
);
1755 w
= MIN(w
, width
- x
);
1756 h
= MIN(h
, height
- y
);
1758 if (!qemu_console_is_visible(con
)) {
1761 dpy_gfx_update_texture(con
, con
->surface
, x
, y
, w
, h
);
1762 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1763 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1766 if (dcl
->ops
->dpy_gfx_update
) {
1767 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1772 void dpy_gfx_update_full(QemuConsole
*con
)
1774 int w
= qemu_console_get_width(con
, 0);
1775 int h
= qemu_console_get_height(con
, 0);
1777 dpy_gfx_update(con
, 0, 0, w
, h
);
1780 void dpy_gfx_replace_surface(QemuConsole
*con
,
1781 DisplaySurface
*surface
)
1783 static const char placeholder_msg
[] = "Display output is not active.";
1784 DisplayState
*s
= con
->ds
;
1785 DisplaySurface
*old_surface
= con
->surface
;
1786 DisplayChangeListener
*dcl
;
1792 width
= surface_width(old_surface
);
1793 height
= surface_height(old_surface
);
1799 surface
= qemu_create_placeholder_surface(width
, height
, placeholder_msg
);
1802 assert(old_surface
!= surface
);
1804 con
->scanout
.kind
= SCANOUT_SURFACE
;
1805 con
->surface
= surface
;
1806 dpy_gfx_create_texture(con
, surface
);
1807 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1808 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1811 displaychangelistener_gfx_switch(dcl
, surface
, FALSE
);
1813 dpy_gfx_destroy_texture(con
, old_surface
);
1814 qemu_free_displaysurface(old_surface
);
1817 bool dpy_gfx_check_format(QemuConsole
*con
,
1818 pixman_format_code_t format
)
1820 DisplayChangeListener
*dcl
;
1821 DisplayState
*s
= con
->ds
;
1823 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1824 if (dcl
->con
&& dcl
->con
!= con
) {
1825 /* dcl bound to another console -> skip */
1828 if (dcl
->ops
->dpy_gfx_check_format
) {
1829 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1833 /* default is to allow native 32 bpp only */
1834 if (format
!= qemu_default_pixman_format(32, true)) {
1842 static void dpy_refresh(DisplayState
*s
)
1844 DisplayChangeListener
*dcl
;
1846 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1847 if (dcl
->ops
->dpy_refresh
) {
1848 dcl
->ops
->dpy_refresh(dcl
);
1853 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1855 DisplayState
*s
= con
->ds
;
1856 DisplayChangeListener
*dcl
;
1858 if (!qemu_console_is_visible(con
)) {
1861 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1862 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1865 if (dcl
->ops
->dpy_text_cursor
) {
1866 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1871 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1873 DisplayState
*s
= con
->ds
;
1874 DisplayChangeListener
*dcl
;
1876 if (!qemu_console_is_visible(con
)) {
1879 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1880 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1883 if (dcl
->ops
->dpy_text_update
) {
1884 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1889 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1891 DisplayState
*s
= con
->ds
;
1892 DisplayChangeListener
*dcl
;
1894 if (!qemu_console_is_visible(con
)) {
1897 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1898 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1901 if (dcl
->ops
->dpy_text_resize
) {
1902 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1907 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1909 DisplayState
*s
= con
->ds
;
1910 DisplayChangeListener
*dcl
;
1912 if (!qemu_console_is_visible(con
)) {
1915 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1916 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1919 if (dcl
->ops
->dpy_mouse_set
) {
1920 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1925 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1927 DisplayState
*s
= con
->ds
;
1928 DisplayChangeListener
*dcl
;
1930 if (!qemu_console_is_visible(con
)) {
1933 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1934 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1937 if (dcl
->ops
->dpy_cursor_define
) {
1938 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1943 bool dpy_cursor_define_supported(QemuConsole
*con
)
1945 DisplayState
*s
= con
->ds
;
1946 DisplayChangeListener
*dcl
;
1948 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1949 if (dcl
->ops
->dpy_cursor_define
) {
1956 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1957 struct QEMUGLParams
*qparams
)
1960 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1963 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1966 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1969 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1972 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1975 void dpy_gl_scanout_disable(QemuConsole
*con
)
1977 DisplayState
*s
= con
->ds
;
1978 DisplayChangeListener
*dcl
;
1980 if (con
->scanout
.kind
!= SCANOUT_SURFACE
) {
1981 con
->scanout
.kind
= SCANOUT_NONE
;
1983 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1984 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1987 if (dcl
->ops
->dpy_gl_scanout_disable
) {
1988 dcl
->ops
->dpy_gl_scanout_disable(dcl
);
1993 void dpy_gl_scanout_texture(QemuConsole
*con
,
1994 uint32_t backing_id
,
1995 bool backing_y_0_top
,
1996 uint32_t backing_width
,
1997 uint32_t backing_height
,
1998 uint32_t x
, uint32_t y
,
1999 uint32_t width
, uint32_t height
)
2001 DisplayState
*s
= con
->ds
;
2002 DisplayChangeListener
*dcl
;
2004 con
->scanout
.kind
= SCANOUT_TEXTURE
;
2005 con
->scanout
.texture
= (ScanoutTexture
) {
2006 backing_id
, backing_y_0_top
, backing_width
, backing_height
,
2009 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2010 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2013 if (dcl
->ops
->dpy_gl_scanout_texture
) {
2014 dcl
->ops
->dpy_gl_scanout_texture(dcl
, backing_id
,
2016 backing_width
, backing_height
,
2017 x
, y
, width
, height
);
2022 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
2025 DisplayState
*s
= con
->ds
;
2026 DisplayChangeListener
*dcl
;
2028 con
->scanout
.kind
= SCANOUT_DMABUF
;
2029 con
->scanout
.dmabuf
= dmabuf
;
2030 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2031 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2034 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
2035 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, dmabuf
);
2040 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
2041 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
2043 DisplayState
*s
= con
->ds
;
2044 DisplayChangeListener
*dcl
;
2046 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2047 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2050 if (dcl
->ops
->dpy_gl_cursor_dmabuf
) {
2051 dcl
->ops
->dpy_gl_cursor_dmabuf(dcl
, dmabuf
,
2052 have_hot
, hot_x
, hot_y
);
2057 void dpy_gl_cursor_position(QemuConsole
*con
,
2058 uint32_t pos_x
, uint32_t pos_y
)
2060 DisplayState
*s
= con
->ds
;
2061 DisplayChangeListener
*dcl
;
2063 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2064 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2067 if (dcl
->ops
->dpy_gl_cursor_position
) {
2068 dcl
->ops
->dpy_gl_cursor_position(dcl
, pos_x
, pos_y
);
2073 void dpy_gl_release_dmabuf(QemuConsole
*con
,
2076 DisplayState
*s
= con
->ds
;
2077 DisplayChangeListener
*dcl
;
2079 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2080 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2083 if (dcl
->ops
->dpy_gl_release_dmabuf
) {
2084 dcl
->ops
->dpy_gl_release_dmabuf(dcl
, dmabuf
);
2089 void dpy_gl_update(QemuConsole
*con
,
2090 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
2092 DisplayState
*s
= con
->ds
;
2093 DisplayChangeListener
*dcl
;
2097 graphic_hw_gl_block(con
, true);
2098 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2099 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2102 if (dcl
->ops
->dpy_gl_update
) {
2103 dcl
->ops
->dpy_gl_update(dcl
, x
, y
, w
, h
);
2106 graphic_hw_gl_block(con
, false);
2109 /***********************************************************/
2110 /* register display */
2112 /* console.c internal use only */
2113 static DisplayState
*get_alloc_displaystate(void)
2115 if (!display_state
) {
2116 display_state
= g_new0(DisplayState
, 1);
2117 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2118 text_console_update_cursor
, NULL
);
2120 return display_state
;
2124 * Called by main(), after creating QemuConsoles
2125 * and before initializing ui (sdl/vnc/...).
2127 DisplayState
*init_displaystate(void)
2132 get_alloc_displaystate();
2133 QTAILQ_FOREACH(con
, &consoles
, next
) {
2134 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
2136 text_console_do_init(con
->chr
, display_state
);
2139 /* Hook up into the qom tree here (not in new_console()), once
2140 * all QemuConsoles are created and the order / numbering
2141 * doesn't change any more */
2142 name
= g_strdup_printf("console[%d]", con
->index
);
2143 object_property_add_child(container_get(object_get_root(), "/backend"),
2148 return display_state
;
2151 void graphic_console_set_hwops(QemuConsole
*con
,
2152 const GraphicHwOps
*hw_ops
,
2155 con
->hw_ops
= hw_ops
;
2159 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
2160 const GraphicHwOps
*hw_ops
,
2163 static const char noinit
[] =
2164 "Guest has not initialized the display (yet).";
2169 DisplaySurface
*surface
;
2171 ds
= get_alloc_displaystate();
2172 s
= qemu_console_lookup_unused();
2174 trace_console_gfx_reuse(s
->index
);
2175 width
= qemu_console_get_width(s
, 0);
2176 height
= qemu_console_get_height(s
, 0);
2178 trace_console_gfx_new();
2179 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
2180 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2181 dpy_set_ui_info_timer
, s
);
2183 graphic_console_set_hwops(s
, hw_ops
, opaque
);
2185 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
2189 surface
= qemu_create_placeholder_surface(width
, height
, noinit
);
2190 dpy_gfx_replace_surface(s
, surface
);
2191 s
->gl_unblock_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2192 graphic_hw_gl_unblock_timer
, s
);
2196 static const GraphicHwOps unused_ops
= {
2200 void graphic_console_close(QemuConsole
*con
)
2202 static const char unplugged
[] =
2203 "Guest display has been unplugged";
2204 DisplaySurface
*surface
;
2205 int width
= qemu_console_get_width(con
, 640);
2206 int height
= qemu_console_get_height(con
, 480);
2208 trace_console_gfx_close(con
->index
);
2209 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
2210 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
2213 dpy_gl_scanout_disable(con
);
2215 surface
= qemu_create_placeholder_surface(width
, height
, unplugged
);
2216 dpy_gfx_replace_surface(con
, surface
);
2219 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
2223 QTAILQ_FOREACH(con
, &consoles
, next
) {
2224 if (con
->index
== index
) {
2231 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
2237 QTAILQ_FOREACH(con
, &consoles
, next
) {
2238 obj
= object_property_get_link(OBJECT(con
),
2239 "device", &error_abort
);
2240 if (DEVICE(obj
) != dev
) {
2243 h
= object_property_get_uint(OBJECT(con
),
2244 "head", &error_abort
);
2253 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
2254 uint32_t head
, Error
**errp
)
2259 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2261 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2262 "Device '%s' not found", device_id
);
2266 con
= qemu_console_lookup_by_device(dev
, head
);
2268 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2276 QemuConsole
*qemu_console_lookup_unused(void)
2281 QTAILQ_FOREACH(con
, &consoles
, next
) {
2282 if (con
->hw_ops
!= &unused_ops
) {
2285 obj
= object_property_get_link(OBJECT(con
),
2286 "device", &error_abort
);
2295 bool qemu_console_is_visible(QemuConsole
*con
)
2297 return (con
== active_console
) || (con
->dcls
> 0);
2300 bool qemu_console_is_graphic(QemuConsole
*con
)
2303 con
= active_console
;
2305 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2308 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2311 con
= active_console
;
2313 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2316 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2318 assert(con
!= NULL
);
2319 return con
->gl_block
;
2322 char *qemu_console_get_label(QemuConsole
*con
)
2324 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2326 return g_strdup(object_get_typename(con
->device
));
2328 return g_strdup("VGA");
2330 if (con
->chr
&& con
->chr
->label
) {
2331 return g_strdup(con
->chr
->label
);
2333 return g_strdup_printf("vc%d", con
->index
);
2337 int qemu_console_get_index(QemuConsole
*con
)
2340 con
= active_console
;
2342 return con
? con
->index
: -1;
2345 uint32_t qemu_console_get_head(QemuConsole
*con
)
2348 con
= active_console
;
2350 return con
? con
->head
: -1;
2353 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2356 con
= active_console
;
2361 switch (con
->scanout
.kind
) {
2362 case SCANOUT_DMABUF
:
2363 return con
->scanout
.dmabuf
->width
;
2364 case SCANOUT_TEXTURE
:
2365 return con
->scanout
.texture
.width
;
2366 case SCANOUT_SURFACE
:
2367 return surface_width(con
->surface
);
2373 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2376 con
= active_console
;
2381 switch (con
->scanout
.kind
) {
2382 case SCANOUT_DMABUF
:
2383 return con
->scanout
.dmabuf
->height
;
2384 case SCANOUT_TEXTURE
:
2385 return con
->scanout
.texture
.height
;
2386 case SCANOUT_SURFACE
:
2387 return surface_height(con
->surface
);
2393 static void vc_chr_accept_input(Chardev
*chr
)
2395 VCChardev
*drv
= VC_CHARDEV(chr
);
2396 QemuConsole
*s
= drv
->console
;
2401 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2403 VCChardev
*drv
= VC_CHARDEV(chr
);
2404 QemuConsole
*s
= drv
->console
;
2409 static void text_console_update_cursor_timer(void)
2411 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2412 + CONSOLE_CURSOR_PERIOD
/ 2);
2415 static void text_console_update_cursor(void *opaque
)
2420 cursor_visible_phase
= !cursor_visible_phase
;
2422 QTAILQ_FOREACH(s
, &consoles
, next
) {
2423 if (qemu_console_is_graphic(s
) ||
2424 !qemu_console_is_visible(s
)) {
2428 graphic_hw_invalidate(s
);
2432 text_console_update_cursor_timer();
2436 static const GraphicHwOps text_console_ops
= {
2437 .invalidate
= text_console_invalidate
,
2438 .text_update
= text_console_update
,
2441 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2443 VCChardev
*drv
= VC_CHARDEV(chr
);
2444 QemuConsole
*s
= drv
->console
;
2445 int g_width
= 80 * FONT_WIDTH
;
2446 int g_height
= 24 * FONT_HEIGHT
;
2448 fifo8_create(&s
->out_fifo
, 16);
2453 s
->total_height
= DEFAULT_BACKSCROLL
;
2456 if (s
->scanout
.kind
!= SCANOUT_SURFACE
) {
2457 if (active_console
&& active_console
->scanout
.kind
== SCANOUT_SURFACE
) {
2458 g_width
= qemu_console_get_width(active_console
, g_width
);
2459 g_height
= qemu_console_get_height(active_console
, g_height
);
2461 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2462 s
->scanout
.kind
= SCANOUT_SURFACE
;
2465 s
->hw_ops
= &text_console_ops
;
2468 /* Set text attribute defaults */
2469 s
->t_attrib_default
.bold
= 0;
2470 s
->t_attrib_default
.uline
= 0;
2471 s
->t_attrib_default
.blink
= 0;
2472 s
->t_attrib_default
.invers
= 0;
2473 s
->t_attrib_default
.unvisible
= 0;
2474 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2475 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2476 /* set current text attributes to default */
2477 s
->t_attrib
= s
->t_attrib_default
;
2478 text_console_resize(s
);
2483 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2484 msg
= g_strdup_printf("%s console\r\n", chr
->label
);
2485 vc_chr_write(chr
, (uint8_t *)msg
, strlen(msg
));
2487 s
->t_attrib
= s
->t_attrib_default
;
2490 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2493 static void vc_chr_open(Chardev
*chr
,
2494 ChardevBackend
*backend
,
2498 ChardevVC
*vc
= backend
->u
.vc
.data
;
2499 VCChardev
*drv
= VC_CHARDEV(chr
);
2502 unsigned height
= 0;
2504 if (vc
->has_width
) {
2506 } else if (vc
->has_cols
) {
2507 width
= vc
->cols
* FONT_WIDTH
;
2510 if (vc
->has_height
) {
2511 height
= vc
->height
;
2512 } else if (vc
->has_rows
) {
2513 height
= vc
->rows
* FONT_HEIGHT
;
2516 trace_console_txt_new(width
, height
);
2517 if (width
== 0 || height
== 0) {
2518 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2520 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2521 s
->scanout
.kind
= SCANOUT_SURFACE
;
2522 s
->surface
= qemu_create_displaysurface(width
, height
);
2526 error_setg(errp
, "cannot create text console");
2533 if (display_state
) {
2534 text_console_do_init(chr
, display_state
);
2537 /* console/chardev init sometimes completes elsewhere in a 2nd
2538 * stage, so defer OPENED events until they are fully initialized
2543 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2545 DisplaySurface
*surface
;
2547 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2549 if (qemu_console_get_width(s
, -1) == width
&&
2550 qemu_console_get_height(s
, -1) == height
) {
2554 surface
= qemu_create_displaysurface(width
, height
);
2555 dpy_gfx_replace_surface(s
, surface
);
2558 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2560 switch (console
->scanout
.kind
) {
2561 case SCANOUT_SURFACE
:
2562 return console
->surface
;
2568 PixelFormat
qemu_default_pixelformat(int bpp
)
2570 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2571 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2575 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2577 void qemu_display_register(QemuDisplay
*ui
)
2579 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2580 dpys
[ui
->type
] = ui
;
2583 bool qemu_display_find_default(DisplayOptions
*opts
)
2585 static DisplayType prio
[] = {
2586 #if defined(CONFIG_GTK)
2589 #if defined(CONFIG_SDL)
2592 #if defined(CONFIG_COCOA)
2598 for (i
= 0; i
< (int)ARRAY_SIZE(prio
); i
++) {
2599 if (dpys
[prio
[i
]] == NULL
) {
2600 ui_module_load_one(DisplayType_str(prio
[i
]));
2602 if (dpys
[prio
[i
]] == NULL
) {
2605 opts
->type
= prio
[i
];
2611 void qemu_display_early_init(DisplayOptions
*opts
)
2613 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2614 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2617 if (dpys
[opts
->type
] == NULL
) {
2618 ui_module_load_one(DisplayType_str(opts
->type
));
2620 if (dpys
[opts
->type
] == NULL
) {
2621 error_report("Display '%s' is not available.",
2622 DisplayType_str(opts
->type
));
2625 if (dpys
[opts
->type
]->early_init
) {
2626 dpys
[opts
->type
]->early_init(opts
);
2630 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2632 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2633 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2636 assert(dpys
[opts
->type
] != NULL
);
2637 dpys
[opts
->type
]->init(ds
, opts
);
2640 void qemu_display_help(void)
2644 printf("Available display backend types:\n");
2646 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2648 ui_module_load_one(DisplayType_str(idx
));
2651 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2656 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2661 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2662 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2663 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2665 val
= qemu_opt_get_number(opts
, "width", 0);
2667 vc
->has_width
= true;
2671 val
= qemu_opt_get_number(opts
, "height", 0);
2673 vc
->has_height
= true;
2677 val
= qemu_opt_get_number(opts
, "cols", 0);
2679 vc
->has_cols
= true;
2683 val
= qemu_opt_get_number(opts
, "rows", 0);
2685 vc
->has_rows
= true;
2690 static const TypeInfo qemu_console_info
= {
2691 .name
= TYPE_QEMU_CONSOLE
,
2692 .parent
= TYPE_OBJECT
,
2693 .instance_size
= sizeof(QemuConsole
),
2694 .class_size
= sizeof(QemuConsoleClass
),
2697 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2699 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2701 cc
->parse
= qemu_chr_parse_vc
;
2702 cc
->open
= vc_chr_open
;
2703 cc
->chr_write
= vc_chr_write
;
2704 cc
->chr_accept_input
= vc_chr_accept_input
;
2705 cc
->chr_set_echo
= vc_chr_set_echo
;
2708 static const TypeInfo char_vc_type_info
= {
2709 .name
= TYPE_CHARDEV_VC
,
2710 .parent
= TYPE_CHARDEV
,
2711 .instance_size
= sizeof(VCChardev
),
2712 .class_init
= char_vc_class_init
,
2715 void qemu_console_early_init(void)
2717 /* set the default vc driver */
2718 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2719 type_register(&char_vc_type_info
);
2723 static void register_types(void)
2725 type_register_static(&qemu_console_info
);
2728 type_init(register_types
);