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