]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
ui/gtk: Make sure the right EGL context is currently bound
[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"
68ba85ce 30#include "qemu/coroutine.h"
5feed38c 31#include "qemu/error-report.h"
0c9d0641 32#include "qemu/fifo8.h"
014b00cc 33#include "qemu/main-loop.h"
0b8fa32f 34#include "qemu/module.h"
922a01a0 35#include "qemu/option.h"
1de7afc9 36#include "qemu/timer.h"
014b00cc 37#include "chardev/char.h"
ac86048b 38#include "trace.h"
a77549b3 39#include "exec/memory.h"
c5f2bce5 40#include "io/channel-file.h"
db1015e9 41#include "qom/object.h"
9a0a119a
KS
42#ifdef CONFIG_PNG
43#include <png.h>
44#endif
e7f0ad58
FB
45
46#define DEFAULT_BACKSCROLL 512
bf1bed81 47#define CONSOLE_CURSOR_PERIOD 500
e7f0ad58 48
6d6f7c28
PB
49typedef struct TextAttributes {
50 uint8_t fgcol:4;
51 uint8_t bgcol:4;
52 uint8_t bold:1;
53 uint8_t uline:1;
54 uint8_t blink:1;
55 uint8_t invers:1;
56 uint8_t unvisible:1;
57} TextAttributes;
58
e7f0ad58
FB
59typedef struct TextCell {
60 uint8_t ch;
6d6f7c28 61 TextAttributes t_attrib;
e7f0ad58
FB
62} TextCell;
63
64#define MAX_ESC_PARAMS 3
65
66enum TTYState {
67 TTY_STATE_NORM,
68 TTY_STATE_ESC,
69 TTY_STATE_CSI,
70};
71
af3a9031
TS
72typedef enum {
73 GRAPHIC_CONSOLE,
c21bbcfa
AZ
74 TEXT_CONSOLE,
75 TEXT_CONSOLE_FIXED_SIZE
c227f099 76} console_type_t;
af3a9031 77
76ffb0b4 78struct QemuConsole {
95be0669
GH
79 Object parent;
80
f81bdefb 81 int index;
c227f099 82 console_type_t console_type;
e7f0ad58 83 DisplayState *ds;
321f048d 84 DisplaySurface *surface;
ebced091 85 DisplayScanout scanout;
284d1c6b 86 int dcls;
5e79d516 87 DisplayGLCtx *gl;
a4ddc314 88 int gl_block;
a9b1e471 89 QEMUTimer *gl_unblock_timer;
b3cb21b9 90 int window_id;
76ffb0b4 91
95219897 92 /* Graphic console state. */
aa2beaa1 93 Object *device;
5643706a 94 uint32_t head;
6f90f3d7 95 QemuUIInfo ui_info;
cf1ecc82 96 QEMUTimer *ui_timer;
385ac97f 97 QEMUCursor *cursor;
6effaa16 98 int cursor_x, cursor_y, cursor_on;
380cd056 99 const GraphicHwOps *hw_ops;
95219897 100 void *hw;
76ffb0b4
GH
101
102 /* Text console state */
e7f0ad58
FB
103 int width;
104 int height;
105 int total_height;
106 int backscroll_height;
e7f0ad58 107 int x, y;
adb47967 108 int x_saved, y_saved;
e7f0ad58
FB
109 int y_displayed;
110 int y_base;
6d6f7c28
PB
111 TextAttributes t_attrib_default; /* default text attributes */
112 TextAttributes t_attrib; /* currently active text attributes */
e7f0ad58 113 TextCell *cells;
4d3b6f6e 114 int text_x[2], text_y[2], cursor_invalidate;
4104833f 115 int echo;
e7f0ad58 116
14778c20
PB
117 int update_x0;
118 int update_y0;
119 int update_x1;
120 int update_y1;
121
e7f0ad58
FB
122 enum TTYState state;
123 int esc_params[MAX_ESC_PARAMS];
124 int nb_esc_params;
125
0ec7b3e7 126 Chardev *chr;
e15d7371 127 /* fifo for key pressed */
0c9d0641 128 Fifo8 out_fifo;
0d9b90ce 129 CoQueue dump_queue;
cd6cd8fa
GH
130
131 QTAILQ_ENTRY(QemuConsole) next;
e7f0ad58
FB
132};
133
27be5587 134struct DisplayState {
1246b259 135 QEMUTimer *gui_timer;
0f7b2864
GH
136 uint64_t last_update;
137 uint64_t update_interval;
138 bool refreshing;
27be5587
GH
139 bool have_gfx;
140 bool have_text;
141
142 QLIST_HEAD(, DisplayChangeListener) listeners;
143};
144
98b50080 145static DisplayState *display_state;
76ffb0b4 146static QemuConsole *active_console;
eae3eb3e 147static QTAILQ_HEAD(, QemuConsole) consoles =
cd6cd8fa 148 QTAILQ_HEAD_INITIALIZER(consoles);
aea7947c
GH
149static bool cursor_visible_phase;
150static QEMUTimer *cursor_timer;
e7f0ad58 151
0ec7b3e7 152static void text_console_do_init(Chardev *chr, DisplayState *ds);
0f7b2864 153static void dpy_refresh(DisplayState *s);
5209089f 154static DisplayState *get_alloc_displaystate(void);
aea7947c
GH
155static void text_console_update_cursor_timer(void);
156static void text_console_update_cursor(void *opaque);
ebced091 157static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
4b7b661d
MAL
158static bool console_compatible_with(QemuConsole *con,
159 DisplayChangeListener *dcl, Error **errp);
64840c66 160
98a9ad90
GH
161static void gui_update(void *opaque)
162{
0f7b2864
GH
163 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
164 uint64_t dcl_interval;
98a9ad90
GH
165 DisplayState *ds = opaque;
166 DisplayChangeListener *dcl;
167
0f7b2864 168 ds->refreshing = true;
98a9ad90 169 dpy_refresh(ds);
0f7b2864 170 ds->refreshing = false;
98a9ad90
GH
171
172 QLIST_FOREACH(dcl, &ds->listeners, next) {
0f7b2864
GH
173 dcl_interval = dcl->update_interval ?
174 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
175 if (interval > dcl_interval) {
176 interval = dcl_interval;
98a9ad90
GH
177 }
178 }
0f7b2864
GH
179 if (ds->update_interval != interval) {
180 ds->update_interval = interval;
181 trace_console_refresh(interval);
182 }
bc72ad67
AB
183 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
184 timer_mod(ds->gui_timer, ds->last_update + interval);
98a9ad90
GH
185}
186
187static void gui_setup_refresh(DisplayState *ds)
188{
189 DisplayChangeListener *dcl;
190 bool need_timer = false;
191 bool have_gfx = false;
192 bool have_text = false;
193
194 QLIST_FOREACH(dcl, &ds->listeners, next) {
195 if (dcl->ops->dpy_refresh != NULL) {
196 need_timer = true;
197 }
198 if (dcl->ops->dpy_gfx_update != NULL) {
199 have_gfx = true;
200 }
201 if (dcl->ops->dpy_text_update != NULL) {
202 have_text = true;
203 }
204 }
205
206 if (need_timer && ds->gui_timer == NULL) {
bc72ad67
AB
207 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
208 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
98a9ad90
GH
209 }
210 if (!need_timer && ds->gui_timer != NULL) {
bc72ad67 211 timer_free(ds->gui_timer);
98a9ad90
GH
212 ds->gui_timer = NULL;
213 }
214
215 ds->have_gfx = have_gfx;
216 ds->have_text = have_text;
217}
218
4d631621
MAL
219void graphic_hw_update_done(QemuConsole *con)
220{
6fc5183a 221 if (con) {
d6ee15ad 222 qemu_co_enter_all(&con->dump_queue, NULL);
6fc5183a 223 }
4d631621
MAL
224}
225
1dbfa005 226void graphic_hw_update(QemuConsole *con)
95219897 227{
4d631621 228 bool async = false;
1cd8b948 229 con = con ? con : active_console;
1dbfa005 230 if (!con) {
1cd8b948 231 return;
1dbfa005 232 }
1cd8b948 233 if (con->hw_ops->gfx_update) {
380cd056 234 con->hw_ops->gfx_update(con->hw);
4d631621
MAL
235 async = con->hw_ops->gfx_update_async;
236 }
237 if (!async) {
238 graphic_hw_update_done(con);
1dbfa005 239 }
95219897
PB
240}
241
a9b1e471
MAL
242static void graphic_hw_gl_unblock_timer(void *opaque)
243{
244 warn_report("console: no gl-unblock within one second");
245}
246
bba19b88
GH
247void graphic_hw_gl_block(QemuConsole *con, bool block)
248{
a9b1e471 249 uint64_t timeout;
f607867c
GH
250 assert(con != NULL);
251
a4ddc314
MAL
252 if (block) {
253 con->gl_block++;
254 } else {
255 con->gl_block--;
bba19b88 256 }
a4ddc314
MAL
257 assert(con->gl_block >= 0);
258 if (!con->hw_ops->gl_block) {
259 return;
260 }
261 if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
262 return;
263 }
264 con->hw_ops->gl_block(con->hw, block);
a9b1e471
MAL
265
266 if (block) {
267 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
268 timeout += 1000; /* one sec */
269 timer_mod(con->gl_unblock_timer, timeout);
270 } else {
271 timer_del(con->gl_unblock_timer);
272 }
bba19b88
GH
273}
274
b3cb21b9
ST
275int qemu_console_get_window_id(QemuConsole *con)
276{
277 return con->window_id;
278}
279
280void qemu_console_set_window_id(QemuConsole *con, int window_id)
281{
282 con->window_id = window_id;
283}
284
1dbfa005 285void graphic_hw_invalidate(QemuConsole *con)
95219897 286{
1dbfa005
GH
287 if (!con) {
288 con = active_console;
289 }
380cd056
GH
290 if (con && con->hw_ops->invalidate) {
291 con->hw_ops->invalidate(con->hw);
1dbfa005 292 }
95219897
PB
293}
294
9a0a119a
KS
295#ifdef CONFIG_PNG
296/**
297 * png_save: Take a screenshot as PNG
298 *
299 * Saves screendump as a PNG file
300 *
301 * Returns true for success or false for error.
302 *
303 * @fd: File descriptor for PNG file.
304 * @image: Image data in pixman format.
305 * @errp: Pointer to an error.
306 */
307static bool png_save(int fd, pixman_image_t *image, Error **errp)
308{
309 int width = pixman_image_get_width(image);
310 int height = pixman_image_get_height(image);
17b55372
VR
311 png_struct *png_ptr;
312 png_info *info_ptr;
9a0a119a 313 g_autoptr(pixman_image_t) linebuf =
cd22a0f5 314 qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
9a0a119a
KS
315 uint8_t *buf = (uint8_t *)pixman_image_get_data(linebuf);
316 FILE *f = fdopen(fd, "wb");
317 int y;
318 if (!f) {
319 error_setg_errno(errp, errno,
320 "Failed to create file from file descriptor");
321 return false;
322 }
323
324 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
325 NULL, NULL);
326 if (!png_ptr) {
327 error_setg(errp, "PNG creation failed. Unable to write struct");
328 fclose(f);
329 return false;
330 }
331
332 info_ptr = png_create_info_struct(png_ptr);
333
334 if (!info_ptr) {
335 error_setg(errp, "PNG creation failed. Unable to write info");
336 fclose(f);
337 png_destroy_write_struct(&png_ptr, &info_ptr);
338 return false;
339 }
340
341 png_init_io(png_ptr, f);
342
343 png_set_IHDR(png_ptr, info_ptr, width, height, 8,
cd22a0f5 344 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
9a0a119a
KS
345 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
346
347 png_write_info(png_ptr, info_ptr);
348
349 for (y = 0; y < height; ++y) {
350 qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
351 png_write_row(png_ptr, buf);
352 }
9a0a119a
KS
353
354 png_write_end(png_ptr, NULL);
355
356 png_destroy_write_struct(&png_ptr, &info_ptr);
357
358 if (fclose(f) != 0) {
359 error_setg_errno(errp, errno,
360 "PNG creation failed. Unable to close file");
361 return false;
362 }
363
364 return true;
365}
366
367#else /* no png support */
368
369static bool png_save(int fd, pixman_image_t *image, Error **errp)
370{
371 error_setg(errp, "Enable PNG support with libpng for screendump");
372 return false;
373}
374
375#endif /* CONFIG_PNG */
376
d00ec2fe 377static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
95219897 378{
d00ec2fe
MAL
379 int width = pixman_image_get_width(image);
380 int height = pixman_image_get_height(image);
c5f2bce5
MAL
381 g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
382 g_autofree char *header = NULL;
383 g_autoptr(pixman_image_t) linebuf = NULL;
2c62f08d 384 int y;
2c62f08d 385
d00ec2fe 386 trace_ppm_save(fd, image);
c5f2bce5
MAL
387
388 header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
389 if (qio_channel_write_all(QIO_CHANNEL(ioc),
390 header, strlen(header), errp) < 0) {
391 return false;
2c62f08d 392 }
c5f2bce5 393
2c62f08d
GH
394 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
395 for (y = 0; y < height; y++) {
d00ec2fe 396 qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
c5f2bce5
MAL
397 if (qio_channel_write_all(QIO_CHANNEL(ioc),
398 (char *)pixman_image_get_data(linebuf),
399 pixman_image_get_stride(linebuf), errp) < 0) {
400 return false;
401 }
f81bdefb
JK
402 }
403
c5f2bce5 404 return true;
2c62f08d
GH
405}
406
0d9b90ce
MAL
407static void graphic_hw_update_bh(void *con)
408{
409 graphic_hw_update(con);
410}
411
412/* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
413void coroutine_fn
3f41a3ad 414qmp_screendump(const char *filename, const char *device,
9a0a119a
KS
415 bool has_head, int64_t head,
416 bool has_format, ImageFormat format, Error **errp)
2c62f08d 417{
d00ec2fe 418 g_autoptr(pixman_image_t) image = NULL;
f771c544 419 QemuConsole *con;
2c62f08d 420 DisplaySurface *surface;
46e5841c 421 int fd;
2c62f08d 422
3f41a3ad 423 if (device) {
f771c544
TH
424 con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
425 errp);
426 if (!con) {
427 return;
428 }
429 } else {
430 if (has_head) {
431 error_setg(errp, "'head' must be specified together with 'device'");
432 return;
433 }
434 con = qemu_console_lookup_by_index(0);
435 if (!con) {
436 error_setg(errp, "There is no console to take a screendump from");
437 return;
438 }
33bcd98c 439 }
2c62f08d 440
0d9b90ce
MAL
441 if (qemu_co_queue_empty(&con->dump_queue)) {
442 /* Defer the update, it will restart the pending coroutines */
443 aio_bh_schedule_oneshot(qemu_get_aio_context(),
444 graphic_hw_update_bh, con);
445 }
446 qemu_co_queue_wait(&con->dump_queue, NULL);
447
448 /*
449 * All pending coroutines are woken up, while the BQL is held. No
450 * further graphic update are possible until it is released. Take
451 * an image ref before that.
452 */
2c62f08d 453 surface = qemu_console_surface(con);
08d9864f
MP
454 if (!surface) {
455 error_setg(errp, "no surface");
456 return;
457 }
d00ec2fe 458 image = pixman_image_ref(surface->image);
08d9864f 459
448058aa 460 fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
46e5841c
MAL
461 if (fd == -1) {
462 error_setg(errp, "failed to open file '%s': %s", filename,
463 strerror(errno));
464 return;
465 }
466
0d9b90ce
MAL
467 /*
468 * The image content could potentially be updated as the coroutine
469 * yields and releases the BQL. It could produce corrupted dump, but
470 * it should be otherwise safe.
471 */
9a0a119a
KS
472 if (has_format && format == IMAGE_FORMAT_PNG) {
473 /* PNG format specified for screendump */
474 if (!png_save(fd, image, errp)) {
475 qemu_unlink(filename);
476 }
477 } else {
478 /* PPM format specified/default for screendump */
479 if (!ppm_save(fd, image, errp)) {
480 qemu_unlink(filename);
481 }
46e5841c 482 }
95219897
PB
483}
484
1dbfa005 485void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
4d3b6f6e 486{
1dbfa005
GH
487 if (!con) {
488 con = active_console;
489 }
380cd056
GH
490 if (con && con->hw_ops->text_update) {
491 con->hw_ops->text_update(con->hw, chardata);
492 }
4d3b6f6e
AZ
493}
494
1562e531
GH
495static void vga_fill_rect(QemuConsole *con,
496 int posx, int posy, int width, int height,
e27bd65a 497 pixman_color_t color)
e7f0ad58 498{
1562e531 499 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
500 pixman_rectangle16_t rect = {
501 .x = posx, .y = posy, .width = width, .height = height
502 };
68db6dc5 503
68db6dc5 504 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
e27bd65a 505 &color, 1, &rect);
e7f0ad58
FB
506}
507
508/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
1562e531
GH
509static void vga_bitblt(QemuConsole *con,
510 int xs, int ys, int xd, int yd, int w, int h)
e7f0ad58 511{
1562e531 512 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
513
514 pixman_image_composite(PIXMAN_OP_SRC,
515 surface->image, NULL, surface->image,
516 xs, ys, 0, 0, xd, yd, w, h);
e7f0ad58
FB
517}
518
519/***********************************************************/
520/* basic char display */
521
522#define FONT_HEIGHT 16
523#define FONT_WIDTH 8
524
525#include "vgafont.h"
526
e27bd65a
GH
527#define QEMU_RGB(r, g, b) \
528 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
529
530static const pixman_color_t color_table_rgb[2][8] = {
6d6f7c28 531 { /* dark */
4083733d
OH
532 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
533 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
534 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
535 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
536 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
537 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
538 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
539 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
6d6f7c28
PB
540 },
541 { /* bright */
4083733d
OH
542 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
543 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
544 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
545 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
546 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
547 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
548 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
549 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
6d6f7c28 550 }
e7f0ad58
FB
551};
552
1562e531 553static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
6d6f7c28 554 TextAttributes *t_attrib)
e7f0ad58 555{
7d6ba01c 556 static pixman_image_t *glyphs[256];
1562e531 557 DisplaySurface *surface = qemu_console_surface(s);
e27bd65a 558 pixman_color_t fgcol, bgcol;
6d6f7c28
PB
559
560 if (t_attrib->invers) {
cf6f0548
GH
561 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
562 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
6d6f7c28 563 } else {
cf6f0548
GH
564 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
565 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
6d6f7c28 566 }
e7f0ad58 567
7d6ba01c
GH
568 if (!glyphs[ch]) {
569 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
e7f0ad58 570 }
7d6ba01c 571 qemu_pixman_glyph_render(glyphs[ch], surface->image,
e27bd65a 572 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
e7f0ad58
FB
573}
574
76ffb0b4 575static void text_console_resize(QemuConsole *s)
e7f0ad58
FB
576{
577 TextCell *cells, *c, *c1;
578 int w1, x, y, last_width;
579
ebced091
MAL
580 assert(s->scanout.kind == SCANOUT_SURFACE);
581
e7f0ad58 582 last_width = s->width;
36671fbd
GH
583 s->width = surface_width(s->surface) / FONT_WIDTH;
584 s->height = surface_height(s->surface) / FONT_HEIGHT;
e7f0ad58
FB
585
586 w1 = last_width;
587 if (s->width < w1)
588 w1 = s->width;
589
5b8541c6 590 cells = g_new(TextCell, s->width * s->total_height + 1);
e7f0ad58
FB
591 for(y = 0; y < s->total_height; y++) {
592 c = &cells[y * s->width];
593 if (w1 > 0) {
594 c1 = &s->cells[y * last_width];
595 for(x = 0; x < w1; x++) {
596 *c++ = *c1++;
597 }
598 }
599 for(x = w1; x < s->width; x++) {
600 c->ch = ' ';
6d6f7c28 601 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
602 c++;
603 }
604 }
7267c094 605 g_free(s->cells);
e7f0ad58
FB
606 s->cells = cells;
607}
608
76ffb0b4 609static inline void text_update_xy(QemuConsole *s, int x, int y)
4d3b6f6e
AZ
610{
611 s->text_x[0] = MIN(s->text_x[0], x);
612 s->text_x[1] = MAX(s->text_x[1], x);
613 s->text_y[0] = MIN(s->text_y[0], y);
614 s->text_y[1] = MAX(s->text_y[1], y);
615}
616
76ffb0b4 617static void invalidate_xy(QemuConsole *s, int x, int y)
14778c20 618{
b35e3ba0
GH
619 if (!qemu_console_is_visible(s)) {
620 return;
621 }
14778c20
PB
622 if (s->update_x0 > x * FONT_WIDTH)
623 s->update_x0 = x * FONT_WIDTH;
624 if (s->update_y0 > y * FONT_HEIGHT)
625 s->update_y0 = y * FONT_HEIGHT;
626 if (s->update_x1 < (x + 1) * FONT_WIDTH)
627 s->update_x1 = (x + 1) * FONT_WIDTH;
628 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
629 s->update_y1 = (y + 1) * FONT_HEIGHT;
630}
631
76ffb0b4 632static void update_xy(QemuConsole *s, int x, int y)
e7f0ad58
FB
633{
634 TextCell *c;
635 int y1, y2;
636
1562e531
GH
637 if (s->ds->have_text) {
638 text_update_xy(s, x, y);
639 }
4d3b6f6e 640
b35e3ba0
GH
641 y1 = (s->y_base + y) % s->total_height;
642 y2 = y1 - s->y_displayed;
643 if (y2 < 0) {
644 y2 += s->total_height;
645 }
646 if (y2 < s->height) {
5b8541c6
GH
647 if (x >= s->width) {
648 x = s->width - 1;
649 }
b35e3ba0
GH
650 c = &s->cells[y1 * s->width + x];
651 vga_putcharxy(s, x, y2, c->ch,
652 &(c->t_attrib));
653 invalidate_xy(s, x, y2);
e7f0ad58
FB
654 }
655}
656
76ffb0b4 657static void console_show_cursor(QemuConsole *s, int show)
e7f0ad58
FB
658{
659 TextCell *c;
660 int y, y1;
1562e531 661 int x = s->x;
e7f0ad58 662
1562e531
GH
663 if (s->ds->have_text) {
664 s->cursor_invalidate = 1;
665 }
4d3b6f6e 666
b35e3ba0
GH
667 if (x >= s->width) {
668 x = s->width - 1;
669 }
670 y1 = (s->y_base + s->y) % s->total_height;
671 y = y1 - s->y_displayed;
672 if (y < 0) {
673 y += s->total_height;
674 }
675 if (y < s->height) {
676 c = &s->cells[y1 * s->width + x];
aea7947c 677 if (show && cursor_visible_phase) {
b35e3ba0
GH
678 TextAttributes t_attrib = s->t_attrib_default;
679 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
680 vga_putcharxy(s, x, y, c->ch, &t_attrib);
681 } else {
682 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
e7f0ad58 683 }
b35e3ba0 684 invalidate_xy(s, x, y);
e7f0ad58
FB
685 }
686}
687
76ffb0b4 688static void console_refresh(QemuConsole *s)
e7f0ad58 689{
1562e531 690 DisplaySurface *surface = qemu_console_surface(s);
e7f0ad58
FB
691 TextCell *c;
692 int x, y, y1;
693
a93a4a22 694 if (s->ds->have_text) {
4d3b6f6e
AZ
695 s->text_x[0] = 0;
696 s->text_y[0] = 0;
697 s->text_x[1] = s->width - 1;
698 s->text_y[1] = s->height - 1;
699 s->cursor_invalidate = 1;
4d3b6f6e 700 }
e7f0ad58 701
b35e3ba0 702 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
4083733d 703 color_table_rgb[0][QEMU_COLOR_BLACK]);
b35e3ba0
GH
704 y1 = s->y_displayed;
705 for (y = 0; y < s->height; y++) {
706 c = s->cells + y1 * s->width;
707 for (x = 0; x < s->width; x++) {
708 vga_putcharxy(s, x, y, c->ch,
709 &(c->t_attrib));
710 c++;
711 }
712 if (++y1 == s->total_height) {
713 y1 = 0;
e7f0ad58 714 }
e7f0ad58 715 }
b35e3ba0
GH
716 console_show_cursor(s, 1);
717 dpy_gfx_update(s, 0, 0,
718 surface_width(surface), surface_height(surface));
e7f0ad58
FB
719}
720
81c0d5a6 721static void console_scroll(QemuConsole *s, int ydelta)
e7f0ad58 722{
e7f0ad58 723 int i, y1;
3b46e624 724
e7f0ad58
FB
725 if (ydelta > 0) {
726 for(i = 0; i < ydelta; i++) {
727 if (s->y_displayed == s->y_base)
728 break;
729 if (++s->y_displayed == s->total_height)
730 s->y_displayed = 0;
731 }
732 } else {
733 ydelta = -ydelta;
734 i = s->backscroll_height;
735 if (i > s->total_height - s->height)
736 i = s->total_height - s->height;
737 y1 = s->y_base - i;
738 if (y1 < 0)
739 y1 += s->total_height;
740 for(i = 0; i < ydelta; i++) {
741 if (s->y_displayed == y1)
742 break;
743 if (--s->y_displayed < 0)
744 s->y_displayed = s->total_height - 1;
745 }
746 }
747 console_refresh(s);
748}
749
76ffb0b4 750static void console_put_lf(QemuConsole *s)
e7f0ad58
FB
751{
752 TextCell *c;
753 int x, y1;
754
e7f0ad58
FB
755 s->y++;
756 if (s->y >= s->height) {
757 s->y = s->height - 1;
6d6f7c28 758
e7f0ad58
FB
759 if (s->y_displayed == s->y_base) {
760 if (++s->y_displayed == s->total_height)
761 s->y_displayed = 0;
762 }
763 if (++s->y_base == s->total_height)
764 s->y_base = 0;
765 if (s->backscroll_height < s->total_height)
766 s->backscroll_height++;
767 y1 = (s->y_base + s->height - 1) % s->total_height;
768 c = &s->cells[y1 * s->width];
769 for(x = 0; x < s->width; x++) {
770 c->ch = ' ';
6d6f7c28 771 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
772 c++;
773 }
b35e3ba0 774 if (s->y_displayed == s->y_base) {
1562e531 775 if (s->ds->have_text) {
4d3b6f6e
AZ
776 s->text_x[0] = 0;
777 s->text_y[0] = 0;
778 s->text_x[1] = s->width - 1;
779 s->text_y[1] = s->height - 1;
4d3b6f6e
AZ
780 }
781
b35e3ba0
GH
782 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
783 s->width * FONT_WIDTH,
784 (s->height - 1) * FONT_HEIGHT);
785 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
786 s->width * FONT_WIDTH, FONT_HEIGHT,
787 color_table_rgb[0][s->t_attrib_default.bgcol]);
788 s->update_x0 = 0;
789 s->update_y0 = 0;
790 s->update_x1 = s->width * FONT_WIDTH;
791 s->update_y1 = s->height * FONT_HEIGHT;
e7f0ad58
FB
792 }
793 }
794}
795
6d6f7c28
PB
796/* Set console attributes depending on the current escape codes.
797 * NOTE: I know this code is not very efficient (checking every color for it
798 * self) but it is more readable and better maintainable.
799 */
76ffb0b4 800static void console_handle_escape(QemuConsole *s)
6d6f7c28
PB
801{
802 int i;
803
6d6f7c28
PB
804 for (i=0; i<s->nb_esc_params; i++) {
805 switch (s->esc_params[i]) {
806 case 0: /* reset all console attributes to default */
807 s->t_attrib = s->t_attrib_default;
808 break;
809 case 1:
810 s->t_attrib.bold = 1;
811 break;
812 case 4:
813 s->t_attrib.uline = 1;
814 break;
815 case 5:
816 s->t_attrib.blink = 1;
817 break;
818 case 7:
819 s->t_attrib.invers = 1;
820 break;
821 case 8:
822 s->t_attrib.unvisible = 1;
823 break;
824 case 22:
825 s->t_attrib.bold = 0;
826 break;
827 case 24:
828 s->t_attrib.uline = 0;
829 break;
830 case 25:
831 s->t_attrib.blink = 0;
832 break;
833 case 27:
834 s->t_attrib.invers = 0;
835 break;
836 case 28:
837 s->t_attrib.unvisible = 0;
838 break;
839 /* set foreground color */
840 case 30:
4083733d 841 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
842 break;
843 case 31:
4083733d 844 s->t_attrib.fgcol = QEMU_COLOR_RED;
6d6f7c28
PB
845 break;
846 case 32:
4083733d 847 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
6d6f7c28
PB
848 break;
849 case 33:
4083733d 850 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
6d6f7c28
PB
851 break;
852 case 34:
4083733d 853 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
6d6f7c28
PB
854 break;
855 case 35:
4083733d 856 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
6d6f7c28
PB
857 break;
858 case 36:
4083733d 859 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
6d6f7c28
PB
860 break;
861 case 37:
4083733d 862 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
6d6f7c28
PB
863 break;
864 /* set background color */
865 case 40:
4083733d 866 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
867 break;
868 case 41:
4083733d 869 s->t_attrib.bgcol = QEMU_COLOR_RED;
6d6f7c28
PB
870 break;
871 case 42:
4083733d 872 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
6d6f7c28
PB
873 break;
874 case 43:
4083733d 875 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
6d6f7c28
PB
876 break;
877 case 44:
4083733d 878 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
6d6f7c28
PB
879 break;
880 case 45:
4083733d 881 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
6d6f7c28
PB
882 break;
883 case 46:
4083733d 884 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
6d6f7c28
PB
885 break;
886 case 47:
4083733d 887 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
6d6f7c28
PB
888 break;
889 }
890 }
891}
892
76ffb0b4 893static void console_clear_xy(QemuConsole *s, int x, int y)
adb47967
TS
894{
895 int y1 = (s->y_base + y) % s->total_height;
5b8541c6
GH
896 if (x >= s->width) {
897 x = s->width - 1;
898 }
adb47967
TS
899 TextCell *c = &s->cells[y1 * s->width + x];
900 c->ch = ' ';
901 c->t_attrib = s->t_attrib_default;
adb47967
TS
902 update_xy(s, x, y);
903}
904
58aa7d8e
RK
905static void console_put_one(QemuConsole *s, int ch)
906{
907 TextCell *c;
908 int y1;
909 if (s->x >= s->width) {
910 /* line wrap */
911 s->x = 0;
912 console_put_lf(s);
913 }
914 y1 = (s->y_base + s->y) % s->total_height;
915 c = &s->cells[y1 * s->width + s->x];
916 c->ch = ch;
917 c->t_attrib = s->t_attrib;
918 update_xy(s, s->x, s->y);
919 s->x++;
920}
921
922static void console_respond_str(QemuConsole *s, const char *buf)
923{
924 while (*buf) {
925 console_put_one(s, *buf);
926 buf++;
927 }
928}
929
3eea5498 930/* set cursor, checking bounds */
76ffb0b4 931static void set_cursor(QemuConsole *s, int x, int y)
3eea5498
IC
932{
933 if (x < 0) {
934 x = 0;
935 }
936 if (y < 0) {
937 y = 0;
938 }
939 if (y >= s->height) {
940 y = s->height - 1;
941 }
942 if (x >= s->width) {
943 x = s->width - 1;
944 }
945
946 s->x = x;
947 s->y = y;
948}
949
76ffb0b4 950static void console_putchar(QemuConsole *s, int ch)
e7f0ad58 951{
58aa7d8e 952 int i;
adb47967 953 int x, y;
58aa7d8e 954 char response[40];
e7f0ad58
FB
955
956 switch(s->state) {
957 case TTY_STATE_NORM:
958 switch(ch) {
6d6f7c28 959 case '\r': /* carriage return */
e7f0ad58
FB
960 s->x = 0;
961 break;
6d6f7c28 962 case '\n': /* newline */
e7f0ad58
FB
963 console_put_lf(s);
964 break;
6d6f7c28 965 case '\b': /* backspace */
5fafdf24 966 if (s->x > 0)
e15d7371 967 s->x--;
6d6f7c28
PB
968 break;
969 case '\t': /* tabspace */
970 if (s->x + (8 - (s->x % 8)) > s->width) {
bd468840 971 s->x = 0;
6d6f7c28
PB
972 console_put_lf(s);
973 } else {
974 s->x = s->x + (8 - (s->x % 8));
975 }
976 break;
977 case '\a': /* alert aka. bell */
978 /* TODO: has to be implemented */
979 break;
adb47967
TS
980 case 14:
981 /* SI (shift in), character set 0 (ignored) */
982 break;
983 case 15:
984 /* SO (shift out), character set 1 (ignored) */
985 break;
6d6f7c28 986 case 27: /* esc (introducing an escape sequence) */
e7f0ad58
FB
987 s->state = TTY_STATE_ESC;
988 break;
989 default:
58aa7d8e 990 console_put_one(s, ch);
e7f0ad58
FB
991 break;
992 }
993 break;
6d6f7c28 994 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
e7f0ad58
FB
995 if (ch == '[') {
996 for(i=0;i<MAX_ESC_PARAMS;i++)
997 s->esc_params[i] = 0;
998 s->nb_esc_params = 0;
999 s->state = TTY_STATE_CSI;
1000 } else {
1001 s->state = TTY_STATE_NORM;
1002 }
1003 break;
6d6f7c28 1004 case TTY_STATE_CSI: /* handle escape sequence parameters */
e7f0ad58
FB
1005 if (ch >= '0' && ch <= '9') {
1006 if (s->nb_esc_params < MAX_ESC_PARAMS) {
c10600af
LE
1007 int *param = &s->esc_params[s->nb_esc_params];
1008 int digit = (ch - '0');
1009
1010 *param = (*param <= (INT_MAX - digit) / 10) ?
1011 *param * 10 + digit : INT_MAX;
e7f0ad58
FB
1012 }
1013 } else {
3eea5498
IC
1014 if (s->nb_esc_params < MAX_ESC_PARAMS)
1015 s->nb_esc_params++;
7c336f9f 1016 if (ch == ';' || ch == '?') {
e7f0ad58 1017 break;
7c336f9f 1018 }
5d28b0e9
SW
1019 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
1020 ch, s->nb_esc_params);
e7f0ad58
FB
1021 s->state = TTY_STATE_NORM;
1022 switch(ch) {
adb47967
TS
1023 case 'A':
1024 /* move cursor up */
1025 if (s->esc_params[0] == 0) {
1026 s->esc_params[0] = 1;
1027 }
3eea5498 1028 set_cursor(s, s->x, s->y - s->esc_params[0]);
adb47967
TS
1029 break;
1030 case 'B':
1031 /* move cursor down */
1032 if (s->esc_params[0] == 0) {
1033 s->esc_params[0] = 1;
1034 }
3eea5498 1035 set_cursor(s, s->x, s->y + s->esc_params[0]);
e7f0ad58
FB
1036 break;
1037 case 'C':
adb47967
TS
1038 /* move cursor right */
1039 if (s->esc_params[0] == 0) {
1040 s->esc_params[0] = 1;
1041 }
3eea5498 1042 set_cursor(s, s->x + s->esc_params[0], s->y);
e7f0ad58 1043 break;
adb47967
TS
1044 case 'D':
1045 /* move cursor left */
1046 if (s->esc_params[0] == 0) {
1047 s->esc_params[0] = 1;
1048 }
3eea5498 1049 set_cursor(s, s->x - s->esc_params[0], s->y);
adb47967
TS
1050 break;
1051 case 'G':
1052 /* move cursor to column */
3eea5498 1053 set_cursor(s, s->esc_params[0] - 1, s->y);
adb47967
TS
1054 break;
1055 case 'f':
1056 case 'H':
1057 /* move cursor to row, column */
3eea5498 1058 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
adb47967
TS
1059 break;
1060 case 'J':
1061 switch (s->esc_params[0]) {
1062 case 0:
1063 /* clear to end of screen */
1064 for (y = s->y; y < s->height; y++) {
1065 for (x = 0; x < s->width; x++) {
1066 if (y == s->y && x < s->x) {
1067 continue;
1068 }
1069 console_clear_xy(s, x, y);
1070 }
1071 }
1072 break;
1073 case 1:
1074 /* clear from beginning of screen */
1075 for (y = 0; y <= s->y; y++) {
1076 for (x = 0; x < s->width; x++) {
1077 if (y == s->y && x > s->x) {
1078 break;
1079 }
1080 console_clear_xy(s, x, y);
1081 }
1082 }
1083 break;
1084 case 2:
1085 /* clear entire screen */
1086 for (y = 0; y <= s->height; y++) {
1087 for (x = 0; x < s->width; x++) {
1088 console_clear_xy(s, x, y);
1089 }
1090 }
f94a950f 1091 break;
adb47967 1092 }
95d8f9f4 1093 break;
e7f0ad58 1094 case 'K':
adb47967
TS
1095 switch (s->esc_params[0]) {
1096 case 0:
f94a950f
MA
1097 /* clear to eol */
1098 for(x = s->x; x < s->width; x++) {
adb47967 1099 console_clear_xy(s, x, s->y);
f94a950f
MA
1100 }
1101 break;
adb47967
TS
1102 case 1:
1103 /* clear from beginning of line */
5b8541c6 1104 for (x = 0; x <= s->x && x < s->width; x++) {
adb47967
TS
1105 console_clear_xy(s, x, s->y);
1106 }
1107 break;
1108 case 2:
1109 /* clear entire line */
1110 for(x = 0; x < s->width; x++) {
1111 console_clear_xy(s, x, s->y);
1112 }
f94a950f
MA
1113 break;
1114 }
adb47967
TS
1115 break;
1116 case 'm':
f94a950f
MA
1117 console_handle_escape(s);
1118 break;
adb47967 1119 case 'n':
58aa7d8e
RK
1120 switch (s->esc_params[0]) {
1121 case 5:
1122 /* report console status (always succeed)*/
1123 console_respond_str(s, "\033[0n");
1124 break;
1125 case 6:
1126 /* report cursor position */
1127 sprintf(response, "\033[%d;%dR",
1128 (s->y_base + s->y) % s->total_height + 1,
1129 s->x + 1);
1130 console_respond_str(s, response);
1131 break;
1132 }
adb47967
TS
1133 break;
1134 case 's':
1135 /* save cursor position */
1136 s->x_saved = s->x;
1137 s->y_saved = s->y;
1138 break;
1139 case 'u':
1140 /* restore cursor position */
1141 s->x = s->x_saved;
1142 s->y = s->y_saved;
1143 break;
1144 default:
5d28b0e9 1145 trace_console_putchar_unhandled(ch);
adb47967
TS
1146 break;
1147 }
1148 break;
e7f0ad58
FB
1149 }
1150 }
1151}
1152
26b032b9 1153static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
c84ab0a5
MAL
1154 struct DisplaySurface *new_surface,
1155 bool update)
26b032b9
MAL
1156{
1157 if (dcl->ops->dpy_gfx_switch) {
1158 dcl->ops->dpy_gfx_switch(dcl, new_surface);
1159 }
c84ab0a5
MAL
1160
1161 if (update && dcl->ops->dpy_gfx_update) {
1162 dcl->ops->dpy_gfx_update(dcl, 0, 0,
1163 surface_width(new_surface),
1164 surface_height(new_surface));
1165 }
26b032b9
MAL
1166}
1167
589089fe
MAL
1168static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
1169{
1170 if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
1171 con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
1172 }
1173}
1174
1175static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
1176{
1177 if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
1178 con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
1179 }
1180}
1181
1182static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
1183 int x, int y, int w, int h)
1184{
1185 if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
1186 con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
1187 }
1188}
26b032b9 1189
ebced091 1190static void displaychangelistener_display_console(DisplayChangeListener *dcl,
4b7b661d
MAL
1191 QemuConsole *con,
1192 Error **errp)
ebced091
MAL
1193{
1194 static const char nodev[] =
1195 "This VM has no graphic display device.";
1196 static DisplaySurface *dummy;
1197
4b7b661d 1198 if (!con || !console_compatible_with(con, dcl, errp)) {
ebced091
MAL
1199 if (!dummy) {
1200 dummy = qemu_create_placeholder_surface(640, 480, nodev);
1201 }
589089fe
MAL
1202 if (con) {
1203 dpy_gfx_create_texture(con, dummy);
1204 }
c84ab0a5 1205 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
ebced091
MAL
1206 return;
1207 }
1208
e1c676a2
MAL
1209 dpy_gfx_create_texture(con, con->surface);
1210 displaychangelistener_gfx_switch(dcl, con->surface,
1211 con->scanout.kind == SCANOUT_SURFACE);
1212
ebced091
MAL
1213 if (con->scanout.kind == SCANOUT_DMABUF &&
1214 displaychangelistener_has_dmabuf(dcl)) {
1215 dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
1216 } else if (con->scanout.kind == SCANOUT_TEXTURE &&
1217 dcl->ops->dpy_gl_scanout_texture) {
1218 dcl->ops->dpy_gl_scanout_texture(dcl,
1219 con->scanout.texture.backing_id,
1220 con->scanout.texture.backing_y_0_top,
1221 con->scanout.texture.backing_width,
1222 con->scanout.texture.backing_height,
1223 con->scanout.texture.x,
1224 con->scanout.texture.y,
1225 con->scanout.texture.width,
bf41ab61
MAL
1226 con->scanout.texture.height,
1227 con->scanout.texture.d3d_tex2d);
ebced091 1228 }
ebced091
MAL
1229}
1230
e7f0ad58
FB
1231void console_select(unsigned int index)
1232{
284d1c6b 1233 DisplayChangeListener *dcl;
76ffb0b4 1234 QemuConsole *s;
6d6f7c28 1235
437fe106 1236 trace_console_select(index);
284d1c6b 1237 s = qemu_console_lookup_by_index(index);
e7f0ad58 1238 if (s) {
7d957bd8 1239 DisplayState *ds = s->ds;
bf1bed81 1240
e7f0ad58 1241 active_console = s;
a93a4a22 1242 if (ds->have_gfx) {
284d1c6b
GH
1243 QLIST_FOREACH(dcl, &ds->listeners, next) {
1244 if (dcl->con != NULL) {
1245 continue;
1246 }
4b7b661d 1247 displaychangelistener_display_console(dcl, s, NULL);
2e5567c9 1248 }
a93a4a22
GH
1249 }
1250 if (ds->have_text) {
c78f7137 1251 dpy_text_resize(s, s->width, s->height);
68f00996 1252 }
aea7947c 1253 text_console_update_cursor(NULL);
e7f0ad58
FB
1254 }
1255}
1256
db1015e9 1257struct VCChardev {
0ec7b3e7 1258 Chardev parent;
41ac54b2 1259 QemuConsole *console;
db1015e9
EH
1260};
1261typedef struct VCChardev VCChardev;
41ac54b2 1262
777357d7 1263#define TYPE_CHARDEV_VC "chardev-vc"
8110fa1d
EH
1264DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
1265 TYPE_CHARDEV_VC)
777357d7 1266
5bf5adae 1267static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
e7f0ad58 1268{
777357d7 1269 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 1270 QemuConsole *s = drv->console;
e7f0ad58
FB
1271 int i;
1272
b68e956a
MAL
1273 if (!s->ds) {
1274 return 0;
1275 }
1276
14778c20
PB
1277 s->update_x0 = s->width * FONT_WIDTH;
1278 s->update_y0 = s->height * FONT_HEIGHT;
1279 s->update_x1 = 0;
1280 s->update_y1 = 0;
e7f0ad58
FB
1281 console_show_cursor(s, 0);
1282 for(i = 0; i < len; i++) {
1283 console_putchar(s, buf[i]);
1284 }
1285 console_show_cursor(s, 1);
a93a4a22 1286 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
c78f7137 1287 dpy_gfx_update(s, s->update_x0, s->update_y0,
a93a4a22
GH
1288 s->update_x1 - s->update_x0,
1289 s->update_y1 - s->update_y0);
14778c20 1290 }
e7f0ad58
FB
1291 return len;
1292}
1293
ec222519 1294static void kbd_send_chars(QemuConsole *s)
e15d7371 1295{
0c9d0641 1296 uint32_t len, avail;
3b46e624 1297
909cda12 1298 len = qemu_chr_be_can_write(s->chr);
0c9d0641 1299 avail = fifo8_num_used(&s->out_fifo);
ec222519 1300 while (len > 0 && avail > 0) {
0c9d0641
VR
1301 const uint8_t *buf;
1302 uint32_t size;
1303
ec222519 1304 buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
cf6280b9 1305 qemu_chr_be_write(s->chr, buf, size);
ec222519 1306 len = qemu_chr_be_can_write(s->chr);
0c9d0641 1307 avail -= size;
e15d7371 1308 }
e15d7371
FB
1309}
1310
e7f0ad58 1311/* called when an ascii key is pressed */
3f9a6e85 1312void kbd_put_keysym_console(QemuConsole *s, int keysym)
e7f0ad58 1313{
e7f0ad58
FB
1314 uint8_t buf[16], *q;
1315 int c;
0c9d0641 1316 uint32_t num_free;
e7f0ad58 1317
af3a9031 1318 if (!s || (s->console_type == GRAPHIC_CONSOLE))
e7f0ad58
FB
1319 return;
1320
1321 switch(keysym) {
1322 case QEMU_KEY_CTRL_UP:
81c0d5a6 1323 console_scroll(s, -1);
e7f0ad58
FB
1324 break;
1325 case QEMU_KEY_CTRL_DOWN:
81c0d5a6 1326 console_scroll(s, 1);
e7f0ad58
FB
1327 break;
1328 case QEMU_KEY_CTRL_PAGEUP:
81c0d5a6 1329 console_scroll(s, -10);
e7f0ad58
FB
1330 break;
1331 case QEMU_KEY_CTRL_PAGEDOWN:
81c0d5a6 1332 console_scroll(s, 10);
e7f0ad58
FB
1333 break;
1334 default:
e15d7371
FB
1335 /* convert the QEMU keysym to VT100 key string */
1336 q = buf;
1337 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1338 *q++ = '\033';
1339 *q++ = '[';
1340 c = keysym - 0xe100;
1341 if (c >= 10)
1342 *q++ = '0' + (c / 10);
1343 *q++ = '0' + (c % 10);
1344 *q++ = '~';
1345 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1346 *q++ = '\033';
1347 *q++ = '[';
1348 *q++ = keysym & 0xff;
4104833f 1349 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
5bf5adae 1350 vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
4104833f 1351 *q++ = '\n';
e15d7371 1352 } else {
4104833f
PB
1353 *q++ = keysym;
1354 }
1355 if (s->echo) {
5bf5adae 1356 vc_chr_write(s->chr, buf, q - buf);
e15d7371 1357 }
014b00cc
VR
1358 num_free = fifo8_num_free(&s->out_fifo);
1359 fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
1360 kbd_send_chars(s);
e7f0ad58
FB
1361 break;
1362 }
1363}
1364
7fb1cf16 1365static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
50ef4679
GH
1366 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1367 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1368 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1369 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1370 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1371 [Q_KEY_CODE_END] = QEMU_KEY_END,
1372 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1373 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1374 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
df6322a8 1375 [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
344aa283 1376 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
50ef4679
GH
1377};
1378
da024b1e
GH
1379static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
1380 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
1381 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
1382 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
1383 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
1384 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
1385 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
1386 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
1387 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
1388};
1389
1390bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
50ef4679
GH
1391{
1392 int keysym;
1393
da024b1e 1394 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
50ef4679
GH
1395 if (keysym == 0) {
1396 return false;
1397 }
1398 kbd_put_keysym_console(s, keysym);
1399 return true;
1400}
1401
bdef9724
GH
1402void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1403{
1404 int i;
1405
1406 for (i = 0; i < len && str[i]; i++) {
1407 kbd_put_keysym_console(s, str[i]);
1408 }
1409}
1410
3f9a6e85
GH
1411void kbd_put_keysym(int keysym)
1412{
1413 kbd_put_keysym_console(active_console, keysym);
1414}
1415
4d3b6f6e
AZ
1416static void text_console_invalidate(void *opaque)
1417{
76ffb0b4 1418 QemuConsole *s = (QemuConsole *) opaque;
1562e531
GH
1419
1420 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
68f00996
AL
1421 text_console_resize(s);
1422 }
4d3b6f6e
AZ
1423 console_refresh(s);
1424}
1425
c227f099 1426static void text_console_update(void *opaque, console_ch_t *chardata)
4d3b6f6e 1427{
76ffb0b4 1428 QemuConsole *s = (QemuConsole *) opaque;
4d3b6f6e
AZ
1429 int i, j, src;
1430
1431 if (s->text_x[0] <= s->text_x[1]) {
1432 src = (s->y_base + s->text_y[0]) * s->width;
1433 chardata += s->text_y[0] * s->width;
1434 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
4083733d
OH
1435 for (j = 0; j < s->width; j++, src++) {
1436 console_write_ch(chardata ++,
1437 ATTR2CHTYPE(s->cells[src].ch,
1438 s->cells[src].t_attrib.fgcol,
1439 s->cells[src].t_attrib.bgcol,
1440 s->cells[src].t_attrib.bold));
1441 }
c78f7137 1442 dpy_text_update(s, s->text_x[0], s->text_y[0],
a93a4a22 1443 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
4d3b6f6e
AZ
1444 s->text_x[0] = s->width;
1445 s->text_y[0] = s->height;
1446 s->text_x[1] = 0;
1447 s->text_y[1] = 0;
1448 }
1449 if (s->cursor_invalidate) {
c78f7137 1450 dpy_text_cursor(s, s->x, s->y);
4d3b6f6e
AZ
1451 s->cursor_invalidate = 0;
1452 }
1453}
1454
afff2b15
KB
1455static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1456 uint32_t head)
e7f0ad58 1457{
95be0669 1458 Object *obj;
76ffb0b4 1459 QemuConsole *s;
95219897 1460 int i;
e7f0ad58 1461
95be0669
GH
1462 obj = object_new(TYPE_QEMU_CONSOLE);
1463 s = QEMU_CONSOLE(obj);
0d9b90ce 1464 qemu_co_queue_init(&s->dump_queue);
afff2b15 1465 s->head = head;
aa2beaa1 1466 object_property_add_link(obj, "device", TYPE_DEVICE,
9561fda8 1467 (Object **)&s->device,
39f72ef9 1468 object_property_allow_set_link,
d2623129 1469 OBJ_PROP_LINK_STRONG);
836e1b38 1470 object_property_add_uint32_ptr(obj, "head", &s->head,
d2623129 1471 OBJ_PROP_FLAG_READ);
aa2beaa1 1472
af3a9031
TS
1473 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1474 (console_type == GRAPHIC_CONSOLE))) {
e7f0ad58 1475 active_console = s;
af3a9031 1476 }
e7f0ad58 1477 s->ds = ds;
af3a9031 1478 s->console_type = console_type;
41d004d8 1479 s->window_id = -1;
a1d2db08 1480
cd6cd8fa
GH
1481 if (QTAILQ_EMPTY(&consoles)) {
1482 s->index = 0;
1483 QTAILQ_INSERT_TAIL(&consoles, s, next);
2f181fbd 1484 } else if (console_type != GRAPHIC_CONSOLE || phase_check(PHASE_MACHINE_READY)) {
eae3eb3e 1485 QemuConsole *last = QTAILQ_LAST(&consoles);
cd6cd8fa
GH
1486 s->index = last->index + 1;
1487 QTAILQ_INSERT_TAIL(&consoles, s, next);
95219897 1488 } else {
9588d67e
GH
1489 /*
1490 * HACK: Put graphical consoles before text consoles.
1491 *
1492 * Only do that for coldplugged devices. After initial device
1493 * initialization we will not renumber the consoles any more.
1494 */
cd6cd8fa
GH
1495 QemuConsole *c = QTAILQ_FIRST(&consoles);
1496
1497 while (QTAILQ_NEXT(c, next) != NULL &&
1498 c->console_type == GRAPHIC_CONSOLE) {
1499 c = QTAILQ_NEXT(c, next);
1500 }
1501 if (c->console_type == GRAPHIC_CONSOLE) {
1502 /* have no text consoles */
1503 s->index = c->index + 1;
1504 QTAILQ_INSERT_AFTER(&consoles, c, s, next);
1505 } else {
1506 s->index = c->index;
1507 QTAILQ_INSERT_BEFORE(c, s, next);
1508 /* renumber text consoles */
1509 for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
1510 c->index = i;
1511 }
95219897 1512 }
95219897
PB
1513 }
1514 return s;
1515}
1516
09b4c198
MAL
1517#ifdef WIN32
1518void qemu_displaysurface_win32_set_handle(DisplaySurface *surface,
1519 HANDLE h, uint32_t offset)
1520{
1521 assert(!surface->handle);
1522
1523 surface->handle = h;
1524 surface->handle_offset = offset;
1525}
1526
1527static void
1528win32_pixman_image_destroy(pixman_image_t *image, void *data)
1529{
1530 DisplaySurface *surface = data;
1531
1532 if (!surface->handle) {
1533 return;
1534 }
1535
1536 assert(surface->handle_offset == 0);
1537
1538 qemu_win32_map_free(
1539 pixman_image_get_data(surface->image),
1540 surface->handle,
1541 &error_warn
1542 );
1543}
1544#endif
1545
eb69442a 1546DisplaySurface *qemu_create_displaysurface(int width, int height)
ffe8b821 1547{
09b4c198
MAL
1548 DisplaySurface *surface;
1549 void *bits = NULL;
1550#ifdef WIN32
1551 HANDLE handle = NULL;
1552#endif
69c77777 1553
09b4c198
MAL
1554 trace_displaysurface_create(width, height);
1555
1556#ifdef WIN32
1557 bits = qemu_win32_map_alloc(width * height * 4, &handle, &error_abort);
1558#endif
1559
1560 surface = qemu_create_displaysurface_from(
1561 width, height,
1562 PIXMAN_x8r8g8b8,
1563 width * 4, bits
1564 );
30f1e661 1565 surface->flags = QEMU_ALLOCATED_FLAG;
98b50080 1566
09b4c198
MAL
1567#ifdef WIN32
1568 qemu_displaysurface_win32_set_handle(surface, handle, 0);
1569#endif
537a4391
GH
1570 return surface;
1571}
1572
30f1e661
GH
1573DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1574 pixman_format_code_t format,
1575 int linesize, uint8_t *data)
98b50080 1576{
69c77777 1577 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 1578
30f1e661
GH
1579 trace_displaysurface_create_from(surface, width, height, format);
1580 surface->format = format;
69c77777
GH
1581 surface->image = pixman_image_create_bits(surface->format,
1582 width, height,
1583 (void *)data, linesize);
1584 assert(surface->image != NULL);
09b4c198
MAL
1585#ifdef WIN32
1586 pixman_image_set_destroy_function(surface->image,
1587 win32_pixman_image_destroy, surface);
1588#endif
69c77777 1589
98b50080
PB
1590 return surface;
1591}
1592
ca58b45f
GH
1593DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1594{
1595 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1596
1597 trace_displaysurface_create_pixman(surface);
1598 surface->format = pixman_image_get_format(image);
1599 surface->image = pixman_image_ref(image);
1600
1601 return surface;
1602}
1603
b5a087b0
AO
1604DisplaySurface *qemu_create_placeholder_surface(int w, int h,
1605 const char *msg)
d3002b04 1606{
521a580d 1607 DisplaySurface *surface = qemu_create_displaysurface(w, h);
4083733d
OH
1608 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1609 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
d3002b04
GH
1610 pixman_image_t *glyph;
1611 int len, x, y, i;
1612
1613 len = strlen(msg);
521a580d
GH
1614 x = (w / FONT_WIDTH - len) / 2;
1615 y = (h / FONT_HEIGHT - 1) / 2;
d3002b04
GH
1616 for (i = 0; i < len; i++) {
1617 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1618 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1619 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1620 qemu_pixman_image_unref(glyph);
1621 }
b5a087b0 1622 surface->flags |= QEMU_PLACEHOLDER_FLAG;
d3002b04
GH
1623 return surface;
1624}
1625
da229ef3 1626void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 1627{
da229ef3 1628 if (surface == NULL) {
98b50080 1629 return;
187cd1d9 1630 }
da229ef3
GH
1631 trace_displaysurface_free(surface);
1632 qemu_pixman_image_unref(surface->image);
1633 g_free(surface);
98b50080
PB
1634}
1635
06020b95
GH
1636bool console_has_gl(QemuConsole *con)
1637{
1638 return con->gl != NULL;
1639}
1640
d0e137bc
MAL
1641static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
1642{
1643 if (dcl->ops->dpy_has_dmabuf) {
1644 return dcl->ops->dpy_has_dmabuf(dcl);
1645 }
1646
1647 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1648 return true;
1649 }
1650
1651 return false;
1652}
1653
4b7b661d
MAL
1654static bool console_compatible_with(QemuConsole *con,
1655 DisplayChangeListener *dcl, Error **errp)
5983fdf1 1656{
5983fdf1
MAL
1657 int flags;
1658
1659 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
1660
a62c4a17
MAL
1661 if (console_has_gl(con) &&
1662 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
398d1c91
MAL
1663 error_setg(errp, "Display %s is incompatible with the GL context",
1664 dcl->ops->dpy_name);
1665 return false;
1666 }
1667
5983fdf1
MAL
1668 if (flags & GRAPHIC_FLAGS_GL &&
1669 !console_has_gl(con)) {
1670 error_setg(errp, "The console requires a GL context.");
1671 return false;
1672
1673 }
1674
1675 if (flags & GRAPHIC_FLAGS_DMABUF &&
1676 !displaychangelistener_has_dmabuf(dcl)) {
1677 error_setg(errp, "The console requires display DMABUF support.");
1678 return false;
1679 }
1680
1681 return true;
1682}
1683
b6596785
BE
1684void console_handle_touch_event(QemuConsole *con,
1685 struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
1686 uint64_t num_slot,
1687 int width, int height,
1688 double x, double y,
1689 InputMultiTouchType type,
1690 Error **errp)
1691{
1692 struct touch_slot *slot;
1693 bool needs_sync = false;
1694 int update;
1695 int i;
1696
1697 if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
1698 error_setg(errp,
1699 "Unexpected touch slot number: % " PRId64" >= %d",
1700 num_slot, INPUT_EVENT_SLOTS_MAX);
1701 return;
1702 }
1703
1704 slot = &touch_slots[num_slot];
1705 slot->x = x;
1706 slot->y = y;
1707
1708 if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
1709 slot->tracking_id = num_slot;
1710 }
1711
1712 for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
1713 if (i == num_slot) {
1714 update = type;
1715 } else {
1716 update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
1717 }
1718
1719 slot = &touch_slots[i];
1720
1721 if (slot->tracking_id == -1) {
1722 continue;
1723 }
1724
1725 if (update == INPUT_MULTI_TOUCH_TYPE_END) {
1726 slot->tracking_id = -1;
1727 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
1728 needs_sync = true;
1729 } else {
1730 qemu_input_queue_mtt(con, update, i, slot->tracking_id);
1731 qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
1732 qemu_input_queue_mtt_abs(con,
1733 INPUT_AXIS_X, (int) slot->x,
1734 0, width,
1735 i, slot->tracking_id);
1736 qemu_input_queue_mtt_abs(con,
1737 INPUT_AXIS_Y, (int) slot->y,
1738 0, height,
1739 i, slot->tracking_id);
1740 needs_sync = true;
1741 }
1742 }
1743
1744 if (needs_sync) {
1745 qemu_input_event_sync();
1746 }
1747}
1748
5e79d516 1749void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
4f418149
MAL
1750{
1751 /* display has opengl support */
5e79d516
MAL
1752 assert(con);
1753 if (con->gl) {
1754 error_report("The console already has an OpenGL context.");
4f418149
MAL
1755 exit(1);
1756 }
5e79d516
MAL
1757 con->gl = gl;
1758}
1759
5209089f 1760void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 1761{
284d1c6b
GH
1762 QemuConsole *con;
1763
e0665c3b
MAL
1764 assert(!dcl->ds);
1765
7c20b4a3 1766 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
1767 dcl->ds = get_alloc_displaystate();
1768 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1769 gui_setup_refresh(dcl->ds);
284d1c6b
GH
1770 if (dcl->con) {
1771 dcl->con->dcls++;
1772 con = dcl->con;
1773 } else {
1774 con = active_console;
1775 }
4b7b661d 1776 displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
de00b60d
MAL
1777 if (con && con->cursor && dcl->ops->dpy_cursor_define) {
1778 dcl->ops->dpy_cursor_define(dcl, con->cursor);
1779 }
6effaa16
MAL
1780 if (con && dcl->ops->dpy_mouse_set) {
1781 dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
1782 }
aea7947c 1783 text_console_update_cursor(NULL);
7c20b4a3
GH
1784}
1785
0f7b2864
GH
1786void update_displaychangelistener(DisplayChangeListener *dcl,
1787 uint64_t interval)
1788{
1789 DisplayState *ds = dcl->ds;
1790
1791 dcl->update_interval = interval;
1792 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 1793 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
1794 }
1795}
1796
7c20b4a3
GH
1797void unregister_displaychangelistener(DisplayChangeListener *dcl)
1798{
1799 DisplayState *ds = dcl->ds;
1800 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
1801 if (dcl->con) {
1802 dcl->con->dcls--;
1803 }
7c20b4a3 1804 QLIST_REMOVE(dcl, next);
777c5f1e 1805 dcl->ds = NULL;
7c20b4a3
GH
1806 gui_setup_refresh(ds);
1807}
1808
cf1ecc82
GH
1809static void dpy_set_ui_info_timer(void *opaque)
1810{
1811 QemuConsole *con = opaque;
1812
1813 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1814}
1815
b7fb49f0
GH
1816bool dpy_ui_info_supported(QemuConsole *con)
1817{
5c4b107f
GH
1818 if (con == NULL) {
1819 con = active_console;
1820 }
1821
b7fb49f0
GH
1822 return con->hw_ops->ui_info != NULL;
1823}
1824
5eaf1e48
MAL
1825const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
1826{
5c4b107f
GH
1827 if (con == NULL) {
1828 con = active_console;
1829 }
5eaf1e48
MAL
1830
1831 return &con->ui_info;
1832}
1833
ca19ef52 1834int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
6f90f3d7 1835{
5c4b107f
GH
1836 if (con == NULL) {
1837 con = active_console;
1838 }
1185fde4 1839
b7fb49f0 1840 if (!dpy_ui_info_supported(con)) {
cf1ecc82 1841 return -1;
6f90f3d7 1842 }
1185fde4
GH
1843 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1844 /* nothing changed -- ignore */
1845 return 0;
1846 }
cf1ecc82
GH
1847
1848 /*
1849 * Typically we get a flood of these as the user resizes the window.
1850 * Wait until the dust has settled (one second without updates), then
1851 * go notify the guest.
1852 */
1185fde4 1853 con->ui_info = *info;
ca19ef52
MAL
1854 timer_mod(con->ui_timer,
1855 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
cf1ecc82 1856 return 0;
6f90f3d7
GH
1857}
1858
c78f7137 1859void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1860{
c78f7137 1861 DisplayState *s = con->ds;
284d1c6b 1862 DisplayChangeListener *dcl;
ebced091
MAL
1863 int width = qemu_console_get_width(con, x + w);
1864 int height = qemu_console_get_height(con, y + h);
7c20b4a3
GH
1865
1866 x = MAX(x, 0);
1867 y = MAX(y, 0);
1868 x = MIN(x, width);
1869 y = MIN(y, height);
1870 w = MIN(w, width - x);
1871 h = MIN(h, height - y);
1872
81c0d5a6 1873 if (!qemu_console_is_visible(con)) {
321f048d
GH
1874 return;
1875 }
589089fe 1876 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
7c20b4a3 1877 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1878 if (con != (dcl->con ? dcl->con : active_console)) {
1879 continue;
1880 }
7c20b4a3 1881 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1882 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1883 }
1884 }
1885}
1886
7cd0afe6
TZ
1887void dpy_gfx_update_full(QemuConsole *con)
1888{
ebced091
MAL
1889 int w = qemu_console_get_width(con, 0);
1890 int h = qemu_console_get_height(con, 0);
1891
1892 dpy_gfx_update(con, 0, 0, w, h);
7cd0afe6
TZ
1893}
1894
321f048d
GH
1895void dpy_gfx_replace_surface(QemuConsole *con,
1896 DisplaySurface *surface)
1897{
c821a58e 1898 static const char placeholder_msg[] = "Display output is not active.";
321f048d
GH
1899 DisplayState *s = con->ds;
1900 DisplaySurface *old_surface = con->surface;
284d1c6b 1901 DisplayChangeListener *dcl;
c821a58e
AO
1902 int width;
1903 int height;
1904
1905 if (!surface) {
1906 if (old_surface) {
1907 width = surface_width(old_surface);
1908 height = surface_height(old_surface);
1909 } else {
1910 width = 640;
1911 height = 480;
1912 }
1913
1914 surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
1915 }
321f048d 1916
c821a58e 1917 assert(old_surface != surface);
6905b934 1918
ebced091 1919 con->scanout.kind = SCANOUT_SURFACE;
321f048d 1920 con->surface = surface;
589089fe 1921 dpy_gfx_create_texture(con, surface);
284d1c6b
GH
1922 QLIST_FOREACH(dcl, &s->listeners, next) {
1923 if (con != (dcl->con ? dcl->con : active_console)) {
1924 continue;
1925 }
c84ab0a5 1926 displaychangelistener_gfx_switch(dcl, surface, FALSE);
321f048d 1927 }
589089fe 1928 dpy_gfx_destroy_texture(con, old_surface);
da229ef3 1929 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1930}
1931
49743df3
BH
1932bool dpy_gfx_check_format(QemuConsole *con,
1933 pixman_format_code_t format)
1934{
1935 DisplayChangeListener *dcl;
1936 DisplayState *s = con->ds;
1937
1938 QLIST_FOREACH(dcl, &s->listeners, next) {
1939 if (dcl->con && dcl->con != con) {
1940 /* dcl bound to another console -> skip */
1941 continue;
1942 }
1943 if (dcl->ops->dpy_gfx_check_format) {
1944 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1945 return false;
1946 }
1947 } else {
75ae7c46 1948 /* default is to allow native 32 bpp only */
49743df3
BH
1949 if (format != qemu_default_pixman_format(32, true)) {
1950 return false;
1951 }
1952 }
1953 }
1954 return true;
1955}
1956
6075137d 1957static void dpy_refresh(DisplayState *s)
7c20b4a3 1958{
284d1c6b
GH
1959 DisplayChangeListener *dcl;
1960
7c20b4a3
GH
1961 QLIST_FOREACH(dcl, &s->listeners, next) {
1962 if (dcl->ops->dpy_refresh) {
3f8f1313 1963 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1964 }
1965 }
1966}
1967
c78f7137 1968void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1969{
c78f7137 1970 DisplayState *s = con->ds;
284d1c6b 1971 DisplayChangeListener *dcl;
321f048d 1972
81c0d5a6 1973 if (!qemu_console_is_visible(con)) {
321f048d
GH
1974 return;
1975 }
7c20b4a3 1976 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1977 if (con != (dcl->con ? dcl->con : active_console)) {
1978 continue;
1979 }
7c20b4a3 1980 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1981 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1982 }
1983 }
1984}
1985
c78f7137 1986void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1987{
c78f7137 1988 DisplayState *s = con->ds;
284d1c6b 1989 DisplayChangeListener *dcl;
321f048d 1990
81c0d5a6 1991 if (!qemu_console_is_visible(con)) {
321f048d
GH
1992 return;
1993 }
7c20b4a3 1994 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1995 if (con != (dcl->con ? dcl->con : active_console)) {
1996 continue;
1997 }
7c20b4a3 1998 if (dcl->ops->dpy_text_update) {
bc2ed970 1999 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
2000 }
2001 }
2002}
2003
c78f7137 2004void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 2005{
c78f7137 2006 DisplayState *s = con->ds;
9425c004 2007 DisplayChangeListener *dcl;
321f048d 2008
81c0d5a6 2009 if (!qemu_console_is_visible(con)) {
321f048d
GH
2010 return;
2011 }
7c20b4a3 2012 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
2013 if (con != (dcl->con ? dcl->con : active_console)) {
2014 continue;
2015 }
7c20b4a3 2016 if (dcl->ops->dpy_text_resize) {
bc2ed970 2017 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
2018 }
2019 }
2020}
2021
c78f7137 2022void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 2023{
c78f7137 2024 DisplayState *s = con->ds;
284d1c6b 2025 DisplayChangeListener *dcl;
321f048d 2026
6effaa16
MAL
2027 con->cursor_x = x;
2028 con->cursor_y = y;
2029 con->cursor_on = on;
81c0d5a6 2030 if (!qemu_console_is_visible(con)) {
321f048d
GH
2031 return;
2032 }
7c20b4a3 2033 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
2034 if (con != (dcl->con ? dcl->con : active_console)) {
2035 continue;
2036 }
7c20b4a3 2037 if (dcl->ops->dpy_mouse_set) {
bc2ed970 2038 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
2039 }
2040 }
2041}
2042
c78f7137 2043void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 2044{
c78f7137 2045 DisplayState *s = con->ds;
284d1c6b 2046 DisplayChangeListener *dcl;
321f048d 2047
385ac97f
MAL
2048 cursor_unref(con->cursor);
2049 con->cursor = cursor_ref(cursor);
81c0d5a6 2050 if (!qemu_console_is_visible(con)) {
321f048d
GH
2051 return;
2052 }
7c20b4a3 2053 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
2054 if (con != (dcl->con ? dcl->con : active_console)) {
2055 continue;
2056 }
7c20b4a3 2057 if (dcl->ops->dpy_cursor_define) {
bc2ed970 2058 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
2059 }
2060 }
2061}
2062
c78f7137 2063bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 2064{
c78f7137 2065 DisplayState *s = con->ds;
284d1c6b
GH
2066 DisplayChangeListener *dcl;
2067
7c20b4a3
GH
2068 QLIST_FOREACH(dcl, &s->listeners, next) {
2069 if (dcl->ops->dpy_cursor_define) {
2070 return true;
2071 }
2072 }
2073 return false;
2074}
2075
06020b95
GH
2076QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
2077 struct QEMUGLParams *qparams)
2078{
2079 assert(con->gl);
2080 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
2081}
2082
2083void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
2084{
2085 assert(con->gl);
2086 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
2087}
2088
2089int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
2090{
2091 assert(con->gl);
2092 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
2093}
2094
eaa92c76
GH
2095void dpy_gl_scanout_disable(QemuConsole *con)
2096{
7cc712e9
MAL
2097 DisplayState *s = con->ds;
2098 DisplayChangeListener *dcl;
2099
ebced091
MAL
2100 if (con->scanout.kind != SCANOUT_SURFACE) {
2101 con->scanout.kind = SCANOUT_NONE;
2102 }
7cc712e9 2103 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2104 if (con != (dcl->con ? dcl->con : active_console)) {
2105 continue;
2106 }
a9fbce5e
MAL
2107 if (dcl->ops->dpy_gl_scanout_disable) {
2108 dcl->ops->dpy_gl_scanout_disable(dcl);
2109 }
7cc712e9 2110 }
eaa92c76
GH
2111}
2112
f4c36bda
GH
2113void dpy_gl_scanout_texture(QemuConsole *con,
2114 uint32_t backing_id,
2115 bool backing_y_0_top,
2116 uint32_t backing_width,
2117 uint32_t backing_height,
2118 uint32_t x, uint32_t y,
bf41ab61
MAL
2119 uint32_t width, uint32_t height,
2120 void *d3d_tex2d)
06020b95 2121{
7cc712e9
MAL
2122 DisplayState *s = con->ds;
2123 DisplayChangeListener *dcl;
2124
ebced091
MAL
2125 con->scanout.kind = SCANOUT_TEXTURE;
2126 con->scanout.texture = (ScanoutTexture) {
2127 backing_id, backing_y_0_top, backing_width, backing_height,
bf41ab61 2128 x, y, width, height, d3d_tex2d,
ebced091 2129 };
7cc712e9 2130 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2131 if (con != (dcl->con ? dcl->con : active_console)) {
2132 continue;
2133 }
a9fbce5e
MAL
2134 if (dcl->ops->dpy_gl_scanout_texture) {
2135 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
2136 backing_y_0_top,
2137 backing_width, backing_height,
bf41ab61
MAL
2138 x, y, width, height,
2139 d3d_tex2d);
a9fbce5e 2140 }
7cc712e9 2141 }
06020b95
GH
2142}
2143
4133fa71
GH
2144void dpy_gl_scanout_dmabuf(QemuConsole *con,
2145 QemuDmaBuf *dmabuf)
2146{
7cc712e9
MAL
2147 DisplayState *s = con->ds;
2148 DisplayChangeListener *dcl;
2149
ebced091
MAL
2150 con->scanout.kind = SCANOUT_DMABUF;
2151 con->scanout.dmabuf = dmabuf;
7cc712e9 2152 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2153 if (con != (dcl->con ? dcl->con : active_console)) {
2154 continue;
2155 }
a9fbce5e
MAL
2156 if (dcl->ops->dpy_gl_scanout_dmabuf) {
2157 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
2158 }
7cc712e9 2159 }
4133fa71
GH
2160}
2161
6e1f2cb5
GH
2162void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
2163 bool have_hot, uint32_t hot_x, uint32_t hot_y)
4133fa71 2164{
7cc712e9
MAL
2165 DisplayState *s = con->ds;
2166 DisplayChangeListener *dcl;
4133fa71 2167
7cc712e9 2168 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2169 if (con != (dcl->con ? dcl->con : active_console)) {
2170 continue;
2171 }
7cc712e9
MAL
2172 if (dcl->ops->dpy_gl_cursor_dmabuf) {
2173 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
6e1f2cb5 2174 have_hot, hot_x, hot_y);
7cc712e9 2175 }
6e1f2cb5
GH
2176 }
2177}
2178
2179void dpy_gl_cursor_position(QemuConsole *con,
2180 uint32_t pos_x, uint32_t pos_y)
2181{
7cc712e9
MAL
2182 DisplayState *s = con->ds;
2183 DisplayChangeListener *dcl;
6e1f2cb5 2184
7cc712e9 2185 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2186 if (con != (dcl->con ? dcl->con : active_console)) {
2187 continue;
2188 }
7cc712e9
MAL
2189 if (dcl->ops->dpy_gl_cursor_position) {
2190 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
2191 }
4133fa71
GH
2192 }
2193}
2194
2195void dpy_gl_release_dmabuf(QemuConsole *con,
2196 QemuDmaBuf *dmabuf)
2197{
7cc712e9
MAL
2198 DisplayState *s = con->ds;
2199 DisplayChangeListener *dcl;
4133fa71 2200
7cc712e9 2201 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2202 if (con != (dcl->con ? dcl->con : active_console)) {
2203 continue;
2204 }
7cc712e9
MAL
2205 if (dcl->ops->dpy_gl_release_dmabuf) {
2206 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
2207 }
4133fa71
GH
2208 }
2209}
2210
06020b95
GH
2211void dpy_gl_update(QemuConsole *con,
2212 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
2213{
7cc712e9
MAL
2214 DisplayState *s = con->ds;
2215 DisplayChangeListener *dcl;
2216
06020b95 2217 assert(con->gl);
f6413cbf
MAL
2218
2219 graphic_hw_gl_block(con, true);
7cc712e9 2220 QLIST_FOREACH(dcl, &s->listeners, next) {
1699d00e
AO
2221 if (con != (dcl->con ? dcl->con : active_console)) {
2222 continue;
2223 }
a9fbce5e
MAL
2224 if (dcl->ops->dpy_gl_update) {
2225 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
2226 }
7cc712e9 2227 }
f6413cbf 2228 graphic_hw_gl_block(con, false);
06020b95
GH
2229}
2230
98b50080
PB
2231/***********************************************************/
2232/* register display */
2233
64840c66
GH
2234/* console.c internal use only */
2235static DisplayState *get_alloc_displaystate(void)
98b50080 2236{
64840c66
GH
2237 if (!display_state) {
2238 display_state = g_new0(DisplayState, 1);
aea7947c
GH
2239 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2240 text_console_update_cursor, NULL);
64840c66
GH
2241 }
2242 return display_state;
98b50080
PB
2243}
2244
64840c66
GH
2245/*
2246 * Called by main(), after creating QemuConsoles
2247 * and before initializing ui (sdl/vnc/...).
2248 */
2249DisplayState *init_displaystate(void)
98b50080 2250{
43f420f8 2251 gchar *name;
cd6cd8fa 2252 QemuConsole *con;
64840c66 2253
333cb18f 2254 get_alloc_displaystate();
cd6cd8fa
GH
2255 QTAILQ_FOREACH(con, &consoles, next) {
2256 if (con->console_type != GRAPHIC_CONSOLE &&
2257 con->ds == NULL) {
2258 text_console_do_init(con->chr, display_state);
64840c66 2259 }
43f420f8
GH
2260
2261 /* Hook up into the qom tree here (not in new_console()), once
2262 * all QemuConsoles are created and the order / numbering
2263 * doesn't change any more */
cd6cd8fa 2264 name = g_strdup_printf("console[%d]", con->index);
43f420f8 2265 object_property_add_child(container_get(object_get_root(), "/backend"),
d2623129 2266 name, OBJECT(con));
43f420f8 2267 g_free(name);
64840c66
GH
2268 }
2269
98b50080
PB
2270 return display_state;
2271}
2272
1c1f9498
GH
2273void graphic_console_set_hwops(QemuConsole *con,
2274 const GraphicHwOps *hw_ops,
2275 void *opaque)
2276{
2277 con->hw_ops = hw_ops;
2278 con->hw = opaque;
2279}
2280
5643706a 2281QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 2282 const GraphicHwOps *hw_ops,
c78f7137 2283 void *opaque)
95219897 2284{
521a580d
GH
2285 static const char noinit[] =
2286 "Guest has not initialized the display (yet).";
64840c66
GH
2287 int width = 640;
2288 int height = 480;
76ffb0b4 2289 QemuConsole *s;
3023f332 2290 DisplayState *ds;
9588d67e 2291 DisplaySurface *surface;
f0f2f976 2292
64840c66 2293 ds = get_alloc_displaystate();
9588d67e
GH
2294 s = qemu_console_lookup_unused();
2295 if (s) {
2296 trace_console_gfx_reuse(s->index);
ebced091
MAL
2297 width = qemu_console_get_width(s, 0);
2298 height = qemu_console_get_height(s, 0);
9588d67e
GH
2299 } else {
2300 trace_console_gfx_new();
2301 s = new_console(ds, GRAPHIC_CONSOLE, head);
2302 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2303 dpy_set_ui_info_timer, s);
2304 }
1c1f9498 2305 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 2306 if (dev) {
5325cc34 2307 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
afff2b15 2308 &error_abort);
aa2beaa1 2309 }
3023f332 2310
b5a087b0 2311 surface = qemu_create_placeholder_surface(width, height, noinit);
9588d67e 2312 dpy_gfx_replace_surface(s, surface);
a9b1e471
MAL
2313 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2314 graphic_hw_gl_unblock_timer, s);
c78f7137 2315 return s;
e7f0ad58
FB
2316}
2317
9588d67e
GH
2318static const GraphicHwOps unused_ops = {
2319 /* no callbacks */
2320};
2321
2322void graphic_console_close(QemuConsole *con)
2323{
2324 static const char unplugged[] =
2325 "Guest display has been unplugged";
2326 DisplaySurface *surface;
ebced091
MAL
2327 int width = qemu_console_get_width(con, 640);
2328 int height = qemu_console_get_height(con, 480);
9588d67e
GH
2329
2330 trace_console_gfx_close(con->index);
5325cc34 2331 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
9588d67e
GH
2332 graphic_console_set_hwops(con, &unused_ops, NULL);
2333
2334 if (con->gl) {
2335 dpy_gl_scanout_disable(con);
2336 }
b5a087b0 2337 surface = qemu_create_placeholder_surface(width, height, unplugged);
9588d67e
GH
2338 dpy_gfx_replace_surface(con, surface);
2339}
2340
284d1c6b
GH
2341QemuConsole *qemu_console_lookup_by_index(unsigned int index)
2342{
cd6cd8fa
GH
2343 QemuConsole *con;
2344
2345 QTAILQ_FOREACH(con, &consoles, next) {
2346 if (con->index == index) {
2347 return con;
2348 }
284d1c6b 2349 }
cd6cd8fa 2350 return NULL;
284d1c6b
GH
2351}
2352
5643706a 2353QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 2354{
cd6cd8fa 2355 QemuConsole *con;
14a93649 2356 Object *obj;
5643706a 2357 uint32_t h;
14a93649 2358
cd6cd8fa
GH
2359 QTAILQ_FOREACH(con, &consoles, next) {
2360 obj = object_property_get_link(OBJECT(con),
afff2b15 2361 "device", &error_abort);
5643706a
GH
2362 if (DEVICE(obj) != dev) {
2363 continue;
2364 }
cd6cd8fa 2365 h = object_property_get_uint(OBJECT(con),
ad664c1d 2366 "head", &error_abort);
5643706a
GH
2367 if (h != head) {
2368 continue;
14a93649 2369 }
cd6cd8fa 2370 return con;
14a93649
GH
2371 }
2372 return NULL;
2373}
2374
f2c1d54c
GH
2375QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
2376 uint32_t head, Error **errp)
2377{
2378 DeviceState *dev;
2379 QemuConsole *con;
2380
2381 dev = qdev_find_recursive(sysbus_get_default(), device_id);
2382 if (dev == NULL) {
2383 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2384 "Device '%s' not found", device_id);
2385 return NULL;
2386 }
2387
2388 con = qemu_console_lookup_by_device(dev, head);
2389 if (con == NULL) {
2390 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
2391 device_id, head);
2392 return NULL;
2393 }
2394
2395 return con;
2396}
2397
9588d67e
GH
2398QemuConsole *qemu_console_lookup_unused(void)
2399{
cd6cd8fa 2400 QemuConsole *con;
9588d67e 2401 Object *obj;
9588d67e 2402
cd6cd8fa
GH
2403 QTAILQ_FOREACH(con, &consoles, next) {
2404 if (con->hw_ops != &unused_ops) {
9588d67e
GH
2405 continue;
2406 }
cd6cd8fa 2407 obj = object_property_get_link(OBJECT(con),
9588d67e
GH
2408 "device", &error_abort);
2409 if (obj != NULL) {
2410 continue;
2411 }
cd6cd8fa 2412 return con;
9588d67e
GH
2413 }
2414 return NULL;
2415}
2416
385ac97f
MAL
2417QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
2418{
3c293a46
MAL
2419 if (con == NULL) {
2420 con = active_console;
2421 }
333e7599 2422 return con ? con->cursor : NULL;
385ac97f
MAL
2423}
2424
81c0d5a6 2425bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 2426{
284d1c6b 2427 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
2428}
2429
81c0d5a6 2430bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 2431{
81c0d5a6
GH
2432 if (con == NULL) {
2433 con = active_console;
2434 }
2435 return con && (con->console_type == GRAPHIC_CONSOLE);
2436}
2437
2438bool qemu_console_is_fixedsize(QemuConsole *con)
2439{
2440 if (con == NULL) {
2441 con = active_console;
2442 }
2443 return con && (con->console_type != TEXT_CONSOLE);
c21bbcfa
AZ
2444}
2445
f607867c
GH
2446bool qemu_console_is_gl_blocked(QemuConsole *con)
2447{
2448 assert(con != NULL);
2449 return con->gl_block;
2450}
2451
839a4826
WJ
2452bool qemu_console_is_multihead(DeviceState *dev)
2453{
2454 QemuConsole *con;
2455 Object *obj;
2456 uint32_t f = 0xffffffff;
2457 uint32_t h;
2458
2459 QTAILQ_FOREACH(con, &consoles, next) {
2460 obj = object_property_get_link(OBJECT(con),
2461 "device", &error_abort);
2462 if (DEVICE(obj) != dev) {
2463 continue;
2464 }
2465
2466 h = object_property_get_uint(OBJECT(con),
2467 "head", &error_abort);
2468 if (f == 0xffffffff) {
2469 f = h;
2470 } else if (h != f) {
2471 return true;
2472 }
2473 }
2474 return false;
2475}
2476
779ce88f
GH
2477char *qemu_console_get_label(QemuConsole *con)
2478{
2479 if (con->console_type == GRAPHIC_CONSOLE) {
2480 if (con->device) {
839a4826
WJ
2481 DeviceState *dev;
2482 bool multihead;
2483
2484 dev = DEVICE(con->device);
2485 multihead = qemu_console_is_multihead(dev);
2486 if (multihead) {
2487 return g_strdup_printf("%s.%d", dev->id ?
2488 dev->id :
2489 object_get_typename(con->device),
2490 con->head);
2491 } else {
2492 return g_strdup_printf("%s", dev->id ?
2493 dev->id :
2494 object_get_typename(con->device));
2495 }
779ce88f
GH
2496 }
2497 return g_strdup("VGA");
2498 } else {
2499 if (con->chr && con->chr->label) {
2500 return g_strdup(con->chr->label);
2501 }
2502 return g_strdup_printf("vc%d", con->index);
2503 }
2504}
2505
d4c85337
GH
2506int qemu_console_get_index(QemuConsole *con)
2507{
2508 if (con == NULL) {
2509 con = active_console;
2510 }
2511 return con ? con->index : -1;
2512}
2513
5643706a
GH
2514uint32_t qemu_console_get_head(QemuConsole *con)
2515{
2516 if (con == NULL) {
2517 con = active_console;
2518 }
2519 return con ? con->head : -1;
2520}
2521
d4c85337
GH
2522int qemu_console_get_width(QemuConsole *con, int fallback)
2523{
2524 if (con == NULL) {
2525 con = active_console;
2526 }
ebced091
MAL
2527 if (con == NULL) {
2528 return fallback;
2529 }
2530 switch (con->scanout.kind) {
2531 case SCANOUT_DMABUF:
2532 return con->scanout.dmabuf->width;
2533 case SCANOUT_TEXTURE:
2534 return con->scanout.texture.width;
2535 case SCANOUT_SURFACE:
2536 return surface_width(con->surface);
2537 default:
2538 return fallback;
2539 }
d4c85337
GH
2540}
2541
2542int qemu_console_get_height(QemuConsole *con, int fallback)
2543{
2544 if (con == NULL) {
2545 con = active_console;
2546 }
ebced091
MAL
2547 if (con == NULL) {
2548 return fallback;
2549 }
2550 switch (con->scanout.kind) {
2551 case SCANOUT_DMABUF:
2552 return con->scanout.dmabuf->height;
2553 case SCANOUT_TEXTURE:
2554 return con->scanout.texture.height;
2555 case SCANOUT_SURFACE:
2556 return surface_height(con->surface);
2557 default:
2558 return fallback;
2559 }
d4c85337
GH
2560}
2561
ec222519
VR
2562static void vc_chr_accept_input(Chardev *chr)
2563{
2564 VCChardev *drv = VC_CHARDEV(chr);
2565 QemuConsole *s = drv->console;
2566
2567 kbd_send_chars(s);
2568}
2569
5bf5adae 2570static void vc_chr_set_echo(Chardev *chr, bool echo)
4104833f 2571{
777357d7 2572 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 2573 QemuConsole *s = drv->console;
4104833f
PB
2574
2575 s->echo = echo;
2576}
2577
aea7947c
GH
2578static void text_console_update_cursor_timer(void)
2579{
2580 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2581 + CONSOLE_CURSOR_PERIOD / 2);
2582}
2583
bf1bed81
JK
2584static void text_console_update_cursor(void *opaque)
2585{
aea7947c 2586 QemuConsole *s;
cd6cd8fa 2587 int count = 0;
aea7947c
GH
2588
2589 cursor_visible_phase = !cursor_visible_phase;
bf1bed81 2590
cd6cd8fa 2591 QTAILQ_FOREACH(s, &consoles, next) {
aea7947c
GH
2592 if (qemu_console_is_graphic(s) ||
2593 !qemu_console_is_visible(s)) {
2594 continue;
2595 }
2596 count++;
2597 graphic_hw_invalidate(s);
2598 }
2599
2600 if (count) {
2601 text_console_update_cursor_timer();
2602 }
bf1bed81
JK
2603}
2604
380cd056
GH
2605static const GraphicHwOps text_console_ops = {
2606 .invalidate = text_console_invalidate,
2607 .text_update = text_console_update,
2608};
2609
0ec7b3e7 2610static void text_console_do_init(Chardev *chr, DisplayState *ds)
e7f0ad58 2611{
777357d7 2612 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 2613 QemuConsole *s = drv->console;
36671fbd
GH
2614 int g_width = 80 * FONT_WIDTH;
2615 int g_height = 24 * FONT_HEIGHT;
6d6f7c28 2616
0c9d0641 2617 fifo8_create(&s->out_fifo, 16);
3023f332 2618 s->ds = ds;
3b46e624 2619
e7f0ad58
FB
2620 s->y_displayed = 0;
2621 s->y_base = 0;
2622 s->total_height = DEFAULT_BACKSCROLL;
2623 s->x = 0;
2624 s->y = 0;
ebced091
MAL
2625 if (s->scanout.kind != SCANOUT_SURFACE) {
2626 if (active_console && active_console->scanout.kind == SCANOUT_SURFACE) {
2627 g_width = qemu_console_get_width(active_console, g_width);
2628 g_height = qemu_console_get_height(active_console, g_height);
321f048d 2629 }
36671fbd 2630 s->surface = qemu_create_displaysurface(g_width, g_height);
ebced091 2631 s->scanout.kind = SCANOUT_SURFACE;
491e114a 2632 }
6d6f7c28 2633
380cd056 2634 s->hw_ops = &text_console_ops;
4d3b6f6e
AZ
2635 s->hw = s;
2636
6d6f7c28
PB
2637 /* Set text attribute defaults */
2638 s->t_attrib_default.bold = 0;
2639 s->t_attrib_default.uline = 0;
2640 s->t_attrib_default.blink = 0;
2641 s->t_attrib_default.invers = 0;
2642 s->t_attrib_default.unvisible = 0;
4083733d
OH
2643 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2644 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
2645 /* set current text attributes to default */
2646 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
2647 text_console_resize(s);
2648
51bfa4d3 2649 if (chr->label) {
18595181 2650 char *msg;
51bfa4d3 2651
4083733d 2652 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
18595181
GH
2653 msg = g_strdup_printf("%s console\r\n", chr->label);
2654 vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
2655 g_free(msg);
735ba588 2656 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
2657 }
2658
63618135 2659 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
e7f0ad58 2660}
c60e08d9 2661
777357d7
MAL
2662static void vc_chr_open(Chardev *chr,
2663 ChardevBackend *backend,
2664 bool *be_opened,
2665 Error **errp)
2796dae0 2666{
6f974c84 2667 ChardevVC *vc = backend->u.vc.data;
777357d7 2668 VCChardev *drv = VC_CHARDEV(chr);
76ffb0b4 2669 QemuConsole *s;
702ec69c
GH
2670 unsigned width = 0;
2671 unsigned height = 0;
2796dae0 2672
702ec69c
GH
2673 if (vc->has_width) {
2674 width = vc->width;
2675 } else if (vc->has_cols) {
2676 width = vc->cols * FONT_WIDTH;
2677 }
491e114a 2678
702ec69c
GH
2679 if (vc->has_height) {
2680 height = vc->height;
2681 } else if (vc->has_rows) {
2682 height = vc->rows * FONT_HEIGHT;
2683 }
491e114a 2684
437fe106 2685 trace_console_txt_new(width, height);
491e114a 2686 if (width == 0 || height == 0) {
afff2b15 2687 s = new_console(NULL, TEXT_CONSOLE, 0);
491e114a 2688 } else {
afff2b15 2689 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
ebced091 2690 s->scanout.kind = SCANOUT_SURFACE;
36671fbd 2691 s->surface = qemu_create_displaysurface(width, height);
491e114a
PB
2692 }
2693
2694 if (!s) {
fa19d025 2695 error_setg(errp, "cannot create text console");
777357d7 2696 return;
491e114a
PB
2697 }
2698
2699 s->chr = chr;
41ac54b2 2700 drv->console = s;
64840c66
GH
2701
2702 if (display_state) {
2703 text_console_do_init(chr, display_state);
2704 }
2796dae0 2705
82878dac
MAL
2706 /* console/chardev init sometimes completes elsewhere in a 2nd
2707 * stage, so defer OPENED events until they are fully initialized
2708 */
2709 *be_opened = false;
d82831db
AL
2710}
2711
c78f7137 2712void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 2713{
88738ea4 2714 DisplaySurface *surface = qemu_console_surface(s);
321f048d
GH
2715
2716 assert(s->console_type == GRAPHIC_CONSOLE);
cd958edb 2717
88738ea4
MAL
2718 if ((s->scanout.kind != SCANOUT_SURFACE ||
2719 (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
2720 qemu_console_get_width(s, -1) == width &&
cb8962c1 2721 qemu_console_get_height(s, -1) == height) {
cd958edb
MAL
2722 return;
2723 }
2724
321f048d
GH
2725 surface = qemu_create_displaysurface(width, height);
2726 dpy_gfx_replace_surface(s, surface);
c60e08d9 2727}
38334f76 2728
c78f7137
GH
2729DisplaySurface *qemu_console_surface(QemuConsole *console)
2730{
ebced091
MAL
2731 switch (console->scanout.kind) {
2732 case SCANOUT_SURFACE:
2733 return console->surface;
2734 default:
2735 return NULL;
2736 }
c78f7137
GH
2737}
2738
0da2ea1b 2739PixelFormat qemu_default_pixelformat(int bpp)
2740{
56bd9ea1
GH
2741 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2742 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
2743 return pf;
2744}
01f45d98 2745
db71589f
GH
2746static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2747
2748void qemu_display_register(QemuDisplay *ui)
2749{
2750 assert(ui->type < DISPLAY_TYPE__MAX);
2751 dpys[ui->type] = ui;
2752}
2753
898f9d41
GH
2754bool qemu_display_find_default(DisplayOptions *opts)
2755{
2756 static DisplayType prio[] = {
66c2207f 2757#if defined(CONFIG_GTK)
898f9d41 2758 DISPLAY_TYPE_GTK,
66c2207f
TH
2759#endif
2760#if defined(CONFIG_SDL)
898f9d41 2761 DISPLAY_TYPE_SDL,
66c2207f
TH
2762#endif
2763#if defined(CONFIG_COCOA)
898f9d41 2764 DISPLAY_TYPE_COCOA
66c2207f 2765#endif
898f9d41
GH
2766 };
2767 int i;
2768
66c2207f 2769 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
61b4d9a2 2770 if (dpys[prio[i]] == NULL) {
c551fb0b
CF
2771 Error *local_err = NULL;
2772 int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
2773 if (rv < 0) {
2774 error_report_err(local_err);
2775 }
61b4d9a2 2776 }
898f9d41
GH
2777 if (dpys[prio[i]] == NULL) {
2778 continue;
2779 }
2780 opts->type = prio[i];
2781 return true;
2782 }
2783 return false;
2784}
2785
db71589f
GH
2786void qemu_display_early_init(DisplayOptions *opts)
2787{
2788 assert(opts->type < DISPLAY_TYPE__MAX);
2789 if (opts->type == DISPLAY_TYPE_NONE) {
2790 return;
2791 }
61b4d9a2 2792 if (dpys[opts->type] == NULL) {
c551fb0b
CF
2793 Error *local_err = NULL;
2794 int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
2795 if (rv < 0) {
2796 error_report_err(local_err);
2797 }
61b4d9a2 2798 }
db71589f
GH
2799 if (dpys[opts->type] == NULL) {
2800 error_report("Display '%s' is not available.",
c809d1d2 2801 DisplayType_str(opts->type));
db71589f
GH
2802 exit(1);
2803 }
2804 if (dpys[opts->type]->early_init) {
2805 dpys[opts->type]->early_init(opts);
2806 }
2807}
2808
2809void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2810{
2811 assert(opts->type < DISPLAY_TYPE__MAX);
2812 if (opts->type == DISPLAY_TYPE_NONE) {
2813 return;
2814 }
2815 assert(dpys[opts->type] != NULL);
2816 dpys[opts->type]->init(ds, opts);
2817}
2818
c388f408
TH
2819void qemu_display_help(void)
2820{
2821 int idx;
2822
2823 printf("Available display backend types:\n");
a1e8853e 2824 printf("none\n");
c388f408
TH
2825 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
2826 if (!dpys[idx]) {
c551fb0b
CF
2827 Error *local_err = NULL;
2828 int rv = ui_module_load(DisplayType_str(idx), &local_err);
2829 if (rv < 0) {
2830 error_report_err(local_err);
2831 }
c388f408
TH
2832 }
2833 if (dpys[idx]) {
2834 printf("%s\n", DisplayType_str(dpys[idx]->type));
2835 }
2836 }
2837}
2838
6f974c84 2839void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
702ec69c
GH
2840{
2841 int val;
21a933ea 2842 ChardevVC *vc;
702ec69c 2843
0b663b7d 2844 backend->type = CHARDEV_BACKEND_KIND_VC;
32bafa8f 2845 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
21a933ea 2846 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
702ec69c
GH
2847
2848 val = qemu_opt_get_number(opts, "width", 0);
2849 if (val != 0) {
21a933ea
EB
2850 vc->has_width = true;
2851 vc->width = val;
702ec69c
GH
2852 }
2853
2854 val = qemu_opt_get_number(opts, "height", 0);
2855 if (val != 0) {
21a933ea
EB
2856 vc->has_height = true;
2857 vc->height = val;
702ec69c
GH
2858 }
2859
2860 val = qemu_opt_get_number(opts, "cols", 0);
2861 if (val != 0) {
21a933ea
EB
2862 vc->has_cols = true;
2863 vc->cols = val;
702ec69c
GH
2864 }
2865
2866 val = qemu_opt_get_number(opts, "rows", 0);
2867 if (val != 0) {
21a933ea
EB
2868 vc->has_rows = true;
2869 vc->rows = val;
702ec69c
GH
2870 }
2871}
2872
95be0669
GH
2873static const TypeInfo qemu_console_info = {
2874 .name = TYPE_QEMU_CONSOLE,
2875 .parent = TYPE_OBJECT,
2876 .instance_size = sizeof(QemuConsole),
2877 .class_size = sizeof(QemuConsoleClass),
2878};
2879
777357d7
MAL
2880static void char_vc_class_init(ObjectClass *oc, void *data)
2881{
2882 ChardevClass *cc = CHARDEV_CLASS(oc);
2883
88cace9f 2884 cc->parse = qemu_chr_parse_vc;
777357d7
MAL
2885 cc->open = vc_chr_open;
2886 cc->chr_write = vc_chr_write;
ec222519 2887 cc->chr_accept_input = vc_chr_accept_input;
777357d7
MAL
2888 cc->chr_set_echo = vc_chr_set_echo;
2889}
2890
2891static const TypeInfo char_vc_type_info = {
2892 .name = TYPE_CHARDEV_VC,
2893 .parent = TYPE_CHARDEV,
0ec7b3e7 2894 .instance_size = sizeof(VCChardev),
777357d7
MAL
2895 .class_init = char_vc_class_init,
2896};
2897
2898void qemu_console_early_init(void)
2899{
2900 /* set the default vc driver */
2901 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2902 type_register(&char_vc_type_info);
777357d7
MAL
2903 }
2904}
2905
01f45d98
AL
2906static void register_types(void)
2907{
95be0669 2908 type_register_static(&qemu_console_info);
01f45d98
AL
2909}
2910
2911type_init(register_types);