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/fifo8.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/module.h"
34 #include "qemu/option.h"
35 #include "qemu/timer.h"
36 #include "chardev/char.h"
38 #include "exec/memory.h"
39 #include "io/channel-file.h"
40 #include "qom/object.h"
45 #define DEFAULT_BACKSCROLL 512
46 #define CONSOLE_CURSOR_PERIOD 500
48 typedef struct TextAttributes
{
58 typedef struct TextCell
{
60 TextAttributes t_attrib
;
63 #define MAX_ESC_PARAMS 3
74 TEXT_CONSOLE_FIXED_SIZE
81 console_type_t console_type
;
83 DisplaySurface
*surface
;
84 DisplayScanout scanout
;
88 QEMUTimer
*gl_unblock_timer
;
91 /* Graphic console state. */
96 const GraphicHwOps
*hw_ops
;
99 /* Text console state */
103 int backscroll_height
;
105 int x_saved
, y_saved
;
108 TextAttributes t_attrib_default
; /* default text attributes */
109 TextAttributes t_attrib
; /* currently active text attributes */
111 int text_x
[2], text_y
[2], cursor_invalidate
;
120 int esc_params
[MAX_ESC_PARAMS
];
124 /* fifo for key pressed */
128 QTAILQ_ENTRY(QemuConsole
) next
;
131 struct DisplayState
{
132 QEMUTimer
*gui_timer
;
133 uint64_t last_update
;
134 uint64_t update_interval
;
139 QLIST_HEAD(, DisplayChangeListener
) listeners
;
142 static DisplayState
*display_state
;
143 static QemuConsole
*active_console
;
144 static QTAILQ_HEAD(, QemuConsole
) consoles
=
145 QTAILQ_HEAD_INITIALIZER(consoles
);
146 static bool cursor_visible_phase
;
147 static QEMUTimer
*cursor_timer
;
149 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
150 static void dpy_refresh(DisplayState
*s
);
151 static DisplayState
*get_alloc_displaystate(void);
152 static void text_console_update_cursor_timer(void);
153 static void text_console_update_cursor(void *opaque
);
154 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
);
155 static bool console_compatible_with(QemuConsole
*con
,
156 DisplayChangeListener
*dcl
, Error
**errp
);
158 static void gui_update(void *opaque
)
160 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
161 uint64_t dcl_interval
;
162 DisplayState
*ds
= opaque
;
163 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 trace_console_refresh(interval
);
180 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
181 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
184 static void gui_setup_refresh(DisplayState
*ds
)
186 DisplayChangeListener
*dcl
;
187 bool need_timer
= false;
188 bool have_gfx
= false;
189 bool have_text
= false;
191 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
192 if (dcl
->ops
->dpy_refresh
!= NULL
) {
195 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
198 if (dcl
->ops
->dpy_text_update
!= NULL
) {
203 if (need_timer
&& ds
->gui_timer
== NULL
) {
204 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
205 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
207 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
208 timer_free(ds
->gui_timer
);
209 ds
->gui_timer
= NULL
;
212 ds
->have_gfx
= have_gfx
;
213 ds
->have_text
= have_text
;
216 void graphic_hw_update_done(QemuConsole
*con
)
219 qemu_co_enter_all(&con
->dump_queue
, NULL
);
223 void graphic_hw_update(QemuConsole
*con
)
226 con
= con
? con
: active_console
;
230 if (con
->hw_ops
->gfx_update
) {
231 con
->hw_ops
->gfx_update(con
->hw
);
232 async
= con
->hw_ops
->gfx_update_async
;
235 graphic_hw_update_done(con
);
239 static void graphic_hw_gl_unblock_timer(void *opaque
)
241 warn_report("console: no gl-unblock within one second");
244 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
254 assert(con
->gl_block
>= 0);
255 if (!con
->hw_ops
->gl_block
) {
258 if ((block
&& con
->gl_block
!= 1) || (!block
&& con
->gl_block
!= 0)) {
261 con
->hw_ops
->gl_block(con
->hw
, block
);
264 timeout
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
265 timeout
+= 1000; /* one sec */
266 timer_mod(con
->gl_unblock_timer
, timeout
);
268 timer_del(con
->gl_unblock_timer
);
272 int qemu_console_get_window_id(QemuConsole
*con
)
274 return con
->window_id
;
277 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
279 con
->window_id
= window_id
;
282 void graphic_hw_invalidate(QemuConsole
*con
)
285 con
= active_console
;
287 if (con
&& con
->hw_ops
->invalidate
) {
288 con
->hw_ops
->invalidate(con
->hw
);
294 * png_save: Take a screenshot as PNG
296 * Saves screendump as a PNG file
298 * Returns true for success or false for error.
300 * @fd: File descriptor for PNG file.
301 * @image: Image data in pixman format.
302 * @errp: Pointer to an error.
304 static bool png_save(int fd
, pixman_image_t
*image
, Error
**errp
)
306 int width
= pixman_image_get_width(image
);
307 int height
= pixman_image_get_height(image
);
310 g_autoptr(pixman_image_t
) linebuf
=
311 qemu_pixman_linebuf_create(PIXMAN_a8r8g8b8
, width
);
312 uint8_t *buf
= (uint8_t *)pixman_image_get_data(linebuf
);
313 FILE *f
= fdopen(fd
, "wb");
316 error_setg_errno(errp
, errno
,
317 "Failed to create file from file descriptor");
321 png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
,
324 error_setg(errp
, "PNG creation failed. Unable to write struct");
329 info_ptr
= png_create_info_struct(png_ptr
);
332 error_setg(errp
, "PNG creation failed. Unable to write info");
334 png_destroy_write_struct(&png_ptr
, &info_ptr
);
338 png_init_io(png_ptr
, f
);
340 png_set_IHDR(png_ptr
, info_ptr
, width
, height
, 8,
341 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
342 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
344 png_write_info(png_ptr
, info_ptr
);
346 for (y
= 0; y
< height
; ++y
) {
347 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
348 png_write_row(png_ptr
, buf
);
351 png_write_end(png_ptr
, NULL
);
353 png_destroy_write_struct(&png_ptr
, &info_ptr
);
355 if (fclose(f
) != 0) {
356 error_setg_errno(errp
, errno
,
357 "PNG creation failed. Unable to close file");
364 #else /* no png support */
366 static bool png_save(int fd
, pixman_image_t
*image
, Error
**errp
)
368 error_setg(errp
, "Enable PNG support with libpng for screendump");
372 #endif /* CONFIG_PNG */
374 static bool ppm_save(int fd
, pixman_image_t
*image
, Error
**errp
)
376 int width
= pixman_image_get_width(image
);
377 int height
= pixman_image_get_height(image
);
378 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
379 g_autofree
char *header
= NULL
;
380 g_autoptr(pixman_image_t
) linebuf
= NULL
;
383 trace_ppm_save(fd
, image
);
385 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
386 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
387 header
, strlen(header
), errp
) < 0) {
391 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
392 for (y
= 0; y
< height
; y
++) {
393 qemu_pixman_linebuf_fill(linebuf
, image
, width
, 0, y
);
394 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
395 (char *)pixman_image_get_data(linebuf
),
396 pixman_image_get_stride(linebuf
), errp
) < 0) {
404 static void graphic_hw_update_bh(void *con
)
406 graphic_hw_update(con
);
409 /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
411 qmp_screendump(const char *filename
, const char *device
,
412 bool has_head
, int64_t head
,
413 bool has_format
, ImageFormat format
, Error
**errp
)
415 g_autoptr(pixman_image_t
) image
= NULL
;
417 DisplaySurface
*surface
;
421 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
428 error_setg(errp
, "'head' must be specified together with 'device'");
431 con
= qemu_console_lookup_by_index(0);
433 error_setg(errp
, "There is no console to take a screendump from");
438 if (qemu_co_queue_empty(&con
->dump_queue
)) {
439 /* Defer the update, it will restart the pending coroutines */
440 aio_bh_schedule_oneshot(qemu_get_aio_context(),
441 graphic_hw_update_bh
, con
);
443 qemu_co_queue_wait(&con
->dump_queue
, NULL
);
446 * All pending coroutines are woken up, while the BQL is held. No
447 * further graphic update are possible until it is released. Take
448 * an image ref before that.
450 surface
= qemu_console_surface(con
);
452 error_setg(errp
, "no surface");
455 image
= pixman_image_ref(surface
->image
);
457 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
459 error_setg(errp
, "failed to open file '%s': %s", filename
,
465 * The image content could potentially be updated as the coroutine
466 * yields and releases the BQL. It could produce corrupted dump, but
467 * it should be otherwise safe.
469 if (has_format
&& format
== IMAGE_FORMAT_PNG
) {
470 /* PNG format specified for screendump */
471 if (!png_save(fd
, image
, errp
)) {
472 qemu_unlink(filename
);
475 /* PPM format specified/default for screendump */
476 if (!ppm_save(fd
, image
, errp
)) {
477 qemu_unlink(filename
);
482 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
485 con
= active_console
;
487 if (con
&& con
->hw_ops
->text_update
) {
488 con
->hw_ops
->text_update(con
->hw
, chardata
);
492 static void vga_fill_rect(QemuConsole
*con
,
493 int posx
, int posy
, int width
, int height
,
494 pixman_color_t color
)
496 DisplaySurface
*surface
= qemu_console_surface(con
);
497 pixman_rectangle16_t rect
= {
498 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
501 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
505 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
506 static void vga_bitblt(QemuConsole
*con
,
507 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
509 DisplaySurface
*surface
= qemu_console_surface(con
);
511 pixman_image_composite(PIXMAN_OP_SRC
,
512 surface
->image
, NULL
, surface
->image
,
513 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
516 /***********************************************************/
517 /* basic char display */
519 #define FONT_HEIGHT 16
524 #define QEMU_RGB(r, g, b) \
525 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
527 static const pixman_color_t color_table_rgb
[2][8] = {
529 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
530 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
531 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
532 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
533 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
534 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
535 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
536 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
539 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
540 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
541 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
542 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
543 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
544 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
545 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
546 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
550 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
551 TextAttributes
*t_attrib
)
553 static pixman_image_t
*glyphs
[256];
554 DisplaySurface
*surface
= qemu_console_surface(s
);
555 pixman_color_t fgcol
, bgcol
;
557 if (t_attrib
->invers
) {
558 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
559 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
561 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
562 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
566 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
568 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
569 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
572 static void text_console_resize(QemuConsole
*s
)
574 TextCell
*cells
, *c
, *c1
;
575 int w1
, x
, y
, last_width
;
577 assert(s
->scanout
.kind
== SCANOUT_SURFACE
);
579 last_width
= s
->width
;
580 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
581 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
587 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
588 for(y
= 0; y
< s
->total_height
; y
++) {
589 c
= &cells
[y
* s
->width
];
591 c1
= &s
->cells
[y
* last_width
];
592 for(x
= 0; x
< w1
; x
++) {
596 for(x
= w1
; x
< s
->width
; x
++) {
598 c
->t_attrib
= s
->t_attrib_default
;
606 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
608 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
609 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
610 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
611 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
614 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
616 if (!qemu_console_is_visible(s
)) {
619 if (s
->update_x0
> x
* FONT_WIDTH
)
620 s
->update_x0
= x
* FONT_WIDTH
;
621 if (s
->update_y0
> y
* FONT_HEIGHT
)
622 s
->update_y0
= y
* FONT_HEIGHT
;
623 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
624 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
625 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
626 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
629 static void update_xy(QemuConsole
*s
, int x
, int y
)
634 if (s
->ds
->have_text
) {
635 text_update_xy(s
, x
, y
);
638 y1
= (s
->y_base
+ y
) % s
->total_height
;
639 y2
= y1
- s
->y_displayed
;
641 y2
+= s
->total_height
;
643 if (y2
< s
->height
) {
647 c
= &s
->cells
[y1
* s
->width
+ x
];
648 vga_putcharxy(s
, x
, y2
, c
->ch
,
650 invalidate_xy(s
, x
, y2
);
654 static void console_show_cursor(QemuConsole
*s
, int show
)
660 if (s
->ds
->have_text
) {
661 s
->cursor_invalidate
= 1;
667 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
668 y
= y1
- s
->y_displayed
;
670 y
+= s
->total_height
;
673 c
= &s
->cells
[y1
* s
->width
+ x
];
674 if (show
&& cursor_visible_phase
) {
675 TextAttributes t_attrib
= s
->t_attrib_default
;
676 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
677 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
679 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
681 invalidate_xy(s
, x
, y
);
685 static void console_refresh(QemuConsole
*s
)
687 DisplaySurface
*surface
= qemu_console_surface(s
);
691 if (s
->ds
->have_text
) {
694 s
->text_x
[1] = s
->width
- 1;
695 s
->text_y
[1] = s
->height
- 1;
696 s
->cursor_invalidate
= 1;
699 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
700 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
702 for (y
= 0; y
< s
->height
; y
++) {
703 c
= s
->cells
+ y1
* s
->width
;
704 for (x
= 0; x
< s
->width
; x
++) {
705 vga_putcharxy(s
, x
, y
, c
->ch
,
709 if (++y1
== s
->total_height
) {
713 console_show_cursor(s
, 1);
714 dpy_gfx_update(s
, 0, 0,
715 surface_width(surface
), surface_height(surface
));
718 static void console_scroll(QemuConsole
*s
, int ydelta
)
723 for(i
= 0; i
< ydelta
; i
++) {
724 if (s
->y_displayed
== s
->y_base
)
726 if (++s
->y_displayed
== s
->total_height
)
731 i
= s
->backscroll_height
;
732 if (i
> s
->total_height
- s
->height
)
733 i
= s
->total_height
- s
->height
;
736 y1
+= s
->total_height
;
737 for(i
= 0; i
< ydelta
; i
++) {
738 if (s
->y_displayed
== y1
)
740 if (--s
->y_displayed
< 0)
741 s
->y_displayed
= s
->total_height
- 1;
747 static void console_put_lf(QemuConsole
*s
)
753 if (s
->y
>= s
->height
) {
754 s
->y
= s
->height
- 1;
756 if (s
->y_displayed
== s
->y_base
) {
757 if (++s
->y_displayed
== s
->total_height
)
760 if (++s
->y_base
== s
->total_height
)
762 if (s
->backscroll_height
< s
->total_height
)
763 s
->backscroll_height
++;
764 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
765 c
= &s
->cells
[y1
* s
->width
];
766 for(x
= 0; x
< s
->width
; x
++) {
768 c
->t_attrib
= s
->t_attrib_default
;
771 if (s
->y_displayed
== s
->y_base
) {
772 if (s
->ds
->have_text
) {
775 s
->text_x
[1] = s
->width
- 1;
776 s
->text_y
[1] = s
->height
- 1;
779 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
780 s
->width
* FONT_WIDTH
,
781 (s
->height
- 1) * FONT_HEIGHT
);
782 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
783 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
784 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
787 s
->update_x1
= s
->width
* FONT_WIDTH
;
788 s
->update_y1
= s
->height
* FONT_HEIGHT
;
793 /* Set console attributes depending on the current escape codes.
794 * NOTE: I know this code is not very efficient (checking every color for it
795 * self) but it is more readable and better maintainable.
797 static void console_handle_escape(QemuConsole
*s
)
801 for (i
=0; i
<s
->nb_esc_params
; i
++) {
802 switch (s
->esc_params
[i
]) {
803 case 0: /* reset all console attributes to default */
804 s
->t_attrib
= s
->t_attrib_default
;
807 s
->t_attrib
.bold
= 1;
810 s
->t_attrib
.uline
= 1;
813 s
->t_attrib
.blink
= 1;
816 s
->t_attrib
.invers
= 1;
819 s
->t_attrib
.unvisible
= 1;
822 s
->t_attrib
.bold
= 0;
825 s
->t_attrib
.uline
= 0;
828 s
->t_attrib
.blink
= 0;
831 s
->t_attrib
.invers
= 0;
834 s
->t_attrib
.unvisible
= 0;
836 /* set foreground color */
838 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
841 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
844 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
847 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
850 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
853 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
856 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
859 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
861 /* set background color */
863 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
866 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
869 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
872 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
875 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
878 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
881 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
884 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
890 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
892 int y1
= (s
->y_base
+ y
) % s
->total_height
;
896 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
898 c
->t_attrib
= s
->t_attrib_default
;
902 static void console_put_one(QemuConsole
*s
, int ch
)
906 if (s
->x
>= s
->width
) {
911 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
912 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
914 c
->t_attrib
= s
->t_attrib
;
915 update_xy(s
, s
->x
, s
->y
);
919 static void console_respond_str(QemuConsole
*s
, const char *buf
)
922 console_put_one(s
, *buf
);
927 /* set cursor, checking bounds */
928 static void set_cursor(QemuConsole
*s
, int x
, int y
)
936 if (y
>= s
->height
) {
947 static void console_putchar(QemuConsole
*s
, int ch
)
956 case '\r': /* carriage return */
959 case '\n': /* newline */
962 case '\b': /* backspace */
966 case '\t': /* tabspace */
967 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
971 s
->x
= s
->x
+ (8 - (s
->x
% 8));
974 case '\a': /* alert aka. bell */
975 /* TODO: has to be implemented */
978 /* SI (shift in), character set 0 (ignored) */
981 /* SO (shift out), character set 1 (ignored) */
983 case 27: /* esc (introducing an escape sequence) */
984 s
->state
= TTY_STATE_ESC
;
987 console_put_one(s
, ch
);
991 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
993 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
994 s
->esc_params
[i
] = 0;
995 s
->nb_esc_params
= 0;
996 s
->state
= TTY_STATE_CSI
;
998 s
->state
= TTY_STATE_NORM
;
1001 case TTY_STATE_CSI
: /* handle escape sequence parameters */
1002 if (ch
>= '0' && ch
<= '9') {
1003 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
1004 int *param
= &s
->esc_params
[s
->nb_esc_params
];
1005 int digit
= (ch
- '0');
1007 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
1008 *param
* 10 + digit
: INT_MAX
;
1011 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
1013 if (ch
== ';' || ch
== '?') {
1016 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
1017 ch
, s
->nb_esc_params
);
1018 s
->state
= TTY_STATE_NORM
;
1021 /* move cursor up */
1022 if (s
->esc_params
[0] == 0) {
1023 s
->esc_params
[0] = 1;
1025 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
1028 /* move cursor down */
1029 if (s
->esc_params
[0] == 0) {
1030 s
->esc_params
[0] = 1;
1032 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
1035 /* move cursor right */
1036 if (s
->esc_params
[0] == 0) {
1037 s
->esc_params
[0] = 1;
1039 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
1042 /* move cursor left */
1043 if (s
->esc_params
[0] == 0) {
1044 s
->esc_params
[0] = 1;
1046 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
1049 /* move cursor to column */
1050 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
1054 /* move cursor to row, column */
1055 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
1058 switch (s
->esc_params
[0]) {
1060 /* clear to end of screen */
1061 for (y
= s
->y
; y
< s
->height
; y
++) {
1062 for (x
= 0; x
< s
->width
; x
++) {
1063 if (y
== s
->y
&& x
< s
->x
) {
1066 console_clear_xy(s
, x
, y
);
1071 /* clear from beginning of screen */
1072 for (y
= 0; y
<= s
->y
; y
++) {
1073 for (x
= 0; x
< s
->width
; x
++) {
1074 if (y
== s
->y
&& x
> s
->x
) {
1077 console_clear_xy(s
, x
, y
);
1082 /* clear entire screen */
1083 for (y
= 0; y
<= s
->height
; y
++) {
1084 for (x
= 0; x
< s
->width
; x
++) {
1085 console_clear_xy(s
, x
, y
);
1092 switch (s
->esc_params
[0]) {
1095 for(x
= s
->x
; x
< s
->width
; x
++) {
1096 console_clear_xy(s
, x
, s
->y
);
1100 /* clear from beginning of line */
1101 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1102 console_clear_xy(s
, x
, s
->y
);
1106 /* clear entire line */
1107 for(x
= 0; x
< s
->width
; x
++) {
1108 console_clear_xy(s
, x
, s
->y
);
1114 console_handle_escape(s
);
1117 switch (s
->esc_params
[0]) {
1119 /* report console status (always succeed)*/
1120 console_respond_str(s
, "\033[0n");
1123 /* report cursor position */
1124 sprintf(response
, "\033[%d;%dR",
1125 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1127 console_respond_str(s
, response
);
1132 /* save cursor position */
1137 /* restore cursor position */
1142 trace_console_putchar_unhandled(ch
);
1150 static void displaychangelistener_gfx_switch(DisplayChangeListener
*dcl
,
1151 struct DisplaySurface
*new_surface
,
1154 if (dcl
->ops
->dpy_gfx_switch
) {
1155 dcl
->ops
->dpy_gfx_switch(dcl
, new_surface
);
1158 if (update
&& dcl
->ops
->dpy_gfx_update
) {
1159 dcl
->ops
->dpy_gfx_update(dcl
, 0, 0,
1160 surface_width(new_surface
),
1161 surface_height(new_surface
));
1165 static void dpy_gfx_create_texture(QemuConsole
*con
, DisplaySurface
*surface
)
1167 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_create_texture
) {
1168 con
->gl
->ops
->dpy_gl_ctx_create_texture(con
->gl
, surface
);
1172 static void dpy_gfx_destroy_texture(QemuConsole
*con
, DisplaySurface
*surface
)
1174 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_destroy_texture
) {
1175 con
->gl
->ops
->dpy_gl_ctx_destroy_texture(con
->gl
, surface
);
1179 static void dpy_gfx_update_texture(QemuConsole
*con
, DisplaySurface
*surface
,
1180 int x
, int y
, int w
, int h
)
1182 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_update_texture
) {
1183 con
->gl
->ops
->dpy_gl_ctx_update_texture(con
->gl
, surface
, x
, y
, w
, h
);
1187 static void displaychangelistener_display_console(DisplayChangeListener
*dcl
,
1191 static const char nodev
[] =
1192 "This VM has no graphic display device.";
1193 static DisplaySurface
*dummy
;
1195 if (!con
|| !console_compatible_with(con
, dcl
, errp
)) {
1197 dummy
= qemu_create_placeholder_surface(640, 480, nodev
);
1200 dpy_gfx_create_texture(con
, dummy
);
1202 displaychangelistener_gfx_switch(dcl
, dummy
, TRUE
);
1206 dpy_gfx_create_texture(con
, con
->surface
);
1207 displaychangelistener_gfx_switch(dcl
, con
->surface
,
1208 con
->scanout
.kind
== SCANOUT_SURFACE
);
1210 if (con
->scanout
.kind
== SCANOUT_DMABUF
&&
1211 displaychangelistener_has_dmabuf(dcl
)) {
1212 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, con
->scanout
.dmabuf
);
1213 } else if (con
->scanout
.kind
== SCANOUT_TEXTURE
&&
1214 dcl
->ops
->dpy_gl_scanout_texture
) {
1215 dcl
->ops
->dpy_gl_scanout_texture(dcl
,
1216 con
->scanout
.texture
.backing_id
,
1217 con
->scanout
.texture
.backing_y_0_top
,
1218 con
->scanout
.texture
.backing_width
,
1219 con
->scanout
.texture
.backing_height
,
1220 con
->scanout
.texture
.x
,
1221 con
->scanout
.texture
.y
,
1222 con
->scanout
.texture
.width
,
1223 con
->scanout
.texture
.height
);
1227 void console_select(unsigned int index
)
1229 DisplayChangeListener
*dcl
;
1232 trace_console_select(index
);
1233 s
= qemu_console_lookup_by_index(index
);
1235 DisplayState
*ds
= s
->ds
;
1239 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1240 if (dcl
->con
!= NULL
) {
1243 displaychangelistener_display_console(dcl
, s
, NULL
);
1246 if (ds
->have_text
) {
1247 dpy_text_resize(s
, s
->width
, s
->height
);
1249 text_console_update_cursor(NULL
);
1255 QemuConsole
*console
;
1257 typedef struct VCChardev VCChardev
;
1259 #define TYPE_CHARDEV_VC "chardev-vc"
1260 DECLARE_INSTANCE_CHECKER(VCChardev
, VC_CHARDEV
,
1263 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1265 VCChardev
*drv
= VC_CHARDEV(chr
);
1266 QemuConsole
*s
= drv
->console
;
1273 s
->update_x0
= s
->width
* FONT_WIDTH
;
1274 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1277 console_show_cursor(s
, 0);
1278 for(i
= 0; i
< len
; i
++) {
1279 console_putchar(s
, buf
[i
]);
1281 console_show_cursor(s
, 1);
1282 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1283 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1284 s
->update_x1
- s
->update_x0
,
1285 s
->update_y1
- s
->update_y0
);
1290 static void kbd_send_chars(QemuConsole
*s
)
1292 uint32_t len
, avail
;
1294 len
= qemu_chr_be_can_write(s
->chr
);
1295 avail
= fifo8_num_used(&s
->out_fifo
);
1296 while (len
> 0 && avail
> 0) {
1300 buf
= fifo8_pop_buf(&s
->out_fifo
, MIN(len
, avail
), &size
);
1301 qemu_chr_be_write(s
->chr
, buf
, size
);
1302 len
= qemu_chr_be_can_write(s
->chr
);
1307 /* called when an ascii key is pressed */
1308 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1310 uint8_t buf
[16], *q
;
1314 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1318 case QEMU_KEY_CTRL_UP
:
1319 console_scroll(s
, -1);
1321 case QEMU_KEY_CTRL_DOWN
:
1322 console_scroll(s
, 1);
1324 case QEMU_KEY_CTRL_PAGEUP
:
1325 console_scroll(s
, -10);
1327 case QEMU_KEY_CTRL_PAGEDOWN
:
1328 console_scroll(s
, 10);
1331 /* convert the QEMU keysym to VT100 key string */
1333 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1336 c
= keysym
- 0xe100;
1338 *q
++ = '0' + (c
/ 10);
1339 *q
++ = '0' + (c
% 10);
1341 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1344 *q
++ = keysym
& 0xff;
1345 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1346 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1352 vc_chr_write(s
->chr
, buf
, q
- buf
);
1354 num_free
= fifo8_num_free(&s
->out_fifo
);
1355 fifo8_push_all(&s
->out_fifo
, buf
, MIN(num_free
, q
- buf
));
1361 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1362 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1363 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1364 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1365 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1366 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1367 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1368 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1369 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1370 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1371 [Q_KEY_CODE_TAB
] = QEMU_KEY_TAB
,
1372 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1375 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1376 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1377 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1378 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1379 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1380 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1381 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1382 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1383 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1386 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1390 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1394 kbd_put_keysym_console(s
, keysym
);
1398 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1402 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1403 kbd_put_keysym_console(s
, str
[i
]);
1407 void kbd_put_keysym(int keysym
)
1409 kbd_put_keysym_console(active_console
, keysym
);
1412 static void text_console_invalidate(void *opaque
)
1414 QemuConsole
*s
= (QemuConsole
*) opaque
;
1416 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1417 text_console_resize(s
);
1422 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1424 QemuConsole
*s
= (QemuConsole
*) opaque
;
1427 if (s
->text_x
[0] <= s
->text_x
[1]) {
1428 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1429 chardata
+= s
->text_y
[0] * s
->width
;
1430 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1431 for (j
= 0; j
< s
->width
; j
++, src
++) {
1432 console_write_ch(chardata
++,
1433 ATTR2CHTYPE(s
->cells
[src
].ch
,
1434 s
->cells
[src
].t_attrib
.fgcol
,
1435 s
->cells
[src
].t_attrib
.bgcol
,
1436 s
->cells
[src
].t_attrib
.bold
));
1438 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1439 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1440 s
->text_x
[0] = s
->width
;
1441 s
->text_y
[0] = s
->height
;
1445 if (s
->cursor_invalidate
) {
1446 dpy_text_cursor(s
, s
->x
, s
->y
);
1447 s
->cursor_invalidate
= 0;
1451 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1458 obj
= object_new(TYPE_QEMU_CONSOLE
);
1459 s
= QEMU_CONSOLE(obj
);
1460 qemu_co_queue_init(&s
->dump_queue
);
1462 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1463 (Object
**)&s
->device
,
1464 object_property_allow_set_link
,
1465 OBJ_PROP_LINK_STRONG
);
1466 object_property_add_uint32_ptr(obj
, "head", &s
->head
,
1467 OBJ_PROP_FLAG_READ
);
1469 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1470 (console_type
== GRAPHIC_CONSOLE
))) {
1474 s
->console_type
= console_type
;
1477 if (QTAILQ_EMPTY(&consoles
)) {
1479 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1480 } else if (console_type
!= GRAPHIC_CONSOLE
|| phase_check(PHASE_MACHINE_READY
)) {
1481 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1482 s
->index
= last
->index
+ 1;
1483 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1486 * HACK: Put graphical consoles before text consoles.
1488 * Only do that for coldplugged devices. After initial device
1489 * initialization we will not renumber the consoles any more.
1491 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1493 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1494 c
->console_type
== GRAPHIC_CONSOLE
) {
1495 c
= QTAILQ_NEXT(c
, next
);
1497 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1498 /* have no text consoles */
1499 s
->index
= c
->index
+ 1;
1500 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1502 s
->index
= c
->index
;
1503 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1504 /* renumber text consoles */
1505 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1513 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1515 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1517 trace_displaysurface_create(surface
, width
, height
);
1518 surface
->format
= PIXMAN_x8r8g8b8
;
1519 surface
->image
= pixman_image_create_bits(surface
->format
,
1522 assert(surface
->image
!= NULL
);
1523 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1528 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1529 pixman_format_code_t format
,
1530 int linesize
, uint8_t *data
)
1532 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1534 trace_displaysurface_create_from(surface
, width
, height
, format
);
1535 surface
->format
= format
;
1536 surface
->image
= pixman_image_create_bits(surface
->format
,
1538 (void *)data
, linesize
);
1539 assert(surface
->image
!= NULL
);
1544 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1546 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1548 trace_displaysurface_create_pixman(surface
);
1549 surface
->format
= pixman_image_get_format(image
);
1550 surface
->image
= pixman_image_ref(image
);
1555 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
1558 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1559 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1560 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1561 pixman_image_t
*glyph
;
1565 x
= (w
/ FONT_WIDTH
- len
) / 2;
1566 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1567 for (i
= 0; i
< len
; i
++) {
1568 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1569 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1570 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1571 qemu_pixman_image_unref(glyph
);
1573 surface
->flags
|= QEMU_PLACEHOLDER_FLAG
;
1577 void qemu_free_displaysurface(DisplaySurface
*surface
)
1579 if (surface
== NULL
) {
1582 trace_displaysurface_free(surface
);
1583 qemu_pixman_image_unref(surface
->image
);
1587 bool console_has_gl(QemuConsole
*con
)
1589 return con
->gl
!= NULL
;
1592 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
)
1594 if (dcl
->ops
->dpy_has_dmabuf
) {
1595 return dcl
->ops
->dpy_has_dmabuf(dcl
);
1598 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
1605 static bool console_compatible_with(QemuConsole
*con
,
1606 DisplayChangeListener
*dcl
, Error
**errp
)
1610 flags
= con
->hw_ops
->get_flags
? con
->hw_ops
->get_flags(con
->hw
) : 0;
1612 if (console_has_gl(con
) &&
1613 !con
->gl
->ops
->dpy_gl_ctx_is_compatible_dcl(con
->gl
, dcl
)) {
1614 error_setg(errp
, "Display %s is incompatible with the GL context",
1615 dcl
->ops
->dpy_name
);
1619 if (flags
& GRAPHIC_FLAGS_GL
&&
1620 !console_has_gl(con
)) {
1621 error_setg(errp
, "The console requires a GL context.");
1626 if (flags
& GRAPHIC_FLAGS_DMABUF
&&
1627 !displaychangelistener_has_dmabuf(dcl
)) {
1628 error_setg(errp
, "The console requires display DMABUF support.");
1635 void qemu_console_set_display_gl_ctx(QemuConsole
*con
, DisplayGLCtx
*gl
)
1637 /* display has opengl support */
1640 error_report("The console already has an OpenGL context.");
1646 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1652 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1653 dcl
->ds
= get_alloc_displaystate();
1654 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1655 gui_setup_refresh(dcl
->ds
);
1660 con
= active_console
;
1662 displaychangelistener_display_console(dcl
, con
, dcl
->con
? &error_fatal
: NULL
);
1663 text_console_update_cursor(NULL
);
1666 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1669 DisplayState
*ds
= dcl
->ds
;
1671 dcl
->update_interval
= interval
;
1672 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1673 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1677 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1679 DisplayState
*ds
= dcl
->ds
;
1680 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1684 QLIST_REMOVE(dcl
, next
);
1686 gui_setup_refresh(ds
);
1689 static void dpy_set_ui_info_timer(void *opaque
)
1691 QemuConsole
*con
= opaque
;
1693 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1696 bool dpy_ui_info_supported(QemuConsole
*con
)
1699 con
= active_console
;
1702 return con
->hw_ops
->ui_info
!= NULL
;
1705 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
1708 con
= active_console
;
1711 return &con
->ui_info
;
1714 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
, bool delay
)
1717 con
= active_console
;
1720 if (!dpy_ui_info_supported(con
)) {
1723 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1724 /* nothing changed -- ignore */
1729 * Typically we get a flood of these as the user resizes the window.
1730 * Wait until the dust has settled (one second without updates), then
1731 * go notify the guest.
1733 con
->ui_info
= *info
;
1734 timer_mod(con
->ui_timer
,
1735 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + (delay
? 1000 : 0));
1739 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1741 DisplayState
*s
= con
->ds
;
1742 DisplayChangeListener
*dcl
;
1743 int width
= qemu_console_get_width(con
, x
+ w
);
1744 int height
= qemu_console_get_height(con
, y
+ h
);
1750 w
= MIN(w
, width
- x
);
1751 h
= MIN(h
, height
- y
);
1753 if (!qemu_console_is_visible(con
)) {
1756 dpy_gfx_update_texture(con
, con
->surface
, x
, y
, w
, h
);
1757 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1758 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1761 if (dcl
->ops
->dpy_gfx_update
) {
1762 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1767 void dpy_gfx_update_full(QemuConsole
*con
)
1769 int w
= qemu_console_get_width(con
, 0);
1770 int h
= qemu_console_get_height(con
, 0);
1772 dpy_gfx_update(con
, 0, 0, w
, h
);
1775 void dpy_gfx_replace_surface(QemuConsole
*con
,
1776 DisplaySurface
*surface
)
1778 static const char placeholder_msg
[] = "Display output is not active.";
1779 DisplayState
*s
= con
->ds
;
1780 DisplaySurface
*old_surface
= con
->surface
;
1781 DisplayChangeListener
*dcl
;
1787 width
= surface_width(old_surface
);
1788 height
= surface_height(old_surface
);
1794 surface
= qemu_create_placeholder_surface(width
, height
, placeholder_msg
);
1797 assert(old_surface
!= surface
);
1799 con
->scanout
.kind
= SCANOUT_SURFACE
;
1800 con
->surface
= surface
;
1801 dpy_gfx_create_texture(con
, surface
);
1802 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1803 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1806 displaychangelistener_gfx_switch(dcl
, surface
, FALSE
);
1808 dpy_gfx_destroy_texture(con
, old_surface
);
1809 qemu_free_displaysurface(old_surface
);
1812 bool dpy_gfx_check_format(QemuConsole
*con
,
1813 pixman_format_code_t format
)
1815 DisplayChangeListener
*dcl
;
1816 DisplayState
*s
= con
->ds
;
1818 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1819 if (dcl
->con
&& dcl
->con
!= con
) {
1820 /* dcl bound to another console -> skip */
1823 if (dcl
->ops
->dpy_gfx_check_format
) {
1824 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1828 /* default is to allow native 32 bpp only */
1829 if (format
!= qemu_default_pixman_format(32, true)) {
1837 static void dpy_refresh(DisplayState
*s
)
1839 DisplayChangeListener
*dcl
;
1841 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1842 if (dcl
->ops
->dpy_refresh
) {
1843 dcl
->ops
->dpy_refresh(dcl
);
1848 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1850 DisplayState
*s
= con
->ds
;
1851 DisplayChangeListener
*dcl
;
1853 if (!qemu_console_is_visible(con
)) {
1856 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1857 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1860 if (dcl
->ops
->dpy_text_cursor
) {
1861 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1866 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1868 DisplayState
*s
= con
->ds
;
1869 DisplayChangeListener
*dcl
;
1871 if (!qemu_console_is_visible(con
)) {
1874 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1875 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1878 if (dcl
->ops
->dpy_text_update
) {
1879 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1884 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1886 DisplayState
*s
= con
->ds
;
1887 DisplayChangeListener
*dcl
;
1889 if (!qemu_console_is_visible(con
)) {
1892 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1893 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1896 if (dcl
->ops
->dpy_text_resize
) {
1897 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1902 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1904 DisplayState
*s
= con
->ds
;
1905 DisplayChangeListener
*dcl
;
1907 if (!qemu_console_is_visible(con
)) {
1910 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1911 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1914 if (dcl
->ops
->dpy_mouse_set
) {
1915 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1920 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1922 DisplayState
*s
= con
->ds
;
1923 DisplayChangeListener
*dcl
;
1925 if (!qemu_console_is_visible(con
)) {
1928 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1929 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1932 if (dcl
->ops
->dpy_cursor_define
) {
1933 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1938 bool dpy_cursor_define_supported(QemuConsole
*con
)
1940 DisplayState
*s
= con
->ds
;
1941 DisplayChangeListener
*dcl
;
1943 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1944 if (dcl
->ops
->dpy_cursor_define
) {
1951 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1952 struct QEMUGLParams
*qparams
)
1955 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1958 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1961 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1964 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1967 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1970 void dpy_gl_scanout_disable(QemuConsole
*con
)
1972 DisplayState
*s
= con
->ds
;
1973 DisplayChangeListener
*dcl
;
1975 if (con
->scanout
.kind
!= SCANOUT_SURFACE
) {
1976 con
->scanout
.kind
= SCANOUT_NONE
;
1978 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1979 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1982 if (dcl
->ops
->dpy_gl_scanout_disable
) {
1983 dcl
->ops
->dpy_gl_scanout_disable(dcl
);
1988 void dpy_gl_scanout_texture(QemuConsole
*con
,
1989 uint32_t backing_id
,
1990 bool backing_y_0_top
,
1991 uint32_t backing_width
,
1992 uint32_t backing_height
,
1993 uint32_t x
, uint32_t y
,
1994 uint32_t width
, uint32_t height
)
1996 DisplayState
*s
= con
->ds
;
1997 DisplayChangeListener
*dcl
;
1999 con
->scanout
.kind
= SCANOUT_TEXTURE
;
2000 con
->scanout
.texture
= (ScanoutTexture
) {
2001 backing_id
, backing_y_0_top
, backing_width
, backing_height
,
2004 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2005 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2008 if (dcl
->ops
->dpy_gl_scanout_texture
) {
2009 dcl
->ops
->dpy_gl_scanout_texture(dcl
, backing_id
,
2011 backing_width
, backing_height
,
2012 x
, y
, width
, height
);
2017 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
2020 DisplayState
*s
= con
->ds
;
2021 DisplayChangeListener
*dcl
;
2023 con
->scanout
.kind
= SCANOUT_DMABUF
;
2024 con
->scanout
.dmabuf
= dmabuf
;
2025 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2026 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2029 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
2030 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, dmabuf
);
2035 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
2036 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
2038 DisplayState
*s
= con
->ds
;
2039 DisplayChangeListener
*dcl
;
2041 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2042 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2045 if (dcl
->ops
->dpy_gl_cursor_dmabuf
) {
2046 dcl
->ops
->dpy_gl_cursor_dmabuf(dcl
, dmabuf
,
2047 have_hot
, hot_x
, hot_y
);
2052 void dpy_gl_cursor_position(QemuConsole
*con
,
2053 uint32_t pos_x
, uint32_t pos_y
)
2055 DisplayState
*s
= con
->ds
;
2056 DisplayChangeListener
*dcl
;
2058 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2059 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2062 if (dcl
->ops
->dpy_gl_cursor_position
) {
2063 dcl
->ops
->dpy_gl_cursor_position(dcl
, pos_x
, pos_y
);
2068 void dpy_gl_release_dmabuf(QemuConsole
*con
,
2071 DisplayState
*s
= con
->ds
;
2072 DisplayChangeListener
*dcl
;
2074 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2075 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2078 if (dcl
->ops
->dpy_gl_release_dmabuf
) {
2079 dcl
->ops
->dpy_gl_release_dmabuf(dcl
, dmabuf
);
2084 void dpy_gl_update(QemuConsole
*con
,
2085 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
2087 DisplayState
*s
= con
->ds
;
2088 DisplayChangeListener
*dcl
;
2092 graphic_hw_gl_block(con
, true);
2093 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
2094 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
2097 if (dcl
->ops
->dpy_gl_update
) {
2098 dcl
->ops
->dpy_gl_update(dcl
, x
, y
, w
, h
);
2101 graphic_hw_gl_block(con
, false);
2104 /***********************************************************/
2105 /* register display */
2107 /* console.c internal use only */
2108 static DisplayState
*get_alloc_displaystate(void)
2110 if (!display_state
) {
2111 display_state
= g_new0(DisplayState
, 1);
2112 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2113 text_console_update_cursor
, NULL
);
2115 return display_state
;
2119 * Called by main(), after creating QemuConsoles
2120 * and before initializing ui (sdl/vnc/...).
2122 DisplayState
*init_displaystate(void)
2127 get_alloc_displaystate();
2128 QTAILQ_FOREACH(con
, &consoles
, next
) {
2129 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
2131 text_console_do_init(con
->chr
, display_state
);
2134 /* Hook up into the qom tree here (not in new_console()), once
2135 * all QemuConsoles are created and the order / numbering
2136 * doesn't change any more */
2137 name
= g_strdup_printf("console[%d]", con
->index
);
2138 object_property_add_child(container_get(object_get_root(), "/backend"),
2143 return display_state
;
2146 void graphic_console_set_hwops(QemuConsole
*con
,
2147 const GraphicHwOps
*hw_ops
,
2150 con
->hw_ops
= hw_ops
;
2154 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
2155 const GraphicHwOps
*hw_ops
,
2158 static const char noinit
[] =
2159 "Guest has not initialized the display (yet).";
2164 DisplaySurface
*surface
;
2166 ds
= get_alloc_displaystate();
2167 s
= qemu_console_lookup_unused();
2169 trace_console_gfx_reuse(s
->index
);
2170 width
= qemu_console_get_width(s
, 0);
2171 height
= qemu_console_get_height(s
, 0);
2173 trace_console_gfx_new();
2174 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
2175 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2176 dpy_set_ui_info_timer
, s
);
2178 graphic_console_set_hwops(s
, hw_ops
, opaque
);
2180 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
2184 surface
= qemu_create_placeholder_surface(width
, height
, noinit
);
2185 dpy_gfx_replace_surface(s
, surface
);
2186 s
->gl_unblock_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
2187 graphic_hw_gl_unblock_timer
, s
);
2191 static const GraphicHwOps unused_ops
= {
2195 void graphic_console_close(QemuConsole
*con
)
2197 static const char unplugged
[] =
2198 "Guest display has been unplugged";
2199 DisplaySurface
*surface
;
2200 int width
= qemu_console_get_width(con
, 640);
2201 int height
= qemu_console_get_height(con
, 480);
2203 trace_console_gfx_close(con
->index
);
2204 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
2205 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
2208 dpy_gl_scanout_disable(con
);
2210 surface
= qemu_create_placeholder_surface(width
, height
, unplugged
);
2211 dpy_gfx_replace_surface(con
, surface
);
2214 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
2218 QTAILQ_FOREACH(con
, &consoles
, next
) {
2219 if (con
->index
== index
) {
2226 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
2232 QTAILQ_FOREACH(con
, &consoles
, next
) {
2233 obj
= object_property_get_link(OBJECT(con
),
2234 "device", &error_abort
);
2235 if (DEVICE(obj
) != dev
) {
2238 h
= object_property_get_uint(OBJECT(con
),
2239 "head", &error_abort
);
2248 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
2249 uint32_t head
, Error
**errp
)
2254 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2256 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2257 "Device '%s' not found", device_id
);
2261 con
= qemu_console_lookup_by_device(dev
, head
);
2263 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2271 QemuConsole
*qemu_console_lookup_unused(void)
2276 QTAILQ_FOREACH(con
, &consoles
, next
) {
2277 if (con
->hw_ops
!= &unused_ops
) {
2280 obj
= object_property_get_link(OBJECT(con
),
2281 "device", &error_abort
);
2290 bool qemu_console_is_visible(QemuConsole
*con
)
2292 return (con
== active_console
) || (con
->dcls
> 0);
2295 bool qemu_console_is_graphic(QemuConsole
*con
)
2298 con
= active_console
;
2300 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2303 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2306 con
= active_console
;
2308 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2311 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2313 assert(con
!= NULL
);
2314 return con
->gl_block
;
2317 bool qemu_console_is_multihead(DeviceState
*dev
)
2321 uint32_t f
= 0xffffffff;
2324 QTAILQ_FOREACH(con
, &consoles
, next
) {
2325 obj
= object_property_get_link(OBJECT(con
),
2326 "device", &error_abort
);
2327 if (DEVICE(obj
) != dev
) {
2331 h
= object_property_get_uint(OBJECT(con
),
2332 "head", &error_abort
);
2333 if (f
== 0xffffffff) {
2335 } else if (h
!= f
) {
2342 char *qemu_console_get_label(QemuConsole
*con
)
2344 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2349 dev
= DEVICE(con
->device
);
2350 multihead
= qemu_console_is_multihead(dev
);
2352 return g_strdup_printf("%s.%d", dev
->id
?
2354 object_get_typename(con
->device
),
2357 return g_strdup_printf("%s", dev
->id
?
2359 object_get_typename(con
->device
));
2362 return g_strdup("VGA");
2364 if (con
->chr
&& con
->chr
->label
) {
2365 return g_strdup(con
->chr
->label
);
2367 return g_strdup_printf("vc%d", con
->index
);
2371 int qemu_console_get_index(QemuConsole
*con
)
2374 con
= active_console
;
2376 return con
? con
->index
: -1;
2379 uint32_t qemu_console_get_head(QemuConsole
*con
)
2382 con
= active_console
;
2384 return con
? con
->head
: -1;
2387 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2390 con
= active_console
;
2395 switch (con
->scanout
.kind
) {
2396 case SCANOUT_DMABUF
:
2397 return con
->scanout
.dmabuf
->width
;
2398 case SCANOUT_TEXTURE
:
2399 return con
->scanout
.texture
.width
;
2400 case SCANOUT_SURFACE
:
2401 return surface_width(con
->surface
);
2407 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2410 con
= active_console
;
2415 switch (con
->scanout
.kind
) {
2416 case SCANOUT_DMABUF
:
2417 return con
->scanout
.dmabuf
->height
;
2418 case SCANOUT_TEXTURE
:
2419 return con
->scanout
.texture
.height
;
2420 case SCANOUT_SURFACE
:
2421 return surface_height(con
->surface
);
2427 static void vc_chr_accept_input(Chardev
*chr
)
2429 VCChardev
*drv
= VC_CHARDEV(chr
);
2430 QemuConsole
*s
= drv
->console
;
2435 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2437 VCChardev
*drv
= VC_CHARDEV(chr
);
2438 QemuConsole
*s
= drv
->console
;
2443 static void text_console_update_cursor_timer(void)
2445 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2446 + CONSOLE_CURSOR_PERIOD
/ 2);
2449 static void text_console_update_cursor(void *opaque
)
2454 cursor_visible_phase
= !cursor_visible_phase
;
2456 QTAILQ_FOREACH(s
, &consoles
, next
) {
2457 if (qemu_console_is_graphic(s
) ||
2458 !qemu_console_is_visible(s
)) {
2462 graphic_hw_invalidate(s
);
2466 text_console_update_cursor_timer();
2470 static const GraphicHwOps text_console_ops
= {
2471 .invalidate
= text_console_invalidate
,
2472 .text_update
= text_console_update
,
2475 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2477 VCChardev
*drv
= VC_CHARDEV(chr
);
2478 QemuConsole
*s
= drv
->console
;
2479 int g_width
= 80 * FONT_WIDTH
;
2480 int g_height
= 24 * FONT_HEIGHT
;
2482 fifo8_create(&s
->out_fifo
, 16);
2487 s
->total_height
= DEFAULT_BACKSCROLL
;
2490 if (s
->scanout
.kind
!= SCANOUT_SURFACE
) {
2491 if (active_console
&& active_console
->scanout
.kind
== SCANOUT_SURFACE
) {
2492 g_width
= qemu_console_get_width(active_console
, g_width
);
2493 g_height
= qemu_console_get_height(active_console
, g_height
);
2495 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2496 s
->scanout
.kind
= SCANOUT_SURFACE
;
2499 s
->hw_ops
= &text_console_ops
;
2502 /* Set text attribute defaults */
2503 s
->t_attrib_default
.bold
= 0;
2504 s
->t_attrib_default
.uline
= 0;
2505 s
->t_attrib_default
.blink
= 0;
2506 s
->t_attrib_default
.invers
= 0;
2507 s
->t_attrib_default
.unvisible
= 0;
2508 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2509 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2510 /* set current text attributes to default */
2511 s
->t_attrib
= s
->t_attrib_default
;
2512 text_console_resize(s
);
2517 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2518 msg
= g_strdup_printf("%s console\r\n", chr
->label
);
2519 vc_chr_write(chr
, (uint8_t *)msg
, strlen(msg
));
2521 s
->t_attrib
= s
->t_attrib_default
;
2524 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2527 static void vc_chr_open(Chardev
*chr
,
2528 ChardevBackend
*backend
,
2532 ChardevVC
*vc
= backend
->u
.vc
.data
;
2533 VCChardev
*drv
= VC_CHARDEV(chr
);
2536 unsigned height
= 0;
2538 if (vc
->has_width
) {
2540 } else if (vc
->has_cols
) {
2541 width
= vc
->cols
* FONT_WIDTH
;
2544 if (vc
->has_height
) {
2545 height
= vc
->height
;
2546 } else if (vc
->has_rows
) {
2547 height
= vc
->rows
* FONT_HEIGHT
;
2550 trace_console_txt_new(width
, height
);
2551 if (width
== 0 || height
== 0) {
2552 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2554 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2555 s
->scanout
.kind
= SCANOUT_SURFACE
;
2556 s
->surface
= qemu_create_displaysurface(width
, height
);
2560 error_setg(errp
, "cannot create text console");
2567 if (display_state
) {
2568 text_console_do_init(chr
, display_state
);
2571 /* console/chardev init sometimes completes elsewhere in a 2nd
2572 * stage, so defer OPENED events until they are fully initialized
2577 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2579 DisplaySurface
*surface
= qemu_console_surface(s
);
2581 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2583 if ((s
->scanout
.kind
!= SCANOUT_SURFACE
||
2584 (surface
&& surface
->flags
& QEMU_ALLOCATED_FLAG
)) &&
2585 qemu_console_get_width(s
, -1) == width
&&
2586 qemu_console_get_height(s
, -1) == height
) {
2590 surface
= qemu_create_displaysurface(width
, height
);
2591 dpy_gfx_replace_surface(s
, surface
);
2594 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2596 switch (console
->scanout
.kind
) {
2597 case SCANOUT_SURFACE
:
2598 return console
->surface
;
2604 PixelFormat
qemu_default_pixelformat(int bpp
)
2606 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2607 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2611 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2613 void qemu_display_register(QemuDisplay
*ui
)
2615 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2616 dpys
[ui
->type
] = ui
;
2619 bool qemu_display_find_default(DisplayOptions
*opts
)
2621 static DisplayType prio
[] = {
2622 #if defined(CONFIG_GTK)
2625 #if defined(CONFIG_SDL)
2628 #if defined(CONFIG_COCOA)
2634 for (i
= 0; i
< (int)ARRAY_SIZE(prio
); i
++) {
2635 if (dpys
[prio
[i
]] == NULL
) {
2636 Error
*local_err
= NULL
;
2637 int rv
= ui_module_load(DisplayType_str(prio
[i
]), &local_err
);
2639 error_report_err(local_err
);
2642 if (dpys
[prio
[i
]] == NULL
) {
2645 opts
->type
= prio
[i
];
2651 void qemu_display_early_init(DisplayOptions
*opts
)
2653 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2654 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2657 if (dpys
[opts
->type
] == NULL
) {
2658 Error
*local_err
= NULL
;
2659 int rv
= ui_module_load(DisplayType_str(opts
->type
), &local_err
);
2661 error_report_err(local_err
);
2664 if (dpys
[opts
->type
] == NULL
) {
2665 error_report("Display '%s' is not available.",
2666 DisplayType_str(opts
->type
));
2669 if (dpys
[opts
->type
]->early_init
) {
2670 dpys
[opts
->type
]->early_init(opts
);
2674 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2676 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2677 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2680 assert(dpys
[opts
->type
] != NULL
);
2681 dpys
[opts
->type
]->init(ds
, opts
);
2684 void qemu_display_help(void)
2688 printf("Available display backend types:\n");
2690 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2692 Error
*local_err
= NULL
;
2693 int rv
= ui_module_load(DisplayType_str(idx
), &local_err
);
2695 error_report_err(local_err
);
2699 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2704 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2709 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2710 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2711 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2713 val
= qemu_opt_get_number(opts
, "width", 0);
2715 vc
->has_width
= true;
2719 val
= qemu_opt_get_number(opts
, "height", 0);
2721 vc
->has_height
= true;
2725 val
= qemu_opt_get_number(opts
, "cols", 0);
2727 vc
->has_cols
= true;
2731 val
= qemu_opt_get_number(opts
, "rows", 0);
2733 vc
->has_rows
= true;
2738 static const TypeInfo qemu_console_info
= {
2739 .name
= TYPE_QEMU_CONSOLE
,
2740 .parent
= TYPE_OBJECT
,
2741 .instance_size
= sizeof(QemuConsole
),
2742 .class_size
= sizeof(QemuConsoleClass
),
2745 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2747 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2749 cc
->parse
= qemu_chr_parse_vc
;
2750 cc
->open
= vc_chr_open
;
2751 cc
->chr_write
= vc_chr_write
;
2752 cc
->chr_accept_input
= vc_chr_accept_input
;
2753 cc
->chr_set_echo
= vc_chr_set_echo
;
2756 static const TypeInfo char_vc_type_info
= {
2757 .name
= TYPE_CHARDEV_VC
,
2758 .parent
= TYPE_CHARDEV
,
2759 .instance_size
= sizeof(VCChardev
),
2760 .class_init
= char_vc_class_init
,
2763 void qemu_console_early_init(void)
2765 /* set the default vc driver */
2766 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2767 type_register(&char_vc_type_info
);
2771 static void register_types(void)
2773 type_register_static(&qemu_console_info
);
2776 type_init(register_types
);