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