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