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