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