]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
console: add device link to QemuConsoles
[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"
e7f0ad58 30
6d6f7c28 31//#define DEBUG_CONSOLE
e7f0ad58
FB
32#define DEFAULT_BACKSCROLL 512
33#define MAX_CONSOLES 12
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;
380cd056 127 const GraphicHwOps *hw_ops;
95219897 128 void *hw;
76ffb0b4
GH
129
130 /* Text console state */
e7f0ad58
FB
131 int width;
132 int height;
133 int total_height;
134 int backscroll_height;
e7f0ad58 135 int x, y;
adb47967 136 int x_saved, y_saved;
e7f0ad58
FB
137 int y_displayed;
138 int y_base;
6d6f7c28
PB
139 TextAttributes t_attrib_default; /* default text attributes */
140 TextAttributes t_attrib; /* currently active text attributes */
e7f0ad58 141 TextCell *cells;
4d3b6f6e 142 int text_x[2], text_y[2], cursor_invalidate;
4104833f 143 int echo;
bf1bed81
JK
144 bool cursor_visible_phase;
145 QEMUTimer *cursor_timer;
e7f0ad58 146
14778c20
PB
147 int update_x0;
148 int update_y0;
149 int update_x1;
150 int update_y1;
151
e7f0ad58
FB
152 enum TTYState state;
153 int esc_params[MAX_ESC_PARAMS];
154 int nb_esc_params;
155
e5b0bc44 156 CharDriverState *chr;
e15d7371
FB
157 /* fifo for key pressed */
158 QEMUFIFO out_fifo;
159 uint8_t out_fifo_buf[16];
160 QEMUTimer *kbd_timer;
e7f0ad58
FB
161};
162
27be5587
GH
163struct DisplayState {
164 struct QEMUTimer *gui_timer;
0f7b2864
GH
165 uint64_t last_update;
166 uint64_t update_interval;
167 bool refreshing;
27be5587
GH
168 bool have_gfx;
169 bool have_text;
170
171 QLIST_HEAD(, DisplayChangeListener) listeners;
172};
173
98b50080 174static DisplayState *display_state;
76ffb0b4
GH
175static QemuConsole *active_console;
176static QemuConsole *consoles[MAX_CONSOLES];
e7f0ad58
FB
177static int nb_consoles = 0;
178
64840c66 179static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
0f7b2864 180static void dpy_refresh(DisplayState *s);
64840c66 181
98a9ad90
GH
182static void gui_update(void *opaque)
183{
0f7b2864
GH
184 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
185 uint64_t dcl_interval;
98a9ad90
GH
186 DisplayState *ds = opaque;
187 DisplayChangeListener *dcl;
dea1b0bd 188 int i;
98a9ad90 189
0f7b2864 190 ds->refreshing = true;
98a9ad90 191 dpy_refresh(ds);
0f7b2864 192 ds->refreshing = false;
98a9ad90
GH
193
194 QLIST_FOREACH(dcl, &ds->listeners, next) {
0f7b2864
GH
195 dcl_interval = dcl->update_interval ?
196 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
197 if (interval > dcl_interval) {
198 interval = dcl_interval;
98a9ad90
GH
199 }
200 }
0f7b2864
GH
201 if (ds->update_interval != interval) {
202 ds->update_interval = interval;
dea1b0bd
GH
203 for (i = 0; i < nb_consoles; i++) {
204 if (consoles[i]->hw_ops->update_interval) {
205 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
206 }
207 }
0f7b2864
GH
208 trace_console_refresh(interval);
209 }
210 ds->last_update = qemu_get_clock_ms(rt_clock);
211 qemu_mod_timer(ds->gui_timer, ds->last_update + interval);
98a9ad90
GH
212}
213
214static void gui_setup_refresh(DisplayState *ds)
215{
216 DisplayChangeListener *dcl;
217 bool need_timer = false;
218 bool have_gfx = false;
219 bool have_text = false;
220
221 QLIST_FOREACH(dcl, &ds->listeners, next) {
222 if (dcl->ops->dpy_refresh != NULL) {
223 need_timer = true;
224 }
225 if (dcl->ops->dpy_gfx_update != NULL) {
226 have_gfx = true;
227 }
228 if (dcl->ops->dpy_text_update != NULL) {
229 have_text = true;
230 }
231 }
232
233 if (need_timer && ds->gui_timer == NULL) {
234 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
235 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
236 }
237 if (!need_timer && ds->gui_timer != NULL) {
238 qemu_del_timer(ds->gui_timer);
239 qemu_free_timer(ds->gui_timer);
240 ds->gui_timer = NULL;
241 }
242
243 ds->have_gfx = have_gfx;
244 ds->have_text = have_text;
245}
246
1dbfa005 247void graphic_hw_update(QemuConsole *con)
95219897 248{
1dbfa005
GH
249 if (!con) {
250 con = active_console;
251 }
380cd056
GH
252 if (con && con->hw_ops->gfx_update) {
253 con->hw_ops->gfx_update(con->hw);
1dbfa005 254 }
95219897
PB
255}
256
1dbfa005 257void graphic_hw_invalidate(QemuConsole *con)
95219897 258{
1dbfa005
GH
259 if (!con) {
260 con = active_console;
261 }
380cd056
GH
262 if (con && con->hw_ops->invalidate) {
263 con->hw_ops->invalidate(con->hw);
1dbfa005 264 }
95219897
PB
265}
266
2c62f08d
GH
267static void ppm_save(const char *filename, struct DisplaySurface *ds,
268 Error **errp)
95219897 269{
2c62f08d
GH
270 int width = pixman_image_get_width(ds->image);
271 int height = pixman_image_get_height(ds->image);
272 FILE *f;
273 int y;
274 int ret;
275 pixman_image_t *linebuf;
276
277 trace_ppm_save(filename, ds);
278 f = fopen(filename, "wb");
279 if (!f) {
280 error_setg(errp, "failed to open file '%s': %s", filename,
281 strerror(errno));
282 return;
45efb161 283 }
2c62f08d
GH
284 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
285 if (ret < 0) {
286 linebuf = NULL;
287 goto write_err;
288 }
289 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
290 for (y = 0; y < height; y++) {
291 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
292 clearerr(f);
293 ret = fwrite(pixman_image_get_data(linebuf), 1,
294 pixman_image_get_stride(linebuf), f);
295 (void)ret;
296 if (ferror(f)) {
297 goto write_err;
298 }
f81bdefb
JK
299 }
300
2c62f08d
GH
301out:
302 qemu_pixman_image_unref(linebuf);
303 fclose(f);
304 return;
305
306write_err:
307 error_setg(errp, "failed to write to file '%s': %s", filename,
308 strerror(errno));
309 unlink(filename);
310 goto out;
311}
312
313void qmp_screendump(const char *filename, Error **errp)
314{
284d1c6b 315 QemuConsole *con = qemu_console_lookup_by_index(0);
2c62f08d
GH
316 DisplaySurface *surface;
317
318 if (con == NULL) {
319 error_setg(errp, "There is no QemuConsole I can screendump from.");
320 return;
33bcd98c 321 }
2c62f08d
GH
322
323 graphic_hw_update(con);
324 surface = qemu_console_surface(con);
325 ppm_save(filename, surface, errp);
95219897
PB
326}
327
1dbfa005 328void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
4d3b6f6e 329{
1dbfa005
GH
330 if (!con) {
331 con = active_console;
332 }
380cd056
GH
333 if (con && con->hw_ops->text_update) {
334 con->hw_ops->text_update(con->hw, chardata);
335 }
4d3b6f6e
AZ
336}
337
1562e531
GH
338static void vga_fill_rect(QemuConsole *con,
339 int posx, int posy, int width, int height,
e27bd65a 340 pixman_color_t color)
e7f0ad58 341{
1562e531 342 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
343 pixman_rectangle16_t rect = {
344 .x = posx, .y = posy, .width = width, .height = height
345 };
68db6dc5 346
68db6dc5 347 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
e27bd65a 348 &color, 1, &rect);
e7f0ad58
FB
349}
350
351/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
1562e531
GH
352static void vga_bitblt(QemuConsole *con,
353 int xs, int ys, int xd, int yd, int w, int h)
e7f0ad58 354{
1562e531 355 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
356
357 pixman_image_composite(PIXMAN_OP_SRC,
358 surface->image, NULL, surface->image,
359 xs, ys, 0, 0, xd, yd, w, h);
e7f0ad58
FB
360}
361
362/***********************************************************/
363/* basic char display */
364
365#define FONT_HEIGHT 16
366#define FONT_WIDTH 8
367
368#include "vgafont.h"
369
df00bed0 370#ifndef CONFIG_CURSES
6d6f7c28
PB
371enum color_names {
372 COLOR_BLACK = 0,
373 COLOR_RED = 1,
374 COLOR_GREEN = 2,
375 COLOR_YELLOW = 3,
376 COLOR_BLUE = 4,
377 COLOR_MAGENTA = 5,
378 COLOR_CYAN = 6,
379 COLOR_WHITE = 7
380};
df00bed0 381#endif
6d6f7c28 382
e27bd65a
GH
383#define QEMU_RGB(r, g, b) \
384 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
385
386static const pixman_color_t color_table_rgb[2][8] = {
6d6f7c28 387 { /* dark */
26489844
FB
388 QEMU_RGB(0x00, 0x00, 0x00), /* black */
389 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
390 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
391 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
392 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
393 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
394 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
395 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
6d6f7c28
PB
396 },
397 { /* bright */
26489844
FB
398 QEMU_RGB(0x00, 0x00, 0x00), /* black */
399 QEMU_RGB(0xff, 0x00, 0x00), /* red */
400 QEMU_RGB(0x00, 0xff, 0x00), /* green */
401 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
402 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
403 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
404 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
405 QEMU_RGB(0xff, 0xff, 0xff), /* white */
6d6f7c28 406 }
e7f0ad58
FB
407};
408
6d6f7c28
PB
409#ifdef DEBUG_CONSOLE
410static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
411{
412 if (t_attrib->bold) {
413 printf("b");
414 } else {
415 printf(" ");
416 }
417 if (t_attrib->uline) {
418 printf("u");
419 } else {
420 printf(" ");
421 }
422 if (t_attrib->blink) {
423 printf("l");
424 } else {
425 printf(" ");
426 }
427 if (t_attrib->invers) {
428 printf("i");
429 } else {
430 printf(" ");
431 }
432 if (t_attrib->unvisible) {
433 printf("n");
434 } else {
435 printf(" ");
436 }
437
438 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
439}
440#endif
e7f0ad58 441
1562e531 442static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
6d6f7c28 443 TextAttributes *t_attrib)
e7f0ad58 444{
7d6ba01c 445 static pixman_image_t *glyphs[256];
1562e531 446 DisplaySurface *surface = qemu_console_surface(s);
e27bd65a 447 pixman_color_t fgcol, bgcol;
6d6f7c28
PB
448
449 if (t_attrib->invers) {
cf6f0548
GH
450 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
451 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
6d6f7c28 452 } else {
cf6f0548
GH
453 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
454 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
6d6f7c28 455 }
e7f0ad58 456
7d6ba01c
GH
457 if (!glyphs[ch]) {
458 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
e7f0ad58 459 }
7d6ba01c 460 qemu_pixman_glyph_render(glyphs[ch], surface->image,
e27bd65a 461 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
e7f0ad58
FB
462}
463
76ffb0b4 464static void text_console_resize(QemuConsole *s)
e7f0ad58
FB
465{
466 TextCell *cells, *c, *c1;
467 int w1, x, y, last_width;
468
469 last_width = s->width;
36671fbd
GH
470 s->width = surface_width(s->surface) / FONT_WIDTH;
471 s->height = surface_height(s->surface) / FONT_HEIGHT;
e7f0ad58
FB
472
473 w1 = last_width;
474 if (s->width < w1)
475 w1 = s->width;
476
7267c094 477 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
e7f0ad58
FB
478 for(y = 0; y < s->total_height; y++) {
479 c = &cells[y * s->width];
480 if (w1 > 0) {
481 c1 = &s->cells[y * last_width];
482 for(x = 0; x < w1; x++) {
483 *c++ = *c1++;
484 }
485 }
486 for(x = w1; x < s->width; x++) {
487 c->ch = ' ';
6d6f7c28 488 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
489 c++;
490 }
491 }
7267c094 492 g_free(s->cells);
e7f0ad58
FB
493 s->cells = cells;
494}
495
76ffb0b4 496static inline void text_update_xy(QemuConsole *s, int x, int y)
4d3b6f6e
AZ
497{
498 s->text_x[0] = MIN(s->text_x[0], x);
499 s->text_x[1] = MAX(s->text_x[1], x);
500 s->text_y[0] = MIN(s->text_y[0], y);
501 s->text_y[1] = MAX(s->text_y[1], y);
502}
503
76ffb0b4 504static void invalidate_xy(QemuConsole *s, int x, int y)
14778c20
PB
505{
506 if (s->update_x0 > x * FONT_WIDTH)
507 s->update_x0 = x * FONT_WIDTH;
508 if (s->update_y0 > y * FONT_HEIGHT)
509 s->update_y0 = y * FONT_HEIGHT;
510 if (s->update_x1 < (x + 1) * FONT_WIDTH)
511 s->update_x1 = (x + 1) * FONT_WIDTH;
512 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
513 s->update_y1 = (y + 1) * FONT_HEIGHT;
514}
515
76ffb0b4 516static void update_xy(QemuConsole *s, int x, int y)
e7f0ad58
FB
517{
518 TextCell *c;
519 int y1, y2;
520
81c0d5a6 521 if (!qemu_console_is_visible(s)) {
1562e531
GH
522 return;
523 }
524
525 if (s->ds->have_text) {
526 text_update_xy(s, x, y);
527 }
4d3b6f6e 528
1562e531 529 if (s->ds->have_gfx) {
e7f0ad58
FB
530 y1 = (s->y_base + y) % s->total_height;
531 y2 = y1 - s->y_displayed;
532 if (y2 < 0)
533 y2 += s->total_height;
534 if (y2 < s->height) {
535 c = &s->cells[y1 * s->width + x];
1562e531 536 vga_putcharxy(s, x, y2, c->ch,
6d6f7c28 537 &(c->t_attrib));
14778c20 538 invalidate_xy(s, x, y2);
e7f0ad58
FB
539 }
540 }
541}
542
76ffb0b4 543static void console_show_cursor(QemuConsole *s, int show)
e7f0ad58
FB
544{
545 TextCell *c;
546 int y, y1;
1562e531 547 int x = s->x;
e7f0ad58 548
81c0d5a6 549 if (!qemu_console_is_visible(s)) {
1562e531
GH
550 return;
551 }
4d3b6f6e 552
1562e531
GH
553 if (s->ds->have_text) {
554 s->cursor_invalidate = 1;
555 }
4d3b6f6e 556
1562e531 557 if (s->ds->have_gfx) {
ed8276ac
TS
558 if (x >= s->width) {
559 x = s->width - 1;
560 }
e7f0ad58
FB
561 y1 = (s->y_base + s->y) % s->total_height;
562 y = y1 - s->y_displayed;
563 if (y < 0)
564 y += s->total_height;
565 if (y < s->height) {
ed8276ac 566 c = &s->cells[y1 * s->width + x];
bf1bed81 567 if (show && s->cursor_visible_phase) {
6d6f7c28
PB
568 TextAttributes t_attrib = s->t_attrib_default;
569 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
1562e531 570 vga_putcharxy(s, x, y, c->ch, &t_attrib);
e7f0ad58 571 } else {
1562e531 572 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
e7f0ad58 573 }
14778c20 574 invalidate_xy(s, x, y);
e7f0ad58
FB
575 }
576 }
577}
578
76ffb0b4 579static void console_refresh(QemuConsole *s)
e7f0ad58 580{
1562e531 581 DisplaySurface *surface = qemu_console_surface(s);
e7f0ad58
FB
582 TextCell *c;
583 int x, y, y1;
584
81c0d5a6 585 if (!qemu_console_is_visible(s)) {
e7f0ad58 586 return;
81c0d5a6 587 }
a93a4a22
GH
588
589 if (s->ds->have_text) {
4d3b6f6e
AZ
590 s->text_x[0] = 0;
591 s->text_y[0] = 0;
592 s->text_x[1] = s->width - 1;
593 s->text_y[1] = s->height - 1;
594 s->cursor_invalidate = 1;
4d3b6f6e 595 }
e7f0ad58 596
a93a4a22 597 if (s->ds->have_gfx) {
1562e531 598 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
cf6f0548 599 color_table_rgb[0][COLOR_BLACK]);
a93a4a22
GH
600 y1 = s->y_displayed;
601 for (y = 0; y < s->height; y++) {
602 c = s->cells + y1 * s->width;
603 for (x = 0; x < s->width; x++) {
1562e531 604 vga_putcharxy(s, x, y, c->ch,
a93a4a22
GH
605 &(c->t_attrib));
606 c++;
607 }
608 if (++y1 == s->total_height) {
609 y1 = 0;
610 }
e7f0ad58 611 }
a93a4a22 612 console_show_cursor(s, 1);
1562e531
GH
613 dpy_gfx_update(s, 0, 0,
614 surface_width(surface), surface_height(surface));
e7f0ad58 615 }
e7f0ad58
FB
616}
617
81c0d5a6 618static void console_scroll(QemuConsole *s, int ydelta)
e7f0ad58 619{
e7f0ad58 620 int i, y1;
3b46e624 621
e7f0ad58
FB
622 if (ydelta > 0) {
623 for(i = 0; i < ydelta; i++) {
624 if (s->y_displayed == s->y_base)
625 break;
626 if (++s->y_displayed == s->total_height)
627 s->y_displayed = 0;
628 }
629 } else {
630 ydelta = -ydelta;
631 i = s->backscroll_height;
632 if (i > s->total_height - s->height)
633 i = s->total_height - s->height;
634 y1 = s->y_base - i;
635 if (y1 < 0)
636 y1 += s->total_height;
637 for(i = 0; i < ydelta; i++) {
638 if (s->y_displayed == y1)
639 break;
640 if (--s->y_displayed < 0)
641 s->y_displayed = s->total_height - 1;
642 }
643 }
644 console_refresh(s);
645}
646
76ffb0b4 647static void console_put_lf(QemuConsole *s)
e7f0ad58
FB
648{
649 TextCell *c;
650 int x, y1;
651
e7f0ad58
FB
652 s->y++;
653 if (s->y >= s->height) {
654 s->y = s->height - 1;
6d6f7c28 655
e7f0ad58
FB
656 if (s->y_displayed == s->y_base) {
657 if (++s->y_displayed == s->total_height)
658 s->y_displayed = 0;
659 }
660 if (++s->y_base == s->total_height)
661 s->y_base = 0;
662 if (s->backscroll_height < s->total_height)
663 s->backscroll_height++;
664 y1 = (s->y_base + s->height - 1) % s->total_height;
665 c = &s->cells[y1 * s->width];
666 for(x = 0; x < s->width; x++) {
667 c->ch = ' ';
6d6f7c28 668 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
669 c++;
670 }
81c0d5a6 671 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
1562e531 672 if (s->ds->have_text) {
4d3b6f6e
AZ
673 s->text_x[0] = 0;
674 s->text_y[0] = 0;
675 s->text_x[1] = s->width - 1;
676 s->text_y[1] = s->height - 1;
4d3b6f6e
AZ
677 }
678
1562e531
GH
679 if (s->ds->have_gfx) {
680 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
681 s->width * FONT_WIDTH,
682 (s->height - 1) * FONT_HEIGHT);
683 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
684 s->width * FONT_WIDTH, FONT_HEIGHT,
685 color_table_rgb[0][s->t_attrib_default.bgcol]);
686 s->update_x0 = 0;
687 s->update_y0 = 0;
688 s->update_x1 = s->width * FONT_WIDTH;
689 s->update_y1 = s->height * FONT_HEIGHT;
690 }
e7f0ad58
FB
691 }
692 }
693}
694
6d6f7c28
PB
695/* Set console attributes depending on the current escape codes.
696 * NOTE: I know this code is not very efficient (checking every color for it
697 * self) but it is more readable and better maintainable.
698 */
76ffb0b4 699static void console_handle_escape(QemuConsole *s)
6d6f7c28
PB
700{
701 int i;
702
6d6f7c28
PB
703 for (i=0; i<s->nb_esc_params; i++) {
704 switch (s->esc_params[i]) {
705 case 0: /* reset all console attributes to default */
706 s->t_attrib = s->t_attrib_default;
707 break;
708 case 1:
709 s->t_attrib.bold = 1;
710 break;
711 case 4:
712 s->t_attrib.uline = 1;
713 break;
714 case 5:
715 s->t_attrib.blink = 1;
716 break;
717 case 7:
718 s->t_attrib.invers = 1;
719 break;
720 case 8:
721 s->t_attrib.unvisible = 1;
722 break;
723 case 22:
724 s->t_attrib.bold = 0;
725 break;
726 case 24:
727 s->t_attrib.uline = 0;
728 break;
729 case 25:
730 s->t_attrib.blink = 0;
731 break;
732 case 27:
733 s->t_attrib.invers = 0;
734 break;
735 case 28:
736 s->t_attrib.unvisible = 0;
737 break;
738 /* set foreground color */
739 case 30:
740 s->t_attrib.fgcol=COLOR_BLACK;
741 break;
742 case 31:
743 s->t_attrib.fgcol=COLOR_RED;
744 break;
745 case 32:
746 s->t_attrib.fgcol=COLOR_GREEN;
747 break;
748 case 33:
749 s->t_attrib.fgcol=COLOR_YELLOW;
750 break;
751 case 34:
752 s->t_attrib.fgcol=COLOR_BLUE;
753 break;
754 case 35:
755 s->t_attrib.fgcol=COLOR_MAGENTA;
756 break;
757 case 36:
758 s->t_attrib.fgcol=COLOR_CYAN;
759 break;
760 case 37:
761 s->t_attrib.fgcol=COLOR_WHITE;
762 break;
763 /* set background color */
764 case 40:
765 s->t_attrib.bgcol=COLOR_BLACK;
766 break;
767 case 41:
768 s->t_attrib.bgcol=COLOR_RED;
769 break;
770 case 42:
771 s->t_attrib.bgcol=COLOR_GREEN;
772 break;
773 case 43:
774 s->t_attrib.bgcol=COLOR_YELLOW;
775 break;
776 case 44:
777 s->t_attrib.bgcol=COLOR_BLUE;
778 break;
779 case 45:
780 s->t_attrib.bgcol=COLOR_MAGENTA;
781 break;
782 case 46:
783 s->t_attrib.bgcol=COLOR_CYAN;
784 break;
785 case 47:
786 s->t_attrib.bgcol=COLOR_WHITE;
787 break;
788 }
789 }
790}
791
76ffb0b4 792static void console_clear_xy(QemuConsole *s, int x, int y)
adb47967
TS
793{
794 int y1 = (s->y_base + y) % s->total_height;
795 TextCell *c = &s->cells[y1 * s->width + x];
796 c->ch = ' ';
797 c->t_attrib = s->t_attrib_default;
adb47967
TS
798 update_xy(s, x, y);
799}
800
3eea5498 801/* set cursor, checking bounds */
76ffb0b4 802static void set_cursor(QemuConsole *s, int x, int y)
3eea5498
IC
803{
804 if (x < 0) {
805 x = 0;
806 }
807 if (y < 0) {
808 y = 0;
809 }
810 if (y >= s->height) {
811 y = s->height - 1;
812 }
813 if (x >= s->width) {
814 x = s->width - 1;
815 }
816
817 s->x = x;
818 s->y = y;
819}
820
76ffb0b4 821static void console_putchar(QemuConsole *s, int ch)
e7f0ad58
FB
822{
823 TextCell *c;
adb47967
TS
824 int y1, i;
825 int x, y;
e7f0ad58
FB
826
827 switch(s->state) {
828 case TTY_STATE_NORM:
829 switch(ch) {
6d6f7c28 830 case '\r': /* carriage return */
e7f0ad58
FB
831 s->x = 0;
832 break;
6d6f7c28 833 case '\n': /* newline */
e7f0ad58
FB
834 console_put_lf(s);
835 break;
6d6f7c28 836 case '\b': /* backspace */
5fafdf24 837 if (s->x > 0)
e15d7371 838 s->x--;
6d6f7c28
PB
839 break;
840 case '\t': /* tabspace */
841 if (s->x + (8 - (s->x % 8)) > s->width) {
bd468840 842 s->x = 0;
6d6f7c28
PB
843 console_put_lf(s);
844 } else {
845 s->x = s->x + (8 - (s->x % 8));
846 }
847 break;
848 case '\a': /* alert aka. bell */
849 /* TODO: has to be implemented */
850 break;
adb47967
TS
851 case 14:
852 /* SI (shift in), character set 0 (ignored) */
853 break;
854 case 15:
855 /* SO (shift out), character set 1 (ignored) */
856 break;
6d6f7c28 857 case 27: /* esc (introducing an escape sequence) */
e7f0ad58
FB
858 s->state = TTY_STATE_ESC;
859 break;
860 default:
ed8276ac
TS
861 if (s->x >= s->width) {
862 /* line wrap */
863 s->x = 0;
864 console_put_lf(s);
adb47967 865 }
e7f0ad58
FB
866 y1 = (s->y_base + s->y) % s->total_height;
867 c = &s->cells[y1 * s->width + s->x];
868 c->ch = ch;
6d6f7c28 869 c->t_attrib = s->t_attrib;
e7f0ad58
FB
870 update_xy(s, s->x, s->y);
871 s->x++;
e7f0ad58
FB
872 break;
873 }
874 break;
6d6f7c28 875 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
e7f0ad58
FB
876 if (ch == '[') {
877 for(i=0;i<MAX_ESC_PARAMS;i++)
878 s->esc_params[i] = 0;
879 s->nb_esc_params = 0;
880 s->state = TTY_STATE_CSI;
881 } else {
882 s->state = TTY_STATE_NORM;
883 }
884 break;
6d6f7c28 885 case TTY_STATE_CSI: /* handle escape sequence parameters */
e7f0ad58
FB
886 if (ch >= '0' && ch <= '9') {
887 if (s->nb_esc_params < MAX_ESC_PARAMS) {
c10600af
LE
888 int *param = &s->esc_params[s->nb_esc_params];
889 int digit = (ch - '0');
890
891 *param = (*param <= (INT_MAX - digit) / 10) ?
892 *param * 10 + digit : INT_MAX;
e7f0ad58
FB
893 }
894 } else {
3eea5498
IC
895 if (s->nb_esc_params < MAX_ESC_PARAMS)
896 s->nb_esc_params++;
e7f0ad58
FB
897 if (ch == ';')
898 break;
adb47967
TS
899#ifdef DEBUG_CONSOLE
900 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
901 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
902#endif
e7f0ad58
FB
903 s->state = TTY_STATE_NORM;
904 switch(ch) {
adb47967
TS
905 case 'A':
906 /* move cursor up */
907 if (s->esc_params[0] == 0) {
908 s->esc_params[0] = 1;
909 }
3eea5498 910 set_cursor(s, s->x, s->y - s->esc_params[0]);
adb47967
TS
911 break;
912 case 'B':
913 /* move cursor down */
914 if (s->esc_params[0] == 0) {
915 s->esc_params[0] = 1;
916 }
3eea5498 917 set_cursor(s, s->x, s->y + s->esc_params[0]);
e7f0ad58
FB
918 break;
919 case 'C':
adb47967
TS
920 /* move cursor right */
921 if (s->esc_params[0] == 0) {
922 s->esc_params[0] = 1;
923 }
3eea5498 924 set_cursor(s, s->x + s->esc_params[0], s->y);
e7f0ad58 925 break;
adb47967
TS
926 case 'D':
927 /* move cursor left */
928 if (s->esc_params[0] == 0) {
929 s->esc_params[0] = 1;
930 }
3eea5498 931 set_cursor(s, s->x - s->esc_params[0], s->y);
adb47967
TS
932 break;
933 case 'G':
934 /* move cursor to column */
3eea5498 935 set_cursor(s, s->esc_params[0] - 1, s->y);
adb47967
TS
936 break;
937 case 'f':
938 case 'H':
939 /* move cursor to row, column */
3eea5498 940 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
adb47967
TS
941 break;
942 case 'J':
943 switch (s->esc_params[0]) {
944 case 0:
945 /* clear to end of screen */
946 for (y = s->y; y < s->height; y++) {
947 for (x = 0; x < s->width; x++) {
948 if (y == s->y && x < s->x) {
949 continue;
950 }
951 console_clear_xy(s, x, y);
952 }
953 }
954 break;
955 case 1:
956 /* clear from beginning of screen */
957 for (y = 0; y <= s->y; y++) {
958 for (x = 0; x < s->width; x++) {
959 if (y == s->y && x > s->x) {
960 break;
961 }
962 console_clear_xy(s, x, y);
963 }
964 }
965 break;
966 case 2:
967 /* clear entire screen */
968 for (y = 0; y <= s->height; y++) {
969 for (x = 0; x < s->width; x++) {
970 console_clear_xy(s, x, y);
971 }
972 }
f94a950f 973 break;
adb47967 974 }
95d8f9f4 975 break;
e7f0ad58 976 case 'K':
adb47967
TS
977 switch (s->esc_params[0]) {
978 case 0:
f94a950f
MA
979 /* clear to eol */
980 for(x = s->x; x < s->width; x++) {
adb47967 981 console_clear_xy(s, x, s->y);
f94a950f
MA
982 }
983 break;
adb47967
TS
984 case 1:
985 /* clear from beginning of line */
986 for (x = 0; x <= s->x; x++) {
987 console_clear_xy(s, x, s->y);
988 }
989 break;
990 case 2:
991 /* clear entire line */
992 for(x = 0; x < s->width; x++) {
993 console_clear_xy(s, x, s->y);
994 }
f94a950f
MA
995 break;
996 }
adb47967
TS
997 break;
998 case 'm':
f94a950f
MA
999 console_handle_escape(s);
1000 break;
adb47967
TS
1001 case 'n':
1002 /* report cursor position */
1003 /* TODO: send ESC[row;colR */
1004 break;
1005 case 's':
1006 /* save cursor position */
1007 s->x_saved = s->x;
1008 s->y_saved = s->y;
1009 break;
1010 case 'u':
1011 /* restore cursor position */
1012 s->x = s->x_saved;
1013 s->y = s->y_saved;
1014 break;
1015 default:
1016#ifdef DEBUG_CONSOLE
1017 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1018#endif
1019 break;
1020 }
1021 break;
e7f0ad58
FB
1022 }
1023 }
1024}
1025
1026void console_select(unsigned int index)
1027{
284d1c6b 1028 DisplayChangeListener *dcl;
76ffb0b4 1029 QemuConsole *s;
6d6f7c28 1030
e7f0ad58
FB
1031 if (index >= MAX_CONSOLES)
1032 return;
437fe106
GH
1033
1034 trace_console_select(index);
284d1c6b 1035 s = qemu_console_lookup_by_index(index);
e7f0ad58 1036 if (s) {
7d957bd8 1037 DisplayState *ds = s->ds;
bf1bed81 1038
8bd6b06d 1039 if (active_console && active_console->cursor_timer) {
bf1bed81
JK
1040 qemu_del_timer(active_console->cursor_timer);
1041 }
e7f0ad58 1042 active_console = s;
a93a4a22 1043 if (ds->have_gfx) {
284d1c6b
GH
1044 QLIST_FOREACH(dcl, &ds->listeners, next) {
1045 if (dcl->con != NULL) {
1046 continue;
1047 }
1048 if (dcl->ops->dpy_gfx_switch) {
1049 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1050 }
1051 }
321f048d
GH
1052 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1053 surface_height(s->surface));
a93a4a22
GH
1054 }
1055 if (ds->have_text) {
c78f7137 1056 dpy_text_resize(s, s->width, s->height);
68f00996 1057 }
bf1bed81
JK
1058 if (s->cursor_timer) {
1059 qemu_mod_timer(s->cursor_timer,
1060 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1061 }
e7f0ad58
FB
1062 }
1063}
1064
1065static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1066{
76ffb0b4 1067 QemuConsole *s = chr->opaque;
e7f0ad58
FB
1068 int i;
1069
14778c20
PB
1070 s->update_x0 = s->width * FONT_WIDTH;
1071 s->update_y0 = s->height * FONT_HEIGHT;
1072 s->update_x1 = 0;
1073 s->update_y1 = 0;
e7f0ad58
FB
1074 console_show_cursor(s, 0);
1075 for(i = 0; i < len; i++) {
1076 console_putchar(s, buf[i]);
1077 }
1078 console_show_cursor(s, 1);
a93a4a22 1079 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
c78f7137 1080 dpy_gfx_update(s, s->update_x0, s->update_y0,
a93a4a22
GH
1081 s->update_x1 - s->update_x0,
1082 s->update_y1 - s->update_y0);
14778c20 1083 }
e7f0ad58
FB
1084 return len;
1085}
1086
e15d7371
FB
1087static void kbd_send_chars(void *opaque)
1088{
76ffb0b4 1089 QemuConsole *s = opaque;
e15d7371
FB
1090 int len;
1091 uint8_t buf[16];
3b46e624 1092
909cda12 1093 len = qemu_chr_be_can_write(s->chr);
e15d7371
FB
1094 if (len > s->out_fifo.count)
1095 len = s->out_fifo.count;
1096 if (len > 0) {
1097 if (len > sizeof(buf))
1098 len = sizeof(buf);
1099 qemu_fifo_read(&s->out_fifo, buf, len);
fa5efccb 1100 qemu_chr_be_write(s->chr, buf, len);
e15d7371
FB
1101 }
1102 /* characters are pending: we send them a bit later (XXX:
1103 horrible, should change char device API) */
1104 if (s->out_fifo.count > 0) {
7bd427d8 1105 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
e15d7371
FB
1106 }
1107}
1108
e7f0ad58
FB
1109/* called when an ascii key is pressed */
1110void kbd_put_keysym(int keysym)
1111{
76ffb0b4 1112 QemuConsole *s;
e7f0ad58
FB
1113 uint8_t buf[16], *q;
1114 int c;
1115
1116 s = active_console;
af3a9031 1117 if (!s || (s->console_type == GRAPHIC_CONSOLE))
e7f0ad58
FB
1118 return;
1119
1120 switch(keysym) {
1121 case QEMU_KEY_CTRL_UP:
81c0d5a6 1122 console_scroll(s, -1);
e7f0ad58
FB
1123 break;
1124 case QEMU_KEY_CTRL_DOWN:
81c0d5a6 1125 console_scroll(s, 1);
e7f0ad58
FB
1126 break;
1127 case QEMU_KEY_CTRL_PAGEUP:
81c0d5a6 1128 console_scroll(s, -10);
e7f0ad58
FB
1129 break;
1130 case QEMU_KEY_CTRL_PAGEDOWN:
81c0d5a6 1131 console_scroll(s, 10);
e7f0ad58
FB
1132 break;
1133 default:
e15d7371
FB
1134 /* convert the QEMU keysym to VT100 key string */
1135 q = buf;
1136 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1137 *q++ = '\033';
1138 *q++ = '[';
1139 c = keysym - 0xe100;
1140 if (c >= 10)
1141 *q++ = '0' + (c / 10);
1142 *q++ = '0' + (c % 10);
1143 *q++ = '~';
1144 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1145 *q++ = '\033';
1146 *q++ = '[';
1147 *q++ = keysym & 0xff;
4104833f
PB
1148 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1149 console_puts(s->chr, (const uint8_t *) "\r", 1);
1150 *q++ = '\n';
e15d7371 1151 } else {
4104833f
PB
1152 *q++ = keysym;
1153 }
1154 if (s->echo) {
1155 console_puts(s->chr, buf, q - buf);
e15d7371 1156 }
e5b0bc44 1157 if (s->chr->chr_read) {
e15d7371
FB
1158 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1159 kbd_send_chars(s);
e7f0ad58
FB
1160 }
1161 break;
1162 }
1163}
1164
4d3b6f6e
AZ
1165static void text_console_invalidate(void *opaque)
1166{
76ffb0b4 1167 QemuConsole *s = (QemuConsole *) opaque;
1562e531
GH
1168
1169 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
68f00996
AL
1170 text_console_resize(s);
1171 }
4d3b6f6e
AZ
1172 console_refresh(s);
1173}
1174
c227f099 1175static void text_console_update(void *opaque, console_ch_t *chardata)
4d3b6f6e 1176{
76ffb0b4 1177 QemuConsole *s = (QemuConsole *) opaque;
4d3b6f6e
AZ
1178 int i, j, src;
1179
1180 if (s->text_x[0] <= s->text_x[1]) {
1181 src = (s->y_base + s->text_y[0]) * s->width;
1182 chardata += s->text_y[0] * s->width;
1183 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1184 for (j = 0; j < s->width; j ++, src ++)
1185 console_write_ch(chardata ++, s->cells[src].ch |
1186 (s->cells[src].t_attrib.fgcol << 12) |
1187 (s->cells[src].t_attrib.bgcol << 8) |
1188 (s->cells[src].t_attrib.bold << 21));
c78f7137 1189 dpy_text_update(s, s->text_x[0], s->text_y[0],
a93a4a22 1190 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
4d3b6f6e
AZ
1191 s->text_x[0] = s->width;
1192 s->text_y[0] = s->height;
1193 s->text_x[1] = 0;
1194 s->text_y[1] = 0;
1195 }
1196 if (s->cursor_invalidate) {
c78f7137 1197 dpy_text_cursor(s, s->x, s->y);
4d3b6f6e
AZ
1198 s->cursor_invalidate = 0;
1199 }
1200}
1201
76ffb0b4 1202static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
e7f0ad58 1203{
aa2beaa1 1204 Error *local_err = NULL;
95be0669 1205 Object *obj;
76ffb0b4 1206 QemuConsole *s;
95219897 1207 int i;
e7f0ad58
FB
1208
1209 if (nb_consoles >= MAX_CONSOLES)
1210 return NULL;
aa2beaa1 1211
95be0669
GH
1212 obj = object_new(TYPE_QEMU_CONSOLE);
1213 s = QEMU_CONSOLE(obj);
aa2beaa1
GH
1214 object_property_add_link(obj, "device", TYPE_DEVICE,
1215 (Object **)&s->device, &local_err);
1216
af3a9031
TS
1217 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1218 (console_type == GRAPHIC_CONSOLE))) {
e7f0ad58 1219 active_console = s;
af3a9031 1220 }
e7f0ad58 1221 s->ds = ds;
af3a9031
TS
1222 s->console_type = console_type;
1223 if (console_type != GRAPHIC_CONSOLE) {
f81bdefb 1224 s->index = nb_consoles;
95219897
PB
1225 consoles[nb_consoles++] = s;
1226 } else {
1227 /* HACK: Put graphical consoles before text consoles. */
1228 for (i = nb_consoles; i > 0; i--) {
af3a9031 1229 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
95219897
PB
1230 break;
1231 consoles[i] = consoles[i - 1];
f81bdefb 1232 consoles[i]->index = i;
95219897 1233 }
f81bdefb 1234 s->index = i;
95219897 1235 consoles[i] = s;
3023f332 1236 nb_consoles++;
95219897
PB
1237 }
1238 return s;
1239}
1240
537a4391
GH
1241static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1242 int linesize, PixelFormat pf, int newflags)
ffe8b821 1243{
ffe8b821 1244 surface->pf = pf;
69c77777
GH
1245
1246 qemu_pixman_image_unref(surface->image);
1247 surface->image = NULL;
69c77777
GH
1248
1249 surface->format = qemu_pixman_get_format(&pf);
1250 assert(surface->format != 0);
1251 surface->image = pixman_image_create_bits(surface->format,
1252 width, height,
1253 NULL, linesize);
1254 assert(surface->image != NULL);
1255
ffe8b821 1256 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
98b50080 1257#ifdef HOST_WORDS_BIGENDIAN
ffe8b821 1258 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
98b50080 1259#endif
98b50080
PB
1260}
1261
da229ef3 1262DisplaySurface *qemu_create_displaysurface(int width, int height)
537a4391
GH
1263{
1264 DisplaySurface *surface = g_new0(DisplaySurface, 1);
537a4391 1265 int linesize = width * 4;
da229ef3
GH
1266
1267 trace_displaysurface_create(surface, width, height);
537a4391
GH
1268 qemu_alloc_display(surface, width, height, linesize,
1269 qemu_default_pixelformat(32), 0);
1270 return surface;
1271}
1272
187cd1d9 1273DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
b1424e03
GH
1274 int linesize, uint8_t *data,
1275 bool byteswap)
98b50080 1276{
69c77777 1277 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 1278
da229ef3 1279 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
b1424e03
GH
1280 if (byteswap) {
1281 surface->pf = qemu_different_endianness_pixelformat(bpp);
1282 } else {
1283 surface->pf = qemu_default_pixelformat(bpp);
1284 }
69c77777
GH
1285
1286 surface->format = qemu_pixman_get_format(&surface->pf);
1287 assert(surface->format != 0);
1288 surface->image = pixman_image_create_bits(surface->format,
1289 width, height,
1290 (void *)data, linesize);
1291 assert(surface->image != NULL);
1292
98b50080
PB
1293#ifdef HOST_WORDS_BIGENDIAN
1294 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1295#endif
98b50080
PB
1296
1297 return surface;
1298}
1299
da229ef3 1300void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 1301{
da229ef3 1302 if (surface == NULL) {
98b50080 1303 return;
187cd1d9 1304 }
da229ef3
GH
1305 trace_displaysurface_free(surface);
1306 qemu_pixman_image_unref(surface->image);
1307 g_free(surface);
98b50080
PB
1308}
1309
7c20b4a3
GH
1310void register_displaychangelistener(DisplayState *ds,
1311 DisplayChangeListener *dcl)
1312{
284d1c6b
GH
1313 QemuConsole *con;
1314
7c20b4a3
GH
1315 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1316 dcl->ds = ds;
1317 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1318 gui_setup_refresh(ds);
284d1c6b
GH
1319 if (dcl->con) {
1320 dcl->con->dcls++;
1321 con = dcl->con;
1322 } else {
1323 con = active_console;
1324 }
1325 if (dcl->ops->dpy_gfx_switch && con) {
1326 dcl->ops->dpy_gfx_switch(dcl, con->surface);
7c20b4a3
GH
1327 }
1328}
1329
0f7b2864
GH
1330void update_displaychangelistener(DisplayChangeListener *dcl,
1331 uint64_t interval)
1332{
1333 DisplayState *ds = dcl->ds;
1334
1335 dcl->update_interval = interval;
1336 if (!ds->refreshing && ds->update_interval > interval) {
1337 qemu_mod_timer(ds->gui_timer, ds->last_update + interval);
1338 }
1339}
1340
7c20b4a3
GH
1341void unregister_displaychangelistener(DisplayChangeListener *dcl)
1342{
1343 DisplayState *ds = dcl->ds;
1344 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
1345 if (dcl->con) {
1346 dcl->con->dcls--;
1347 }
7c20b4a3
GH
1348 QLIST_REMOVE(dcl, next);
1349 gui_setup_refresh(ds);
1350}
1351
c78f7137 1352void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1353{
c78f7137 1354 DisplayState *s = con->ds;
284d1c6b 1355 DisplayChangeListener *dcl;
321f048d
GH
1356 int width = surface_width(con->surface);
1357 int height = surface_height(con->surface);
7c20b4a3
GH
1358
1359 x = MAX(x, 0);
1360 y = MAX(y, 0);
1361 x = MIN(x, width);
1362 y = MIN(y, height);
1363 w = MIN(w, width - x);
1364 h = MIN(h, height - y);
1365
81c0d5a6 1366 if (!qemu_console_is_visible(con)) {
321f048d
GH
1367 return;
1368 }
7c20b4a3 1369 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1370 if (con != (dcl->con ? dcl->con : active_console)) {
1371 continue;
1372 }
7c20b4a3 1373 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1374 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1375 }
1376 }
1377}
1378
321f048d
GH
1379void dpy_gfx_replace_surface(QemuConsole *con,
1380 DisplaySurface *surface)
1381{
1382 DisplayState *s = con->ds;
1383 DisplaySurface *old_surface = con->surface;
284d1c6b 1384 DisplayChangeListener *dcl;
321f048d
GH
1385
1386 con->surface = surface;
284d1c6b
GH
1387 QLIST_FOREACH(dcl, &s->listeners, next) {
1388 if (con != (dcl->con ? dcl->con : active_console)) {
1389 continue;
1390 }
1391 if (dcl->ops->dpy_gfx_switch) {
1392 dcl->ops->dpy_gfx_switch(dcl, surface);
1393 }
321f048d 1394 }
da229ef3 1395 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1396}
1397
1398void dpy_refresh(DisplayState *s)
1399{
284d1c6b
GH
1400 DisplayChangeListener *dcl;
1401
7c20b4a3
GH
1402 QLIST_FOREACH(dcl, &s->listeners, next) {
1403 if (dcl->ops->dpy_refresh) {
bc2ed970 1404 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1405 }
1406 }
1407}
1408
c78f7137
GH
1409void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1410 int dst_x, int dst_y, int w, int h)
7c20b4a3 1411{
c78f7137 1412 DisplayState *s = con->ds;
284d1c6b 1413 DisplayChangeListener *dcl;
321f048d 1414
81c0d5a6 1415 if (!qemu_console_is_visible(con)) {
321f048d
GH
1416 return;
1417 }
7c20b4a3 1418 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1419 if (con != (dcl->con ? dcl->con : active_console)) {
1420 continue;
1421 }
7c20b4a3 1422 if (dcl->ops->dpy_gfx_copy) {
bc2ed970 1423 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
7c20b4a3 1424 } else { /* TODO */
bc2ed970 1425 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
7c20b4a3
GH
1426 }
1427 }
1428}
1429
c78f7137 1430void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1431{
c78f7137 1432 DisplayState *s = con->ds;
284d1c6b 1433 DisplayChangeListener *dcl;
321f048d 1434
81c0d5a6 1435 if (!qemu_console_is_visible(con)) {
321f048d
GH
1436 return;
1437 }
7c20b4a3 1438 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1439 if (con != (dcl->con ? dcl->con : active_console)) {
1440 continue;
1441 }
7c20b4a3 1442 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1443 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1444 }
1445 }
1446}
1447
c78f7137 1448void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1449{
c78f7137 1450 DisplayState *s = con->ds;
284d1c6b 1451 DisplayChangeListener *dcl;
321f048d 1452
81c0d5a6 1453 if (!qemu_console_is_visible(con)) {
321f048d
GH
1454 return;
1455 }
7c20b4a3 1456 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1457 if (con != (dcl->con ? dcl->con : active_console)) {
1458 continue;
1459 }
7c20b4a3 1460 if (dcl->ops->dpy_text_update) {
bc2ed970 1461 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
1462 }
1463 }
1464}
1465
c78f7137 1466void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1467{
c78f7137 1468 DisplayState *s = con->ds;
7c20b4a3 1469 struct DisplayChangeListener *dcl;
321f048d 1470
81c0d5a6 1471 if (!qemu_console_is_visible(con)) {
321f048d
GH
1472 return;
1473 }
7c20b4a3 1474 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1475 if (con != (dcl->con ? dcl->con : active_console)) {
1476 continue;
1477 }
7c20b4a3 1478 if (dcl->ops->dpy_text_resize) {
bc2ed970 1479 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1480 }
1481 }
1482}
1483
c78f7137 1484void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 1485{
c78f7137 1486 DisplayState *s = con->ds;
284d1c6b 1487 DisplayChangeListener *dcl;
321f048d 1488
81c0d5a6 1489 if (!qemu_console_is_visible(con)) {
321f048d
GH
1490 return;
1491 }
7c20b4a3 1492 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1493 if (con != (dcl->con ? dcl->con : active_console)) {
1494 continue;
1495 }
7c20b4a3 1496 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1497 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1498 }
1499 }
1500}
1501
c78f7137 1502void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 1503{
c78f7137 1504 DisplayState *s = con->ds;
284d1c6b 1505 DisplayChangeListener *dcl;
321f048d 1506
81c0d5a6 1507 if (!qemu_console_is_visible(con)) {
321f048d
GH
1508 return;
1509 }
7c20b4a3 1510 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1511 if (con != (dcl->con ? dcl->con : active_console)) {
1512 continue;
1513 }
7c20b4a3 1514 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1515 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1516 }
1517 }
1518}
1519
c78f7137 1520bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1521{
c78f7137 1522 DisplayState *s = con->ds;
284d1c6b
GH
1523 DisplayChangeListener *dcl;
1524
7c20b4a3
GH
1525 QLIST_FOREACH(dcl, &s->listeners, next) {
1526 if (dcl->ops->dpy_cursor_define) {
1527 return true;
1528 }
1529 }
1530 return false;
1531}
1532
98b50080
PB
1533/***********************************************************/
1534/* register display */
1535
64840c66
GH
1536/* console.c internal use only */
1537static DisplayState *get_alloc_displaystate(void)
98b50080 1538{
64840c66
GH
1539 if (!display_state) {
1540 display_state = g_new0(DisplayState, 1);
1541 }
1542 return display_state;
98b50080
PB
1543}
1544
64840c66
GH
1545/*
1546 * Called by main(), after creating QemuConsoles
1547 * and before initializing ui (sdl/vnc/...).
1548 */
1549DisplayState *init_displaystate(void)
98b50080 1550{
64840c66
GH
1551 int i;
1552
98b50080 1553 if (!display_state) {
64840c66 1554 display_state = g_new0(DisplayState, 1);
98b50080 1555 }
64840c66
GH
1556
1557 for (i = 0; i < nb_consoles; i++) {
1558 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1559 consoles[i]->ds == NULL) {
1560 text_console_do_init(consoles[i]->chr, display_state);
1561 }
1562 }
1563
98b50080
PB
1564 return display_state;
1565}
1566
aa2beaa1
GH
1567QemuConsole *graphic_console_init(DeviceState *dev,
1568 const GraphicHwOps *hw_ops,
c78f7137 1569 void *opaque)
95219897 1570{
aa2beaa1 1571 Error *local_err = NULL;
64840c66
GH
1572 int width = 640;
1573 int height = 480;
76ffb0b4 1574 QemuConsole *s;
3023f332 1575 DisplayState *ds;
f0f2f976 1576
64840c66 1577 ds = get_alloc_displaystate();
437fe106 1578 trace_console_gfx_new();
af3a9031 1579 s = new_console(ds, GRAPHIC_CONSOLE);
380cd056 1580 s->hw_ops = hw_ops;
95219897 1581 s->hw = opaque;
aa2beaa1
GH
1582 if (dev) {
1583 object_property_set_link(OBJECT(s), OBJECT(dev),
1584 "device", &local_err);
1585 }
3023f332 1586
321f048d 1587 s->surface = qemu_create_displaysurface(width, height);
c78f7137 1588 return s;
e7f0ad58
FB
1589}
1590
284d1c6b
GH
1591QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1592{
1593 if (index >= MAX_CONSOLES) {
1594 return NULL;
1595 }
1596 return consoles[index];
1597}
1598
81c0d5a6 1599bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 1600{
284d1c6b 1601 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
1602}
1603
81c0d5a6 1604bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 1605{
81c0d5a6
GH
1606 if (con == NULL) {
1607 con = active_console;
1608 }
1609 return con && (con->console_type == GRAPHIC_CONSOLE);
1610}
1611
1612bool qemu_console_is_fixedsize(QemuConsole *con)
1613{
1614 if (con == NULL) {
1615 con = active_console;
1616 }
1617 return con && (con->console_type != TEXT_CONSOLE);
c21bbcfa
AZ
1618}
1619
4104833f
PB
1620static void text_console_set_echo(CharDriverState *chr, bool echo)
1621{
76ffb0b4 1622 QemuConsole *s = chr->opaque;
4104833f
PB
1623
1624 s->echo = echo;
1625}
1626
bf1bed81
JK
1627static void text_console_update_cursor(void *opaque)
1628{
76ffb0b4 1629 QemuConsole *s = opaque;
bf1bed81
JK
1630
1631 s->cursor_visible_phase = !s->cursor_visible_phase;
1dbfa005 1632 graphic_hw_invalidate(s);
bf1bed81
JK
1633 qemu_mod_timer(s->cursor_timer,
1634 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1635}
1636
380cd056
GH
1637static const GraphicHwOps text_console_ops = {
1638 .invalidate = text_console_invalidate,
1639 .text_update = text_console_update,
1640};
1641
44b37b93 1642static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
e7f0ad58 1643{
76ffb0b4 1644 QemuConsole *s;
36671fbd
GH
1645 int g_width = 80 * FONT_WIDTH;
1646 int g_height = 24 * FONT_HEIGHT;
6d6f7c28 1647
491e114a 1648 s = chr->opaque;
6ea314d9 1649
e7f0ad58 1650 chr->chr_write = console_puts;
6fcfafb7 1651
e15d7371
FB
1652 s->out_fifo.buf = s->out_fifo_buf;
1653 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
7bd427d8 1654 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
3023f332 1655 s->ds = ds;
3b46e624 1656
e7f0ad58
FB
1657 s->y_displayed = 0;
1658 s->y_base = 0;
1659 s->total_height = DEFAULT_BACKSCROLL;
1660 s->x = 0;
1661 s->y = 0;
36671fbd 1662 if (!s->surface) {
321f048d 1663 if (active_console && active_console->surface) {
36671fbd
GH
1664 g_width = surface_width(active_console->surface);
1665 g_height = surface_height(active_console->surface);
321f048d 1666 }
36671fbd 1667 s->surface = qemu_create_displaysurface(g_width, g_height);
491e114a 1668 }
6d6f7c28 1669
bf1bed81
JK
1670 s->cursor_timer =
1671 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1672
380cd056 1673 s->hw_ops = &text_console_ops;
4d3b6f6e
AZ
1674 s->hw = s;
1675
6d6f7c28
PB
1676 /* Set text attribute defaults */
1677 s->t_attrib_default.bold = 0;
1678 s->t_attrib_default.uline = 0;
1679 s->t_attrib_default.blink = 0;
1680 s->t_attrib_default.invers = 0;
1681 s->t_attrib_default.unvisible = 0;
1682 s->t_attrib_default.fgcol = COLOR_WHITE;
1683 s->t_attrib_default.bgcol = COLOR_BLACK;
6d6f7c28
PB
1684 /* set current text attributes to default */
1685 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
1686 text_console_resize(s);
1687
51bfa4d3
GH
1688 if (chr->label) {
1689 char msg[128];
1690 int len;
1691
735ba588 1692 s->t_attrib.bgcol = COLOR_BLUE;
51bfa4d3
GH
1693 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1694 console_puts(chr, (uint8_t*)msg, len);
735ba588 1695 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
1696 }
1697
fee204fd 1698 qemu_chr_be_generic_open(chr);
ceecf1d1
AJ
1699 if (chr->init)
1700 chr->init(chr);
e7f0ad58 1701}
c60e08d9 1702
702ec69c 1703static CharDriverState *text_console_init(ChardevVC *vc)
2796dae0
AL
1704{
1705 CharDriverState *chr;
76ffb0b4 1706 QemuConsole *s;
702ec69c
GH
1707 unsigned width = 0;
1708 unsigned height = 0;
2796dae0 1709
7267c094 1710 chr = g_malloc0(sizeof(CharDriverState));
2796dae0 1711
702ec69c
GH
1712 if (vc->has_width) {
1713 width = vc->width;
1714 } else if (vc->has_cols) {
1715 width = vc->cols * FONT_WIDTH;
1716 }
491e114a 1717
702ec69c
GH
1718 if (vc->has_height) {
1719 height = vc->height;
1720 } else if (vc->has_rows) {
1721 height = vc->rows * FONT_HEIGHT;
1722 }
491e114a 1723
437fe106 1724 trace_console_txt_new(width, height);
491e114a
PB
1725 if (width == 0 || height == 0) {
1726 s = new_console(NULL, TEXT_CONSOLE);
1727 } else {
1728 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
36671fbd 1729 s->surface = qemu_create_displaysurface(width, height);
491e114a
PB
1730 }
1731
1732 if (!s) {
5354d083 1733 g_free(chr);
1f51470d 1734 return NULL;
491e114a
PB
1735 }
1736
1737 s->chr = chr;
491e114a 1738 chr->opaque = s;
4104833f 1739 chr->chr_set_echo = text_console_set_echo;
64840c66
GH
1740
1741 if (display_state) {
1742 text_console_do_init(chr, display_state);
1743 }
1f51470d 1744 return chr;
2796dae0
AL
1745}
1746
d82831db
AL
1747static VcHandler *vc_handler = text_console_init;
1748
702ec69c 1749CharDriverState *vc_init(ChardevVC *vc)
d82831db 1750{
702ec69c 1751 return vc_handler(vc);
d82831db
AL
1752}
1753
1754void register_vc_handler(VcHandler *handler)
1755{
1756 vc_handler = handler;
1757}
1758
c78f7137 1759void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 1760{
321f048d
GH
1761 DisplaySurface *surface;
1762
1763 assert(s->console_type == GRAPHIC_CONSOLE);
321f048d
GH
1764 surface = qemu_create_displaysurface(width, height);
1765 dpy_gfx_replace_surface(s, surface);
c60e08d9 1766}
38334f76 1767
c78f7137 1768void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
3023f332 1769 int dst_x, int dst_y, int w, int h)
c21bbcfa 1770{
321f048d
GH
1771 assert(con->console_type == GRAPHIC_CONSOLE);
1772 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
38334f76 1773}
7d957bd8 1774
c78f7137
GH
1775DisplaySurface *qemu_console_surface(QemuConsole *console)
1776{
321f048d 1777 return console->surface;
c78f7137
GH
1778}
1779
1780DisplayState *qemu_console_displaystate(QemuConsole *console)
1781{
1782 return console->ds;
1783}
1784
0da2ea1b 1785PixelFormat qemu_different_endianness_pixelformat(int bpp)
7d957bd8
AL
1786{
1787 PixelFormat pf;
1788
1789 memset(&pf, 0x00, sizeof(PixelFormat));
1790
1791 pf.bits_per_pixel = bpp;
feadf1a4 1792 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
7d957bd8
AL
1793 pf.depth = bpp == 32 ? 24 : bpp;
1794
1795 switch (bpp) {
0da2ea1b 1796 case 24:
1797 pf.rmask = 0x000000FF;
1798 pf.gmask = 0x0000FF00;
1799 pf.bmask = 0x00FF0000;
1800 pf.rmax = 255;
1801 pf.gmax = 255;
1802 pf.bmax = 255;
1803 pf.rshift = 0;
1804 pf.gshift = 8;
1805 pf.bshift = 16;
90a1e3c0
AL
1806 pf.rbits = 8;
1807 pf.gbits = 8;
1808 pf.bbits = 8;
7d957bd8 1809 break;
0da2ea1b 1810 case 32:
1811 pf.rmask = 0x0000FF00;
1812 pf.gmask = 0x00FF0000;
1813 pf.bmask = 0xFF000000;
1814 pf.amask = 0x00000000;
1815 pf.amax = 255;
1816 pf.rmax = 255;
1817 pf.gmax = 255;
1818 pf.bmax = 255;
1819 pf.ashift = 0;
1820 pf.rshift = 8;
1821 pf.gshift = 16;
1822 pf.bshift = 24;
90a1e3c0
AL
1823 pf.rbits = 8;
1824 pf.gbits = 8;
1825 pf.bbits = 8;
1826 pf.abits = 8;
0da2ea1b 1827 break;
1828 default:
1829 break;
1830 }
1831 return pf;
1832}
1833
1834PixelFormat qemu_default_pixelformat(int bpp)
1835{
1836 PixelFormat pf;
1837
1838 memset(&pf, 0x00, sizeof(PixelFormat));
1839
1840 pf.bits_per_pixel = bpp;
feadf1a4 1841 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
0da2ea1b 1842 pf.depth = bpp == 32 ? 24 : bpp;
1843
1844 switch (bpp) {
b6278084
GH
1845 case 15:
1846 pf.bits_per_pixel = 16;
b6278084
GH
1847 pf.rmask = 0x00007c00;
1848 pf.gmask = 0x000003E0;
1849 pf.bmask = 0x0000001F;
1850 pf.rmax = 31;
1851 pf.gmax = 31;
1852 pf.bmax = 31;
1853 pf.rshift = 10;
1854 pf.gshift = 5;
1855 pf.bshift = 0;
1856 pf.rbits = 5;
1857 pf.gbits = 5;
1858 pf.bbits = 5;
1859 break;
7d957bd8
AL
1860 case 16:
1861 pf.rmask = 0x0000F800;
1862 pf.gmask = 0x000007E0;
1863 pf.bmask = 0x0000001F;
1864 pf.rmax = 31;
1865 pf.gmax = 63;
1866 pf.bmax = 31;
1867 pf.rshift = 11;
1868 pf.gshift = 5;
1869 pf.bshift = 0;
90a1e3c0
AL
1870 pf.rbits = 5;
1871 pf.gbits = 6;
1872 pf.bbits = 5;
7d957bd8
AL
1873 break;
1874 case 24:
0da2ea1b 1875 pf.rmask = 0x00FF0000;
1876 pf.gmask = 0x0000FF00;
1877 pf.bmask = 0x000000FF;
1878 pf.rmax = 255;
1879 pf.gmax = 255;
1880 pf.bmax = 255;
1881 pf.rshift = 16;
1882 pf.gshift = 8;
1883 pf.bshift = 0;
90a1e3c0
AL
1884 pf.rbits = 8;
1885 pf.gbits = 8;
1886 pf.bbits = 8;
0eba62e0 1887 break;
7d957bd8
AL
1888 case 32:
1889 pf.rmask = 0x00FF0000;
1890 pf.gmask = 0x0000FF00;
1891 pf.bmask = 0x000000FF;
1892 pf.rmax = 255;
1893 pf.gmax = 255;
1894 pf.bmax = 255;
1895 pf.rshift = 16;
1896 pf.gshift = 8;
1897 pf.bshift = 0;
90a1e3c0
AL
1898 pf.rbits = 8;
1899 pf.gbits = 8;
1900 pf.bbits = 8;
7d957bd8
AL
1901 break;
1902 default:
1903 break;
1904 }
1905 return pf;
1906}
01f45d98 1907
702ec69c
GH
1908static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1909 Error **errp)
1910{
1911 int val;
1912
1913 backend->vc = g_new0(ChardevVC, 1);
1914
1915 val = qemu_opt_get_number(opts, "width", 0);
1916 if (val != 0) {
1917 backend->vc->has_width = true;
1918 backend->vc->width = val;
1919 }
1920
1921 val = qemu_opt_get_number(opts, "height", 0);
1922 if (val != 0) {
1923 backend->vc->has_height = true;
1924 backend->vc->height = val;
1925 }
1926
1927 val = qemu_opt_get_number(opts, "cols", 0);
1928 if (val != 0) {
1929 backend->vc->has_cols = true;
1930 backend->vc->cols = val;
1931 }
1932
1933 val = qemu_opt_get_number(opts, "rows", 0);
1934 if (val != 0) {
1935 backend->vc->has_rows = true;
1936 backend->vc->rows = val;
1937 }
1938}
1939
95be0669
GH
1940static const TypeInfo qemu_console_info = {
1941 .name = TYPE_QEMU_CONSOLE,
1942 .parent = TYPE_OBJECT,
1943 .instance_size = sizeof(QemuConsole),
1944 .class_size = sizeof(QemuConsoleClass),
1945};
1946
1947
01f45d98
AL
1948static void register_types(void)
1949{
95be0669 1950 type_register_static(&qemu_console_info);
702ec69c
GH
1951 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1952 qemu_chr_parse_vc);
01f45d98
AL
1953}
1954
1955type_init(register_types);