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