]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[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);
d7e94796 587#ifdef CONFIG_PIXMAN
1ece6777
MAL
588 pixman_color_t bg = QEMU_PIXMAN_COLOR_BLACK;
589 pixman_color_t fg = QEMU_PIXMAN_COLOR_GRAY;
d3002b04
GH
590 pixman_image_t *glyph;
591 int len, x, y, i;
592
593 len = strlen(msg);
521a580d
GH
594 x = (w / FONT_WIDTH - len) / 2;
595 y = (h / FONT_HEIGHT - 1) / 2;
d3002b04
GH
596 for (i = 0; i < len; i++) {
597 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
598 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
599 x+i, y, FONT_WIDTH, FONT_HEIGHT);
600 qemu_pixman_image_unref(glyph);
601 }
d7e94796 602#endif
b5a087b0 603 surface->flags |= QEMU_PLACEHOLDER_FLAG;
d3002b04
GH
604 return surface;
605}
606
da229ef3 607void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 608{
da229ef3 609 if (surface == NULL) {
98b50080 610 return;
187cd1d9 611 }
da229ef3
GH
612 trace_displaysurface_free(surface);
613 qemu_pixman_image_unref(surface->image);
614 g_free(surface);
98b50080
PB
615}
616
06020b95
GH
617bool console_has_gl(QemuConsole *con)
618{
619 return con->gl != NULL;
620}
621
d0e137bc
MAL
622static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
623{
624 if (dcl->ops->dpy_has_dmabuf) {
625 return dcl->ops->dpy_has_dmabuf(dcl);
626 }
627
628 if (dcl->ops->dpy_gl_scanout_dmabuf) {
629 return true;
630 }
631
632 return false;
633}
634
4b7b661d
MAL
635static bool console_compatible_with(QemuConsole *con,
636 DisplayChangeListener *dcl, Error **errp)
5983fdf1 637{
5983fdf1
MAL
638 int flags;
639
640 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
641
a62c4a17
MAL
642 if (console_has_gl(con) &&
643 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
398d1c91
MAL
644 error_setg(errp, "Display %s is incompatible with the GL context",
645 dcl->ops->dpy_name);
646 return false;
647 }
648
5983fdf1
MAL
649 if (flags & GRAPHIC_FLAGS_GL &&
650 !console_has_gl(con)) {
651 error_setg(errp, "The console requires a GL context.");
652 return false;
653
654 }
655
656 if (flags & GRAPHIC_FLAGS_DMABUF &&
657 !displaychangelistener_has_dmabuf(dcl)) {
658 error_setg(errp, "The console requires display DMABUF support.");
659 return false;
660 }
661
662 return true;
663}
664
b6596785
BE
665void console_handle_touch_event(QemuConsole *con,
666 struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
667 uint64_t num_slot,
668 int width, int height,
669 double x, double y,
670 InputMultiTouchType type,
671 Error **errp)
672{
673 struct touch_slot *slot;
674 bool needs_sync = false;
675 int update;
676 int i;
677
678 if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
679 error_setg(errp,
680 "Unexpected touch slot number: % " PRId64" >= %d",
681 num_slot, INPUT_EVENT_SLOTS_MAX);
682 return;
683 }
684
685 slot = &touch_slots[num_slot];
686 slot->x = x;
687 slot->y = y;
688
689 if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
690 slot->tracking_id = num_slot;
691 }
692
693 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
694 if (i == num_slot) {
695 update = type;
696 } else {
697 update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
698 }
699
700 slot = &touch_slots[i];
701
702 if (slot->tracking_id == -1) {
703 continue;
704 }
705
706 if (update == INPUT_MULTI_TOUCH_TYPE_END) {
707 slot->tracking_id = -1;
708 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
709 needs_sync = true;
710 } else {
711 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
712 qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
713 qemu_input_queue_mtt_abs(con,
714 INPUT_AXIS_X, (int) slot->x,
715 0, width,
716 i, slot->tracking_id);
717 qemu_input_queue_mtt_abs(con,
718 INPUT_AXIS_Y, (int) slot->y,
719 0, height,
720 i, slot->tracking_id);
721 needs_sync = true;
722 }
723 }
724
725 if (needs_sync) {
726 qemu_input_event_sync();
727 }
728}
729
5e79d516 730void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
4f418149
MAL
731{
732 /* display has opengl support */
5e79d516
MAL
733 assert(con);
734 if (con->gl) {
735 error_report("The console already has an OpenGL context.");
4f418149
MAL
736 exit(1);
737 }
5e79d516
MAL
738 con->gl = gl;
739}
740
58d58708
MAL
741static void
742dcl_set_graphic_cursor(DisplayChangeListener *dcl, QemuGraphicConsole *con)
743{
744 if (con && con->cursor && dcl->ops->dpy_cursor_define) {
745 dcl->ops->dpy_cursor_define(dcl, con->cursor);
746 }
747 if (con && dcl->ops->dpy_mouse_set) {
748 dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
749 }
750}
6f110819 751
5209089f 752void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 753{
284d1c6b
GH
754 QemuConsole *con;
755
e0665c3b
MAL
756 assert(!dcl->ds);
757
7c20b4a3 758 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
759 dcl->ds = get_alloc_displaystate();
760 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
761 gui_setup_refresh(dcl->ds);
284d1c6b
GH
762 if (dcl->con) {
763 dcl->con->dcls++;
764 con = dcl->con;
765 } else {
766 con = active_console;
767 }
4b7b661d 768 displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
58d58708
MAL
769 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
770 dcl_set_graphic_cursor(dcl, QEMU_GRAPHIC_CONSOLE(con));
6effaa16 771 }
6f110819 772 qemu_text_console_update_cursor();
7c20b4a3
GH
773}
774
0f7b2864
GH
775void update_displaychangelistener(DisplayChangeListener *dcl,
776 uint64_t interval)
777{
778 DisplayState *ds = dcl->ds;
779
780 dcl->update_interval = interval;
781 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 782 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
783 }
784}
785
7c20b4a3
GH
786void unregister_displaychangelistener(DisplayChangeListener *dcl)
787{
788 DisplayState *ds = dcl->ds;
789 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
790 if (dcl->con) {
791 dcl->con->dcls--;
792 }
7c20b4a3 793 QLIST_REMOVE(dcl, next);
777c5f1e 794 dcl->ds = NULL;
7c20b4a3
GH
795 gui_setup_refresh(ds);
796}
797
cf1ecc82
GH
798static void dpy_set_ui_info_timer(void *opaque)
799{
800 QemuConsole *con = opaque;
58d58708 801 uint32_t head = qemu_console_get_head(con);
cf1ecc82 802
58d58708 803 con->hw_ops->ui_info(con->hw, head, &con->ui_info);
cf1ecc82
GH
804}
805
a92e7bb4 806bool dpy_ui_info_supported(const QemuConsole *con)
b7fb49f0 807{
5c4b107f
GH
808 if (con == NULL) {
809 con = active_console;
810 }
48a35e12
MAL
811 if (con == NULL) {
812 return false;
813 }
5c4b107f 814
b7fb49f0
GH
815 return con->hw_ops->ui_info != NULL;
816}
817
5eaf1e48
MAL
818const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
819{
a92e7bb4
MAL
820 assert(dpy_ui_info_supported(con));
821
5c4b107f
GH
822 if (con == NULL) {
823 con = active_console;
824 }
5eaf1e48
MAL
825
826 return &con->ui_info;
827}
828
ca19ef52 829int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
6f90f3d7 830{
5c4b107f
GH
831 if (con == NULL) {
832 con = active_console;
833 }
1185fde4 834
b7fb49f0 835 if (!dpy_ui_info_supported(con)) {
cf1ecc82 836 return -1;
6f90f3d7 837 }
1185fde4
GH
838 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
839 /* nothing changed -- ignore */
840 return 0;
841 }
cf1ecc82
GH
842
843 /*
844 * Typically we get a flood of these as the user resizes the window.
845 * Wait until the dust has settled (one second without updates), then
846 * go notify the guest.
847 */
1185fde4 848 con->ui_info = *info;
ca19ef52
MAL
849 timer_mod(con->ui_timer,
850 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
cf1ecc82 851 return 0;
6f90f3d7
GH
852}
853
c78f7137 854void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 855{
c78f7137 856 DisplayState *s = con->ds;
284d1c6b 857 DisplayChangeListener *dcl;
ebced091
MAL
858 int width = qemu_console_get_width(con, x + w);
859 int height = qemu_console_get_height(con, y + h);
7c20b4a3
GH
860
861 x = MAX(x, 0);
862 y = MAX(y, 0);
863 x = MIN(x, width);
864 y = MIN(y, height);
865 w = MIN(w, width - x);
866 h = MIN(h, height - y);
867
81c0d5a6 868 if (!qemu_console_is_visible(con)) {
321f048d
GH
869 return;
870 }
589089fe 871 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
7c20b4a3 872 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
873 if (con != (dcl->con ? dcl->con : active_console)) {
874 continue;
875 }
7c20b4a3 876 if (dcl->ops->dpy_gfx_update) {
bc2ed970 877 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
878 }
879 }
880}
881
7cd0afe6
TZ
882void dpy_gfx_update_full(QemuConsole *con)
883{
ebced091
MAL
884 int w = qemu_console_get_width(con, 0);
885 int h = qemu_console_get_height(con, 0);
886
887 dpy_gfx_update(con, 0, 0, w, h);
7cd0afe6
TZ
888}
889
321f048d
GH
890void dpy_gfx_replace_surface(QemuConsole *con,
891 DisplaySurface *surface)
892{
c821a58e 893 static const char placeholder_msg[] = "Display output is not active.";
321f048d
GH
894 DisplayState *s = con->ds;
895 DisplaySurface *old_surface = con->surface;
0d0be876 896 DisplaySurface *new_surface = surface;
284d1c6b 897 DisplayChangeListener *dcl;
c821a58e
AO
898 int width;
899 int height;
900
901 if (!surface) {
902 if (old_surface) {
903 width = surface_width(old_surface);
904 height = surface_height(old_surface);
905 } else {
906 width = 640;
907 height = 480;
908 }
909
0d0be876 910 new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
c821a58e 911 }
321f048d 912
0d0be876 913 assert(old_surface != new_surface);
6905b934 914
ebced091 915 con->scanout.kind = SCANOUT_SURFACE;
0d0be876
DK
916 con->surface = new_surface;
917 dpy_gfx_create_texture(con, new_surface);
284d1c6b
GH
918 QLIST_FOREACH(dcl, &s->listeners, next) {
919 if (con != (dcl->con ? dcl->con : active_console)) {
920 continue;
921 }
0d0be876 922 displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
321f048d 923 }
589089fe 924 dpy_gfx_destroy_texture(con, old_surface);
da229ef3 925 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
926}
927
49743df3
BH
928bool dpy_gfx_check_format(QemuConsole *con,
929 pixman_format_code_t format)
930{
931 DisplayChangeListener *dcl;
932 DisplayState *s = con->ds;
933
934 QLIST_FOREACH(dcl, &s->listeners, next) {
935 if (dcl->con && dcl->con != con) {
936 /* dcl bound to another console -> skip */
937 continue;
938 }
939 if (dcl->ops->dpy_gfx_check_format) {
940 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
941 return false;
942 }
943 } else {
75ae7c46 944 /* default is to allow native 32 bpp only */
49743df3
BH
945 if (format != qemu_default_pixman_format(32, true)) {
946 return false;
947 }
948 }
949 }
950 return true;
951}
952
6075137d 953static void dpy_refresh(DisplayState *s)
7c20b4a3 954{
284d1c6b
GH
955 DisplayChangeListener *dcl;
956
7c20b4a3
GH
957 QLIST_FOREACH(dcl, &s->listeners, next) {
958 if (dcl->ops->dpy_refresh) {
3f8f1313 959 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
960 }
961 }
962}
963
c78f7137 964void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 965{
c78f7137 966 DisplayState *s = con->ds;
284d1c6b 967 DisplayChangeListener *dcl;
321f048d 968
81c0d5a6 969 if (!qemu_console_is_visible(con)) {
321f048d
GH
970 return;
971 }
7c20b4a3 972 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
973 if (con != (dcl->con ? dcl->con : active_console)) {
974 continue;
975 }
7c20b4a3 976 if (dcl->ops->dpy_text_cursor) {
bc2ed970 977 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
978 }
979 }
980}
981
c78f7137 982void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 983{
c78f7137 984 DisplayState *s = con->ds;
284d1c6b 985 DisplayChangeListener *dcl;
321f048d 986
81c0d5a6 987 if (!qemu_console_is_visible(con)) {
321f048d
GH
988 return;
989 }
7c20b4a3 990 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
991 if (con != (dcl->con ? dcl->con : active_console)) {
992 continue;
993 }
7c20b4a3 994 if (dcl->ops->dpy_text_update) {
bc2ed970 995 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
996 }
997 }
998}
999
c78f7137 1000void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1001{
c78f7137 1002 DisplayState *s = con->ds;
9425c004 1003 DisplayChangeListener *dcl;
321f048d 1004
81c0d5a6 1005 if (!qemu_console_is_visible(con)) {
321f048d
GH
1006 return;
1007 }
7c20b4a3 1008 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1009 if (con != (dcl->con ? dcl->con : active_console)) {
1010 continue;
1011 }
7c20b4a3 1012 if (dcl->ops->dpy_text_resize) {
bc2ed970 1013 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1014 }
1015 }
1016}
1017
58d58708 1018void dpy_mouse_set(QemuConsole *c, int x, int y, int on)
7c20b4a3 1019{
58d58708
MAL
1020 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1021 DisplayState *s = c->ds;
284d1c6b 1022 DisplayChangeListener *dcl;
321f048d 1023
6effaa16
MAL
1024 con->cursor_x = x;
1025 con->cursor_y = y;
1026 con->cursor_on = on;
58d58708 1027 if (!qemu_console_is_visible(c)) {
321f048d
GH
1028 return;
1029 }
7c20b4a3 1030 QLIST_FOREACH(dcl, &s->listeners, next) {
58d58708 1031 if (c != (dcl->con ? dcl->con : active_console)) {
284d1c6b
GH
1032 continue;
1033 }
7c20b4a3 1034 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1035 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1036 }
1037 }
1038}
1039
58d58708 1040void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
7c20b4a3 1041{
58d58708
MAL
1042 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
1043 DisplayState *s = c->ds;
284d1c6b 1044 DisplayChangeListener *dcl;
321f048d 1045
385ac97f
MAL
1046 cursor_unref(con->cursor);
1047 con->cursor = cursor_ref(cursor);
58d58708 1048 if (!qemu_console_is_visible(c)) {
321f048d
GH
1049 return;
1050 }
7c20b4a3 1051 QLIST_FOREACH(dcl, &s->listeners, next) {
58d58708 1052 if (c != (dcl->con ? dcl->con : active_console)) {
284d1c6b
GH
1053 continue;
1054 }
7c20b4a3 1055 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1056 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1057 }
1058 }
1059}
1060
c78f7137 1061bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1062{
c78f7137 1063 DisplayState *s = con->ds;
284d1c6b
GH
1064 DisplayChangeListener *dcl;
1065
7c20b4a3
GH
1066 QLIST_FOREACH(dcl, &s->listeners, next) {
1067 if (dcl->ops->dpy_cursor_define) {
1068 return true;
1069 }
1070 }
1071 return false;
1072}
1073
06020b95
GH
1074QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1075 struct QEMUGLParams *qparams)
1076{
1077 assert(con->gl);
1078 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1079}
1080
1081void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1082{
1083 assert(con->gl);
1084 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1085}
1086
1087int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1088{
1089 assert(con->gl);
1090 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1091}
1092
eaa92c76
GH
1093void dpy_gl_scanout_disable(QemuConsole *con)
1094{
7cc712e9
MAL
1095 DisplayState *s = con->ds;
1096 DisplayChangeListener *dcl;
1097
ebced091
MAL
1098 if (con->scanout.kind != SCANOUT_SURFACE) {
1099 con->scanout.kind = SCANOUT_NONE;
1100 }
7cc712e9 1101 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1102 if (con != (dcl->con ? dcl->con : active_console)) {
1103 continue;
1104 }
a9fbce5e
MAL
1105 if (dcl->ops->dpy_gl_scanout_disable) {
1106 dcl->ops->dpy_gl_scanout_disable(dcl);
1107 }
7cc712e9 1108 }
eaa92c76
GH
1109}
1110
f4c36bda
GH
1111void dpy_gl_scanout_texture(QemuConsole *con,
1112 uint32_t backing_id,
1113 bool backing_y_0_top,
1114 uint32_t backing_width,
1115 uint32_t backing_height,
1116 uint32_t x, uint32_t y,
bf41ab61
MAL
1117 uint32_t width, uint32_t height,
1118 void *d3d_tex2d)
06020b95 1119{
7cc712e9
MAL
1120 DisplayState *s = con->ds;
1121 DisplayChangeListener *dcl;
1122
ebced091
MAL
1123 con->scanout.kind = SCANOUT_TEXTURE;
1124 con->scanout.texture = (ScanoutTexture) {
1125 backing_id, backing_y_0_top, backing_width, backing_height,
bf41ab61 1126 x, y, width, height, d3d_tex2d,
ebced091 1127 };
7cc712e9 1128 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1129 if (con != (dcl->con ? dcl->con : active_console)) {
1130 continue;
1131 }
a9fbce5e
MAL
1132 if (dcl->ops->dpy_gl_scanout_texture) {
1133 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1134 backing_y_0_top,
1135 backing_width, backing_height,
bf41ab61
MAL
1136 x, y, width, height,
1137 d3d_tex2d);
a9fbce5e 1138 }
7cc712e9 1139 }
06020b95
GH
1140}
1141
4133fa71
GH
1142void dpy_gl_scanout_dmabuf(QemuConsole *con,
1143 QemuDmaBuf *dmabuf)
1144{
7cc712e9
MAL
1145 DisplayState *s = con->ds;
1146 DisplayChangeListener *dcl;
1147
ebced091
MAL
1148 con->scanout.kind = SCANOUT_DMABUF;
1149 con->scanout.dmabuf = dmabuf;
7cc712e9 1150 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1151 if (con != (dcl->con ? dcl->con : active_console)) {
1152 continue;
1153 }
a9fbce5e
MAL
1154 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1155 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1156 }
7cc712e9 1157 }
4133fa71
GH
1158}
1159
6e1f2cb5
GH
1160void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1161 bool have_hot, uint32_t hot_x, uint32_t hot_y)
4133fa71 1162{
7cc712e9
MAL
1163 DisplayState *s = con->ds;
1164 DisplayChangeListener *dcl;
4133fa71 1165
7cc712e9 1166 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1167 if (con != (dcl->con ? dcl->con : active_console)) {
1168 continue;
1169 }
7cc712e9
MAL
1170 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1171 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
6e1f2cb5 1172 have_hot, hot_x, hot_y);
7cc712e9 1173 }
6e1f2cb5
GH
1174 }
1175}
1176
1177void dpy_gl_cursor_position(QemuConsole *con,
1178 uint32_t pos_x, uint32_t pos_y)
1179{
7cc712e9
MAL
1180 DisplayState *s = con->ds;
1181 DisplayChangeListener *dcl;
6e1f2cb5 1182
7cc712e9 1183 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1184 if (con != (dcl->con ? dcl->con : active_console)) {
1185 continue;
1186 }
7cc712e9
MAL
1187 if (dcl->ops->dpy_gl_cursor_position) {
1188 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1189 }
4133fa71
GH
1190 }
1191}
1192
1193void dpy_gl_release_dmabuf(QemuConsole *con,
1194 QemuDmaBuf *dmabuf)
1195{
7cc712e9
MAL
1196 DisplayState *s = con->ds;
1197 DisplayChangeListener *dcl;
4133fa71 1198
7cc712e9 1199 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1200 if (con != (dcl->con ? dcl->con : active_console)) {
1201 continue;
1202 }
7cc712e9
MAL
1203 if (dcl->ops->dpy_gl_release_dmabuf) {
1204 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
1205 }
4133fa71
GH
1206 }
1207}
1208
06020b95
GH
1209void dpy_gl_update(QemuConsole *con,
1210 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1211{
7cc712e9
MAL
1212 DisplayState *s = con->ds;
1213 DisplayChangeListener *dcl;
1214
06020b95 1215 assert(con->gl);
f6413cbf
MAL
1216
1217 graphic_hw_gl_block(con, true);
7cc712e9 1218 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
1219 if (con != (dcl->con ? dcl->con : active_console)) {
1220 continue;
1221 }
a9fbce5e
MAL
1222 if (dcl->ops->dpy_gl_update) {
1223 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
1224 }
7cc712e9 1225 }
f6413cbf 1226 graphic_hw_gl_block(con, false);
06020b95
GH
1227}
1228
98b50080
PB
1229/***********************************************************/
1230/* register display */
1231
64840c66
GH
1232/* console.c internal use only */
1233static DisplayState *get_alloc_displaystate(void)
98b50080 1234{
64840c66
GH
1235 if (!display_state) {
1236 display_state = g_new0(DisplayState, 1);
1237 }
1238 return display_state;
98b50080
PB
1239}
1240
64840c66
GH
1241/*
1242 * Called by main(), after creating QemuConsoles
1243 * and before initializing ui (sdl/vnc/...).
1244 */
1245DisplayState *init_displaystate(void)
98b50080 1246{
43f420f8 1247 gchar *name;
cd6cd8fa 1248 QemuConsole *con;
64840c66 1249
cd6cd8fa 1250 QTAILQ_FOREACH(con, &consoles, next) {
34b77515 1251 /* Hook up into the qom tree here (not in object_new()), once
43f420f8
GH
1252 * all QemuConsoles are created and the order / numbering
1253 * doesn't change any more */
cd6cd8fa 1254 name = g_strdup_printf("console[%d]", con->index);
43f420f8 1255 object_property_add_child(container_get(object_get_root(), "/backend"),
d2623129 1256 name, OBJECT(con));
43f420f8 1257 g_free(name);
64840c66
GH
1258 }
1259
98b50080
PB
1260 return display_state;
1261}
1262
1c1f9498
GH
1263void graphic_console_set_hwops(QemuConsole *con,
1264 const GraphicHwOps *hw_ops,
1265 void *opaque)
1266{
1267 con->hw_ops = hw_ops;
1268 con->hw = opaque;
1269}
1270
5643706a 1271QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 1272 const GraphicHwOps *hw_ops,
c78f7137 1273 void *opaque)
95219897 1274{
521a580d
GH
1275 static const char noinit[] =
1276 "Guest has not initialized the display (yet).";
64840c66
GH
1277 int width = 640;
1278 int height = 480;
76ffb0b4 1279 QemuConsole *s;
9588d67e 1280 DisplaySurface *surface;
f0f2f976 1281
f9411aae 1282 s = qemu_graphic_console_lookup_unused();
9588d67e
GH
1283 if (s) {
1284 trace_console_gfx_reuse(s->index);
ebced091
MAL
1285 width = qemu_console_get_width(s, 0);
1286 height = qemu_console_get_height(s, 0);
9588d67e
GH
1287 } else {
1288 trace_console_gfx_new();
34b77515 1289 s = (QemuConsole *)object_new(TYPE_QEMU_GRAPHIC_CONSOLE);
9588d67e 1290 }
58d58708 1291 QEMU_GRAPHIC_CONSOLE(s)->head = head;
1c1f9498 1292 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 1293 if (dev) {
5325cc34 1294 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
afff2b15 1295 &error_abort);
aa2beaa1 1296 }
3023f332 1297
b5a087b0 1298 surface = qemu_create_placeholder_surface(width, height, noinit);
9588d67e 1299 dpy_gfx_replace_surface(s, surface);
a9b1e471
MAL
1300 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1301 graphic_hw_gl_unblock_timer, s);
c78f7137 1302 return s;
e7f0ad58
FB
1303}
1304
9588d67e
GH
1305static const GraphicHwOps unused_ops = {
1306 /* no callbacks */
1307};
1308
1309void graphic_console_close(QemuConsole *con)
1310{
1311 static const char unplugged[] =
1312 "Guest display has been unplugged";
1313 DisplaySurface *surface;
ebced091
MAL
1314 int width = qemu_console_get_width(con, 640);
1315 int height = qemu_console_get_height(con, 480);
9588d67e
GH
1316
1317 trace_console_gfx_close(con->index);
5325cc34 1318 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
9588d67e
GH
1319 graphic_console_set_hwops(con, &unused_ops, NULL);
1320
1321 if (con->gl) {
1322 dpy_gl_scanout_disable(con);
1323 }
b5a087b0 1324 surface = qemu_create_placeholder_surface(width, height, unplugged);
9588d67e
GH
1325 dpy_gfx_replace_surface(con, surface);
1326}
1327
284d1c6b
GH
1328QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1329{
cd6cd8fa
GH
1330 QemuConsole *con;
1331
1332 QTAILQ_FOREACH(con, &consoles, next) {
1333 if (con->index == index) {
1334 return con;
1335 }
284d1c6b 1336 }
cd6cd8fa 1337 return NULL;
284d1c6b
GH
1338}
1339
5643706a 1340QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 1341{
cd6cd8fa 1342 QemuConsole *con;
14a93649 1343 Object *obj;
5643706a 1344 uint32_t h;
14a93649 1345
cd6cd8fa
GH
1346 QTAILQ_FOREACH(con, &consoles, next) {
1347 obj = object_property_get_link(OBJECT(con),
afff2b15 1348 "device", &error_abort);
5643706a
GH
1349 if (DEVICE(obj) != dev) {
1350 continue;
1351 }
cd6cd8fa 1352 h = object_property_get_uint(OBJECT(con),
ad664c1d 1353 "head", &error_abort);
5643706a
GH
1354 if (h != head) {
1355 continue;
14a93649 1356 }
cd6cd8fa 1357 return con;
14a93649
GH
1358 }
1359 return NULL;
1360}
1361
f2c1d54c
GH
1362QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1363 uint32_t head, Error **errp)
1364{
1365 DeviceState *dev;
1366 QemuConsole *con;
1367
1368 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1369 if (dev == NULL) {
1370 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1371 "Device '%s' not found", device_id);
1372 return NULL;
1373 }
1374
1375 con = qemu_console_lookup_by_device(dev, head);
1376 if (con == NULL) {
1377 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1378 device_id, head);
1379 return NULL;
1380 }
1381
1382 return con;
1383}
1384
f9411aae 1385static QemuConsole *qemu_graphic_console_lookup_unused(void)
9588d67e 1386{
cd6cd8fa 1387 QemuConsole *con;
9588d67e 1388 Object *obj;
9588d67e 1389
cd6cd8fa 1390 QTAILQ_FOREACH(con, &consoles, next) {
f9411aae 1391 if (!QEMU_IS_GRAPHIC_CONSOLE(con) || con->hw_ops != &unused_ops) {
9588d67e
GH
1392 continue;
1393 }
cd6cd8fa 1394 obj = object_property_get_link(OBJECT(con),
9588d67e
GH
1395 "device", &error_abort);
1396 if (obj != NULL) {
1397 continue;
1398 }
cd6cd8fa 1399 return con;
9588d67e
GH
1400 }
1401 return NULL;
1402}
1403
385ac97f
MAL
1404QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
1405{
3c293a46
MAL
1406 if (con == NULL) {
1407 con = active_console;
1408 }
58d58708 1409 return QEMU_IS_GRAPHIC_CONSOLE(con) ? QEMU_GRAPHIC_CONSOLE(con)->cursor : NULL;
385ac97f
MAL
1410}
1411
81c0d5a6 1412bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 1413{
284d1c6b 1414 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
1415}
1416
81c0d5a6 1417bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 1418{
81c0d5a6
GH
1419 if (con == NULL) {
1420 con = active_console;
1421 }
c105d60f 1422 return con && QEMU_IS_GRAPHIC_CONSOLE(con);
81c0d5a6
GH
1423}
1424
1425bool qemu_console_is_fixedsize(QemuConsole *con)
1426{
1427 if (con == NULL) {
1428 con = active_console;
1429 }
c105d60f 1430 return con && (QEMU_IS_GRAPHIC_CONSOLE(con) || QEMU_IS_FIXED_TEXT_CONSOLE(con));
c21bbcfa
AZ
1431}
1432
f607867c
GH
1433bool qemu_console_is_gl_blocked(QemuConsole *con)
1434{
1435 assert(con != NULL);
1436 return con->gl_block;
1437}
1438
2c0c4c1f 1439static bool qemu_graphic_console_is_multihead(QemuGraphicConsole *c)
839a4826
WJ
1440{
1441 QemuConsole *con;
839a4826
WJ
1442
1443 QTAILQ_FOREACH(con, &consoles, next) {
2c0c4c1f
LE
1444 QemuGraphicConsole *candidate;
1445
4ce2f97c
LE
1446 if (!QEMU_IS_GRAPHIC_CONSOLE(con)) {
1447 continue;
1448 }
2c0c4c1f
LE
1449
1450 candidate = QEMU_GRAPHIC_CONSOLE(con);
1451 if (candidate->device != c->device) {
839a4826
WJ
1452 continue;
1453 }
1454
65d7ceb4 1455 if (candidate->head != c->head) {
839a4826
WJ
1456 return true;
1457 }
1458 }
1459 return false;
1460}
1461
779ce88f
GH
1462char *qemu_console_get_label(QemuConsole *con)
1463{
c105d60f 1464 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
58d58708
MAL
1465 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(con);
1466 if (c->device) {
839a4826
WJ
1467 DeviceState *dev;
1468 bool multihead;
1469
58d58708 1470 dev = DEVICE(c->device);
2c0c4c1f 1471 multihead = qemu_graphic_console_is_multihead(c);
839a4826
WJ
1472 if (multihead) {
1473 return g_strdup_printf("%s.%d", dev->id ?
1474 dev->id :
58d58708
MAL
1475 object_get_typename(c->device),
1476 c->head);
839a4826
WJ
1477 } else {
1478 return g_strdup_printf("%s", dev->id ?
1479 dev->id :
58d58708 1480 object_get_typename(c->device));
839a4826 1481 }
779ce88f
GH
1482 }
1483 return g_strdup("VGA");
b2bb9cc4 1484 } else if (QEMU_IS_TEXT_CONSOLE(con)) {
f7ce755d
MAL
1485 const char *label = qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con));
1486 if (label) {
1487 return g_strdup(label);
779ce88f 1488 }
779ce88f 1489 }
b2bb9cc4
MAL
1490
1491 return g_strdup_printf("vc%d", con->index);
779ce88f
GH
1492}
1493
d4c85337
GH
1494int qemu_console_get_index(QemuConsole *con)
1495{
1496 if (con == NULL) {
1497 con = active_console;
1498 }
1499 return con ? con->index : -1;
1500}
1501
5643706a
GH
1502uint32_t qemu_console_get_head(QemuConsole *con)
1503{
1504 if (con == NULL) {
1505 con = active_console;
1506 }
58d58708
MAL
1507 if (con == NULL) {
1508 return -1;
1509 }
1510 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1511 return QEMU_GRAPHIC_CONSOLE(con)->head;
1512 }
1513 return 0;
5643706a
GH
1514}
1515
d4c85337
GH
1516int qemu_console_get_width(QemuConsole *con, int fallback)
1517{
1518 if (con == NULL) {
1519 con = active_console;
1520 }
ebced091
MAL
1521 if (con == NULL) {
1522 return fallback;
1523 }
1524 switch (con->scanout.kind) {
1525 case SCANOUT_DMABUF:
1526 return con->scanout.dmabuf->width;
1527 case SCANOUT_TEXTURE:
1528 return con->scanout.texture.width;
1529 case SCANOUT_SURFACE:
1530 return surface_width(con->surface);
1531 default:
1532 return fallback;
1533 }
d4c85337
GH
1534}
1535
1536int qemu_console_get_height(QemuConsole *con, int fallback)
1537{
1538 if (con == NULL) {
1539 con = active_console;
1540 }
ebced091
MAL
1541 if (con == NULL) {
1542 return fallback;
1543 }
1544 switch (con->scanout.kind) {
1545 case SCANOUT_DMABUF:
1546 return con->scanout.dmabuf->height;
1547 case SCANOUT_TEXTURE:
1548 return con->scanout.texture.height;
1549 case SCANOUT_SURFACE:
1550 return surface_height(con->surface);
1551 default:
1552 return fallback;
1553 }
d4c85337
GH
1554}
1555
322dae4b 1556int qemu_invalidate_text_consoles(void)
bf1bed81 1557{
aea7947c 1558 QemuConsole *s;
cd6cd8fa 1559 int count = 0;
aea7947c 1560
cd6cd8fa 1561 QTAILQ_FOREACH(s, &consoles, next) {
aea7947c
GH
1562 if (qemu_console_is_graphic(s) ||
1563 !qemu_console_is_visible(s)) {
1564 continue;
1565 }
1566 count++;
1567 graphic_hw_invalidate(s);
1568 }
1569
322dae4b
MAL
1570 return count;
1571}
1572
c78f7137 1573void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 1574{
88738ea4 1575 DisplaySurface *surface = qemu_console_surface(s);
321f048d 1576
c105d60f 1577 assert(QEMU_IS_GRAPHIC_CONSOLE(s));
cd958edb 1578
88738ea4
MAL
1579 if ((s->scanout.kind != SCANOUT_SURFACE ||
1580 (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
1581 qemu_console_get_width(s, -1) == width &&
cb8962c1 1582 qemu_console_get_height(s, -1) == height) {
cd958edb
MAL
1583 return;
1584 }
1585
321f048d
GH
1586 surface = qemu_create_displaysurface(width, height);
1587 dpy_gfx_replace_surface(s, surface);
c60e08d9 1588}
38334f76 1589
c78f7137
GH
1590DisplaySurface *qemu_console_surface(QemuConsole *console)
1591{
ebced091
MAL
1592 switch (console->scanout.kind) {
1593 case SCANOUT_SURFACE:
1594 return console->surface;
1595 default:
1596 return NULL;
1597 }
c78f7137
GH
1598}
1599
0da2ea1b 1600PixelFormat qemu_default_pixelformat(int bpp)
1601{
56bd9ea1
GH
1602 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
1603 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
1604 return pf;
1605}
01f45d98 1606
db71589f
GH
1607static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
1608
1609void qemu_display_register(QemuDisplay *ui)
1610{
1611 assert(ui->type < DISPLAY_TYPE__MAX);
1612 dpys[ui->type] = ui;
1613}
1614
898f9d41
GH
1615bool qemu_display_find_default(DisplayOptions *opts)
1616{
1617 static DisplayType prio[] = {
66c2207f 1618#if defined(CONFIG_GTK)
898f9d41 1619 DISPLAY_TYPE_GTK,
66c2207f
TH
1620#endif
1621#if defined(CONFIG_SDL)
898f9d41 1622 DISPLAY_TYPE_SDL,
66c2207f
TH
1623#endif
1624#if defined(CONFIG_COCOA)
898f9d41 1625 DISPLAY_TYPE_COCOA
66c2207f 1626#endif
898f9d41
GH
1627 };
1628 int i;
1629
66c2207f 1630 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
61b4d9a2 1631 if (dpys[prio[i]] == NULL) {
c551fb0b
CF
1632 Error *local_err = NULL;
1633 int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
1634 if (rv < 0) {
1635 error_report_err(local_err);
1636 }
61b4d9a2 1637 }
898f9d41
GH
1638 if (dpys[prio[i]] == NULL) {
1639 continue;
1640 }
1641 opts->type = prio[i];
1642 return true;
1643 }
1644 return false;
1645}
1646
db71589f
GH
1647void qemu_display_early_init(DisplayOptions *opts)
1648{
1649 assert(opts->type < DISPLAY_TYPE__MAX);
1650 if (opts->type == DISPLAY_TYPE_NONE) {
1651 return;
1652 }
61b4d9a2 1653 if (dpys[opts->type] == NULL) {
c551fb0b
CF
1654 Error *local_err = NULL;
1655 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
1656 if (rv < 0) {
1657 error_report_err(local_err);
1658 }
61b4d9a2 1659 }
db71589f
GH
1660 if (dpys[opts->type] == NULL) {
1661 error_report("Display '%s' is not available.",
c809d1d2 1662 DisplayType_str(opts->type));
db71589f
GH
1663 exit(1);
1664 }
1665 if (dpys[opts->type]->early_init) {
1666 dpys[opts->type]->early_init(opts);
1667 }
1668}
1669
1670void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
1671{
1672 assert(opts->type < DISPLAY_TYPE__MAX);
1673 if (opts->type == DISPLAY_TYPE_NONE) {
1674 return;
1675 }
1676 assert(dpys[opts->type] != NULL);
1677 dpys[opts->type]->init(ds, opts);
1678}
1679
1bec1cc0
MAL
1680const char *qemu_display_get_vc(DisplayOptions *opts)
1681{
600179c3 1682#ifdef CONFIG_PIXMAN
0e882307
MAL
1683 const char *vc = "vc:80Cx24C";
1684#else
1685 const char *vc = NULL;
600179c3 1686#endif
0e882307
MAL
1687
1688 assert(opts->type < DISPLAY_TYPE__MAX);
1689 if (dpys[opts->type] && dpys[opts->type]->vc) {
1690 vc = dpys[opts->type]->vc;
1bec1cc0 1691 }
0e882307 1692 return vc;
1bec1cc0
MAL
1693}
1694
c388f408
TH
1695void qemu_display_help(void)
1696{
1697 int idx;
1698
1699 printf("Available display backend types:\n");
a1e8853e 1700 printf("none\n");
c388f408
TH
1701 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
1702 if (!dpys[idx]) {
c551fb0b
CF
1703 Error *local_err = NULL;
1704 int rv = ui_module_load(DisplayType_str(idx), &local_err);
1705 if (rv < 0) {
1706 error_report_err(local_err);
1707 }
c388f408
TH
1708 }
1709 if (dpys[idx]) {
1710 printf("%s\n", DisplayType_str(dpys[idx]->type));
1711 }
1712 }
1713}