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