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