]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
tests: start chardev unit tests
[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 */
e16f4c87 24#include "qemu/osdep.h"
87ecb68b 25#include "qemu-common.h"
28ecbaee 26#include "ui/console.h"
aa2beaa1 27#include "hw/qdev-core.h"
1de7afc9 28#include "qemu/timer.h"
ad39cf6d 29#include "qmp-commands.h"
dccfcd0e 30#include "sysemu/char.h"
ac86048b 31#include "trace.h"
a77549b3 32#include "exec/memory.h"
e7f0ad58
FB
33
34#define DEFAULT_BACKSCROLL 512
bf1bed81 35#define CONSOLE_CURSOR_PERIOD 500
e7f0ad58 36
6d6f7c28
PB
37typedef struct TextAttributes {
38 uint8_t fgcol:4;
39 uint8_t bgcol:4;
40 uint8_t bold:1;
41 uint8_t uline:1;
42 uint8_t blink:1;
43 uint8_t invers:1;
44 uint8_t unvisible:1;
45} TextAttributes;
46
e7f0ad58
FB
47typedef struct TextCell {
48 uint8_t ch;
6d6f7c28 49 TextAttributes t_attrib;
e7f0ad58
FB
50} TextCell;
51
52#define MAX_ESC_PARAMS 3
53
54enum TTYState {
55 TTY_STATE_NORM,
56 TTY_STATE_ESC,
57 TTY_STATE_CSI,
58};
59
e15d7371
FB
60typedef struct QEMUFIFO {
61 uint8_t *buf;
62 int buf_size;
63 int count, wptr, rptr;
64} QEMUFIFO;
65
9596ebb7 66static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
e15d7371
FB
67{
68 int l, len;
69
70 l = f->buf_size - f->count;
71 if (len1 > l)
72 len1 = l;
73 len = len1;
74 while (len > 0) {
75 l = f->buf_size - f->wptr;
76 if (l > len)
77 l = len;
78 memcpy(f->buf + f->wptr, buf, l);
79 f->wptr += l;
80 if (f->wptr >= f->buf_size)
81 f->wptr = 0;
82 buf += l;
83 len -= l;
84 }
85 f->count += len1;
86 return len1;
87}
88
9596ebb7 89static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
e15d7371
FB
90{
91 int l, len;
92
93 if (len1 > f->count)
94 len1 = f->count;
95 len = len1;
96 while (len > 0) {
97 l = f->buf_size - f->rptr;
98 if (l > len)
99 l = len;
100 memcpy(buf, f->buf + f->rptr, l);
101 f->rptr += l;
102 if (f->rptr >= f->buf_size)
103 f->rptr = 0;
104 buf += l;
105 len -= l;
106 }
107 f->count -= len1;
108 return len1;
109}
110
af3a9031
TS
111typedef enum {
112 GRAPHIC_CONSOLE,
c21bbcfa
AZ
113 TEXT_CONSOLE,
114 TEXT_CONSOLE_FIXED_SIZE
c227f099 115} console_type_t;
af3a9031 116
76ffb0b4 117struct QemuConsole {
95be0669
GH
118 Object parent;
119
f81bdefb 120 int index;
c227f099 121 console_type_t console_type;
e7f0ad58 122 DisplayState *ds;
321f048d 123 DisplaySurface *surface;
284d1c6b 124 int dcls;
06020b95 125 DisplayChangeListener *gl;
f607867c 126 bool gl_block;
76ffb0b4 127
95219897 128 /* Graphic console state. */
aa2beaa1 129 Object *device;
5643706a 130 uint32_t head;
6f90f3d7 131 QemuUIInfo ui_info;
cf1ecc82 132 QEMUTimer *ui_timer;
380cd056 133 const GraphicHwOps *hw_ops;
95219897 134 void *hw;
76ffb0b4
GH
135
136 /* Text console state */
e7f0ad58
FB
137 int width;
138 int height;
139 int total_height;
140 int backscroll_height;
e7f0ad58 141 int x, y;
adb47967 142 int x_saved, y_saved;
e7f0ad58
FB
143 int y_displayed;
144 int y_base;
6d6f7c28
PB
145 TextAttributes t_attrib_default; /* default text attributes */
146 TextAttributes t_attrib; /* currently active text attributes */
e7f0ad58 147 TextCell *cells;
4d3b6f6e 148 int text_x[2], text_y[2], cursor_invalidate;
4104833f 149 int echo;
e7f0ad58 150
14778c20
PB
151 int update_x0;
152 int update_y0;
153 int update_x1;
154 int update_y1;
155
e7f0ad58
FB
156 enum TTYState state;
157 int esc_params[MAX_ESC_PARAMS];
158 int nb_esc_params;
159
e5b0bc44 160 CharDriverState *chr;
e15d7371
FB
161 /* fifo for key pressed */
162 QEMUFIFO out_fifo;
163 uint8_t out_fifo_buf[16];
164 QEMUTimer *kbd_timer;
e7f0ad58
FB
165};
166
27be5587 167struct DisplayState {
1246b259 168 QEMUTimer *gui_timer;
0f7b2864
GH
169 uint64_t last_update;
170 uint64_t update_interval;
171 bool refreshing;
27be5587
GH
172 bool have_gfx;
173 bool have_text;
174
175 QLIST_HEAD(, DisplayChangeListener) listeners;
176};
177
98b50080 178static DisplayState *display_state;
76ffb0b4 179static QemuConsole *active_console;
a1d2db08 180static QemuConsole **consoles;
e7f0ad58 181static int nb_consoles = 0;
aea7947c
GH
182static bool cursor_visible_phase;
183static QEMUTimer *cursor_timer;
e7f0ad58 184
64840c66 185static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
0f7b2864 186static void dpy_refresh(DisplayState *s);
5209089f 187static DisplayState *get_alloc_displaystate(void);
aea7947c
GH
188static void text_console_update_cursor_timer(void);
189static void text_console_update_cursor(void *opaque);
64840c66 190
98a9ad90
GH
191static void gui_update(void *opaque)
192{
0f7b2864
GH
193 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
194 uint64_t dcl_interval;
98a9ad90
GH
195 DisplayState *ds = opaque;
196 DisplayChangeListener *dcl;
dea1b0bd 197 int i;
98a9ad90 198
0f7b2864 199 ds->refreshing = true;
98a9ad90 200 dpy_refresh(ds);
0f7b2864 201 ds->refreshing = false;
98a9ad90
GH
202
203 QLIST_FOREACH(dcl, &ds->listeners, next) {
0f7b2864
GH
204 dcl_interval = dcl->update_interval ?
205 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
206 if (interval > dcl_interval) {
207 interval = dcl_interval;
98a9ad90
GH
208 }
209 }
0f7b2864
GH
210 if (ds->update_interval != interval) {
211 ds->update_interval = interval;
dea1b0bd
GH
212 for (i = 0; i < nb_consoles; i++) {
213 if (consoles[i]->hw_ops->update_interval) {
214 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
215 }
216 }
0f7b2864
GH
217 trace_console_refresh(interval);
218 }
bc72ad67
AB
219 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
220 timer_mod(ds->gui_timer, ds->last_update + interval);
98a9ad90
GH
221}
222
223static void gui_setup_refresh(DisplayState *ds)
224{
225 DisplayChangeListener *dcl;
226 bool need_timer = false;
227 bool have_gfx = false;
228 bool have_text = false;
229
230 QLIST_FOREACH(dcl, &ds->listeners, next) {
231 if (dcl->ops->dpy_refresh != NULL) {
232 need_timer = true;
233 }
234 if (dcl->ops->dpy_gfx_update != NULL) {
235 have_gfx = true;
236 }
237 if (dcl->ops->dpy_text_update != NULL) {
238 have_text = true;
239 }
240 }
241
242 if (need_timer && ds->gui_timer == NULL) {
bc72ad67
AB
243 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
244 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
98a9ad90
GH
245 }
246 if (!need_timer && ds->gui_timer != NULL) {
bc72ad67
AB
247 timer_del(ds->gui_timer);
248 timer_free(ds->gui_timer);
98a9ad90
GH
249 ds->gui_timer = NULL;
250 }
251
252 ds->have_gfx = have_gfx;
253 ds->have_text = have_text;
254}
255
1dbfa005 256void graphic_hw_update(QemuConsole *con)
95219897 257{
1dbfa005
GH
258 if (!con) {
259 con = active_console;
260 }
380cd056
GH
261 if (con && con->hw_ops->gfx_update) {
262 con->hw_ops->gfx_update(con->hw);
1dbfa005 263 }
95219897
PB
264}
265
bba19b88
GH
266void graphic_hw_gl_block(QemuConsole *con, bool block)
267{
f607867c
GH
268 assert(con != NULL);
269
270 con->gl_block = block;
271 if (con->hw_ops->gl_block) {
bba19b88
GH
272 con->hw_ops->gl_block(con->hw, block);
273 }
274}
275
1dbfa005 276void graphic_hw_invalidate(QemuConsole *con)
95219897 277{
1dbfa005
GH
278 if (!con) {
279 con = active_console;
280 }
380cd056
GH
281 if (con && con->hw_ops->invalidate) {
282 con->hw_ops->invalidate(con->hw);
1dbfa005 283 }
95219897
PB
284}
285
9425c004 286static void ppm_save(const char *filename, DisplaySurface *ds,
2c62f08d 287 Error **errp)
95219897 288{
2c62f08d
GH
289 int width = pixman_image_get_width(ds->image);
290 int height = pixman_image_get_height(ds->image);
cdd5b937 291 int fd;
2c62f08d
GH
292 FILE *f;
293 int y;
294 int ret;
295 pixman_image_t *linebuf;
296
297 trace_ppm_save(filename, ds);
cdd5b937
GH
298 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
299 if (fd == -1) {
2c62f08d
GH
300 error_setg(errp, "failed to open file '%s': %s", filename,
301 strerror(errno));
302 return;
45efb161 303 }
cdd5b937 304 f = fdopen(fd, "wb");
2c62f08d
GH
305 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
306 if (ret < 0) {
307 linebuf = NULL;
308 goto write_err;
309 }
310 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
311 for (y = 0; y < height; y++) {
312 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
313 clearerr(f);
314 ret = fwrite(pixman_image_get_data(linebuf), 1,
315 pixman_image_get_stride(linebuf), f);
316 (void)ret;
317 if (ferror(f)) {
318 goto write_err;
319 }
f81bdefb
JK
320 }
321
2c62f08d
GH
322out:
323 qemu_pixman_image_unref(linebuf);
324 fclose(f);
325 return;
326
327write_err:
328 error_setg(errp, "failed to write to file '%s': %s", filename,
329 strerror(errno));
330 unlink(filename);
331 goto out;
332}
333
334void qmp_screendump(const char *filename, Error **errp)
335{
284d1c6b 336 QemuConsole *con = qemu_console_lookup_by_index(0);
2c62f08d
GH
337 DisplaySurface *surface;
338
339 if (con == NULL) {
340 error_setg(errp, "There is no QemuConsole I can screendump from.");
341 return;
33bcd98c 342 }
2c62f08d
GH
343
344 graphic_hw_update(con);
345 surface = qemu_console_surface(con);
346 ppm_save(filename, surface, errp);
95219897
PB
347}
348
1dbfa005 349void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
4d3b6f6e 350{
1dbfa005
GH
351 if (!con) {
352 con = active_console;
353 }
380cd056
GH
354 if (con && con->hw_ops->text_update) {
355 con->hw_ops->text_update(con->hw, chardata);
356 }
4d3b6f6e
AZ
357}
358
1562e531
GH
359static void vga_fill_rect(QemuConsole *con,
360 int posx, int posy, int width, int height,
e27bd65a 361 pixman_color_t color)
e7f0ad58 362{
1562e531 363 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
364 pixman_rectangle16_t rect = {
365 .x = posx, .y = posy, .width = width, .height = height
366 };
68db6dc5 367
68db6dc5 368 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
e27bd65a 369 &color, 1, &rect);
e7f0ad58
FB
370}
371
372/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
1562e531
GH
373static void vga_bitblt(QemuConsole *con,
374 int xs, int ys, int xd, int yd, int w, int h)
e7f0ad58 375{
1562e531 376 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
377
378 pixman_image_composite(PIXMAN_OP_SRC,
379 surface->image, NULL, surface->image,
380 xs, ys, 0, 0, xd, yd, w, h);
e7f0ad58
FB
381}
382
383/***********************************************************/
384/* basic char display */
385
386#define FONT_HEIGHT 16
387#define FONT_WIDTH 8
388
389#include "vgafont.h"
390
e27bd65a
GH
391#define QEMU_RGB(r, g, b) \
392 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
393
394static const pixman_color_t color_table_rgb[2][8] = {
6d6f7c28 395 { /* dark */
4083733d
OH
396 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
397 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
398 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
399 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
400 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
401 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
402 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
403 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
6d6f7c28
PB
404 },
405 { /* bright */
4083733d
OH
406 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
407 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
408 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
409 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
410 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
411 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
412 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
413 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
6d6f7c28 414 }
e7f0ad58
FB
415};
416
1562e531 417static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
6d6f7c28 418 TextAttributes *t_attrib)
e7f0ad58 419{
7d6ba01c 420 static pixman_image_t *glyphs[256];
1562e531 421 DisplaySurface *surface = qemu_console_surface(s);
e27bd65a 422 pixman_color_t fgcol, bgcol;
6d6f7c28
PB
423
424 if (t_attrib->invers) {
cf6f0548
GH
425 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
426 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
6d6f7c28 427 } else {
cf6f0548
GH
428 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
429 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
6d6f7c28 430 }
e7f0ad58 431
7d6ba01c
GH
432 if (!glyphs[ch]) {
433 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
e7f0ad58 434 }
7d6ba01c 435 qemu_pixman_glyph_render(glyphs[ch], surface->image,
e27bd65a 436 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
e7f0ad58
FB
437}
438
76ffb0b4 439static void text_console_resize(QemuConsole *s)
e7f0ad58
FB
440{
441 TextCell *cells, *c, *c1;
442 int w1, x, y, last_width;
443
444 last_width = s->width;
36671fbd
GH
445 s->width = surface_width(s->surface) / FONT_WIDTH;
446 s->height = surface_height(s->surface) / FONT_HEIGHT;
e7f0ad58
FB
447
448 w1 = last_width;
449 if (s->width < w1)
450 w1 = s->width;
451
fedf0d35 452 cells = g_new(TextCell, s->width * s->total_height);
e7f0ad58
FB
453 for(y = 0; y < s->total_height; y++) {
454 c = &cells[y * s->width];
455 if (w1 > 0) {
456 c1 = &s->cells[y * last_width];
457 for(x = 0; x < w1; x++) {
458 *c++ = *c1++;
459 }
460 }
461 for(x = w1; x < s->width; x++) {
462 c->ch = ' ';
6d6f7c28 463 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
464 c++;
465 }
466 }
7267c094 467 g_free(s->cells);
e7f0ad58
FB
468 s->cells = cells;
469}
470
76ffb0b4 471static inline void text_update_xy(QemuConsole *s, int x, int y)
4d3b6f6e
AZ
472{
473 s->text_x[0] = MIN(s->text_x[0], x);
474 s->text_x[1] = MAX(s->text_x[1], x);
475 s->text_y[0] = MIN(s->text_y[0], y);
476 s->text_y[1] = MAX(s->text_y[1], y);
477}
478
76ffb0b4 479static void invalidate_xy(QemuConsole *s, int x, int y)
14778c20 480{
b35e3ba0
GH
481 if (!qemu_console_is_visible(s)) {
482 return;
483 }
14778c20
PB
484 if (s->update_x0 > x * FONT_WIDTH)
485 s->update_x0 = x * FONT_WIDTH;
486 if (s->update_y0 > y * FONT_HEIGHT)
487 s->update_y0 = y * FONT_HEIGHT;
488 if (s->update_x1 < (x + 1) * FONT_WIDTH)
489 s->update_x1 = (x + 1) * FONT_WIDTH;
490 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
491 s->update_y1 = (y + 1) * FONT_HEIGHT;
492}
493
76ffb0b4 494static void update_xy(QemuConsole *s, int x, int y)
e7f0ad58
FB
495{
496 TextCell *c;
497 int y1, y2;
498
1562e531
GH
499 if (s->ds->have_text) {
500 text_update_xy(s, x, y);
501 }
4d3b6f6e 502
b35e3ba0
GH
503 y1 = (s->y_base + y) % s->total_height;
504 y2 = y1 - s->y_displayed;
505 if (y2 < 0) {
506 y2 += s->total_height;
507 }
508 if (y2 < s->height) {
509 c = &s->cells[y1 * s->width + x];
510 vga_putcharxy(s, x, y2, c->ch,
511 &(c->t_attrib));
512 invalidate_xy(s, x, y2);
e7f0ad58
FB
513 }
514}
515
76ffb0b4 516static void console_show_cursor(QemuConsole *s, int show)
e7f0ad58
FB
517{
518 TextCell *c;
519 int y, y1;
1562e531 520 int x = s->x;
e7f0ad58 521
1562e531
GH
522 if (s->ds->have_text) {
523 s->cursor_invalidate = 1;
524 }
4d3b6f6e 525
b35e3ba0
GH
526 if (x >= s->width) {
527 x = s->width - 1;
528 }
529 y1 = (s->y_base + s->y) % s->total_height;
530 y = y1 - s->y_displayed;
531 if (y < 0) {
532 y += s->total_height;
533 }
534 if (y < s->height) {
535 c = &s->cells[y1 * s->width + x];
aea7947c 536 if (show && cursor_visible_phase) {
b35e3ba0
GH
537 TextAttributes t_attrib = s->t_attrib_default;
538 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
539 vga_putcharxy(s, x, y, c->ch, &t_attrib);
540 } else {
541 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
e7f0ad58 542 }
b35e3ba0 543 invalidate_xy(s, x, y);
e7f0ad58
FB
544 }
545}
546
76ffb0b4 547static void console_refresh(QemuConsole *s)
e7f0ad58 548{
1562e531 549 DisplaySurface *surface = qemu_console_surface(s);
e7f0ad58
FB
550 TextCell *c;
551 int x, y, y1;
552
a93a4a22 553 if (s->ds->have_text) {
4d3b6f6e
AZ
554 s->text_x[0] = 0;
555 s->text_y[0] = 0;
556 s->text_x[1] = s->width - 1;
557 s->text_y[1] = s->height - 1;
558 s->cursor_invalidate = 1;
4d3b6f6e 559 }
e7f0ad58 560
b35e3ba0 561 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
4083733d 562 color_table_rgb[0][QEMU_COLOR_BLACK]);
b35e3ba0
GH
563 y1 = s->y_displayed;
564 for (y = 0; y < s->height; y++) {
565 c = s->cells + y1 * s->width;
566 for (x = 0; x < s->width; x++) {
567 vga_putcharxy(s, x, y, c->ch,
568 &(c->t_attrib));
569 c++;
570 }
571 if (++y1 == s->total_height) {
572 y1 = 0;
e7f0ad58 573 }
e7f0ad58 574 }
b35e3ba0
GH
575 console_show_cursor(s, 1);
576 dpy_gfx_update(s, 0, 0,
577 surface_width(surface), surface_height(surface));
e7f0ad58
FB
578}
579
81c0d5a6 580static void console_scroll(QemuConsole *s, int ydelta)
e7f0ad58 581{
e7f0ad58 582 int i, y1;
3b46e624 583
e7f0ad58
FB
584 if (ydelta > 0) {
585 for(i = 0; i < ydelta; i++) {
586 if (s->y_displayed == s->y_base)
587 break;
588 if (++s->y_displayed == s->total_height)
589 s->y_displayed = 0;
590 }
591 } else {
592 ydelta = -ydelta;
593 i = s->backscroll_height;
594 if (i > s->total_height - s->height)
595 i = s->total_height - s->height;
596 y1 = s->y_base - i;
597 if (y1 < 0)
598 y1 += s->total_height;
599 for(i = 0; i < ydelta; i++) {
600 if (s->y_displayed == y1)
601 break;
602 if (--s->y_displayed < 0)
603 s->y_displayed = s->total_height - 1;
604 }
605 }
606 console_refresh(s);
607}
608
76ffb0b4 609static void console_put_lf(QemuConsole *s)
e7f0ad58
FB
610{
611 TextCell *c;
612 int x, y1;
613
e7f0ad58
FB
614 s->y++;
615 if (s->y >= s->height) {
616 s->y = s->height - 1;
6d6f7c28 617
e7f0ad58
FB
618 if (s->y_displayed == s->y_base) {
619 if (++s->y_displayed == s->total_height)
620 s->y_displayed = 0;
621 }
622 if (++s->y_base == s->total_height)
623 s->y_base = 0;
624 if (s->backscroll_height < s->total_height)
625 s->backscroll_height++;
626 y1 = (s->y_base + s->height - 1) % s->total_height;
627 c = &s->cells[y1 * s->width];
628 for(x = 0; x < s->width; x++) {
629 c->ch = ' ';
6d6f7c28 630 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
631 c++;
632 }
b35e3ba0 633 if (s->y_displayed == s->y_base) {
1562e531 634 if (s->ds->have_text) {
4d3b6f6e
AZ
635 s->text_x[0] = 0;
636 s->text_y[0] = 0;
637 s->text_x[1] = s->width - 1;
638 s->text_y[1] = s->height - 1;
4d3b6f6e
AZ
639 }
640
b35e3ba0
GH
641 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
642 s->width * FONT_WIDTH,
643 (s->height - 1) * FONT_HEIGHT);
644 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
645 s->width * FONT_WIDTH, FONT_HEIGHT,
646 color_table_rgb[0][s->t_attrib_default.bgcol]);
647 s->update_x0 = 0;
648 s->update_y0 = 0;
649 s->update_x1 = s->width * FONT_WIDTH;
650 s->update_y1 = s->height * FONT_HEIGHT;
e7f0ad58
FB
651 }
652 }
653}
654
6d6f7c28
PB
655/* Set console attributes depending on the current escape codes.
656 * NOTE: I know this code is not very efficient (checking every color for it
657 * self) but it is more readable and better maintainable.
658 */
76ffb0b4 659static void console_handle_escape(QemuConsole *s)
6d6f7c28
PB
660{
661 int i;
662
6d6f7c28
PB
663 for (i=0; i<s->nb_esc_params; i++) {
664 switch (s->esc_params[i]) {
665 case 0: /* reset all console attributes to default */
666 s->t_attrib = s->t_attrib_default;
667 break;
668 case 1:
669 s->t_attrib.bold = 1;
670 break;
671 case 4:
672 s->t_attrib.uline = 1;
673 break;
674 case 5:
675 s->t_attrib.blink = 1;
676 break;
677 case 7:
678 s->t_attrib.invers = 1;
679 break;
680 case 8:
681 s->t_attrib.unvisible = 1;
682 break;
683 case 22:
684 s->t_attrib.bold = 0;
685 break;
686 case 24:
687 s->t_attrib.uline = 0;
688 break;
689 case 25:
690 s->t_attrib.blink = 0;
691 break;
692 case 27:
693 s->t_attrib.invers = 0;
694 break;
695 case 28:
696 s->t_attrib.unvisible = 0;
697 break;
698 /* set foreground color */
699 case 30:
4083733d 700 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
701 break;
702 case 31:
4083733d 703 s->t_attrib.fgcol = QEMU_COLOR_RED;
6d6f7c28
PB
704 break;
705 case 32:
4083733d 706 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
6d6f7c28
PB
707 break;
708 case 33:
4083733d 709 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
6d6f7c28
PB
710 break;
711 case 34:
4083733d 712 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
6d6f7c28
PB
713 break;
714 case 35:
4083733d 715 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
6d6f7c28
PB
716 break;
717 case 36:
4083733d 718 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
6d6f7c28
PB
719 break;
720 case 37:
4083733d 721 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
6d6f7c28
PB
722 break;
723 /* set background color */
724 case 40:
4083733d 725 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
726 break;
727 case 41:
4083733d 728 s->t_attrib.bgcol = QEMU_COLOR_RED;
6d6f7c28
PB
729 break;
730 case 42:
4083733d 731 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
6d6f7c28
PB
732 break;
733 case 43:
4083733d 734 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
6d6f7c28
PB
735 break;
736 case 44:
4083733d 737 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
6d6f7c28
PB
738 break;
739 case 45:
4083733d 740 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
6d6f7c28
PB
741 break;
742 case 46:
4083733d 743 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
6d6f7c28
PB
744 break;
745 case 47:
4083733d 746 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
6d6f7c28
PB
747 break;
748 }
749 }
750}
751
76ffb0b4 752static void console_clear_xy(QemuConsole *s, int x, int y)
adb47967
TS
753{
754 int y1 = (s->y_base + y) % s->total_height;
755 TextCell *c = &s->cells[y1 * s->width + x];
756 c->ch = ' ';
757 c->t_attrib = s->t_attrib_default;
adb47967
TS
758 update_xy(s, x, y);
759}
760
58aa7d8e
RK
761static void console_put_one(QemuConsole *s, int ch)
762{
763 TextCell *c;
764 int y1;
765 if (s->x >= s->width) {
766 /* line wrap */
767 s->x = 0;
768 console_put_lf(s);
769 }
770 y1 = (s->y_base + s->y) % s->total_height;
771 c = &s->cells[y1 * s->width + s->x];
772 c->ch = ch;
773 c->t_attrib = s->t_attrib;
774 update_xy(s, s->x, s->y);
775 s->x++;
776}
777
778static void console_respond_str(QemuConsole *s, const char *buf)
779{
780 while (*buf) {
781 console_put_one(s, *buf);
782 buf++;
783 }
784}
785
3eea5498 786/* set cursor, checking bounds */
76ffb0b4 787static void set_cursor(QemuConsole *s, int x, int y)
3eea5498
IC
788{
789 if (x < 0) {
790 x = 0;
791 }
792 if (y < 0) {
793 y = 0;
794 }
795 if (y >= s->height) {
796 y = s->height - 1;
797 }
798 if (x >= s->width) {
799 x = s->width - 1;
800 }
801
802 s->x = x;
803 s->y = y;
804}
805
76ffb0b4 806static void console_putchar(QemuConsole *s, int ch)
e7f0ad58 807{
58aa7d8e 808 int i;
adb47967 809 int x, y;
58aa7d8e 810 char response[40];
e7f0ad58
FB
811
812 switch(s->state) {
813 case TTY_STATE_NORM:
814 switch(ch) {
6d6f7c28 815 case '\r': /* carriage return */
e7f0ad58
FB
816 s->x = 0;
817 break;
6d6f7c28 818 case '\n': /* newline */
e7f0ad58
FB
819 console_put_lf(s);
820 break;
6d6f7c28 821 case '\b': /* backspace */
5fafdf24 822 if (s->x > 0)
e15d7371 823 s->x--;
6d6f7c28
PB
824 break;
825 case '\t': /* tabspace */
826 if (s->x + (8 - (s->x % 8)) > s->width) {
bd468840 827 s->x = 0;
6d6f7c28
PB
828 console_put_lf(s);
829 } else {
830 s->x = s->x + (8 - (s->x % 8));
831 }
832 break;
833 case '\a': /* alert aka. bell */
834 /* TODO: has to be implemented */
835 break;
adb47967
TS
836 case 14:
837 /* SI (shift in), character set 0 (ignored) */
838 break;
839 case 15:
840 /* SO (shift out), character set 1 (ignored) */
841 break;
6d6f7c28 842 case 27: /* esc (introducing an escape sequence) */
e7f0ad58
FB
843 s->state = TTY_STATE_ESC;
844 break;
845 default:
58aa7d8e 846 console_put_one(s, ch);
e7f0ad58
FB
847 break;
848 }
849 break;
6d6f7c28 850 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
e7f0ad58
FB
851 if (ch == '[') {
852 for(i=0;i<MAX_ESC_PARAMS;i++)
853 s->esc_params[i] = 0;
854 s->nb_esc_params = 0;
855 s->state = TTY_STATE_CSI;
856 } else {
857 s->state = TTY_STATE_NORM;
858 }
859 break;
6d6f7c28 860 case TTY_STATE_CSI: /* handle escape sequence parameters */
e7f0ad58
FB
861 if (ch >= '0' && ch <= '9') {
862 if (s->nb_esc_params < MAX_ESC_PARAMS) {
c10600af
LE
863 int *param = &s->esc_params[s->nb_esc_params];
864 int digit = (ch - '0');
865
866 *param = (*param <= (INT_MAX - digit) / 10) ?
867 *param * 10 + digit : INT_MAX;
e7f0ad58
FB
868 }
869 } else {
3eea5498
IC
870 if (s->nb_esc_params < MAX_ESC_PARAMS)
871 s->nb_esc_params++;
e7f0ad58
FB
872 if (ch == ';')
873 break;
5d28b0e9
SW
874 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
875 ch, s->nb_esc_params);
e7f0ad58
FB
876 s->state = TTY_STATE_NORM;
877 switch(ch) {
adb47967
TS
878 case 'A':
879 /* move cursor up */
880 if (s->esc_params[0] == 0) {
881 s->esc_params[0] = 1;
882 }
3eea5498 883 set_cursor(s, s->x, s->y - s->esc_params[0]);
adb47967
TS
884 break;
885 case 'B':
886 /* move cursor down */
887 if (s->esc_params[0] == 0) {
888 s->esc_params[0] = 1;
889 }
3eea5498 890 set_cursor(s, s->x, s->y + s->esc_params[0]);
e7f0ad58
FB
891 break;
892 case 'C':
adb47967
TS
893 /* move cursor right */
894 if (s->esc_params[0] == 0) {
895 s->esc_params[0] = 1;
896 }
3eea5498 897 set_cursor(s, s->x + s->esc_params[0], s->y);
e7f0ad58 898 break;
adb47967
TS
899 case 'D':
900 /* move cursor left */
901 if (s->esc_params[0] == 0) {
902 s->esc_params[0] = 1;
903 }
3eea5498 904 set_cursor(s, s->x - s->esc_params[0], s->y);
adb47967
TS
905 break;
906 case 'G':
907 /* move cursor to column */
3eea5498 908 set_cursor(s, s->esc_params[0] - 1, s->y);
adb47967
TS
909 break;
910 case 'f':
911 case 'H':
912 /* move cursor to row, column */
3eea5498 913 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
adb47967
TS
914 break;
915 case 'J':
916 switch (s->esc_params[0]) {
917 case 0:
918 /* clear to end of screen */
919 for (y = s->y; y < s->height; y++) {
920 for (x = 0; x < s->width; x++) {
921 if (y == s->y && x < s->x) {
922 continue;
923 }
924 console_clear_xy(s, x, y);
925 }
926 }
927 break;
928 case 1:
929 /* clear from beginning of screen */
930 for (y = 0; y <= s->y; y++) {
931 for (x = 0; x < s->width; x++) {
932 if (y == s->y && x > s->x) {
933 break;
934 }
935 console_clear_xy(s, x, y);
936 }
937 }
938 break;
939 case 2:
940 /* clear entire screen */
941 for (y = 0; y <= s->height; y++) {
942 for (x = 0; x < s->width; x++) {
943 console_clear_xy(s, x, y);
944 }
945 }
f94a950f 946 break;
adb47967 947 }
95d8f9f4 948 break;
e7f0ad58 949 case 'K':
adb47967
TS
950 switch (s->esc_params[0]) {
951 case 0:
f94a950f
MA
952 /* clear to eol */
953 for(x = s->x; x < s->width; x++) {
adb47967 954 console_clear_xy(s, x, s->y);
f94a950f
MA
955 }
956 break;
adb47967
TS
957 case 1:
958 /* clear from beginning of line */
959 for (x = 0; x <= s->x; x++) {
960 console_clear_xy(s, x, s->y);
961 }
962 break;
963 case 2:
964 /* clear entire line */
965 for(x = 0; x < s->width; x++) {
966 console_clear_xy(s, x, s->y);
967 }
f94a950f
MA
968 break;
969 }
adb47967
TS
970 break;
971 case 'm':
f94a950f
MA
972 console_handle_escape(s);
973 break;
adb47967 974 case 'n':
58aa7d8e
RK
975 switch (s->esc_params[0]) {
976 case 5:
977 /* report console status (always succeed)*/
978 console_respond_str(s, "\033[0n");
979 break;
980 case 6:
981 /* report cursor position */
982 sprintf(response, "\033[%d;%dR",
983 (s->y_base + s->y) % s->total_height + 1,
984 s->x + 1);
985 console_respond_str(s, response);
986 break;
987 }
adb47967
TS
988 break;
989 case 's':
990 /* save cursor position */
991 s->x_saved = s->x;
992 s->y_saved = s->y;
993 break;
994 case 'u':
995 /* restore cursor position */
996 s->x = s->x_saved;
997 s->y = s->y_saved;
998 break;
999 default:
5d28b0e9 1000 trace_console_putchar_unhandled(ch);
adb47967
TS
1001 break;
1002 }
1003 break;
e7f0ad58
FB
1004 }
1005 }
1006}
1007
1008void console_select(unsigned int index)
1009{
284d1c6b 1010 DisplayChangeListener *dcl;
76ffb0b4 1011 QemuConsole *s;
6d6f7c28 1012
437fe106 1013 trace_console_select(index);
284d1c6b 1014 s = qemu_console_lookup_by_index(index);
e7f0ad58 1015 if (s) {
7d957bd8 1016 DisplayState *ds = s->ds;
bf1bed81 1017
e7f0ad58 1018 active_console = s;
a93a4a22 1019 if (ds->have_gfx) {
284d1c6b
GH
1020 QLIST_FOREACH(dcl, &ds->listeners, next) {
1021 if (dcl->con != NULL) {
1022 continue;
1023 }
1024 if (dcl->ops->dpy_gfx_switch) {
1025 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1026 }
1027 }
321f048d
GH
1028 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1029 surface_height(s->surface));
a93a4a22
GH
1030 }
1031 if (ds->have_text) {
c78f7137 1032 dpy_text_resize(s, s->width, s->height);
68f00996 1033 }
aea7947c 1034 text_console_update_cursor(NULL);
e7f0ad58
FB
1035 }
1036}
1037
1038static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1039{
76ffb0b4 1040 QemuConsole *s = chr->opaque;
e7f0ad58
FB
1041 int i;
1042
14778c20
PB
1043 s->update_x0 = s->width * FONT_WIDTH;
1044 s->update_y0 = s->height * FONT_HEIGHT;
1045 s->update_x1 = 0;
1046 s->update_y1 = 0;
e7f0ad58
FB
1047 console_show_cursor(s, 0);
1048 for(i = 0; i < len; i++) {
1049 console_putchar(s, buf[i]);
1050 }
1051 console_show_cursor(s, 1);
a93a4a22 1052 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
c78f7137 1053 dpy_gfx_update(s, s->update_x0, s->update_y0,
a93a4a22
GH
1054 s->update_x1 - s->update_x0,
1055 s->update_y1 - s->update_y0);
14778c20 1056 }
e7f0ad58
FB
1057 return len;
1058}
1059
e15d7371
FB
1060static void kbd_send_chars(void *opaque)
1061{
76ffb0b4 1062 QemuConsole *s = opaque;
e15d7371
FB
1063 int len;
1064 uint8_t buf[16];
3b46e624 1065
909cda12 1066 len = qemu_chr_be_can_write(s->chr);
e15d7371
FB
1067 if (len > s->out_fifo.count)
1068 len = s->out_fifo.count;
1069 if (len > 0) {
1070 if (len > sizeof(buf))
1071 len = sizeof(buf);
1072 qemu_fifo_read(&s->out_fifo, buf, len);
fa5efccb 1073 qemu_chr_be_write(s->chr, buf, len);
e15d7371
FB
1074 }
1075 /* characters are pending: we send them a bit later (XXX:
1076 horrible, should change char device API) */
1077 if (s->out_fifo.count > 0) {
bc72ad67 1078 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
e15d7371
FB
1079 }
1080}
1081
e7f0ad58 1082/* called when an ascii key is pressed */
3f9a6e85 1083void kbd_put_keysym_console(QemuConsole *s, int keysym)
e7f0ad58 1084{
e7f0ad58
FB
1085 uint8_t buf[16], *q;
1086 int c;
1087
af3a9031 1088 if (!s || (s->console_type == GRAPHIC_CONSOLE))
e7f0ad58
FB
1089 return;
1090
1091 switch(keysym) {
1092 case QEMU_KEY_CTRL_UP:
81c0d5a6 1093 console_scroll(s, -1);
e7f0ad58
FB
1094 break;
1095 case QEMU_KEY_CTRL_DOWN:
81c0d5a6 1096 console_scroll(s, 1);
e7f0ad58
FB
1097 break;
1098 case QEMU_KEY_CTRL_PAGEUP:
81c0d5a6 1099 console_scroll(s, -10);
e7f0ad58
FB
1100 break;
1101 case QEMU_KEY_CTRL_PAGEDOWN:
81c0d5a6 1102 console_scroll(s, 10);
e7f0ad58
FB
1103 break;
1104 default:
e15d7371
FB
1105 /* convert the QEMU keysym to VT100 key string */
1106 q = buf;
1107 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1108 *q++ = '\033';
1109 *q++ = '[';
1110 c = keysym - 0xe100;
1111 if (c >= 10)
1112 *q++ = '0' + (c / 10);
1113 *q++ = '0' + (c % 10);
1114 *q++ = '~';
1115 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1116 *q++ = '\033';
1117 *q++ = '[';
1118 *q++ = keysym & 0xff;
4104833f
PB
1119 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1120 console_puts(s->chr, (const uint8_t *) "\r", 1);
1121 *q++ = '\n';
e15d7371 1122 } else {
4104833f
PB
1123 *q++ = keysym;
1124 }
1125 if (s->echo) {
1126 console_puts(s->chr, buf, q - buf);
e15d7371 1127 }
e5b0bc44 1128 if (s->chr->chr_read) {
e15d7371
FB
1129 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1130 kbd_send_chars(s);
e7f0ad58
FB
1131 }
1132 break;
1133 }
1134}
1135
7fb1cf16 1136static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
50ef4679
GH
1137 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1138 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1139 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1140 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1141 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1142 [Q_KEY_CODE_END] = QEMU_KEY_END,
1143 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1144 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1145 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
344aa283 1146 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
50ef4679
GH
1147};
1148
1149bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1150{
1151 int keysym;
1152
1153 keysym = qcode_to_keysym[qcode];
1154 if (keysym == 0) {
1155 return false;
1156 }
1157 kbd_put_keysym_console(s, keysym);
1158 return true;
1159}
1160
bdef9724
GH
1161void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1162{
1163 int i;
1164
1165 for (i = 0; i < len && str[i]; i++) {
1166 kbd_put_keysym_console(s, str[i]);
1167 }
1168}
1169
3f9a6e85
GH
1170void kbd_put_keysym(int keysym)
1171{
1172 kbd_put_keysym_console(active_console, keysym);
1173}
1174
4d3b6f6e
AZ
1175static void text_console_invalidate(void *opaque)
1176{
76ffb0b4 1177 QemuConsole *s = (QemuConsole *) opaque;
1562e531
GH
1178
1179 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
68f00996
AL
1180 text_console_resize(s);
1181 }
4d3b6f6e
AZ
1182 console_refresh(s);
1183}
1184
c227f099 1185static void text_console_update(void *opaque, console_ch_t *chardata)
4d3b6f6e 1186{
76ffb0b4 1187 QemuConsole *s = (QemuConsole *) opaque;
4d3b6f6e
AZ
1188 int i, j, src;
1189
1190 if (s->text_x[0] <= s->text_x[1]) {
1191 src = (s->y_base + s->text_y[0]) * s->width;
1192 chardata += s->text_y[0] * s->width;
1193 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
4083733d
OH
1194 for (j = 0; j < s->width; j++, src++) {
1195 console_write_ch(chardata ++,
1196 ATTR2CHTYPE(s->cells[src].ch,
1197 s->cells[src].t_attrib.fgcol,
1198 s->cells[src].t_attrib.bgcol,
1199 s->cells[src].t_attrib.bold));
1200 }
c78f7137 1201 dpy_text_update(s, s->text_x[0], s->text_y[0],
a93a4a22 1202 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
4d3b6f6e
AZ
1203 s->text_x[0] = s->width;
1204 s->text_y[0] = s->height;
1205 s->text_x[1] = 0;
1206 s->text_y[1] = 0;
1207 }
1208 if (s->cursor_invalidate) {
c78f7137 1209 dpy_text_cursor(s, s->x, s->y);
4d3b6f6e
AZ
1210 s->cursor_invalidate = 0;
1211 }
1212}
1213
afff2b15
KB
1214static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1215 uint32_t head)
e7f0ad58 1216{
95be0669 1217 Object *obj;
76ffb0b4 1218 QemuConsole *s;
95219897 1219 int i;
e7f0ad58 1220
95be0669
GH
1221 obj = object_new(TYPE_QEMU_CONSOLE);
1222 s = QEMU_CONSOLE(obj);
afff2b15 1223 s->head = head;
aa2beaa1 1224 object_property_add_link(obj, "device", TYPE_DEVICE,
9561fda8 1225 (Object **)&s->device,
39f72ef9 1226 object_property_allow_set_link,
9561fda8 1227 OBJ_PROP_LINK_UNREF_ON_RELEASE,
afff2b15 1228 &error_abort);
5643706a 1229 object_property_add_uint32_ptr(obj, "head",
afff2b15 1230 &s->head, &error_abort);
aa2beaa1 1231
af3a9031
TS
1232 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1233 (console_type == GRAPHIC_CONSOLE))) {
e7f0ad58 1234 active_console = s;
af3a9031 1235 }
e7f0ad58 1236 s->ds = ds;
af3a9031 1237 s->console_type = console_type;
a1d2db08
GH
1238
1239 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
af3a9031 1240 if (console_type != GRAPHIC_CONSOLE) {
f81bdefb 1241 s->index = nb_consoles;
95219897
PB
1242 consoles[nb_consoles++] = s;
1243 } else {
1244 /* HACK: Put graphical consoles before text consoles. */
1245 for (i = nb_consoles; i > 0; i--) {
af3a9031 1246 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
95219897
PB
1247 break;
1248 consoles[i] = consoles[i - 1];
f81bdefb 1249 consoles[i]->index = i;
95219897 1250 }
f81bdefb 1251 s->index = i;
95219897 1252 consoles[i] = s;
3023f332 1253 nb_consoles++;
95219897
PB
1254 }
1255 return s;
1256}
1257
30f1e661 1258static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
ffe8b821 1259{
69c77777
GH
1260 qemu_pixman_image_unref(surface->image);
1261 surface->image = NULL;
69c77777 1262
30f1e661 1263 surface->format = PIXMAN_x8r8g8b8;
69c77777
GH
1264 surface->image = pixman_image_create_bits(surface->format,
1265 width, height,
30f1e661 1266 NULL, width * 4);
69c77777
GH
1267 assert(surface->image != NULL);
1268
30f1e661 1269 surface->flags = QEMU_ALLOCATED_FLAG;
98b50080
PB
1270}
1271
da229ef3 1272DisplaySurface *qemu_create_displaysurface(int width, int height)
537a4391
GH
1273{
1274 DisplaySurface *surface = g_new0(DisplaySurface, 1);
da229ef3
GH
1275
1276 trace_displaysurface_create(surface, width, height);
30f1e661 1277 qemu_alloc_display(surface, width, height);
537a4391
GH
1278 return surface;
1279}
1280
30f1e661
GH
1281DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1282 pixman_format_code_t format,
1283 int linesize, uint8_t *data)
98b50080 1284{
69c77777 1285 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 1286
30f1e661
GH
1287 trace_displaysurface_create_from(surface, width, height, format);
1288 surface->format = format;
69c77777
GH
1289 surface->image = pixman_image_create_bits(surface->format,
1290 width, height,
1291 (void *)data, linesize);
1292 assert(surface->image != NULL);
1293
98b50080
PB
1294 return surface;
1295}
1296
ca58b45f
GH
1297DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1298{
1299 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1300
1301 trace_displaysurface_create_pixman(surface);
1302 surface->format = pixman_image_get_format(image);
1303 surface->image = pixman_image_ref(image);
1304
1305 return surface;
1306}
1307
a77549b3
GH
1308static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1309 void *unused)
1310{
1311 void *data = pixman_image_get_data(image);
1312 uint32_t size = pixman_image_get_stride(image) *
1313 pixman_image_get_height(image);
1314 cpu_physical_memory_unmap(data, size, 0, 0);
1315}
1316
1317DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1318 pixman_format_code_t format,
1319 int linesize, uint64_t addr)
1320{
1321 DisplaySurface *surface;
1322 hwaddr size;
1323 void *data;
1324
1325 if (linesize == 0) {
1326 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1327 }
1328
f76b84a0 1329 size = (hwaddr)linesize * height;
a77549b3 1330 data = cpu_physical_memory_map(addr, &size, 0);
f76b84a0 1331 if (size != (hwaddr)linesize * height) {
a77549b3
GH
1332 cpu_physical_memory_unmap(data, size, 0, 0);
1333 return NULL;
1334 }
1335
1336 surface = qemu_create_displaysurface_from
1337 (width, height, format, linesize, data);
1338 pixman_image_set_destroy_function
1339 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1340
1341 return surface;
1342}
1343
521a580d
GH
1344static DisplaySurface *qemu_create_message_surface(int w, int h,
1345 const char *msg)
d3002b04 1346{
521a580d 1347 DisplaySurface *surface = qemu_create_displaysurface(w, h);
4083733d
OH
1348 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1349 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
d3002b04
GH
1350 pixman_image_t *glyph;
1351 int len, x, y, i;
1352
1353 len = strlen(msg);
521a580d
GH
1354 x = (w / FONT_WIDTH - len) / 2;
1355 y = (h / FONT_HEIGHT - 1) / 2;
d3002b04
GH
1356 for (i = 0; i < len; i++) {
1357 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1358 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1359 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1360 qemu_pixman_image_unref(glyph);
1361 }
1362 return surface;
1363}
1364
da229ef3 1365void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 1366{
da229ef3 1367 if (surface == NULL) {
98b50080 1368 return;
187cd1d9 1369 }
da229ef3
GH
1370 trace_displaysurface_free(surface);
1371 qemu_pixman_image_unref(surface->image);
1372 g_free(surface);
98b50080
PB
1373}
1374
06020b95
GH
1375bool console_has_gl(QemuConsole *con)
1376{
1377 return con->gl != NULL;
1378}
1379
5209089f 1380void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 1381{
521a580d
GH
1382 static const char nodev[] =
1383 "This VM has no graphic display device.";
d3002b04 1384 static DisplaySurface *dummy;
284d1c6b
GH
1385 QemuConsole *con;
1386
06020b95
GH
1387 if (dcl->ops->dpy_gl_ctx_create) {
1388 /* display has opengl support */
1389 assert(dcl->con);
1390 if (dcl->con->gl) {
1391 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1392 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1393 exit(1);
1394 }
1395 dcl->con->gl = dcl;
1396 }
1397
7c20b4a3 1398 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
1399 dcl->ds = get_alloc_displaystate();
1400 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1401 gui_setup_refresh(dcl->ds);
284d1c6b
GH
1402 if (dcl->con) {
1403 dcl->con->dcls++;
1404 con = dcl->con;
1405 } else {
1406 con = active_console;
1407 }
d3002b04
GH
1408 if (dcl->ops->dpy_gfx_switch) {
1409 if (con) {
1410 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1411 } else {
1412 if (!dummy) {
521a580d 1413 dummy = qemu_create_message_surface(640, 480, nodev);
d3002b04
GH
1414 }
1415 dcl->ops->dpy_gfx_switch(dcl, dummy);
1416 }
7c20b4a3 1417 }
aea7947c 1418 text_console_update_cursor(NULL);
7c20b4a3
GH
1419}
1420
0f7b2864
GH
1421void update_displaychangelistener(DisplayChangeListener *dcl,
1422 uint64_t interval)
1423{
1424 DisplayState *ds = dcl->ds;
1425
1426 dcl->update_interval = interval;
1427 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 1428 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
1429 }
1430}
1431
7c20b4a3
GH
1432void unregister_displaychangelistener(DisplayChangeListener *dcl)
1433{
1434 DisplayState *ds = dcl->ds;
1435 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
1436 if (dcl->con) {
1437 dcl->con->dcls--;
1438 }
7c20b4a3
GH
1439 QLIST_REMOVE(dcl, next);
1440 gui_setup_refresh(ds);
1441}
1442
cf1ecc82
GH
1443static void dpy_set_ui_info_timer(void *opaque)
1444{
1445 QemuConsole *con = opaque;
1446
1447 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1448}
1449
b7fb49f0
GH
1450bool dpy_ui_info_supported(QemuConsole *con)
1451{
1452 return con->hw_ops->ui_info != NULL;
1453}
1454
6f90f3d7
GH
1455int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1456{
1457 assert(con != NULL);
1185fde4 1458
b7fb49f0 1459 if (!dpy_ui_info_supported(con)) {
cf1ecc82 1460 return -1;
6f90f3d7 1461 }
1185fde4
GH
1462 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1463 /* nothing changed -- ignore */
1464 return 0;
1465 }
cf1ecc82
GH
1466
1467 /*
1468 * Typically we get a flood of these as the user resizes the window.
1469 * Wait until the dust has settled (one second without updates), then
1470 * go notify the guest.
1471 */
1185fde4 1472 con->ui_info = *info;
cf1ecc82
GH
1473 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1474 return 0;
6f90f3d7
GH
1475}
1476
c78f7137 1477void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1478{
c78f7137 1479 DisplayState *s = con->ds;
284d1c6b 1480 DisplayChangeListener *dcl;
06020b95
GH
1481 int width = w;
1482 int height = h;
7c20b4a3 1483
06020b95
GH
1484 if (con->surface) {
1485 width = surface_width(con->surface);
1486 height = surface_height(con->surface);
1487 }
7c20b4a3
GH
1488 x = MAX(x, 0);
1489 y = MAX(y, 0);
1490 x = MIN(x, width);
1491 y = MIN(y, height);
1492 w = MIN(w, width - x);
1493 h = MIN(h, height - y);
1494
81c0d5a6 1495 if (!qemu_console_is_visible(con)) {
321f048d
GH
1496 return;
1497 }
7c20b4a3 1498 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1499 if (con != (dcl->con ? dcl->con : active_console)) {
1500 continue;
1501 }
7c20b4a3 1502 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1503 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1504 }
1505 }
1506}
1507
321f048d
GH
1508void dpy_gfx_replace_surface(QemuConsole *con,
1509 DisplaySurface *surface)
1510{
1511 DisplayState *s = con->ds;
1512 DisplaySurface *old_surface = con->surface;
284d1c6b 1513 DisplayChangeListener *dcl;
321f048d
GH
1514
1515 con->surface = surface;
284d1c6b
GH
1516 QLIST_FOREACH(dcl, &s->listeners, next) {
1517 if (con != (dcl->con ? dcl->con : active_console)) {
1518 continue;
1519 }
1520 if (dcl->ops->dpy_gfx_switch) {
1521 dcl->ops->dpy_gfx_switch(dcl, surface);
1522 }
321f048d 1523 }
da229ef3 1524 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1525}
1526
49743df3
BH
1527bool dpy_gfx_check_format(QemuConsole *con,
1528 pixman_format_code_t format)
1529{
1530 DisplayChangeListener *dcl;
1531 DisplayState *s = con->ds;
1532
1533 QLIST_FOREACH(dcl, &s->listeners, next) {
1534 if (dcl->con && dcl->con != con) {
1535 /* dcl bound to another console -> skip */
1536 continue;
1537 }
1538 if (dcl->ops->dpy_gfx_check_format) {
1539 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1540 return false;
1541 }
1542 } else {
1543 /* default is to whitelist native 32 bpp only */
1544 if (format != qemu_default_pixman_format(32, true)) {
1545 return false;
1546 }
1547 }
1548 }
1549 return true;
1550}
1551
6075137d 1552static void dpy_refresh(DisplayState *s)
7c20b4a3 1553{
284d1c6b
GH
1554 DisplayChangeListener *dcl;
1555
7c20b4a3
GH
1556 QLIST_FOREACH(dcl, &s->listeners, next) {
1557 if (dcl->ops->dpy_refresh) {
bc2ed970 1558 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1559 }
1560 }
1561}
1562
c78f7137
GH
1563void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1564 int dst_x, int dst_y, int w, int h)
7c20b4a3 1565{
c78f7137 1566 DisplayState *s = con->ds;
284d1c6b 1567 DisplayChangeListener *dcl;
321f048d 1568
81c0d5a6 1569 if (!qemu_console_is_visible(con)) {
321f048d
GH
1570 return;
1571 }
7c20b4a3 1572 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1573 if (con != (dcl->con ? dcl->con : active_console)) {
1574 continue;
1575 }
7c20b4a3 1576 if (dcl->ops->dpy_gfx_copy) {
bc2ed970 1577 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
7c20b4a3 1578 } else { /* TODO */
bc2ed970 1579 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
7c20b4a3
GH
1580 }
1581 }
1582}
1583
c78f7137 1584void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1585{
c78f7137 1586 DisplayState *s = con->ds;
284d1c6b 1587 DisplayChangeListener *dcl;
321f048d 1588
81c0d5a6 1589 if (!qemu_console_is_visible(con)) {
321f048d
GH
1590 return;
1591 }
7c20b4a3 1592 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1593 if (con != (dcl->con ? dcl->con : active_console)) {
1594 continue;
1595 }
7c20b4a3 1596 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1597 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1598 }
1599 }
1600}
1601
c78f7137 1602void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1603{
c78f7137 1604 DisplayState *s = con->ds;
284d1c6b 1605 DisplayChangeListener *dcl;
321f048d 1606
81c0d5a6 1607 if (!qemu_console_is_visible(con)) {
321f048d
GH
1608 return;
1609 }
7c20b4a3 1610 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1611 if (con != (dcl->con ? dcl->con : active_console)) {
1612 continue;
1613 }
7c20b4a3 1614 if (dcl->ops->dpy_text_update) {
bc2ed970 1615 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
1616 }
1617 }
1618}
1619
c78f7137 1620void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1621{
c78f7137 1622 DisplayState *s = con->ds;
9425c004 1623 DisplayChangeListener *dcl;
321f048d 1624
81c0d5a6 1625 if (!qemu_console_is_visible(con)) {
321f048d
GH
1626 return;
1627 }
7c20b4a3 1628 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1629 if (con != (dcl->con ? dcl->con : active_console)) {
1630 continue;
1631 }
7c20b4a3 1632 if (dcl->ops->dpy_text_resize) {
bc2ed970 1633 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1634 }
1635 }
1636}
1637
c78f7137 1638void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 1639{
c78f7137 1640 DisplayState *s = con->ds;
284d1c6b 1641 DisplayChangeListener *dcl;
321f048d 1642
81c0d5a6 1643 if (!qemu_console_is_visible(con)) {
321f048d
GH
1644 return;
1645 }
7c20b4a3 1646 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1647 if (con != (dcl->con ? dcl->con : active_console)) {
1648 continue;
1649 }
7c20b4a3 1650 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1651 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1652 }
1653 }
1654}
1655
c78f7137 1656void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 1657{
c78f7137 1658 DisplayState *s = con->ds;
284d1c6b 1659 DisplayChangeListener *dcl;
321f048d 1660
81c0d5a6 1661 if (!qemu_console_is_visible(con)) {
321f048d
GH
1662 return;
1663 }
7c20b4a3 1664 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1665 if (con != (dcl->con ? dcl->con : active_console)) {
1666 continue;
1667 }
7c20b4a3 1668 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1669 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1670 }
1671 }
1672}
1673
c78f7137 1674bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1675{
c78f7137 1676 DisplayState *s = con->ds;
284d1c6b
GH
1677 DisplayChangeListener *dcl;
1678
7c20b4a3
GH
1679 QLIST_FOREACH(dcl, &s->listeners, next) {
1680 if (dcl->ops->dpy_cursor_define) {
1681 return true;
1682 }
1683 }
1684 return false;
1685}
1686
06020b95
GH
1687QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1688 struct QEMUGLParams *qparams)
1689{
1690 assert(con->gl);
1691 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1692}
1693
1694void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1695{
1696 assert(con->gl);
1697 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1698}
1699
1700int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1701{
1702 assert(con->gl);
1703 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1704}
1705
1706QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1707{
1708 assert(con->gl);
1709 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1710}
1711
1712void dpy_gl_scanout(QemuConsole *con,
1713 uint32_t backing_id, bool backing_y_0_top,
9d8256eb 1714 uint32_t backing_width, uint32_t backing_height,
06020b95
GH
1715 uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1716{
1717 assert(con->gl);
1718 con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1719 backing_y_0_top,
9d8256eb 1720 backing_width, backing_height,
06020b95
GH
1721 x, y, width, height);
1722}
1723
1724void dpy_gl_update(QemuConsole *con,
1725 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1726{
1727 assert(con->gl);
1728 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1729}
1730
98b50080
PB
1731/***********************************************************/
1732/* register display */
1733
64840c66
GH
1734/* console.c internal use only */
1735static DisplayState *get_alloc_displaystate(void)
98b50080 1736{
64840c66
GH
1737 if (!display_state) {
1738 display_state = g_new0(DisplayState, 1);
aea7947c
GH
1739 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1740 text_console_update_cursor, NULL);
64840c66
GH
1741 }
1742 return display_state;
98b50080
PB
1743}
1744
64840c66
GH
1745/*
1746 * Called by main(), after creating QemuConsoles
1747 * and before initializing ui (sdl/vnc/...).
1748 */
1749DisplayState *init_displaystate(void)
98b50080 1750{
43f420f8 1751 gchar *name;
64840c66
GH
1752 int i;
1753
333cb18f 1754 get_alloc_displaystate();
64840c66
GH
1755 for (i = 0; i < nb_consoles; i++) {
1756 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1757 consoles[i]->ds == NULL) {
1758 text_console_do_init(consoles[i]->chr, display_state);
1759 }
43f420f8
GH
1760
1761 /* Hook up into the qom tree here (not in new_console()), once
1762 * all QemuConsoles are created and the order / numbering
1763 * doesn't change any more */
1764 name = g_strdup_printf("console[%d]", i);
1765 object_property_add_child(container_get(object_get_root(), "/backend"),
afff2b15 1766 name, OBJECT(consoles[i]), &error_abort);
43f420f8 1767 g_free(name);
64840c66
GH
1768 }
1769
98b50080
PB
1770 return display_state;
1771}
1772
1c1f9498
GH
1773void graphic_console_set_hwops(QemuConsole *con,
1774 const GraphicHwOps *hw_ops,
1775 void *opaque)
1776{
1777 con->hw_ops = hw_ops;
1778 con->hw = opaque;
1779}
1780
5643706a 1781QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 1782 const GraphicHwOps *hw_ops,
c78f7137 1783 void *opaque)
95219897 1784{
521a580d
GH
1785 static const char noinit[] =
1786 "Guest has not initialized the display (yet).";
64840c66
GH
1787 int width = 640;
1788 int height = 480;
76ffb0b4 1789 QemuConsole *s;
3023f332 1790 DisplayState *ds;
f0f2f976 1791
64840c66 1792 ds = get_alloc_displaystate();
437fe106 1793 trace_console_gfx_new();
afff2b15 1794 s = new_console(ds, GRAPHIC_CONSOLE, head);
cf1ecc82 1795 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1c1f9498 1796 graphic_console_set_hwops(s, hw_ops, opaque);
aa2beaa1 1797 if (dev) {
afff2b15
KB
1798 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1799 &error_abort);
aa2beaa1 1800 }
3023f332 1801
521a580d 1802 s->surface = qemu_create_message_surface(width, height, noinit);
c78f7137 1803 return s;
e7f0ad58
FB
1804}
1805
284d1c6b
GH
1806QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1807{
a1d2db08 1808 if (index >= nb_consoles) {
284d1c6b
GH
1809 return NULL;
1810 }
1811 return consoles[index];
1812}
1813
5643706a 1814QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649 1815{
14a93649 1816 Object *obj;
5643706a 1817 uint32_t h;
14a93649
GH
1818 int i;
1819
1820 for (i = 0; i < nb_consoles; i++) {
1821 if (!consoles[i]) {
1822 continue;
1823 }
1824 obj = object_property_get_link(OBJECT(consoles[i]),
afff2b15 1825 "device", &error_abort);
5643706a
GH
1826 if (DEVICE(obj) != dev) {
1827 continue;
1828 }
1829 h = object_property_get_int(OBJECT(consoles[i]),
afff2b15 1830 "head", &error_abort);
5643706a
GH
1831 if (h != head) {
1832 continue;
14a93649 1833 }
5643706a 1834 return consoles[i];
14a93649
GH
1835 }
1836 return NULL;
1837}
1838
f2c1d54c
GH
1839QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1840 uint32_t head, Error **errp)
1841{
1842 DeviceState *dev;
1843 QemuConsole *con;
1844
1845 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1846 if (dev == NULL) {
1847 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1848 "Device '%s' not found", device_id);
1849 return NULL;
1850 }
1851
1852 con = qemu_console_lookup_by_device(dev, head);
1853 if (con == NULL) {
1854 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1855 device_id, head);
1856 return NULL;
1857 }
1858
1859 return con;
1860}
1861
81c0d5a6 1862bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 1863{
284d1c6b 1864 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
1865}
1866
81c0d5a6 1867bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 1868{
81c0d5a6
GH
1869 if (con == NULL) {
1870 con = active_console;
1871 }
1872 return con && (con->console_type == GRAPHIC_CONSOLE);
1873}
1874
1875bool qemu_console_is_fixedsize(QemuConsole *con)
1876{
1877 if (con == NULL) {
1878 con = active_console;
1879 }
1880 return con && (con->console_type != TEXT_CONSOLE);
c21bbcfa
AZ
1881}
1882
f607867c
GH
1883bool qemu_console_is_gl_blocked(QemuConsole *con)
1884{
1885 assert(con != NULL);
1886 return con->gl_block;
1887}
1888
779ce88f
GH
1889char *qemu_console_get_label(QemuConsole *con)
1890{
1891 if (con->console_type == GRAPHIC_CONSOLE) {
1892 if (con->device) {
1893 return g_strdup(object_get_typename(con->device));
1894 }
1895 return g_strdup("VGA");
1896 } else {
1897 if (con->chr && con->chr->label) {
1898 return g_strdup(con->chr->label);
1899 }
1900 return g_strdup_printf("vc%d", con->index);
1901 }
1902}
1903
d4c85337
GH
1904int qemu_console_get_index(QemuConsole *con)
1905{
1906 if (con == NULL) {
1907 con = active_console;
1908 }
1909 return con ? con->index : -1;
1910}
1911
5643706a
GH
1912uint32_t qemu_console_get_head(QemuConsole *con)
1913{
1914 if (con == NULL) {
1915 con = active_console;
1916 }
1917 return con ? con->head : -1;
1918}
1919
6f90f3d7
GH
1920QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1921{
1922 assert(con != NULL);
1923 return &con->ui_info;
1924}
1925
d4c85337
GH
1926int qemu_console_get_width(QemuConsole *con, int fallback)
1927{
1928 if (con == NULL) {
1929 con = active_console;
1930 }
1931 return con ? surface_width(con->surface) : fallback;
1932}
1933
1934int qemu_console_get_height(QemuConsole *con, int fallback)
1935{
1936 if (con == NULL) {
1937 con = active_console;
1938 }
1939 return con ? surface_height(con->surface) : fallback;
1940}
1941
4104833f
PB
1942static void text_console_set_echo(CharDriverState *chr, bool echo)
1943{
76ffb0b4 1944 QemuConsole *s = chr->opaque;
4104833f
PB
1945
1946 s->echo = echo;
1947}
1948
aea7947c
GH
1949static void text_console_update_cursor_timer(void)
1950{
1951 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1952 + CONSOLE_CURSOR_PERIOD / 2);
1953}
1954
bf1bed81
JK
1955static void text_console_update_cursor(void *opaque)
1956{
aea7947c
GH
1957 QemuConsole *s;
1958 int i, count = 0;
1959
1960 cursor_visible_phase = !cursor_visible_phase;
bf1bed81 1961
aea7947c
GH
1962 for (i = 0; i < nb_consoles; i++) {
1963 s = consoles[i];
1964 if (qemu_console_is_graphic(s) ||
1965 !qemu_console_is_visible(s)) {
1966 continue;
1967 }
1968 count++;
1969 graphic_hw_invalidate(s);
1970 }
1971
1972 if (count) {
1973 text_console_update_cursor_timer();
1974 }
bf1bed81
JK
1975}
1976
380cd056
GH
1977static const GraphicHwOps text_console_ops = {
1978 .invalidate = text_console_invalidate,
1979 .text_update = text_console_update,
1980};
1981
44b37b93 1982static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
e7f0ad58 1983{
76ffb0b4 1984 QemuConsole *s;
36671fbd
GH
1985 int g_width = 80 * FONT_WIDTH;
1986 int g_height = 24 * FONT_HEIGHT;
6d6f7c28 1987
491e114a 1988 s = chr->opaque;
6ea314d9 1989
e7f0ad58 1990 chr->chr_write = console_puts;
6fcfafb7 1991
e15d7371
FB
1992 s->out_fifo.buf = s->out_fifo_buf;
1993 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
bc72ad67 1994 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
3023f332 1995 s->ds = ds;
3b46e624 1996
e7f0ad58
FB
1997 s->y_displayed = 0;
1998 s->y_base = 0;
1999 s->total_height = DEFAULT_BACKSCROLL;
2000 s->x = 0;
2001 s->y = 0;
36671fbd 2002 if (!s->surface) {
321f048d 2003 if (active_console && active_console->surface) {
36671fbd
GH
2004 g_width = surface_width(active_console->surface);
2005 g_height = surface_height(active_console->surface);
321f048d 2006 }
36671fbd 2007 s->surface = qemu_create_displaysurface(g_width, g_height);
491e114a 2008 }
6d6f7c28 2009
380cd056 2010 s->hw_ops = &text_console_ops;
4d3b6f6e
AZ
2011 s->hw = s;
2012
6d6f7c28
PB
2013 /* Set text attribute defaults */
2014 s->t_attrib_default.bold = 0;
2015 s->t_attrib_default.uline = 0;
2016 s->t_attrib_default.blink = 0;
2017 s->t_attrib_default.invers = 0;
2018 s->t_attrib_default.unvisible = 0;
4083733d
OH
2019 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2020 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
6d6f7c28
PB
2021 /* set current text attributes to default */
2022 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
2023 text_console_resize(s);
2024
51bfa4d3
GH
2025 if (chr->label) {
2026 char msg[128];
2027 int len;
2028
4083733d 2029 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
51bfa4d3
GH
2030 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2031 console_puts(chr, (uint8_t*)msg, len);
735ba588 2032 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
2033 }
2034
fee204fd 2035 qemu_chr_be_generic_open(chr);
e7f0ad58 2036}
c60e08d9 2037
fa19d025 2038static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2796dae0 2039{
d0d7708b 2040 ChardevCommon *common = qapi_ChardevVC_base(vc);
2796dae0 2041 CharDriverState *chr;
76ffb0b4 2042 QemuConsole *s;
702ec69c
GH
2043 unsigned width = 0;
2044 unsigned height = 0;
2796dae0 2045
d0d7708b
DB
2046 chr = qemu_chr_alloc(common, errp);
2047 if (!chr) {
2048 return NULL;
2049 }
2796dae0 2050
702ec69c
GH
2051 if (vc->has_width) {
2052 width = vc->width;
2053 } else if (vc->has_cols) {
2054 width = vc->cols * FONT_WIDTH;
2055 }
491e114a 2056
702ec69c
GH
2057 if (vc->has_height) {
2058 height = vc->height;
2059 } else if (vc->has_rows) {
2060 height = vc->rows * FONT_HEIGHT;
2061 }
491e114a 2062
437fe106 2063 trace_console_txt_new(width, height);
491e114a 2064 if (width == 0 || height == 0) {
afff2b15 2065 s = new_console(NULL, TEXT_CONSOLE, 0);
491e114a 2066 } else {
afff2b15 2067 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
36671fbd 2068 s->surface = qemu_create_displaysurface(width, height);
491e114a
PB
2069 }
2070
2071 if (!s) {
5354d083 2072 g_free(chr);
fa19d025 2073 error_setg(errp, "cannot create text console");
1f51470d 2074 return NULL;
491e114a
PB
2075 }
2076
2077 s->chr = chr;
491e114a 2078 chr->opaque = s;
4104833f 2079 chr->chr_set_echo = text_console_set_echo;
bd5c51ee
MR
2080 /* console/chardev init sometimes completes elsewhere in a 2nd
2081 * stage, so defer OPENED events until they are fully initialized
2082 */
2083 chr->explicit_be_open = true;
64840c66
GH
2084
2085 if (display_state) {
2086 text_console_do_init(chr, display_state);
2087 }
1f51470d 2088 return chr;
2796dae0
AL
2089}
2090
d82831db
AL
2091static VcHandler *vc_handler = text_console_init;
2092
fa19d025
PB
2093static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2094 ChardevReturn *ret, Error **errp)
d82831db 2095{
32bafa8f 2096 return vc_handler(backend->u.vc.data, errp);
d82831db
AL
2097}
2098
2099void register_vc_handler(VcHandler *handler)
2100{
2101 vc_handler = handler;
2102}
2103
c78f7137 2104void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 2105{
321f048d
GH
2106 DisplaySurface *surface;
2107
2108 assert(s->console_type == GRAPHIC_CONSOLE);
cd958edb
MAL
2109
2110 if (s->surface &&
2111 pixman_image_get_width(s->surface->image) == width &&
2112 pixman_image_get_height(s->surface->image) == height) {
2113 return;
2114 }
2115
321f048d
GH
2116 surface = qemu_create_displaysurface(width, height);
2117 dpy_gfx_replace_surface(s, surface);
c60e08d9 2118}
38334f76 2119
c78f7137 2120void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
3023f332 2121 int dst_x, int dst_y, int w, int h)
c21bbcfa 2122{
321f048d
GH
2123 assert(con->console_type == GRAPHIC_CONSOLE);
2124 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
38334f76 2125}
7d957bd8 2126
c78f7137
GH
2127DisplaySurface *qemu_console_surface(QemuConsole *console)
2128{
321f048d 2129 return console->surface;
c78f7137
GH
2130}
2131
0da2ea1b 2132PixelFormat qemu_default_pixelformat(int bpp)
2133{
56bd9ea1
GH
2134 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2135 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
7d957bd8
AL
2136 return pf;
2137}
01f45d98 2138
702ec69c
GH
2139static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2140 Error **errp)
2141{
2142 int val;
21a933ea 2143 ChardevVC *vc;
702ec69c 2144
32bafa8f 2145 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
21a933ea 2146 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
702ec69c
GH
2147
2148 val = qemu_opt_get_number(opts, "width", 0);
2149 if (val != 0) {
21a933ea
EB
2150 vc->has_width = true;
2151 vc->width = val;
702ec69c
GH
2152 }
2153
2154 val = qemu_opt_get_number(opts, "height", 0);
2155 if (val != 0) {
21a933ea
EB
2156 vc->has_height = true;
2157 vc->height = val;
702ec69c
GH
2158 }
2159
2160 val = qemu_opt_get_number(opts, "cols", 0);
2161 if (val != 0) {
21a933ea
EB
2162 vc->has_cols = true;
2163 vc->cols = val;
702ec69c
GH
2164 }
2165
2166 val = qemu_opt_get_number(opts, "rows", 0);
2167 if (val != 0) {
21a933ea
EB
2168 vc->has_rows = true;
2169 vc->rows = val;
702ec69c
GH
2170 }
2171}
2172
95be0669
GH
2173static const TypeInfo qemu_console_info = {
2174 .name = TYPE_QEMU_CONSOLE,
2175 .parent = TYPE_OBJECT,
2176 .instance_size = sizeof(QemuConsole),
2177 .class_size = sizeof(QemuConsoleClass),
2178};
2179
2180
01f45d98
AL
2181static void register_types(void)
2182{
95be0669 2183 type_register_static(&qemu_console_info);
4ca17281 2184 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
fa19d025 2185 vc_init);
01f45d98
AL
2186}
2187
2188type_init(register_types);