]> git.proxmox.com Git - mirror_qemu.git/blame - ui/console.c
console: add head to index to qemu consoles.
[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"
aa2beaa1 26#include "hw/qdev-core.h"
1de7afc9 27#include "qemu/timer.h"
ad39cf6d 28#include "qmp-commands.h"
dccfcd0e 29#include "sysemu/char.h"
ac86048b 30#include "trace.h"
e7f0ad58
FB
31
32#define DEFAULT_BACKSCROLL 512
33#define MAX_CONSOLES 12
bf1bed81 34#define CONSOLE_CURSOR_PERIOD 500
e7f0ad58 35
6d6f7c28
PB
36typedef struct TextAttributes {
37 uint8_t fgcol:4;
38 uint8_t bgcol:4;
39 uint8_t bold:1;
40 uint8_t uline:1;
41 uint8_t blink:1;
42 uint8_t invers:1;
43 uint8_t unvisible:1;
44} TextAttributes;
45
e7f0ad58
FB
46typedef struct TextCell {
47 uint8_t ch;
6d6f7c28 48 TextAttributes t_attrib;
e7f0ad58
FB
49} TextCell;
50
51#define MAX_ESC_PARAMS 3
52
53enum TTYState {
54 TTY_STATE_NORM,
55 TTY_STATE_ESC,
56 TTY_STATE_CSI,
57};
58
e15d7371
FB
59typedef struct QEMUFIFO {
60 uint8_t *buf;
61 int buf_size;
62 int count, wptr, rptr;
63} QEMUFIFO;
64
9596ebb7 65static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
e15d7371
FB
66{
67 int l, len;
68
69 l = f->buf_size - f->count;
70 if (len1 > l)
71 len1 = l;
72 len = len1;
73 while (len > 0) {
74 l = f->buf_size - f->wptr;
75 if (l > len)
76 l = len;
77 memcpy(f->buf + f->wptr, buf, l);
78 f->wptr += l;
79 if (f->wptr >= f->buf_size)
80 f->wptr = 0;
81 buf += l;
82 len -= l;
83 }
84 f->count += len1;
85 return len1;
86}
87
9596ebb7 88static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
e15d7371
FB
89{
90 int l, len;
91
92 if (len1 > f->count)
93 len1 = f->count;
94 len = len1;
95 while (len > 0) {
96 l = f->buf_size - f->rptr;
97 if (l > len)
98 l = len;
99 memcpy(buf, f->buf + f->rptr, l);
100 f->rptr += l;
101 if (f->rptr >= f->buf_size)
102 f->rptr = 0;
103 buf += l;
104 len -= l;
105 }
106 f->count -= len1;
107 return len1;
108}
109
af3a9031
TS
110typedef enum {
111 GRAPHIC_CONSOLE,
c21bbcfa
AZ
112 TEXT_CONSOLE,
113 TEXT_CONSOLE_FIXED_SIZE
c227f099 114} console_type_t;
af3a9031 115
76ffb0b4 116struct QemuConsole {
95be0669
GH
117 Object parent;
118
f81bdefb 119 int index;
c227f099 120 console_type_t console_type;
e7f0ad58 121 DisplayState *ds;
321f048d 122 DisplaySurface *surface;
284d1c6b 123 int dcls;
76ffb0b4 124
95219897 125 /* Graphic console state. */
aa2beaa1 126 Object *device;
5643706a 127 uint32_t head;
380cd056 128 const GraphicHwOps *hw_ops;
95219897 129 void *hw;
76ffb0b4
GH
130
131 /* Text console state */
e7f0ad58
FB
132 int width;
133 int height;
134 int total_height;
135 int backscroll_height;
e7f0ad58 136 int x, y;
adb47967 137 int x_saved, y_saved;
e7f0ad58
FB
138 int y_displayed;
139 int y_base;
6d6f7c28
PB
140 TextAttributes t_attrib_default; /* default text attributes */
141 TextAttributes t_attrib; /* currently active text attributes */
e7f0ad58 142 TextCell *cells;
4d3b6f6e 143 int text_x[2], text_y[2], cursor_invalidate;
4104833f 144 int echo;
bf1bed81
JK
145 bool cursor_visible_phase;
146 QEMUTimer *cursor_timer;
e7f0ad58 147
14778c20
PB
148 int update_x0;
149 int update_y0;
150 int update_x1;
151 int update_y1;
152
e7f0ad58
FB
153 enum TTYState state;
154 int esc_params[MAX_ESC_PARAMS];
155 int nb_esc_params;
156
e5b0bc44 157 CharDriverState *chr;
e15d7371
FB
158 /* fifo for key pressed */
159 QEMUFIFO out_fifo;
160 uint8_t out_fifo_buf[16];
161 QEMUTimer *kbd_timer;
e7f0ad58
FB
162};
163
27be5587 164struct DisplayState {
1246b259 165 QEMUTimer *gui_timer;
0f7b2864
GH
166 uint64_t last_update;
167 uint64_t update_interval;
168 bool refreshing;
27be5587
GH
169 bool have_gfx;
170 bool have_text;
171
172 QLIST_HEAD(, DisplayChangeListener) listeners;
173};
174
98b50080 175static DisplayState *display_state;
76ffb0b4
GH
176static QemuConsole *active_console;
177static QemuConsole *consoles[MAX_CONSOLES];
e7f0ad58
FB
178static int nb_consoles = 0;
179
64840c66 180static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
0f7b2864 181static void dpy_refresh(DisplayState *s);
5209089f 182static DisplayState *get_alloc_displaystate(void);
64840c66 183
98a9ad90
GH
184static void gui_update(void *opaque)
185{
0f7b2864
GH
186 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
187 uint64_t dcl_interval;
98a9ad90
GH
188 DisplayState *ds = opaque;
189 DisplayChangeListener *dcl;
dea1b0bd 190 int i;
98a9ad90 191
0f7b2864 192 ds->refreshing = true;
98a9ad90 193 dpy_refresh(ds);
0f7b2864 194 ds->refreshing = false;
98a9ad90
GH
195
196 QLIST_FOREACH(dcl, &ds->listeners, next) {
0f7b2864
GH
197 dcl_interval = dcl->update_interval ?
198 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
199 if (interval > dcl_interval) {
200 interval = dcl_interval;
98a9ad90
GH
201 }
202 }
0f7b2864
GH
203 if (ds->update_interval != interval) {
204 ds->update_interval = interval;
dea1b0bd
GH
205 for (i = 0; i < nb_consoles; i++) {
206 if (consoles[i]->hw_ops->update_interval) {
207 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
208 }
209 }
0f7b2864
GH
210 trace_console_refresh(interval);
211 }
bc72ad67
AB
212 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
213 timer_mod(ds->gui_timer, ds->last_update + interval);
98a9ad90
GH
214}
215
216static void gui_setup_refresh(DisplayState *ds)
217{
218 DisplayChangeListener *dcl;
219 bool need_timer = false;
220 bool have_gfx = false;
221 bool have_text = false;
222
223 QLIST_FOREACH(dcl, &ds->listeners, next) {
224 if (dcl->ops->dpy_refresh != NULL) {
225 need_timer = true;
226 }
227 if (dcl->ops->dpy_gfx_update != NULL) {
228 have_gfx = true;
229 }
230 if (dcl->ops->dpy_text_update != NULL) {
231 have_text = true;
232 }
233 }
234
235 if (need_timer && ds->gui_timer == NULL) {
bc72ad67
AB
236 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
237 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
98a9ad90
GH
238 }
239 if (!need_timer && ds->gui_timer != NULL) {
bc72ad67
AB
240 timer_del(ds->gui_timer);
241 timer_free(ds->gui_timer);
98a9ad90
GH
242 ds->gui_timer = NULL;
243 }
244
245 ds->have_gfx = have_gfx;
246 ds->have_text = have_text;
247}
248
1dbfa005 249void graphic_hw_update(QemuConsole *con)
95219897 250{
1dbfa005
GH
251 if (!con) {
252 con = active_console;
253 }
380cd056
GH
254 if (con && con->hw_ops->gfx_update) {
255 con->hw_ops->gfx_update(con->hw);
1dbfa005 256 }
95219897
PB
257}
258
1dbfa005 259void graphic_hw_invalidate(QemuConsole *con)
95219897 260{
1dbfa005
GH
261 if (!con) {
262 con = active_console;
263 }
380cd056
GH
264 if (con && con->hw_ops->invalidate) {
265 con->hw_ops->invalidate(con->hw);
1dbfa005 266 }
95219897
PB
267}
268
2c62f08d
GH
269static void ppm_save(const char *filename, struct DisplaySurface *ds,
270 Error **errp)
95219897 271{
2c62f08d
GH
272 int width = pixman_image_get_width(ds->image);
273 int height = pixman_image_get_height(ds->image);
cdd5b937 274 int fd;
2c62f08d
GH
275 FILE *f;
276 int y;
277 int ret;
278 pixman_image_t *linebuf;
279
280 trace_ppm_save(filename, ds);
cdd5b937
GH
281 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
282 if (fd == -1) {
2c62f08d
GH
283 error_setg(errp, "failed to open file '%s': %s", filename,
284 strerror(errno));
285 return;
45efb161 286 }
cdd5b937 287 f = fdopen(fd, "wb");
2c62f08d
GH
288 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
289 if (ret < 0) {
290 linebuf = NULL;
291 goto write_err;
292 }
293 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
294 for (y = 0; y < height; y++) {
295 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
296 clearerr(f);
297 ret = fwrite(pixman_image_get_data(linebuf), 1,
298 pixman_image_get_stride(linebuf), f);
299 (void)ret;
300 if (ferror(f)) {
301 goto write_err;
302 }
f81bdefb
JK
303 }
304
2c62f08d
GH
305out:
306 qemu_pixman_image_unref(linebuf);
307 fclose(f);
308 return;
309
310write_err:
311 error_setg(errp, "failed to write to file '%s': %s", filename,
312 strerror(errno));
313 unlink(filename);
314 goto out;
315}
316
317void qmp_screendump(const char *filename, Error **errp)
318{
284d1c6b 319 QemuConsole *con = qemu_console_lookup_by_index(0);
2c62f08d
GH
320 DisplaySurface *surface;
321
322 if (con == NULL) {
323 error_setg(errp, "There is no QemuConsole I can screendump from.");
324 return;
33bcd98c 325 }
2c62f08d
GH
326
327 graphic_hw_update(con);
328 surface = qemu_console_surface(con);
329 ppm_save(filename, surface, errp);
95219897
PB
330}
331
1dbfa005 332void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
4d3b6f6e 333{
1dbfa005
GH
334 if (!con) {
335 con = active_console;
336 }
380cd056
GH
337 if (con && con->hw_ops->text_update) {
338 con->hw_ops->text_update(con->hw, chardata);
339 }
4d3b6f6e
AZ
340}
341
1562e531
GH
342static void vga_fill_rect(QemuConsole *con,
343 int posx, int posy, int width, int height,
e27bd65a 344 pixman_color_t color)
e7f0ad58 345{
1562e531 346 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
347 pixman_rectangle16_t rect = {
348 .x = posx, .y = posy, .width = width, .height = height
349 };
68db6dc5 350
68db6dc5 351 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
e27bd65a 352 &color, 1, &rect);
e7f0ad58
FB
353}
354
355/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
1562e531
GH
356static void vga_bitblt(QemuConsole *con,
357 int xs, int ys, int xd, int yd, int w, int h)
e7f0ad58 358{
1562e531 359 DisplaySurface *surface = qemu_console_surface(con);
68db6dc5
GH
360
361 pixman_image_composite(PIXMAN_OP_SRC,
362 surface->image, NULL, surface->image,
363 xs, ys, 0, 0, xd, yd, w, h);
e7f0ad58
FB
364}
365
366/***********************************************************/
367/* basic char display */
368
369#define FONT_HEIGHT 16
370#define FONT_WIDTH 8
371
372#include "vgafont.h"
373
df00bed0 374#ifndef CONFIG_CURSES
6d6f7c28
PB
375enum color_names {
376 COLOR_BLACK = 0,
377 COLOR_RED = 1,
378 COLOR_GREEN = 2,
379 COLOR_YELLOW = 3,
380 COLOR_BLUE = 4,
381 COLOR_MAGENTA = 5,
382 COLOR_CYAN = 6,
383 COLOR_WHITE = 7
384};
df00bed0 385#endif
6d6f7c28 386
e27bd65a
GH
387#define QEMU_RGB(r, g, b) \
388 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
389
390static const pixman_color_t color_table_rgb[2][8] = {
6d6f7c28 391 { /* dark */
26489844
FB
392 QEMU_RGB(0x00, 0x00, 0x00), /* black */
393 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
394 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
395 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
396 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
397 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
398 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
399 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
6d6f7c28
PB
400 },
401 { /* bright */
26489844
FB
402 QEMU_RGB(0x00, 0x00, 0x00), /* black */
403 QEMU_RGB(0xff, 0x00, 0x00), /* red */
404 QEMU_RGB(0x00, 0xff, 0x00), /* green */
405 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
406 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
407 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
408 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
409 QEMU_RGB(0xff, 0xff, 0xff), /* white */
6d6f7c28 410 }
e7f0ad58
FB
411};
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
81c0d5a6 492 if (!qemu_console_is_visible(s)) {
1562e531
GH
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
81c0d5a6 520 if (!qemu_console_is_visible(s)) {
1562e531
GH
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
81c0d5a6 556 if (!qemu_console_is_visible(s)) {
e7f0ad58 557 return;
81c0d5a6 558 }
a93a4a22
GH
559
560 if (s->ds->have_text) {
4d3b6f6e
AZ
561 s->text_x[0] = 0;
562 s->text_y[0] = 0;
563 s->text_x[1] = s->width - 1;
564 s->text_y[1] = s->height - 1;
565 s->cursor_invalidate = 1;
4d3b6f6e 566 }
e7f0ad58 567
a93a4a22 568 if (s->ds->have_gfx) {
1562e531 569 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
cf6f0548 570 color_table_rgb[0][COLOR_BLACK]);
a93a4a22
GH
571 y1 = s->y_displayed;
572 for (y = 0; y < s->height; y++) {
573 c = s->cells + y1 * s->width;
574 for (x = 0; x < s->width; x++) {
1562e531 575 vga_putcharxy(s, x, y, c->ch,
a93a4a22
GH
576 &(c->t_attrib));
577 c++;
578 }
579 if (++y1 == s->total_height) {
580 y1 = 0;
581 }
e7f0ad58 582 }
a93a4a22 583 console_show_cursor(s, 1);
1562e531
GH
584 dpy_gfx_update(s, 0, 0,
585 surface_width(surface), surface_height(surface));
e7f0ad58 586 }
e7f0ad58
FB
587}
588
81c0d5a6 589static void console_scroll(QemuConsole *s, int ydelta)
e7f0ad58 590{
e7f0ad58 591 int i, y1;
3b46e624 592
e7f0ad58
FB
593 if (ydelta > 0) {
594 for(i = 0; i < ydelta; i++) {
595 if (s->y_displayed == s->y_base)
596 break;
597 if (++s->y_displayed == s->total_height)
598 s->y_displayed = 0;
599 }
600 } else {
601 ydelta = -ydelta;
602 i = s->backscroll_height;
603 if (i > s->total_height - s->height)
604 i = s->total_height - s->height;
605 y1 = s->y_base - i;
606 if (y1 < 0)
607 y1 += s->total_height;
608 for(i = 0; i < ydelta; i++) {
609 if (s->y_displayed == y1)
610 break;
611 if (--s->y_displayed < 0)
612 s->y_displayed = s->total_height - 1;
613 }
614 }
615 console_refresh(s);
616}
617
76ffb0b4 618static void console_put_lf(QemuConsole *s)
e7f0ad58
FB
619{
620 TextCell *c;
621 int x, y1;
622
e7f0ad58
FB
623 s->y++;
624 if (s->y >= s->height) {
625 s->y = s->height - 1;
6d6f7c28 626
e7f0ad58
FB
627 if (s->y_displayed == s->y_base) {
628 if (++s->y_displayed == s->total_height)
629 s->y_displayed = 0;
630 }
631 if (++s->y_base == s->total_height)
632 s->y_base = 0;
633 if (s->backscroll_height < s->total_height)
634 s->backscroll_height++;
635 y1 = (s->y_base + s->height - 1) % s->total_height;
636 c = &s->cells[y1 * s->width];
637 for(x = 0; x < s->width; x++) {
638 c->ch = ' ';
6d6f7c28 639 c->t_attrib = s->t_attrib_default;
e7f0ad58
FB
640 c++;
641 }
81c0d5a6 642 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
1562e531 643 if (s->ds->have_text) {
4d3b6f6e
AZ
644 s->text_x[0] = 0;
645 s->text_y[0] = 0;
646 s->text_x[1] = s->width - 1;
647 s->text_y[1] = s->height - 1;
4d3b6f6e
AZ
648 }
649
1562e531
GH
650 if (s->ds->have_gfx) {
651 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
652 s->width * FONT_WIDTH,
653 (s->height - 1) * FONT_HEIGHT);
654 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
655 s->width * FONT_WIDTH, FONT_HEIGHT,
656 color_table_rgb[0][s->t_attrib_default.bgcol]);
657 s->update_x0 = 0;
658 s->update_y0 = 0;
659 s->update_x1 = s->width * FONT_WIDTH;
660 s->update_y1 = s->height * FONT_HEIGHT;
661 }
e7f0ad58
FB
662 }
663 }
664}
665
6d6f7c28
PB
666/* Set console attributes depending on the current escape codes.
667 * NOTE: I know this code is not very efficient (checking every color for it
668 * self) but it is more readable and better maintainable.
669 */
76ffb0b4 670static void console_handle_escape(QemuConsole *s)
6d6f7c28
PB
671{
672 int i;
673
6d6f7c28
PB
674 for (i=0; i<s->nb_esc_params; i++) {
675 switch (s->esc_params[i]) {
676 case 0: /* reset all console attributes to default */
677 s->t_attrib = s->t_attrib_default;
678 break;
679 case 1:
680 s->t_attrib.bold = 1;
681 break;
682 case 4:
683 s->t_attrib.uline = 1;
684 break;
685 case 5:
686 s->t_attrib.blink = 1;
687 break;
688 case 7:
689 s->t_attrib.invers = 1;
690 break;
691 case 8:
692 s->t_attrib.unvisible = 1;
693 break;
694 case 22:
695 s->t_attrib.bold = 0;
696 break;
697 case 24:
698 s->t_attrib.uline = 0;
699 break;
700 case 25:
701 s->t_attrib.blink = 0;
702 break;
703 case 27:
704 s->t_attrib.invers = 0;
705 break;
706 case 28:
707 s->t_attrib.unvisible = 0;
708 break;
709 /* set foreground color */
710 case 30:
711 s->t_attrib.fgcol=COLOR_BLACK;
712 break;
713 case 31:
714 s->t_attrib.fgcol=COLOR_RED;
715 break;
716 case 32:
717 s->t_attrib.fgcol=COLOR_GREEN;
718 break;
719 case 33:
720 s->t_attrib.fgcol=COLOR_YELLOW;
721 break;
722 case 34:
723 s->t_attrib.fgcol=COLOR_BLUE;
724 break;
725 case 35:
726 s->t_attrib.fgcol=COLOR_MAGENTA;
727 break;
728 case 36:
729 s->t_attrib.fgcol=COLOR_CYAN;
730 break;
731 case 37:
732 s->t_attrib.fgcol=COLOR_WHITE;
733 break;
734 /* set background color */
735 case 40:
736 s->t_attrib.bgcol=COLOR_BLACK;
737 break;
738 case 41:
739 s->t_attrib.bgcol=COLOR_RED;
740 break;
741 case 42:
742 s->t_attrib.bgcol=COLOR_GREEN;
743 break;
744 case 43:
745 s->t_attrib.bgcol=COLOR_YELLOW;
746 break;
747 case 44:
748 s->t_attrib.bgcol=COLOR_BLUE;
749 break;
750 case 45:
751 s->t_attrib.bgcol=COLOR_MAGENTA;
752 break;
753 case 46:
754 s->t_attrib.bgcol=COLOR_CYAN;
755 break;
756 case 47:
757 s->t_attrib.bgcol=COLOR_WHITE;
758 break;
759 }
760 }
761}
762
76ffb0b4 763static void console_clear_xy(QemuConsole *s, int x, int y)
adb47967
TS
764{
765 int y1 = (s->y_base + y) % s->total_height;
766 TextCell *c = &s->cells[y1 * s->width + x];
767 c->ch = ' ';
768 c->t_attrib = s->t_attrib_default;
adb47967
TS
769 update_xy(s, x, y);
770}
771
3eea5498 772/* set cursor, checking bounds */
76ffb0b4 773static void set_cursor(QemuConsole *s, int x, int y)
3eea5498
IC
774{
775 if (x < 0) {
776 x = 0;
777 }
778 if (y < 0) {
779 y = 0;
780 }
781 if (y >= s->height) {
782 y = s->height - 1;
783 }
784 if (x >= s->width) {
785 x = s->width - 1;
786 }
787
788 s->x = x;
789 s->y = y;
790}
791
76ffb0b4 792static void console_putchar(QemuConsole *s, int ch)
e7f0ad58
FB
793{
794 TextCell *c;
adb47967
TS
795 int y1, i;
796 int x, y;
e7f0ad58
FB
797
798 switch(s->state) {
799 case TTY_STATE_NORM:
800 switch(ch) {
6d6f7c28 801 case '\r': /* carriage return */
e7f0ad58
FB
802 s->x = 0;
803 break;
6d6f7c28 804 case '\n': /* newline */
e7f0ad58
FB
805 console_put_lf(s);
806 break;
6d6f7c28 807 case '\b': /* backspace */
5fafdf24 808 if (s->x > 0)
e15d7371 809 s->x--;
6d6f7c28
PB
810 break;
811 case '\t': /* tabspace */
812 if (s->x + (8 - (s->x % 8)) > s->width) {
bd468840 813 s->x = 0;
6d6f7c28
PB
814 console_put_lf(s);
815 } else {
816 s->x = s->x + (8 - (s->x % 8));
817 }
818 break;
819 case '\a': /* alert aka. bell */
820 /* TODO: has to be implemented */
821 break;
adb47967
TS
822 case 14:
823 /* SI (shift in), character set 0 (ignored) */
824 break;
825 case 15:
826 /* SO (shift out), character set 1 (ignored) */
827 break;
6d6f7c28 828 case 27: /* esc (introducing an escape sequence) */
e7f0ad58
FB
829 s->state = TTY_STATE_ESC;
830 break;
831 default:
ed8276ac
TS
832 if (s->x >= s->width) {
833 /* line wrap */
834 s->x = 0;
835 console_put_lf(s);
adb47967 836 }
e7f0ad58
FB
837 y1 = (s->y_base + s->y) % s->total_height;
838 c = &s->cells[y1 * s->width + s->x];
839 c->ch = ch;
6d6f7c28 840 c->t_attrib = s->t_attrib;
e7f0ad58
FB
841 update_xy(s, s->x, s->y);
842 s->x++;
e7f0ad58
FB
843 break;
844 }
845 break;
6d6f7c28 846 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
e7f0ad58
FB
847 if (ch == '[') {
848 for(i=0;i<MAX_ESC_PARAMS;i++)
849 s->esc_params[i] = 0;
850 s->nb_esc_params = 0;
851 s->state = TTY_STATE_CSI;
852 } else {
853 s->state = TTY_STATE_NORM;
854 }
855 break;
6d6f7c28 856 case TTY_STATE_CSI: /* handle escape sequence parameters */
e7f0ad58
FB
857 if (ch >= '0' && ch <= '9') {
858 if (s->nb_esc_params < MAX_ESC_PARAMS) {
c10600af
LE
859 int *param = &s->esc_params[s->nb_esc_params];
860 int digit = (ch - '0');
861
862 *param = (*param <= (INT_MAX - digit) / 10) ?
863 *param * 10 + digit : INT_MAX;
e7f0ad58
FB
864 }
865 } else {
3eea5498
IC
866 if (s->nb_esc_params < MAX_ESC_PARAMS)
867 s->nb_esc_params++;
e7f0ad58
FB
868 if (ch == ';')
869 break;
5d28b0e9
SW
870 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
871 ch, s->nb_esc_params);
e7f0ad58
FB
872 s->state = TTY_STATE_NORM;
873 switch(ch) {
adb47967
TS
874 case 'A':
875 /* move cursor up */
876 if (s->esc_params[0] == 0) {
877 s->esc_params[0] = 1;
878 }
3eea5498 879 set_cursor(s, s->x, s->y - s->esc_params[0]);
adb47967
TS
880 break;
881 case 'B':
882 /* move cursor down */
883 if (s->esc_params[0] == 0) {
884 s->esc_params[0] = 1;
885 }
3eea5498 886 set_cursor(s, s->x, s->y + s->esc_params[0]);
e7f0ad58
FB
887 break;
888 case 'C':
adb47967
TS
889 /* move cursor right */
890 if (s->esc_params[0] == 0) {
891 s->esc_params[0] = 1;
892 }
3eea5498 893 set_cursor(s, s->x + s->esc_params[0], s->y);
e7f0ad58 894 break;
adb47967
TS
895 case 'D':
896 /* move cursor left */
897 if (s->esc_params[0] == 0) {
898 s->esc_params[0] = 1;
899 }
3eea5498 900 set_cursor(s, s->x - s->esc_params[0], s->y);
adb47967
TS
901 break;
902 case 'G':
903 /* move cursor to column */
3eea5498 904 set_cursor(s, s->esc_params[0] - 1, s->y);
adb47967
TS
905 break;
906 case 'f':
907 case 'H':
908 /* move cursor to row, column */
3eea5498 909 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
adb47967
TS
910 break;
911 case 'J':
912 switch (s->esc_params[0]) {
913 case 0:
914 /* clear to end of screen */
915 for (y = s->y; y < s->height; y++) {
916 for (x = 0; x < s->width; x++) {
917 if (y == s->y && x < s->x) {
918 continue;
919 }
920 console_clear_xy(s, x, y);
921 }
922 }
923 break;
924 case 1:
925 /* clear from beginning of screen */
926 for (y = 0; y <= s->y; y++) {
927 for (x = 0; x < s->width; x++) {
928 if (y == s->y && x > s->x) {
929 break;
930 }
931 console_clear_xy(s, x, y);
932 }
933 }
934 break;
935 case 2:
936 /* clear entire screen */
937 for (y = 0; y <= s->height; y++) {
938 for (x = 0; x < s->width; x++) {
939 console_clear_xy(s, x, y);
940 }
941 }
f94a950f 942 break;
adb47967 943 }
95d8f9f4 944 break;
e7f0ad58 945 case 'K':
adb47967
TS
946 switch (s->esc_params[0]) {
947 case 0:
f94a950f
MA
948 /* clear to eol */
949 for(x = s->x; x < s->width; x++) {
adb47967 950 console_clear_xy(s, x, s->y);
f94a950f
MA
951 }
952 break;
adb47967
TS
953 case 1:
954 /* clear from beginning of line */
955 for (x = 0; x <= s->x; x++) {
956 console_clear_xy(s, x, s->y);
957 }
958 break;
959 case 2:
960 /* clear entire line */
961 for(x = 0; x < s->width; x++) {
962 console_clear_xy(s, x, s->y);
963 }
f94a950f
MA
964 break;
965 }
adb47967
TS
966 break;
967 case 'm':
f94a950f
MA
968 console_handle_escape(s);
969 break;
adb47967
TS
970 case 'n':
971 /* report cursor position */
972 /* TODO: send ESC[row;colR */
973 break;
974 case 's':
975 /* save cursor position */
976 s->x_saved = s->x;
977 s->y_saved = s->y;
978 break;
979 case 'u':
980 /* restore cursor position */
981 s->x = s->x_saved;
982 s->y = s->y_saved;
983 break;
984 default:
5d28b0e9 985 trace_console_putchar_unhandled(ch);
adb47967
TS
986 break;
987 }
988 break;
e7f0ad58
FB
989 }
990 }
991}
992
993void console_select(unsigned int index)
994{
284d1c6b 995 DisplayChangeListener *dcl;
76ffb0b4 996 QemuConsole *s;
6d6f7c28 997
e7f0ad58
FB
998 if (index >= MAX_CONSOLES)
999 return;
437fe106
GH
1000
1001 trace_console_select(index);
284d1c6b 1002 s = qemu_console_lookup_by_index(index);
e7f0ad58 1003 if (s) {
7d957bd8 1004 DisplayState *ds = s->ds;
bf1bed81 1005
8bd6b06d 1006 if (active_console && active_console->cursor_timer) {
bc72ad67 1007 timer_del(active_console->cursor_timer);
bf1bed81 1008 }
e7f0ad58 1009 active_console = s;
a93a4a22 1010 if (ds->have_gfx) {
284d1c6b
GH
1011 QLIST_FOREACH(dcl, &ds->listeners, next) {
1012 if (dcl->con != NULL) {
1013 continue;
1014 }
1015 if (dcl->ops->dpy_gfx_switch) {
1016 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1017 }
1018 }
321f048d
GH
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 1025 if (s->cursor_timer) {
bc72ad67
AB
1026 timer_mod(s->cursor_timer,
1027 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
bf1bed81 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) {
bc72ad67 1072 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 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:
81c0d5a6 1089 console_scroll(s, -1);
e7f0ad58
FB
1090 break;
1091 case QEMU_KEY_CTRL_DOWN:
81c0d5a6 1092 console_scroll(s, 1);
e7f0ad58
FB
1093 break;
1094 case QEMU_KEY_CTRL_PAGEUP:
81c0d5a6 1095 console_scroll(s, -10);
e7f0ad58
FB
1096 break;
1097 case QEMU_KEY_CTRL_PAGEDOWN:
81c0d5a6 1098 console_scroll(s, 10);
e7f0ad58
FB
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{
aa2beaa1 1171 Error *local_err = NULL;
95be0669 1172 Object *obj;
76ffb0b4 1173 QemuConsole *s;
95219897 1174 int i;
e7f0ad58
FB
1175
1176 if (nb_consoles >= MAX_CONSOLES)
1177 return NULL;
aa2beaa1 1178
95be0669
GH
1179 obj = object_new(TYPE_QEMU_CONSOLE);
1180 s = QEMU_CONSOLE(obj);
aa2beaa1
GH
1181 object_property_add_link(obj, "device", TYPE_DEVICE,
1182 (Object **)&s->device, &local_err);
5643706a
GH
1183 object_property_add_uint32_ptr(obj, "head",
1184 &s->head, &local_err);
aa2beaa1 1185
af3a9031
TS
1186 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1187 (console_type == GRAPHIC_CONSOLE))) {
e7f0ad58 1188 active_console = s;
af3a9031 1189 }
e7f0ad58 1190 s->ds = ds;
af3a9031
TS
1191 s->console_type = console_type;
1192 if (console_type != GRAPHIC_CONSOLE) {
f81bdefb 1193 s->index = nb_consoles;
95219897
PB
1194 consoles[nb_consoles++] = s;
1195 } else {
1196 /* HACK: Put graphical consoles before text consoles. */
1197 for (i = nb_consoles; i > 0; i--) {
af3a9031 1198 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
95219897
PB
1199 break;
1200 consoles[i] = consoles[i - 1];
f81bdefb 1201 consoles[i]->index = i;
95219897 1202 }
f81bdefb 1203 s->index = i;
95219897 1204 consoles[i] = s;
3023f332 1205 nb_consoles++;
95219897
PB
1206 }
1207 return s;
1208}
1209
537a4391
GH
1210static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1211 int linesize, PixelFormat pf, int newflags)
ffe8b821 1212{
ffe8b821 1213 surface->pf = pf;
69c77777
GH
1214
1215 qemu_pixman_image_unref(surface->image);
1216 surface->image = NULL;
69c77777
GH
1217
1218 surface->format = qemu_pixman_get_format(&pf);
1219 assert(surface->format != 0);
1220 surface->image = pixman_image_create_bits(surface->format,
1221 width, height,
1222 NULL, linesize);
1223 assert(surface->image != NULL);
1224
ffe8b821 1225 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
98b50080 1226#ifdef HOST_WORDS_BIGENDIAN
ffe8b821 1227 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
98b50080 1228#endif
98b50080
PB
1229}
1230
da229ef3 1231DisplaySurface *qemu_create_displaysurface(int width, int height)
537a4391
GH
1232{
1233 DisplaySurface *surface = g_new0(DisplaySurface, 1);
537a4391 1234 int linesize = width * 4;
da229ef3
GH
1235
1236 trace_displaysurface_create(surface, width, height);
537a4391
GH
1237 qemu_alloc_display(surface, width, height, linesize,
1238 qemu_default_pixelformat(32), 0);
1239 return surface;
1240}
1241
187cd1d9 1242DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
b1424e03
GH
1243 int linesize, uint8_t *data,
1244 bool byteswap)
98b50080 1245{
69c77777 1246 DisplaySurface *surface = g_new0(DisplaySurface, 1);
98b50080 1247
da229ef3 1248 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
b1424e03
GH
1249 if (byteswap) {
1250 surface->pf = qemu_different_endianness_pixelformat(bpp);
1251 } else {
1252 surface->pf = qemu_default_pixelformat(bpp);
1253 }
69c77777
GH
1254
1255 surface->format = qemu_pixman_get_format(&surface->pf);
1256 assert(surface->format != 0);
1257 surface->image = pixman_image_create_bits(surface->format,
1258 width, height,
1259 (void *)data, linesize);
1260 assert(surface->image != NULL);
1261
98b50080
PB
1262#ifdef HOST_WORDS_BIGENDIAN
1263 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1264#endif
98b50080
PB
1265
1266 return surface;
1267}
1268
d3002b04
GH
1269static DisplaySurface *qemu_create_dummy_surface(void)
1270{
1271 static const char msg[] =
1272 "This VM has no graphic display device.";
1273 DisplaySurface *surface = qemu_create_displaysurface(640, 480);
1274 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1275 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1276 pixman_image_t *glyph;
1277 int len, x, y, i;
1278
1279 len = strlen(msg);
1280 x = (640/FONT_WIDTH - len) / 2;
1281 y = (480/FONT_HEIGHT - 1) / 2;
1282 for (i = 0; i < len; i++) {
1283 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1284 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1285 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1286 qemu_pixman_image_unref(glyph);
1287 }
1288 return surface;
1289}
1290
da229ef3 1291void qemu_free_displaysurface(DisplaySurface *surface)
98b50080 1292{
da229ef3 1293 if (surface == NULL) {
98b50080 1294 return;
187cd1d9 1295 }
da229ef3
GH
1296 trace_displaysurface_free(surface);
1297 qemu_pixman_image_unref(surface->image);
1298 g_free(surface);
98b50080
PB
1299}
1300
5209089f 1301void register_displaychangelistener(DisplayChangeListener *dcl)
7c20b4a3 1302{
d3002b04 1303 static DisplaySurface *dummy;
284d1c6b
GH
1304 QemuConsole *con;
1305
7c20b4a3 1306 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
5209089f
GH
1307 dcl->ds = get_alloc_displaystate();
1308 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1309 gui_setup_refresh(dcl->ds);
284d1c6b
GH
1310 if (dcl->con) {
1311 dcl->con->dcls++;
1312 con = dcl->con;
1313 } else {
1314 con = active_console;
1315 }
d3002b04
GH
1316 if (dcl->ops->dpy_gfx_switch) {
1317 if (con) {
1318 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1319 } else {
1320 if (!dummy) {
1321 dummy = qemu_create_dummy_surface();
1322 }
1323 dcl->ops->dpy_gfx_switch(dcl, dummy);
1324 }
7c20b4a3
GH
1325 }
1326}
1327
0f7b2864
GH
1328void update_displaychangelistener(DisplayChangeListener *dcl,
1329 uint64_t interval)
1330{
1331 DisplayState *ds = dcl->ds;
1332
1333 dcl->update_interval = interval;
1334 if (!ds->refreshing && ds->update_interval > interval) {
bc72ad67 1335 timer_mod(ds->gui_timer, ds->last_update + interval);
0f7b2864
GH
1336 }
1337}
1338
7c20b4a3
GH
1339void unregister_displaychangelistener(DisplayChangeListener *dcl)
1340{
1341 DisplayState *ds = dcl->ds;
1342 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
284d1c6b
GH
1343 if (dcl->con) {
1344 dcl->con->dcls--;
1345 }
7c20b4a3
GH
1346 QLIST_REMOVE(dcl, next);
1347 gui_setup_refresh(ds);
1348}
1349
c78f7137 1350void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1351{
c78f7137 1352 DisplayState *s = con->ds;
284d1c6b 1353 DisplayChangeListener *dcl;
321f048d
GH
1354 int width = surface_width(con->surface);
1355 int height = surface_height(con->surface);
7c20b4a3
GH
1356
1357 x = MAX(x, 0);
1358 y = MAX(y, 0);
1359 x = MIN(x, width);
1360 y = MIN(y, height);
1361 w = MIN(w, width - x);
1362 h = MIN(h, height - y);
1363
81c0d5a6 1364 if (!qemu_console_is_visible(con)) {
321f048d
GH
1365 return;
1366 }
7c20b4a3 1367 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1368 if (con != (dcl->con ? dcl->con : active_console)) {
1369 continue;
1370 }
7c20b4a3 1371 if (dcl->ops->dpy_gfx_update) {
bc2ed970 1372 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
7c20b4a3
GH
1373 }
1374 }
1375}
1376
321f048d
GH
1377void dpy_gfx_replace_surface(QemuConsole *con,
1378 DisplaySurface *surface)
1379{
1380 DisplayState *s = con->ds;
1381 DisplaySurface *old_surface = con->surface;
284d1c6b 1382 DisplayChangeListener *dcl;
321f048d
GH
1383
1384 con->surface = surface;
284d1c6b
GH
1385 QLIST_FOREACH(dcl, &s->listeners, next) {
1386 if (con != (dcl->con ? dcl->con : active_console)) {
1387 continue;
1388 }
1389 if (dcl->ops->dpy_gfx_switch) {
1390 dcl->ops->dpy_gfx_switch(dcl, surface);
1391 }
321f048d 1392 }
da229ef3 1393 qemu_free_displaysurface(old_surface);
7c20b4a3
GH
1394}
1395
1396void dpy_refresh(DisplayState *s)
1397{
284d1c6b
GH
1398 DisplayChangeListener *dcl;
1399
7c20b4a3
GH
1400 QLIST_FOREACH(dcl, &s->listeners, next) {
1401 if (dcl->ops->dpy_refresh) {
bc2ed970 1402 dcl->ops->dpy_refresh(dcl);
7c20b4a3
GH
1403 }
1404 }
1405}
1406
c78f7137
GH
1407void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1408 int dst_x, int dst_y, int w, int h)
7c20b4a3 1409{
c78f7137 1410 DisplayState *s = con->ds;
284d1c6b 1411 DisplayChangeListener *dcl;
321f048d 1412
81c0d5a6 1413 if (!qemu_console_is_visible(con)) {
321f048d
GH
1414 return;
1415 }
7c20b4a3 1416 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1417 if (con != (dcl->con ? dcl->con : active_console)) {
1418 continue;
1419 }
7c20b4a3 1420 if (dcl->ops->dpy_gfx_copy) {
bc2ed970 1421 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
7c20b4a3 1422 } else { /* TODO */
bc2ed970 1423 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
7c20b4a3
GH
1424 }
1425 }
1426}
1427
c78f7137 1428void dpy_text_cursor(QemuConsole *con, int x, int y)
7c20b4a3 1429{
c78f7137 1430 DisplayState *s = con->ds;
284d1c6b 1431 DisplayChangeListener *dcl;
321f048d 1432
81c0d5a6 1433 if (!qemu_console_is_visible(con)) {
321f048d
GH
1434 return;
1435 }
7c20b4a3 1436 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1437 if (con != (dcl->con ? dcl->con : active_console)) {
1438 continue;
1439 }
7c20b4a3 1440 if (dcl->ops->dpy_text_cursor) {
bc2ed970 1441 dcl->ops->dpy_text_cursor(dcl, x, y);
7c20b4a3
GH
1442 }
1443 }
1444}
1445
c78f7137 1446void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
7c20b4a3 1447{
c78f7137 1448 DisplayState *s = con->ds;
284d1c6b 1449 DisplayChangeListener *dcl;
321f048d 1450
81c0d5a6 1451 if (!qemu_console_is_visible(con)) {
321f048d
GH
1452 return;
1453 }
7c20b4a3 1454 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1455 if (con != (dcl->con ? dcl->con : active_console)) {
1456 continue;
1457 }
7c20b4a3 1458 if (dcl->ops->dpy_text_update) {
bc2ed970 1459 dcl->ops->dpy_text_update(dcl, x, y, w, h);
7c20b4a3
GH
1460 }
1461 }
1462}
1463
c78f7137 1464void dpy_text_resize(QemuConsole *con, int w, int h)
7c20b4a3 1465{
c78f7137 1466 DisplayState *s = con->ds;
7c20b4a3 1467 struct DisplayChangeListener *dcl;
321f048d 1468
81c0d5a6 1469 if (!qemu_console_is_visible(con)) {
321f048d
GH
1470 return;
1471 }
7c20b4a3 1472 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1473 if (con != (dcl->con ? dcl->con : active_console)) {
1474 continue;
1475 }
7c20b4a3 1476 if (dcl->ops->dpy_text_resize) {
bc2ed970 1477 dcl->ops->dpy_text_resize(dcl, w, h);
7c20b4a3
GH
1478 }
1479 }
1480}
1481
c78f7137 1482void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
7c20b4a3 1483{
c78f7137 1484 DisplayState *s = con->ds;
284d1c6b 1485 DisplayChangeListener *dcl;
321f048d 1486
81c0d5a6 1487 if (!qemu_console_is_visible(con)) {
321f048d
GH
1488 return;
1489 }
7c20b4a3 1490 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1491 if (con != (dcl->con ? dcl->con : active_console)) {
1492 continue;
1493 }
7c20b4a3 1494 if (dcl->ops->dpy_mouse_set) {
bc2ed970 1495 dcl->ops->dpy_mouse_set(dcl, x, y, on);
7c20b4a3
GH
1496 }
1497 }
1498}
1499
c78f7137 1500void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
7c20b4a3 1501{
c78f7137 1502 DisplayState *s = con->ds;
284d1c6b 1503 DisplayChangeListener *dcl;
321f048d 1504
81c0d5a6 1505 if (!qemu_console_is_visible(con)) {
321f048d
GH
1506 return;
1507 }
7c20b4a3 1508 QLIST_FOREACH(dcl, &s->listeners, next) {
284d1c6b
GH
1509 if (con != (dcl->con ? dcl->con : active_console)) {
1510 continue;
1511 }
7c20b4a3 1512 if (dcl->ops->dpy_cursor_define) {
bc2ed970 1513 dcl->ops->dpy_cursor_define(dcl, cursor);
7c20b4a3
GH
1514 }
1515 }
1516}
1517
c78f7137 1518bool dpy_cursor_define_supported(QemuConsole *con)
7c20b4a3 1519{
c78f7137 1520 DisplayState *s = con->ds;
284d1c6b
GH
1521 DisplayChangeListener *dcl;
1522
7c20b4a3
GH
1523 QLIST_FOREACH(dcl, &s->listeners, next) {
1524 if (dcl->ops->dpy_cursor_define) {
1525 return true;
1526 }
1527 }
1528 return false;
1529}
1530
98b50080
PB
1531/***********************************************************/
1532/* register display */
1533
64840c66
GH
1534/* console.c internal use only */
1535static DisplayState *get_alloc_displaystate(void)
98b50080 1536{
64840c66
GH
1537 if (!display_state) {
1538 display_state = g_new0(DisplayState, 1);
1539 }
1540 return display_state;
98b50080
PB
1541}
1542
64840c66
GH
1543/*
1544 * Called by main(), after creating QemuConsoles
1545 * and before initializing ui (sdl/vnc/...).
1546 */
1547DisplayState *init_displaystate(void)
98b50080 1548{
43f420f8
GH
1549 Error *local_err = NULL;
1550 gchar *name;
64840c66
GH
1551 int i;
1552
98b50080 1553 if (!display_state) {
64840c66 1554 display_state = g_new0(DisplayState, 1);
98b50080 1555 }
64840c66
GH
1556
1557 for (i = 0; i < nb_consoles; i++) {
1558 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1559 consoles[i]->ds == NULL) {
1560 text_console_do_init(consoles[i]->chr, display_state);
1561 }
43f420f8
GH
1562
1563 /* Hook up into the qom tree here (not in new_console()), once
1564 * all QemuConsoles are created and the order / numbering
1565 * doesn't change any more */
1566 name = g_strdup_printf("console[%d]", i);
1567 object_property_add_child(container_get(object_get_root(), "/backend"),
1568 name, OBJECT(consoles[i]), &local_err);
1569 g_free(name);
64840c66
GH
1570 }
1571
98b50080
PB
1572 return display_state;
1573}
1574
5643706a 1575QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
aa2beaa1 1576 const GraphicHwOps *hw_ops,
c78f7137 1577 void *opaque)
95219897 1578{
aa2beaa1 1579 Error *local_err = NULL;
64840c66
GH
1580 int width = 640;
1581 int height = 480;
76ffb0b4 1582 QemuConsole *s;
3023f332 1583 DisplayState *ds;
f0f2f976 1584
64840c66 1585 ds = get_alloc_displaystate();
437fe106 1586 trace_console_gfx_new();
af3a9031 1587 s = new_console(ds, GRAPHIC_CONSOLE);
380cd056 1588 s->hw_ops = hw_ops;
95219897 1589 s->hw = opaque;
aa2beaa1
GH
1590 if (dev) {
1591 object_property_set_link(OBJECT(s), OBJECT(dev),
1592 "device", &local_err);
5643706a
GH
1593 object_property_set_int(OBJECT(s), head,
1594 "head", &local_err);
aa2beaa1 1595 }
3023f332 1596
321f048d 1597 s->surface = qemu_create_displaysurface(width, height);
c78f7137 1598 return s;
e7f0ad58
FB
1599}
1600
284d1c6b
GH
1601QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1602{
1603 if (index >= MAX_CONSOLES) {
1604 return NULL;
1605 }
1606 return consoles[index];
1607}
1608
5643706a 1609QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
14a93649
GH
1610{
1611 Error *local_err = NULL;
1612 Object *obj;
5643706a 1613 uint32_t h;
14a93649
GH
1614 int i;
1615
1616 for (i = 0; i < nb_consoles; i++) {
1617 if (!consoles[i]) {
1618 continue;
1619 }
1620 obj = object_property_get_link(OBJECT(consoles[i]),
1621 "device", &local_err);
5643706a
GH
1622 if (DEVICE(obj) != dev) {
1623 continue;
1624 }
1625 h = object_property_get_int(OBJECT(consoles[i]),
1626 "head", &local_err);
1627 if (h != head) {
1628 continue;
14a93649 1629 }
5643706a 1630 return consoles[i];
14a93649
GH
1631 }
1632 return NULL;
1633}
1634
81c0d5a6 1635bool qemu_console_is_visible(QemuConsole *con)
e7f0ad58 1636{
284d1c6b 1637 return (con == active_console) || (con->dcls > 0);
e7f0ad58
FB
1638}
1639
81c0d5a6 1640bool qemu_console_is_graphic(QemuConsole *con)
c21bbcfa 1641{
81c0d5a6
GH
1642 if (con == NULL) {
1643 con = active_console;
1644 }
1645 return con && (con->console_type == GRAPHIC_CONSOLE);
1646}
1647
1648bool qemu_console_is_fixedsize(QemuConsole *con)
1649{
1650 if (con == NULL) {
1651 con = active_console;
1652 }
1653 return con && (con->console_type != TEXT_CONSOLE);
c21bbcfa
AZ
1654}
1655
d4c85337
GH
1656int qemu_console_get_index(QemuConsole *con)
1657{
1658 if (con == NULL) {
1659 con = active_console;
1660 }
1661 return con ? con->index : -1;
1662}
1663
5643706a
GH
1664uint32_t qemu_console_get_head(QemuConsole *con)
1665{
1666 if (con == NULL) {
1667 con = active_console;
1668 }
1669 return con ? con->head : -1;
1670}
1671
d4c85337
GH
1672int qemu_console_get_width(QemuConsole *con, int fallback)
1673{
1674 if (con == NULL) {
1675 con = active_console;
1676 }
1677 return con ? surface_width(con->surface) : fallback;
1678}
1679
1680int qemu_console_get_height(QemuConsole *con, int fallback)
1681{
1682 if (con == NULL) {
1683 con = active_console;
1684 }
1685 return con ? surface_height(con->surface) : fallback;
1686}
1687
4104833f
PB
1688static void text_console_set_echo(CharDriverState *chr, bool echo)
1689{
76ffb0b4 1690 QemuConsole *s = chr->opaque;
4104833f
PB
1691
1692 s->echo = echo;
1693}
1694
bf1bed81
JK
1695static void text_console_update_cursor(void *opaque)
1696{
76ffb0b4 1697 QemuConsole *s = opaque;
bf1bed81
JK
1698
1699 s->cursor_visible_phase = !s->cursor_visible_phase;
1dbfa005 1700 graphic_hw_invalidate(s);
bc72ad67
AB
1701 timer_mod(s->cursor_timer,
1702 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
bf1bed81
JK
1703}
1704
380cd056
GH
1705static const GraphicHwOps text_console_ops = {
1706 .invalidate = text_console_invalidate,
1707 .text_update = text_console_update,
1708};
1709
44b37b93 1710static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
e7f0ad58 1711{
76ffb0b4 1712 QemuConsole *s;
36671fbd
GH
1713 int g_width = 80 * FONT_WIDTH;
1714 int g_height = 24 * FONT_HEIGHT;
6d6f7c28 1715
491e114a 1716 s = chr->opaque;
6ea314d9 1717
e7f0ad58 1718 chr->chr_write = console_puts;
6fcfafb7 1719
e15d7371
FB
1720 s->out_fifo.buf = s->out_fifo_buf;
1721 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
bc72ad67 1722 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
3023f332 1723 s->ds = ds;
3b46e624 1724
e7f0ad58
FB
1725 s->y_displayed = 0;
1726 s->y_base = 0;
1727 s->total_height = DEFAULT_BACKSCROLL;
1728 s->x = 0;
1729 s->y = 0;
36671fbd 1730 if (!s->surface) {
321f048d 1731 if (active_console && active_console->surface) {
36671fbd
GH
1732 g_width = surface_width(active_console->surface);
1733 g_height = surface_height(active_console->surface);
321f048d 1734 }
36671fbd 1735 s->surface = qemu_create_displaysurface(g_width, g_height);
491e114a 1736 }
6d6f7c28 1737
bf1bed81 1738 s->cursor_timer =
bc72ad67 1739 timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
bf1bed81 1740
380cd056 1741 s->hw_ops = &text_console_ops;
4d3b6f6e
AZ
1742 s->hw = s;
1743
6d6f7c28
PB
1744 /* Set text attribute defaults */
1745 s->t_attrib_default.bold = 0;
1746 s->t_attrib_default.uline = 0;
1747 s->t_attrib_default.blink = 0;
1748 s->t_attrib_default.invers = 0;
1749 s->t_attrib_default.unvisible = 0;
1750 s->t_attrib_default.fgcol = COLOR_WHITE;
1751 s->t_attrib_default.bgcol = COLOR_BLACK;
6d6f7c28
PB
1752 /* set current text attributes to default */
1753 s->t_attrib = s->t_attrib_default;
e7f0ad58
FB
1754 text_console_resize(s);
1755
51bfa4d3
GH
1756 if (chr->label) {
1757 char msg[128];
1758 int len;
1759
735ba588 1760 s->t_attrib.bgcol = COLOR_BLUE;
51bfa4d3
GH
1761 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1762 console_puts(chr, (uint8_t*)msg, len);
735ba588 1763 s->t_attrib = s->t_attrib_default;
51bfa4d3
GH
1764 }
1765
fee204fd 1766 qemu_chr_be_generic_open(chr);
ceecf1d1
AJ
1767 if (chr->init)
1768 chr->init(chr);
e7f0ad58 1769}
c60e08d9 1770
702ec69c 1771static CharDriverState *text_console_init(ChardevVC *vc)
2796dae0
AL
1772{
1773 CharDriverState *chr;
76ffb0b4 1774 QemuConsole *s;
702ec69c
GH
1775 unsigned width = 0;
1776 unsigned height = 0;
2796dae0 1777
7267c094 1778 chr = g_malloc0(sizeof(CharDriverState));
2796dae0 1779
702ec69c
GH
1780 if (vc->has_width) {
1781 width = vc->width;
1782 } else if (vc->has_cols) {
1783 width = vc->cols * FONT_WIDTH;
1784 }
491e114a 1785
702ec69c
GH
1786 if (vc->has_height) {
1787 height = vc->height;
1788 } else if (vc->has_rows) {
1789 height = vc->rows * FONT_HEIGHT;
1790 }
491e114a 1791
437fe106 1792 trace_console_txt_new(width, height);
491e114a
PB
1793 if (width == 0 || height == 0) {
1794 s = new_console(NULL, TEXT_CONSOLE);
1795 } else {
1796 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
36671fbd 1797 s->surface = qemu_create_displaysurface(width, height);
491e114a
PB
1798 }
1799
1800 if (!s) {
5354d083 1801 g_free(chr);
1f51470d 1802 return NULL;
491e114a
PB
1803 }
1804
1805 s->chr = chr;
491e114a 1806 chr->opaque = s;
4104833f 1807 chr->chr_set_echo = text_console_set_echo;
bd5c51ee
MR
1808 /* console/chardev init sometimes completes elsewhere in a 2nd
1809 * stage, so defer OPENED events until they are fully initialized
1810 */
1811 chr->explicit_be_open = true;
64840c66
GH
1812
1813 if (display_state) {
1814 text_console_do_init(chr, display_state);
1815 }
1f51470d 1816 return chr;
2796dae0
AL
1817}
1818
d82831db
AL
1819static VcHandler *vc_handler = text_console_init;
1820
702ec69c 1821CharDriverState *vc_init(ChardevVC *vc)
d82831db 1822{
702ec69c 1823 return vc_handler(vc);
d82831db
AL
1824}
1825
1826void register_vc_handler(VcHandler *handler)
1827{
1828 vc_handler = handler;
1829}
1830
c78f7137 1831void qemu_console_resize(QemuConsole *s, int width, int height)
c60e08d9 1832{
321f048d
GH
1833 DisplaySurface *surface;
1834
1835 assert(s->console_type == GRAPHIC_CONSOLE);
321f048d
GH
1836 surface = qemu_create_displaysurface(width, height);
1837 dpy_gfx_replace_surface(s, surface);
c60e08d9 1838}
38334f76 1839
c78f7137 1840void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
3023f332 1841 int dst_x, int dst_y, int w, int h)
c21bbcfa 1842{
321f048d
GH
1843 assert(con->console_type == GRAPHIC_CONSOLE);
1844 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
38334f76 1845}
7d957bd8 1846
c78f7137
GH
1847DisplaySurface *qemu_console_surface(QemuConsole *console)
1848{
321f048d 1849 return console->surface;
c78f7137
GH
1850}
1851
1852DisplayState *qemu_console_displaystate(QemuConsole *console)
1853{
1854 return console->ds;
1855}
1856
0da2ea1b 1857PixelFormat qemu_different_endianness_pixelformat(int bpp)
7d957bd8
AL
1858{
1859 PixelFormat pf;
1860
1861 memset(&pf, 0x00, sizeof(PixelFormat));
1862
1863 pf.bits_per_pixel = bpp;
feadf1a4 1864 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
7d957bd8
AL
1865 pf.depth = bpp == 32 ? 24 : bpp;
1866
1867 switch (bpp) {
0da2ea1b 1868 case 24:
1869 pf.rmask = 0x000000FF;
1870 pf.gmask = 0x0000FF00;
1871 pf.bmask = 0x00FF0000;
1872 pf.rmax = 255;
1873 pf.gmax = 255;
1874 pf.bmax = 255;
1875 pf.rshift = 0;
1876 pf.gshift = 8;
1877 pf.bshift = 16;
90a1e3c0
AL
1878 pf.rbits = 8;
1879 pf.gbits = 8;
1880 pf.bbits = 8;
7d957bd8 1881 break;
0da2ea1b 1882 case 32:
1883 pf.rmask = 0x0000FF00;
1884 pf.gmask = 0x00FF0000;
1885 pf.bmask = 0xFF000000;
1886 pf.amask = 0x00000000;
1887 pf.amax = 255;
1888 pf.rmax = 255;
1889 pf.gmax = 255;
1890 pf.bmax = 255;
1891 pf.ashift = 0;
1892 pf.rshift = 8;
1893 pf.gshift = 16;
1894 pf.bshift = 24;
90a1e3c0
AL
1895 pf.rbits = 8;
1896 pf.gbits = 8;
1897 pf.bbits = 8;
1898 pf.abits = 8;
0da2ea1b 1899 break;
1900 default:
1901 break;
1902 }
1903 return pf;
1904}
1905
1906PixelFormat qemu_default_pixelformat(int bpp)
1907{
1908 PixelFormat pf;
1909
1910 memset(&pf, 0x00, sizeof(PixelFormat));
1911
1912 pf.bits_per_pixel = bpp;
feadf1a4 1913 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
0da2ea1b 1914 pf.depth = bpp == 32 ? 24 : bpp;
1915
1916 switch (bpp) {
b6278084
GH
1917 case 15:
1918 pf.bits_per_pixel = 16;
b6278084
GH
1919 pf.rmask = 0x00007c00;
1920 pf.gmask = 0x000003E0;
1921 pf.bmask = 0x0000001F;
1922 pf.rmax = 31;
1923 pf.gmax = 31;
1924 pf.bmax = 31;
1925 pf.rshift = 10;
1926 pf.gshift = 5;
1927 pf.bshift = 0;
1928 pf.rbits = 5;
1929 pf.gbits = 5;
1930 pf.bbits = 5;
1931 break;
7d957bd8
AL
1932 case 16:
1933 pf.rmask = 0x0000F800;
1934 pf.gmask = 0x000007E0;
1935 pf.bmask = 0x0000001F;
1936 pf.rmax = 31;
1937 pf.gmax = 63;
1938 pf.bmax = 31;
1939 pf.rshift = 11;
1940 pf.gshift = 5;
1941 pf.bshift = 0;
90a1e3c0
AL
1942 pf.rbits = 5;
1943 pf.gbits = 6;
1944 pf.bbits = 5;
7d957bd8
AL
1945 break;
1946 case 24:
0da2ea1b 1947 pf.rmask = 0x00FF0000;
1948 pf.gmask = 0x0000FF00;
1949 pf.bmask = 0x000000FF;
1950 pf.rmax = 255;
1951 pf.gmax = 255;
1952 pf.bmax = 255;
1953 pf.rshift = 16;
1954 pf.gshift = 8;
1955 pf.bshift = 0;
90a1e3c0
AL
1956 pf.rbits = 8;
1957 pf.gbits = 8;
1958 pf.bbits = 8;
0eba62e0 1959 break;
7d957bd8
AL
1960 case 32:
1961 pf.rmask = 0x00FF0000;
1962 pf.gmask = 0x0000FF00;
1963 pf.bmask = 0x000000FF;
1964 pf.rmax = 255;
1965 pf.gmax = 255;
1966 pf.bmax = 255;
1967 pf.rshift = 16;
1968 pf.gshift = 8;
1969 pf.bshift = 0;
90a1e3c0
AL
1970 pf.rbits = 8;
1971 pf.gbits = 8;
1972 pf.bbits = 8;
7d957bd8
AL
1973 break;
1974 default:
1975 break;
1976 }
1977 return pf;
1978}
01f45d98 1979
702ec69c
GH
1980static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1981 Error **errp)
1982{
1983 int val;
1984
1985 backend->vc = g_new0(ChardevVC, 1);
1986
1987 val = qemu_opt_get_number(opts, "width", 0);
1988 if (val != 0) {
1989 backend->vc->has_width = true;
1990 backend->vc->width = val;
1991 }
1992
1993 val = qemu_opt_get_number(opts, "height", 0);
1994 if (val != 0) {
1995 backend->vc->has_height = true;
1996 backend->vc->height = val;
1997 }
1998
1999 val = qemu_opt_get_number(opts, "cols", 0);
2000 if (val != 0) {
2001 backend->vc->has_cols = true;
2002 backend->vc->cols = val;
2003 }
2004
2005 val = qemu_opt_get_number(opts, "rows", 0);
2006 if (val != 0) {
2007 backend->vc->has_rows = true;
2008 backend->vc->rows = val;
2009 }
2010}
2011
95be0669
GH
2012static const TypeInfo qemu_console_info = {
2013 .name = TYPE_QEMU_CONSOLE,
2014 .parent = TYPE_OBJECT,
2015 .instance_size = sizeof(QemuConsole),
2016 .class_size = sizeof(QemuConsoleClass),
2017};
2018
2019
01f45d98
AL
2020static void register_types(void)
2021{
95be0669 2022 type_register_static(&qemu_console_info);
702ec69c
GH
2023 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
2024 qemu_chr_parse_vc);
01f45d98
AL
2025}
2026
2027type_init(register_types);