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