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