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