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