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