]> git.proxmox.com Git - qemu.git/blame - ui/console.c
console: add trace events
[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;
437fe106
GH
907
908 trace_console_select(index);
358664cc 909 if (active_console) {
1562e531
GH
910 surface = qemu_console_surface(active_console);
911 active_console->g_width = surface_width(surface);
912 active_console->g_height = surface_height(surface);
358664cc 913 }
e7f0ad58
FB
914 s = consoles[index];
915 if (s) {
7d957bd8 916 DisplayState *ds = s->ds;
bf1bed81 917
8bd6b06d 918 if (active_console && active_console->cursor_timer) {
bf1bed81
JK
919 qemu_del_timer(active_console->cursor_timer);
920 }
e7f0ad58 921 active_console = s;
a93a4a22 922 if (ds->have_gfx) {
da229ef3 923 surface = qemu_create_displaysurface(s->g_width, s->g_height);
c78f7137 924 dpy_gfx_replace_surface(s, surface);
a93a4a22
GH
925 }
926 if (ds->have_text) {
c78f7137 927 dpy_text_resize(s, s->width, s->height);
68f00996 928 }
bf1bed81
JK
929 if (s->cursor_timer) {
930 qemu_mod_timer(s->cursor_timer,
931 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
932 }
4d3b6f6e 933 vga_hw_invalidate();
e7f0ad58
FB
934 }
935}
936
937static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
938{
76ffb0b4 939 QemuConsole *s = chr->opaque;
e7f0ad58
FB
940 int i;
941
14778c20
PB
942 s->update_x0 = s->width * FONT_WIDTH;
943 s->update_y0 = s->height * FONT_HEIGHT;
944 s->update_x1 = 0;
945 s->update_y1 = 0;
e7f0ad58
FB
946 console_show_cursor(s, 0);
947 for(i = 0; i < len; i++) {
948 console_putchar(s, buf[i]);
949 }
950 console_show_cursor(s, 1);
a93a4a22 951 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
c78f7137 952 dpy_gfx_update(s, s->update_x0, s->update_y0,
a93a4a22
GH
953 s->update_x1 - s->update_x0,
954 s->update_y1 - s->update_y0);
14778c20 955 }
e7f0ad58
FB
956 return len;
957}
958
e15d7371
FB
959static void kbd_send_chars(void *opaque)
960{
76ffb0b4 961 QemuConsole *s = opaque;
e15d7371
FB
962 int len;
963 uint8_t buf[16];
3b46e624 964
909cda12 965 len = qemu_chr_be_can_write(s->chr);
e15d7371
FB
966 if (len > s->out_fifo.count)
967 len = s->out_fifo.count;
968 if (len > 0) {
969 if (len > sizeof(buf))
970 len = sizeof(buf);
971 qemu_fifo_read(&s->out_fifo, buf, len);
fa5efccb 972 qemu_chr_be_write(s->chr, buf, len);
e15d7371
FB
973 }
974 /* characters are pending: we send them a bit later (XXX:
975 horrible, should change char device API) */
976 if (s->out_fifo.count > 0) {
7bd427d8 977 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
e15d7371
FB
978 }
979}
980
e7f0ad58
FB
981/* called when an ascii key is pressed */
982void kbd_put_keysym(int keysym)
983{
76ffb0b4 984 QemuConsole *s;
e7f0ad58
FB
985 uint8_t buf[16], *q;
986 int c;
987
988 s = active_console;
af3a9031 989 if (!s || (s->console_type == GRAPHIC_CONSOLE))
e7f0ad58
FB
990 return;
991
992 switch(keysym) {
993 case QEMU_KEY_CTRL_UP:
994 console_scroll(-1);
995 break;
996 case QEMU_KEY_CTRL_DOWN:
997 console_scroll(1);
998 break;
999 case QEMU_KEY_CTRL_PAGEUP:
1000 console_scroll(-10);
1001 break;
1002 case QEMU_KEY_CTRL_PAGEDOWN:
1003 console_scroll(10);
1004 break;
1005 default:
e15d7371
FB
1006 /* convert the QEMU keysym to VT100 key string */
1007 q = buf;
1008 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1009 *q++ = '\033';
1010 *q++ = '[';
1011 c = keysym - 0xe100;
1012 if (c >= 10)
1013 *q++ = '0' + (c / 10);
1014 *q++ = '0' + (c % 10);
1015 *q++ = '~';
1016 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1017 *q++ = '\033';
1018 *q++ = '[';
1019 *q++ = keysym & 0xff;
4104833f
PB
1020 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1021 console_puts(s->chr, (const uint8_t *) "\r", 1);
1022 *q++ = '\n';
e15d7371 1023 } else {
4104833f
PB
1024 *q++ = keysym;
1025 }
1026 if (s->echo) {
1027 console_puts(s->chr, buf, q - buf);
e15d7371 1028 }
e5b0bc44 1029 if (s->chr->chr_read) {
e15d7371
FB
1030 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1031 kbd_send_chars(s);
e7f0ad58
FB
1032 }
1033 break;
1034 }
1035}
1036
4d3b6f6e
AZ
1037static void text_console_invalidate(void *opaque)
1038{
76ffb0b4 1039 QemuConsole *s = (QemuConsole *) opaque;
1562e531
GH
1040 DisplaySurface *surface = qemu_console_surface(s);
1041
1042 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1043 s->g_width = surface_width(surface);
1044 s->g_height = surface_height(surface);
68f00996
AL
1045 text_console_resize(s);
1046 }
4d3b6f6e
AZ
1047 console_refresh(s);
1048}
1049
c227f099 1050static void text_console_update(void *opaque, console_ch_t *chardata)
4d3b6f6e 1051{
76ffb0b4 1052 QemuConsole *s = (QemuConsole *) opaque;
4d3b6f6e
AZ
1053 int i, j, src;
1054
1055 if (s->text_x[0] <= s->text_x[1]) {
1056 src = (s->y_base + s->text_y[0]) * s->width;
1057 chardata += s->text_y[0] * s->width;
1058 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1059 for (j = 0; j < s->width; j ++, src ++)
1060 console_write_ch(chardata ++, s->cells[src].ch |
1061 (s->cells[src].t_attrib.fgcol << 12) |
1062 (s->cells[src].t_attrib.bgcol << 8) |
1063 (s->cells[src].t_attrib.bold << 21));
c78f7137 1064 dpy_text_update(s, s->text_x[0], s->text_y[0],
a93a4a22 1065 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
4d3b6f6e
AZ
1066 s->text_x[0] = s->width;
1067 s->text_y[0] = s->height;
1068 s->text_x[1] = 0;
1069 s->text_y[1] = 0;
1070 }
1071 if (s->cursor_invalidate) {
c78f7137 1072 dpy_text_cursor(s, s->x, s->y);
4d3b6f6e
AZ
1073 s->cursor_invalidate = 0;
1074 }
1075}
1076
76ffb0b4 1077static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
e7f0ad58 1078{
76ffb0b4 1079 QemuConsole *s;
95219897 1080 int i;
e7f0ad58
FB
1081
1082 if (nb_consoles >= MAX_CONSOLES)
1083 return NULL;
76ffb0b4 1084 s = g_malloc0(sizeof(QemuConsole));
af3a9031
TS
1085 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1086 (console_type == GRAPHIC_CONSOLE))) {
e7f0ad58 1087 active_console = s;
af3a9031 1088 }
e7f0ad58 1089 s->ds = ds;
af3a9031
TS
1090 s->console_type = console_type;
1091 if (console_type != GRAPHIC_CONSOLE) {
f81bdefb 1092 s->index = nb_consoles;
95219897
PB
1093 consoles[nb_consoles++] = s;
1094 } else {
1095 /* HACK: Put graphical consoles before text consoles. */
1096 for (i = nb_consoles; i > 0; i--) {
af3a9031 1097 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
95219897
PB
1098 break;
1099 consoles[i] = consoles[i - 1];
f81bdefb 1100 consoles[i]->index = i;
95219897 1101 }
f81bdefb 1102 s->index = i;
95219897 1103 consoles[i] = s;
3023f332 1104 nb_consoles++;
95219897
PB
1105 }
1106 return s;
1107}
1108
537a4391
GH
1109static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1110 int linesize, PixelFormat pf, int newflags)
ffe8b821 1111{
ffe8b821 1112 surface->pf = pf;
69c77777
GH
1113
1114 qemu_pixman_image_unref(surface->image);
1115 surface->image = NULL;
69c77777
GH
1116
1117 surface->format = qemu_pixman_get_format(&pf);
1118 assert(surface->format != 0);
1119 surface->image = pixman_image_create_bits(surface->format,
1120 width, height,
1121 NULL, linesize);
1122 assert(surface->image != NULL);
1123
ffe8b821 1124 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
98b50080 1125#ifdef HOST_WORDS_BIGENDIAN
ffe8b821 1126 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
98b50080 1127#endif
98b50080
PB
1128}
1129
da229ef3 1130DisplaySurface *qemu_create_displaysurface(int width, int height)
537a4391
GH
1131{
1132 DisplaySurface *surface = g_new0(DisplaySurface, 1);
537a4391 1133 int linesize = width * 4;
da229ef3
GH
1134
1135 trace_displaysurface_create(surface, width, height);
537a4391
GH
1136 qemu_alloc_display(surface, width, height, linesize,
1137 qemu_default_pixelformat(32), 0);
1138 return surface;
1139}
1140
187cd1d9 1141DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
b1424e03
GH
1142 int linesize, uint8_t *data,
1143 bool byteswap)
98b50080 1144{
69c77777 1145 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 1146
da229ef3 1147 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
b1424e03
GH
1148 if (byteswap) {
1149 surface->pf = qemu_different_endianness_pixelformat(bpp);
1150 } else {
1151 surface->pf = qemu_default_pixelformat(bpp);
1152 }
69c77777
GH
1153
1154 surface->format = qemu_pixman_get_format(&surface->pf);
1155 assert(surface->format != 0);
1156 surface->image = pixman_image_create_bits(surface->format,
1157 width, height,
1158 (void *)data, linesize);
1159 assert(surface->image != NULL);
1160
98b50080
PB
1161#ifdef HOST_WORDS_BIGENDIAN
1162 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1163#endif
98b50080
PB
1164
1165 return surface;
1166}
1167
da229ef3 1168void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 1169{
da229ef3 1170 if (surface == NULL) {
98b50080 1171 return;
187cd1d9 1172 }
da229ef3
GH
1173 trace_displaysurface_free(surface);
1174 qemu_pixman_image_unref(surface->image);
1175 g_free(surface);
98b50080
PB
1176}
1177
7c20b4a3
GH
1178void register_displaychangelistener(DisplayState *ds,
1179 DisplayChangeListener *dcl)
1180{
1181 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1182 dcl->ds = ds;
1183 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1184 gui_setup_refresh(ds);
c12aeb86 1185 if (dcl->ops->dpy_gfx_switch) {
bc2ed970 1186 dcl->ops->dpy_gfx_switch(dcl, ds->surface);
7c20b4a3
GH
1187 }
1188}
1189
1190void unregister_displaychangelistener(DisplayChangeListener *dcl)
1191{
1192 DisplayState *ds = dcl->ds;
1193 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1194 QLIST_REMOVE(dcl, next);
1195 gui_setup_refresh(ds);
1196}
1197
c78f7137 1198void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1199{
c78f7137 1200 DisplayState *s = con->ds;
7c20b4a3
GH
1201 struct DisplayChangeListener *dcl;
1202 int width = pixman_image_get_width(s->surface->image);
1203 int height = pixman_image_get_height(s->surface->image);
1204
1205 x = MAX(x, 0);
1206 y = MAX(y, 0);
1207 x = MIN(x, width);
1208 y = MIN(y, height);
1209 w = MIN(w, width - x);
1210 h = MIN(h, height - y);
1211
1212 QLIST_FOREACH(dcl, &s->listeners, next) {
1213 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1214 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1215 }
1216 }
1217}
1218
c78f7137 1219void dpy_gfx_replace_surface(QemuConsole *con,
da229ef3 1220 DisplaySurface *surface)
7c20b4a3 1221{
c78f7137 1222 DisplayState *s = con->ds;
da229ef3 1223 DisplaySurface *old_surface = s->surface;
7c20b4a3 1224 struct DisplayChangeListener *dcl;
da229ef3
GH
1225
1226 s->surface = surface;
7c20b4a3 1227 QLIST_FOREACH(dcl, &s->listeners, next) {
c12aeb86 1228 if (dcl->ops->dpy_gfx_switch) {
bc2ed970 1229 dcl->ops->dpy_gfx_switch(dcl, surface);
7c20b4a3
GH
1230 }
1231 }
da229ef3 1232 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1233}
1234
1235void dpy_refresh(DisplayState *s)
1236{
1237 struct DisplayChangeListener *dcl;
1238 QLIST_FOREACH(dcl, &s->listeners, next) {
1239 if (dcl->ops->dpy_refresh) {
bc2ed970 1240 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1241 }
1242 }
1243}
1244
c78f7137
GH
1245void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1246 int dst_x, int dst_y, int w, int h)
7c20b4a3 1247{
c78f7137 1248 DisplayState *s = con->ds;
7c20b4a3
GH
1249 struct DisplayChangeListener *dcl;
1250 QLIST_FOREACH(dcl, &s->listeners, next) {
1251 if (dcl->ops->dpy_gfx_copy) {
bc2ed970 1252 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
7c20b4a3 1253 } else { /* TODO */
bc2ed970 1254 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
7c20b4a3
GH
1255 }
1256 }
1257}
1258
c78f7137 1259void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1260{
c78f7137 1261 DisplayState *s = con->ds;
7c20b4a3
GH
1262 struct DisplayChangeListener *dcl;
1263 QLIST_FOREACH(dcl, &s->listeners, next) {
1264 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1265 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1266 }
1267 }
1268}
1269
c78f7137 1270void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1271{
c78f7137 1272 DisplayState *s = con->ds;
7c20b4a3
GH
1273 struct DisplayChangeListener *dcl;
1274 QLIST_FOREACH(dcl, &s->listeners, next) {
1275 if (dcl->ops->dpy_text_update) {
bc2ed970 1276 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
1277 }
1278 }
1279}
1280
c78f7137 1281void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1282{
c78f7137 1283 DisplayState *s = con->ds;
7c20b4a3
GH
1284 struct DisplayChangeListener *dcl;
1285 QLIST_FOREACH(dcl, &s->listeners, next) {
1286 if (dcl->ops->dpy_text_resize) {
bc2ed970 1287 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1288 }
1289 }
1290}
1291
c78f7137 1292void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 1293{
c78f7137 1294 DisplayState *s = con->ds;
7c20b4a3
GH
1295 struct DisplayChangeListener *dcl;
1296 QLIST_FOREACH(dcl, &s->listeners, next) {
1297 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1298 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1299 }
1300 }
1301}
1302
c78f7137 1303void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 1304{
c78f7137 1305 DisplayState *s = con->ds;
7c20b4a3
GH
1306 struct DisplayChangeListener *dcl;
1307 QLIST_FOREACH(dcl, &s->listeners, next) {
1308 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1309 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1310 }
1311 }
1312}
1313
c78f7137 1314bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1315{
c78f7137 1316 DisplayState *s = con->ds;
7c20b4a3
GH
1317 struct DisplayChangeListener *dcl;
1318 QLIST_FOREACH(dcl, &s->listeners, next) {
1319 if (dcl->ops->dpy_cursor_define) {
1320 return true;
1321 }
1322 }
1323 return false;
1324}
1325
98b50080
PB
1326static void dumb_display_init(void)
1327{
7267c094 1328 DisplayState *ds = g_malloc0(sizeof(DisplayState));
1802651c
JK
1329 int width = 640;
1330 int height = 480;
1331
1802651c
JK
1332 if (is_fixedsize_console()) {
1333 width = active_console->g_width;
1334 height = active_console->g_height;
1335 }
c78f7137 1336 ds->surface = qemu_create_displaysurface(width, height);
da229ef3 1337
98b50080
PB
1338 register_displaystate(ds);
1339}
1340
1341/***********************************************************/
1342/* register display */
1343
1344void register_displaystate(DisplayState *ds)
1345{
1346 DisplayState **s;
1347 s = &display_state;
1348 while (*s != NULL)
1349 s = &(*s)->next;
1350 ds->next = NULL;
1351 *s = ds;
1352}
1353
1354DisplayState *get_displaystate(void)
1355{
1356 if (!display_state) {
1357 dumb_display_init ();
1358 }
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{
76ffb0b4 1368 QemuConsole *s;
3023f332 1369 DisplayState *ds;
f0f2f976 1370
7267c094 1371 ds = (DisplayState *) g_malloc0(sizeof(DisplayState));
437fe106 1372 trace_console_gfx_new();
af3a9031 1373 s = new_console(ds, GRAPHIC_CONSOLE);
95219897
PB
1374 s->hw_update = update;
1375 s->hw_invalidate = invalidate;
1376 s->hw_screen_dump = screen_dump;
4d3b6f6e 1377 s->hw_text_update = text_update;
95219897 1378 s->hw = opaque;
3023f332 1379
c78f7137 1380 ds->surface = qemu_create_displaysurface(640, 480);
da229ef3 1381
f0f2f976 1382 register_displaystate(ds);
c78f7137 1383 return s;
e7f0ad58
FB
1384}
1385
95219897 1386int is_graphic_console(void)
e7f0ad58 1387{
4d3b6f6e 1388 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
e7f0ad58
FB
1389}
1390
c21bbcfa
AZ
1391int is_fixedsize_console(void)
1392{
1393 return active_console && active_console->console_type != TEXT_CONSOLE;
1394}
1395
4104833f
PB
1396static void text_console_set_echo(CharDriverState *chr, bool echo)
1397{
76ffb0b4 1398 QemuConsole *s = chr->opaque;
4104833f
PB
1399
1400 s->echo = echo;
1401}
1402
bf1bed81
JK
1403static void text_console_update_cursor(void *opaque)
1404{
76ffb0b4 1405 QemuConsole *s = opaque;
bf1bed81
JK
1406
1407 s->cursor_visible_phase = !s->cursor_visible_phase;
1408 vga_hw_invalidate();
1409 qemu_mod_timer(s->cursor_timer,
1410 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1411}
1412
44b37b93 1413static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
e7f0ad58 1414{
76ffb0b4 1415 QemuConsole *s;
6d6f7c28 1416
491e114a 1417 s = chr->opaque;
6ea314d9 1418
e7f0ad58 1419 chr->chr_write = console_puts;
6fcfafb7 1420
e15d7371
FB
1421 s->out_fifo.buf = s->out_fifo_buf;
1422 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
7bd427d8 1423 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
3023f332 1424 s->ds = ds;
3b46e624 1425
e7f0ad58
FB
1426 s->y_displayed = 0;
1427 s->y_base = 0;
1428 s->total_height = DEFAULT_BACKSCROLL;
1429 s->x = 0;
1430 s->y = 0;
491e114a 1431 if (s->console_type == TEXT_CONSOLE) {
1562e531
GH
1432 s->g_width = surface_width(s->ds->surface);
1433 s->g_height = surface_height(s->ds->surface);
491e114a 1434 }
6d6f7c28 1435
bf1bed81
JK
1436 s->cursor_timer =
1437 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1438
4d3b6f6e
AZ
1439 s->hw_invalidate = text_console_invalidate;
1440 s->hw_text_update = text_console_update;
1441 s->hw = s;
1442
6d6f7c28
PB
1443 /* Set text attribute defaults */
1444 s->t_attrib_default.bold = 0;
1445 s->t_attrib_default.uline = 0;
1446 s->t_attrib_default.blink = 0;
1447 s->t_attrib_default.invers = 0;
1448 s->t_attrib_default.unvisible = 0;
1449 s->t_attrib_default.fgcol = COLOR_WHITE;
1450 s->t_attrib_default.bgcol = COLOR_BLACK;
6d6f7c28
PB
1451 /* set current text attributes to default */
1452 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
1453 text_console_resize(s);
1454
51bfa4d3
GH
1455 if (chr->label) {
1456 char msg[128];
1457 int len;
1458
735ba588 1459 s->t_attrib.bgcol = COLOR_BLUE;
51bfa4d3
GH
1460 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1461 console_puts(chr, (uint8_t*)msg, len);
735ba588 1462 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
1463 }
1464
fee204fd 1465 qemu_chr_be_generic_open(chr);
ceecf1d1
AJ
1466 if (chr->init)
1467 chr->init(chr);
e7f0ad58 1468}
c60e08d9 1469
702ec69c 1470static CharDriverState *text_console_init(ChardevVC *vc)
2796dae0
AL
1471{
1472 CharDriverState *chr;
76ffb0b4 1473 QemuConsole *s;
702ec69c
GH
1474 unsigned width = 0;
1475 unsigned height = 0;
2796dae0 1476
7267c094 1477 chr = g_malloc0(sizeof(CharDriverState));
2796dae0 1478
702ec69c
GH
1479 if (vc->has_width) {
1480 width = vc->width;
1481 } else if (vc->has_cols) {
1482 width = vc->cols * FONT_WIDTH;
1483 }
491e114a 1484
702ec69c
GH
1485 if (vc->has_height) {
1486 height = vc->height;
1487 } else if (vc->has_rows) {
1488 height = vc->rows * FONT_HEIGHT;
1489 }
491e114a 1490
437fe106 1491 trace_console_txt_new(width, height);
491e114a
PB
1492 if (width == 0 || height == 0) {
1493 s = new_console(NULL, TEXT_CONSOLE);
1494 } else {
1495 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1496 }
1497
1498 if (!s) {
5354d083 1499 g_free(chr);
1f51470d 1500 return NULL;
491e114a
PB
1501 }
1502
1503 s->chr = chr;
1504 s->g_width = width;
1505 s->g_height = height;
1506 chr->opaque = s;
4104833f 1507 chr->chr_set_echo = text_console_set_echo;
1f51470d 1508 return chr;
2796dae0
AL
1509}
1510
d82831db
AL
1511static VcHandler *vc_handler = text_console_init;
1512
702ec69c 1513CharDriverState *vc_init(ChardevVC *vc)
d82831db 1514{
702ec69c 1515 return vc_handler(vc);
d82831db
AL
1516}
1517
1518void register_vc_handler(VcHandler *handler)
1519{
1520 vc_handler = handler;
1521}
1522
2796dae0
AL
1523void text_consoles_set_display(DisplayState *ds)
1524{
1525 int i;
1526
8811e1e1
MA
1527 for (i = 0; i < nb_consoles; i++) {
1528 if (consoles[i]->console_type != GRAPHIC_CONSOLE) {
1529 text_console_do_init(consoles[i]->chr, ds);
1530 }
2796dae0 1531 }
2796dae0
AL
1532}
1533
c78f7137 1534void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 1535{
3023f332
AL
1536 s->g_width = width;
1537 s->g_height = height;
1538 if (is_graphic_console()) {
da229ef3
GH
1539 DisplaySurface *surface;
1540 surface = qemu_create_displaysurface(width, height);
c78f7137 1541 dpy_gfx_replace_surface(s, surface);
c60e08d9
PB
1542 }
1543}
38334f76 1544
c78f7137 1545void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
3023f332 1546 int dst_x, int dst_y, int w, int h)
c21bbcfa 1547{
3023f332 1548 if (is_graphic_console()) {
c78f7137 1549 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
38334f76
AZ
1550 }
1551}
7d957bd8 1552
c78f7137
GH
1553DisplaySurface *qemu_console_surface(QemuConsole *console)
1554{
1555 return console->ds->surface;
1556}
1557
1558DisplayState *qemu_console_displaystate(QemuConsole *console)
1559{
1560 return console->ds;
1561}
1562
0da2ea1b 1563PixelFormat qemu_different_endianness_pixelformat(int bpp)
7d957bd8
AL
1564{
1565 PixelFormat pf;
1566
1567 memset(&pf, 0x00, sizeof(PixelFormat));
1568
1569 pf.bits_per_pixel = bpp;
feadf1a4 1570 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
7d957bd8
AL
1571 pf.depth = bpp == 32 ? 24 : bpp;
1572
1573 switch (bpp) {
0da2ea1b 1574 case 24:
1575 pf.rmask = 0x000000FF;
1576 pf.gmask = 0x0000FF00;
1577 pf.bmask = 0x00FF0000;
1578 pf.rmax = 255;
1579 pf.gmax = 255;
1580 pf.bmax = 255;
1581 pf.rshift = 0;
1582 pf.gshift = 8;
1583 pf.bshift = 16;
90a1e3c0
AL
1584 pf.rbits = 8;
1585 pf.gbits = 8;
1586 pf.bbits = 8;
7d957bd8 1587 break;
0da2ea1b 1588 case 32:
1589 pf.rmask = 0x0000FF00;
1590 pf.gmask = 0x00FF0000;
1591 pf.bmask = 0xFF000000;
1592 pf.amask = 0x00000000;
1593 pf.amax = 255;
1594 pf.rmax = 255;
1595 pf.gmax = 255;
1596 pf.bmax = 255;
1597 pf.ashift = 0;
1598 pf.rshift = 8;
1599 pf.gshift = 16;
1600 pf.bshift = 24;
90a1e3c0
AL
1601 pf.rbits = 8;
1602 pf.gbits = 8;
1603 pf.bbits = 8;
1604 pf.abits = 8;
0da2ea1b 1605 break;
1606 default:
1607 break;
1608 }
1609 return pf;
1610}
1611
1612PixelFormat qemu_default_pixelformat(int bpp)
1613{
1614 PixelFormat pf;
1615
1616 memset(&pf, 0x00, sizeof(PixelFormat));
1617
1618 pf.bits_per_pixel = bpp;
feadf1a4 1619 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
0da2ea1b 1620 pf.depth = bpp == 32 ? 24 : bpp;
1621
1622 switch (bpp) {
b6278084
GH
1623 case 15:
1624 pf.bits_per_pixel = 16;
b6278084
GH
1625 pf.rmask = 0x00007c00;
1626 pf.gmask = 0x000003E0;
1627 pf.bmask = 0x0000001F;
1628 pf.rmax = 31;
1629 pf.gmax = 31;
1630 pf.bmax = 31;
1631 pf.rshift = 10;
1632 pf.gshift = 5;
1633 pf.bshift = 0;
1634 pf.rbits = 5;
1635 pf.gbits = 5;
1636 pf.bbits = 5;
1637 break;
7d957bd8
AL
1638 case 16:
1639 pf.rmask = 0x0000F800;
1640 pf.gmask = 0x000007E0;
1641 pf.bmask = 0x0000001F;
1642 pf.rmax = 31;
1643 pf.gmax = 63;
1644 pf.bmax = 31;
1645 pf.rshift = 11;
1646 pf.gshift = 5;
1647 pf.bshift = 0;
90a1e3c0
AL
1648 pf.rbits = 5;
1649 pf.gbits = 6;
1650 pf.bbits = 5;
7d957bd8
AL
1651 break;
1652 case 24:
0da2ea1b 1653 pf.rmask = 0x00FF0000;
1654 pf.gmask = 0x0000FF00;
1655 pf.bmask = 0x000000FF;
1656 pf.rmax = 255;
1657 pf.gmax = 255;
1658 pf.bmax = 255;
1659 pf.rshift = 16;
1660 pf.gshift = 8;
1661 pf.bshift = 0;
90a1e3c0
AL
1662 pf.rbits = 8;
1663 pf.gbits = 8;
1664 pf.bbits = 8;
0eba62e0 1665 break;
7d957bd8
AL
1666 case 32:
1667 pf.rmask = 0x00FF0000;
1668 pf.gmask = 0x0000FF00;
1669 pf.bmask = 0x000000FF;
1670 pf.rmax = 255;
1671 pf.gmax = 255;
1672 pf.bmax = 255;
1673 pf.rshift = 16;
1674 pf.gshift = 8;
1675 pf.bshift = 0;
90a1e3c0
AL
1676 pf.rbits = 8;
1677 pf.gbits = 8;
1678 pf.bbits = 8;
7d957bd8
AL
1679 break;
1680 default:
1681 break;
1682 }
1683 return pf;
1684}
01f45d98 1685
702ec69c
GH
1686static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1687 Error **errp)
1688{
1689 int val;
1690
1691 backend->vc = g_new0(ChardevVC, 1);
1692
1693 val = qemu_opt_get_number(opts, "width", 0);
1694 if (val != 0) {
1695 backend->vc->has_width = true;
1696 backend->vc->width = val;
1697 }
1698
1699 val = qemu_opt_get_number(opts, "height", 0);
1700 if (val != 0) {
1701 backend->vc->has_height = true;
1702 backend->vc->height = val;
1703 }
1704
1705 val = qemu_opt_get_number(opts, "cols", 0);
1706 if (val != 0) {
1707 backend->vc->has_cols = true;
1708 backend->vc->cols = val;
1709 }
1710
1711 val = qemu_opt_get_number(opts, "rows", 0);
1712 if (val != 0) {
1713 backend->vc->has_rows = true;
1714 backend->vc->rows = val;
1715 }
1716}
1717
01f45d98
AL
1718static void register_types(void)
1719{
702ec69c
GH
1720 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1721 qemu_chr_parse_vc);
01f45d98
AL
1722}
1723
1724type_init(register_types);