]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
virtio-gpu/win32: set the destroy function on load
[mirror_qemu.git] / ui / console.c
CommitLineData
e7f0ad58
FB
1/*
2 * QEMU graphical console
5fafdf24 3 *
e7f0ad58 4 * Copyright (c) 2004 Fabrice Bellard
5fafdf24 5 *
e7f0ad58
FB
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 */
e688df6b 24
e16f4c87 25#include "qemu/osdep.h"
28ecbaee 26#include "ui/console.h"
aa2beaa1 27#include "hw/qdev-core.h"
e688df6b 28#include "qapi/error.h"
9af23989 29#include "qapi/qapi-commands-ui.h"
098d57e7 30#include "qapi/visitor.h"
68ba85ce 31#include "qemu/coroutine.h"
4f2c765b 32#include "qemu/error-report.h"
014b00cc 33#include "qemu/main-loop.h"
0b8fa32f 34#include "qemu/module.h"
922a01a0 35#include "qemu/option.h"
014b00cc 36#include "chardev/char.h"
ac86048b 37#include "trace.h"
a77549b3 38#include "exec/memory.h"
db1015e9 39#include "qom/object.h"
e7f0ad58 40
6f110819 41#include "console-priv.h"
e7f0ad58 42
c105d60f 43OBJECT_DEFINE_ABSTRACT_TYPE(QemuConsole, qemu_console, QEMU_CONSOLE, OBJECT)
e265917c 44
b208f745
MAL
45typedef struct QemuGraphicConsole {
46 QemuConsole parent;
58d58708
MAL
47
48 Object *device;
49 uint32_t head;
50
51 QEMUCursor *cursor;
52 int cursor_x, cursor_y, cursor_on;
b208f745
MAL
53} QemuGraphicConsole;
54
55typedef QemuConsoleClass QemuGraphicConsoleClass;
56
b208f745
MAL
57OBJECT_DEFINE_TYPE(QemuGraphicConsole, qemu_graphic_console, QEMU_GRAPHIC_CONSOLE, QEMU_CONSOLE)
58
27be5587 59struct DisplayState {
1246b259 60 QEMUTimer *gui_timer;
0f7b2864
GH
61 uint64_t last_update;
62 uint64_t update_interval;
63 bool refreshing;
27be5587
GH
64
65 QLIST_HEAD(, DisplayChangeListener) listeners;
66};
67
98b50080 68static DisplayState *display_state;
76ffb0b4 69static QemuConsole *active_console;
eae3eb3e 70static QTAILQ_HEAD(, QemuConsole) consoles =
cd6cd8fa 71 QTAILQ_HEAD_INITIALIZER(consoles);
e7f0ad58 72
0f7b2864 73static void dpy_refresh(DisplayState *s);
5209089f 74static DisplayState *get_alloc_displaystate(void);
ebced091 75static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
4b7b661d
MAL
76static bool console_compatible_with(QemuConsole *con,
77 DisplayChangeListener *dcl, Error **errp);
f9411aae 78static QemuConsole *qemu_graphic_console_lookup_unused(void);
cfde05d1 79static void dpy_set_ui_info_timer(void *opaque);
64840c66 80
98a9ad90
GH
81static void gui_update(void *opaque)
82{
0f7b2864
GH
83 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
84 uint64_t dcl_interval;
98a9ad90
GH
85 DisplayState *ds = opaque;
86 DisplayChangeListener *dcl;
87
0f7b2864 88 ds->refreshing = true;
98a9ad90 89 dpy_refresh(ds);
0f7b2864 90 ds->refreshing = false;
98a9ad90
GH
91
92 QLIST_FOREACH(dcl, &ds->listeners, next) {
0f7b2864
GH
93 dcl_interval = dcl->update_interval ?
94 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
95 if (interval > dcl_interval) {
96 interval = dcl_interval;
98a9ad90
GH
97 }
98 }
0f7b2864
GH
99 if (ds->update_interval != interval) {
100 ds->update_interval = interval;
101 trace_console_refresh(interval);
102 }
bc72ad67
AB
103 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
104 timer_mod(ds->gui_timer, ds->last_update + interval);
98a9ad90
GH
105}
106
107static void gui_setup_refresh(DisplayState *ds)
108{
109 DisplayChangeListener *dcl;
110 bool need_timer = false;
98a9ad90
GH
111
112 QLIST_FOREACH(dcl, &ds->listeners, next) {
113 if (dcl->ops->dpy_refresh != NULL) {
114 need_timer = true;
115 }
98a9ad90
GH
116 }
117
118 if (need_timer && ds->gui_timer == NULL) {
bc72ad67
AB
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));
98a9ad90
GH
121 }
122 if (!need_timer && ds->gui_timer != NULL) {
bc72ad67 123 timer_free(ds->gui_timer);
98a9ad90
GH
124 ds->gui_timer = NULL;
125 }
98a9ad90
GH
126}
127
4d631621
MAL
128void graphic_hw_update_done(QemuConsole *con)
129{
6fc5183a 130 if (con) {
d6ee15ad 131 qemu_co_enter_all(&con->dump_queue, NULL);
6fc5183a 132 }
4d631621
MAL
133}
134
1dbfa005 135void graphic_hw_update(QemuConsole *con)
95219897 136{
4d631621 137 bool async = false;
1cd8b948 138 con = con ? con : active_console;
1dbfa005 139 if (!con) {
1cd8b948 140 return;
1dbfa005 141 }
1cd8b948 142 if (con->hw_ops->gfx_update) {
380cd056 143 con->hw_ops->gfx_update(con->hw);
4d631621
MAL
144 async = con->hw_ops->gfx_update_async;
145 }
146 if (!async) {
147 graphic_hw_update_done(con);
1dbfa005 148 }
95219897
PB
149}
150
4f2c765b
MAL
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
a9b1e471
MAL
167static void graphic_hw_gl_unblock_timer(void *opaque)
168{
169 warn_report("console: no gl-unblock within one second");
170}
171
bba19b88
GH
172void graphic_hw_gl_block(QemuConsole *con, bool block)
173{
a9b1e471 174 uint64_t timeout;
f607867c
GH
175 assert(con != NULL);
176
a4ddc314
MAL
177 if (block) {
178 con->gl_block++;
179 } else {
180 con->gl_block--;
bba19b88 181 }
a4ddc314
MAL
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);
a9b1e471
MAL
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 }
bba19b88
GH
198}
199
b3cb21b9
ST
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
1dbfa005 210void graphic_hw_invalidate(QemuConsole *con)
95219897 211{
1dbfa005
GH
212 if (!con) {
213 con = active_console;
214 }
380cd056
GH
215 if (con && con->hw_ops->invalidate) {
216 con->hw_ops->invalidate(con->hw);
1dbfa005 217 }
95219897
PB
218}
219
1dbfa005 220void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
4d3b6f6e 221{
1dbfa005
GH
222 if (!con) {
223 con = active_console;
224 }
380cd056
GH
225 if (con && con->hw_ops->text_update) {
226 con->hw_ops->text_update(con->hw, chardata);
227 }
4d3b6f6e
AZ
228}
229
26b032b9 230static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
c84ab0a5
MAL
231 struct DisplaySurface *new_surface,
232 bool update)
26b032b9
MAL
233{
234 if (dcl->ops->dpy_gfx_switch) {
235 dcl->ops->dpy_gfx_switch(dcl, new_surface);
236 }
c84ab0a5
MAL
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 }
26b032b9
MAL
243}
244
589089fe
MAL
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}
26b032b9 266
ebced091 267static void displaychangelistener_display_console(DisplayChangeListener *dcl,
4b7b661d
MAL
268 QemuConsole *con,
269 Error **errp)
ebced091
MAL
270{
271 static const char nodev[] =
272 "This VM has no graphic display device.";
273 static DisplaySurface *dummy;
274
4b7b661d 275 if (!con || !console_compatible_with(con, dcl, errp)) {
ebced091
MAL
276 if (!dummy) {
277 dummy = qemu_create_placeholder_surface(640, 480, nodev);
278 }
589089fe
MAL
279 if (con) {
280 dpy_gfx_create_texture(con, dummy);
281 }
c84ab0a5 282 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
ebced091
MAL
283 return;
284 }
285
e1c676a2
MAL
286 dpy_gfx_create_texture(con, con->surface);
287 displaychangelistener_gfx_switch(dcl, con->surface,
288 con->scanout.kind == SCANOUT_SURFACE);
289
ebced091
MAL
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,
bf41ab61
MAL
303 con->scanout.texture.height,
304 con->scanout.texture.d3d_tex2d);
ebced091 305 }
ebced091
MAL
306}
307
e7f0ad58
FB
308void console_select(unsigned int index)
309{
284d1c6b 310 DisplayChangeListener *dcl;
76ffb0b4 311 QemuConsole *s;
6d6f7c28 312
437fe106 313 trace_console_select(index);
284d1c6b 314 s = qemu_console_lookup_by_index(index);
e7f0ad58 315 if (s) {
7d957bd8 316 DisplayState *ds = s->ds;
bf1bed81 317
e7f0ad58 318 active_console = s;
074b2409
MAL
319 QLIST_FOREACH (dcl, &ds->listeners, next) {
320 if (dcl->con != NULL) {
321 continue;
2e5567c9 322 }
074b2409 323 displaychangelistener_display_console(dcl, s, NULL);
a93a4a22 324 }
b2bb9cc4
MAL
325
326 if (QEMU_IS_TEXT_CONSOLE(s)) {
f7ce755d 327 qemu_text_console_select(QEMU_TEXT_CONSOLE(s));
b2bb9cc4 328 }
e7f0ad58
FB
329 }
330}
331
f7ce755d
MAL
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
7fb1cf16 344static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
50ef4679
GH
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,
df6322a8 354 [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
344aa283 355 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
50ef4679
GH
356};
357
da024b1e
GH
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
cc6ba2c6 369bool qemu_text_console_put_qcode(QemuTextConsole *s, int qcode, bool ctrl)
50ef4679
GH
370{
371 int keysym;
372
da024b1e 373 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
50ef4679
GH
374 if (keysym == 0) {
375 return false;
376 }
cc6ba2c6 377 qemu_text_console_put_keysym(s, keysym);
50ef4679
GH
378 return true;
379}
380
cc6ba2c6 381void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len)
bdef9724
GH
382{
383 int i;
384
385 for (i = 0; i < len && str[i]; i++) {
cc6ba2c6 386 qemu_text_console_put_keysym(s, str[i]);
bdef9724
GH
387 }
388}
389
098d57e7 390static void
c105d60f 391qemu_console_register(QemuConsole *c)
e7f0ad58 392{
95219897 393 int i;
e7f0ad58 394
c105d60f
MAL
395 if (!active_console || (!QEMU_IS_GRAPHIC_CONSOLE(active_console) &&
396 QEMU_IS_GRAPHIC_CONSOLE(c))) {
098d57e7 397 active_console = c;
af3a9031 398 }
a1d2db08 399
cd6cd8fa 400 if (QTAILQ_EMPTY(&consoles)) {
098d57e7
MAL
401 c->index = 0;
402 QTAILQ_INSERT_TAIL(&consoles, c, next);
c105d60f 403 } else if (!QEMU_IS_GRAPHIC_CONSOLE(c) || phase_check(PHASE_MACHINE_READY)) {
eae3eb3e 404 QemuConsole *last = QTAILQ_LAST(&consoles);
098d57e7
MAL
405 c->index = last->index + 1;
406 QTAILQ_INSERT_TAIL(&consoles, c, next);
95219897 407 } else {
9588d67e
GH
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 */
098d57e7 414 QemuConsole *it = QTAILQ_FIRST(&consoles);
cd6cd8fa 415
c105d60f 416 while (QTAILQ_NEXT(it, next) != NULL && QEMU_IS_GRAPHIC_CONSOLE(it)) {
098d57e7 417 it = QTAILQ_NEXT(it, next);
cd6cd8fa 418 }
c105d60f 419 if (QEMU_IS_GRAPHIC_CONSOLE(it)) {
cd6cd8fa 420 /* have no text consoles */
098d57e7
MAL
421 c->index = it->index + 1;
422 QTAILQ_INSERT_AFTER(&consoles, it, c, next);
cd6cd8fa 423 } else {
098d57e7
MAL
424 c->index = it->index;
425 QTAILQ_INSERT_BEFORE(it, c, next);
cd6cd8fa 426 /* renumber text consoles */
098d57e7
MAL
427 for (i = c->index + 1; it != NULL; it = QTAILQ_NEXT(it, next), i++) {
428 it->index = i;
cd6cd8fa 429 }
95219897 430 }
95219897 431 }
95219897
PB
432}
433
e265917c
MAL
434static void
435qemu_console_finalize(Object *obj)
436{
cfde05d1
MAL
437 QemuConsole *c = QEMU_CONSOLE(obj);
438
463c6b19 439 /* TODO: check this code path, and unregister from consoles */
463c6b19
MAL
440 g_clear_pointer(&c->surface, qemu_free_displaysurface);
441 g_clear_pointer(&c->gl_unblock_timer, timer_free);
cfde05d1 442 g_clear_pointer(&c->ui_timer, timer_free);
098d57e7
MAL
443}
444
e265917c
MAL
445static void
446qemu_console_class_init(ObjectClass *oc, void *data)
447{
448}
449
450static void
451qemu_console_init(Object *obj)
452{
098d57e7
MAL
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;
cfde05d1
MAL
459 c->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
460 dpy_set_ui_info_timer, c);
ba0ec5c2 461 qemu_console_register(c);
098d57e7
MAL
462}
463
b208f745
MAL
464static void
465qemu_graphic_console_finalize(Object *obj)
466{
58d58708
MAL
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);
b208f745
MAL
479}
480
481static void
482qemu_graphic_console_class_init(ObjectClass *oc, void *data)
483{
58d58708
MAL
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);
b208f745
MAL
491}
492
493static void
494qemu_graphic_console_init(Object *obj)
495{
496}
497
09b4c198
MAL
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
eb69442a 527DisplaySurface *qemu_create_displaysurface(int width, int height)
ffe8b821 528{
09b4c198
MAL
529 DisplaySurface *surface;
530 void *bits = NULL;
531#ifdef WIN32
532 HANDLE handle = NULL;
533#endif
69c77777 534
09b4c198
MAL
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 );
30f1e661 546 surface->flags = QEMU_ALLOCATED_FLAG;
98b50080 547
09b4c198
MAL
548#ifdef WIN32
549 qemu_displaysurface_win32_set_handle(surface, handle, 0);
550#endif
537a4391
GH
551 return surface;
552}
553
30f1e661
GH
554DisplaySurface *qemu_create_displaysurface_from(int width, int height,
555 pixman_format_code_t format,
556 int linesize, uint8_t *data)
98b50080 557{
69c77777 558 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 559
30f1e661 560 trace_displaysurface_create_from(surface, width, height, format);
ff174c67 561 surface->image = pixman_image_create_bits(format,
69c77777
GH
562 width, height,
563 (void *)data, linesize);
564 assert(surface->image != NULL);
09b4c198
MAL
565#ifdef WIN32
566 pixman_image_set_destroy_function(surface->image,
567 win32_pixman_image_destroy, surface);
568#endif
69c77777 569
98b50080
PB
570 return surface;
571}
572
ca58b45f
GH
573DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
574{
575 DisplaySurface *surface = g_new0(DisplaySurface, 1);
576
577 trace_displaysurface_create_pixman(surface);
ca58b45f
GH
578 surface->image = pixman_image_ref(image);
579
580 return surface;
581}
582
b5a087b0
AO
583DisplaySurface *qemu_create_placeholder_surface(int w, int h,
584 const char *msg)
d3002b04 585{
521a580d 586 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1ece6777
MAL
587 pixman_color_t bg = QEMU_PIXMAN_COLOR_BLACK;
588 pixman_color_t fg = QEMU_PIXMAN_COLOR_GRAY;
d3002b04
GH
589 pixman_image_t *glyph;
590 int len, x, y, i;
591
592 len = strlen(msg);
521a580d
GH
593 x = (w / FONT_WIDTH - len) / 2;
594 y = (h / FONT_HEIGHT - 1) / 2;
d3002b04
GH
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 }
b5a087b0 601 surface->flags |= QEMU_PLACEHOLDER_FLAG;
d3002b04
GH
602 return surface;
603}
604
da229ef3 605void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 606{
da229ef3 607 if (surface == NULL) {
98b50080 608 return;
187cd1d9 609 }
da229ef3
GH
610 trace_displaysurface_free(surface);
611 qemu_pixman_image_unref(surface->image);
612 g_free(surface);
98b50080
PB
613}
614
06020b95
GH
615bool console_has_gl(QemuConsole *con)
616{
617 return con->gl != NULL;
618}
619
d0e137bc
MAL
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
4b7b661d
MAL
633static bool console_compatible_with(QemuConsole *con,
634 DisplayChangeListener *dcl, Error **errp)
5983fdf1 635{
5983fdf1
MAL
636 int flags;
637
638 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
639
a62c4a17
MAL
640 if (console_has_gl(con) &&
641 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
398d1c91
MAL
642 error_setg(errp, "Display %s is incompatible with the GL context",
643 dcl->ops->dpy_name);
644 return false;
645 }
646
5983fdf1
MAL
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
b6596785
BE
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
5e79d516 728void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
4f418149
MAL
729{
730 /* display has opengl support */
5e79d516
MAL
731 assert(con);
732 if (con->gl) {
733 error_report("The console already has an OpenGL context.");
4f418149
MAL
734 exit(1);
735 }
5e79d516
MAL
736 con->gl = gl;
737}
738
58d58708
MAL
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}
6f110819 749
5209089f 750void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 751{
284d1c6b
GH
752 QemuConsole *con;
753
e0665c3b
MAL
754 assert(!dcl->ds);
755
7c20b4a3 756 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
757 dcl->ds = get_alloc_displaystate();
758 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
759 gui_setup_refresh(dcl->ds);
284d1c6b
GH
760 if (dcl->con) {
761 dcl->con->dcls++;
762 con = dcl->con;
763 } else {
764 con = active_console;
765 }
4b7b661d 766 displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
58d58708
MAL
767 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
768 dcl_set_graphic_cursor(dcl, QEMU_GRAPHIC_CONSOLE(con));
6effaa16 769 }
6f110819 770 qemu_text_console_update_cursor();
7c20b4a3
GH
771}
772
0f7b2864
GH
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) {
bc72ad67 780 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
781 }
782}
783
7c20b4a3
GH
784void unregister_displaychangelistener(DisplayChangeListener *dcl)
785{
786 DisplayState *ds = dcl->ds;
787 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
788 if (dcl->con) {
789 dcl->con->dcls--;
790 }
7c20b4a3 791 QLIST_REMOVE(dcl, next);
777c5f1e 792 dcl->ds = NULL;
7c20b4a3
GH
793 gui_setup_refresh(ds);
794}
795
cf1ecc82
GH
796static void dpy_set_ui_info_timer(void *opaque)
797{
798 QemuConsole *con = opaque;
58d58708 799 uint32_t head = qemu_console_get_head(con);
cf1ecc82 800
58d58708 801 con->hw_ops->ui_info(con->hw, head, &con->ui_info);
cf1ecc82
GH
802}
803
b7fb49f0
GH
804bool dpy_ui_info_supported(QemuConsole *con)
805{
5c4b107f
GH
806 if (con == NULL) {
807 con = active_console;
808 }
809
b7fb49f0
GH
810 return con->hw_ops->ui_info != NULL;
811}
812
5eaf1e48
MAL
813const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
814{
5c4b107f
GH
815 if (con == NULL) {
816 con = active_console;
817 }
5eaf1e48
MAL
818
819 return &con->ui_info;
820}
821
ca19ef52 822int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
6f90f3d7 823{
5c4b107f
GH
824 if (con == NULL) {
825 con = active_console;
826 }
1185fde4 827
b7fb49f0 828 if (!dpy_ui_info_supported(con)) {
cf1ecc82 829 return -1;
6f90f3d7 830 }
1185fde4
GH
831 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
832 /* nothing changed -- ignore */
833 return 0;
834 }
cf1ecc82
GH
835
836 /*
837 * Typically we get a flood of these as the user resizes the window.
838 * Wait until the dust has settled (one second without updates), then
839 * go notify the guest.
840 */
1185fde4 841 con->ui_info = *info;
ca19ef52
MAL
842 timer_mod(con->ui_timer,
843 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
cf1ecc82 844 return 0;
6f90f3d7
GH
845}
846
c78f7137 847void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 848{
c78f7137 849 DisplayState *s = con->ds;
284d1c6b 850 DisplayChangeListener *dcl;
ebced091
MAL
851 int width = qemu_console_get_width(con, x + w);
852 int height = qemu_console_get_height(con, y + h);
7c20b4a3
GH
853
854 x = MAX(x, 0);
855 y = MAX(y, 0);
856 x = MIN(x, width);
857 y = MIN(y, height);
858 w = MIN(w, width - x);
859 h = MIN(h, height - y);
860
81c0d5a6 861 if (!qemu_console_is_visible(con)) {
321f048d
GH
862 return;
863 }
589089fe 864 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
7c20b4a3 865 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
866 if (con != (dcl->con ? dcl->con : active_console)) {
867 continue;
868 }
7c20b4a3 869 if (dcl->ops->dpy_gfx_update) {
bc2ed970 870 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
871 }
872 }
873}
874
7cd0afe6
TZ
875void dpy_gfx_update_full(QemuConsole *con)
876{
ebced091
MAL
877 int w = qemu_console_get_width(con, 0);
878 int h = qemu_console_get_height(con, 0);
879
880 dpy_gfx_update(con, 0, 0, w, h);
7cd0afe6
TZ
881}
882
321f048d
GH
883void dpy_gfx_replace_surface(QemuConsole *con,
884 DisplaySurface *surface)
885{
c821a58e 886 static const char placeholder_msg[] = "Display output is not active.";
321f048d
GH
887 DisplayState *s = con->ds;
888 DisplaySurface *old_surface = con->surface;
0d0be876 889 DisplaySurface *new_surface = surface;
284d1c6b 890 DisplayChangeListener *dcl;
c821a58e
AO
891 int width;
892 int height;
893
894 if (!surface) {
895 if (old_surface) {
896 width = surface_width(old_surface);
897 height = surface_height(old_surface);
898 } else {
899 width = 640;
900 height = 480;
901 }
902
0d0be876 903 new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
c821a58e 904 }
321f048d 905
0d0be876 906 assert(old_surface != new_surface);
6905b934 907
ebced091 908 con->scanout.kind = SCANOUT_SURFACE;
0d0be876
DK
909 con->surface = new_surface;
910 dpy_gfx_create_texture(con, new_surface);
284d1c6b
GH
911 QLIST_FOREACH(dcl, &s->listeners, next) {
912 if (con != (dcl->con ? dcl->con : active_console)) {
913 continue;
914 }
0d0be876 915 displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
321f048d 916 }
589089fe 917 dpy_gfx_destroy_texture(con, old_surface);
da229ef3 918 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
919}
920
49743df3
BH
921bool dpy_gfx_check_format(QemuConsole *con,
922 pixman_format_code_t format)
923{
924 DisplayChangeListener *dcl;
925 DisplayState *s = con->ds;
926
927 QLIST_FOREACH(dcl, &s->listeners, next) {
928 if (dcl->con && dcl->con != con) {
929 /* dcl bound to another console -> skip */
930 continue;
931 }
932 if (dcl->ops->dpy_gfx_check_format) {
933 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
934 return false;
935 }
936 } else {
75ae7c46 937 /* default is to allow native 32 bpp only */
49743df3
BH
938 if (format != qemu_default_pixman_format(32, true)) {
939 return false;
940 }
941 }
942 }
943 return true;
944}
945
6075137d 946static void dpy_refresh(DisplayState *s)
7c20b4a3 947{
284d1c6b
GH
948 DisplayChangeListener *dcl;
949
7c20b4a3
GH
950 QLIST_FOREACH(dcl, &s->listeners, next) {
951 if (dcl->ops->dpy_refresh) {
3f8f1313 952 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
953 }
954 }
955}
956
c78f7137 957void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 958{
c78f7137 959 DisplayState *s = con->ds;
284d1c6b 960 DisplayChangeListener *dcl;
321f048d 961
81c0d5a6 962 if (!qemu_console_is_visible(con)) {
321f048d
GH
963 return;
964 }
7c20b4a3 965 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
966 if (con != (dcl->con ? dcl->con : active_console)) {
967 continue;
968 }
7c20b4a3 969 if (dcl->ops->dpy_text_cursor) {
bc2ed970 970 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
971 }
972 }
973}
974
c78f7137 975void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 976{
c78f7137 977 DisplayState *s = con->ds;
284d1c6b 978 DisplayChangeListener *dcl;
321f048d 979
81c0d5a6 980 if (!qemu_console_is_visible(con)) {
321f048d
GH
981 return;
982 }
7c20b4a3 983 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
984 if (con != (dcl->con ? dcl->con : active_console)) {
985 continue;
986 }
7c20b4a3 987 if (dcl->ops->dpy_text_update) {
bc2ed970 988 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
989 }
990 }
991}
992
c78f7137 993void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 994{
c78f7137 995 DisplayState *s = con->ds;
9425c004 996 DisplayChangeListener *dcl;
321f048d 997
81c0d5a6 998 if (!qemu_console_is_visible(con)) {
321f048d
GH
999 return;
1000 }
7c20b4a3 1001 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1002 if (con != (dcl->con ? dcl->con : active_console)) {
1003 continue;
1004 }
7c20b4a3 1005 if (dcl->ops->dpy_text_resize) {
bc2ed970 1006 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1007 }
1008 }
1009}
1010
58d58708 1011void dpy_mouse_set(QemuConsole *c, int x, int y, int on)
7c20b4a3 1012{
58d58708
MAL
1013 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1014 DisplayState *s = c->ds;
284d1c6b 1015 DisplayChangeListener *dcl;
321f048d 1016
6effaa16
MAL
1017 con->cursor_x = x;
1018 con->cursor_y = y;
1019 con->cursor_on = on;
58d58708 1020 if (!qemu_console_is_visible(c)) {
321f048d
GH
1021 return;
1022 }
7c20b4a3 1023 QLIST_FOREACH(dcl, &s->listeners, next) {
58d58708 1024 if (c != (dcl->con ? dcl->con : active_console)) {
284d1c6b
GH
1025 continue;
1026 }
7c20b4a3 1027 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1028 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1029 }
1030 }
1031}
1032
58d58708 1033void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
7c20b4a3 1034{
58d58708
MAL
1035 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1036 DisplayState *s = c->ds;
284d1c6b 1037 DisplayChangeListener *dcl;
321f048d 1038
385ac97f
MAL
1039 cursor_unref(con->cursor);
1040 con->cursor = cursor_ref(cursor);
58d58708 1041 if (!qemu_console_is_visible(c)) {
321f048d
GH
1042 return;
1043 }
7c20b4a3 1044 QLIST_FOREACH(dcl, &s->listeners, next) {
58d58708 1045 if (c != (dcl->con ? dcl->con : active_console)) {
284d1c6b
GH
1046 continue;
1047 }
7c20b4a3 1048 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1049 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1050 }
1051 }
1052}
1053
c78f7137 1054bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1055{
c78f7137 1056 DisplayState *s = con->ds;
284d1c6b
GH
1057 DisplayChangeListener *dcl;
1058
7c20b4a3
GH
1059 QLIST_FOREACH(dcl, &s->listeners, next) {
1060 if (dcl->ops->dpy_cursor_define) {
1061 return true;
1062 }
1063 }
1064 return false;
1065}
1066
06020b95
GH
1067QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1068 struct QEMUGLParams *qparams)
1069{
1070 assert(con->gl);
1071 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1072}
1073
1074void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1075{
1076 assert(con->gl);
1077 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1078}
1079
1080int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1081{
1082 assert(con->gl);
1083 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1084}
1085
eaa92c76
GH
1086void dpy_gl_scanout_disable(QemuConsole *con)
1087{
7cc712e9
MAL
1088 DisplayState *s = con->ds;
1089 DisplayChangeListener *dcl;
1090
ebced091
MAL
1091 if (con->scanout.kind != SCANOUT_SURFACE) {
1092 con->scanout.kind = SCANOUT_NONE;
1093 }
7cc712e9 1094 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1095 if (con != (dcl->con ? dcl->con : active_console)) {
1096 continue;
1097 }
a9fbce5e
MAL
1098 if (dcl->ops->dpy_gl_scanout_disable) {
1099 dcl->ops->dpy_gl_scanout_disable(dcl);
1100 }
7cc712e9 1101 }
eaa92c76
GH
1102}
1103
f4c36bda
GH
1104void dpy_gl_scanout_texture(QemuConsole *con,
1105 uint32_t backing_id,
1106 bool backing_y_0_top,
1107 uint32_t backing_width,
1108 uint32_t backing_height,
1109 uint32_t x, uint32_t y,
bf41ab61
MAL
1110 uint32_t width, uint32_t height,
1111 void *d3d_tex2d)
06020b95 1112{
7cc712e9
MAL
1113 DisplayState *s = con->ds;
1114 DisplayChangeListener *dcl;
1115
ebced091
MAL
1116 con->scanout.kind = SCANOUT_TEXTURE;
1117 con->scanout.texture = (ScanoutTexture) {
1118 backing_id, backing_y_0_top, backing_width, backing_height,
bf41ab61 1119 x, y, width, height, d3d_tex2d,
ebced091 1120 };
7cc712e9 1121 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1122 if (con != (dcl->con ? dcl->con : active_console)) {
1123 continue;
1124 }
a9fbce5e
MAL
1125 if (dcl->ops->dpy_gl_scanout_texture) {
1126 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1127 backing_y_0_top,
1128 backing_width, backing_height,
bf41ab61
MAL
1129 x, y, width, height,
1130 d3d_tex2d);
a9fbce5e 1131 }
7cc712e9 1132 }
06020b95
GH
1133}
1134
4133fa71
GH
1135void dpy_gl_scanout_dmabuf(QemuConsole *con,
1136 QemuDmaBuf *dmabuf)
1137{
7cc712e9
MAL
1138 DisplayState *s = con->ds;
1139 DisplayChangeListener *dcl;
1140
ebced091
MAL
1141 con->scanout.kind = SCANOUT_DMABUF;
1142 con->scanout.dmabuf = dmabuf;
7cc712e9 1143 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1144 if (con != (dcl->con ? dcl->con : active_console)) {
1145 continue;
1146 }
a9fbce5e
MAL
1147 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1148 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1149 }
7cc712e9 1150 }
4133fa71
GH
1151}
1152
6e1f2cb5
GH
1153void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1154 bool have_hot, uint32_t hot_x, uint32_t hot_y)
4133fa71 1155{
7cc712e9
MAL
1156 DisplayState *s = con->ds;
1157 DisplayChangeListener *dcl;
4133fa71 1158
7cc712e9 1159 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1160 if (con != (dcl->con ? dcl->con : active_console)) {
1161 continue;
1162 }
7cc712e9
MAL
1163 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1164 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
6e1f2cb5 1165 have_hot, hot_x, hot_y);
7cc712e9 1166 }
6e1f2cb5
GH
1167 }
1168}
1169
1170void dpy_gl_cursor_position(QemuConsole *con,
1171 uint32_t pos_x, uint32_t pos_y)
1172{
7cc712e9
MAL
1173 DisplayState *s = con->ds;
1174 DisplayChangeListener *dcl;
6e1f2cb5 1175
7cc712e9 1176 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1177 if (con != (dcl->con ? dcl->con : active_console)) {
1178 continue;
1179 }
7cc712e9
MAL
1180 if (dcl->ops->dpy_gl_cursor_position) {
1181 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1182 }
4133fa71
GH
1183 }
1184}
1185
1186void dpy_gl_release_dmabuf(QemuConsole *con,
1187 QemuDmaBuf *dmabuf)
1188{
7cc712e9
MAL
1189 DisplayState *s = con->ds;
1190 DisplayChangeListener *dcl;
4133fa71 1191
7cc712e9 1192 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1193 if (con != (dcl->con ? dcl->con : active_console)) {
1194 continue;
1195 }
7cc712e9
MAL
1196 if (dcl->ops->dpy_gl_release_dmabuf) {
1197 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
1198 }
4133fa71
GH
1199 }
1200}
1201
06020b95
GH
1202void dpy_gl_update(QemuConsole *con,
1203 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1204{
7cc712e9
MAL
1205 DisplayState *s = con->ds;
1206 DisplayChangeListener *dcl;
1207
06020b95 1208 assert(con->gl);
f6413cbf
MAL
1209
1210 graphic_hw_gl_block(con, true);
7cc712e9 1211 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1212 if (con != (dcl->con ? dcl->con : active_console)) {
1213 continue;
1214 }
a9fbce5e
MAL
1215 if (dcl->ops->dpy_gl_update) {
1216 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
1217 }
7cc712e9 1218 }
f6413cbf 1219 graphic_hw_gl_block(con, false);
06020b95
GH
1220}
1221
98b50080
PB
1222/***********************************************************/
1223/* register display */
1224
64840c66
GH
1225/* console.c internal use only */
1226static DisplayState *get_alloc_displaystate(void)
98b50080 1227{
64840c66
GH
1228 if (!display_state) {
1229 display_state = g_new0(DisplayState, 1);
1230 }
1231 return display_state;
98b50080
PB
1232}
1233
64840c66
GH
1234/*
1235 * Called by main(), after creating QemuConsoles
1236 * and before initializing ui (sdl/vnc/...).
1237 */
1238DisplayState *init_displaystate(void)
98b50080 1239{
43f420f8 1240 gchar *name;
cd6cd8fa 1241 QemuConsole *con;
64840c66 1242
cd6cd8fa 1243 QTAILQ_FOREACH(con, &consoles, next) {
34b77515 1244 /* Hook up into the qom tree here (not in object_new()), once
43f420f8
GH
1245 * all QemuConsoles are created and the order / numbering
1246 * doesn't change any more */
cd6cd8fa 1247 name = g_strdup_printf("console[%d]", con->index);
43f420f8 1248 object_property_add_child(container_get(object_get_root(), "/backend"),
d2623129 1249 name, OBJECT(con));
43f420f8 1250 g_free(name);
64840c66
GH
1251 }
1252
98b50080
PB
1253 return display_state;
1254}
1255
1c1f9498
GH
1256void graphic_console_set_hwops(QemuConsole *con,
1257 const GraphicHwOps *hw_ops,
1258 void *opaque)
1259{
1260 con->hw_ops = hw_ops;
1261 con->hw = opaque;
1262}
1263
5643706a 1264QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 1265 const GraphicHwOps *hw_ops,
c78f7137 1266 void *opaque)
95219897 1267{
521a580d
GH
1268 static const char noinit[] =
1269 "Guest has not initialized the display (yet).";
64840c66
GH
1270 int width = 640;
1271 int height = 480;
76ffb0b4 1272 QemuConsole *s;
9588d67e 1273 DisplaySurface *surface;
f0f2f976 1274
f9411aae 1275 s = qemu_graphic_console_lookup_unused();
9588d67e
GH
1276 if (s) {
1277 trace_console_gfx_reuse(s->index);
ebced091
MAL
1278 width = qemu_console_get_width(s, 0);
1279 height = qemu_console_get_height(s, 0);
9588d67e
GH
1280 } else {
1281 trace_console_gfx_new();
34b77515 1282 s = (QemuConsole *)object_new(TYPE_QEMU_GRAPHIC_CONSOLE);
9588d67e 1283 }
58d58708 1284 QEMU_GRAPHIC_CONSOLE(s)->head = head;
1c1f9498 1285 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 1286 if (dev) {
5325cc34 1287 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
afff2b15 1288 &error_abort);
aa2beaa1 1289 }
3023f332 1290
b5a087b0 1291 surface = qemu_create_placeholder_surface(width, height, noinit);
9588d67e 1292 dpy_gfx_replace_surface(s, surface);
a9b1e471
MAL
1293 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1294 graphic_hw_gl_unblock_timer, s);
c78f7137 1295 return s;
e7f0ad58
FB
1296}
1297
9588d67e
GH
1298static const GraphicHwOps unused_ops = {
1299 /* no callbacks */
1300};
1301
1302void graphic_console_close(QemuConsole *con)
1303{
1304 static const char unplugged[] =
1305 "Guest display has been unplugged";
1306 DisplaySurface *surface;
ebced091
MAL
1307 int width = qemu_console_get_width(con, 640);
1308 int height = qemu_console_get_height(con, 480);
9588d67e
GH
1309
1310 trace_console_gfx_close(con->index);
5325cc34 1311 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
9588d67e
GH
1312 graphic_console_set_hwops(con, &unused_ops, NULL);
1313
1314 if (con->gl) {
1315 dpy_gl_scanout_disable(con);
1316 }
b5a087b0 1317 surface = qemu_create_placeholder_surface(width, height, unplugged);
9588d67e
GH
1318 dpy_gfx_replace_surface(con, surface);
1319}
1320
284d1c6b
GH
1321QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1322{
cd6cd8fa
GH
1323 QemuConsole *con;
1324
1325 QTAILQ_FOREACH(con, &consoles, next) {
1326 if (con->index == index) {
1327 return con;
1328 }
284d1c6b 1329 }
cd6cd8fa 1330 return NULL;
284d1c6b
GH
1331}
1332
5643706a 1333QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 1334{
cd6cd8fa 1335 QemuConsole *con;
14a93649 1336 Object *obj;
5643706a 1337 uint32_t h;
14a93649 1338
cd6cd8fa
GH
1339 QTAILQ_FOREACH(con, &consoles, next) {
1340 obj = object_property_get_link(OBJECT(con),
afff2b15 1341 "device", &error_abort);
5643706a
GH
1342 if (DEVICE(obj) != dev) {
1343 continue;
1344 }
cd6cd8fa 1345 h = object_property_get_uint(OBJECT(con),
ad664c1d 1346 "head", &error_abort);
5643706a
GH
1347 if (h != head) {
1348 continue;
14a93649 1349 }
cd6cd8fa 1350 return con;
14a93649
GH
1351 }
1352 return NULL;
1353}
1354
f2c1d54c
GH
1355QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1356 uint32_t head, Error **errp)
1357{
1358 DeviceState *dev;
1359 QemuConsole *con;
1360
1361 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1362 if (dev == NULL) {
1363 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1364 "Device '%s' not found", device_id);
1365 return NULL;
1366 }
1367
1368 con = qemu_console_lookup_by_device(dev, head);
1369 if (con == NULL) {
1370 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1371 device_id, head);
1372 return NULL;
1373 }
1374
1375 return con;
1376}
1377
f9411aae 1378static QemuConsole *qemu_graphic_console_lookup_unused(void)
9588d67e 1379{
cd6cd8fa 1380 QemuConsole *con;
9588d67e 1381 Object *obj;
9588d67e 1382
cd6cd8fa 1383 QTAILQ_FOREACH(con, &consoles, next) {
f9411aae 1384 if (!QEMU_IS_GRAPHIC_CONSOLE(con) || con->hw_ops != &unused_ops) {
9588d67e
GH
1385 continue;
1386 }
cd6cd8fa 1387 obj = object_property_get_link(OBJECT(con),
9588d67e
GH
1388 "device", &error_abort);
1389 if (obj != NULL) {
1390 continue;
1391 }
cd6cd8fa 1392 return con;
9588d67e
GH
1393 }
1394 return NULL;
1395}
1396
385ac97f
MAL
1397QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
1398{
3c293a46
MAL
1399 if (con == NULL) {
1400 con = active_console;
1401 }
58d58708 1402 return QEMU_IS_GRAPHIC_CONSOLE(con) ? QEMU_GRAPHIC_CONSOLE(con)->cursor : NULL;
385ac97f
MAL
1403}
1404
81c0d5a6 1405bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 1406{
284d1c6b 1407 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
1408}
1409
81c0d5a6 1410bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 1411{
81c0d5a6
GH
1412 if (con == NULL) {
1413 con = active_console;
1414 }
c105d60f 1415 return con && QEMU_IS_GRAPHIC_CONSOLE(con);
81c0d5a6
GH
1416}
1417
1418bool qemu_console_is_fixedsize(QemuConsole *con)
1419{
1420 if (con == NULL) {
1421 con = active_console;
1422 }
c105d60f 1423 return con && (QEMU_IS_GRAPHIC_CONSOLE(con) || QEMU_IS_FIXED_TEXT_CONSOLE(con));
c21bbcfa
AZ
1424}
1425
f607867c
GH
1426bool qemu_console_is_gl_blocked(QemuConsole *con)
1427{
1428 assert(con != NULL);
1429 return con->gl_block;
1430}
1431
839a4826
WJ
1432bool qemu_console_is_multihead(DeviceState *dev)
1433{
1434 QemuConsole *con;
1435 Object *obj;
1436 uint32_t f = 0xffffffff;
1437 uint32_t h;
1438
1439 QTAILQ_FOREACH(con, &consoles, next) {
1440 obj = object_property_get_link(OBJECT(con),
1441 "device", &error_abort);
1442 if (DEVICE(obj) != dev) {
1443 continue;
1444 }
1445
1446 h = object_property_get_uint(OBJECT(con),
1447 "head", &error_abort);
1448 if (f == 0xffffffff) {
1449 f = h;
1450 } else if (h != f) {
1451 return true;
1452 }
1453 }
1454 return false;
1455}
1456
779ce88f
GH
1457char *qemu_console_get_label(QemuConsole *con)
1458{
c105d60f 1459 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
58d58708
MAL
1460 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(con);
1461 if (c->device) {
839a4826
WJ
1462 DeviceState *dev;
1463 bool multihead;
1464
58d58708 1465 dev = DEVICE(c->device);
839a4826
WJ
1466 multihead = qemu_console_is_multihead(dev);
1467 if (multihead) {
1468 return g_strdup_printf("%s.%d", dev->id ?
1469 dev->id :
58d58708
MAL
1470 object_get_typename(c->device),
1471 c->head);
839a4826
WJ
1472 } else {
1473 return g_strdup_printf("%s", dev->id ?
1474 dev->id :
58d58708 1475 object_get_typename(c->device));
839a4826 1476 }
779ce88f
GH
1477 }
1478 return g_strdup("VGA");
b2bb9cc4 1479 } else if (QEMU_IS_TEXT_CONSOLE(con)) {
f7ce755d
MAL
1480 const char *label = qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con));
1481 if (label) {
1482 return g_strdup(label);
779ce88f 1483 }
779ce88f 1484 }
b2bb9cc4
MAL
1485
1486 return g_strdup_printf("vc%d", con->index);
779ce88f
GH
1487}
1488
d4c85337
GH
1489int qemu_console_get_index(QemuConsole *con)
1490{
1491 if (con == NULL) {
1492 con = active_console;
1493 }
1494 return con ? con->index : -1;
1495}
1496
5643706a
GH
1497uint32_t qemu_console_get_head(QemuConsole *con)
1498{
1499 if (con == NULL) {
1500 con = active_console;
1501 }
58d58708
MAL
1502 if (con == NULL) {
1503 return -1;
1504 }
1505 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1506 return QEMU_GRAPHIC_CONSOLE(con)->head;
1507 }
1508 return 0;
5643706a
GH
1509}
1510
d4c85337
GH
1511int qemu_console_get_width(QemuConsole *con, int fallback)
1512{
1513 if (con == NULL) {
1514 con = active_console;
1515 }
ebced091
MAL
1516 if (con == NULL) {
1517 return fallback;
1518 }
1519 switch (con->scanout.kind) {
1520 case SCANOUT_DMABUF:
1521 return con->scanout.dmabuf->width;
1522 case SCANOUT_TEXTURE:
1523 return con->scanout.texture.width;
1524 case SCANOUT_SURFACE:
1525 return surface_width(con->surface);
1526 default:
1527 return fallback;
1528 }
d4c85337
GH
1529}
1530
1531int qemu_console_get_height(QemuConsole *con, int fallback)
1532{
1533 if (con == NULL) {
1534 con = active_console;
1535 }
ebced091
MAL
1536 if (con == NULL) {
1537 return fallback;
1538 }
1539 switch (con->scanout.kind) {
1540 case SCANOUT_DMABUF:
1541 return con->scanout.dmabuf->height;
1542 case SCANOUT_TEXTURE:
1543 return con->scanout.texture.height;
1544 case SCANOUT_SURFACE:
1545 return surface_height(con->surface);
1546 default:
1547 return fallback;
1548 }
d4c85337
GH
1549}
1550
322dae4b 1551int qemu_invalidate_text_consoles(void)
bf1bed81 1552{
aea7947c 1553 QemuConsole *s;
cd6cd8fa 1554 int count = 0;
aea7947c 1555
cd6cd8fa 1556 QTAILQ_FOREACH(s, &consoles, next) {
aea7947c
GH
1557 if (qemu_console_is_graphic(s) ||
1558 !qemu_console_is_visible(s)) {
1559 continue;
1560 }
1561 count++;
1562 graphic_hw_invalidate(s);
1563 }
1564
322dae4b
MAL
1565 return count;
1566}
1567
c78f7137 1568void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 1569{
88738ea4 1570 DisplaySurface *surface = qemu_console_surface(s);
321f048d 1571
c105d60f 1572 assert(QEMU_IS_GRAPHIC_CONSOLE(s));
cd958edb 1573
88738ea4
MAL
1574 if ((s->scanout.kind != SCANOUT_SURFACE ||
1575 (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
1576 qemu_console_get_width(s, -1) == width &&
cb8962c1 1577 qemu_console_get_height(s, -1) == height) {
cd958edb
MAL
1578 return;
1579 }
1580
321f048d
GH
1581 surface = qemu_create_displaysurface(width, height);
1582 dpy_gfx_replace_surface(s, surface);
c60e08d9 1583}
38334f76 1584
c78f7137
GH
1585DisplaySurface *qemu_console_surface(QemuConsole *console)
1586{
ebced091
MAL
1587 switch (console->scanout.kind) {
1588 case SCANOUT_SURFACE:
1589 return console->surface;
1590 default:
1591 return NULL;
1592 }
c78f7137
GH
1593}
1594
0da2ea1b 1595PixelFormat qemu_default_pixelformat(int bpp)
1596{
56bd9ea1
GH
1597 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
1598 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
1599 return pf;
1600}
01f45d98 1601
db71589f
GH
1602static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
1603
1604void qemu_display_register(QemuDisplay *ui)
1605{
1606 assert(ui->type < DISPLAY_TYPE__MAX);
1607 dpys[ui->type] = ui;
1608}
1609
898f9d41
GH
1610bool qemu_display_find_default(DisplayOptions *opts)
1611{
1612 static DisplayType prio[] = {
66c2207f 1613#if defined(CONFIG_GTK)
898f9d41 1614 DISPLAY_TYPE_GTK,
66c2207f
TH
1615#endif
1616#if defined(CONFIG_SDL)
898f9d41 1617 DISPLAY_TYPE_SDL,
66c2207f
TH
1618#endif
1619#if defined(CONFIG_COCOA)
898f9d41 1620 DISPLAY_TYPE_COCOA
66c2207f 1621#endif
898f9d41
GH
1622 };
1623 int i;
1624
66c2207f 1625 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
61b4d9a2 1626 if (dpys[prio[i]] == NULL) {
c551fb0b
CF
1627 Error *local_err = NULL;
1628 int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
1629 if (rv < 0) {
1630 error_report_err(local_err);
1631 }
61b4d9a2 1632 }
898f9d41
GH
1633 if (dpys[prio[i]] == NULL) {
1634 continue;
1635 }
1636 opts->type = prio[i];
1637 return true;
1638 }
1639 return false;
1640}
1641
db71589f
GH
1642void qemu_display_early_init(DisplayOptions *opts)
1643{
1644 assert(opts->type < DISPLAY_TYPE__MAX);
1645 if (opts->type == DISPLAY_TYPE_NONE) {
1646 return;
1647 }
61b4d9a2 1648 if (dpys[opts->type] == NULL) {
c551fb0b
CF
1649 Error *local_err = NULL;
1650 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
1651 if (rv < 0) {
1652 error_report_err(local_err);
1653 }
61b4d9a2 1654 }
db71589f
GH
1655 if (dpys[opts->type] == NULL) {
1656 error_report("Display '%s' is not available.",
c809d1d2 1657 DisplayType_str(opts->type));
db71589f
GH
1658 exit(1);
1659 }
1660 if (dpys[opts->type]->early_init) {
1661 dpys[opts->type]->early_init(opts);
1662 }
1663}
1664
1665void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
1666{
1667 assert(opts->type < DISPLAY_TYPE__MAX);
1668 if (opts->type == DISPLAY_TYPE_NONE) {
1669 return;
1670 }
1671 assert(dpys[opts->type] != NULL);
1672 dpys[opts->type]->init(ds, opts);
1673}
1674
c388f408
TH
1675void qemu_display_help(void)
1676{
1677 int idx;
1678
1679 printf("Available display backend types:\n");
a1e8853e 1680 printf("none\n");
c388f408
TH
1681 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
1682 if (!dpys[idx]) {
c551fb0b
CF
1683 Error *local_err = NULL;
1684 int rv = ui_module_load(DisplayType_str(idx), &local_err);
1685 if (rv < 0) {
1686 error_report_err(local_err);
1687 }
c388f408
TH
1688 }
1689 if (dpys[idx]) {
1690 printf("%s\n", DisplayType_str(dpys[idx]->type));
1691 }
1692 }
1693}