]> git.proxmox.com Git - mirror_qemu.git/blame_incremental - ui/console.c
ui: fix crash when there are no active_console
[mirror_qemu.git] / ui / console.c
... / ...
CommitLineData
1/*
2 * QEMU graphical console
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
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"
37#include "trace.h"
38#include "exec/memory.h"
39#include "qom/object.h"
40
41#include "console-priv.h"
42
43OBJECT_DEFINE_ABSTRACT_TYPE(QemuConsole, qemu_console, QEMU_CONSOLE, OBJECT)
44
45typedef struct QemuGraphicConsole {
46 QemuConsole parent;
47
48 Object *device;
49 uint32_t head;
50
51 QEMUCursor *cursor;
52 int cursor_x, cursor_y, cursor_on;
53} QemuGraphicConsole;
54
55typedef QemuConsoleClass QemuGraphicConsoleClass;
56
57OBJECT_DEFINE_TYPE(QemuGraphicConsole, qemu_graphic_console, QEMU_GRAPHIC_CONSOLE, QEMU_CONSOLE)
58
59struct DisplayState {
60 QEMUTimer *gui_timer;
61 uint64_t last_update;
62 uint64_t update_interval;
63 bool refreshing;
64
65 QLIST_HEAD(, DisplayChangeListener) listeners;
66};
67
68static DisplayState *display_state;
69static QemuConsole *active_console;
70static QTAILQ_HEAD(, QemuConsole) consoles =
71 QTAILQ_HEAD_INITIALIZER(consoles);
72
73static void dpy_refresh(DisplayState *s);
74static DisplayState *get_alloc_displaystate(void);
75static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
76static bool console_compatible_with(QemuConsole *con,
77 DisplayChangeListener *dcl, Error **errp);
78static QemuConsole *qemu_graphic_console_lookup_unused(void);
79static void dpy_set_ui_info_timer(void *opaque);
80
81static void gui_update(void *opaque)
82{
83 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
84 uint64_t dcl_interval;
85 DisplayState *ds = opaque;
86 DisplayChangeListener *dcl;
87
88 ds->refreshing = true;
89 dpy_refresh(ds);
90 ds->refreshing = false;
91
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;
97 }
98 }
99 if (ds->update_interval != interval) {
100 ds->update_interval = interval;
101 trace_console_refresh(interval);
102 }
103 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
104 timer_mod(ds->gui_timer, ds->last_update + interval);
105}
106
107static void gui_setup_refresh(DisplayState *ds)
108{
109 DisplayChangeListener *dcl;
110 bool need_timer = false;
111
112 QLIST_FOREACH(dcl, &ds->listeners, next) {
113 if (dcl->ops->dpy_refresh != NULL) {
114 need_timer = true;
115 }
116 }
117
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));
121 }
122 if (!need_timer && ds->gui_timer != NULL) {
123 timer_free(ds->gui_timer);
124 ds->gui_timer = NULL;
125 }
126}
127
128void graphic_hw_update_done(QemuConsole *con)
129{
130 if (con) {
131 qemu_co_enter_all(&con->dump_queue, NULL);
132 }
133}
134
135void graphic_hw_update(QemuConsole *con)
136{
137 bool async = false;
138 con = con ? con : active_console;
139 if (!con) {
140 return;
141 }
142 if (con->hw_ops->gfx_update) {
143 con->hw_ops->gfx_update(con->hw);
144 async = con->hw_ops->gfx_update_async;
145 }
146 if (!async) {
147 graphic_hw_update_done(con);
148 }
149}
150
151static void graphic_hw_update_bh(void *con)
152{
153 graphic_hw_update(con);
154}
155
156void qemu_console_co_wait_update(QemuConsole *con)
157{
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);
162 }
163 qemu_co_queue_wait(&con->dump_queue, NULL);
164
165}
166
167static void graphic_hw_gl_unblock_timer(void *opaque)
168{
169 warn_report("console: no gl-unblock within one second");
170}
171
172void graphic_hw_gl_block(QemuConsole *con, bool block)
173{
174 uint64_t timeout;
175 assert(con != NULL);
176
177 if (block) {
178 con->gl_block++;
179 } else {
180 con->gl_block--;
181 }
182 assert(con->gl_block >= 0);
183 if (!con->hw_ops->gl_block) {
184 return;
185 }
186 if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
187 return;
188 }
189 con->hw_ops->gl_block(con->hw, block);
190
191 if (block) {
192 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
193 timeout += 1000; /* one sec */
194 timer_mod(con->gl_unblock_timer, timeout);
195 } else {
196 timer_del(con->gl_unblock_timer);
197 }
198}
199
200int qemu_console_get_window_id(QemuConsole *con)
201{
202 return con->window_id;
203}
204
205void qemu_console_set_window_id(QemuConsole *con, int window_id)
206{
207 con->window_id = window_id;
208}
209
210void graphic_hw_invalidate(QemuConsole *con)
211{
212 if (!con) {
213 con = active_console;
214 }
215 if (con && con->hw_ops->invalidate) {
216 con->hw_ops->invalidate(con->hw);
217 }
218}
219
220void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
221{
222 if (!con) {
223 con = active_console;
224 }
225 if (con && con->hw_ops->text_update) {
226 con->hw_ops->text_update(con->hw, chardata);
227 }
228}
229
230static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
231 struct DisplaySurface *new_surface,
232 bool update)
233{
234 if (dcl->ops->dpy_gfx_switch) {
235 dcl->ops->dpy_gfx_switch(dcl, new_surface);
236 }
237
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));
242 }
243}
244
245static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
246{
247 if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
248 con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
249 }
250}
251
252static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
253{
254 if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
255 con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
256 }
257}
258
259static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
260 int x, int y, int w, int h)
261{
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);
264 }
265}
266
267static void displaychangelistener_display_console(DisplayChangeListener *dcl,
268 QemuConsole *con,
269 Error **errp)
270{
271 static const char nodev[] =
272 "This VM has no graphic display device.";
273 static DisplaySurface *dummy;
274
275 if (!con || !console_compatible_with(con, dcl, errp)) {
276 if (!dummy) {
277 dummy = qemu_create_placeholder_surface(640, 480, nodev);
278 }
279 if (con) {
280 dpy_gfx_create_texture(con, dummy);
281 }
282 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
283 return;
284 }
285
286 dpy_gfx_create_texture(con, con->surface);
287 displaychangelistener_gfx_switch(dcl, con->surface,
288 con->scanout.kind == SCANOUT_SURFACE);
289
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);
305 }
306}
307
308void console_select(unsigned int index)
309{
310 DisplayChangeListener *dcl;
311 QemuConsole *s;
312
313 trace_console_select(index);
314 s = qemu_console_lookup_by_index(index);
315 if (s) {
316 DisplayState *ds = s->ds;
317
318 active_console = s;
319 QLIST_FOREACH (dcl, &ds->listeners, next) {
320 if (dcl->con != NULL) {
321 continue;
322 }
323 displaychangelistener_display_console(dcl, s, NULL);
324 }
325
326 if (QEMU_IS_TEXT_CONSOLE(s)) {
327 qemu_text_console_select(QEMU_TEXT_CONSOLE(s));
328 }
329 }
330}
331
332void qemu_text_console_put_keysym(QemuTextConsole *s, int keysym)
333{
334 if (!s) {
335 if (!QEMU_IS_TEXT_CONSOLE(active_console)) {
336 return;
337 }
338 s = QEMU_TEXT_CONSOLE(active_console);
339 }
340
341 qemu_text_console_handle_keysym(s, keysym);
342}
343
344static 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,
356};
357
358static 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,
367};
368
369bool qemu_text_console_put_qcode(QemuTextConsole *s, int qcode, bool ctrl)
370{
371 int keysym;
372
373 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
374 if (keysym == 0) {
375 return false;
376 }
377 qemu_text_console_put_keysym(s, keysym);
378 return true;
379}
380
381void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len)
382{
383 int i;
384
385 for (i = 0; i < len && str[i]; i++) {
386 qemu_text_console_put_keysym(s, str[i]);
387 }
388}
389
390static void
391qemu_console_register(QemuConsole *c)
392{
393 int i;
394
395 if (!active_console || (!QEMU_IS_GRAPHIC_CONSOLE(active_console) &&
396 QEMU_IS_GRAPHIC_CONSOLE(c))) {
397 active_console = c;
398 }
399
400 if (QTAILQ_EMPTY(&consoles)) {
401 c->index = 0;
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);
407 } else {
408 /*
409 * HACK: Put graphical consoles before text consoles.
410 *
411 * Only do that for coldplugged devices. After initial device
412 * initialization we will not renumber the consoles any more.
413 */
414 QemuConsole *it = QTAILQ_FIRST(&consoles);
415
416 while (QTAILQ_NEXT(it, next) != NULL && QEMU_IS_GRAPHIC_CONSOLE(it)) {
417 it = QTAILQ_NEXT(it, next);
418 }
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);
423 } else {
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++) {
428 it->index = i;
429 }
430 }
431 }
432}
433
434static void
435qemu_console_finalize(Object *obj)
436{
437 QemuConsole *c = QEMU_CONSOLE(obj);
438
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);
443}
444
445static void
446qemu_console_class_init(ObjectClass *oc, void *data)
447{
448}
449
450static void
451qemu_console_init(Object *obj)
452{
453 QemuConsole *c = QEMU_CONSOLE(obj);
454 DisplayState *ds = get_alloc_displaystate();
455
456 qemu_co_queue_init(&c->dump_queue);
457 c->ds = ds;
458 c->window_id = -1;
459 c->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
460 dpy_set_ui_info_timer, c);
461 qemu_console_register(c);
462}
463
464static void
465qemu_graphic_console_finalize(Object *obj)
466{
467 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
468
469 g_clear_pointer(&c->device, object_unref);
470}
471
472static void
473qemu_graphic_console_prop_get_head(Object *obj, Visitor *v, const char *name,
474 void *opaque, Error **errp)
475{
476 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
477
478 visit_type_uint32(v, name, &c->head, errp);
479}
480
481static void
482qemu_graphic_console_class_init(ObjectClass *oc, void *data)
483{
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,
490 NULL, NULL, NULL);
491}
492
493static void
494qemu_graphic_console_init(Object *obj)
495{
496}
497
498#ifdef WIN32
499void qemu_displaysurface_win32_set_handle(DisplaySurface *surface,
500 HANDLE h, uint32_t offset)
501{
502 assert(!surface->handle);
503
504 surface->handle = h;
505 surface->handle_offset = offset;
506}
507
508static void
509win32_pixman_image_destroy(pixman_image_t *image, void *data)
510{
511 DisplaySurface *surface = data;
512
513 if (!surface->handle) {
514 return;
515 }
516
517 assert(surface->handle_offset == 0);
518
519 qemu_win32_map_free(
520 pixman_image_get_data(surface->image),
521 surface->handle,
522 &error_warn
523 );
524}
525#endif
526
527DisplaySurface *qemu_create_displaysurface(int width, int height)
528{
529 DisplaySurface *surface;
530 void *bits = NULL;
531#ifdef WIN32
532 HANDLE handle = NULL;
533#endif
534
535 trace_displaysurface_create(width, height);
536
537#ifdef WIN32
538 bits = qemu_win32_map_alloc(width * height * 4, &handle, &error_abort);
539#endif
540
541 surface = qemu_create_displaysurface_from(
542 width, height,
543 PIXMAN_x8r8g8b8,
544 width * 4, bits
545 );
546 surface->flags = QEMU_ALLOCATED_FLAG;
547
548#ifdef WIN32
549 qemu_displaysurface_win32_set_handle(surface, handle, 0);
550#endif
551 return surface;
552}
553
554DisplaySurface *qemu_create_displaysurface_from(int width, int height,
555 pixman_format_code_t format,
556 int linesize, uint8_t *data)
557{
558 DisplaySurface *surface = g_new0(DisplaySurface, 1);
559
560 trace_displaysurface_create_from(surface, width, height, format);
561 surface->image = pixman_image_create_bits(format,
562 width, height,
563 (void *)data, linesize);
564 assert(surface->image != NULL);
565#ifdef WIN32
566 pixman_image_set_destroy_function(surface->image,
567 win32_pixman_image_destroy, surface);
568#endif
569
570 return surface;
571}
572
573DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
574{
575 DisplaySurface *surface = g_new0(DisplaySurface, 1);
576
577 trace_displaysurface_create_pixman(surface);
578 surface->image = pixman_image_ref(image);
579
580 return surface;
581}
582
583DisplaySurface *qemu_create_placeholder_surface(int w, int h,
584 const char *msg)
585{
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;
590 int len, x, y, i;
591
592 len = strlen(msg);
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);
600 }
601 surface->flags |= QEMU_PLACEHOLDER_FLAG;
602 return surface;
603}
604
605void qemu_free_displaysurface(DisplaySurface *surface)
606{
607 if (surface == NULL) {
608 return;
609 }
610 trace_displaysurface_free(surface);
611 qemu_pixman_image_unref(surface->image);
612 g_free(surface);
613}
614
615bool console_has_gl(QemuConsole *con)
616{
617 return con->gl != NULL;
618}
619
620static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
621{
622 if (dcl->ops->dpy_has_dmabuf) {
623 return dcl->ops->dpy_has_dmabuf(dcl);
624 }
625
626 if (dcl->ops->dpy_gl_scanout_dmabuf) {
627 return true;
628 }
629
630 return false;
631}
632
633static bool console_compatible_with(QemuConsole *con,
634 DisplayChangeListener *dcl, Error **errp)
635{
636 int flags;
637
638 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
639
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",
643 dcl->ops->dpy_name);
644 return false;
645 }
646
647 if (flags & GRAPHIC_FLAGS_GL &&
648 !console_has_gl(con)) {
649 error_setg(errp, "The console requires a GL context.");
650 return false;
651
652 }
653
654 if (flags & GRAPHIC_FLAGS_DMABUF &&
655 !displaychangelistener_has_dmabuf(dcl)) {
656 error_setg(errp, "The console requires display DMABUF support.");
657 return false;
658 }
659
660 return true;
661}
662
663void console_handle_touch_event(QemuConsole *con,
664 struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
665 uint64_t num_slot,
666 int width, int height,
667 double x, double y,
668 InputMultiTouchType type,
669 Error **errp)
670{
671 struct touch_slot *slot;
672 bool needs_sync = false;
673 int update;
674 int i;
675
676 if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
677 error_setg(errp,
678 "Unexpected touch slot number: % " PRId64" >= %d",
679 num_slot, INPUT_EVENT_SLOTS_MAX);
680 return;
681 }
682
683 slot = &touch_slots[num_slot];
684 slot->x = x;
685 slot->y = y;
686
687 if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
688 slot->tracking_id = num_slot;
689 }
690
691 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
692 if (i == num_slot) {
693 update = type;
694 } else {
695 update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
696 }
697
698 slot = &touch_slots[i];
699
700 if (slot->tracking_id == -1) {
701 continue;
702 }
703
704 if (update == INPUT_MULTI_TOUCH_TYPE_END) {
705 slot->tracking_id = -1;
706 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
707 needs_sync = true;
708 } else {
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,
713 0, width,
714 i, slot->tracking_id);
715 qemu_input_queue_mtt_abs(con,
716 INPUT_AXIS_Y, (int) slot->y,
717 0, height,
718 i, slot->tracking_id);
719 needs_sync = true;
720 }
721 }
722
723 if (needs_sync) {
724 qemu_input_event_sync();
725 }
726}
727
728void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
729{
730 /* display has opengl support */
731 assert(con);
732 if (con->gl) {
733 error_report("The console already has an OpenGL context.");
734 exit(1);
735 }
736 con->gl = gl;
737}
738
739static void
740dcl_set_graphic_cursor(DisplayChangeListener *dcl, QemuGraphicConsole *con)
741{
742 if (con && con->cursor && dcl->ops->dpy_cursor_define) {
743 dcl->ops->dpy_cursor_define(dcl, con->cursor);
744 }
745 if (con && dcl->ops->dpy_mouse_set) {
746 dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
747 }
748}
749
750void register_displaychangelistener(DisplayChangeListener *dcl)
751{
752 QemuConsole *con;
753
754 assert(!dcl->ds);
755
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);
760 if (dcl->con) {
761 dcl->con->dcls++;
762 con = dcl->con;
763 } else {
764 con = active_console;
765 }
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));
769 }
770 qemu_text_console_update_cursor();
771}
772
773void update_displaychangelistener(DisplayChangeListener *dcl,
774 uint64_t interval)
775{
776 DisplayState *ds = dcl->ds;
777
778 dcl->update_interval = interval;
779 if (!ds->refreshing && ds->update_interval > interval) {
780 timer_mod(ds->gui_timer, ds->last_update + interval);
781 }
782}
783
784void unregister_displaychangelistener(DisplayChangeListener *dcl)
785{
786 DisplayState *ds = dcl->ds;
787 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
788 if (dcl->con) {
789 dcl->con->dcls--;
790 }
791 QLIST_REMOVE(dcl, next);
792 dcl->ds = NULL;
793 gui_setup_refresh(ds);
794}
795
796static void dpy_set_ui_info_timer(void *opaque)
797{
798 QemuConsole *con = opaque;
799 uint32_t head = qemu_console_get_head(con);
800
801 con->hw_ops->ui_info(con->hw, head, &con->ui_info);
802}
803
804bool dpy_ui_info_supported(QemuConsole *con)
805{
806 if (con == NULL) {
807 con = active_console;
808 }
809 if (con == NULL) {
810 return false;
811 }
812
813 return con->hw_ops->ui_info != NULL;
814}
815
816const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
817{
818 if (con == NULL) {
819 con = active_console;
820 }
821
822 return &con->ui_info;
823}
824
825int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
826{
827 if (con == NULL) {
828 con = active_console;
829 }
830
831 if (!dpy_ui_info_supported(con)) {
832 return -1;
833 }
834 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
835 /* nothing changed -- ignore */
836 return 0;
837 }
838
839 /*
840 * Typically we get a flood of these as the user resizes the window.
841 * Wait until the dust has settled (one second without updates), then
842 * go notify the guest.
843 */
844 con->ui_info = *info;
845 timer_mod(con->ui_timer,
846 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
847 return 0;
848}
849
850void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
851{
852 DisplayState *s = con->ds;
853 DisplayChangeListener *dcl;
854 int width = qemu_console_get_width(con, x + w);
855 int height = qemu_console_get_height(con, y + h);
856
857 x = MAX(x, 0);
858 y = MAX(y, 0);
859 x = MIN(x, width);
860 y = MIN(y, height);
861 w = MIN(w, width - x);
862 h = MIN(h, height - y);
863
864 if (!qemu_console_is_visible(con)) {
865 return;
866 }
867 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
868 QLIST_FOREACH(dcl, &s->listeners, next) {
869 if (con != (dcl->con ? dcl->con : active_console)) {
870 continue;
871 }
872 if (dcl->ops->dpy_gfx_update) {
873 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
874 }
875 }
876}
877
878void dpy_gfx_update_full(QemuConsole *con)
879{
880 int w = qemu_console_get_width(con, 0);
881 int h = qemu_console_get_height(con, 0);
882
883 dpy_gfx_update(con, 0, 0, w, h);
884}
885
886void dpy_gfx_replace_surface(QemuConsole *con,
887 DisplaySurface *surface)
888{
889 static const char placeholder_msg[] = "Display output is not active.";
890 DisplayState *s = con->ds;
891 DisplaySurface *old_surface = con->surface;
892 DisplaySurface *new_surface = surface;
893 DisplayChangeListener *dcl;
894 int width;
895 int height;
896
897 if (!surface) {
898 if (old_surface) {
899 width = surface_width(old_surface);
900 height = surface_height(old_surface);
901 } else {
902 width = 640;
903 height = 480;
904 }
905
906 new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
907 }
908
909 assert(old_surface != new_surface);
910
911 con->scanout.kind = SCANOUT_SURFACE;
912 con->surface = new_surface;
913 dpy_gfx_create_texture(con, new_surface);
914 QLIST_FOREACH(dcl, &s->listeners, next) {
915 if (con != (dcl->con ? dcl->con : active_console)) {
916 continue;
917 }
918 displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
919 }
920 dpy_gfx_destroy_texture(con, old_surface);
921 qemu_free_displaysurface(old_surface);
922}
923
924bool dpy_gfx_check_format(QemuConsole *con,
925 pixman_format_code_t format)
926{
927 DisplayChangeListener *dcl;
928 DisplayState *s = con->ds;
929
930 QLIST_FOREACH(dcl, &s->listeners, next) {
931 if (dcl->con && dcl->con != con) {
932 /* dcl bound to another console -> skip */
933 continue;
934 }
935 if (dcl->ops->dpy_gfx_check_format) {
936 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
937 return false;
938 }
939 } else {
940 /* default is to allow native 32 bpp only */
941 if (format != qemu_default_pixman_format(32, true)) {
942 return false;
943 }
944 }
945 }
946 return true;
947}
948
949static void dpy_refresh(DisplayState *s)
950{
951 DisplayChangeListener *dcl;
952
953 QLIST_FOREACH(dcl, &s->listeners, next) {
954 if (dcl->ops->dpy_refresh) {
955 dcl->ops->dpy_refresh(dcl);
956 }
957 }
958}
959
960void dpy_text_cursor(QemuConsole *con, int x, int y)
961{
962 DisplayState *s = con->ds;
963 DisplayChangeListener *dcl;
964
965 if (!qemu_console_is_visible(con)) {
966 return;
967 }
968 QLIST_FOREACH(dcl, &s->listeners, next) {
969 if (con != (dcl->con ? dcl->con : active_console)) {
970 continue;
971 }
972 if (dcl->ops->dpy_text_cursor) {
973 dcl->ops->dpy_text_cursor(dcl, x, y);
974 }
975 }
976}
977
978void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
979{
980 DisplayState *s = con->ds;
981 DisplayChangeListener *dcl;
982
983 if (!qemu_console_is_visible(con)) {
984 return;
985 }
986 QLIST_FOREACH(dcl, &s->listeners, next) {
987 if (con != (dcl->con ? dcl->con : active_console)) {
988 continue;
989 }
990 if (dcl->ops->dpy_text_update) {
991 dcl->ops->dpy_text_update(dcl, x, y, w, h);
992 }
993 }
994}
995
996void dpy_text_resize(QemuConsole *con, int w, int h)
997{
998 DisplayState *s = con->ds;
999 DisplayChangeListener *dcl;
1000
1001 if (!qemu_console_is_visible(con)) {
1002 return;
1003 }
1004 QLIST_FOREACH(dcl, &s->listeners, next) {
1005 if (con != (dcl->con ? dcl->con : active_console)) {
1006 continue;
1007 }
1008 if (dcl->ops->dpy_text_resize) {
1009 dcl->ops->dpy_text_resize(dcl, w, h);
1010 }
1011 }
1012}
1013
1014void dpy_mouse_set(QemuConsole *c, int x, int y, int on)
1015{
1016 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1017 DisplayState *s = c->ds;
1018 DisplayChangeListener *dcl;
1019
1020 con->cursor_x = x;
1021 con->cursor_y = y;
1022 con->cursor_on = on;
1023 if (!qemu_console_is_visible(c)) {
1024 return;
1025 }
1026 QLIST_FOREACH(dcl, &s->listeners, next) {
1027 if (c != (dcl->con ? dcl->con : active_console)) {
1028 continue;
1029 }
1030 if (dcl->ops->dpy_mouse_set) {
1031 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1032 }
1033 }
1034}
1035
1036void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
1037{
1038 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1039 DisplayState *s = c->ds;
1040 DisplayChangeListener *dcl;
1041
1042 cursor_unref(con->cursor);
1043 con->cursor = cursor_ref(cursor);
1044 if (!qemu_console_is_visible(c)) {
1045 return;
1046 }
1047 QLIST_FOREACH(dcl, &s->listeners, next) {
1048 if (c != (dcl->con ? dcl->con : active_console)) {
1049 continue;
1050 }
1051 if (dcl->ops->dpy_cursor_define) {
1052 dcl->ops->dpy_cursor_define(dcl, cursor);
1053 }
1054 }
1055}
1056
1057bool dpy_cursor_define_supported(QemuConsole *con)
1058{
1059 DisplayState *s = con->ds;
1060 DisplayChangeListener *dcl;
1061
1062 QLIST_FOREACH(dcl, &s->listeners, next) {
1063 if (dcl->ops->dpy_cursor_define) {
1064 return true;
1065 }
1066 }
1067 return false;
1068}
1069
1070QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1071 struct QEMUGLParams *qparams)
1072{
1073 assert(con->gl);
1074 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1075}
1076
1077void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1078{
1079 assert(con->gl);
1080 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1081}
1082
1083int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1084{
1085 assert(con->gl);
1086 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1087}
1088
1089void dpy_gl_scanout_disable(QemuConsole *con)
1090{
1091 DisplayState *s = con->ds;
1092 DisplayChangeListener *dcl;
1093
1094 if (con->scanout.kind != SCANOUT_SURFACE) {
1095 con->scanout.kind = SCANOUT_NONE;
1096 }
1097 QLIST_FOREACH(dcl, &s->listeners, next) {
1098 if (con != (dcl->con ? dcl->con : active_console)) {
1099 continue;
1100 }
1101 if (dcl->ops->dpy_gl_scanout_disable) {
1102 dcl->ops->dpy_gl_scanout_disable(dcl);
1103 }
1104 }
1105}
1106
1107void dpy_gl_scanout_texture(QemuConsole *con,
1108 uint32_t backing_id,
1109 bool backing_y_0_top,
1110 uint32_t backing_width,
1111 uint32_t backing_height,
1112 uint32_t x, uint32_t y,
1113 uint32_t width, uint32_t height,
1114 void *d3d_tex2d)
1115{
1116 DisplayState *s = con->ds;
1117 DisplayChangeListener *dcl;
1118
1119 con->scanout.kind = SCANOUT_TEXTURE;
1120 con->scanout.texture = (ScanoutTexture) {
1121 backing_id, backing_y_0_top, backing_width, backing_height,
1122 x, y, width, height, d3d_tex2d,
1123 };
1124 QLIST_FOREACH(dcl, &s->listeners, next) {
1125 if (con != (dcl->con ? dcl->con : active_console)) {
1126 continue;
1127 }
1128 if (dcl->ops->dpy_gl_scanout_texture) {
1129 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1130 backing_y_0_top,
1131 backing_width, backing_height,
1132 x, y, width, height,
1133 d3d_tex2d);
1134 }
1135 }
1136}
1137
1138void dpy_gl_scanout_dmabuf(QemuConsole *con,
1139 QemuDmaBuf *dmabuf)
1140{
1141 DisplayState *s = con->ds;
1142 DisplayChangeListener *dcl;
1143
1144 con->scanout.kind = SCANOUT_DMABUF;
1145 con->scanout.dmabuf = dmabuf;
1146 QLIST_FOREACH(dcl, &s->listeners, next) {
1147 if (con != (dcl->con ? dcl->con : active_console)) {
1148 continue;
1149 }
1150 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1151 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1152 }
1153 }
1154}
1155
1156void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1157 bool have_hot, uint32_t hot_x, uint32_t hot_y)
1158{
1159 DisplayState *s = con->ds;
1160 DisplayChangeListener *dcl;
1161
1162 QLIST_FOREACH(dcl, &s->listeners, next) {
1163 if (con != (dcl->con ? dcl->con : active_console)) {
1164 continue;
1165 }
1166 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1167 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
1168 have_hot, hot_x, hot_y);
1169 }
1170 }
1171}
1172
1173void dpy_gl_cursor_position(QemuConsole *con,
1174 uint32_t pos_x, uint32_t pos_y)
1175{
1176 DisplayState *s = con->ds;
1177 DisplayChangeListener *dcl;
1178
1179 QLIST_FOREACH(dcl, &s->listeners, next) {
1180 if (con != (dcl->con ? dcl->con : active_console)) {
1181 continue;
1182 }
1183 if (dcl->ops->dpy_gl_cursor_position) {
1184 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1185 }
1186 }
1187}
1188
1189void dpy_gl_release_dmabuf(QemuConsole *con,
1190 QemuDmaBuf *dmabuf)
1191{
1192 DisplayState *s = con->ds;
1193 DisplayChangeListener *dcl;
1194
1195 QLIST_FOREACH(dcl, &s->listeners, next) {
1196 if (con != (dcl->con ? dcl->con : active_console)) {
1197 continue;
1198 }
1199 if (dcl->ops->dpy_gl_release_dmabuf) {
1200 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
1201 }
1202 }
1203}
1204
1205void dpy_gl_update(QemuConsole *con,
1206 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1207{
1208 DisplayState *s = con->ds;
1209 DisplayChangeListener *dcl;
1210
1211 assert(con->gl);
1212
1213 graphic_hw_gl_block(con, true);
1214 QLIST_FOREACH(dcl, &s->listeners, next) {
1215 if (con != (dcl->con ? dcl->con : active_console)) {
1216 continue;
1217 }
1218 if (dcl->ops->dpy_gl_update) {
1219 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
1220 }
1221 }
1222 graphic_hw_gl_block(con, false);
1223}
1224
1225/***********************************************************/
1226/* register display */
1227
1228/* console.c internal use only */
1229static DisplayState *get_alloc_displaystate(void)
1230{
1231 if (!display_state) {
1232 display_state = g_new0(DisplayState, 1);
1233 }
1234 return display_state;
1235}
1236
1237/*
1238 * Called by main(), after creating QemuConsoles
1239 * and before initializing ui (sdl/vnc/...).
1240 */
1241DisplayState *init_displaystate(void)
1242{
1243 gchar *name;
1244 QemuConsole *con;
1245
1246 QTAILQ_FOREACH(con, &consoles, next) {
1247 /* Hook up into the qom tree here (not in object_new()), once
1248 * all QemuConsoles are created and the order / numbering
1249 * doesn't change any more */
1250 name = g_strdup_printf("console[%d]", con->index);
1251 object_property_add_child(container_get(object_get_root(), "/backend"),
1252 name, OBJECT(con));
1253 g_free(name);
1254 }
1255
1256 return display_state;
1257}
1258
1259void graphic_console_set_hwops(QemuConsole *con,
1260 const GraphicHwOps *hw_ops,
1261 void *opaque)
1262{
1263 con->hw_ops = hw_ops;
1264 con->hw = opaque;
1265}
1266
1267QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1268 const GraphicHwOps *hw_ops,
1269 void *opaque)
1270{
1271 static const char noinit[] =
1272 "Guest has not initialized the display (yet).";
1273 int width = 640;
1274 int height = 480;
1275 QemuConsole *s;
1276 DisplaySurface *surface;
1277
1278 s = qemu_graphic_console_lookup_unused();
1279 if (s) {
1280 trace_console_gfx_reuse(s->index);
1281 width = qemu_console_get_width(s, 0);
1282 height = qemu_console_get_height(s, 0);
1283 } else {
1284 trace_console_gfx_new();
1285 s = (QemuConsole *)object_new(TYPE_QEMU_GRAPHIC_CONSOLE);
1286 }
1287 QEMU_GRAPHIC_CONSOLE(s)->head = head;
1288 graphic_console_set_hwops(s, hw_ops, opaque);
1289 if (dev) {
1290 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
1291 &error_abort);
1292 }
1293
1294 surface = qemu_create_placeholder_surface(width, height, noinit);
1295 dpy_gfx_replace_surface(s, surface);
1296 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1297 graphic_hw_gl_unblock_timer, s);
1298 return s;
1299}
1300
1301static const GraphicHwOps unused_ops = {
1302 /* no callbacks */
1303};
1304
1305void graphic_console_close(QemuConsole *con)
1306{
1307 static const char unplugged[] =
1308 "Guest display has been unplugged";
1309 DisplaySurface *surface;
1310 int width = qemu_console_get_width(con, 640);
1311 int height = qemu_console_get_height(con, 480);
1312
1313 trace_console_gfx_close(con->index);
1314 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
1315 graphic_console_set_hwops(con, &unused_ops, NULL);
1316
1317 if (con->gl) {
1318 dpy_gl_scanout_disable(con);
1319 }
1320 surface = qemu_create_placeholder_surface(width, height, unplugged);
1321 dpy_gfx_replace_surface(con, surface);
1322}
1323
1324QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1325{
1326 QemuConsole *con;
1327
1328 QTAILQ_FOREACH(con, &consoles, next) {
1329 if (con->index == index) {
1330 return con;
1331 }
1332 }
1333 return NULL;
1334}
1335
1336QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1337{
1338 QemuConsole *con;
1339 Object *obj;
1340 uint32_t h;
1341
1342 QTAILQ_FOREACH(con, &consoles, next) {
1343 obj = object_property_get_link(OBJECT(con),
1344 "device", &error_abort);
1345 if (DEVICE(obj) != dev) {
1346 continue;
1347 }
1348 h = object_property_get_uint(OBJECT(con),
1349 "head", &error_abort);
1350 if (h != head) {
1351 continue;
1352 }
1353 return con;
1354 }
1355 return NULL;
1356}
1357
1358QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1359 uint32_t head, Error **errp)
1360{
1361 DeviceState *dev;
1362 QemuConsole *con;
1363
1364 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1365 if (dev == NULL) {
1366 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1367 "Device '%s' not found", device_id);
1368 return NULL;
1369 }
1370
1371 con = qemu_console_lookup_by_device(dev, head);
1372 if (con == NULL) {
1373 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1374 device_id, head);
1375 return NULL;
1376 }
1377
1378 return con;
1379}
1380
1381static QemuConsole *qemu_graphic_console_lookup_unused(void)
1382{
1383 QemuConsole *con;
1384 Object *obj;
1385
1386 QTAILQ_FOREACH(con, &consoles, next) {
1387 if (!QEMU_IS_GRAPHIC_CONSOLE(con) || con->hw_ops != &unused_ops) {
1388 continue;
1389 }
1390 obj = object_property_get_link(OBJECT(con),
1391 "device", &error_abort);
1392 if (obj != NULL) {
1393 continue;
1394 }
1395 return con;
1396 }
1397 return NULL;
1398}
1399
1400QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
1401{
1402 if (con == NULL) {
1403 con = active_console;
1404 }
1405 return QEMU_IS_GRAPHIC_CONSOLE(con) ? QEMU_GRAPHIC_CONSOLE(con)->cursor : NULL;
1406}
1407
1408bool qemu_console_is_visible(QemuConsole *con)
1409{
1410 return (con == active_console) || (con->dcls > 0);
1411}
1412
1413bool qemu_console_is_graphic(QemuConsole *con)
1414{
1415 if (con == NULL) {
1416 con = active_console;
1417 }
1418 return con && QEMU_IS_GRAPHIC_CONSOLE(con);
1419}
1420
1421bool qemu_console_is_fixedsize(QemuConsole *con)
1422{
1423 if (con == NULL) {
1424 con = active_console;
1425 }
1426 return con && (QEMU_IS_GRAPHIC_CONSOLE(con) || QEMU_IS_FIXED_TEXT_CONSOLE(con));
1427}
1428
1429bool qemu_console_is_gl_blocked(QemuConsole *con)
1430{
1431 assert(con != NULL);
1432 return con->gl_block;
1433}
1434
1435bool qemu_console_is_multihead(DeviceState *dev)
1436{
1437 QemuConsole *con;
1438 Object *obj;
1439 uint32_t f = 0xffffffff;
1440 uint32_t h;
1441
1442 QTAILQ_FOREACH(con, &consoles, next) {
1443 obj = object_property_get_link(OBJECT(con),
1444 "device", &error_abort);
1445 if (DEVICE(obj) != dev) {
1446 continue;
1447 }
1448
1449 h = object_property_get_uint(OBJECT(con),
1450 "head", &error_abort);
1451 if (f == 0xffffffff) {
1452 f = h;
1453 } else if (h != f) {
1454 return true;
1455 }
1456 }
1457 return false;
1458}
1459
1460char *qemu_console_get_label(QemuConsole *con)
1461{
1462 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1463 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(con);
1464 if (c->device) {
1465 DeviceState *dev;
1466 bool multihead;
1467
1468 dev = DEVICE(c->device);
1469 multihead = qemu_console_is_multihead(dev);
1470 if (multihead) {
1471 return g_strdup_printf("%s.%d", dev->id ?
1472 dev->id :
1473 object_get_typename(c->device),
1474 c->head);
1475 } else {
1476 return g_strdup_printf("%s", dev->id ?
1477 dev->id :
1478 object_get_typename(c->device));
1479 }
1480 }
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));
1484 if (label) {
1485 return g_strdup(label);
1486 }
1487 }
1488
1489 return g_strdup_printf("vc%d", con->index);
1490}
1491
1492int qemu_console_get_index(QemuConsole *con)
1493{
1494 if (con == NULL) {
1495 con = active_console;
1496 }
1497 return con ? con->index : -1;
1498}
1499
1500uint32_t qemu_console_get_head(QemuConsole *con)
1501{
1502 if (con == NULL) {
1503 con = active_console;
1504 }
1505 if (con == NULL) {
1506 return -1;
1507 }
1508 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1509 return QEMU_GRAPHIC_CONSOLE(con)->head;
1510 }
1511 return 0;
1512}
1513
1514int qemu_console_get_width(QemuConsole *con, int fallback)
1515{
1516 if (con == NULL) {
1517 con = active_console;
1518 }
1519 if (con == NULL) {
1520 return fallback;
1521 }
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);
1529 default:
1530 return fallback;
1531 }
1532}
1533
1534int qemu_console_get_height(QemuConsole *con, int fallback)
1535{
1536 if (con == NULL) {
1537 con = active_console;
1538 }
1539 if (con == NULL) {
1540 return fallback;
1541 }
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);
1549 default:
1550 return fallback;
1551 }
1552}
1553
1554int qemu_invalidate_text_consoles(void)
1555{
1556 QemuConsole *s;
1557 int count = 0;
1558
1559 QTAILQ_FOREACH(s, &consoles, next) {
1560 if (qemu_console_is_graphic(s) ||
1561 !qemu_console_is_visible(s)) {
1562 continue;
1563 }
1564 count++;
1565 graphic_hw_invalidate(s);
1566 }
1567
1568 return count;
1569}
1570
1571void qemu_console_resize(QemuConsole *s, int width, int height)
1572{
1573 DisplaySurface *surface = qemu_console_surface(s);
1574
1575 assert(QEMU_IS_GRAPHIC_CONSOLE(s));
1576
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) {
1581 return;
1582 }
1583
1584 surface = qemu_create_displaysurface(width, height);
1585 dpy_gfx_replace_surface(s, surface);
1586}
1587
1588DisplaySurface *qemu_console_surface(QemuConsole *console)
1589{
1590 switch (console->scanout.kind) {
1591 case SCANOUT_SURFACE:
1592 return console->surface;
1593 default:
1594 return NULL;
1595 }
1596}
1597
1598PixelFormat qemu_default_pixelformat(int bpp)
1599{
1600 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
1601 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
1602 return pf;
1603}
1604
1605static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
1606
1607void qemu_display_register(QemuDisplay *ui)
1608{
1609 assert(ui->type < DISPLAY_TYPE__MAX);
1610 dpys[ui->type] = ui;
1611}
1612
1613bool qemu_display_find_default(DisplayOptions *opts)
1614{
1615 static DisplayType prio[] = {
1616#if defined(CONFIG_GTK)
1617 DISPLAY_TYPE_GTK,
1618#endif
1619#if defined(CONFIG_SDL)
1620 DISPLAY_TYPE_SDL,
1621#endif
1622#if defined(CONFIG_COCOA)
1623 DISPLAY_TYPE_COCOA
1624#endif
1625 };
1626 int i;
1627
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);
1632 if (rv < 0) {
1633 error_report_err(local_err);
1634 }
1635 }
1636 if (dpys[prio[i]] == NULL) {
1637 continue;
1638 }
1639 opts->type = prio[i];
1640 return true;
1641 }
1642 return false;
1643}
1644
1645void qemu_display_early_init(DisplayOptions *opts)
1646{
1647 assert(opts->type < DISPLAY_TYPE__MAX);
1648 if (opts->type == DISPLAY_TYPE_NONE) {
1649 return;
1650 }
1651 if (dpys[opts->type] == NULL) {
1652 Error *local_err = NULL;
1653 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
1654 if (rv < 0) {
1655 error_report_err(local_err);
1656 }
1657 }
1658 if (dpys[opts->type] == NULL) {
1659 error_report("Display '%s' is not available.",
1660 DisplayType_str(opts->type));
1661 exit(1);
1662 }
1663 if (dpys[opts->type]->early_init) {
1664 dpys[opts->type]->early_init(opts);
1665 }
1666}
1667
1668void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
1669{
1670 assert(opts->type < DISPLAY_TYPE__MAX);
1671 if (opts->type == DISPLAY_TYPE_NONE) {
1672 return;
1673 }
1674 assert(dpys[opts->type] != NULL);
1675 dpys[opts->type]->init(ds, opts);
1676}
1677
1678void qemu_display_help(void)
1679{
1680 int idx;
1681
1682 printf("Available display backend types:\n");
1683 printf("none\n");
1684 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
1685 if (!dpys[idx]) {
1686 Error *local_err = NULL;
1687 int rv = ui_module_load(DisplayType_str(idx), &local_err);
1688 if (rv < 0) {
1689 error_report_err(local_err);
1690 }
1691 }
1692 if (dpys[idx]) {
1693 printf("%s\n", DisplayType_str(dpys[idx]->type));
1694 }
1695 }
1696}