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