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