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