]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
ui: associate GL context outside of display listener registration
[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
4f418149
MAL
1446void qemu_console_set_display_gl_ctx(QemuConsole *con,
1447 DisplayChangeListener *dcl)
1448{
1449 /* display has opengl support */
1450 assert(dcl->con);
1451 if (dcl->con->gl) {
1452 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1453 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1454 exit(1);
1455 }
1456 dcl->con->gl = dcl;
1457}
1458
5209089f 1459void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 1460{
521a580d
GH
1461 static const char nodev[] =
1462 "This VM has no graphic display device.";
d3002b04 1463 static DisplaySurface *dummy;
284d1c6b
GH
1464 QemuConsole *con;
1465
e0665c3b
MAL
1466 assert(!dcl->ds);
1467
ac32b2ff
MAL
1468 if (dcl->con && dcl->con->gl &&
1469 dcl->con->gl != dcl) {
1470 error_report("Display %s is incompatible with the GL context",
1471 dcl->ops->dpy_name);
1472 exit(1);
06020b95
GH
1473 }
1474
f9734d5d
MA
1475 if (dcl->con) {
1476 dpy_compatible_with(dcl->con, dcl, &error_fatal);
5983fdf1
MAL
1477 }
1478
7c20b4a3 1479 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
1480 dcl->ds = get_alloc_displaystate();
1481 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1482 gui_setup_refresh(dcl->ds);
284d1c6b
GH
1483 if (dcl->con) {
1484 dcl->con->dcls++;
1485 con = dcl->con;
1486 } else {
1487 con = active_console;
1488 }
d3002b04
GH
1489 if (dcl->ops->dpy_gfx_switch) {
1490 if (con) {
1491 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1492 } else {
1493 if (!dummy) {
b5a087b0 1494 dummy = qemu_create_placeholder_surface(640, 480, nodev);
d3002b04
GH
1495 }
1496 dcl->ops->dpy_gfx_switch(dcl, dummy);
1497 }
7c20b4a3 1498 }
aea7947c 1499 text_console_update_cursor(NULL);
7c20b4a3
GH
1500}
1501
0f7b2864
GH
1502void update_displaychangelistener(DisplayChangeListener *dcl,
1503 uint64_t interval)
1504{
1505 DisplayState *ds = dcl->ds;
1506
1507 dcl->update_interval = interval;
1508 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 1509 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
1510 }
1511}
1512
7c20b4a3
GH
1513void unregister_displaychangelistener(DisplayChangeListener *dcl)
1514{
1515 DisplayState *ds = dcl->ds;
1516 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
1517 if (dcl->con) {
1518 dcl->con->dcls--;
1519 }
7c20b4a3 1520 QLIST_REMOVE(dcl, next);
777c5f1e 1521 dcl->ds = NULL;
7c20b4a3
GH
1522 gui_setup_refresh(ds);
1523}
1524
cf1ecc82
GH
1525static void dpy_set_ui_info_timer(void *opaque)
1526{
1527 QemuConsole *con = opaque;
1528
1529 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1530}
1531
b7fb49f0
GH
1532bool dpy_ui_info_supported(QemuConsole *con)
1533{
5c4b107f
GH
1534 if (con == NULL) {
1535 con = active_console;
1536 }
1537
b7fb49f0
GH
1538 return con->hw_ops->ui_info != NULL;
1539}
1540
5eaf1e48
MAL
1541const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
1542{
5c4b107f
GH
1543 if (con == NULL) {
1544 con = active_console;
1545 }
5eaf1e48
MAL
1546
1547 return &con->ui_info;
1548}
1549
ca19ef52 1550int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
6f90f3d7 1551{
5c4b107f
GH
1552 if (con == NULL) {
1553 con = active_console;
1554 }
1185fde4 1555
b7fb49f0 1556 if (!dpy_ui_info_supported(con)) {
cf1ecc82 1557 return -1;
6f90f3d7 1558 }
1185fde4
GH
1559 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1560 /* nothing changed -- ignore */
1561 return 0;
1562 }
cf1ecc82
GH
1563
1564 /*
1565 * Typically we get a flood of these as the user resizes the window.
1566 * Wait until the dust has settled (one second without updates), then
1567 * go notify the guest.
1568 */
1185fde4 1569 con->ui_info = *info;
ca19ef52
MAL
1570 timer_mod(con->ui_timer,
1571 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
cf1ecc82 1572 return 0;
6f90f3d7
GH
1573}
1574
c78f7137 1575void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1576{
c78f7137 1577 DisplayState *s = con->ds;
284d1c6b 1578 DisplayChangeListener *dcl;
06020b95
GH
1579 int width = w;
1580 int height = h;
7c20b4a3 1581
06020b95
GH
1582 if (con->surface) {
1583 width = surface_width(con->surface);
1584 height = surface_height(con->surface);
1585 }
7c20b4a3
GH
1586 x = MAX(x, 0);
1587 y = MAX(y, 0);
1588 x = MIN(x, width);
1589 y = MIN(y, height);
1590 w = MIN(w, width - x);
1591 h = MIN(h, height - y);
1592
81c0d5a6 1593 if (!qemu_console_is_visible(con)) {
321f048d
GH
1594 return;
1595 }
7c20b4a3 1596 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1597 if (con != (dcl->con ? dcl->con : active_console)) {
1598 continue;
1599 }
7c20b4a3 1600 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1601 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1602 }
1603 }
1604}
1605
7cd0afe6
TZ
1606void dpy_gfx_update_full(QemuConsole *con)
1607{
1608 if (!con->surface) {
1609 return;
1610 }
1611 dpy_gfx_update(con, 0, 0,
1612 surface_width(con->surface),
1613 surface_height(con->surface));
1614}
1615
321f048d
GH
1616void dpy_gfx_replace_surface(QemuConsole *con,
1617 DisplaySurface *surface)
1618{
c821a58e 1619 static const char placeholder_msg[] = "Display output is not active.";
321f048d
GH
1620 DisplayState *s = con->ds;
1621 DisplaySurface *old_surface = con->surface;
284d1c6b 1622 DisplayChangeListener *dcl;
c821a58e
AO
1623 int width;
1624 int height;
1625
1626 if (!surface) {
1627 if (old_surface) {
1628 width = surface_width(old_surface);
1629 height = surface_height(old_surface);
1630 } else {
1631 width = 640;
1632 height = 480;
1633 }
1634
1635 surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
1636 }
321f048d 1637
c821a58e 1638 assert(old_surface != surface);
6905b934 1639
321f048d 1640 con->surface = surface;
284d1c6b
GH
1641 QLIST_FOREACH(dcl, &s->listeners, next) {
1642 if (con != (dcl->con ? dcl->con : active_console)) {
1643 continue;
1644 }
1645 if (dcl->ops->dpy_gfx_switch) {
1646 dcl->ops->dpy_gfx_switch(dcl, surface);
1647 }
321f048d 1648 }
da229ef3 1649 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1650}
1651
49743df3
BH
1652bool dpy_gfx_check_format(QemuConsole *con,
1653 pixman_format_code_t format)
1654{
1655 DisplayChangeListener *dcl;
1656 DisplayState *s = con->ds;
1657
1658 QLIST_FOREACH(dcl, &s->listeners, next) {
1659 if (dcl->con && dcl->con != con) {
1660 /* dcl bound to another console -> skip */
1661 continue;
1662 }
1663 if (dcl->ops->dpy_gfx_check_format) {
1664 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1665 return false;
1666 }
1667 } else {
75ae7c46 1668 /* default is to allow native 32 bpp only */
49743df3
BH
1669 if (format != qemu_default_pixman_format(32, true)) {
1670 return false;
1671 }
1672 }
1673 }
1674 return true;
1675}
1676
6075137d 1677static void dpy_refresh(DisplayState *s)
7c20b4a3 1678{
284d1c6b
GH
1679 DisplayChangeListener *dcl;
1680
7c20b4a3
GH
1681 QLIST_FOREACH(dcl, &s->listeners, next) {
1682 if (dcl->ops->dpy_refresh) {
3f8f1313 1683 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1684 }
1685 }
1686}
1687
c78f7137 1688void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1689{
c78f7137 1690 DisplayState *s = con->ds;
284d1c6b 1691 DisplayChangeListener *dcl;
321f048d 1692
81c0d5a6 1693 if (!qemu_console_is_visible(con)) {
321f048d
GH
1694 return;
1695 }
7c20b4a3 1696 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1697 if (con != (dcl->con ? dcl->con : active_console)) {
1698 continue;
1699 }
7c20b4a3 1700 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1701 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1702 }
1703 }
1704}
1705
c78f7137 1706void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1707{
c78f7137 1708 DisplayState *s = con->ds;
284d1c6b 1709 DisplayChangeListener *dcl;
321f048d 1710
81c0d5a6 1711 if (!qemu_console_is_visible(con)) {
321f048d
GH
1712 return;
1713 }
7c20b4a3 1714 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1715 if (con != (dcl->con ? dcl->con : active_console)) {
1716 continue;
1717 }
7c20b4a3 1718 if (dcl->ops->dpy_text_update) {
bc2ed970 1719 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
1720 }
1721 }
1722}
1723
c78f7137 1724void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1725{
c78f7137 1726 DisplayState *s = con->ds;
9425c004 1727 DisplayChangeListener *dcl;
321f048d 1728
81c0d5a6 1729 if (!qemu_console_is_visible(con)) {
321f048d
GH
1730 return;
1731 }
7c20b4a3 1732 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1733 if (con != (dcl->con ? dcl->con : active_console)) {
1734 continue;
1735 }
7c20b4a3 1736 if (dcl->ops->dpy_text_resize) {
bc2ed970 1737 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1738 }
1739 }
1740}
1741
c78f7137 1742void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 1743{
c78f7137 1744 DisplayState *s = con->ds;
284d1c6b 1745 DisplayChangeListener *dcl;
321f048d 1746
81c0d5a6 1747 if (!qemu_console_is_visible(con)) {
321f048d
GH
1748 return;
1749 }
7c20b4a3 1750 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1751 if (con != (dcl->con ? dcl->con : active_console)) {
1752 continue;
1753 }
7c20b4a3 1754 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1755 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1756 }
1757 }
1758}
1759
c78f7137 1760void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 1761{
c78f7137 1762 DisplayState *s = con->ds;
284d1c6b 1763 DisplayChangeListener *dcl;
321f048d 1764
81c0d5a6 1765 if (!qemu_console_is_visible(con)) {
321f048d
GH
1766 return;
1767 }
7c20b4a3 1768 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1769 if (con != (dcl->con ? dcl->con : active_console)) {
1770 continue;
1771 }
7c20b4a3 1772 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1773 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1774 }
1775 }
1776}
1777
c78f7137 1778bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1779{
c78f7137 1780 DisplayState *s = con->ds;
284d1c6b
GH
1781 DisplayChangeListener *dcl;
1782
7c20b4a3
GH
1783 QLIST_FOREACH(dcl, &s->listeners, next) {
1784 if (dcl->ops->dpy_cursor_define) {
1785 return true;
1786 }
1787 }
1788 return false;
1789}
1790
06020b95
GH
1791QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1792 struct QEMUGLParams *qparams)
1793{
1794 assert(con->gl);
1795 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1796}
1797
1798void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1799{
1800 assert(con->gl);
1801 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1802}
1803
1804int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1805{
1806 assert(con->gl);
1807 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1808}
1809
eaa92c76
GH
1810void dpy_gl_scanout_disable(QemuConsole *con)
1811{
1812 assert(con->gl);
568b12fc 1813 con->gl->ops->dpy_gl_scanout_disable(con->gl);
eaa92c76
GH
1814}
1815
f4c36bda
GH
1816void dpy_gl_scanout_texture(QemuConsole *con,
1817 uint32_t backing_id,
1818 bool backing_y_0_top,
1819 uint32_t backing_width,
1820 uint32_t backing_height,
1821 uint32_t x, uint32_t y,
1822 uint32_t width, uint32_t height)
06020b95
GH
1823{
1824 assert(con->gl);
f4c36bda
GH
1825 con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id,
1826 backing_y_0_top,
1827 backing_width, backing_height,
1828 x, y, width, height);
06020b95
GH
1829}
1830
4133fa71
GH
1831void dpy_gl_scanout_dmabuf(QemuConsole *con,
1832 QemuDmaBuf *dmabuf)
1833{
1834 assert(con->gl);
1835 con->gl->ops->dpy_gl_scanout_dmabuf(con->gl, dmabuf);
1836}
1837
6e1f2cb5
GH
1838void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1839 bool have_hot, uint32_t hot_x, uint32_t hot_y)
4133fa71
GH
1840{
1841 assert(con->gl);
1842
1843 if (con->gl->ops->dpy_gl_cursor_dmabuf) {
6e1f2cb5
GH
1844 con->gl->ops->dpy_gl_cursor_dmabuf(con->gl, dmabuf,
1845 have_hot, hot_x, hot_y);
1846 }
1847}
1848
1849void dpy_gl_cursor_position(QemuConsole *con,
1850 uint32_t pos_x, uint32_t pos_y)
1851{
1852 assert(con->gl);
1853
1854 if (con->gl->ops->dpy_gl_cursor_position) {
1855 con->gl->ops->dpy_gl_cursor_position(con->gl, pos_x, pos_y);
4133fa71
GH
1856 }
1857}
1858
1859void dpy_gl_release_dmabuf(QemuConsole *con,
1860 QemuDmaBuf *dmabuf)
1861{
1862 assert(con->gl);
1863
1864 if (con->gl->ops->dpy_gl_release_dmabuf) {
1865 con->gl->ops->dpy_gl_release_dmabuf(con->gl, dmabuf);
1866 }
1867}
1868
06020b95
GH
1869void dpy_gl_update(QemuConsole *con,
1870 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1871{
1872 assert(con->gl);
1873 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1874}
1875
98b50080
PB
1876/***********************************************************/
1877/* register display */
1878
64840c66
GH
1879/* console.c internal use only */
1880static DisplayState *get_alloc_displaystate(void)
98b50080 1881{
64840c66
GH
1882 if (!display_state) {
1883 display_state = g_new0(DisplayState, 1);
aea7947c
GH
1884 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1885 text_console_update_cursor, NULL);
64840c66
GH
1886 }
1887 return display_state;
98b50080
PB
1888}
1889
64840c66
GH
1890/*
1891 * Called by main(), after creating QemuConsoles
1892 * and before initializing ui (sdl/vnc/...).
1893 */
1894DisplayState *init_displaystate(void)
98b50080 1895{
43f420f8 1896 gchar *name;
cd6cd8fa 1897 QemuConsole *con;
64840c66 1898
333cb18f 1899 get_alloc_displaystate();
cd6cd8fa
GH
1900 QTAILQ_FOREACH(con, &consoles, next) {
1901 if (con->console_type != GRAPHIC_CONSOLE &&
1902 con->ds == NULL) {
1903 text_console_do_init(con->chr, display_state);
64840c66 1904 }
43f420f8
GH
1905
1906 /* Hook up into the qom tree here (not in new_console()), once
1907 * all QemuConsoles are created and the order / numbering
1908 * doesn't change any more */
cd6cd8fa 1909 name = g_strdup_printf("console[%d]", con->index);
43f420f8 1910 object_property_add_child(container_get(object_get_root(), "/backend"),
d2623129 1911 name, OBJECT(con));
43f420f8 1912 g_free(name);
64840c66
GH
1913 }
1914
98b50080
PB
1915 return display_state;
1916}
1917
1c1f9498
GH
1918void graphic_console_set_hwops(QemuConsole *con,
1919 const GraphicHwOps *hw_ops,
1920 void *opaque)
1921{
1922 con->hw_ops = hw_ops;
1923 con->hw = opaque;
1924}
1925
5643706a 1926QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 1927 const GraphicHwOps *hw_ops,
c78f7137 1928 void *opaque)
95219897 1929{
521a580d
GH
1930 static const char noinit[] =
1931 "Guest has not initialized the display (yet).";
64840c66
GH
1932 int width = 640;
1933 int height = 480;
76ffb0b4 1934 QemuConsole *s;
3023f332 1935 DisplayState *ds;
9588d67e 1936 DisplaySurface *surface;
f0f2f976 1937
64840c66 1938 ds = get_alloc_displaystate();
9588d67e
GH
1939 s = qemu_console_lookup_unused();
1940 if (s) {
1941 trace_console_gfx_reuse(s->index);
1942 if (s->surface) {
1943 width = surface_width(s->surface);
1944 height = surface_height(s->surface);
1945 }
1946 } else {
1947 trace_console_gfx_new();
1948 s = new_console(ds, GRAPHIC_CONSOLE, head);
1949 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1950 dpy_set_ui_info_timer, s);
1951 }
1c1f9498 1952 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 1953 if (dev) {
5325cc34 1954 object_property_set_link(OBJECT(s), "device", OBJECT(dev),
afff2b15 1955 &error_abort);
aa2beaa1 1956 }
3023f332 1957
b5a087b0 1958 surface = qemu_create_placeholder_surface(width, height, noinit);
9588d67e 1959 dpy_gfx_replace_surface(s, surface);
c78f7137 1960 return s;
e7f0ad58
FB
1961}
1962
9588d67e
GH
1963static const GraphicHwOps unused_ops = {
1964 /* no callbacks */
1965};
1966
1967void graphic_console_close(QemuConsole *con)
1968{
1969 static const char unplugged[] =
1970 "Guest display has been unplugged";
1971 DisplaySurface *surface;
1972 int width = 640;
1973 int height = 480;
1974
1975 if (con->surface) {
1976 width = surface_width(con->surface);
1977 height = surface_height(con->surface);
1978 }
1979
1980 trace_console_gfx_close(con->index);
5325cc34 1981 object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
9588d67e
GH
1982 graphic_console_set_hwops(con, &unused_ops, NULL);
1983
1984 if (con->gl) {
1985 dpy_gl_scanout_disable(con);
1986 }
b5a087b0 1987 surface = qemu_create_placeholder_surface(width, height, unplugged);
9588d67e
GH
1988 dpy_gfx_replace_surface(con, surface);
1989}
1990
284d1c6b
GH
1991QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1992{
cd6cd8fa
GH
1993 QemuConsole *con;
1994
1995 QTAILQ_FOREACH(con, &consoles, next) {
1996 if (con->index == index) {
1997 return con;
1998 }
284d1c6b 1999 }
cd6cd8fa 2000 return NULL;
284d1c6b
GH
2001}
2002
5643706a 2003QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 2004{
cd6cd8fa 2005 QemuConsole *con;
14a93649 2006 Object *obj;
5643706a 2007 uint32_t h;
14a93649 2008
cd6cd8fa
GH
2009 QTAILQ_FOREACH(con, &consoles, next) {
2010 obj = object_property_get_link(OBJECT(con),
afff2b15 2011 "device", &error_abort);
5643706a
GH
2012 if (DEVICE(obj) != dev) {
2013 continue;
2014 }
cd6cd8fa 2015 h = object_property_get_uint(OBJECT(con),
ad664c1d 2016 "head", &error_abort);
5643706a
GH
2017 if (h != head) {
2018 continue;
14a93649 2019 }
cd6cd8fa 2020 return con;
14a93649
GH
2021 }
2022 return NULL;
2023}
2024
f2c1d54c
GH
2025QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
2026 uint32_t head, Error **errp)
2027{
2028 DeviceState *dev;
2029 QemuConsole *con;
2030
2031 dev = qdev_find_recursive(sysbus_get_default(), device_id);
2032 if (dev == NULL) {
2033 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2034 "Device '%s' not found", device_id);
2035 return NULL;
2036 }
2037
2038 con = qemu_console_lookup_by_device(dev, head);
2039 if (con == NULL) {
2040 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
2041 device_id, head);
2042 return NULL;
2043 }
2044
2045 return con;
2046}
2047
9588d67e
GH
2048QemuConsole *qemu_console_lookup_unused(void)
2049{
cd6cd8fa 2050 QemuConsole *con;
9588d67e 2051 Object *obj;
9588d67e 2052
cd6cd8fa
GH
2053 QTAILQ_FOREACH(con, &consoles, next) {
2054 if (con->hw_ops != &unused_ops) {
9588d67e
GH
2055 continue;
2056 }
cd6cd8fa 2057 obj = object_property_get_link(OBJECT(con),
9588d67e
GH
2058 "device", &error_abort);
2059 if (obj != NULL) {
2060 continue;
2061 }
cd6cd8fa 2062 return con;
9588d67e
GH
2063 }
2064 return NULL;
2065}
2066
81c0d5a6 2067bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 2068{
284d1c6b 2069 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
2070}
2071
81c0d5a6 2072bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 2073{
81c0d5a6
GH
2074 if (con == NULL) {
2075 con = active_console;
2076 }
2077 return con && (con->console_type == GRAPHIC_CONSOLE);
2078}
2079
2080bool qemu_console_is_fixedsize(QemuConsole *con)
2081{
2082 if (con == NULL) {
2083 con = active_console;
2084 }
2085 return con && (con->console_type != TEXT_CONSOLE);
c21bbcfa
AZ
2086}
2087
f607867c
GH
2088bool qemu_console_is_gl_blocked(QemuConsole *con)
2089{
2090 assert(con != NULL);
2091 return con->gl_block;
2092}
2093
779ce88f
GH
2094char *qemu_console_get_label(QemuConsole *con)
2095{
2096 if (con->console_type == GRAPHIC_CONSOLE) {
2097 if (con->device) {
2098 return g_strdup(object_get_typename(con->device));
2099 }
2100 return g_strdup("VGA");
2101 } else {
2102 if (con->chr && con->chr->label) {
2103 return g_strdup(con->chr->label);
2104 }
2105 return g_strdup_printf("vc%d", con->index);
2106 }
2107}
2108
d4c85337
GH
2109int qemu_console_get_index(QemuConsole *con)
2110{
2111 if (con == NULL) {
2112 con = active_console;
2113 }
2114 return con ? con->index : -1;
2115}
2116
5643706a
GH
2117uint32_t qemu_console_get_head(QemuConsole *con)
2118{
2119 if (con == NULL) {
2120 con = active_console;
2121 }
2122 return con ? con->head : -1;
2123}
2124
d4c85337
GH
2125int qemu_console_get_width(QemuConsole *con, int fallback)
2126{
2127 if (con == NULL) {
2128 con = active_console;
2129 }
2130 return con ? surface_width(con->surface) : fallback;
2131}
2132
2133int qemu_console_get_height(QemuConsole *con, int fallback)
2134{
2135 if (con == NULL) {
2136 con = active_console;
2137 }
2138 return con ? surface_height(con->surface) : fallback;
2139}
2140
ec222519
VR
2141static void vc_chr_accept_input(Chardev *chr)
2142{
2143 VCChardev *drv = VC_CHARDEV(chr);
2144 QemuConsole *s = drv->console;
2145
2146 kbd_send_chars(s);
2147}
2148
5bf5adae 2149static void vc_chr_set_echo(Chardev *chr, bool echo)
4104833f 2150{
777357d7 2151 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 2152 QemuConsole *s = drv->console;
4104833f
PB
2153
2154 s->echo = echo;
2155}
2156
aea7947c
GH
2157static void text_console_update_cursor_timer(void)
2158{
2159 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2160 + CONSOLE_CURSOR_PERIOD / 2);
2161}
2162
bf1bed81
JK
2163static void text_console_update_cursor(void *opaque)
2164{
aea7947c 2165 QemuConsole *s;
cd6cd8fa 2166 int count = 0;
aea7947c
GH
2167
2168 cursor_visible_phase = !cursor_visible_phase;
bf1bed81 2169
cd6cd8fa 2170 QTAILQ_FOREACH(s, &consoles, next) {
aea7947c
GH
2171 if (qemu_console_is_graphic(s) ||
2172 !qemu_console_is_visible(s)) {
2173 continue;
2174 }
2175 count++;
2176 graphic_hw_invalidate(s);
2177 }
2178
2179 if (count) {
2180 text_console_update_cursor_timer();
2181 }
bf1bed81
JK
2182}
2183
380cd056
GH
2184static const GraphicHwOps text_console_ops = {
2185 .invalidate = text_console_invalidate,
2186 .text_update = text_console_update,
2187};
2188
0ec7b3e7 2189static void text_console_do_init(Chardev *chr, DisplayState *ds)
e7f0ad58 2190{
777357d7 2191 VCChardev *drv = VC_CHARDEV(chr);
41ac54b2 2192 QemuConsole *s = drv->console;
36671fbd
GH
2193 int g_width = 80 * FONT_WIDTH;
2194 int g_height = 24 * FONT_HEIGHT;
6d6f7c28 2195
0c9d0641 2196 fifo8_create(&s->out_fifo, 16);
3023f332 2197 s->ds = ds;
3b46e624 2198
e7f0ad58
FB
2199 s->y_displayed = 0;
2200 s->y_base = 0;
2201 s->total_height = DEFAULT_BACKSCROLL;
2202 s->x = 0;
2203 s->y = 0;
36671fbd 2204 if (!s->surface) {
321f048d 2205 if (active_console && active_console->surface) {
36671fbd
GH
2206 g_width = surface_width(active_console->surface);
2207 g_height = surface_height(active_console->surface);
321f048d 2208 }
36671fbd 2209 s->surface = qemu_create_displaysurface(g_width, g_height);
491e114a 2210 }
6d6f7c28 2211
380cd056 2212 s->hw_ops = &text_console_ops;
4d3b6f6e
AZ
2213 s->hw = s;
2214
6d6f7c28
PB
2215 /* Set text attribute defaults */
2216 s->t_attrib_default.bold = 0;
2217 s->t_attrib_default.uline = 0;
2218 s->t_attrib_default.blink = 0;
2219 s->t_attrib_default.invers = 0;
2220 s->t_attrib_default.unvisible = 0;
4083733d
OH
2221 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2222 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
2223 /* set current text attributes to default */
2224 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
2225 text_console_resize(s);
2226
51bfa4d3 2227 if (chr->label) {
18595181 2228 char *msg;
51bfa4d3 2229
4083733d 2230 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
18595181
GH
2231 msg = g_strdup_printf("%s console\r\n", chr->label);
2232 vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
2233 g_free(msg);
735ba588 2234 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
2235 }
2236
63618135 2237 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
e7f0ad58 2238}
c60e08d9 2239
777357d7
MAL
2240static void vc_chr_open(Chardev *chr,
2241 ChardevBackend *backend,
2242 bool *be_opened,
2243 Error **errp)
2796dae0 2244{
6f974c84 2245 ChardevVC *vc = backend->u.vc.data;
777357d7 2246 VCChardev *drv = VC_CHARDEV(chr);
76ffb0b4 2247 QemuConsole *s;
702ec69c
GH
2248 unsigned width = 0;
2249 unsigned height = 0;
2796dae0 2250
702ec69c
GH
2251 if (vc->has_width) {
2252 width = vc->width;
2253 } else if (vc->has_cols) {
2254 width = vc->cols * FONT_WIDTH;
2255 }
491e114a 2256
702ec69c
GH
2257 if (vc->has_height) {
2258 height = vc->height;
2259 } else if (vc->has_rows) {
2260 height = vc->rows * FONT_HEIGHT;
2261 }
491e114a 2262
437fe106 2263 trace_console_txt_new(width, height);
491e114a 2264 if (width == 0 || height == 0) {
afff2b15 2265 s = new_console(NULL, TEXT_CONSOLE, 0);
491e114a 2266 } else {
afff2b15 2267 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
36671fbd 2268 s->surface = qemu_create_displaysurface(width, height);
491e114a
PB
2269 }
2270
2271 if (!s) {
fa19d025 2272 error_setg(errp, "cannot create text console");
777357d7 2273 return;
491e114a
PB
2274 }
2275
2276 s->chr = chr;
41ac54b2 2277 drv->console = s;
64840c66
GH
2278
2279 if (display_state) {
2280 text_console_do_init(chr, display_state);
2281 }
2796dae0 2282
82878dac
MAL
2283 /* console/chardev init sometimes completes elsewhere in a 2nd
2284 * stage, so defer OPENED events until they are fully initialized
2285 */
2286 *be_opened = false;
d82831db
AL
2287}
2288
c78f7137 2289void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 2290{
321f048d
GH
2291 DisplaySurface *surface;
2292
2293 assert(s->console_type == GRAPHIC_CONSOLE);
cd958edb 2294
3ef0c573 2295 if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) &&
cd958edb
MAL
2296 pixman_image_get_width(s->surface->image) == width &&
2297 pixman_image_get_height(s->surface->image) == height) {
2298 return;
2299 }
2300
321f048d
GH
2301 surface = qemu_create_displaysurface(width, height);
2302 dpy_gfx_replace_surface(s, surface);
c60e08d9 2303}
38334f76 2304
c78f7137
GH
2305DisplaySurface *qemu_console_surface(QemuConsole *console)
2306{
321f048d 2307 return console->surface;
c78f7137
GH
2308}
2309
0da2ea1b 2310PixelFormat qemu_default_pixelformat(int bpp)
2311{
56bd9ea1
GH
2312 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2313 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
2314 return pf;
2315}
01f45d98 2316
db71589f
GH
2317static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2318
2319void qemu_display_register(QemuDisplay *ui)
2320{
2321 assert(ui->type < DISPLAY_TYPE__MAX);
2322 dpys[ui->type] = ui;
2323}
2324
898f9d41
GH
2325bool qemu_display_find_default(DisplayOptions *opts)
2326{
2327 static DisplayType prio[] = {
66c2207f 2328#if defined(CONFIG_GTK)
898f9d41 2329 DISPLAY_TYPE_GTK,
66c2207f
TH
2330#endif
2331#if defined(CONFIG_SDL)
898f9d41 2332 DISPLAY_TYPE_SDL,
66c2207f
TH
2333#endif
2334#if defined(CONFIG_COCOA)
898f9d41 2335 DISPLAY_TYPE_COCOA
66c2207f 2336#endif
898f9d41
GH
2337 };
2338 int i;
2339
66c2207f 2340 for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
61b4d9a2 2341 if (dpys[prio[i]] == NULL) {
c809d1d2 2342 ui_module_load_one(DisplayType_str(prio[i]));
61b4d9a2 2343 }
898f9d41
GH
2344 if (dpys[prio[i]] == NULL) {
2345 continue;
2346 }
2347 opts->type = prio[i];
2348 return true;
2349 }
2350 return false;
2351}
2352
db71589f
GH
2353void qemu_display_early_init(DisplayOptions *opts)
2354{
2355 assert(opts->type < DISPLAY_TYPE__MAX);
2356 if (opts->type == DISPLAY_TYPE_NONE) {
2357 return;
2358 }
61b4d9a2 2359 if (dpys[opts->type] == NULL) {
c809d1d2 2360 ui_module_load_one(DisplayType_str(opts->type));
61b4d9a2 2361 }
db71589f
GH
2362 if (dpys[opts->type] == NULL) {
2363 error_report("Display '%s' is not available.",
c809d1d2 2364 DisplayType_str(opts->type));
db71589f
GH
2365 exit(1);
2366 }
2367 if (dpys[opts->type]->early_init) {
2368 dpys[opts->type]->early_init(opts);
2369 }
2370}
2371
2372void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2373{
2374 assert(opts->type < DISPLAY_TYPE__MAX);
2375 if (opts->type == DISPLAY_TYPE_NONE) {
2376 return;
2377 }
2378 assert(dpys[opts->type] != NULL);
2379 dpys[opts->type]->init(ds, opts);
2380}
2381
c388f408
TH
2382void qemu_display_help(void)
2383{
2384 int idx;
2385
2386 printf("Available display backend types:\n");
a1e8853e 2387 printf("none\n");
c388f408
TH
2388 for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
2389 if (!dpys[idx]) {
2390 ui_module_load_one(DisplayType_str(idx));
2391 }
2392 if (dpys[idx]) {
2393 printf("%s\n", DisplayType_str(dpys[idx]->type));
2394 }
2395 }
2396}
2397
6f974c84 2398void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
702ec69c
GH
2399{
2400 int val;
21a933ea 2401 ChardevVC *vc;
702ec69c 2402
0b663b7d 2403 backend->type = CHARDEV_BACKEND_KIND_VC;
32bafa8f 2404 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
21a933ea 2405 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
702ec69c
GH
2406
2407 val = qemu_opt_get_number(opts, "width", 0);
2408 if (val != 0) {
21a933ea
EB
2409 vc->has_width = true;
2410 vc->width = val;
702ec69c
GH
2411 }
2412
2413 val = qemu_opt_get_number(opts, "height", 0);
2414 if (val != 0) {
21a933ea
EB
2415 vc->has_height = true;
2416 vc->height = val;
702ec69c
GH
2417 }
2418
2419 val = qemu_opt_get_number(opts, "cols", 0);
2420 if (val != 0) {
21a933ea
EB
2421 vc->has_cols = true;
2422 vc->cols = val;
702ec69c
GH
2423 }
2424
2425 val = qemu_opt_get_number(opts, "rows", 0);
2426 if (val != 0) {
21a933ea
EB
2427 vc->has_rows = true;
2428 vc->rows = val;
702ec69c
GH
2429 }
2430}
2431
95be0669
GH
2432static const TypeInfo qemu_console_info = {
2433 .name = TYPE_QEMU_CONSOLE,
2434 .parent = TYPE_OBJECT,
2435 .instance_size = sizeof(QemuConsole),
2436 .class_size = sizeof(QemuConsoleClass),
2437};
2438
777357d7
MAL
2439static void char_vc_class_init(ObjectClass *oc, void *data)
2440{
2441 ChardevClass *cc = CHARDEV_CLASS(oc);
2442
88cace9f 2443 cc->parse = qemu_chr_parse_vc;
777357d7
MAL
2444 cc->open = vc_chr_open;
2445 cc->chr_write = vc_chr_write;
ec222519 2446 cc->chr_accept_input = vc_chr_accept_input;
777357d7
MAL
2447 cc->chr_set_echo = vc_chr_set_echo;
2448}
2449
2450static const TypeInfo char_vc_type_info = {
2451 .name = TYPE_CHARDEV_VC,
2452 .parent = TYPE_CHARDEV,
0ec7b3e7 2453 .instance_size = sizeof(VCChardev),
777357d7
MAL
2454 .class_init = char_vc_class_init,
2455};
2456
2457void qemu_console_early_init(void)
2458{
2459 /* set the default vc driver */
2460 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2461 type_register(&char_vc_type_info);
777357d7
MAL
2462 }
2463}
2464
01f45d98
AL
2465static void register_types(void)
2466{
95be0669 2467 type_register_static(&qemu_console_info);
01f45d98
AL
2468}
2469
2470type_init(register_types);