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