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