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