curses: Fix compiler warnings (Mingw-w64 redefinition of macro KEY_EVENT)
[qemu/ar7.git] / ui / console.c
blob5026d62bc04e33e8d17a30948001ec2381ac63d2
1 /*
2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "trace.h"
27 #include "ui/console.h"
28 #include "hw/qdev-core.h"
29 #include "qemu/timer.h"
30 #include "qmp-commands.h"
31 #include "sysemu/char.h"
32 #include "trace.h"
33 #include "exec/memory.h"
35 #define DEFAULT_BACKSCROLL 512
36 #define CONSOLE_CURSOR_PERIOD 500
38 typedef struct TextAttributes {
39 uint8_t fgcol:4;
40 uint8_t bgcol:4;
41 uint8_t bold:1;
42 uint8_t uline:1;
43 uint8_t blink:1;
44 uint8_t invers:1;
45 uint8_t unvisible:1;
46 } TextAttributes;
48 typedef struct TextCell {
49 uint8_t ch;
50 TextAttributes t_attrib;
51 } TextCell;
53 #define MAX_ESC_PARAMS 3
55 enum TTYState {
56 TTY_STATE_NORM,
57 TTY_STATE_ESC,
58 TTY_STATE_CSI,
61 typedef struct QEMUFIFO {
62 uint8_t *buf;
63 int buf_size;
64 int count, wptr, rptr;
65 } QEMUFIFO;
67 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
69 int l, len;
71 l = f->buf_size - f->count;
72 if (len1 > l)
73 len1 = l;
74 len = len1;
75 while (len > 0) {
76 l = f->buf_size - f->wptr;
77 if (l > len)
78 l = len;
79 memcpy(f->buf + f->wptr, buf, l);
80 f->wptr += l;
81 if (f->wptr >= f->buf_size)
82 f->wptr = 0;
83 buf += l;
84 len -= l;
86 f->count += len1;
87 return len1;
90 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
92 int l, len;
94 if (len1 > f->count)
95 len1 = f->count;
96 len = len1;
97 while (len > 0) {
98 l = f->buf_size - f->rptr;
99 if (l > len)
100 l = len;
101 memcpy(buf, f->buf + f->rptr, l);
102 f->rptr += l;
103 if (f->rptr >= f->buf_size)
104 f->rptr = 0;
105 buf += l;
106 len -= l;
108 f->count -= len1;
109 return len1;
112 typedef enum {
113 GRAPHIC_CONSOLE,
114 TEXT_CONSOLE,
115 TEXT_CONSOLE_FIXED_SIZE
116 } console_type_t;
118 struct QemuConsole {
119 Object parent;
121 int index;
122 console_type_t console_type;
123 DisplayState *ds;
124 DisplaySurface *surface;
125 int dcls;
126 DisplayChangeListener *gl;
127 bool gl_block;
129 /* Graphic console state. */
130 Object *device;
131 uint32_t head;
132 QemuUIInfo ui_info;
133 QEMUTimer *ui_timer;
134 const GraphicHwOps *hw_ops;
135 void *hw;
137 /* Text console state */
138 int width;
139 int height;
140 int total_height;
141 int backscroll_height;
142 int x, y;
143 int x_saved, y_saved;
144 int y_displayed;
145 int y_base;
146 TextAttributes t_attrib_default; /* default text attributes */
147 TextAttributes t_attrib; /* currently active text attributes */
148 TextCell *cells;
149 int text_x[2], text_y[2], cursor_invalidate;
150 int echo;
152 int update_x0;
153 int update_y0;
154 int update_x1;
155 int update_y1;
157 enum TTYState state;
158 int esc_params[MAX_ESC_PARAMS];
159 int nb_esc_params;
161 CharDriverState *chr;
162 /* fifo for key pressed */
163 QEMUFIFO out_fifo;
164 uint8_t out_fifo_buf[16];
165 QEMUTimer *kbd_timer;
168 struct DisplayState {
169 QEMUTimer *gui_timer;
170 uint64_t last_update;
171 uint64_t update_interval;
172 bool refreshing;
173 bool have_gfx;
174 bool have_text;
176 QLIST_HEAD(, DisplayChangeListener) listeners;
179 static DisplayState *display_state;
180 static QemuConsole *active_console;
181 static QemuConsole **consoles;
182 static int nb_consoles = 0;
183 static bool cursor_visible_phase;
184 static QEMUTimer *cursor_timer;
186 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
187 static void dpy_refresh(DisplayState *s);
188 static DisplayState *get_alloc_displaystate(void);
189 static void text_console_update_cursor_timer(void);
190 static void text_console_update_cursor(void *opaque);
192 static void gui_update(void *opaque)
194 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
195 uint64_t dcl_interval;
196 DisplayState *ds = opaque;
197 DisplayChangeListener *dcl;
198 int i;
200 ds->refreshing = true;
201 dpy_refresh(ds);
202 ds->refreshing = false;
204 QLIST_FOREACH(dcl, &ds->listeners, next) {
205 dcl_interval = dcl->update_interval ?
206 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
207 if (interval > dcl_interval) {
208 interval = dcl_interval;
211 if (ds->update_interval != interval) {
212 ds->update_interval = interval;
213 for (i = 0; i < nb_consoles; i++) {
214 if (consoles[i]->hw_ops->update_interval) {
215 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
218 trace_console_refresh(interval);
220 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
221 timer_mod(ds->gui_timer, ds->last_update + interval);
224 static void gui_setup_refresh(DisplayState *ds)
226 DisplayChangeListener *dcl;
227 bool need_timer = false;
228 bool have_gfx = false;
229 bool have_text = false;
231 QLIST_FOREACH(dcl, &ds->listeners, next) {
232 if (dcl->ops->dpy_refresh != NULL) {
233 need_timer = true;
235 if (dcl->ops->dpy_gfx_update != NULL) {
236 have_gfx = true;
238 if (dcl->ops->dpy_text_update != NULL) {
239 have_text = true;
243 if (need_timer && ds->gui_timer == NULL) {
244 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
245 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
247 if (!need_timer && ds->gui_timer != NULL) {
248 timer_del(ds->gui_timer);
249 timer_free(ds->gui_timer);
250 ds->gui_timer = NULL;
253 ds->have_gfx = have_gfx;
254 ds->have_text = have_text;
257 void graphic_hw_update(QemuConsole *con)
259 if (!con) {
260 con = active_console;
262 if (con && con->hw_ops->gfx_update) {
263 con->hw_ops->gfx_update(con->hw);
267 void graphic_hw_gl_block(QemuConsole *con, bool block)
269 assert(con != NULL);
271 con->gl_block = block;
272 if (con->hw_ops->gl_block) {
273 con->hw_ops->gl_block(con->hw, block);
277 void graphic_hw_invalidate(QemuConsole *con)
279 if (!con) {
280 con = active_console;
282 if (con && con->hw_ops->invalidate) {
283 con->hw_ops->invalidate(con->hw);
287 static void ppm_save(const char *filename, DisplaySurface *ds,
288 Error **errp)
290 int width = pixman_image_get_width(ds->image);
291 int height = pixman_image_get_height(ds->image);
292 int fd;
293 FILE *f;
294 int y;
295 int ret;
296 pixman_image_t *linebuf;
298 trace_ppm_save(filename, ds);
299 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
300 if (fd == -1) {
301 error_setg(errp, "failed to open file '%s': %s", filename,
302 strerror(errno));
303 return;
305 f = fdopen(fd, "wb");
306 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
307 if (ret < 0) {
308 linebuf = NULL;
309 goto write_err;
311 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
312 for (y = 0; y < height; y++) {
313 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
314 clearerr(f);
315 ret = fwrite(pixman_image_get_data(linebuf), 1,
316 pixman_image_get_stride(linebuf), f);
317 (void)ret;
318 if (ferror(f)) {
319 goto write_err;
323 out:
324 qemu_pixman_image_unref(linebuf);
325 fclose(f);
326 return;
328 write_err:
329 error_setg(errp, "failed to write to file '%s': %s", filename,
330 strerror(errno));
331 unlink(filename);
332 goto out;
335 void qmp_screendump(const char *filename, Error **errp)
337 QemuConsole *con = qemu_console_lookup_by_index(0);
338 DisplaySurface *surface;
340 if (con == NULL) {
341 error_setg(errp, "There is no QemuConsole I can screendump from.");
342 return;
345 graphic_hw_update(con);
346 surface = qemu_console_surface(con);
347 ppm_save(filename, surface, errp);
350 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
352 if (!con) {
353 con = active_console;
355 if (con && con->hw_ops->text_update) {
356 con->hw_ops->text_update(con->hw, chardata);
360 static void vga_fill_rect(QemuConsole *con,
361 int posx, int posy, int width, int height,
362 pixman_color_t color)
364 DisplaySurface *surface = qemu_console_surface(con);
365 pixman_rectangle16_t rect = {
366 .x = posx, .y = posy, .width = width, .height = height
369 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
370 &color, 1, &rect);
373 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
374 static void vga_bitblt(QemuConsole *con,
375 int xs, int ys, int xd, int yd, int w, int h)
377 DisplaySurface *surface = qemu_console_surface(con);
379 pixman_image_composite(PIXMAN_OP_SRC,
380 surface->image, NULL, surface->image,
381 xs, ys, 0, 0, xd, yd, w, h);
384 /***********************************************************/
385 /* basic char display */
387 #define FONT_HEIGHT 16
388 #define FONT_WIDTH 8
390 #include "vgafont.h"
392 #define QEMU_RGB(r, g, b) \
393 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
395 static const pixman_color_t color_table_rgb[2][8] = {
396 { /* dark */
397 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
398 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
399 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
400 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
401 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
402 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
403 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
404 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
406 { /* bright */
407 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
408 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
409 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
410 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
411 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
412 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
413 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
414 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
418 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
419 TextAttributes *t_attrib)
421 static pixman_image_t *glyphs[256];
422 DisplaySurface *surface = qemu_console_surface(s);
423 pixman_color_t fgcol, bgcol;
425 if (t_attrib->invers) {
426 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
427 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
428 } else {
429 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
430 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
433 if (!glyphs[ch]) {
434 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
436 qemu_pixman_glyph_render(glyphs[ch], surface->image,
437 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
440 static void text_console_resize(QemuConsole *s)
442 TextCell *cells, *c, *c1;
443 int w1, x, y, last_width;
445 last_width = s->width;
446 s->width = surface_width(s->surface) / FONT_WIDTH;
447 s->height = surface_height(s->surface) / FONT_HEIGHT;
449 w1 = last_width;
450 if (s->width < w1)
451 w1 = s->width;
453 cells = g_new(TextCell, s->width * s->total_height);
454 for(y = 0; y < s->total_height; y++) {
455 c = &cells[y * s->width];
456 if (w1 > 0) {
457 c1 = &s->cells[y * last_width];
458 for(x = 0; x < w1; x++) {
459 *c++ = *c1++;
462 for(x = w1; x < s->width; x++) {
463 c->ch = ' ';
464 c->t_attrib = s->t_attrib_default;
465 c++;
468 g_free(s->cells);
469 s->cells = cells;
472 static inline void text_update_xy(QemuConsole *s, int x, int y)
474 s->text_x[0] = MIN(s->text_x[0], x);
475 s->text_x[1] = MAX(s->text_x[1], x);
476 s->text_y[0] = MIN(s->text_y[0], y);
477 s->text_y[1] = MAX(s->text_y[1], y);
480 static void invalidate_xy(QemuConsole *s, int x, int y)
482 if (!qemu_console_is_visible(s)) {
483 return;
485 if (s->update_x0 > x * FONT_WIDTH)
486 s->update_x0 = x * FONT_WIDTH;
487 if (s->update_y0 > y * FONT_HEIGHT)
488 s->update_y0 = y * FONT_HEIGHT;
489 if (s->update_x1 < (x + 1) * FONT_WIDTH)
490 s->update_x1 = (x + 1) * FONT_WIDTH;
491 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
492 s->update_y1 = (y + 1) * FONT_HEIGHT;
495 static void update_xy(QemuConsole *s, int x, int y)
497 TextCell *c;
498 int y1, y2;
500 if (s->ds->have_text) {
501 text_update_xy(s, x, y);
504 y1 = (s->y_base + y) % s->total_height;
505 y2 = y1 - s->y_displayed;
506 if (y2 < 0) {
507 y2 += s->total_height;
509 if (y2 < s->height) {
510 c = &s->cells[y1 * s->width + x];
511 vga_putcharxy(s, x, y2, c->ch,
512 &(c->t_attrib));
513 invalidate_xy(s, x, y2);
517 static void console_show_cursor(QemuConsole *s, int show)
519 TextCell *c;
520 int y, y1;
521 int x = s->x;
523 if (s->ds->have_text) {
524 s->cursor_invalidate = 1;
527 if (x >= s->width) {
528 x = s->width - 1;
530 y1 = (s->y_base + s->y) % s->total_height;
531 y = y1 - s->y_displayed;
532 if (y < 0) {
533 y += s->total_height;
535 if (y < s->height) {
536 c = &s->cells[y1 * s->width + x];
537 if (show && cursor_visible_phase) {
538 TextAttributes t_attrib = s->t_attrib_default;
539 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
540 vga_putcharxy(s, x, y, c->ch, &t_attrib);
541 } else {
542 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
544 invalidate_xy(s, x, y);
548 static void console_refresh(QemuConsole *s)
550 DisplaySurface *surface = qemu_console_surface(s);
551 TextCell *c;
552 int x, y, y1;
554 if (s->ds->have_text) {
555 s->text_x[0] = 0;
556 s->text_y[0] = 0;
557 s->text_x[1] = s->width - 1;
558 s->text_y[1] = s->height - 1;
559 s->cursor_invalidate = 1;
562 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
563 color_table_rgb[0][QEMU_COLOR_BLACK]);
564 y1 = s->y_displayed;
565 for (y = 0; y < s->height; y++) {
566 c = s->cells + y1 * s->width;
567 for (x = 0; x < s->width; x++) {
568 vga_putcharxy(s, x, y, c->ch,
569 &(c->t_attrib));
570 c++;
572 if (++y1 == s->total_height) {
573 y1 = 0;
576 console_show_cursor(s, 1);
577 dpy_gfx_update(s, 0, 0,
578 surface_width(surface), surface_height(surface));
581 static void console_scroll(QemuConsole *s, int ydelta)
583 int i, y1;
585 if (ydelta > 0) {
586 for(i = 0; i < ydelta; i++) {
587 if (s->y_displayed == s->y_base)
588 break;
589 if (++s->y_displayed == s->total_height)
590 s->y_displayed = 0;
592 } else {
593 ydelta = -ydelta;
594 i = s->backscroll_height;
595 if (i > s->total_height - s->height)
596 i = s->total_height - s->height;
597 y1 = s->y_base - i;
598 if (y1 < 0)
599 y1 += s->total_height;
600 for(i = 0; i < ydelta; i++) {
601 if (s->y_displayed == y1)
602 break;
603 if (--s->y_displayed < 0)
604 s->y_displayed = s->total_height - 1;
607 console_refresh(s);
610 static void console_put_lf(QemuConsole *s)
612 TextCell *c;
613 int x, y1;
615 s->y++;
616 if (s->y >= s->height) {
617 s->y = s->height - 1;
619 if (s->y_displayed == s->y_base) {
620 if (++s->y_displayed == s->total_height)
621 s->y_displayed = 0;
623 if (++s->y_base == s->total_height)
624 s->y_base = 0;
625 if (s->backscroll_height < s->total_height)
626 s->backscroll_height++;
627 y1 = (s->y_base + s->height - 1) % s->total_height;
628 c = &s->cells[y1 * s->width];
629 for(x = 0; x < s->width; x++) {
630 c->ch = ' ';
631 c->t_attrib = s->t_attrib_default;
632 c++;
634 if (s->y_displayed == s->y_base) {
635 if (s->ds->have_text) {
636 s->text_x[0] = 0;
637 s->text_y[0] = 0;
638 s->text_x[1] = s->width - 1;
639 s->text_y[1] = s->height - 1;
642 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
643 s->width * FONT_WIDTH,
644 (s->height - 1) * FONT_HEIGHT);
645 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
646 s->width * FONT_WIDTH, FONT_HEIGHT,
647 color_table_rgb[0][s->t_attrib_default.bgcol]);
648 s->update_x0 = 0;
649 s->update_y0 = 0;
650 s->update_x1 = s->width * FONT_WIDTH;
651 s->update_y1 = s->height * FONT_HEIGHT;
656 /* Set console attributes depending on the current escape codes.
657 * NOTE: I know this code is not very efficient (checking every color for it
658 * self) but it is more readable and better maintainable.
660 static void console_handle_escape(QemuConsole *s)
662 int i;
664 for (i=0; i<s->nb_esc_params; i++) {
665 switch (s->esc_params[i]) {
666 case 0: /* reset all console attributes to default */
667 s->t_attrib = s->t_attrib_default;
668 break;
669 case 1:
670 s->t_attrib.bold = 1;
671 break;
672 case 4:
673 s->t_attrib.uline = 1;
674 break;
675 case 5:
676 s->t_attrib.blink = 1;
677 break;
678 case 7:
679 s->t_attrib.invers = 1;
680 break;
681 case 8:
682 s->t_attrib.unvisible = 1;
683 break;
684 case 22:
685 s->t_attrib.bold = 0;
686 break;
687 case 24:
688 s->t_attrib.uline = 0;
689 break;
690 case 25:
691 s->t_attrib.blink = 0;
692 break;
693 case 27:
694 s->t_attrib.invers = 0;
695 break;
696 case 28:
697 s->t_attrib.unvisible = 0;
698 break;
699 /* set foreground color */
700 case 30:
701 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
702 break;
703 case 31:
704 s->t_attrib.fgcol = QEMU_COLOR_RED;
705 break;
706 case 32:
707 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
708 break;
709 case 33:
710 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
711 break;
712 case 34:
713 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
714 break;
715 case 35:
716 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
717 break;
718 case 36:
719 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
720 break;
721 case 37:
722 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
723 break;
724 /* set background color */
725 case 40:
726 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
727 break;
728 case 41:
729 s->t_attrib.bgcol = QEMU_COLOR_RED;
730 break;
731 case 42:
732 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
733 break;
734 case 43:
735 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
736 break;
737 case 44:
738 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
739 break;
740 case 45:
741 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
742 break;
743 case 46:
744 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
745 break;
746 case 47:
747 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
748 break;
753 static void console_clear_xy(QemuConsole *s, int x, int y)
755 int y1 = (s->y_base + y) % s->total_height;
756 TextCell *c = &s->cells[y1 * s->width + x];
757 c->ch = ' ';
758 c->t_attrib = s->t_attrib_default;
759 update_xy(s, x, y);
762 static void console_put_one(QemuConsole *s, int ch)
764 TextCell *c;
765 int y1;
766 if (s->x >= s->width) {
767 /* line wrap */
768 s->x = 0;
769 console_put_lf(s);
771 y1 = (s->y_base + s->y) % s->total_height;
772 c = &s->cells[y1 * s->width + s->x];
773 c->ch = ch;
774 c->t_attrib = s->t_attrib;
775 update_xy(s, s->x, s->y);
776 s->x++;
779 static void console_respond_str(QemuConsole *s, const char *buf)
781 while (*buf) {
782 console_put_one(s, *buf);
783 buf++;
787 /* set cursor, checking bounds */
788 static void set_cursor(QemuConsole *s, int x, int y)
790 if (x < 0) {
791 x = 0;
793 if (y < 0) {
794 y = 0;
796 if (y >= s->height) {
797 y = s->height - 1;
799 if (x >= s->width) {
800 x = s->width - 1;
803 s->x = x;
804 s->y = y;
807 static void console_putchar(QemuConsole *s, int ch)
809 int i;
810 int x, y;
811 char response[40];
813 switch(s->state) {
814 case TTY_STATE_NORM:
815 switch(ch) {
816 case '\r': /* carriage return */
817 s->x = 0;
818 break;
819 case '\n': /* newline */
820 console_put_lf(s);
821 break;
822 case '\b': /* backspace */
823 if (s->x > 0)
824 s->x--;
825 break;
826 case '\t': /* tabspace */
827 if (s->x + (8 - (s->x % 8)) > s->width) {
828 s->x = 0;
829 console_put_lf(s);
830 } else {
831 s->x = s->x + (8 - (s->x % 8));
833 break;
834 case '\a': /* alert aka. bell */
835 /* TODO: has to be implemented */
836 break;
837 case 14:
838 /* SI (shift in), character set 0 (ignored) */
839 break;
840 case 15:
841 /* SO (shift out), character set 1 (ignored) */
842 break;
843 case 27: /* esc (introducing an escape sequence) */
844 s->state = TTY_STATE_ESC;
845 break;
846 default:
847 console_put_one(s, ch);
848 break;
850 break;
851 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
852 if (ch == '[') {
853 for(i=0;i<MAX_ESC_PARAMS;i++)
854 s->esc_params[i] = 0;
855 s->nb_esc_params = 0;
856 s->state = TTY_STATE_CSI;
857 } else {
858 s->state = TTY_STATE_NORM;
860 break;
861 case TTY_STATE_CSI: /* handle escape sequence parameters */
862 if (ch >= '0' && ch <= '9') {
863 if (s->nb_esc_params < MAX_ESC_PARAMS) {
864 int *param = &s->esc_params[s->nb_esc_params];
865 int digit = (ch - '0');
867 *param = (*param <= (INT_MAX - digit) / 10) ?
868 *param * 10 + digit : INT_MAX;
870 } else {
871 if (s->nb_esc_params < MAX_ESC_PARAMS)
872 s->nb_esc_params++;
873 if (ch == ';')
874 break;
875 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
876 ch, s->nb_esc_params);
877 s->state = TTY_STATE_NORM;
878 switch(ch) {
879 case 'A':
880 /* move cursor up */
881 if (s->esc_params[0] == 0) {
882 s->esc_params[0] = 1;
884 set_cursor(s, s->x, s->y - s->esc_params[0]);
885 break;
886 case 'B':
887 /* move cursor down */
888 if (s->esc_params[0] == 0) {
889 s->esc_params[0] = 1;
891 set_cursor(s, s->x, s->y + s->esc_params[0]);
892 break;
893 case 'C':
894 /* move cursor right */
895 if (s->esc_params[0] == 0) {
896 s->esc_params[0] = 1;
898 set_cursor(s, s->x + s->esc_params[0], s->y);
899 break;
900 case 'D':
901 /* move cursor left */
902 if (s->esc_params[0] == 0) {
903 s->esc_params[0] = 1;
905 set_cursor(s, s->x - s->esc_params[0], s->y);
906 break;
907 case 'G':
908 /* move cursor to column */
909 set_cursor(s, s->esc_params[0] - 1, s->y);
910 break;
911 case 'f':
912 case 'H':
913 /* move cursor to row, column */
914 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
915 break;
916 case 'J':
917 switch (s->esc_params[0]) {
918 case 0:
919 /* clear to end of screen */
920 for (y = s->y; y < s->height; y++) {
921 for (x = 0; x < s->width; x++) {
922 if (y == s->y && x < s->x) {
923 continue;
925 console_clear_xy(s, x, y);
928 break;
929 case 1:
930 /* clear from beginning of screen */
931 for (y = 0; y <= s->y; y++) {
932 for (x = 0; x < s->width; x++) {
933 if (y == s->y && x > s->x) {
934 break;
936 console_clear_xy(s, x, y);
939 break;
940 case 2:
941 /* clear entire screen */
942 for (y = 0; y <= s->height; y++) {
943 for (x = 0; x < s->width; x++) {
944 console_clear_xy(s, x, y);
947 break;
949 break;
950 case 'K':
951 switch (s->esc_params[0]) {
952 case 0:
953 /* clear to eol */
954 for (x = s->x; x < s->width; x++) {
955 console_clear_xy(s, x, s->y);
957 break;
958 case 1:
959 /* clear from beginning of line */
960 for (x = 0; x <= s->x; x++) {
961 console_clear_xy(s, x, s->y);
963 break;
964 case 2:
965 /* clear entire line */
966 for(x = 0; x < s->width; x++) {
967 console_clear_xy(s, x, s->y);
969 break;
971 break;
972 case 'm':
973 console_handle_escape(s);
974 break;
975 case 'n':
976 switch (s->esc_params[0]) {
977 case 5:
978 /* report console status (always succeed)*/
979 console_respond_str(s, "\033[0n");
980 break;
981 case 6:
982 /* report cursor position */
983 sprintf(response, "\033[%d;%dR",
984 (s->y_base + s->y) % s->total_height + 1,
985 s->x + 1);
986 console_respond_str(s, response);
987 break;
989 break;
990 case 's':
991 /* save cursor position */
992 s->x_saved = s->x;
993 s->y_saved = s->y;
994 break;
995 case 'u':
996 /* restore cursor position */
997 s->x = s->x_saved;
998 s->y = s->y_saved;
999 break;
1000 default:
1001 trace_console_putchar_unhandled(ch);
1002 break;
1004 break;
1009 void console_select(unsigned int index)
1011 DisplayChangeListener *dcl;
1012 QemuConsole *s;
1014 trace_console_select(index);
1015 s = qemu_console_lookup_by_index(index);
1016 if (s) {
1017 DisplayState *ds = s->ds;
1019 active_console = s;
1020 if (ds->have_gfx) {
1021 QLIST_FOREACH(dcl, &ds->listeners, next) {
1022 if (dcl->con != NULL) {
1023 continue;
1025 if (dcl->ops->dpy_gfx_switch) {
1026 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1029 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1030 surface_height(s->surface));
1032 if (ds->have_text) {
1033 dpy_text_resize(s, s->width, s->height);
1035 text_console_update_cursor(NULL);
1039 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1041 QemuConsole *s = chr->opaque;
1042 int i;
1044 s->update_x0 = s->width * FONT_WIDTH;
1045 s->update_y0 = s->height * FONT_HEIGHT;
1046 s->update_x1 = 0;
1047 s->update_y1 = 0;
1048 console_show_cursor(s, 0);
1049 for(i = 0; i < len; i++) {
1050 console_putchar(s, buf[i]);
1052 console_show_cursor(s, 1);
1053 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1054 dpy_gfx_update(s, s->update_x0, s->update_y0,
1055 s->update_x1 - s->update_x0,
1056 s->update_y1 - s->update_y0);
1058 return len;
1061 static void kbd_send_chars(void *opaque)
1063 QemuConsole *s = opaque;
1064 int len;
1065 uint8_t buf[16];
1067 len = qemu_chr_be_can_write(s->chr);
1068 if (len > s->out_fifo.count)
1069 len = s->out_fifo.count;
1070 if (len > 0) {
1071 if (len > sizeof(buf))
1072 len = sizeof(buf);
1073 qemu_fifo_read(&s->out_fifo, buf, len);
1074 qemu_chr_be_write(s->chr, buf, len);
1076 /* characters are pending: we send them a bit later (XXX:
1077 horrible, should change char device API) */
1078 if (s->out_fifo.count > 0) {
1079 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1083 /* called when an ascii key is pressed */
1084 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1086 uint8_t buf[16], *q;
1087 int c;
1089 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1090 return;
1092 switch(keysym) {
1093 case QEMU_KEY_CTRL_UP:
1094 console_scroll(s, -1);
1095 break;
1096 case QEMU_KEY_CTRL_DOWN:
1097 console_scroll(s, 1);
1098 break;
1099 case QEMU_KEY_CTRL_PAGEUP:
1100 console_scroll(s, -10);
1101 break;
1102 case QEMU_KEY_CTRL_PAGEDOWN:
1103 console_scroll(s, 10);
1104 break;
1105 default:
1106 /* convert the QEMU keysym to VT100 key string */
1107 q = buf;
1108 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1109 *q++ = '\033';
1110 *q++ = '[';
1111 c = keysym - 0xe100;
1112 if (c >= 10)
1113 *q++ = '0' + (c / 10);
1114 *q++ = '0' + (c % 10);
1115 *q++ = '~';
1116 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1117 *q++ = '\033';
1118 *q++ = '[';
1119 *q++ = keysym & 0xff;
1120 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1121 console_puts(s->chr, (const uint8_t *) "\r", 1);
1122 *q++ = '\n';
1123 } else {
1124 *q++ = keysym;
1126 if (s->echo) {
1127 console_puts(s->chr, buf, q - buf);
1129 if (s->chr->chr_read) {
1130 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1131 kbd_send_chars(s);
1133 break;
1137 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1138 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1139 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1140 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1141 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1142 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1143 [Q_KEY_CODE_END] = QEMU_KEY_END,
1144 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1145 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1146 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1147 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1150 bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1152 int keysym;
1154 keysym = qcode_to_keysym[qcode];
1155 if (keysym == 0) {
1156 return false;
1158 kbd_put_keysym_console(s, keysym);
1159 return true;
1162 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1164 int i;
1166 for (i = 0; i < len && str[i]; i++) {
1167 kbd_put_keysym_console(s, str[i]);
1171 void kbd_put_keysym(int keysym)
1173 kbd_put_keysym_console(active_console, keysym);
1176 static void text_console_invalidate(void *opaque)
1178 QemuConsole *s = (QemuConsole *) opaque;
1180 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1181 text_console_resize(s);
1183 console_refresh(s);
1186 static void text_console_update(void *opaque, console_ch_t *chardata)
1188 QemuConsole *s = (QemuConsole *) opaque;
1189 int i, j, src;
1191 if (s->text_x[0] <= s->text_x[1]) {
1192 src = (s->y_base + s->text_y[0]) * s->width;
1193 chardata += s->text_y[0] * s->width;
1194 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1195 for (j = 0; j < s->width; j++, src++) {
1196 console_write_ch(chardata ++,
1197 ATTR2CHTYPE(s->cells[src].ch,
1198 s->cells[src].t_attrib.fgcol,
1199 s->cells[src].t_attrib.bgcol,
1200 s->cells[src].t_attrib.bold));
1202 dpy_text_update(s, s->text_x[0], s->text_y[0],
1203 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1204 s->text_x[0] = s->width;
1205 s->text_y[0] = s->height;
1206 s->text_x[1] = 0;
1207 s->text_y[1] = 0;
1209 if (s->cursor_invalidate) {
1210 dpy_text_cursor(s, s->x, s->y);
1211 s->cursor_invalidate = 0;
1215 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1216 uint32_t head)
1218 Object *obj;
1219 QemuConsole *s;
1220 int i;
1222 obj = object_new(TYPE_QEMU_CONSOLE);
1223 s = QEMU_CONSOLE(obj);
1224 s->head = head;
1225 object_property_add_link(obj, "device", TYPE_DEVICE,
1226 (Object **)&s->device,
1227 object_property_allow_set_link,
1228 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1229 &error_abort);
1230 object_property_add_uint32_ptr(obj, "head",
1231 &s->head, &error_abort);
1233 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1234 (console_type == GRAPHIC_CONSOLE))) {
1235 active_console = s;
1237 s->ds = ds;
1238 s->console_type = console_type;
1240 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1241 if (console_type != GRAPHIC_CONSOLE) {
1242 s->index = nb_consoles;
1243 consoles[nb_consoles++] = s;
1244 } else {
1245 /* HACK: Put graphical consoles before text consoles. */
1246 for (i = nb_consoles; i > 0; i--) {
1247 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1248 break;
1249 consoles[i] = consoles[i - 1];
1250 consoles[i]->index = i;
1252 s->index = i;
1253 consoles[i] = s;
1254 nb_consoles++;
1256 return s;
1259 static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1261 qemu_pixman_image_unref(surface->image);
1262 surface->image = NULL;
1264 surface->format = PIXMAN_x8r8g8b8;
1265 surface->image = pixman_image_create_bits(surface->format,
1266 width, height,
1267 NULL, width * 4);
1268 assert(surface->image != NULL);
1270 surface->flags = QEMU_ALLOCATED_FLAG;
1273 DisplaySurface *qemu_create_displaysurface(int width, int height)
1275 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1277 trace_displaysurface_create(surface, width, height);
1278 qemu_alloc_display(surface, width, height);
1279 return surface;
1282 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1283 pixman_format_code_t format,
1284 int linesize, uint8_t *data)
1286 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1288 trace_displaysurface_create_from(surface, width, height, format);
1289 surface->format = format;
1290 surface->image = pixman_image_create_bits(surface->format,
1291 width, height,
1292 (void *)data, linesize);
1293 assert(surface->image != NULL);
1295 return surface;
1298 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1300 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1302 trace_displaysurface_create_pixman(surface);
1303 surface->format = pixman_image_get_format(image);
1304 surface->image = pixman_image_ref(image);
1306 return surface;
1309 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1310 void *unused)
1312 void *data = pixman_image_get_data(image);
1313 uint32_t size = pixman_image_get_stride(image) *
1314 pixman_image_get_height(image);
1315 cpu_physical_memory_unmap(data, size, 0, 0);
1318 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1319 pixman_format_code_t format,
1320 int linesize, uint64_t addr)
1322 DisplaySurface *surface;
1323 hwaddr size;
1324 void *data;
1326 if (linesize == 0) {
1327 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1330 size = (hwaddr)linesize * height;
1331 data = cpu_physical_memory_map(addr, &size, 0);
1332 if (size != (hwaddr)linesize * height) {
1333 cpu_physical_memory_unmap(data, size, 0, 0);
1334 return NULL;
1337 surface = qemu_create_displaysurface_from
1338 (width, height, format, linesize, data);
1339 pixman_image_set_destroy_function
1340 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1342 return surface;
1345 static DisplaySurface *qemu_create_message_surface(int w, int h,
1346 const char *msg)
1348 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1349 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1350 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1351 pixman_image_t *glyph;
1352 int len, x, y, i;
1354 len = strlen(msg);
1355 x = (w / FONT_WIDTH - len) / 2;
1356 y = (h / FONT_HEIGHT - 1) / 2;
1357 for (i = 0; i < len; i++) {
1358 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1359 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1360 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1361 qemu_pixman_image_unref(glyph);
1363 return surface;
1366 void qemu_free_displaysurface(DisplaySurface *surface)
1368 if (surface == NULL) {
1369 return;
1371 trace_displaysurface_free(surface);
1372 qemu_pixman_image_unref(surface->image);
1373 g_free(surface);
1376 bool console_has_gl(QemuConsole *con)
1378 return con->gl != NULL;
1381 void register_displaychangelistener(DisplayChangeListener *dcl)
1383 static const char nodev[] =
1384 "This VM has no graphic display device.";
1385 static DisplaySurface *dummy;
1386 QemuConsole *con;
1388 if (dcl->ops->dpy_gl_ctx_create) {
1389 /* display has opengl support */
1390 assert(dcl->con);
1391 if (dcl->con->gl) {
1392 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1393 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1394 exit(1);
1396 dcl->con->gl = dcl;
1399 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1400 dcl->ds = get_alloc_displaystate();
1401 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1402 gui_setup_refresh(dcl->ds);
1403 if (dcl->con) {
1404 dcl->con->dcls++;
1405 con = dcl->con;
1406 } else {
1407 con = active_console;
1409 if (dcl->ops->dpy_gfx_switch) {
1410 if (con) {
1411 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1412 } else {
1413 if (!dummy) {
1414 dummy = qemu_create_message_surface(640, 480, nodev);
1416 dcl->ops->dpy_gfx_switch(dcl, dummy);
1419 text_console_update_cursor(NULL);
1422 void update_displaychangelistener(DisplayChangeListener *dcl,
1423 uint64_t interval)
1425 DisplayState *ds = dcl->ds;
1427 dcl->update_interval = interval;
1428 if (!ds->refreshing && ds->update_interval > interval) {
1429 timer_mod(ds->gui_timer, ds->last_update + interval);
1433 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1435 DisplayState *ds = dcl->ds;
1436 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1437 if (dcl->con) {
1438 dcl->con->dcls--;
1440 QLIST_REMOVE(dcl, next);
1441 gui_setup_refresh(ds);
1444 static void dpy_set_ui_info_timer(void *opaque)
1446 QemuConsole *con = opaque;
1448 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1451 bool dpy_ui_info_supported(QemuConsole *con)
1453 return con->hw_ops->ui_info != NULL;
1456 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1458 assert(con != NULL);
1460 if (!dpy_ui_info_supported(con)) {
1461 return -1;
1463 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1464 /* nothing changed -- ignore */
1465 return 0;
1469 * Typically we get a flood of these as the user resizes the window.
1470 * Wait until the dust has settled (one second without updates), then
1471 * go notify the guest.
1473 con->ui_info = *info;
1474 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1475 return 0;
1478 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1480 DisplayState *s = con->ds;
1481 DisplayChangeListener *dcl;
1482 int width = w;
1483 int height = h;
1485 if (con->surface) {
1486 width = surface_width(con->surface);
1487 height = surface_height(con->surface);
1489 x = MAX(x, 0);
1490 y = MAX(y, 0);
1491 x = MIN(x, width);
1492 y = MIN(y, height);
1493 w = MIN(w, width - x);
1494 h = MIN(h, height - y);
1496 if (!qemu_console_is_visible(con)) {
1497 return;
1499 QLIST_FOREACH(dcl, &s->listeners, next) {
1500 if (con != (dcl->con ? dcl->con : active_console)) {
1501 continue;
1503 if (dcl->ops->dpy_gfx_update) {
1504 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1509 void dpy_gfx_replace_surface(QemuConsole *con,
1510 DisplaySurface *surface)
1512 DisplayState *s = con->ds;
1513 DisplaySurface *old_surface = con->surface;
1514 DisplayChangeListener *dcl;
1516 con->surface = surface;
1517 QLIST_FOREACH(dcl, &s->listeners, next) {
1518 if (con != (dcl->con ? dcl->con : active_console)) {
1519 continue;
1521 if (dcl->ops->dpy_gfx_switch) {
1522 dcl->ops->dpy_gfx_switch(dcl, surface);
1525 qemu_free_displaysurface(old_surface);
1528 bool dpy_gfx_check_format(QemuConsole *con,
1529 pixman_format_code_t format)
1531 DisplayChangeListener *dcl;
1532 DisplayState *s = con->ds;
1534 QLIST_FOREACH(dcl, &s->listeners, next) {
1535 if (dcl->con && dcl->con != con) {
1536 /* dcl bound to another console -> skip */
1537 continue;
1539 if (dcl->ops->dpy_gfx_check_format) {
1540 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1541 return false;
1543 } else {
1544 /* default is to whitelist native 32 bpp only */
1545 if (format != qemu_default_pixman_format(32, true)) {
1546 return false;
1550 return true;
1553 static void dpy_refresh(DisplayState *s)
1555 DisplayChangeListener *dcl;
1557 QLIST_FOREACH(dcl, &s->listeners, next) {
1558 if (dcl->ops->dpy_refresh) {
1559 dcl->ops->dpy_refresh(dcl);
1564 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1565 int dst_x, int dst_y, int w, int h)
1567 DisplayState *s = con->ds;
1568 DisplayChangeListener *dcl;
1570 if (!qemu_console_is_visible(con)) {
1571 return;
1573 QLIST_FOREACH(dcl, &s->listeners, next) {
1574 if (con != (dcl->con ? dcl->con : active_console)) {
1575 continue;
1577 if (dcl->ops->dpy_gfx_copy) {
1578 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1579 } else { /* TODO */
1580 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1585 void dpy_text_cursor(QemuConsole *con, int x, int y)
1587 DisplayState *s = con->ds;
1588 DisplayChangeListener *dcl;
1590 if (!qemu_console_is_visible(con)) {
1591 return;
1593 QLIST_FOREACH(dcl, &s->listeners, next) {
1594 if (con != (dcl->con ? dcl->con : active_console)) {
1595 continue;
1597 if (dcl->ops->dpy_text_cursor) {
1598 dcl->ops->dpy_text_cursor(dcl, x, y);
1603 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1605 DisplayState *s = con->ds;
1606 DisplayChangeListener *dcl;
1608 if (!qemu_console_is_visible(con)) {
1609 return;
1611 QLIST_FOREACH(dcl, &s->listeners, next) {
1612 if (con != (dcl->con ? dcl->con : active_console)) {
1613 continue;
1615 if (dcl->ops->dpy_text_update) {
1616 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1621 void dpy_text_resize(QemuConsole *con, int w, int h)
1623 DisplayState *s = con->ds;
1624 DisplayChangeListener *dcl;
1626 if (!qemu_console_is_visible(con)) {
1627 return;
1629 QLIST_FOREACH(dcl, &s->listeners, next) {
1630 if (con != (dcl->con ? dcl->con : active_console)) {
1631 continue;
1633 if (dcl->ops->dpy_text_resize) {
1634 dcl->ops->dpy_text_resize(dcl, w, h);
1639 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1641 DisplayState *s = con->ds;
1642 DisplayChangeListener *dcl;
1644 if (!qemu_console_is_visible(con)) {
1645 return;
1647 QLIST_FOREACH(dcl, &s->listeners, next) {
1648 if (con != (dcl->con ? dcl->con : active_console)) {
1649 continue;
1651 if (dcl->ops->dpy_mouse_set) {
1652 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1657 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1659 DisplayState *s = con->ds;
1660 DisplayChangeListener *dcl;
1662 if (!qemu_console_is_visible(con)) {
1663 return;
1665 QLIST_FOREACH(dcl, &s->listeners, next) {
1666 if (con != (dcl->con ? dcl->con : active_console)) {
1667 continue;
1669 if (dcl->ops->dpy_cursor_define) {
1670 dcl->ops->dpy_cursor_define(dcl, cursor);
1675 bool dpy_cursor_define_supported(QemuConsole *con)
1677 DisplayState *s = con->ds;
1678 DisplayChangeListener *dcl;
1680 QLIST_FOREACH(dcl, &s->listeners, next) {
1681 if (dcl->ops->dpy_cursor_define) {
1682 return true;
1685 return false;
1688 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1689 struct QEMUGLParams *qparams)
1691 assert(con->gl);
1692 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1695 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1697 assert(con->gl);
1698 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1701 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1703 assert(con->gl);
1704 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1707 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1709 assert(con->gl);
1710 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1713 void dpy_gl_scanout(QemuConsole *con,
1714 uint32_t backing_id, bool backing_y_0_top,
1715 uint32_t backing_width, uint32_t backing_height,
1716 uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1718 assert(con->gl);
1719 con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1720 backing_y_0_top,
1721 backing_width, backing_height,
1722 x, y, width, height);
1725 void dpy_gl_update(QemuConsole *con,
1726 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1728 assert(con->gl);
1729 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1732 /***********************************************************/
1733 /* register display */
1735 /* console.c internal use only */
1736 static DisplayState *get_alloc_displaystate(void)
1738 if (!display_state) {
1739 display_state = g_new0(DisplayState, 1);
1740 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1741 text_console_update_cursor, NULL);
1743 return display_state;
1747 * Called by main(), after creating QemuConsoles
1748 * and before initializing ui (sdl/vnc/...).
1750 DisplayState *init_displaystate(void)
1752 gchar *name;
1753 int i;
1755 get_alloc_displaystate();
1756 for (i = 0; i < nb_consoles; i++) {
1757 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1758 consoles[i]->ds == NULL) {
1759 text_console_do_init(consoles[i]->chr, display_state);
1762 /* Hook up into the qom tree here (not in new_console()), once
1763 * all QemuConsoles are created and the order / numbering
1764 * doesn't change any more */
1765 name = g_strdup_printf("console[%d]", i);
1766 object_property_add_child(container_get(object_get_root(), "/backend"),
1767 name, OBJECT(consoles[i]), &error_abort);
1768 g_free(name);
1771 return display_state;
1774 void graphic_console_set_hwops(QemuConsole *con,
1775 const GraphicHwOps *hw_ops,
1776 void *opaque)
1778 con->hw_ops = hw_ops;
1779 con->hw = opaque;
1782 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1783 const GraphicHwOps *hw_ops,
1784 void *opaque)
1786 static const char noinit[] =
1787 "Guest has not initialized the display (yet).";
1788 int width = 640;
1789 int height = 480;
1790 QemuConsole *s;
1791 DisplayState *ds;
1793 ds = get_alloc_displaystate();
1794 trace_console_gfx_new();
1795 s = new_console(ds, GRAPHIC_CONSOLE, head);
1796 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1797 graphic_console_set_hwops(s, hw_ops, opaque);
1798 if (dev) {
1799 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1800 &error_abort);
1803 s->surface = qemu_create_message_surface(width, height, noinit);
1804 return s;
1807 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1809 if (index >= nb_consoles) {
1810 return NULL;
1812 return consoles[index];
1815 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1817 Object *obj;
1818 uint32_t h;
1819 int i;
1821 for (i = 0; i < nb_consoles; i++) {
1822 if (!consoles[i]) {
1823 continue;
1825 obj = object_property_get_link(OBJECT(consoles[i]),
1826 "device", &error_abort);
1827 if (DEVICE(obj) != dev) {
1828 continue;
1830 h = object_property_get_int(OBJECT(consoles[i]),
1831 "head", &error_abort);
1832 if (h != head) {
1833 continue;
1835 return consoles[i];
1837 return NULL;
1840 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1841 uint32_t head, Error **errp)
1843 DeviceState *dev;
1844 QemuConsole *con;
1846 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1847 if (dev == NULL) {
1848 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1849 "Device '%s' not found", device_id);
1850 return NULL;
1853 con = qemu_console_lookup_by_device(dev, head);
1854 if (con == NULL) {
1855 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1856 device_id, head);
1857 return NULL;
1860 return con;
1863 bool qemu_console_is_visible(QemuConsole *con)
1865 return (con == active_console) || (con->dcls > 0);
1868 bool qemu_console_is_graphic(QemuConsole *con)
1870 if (con == NULL) {
1871 con = active_console;
1873 return con && (con->console_type == GRAPHIC_CONSOLE);
1876 bool qemu_console_is_fixedsize(QemuConsole *con)
1878 if (con == NULL) {
1879 con = active_console;
1881 return con && (con->console_type != TEXT_CONSOLE);
1884 bool qemu_console_is_gl_blocked(QemuConsole *con)
1886 assert(con != NULL);
1887 return con->gl_block;
1890 char *qemu_console_get_label(QemuConsole *con)
1892 if (con->console_type == GRAPHIC_CONSOLE) {
1893 if (con->device) {
1894 return g_strdup(object_get_typename(con->device));
1896 return g_strdup("VGA");
1897 } else {
1898 if (con->chr && con->chr->label) {
1899 return g_strdup(con->chr->label);
1901 return g_strdup_printf("vc%d", con->index);
1905 int qemu_console_get_index(QemuConsole *con)
1907 if (con == NULL) {
1908 con = active_console;
1910 return con ? con->index : -1;
1913 uint32_t qemu_console_get_head(QemuConsole *con)
1915 if (con == NULL) {
1916 con = active_console;
1918 return con ? con->head : -1;
1921 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1923 assert(con != NULL);
1924 return &con->ui_info;
1927 int qemu_console_get_width(QemuConsole *con, int fallback)
1929 if (con == NULL) {
1930 con = active_console;
1932 return con ? surface_width(con->surface) : fallback;
1935 int qemu_console_get_height(QemuConsole *con, int fallback)
1937 if (con == NULL) {
1938 con = active_console;
1940 return con ? surface_height(con->surface) : fallback;
1943 static void text_console_set_echo(CharDriverState *chr, bool echo)
1945 QemuConsole *s = chr->opaque;
1947 s->echo = echo;
1950 static void text_console_update_cursor_timer(void)
1952 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1953 + CONSOLE_CURSOR_PERIOD / 2);
1956 static void text_console_update_cursor(void *opaque)
1958 QemuConsole *s;
1959 int i, count = 0;
1961 cursor_visible_phase = !cursor_visible_phase;
1963 for (i = 0; i < nb_consoles; i++) {
1964 s = consoles[i];
1965 if (qemu_console_is_graphic(s) ||
1966 !qemu_console_is_visible(s)) {
1967 continue;
1969 count++;
1970 graphic_hw_invalidate(s);
1973 if (count) {
1974 text_console_update_cursor_timer();
1978 static const GraphicHwOps text_console_ops = {
1979 .invalidate = text_console_invalidate,
1980 .text_update = text_console_update,
1983 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1985 QemuConsole *s;
1986 int g_width = 80 * FONT_WIDTH;
1987 int g_height = 24 * FONT_HEIGHT;
1989 s = chr->opaque;
1991 chr->chr_write = console_puts;
1993 s->out_fifo.buf = s->out_fifo_buf;
1994 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1995 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1996 s->ds = ds;
1998 s->y_displayed = 0;
1999 s->y_base = 0;
2000 s->total_height = DEFAULT_BACKSCROLL;
2001 s->x = 0;
2002 s->y = 0;
2003 if (!s->surface) {
2004 if (active_console && active_console->surface) {
2005 g_width = surface_width(active_console->surface);
2006 g_height = surface_height(active_console->surface);
2008 s->surface = qemu_create_displaysurface(g_width, g_height);
2011 s->hw_ops = &text_console_ops;
2012 s->hw = s;
2014 /* Set text attribute defaults */
2015 s->t_attrib_default.bold = 0;
2016 s->t_attrib_default.uline = 0;
2017 s->t_attrib_default.blink = 0;
2018 s->t_attrib_default.invers = 0;
2019 s->t_attrib_default.unvisible = 0;
2020 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2021 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2022 /* set current text attributes to default */
2023 s->t_attrib = s->t_attrib_default;
2024 text_console_resize(s);
2026 if (chr->label) {
2027 char msg[128];
2028 int len;
2030 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2031 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2032 console_puts(chr, (uint8_t*)msg, len);
2033 s->t_attrib = s->t_attrib_default;
2036 qemu_chr_be_generic_open(chr);
2037 if (chr->init)
2038 chr->init(chr);
2041 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2043 ChardevCommon *common = qapi_ChardevVC_base(vc);
2044 CharDriverState *chr;
2045 QemuConsole *s;
2046 unsigned width = 0;
2047 unsigned height = 0;
2049 chr = qemu_chr_alloc(common, errp);
2050 if (!chr) {
2051 return NULL;
2054 if (vc->has_width) {
2055 width = vc->width;
2056 } else if (vc->has_cols) {
2057 width = vc->cols * FONT_WIDTH;
2060 if (vc->has_height) {
2061 height = vc->height;
2062 } else if (vc->has_rows) {
2063 height = vc->rows * FONT_HEIGHT;
2066 trace_console_txt_new(width, height);
2067 if (width == 0 || height == 0) {
2068 s = new_console(NULL, TEXT_CONSOLE, 0);
2069 } else {
2070 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2071 s->surface = qemu_create_displaysurface(width, height);
2074 if (!s) {
2075 g_free(chr);
2076 error_setg(errp, "cannot create text console");
2077 return NULL;
2080 s->chr = chr;
2081 chr->opaque = s;
2082 chr->chr_set_echo = text_console_set_echo;
2083 /* console/chardev init sometimes completes elsewhere in a 2nd
2084 * stage, so defer OPENED events until they are fully initialized
2086 chr->explicit_be_open = true;
2088 if (display_state) {
2089 text_console_do_init(chr, display_state);
2091 return chr;
2094 static VcHandler *vc_handler = text_console_init;
2096 static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2097 ChardevReturn *ret, Error **errp)
2099 return vc_handler(backend->u.vc.data, errp);
2102 void register_vc_handler(VcHandler *handler)
2104 vc_handler = handler;
2107 void qemu_console_resize(QemuConsole *s, int width, int height)
2109 DisplaySurface *surface;
2111 assert(s->console_type == GRAPHIC_CONSOLE);
2113 if (s->surface &&
2114 pixman_image_get_width(s->surface->image) == width &&
2115 pixman_image_get_height(s->surface->image) == height) {
2116 return;
2119 surface = qemu_create_displaysurface(width, height);
2120 dpy_gfx_replace_surface(s, surface);
2123 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
2124 int dst_x, int dst_y, int w, int h)
2126 assert(con->console_type == GRAPHIC_CONSOLE);
2127 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
2130 DisplaySurface *qemu_console_surface(QemuConsole *console)
2132 return console->surface;
2135 PixelFormat qemu_default_pixelformat(int bpp)
2137 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2138 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2139 return pf;
2142 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2143 Error **errp)
2145 int val;
2146 ChardevVC *vc;
2148 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2149 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2151 val = qemu_opt_get_number(opts, "width", 0);
2152 if (val != 0) {
2153 vc->has_width = true;
2154 vc->width = val;
2157 val = qemu_opt_get_number(opts, "height", 0);
2158 if (val != 0) {
2159 vc->has_height = true;
2160 vc->height = val;
2163 val = qemu_opt_get_number(opts, "cols", 0);
2164 if (val != 0) {
2165 vc->has_cols = true;
2166 vc->cols = val;
2169 val = qemu_opt_get_number(opts, "rows", 0);
2170 if (val != 0) {
2171 vc->has_rows = true;
2172 vc->rows = val;
2176 static const TypeInfo qemu_console_info = {
2177 .name = TYPE_QEMU_CONSOLE,
2178 .parent = TYPE_OBJECT,
2179 .instance_size = sizeof(QemuConsole),
2180 .class_size = sizeof(QemuConsoleClass),
2184 static void register_types(void)
2186 type_register_static(&qemu_console_info);
2187 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
2188 vc_init);
2191 type_init(register_types);