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