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