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