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