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