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