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