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