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