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"
41 #include "console-priv.h"
43 OBJECT_DEFINE_ABSTRACT_TYPE(QemuConsole, qemu_console, QEMU_CONSOLE, OBJECT)
45 typedef struct QemuGraphicConsole {
52 int cursor_x, cursor_y, cursor_on;
55 typedef QemuConsoleClass QemuGraphicConsoleClass;
57 OBJECT_DEFINE_TYPE(QemuGraphicConsole, qemu_graphic_console, QEMU_GRAPHIC_CONSOLE, QEMU_CONSOLE)
62 uint64_t update_interval;
65 QLIST_HEAD(, DisplayChangeListener) listeners;
68 static DisplayState *display_state;
69 static QemuConsole *active_console;
70 static QTAILQ_HEAD(, QemuConsole) consoles =
71 QTAILQ_HEAD_INITIALIZER(consoles);
73 static void dpy_refresh(DisplayState *s);
74 static DisplayState *get_alloc_displaystate(void);
75 static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
76 static bool console_compatible_with(QemuConsole *con,
77 DisplayChangeListener *dcl, Error **errp);
78 static QemuConsole *qemu_graphic_console_lookup_unused(void);
79 static void dpy_set_ui_info_timer(void *opaque);
81 static void gui_update(void *opaque)
83 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
84 uint64_t dcl_interval;
85 DisplayState *ds = opaque;
86 DisplayChangeListener *dcl;
88 ds->refreshing = true;
90 ds->refreshing = false;
92 QLIST_FOREACH(dcl, &ds->listeners, next) {
93 dcl_interval = dcl->update_interval ?
94 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
95 if (interval > dcl_interval) {
96 interval = dcl_interval;
99 if (ds->update_interval != interval) {
100 ds->update_interval = interval;
101 trace_console_refresh(interval);
103 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
104 timer_mod(ds->gui_timer, ds->last_update + interval);
107 static void gui_setup_refresh(DisplayState *ds)
109 DisplayChangeListener *dcl;
110 bool need_timer = false;
112 QLIST_FOREACH(dcl, &ds->listeners, next) {
113 if (dcl->ops->dpy_refresh != NULL) {
118 if (need_timer && ds->gui_timer == NULL) {
119 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
120 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
122 if (!need_timer && ds->gui_timer != NULL) {
123 timer_free(ds->gui_timer);
124 ds->gui_timer = NULL;
128 void graphic_hw_update_done(QemuConsole *con)
131 qemu_co_enter_all(&con->dump_queue, NULL);
135 void graphic_hw_update(QemuConsole *con)
138 con = con ? con : active_console;
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)
213 con = active_console;
215 if (con && con->hw_ops->invalidate) {
216 con->hw_ops->invalidate(con->hw);
220 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
223 con = active_console;
225 if (con && con->hw_ops->text_update) {
226 con->hw_ops->text_update(con->hw, chardata);
230 static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
231 struct DisplaySurface *new_surface,
234 if (dcl->ops->dpy_gfx_switch) {
235 dcl->ops->dpy_gfx_switch(dcl, new_surface);
238 if (update && dcl->ops->dpy_gfx_update) {
239 dcl->ops->dpy_gfx_update(dcl, 0, 0,
240 surface_width(new_surface),
241 surface_height(new_surface));
245 static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
247 if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
248 con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
252 static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
254 if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
255 con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
259 static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
260 int x, int y, int w, int h)
262 if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
263 con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
267 static void displaychangelistener_display_console(DisplayChangeListener *dcl,
271 static const char nodev[] =
272 "This VM has no graphic display device.";
273 static DisplaySurface *dummy;
275 if (!con || !console_compatible_with(con, dcl, errp)) {
277 dummy = qemu_create_placeholder_surface(640, 480, nodev);
280 dpy_gfx_create_texture(con, dummy);
282 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
286 dpy_gfx_create_texture(con, con->surface);
287 displaychangelistener_gfx_switch(dcl, con->surface,
288 con->scanout.kind == SCANOUT_SURFACE);
290 if (con->scanout.kind == SCANOUT_DMABUF &&
291 displaychangelistener_has_dmabuf(dcl)) {
292 dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
293 } else if (con->scanout.kind == SCANOUT_TEXTURE &&
294 dcl->ops->dpy_gl_scanout_texture) {
295 dcl->ops->dpy_gl_scanout_texture(dcl,
296 con->scanout.texture.backing_id,
297 con->scanout.texture.backing_y_0_top,
298 con->scanout.texture.backing_width,
299 con->scanout.texture.backing_height,
300 con->scanout.texture.x,
301 con->scanout.texture.y,
302 con->scanout.texture.width,
303 con->scanout.texture.height,
304 con->scanout.texture.d3d_tex2d);
308 void console_select(unsigned int index)
310 DisplayChangeListener *dcl;
313 trace_console_select(index);
314 s = qemu_console_lookup_by_index(index);
316 DisplayState *ds = s->ds;
319 QLIST_FOREACH (dcl, &ds->listeners, next) {
320 if (dcl->con != NULL) {
323 displaychangelistener_display_console(dcl, s, NULL);
326 if (QEMU_IS_TEXT_CONSOLE(s)) {
327 qemu_text_console_select(QEMU_TEXT_CONSOLE(s));
332 void qemu_text_console_put_keysym(QemuTextConsole *s, int keysym)
335 if (!QEMU_IS_TEXT_CONSOLE(active_console)) {
338 s = QEMU_TEXT_CONSOLE(active_console);
341 qemu_text_console_handle_keysym(s, keysym);
344 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
345 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
346 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
347 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
348 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
349 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
350 [Q_KEY_CODE_END] = QEMU_KEY_END,
351 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
352 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
353 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
354 [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
355 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
358 static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
359 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
360 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
361 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
362 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
363 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
364 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
365 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
366 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
369 bool qemu_text_console_put_qcode(QemuTextConsole *s, int qcode, bool ctrl)
373 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
377 qemu_text_console_put_keysym(s, keysym);
381 void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len)
385 for (i = 0; i < len && str[i]; i++) {
386 qemu_text_console_put_keysym(s, str[i]);
391 qemu_console_register(QemuConsole *c)
395 if (!active_console || (!QEMU_IS_GRAPHIC_CONSOLE(active_console) &&
396 QEMU_IS_GRAPHIC_CONSOLE(c))) {
400 if (QTAILQ_EMPTY(&consoles)) {
402 QTAILQ_INSERT_TAIL(&consoles, c, next);
403 } else if (!QEMU_IS_GRAPHIC_CONSOLE(c) || phase_check(PHASE_MACHINE_READY)) {
404 QemuConsole *last = QTAILQ_LAST(&consoles);
405 c->index = last->index + 1;
406 QTAILQ_INSERT_TAIL(&consoles, c, next);
409 * HACK: Put graphical consoles before text consoles.
411 * Only do that for coldplugged devices. After initial device
412 * initialization we will not renumber the consoles any more.
414 QemuConsole *it = QTAILQ_FIRST(&consoles);
416 while (QTAILQ_NEXT(it, next) != NULL && QEMU_IS_GRAPHIC_CONSOLE(it)) {
417 it = QTAILQ_NEXT(it, next);
419 if (QEMU_IS_GRAPHIC_CONSOLE(it)) {
420 /* have no text consoles */
421 c->index = it->index + 1;
422 QTAILQ_INSERT_AFTER(&consoles, it, c, next);
424 c->index = it->index;
425 QTAILQ_INSERT_BEFORE(it, c, next);
426 /* renumber text consoles */
427 for (i = c->index + 1; it != NULL; it = QTAILQ_NEXT(it, next), i++) {
435 qemu_console_finalize(Object *obj)
437 QemuConsole *c = QEMU_CONSOLE(obj);
439 /* TODO: check this code path, and unregister from consoles */
440 g_clear_pointer(&c->surface, qemu_free_displaysurface);
441 g_clear_pointer(&c->gl_unblock_timer, timer_free);
442 g_clear_pointer(&c->ui_timer, timer_free);
446 qemu_console_class_init(ObjectClass *oc, void *data)
451 qemu_console_init(Object *obj)
453 QemuConsole *c = QEMU_CONSOLE(obj);
454 DisplayState *ds = get_alloc_displaystate();
456 qemu_co_queue_init(&c->dump_queue);
459 c->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
460 dpy_set_ui_info_timer, c);
461 qemu_console_register(c);
465 qemu_graphic_console_finalize(Object *obj)
467 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
469 g_clear_pointer(&c->device, object_unref);
473 qemu_graphic_console_prop_get_head(Object *obj, Visitor *v, const char *name,
474 void *opaque, Error **errp)
476 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
478 visit_type_uint32(v, name, &c->head, errp);
482 qemu_graphic_console_class_init(ObjectClass *oc, void *data)
484 object_class_property_add_link(oc, "device", TYPE_DEVICE,
485 offsetof(QemuGraphicConsole, device),
486 object_property_allow_set_link,
487 OBJ_PROP_LINK_STRONG);
488 object_class_property_add(oc, "head", "uint32",
489 qemu_graphic_console_prop_get_head,
494 qemu_graphic_console_init(Object *obj)
499 void qemu_displaysurface_win32_set_handle(DisplaySurface *surface,
500 HANDLE h, uint32_t offset)
502 assert(!surface->handle);
505 surface->handle_offset = offset;
509 win32_pixman_image_destroy(pixman_image_t *image, void *data)
511 DisplaySurface *surface = data;
513 if (!surface->handle) {
517 assert(surface->handle_offset == 0);
520 pixman_image_get_data(surface->image),
527 DisplaySurface *qemu_create_displaysurface(int width, int height)
529 DisplaySurface *surface;
532 HANDLE handle = NULL;
535 trace_displaysurface_create(width, height);
538 bits = qemu_win32_map_alloc(width * height * 4, &handle, &error_abort);
541 surface = qemu_create_displaysurface_from(
546 surface->flags = QEMU_ALLOCATED_FLAG;
549 qemu_displaysurface_win32_set_handle(surface, handle, 0);
554 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
555 pixman_format_code_t format,
556 int linesize, uint8_t *data)
558 DisplaySurface *surface = g_new0(DisplaySurface, 1);
560 trace_displaysurface_create_from(surface, width, height, format);
561 surface->image = pixman_image_create_bits(format,
563 (void *)data, linesize);
564 assert(surface->image != NULL);
566 pixman_image_set_destroy_function(surface->image,
567 win32_pixman_image_destroy, surface);
573 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
575 DisplaySurface *surface = g_new0(DisplaySurface, 1);
577 trace_displaysurface_create_pixman(surface);
578 surface->image = pixman_image_ref(image);
583 DisplaySurface *qemu_create_placeholder_surface(int w, int h,
586 DisplaySurface *surface = qemu_create_displaysurface(w, h);
587 pixman_color_t bg = QEMU_PIXMAN_COLOR_BLACK;
588 pixman_color_t fg = QEMU_PIXMAN_COLOR_GRAY;
589 pixman_image_t *glyph;
593 x = (w / FONT_WIDTH - len) / 2;
594 y = (h / FONT_HEIGHT - 1) / 2;
595 for (i = 0; i < len; i++) {
596 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
597 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
598 x+i, y, FONT_WIDTH, FONT_HEIGHT);
599 qemu_pixman_image_unref(glyph);
601 surface->flags |= QEMU_PLACEHOLDER_FLAG;
605 void qemu_free_displaysurface(DisplaySurface *surface)
607 if (surface == NULL) {
610 trace_displaysurface_free(surface);
611 qemu_pixman_image_unref(surface->image);
615 bool console_has_gl(QemuConsole *con)
617 return con->gl != NULL;
620 static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
622 if (dcl->ops->dpy_has_dmabuf) {
623 return dcl->ops->dpy_has_dmabuf(dcl);
626 if (dcl->ops->dpy_gl_scanout_dmabuf) {
633 static bool console_compatible_with(QemuConsole *con,
634 DisplayChangeListener *dcl, Error **errp)
638 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
640 if (console_has_gl(con) &&
641 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
642 error_setg(errp, "Display %s is incompatible with the GL context",
647 if (flags & GRAPHIC_FLAGS_GL &&
648 !console_has_gl(con)) {
649 error_setg(errp, "The console requires a GL context.");
654 if (flags & GRAPHIC_FLAGS_DMABUF &&
655 !displaychangelistener_has_dmabuf(dcl)) {
656 error_setg(errp, "The console requires display DMABUF support.");
663 void console_handle_touch_event(QemuConsole *con,
664 struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
666 int width, int height,
668 InputMultiTouchType type,
671 struct touch_slot *slot;
672 bool needs_sync = false;
676 if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
678 "Unexpected touch slot number: % " PRId64" >= %d",
679 num_slot, INPUT_EVENT_SLOTS_MAX);
683 slot = &touch_slots[num_slot];
687 if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
688 slot->tracking_id = num_slot;
691 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
695 update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
698 slot = &touch_slots[i];
700 if (slot->tracking_id == -1) {
704 if (update == INPUT_MULTI_TOUCH_TYPE_END) {
705 slot->tracking_id = -1;
706 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
709 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
710 qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
711 qemu_input_queue_mtt_abs(con,
712 INPUT_AXIS_X, (int) slot->x,
714 i, slot->tracking_id);
715 qemu_input_queue_mtt_abs(con,
716 INPUT_AXIS_Y, (int) slot->y,
718 i, slot->tracking_id);
724 qemu_input_event_sync();
728 void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
730 /* display has opengl support */
733 error_report("The console already has an OpenGL context.");
740 dcl_set_graphic_cursor(DisplayChangeListener *dcl, QemuGraphicConsole *con)
742 if (con && con->cursor && dcl->ops->dpy_cursor_define) {
743 dcl->ops->dpy_cursor_define(dcl, con->cursor);
745 if (con && dcl->ops->dpy_mouse_set) {
746 dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
750 void register_displaychangelistener(DisplayChangeListener *dcl)
756 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
757 dcl->ds = get_alloc_displaystate();
758 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
759 gui_setup_refresh(dcl->ds);
764 con = active_console;
766 displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
767 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
768 dcl_set_graphic_cursor(dcl, QEMU_GRAPHIC_CONSOLE(con));
770 qemu_text_console_update_cursor();
773 void update_displaychangelistener(DisplayChangeListener *dcl,
776 DisplayState *ds = dcl->ds;
778 dcl->update_interval = interval;
779 if (!ds->refreshing && ds->update_interval > interval) {
780 timer_mod(ds->gui_timer, ds->last_update + interval);
784 void unregister_displaychangelistener(DisplayChangeListener *dcl)
786 DisplayState *ds = dcl->ds;
787 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
791 QLIST_REMOVE(dcl, next);
793 gui_setup_refresh(ds);
796 static void dpy_set_ui_info_timer(void *opaque)
798 QemuConsole *con = opaque;
799 uint32_t head = qemu_console_get_head(con);
801 con->hw_ops->ui_info(con->hw, head, &con->ui_info);
804 bool dpy_ui_info_supported(const QemuConsole *con)
807 con = active_console;
813 return con->hw_ops->ui_info != NULL;
816 const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
818 assert(dpy_ui_info_supported(con));
821 con = active_console;
824 return &con->ui_info;
827 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
830 con = active_console;
833 if (!dpy_ui_info_supported(con)) {
836 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
837 /* nothing changed -- ignore */
842 * Typically we get a flood of these as the user resizes the window.
843 * Wait until the dust has settled (one second without updates), then
844 * go notify the guest.
846 con->ui_info = *info;
847 timer_mod(con->ui_timer,
848 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
852 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
854 DisplayState *s = con->ds;
855 DisplayChangeListener *dcl;
856 int width = qemu_console_get_width(con, x + w);
857 int height = qemu_console_get_height(con, y + h);
863 w = MIN(w, width - x);
864 h = MIN(h, height - y);
866 if (!qemu_console_is_visible(con)) {
869 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
870 QLIST_FOREACH(dcl, &s->listeners, next) {
871 if (con != (dcl->con ? dcl->con : active_console)) {
874 if (dcl->ops->dpy_gfx_update) {
875 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
880 void dpy_gfx_update_full(QemuConsole *con)
882 int w = qemu_console_get_width(con, 0);
883 int h = qemu_console_get_height(con, 0);
885 dpy_gfx_update(con, 0, 0, w, h);
888 void dpy_gfx_replace_surface(QemuConsole *con,
889 DisplaySurface *surface)
891 static const char placeholder_msg[] = "Display output is not active.";
892 DisplayState *s = con->ds;
893 DisplaySurface *old_surface = con->surface;
894 DisplaySurface *new_surface = surface;
895 DisplayChangeListener *dcl;
901 width = surface_width(old_surface);
902 height = surface_height(old_surface);
908 new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
911 assert(old_surface != new_surface);
913 con->scanout.kind = SCANOUT_SURFACE;
914 con->surface = new_surface;
915 dpy_gfx_create_texture(con, new_surface);
916 QLIST_FOREACH(dcl, &s->listeners, next) {
917 if (con != (dcl->con ? dcl->con : active_console)) {
920 displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
922 dpy_gfx_destroy_texture(con, old_surface);
923 qemu_free_displaysurface(old_surface);
926 bool dpy_gfx_check_format(QemuConsole *con,
927 pixman_format_code_t format)
929 DisplayChangeListener *dcl;
930 DisplayState *s = con->ds;
932 QLIST_FOREACH(dcl, &s->listeners, next) {
933 if (dcl->con && dcl->con != con) {
934 /* dcl bound to another console -> skip */
937 if (dcl->ops->dpy_gfx_check_format) {
938 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
942 /* default is to allow native 32 bpp only */
943 if (format != qemu_default_pixman_format(32, true)) {
951 static void dpy_refresh(DisplayState *s)
953 DisplayChangeListener *dcl;
955 QLIST_FOREACH(dcl, &s->listeners, next) {
956 if (dcl->ops->dpy_refresh) {
957 dcl->ops->dpy_refresh(dcl);
962 void dpy_text_cursor(QemuConsole *con, int x, int y)
964 DisplayState *s = con->ds;
965 DisplayChangeListener *dcl;
967 if (!qemu_console_is_visible(con)) {
970 QLIST_FOREACH(dcl, &s->listeners, next) {
971 if (con != (dcl->con ? dcl->con : active_console)) {
974 if (dcl->ops->dpy_text_cursor) {
975 dcl->ops->dpy_text_cursor(dcl, x, y);
980 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
982 DisplayState *s = con->ds;
983 DisplayChangeListener *dcl;
985 if (!qemu_console_is_visible(con)) {
988 QLIST_FOREACH(dcl, &s->listeners, next) {
989 if (con != (dcl->con ? dcl->con : active_console)) {
992 if (dcl->ops->dpy_text_update) {
993 dcl->ops->dpy_text_update(dcl, x, y, w, h);
998 void dpy_text_resize(QemuConsole *con, int w, int h)
1000 DisplayState *s = con->ds;
1001 DisplayChangeListener *dcl;
1003 if (!qemu_console_is_visible(con)) {
1006 QLIST_FOREACH(dcl, &s->listeners, next) {
1007 if (con != (dcl->con ? dcl->con : active_console)) {
1010 if (dcl->ops->dpy_text_resize) {
1011 dcl->ops->dpy_text_resize(dcl, w, h);
1016 void dpy_mouse_set(QemuConsole *c, int x, int y, int on)
1018 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1019 DisplayState *s = c->ds;
1020 DisplayChangeListener *dcl;
1024 con->cursor_on = on;
1025 if (!qemu_console_is_visible(c)) {
1028 QLIST_FOREACH(dcl, &s->listeners, next) {
1029 if (c != (dcl->con ? dcl->con : active_console)) {
1032 if (dcl->ops->dpy_mouse_set) {
1033 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1038 void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
1040 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1041 DisplayState *s = c->ds;
1042 DisplayChangeListener *dcl;
1044 cursor_unref(con->cursor);
1045 con->cursor = cursor_ref(cursor);
1046 if (!qemu_console_is_visible(c)) {
1049 QLIST_FOREACH(dcl, &s->listeners, next) {
1050 if (c != (dcl->con ? dcl->con : active_console)) {
1053 if (dcl->ops->dpy_cursor_define) {
1054 dcl->ops->dpy_cursor_define(dcl, cursor);
1059 bool dpy_cursor_define_supported(QemuConsole *con)
1061 DisplayState *s = con->ds;
1062 DisplayChangeListener *dcl;
1064 QLIST_FOREACH(dcl, &s->listeners, next) {
1065 if (dcl->ops->dpy_cursor_define) {
1072 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1073 struct QEMUGLParams *qparams)
1076 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1079 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1082 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1085 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1088 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1091 void dpy_gl_scanout_disable(QemuConsole *con)
1093 DisplayState *s = con->ds;
1094 DisplayChangeListener *dcl;
1096 if (con->scanout.kind != SCANOUT_SURFACE) {
1097 con->scanout.kind = SCANOUT_NONE;
1099 QLIST_FOREACH(dcl, &s->listeners, next) {
1100 if (con != (dcl->con ? dcl->con : active_console)) {
1103 if (dcl->ops->dpy_gl_scanout_disable) {
1104 dcl->ops->dpy_gl_scanout_disable(dcl);
1109 void dpy_gl_scanout_texture(QemuConsole *con,
1110 uint32_t backing_id,
1111 bool backing_y_0_top,
1112 uint32_t backing_width,
1113 uint32_t backing_height,
1114 uint32_t x, uint32_t y,
1115 uint32_t width, uint32_t height,
1118 DisplayState *s = con->ds;
1119 DisplayChangeListener *dcl;
1121 con->scanout.kind = SCANOUT_TEXTURE;
1122 con->scanout.texture = (ScanoutTexture) {
1123 backing_id, backing_y_0_top, backing_width, backing_height,
1124 x, y, width, height, d3d_tex2d,
1126 QLIST_FOREACH(dcl, &s->listeners, next) {
1127 if (con != (dcl->con ? dcl->con : active_console)) {
1130 if (dcl->ops->dpy_gl_scanout_texture) {
1131 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1133 backing_width, backing_height,
1134 x, y, width, height,
1140 void dpy_gl_scanout_dmabuf(QemuConsole *con,
1143 DisplayState *s = con->ds;
1144 DisplayChangeListener *dcl;
1146 con->scanout.kind = SCANOUT_DMABUF;
1147 con->scanout.dmabuf = dmabuf;
1148 QLIST_FOREACH(dcl, &s->listeners, next) {
1149 if (con != (dcl->con ? dcl->con : active_console)) {
1152 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1153 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1158 void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1159 bool have_hot, uint32_t hot_x, uint32_t hot_y)
1161 DisplayState *s = con->ds;
1162 DisplayChangeListener *dcl;
1164 QLIST_FOREACH(dcl, &s->listeners, next) {
1165 if (con != (dcl->con ? dcl->con : active_console)) {
1168 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1169 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
1170 have_hot, hot_x, hot_y);
1175 void dpy_gl_cursor_position(QemuConsole *con,
1176 uint32_t pos_x, uint32_t pos_y)
1178 DisplayState *s = con->ds;
1179 DisplayChangeListener *dcl;
1181 QLIST_FOREACH(dcl, &s->listeners, next) {
1182 if (con != (dcl->con ? dcl->con : active_console)) {
1185 if (dcl->ops->dpy_gl_cursor_position) {
1186 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1191 void dpy_gl_release_dmabuf(QemuConsole *con,
1194 DisplayState *s = con->ds;
1195 DisplayChangeListener *dcl;
1197 QLIST_FOREACH(dcl, &s->listeners, next) {
1198 if (con != (dcl->con ? dcl->con : active_console)) {
1201 if (dcl->ops->dpy_gl_release_dmabuf) {
1202 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
1207 void dpy_gl_update(QemuConsole *con,
1208 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1210 DisplayState *s = con->ds;
1211 DisplayChangeListener *dcl;
1215 graphic_hw_gl_block(con, true);
1216 QLIST_FOREACH(dcl, &s->listeners, next) {
1217 if (con != (dcl->con ? dcl->con : active_console)) {
1220 if (dcl->ops->dpy_gl_update) {
1221 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
1224 graphic_hw_gl_block(con, false);
1227 /***********************************************************/
1228 /* register display */
1230 /* console.c internal use only */
1231 static DisplayState *get_alloc_displaystate(void)
1233 if (!display_state) {
1234 display_state = g_new0(DisplayState, 1);
1236 return display_state;
1240 * Called by main(), after creating QemuConsoles
1241 * and before initializing ui (sdl/vnc/...).
1243 DisplayState *init_displaystate(void)
1248 QTAILQ_FOREACH(con, &consoles, next) {
1249 /* Hook up into the qom tree here (not in object_new()), once
1250 * all QemuConsoles are created and the order / numbering
1251 * doesn't change any more */
1252 name = g_strdup_printf("console[%d]", con->index);
1253 object_property_add_child(container_get(object_get_root(), "/backend"),
1258 return display_state;
1261 void graphic_console_set_hwops(QemuConsole *con,
1262 const GraphicHwOps *hw_ops,
1265 con->hw_ops = hw_ops;
1269 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1270 const GraphicHwOps *hw_ops,
1273 static const char noinit[] =
1274 "Guest has not initialized the display (yet).";
1278 DisplaySurface *surface;
1280 s = qemu_graphic_console_lookup_unused();
1282 trace_console_gfx_reuse(s->index);
1283 width = qemu_console_get_width(s, 0);
1284 height = qemu_console_get_height(s, 0);
1286 trace_console_gfx_new();
1287 s = (QemuConsole *)object_new(TYPE_QEMU_GRAPHIC_CONSOLE);
1289 QEMU_GRAPHIC_CONSOLE(s)->head = head;
1290 graphic_console_set_hwops(s, hw_ops, opaque);
1292 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
1296 surface = qemu_create_placeholder_surface(width, height, noinit);
1297 dpy_gfx_replace_surface(s, surface);
1298 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1299 graphic_hw_gl_unblock_timer, s);
1303 static const GraphicHwOps unused_ops = {
1307 void graphic_console_close(QemuConsole *con)
1309 static const char unplugged[] =
1310 "Guest display has been unplugged";
1311 DisplaySurface *surface;
1312 int width = qemu_console_get_width(con, 640);
1313 int height = qemu_console_get_height(con, 480);
1315 trace_console_gfx_close(con->index);
1316 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
1317 graphic_console_set_hwops(con, &unused_ops, NULL);
1320 dpy_gl_scanout_disable(con);
1322 surface = qemu_create_placeholder_surface(width, height, unplugged);
1323 dpy_gfx_replace_surface(con, surface);
1326 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1330 QTAILQ_FOREACH(con, &consoles, next) {
1331 if (con->index == index) {
1338 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1344 QTAILQ_FOREACH(con, &consoles, next) {
1345 obj = object_property_get_link(OBJECT(con),
1346 "device", &error_abort);
1347 if (DEVICE(obj) != dev) {
1350 h = object_property_get_uint(OBJECT(con),
1351 "head", &error_abort);
1360 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1361 uint32_t head, Error **errp)
1366 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1368 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1369 "Device '%s' not found", device_id);
1373 con = qemu_console_lookup_by_device(dev, head);
1375 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1383 static QemuConsole *qemu_graphic_console_lookup_unused(void)
1388 QTAILQ_FOREACH(con, &consoles, next) {
1389 if (!QEMU_IS_GRAPHIC_CONSOLE(con) || con->hw_ops != &unused_ops) {
1392 obj = object_property_get_link(OBJECT(con),
1393 "device", &error_abort);
1402 QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
1405 con = active_console;
1407 return QEMU_IS_GRAPHIC_CONSOLE(con) ? QEMU_GRAPHIC_CONSOLE(con)->cursor : NULL;
1410 bool qemu_console_is_visible(QemuConsole *con)
1412 return (con == active_console) || (con->dcls > 0);
1415 bool qemu_console_is_graphic(QemuConsole *con)
1418 con = active_console;
1420 return con && QEMU_IS_GRAPHIC_CONSOLE(con);
1423 bool qemu_console_is_fixedsize(QemuConsole *con)
1426 con = active_console;
1428 return con && (QEMU_IS_GRAPHIC_CONSOLE(con) || QEMU_IS_FIXED_TEXT_CONSOLE(con));
1431 bool qemu_console_is_gl_blocked(QemuConsole *con)
1433 assert(con != NULL);
1434 return con->gl_block;
1437 static bool qemu_graphic_console_is_multihead(QemuGraphicConsole *c)
1441 QTAILQ_FOREACH(con, &consoles, next) {
1442 QemuGraphicConsole *candidate;
1444 if (!QEMU_IS_GRAPHIC_CONSOLE(con)) {
1448 candidate = QEMU_GRAPHIC_CONSOLE(con);
1449 if (candidate->device != c->device) {
1453 if (candidate->head != c->head) {
1460 char *qemu_console_get_label(QemuConsole *con)
1462 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1463 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(con);
1468 dev = DEVICE(c->device);
1469 multihead = qemu_graphic_console_is_multihead(c);
1471 return g_strdup_printf("%s.%d", dev->id ?
1473 object_get_typename(c->device),
1476 return g_strdup_printf("%s", dev->id ?
1478 object_get_typename(c->device));
1481 return g_strdup("VGA");
1482 } else if (QEMU_IS_TEXT_CONSOLE(con)) {
1483 const char *label = qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con));
1485 return g_strdup(label);
1489 return g_strdup_printf("vc%d", con->index);
1492 int qemu_console_get_index(QemuConsole *con)
1495 con = active_console;
1497 return con ? con->index : -1;
1500 uint32_t qemu_console_get_head(QemuConsole *con)
1503 con = active_console;
1508 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1509 return QEMU_GRAPHIC_CONSOLE(con)->head;
1514 int qemu_console_get_width(QemuConsole *con, int fallback)
1517 con = active_console;
1522 switch (con->scanout.kind) {
1523 case SCANOUT_DMABUF:
1524 return con->scanout.dmabuf->width;
1525 case SCANOUT_TEXTURE:
1526 return con->scanout.texture.width;
1527 case SCANOUT_SURFACE:
1528 return surface_width(con->surface);
1534 int qemu_console_get_height(QemuConsole *con, int fallback)
1537 con = active_console;
1542 switch (con->scanout.kind) {
1543 case SCANOUT_DMABUF:
1544 return con->scanout.dmabuf->height;
1545 case SCANOUT_TEXTURE:
1546 return con->scanout.texture.height;
1547 case SCANOUT_SURFACE:
1548 return surface_height(con->surface);
1554 int qemu_invalidate_text_consoles(void)
1559 QTAILQ_FOREACH(s, &consoles, next) {
1560 if (qemu_console_is_graphic(s) ||
1561 !qemu_console_is_visible(s)) {
1565 graphic_hw_invalidate(s);
1571 void qemu_console_resize(QemuConsole *s, int width, int height)
1573 DisplaySurface *surface = qemu_console_surface(s);
1575 assert(QEMU_IS_GRAPHIC_CONSOLE(s));
1577 if ((s->scanout.kind != SCANOUT_SURFACE ||
1578 (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
1579 qemu_console_get_width(s, -1) == width &&
1580 qemu_console_get_height(s, -1) == height) {
1584 surface = qemu_create_displaysurface(width, height);
1585 dpy_gfx_replace_surface(s, surface);
1588 DisplaySurface *qemu_console_surface(QemuConsole *console)
1590 switch (console->scanout.kind) {
1591 case SCANOUT_SURFACE:
1592 return console->surface;
1598 PixelFormat qemu_default_pixelformat(int bpp)
1600 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
1601 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
1605 static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
1607 void qemu_display_register(QemuDisplay *ui)
1609 assert(ui->type < DISPLAY_TYPE__MAX);
1610 dpys[ui->type] = ui;
1613 bool qemu_display_find_default(DisplayOptions *opts)
1615 static DisplayType prio[] = {
1616 #if defined(CONFIG_GTK)
1619 #if defined(CONFIG_SDL)
1622 #if defined(CONFIG_COCOA)
1628 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
1629 if (dpys[prio[i]] == NULL) {
1630 Error *local_err = NULL;
1631 int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
1633 error_report_err(local_err);
1636 if (dpys[prio[i]] == NULL) {
1639 opts->type = prio[i];
1645 void qemu_display_early_init(DisplayOptions *opts)
1647 assert(opts->type < DISPLAY_TYPE__MAX);
1648 if (opts->type == DISPLAY_TYPE_NONE) {
1651 if (dpys[opts->type] == NULL) {
1652 Error *local_err = NULL;
1653 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
1655 error_report_err(local_err);
1658 if (dpys[opts->type] == NULL) {
1659 error_report("Display '%s' is not available.",
1660 DisplayType_str(opts->type));
1663 if (dpys[opts->type]->early_init) {
1664 dpys[opts->type]->early_init(opts);
1668 void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
1670 assert(opts->type < DISPLAY_TYPE__MAX);
1671 if (opts->type == DISPLAY_TYPE_NONE) {
1674 assert(dpys[opts->type] != NULL);
1675 dpys[opts->type]->init(ds, opts);
1678 void qemu_display_help(void)
1682 printf("Available display backend types:\n");
1684 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
1686 Error *local_err = NULL;
1687 int rv = ui_module_load(DisplayType_str(idx), &local_err);
1689 error_report_err(local_err);
1693 printf("%s\n", DisplayType_str(dpys[idx]->type));