]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[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;
eae3eb3e 69static QTAILQ_HEAD(, QemuConsole) consoles =
cd6cd8fa 70 QTAILQ_HEAD_INITIALIZER(consoles);
e7f0ad58 71
0f7b2864 72static void dpy_refresh(DisplayState *s);
5209089f 73static DisplayState *get_alloc_displaystate(void);
ebced091 74static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
4b7b661d
MAL
75static bool console_compatible_with(QemuConsole *con,
76 DisplayChangeListener *dcl, Error **errp);
f9411aae 77static QemuConsole *qemu_graphic_console_lookup_unused(void);
cfde05d1 78static void dpy_set_ui_info_timer(void *opaque);
64840c66 79
98a9ad90
GH
80static void gui_update(void *opaque)
81{
0f7b2864
GH
82 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
83 uint64_t dcl_interval;
98a9ad90
GH
84 DisplayState *ds = opaque;
85 DisplayChangeListener *dcl;
86
0f7b2864 87 ds->refreshing = true;
98a9ad90 88 dpy_refresh(ds);
0f7b2864 89 ds->refreshing = false;
98a9ad90
GH
90
91 QLIST_FOREACH(dcl, &ds->listeners, next) {
0f7b2864
GH
92 dcl_interval = dcl->update_interval ?
93 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
94 if (interval > dcl_interval) {
95 interval = dcl_interval;
98a9ad90
GH
96 }
97 }
0f7b2864
GH
98 if (ds->update_interval != interval) {
99 ds->update_interval = interval;
100 trace_console_refresh(interval);
101 }
bc72ad67
AB
102 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
103 timer_mod(ds->gui_timer, ds->last_update + interval);
98a9ad90
GH
104}
105
106static void gui_setup_refresh(DisplayState *ds)
107{
108 DisplayChangeListener *dcl;
109 bool need_timer = false;
98a9ad90
GH
110
111 QLIST_FOREACH(dcl, &ds->listeners, next) {
112 if (dcl->ops->dpy_refresh != NULL) {
113 need_timer = true;
114 }
98a9ad90
GH
115 }
116
117 if (need_timer && ds->gui_timer == NULL) {
bc72ad67
AB
118 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
119 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
98a9ad90
GH
120 }
121 if (!need_timer && ds->gui_timer != NULL) {
bc72ad67 122 timer_free(ds->gui_timer);
98a9ad90
GH
123 ds->gui_timer = NULL;
124 }
98a9ad90
GH
125}
126
4d631621
MAL
127void graphic_hw_update_done(QemuConsole *con)
128{
6fc5183a 129 if (con) {
d6ee15ad 130 qemu_co_enter_all(&con->dump_queue, NULL);
6fc5183a 131 }
4d631621
MAL
132}
133
1dbfa005 134void graphic_hw_update(QemuConsole *con)
95219897 135{
4d631621 136 bool async = false;
1dbfa005 137 if (!con) {
1cd8b948 138 return;
1dbfa005 139 }
1cd8b948 140 if (con->hw_ops->gfx_update) {
380cd056 141 con->hw_ops->gfx_update(con->hw);
4d631621
MAL
142 async = con->hw_ops->gfx_update_async;
143 }
144 if (!async) {
145 graphic_hw_update_done(con);
1dbfa005 146 }
95219897
PB
147}
148
4f2c765b
MAL
149static void graphic_hw_update_bh(void *con)
150{
151 graphic_hw_update(con);
152}
153
154void qemu_console_co_wait_update(QemuConsole *con)
155{
156 if (qemu_co_queue_empty(&con->dump_queue)) {
157 /* Defer the update, it will restart the pending coroutines */
158 aio_bh_schedule_oneshot(qemu_get_aio_context(),
159 graphic_hw_update_bh, con);
160 }
161 qemu_co_queue_wait(&con->dump_queue, NULL);
162
163}
164
a9b1e471
MAL
165static void graphic_hw_gl_unblock_timer(void *opaque)
166{
167 warn_report("console: no gl-unblock within one second");
168}
169
bba19b88
GH
170void graphic_hw_gl_block(QemuConsole *con, bool block)
171{
a9b1e471 172 uint64_t timeout;
f607867c
GH
173 assert(con != NULL);
174
a4ddc314
MAL
175 if (block) {
176 con->gl_block++;
177 } else {
178 con->gl_block--;
bba19b88 179 }
a4ddc314
MAL
180 assert(con->gl_block >= 0);
181 if (!con->hw_ops->gl_block) {
182 return;
183 }
184 if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
185 return;
186 }
187 con->hw_ops->gl_block(con->hw, block);
a9b1e471
MAL
188
189 if (block) {
190 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
191 timeout += 1000; /* one sec */
192 timer_mod(con->gl_unblock_timer, timeout);
193 } else {
194 timer_del(con->gl_unblock_timer);
195 }
bba19b88
GH
196}
197
b3cb21b9
ST
198int qemu_console_get_window_id(QemuConsole *con)
199{
200 return con->window_id;
201}
202
203void qemu_console_set_window_id(QemuConsole *con, int window_id)
204{
205 con->window_id = window_id;
206}
207
1dbfa005 208void graphic_hw_invalidate(QemuConsole *con)
95219897 209{
380cd056
GH
210 if (con && con->hw_ops->invalidate) {
211 con->hw_ops->invalidate(con->hw);
1dbfa005 212 }
95219897
PB
213}
214
1dbfa005 215void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
4d3b6f6e 216{
380cd056
GH
217 if (con && con->hw_ops->text_update) {
218 con->hw_ops->text_update(con->hw, chardata);
219 }
4d3b6f6e
AZ
220}
221
26b032b9 222static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
c84ab0a5
MAL
223 struct DisplaySurface *new_surface,
224 bool update)
26b032b9
MAL
225{
226 if (dcl->ops->dpy_gfx_switch) {
227 dcl->ops->dpy_gfx_switch(dcl, new_surface);
228 }
c84ab0a5
MAL
229
230 if (update && dcl->ops->dpy_gfx_update) {
231 dcl->ops->dpy_gfx_update(dcl, 0, 0,
232 surface_width(new_surface),
233 surface_height(new_surface));
234 }
26b032b9
MAL
235}
236
589089fe
MAL
237static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
238{
239 if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
240 con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
241 }
242}
243
244static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
245{
246 if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
247 con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
248 }
249}
250
251static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
252 int x, int y, int w, int h)
253{
254 if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
255 con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
256 }
257}
26b032b9 258
ebced091 259static void displaychangelistener_display_console(DisplayChangeListener *dcl,
4b7b661d 260 Error **errp)
ebced091
MAL
261{
262 static const char nodev[] =
263 "This VM has no graphic display device.";
264 static DisplaySurface *dummy;
e99441a3 265 QemuConsole *con = dcl->con;
ebced091 266
4b7b661d 267 if (!con || !console_compatible_with(con, dcl, errp)) {
ebced091
MAL
268 if (!dummy) {
269 dummy = qemu_create_placeholder_surface(640, 480, nodev);
270 }
589089fe
MAL
271 if (con) {
272 dpy_gfx_create_texture(con, dummy);
273 }
c84ab0a5 274 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
ebced091
MAL
275 return;
276 }
277
e1c676a2
MAL
278 dpy_gfx_create_texture(con, con->surface);
279 displaychangelistener_gfx_switch(dcl, con->surface,
280 con->scanout.kind == SCANOUT_SURFACE);
281
ebced091
MAL
282 if (con->scanout.kind == SCANOUT_DMABUF &&
283 displaychangelistener_has_dmabuf(dcl)) {
284 dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
285 } else if (con->scanout.kind == SCANOUT_TEXTURE &&
286 dcl->ops->dpy_gl_scanout_texture) {
287 dcl->ops->dpy_gl_scanout_texture(dcl,
288 con->scanout.texture.backing_id,
289 con->scanout.texture.backing_y_0_top,
290 con->scanout.texture.backing_width,
291 con->scanout.texture.backing_height,
292 con->scanout.texture.x,
293 con->scanout.texture.y,
294 con->scanout.texture.width,
bf41ab61
MAL
295 con->scanout.texture.height,
296 con->scanout.texture.d3d_tex2d);
ebced091 297 }
ebced091
MAL
298}
299
f7ce755d
MAL
300void qemu_text_console_put_keysym(QemuTextConsole *s, int keysym)
301{
f7ce755d
MAL
302 qemu_text_console_handle_keysym(s, keysym);
303}
304
7fb1cf16 305static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
50ef4679
GH
306 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
307 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
308 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
309 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
310 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
311 [Q_KEY_CODE_END] = QEMU_KEY_END,
312 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
313 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
314 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
df6322a8 315 [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
344aa283 316 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
50ef4679
GH
317};
318
da024b1e
GH
319static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
320 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
321 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
322 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
323 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
324 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
325 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
326 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
327 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
328};
329
cc6ba2c6 330bool qemu_text_console_put_qcode(QemuTextConsole *s, int qcode, bool ctrl)
50ef4679
GH
331{
332 int keysym;
333
da024b1e 334 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
50ef4679
GH
335 if (keysym == 0) {
336 return false;
337 }
cc6ba2c6 338 qemu_text_console_put_keysym(s, keysym);
50ef4679
GH
339 return true;
340}
341
cc6ba2c6 342void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len)
bdef9724
GH
343{
344 int i;
345
346 for (i = 0; i < len && str[i]; i++) {
cc6ba2c6 347 qemu_text_console_put_keysym(s, str[i]);
bdef9724
GH
348 }
349}
350
098d57e7 351static void
c105d60f 352qemu_console_register(QemuConsole *c)
e7f0ad58 353{
95219897 354 int i;
e7f0ad58 355
cd6cd8fa 356 if (QTAILQ_EMPTY(&consoles)) {
098d57e7
MAL
357 c->index = 0;
358 QTAILQ_INSERT_TAIL(&consoles, c, next);
c105d60f 359 } else if (!QEMU_IS_GRAPHIC_CONSOLE(c) || phase_check(PHASE_MACHINE_READY)) {
eae3eb3e 360 QemuConsole *last = QTAILQ_LAST(&consoles);
098d57e7
MAL
361 c->index = last->index + 1;
362 QTAILQ_INSERT_TAIL(&consoles, c, next);
95219897 363 } else {
9588d67e
GH
364 /*
365 * HACK: Put graphical consoles before text consoles.
366 *
367 * Only do that for coldplugged devices. After initial device
368 * initialization we will not renumber the consoles any more.
369 */
098d57e7 370 QemuConsole *it = QTAILQ_FIRST(&consoles);
cd6cd8fa 371
c105d60f 372 while (QTAILQ_NEXT(it, next) != NULL && QEMU_IS_GRAPHIC_CONSOLE(it)) {
098d57e7 373 it = QTAILQ_NEXT(it, next);
cd6cd8fa 374 }
c105d60f 375 if (QEMU_IS_GRAPHIC_CONSOLE(it)) {
cd6cd8fa 376 /* have no text consoles */
098d57e7
MAL
377 c->index = it->index + 1;
378 QTAILQ_INSERT_AFTER(&consoles, it, c, next);
cd6cd8fa 379 } else {
098d57e7
MAL
380 c->index = it->index;
381 QTAILQ_INSERT_BEFORE(it, c, next);
cd6cd8fa 382 /* renumber text consoles */
098d57e7
MAL
383 for (i = c->index + 1; it != NULL; it = QTAILQ_NEXT(it, next), i++) {
384 it->index = i;
cd6cd8fa 385 }
95219897 386 }
95219897 387 }
95219897
PB
388}
389
e265917c
MAL
390static void
391qemu_console_finalize(Object *obj)
392{
cfde05d1
MAL
393 QemuConsole *c = QEMU_CONSOLE(obj);
394
463c6b19 395 /* TODO: check this code path, and unregister from consoles */
463c6b19
MAL
396 g_clear_pointer(&c->surface, qemu_free_displaysurface);
397 g_clear_pointer(&c->gl_unblock_timer, timer_free);
cfde05d1 398 g_clear_pointer(&c->ui_timer, timer_free);
098d57e7
MAL
399}
400
e265917c
MAL
401static void
402qemu_console_class_init(ObjectClass *oc, void *data)
403{
404}
405
406static void
407qemu_console_init(Object *obj)
408{
098d57e7
MAL
409 QemuConsole *c = QEMU_CONSOLE(obj);
410 DisplayState *ds = get_alloc_displaystate();
411
412 qemu_co_queue_init(&c->dump_queue);
413 c->ds = ds;
414 c->window_id = -1;
cfde05d1
MAL
415 c->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
416 dpy_set_ui_info_timer, c);
ba0ec5c2 417 qemu_console_register(c);
098d57e7
MAL
418}
419
b208f745
MAL
420static void
421qemu_graphic_console_finalize(Object *obj)
422{
58d58708
MAL
423 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
424
425 g_clear_pointer(&c->device, object_unref);
426}
427
428static void
429qemu_graphic_console_prop_get_head(Object *obj, Visitor *v, const char *name,
430 void *opaque, Error **errp)
431{
432 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
433
434 visit_type_uint32(v, name, &c->head, errp);
b208f745
MAL
435}
436
437static void
438qemu_graphic_console_class_init(ObjectClass *oc, void *data)
439{
58d58708
MAL
440 object_class_property_add_link(oc, "device", TYPE_DEVICE,
441 offsetof(QemuGraphicConsole, device),
442 object_property_allow_set_link,
443 OBJ_PROP_LINK_STRONG);
444 object_class_property_add(oc, "head", "uint32",
445 qemu_graphic_console_prop_get_head,
446 NULL, NULL, NULL);
b208f745
MAL
447}
448
449static void
450qemu_graphic_console_init(Object *obj)
451{
452}
453
09b4c198
MAL
454#ifdef WIN32
455void qemu_displaysurface_win32_set_handle(DisplaySurface *surface,
456 HANDLE h, uint32_t offset)
457{
458 assert(!surface->handle);
459
460 surface->handle = h;
461 surface->handle_offset = offset;
462}
463
464static void
465win32_pixman_image_destroy(pixman_image_t *image, void *data)
466{
467 DisplaySurface *surface = data;
468
469 if (!surface->handle) {
470 return;
471 }
472
473 assert(surface->handle_offset == 0);
474
475 qemu_win32_map_free(
476 pixman_image_get_data(surface->image),
477 surface->handle,
478 &error_warn
479 );
480}
481#endif
482
eb69442a 483DisplaySurface *qemu_create_displaysurface(int width, int height)
ffe8b821 484{
09b4c198
MAL
485 DisplaySurface *surface;
486 void *bits = NULL;
487#ifdef WIN32
488 HANDLE handle = NULL;
489#endif
69c77777 490
09b4c198
MAL
491 trace_displaysurface_create(width, height);
492
493#ifdef WIN32
494 bits = qemu_win32_map_alloc(width * height * 4, &handle, &error_abort);
495#endif
496
497 surface = qemu_create_displaysurface_from(
498 width, height,
499 PIXMAN_x8r8g8b8,
500 width * 4, bits
501 );
30f1e661 502 surface->flags = QEMU_ALLOCATED_FLAG;
98b50080 503
09b4c198
MAL
504#ifdef WIN32
505 qemu_displaysurface_win32_set_handle(surface, handle, 0);
506#endif
537a4391
GH
507 return surface;
508}
509
30f1e661
GH
510DisplaySurface *qemu_create_displaysurface_from(int width, int height,
511 pixman_format_code_t format,
512 int linesize, uint8_t *data)
98b50080 513{
69c77777 514 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 515
30f1e661 516 trace_displaysurface_create_from(surface, width, height, format);
ff174c67 517 surface->image = pixman_image_create_bits(format,
69c77777
GH
518 width, height,
519 (void *)data, linesize);
520 assert(surface->image != NULL);
09b4c198
MAL
521#ifdef WIN32
522 pixman_image_set_destroy_function(surface->image,
523 win32_pixman_image_destroy, surface);
524#endif
69c77777 525
98b50080
PB
526 return surface;
527}
528
ca58b45f
GH
529DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
530{
531 DisplaySurface *surface = g_new0(DisplaySurface, 1);
532
533 trace_displaysurface_create_pixman(surface);
ca58b45f
GH
534 surface->image = pixman_image_ref(image);
535
536 return surface;
537}
538
b5a087b0
AO
539DisplaySurface *qemu_create_placeholder_surface(int w, int h,
540 const char *msg)
d3002b04 541{
521a580d 542 DisplaySurface *surface = qemu_create_displaysurface(w, h);
d7e94796 543#ifdef CONFIG_PIXMAN
1ece6777
MAL
544 pixman_color_t bg = QEMU_PIXMAN_COLOR_BLACK;
545 pixman_color_t fg = QEMU_PIXMAN_COLOR_GRAY;
d3002b04
GH
546 pixman_image_t *glyph;
547 int len, x, y, i;
548
549 len = strlen(msg);
521a580d
GH
550 x = (w / FONT_WIDTH - len) / 2;
551 y = (h / FONT_HEIGHT - 1) / 2;
d3002b04
GH
552 for (i = 0; i < len; i++) {
553 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
554 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
555 x+i, y, FONT_WIDTH, FONT_HEIGHT);
556 qemu_pixman_image_unref(glyph);
557 }
d7e94796 558#endif
b5a087b0 559 surface->flags |= QEMU_PLACEHOLDER_FLAG;
d3002b04
GH
560 return surface;
561}
562
da229ef3 563void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 564{
da229ef3 565 if (surface == NULL) {
98b50080 566 return;
187cd1d9 567 }
da229ef3
GH
568 trace_displaysurface_free(surface);
569 qemu_pixman_image_unref(surface->image);
570 g_free(surface);
98b50080
PB
571}
572
06020b95
GH
573bool console_has_gl(QemuConsole *con)
574{
575 return con->gl != NULL;
576}
577
d0e137bc
MAL
578static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
579{
580 if (dcl->ops->dpy_has_dmabuf) {
581 return dcl->ops->dpy_has_dmabuf(dcl);
582 }
583
584 if (dcl->ops->dpy_gl_scanout_dmabuf) {
585 return true;
586 }
587
588 return false;
589}
590
4b7b661d
MAL
591static bool console_compatible_with(QemuConsole *con,
592 DisplayChangeListener *dcl, Error **errp)
5983fdf1 593{
5983fdf1
MAL
594 int flags;
595
596 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
597
a62c4a17
MAL
598 if (console_has_gl(con) &&
599 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
398d1c91
MAL
600 error_setg(errp, "Display %s is incompatible with the GL context",
601 dcl->ops->dpy_name);
602 return false;
603 }
604
5983fdf1
MAL
605 if (flags & GRAPHIC_FLAGS_GL &&
606 !console_has_gl(con)) {
607 error_setg(errp, "The console requires a GL context.");
608 return false;
609
610 }
611
612 if (flags & GRAPHIC_FLAGS_DMABUF &&
613 !displaychangelistener_has_dmabuf(dcl)) {
614 error_setg(errp, "The console requires display DMABUF support.");
615 return false;
616 }
617
618 return true;
619}
620
b6596785
BE
621void console_handle_touch_event(QemuConsole *con,
622 struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
623 uint64_t num_slot,
624 int width, int height,
625 double x, double y,
626 InputMultiTouchType type,
627 Error **errp)
628{
629 struct touch_slot *slot;
630 bool needs_sync = false;
631 int update;
632 int i;
633
634 if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
635 error_setg(errp,
636 "Unexpected touch slot number: % " PRId64" >= %d",
637 num_slot, INPUT_EVENT_SLOTS_MAX);
638 return;
639 }
640
641 slot = &touch_slots[num_slot];
642 slot->x = x;
643 slot->y = y;
644
645 if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
646 slot->tracking_id = num_slot;
647 }
648
649 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
650 if (i == num_slot) {
651 update = type;
652 } else {
653 update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
654 }
655
656 slot = &touch_slots[i];
657
658 if (slot->tracking_id == -1) {
659 continue;
660 }
661
662 if (update == INPUT_MULTI_TOUCH_TYPE_END) {
663 slot->tracking_id = -1;
664 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
665 needs_sync = true;
666 } else {
667 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
668 qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
669 qemu_input_queue_mtt_abs(con,
670 INPUT_AXIS_X, (int) slot->x,
671 0, width,
672 i, slot->tracking_id);
673 qemu_input_queue_mtt_abs(con,
674 INPUT_AXIS_Y, (int) slot->y,
675 0, height,
676 i, slot->tracking_id);
677 needs_sync = true;
678 }
679 }
680
681 if (needs_sync) {
682 qemu_input_event_sync();
683 }
684}
685
5e79d516 686void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
4f418149
MAL
687{
688 /* display has opengl support */
5e79d516
MAL
689 assert(con);
690 if (con->gl) {
691 error_report("The console already has an OpenGL context.");
4f418149
MAL
692 exit(1);
693 }
5e79d516
MAL
694 con->gl = gl;
695}
696
58d58708
MAL
697static void
698dcl_set_graphic_cursor(DisplayChangeListener *dcl, QemuGraphicConsole *con)
699{
700 if (con && con->cursor && dcl->ops->dpy_cursor_define) {
701 dcl->ops->dpy_cursor_define(dcl, con->cursor);
702 }
703 if (con && dcl->ops->dpy_mouse_set) {
704 dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
705 }
706}
6f110819 707
5209089f 708void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 709{
e0665c3b
MAL
710 assert(!dcl->ds);
711
7c20b4a3 712 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
713 dcl->ds = get_alloc_displaystate();
714 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
715 gui_setup_refresh(dcl->ds);
284d1c6b
GH
716 if (dcl->con) {
717 dcl->con->dcls++;
284d1c6b 718 }
e99441a3
AO
719 displaychangelistener_display_console(dcl, &error_fatal);
720 if (QEMU_IS_GRAPHIC_CONSOLE(dcl->con)) {
721 dcl_set_graphic_cursor(dcl, QEMU_GRAPHIC_CONSOLE(dcl->con));
722 } else if (QEMU_IS_TEXT_CONSOLE(dcl->con)) {
723 qemu_text_console_update_size(QEMU_TEXT_CONSOLE(dcl->con));
6effaa16 724 }
6f110819 725 qemu_text_console_update_cursor();
7c20b4a3
GH
726}
727
0f7b2864
GH
728void update_displaychangelistener(DisplayChangeListener *dcl,
729 uint64_t interval)
730{
731 DisplayState *ds = dcl->ds;
732
733 dcl->update_interval = interval;
734 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 735 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
736 }
737}
738
7c20b4a3
GH
739void unregister_displaychangelistener(DisplayChangeListener *dcl)
740{
741 DisplayState *ds = dcl->ds;
742 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
743 if (dcl->con) {
744 dcl->con->dcls--;
745 }
7c20b4a3 746 QLIST_REMOVE(dcl, next);
777c5f1e 747 dcl->ds = NULL;
7c20b4a3
GH
748 gui_setup_refresh(ds);
749}
750
cf1ecc82
GH
751static void dpy_set_ui_info_timer(void *opaque)
752{
753 QemuConsole *con = opaque;
58d58708 754 uint32_t head = qemu_console_get_head(con);
cf1ecc82 755
58d58708 756 con->hw_ops->ui_info(con->hw, head, &con->ui_info);
cf1ecc82
GH
757}
758
a92e7bb4 759bool dpy_ui_info_supported(const QemuConsole *con)
b7fb49f0 760{
48a35e12
MAL
761 if (con == NULL) {
762 return false;
763 }
5c4b107f 764
b7fb49f0
GH
765 return con->hw_ops->ui_info != NULL;
766}
767
5eaf1e48
MAL
768const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
769{
a92e7bb4
MAL
770 assert(dpy_ui_info_supported(con));
771
5eaf1e48
MAL
772 return &con->ui_info;
773}
774
ca19ef52 775int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
6f90f3d7 776{
b7fb49f0 777 if (!dpy_ui_info_supported(con)) {
cf1ecc82 778 return -1;
6f90f3d7 779 }
1185fde4
GH
780 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
781 /* nothing changed -- ignore */
782 return 0;
783 }
cf1ecc82
GH
784
785 /*
786 * Typically we get a flood of these as the user resizes the window.
787 * Wait until the dust has settled (one second without updates), then
788 * go notify the guest.
789 */
1185fde4 790 con->ui_info = *info;
ca19ef52
MAL
791 timer_mod(con->ui_timer,
792 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
cf1ecc82 793 return 0;
6f90f3d7
GH
794}
795
c78f7137 796void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 797{
c78f7137 798 DisplayState *s = con->ds;
284d1c6b 799 DisplayChangeListener *dcl;
ebced091
MAL
800 int width = qemu_console_get_width(con, x + w);
801 int height = qemu_console_get_height(con, y + h);
7c20b4a3
GH
802
803 x = MAX(x, 0);
804 y = MAX(y, 0);
805 x = MIN(x, width);
806 y = MIN(y, height);
807 w = MIN(w, width - x);
808 h = MIN(h, height - y);
809
81c0d5a6 810 if (!qemu_console_is_visible(con)) {
321f048d
GH
811 return;
812 }
589089fe 813 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
7c20b4a3 814 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 815 if (con != dcl->con) {
284d1c6b
GH
816 continue;
817 }
7c20b4a3 818 if (dcl->ops->dpy_gfx_update) {
bc2ed970 819 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
820 }
821 }
822}
823
7cd0afe6
TZ
824void dpy_gfx_update_full(QemuConsole *con)
825{
ebced091
MAL
826 int w = qemu_console_get_width(con, 0);
827 int h = qemu_console_get_height(con, 0);
828
829 dpy_gfx_update(con, 0, 0, w, h);
7cd0afe6
TZ
830}
831
321f048d
GH
832void dpy_gfx_replace_surface(QemuConsole *con,
833 DisplaySurface *surface)
834{
c821a58e 835 static const char placeholder_msg[] = "Display output is not active.";
321f048d
GH
836 DisplayState *s = con->ds;
837 DisplaySurface *old_surface = con->surface;
0d0be876 838 DisplaySurface *new_surface = surface;
284d1c6b 839 DisplayChangeListener *dcl;
c821a58e
AO
840 int width;
841 int height;
842
843 if (!surface) {
844 if (old_surface) {
845 width = surface_width(old_surface);
846 height = surface_height(old_surface);
847 } else {
848 width = 640;
849 height = 480;
850 }
851
0d0be876 852 new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
c821a58e 853 }
321f048d 854
0d0be876 855 assert(old_surface != new_surface);
6905b934 856
ebced091 857 con->scanout.kind = SCANOUT_SURFACE;
0d0be876
DK
858 con->surface = new_surface;
859 dpy_gfx_create_texture(con, new_surface);
284d1c6b 860 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 861 if (con != dcl->con) {
284d1c6b
GH
862 continue;
863 }
0d0be876 864 displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
321f048d 865 }
589089fe 866 dpy_gfx_destroy_texture(con, old_surface);
da229ef3 867 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
868}
869
49743df3
BH
870bool dpy_gfx_check_format(QemuConsole *con,
871 pixman_format_code_t format)
872{
873 DisplayChangeListener *dcl;
874 DisplayState *s = con->ds;
875
876 QLIST_FOREACH(dcl, &s->listeners, next) {
877 if (dcl->con && dcl->con != con) {
878 /* dcl bound to another console -> skip */
879 continue;
880 }
881 if (dcl->ops->dpy_gfx_check_format) {
882 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
883 return false;
884 }
885 } else {
75ae7c46 886 /* default is to allow native 32 bpp only */
49743df3
BH
887 if (format != qemu_default_pixman_format(32, true)) {
888 return false;
889 }
890 }
891 }
892 return true;
893}
894
6075137d 895static void dpy_refresh(DisplayState *s)
7c20b4a3 896{
284d1c6b
GH
897 DisplayChangeListener *dcl;
898
7c20b4a3
GH
899 QLIST_FOREACH(dcl, &s->listeners, next) {
900 if (dcl->ops->dpy_refresh) {
3f8f1313 901 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
902 }
903 }
904}
905
c78f7137 906void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 907{
c78f7137 908 DisplayState *s = con->ds;
284d1c6b 909 DisplayChangeListener *dcl;
321f048d 910
81c0d5a6 911 if (!qemu_console_is_visible(con)) {
321f048d
GH
912 return;
913 }
7c20b4a3 914 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 915 if (con != dcl->con) {
284d1c6b
GH
916 continue;
917 }
7c20b4a3 918 if (dcl->ops->dpy_text_cursor) {
bc2ed970 919 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
920 }
921 }
922}
923
c78f7137 924void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 925{
c78f7137 926 DisplayState *s = con->ds;
284d1c6b 927 DisplayChangeListener *dcl;
321f048d 928
81c0d5a6 929 if (!qemu_console_is_visible(con)) {
321f048d
GH
930 return;
931 }
7c20b4a3 932 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 933 if (con != dcl->con) {
284d1c6b
GH
934 continue;
935 }
7c20b4a3 936 if (dcl->ops->dpy_text_update) {
bc2ed970 937 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
938 }
939 }
940}
941
c78f7137 942void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 943{
c78f7137 944 DisplayState *s = con->ds;
9425c004 945 DisplayChangeListener *dcl;
321f048d 946
81c0d5a6 947 if (!qemu_console_is_visible(con)) {
321f048d
GH
948 return;
949 }
7c20b4a3 950 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 951 if (con != dcl->con) {
284d1c6b
GH
952 continue;
953 }
7c20b4a3 954 if (dcl->ops->dpy_text_resize) {
bc2ed970 955 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
956 }
957 }
958}
959
58d58708 960void dpy_mouse_set(QemuConsole *c, int x, int y, int on)
7c20b4a3 961{
58d58708
MAL
962 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
963 DisplayState *s = c->ds;
284d1c6b 964 DisplayChangeListener *dcl;
321f048d 965
6effaa16
MAL
966 con->cursor_x = x;
967 con->cursor_y = y;
968 con->cursor_on = on;
58d58708 969 if (!qemu_console_is_visible(c)) {
321f048d
GH
970 return;
971 }
7c20b4a3 972 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 973 if (c != dcl->con) {
284d1c6b
GH
974 continue;
975 }
7c20b4a3 976 if (dcl->ops->dpy_mouse_set) {
bc2ed970 977 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
978 }
979 }
980}
981
58d58708 982void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
7c20b4a3 983{
58d58708
MAL
984 QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
985 DisplayState *s = c->ds;
284d1c6b 986 DisplayChangeListener *dcl;
321f048d 987
385ac97f
MAL
988 cursor_unref(con->cursor);
989 con->cursor = cursor_ref(cursor);
58d58708 990 if (!qemu_console_is_visible(c)) {
321f048d
GH
991 return;
992 }
7c20b4a3 993 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 994 if (c != dcl->con) {
284d1c6b
GH
995 continue;
996 }
7c20b4a3 997 if (dcl->ops->dpy_cursor_define) {
bc2ed970 998 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
999 }
1000 }
1001}
1002
c78f7137 1003bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1004{
c78f7137 1005 DisplayState *s = con->ds;
284d1c6b
GH
1006 DisplayChangeListener *dcl;
1007
7c20b4a3
GH
1008 QLIST_FOREACH(dcl, &s->listeners, next) {
1009 if (dcl->ops->dpy_cursor_define) {
1010 return true;
1011 }
1012 }
1013 return false;
1014}
1015
06020b95
GH
1016QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1017 struct QEMUGLParams *qparams)
1018{
1019 assert(con->gl);
1020 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1021}
1022
1023void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1024{
1025 assert(con->gl);
1026 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1027}
1028
1029int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1030{
1031 assert(con->gl);
1032 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1033}
1034
eaa92c76
GH
1035void dpy_gl_scanout_disable(QemuConsole *con)
1036{
7cc712e9
MAL
1037 DisplayState *s = con->ds;
1038 DisplayChangeListener *dcl;
1039
ebced091
MAL
1040 if (con->scanout.kind != SCANOUT_SURFACE) {
1041 con->scanout.kind = SCANOUT_NONE;
1042 }
7cc712e9 1043 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1044 if (con != dcl->con) {
1699d00e
AO
1045 continue;
1046 }
a9fbce5e
MAL
1047 if (dcl->ops->dpy_gl_scanout_disable) {
1048 dcl->ops->dpy_gl_scanout_disable(dcl);
1049 }
7cc712e9 1050 }
eaa92c76
GH
1051}
1052
f4c36bda
GH
1053void dpy_gl_scanout_texture(QemuConsole *con,
1054 uint32_t backing_id,
1055 bool backing_y_0_top,
1056 uint32_t backing_width,
1057 uint32_t backing_height,
1058 uint32_t x, uint32_t y,
bf41ab61
MAL
1059 uint32_t width, uint32_t height,
1060 void *d3d_tex2d)
06020b95 1061{
7cc712e9
MAL
1062 DisplayState *s = con->ds;
1063 DisplayChangeListener *dcl;
1064
ebced091
MAL
1065 con->scanout.kind = SCANOUT_TEXTURE;
1066 con->scanout.texture = (ScanoutTexture) {
1067 backing_id, backing_y_0_top, backing_width, backing_height,
bf41ab61 1068 x, y, width, height, d3d_tex2d,
ebced091 1069 };
7cc712e9 1070 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1071 if (con != dcl->con) {
1699d00e
AO
1072 continue;
1073 }
a9fbce5e
MAL
1074 if (dcl->ops->dpy_gl_scanout_texture) {
1075 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1076 backing_y_0_top,
1077 backing_width, backing_height,
bf41ab61
MAL
1078 x, y, width, height,
1079 d3d_tex2d);
a9fbce5e 1080 }
7cc712e9 1081 }
06020b95
GH
1082}
1083
4133fa71
GH
1084void dpy_gl_scanout_dmabuf(QemuConsole *con,
1085 QemuDmaBuf *dmabuf)
1086{
7cc712e9
MAL
1087 DisplayState *s = con->ds;
1088 DisplayChangeListener *dcl;
1089
ebced091
MAL
1090 con->scanout.kind = SCANOUT_DMABUF;
1091 con->scanout.dmabuf = dmabuf;
7cc712e9 1092 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1093 if (con != dcl->con) {
1699d00e
AO
1094 continue;
1095 }
a9fbce5e
MAL
1096 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1097 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1098 }
7cc712e9 1099 }
4133fa71
GH
1100}
1101
6e1f2cb5
GH
1102void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1103 bool have_hot, uint32_t hot_x, uint32_t hot_y)
4133fa71 1104{
7cc712e9
MAL
1105 DisplayState *s = con->ds;
1106 DisplayChangeListener *dcl;
4133fa71 1107
7cc712e9 1108 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1109 if (con != dcl->con) {
1699d00e
AO
1110 continue;
1111 }
7cc712e9
MAL
1112 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1113 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
6e1f2cb5 1114 have_hot, hot_x, hot_y);
7cc712e9 1115 }
6e1f2cb5
GH
1116 }
1117}
1118
1119void dpy_gl_cursor_position(QemuConsole *con,
1120 uint32_t pos_x, uint32_t pos_y)
1121{
7cc712e9
MAL
1122 DisplayState *s = con->ds;
1123 DisplayChangeListener *dcl;
6e1f2cb5 1124
7cc712e9 1125 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1126 if (con != dcl->con) {
1699d00e
AO
1127 continue;
1128 }
7cc712e9
MAL
1129 if (dcl->ops->dpy_gl_cursor_position) {
1130 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1131 }
4133fa71
GH
1132 }
1133}
1134
1135void dpy_gl_release_dmabuf(QemuConsole *con,
1136 QemuDmaBuf *dmabuf)
1137{
7cc712e9
MAL
1138 DisplayState *s = con->ds;
1139 DisplayChangeListener *dcl;
4133fa71 1140
7cc712e9 1141 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1142 if (con != dcl->con) {
1699d00e
AO
1143 continue;
1144 }
7cc712e9
MAL
1145 if (dcl->ops->dpy_gl_release_dmabuf) {
1146 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
1147 }
4133fa71
GH
1148 }
1149}
1150
06020b95
GH
1151void dpy_gl_update(QemuConsole *con,
1152 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1153{
7cc712e9
MAL
1154 DisplayState *s = con->ds;
1155 DisplayChangeListener *dcl;
1156
06020b95 1157 assert(con->gl);
f6413cbf
MAL
1158
1159 graphic_hw_gl_block(con, true);
7cc712e9 1160 QLIST_FOREACH(dcl, &s->listeners, next) {
e99441a3 1161 if (con != dcl->con) {
1699d00e
AO
1162 continue;
1163 }
a9fbce5e
MAL
1164 if (dcl->ops->dpy_gl_update) {
1165 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
1166 }
7cc712e9 1167 }
f6413cbf 1168 graphic_hw_gl_block(con, false);
06020b95
GH
1169}
1170
98b50080
PB
1171/***********************************************************/
1172/* register display */
1173
64840c66
GH
1174/* console.c internal use only */
1175static DisplayState *get_alloc_displaystate(void)
98b50080 1176{
64840c66
GH
1177 if (!display_state) {
1178 display_state = g_new0(DisplayState, 1);
1179 }
1180 return display_state;
98b50080
PB
1181}
1182
64840c66
GH
1183/*
1184 * Called by main(), after creating QemuConsoles
1185 * and before initializing ui (sdl/vnc/...).
1186 */
1187DisplayState *init_displaystate(void)
98b50080 1188{
43f420f8 1189 gchar *name;
cd6cd8fa 1190 QemuConsole *con;
64840c66 1191
cd6cd8fa 1192 QTAILQ_FOREACH(con, &consoles, next) {
34b77515 1193 /* Hook up into the qom tree here (not in object_new()), once
43f420f8
GH
1194 * all QemuConsoles are created and the order / numbering
1195 * doesn't change any more */
cd6cd8fa 1196 name = g_strdup_printf("console[%d]", con->index);
43f420f8 1197 object_property_add_child(container_get(object_get_root(), "/backend"),
d2623129 1198 name, OBJECT(con));
43f420f8 1199 g_free(name);
64840c66
GH
1200 }
1201
98b50080
PB
1202 return display_state;
1203}
1204
1c1f9498
GH
1205void graphic_console_set_hwops(QemuConsole *con,
1206 const GraphicHwOps *hw_ops,
1207 void *opaque)
1208{
1209 con->hw_ops = hw_ops;
1210 con->hw = opaque;
1211}
1212
5643706a 1213QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 1214 const GraphicHwOps *hw_ops,
c78f7137 1215 void *opaque)
95219897 1216{
521a580d
GH
1217 static const char noinit[] =
1218 "Guest has not initialized the display (yet).";
64840c66
GH
1219 int width = 640;
1220 int height = 480;
76ffb0b4 1221 QemuConsole *s;
9588d67e 1222 DisplaySurface *surface;
f0f2f976 1223
f9411aae 1224 s = qemu_graphic_console_lookup_unused();
9588d67e
GH
1225 if (s) {
1226 trace_console_gfx_reuse(s->index);
ebced091
MAL
1227 width = qemu_console_get_width(s, 0);
1228 height = qemu_console_get_height(s, 0);
9588d67e
GH
1229 } else {
1230 trace_console_gfx_new();
34b77515 1231 s = (QemuConsole *)object_new(TYPE_QEMU_GRAPHIC_CONSOLE);
9588d67e 1232 }
58d58708 1233 QEMU_GRAPHIC_CONSOLE(s)->head = head;
1c1f9498 1234 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 1235 if (dev) {
5325cc34 1236 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
afff2b15 1237 &error_abort);
aa2beaa1 1238 }
3023f332 1239
b5a087b0 1240 surface = qemu_create_placeholder_surface(width, height, noinit);
9588d67e 1241 dpy_gfx_replace_surface(s, surface);
a9b1e471
MAL
1242 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1243 graphic_hw_gl_unblock_timer, s);
c78f7137 1244 return s;
e7f0ad58
FB
1245}
1246
9588d67e
GH
1247static const GraphicHwOps unused_ops = {
1248 /* no callbacks */
1249};
1250
1251void graphic_console_close(QemuConsole *con)
1252{
1253 static const char unplugged[] =
1254 "Guest display has been unplugged";
1255 DisplaySurface *surface;
ebced091
MAL
1256 int width = qemu_console_get_width(con, 640);
1257 int height = qemu_console_get_height(con, 480);
9588d67e
GH
1258
1259 trace_console_gfx_close(con->index);
5325cc34 1260 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
9588d67e
GH
1261 graphic_console_set_hwops(con, &unused_ops, NULL);
1262
1263 if (con->gl) {
1264 dpy_gl_scanout_disable(con);
1265 }
b5a087b0 1266 surface = qemu_create_placeholder_surface(width, height, unplugged);
9588d67e
GH
1267 dpy_gfx_replace_surface(con, surface);
1268}
1269
d4c19956
AO
1270QemuConsole *qemu_console_lookup_default(void)
1271{
1272 QemuConsole *con;
1273
1274 QTAILQ_FOREACH(con, &consoles, next) {
1275 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1276 return con;
1277 }
1278 }
1279 return QTAILQ_FIRST(&consoles);
1280}
1281
284d1c6b
GH
1282QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1283{
cd6cd8fa
GH
1284 QemuConsole *con;
1285
1286 QTAILQ_FOREACH(con, &consoles, next) {
1287 if (con->index == index) {
1288 return con;
1289 }
284d1c6b 1290 }
cd6cd8fa 1291 return NULL;
284d1c6b
GH
1292}
1293
5643706a 1294QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 1295{
cd6cd8fa 1296 QemuConsole *con;
14a93649 1297 Object *obj;
5643706a 1298 uint32_t h;
14a93649 1299
cd6cd8fa
GH
1300 QTAILQ_FOREACH(con, &consoles, next) {
1301 obj = object_property_get_link(OBJECT(con),
afff2b15 1302 "device", &error_abort);
5643706a
GH
1303 if (DEVICE(obj) != dev) {
1304 continue;
1305 }
cd6cd8fa 1306 h = object_property_get_uint(OBJECT(con),
ad664c1d 1307 "head", &error_abort);
5643706a
GH
1308 if (h != head) {
1309 continue;
14a93649 1310 }
cd6cd8fa 1311 return con;
14a93649
GH
1312 }
1313 return NULL;
1314}
1315
f2c1d54c
GH
1316QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1317 uint32_t head, Error **errp)
1318{
1319 DeviceState *dev;
1320 QemuConsole *con;
1321
1322 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1323 if (dev == NULL) {
1324 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1325 "Device '%s' not found", device_id);
1326 return NULL;
1327 }
1328
1329 con = qemu_console_lookup_by_device(dev, head);
1330 if (con == NULL) {
1331 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1332 device_id, head);
1333 return NULL;
1334 }
1335
1336 return con;
1337}
1338
f9411aae 1339static QemuConsole *qemu_graphic_console_lookup_unused(void)
9588d67e 1340{
cd6cd8fa 1341 QemuConsole *con;
9588d67e 1342 Object *obj;
9588d67e 1343
cd6cd8fa 1344 QTAILQ_FOREACH(con, &consoles, next) {
f9411aae 1345 if (!QEMU_IS_GRAPHIC_CONSOLE(con) || con->hw_ops != &unused_ops) {
9588d67e
GH
1346 continue;
1347 }
cd6cd8fa 1348 obj = object_property_get_link(OBJECT(con),
9588d67e
GH
1349 "device", &error_abort);
1350 if (obj != NULL) {
1351 continue;
1352 }
cd6cd8fa 1353 return con;
9588d67e
GH
1354 }
1355 return NULL;
1356}
1357
385ac97f
MAL
1358QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
1359{
58d58708 1360 return QEMU_IS_GRAPHIC_CONSOLE(con) ? QEMU_GRAPHIC_CONSOLE(con)->cursor : NULL;
385ac97f
MAL
1361}
1362
81c0d5a6 1363bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 1364{
e99441a3 1365 return con->dcls > 0;
e7f0ad58
FB
1366}
1367
81c0d5a6 1368bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 1369{
c105d60f 1370 return con && QEMU_IS_GRAPHIC_CONSOLE(con);
81c0d5a6
GH
1371}
1372
1373bool qemu_console_is_fixedsize(QemuConsole *con)
1374{
c105d60f 1375 return con && (QEMU_IS_GRAPHIC_CONSOLE(con) || QEMU_IS_FIXED_TEXT_CONSOLE(con));
c21bbcfa
AZ
1376}
1377
f607867c
GH
1378bool qemu_console_is_gl_blocked(QemuConsole *con)
1379{
1380 assert(con != NULL);
1381 return con->gl_block;
1382}
1383
2c0c4c1f 1384static bool qemu_graphic_console_is_multihead(QemuGraphicConsole *c)
839a4826
WJ
1385{
1386 QemuConsole *con;
839a4826
WJ
1387
1388 QTAILQ_FOREACH(con, &consoles, next) {
2c0c4c1f
LE
1389 QemuGraphicConsole *candidate;
1390
4ce2f97c
LE
1391 if (!QEMU_IS_GRAPHIC_CONSOLE(con)) {
1392 continue;
1393 }
2c0c4c1f
LE
1394
1395 candidate = QEMU_GRAPHIC_CONSOLE(con);
1396 if (candidate->device != c->device) {
839a4826
WJ
1397 continue;
1398 }
1399
65d7ceb4 1400 if (candidate->head != c->head) {
839a4826
WJ
1401 return true;
1402 }
1403 }
1404 return false;
1405}
1406
779ce88f
GH
1407char *qemu_console_get_label(QemuConsole *con)
1408{
c105d60f 1409 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
58d58708
MAL
1410 QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(con);
1411 if (c->device) {
839a4826
WJ
1412 DeviceState *dev;
1413 bool multihead;
1414
58d58708 1415 dev = DEVICE(c->device);
2c0c4c1f 1416 multihead = qemu_graphic_console_is_multihead(c);
839a4826
WJ
1417 if (multihead) {
1418 return g_strdup_printf("%s.%d", dev->id ?
1419 dev->id :
58d58708
MAL
1420 object_get_typename(c->device),
1421 c->head);
839a4826
WJ
1422 } else {
1423 return g_strdup_printf("%s", dev->id ?
1424 dev->id :
58d58708 1425 object_get_typename(c->device));
839a4826 1426 }
779ce88f
GH
1427 }
1428 return g_strdup("VGA");
b2bb9cc4 1429 } else if (QEMU_IS_TEXT_CONSOLE(con)) {
f7ce755d
MAL
1430 const char *label = qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con));
1431 if (label) {
1432 return g_strdup(label);
779ce88f 1433 }
779ce88f 1434 }
b2bb9cc4
MAL
1435
1436 return g_strdup_printf("vc%d", con->index);
779ce88f
GH
1437}
1438
d4c85337
GH
1439int qemu_console_get_index(QemuConsole *con)
1440{
d4c85337
GH
1441 return con ? con->index : -1;
1442}
1443
5643706a
GH
1444uint32_t qemu_console_get_head(QemuConsole *con)
1445{
58d58708
MAL
1446 if (con == NULL) {
1447 return -1;
1448 }
1449 if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
1450 return QEMU_GRAPHIC_CONSOLE(con)->head;
1451 }
1452 return 0;
5643706a
GH
1453}
1454
d4c85337
GH
1455int qemu_console_get_width(QemuConsole *con, int fallback)
1456{
ebced091
MAL
1457 if (con == NULL) {
1458 return fallback;
1459 }
1460 switch (con->scanout.kind) {
1461 case SCANOUT_DMABUF:
6779a307 1462 return qemu_dmabuf_get_width(con->scanout.dmabuf);
ebced091
MAL
1463 case SCANOUT_TEXTURE:
1464 return con->scanout.texture.width;
1465 case SCANOUT_SURFACE:
1466 return surface_width(con->surface);
1467 default:
1468 return fallback;
1469 }
d4c85337
GH
1470}
1471
1472int qemu_console_get_height(QemuConsole *con, int fallback)
1473{
ebced091
MAL
1474 if (con == NULL) {
1475 return fallback;
1476 }
1477 switch (con->scanout.kind) {
1478 case SCANOUT_DMABUF:
6779a307 1479 return qemu_dmabuf_get_height(con->scanout.dmabuf);
ebced091
MAL
1480 case SCANOUT_TEXTURE:
1481 return con->scanout.texture.height;
1482 case SCANOUT_SURFACE:
1483 return surface_height(con->surface);
1484 default:
1485 return fallback;
1486 }
d4c85337
GH
1487}
1488
322dae4b 1489int qemu_invalidate_text_consoles(void)
bf1bed81 1490{
aea7947c 1491 QemuConsole *s;
cd6cd8fa 1492 int count = 0;
aea7947c 1493
cd6cd8fa 1494 QTAILQ_FOREACH(s, &consoles, next) {
aea7947c
GH
1495 if (qemu_console_is_graphic(s) ||
1496 !qemu_console_is_visible(s)) {
1497 continue;
1498 }
1499 count++;
1500 graphic_hw_invalidate(s);
1501 }
1502
322dae4b
MAL
1503 return count;
1504}
1505
c78f7137 1506void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 1507{
88738ea4 1508 DisplaySurface *surface = qemu_console_surface(s);
321f048d 1509
c105d60f 1510 assert(QEMU_IS_GRAPHIC_CONSOLE(s));
cd958edb 1511
88738ea4 1512 if ((s->scanout.kind != SCANOUT_SURFACE ||
95b08fee 1513 (surface && !is_buffer_shared(surface) && !is_placeholder(surface))) &&
88738ea4 1514 qemu_console_get_width(s, -1) == width &&
cb8962c1 1515 qemu_console_get_height(s, -1) == height) {
cd958edb
MAL
1516 return;
1517 }
1518
321f048d
GH
1519 surface = qemu_create_displaysurface(width, height);
1520 dpy_gfx_replace_surface(s, surface);
c60e08d9 1521}
38334f76 1522
c78f7137
GH
1523DisplaySurface *qemu_console_surface(QemuConsole *console)
1524{
ebced091
MAL
1525 switch (console->scanout.kind) {
1526 case SCANOUT_SURFACE:
1527 return console->surface;
1528 default:
1529 return NULL;
1530 }
c78f7137
GH
1531}
1532
0da2ea1b 1533PixelFormat qemu_default_pixelformat(int bpp)
1534{
56bd9ea1
GH
1535 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
1536 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
1537 return pf;
1538}
01f45d98 1539
db71589f
GH
1540static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
1541
1542void qemu_display_register(QemuDisplay *ui)
1543{
1544 assert(ui->type < DISPLAY_TYPE__MAX);
1545 dpys[ui->type] = ui;
1546}
1547
898f9d41
GH
1548bool qemu_display_find_default(DisplayOptions *opts)
1549{
1550 static DisplayType prio[] = {
66c2207f 1551#if defined(CONFIG_GTK)
898f9d41 1552 DISPLAY_TYPE_GTK,
66c2207f
TH
1553#endif
1554#if defined(CONFIG_SDL)
898f9d41 1555 DISPLAY_TYPE_SDL,
66c2207f
TH
1556#endif
1557#if defined(CONFIG_COCOA)
898f9d41 1558 DISPLAY_TYPE_COCOA
66c2207f 1559#endif
898f9d41
GH
1560 };
1561 int i;
1562
66c2207f 1563 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
61b4d9a2 1564 if (dpys[prio[i]] == NULL) {
c551fb0b
CF
1565 Error *local_err = NULL;
1566 int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
1567 if (rv < 0) {
1568 error_report_err(local_err);
1569 }
61b4d9a2 1570 }
898f9d41
GH
1571 if (dpys[prio[i]] == NULL) {
1572 continue;
1573 }
1574 opts->type = prio[i];
1575 return true;
1576 }
1577 return false;
1578}
1579
db71589f
GH
1580void qemu_display_early_init(DisplayOptions *opts)
1581{
1582 assert(opts->type < DISPLAY_TYPE__MAX);
1583 if (opts->type == DISPLAY_TYPE_NONE) {
1584 return;
1585 }
61b4d9a2 1586 if (dpys[opts->type] == NULL) {
c551fb0b
CF
1587 Error *local_err = NULL;
1588 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
1589 if (rv < 0) {
1590 error_report_err(local_err);
1591 }
61b4d9a2 1592 }
db71589f
GH
1593 if (dpys[opts->type] == NULL) {
1594 error_report("Display '%s' is not available.",
c809d1d2 1595 DisplayType_str(opts->type));
db71589f
GH
1596 exit(1);
1597 }
1598 if (dpys[opts->type]->early_init) {
1599 dpys[opts->type]->early_init(opts);
1600 }
1601}
1602
1603void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
1604{
1605 assert(opts->type < DISPLAY_TYPE__MAX);
1606 if (opts->type == DISPLAY_TYPE_NONE) {
1607 return;
1608 }
1609 assert(dpys[opts->type] != NULL);
1610 dpys[opts->type]->init(ds, opts);
1611}
1612
1bec1cc0
MAL
1613const char *qemu_display_get_vc(DisplayOptions *opts)
1614{
600179c3 1615#ifdef CONFIG_PIXMAN
0e882307
MAL
1616 const char *vc = "vc:80Cx24C";
1617#else
1618 const char *vc = NULL;
600179c3 1619#endif
0e882307
MAL
1620
1621 assert(opts->type < DISPLAY_TYPE__MAX);
1622 if (dpys[opts->type] && dpys[opts->type]->vc) {
1623 vc = dpys[opts->type]->vc;
1bec1cc0 1624 }
0e882307 1625 return vc;
1bec1cc0
MAL
1626}
1627
c388f408
TH
1628void qemu_display_help(void)
1629{
1630 int idx;
1631
1632 printf("Available display backend types:\n");
a1e8853e 1633 printf("none\n");
c388f408
TH
1634 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
1635 if (!dpys[idx]) {
c551fb0b
CF
1636 Error *local_err = NULL;
1637 int rv = ui_module_load(DisplayType_str(idx), &local_err);
1638 if (rv < 0) {
1639 error_report_err(local_err);
1640 }
c388f408
TH
1641 }
1642 if (dpys[idx]) {
1643 printf("%s\n", DisplayType_str(dpys[idx]->type));
1644 }
1645 }
1646}