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 "qapi/visitor.h"
31 #include "qemu/coroutine.h"
32 #include "qemu/error-report.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/module.h"
35 #include "qemu/option.h"
36 #include "chardev/char.h"
38 #include "exec/memory.h"
39 #include "qom/object.h"
40 #include "qemu/memfd.h"
42 #include "console-priv.h"
44 OBJECT_DEFINE_ABSTRACT_TYPE(QemuConsole
, qemu_console
, QEMU_CONSOLE
, OBJECT
)
46 typedef struct QemuGraphicConsole
{
53 int cursor_x
, cursor_y
;
57 typedef QemuConsoleClass QemuGraphicConsoleClass
;
59 OBJECT_DEFINE_TYPE(QemuGraphicConsole
, qemu_graphic_console
, QEMU_GRAPHIC_CONSOLE
, QEMU_CONSOLE
)
64 uint64_t update_interval
;
67 QLIST_HEAD(, DisplayChangeListener
) listeners
;
70 static DisplayState
*display_state
;
71 static QTAILQ_HEAD(, QemuConsole
) consoles
=
72 QTAILQ_HEAD_INITIALIZER(consoles
);
74 static void dpy_refresh(DisplayState
*s
);
75 static DisplayState
*get_alloc_displaystate(void);
76 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
);
77 static bool console_compatible_with(QemuConsole
*con
,
78 DisplayChangeListener
*dcl
, Error
**errp
);
79 static QemuConsole
*qemu_graphic_console_lookup_unused(void);
80 static void dpy_set_ui_info_timer(void *opaque
);
82 static void gui_update(void *opaque
)
84 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
85 uint64_t dcl_interval
;
86 DisplayState
*ds
= opaque
;
87 DisplayChangeListener
*dcl
;
89 ds
->refreshing
= true;
91 ds
->refreshing
= false;
93 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
94 dcl_interval
= dcl
->update_interval
?
95 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
96 if (interval
> dcl_interval
) {
97 interval
= dcl_interval
;
100 if (ds
->update_interval
!= interval
) {
101 ds
->update_interval
= interval
;
102 trace_console_refresh(interval
);
104 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
105 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
108 static void gui_setup_refresh(DisplayState
*ds
)
110 DisplayChangeListener
*dcl
;
111 bool need_timer
= false;
113 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
114 if (dcl
->ops
->dpy_refresh
!= NULL
) {
119 if (need_timer
&& ds
->gui_timer
== NULL
) {
120 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
121 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
123 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
124 timer_free(ds
->gui_timer
);
125 ds
->gui_timer
= NULL
;
129 void graphic_hw_update_done(QemuConsole
*con
)
132 qemu_co_enter_all(&con
->dump_queue
, NULL
);
136 void graphic_hw_update(QemuConsole
*con
)
142 if (con
->hw_ops
->gfx_update
) {
143 con
->hw_ops
->gfx_update(con
->hw
);
144 async
= con
->hw_ops
->gfx_update_async
;
147 graphic_hw_update_done(con
);
151 static void graphic_hw_update_bh(void *con
)
153 graphic_hw_update(con
);
156 void qemu_console_co_wait_update(QemuConsole
*con
)
158 if (qemu_co_queue_empty(&con
->dump_queue
)) {
159 /* Defer the update, it will restart the pending coroutines */
160 aio_bh_schedule_oneshot(qemu_get_aio_context(),
161 graphic_hw_update_bh
, con
);
163 qemu_co_queue_wait(&con
->dump_queue
, NULL
);
167 static void graphic_hw_gl_unblock_timer(void *opaque
)
169 warn_report("console: no gl-unblock within one second");
172 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
182 assert(con
->gl_block
>= 0);
183 if (!con
->hw_ops
->gl_block
) {
186 if ((block
&& con
->gl_block
!= 1) || (!block
&& con
->gl_block
!= 0)) {
189 con
->hw_ops
->gl_block(con
->hw
, block
);
192 timeout
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
193 timeout
+= 1000; /* one sec */
194 timer_mod(con
->gl_unblock_timer
, timeout
);
196 timer_del(con
->gl_unblock_timer
);
200 int qemu_console_get_window_id(QemuConsole
*con
)
202 return con
->window_id
;
205 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
207 con
->window_id
= window_id
;
210 void graphic_hw_invalidate(QemuConsole
*con
)
212 if (con
&& con
->hw_ops
->invalidate
) {
213 con
->hw_ops
->invalidate(con
->hw
);
217 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
219 if (con
&& con
->hw_ops
->text_update
) {
220 con
->hw_ops
->text_update(con
->hw
, chardata
);
224 static void displaychangelistener_gfx_switch(DisplayChangeListener
*dcl
,
225 struct DisplaySurface
*new_surface
,
228 if (dcl
->ops
->dpy_gfx_switch
) {
229 dcl
->ops
->dpy_gfx_switch(dcl
, new_surface
);
232 if (update
&& dcl
->ops
->dpy_gfx_update
) {
233 dcl
->ops
->dpy_gfx_update(dcl
, 0, 0,
234 surface_width(new_surface
),
235 surface_height(new_surface
));
239 static void dpy_gfx_create_texture(QemuConsole
*con
, DisplaySurface
*surface
)
241 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_create_texture
) {
242 con
->gl
->ops
->dpy_gl_ctx_create_texture(con
->gl
, surface
);
246 static void dpy_gfx_destroy_texture(QemuConsole
*con
, DisplaySurface
*surface
)
248 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_destroy_texture
) {
249 con
->gl
->ops
->dpy_gl_ctx_destroy_texture(con
->gl
, surface
);
253 static void dpy_gfx_update_texture(QemuConsole
*con
, DisplaySurface
*surface
,
254 int x
, int y
, int w
, int h
)
256 if (con
->gl
&& con
->gl
->ops
->dpy_gl_ctx_update_texture
) {
257 con
->gl
->ops
->dpy_gl_ctx_update_texture(con
->gl
, surface
, x
, y
, w
, h
);
261 static void displaychangelistener_display_console(DisplayChangeListener
*dcl
,
264 static const char nodev
[] =
265 "This VM has no graphic display device.";
266 static DisplaySurface
*dummy
;
267 QemuConsole
*con
= dcl
->con
;
269 if (!con
|| !console_compatible_with(con
, dcl
, errp
)) {
271 dummy
= qemu_create_placeholder_surface(640, 480, nodev
);
274 dpy_gfx_create_texture(con
, dummy
);
276 displaychangelistener_gfx_switch(dcl
, dummy
, TRUE
);
280 dpy_gfx_create_texture(con
, con
->surface
);
281 displaychangelistener_gfx_switch(dcl
, con
->surface
,
282 con
->scanout
.kind
== SCANOUT_SURFACE
);
284 if (con
->scanout
.kind
== SCANOUT_DMABUF
&&
285 displaychangelistener_has_dmabuf(dcl
)) {
286 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, con
->scanout
.dmabuf
);
287 } else if (con
->scanout
.kind
== SCANOUT_TEXTURE
&&
288 dcl
->ops
->dpy_gl_scanout_texture
) {
289 dcl
->ops
->dpy_gl_scanout_texture(dcl
,
290 con
->scanout
.texture
.backing_id
,
291 con
->scanout
.texture
.backing_y_0_top
,
292 con
->scanout
.texture
.backing_width
,
293 con
->scanout
.texture
.backing_height
,
294 con
->scanout
.texture
.x
,
295 con
->scanout
.texture
.y
,
296 con
->scanout
.texture
.width
,
297 con
->scanout
.texture
.height
,
298 con
->scanout
.texture
.d3d_tex2d
);
302 void qemu_text_console_put_keysym(QemuTextConsole
*s
, int keysym
)
304 qemu_text_console_handle_keysym(s
, keysym
);
307 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
308 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
309 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
310 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
311 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
312 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
313 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
314 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
315 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
316 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
317 [Q_KEY_CODE_TAB
] = QEMU_KEY_TAB
,
318 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
321 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
322 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
323 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
324 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
325 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
326 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
327 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
328 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
329 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
332 bool qemu_text_console_put_qcode(QemuTextConsole
*s
, int qcode
, bool ctrl
)
336 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
340 qemu_text_console_put_keysym(s
, keysym
);
344 void qemu_text_console_put_string(QemuTextConsole
*s
, const char *str
, int len
)
348 for (i
= 0; i
< len
&& str
[i
]; i
++) {
349 qemu_text_console_put_keysym(s
, str
[i
]);
354 qemu_console_register(QemuConsole
*c
)
358 if (QTAILQ_EMPTY(&consoles
)) {
360 QTAILQ_INSERT_TAIL(&consoles
, c
, next
);
361 } else if (!QEMU_IS_GRAPHIC_CONSOLE(c
) || phase_check(PHASE_MACHINE_READY
)) {
362 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
363 c
->index
= last
->index
+ 1;
364 QTAILQ_INSERT_TAIL(&consoles
, c
, next
);
367 * HACK: Put graphical consoles before text consoles.
369 * Only do that for coldplugged devices. After initial device
370 * initialization we will not renumber the consoles any more.
372 QemuConsole
*it
= QTAILQ_FIRST(&consoles
);
374 while (QTAILQ_NEXT(it
, next
) != NULL
&& QEMU_IS_GRAPHIC_CONSOLE(it
)) {
375 it
= QTAILQ_NEXT(it
, next
);
377 if (QEMU_IS_GRAPHIC_CONSOLE(it
)) {
378 /* have no text consoles */
379 c
->index
= it
->index
+ 1;
380 QTAILQ_INSERT_AFTER(&consoles
, it
, c
, next
);
382 c
->index
= it
->index
;
383 QTAILQ_INSERT_BEFORE(it
, c
, next
);
384 /* renumber text consoles */
385 for (i
= c
->index
+ 1; it
!= NULL
; it
= QTAILQ_NEXT(it
, next
), i
++) {
393 qemu_console_finalize(Object
*obj
)
395 QemuConsole
*c
= QEMU_CONSOLE(obj
);
397 /* TODO: check this code path, and unregister from consoles */
398 g_clear_pointer(&c
->surface
, qemu_free_displaysurface
);
399 g_clear_pointer(&c
->gl_unblock_timer
, timer_free
);
400 g_clear_pointer(&c
->ui_timer
, timer_free
);
404 qemu_console_class_init(ObjectClass
*oc
, void *data
)
409 qemu_console_init(Object
*obj
)
411 QemuConsole
*c
= QEMU_CONSOLE(obj
);
412 DisplayState
*ds
= get_alloc_displaystate();
414 qemu_co_queue_init(&c
->dump_queue
);
417 c
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
418 dpy_set_ui_info_timer
, c
);
419 qemu_console_register(c
);
423 qemu_graphic_console_finalize(Object
*obj
)
425 QemuGraphicConsole
*c
= QEMU_GRAPHIC_CONSOLE(obj
);
427 g_clear_pointer(&c
->device
, object_unref
);
431 qemu_graphic_console_prop_get_head(Object
*obj
, Visitor
*v
, const char *name
,
432 void *opaque
, Error
**errp
)
434 QemuGraphicConsole
*c
= QEMU_GRAPHIC_CONSOLE(obj
);
436 visit_type_uint32(v
, name
, &c
->head
, errp
);
440 qemu_graphic_console_class_init(ObjectClass
*oc
, void *data
)
442 object_class_property_add_link(oc
, "device", TYPE_DEVICE
,
443 offsetof(QemuGraphicConsole
, device
),
444 object_property_allow_set_link
,
445 OBJ_PROP_LINK_STRONG
);
446 object_class_property_add(oc
, "head", "uint32",
447 qemu_graphic_console_prop_get_head
,
452 qemu_graphic_console_init(Object
*obj
)
456 void qemu_displaysurface_set_share_handle(DisplaySurface
*surface
,
457 qemu_pixman_shareable handle
,
460 assert(surface
->share_handle
== SHAREABLE_NONE
);
462 surface
->share_handle
= handle
;
463 surface
->share_handle_offset
= offset
;
467 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
469 trace_displaysurface_create(width
, height
);
471 return qemu_create_displaysurface_from(
478 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
479 pixman_format_code_t format
,
480 int linesize
, uint8_t *data
)
482 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
484 trace_displaysurface_create_from(surface
, width
, height
, format
);
485 surface
->share_handle
= SHAREABLE_NONE
;
488 surface
->image
= pixman_image_create_bits(format
,
490 (void *)data
, linesize
);
492 qemu_pixman_image_new_shareable(&surface
->image
,
493 &surface
->share_handle
,
500 surface
->flags
= QEMU_ALLOCATED_FLAG
;
503 assert(surface
->image
!= NULL
);
507 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
509 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
511 trace_displaysurface_create_pixman(surface
);
512 surface
->share_handle
= SHAREABLE_NONE
;
513 surface
->image
= pixman_image_ref(image
);
518 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
521 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
523 pixman_color_t bg
= QEMU_PIXMAN_COLOR_BLACK
;
524 pixman_color_t fg
= QEMU_PIXMAN_COLOR_GRAY
;
525 pixman_image_t
*glyph
;
529 x
= (w
/ FONT_WIDTH
- len
) / 2;
530 y
= (h
/ FONT_HEIGHT
- 1) / 2;
531 for (i
= 0; i
< len
; i
++) {
532 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
533 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
534 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
535 qemu_pixman_image_unref(glyph
);
538 surface
->flags
|= QEMU_PLACEHOLDER_FLAG
;
542 void qemu_free_displaysurface(DisplaySurface
*surface
)
544 if (surface
== NULL
) {
547 trace_displaysurface_free(surface
);
548 qemu_pixman_image_unref(surface
->image
);
552 bool console_has_gl(QemuConsole
*con
)
554 return con
->gl
!= NULL
;
557 static bool displaychangelistener_has_dmabuf(DisplayChangeListener
*dcl
)
559 if (dcl
->ops
->dpy_has_dmabuf
) {
560 return dcl
->ops
->dpy_has_dmabuf(dcl
);
563 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
570 static bool console_compatible_with(QemuConsole
*con
,
571 DisplayChangeListener
*dcl
, Error
**errp
)
575 flags
= con
->hw_ops
->get_flags
? con
->hw_ops
->get_flags(con
->hw
) : 0;
577 if (console_has_gl(con
) &&
578 !con
->gl
->ops
->dpy_gl_ctx_is_compatible_dcl(con
->gl
, dcl
)) {
579 error_setg(errp
, "Display %s is incompatible with the GL context",
584 if (flags
& GRAPHIC_FLAGS_GL
&&
585 !console_has_gl(con
)) {
586 error_setg(errp
, "The console requires a GL context.");
591 if (flags
& GRAPHIC_FLAGS_DMABUF
&&
592 !displaychangelistener_has_dmabuf(dcl
)) {
593 error_setg(errp
, "The console requires display DMABUF support.");
600 void console_handle_touch_event(QemuConsole
*con
,
601 struct touch_slot touch_slots
[INPUT_EVENT_SLOTS_MAX
],
603 int width
, int height
,
605 InputMultiTouchType type
,
608 struct touch_slot
*slot
;
609 bool needs_sync
= false;
613 if (num_slot
>= INPUT_EVENT_SLOTS_MAX
) {
615 "Unexpected touch slot number: % " PRId64
" >= %d",
616 num_slot
, INPUT_EVENT_SLOTS_MAX
);
620 slot
= &touch_slots
[num_slot
];
624 if (type
== INPUT_MULTI_TOUCH_TYPE_BEGIN
) {
625 slot
->tracking_id
= num_slot
;
628 for (i
= 0; i
< INPUT_EVENT_SLOTS_MAX
; ++i
) {
632 update
= INPUT_MULTI_TOUCH_TYPE_UPDATE
;
635 slot
= &touch_slots
[i
];
637 if (slot
->tracking_id
== -1) {
641 if (update
== INPUT_MULTI_TOUCH_TYPE_END
) {
642 slot
->tracking_id
= -1;
643 qemu_input_queue_mtt(con
, update
, i
, slot
->tracking_id
);
646 qemu_input_queue_mtt(con
, update
, i
, slot
->tracking_id
);
647 qemu_input_queue_btn(con
, INPUT_BUTTON_TOUCH
, true);
648 qemu_input_queue_mtt_abs(con
,
649 INPUT_AXIS_X
, (int) slot
->x
,
651 i
, slot
->tracking_id
);
652 qemu_input_queue_mtt_abs(con
,
653 INPUT_AXIS_Y
, (int) slot
->y
,
655 i
, slot
->tracking_id
);
661 qemu_input_event_sync();
665 void qemu_console_set_display_gl_ctx(QemuConsole
*con
, DisplayGLCtx
*gl
)
667 /* display has opengl support */
670 error_report("The console already has an OpenGL context.");
677 dcl_set_graphic_cursor(DisplayChangeListener
*dcl
, QemuGraphicConsole
*con
)
679 if (con
&& con
->cursor
&& dcl
->ops
->dpy_cursor_define
) {
680 dcl
->ops
->dpy_cursor_define(dcl
, con
->cursor
);
682 if (con
&& dcl
->ops
->dpy_mouse_set
) {
683 dcl
->ops
->dpy_mouse_set(dcl
, con
->cursor_x
, con
->cursor_y
, con
->cursor_on
);
687 void register_displaychangelistener(DisplayChangeListener
*dcl
)
691 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
692 dcl
->ds
= get_alloc_displaystate();
693 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
694 gui_setup_refresh(dcl
->ds
);
698 displaychangelistener_display_console(dcl
, &error_fatal
);
699 if (QEMU_IS_GRAPHIC_CONSOLE(dcl
->con
)) {
700 dcl_set_graphic_cursor(dcl
, QEMU_GRAPHIC_CONSOLE(dcl
->con
));
701 } else if (QEMU_IS_TEXT_CONSOLE(dcl
->con
)) {
702 qemu_text_console_update_size(QEMU_TEXT_CONSOLE(dcl
->con
));
704 qemu_text_console_update_cursor();
707 void update_displaychangelistener(DisplayChangeListener
*dcl
,
710 DisplayState
*ds
= dcl
->ds
;
712 dcl
->update_interval
= interval
;
713 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
714 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
718 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
720 DisplayState
*ds
= dcl
->ds
;
721 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
725 QLIST_REMOVE(dcl
, next
);
727 gui_setup_refresh(ds
);
730 static void dpy_set_ui_info_timer(void *opaque
)
732 QemuConsole
*con
= opaque
;
733 uint32_t head
= qemu_console_get_head(con
);
735 con
->hw_ops
->ui_info(con
->hw
, head
, &con
->ui_info
);
738 bool dpy_ui_info_supported(const QemuConsole
*con
)
744 return con
->hw_ops
->ui_info
!= NULL
;
747 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
)
749 assert(dpy_ui_info_supported(con
));
751 return &con
->ui_info
;
754 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
, bool delay
)
756 if (!dpy_ui_info_supported(con
)) {
759 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
760 /* nothing changed -- ignore */
765 * Typically we get a flood of these as the user resizes the window.
766 * Wait until the dust has settled (one second without updates), then
767 * go notify the guest.
769 con
->ui_info
= *info
;
770 timer_mod(con
->ui_timer
,
771 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + (delay
? 1000 : 0));
775 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
777 DisplayState
*s
= con
->ds
;
778 DisplayChangeListener
*dcl
;
779 int width
= qemu_console_get_width(con
, x
+ w
);
780 int height
= qemu_console_get_height(con
, y
+ h
);
786 w
= MIN(w
, width
- x
);
787 h
= MIN(h
, height
- y
);
789 if (!qemu_console_is_visible(con
)) {
792 dpy_gfx_update_texture(con
, con
->surface
, x
, y
, w
, h
);
793 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
794 if (con
!= dcl
->con
) {
797 if (dcl
->ops
->dpy_gfx_update
) {
798 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
803 void dpy_gfx_update_full(QemuConsole
*con
)
805 int w
= qemu_console_get_width(con
, 0);
806 int h
= qemu_console_get_height(con
, 0);
808 dpy_gfx_update(con
, 0, 0, w
, h
);
811 void dpy_gfx_replace_surface(QemuConsole
*con
,
812 DisplaySurface
*surface
)
814 static const char placeholder_msg
[] = "Display output is not active.";
815 DisplayState
*s
= con
->ds
;
816 DisplaySurface
*old_surface
= con
->surface
;
817 DisplaySurface
*new_surface
= surface
;
818 DisplayChangeListener
*dcl
;
824 width
= surface_width(old_surface
);
825 height
= surface_height(old_surface
);
831 new_surface
= qemu_create_placeholder_surface(width
, height
, placeholder_msg
);
834 assert(old_surface
!= new_surface
);
836 con
->scanout
.kind
= SCANOUT_SURFACE
;
837 con
->surface
= new_surface
;
838 dpy_gfx_create_texture(con
, new_surface
);
839 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
840 if (con
!= dcl
->con
) {
843 displaychangelistener_gfx_switch(dcl
, new_surface
, surface
? FALSE
: TRUE
);
845 dpy_gfx_destroy_texture(con
, old_surface
);
846 qemu_free_displaysurface(old_surface
);
849 bool dpy_gfx_check_format(QemuConsole
*con
,
850 pixman_format_code_t format
)
852 DisplayChangeListener
*dcl
;
853 DisplayState
*s
= con
->ds
;
855 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
856 if (dcl
->con
&& dcl
->con
!= con
) {
857 /* dcl bound to another console -> skip */
860 if (dcl
->ops
->dpy_gfx_check_format
) {
861 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
865 /* default is to allow native 32 bpp only */
866 if (format
!= qemu_default_pixman_format(32, true)) {
874 static void dpy_refresh(DisplayState
*s
)
876 DisplayChangeListener
*dcl
;
878 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
879 if (dcl
->ops
->dpy_refresh
) {
880 dcl
->ops
->dpy_refresh(dcl
);
885 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
887 DisplayState
*s
= con
->ds
;
888 DisplayChangeListener
*dcl
;
890 if (!qemu_console_is_visible(con
)) {
893 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
894 if (con
!= dcl
->con
) {
897 if (dcl
->ops
->dpy_text_cursor
) {
898 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
903 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
905 DisplayState
*s
= con
->ds
;
906 DisplayChangeListener
*dcl
;
908 if (!qemu_console_is_visible(con
)) {
911 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
912 if (con
!= dcl
->con
) {
915 if (dcl
->ops
->dpy_text_update
) {
916 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
921 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
923 DisplayState
*s
= con
->ds
;
924 DisplayChangeListener
*dcl
;
926 if (!qemu_console_is_visible(con
)) {
929 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
930 if (con
!= dcl
->con
) {
933 if (dcl
->ops
->dpy_text_resize
) {
934 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
939 void dpy_mouse_set(QemuConsole
*c
, int x
, int y
, bool on
)
941 QemuGraphicConsole
*con
= QEMU_GRAPHIC_CONSOLE(c
);
942 DisplayState
*s
= c
->ds
;
943 DisplayChangeListener
*dcl
;
948 if (!qemu_console_is_visible(c
)) {
951 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
955 if (dcl
->ops
->dpy_mouse_set
) {
956 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
961 void dpy_cursor_define(QemuConsole
*c
, QEMUCursor
*cursor
)
963 QemuGraphicConsole
*con
= QEMU_GRAPHIC_CONSOLE(c
);
964 DisplayState
*s
= c
->ds
;
965 DisplayChangeListener
*dcl
;
967 cursor_unref(con
->cursor
);
968 con
->cursor
= cursor_ref(cursor
);
969 if (!qemu_console_is_visible(c
)) {
972 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
976 if (dcl
->ops
->dpy_cursor_define
) {
977 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
982 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
983 struct QEMUGLParams
*qparams
)
986 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
989 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
992 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
995 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
998 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1001 void dpy_gl_scanout_disable(QemuConsole
*con
)
1003 DisplayState
*s
= con
->ds
;
1004 DisplayChangeListener
*dcl
;
1006 if (con
->scanout
.kind
!= SCANOUT_SURFACE
) {
1007 con
->scanout
.kind
= SCANOUT_NONE
;
1009 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1010 if (con
!= dcl
->con
) {
1013 if (dcl
->ops
->dpy_gl_scanout_disable
) {
1014 dcl
->ops
->dpy_gl_scanout_disable(dcl
);
1019 void dpy_gl_scanout_texture(QemuConsole
*con
,
1020 uint32_t backing_id
,
1021 bool backing_y_0_top
,
1022 uint32_t backing_width
,
1023 uint32_t backing_height
,
1024 uint32_t x
, uint32_t y
,
1025 uint32_t width
, uint32_t height
,
1028 DisplayState
*s
= con
->ds
;
1029 DisplayChangeListener
*dcl
;
1031 con
->scanout
.kind
= SCANOUT_TEXTURE
;
1032 con
->scanout
.texture
= (ScanoutTexture
) {
1033 backing_id
, backing_y_0_top
, backing_width
, backing_height
,
1034 x
, y
, width
, height
, d3d_tex2d
,
1036 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1037 if (con
!= dcl
->con
) {
1040 if (dcl
->ops
->dpy_gl_scanout_texture
) {
1041 dcl
->ops
->dpy_gl_scanout_texture(dcl
, backing_id
,
1043 backing_width
, backing_height
,
1044 x
, y
, width
, height
,
1050 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1053 DisplayState
*s
= con
->ds
;
1054 DisplayChangeListener
*dcl
;
1056 con
->scanout
.kind
= SCANOUT_DMABUF
;
1057 con
->scanout
.dmabuf
= dmabuf
;
1058 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1059 if (con
!= dcl
->con
) {
1062 if (dcl
->ops
->dpy_gl_scanout_dmabuf
) {
1063 dcl
->ops
->dpy_gl_scanout_dmabuf(dcl
, dmabuf
);
1068 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1069 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1071 DisplayState
*s
= con
->ds
;
1072 DisplayChangeListener
*dcl
;
1074 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1075 if (con
!= dcl
->con
) {
1078 if (dcl
->ops
->dpy_gl_cursor_dmabuf
) {
1079 dcl
->ops
->dpy_gl_cursor_dmabuf(dcl
, dmabuf
,
1080 have_hot
, hot_x
, hot_y
);
1085 void dpy_gl_cursor_position(QemuConsole
*con
,
1086 uint32_t pos_x
, uint32_t pos_y
)
1088 DisplayState
*s
= con
->ds
;
1089 DisplayChangeListener
*dcl
;
1091 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1092 if (con
!= dcl
->con
) {
1095 if (dcl
->ops
->dpy_gl_cursor_position
) {
1096 dcl
->ops
->dpy_gl_cursor_position(dcl
, pos_x
, pos_y
);
1101 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1104 DisplayState
*s
= con
->ds
;
1105 DisplayChangeListener
*dcl
;
1107 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1108 if (con
!= dcl
->con
) {
1111 if (dcl
->ops
->dpy_gl_release_dmabuf
) {
1112 dcl
->ops
->dpy_gl_release_dmabuf(dcl
, dmabuf
);
1117 void dpy_gl_update(QemuConsole
*con
,
1118 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1120 DisplayState
*s
= con
->ds
;
1121 DisplayChangeListener
*dcl
;
1125 graphic_hw_gl_block(con
, true);
1126 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1127 if (con
!= dcl
->con
) {
1130 if (dcl
->ops
->dpy_gl_update
) {
1131 dcl
->ops
->dpy_gl_update(dcl
, x
, y
, w
, h
);
1134 graphic_hw_gl_block(con
, false);
1137 /***********************************************************/
1138 /* register display */
1140 /* console.c internal use only */
1141 static DisplayState
*get_alloc_displaystate(void)
1143 if (!display_state
) {
1144 display_state
= g_new0(DisplayState
, 1);
1146 return display_state
;
1150 * Called by main(), after creating QemuConsoles
1151 * and before initializing ui (sdl/vnc/...).
1153 DisplayState
*init_displaystate(void)
1158 QTAILQ_FOREACH(con
, &consoles
, next
) {
1159 /* Hook up into the qom tree here (not in object_new()), once
1160 * all QemuConsoles are created and the order / numbering
1161 * doesn't change any more */
1162 name
= g_strdup_printf("console[%d]", con
->index
);
1163 object_property_add_child(container_get(object_get_root(), "/backend"),
1168 return display_state
;
1171 void graphic_console_set_hwops(QemuConsole
*con
,
1172 const GraphicHwOps
*hw_ops
,
1175 con
->hw_ops
= hw_ops
;
1179 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1180 const GraphicHwOps
*hw_ops
,
1183 static const char noinit
[] =
1184 "Guest has not initialized the display (yet).";
1188 DisplaySurface
*surface
;
1190 s
= qemu_graphic_console_lookup_unused();
1192 trace_console_gfx_reuse(s
->index
);
1193 width
= qemu_console_get_width(s
, 0);
1194 height
= qemu_console_get_height(s
, 0);
1196 trace_console_gfx_new();
1197 s
= (QemuConsole
*)object_new(TYPE_QEMU_GRAPHIC_CONSOLE
);
1199 QEMU_GRAPHIC_CONSOLE(s
)->head
= head
;
1200 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1202 object_property_set_link(OBJECT(s
), "device", OBJECT(dev
),
1206 surface
= qemu_create_placeholder_surface(width
, height
, noinit
);
1207 dpy_gfx_replace_surface(s
, surface
);
1208 s
->gl_unblock_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1209 graphic_hw_gl_unblock_timer
, s
);
1213 static const GraphicHwOps unused_ops
= {
1217 void graphic_console_close(QemuConsole
*con
)
1219 static const char unplugged
[] =
1220 "Guest display has been unplugged";
1221 DisplaySurface
*surface
;
1222 int width
= qemu_console_get_width(con
, 640);
1223 int height
= qemu_console_get_height(con
, 480);
1225 trace_console_gfx_close(con
->index
);
1226 object_property_set_link(OBJECT(con
), "device", NULL
, &error_abort
);
1227 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1230 dpy_gl_scanout_disable(con
);
1232 surface
= qemu_create_placeholder_surface(width
, height
, unplugged
);
1233 dpy_gfx_replace_surface(con
, surface
);
1236 QemuConsole
*qemu_console_lookup_default(void)
1240 QTAILQ_FOREACH(con
, &consoles
, next
) {
1241 if (QEMU_IS_GRAPHIC_CONSOLE(con
)) {
1245 return QTAILQ_FIRST(&consoles
);
1248 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1252 QTAILQ_FOREACH(con
, &consoles
, next
) {
1253 if (con
->index
== index
) {
1260 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1266 QTAILQ_FOREACH(con
, &consoles
, next
) {
1267 obj
= object_property_get_link(OBJECT(con
),
1268 "device", &error_abort
);
1269 if (DEVICE(obj
) != dev
) {
1272 h
= object_property_get_uint(OBJECT(con
),
1273 "head", &error_abort
);
1282 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1283 uint32_t head
, Error
**errp
)
1288 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1290 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1291 "Device '%s' not found", device_id
);
1295 con
= qemu_console_lookup_by_device(dev
, head
);
1297 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1305 static QemuConsole
*qemu_graphic_console_lookup_unused(void)
1310 QTAILQ_FOREACH(con
, &consoles
, next
) {
1311 if (!QEMU_IS_GRAPHIC_CONSOLE(con
) || con
->hw_ops
!= &unused_ops
) {
1314 obj
= object_property_get_link(OBJECT(con
),
1315 "device", &error_abort
);
1324 QEMUCursor
*qemu_console_get_cursor(QemuConsole
*con
)
1326 return QEMU_IS_GRAPHIC_CONSOLE(con
) ? QEMU_GRAPHIC_CONSOLE(con
)->cursor
: NULL
;
1329 bool qemu_console_is_visible(QemuConsole
*con
)
1331 return con
->dcls
> 0;
1334 bool qemu_console_is_graphic(QemuConsole
*con
)
1336 return con
&& QEMU_IS_GRAPHIC_CONSOLE(con
);
1339 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1341 return con
&& (QEMU_IS_GRAPHIC_CONSOLE(con
) || QEMU_IS_FIXED_TEXT_CONSOLE(con
));
1344 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
1346 assert(con
!= NULL
);
1347 return con
->gl_block
;
1350 static bool qemu_graphic_console_is_multihead(QemuGraphicConsole
*c
)
1354 QTAILQ_FOREACH(con
, &consoles
, next
) {
1355 QemuGraphicConsole
*candidate
;
1357 if (!QEMU_IS_GRAPHIC_CONSOLE(con
)) {
1361 candidate
= QEMU_GRAPHIC_CONSOLE(con
);
1362 if (candidate
->device
!= c
->device
) {
1366 if (candidate
->head
!= c
->head
) {
1373 char *qemu_console_get_label(QemuConsole
*con
)
1375 if (QEMU_IS_GRAPHIC_CONSOLE(con
)) {
1376 QemuGraphicConsole
*c
= QEMU_GRAPHIC_CONSOLE(con
);
1381 dev
= DEVICE(c
->device
);
1382 multihead
= qemu_graphic_console_is_multihead(c
);
1384 return g_strdup_printf("%s.%d", dev
->id
?
1386 object_get_typename(c
->device
),
1389 return g_strdup_printf("%s", dev
->id
?
1391 object_get_typename(c
->device
));
1394 return g_strdup("VGA");
1395 } else if (QEMU_IS_TEXT_CONSOLE(con
)) {
1396 const char *label
= qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con
));
1398 return g_strdup(label
);
1402 return g_strdup_printf("vc%d", con
->index
);
1405 int qemu_console_get_index(QemuConsole
*con
)
1407 return con
? con
->index
: -1;
1410 uint32_t qemu_console_get_head(QemuConsole
*con
)
1415 if (QEMU_IS_GRAPHIC_CONSOLE(con
)) {
1416 return QEMU_GRAPHIC_CONSOLE(con
)->head
;
1421 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1426 switch (con
->scanout
.kind
) {
1427 case SCANOUT_DMABUF
:
1428 return qemu_dmabuf_get_width(con
->scanout
.dmabuf
);
1429 case SCANOUT_TEXTURE
:
1430 return con
->scanout
.texture
.width
;
1431 case SCANOUT_SURFACE
:
1432 return surface_width(con
->surface
);
1438 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1443 switch (con
->scanout
.kind
) {
1444 case SCANOUT_DMABUF
:
1445 return qemu_dmabuf_get_height(con
->scanout
.dmabuf
);
1446 case SCANOUT_TEXTURE
:
1447 return con
->scanout
.texture
.height
;
1448 case SCANOUT_SURFACE
:
1449 return surface_height(con
->surface
);
1455 int qemu_invalidate_text_consoles(void)
1460 QTAILQ_FOREACH(s
, &consoles
, next
) {
1461 if (qemu_console_is_graphic(s
) ||
1462 !qemu_console_is_visible(s
)) {
1466 graphic_hw_invalidate(s
);
1472 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
1474 DisplaySurface
*surface
= qemu_console_surface(s
);
1476 assert(QEMU_IS_GRAPHIC_CONSOLE(s
));
1478 if ((s
->scanout
.kind
!= SCANOUT_SURFACE
||
1479 (surface
&& surface_is_allocated(surface
) &&
1480 !surface_is_placeholder(surface
))) &&
1481 qemu_console_get_width(s
, -1) == width
&&
1482 qemu_console_get_height(s
, -1) == height
) {
1486 surface
= qemu_create_displaysurface(width
, height
);
1487 dpy_gfx_replace_surface(s
, surface
);
1490 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
1492 switch (console
->scanout
.kind
) {
1493 case SCANOUT_SURFACE
:
1494 return console
->surface
;
1500 PixelFormat
qemu_default_pixelformat(int bpp
)
1502 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
1503 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
1507 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
1509 void qemu_display_register(QemuDisplay
*ui
)
1511 assert(ui
->type
< DISPLAY_TYPE__MAX
);
1512 dpys
[ui
->type
] = ui
;
1515 bool qemu_display_find_default(DisplayOptions
*opts
)
1517 static DisplayType prio
[] = {
1518 #if defined(CONFIG_GTK)
1521 #if defined(CONFIG_SDL)
1524 #if defined(CONFIG_COCOA)
1530 for (i
= 0; i
< (int)ARRAY_SIZE(prio
); i
++) {
1531 if (dpys
[prio
[i
]] == NULL
) {
1532 Error
*local_err
= NULL
;
1533 int rv
= ui_module_load(DisplayType_str(prio
[i
]), &local_err
);
1535 error_report_err(local_err
);
1538 if (dpys
[prio
[i
]] == NULL
) {
1541 opts
->type
= prio
[i
];
1547 void qemu_display_early_init(DisplayOptions
*opts
)
1549 assert(opts
->type
< DISPLAY_TYPE__MAX
);
1550 if (opts
->type
== DISPLAY_TYPE_NONE
) {
1553 if (dpys
[opts
->type
] == NULL
) {
1554 Error
*local_err
= NULL
;
1555 int rv
= ui_module_load(DisplayType_str(opts
->type
), &local_err
);
1557 error_report_err(local_err
);
1560 if (dpys
[opts
->type
] == NULL
) {
1561 error_report("Display '%s' is not available.",
1562 DisplayType_str(opts
->type
));
1565 if (dpys
[opts
->type
]->early_init
) {
1566 dpys
[opts
->type
]->early_init(opts
);
1570 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
1572 assert(opts
->type
< DISPLAY_TYPE__MAX
);
1573 if (opts
->type
== DISPLAY_TYPE_NONE
) {
1576 assert(dpys
[opts
->type
] != NULL
);
1577 dpys
[opts
->type
]->init(ds
, opts
);
1580 const char *qemu_display_get_vc(DisplayOptions
*opts
)
1582 #ifdef CONFIG_PIXMAN
1583 const char *vc
= "vc:80Cx24C";
1585 const char *vc
= NULL
;
1588 assert(opts
->type
< DISPLAY_TYPE__MAX
);
1589 if (dpys
[opts
->type
] && dpys
[opts
->type
]->vc
) {
1590 vc
= dpys
[opts
->type
]->vc
;
1595 void qemu_display_help(void)
1599 printf("Available display backend types:\n");
1601 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
1603 Error
*local_err
= NULL
;
1604 int rv
= ui_module_load(DisplayType_str(idx
), &local_err
);
1606 error_report_err(local_err
);
1610 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
1614 "Some display backends support suboptions, which can be set with\n"
1615 " -display backend,option=value,option=value...\n"
1616 "For a short list of the suboptions for each display, see the "
1617 "top-level -help output; more detail is in the documentation.\n");