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