]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
ui/dbus: do not send 2d scanout until gfx_update
[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
589089fe
MAL
1076static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
1077{
1078 if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
1079 con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
1080 }
1081}
1082
1083static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
1084{
1085 if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
1086 con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
1087 }
1088}
1089
1090static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
1091 int x, int y, int w, int h)
1092{
1093 if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
1094 con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
1095 }
1096}
26b032b9 1097
ebced091 1098static void displaychangelistener_display_console(DisplayChangeListener *dcl,
4b7b661d
MAL
1099 QemuConsole *con,
1100 Error **errp)
ebced091
MAL
1101{
1102 static const char nodev[] =
1103 "This VM has no graphic display device.";
1104 static DisplaySurface *dummy;
1105
4b7b661d 1106 if (!con || !console_compatible_with(con, dcl, errp)) {
ebced091
MAL
1107 if (!dummy) {
1108 dummy = qemu_create_placeholder_surface(640, 480, nodev);
1109 }
589089fe
MAL
1110 if (con) {
1111 dpy_gfx_create_texture(con, dummy);
1112 }
c84ab0a5 1113 displaychangelistener_gfx_switch(dcl, dummy, TRUE);
ebced091
MAL
1114 return;
1115 }
1116
1117 if (con->scanout.kind == SCANOUT_DMABUF &&
1118 displaychangelistener_has_dmabuf(dcl)) {
1119 dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
1120 } else if (con->scanout.kind == SCANOUT_TEXTURE &&
1121 dcl->ops->dpy_gl_scanout_texture) {
1122 dcl->ops->dpy_gl_scanout_texture(dcl,
1123 con->scanout.texture.backing_id,
1124 con->scanout.texture.backing_y_0_top,
1125 con->scanout.texture.backing_width,
1126 con->scanout.texture.backing_height,
1127 con->scanout.texture.x,
1128 con->scanout.texture.y,
1129 con->scanout.texture.width,
1130 con->scanout.texture.height);
26b032b9 1131 } else if (con->scanout.kind == SCANOUT_SURFACE) {
589089fe 1132 dpy_gfx_create_texture(con, con->surface);
c84ab0a5 1133 displaychangelistener_gfx_switch(dcl, con->surface, TRUE);
ebced091 1134 }
ebced091
MAL
1135}
1136
e7f0ad58
FB
1137void console_select(unsigned int index)
1138{
284d1c6b 1139 DisplayChangeListener *dcl;
76ffb0b4 1140 QemuConsole *s;
6d6f7c28 1141
437fe106 1142 trace_console_select(index);
284d1c6b 1143 s = qemu_console_lookup_by_index(index);
e7f0ad58 1144 if (s) {
7d957bd8 1145 DisplayState *ds = s->ds;
bf1bed81 1146
e7f0ad58 1147 active_console = s;
a93a4a22 1148 if (ds->have_gfx) {
284d1c6b
GH
1149 QLIST_FOREACH(dcl, &ds->listeners, next) {
1150 if (dcl->con != NULL) {
1151 continue;
1152 }
4b7b661d 1153 displaychangelistener_display_console(dcl, s, NULL);
2e5567c9 1154 }
a93a4a22
GH
1155 }
1156 if (ds->have_text) {
c78f7137 1157 dpy_text_resize(s, s->width, s->height);
68f00996 1158 }
aea7947c 1159 text_console_update_cursor(NULL);
e7f0ad58
FB
1160 }
1161}
1162
db1015e9 1163struct VCChardev {
0ec7b3e7 1164 Chardev parent;
41ac54b2 1165 QemuConsole *console;
db1015e9
EH
1166};
1167typedef struct VCChardev VCChardev;
41ac54b2 1168
777357d7 1169#define TYPE_CHARDEV_VC "chardev-vc"
8110fa1d
EH
1170DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
1171 TYPE_CHARDEV_VC)
777357d7 1172
5bf5adae 1173static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
e7f0ad58 1174{
777357d7 1175 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 1176 QemuConsole *s = drv->console;
e7f0ad58
FB
1177 int i;
1178
b68e956a
MAL
1179 if (!s->ds) {
1180 return 0;
1181 }
1182
14778c20
PB
1183 s->update_x0 = s->width * FONT_WIDTH;
1184 s->update_y0 = s->height * FONT_HEIGHT;
1185 s->update_x1 = 0;
1186 s->update_y1 = 0;
e7f0ad58
FB
1187 console_show_cursor(s, 0);
1188 for(i = 0; i < len; i++) {
1189 console_putchar(s, buf[i]);
1190 }
1191 console_show_cursor(s, 1);
a93a4a22 1192 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
c78f7137 1193 dpy_gfx_update(s, s->update_x0, s->update_y0,
a93a4a22
GH
1194 s->update_x1 - s->update_x0,
1195 s->update_y1 - s->update_y0);
14778c20 1196 }
e7f0ad58
FB
1197 return len;
1198}
1199
ec222519 1200static void kbd_send_chars(QemuConsole *s)
e15d7371 1201{
0c9d0641 1202 uint32_t len, avail;
3b46e624 1203
909cda12 1204 len = qemu_chr_be_can_write(s->chr);
0c9d0641 1205 avail = fifo8_num_used(&s->out_fifo);
ec222519 1206 while (len > 0 && avail > 0) {
0c9d0641
VR
1207 const uint8_t *buf;
1208 uint32_t size;
1209
ec222519 1210 buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
0c9d0641 1211 qemu_chr_be_write(s->chr, (uint8_t *)buf, size);
ec222519 1212 len = qemu_chr_be_can_write(s->chr);
0c9d0641 1213 avail -= size;
e15d7371 1214 }
e15d7371
FB
1215}
1216
e7f0ad58 1217/* called when an ascii key is pressed */
3f9a6e85 1218void kbd_put_keysym_console(QemuConsole *s, int keysym)
e7f0ad58 1219{
e7f0ad58
FB
1220 uint8_t buf[16], *q;
1221 int c;
0c9d0641 1222 uint32_t num_free;
e7f0ad58 1223
af3a9031 1224 if (!s || (s->console_type == GRAPHIC_CONSOLE))
e7f0ad58
FB
1225 return;
1226
1227 switch(keysym) {
1228 case QEMU_KEY_CTRL_UP:
81c0d5a6 1229 console_scroll(s, -1);
e7f0ad58
FB
1230 break;
1231 case QEMU_KEY_CTRL_DOWN:
81c0d5a6 1232 console_scroll(s, 1);
e7f0ad58
FB
1233 break;
1234 case QEMU_KEY_CTRL_PAGEUP:
81c0d5a6 1235 console_scroll(s, -10);
e7f0ad58
FB
1236 break;
1237 case QEMU_KEY_CTRL_PAGEDOWN:
81c0d5a6 1238 console_scroll(s, 10);
e7f0ad58
FB
1239 break;
1240 default:
e15d7371
FB
1241 /* convert the QEMU keysym to VT100 key string */
1242 q = buf;
1243 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1244 *q++ = '\033';
1245 *q++ = '[';
1246 c = keysym - 0xe100;
1247 if (c >= 10)
1248 *q++ = '0' + (c / 10);
1249 *q++ = '0' + (c % 10);
1250 *q++ = '~';
1251 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1252 *q++ = '\033';
1253 *q++ = '[';
1254 *q++ = keysym & 0xff;
4104833f 1255 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
5bf5adae 1256 vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
4104833f 1257 *q++ = '\n';
e15d7371 1258 } else {
4104833f
PB
1259 *q++ = keysym;
1260 }
1261 if (s->echo) {
5bf5adae 1262 vc_chr_write(s->chr, buf, q - buf);
e15d7371 1263 }
014b00cc
VR
1264 num_free = fifo8_num_free(&s->out_fifo);
1265 fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
1266 kbd_send_chars(s);
e7f0ad58
FB
1267 break;
1268 }
1269}
1270
7fb1cf16 1271static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
50ef4679
GH
1272 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1273 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1274 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1275 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1276 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1277 [Q_KEY_CODE_END] = QEMU_KEY_END,
1278 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1279 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1280 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
344aa283 1281 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
50ef4679
GH
1282};
1283
da024b1e
GH
1284static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
1285 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
1286 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
1287 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
1288 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
1289 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
1290 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
1291 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
1292 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
1293};
1294
1295bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
50ef4679
GH
1296{
1297 int keysym;
1298
da024b1e 1299 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
50ef4679
GH
1300 if (keysym == 0) {
1301 return false;
1302 }
1303 kbd_put_keysym_console(s, keysym);
1304 return true;
1305}
1306
bdef9724
GH
1307void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1308{
1309 int i;
1310
1311 for (i = 0; i < len && str[i]; i++) {
1312 kbd_put_keysym_console(s, str[i]);
1313 }
1314}
1315
3f9a6e85
GH
1316void kbd_put_keysym(int keysym)
1317{
1318 kbd_put_keysym_console(active_console, keysym);
1319}
1320
4d3b6f6e
AZ
1321static void text_console_invalidate(void *opaque)
1322{
76ffb0b4 1323 QemuConsole *s = (QemuConsole *) opaque;
1562e531
GH
1324
1325 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
68f00996
AL
1326 text_console_resize(s);
1327 }
4d3b6f6e
AZ
1328 console_refresh(s);
1329}
1330
c227f099 1331static void text_console_update(void *opaque, console_ch_t *chardata)
4d3b6f6e 1332{
76ffb0b4 1333 QemuConsole *s = (QemuConsole *) opaque;
4d3b6f6e
AZ
1334 int i, j, src;
1335
1336 if (s->text_x[0] <= s->text_x[1]) {
1337 src = (s->y_base + s->text_y[0]) * s->width;
1338 chardata += s->text_y[0] * s->width;
1339 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
4083733d
OH
1340 for (j = 0; j < s->width; j++, src++) {
1341 console_write_ch(chardata ++,
1342 ATTR2CHTYPE(s->cells[src].ch,
1343 s->cells[src].t_attrib.fgcol,
1344 s->cells[src].t_attrib.bgcol,
1345 s->cells[src].t_attrib.bold));
1346 }
c78f7137 1347 dpy_text_update(s, s->text_x[0], s->text_y[0],
a93a4a22 1348 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
4d3b6f6e
AZ
1349 s->text_x[0] = s->width;
1350 s->text_y[0] = s->height;
1351 s->text_x[1] = 0;
1352 s->text_y[1] = 0;
1353 }
1354 if (s->cursor_invalidate) {
c78f7137 1355 dpy_text_cursor(s, s->x, s->y);
4d3b6f6e
AZ
1356 s->cursor_invalidate = 0;
1357 }
1358}
1359
afff2b15
KB
1360static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1361 uint32_t head)
e7f0ad58 1362{
95be0669 1363 Object *obj;
76ffb0b4 1364 QemuConsole *s;
95219897 1365 int i;
e7f0ad58 1366
95be0669
GH
1367 obj = object_new(TYPE_QEMU_CONSOLE);
1368 s = QEMU_CONSOLE(obj);
0d9b90ce 1369 qemu_co_queue_init(&s->dump_queue);
afff2b15 1370 s->head = head;
aa2beaa1 1371 object_property_add_link(obj, "device", TYPE_DEVICE,
9561fda8 1372 (Object **)&s->device,
39f72ef9 1373 object_property_allow_set_link,
d2623129 1374 OBJ_PROP_LINK_STRONG);
836e1b38 1375 object_property_add_uint32_ptr(obj, "head", &s->head,
d2623129 1376 OBJ_PROP_FLAG_READ);
aa2beaa1 1377
af3a9031
TS
1378 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1379 (console_type == GRAPHIC_CONSOLE))) {
e7f0ad58 1380 active_console = s;
af3a9031 1381 }
e7f0ad58 1382 s->ds = ds;
af3a9031 1383 s->console_type = console_type;
41d004d8 1384 s->window_id = -1;
a1d2db08 1385
cd6cd8fa
GH
1386 if (QTAILQ_EMPTY(&consoles)) {
1387 s->index = 0;
1388 QTAILQ_INSERT_TAIL(&consoles, s, next);
2f181fbd 1389 } else if (console_type != GRAPHIC_CONSOLE || phase_check(PHASE_MACHINE_READY)) {
eae3eb3e 1390 QemuConsole *last = QTAILQ_LAST(&consoles);
cd6cd8fa
GH
1391 s->index = last->index + 1;
1392 QTAILQ_INSERT_TAIL(&consoles, s, next);
95219897 1393 } else {
9588d67e
GH
1394 /*
1395 * HACK: Put graphical consoles before text consoles.
1396 *
1397 * Only do that for coldplugged devices. After initial device
1398 * initialization we will not renumber the consoles any more.
1399 */
cd6cd8fa
GH
1400 QemuConsole *c = QTAILQ_FIRST(&consoles);
1401
1402 while (QTAILQ_NEXT(c, next) != NULL &&
1403 c->console_type == GRAPHIC_CONSOLE) {
1404 c = QTAILQ_NEXT(c, next);
1405 }
1406 if (c->console_type == GRAPHIC_CONSOLE) {
1407 /* have no text consoles */
1408 s->index = c->index + 1;
1409 QTAILQ_INSERT_AFTER(&consoles, c, s, next);
1410 } else {
1411 s->index = c->index;
1412 QTAILQ_INSERT_BEFORE(c, s, next);
1413 /* renumber text consoles */
1414 for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
1415 c->index = i;
1416 }
95219897 1417 }
95219897
PB
1418 }
1419 return s;
1420}
1421
eb69442a 1422DisplaySurface *qemu_create_displaysurface(int width, int height)
ffe8b821 1423{
eb69442a 1424 DisplaySurface *surface = g_new0(DisplaySurface, 1);
69c77777 1425
eb69442a 1426 trace_displaysurface_create(surface, width, height);
30f1e661 1427 surface->format = PIXMAN_x8r8g8b8;
69c77777
GH
1428 surface->image = pixman_image_create_bits(surface->format,
1429 width, height,
30f1e661 1430 NULL, width * 4);
69c77777 1431 assert(surface->image != NULL);
30f1e661 1432 surface->flags = QEMU_ALLOCATED_FLAG;
98b50080 1433
537a4391
GH
1434 return surface;
1435}
1436
30f1e661
GH
1437DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1438 pixman_format_code_t format,
1439 int linesize, uint8_t *data)
98b50080 1440{
69c77777 1441 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 1442
30f1e661
GH
1443 trace_displaysurface_create_from(surface, width, height, format);
1444 surface->format = format;
69c77777
GH
1445 surface->image = pixman_image_create_bits(surface->format,
1446 width, height,
1447 (void *)data, linesize);
1448 assert(surface->image != NULL);
1449
98b50080
PB
1450 return surface;
1451}
1452
ca58b45f
GH
1453DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1454{
1455 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1456
1457 trace_displaysurface_create_pixman(surface);
1458 surface->format = pixman_image_get_format(image);
1459 surface->image = pixman_image_ref(image);
1460
1461 return surface;
1462}
1463
b5a087b0
AO
1464DisplaySurface *qemu_create_placeholder_surface(int w, int h,
1465 const char *msg)
d3002b04 1466{
521a580d 1467 DisplaySurface *surface = qemu_create_displaysurface(w, h);
4083733d
OH
1468 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1469 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
d3002b04
GH
1470 pixman_image_t *glyph;
1471 int len, x, y, i;
1472
1473 len = strlen(msg);
521a580d
GH
1474 x = (w / FONT_WIDTH - len) / 2;
1475 y = (h / FONT_HEIGHT - 1) / 2;
d3002b04
GH
1476 for (i = 0; i < len; i++) {
1477 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1478 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1479 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1480 qemu_pixman_image_unref(glyph);
1481 }
b5a087b0 1482 surface->flags |= QEMU_PLACEHOLDER_FLAG;
d3002b04
GH
1483 return surface;
1484}
1485
da229ef3 1486void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 1487{
da229ef3 1488 if (surface == NULL) {
98b50080 1489 return;
187cd1d9 1490 }
da229ef3
GH
1491 trace_displaysurface_free(surface);
1492 qemu_pixman_image_unref(surface->image);
1493 g_free(surface);
98b50080
PB
1494}
1495
06020b95
GH
1496bool console_has_gl(QemuConsole *con)
1497{
1498 return con->gl != NULL;
1499}
1500
d0e137bc
MAL
1501static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
1502{
1503 if (dcl->ops->dpy_has_dmabuf) {
1504 return dcl->ops->dpy_has_dmabuf(dcl);
1505 }
1506
1507 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1508 return true;
1509 }
1510
1511 return false;
1512}
1513
4b7b661d
MAL
1514static bool console_compatible_with(QemuConsole *con,
1515 DisplayChangeListener *dcl, Error **errp)
5983fdf1 1516{
5983fdf1
MAL
1517 int flags;
1518
1519 flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
1520
a62c4a17
MAL
1521 if (console_has_gl(con) &&
1522 !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
398d1c91
MAL
1523 error_setg(errp, "Display %s is incompatible with the GL context",
1524 dcl->ops->dpy_name);
1525 return false;
1526 }
1527
5983fdf1
MAL
1528 if (flags & GRAPHIC_FLAGS_GL &&
1529 !console_has_gl(con)) {
1530 error_setg(errp, "The console requires a GL context.");
1531 return false;
1532
1533 }
1534
1535 if (flags & GRAPHIC_FLAGS_DMABUF &&
1536 !displaychangelistener_has_dmabuf(dcl)) {
1537 error_setg(errp, "The console requires display DMABUF support.");
1538 return false;
1539 }
1540
1541 return true;
1542}
1543
5e79d516 1544void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
4f418149
MAL
1545{
1546 /* display has opengl support */
5e79d516
MAL
1547 assert(con);
1548 if (con->gl) {
1549 error_report("The console already has an OpenGL context.");
4f418149
MAL
1550 exit(1);
1551 }
5e79d516
MAL
1552 con->gl = gl;
1553}
1554
5209089f 1555void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 1556{
284d1c6b
GH
1557 QemuConsole *con;
1558
e0665c3b
MAL
1559 assert(!dcl->ds);
1560
7c20b4a3 1561 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
1562 dcl->ds = get_alloc_displaystate();
1563 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1564 gui_setup_refresh(dcl->ds);
284d1c6b
GH
1565 if (dcl->con) {
1566 dcl->con->dcls++;
1567 con = dcl->con;
1568 } else {
1569 con = active_console;
1570 }
4b7b661d 1571 displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
aea7947c 1572 text_console_update_cursor(NULL);
7c20b4a3
GH
1573}
1574
0f7b2864
GH
1575void update_displaychangelistener(DisplayChangeListener *dcl,
1576 uint64_t interval)
1577{
1578 DisplayState *ds = dcl->ds;
1579
1580 dcl->update_interval = interval;
1581 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 1582 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
1583 }
1584}
1585
7c20b4a3
GH
1586void unregister_displaychangelistener(DisplayChangeListener *dcl)
1587{
1588 DisplayState *ds = dcl->ds;
1589 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
1590 if (dcl->con) {
1591 dcl->con->dcls--;
1592 }
7c20b4a3 1593 QLIST_REMOVE(dcl, next);
777c5f1e 1594 dcl->ds = NULL;
7c20b4a3
GH
1595 gui_setup_refresh(ds);
1596}
1597
cf1ecc82
GH
1598static void dpy_set_ui_info_timer(void *opaque)
1599{
1600 QemuConsole *con = opaque;
1601
1602 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1603}
1604
b7fb49f0
GH
1605bool dpy_ui_info_supported(QemuConsole *con)
1606{
5c4b107f
GH
1607 if (con == NULL) {
1608 con = active_console;
1609 }
1610
b7fb49f0
GH
1611 return con->hw_ops->ui_info != NULL;
1612}
1613
5eaf1e48
MAL
1614const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
1615{
5c4b107f
GH
1616 if (con == NULL) {
1617 con = active_console;
1618 }
5eaf1e48
MAL
1619
1620 return &con->ui_info;
1621}
1622
ca19ef52 1623int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
6f90f3d7 1624{
5c4b107f
GH
1625 if (con == NULL) {
1626 con = active_console;
1627 }
1185fde4 1628
b7fb49f0 1629 if (!dpy_ui_info_supported(con)) {
cf1ecc82 1630 return -1;
6f90f3d7 1631 }
1185fde4
GH
1632 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1633 /* nothing changed -- ignore */
1634 return 0;
1635 }
cf1ecc82
GH
1636
1637 /*
1638 * Typically we get a flood of these as the user resizes the window.
1639 * Wait until the dust has settled (one second without updates), then
1640 * go notify the guest.
1641 */
1185fde4 1642 con->ui_info = *info;
ca19ef52
MAL
1643 timer_mod(con->ui_timer,
1644 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
cf1ecc82 1645 return 0;
6f90f3d7
GH
1646}
1647
c78f7137 1648void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1649{
c78f7137 1650 DisplayState *s = con->ds;
284d1c6b 1651 DisplayChangeListener *dcl;
ebced091
MAL
1652 int width = qemu_console_get_width(con, x + w);
1653 int height = qemu_console_get_height(con, y + h);
7c20b4a3
GH
1654
1655 x = MAX(x, 0);
1656 y = MAX(y, 0);
1657 x = MIN(x, width);
1658 y = MIN(y, height);
1659 w = MIN(w, width - x);
1660 h = MIN(h, height - y);
1661
81c0d5a6 1662 if (!qemu_console_is_visible(con)) {
321f048d
GH
1663 return;
1664 }
589089fe 1665 dpy_gfx_update_texture(con, con->surface, x, y, w, h);
7c20b4a3 1666 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1667 if (con != (dcl->con ? dcl->con : active_console)) {
1668 continue;
1669 }
7c20b4a3 1670 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1671 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1672 }
1673 }
1674}
1675
7cd0afe6
TZ
1676void dpy_gfx_update_full(QemuConsole *con)
1677{
ebced091
MAL
1678 int w = qemu_console_get_width(con, 0);
1679 int h = qemu_console_get_height(con, 0);
1680
1681 dpy_gfx_update(con, 0, 0, w, h);
7cd0afe6
TZ
1682}
1683
321f048d
GH
1684void dpy_gfx_replace_surface(QemuConsole *con,
1685 DisplaySurface *surface)
1686{
c821a58e 1687 static const char placeholder_msg[] = "Display output is not active.";
321f048d
GH
1688 DisplayState *s = con->ds;
1689 DisplaySurface *old_surface = con->surface;
284d1c6b 1690 DisplayChangeListener *dcl;
c821a58e
AO
1691 int width;
1692 int height;
1693
1694 if (!surface) {
1695 if (old_surface) {
1696 width = surface_width(old_surface);
1697 height = surface_height(old_surface);
1698 } else {
1699 width = 640;
1700 height = 480;
1701 }
1702
1703 surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
1704 }
321f048d 1705
c821a58e 1706 assert(old_surface != surface);
6905b934 1707
ebced091 1708 con->scanout.kind = SCANOUT_SURFACE;
321f048d 1709 con->surface = surface;
589089fe 1710 dpy_gfx_create_texture(con, surface);
284d1c6b
GH
1711 QLIST_FOREACH(dcl, &s->listeners, next) {
1712 if (con != (dcl->con ? dcl->con : active_console)) {
1713 continue;
1714 }
c84ab0a5 1715 displaychangelistener_gfx_switch(dcl, surface, FALSE);
321f048d 1716 }
589089fe 1717 dpy_gfx_destroy_texture(con, old_surface);
da229ef3 1718 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1719}
1720
49743df3
BH
1721bool dpy_gfx_check_format(QemuConsole *con,
1722 pixman_format_code_t format)
1723{
1724 DisplayChangeListener *dcl;
1725 DisplayState *s = con->ds;
1726
1727 QLIST_FOREACH(dcl, &s->listeners, next) {
1728 if (dcl->con && dcl->con != con) {
1729 /* dcl bound to another console -> skip */
1730 continue;
1731 }
1732 if (dcl->ops->dpy_gfx_check_format) {
1733 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1734 return false;
1735 }
1736 } else {
75ae7c46 1737 /* default is to allow native 32 bpp only */
49743df3
BH
1738 if (format != qemu_default_pixman_format(32, true)) {
1739 return false;
1740 }
1741 }
1742 }
1743 return true;
1744}
1745
6075137d 1746static void dpy_refresh(DisplayState *s)
7c20b4a3 1747{
284d1c6b
GH
1748 DisplayChangeListener *dcl;
1749
7c20b4a3
GH
1750 QLIST_FOREACH(dcl, &s->listeners, next) {
1751 if (dcl->ops->dpy_refresh) {
3f8f1313 1752 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1753 }
1754 }
1755}
1756
c78f7137 1757void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1758{
c78f7137 1759 DisplayState *s = con->ds;
284d1c6b 1760 DisplayChangeListener *dcl;
321f048d 1761
81c0d5a6 1762 if (!qemu_console_is_visible(con)) {
321f048d
GH
1763 return;
1764 }
7c20b4a3 1765 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1766 if (con != (dcl->con ? dcl->con : active_console)) {
1767 continue;
1768 }
7c20b4a3 1769 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1770 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1771 }
1772 }
1773}
1774
c78f7137 1775void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1776{
c78f7137 1777 DisplayState *s = con->ds;
284d1c6b 1778 DisplayChangeListener *dcl;
321f048d 1779
81c0d5a6 1780 if (!qemu_console_is_visible(con)) {
321f048d
GH
1781 return;
1782 }
7c20b4a3 1783 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1784 if (con != (dcl->con ? dcl->con : active_console)) {
1785 continue;
1786 }
7c20b4a3 1787 if (dcl->ops->dpy_text_update) {
bc2ed970 1788 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
1789 }
1790 }
1791}
1792
c78f7137 1793void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1794{
c78f7137 1795 DisplayState *s = con->ds;
9425c004 1796 DisplayChangeListener *dcl;
321f048d 1797
81c0d5a6 1798 if (!qemu_console_is_visible(con)) {
321f048d
GH
1799 return;
1800 }
7c20b4a3 1801 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1802 if (con != (dcl->con ? dcl->con : active_console)) {
1803 continue;
1804 }
7c20b4a3 1805 if (dcl->ops->dpy_text_resize) {
bc2ed970 1806 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1807 }
1808 }
1809}
1810
c78f7137 1811void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 1812{
c78f7137 1813 DisplayState *s = con->ds;
284d1c6b 1814 DisplayChangeListener *dcl;
321f048d 1815
81c0d5a6 1816 if (!qemu_console_is_visible(con)) {
321f048d
GH
1817 return;
1818 }
7c20b4a3 1819 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1820 if (con != (dcl->con ? dcl->con : active_console)) {
1821 continue;
1822 }
7c20b4a3 1823 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1824 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1825 }
1826 }
1827}
1828
c78f7137 1829void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 1830{
c78f7137 1831 DisplayState *s = con->ds;
284d1c6b 1832 DisplayChangeListener *dcl;
321f048d 1833
81c0d5a6 1834 if (!qemu_console_is_visible(con)) {
321f048d
GH
1835 return;
1836 }
7c20b4a3 1837 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1838 if (con != (dcl->con ? dcl->con : active_console)) {
1839 continue;
1840 }
7c20b4a3 1841 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1842 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1843 }
1844 }
1845}
1846
c78f7137 1847bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1848{
c78f7137 1849 DisplayState *s = con->ds;
284d1c6b
GH
1850 DisplayChangeListener *dcl;
1851
7c20b4a3
GH
1852 QLIST_FOREACH(dcl, &s->listeners, next) {
1853 if (dcl->ops->dpy_cursor_define) {
1854 return true;
1855 }
1856 }
1857 return false;
1858}
1859
06020b95
GH
1860QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1861 struct QEMUGLParams *qparams)
1862{
1863 assert(con->gl);
1864 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1865}
1866
1867void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1868{
1869 assert(con->gl);
1870 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1871}
1872
1873int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1874{
1875 assert(con->gl);
1876 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1877}
1878
eaa92c76
GH
1879void dpy_gl_scanout_disable(QemuConsole *con)
1880{
7cc712e9
MAL
1881 DisplayState *s = con->ds;
1882 DisplayChangeListener *dcl;
1883
ebced091
MAL
1884 if (con->scanout.kind != SCANOUT_SURFACE) {
1885 con->scanout.kind = SCANOUT_NONE;
1886 }
7cc712e9 1887 QLIST_FOREACH(dcl, &s->listeners, next) {
a9fbce5e
MAL
1888 if (dcl->ops->dpy_gl_scanout_disable) {
1889 dcl->ops->dpy_gl_scanout_disable(dcl);
1890 }
7cc712e9 1891 }
eaa92c76
GH
1892}
1893
f4c36bda
GH
1894void dpy_gl_scanout_texture(QemuConsole *con,
1895 uint32_t backing_id,
1896 bool backing_y_0_top,
1897 uint32_t backing_width,
1898 uint32_t backing_height,
1899 uint32_t x, uint32_t y,
1900 uint32_t width, uint32_t height)
06020b95 1901{
7cc712e9
MAL
1902 DisplayState *s = con->ds;
1903 DisplayChangeListener *dcl;
1904
ebced091
MAL
1905 con->scanout.kind = SCANOUT_TEXTURE;
1906 con->scanout.texture = (ScanoutTexture) {
1907 backing_id, backing_y_0_top, backing_width, backing_height,
1908 x, y, width, height
1909 };
7cc712e9 1910 QLIST_FOREACH(dcl, &s->listeners, next) {
a9fbce5e
MAL
1911 if (dcl->ops->dpy_gl_scanout_texture) {
1912 dcl->ops->dpy_gl_scanout_texture(dcl, backing_id,
1913 backing_y_0_top,
1914 backing_width, backing_height,
1915 x, y, width, height);
1916 }
7cc712e9 1917 }
06020b95
GH
1918}
1919
4133fa71
GH
1920void dpy_gl_scanout_dmabuf(QemuConsole *con,
1921 QemuDmaBuf *dmabuf)
1922{
7cc712e9
MAL
1923 DisplayState *s = con->ds;
1924 DisplayChangeListener *dcl;
1925
ebced091
MAL
1926 con->scanout.kind = SCANOUT_DMABUF;
1927 con->scanout.dmabuf = dmabuf;
7cc712e9 1928 QLIST_FOREACH(dcl, &s->listeners, next) {
a9fbce5e
MAL
1929 if (dcl->ops->dpy_gl_scanout_dmabuf) {
1930 dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
1931 }
7cc712e9 1932 }
4133fa71
GH
1933}
1934
6e1f2cb5
GH
1935void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1936 bool have_hot, uint32_t hot_x, uint32_t hot_y)
4133fa71 1937{
7cc712e9
MAL
1938 DisplayState *s = con->ds;
1939 DisplayChangeListener *dcl;
4133fa71 1940
7cc712e9
MAL
1941 QLIST_FOREACH(dcl, &s->listeners, next) {
1942 if (dcl->ops->dpy_gl_cursor_dmabuf) {
1943 dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
6e1f2cb5 1944 have_hot, hot_x, hot_y);
7cc712e9 1945 }
6e1f2cb5
GH
1946 }
1947}
1948
1949void dpy_gl_cursor_position(QemuConsole *con,
1950 uint32_t pos_x, uint32_t pos_y)
1951{
7cc712e9
MAL
1952 DisplayState *s = con->ds;
1953 DisplayChangeListener *dcl;
6e1f2cb5 1954
7cc712e9
MAL
1955 QLIST_FOREACH(dcl, &s->listeners, next) {
1956 if (dcl->ops->dpy_gl_cursor_position) {
1957 dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
1958 }
4133fa71
GH
1959 }
1960}
1961
1962void dpy_gl_release_dmabuf(QemuConsole *con,
1963 QemuDmaBuf *dmabuf)
1964{
7cc712e9
MAL
1965 DisplayState *s = con->ds;
1966 DisplayChangeListener *dcl;
4133fa71 1967
7cc712e9
MAL
1968 QLIST_FOREACH(dcl, &s->listeners, next) {
1969 if (dcl->ops->dpy_gl_release_dmabuf) {
1970 dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
1971 }
4133fa71
GH
1972 }
1973}
1974
06020b95
GH
1975void dpy_gl_update(QemuConsole *con,
1976 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1977{
7cc712e9
MAL
1978 DisplayState *s = con->ds;
1979 DisplayChangeListener *dcl;
1980
06020b95 1981 assert(con->gl);
f6413cbf
MAL
1982
1983 graphic_hw_gl_block(con, true);
7cc712e9 1984 QLIST_FOREACH(dcl, &s->listeners, next) {
a9fbce5e
MAL
1985 if (dcl->ops->dpy_gl_update) {
1986 dcl->ops->dpy_gl_update(dcl, x, y, w, h);
1987 }
7cc712e9 1988 }
f6413cbf 1989 graphic_hw_gl_block(con, false);
06020b95
GH
1990}
1991
98b50080
PB
1992/***********************************************************/
1993/* register display */
1994
64840c66
GH
1995/* console.c internal use only */
1996static DisplayState *get_alloc_displaystate(void)
98b50080 1997{
64840c66
GH
1998 if (!display_state) {
1999 display_state = g_new0(DisplayState, 1);
aea7947c
GH
2000 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2001 text_console_update_cursor, NULL);
64840c66
GH
2002 }
2003 return display_state;
98b50080
PB
2004}
2005
64840c66
GH
2006/*
2007 * Called by main(), after creating QemuConsoles
2008 * and before initializing ui (sdl/vnc/...).
2009 */
2010DisplayState *init_displaystate(void)
98b50080 2011{
43f420f8 2012 gchar *name;
cd6cd8fa 2013 QemuConsole *con;
64840c66 2014
333cb18f 2015 get_alloc_displaystate();
cd6cd8fa
GH
2016 QTAILQ_FOREACH(con, &consoles, next) {
2017 if (con->console_type != GRAPHIC_CONSOLE &&
2018 con->ds == NULL) {
2019 text_console_do_init(con->chr, display_state);
64840c66 2020 }
43f420f8
GH
2021
2022 /* Hook up into the qom tree here (not in new_console()), once
2023 * all QemuConsoles are created and the order / numbering
2024 * doesn't change any more */
cd6cd8fa 2025 name = g_strdup_printf("console[%d]", con->index);
43f420f8 2026 object_property_add_child(container_get(object_get_root(), "/backend"),
d2623129 2027 name, OBJECT(con));
43f420f8 2028 g_free(name);
64840c66
GH
2029 }
2030
98b50080
PB
2031 return display_state;
2032}
2033
1c1f9498
GH
2034void graphic_console_set_hwops(QemuConsole *con,
2035 const GraphicHwOps *hw_ops,
2036 void *opaque)
2037{
2038 con->hw_ops = hw_ops;
2039 con->hw = opaque;
2040}
2041
5643706a 2042QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 2043 const GraphicHwOps *hw_ops,
c78f7137 2044 void *opaque)
95219897 2045{
521a580d
GH
2046 static const char noinit[] =
2047 "Guest has not initialized the display (yet).";
64840c66
GH
2048 int width = 640;
2049 int height = 480;
76ffb0b4 2050 QemuConsole *s;
3023f332 2051 DisplayState *ds;
9588d67e 2052 DisplaySurface *surface;
f0f2f976 2053
64840c66 2054 ds = get_alloc_displaystate();
9588d67e
GH
2055 s = qemu_console_lookup_unused();
2056 if (s) {
2057 trace_console_gfx_reuse(s->index);
ebced091
MAL
2058 width = qemu_console_get_width(s, 0);
2059 height = qemu_console_get_height(s, 0);
9588d67e
GH
2060 } else {
2061 trace_console_gfx_new();
2062 s = new_console(ds, GRAPHIC_CONSOLE, head);
2063 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2064 dpy_set_ui_info_timer, s);
2065 }
1c1f9498 2066 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 2067 if (dev) {
5325cc34 2068 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
afff2b15 2069 &error_abort);
aa2beaa1 2070 }
3023f332 2071
b5a087b0 2072 surface = qemu_create_placeholder_surface(width, height, noinit);
9588d67e 2073 dpy_gfx_replace_surface(s, surface);
a9b1e471
MAL
2074 s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
2075 graphic_hw_gl_unblock_timer, s);
c78f7137 2076 return s;
e7f0ad58
FB
2077}
2078
9588d67e
GH
2079static const GraphicHwOps unused_ops = {
2080 /* no callbacks */
2081};
2082
2083void graphic_console_close(QemuConsole *con)
2084{
2085 static const char unplugged[] =
2086 "Guest display has been unplugged";
2087 DisplaySurface *surface;
ebced091
MAL
2088 int width = qemu_console_get_width(con, 640);
2089 int height = qemu_console_get_height(con, 480);
9588d67e
GH
2090
2091 trace_console_gfx_close(con->index);
5325cc34 2092 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
9588d67e
GH
2093 graphic_console_set_hwops(con, &unused_ops, NULL);
2094
2095 if (con->gl) {
2096 dpy_gl_scanout_disable(con);
2097 }
b5a087b0 2098 surface = qemu_create_placeholder_surface(width, height, unplugged);
9588d67e
GH
2099 dpy_gfx_replace_surface(con, surface);
2100}
2101
284d1c6b
GH
2102QemuConsole *qemu_console_lookup_by_index(unsigned int index)
2103{
cd6cd8fa
GH
2104 QemuConsole *con;
2105
2106 QTAILQ_FOREACH(con, &consoles, next) {
2107 if (con->index == index) {
2108 return con;
2109 }
284d1c6b 2110 }
cd6cd8fa 2111 return NULL;
284d1c6b
GH
2112}
2113
5643706a 2114QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 2115{
cd6cd8fa 2116 QemuConsole *con;
14a93649 2117 Object *obj;
5643706a 2118 uint32_t h;
14a93649 2119
cd6cd8fa
GH
2120 QTAILQ_FOREACH(con, &consoles, next) {
2121 obj = object_property_get_link(OBJECT(con),
afff2b15 2122 "device", &error_abort);
5643706a
GH
2123 if (DEVICE(obj) != dev) {
2124 continue;
2125 }
cd6cd8fa 2126 h = object_property_get_uint(OBJECT(con),
ad664c1d 2127 "head", &error_abort);
5643706a
GH
2128 if (h != head) {
2129 continue;
14a93649 2130 }
cd6cd8fa 2131 return con;
14a93649
GH
2132 }
2133 return NULL;
2134}
2135
f2c1d54c
GH
2136QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
2137 uint32_t head, Error **errp)
2138{
2139 DeviceState *dev;
2140 QemuConsole *con;
2141
2142 dev = qdev_find_recursive(sysbus_get_default(), device_id);
2143 if (dev == NULL) {
2144 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2145 "Device '%s' not found", device_id);
2146 return NULL;
2147 }
2148
2149 con = qemu_console_lookup_by_device(dev, head);
2150 if (con == NULL) {
2151 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
2152 device_id, head);
2153 return NULL;
2154 }
2155
2156 return con;
2157}
2158
9588d67e
GH
2159QemuConsole *qemu_console_lookup_unused(void)
2160{
cd6cd8fa 2161 QemuConsole *con;
9588d67e 2162 Object *obj;
9588d67e 2163
cd6cd8fa
GH
2164 QTAILQ_FOREACH(con, &consoles, next) {
2165 if (con->hw_ops != &unused_ops) {
9588d67e
GH
2166 continue;
2167 }
cd6cd8fa 2168 obj = object_property_get_link(OBJECT(con),
9588d67e
GH
2169 "device", &error_abort);
2170 if (obj != NULL) {
2171 continue;
2172 }
cd6cd8fa 2173 return con;
9588d67e
GH
2174 }
2175 return NULL;
2176}
2177
81c0d5a6 2178bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 2179{
284d1c6b 2180 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
2181}
2182
81c0d5a6 2183bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 2184{
81c0d5a6
GH
2185 if (con == NULL) {
2186 con = active_console;
2187 }
2188 return con && (con->console_type == GRAPHIC_CONSOLE);
2189}
2190
2191bool qemu_console_is_fixedsize(QemuConsole *con)
2192{
2193 if (con == NULL) {
2194 con = active_console;
2195 }
2196 return con && (con->console_type != TEXT_CONSOLE);
c21bbcfa
AZ
2197}
2198
f607867c
GH
2199bool qemu_console_is_gl_blocked(QemuConsole *con)
2200{
2201 assert(con != NULL);
2202 return con->gl_block;
2203}
2204
779ce88f
GH
2205char *qemu_console_get_label(QemuConsole *con)
2206{
2207 if (con->console_type == GRAPHIC_CONSOLE) {
2208 if (con->device) {
2209 return g_strdup(object_get_typename(con->device));
2210 }
2211 return g_strdup("VGA");
2212 } else {
2213 if (con->chr && con->chr->label) {
2214 return g_strdup(con->chr->label);
2215 }
2216 return g_strdup_printf("vc%d", con->index);
2217 }
2218}
2219
d4c85337
GH
2220int qemu_console_get_index(QemuConsole *con)
2221{
2222 if (con == NULL) {
2223 con = active_console;
2224 }
2225 return con ? con->index : -1;
2226}
2227
5643706a
GH
2228uint32_t qemu_console_get_head(QemuConsole *con)
2229{
2230 if (con == NULL) {
2231 con = active_console;
2232 }
2233 return con ? con->head : -1;
2234}
2235
d4c85337
GH
2236int qemu_console_get_width(QemuConsole *con, int fallback)
2237{
2238 if (con == NULL) {
2239 con = active_console;
2240 }
ebced091
MAL
2241 if (con == NULL) {
2242 return fallback;
2243 }
2244 switch (con->scanout.kind) {
2245 case SCANOUT_DMABUF:
2246 return con->scanout.dmabuf->width;
2247 case SCANOUT_TEXTURE:
2248 return con->scanout.texture.width;
2249 case SCANOUT_SURFACE:
2250 return surface_width(con->surface);
2251 default:
2252 return fallback;
2253 }
d4c85337
GH
2254}
2255
2256int qemu_console_get_height(QemuConsole *con, int fallback)
2257{
2258 if (con == NULL) {
2259 con = active_console;
2260 }
ebced091
MAL
2261 if (con == NULL) {
2262 return fallback;
2263 }
2264 switch (con->scanout.kind) {
2265 case SCANOUT_DMABUF:
2266 return con->scanout.dmabuf->height;
2267 case SCANOUT_TEXTURE:
2268 return con->scanout.texture.height;
2269 case SCANOUT_SURFACE:
2270 return surface_height(con->surface);
2271 default:
2272 return fallback;
2273 }
d4c85337
GH
2274}
2275
ec222519
VR
2276static void vc_chr_accept_input(Chardev *chr)
2277{
2278 VCChardev *drv = VC_CHARDEV(chr);
2279 QemuConsole *s = drv->console;
2280
2281 kbd_send_chars(s);
2282}
2283
5bf5adae 2284static void vc_chr_set_echo(Chardev *chr, bool echo)
4104833f 2285{
777357d7 2286 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 2287 QemuConsole *s = drv->console;
4104833f
PB
2288
2289 s->echo = echo;
2290}
2291
aea7947c
GH
2292static void text_console_update_cursor_timer(void)
2293{
2294 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2295 + CONSOLE_CURSOR_PERIOD / 2);
2296}
2297
bf1bed81
JK
2298static void text_console_update_cursor(void *opaque)
2299{
aea7947c 2300 QemuConsole *s;
cd6cd8fa 2301 int count = 0;
aea7947c
GH
2302
2303 cursor_visible_phase = !cursor_visible_phase;
bf1bed81 2304
cd6cd8fa 2305 QTAILQ_FOREACH(s, &consoles, next) {
aea7947c
GH
2306 if (qemu_console_is_graphic(s) ||
2307 !qemu_console_is_visible(s)) {
2308 continue;
2309 }
2310 count++;
2311 graphic_hw_invalidate(s);
2312 }
2313
2314 if (count) {
2315 text_console_update_cursor_timer();
2316 }
bf1bed81
JK
2317}
2318
380cd056
GH
2319static const GraphicHwOps text_console_ops = {
2320 .invalidate = text_console_invalidate,
2321 .text_update = text_console_update,
2322};
2323
0ec7b3e7 2324static void text_console_do_init(Chardev *chr, DisplayState *ds)
e7f0ad58 2325{
777357d7 2326 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 2327 QemuConsole *s = drv->console;
36671fbd
GH
2328 int g_width = 80 * FONT_WIDTH;
2329 int g_height = 24 * FONT_HEIGHT;
6d6f7c28 2330
0c9d0641 2331 fifo8_create(&s->out_fifo, 16);
3023f332 2332 s->ds = ds;
3b46e624 2333
e7f0ad58
FB
2334 s->y_displayed = 0;
2335 s->y_base = 0;
2336 s->total_height = DEFAULT_BACKSCROLL;
2337 s->x = 0;
2338 s->y = 0;
ebced091
MAL
2339 if (s->scanout.kind != SCANOUT_SURFACE) {
2340 if (active_console && active_console->scanout.kind == SCANOUT_SURFACE) {
2341 g_width = qemu_console_get_width(active_console, g_width);
2342 g_height = qemu_console_get_height(active_console, g_height);
321f048d 2343 }
36671fbd 2344 s->surface = qemu_create_displaysurface(g_width, g_height);
ebced091 2345 s->scanout.kind = SCANOUT_SURFACE;
491e114a 2346 }
6d6f7c28 2347
380cd056 2348 s->hw_ops = &text_console_ops;
4d3b6f6e
AZ
2349 s->hw = s;
2350
6d6f7c28
PB
2351 /* Set text attribute defaults */
2352 s->t_attrib_default.bold = 0;
2353 s->t_attrib_default.uline = 0;
2354 s->t_attrib_default.blink = 0;
2355 s->t_attrib_default.invers = 0;
2356 s->t_attrib_default.unvisible = 0;
4083733d
OH
2357 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2358 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
2359 /* set current text attributes to default */
2360 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
2361 text_console_resize(s);
2362
51bfa4d3 2363 if (chr->label) {
18595181 2364 char *msg;
51bfa4d3 2365
4083733d 2366 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
18595181
GH
2367 msg = g_strdup_printf("%s console\r\n", chr->label);
2368 vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
2369 g_free(msg);
735ba588 2370 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
2371 }
2372
63618135 2373 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
e7f0ad58 2374}
c60e08d9 2375
777357d7
MAL
2376static void vc_chr_open(Chardev *chr,
2377 ChardevBackend *backend,
2378 bool *be_opened,
2379 Error **errp)
2796dae0 2380{
6f974c84 2381 ChardevVC *vc = backend->u.vc.data;
777357d7 2382 VCChardev *drv = VC_CHARDEV(chr);
76ffb0b4 2383 QemuConsole *s;
702ec69c
GH
2384 unsigned width = 0;
2385 unsigned height = 0;
2796dae0 2386
702ec69c
GH
2387 if (vc->has_width) {
2388 width = vc->width;
2389 } else if (vc->has_cols) {
2390 width = vc->cols * FONT_WIDTH;
2391 }
491e114a 2392
702ec69c
GH
2393 if (vc->has_height) {
2394 height = vc->height;
2395 } else if (vc->has_rows) {
2396 height = vc->rows * FONT_HEIGHT;
2397 }
491e114a 2398
437fe106 2399 trace_console_txt_new(width, height);
491e114a 2400 if (width == 0 || height == 0) {
afff2b15 2401 s = new_console(NULL, TEXT_CONSOLE, 0);
491e114a 2402 } else {
afff2b15 2403 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
ebced091 2404 s->scanout.kind = SCANOUT_SURFACE;
36671fbd 2405 s->surface = qemu_create_displaysurface(width, height);
491e114a
PB
2406 }
2407
2408 if (!s) {
fa19d025 2409 error_setg(errp, "cannot create text console");
777357d7 2410 return;
491e114a
PB
2411 }
2412
2413 s->chr = chr;
41ac54b2 2414 drv->console = s;
64840c66
GH
2415
2416 if (display_state) {
2417 text_console_do_init(chr, display_state);
2418 }
2796dae0 2419
82878dac
MAL
2420 /* console/chardev init sometimes completes elsewhere in a 2nd
2421 * stage, so defer OPENED events until they are fully initialized
2422 */
2423 *be_opened = false;
d82831db
AL
2424}
2425
c78f7137 2426void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 2427{
cb8962c1 2428 DisplaySurface *surface;
321f048d
GH
2429
2430 assert(s->console_type == GRAPHIC_CONSOLE);
cd958edb 2431
cb8962c1
MAL
2432 if (qemu_console_get_width(s, -1) == width &&
2433 qemu_console_get_height(s, -1) == height) {
cd958edb
MAL
2434 return;
2435 }
2436
321f048d
GH
2437 surface = qemu_create_displaysurface(width, height);
2438 dpy_gfx_replace_surface(s, surface);
c60e08d9 2439}
38334f76 2440
c78f7137
GH
2441DisplaySurface *qemu_console_surface(QemuConsole *console)
2442{
ebced091
MAL
2443 switch (console->scanout.kind) {
2444 case SCANOUT_SURFACE:
2445 return console->surface;
2446 default:
2447 return NULL;
2448 }
c78f7137
GH
2449}
2450
0da2ea1b 2451PixelFormat qemu_default_pixelformat(int bpp)
2452{
56bd9ea1
GH
2453 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2454 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
2455 return pf;
2456}
01f45d98 2457
db71589f
GH
2458static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2459
2460void qemu_display_register(QemuDisplay *ui)
2461{
2462 assert(ui->type < DISPLAY_TYPE__MAX);
2463 dpys[ui->type] = ui;
2464}
2465
898f9d41
GH
2466bool qemu_display_find_default(DisplayOptions *opts)
2467{
2468 static DisplayType prio[] = {
66c2207f 2469#if defined(CONFIG_GTK)
898f9d41 2470 DISPLAY_TYPE_GTK,
66c2207f
TH
2471#endif
2472#if defined(CONFIG_SDL)
898f9d41 2473 DISPLAY_TYPE_SDL,
66c2207f
TH
2474#endif
2475#if defined(CONFIG_COCOA)
898f9d41 2476 DISPLAY_TYPE_COCOA
66c2207f 2477#endif
898f9d41
GH
2478 };
2479 int i;
2480
66c2207f 2481 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
61b4d9a2 2482 if (dpys[prio[i]] == NULL) {
c809d1d2 2483 ui_module_load_one(DisplayType_str(prio[i]));
61b4d9a2 2484 }
898f9d41
GH
2485 if (dpys[prio[i]] == NULL) {
2486 continue;
2487 }
2488 opts->type = prio[i];
2489 return true;
2490 }
2491 return false;
2492}
2493
db71589f
GH
2494void qemu_display_early_init(DisplayOptions *opts)
2495{
2496 assert(opts->type < DISPLAY_TYPE__MAX);
2497 if (opts->type == DISPLAY_TYPE_NONE) {
2498 return;
2499 }
61b4d9a2 2500 if (dpys[opts->type] == NULL) {
c809d1d2 2501 ui_module_load_one(DisplayType_str(opts->type));
61b4d9a2 2502 }
db71589f
GH
2503 if (dpys[opts->type] == NULL) {
2504 error_report("Display '%s' is not available.",
c809d1d2 2505 DisplayType_str(opts->type));
db71589f
GH
2506 exit(1);
2507 }
2508 if (dpys[opts->type]->early_init) {
2509 dpys[opts->type]->early_init(opts);
2510 }
2511}
2512
2513void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2514{
2515 assert(opts->type < DISPLAY_TYPE__MAX);
2516 if (opts->type == DISPLAY_TYPE_NONE) {
2517 return;
2518 }
2519 assert(dpys[opts->type] != NULL);
2520 dpys[opts->type]->init(ds, opts);
2521}
2522
c388f408
TH
2523void qemu_display_help(void)
2524{
2525 int idx;
2526
2527 printf("Available display backend types:\n");
a1e8853e 2528 printf("none\n");
c388f408
TH
2529 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
2530 if (!dpys[idx]) {
2531 ui_module_load_one(DisplayType_str(idx));
2532 }
2533 if (dpys[idx]) {
2534 printf("%s\n", DisplayType_str(dpys[idx]->type));
2535 }
2536 }
2537}
2538
6f974c84 2539void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
702ec69c
GH
2540{
2541 int val;
21a933ea 2542 ChardevVC *vc;
702ec69c 2543
0b663b7d 2544 backend->type = CHARDEV_BACKEND_KIND_VC;
32bafa8f 2545 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
21a933ea 2546 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
702ec69c
GH
2547
2548 val = qemu_opt_get_number(opts, "width", 0);
2549 if (val != 0) {
21a933ea
EB
2550 vc->has_width = true;
2551 vc->width = val;
702ec69c
GH
2552 }
2553
2554 val = qemu_opt_get_number(opts, "height", 0);
2555 if (val != 0) {
21a933ea
EB
2556 vc->has_height = true;
2557 vc->height = val;
702ec69c
GH
2558 }
2559
2560 val = qemu_opt_get_number(opts, "cols", 0);
2561 if (val != 0) {
21a933ea
EB
2562 vc->has_cols = true;
2563 vc->cols = val;
702ec69c
GH
2564 }
2565
2566 val = qemu_opt_get_number(opts, "rows", 0);
2567 if (val != 0) {
21a933ea
EB
2568 vc->has_rows = true;
2569 vc->rows = val;
702ec69c
GH
2570 }
2571}
2572
95be0669
GH
2573static const TypeInfo qemu_console_info = {
2574 .name = TYPE_QEMU_CONSOLE,
2575 .parent = TYPE_OBJECT,
2576 .instance_size = sizeof(QemuConsole),
2577 .class_size = sizeof(QemuConsoleClass),
2578};
2579
777357d7
MAL
2580static void char_vc_class_init(ObjectClass *oc, void *data)
2581{
2582 ChardevClass *cc = CHARDEV_CLASS(oc);
2583
88cace9f 2584 cc->parse = qemu_chr_parse_vc;
777357d7
MAL
2585 cc->open = vc_chr_open;
2586 cc->chr_write = vc_chr_write;
ec222519 2587 cc->chr_accept_input = vc_chr_accept_input;
777357d7
MAL
2588 cc->chr_set_echo = vc_chr_set_echo;
2589}
2590
2591static const TypeInfo char_vc_type_info = {
2592 .name = TYPE_CHARDEV_VC,
2593 .parent = TYPE_CHARDEV,
0ec7b3e7 2594 .instance_size = sizeof(VCChardev),
777357d7
MAL
2595 .class_init = char_vc_class_init,
2596};
2597
2598void qemu_console_early_init(void)
2599{
2600 /* set the default vc driver */
2601 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2602 type_register(&char_vc_type_info);
777357d7
MAL
2603 }
2604}
2605
01f45d98
AL
2606static void register_types(void)
2607{
95be0669 2608 type_register_static(&qemu_console_info);
01f45d98
AL
2609}
2610
2611type_init(register_types);