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