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