Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / ui / console.c
blobae5561321457922cfdee7bf4997a711c3c33bdab
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;
128 /* Graphic console state. */
129 Object *device;
130 uint32_t head;
131 QemuUIInfo ui_info;
132 QEMUTimer *ui_timer;
133 const GraphicHwOps *hw_ops;
134 void *hw;
136 /* Text console state */
137 int width;
138 int height;
139 int total_height;
140 int backscroll_height;
141 int x, y;
142 int x_saved, y_saved;
143 int y_displayed;
144 int y_base;
145 TextAttributes t_attrib_default; /* default text attributes */
146 TextAttributes t_attrib; /* currently active text attributes */
147 TextCell *cells;
148 int text_x[2], text_y[2], cursor_invalidate;
149 int echo;
151 int update_x0;
152 int update_y0;
153 int update_x1;
154 int update_y1;
156 enum TTYState state;
157 int esc_params[MAX_ESC_PARAMS];
158 int nb_esc_params;
160 CharDriverState *chr;
161 /* fifo for key pressed */
162 QEMUFIFO out_fifo;
163 uint8_t out_fifo_buf[16];
164 QEMUTimer *kbd_timer;
167 struct DisplayState {
168 QEMUTimer *gui_timer;
169 uint64_t last_update;
170 uint64_t update_interval;
171 bool refreshing;
172 bool have_gfx;
173 bool have_text;
175 QLIST_HEAD(, DisplayChangeListener) listeners;
178 static DisplayState *display_state;
179 static QemuConsole *active_console;
180 static QemuConsole **consoles;
181 static int nb_consoles = 0;
182 static bool cursor_visible_phase;
183 static QEMUTimer *cursor_timer;
185 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
186 static void dpy_refresh(DisplayState *s);
187 static DisplayState *get_alloc_displaystate(void);
188 static void text_console_update_cursor_timer(void);
189 static void text_console_update_cursor(void *opaque);
191 static void gui_update(void *opaque)
193 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
194 uint64_t dcl_interval;
195 DisplayState *ds = opaque;
196 DisplayChangeListener *dcl;
197 int i;
199 ds->refreshing = true;
200 dpy_refresh(ds);
201 ds->refreshing = false;
203 QLIST_FOREACH(dcl, &ds->listeners, next) {
204 dcl_interval = dcl->update_interval ?
205 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
206 if (interval > dcl_interval) {
207 interval = dcl_interval;
210 if (ds->update_interval != interval) {
211 ds->update_interval = interval;
212 for (i = 0; i < nb_consoles; i++) {
213 if (consoles[i]->hw_ops->update_interval) {
214 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
217 trace_console_refresh(interval);
219 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
220 timer_mod(ds->gui_timer, ds->last_update + interval);
223 static void gui_setup_refresh(DisplayState *ds)
225 DisplayChangeListener *dcl;
226 bool need_timer = false;
227 bool have_gfx = false;
228 bool have_text = false;
230 QLIST_FOREACH(dcl, &ds->listeners, next) {
231 if (dcl->ops->dpy_refresh != NULL) {
232 need_timer = true;
234 if (dcl->ops->dpy_gfx_update != NULL) {
235 have_gfx = true;
237 if (dcl->ops->dpy_text_update != NULL) {
238 have_text = true;
242 if (need_timer && ds->gui_timer == NULL) {
243 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
244 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
246 if (!need_timer && ds->gui_timer != NULL) {
247 timer_del(ds->gui_timer);
248 timer_free(ds->gui_timer);
249 ds->gui_timer = NULL;
252 ds->have_gfx = have_gfx;
253 ds->have_text = have_text;
256 void graphic_hw_update(QemuConsole *con)
258 if (!con) {
259 con = active_console;
261 if (con && con->hw_ops->gfx_update) {
262 con->hw_ops->gfx_update(con->hw);
266 void graphic_hw_gl_block(QemuConsole *con, bool block)
268 if (!con) {
269 con = active_console;
271 if (con && con->hw_ops->gl_block) {
272 con->hw_ops->gl_block(con->hw, block);
276 void graphic_hw_invalidate(QemuConsole *con)
278 if (!con) {
279 con = active_console;
281 if (con && con->hw_ops->invalidate) {
282 con->hw_ops->invalidate(con->hw);
286 static void ppm_save(const char *filename, DisplaySurface *ds,
287 Error **errp)
289 int width = pixman_image_get_width(ds->image);
290 int height = pixman_image_get_height(ds->image);
291 int fd;
292 FILE *f;
293 int y;
294 int ret;
295 pixman_image_t *linebuf;
297 trace_ppm_save(filename, ds);
298 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
299 if (fd == -1) {
300 error_setg(errp, "failed to open file '%s': %s", filename,
301 strerror(errno));
302 return;
304 f = fdopen(fd, "wb");
305 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
306 if (ret < 0) {
307 linebuf = NULL;
308 goto write_err;
310 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
311 for (y = 0; y < height; y++) {
312 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
313 clearerr(f);
314 ret = fwrite(pixman_image_get_data(linebuf), 1,
315 pixman_image_get_stride(linebuf), f);
316 (void)ret;
317 if (ferror(f)) {
318 goto write_err;
322 out:
323 qemu_pixman_image_unref(linebuf);
324 fclose(f);
325 return;
327 write_err:
328 error_setg(errp, "failed to write to file '%s': %s", filename,
329 strerror(errno));
330 unlink(filename);
331 goto out;
334 void qmp_screendump(const char *filename, Error **errp)
336 QemuConsole *con = qemu_console_lookup_by_index(0);
337 DisplaySurface *surface;
339 if (con == NULL) {
340 error_setg(errp, "There is no QemuConsole I can screendump from.");
341 return;
344 graphic_hw_update(con);
345 surface = qemu_console_surface(con);
346 ppm_save(filename, surface, errp);
349 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
351 if (!con) {
352 con = active_console;
354 if (con && con->hw_ops->text_update) {
355 con->hw_ops->text_update(con->hw, chardata);
359 static void vga_fill_rect(QemuConsole *con,
360 int posx, int posy, int width, int height,
361 pixman_color_t color)
363 DisplaySurface *surface = qemu_console_surface(con);
364 pixman_rectangle16_t rect = {
365 .x = posx, .y = posy, .width = width, .height = height
368 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
369 &color, 1, &rect);
372 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
373 static void vga_bitblt(QemuConsole *con,
374 int xs, int ys, int xd, int yd, int w, int h)
376 DisplaySurface *surface = qemu_console_surface(con);
378 pixman_image_composite(PIXMAN_OP_SRC,
379 surface->image, NULL, surface->image,
380 xs, ys, 0, 0, xd, yd, w, h);
383 /***********************************************************/
384 /* basic char display */
386 #define FONT_HEIGHT 16
387 #define FONT_WIDTH 8
389 #include "vgafont.h"
391 #define QEMU_RGB(r, g, b) \
392 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
394 static const pixman_color_t color_table_rgb[2][8] = {
395 { /* dark */
396 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
397 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
398 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
399 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
400 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
401 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
402 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
403 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
405 { /* bright */
406 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
407 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
408 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
409 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
410 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
411 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
412 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
413 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
417 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
418 TextAttributes *t_attrib)
420 static pixman_image_t *glyphs[256];
421 DisplaySurface *surface = qemu_console_surface(s);
422 pixman_color_t fgcol, bgcol;
424 if (t_attrib->invers) {
425 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
426 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
427 } else {
428 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
429 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
432 if (!glyphs[ch]) {
433 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
435 qemu_pixman_glyph_render(glyphs[ch], surface->image,
436 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
439 static void text_console_resize(QemuConsole *s)
441 TextCell *cells, *c, *c1;
442 int w1, x, y, last_width;
444 last_width = s->width;
445 s->width = surface_width(s->surface) / FONT_WIDTH;
446 s->height = surface_height(s->surface) / FONT_HEIGHT;
448 w1 = last_width;
449 if (s->width < w1)
450 w1 = s->width;
452 cells = g_new(TextCell, s->width * s->total_height);
453 for(y = 0; y < s->total_height; y++) {
454 c = &cells[y * s->width];
455 if (w1 > 0) {
456 c1 = &s->cells[y * last_width];
457 for(x = 0; x < w1; x++) {
458 *c++ = *c1++;
461 for(x = w1; x < s->width; x++) {
462 c->ch = ' ';
463 c->t_attrib = s->t_attrib_default;
464 c++;
467 g_free(s->cells);
468 s->cells = cells;
471 static inline void text_update_xy(QemuConsole *s, int x, int y)
473 s->text_x[0] = MIN(s->text_x[0], x);
474 s->text_x[1] = MAX(s->text_x[1], x);
475 s->text_y[0] = MIN(s->text_y[0], y);
476 s->text_y[1] = MAX(s->text_y[1], y);
479 static void invalidate_xy(QemuConsole *s, int x, int y)
481 if (!qemu_console_is_visible(s)) {
482 return;
484 if (s->update_x0 > x * FONT_WIDTH)
485 s->update_x0 = x * FONT_WIDTH;
486 if (s->update_y0 > y * FONT_HEIGHT)
487 s->update_y0 = y * FONT_HEIGHT;
488 if (s->update_x1 < (x + 1) * FONT_WIDTH)
489 s->update_x1 = (x + 1) * FONT_WIDTH;
490 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
491 s->update_y1 = (y + 1) * FONT_HEIGHT;
494 static void update_xy(QemuConsole *s, int x, int y)
496 TextCell *c;
497 int y1, y2;
499 if (s->ds->have_text) {
500 text_update_xy(s, x, y);
503 y1 = (s->y_base + y) % s->total_height;
504 y2 = y1 - s->y_displayed;
505 if (y2 < 0) {
506 y2 += s->total_height;
508 if (y2 < s->height) {
509 c = &s->cells[y1 * s->width + x];
510 vga_putcharxy(s, x, y2, c->ch,
511 &(c->t_attrib));
512 invalidate_xy(s, x, y2);
516 static void console_show_cursor(QemuConsole *s, int show)
518 TextCell *c;
519 int y, y1;
520 int x = s->x;
522 if (s->ds->have_text) {
523 s->cursor_invalidate = 1;
526 if (x >= s->width) {
527 x = s->width - 1;
529 y1 = (s->y_base + s->y) % s->total_height;
530 y = y1 - s->y_displayed;
531 if (y < 0) {
532 y += s->total_height;
534 if (y < s->height) {
535 c = &s->cells[y1 * s->width + x];
536 if (show && cursor_visible_phase) {
537 TextAttributes t_attrib = s->t_attrib_default;
538 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
539 vga_putcharxy(s, x, y, c->ch, &t_attrib);
540 } else {
541 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
543 invalidate_xy(s, x, y);
547 static void console_refresh(QemuConsole *s)
549 DisplaySurface *surface = qemu_console_surface(s);
550 TextCell *c;
551 int x, y, y1;
553 if (s->ds->have_text) {
554 s->text_x[0] = 0;
555 s->text_y[0] = 0;
556 s->text_x[1] = s->width - 1;
557 s->text_y[1] = s->height - 1;
558 s->cursor_invalidate = 1;
561 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
562 color_table_rgb[0][QEMU_COLOR_BLACK]);
563 y1 = s->y_displayed;
564 for (y = 0; y < s->height; y++) {
565 c = s->cells + y1 * s->width;
566 for (x = 0; x < s->width; x++) {
567 vga_putcharxy(s, x, y, c->ch,
568 &(c->t_attrib));
569 c++;
571 if (++y1 == s->total_height) {
572 y1 = 0;
575 console_show_cursor(s, 1);
576 dpy_gfx_update(s, 0, 0,
577 surface_width(surface), surface_height(surface));
580 static void console_scroll(QemuConsole *s, int ydelta)
582 int i, y1;
584 if (ydelta > 0) {
585 for(i = 0; i < ydelta; i++) {
586 if (s->y_displayed == s->y_base)
587 break;
588 if (++s->y_displayed == s->total_height)
589 s->y_displayed = 0;
591 } else {
592 ydelta = -ydelta;
593 i = s->backscroll_height;
594 if (i > s->total_height - s->height)
595 i = s->total_height - s->height;
596 y1 = s->y_base - i;
597 if (y1 < 0)
598 y1 += s->total_height;
599 for(i = 0; i < ydelta; i++) {
600 if (s->y_displayed == y1)
601 break;
602 if (--s->y_displayed < 0)
603 s->y_displayed = s->total_height - 1;
606 console_refresh(s);
609 static void console_put_lf(QemuConsole *s)
611 TextCell *c;
612 int x, y1;
614 s->y++;
615 if (s->y >= s->height) {
616 s->y = s->height - 1;
618 if (s->y_displayed == s->y_base) {
619 if (++s->y_displayed == s->total_height)
620 s->y_displayed = 0;
622 if (++s->y_base == s->total_height)
623 s->y_base = 0;
624 if (s->backscroll_height < s->total_height)
625 s->backscroll_height++;
626 y1 = (s->y_base + s->height - 1) % s->total_height;
627 c = &s->cells[y1 * s->width];
628 for(x = 0; x < s->width; x++) {
629 c->ch = ' ';
630 c->t_attrib = s->t_attrib_default;
631 c++;
633 if (s->y_displayed == s->y_base) {
634 if (s->ds->have_text) {
635 s->text_x[0] = 0;
636 s->text_y[0] = 0;
637 s->text_x[1] = s->width - 1;
638 s->text_y[1] = s->height - 1;
641 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
642 s->width * FONT_WIDTH,
643 (s->height - 1) * FONT_HEIGHT);
644 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
645 s->width * FONT_WIDTH, FONT_HEIGHT,
646 color_table_rgb[0][s->t_attrib_default.bgcol]);
647 s->update_x0 = 0;
648 s->update_y0 = 0;
649 s->update_x1 = s->width * FONT_WIDTH;
650 s->update_y1 = s->height * FONT_HEIGHT;
655 /* Set console attributes depending on the current escape codes.
656 * NOTE: I know this code is not very efficient (checking every color for it
657 * self) but it is more readable and better maintainable.
659 static void console_handle_escape(QemuConsole *s)
661 int i;
663 for (i=0; i<s->nb_esc_params; i++) {
664 switch (s->esc_params[i]) {
665 case 0: /* reset all console attributes to default */
666 s->t_attrib = s->t_attrib_default;
667 break;
668 case 1:
669 s->t_attrib.bold = 1;
670 break;
671 case 4:
672 s->t_attrib.uline = 1;
673 break;
674 case 5:
675 s->t_attrib.blink = 1;
676 break;
677 case 7:
678 s->t_attrib.invers = 1;
679 break;
680 case 8:
681 s->t_attrib.unvisible = 1;
682 break;
683 case 22:
684 s->t_attrib.bold = 0;
685 break;
686 case 24:
687 s->t_attrib.uline = 0;
688 break;
689 case 25:
690 s->t_attrib.blink = 0;
691 break;
692 case 27:
693 s->t_attrib.invers = 0;
694 break;
695 case 28:
696 s->t_attrib.unvisible = 0;
697 break;
698 /* set foreground color */
699 case 30:
700 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
701 break;
702 case 31:
703 s->t_attrib.fgcol = QEMU_COLOR_RED;
704 break;
705 case 32:
706 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
707 break;
708 case 33:
709 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
710 break;
711 case 34:
712 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
713 break;
714 case 35:
715 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
716 break;
717 case 36:
718 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
719 break;
720 case 37:
721 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
722 break;
723 /* set background color */
724 case 40:
725 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
726 break;
727 case 41:
728 s->t_attrib.bgcol = QEMU_COLOR_RED;
729 break;
730 case 42:
731 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
732 break;
733 case 43:
734 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
735 break;
736 case 44:
737 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
738 break;
739 case 45:
740 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
741 break;
742 case 46:
743 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
744 break;
745 case 47:
746 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
747 break;
752 static void console_clear_xy(QemuConsole *s, int x, int y)
754 int y1 = (s->y_base + y) % s->total_height;
755 TextCell *c = &s->cells[y1 * s->width + x];
756 c->ch = ' ';
757 c->t_attrib = s->t_attrib_default;
758 update_xy(s, x, y);
761 static void console_put_one(QemuConsole *s, int ch)
763 TextCell *c;
764 int y1;
765 if (s->x >= s->width) {
766 /* line wrap */
767 s->x = 0;
768 console_put_lf(s);
770 y1 = (s->y_base + s->y) % s->total_height;
771 c = &s->cells[y1 * s->width + s->x];
772 c->ch = ch;
773 c->t_attrib = s->t_attrib;
774 update_xy(s, s->x, s->y);
775 s->x++;
778 static void console_respond_str(QemuConsole *s, const char *buf)
780 while (*buf) {
781 console_put_one(s, *buf);
782 buf++;
786 /* set cursor, checking bounds */
787 static void set_cursor(QemuConsole *s, int x, int y)
789 if (x < 0) {
790 x = 0;
792 if (y < 0) {
793 y = 0;
795 if (y >= s->height) {
796 y = s->height - 1;
798 if (x >= s->width) {
799 x = s->width - 1;
802 s->x = x;
803 s->y = y;
806 static void console_putchar(QemuConsole *s, int ch)
808 int i;
809 int x, y;
810 char response[40];
812 switch(s->state) {
813 case TTY_STATE_NORM:
814 switch(ch) {
815 case '\r': /* carriage return */
816 s->x = 0;
817 break;
818 case '\n': /* newline */
819 console_put_lf(s);
820 break;
821 case '\b': /* backspace */
822 if (s->x > 0)
823 s->x--;
824 break;
825 case '\t': /* tabspace */
826 if (s->x + (8 - (s->x % 8)) > s->width) {
827 s->x = 0;
828 console_put_lf(s);
829 } else {
830 s->x = s->x + (8 - (s->x % 8));
832 break;
833 case '\a': /* alert aka. bell */
834 /* TODO: has to be implemented */
835 break;
836 case 14:
837 /* SI (shift in), character set 0 (ignored) */
838 break;
839 case 15:
840 /* SO (shift out), character set 1 (ignored) */
841 break;
842 case 27: /* esc (introducing an escape sequence) */
843 s->state = TTY_STATE_ESC;
844 break;
845 default:
846 console_put_one(s, ch);
847 break;
849 break;
850 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
851 if (ch == '[') {
852 for(i=0;i<MAX_ESC_PARAMS;i++)
853 s->esc_params[i] = 0;
854 s->nb_esc_params = 0;
855 s->state = TTY_STATE_CSI;
856 } else {
857 s->state = TTY_STATE_NORM;
859 break;
860 case TTY_STATE_CSI: /* handle escape sequence parameters */
861 if (ch >= '0' && ch <= '9') {
862 if (s->nb_esc_params < MAX_ESC_PARAMS) {
863 int *param = &s->esc_params[s->nb_esc_params];
864 int digit = (ch - '0');
866 *param = (*param <= (INT_MAX - digit) / 10) ?
867 *param * 10 + digit : INT_MAX;
869 } else {
870 if (s->nb_esc_params < MAX_ESC_PARAMS)
871 s->nb_esc_params++;
872 if (ch == ';')
873 break;
874 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
875 ch, s->nb_esc_params);
876 s->state = TTY_STATE_NORM;
877 switch(ch) {
878 case 'A':
879 /* move cursor up */
880 if (s->esc_params[0] == 0) {
881 s->esc_params[0] = 1;
883 set_cursor(s, s->x, s->y - s->esc_params[0]);
884 break;
885 case 'B':
886 /* move cursor down */
887 if (s->esc_params[0] == 0) {
888 s->esc_params[0] = 1;
890 set_cursor(s, s->x, s->y + s->esc_params[0]);
891 break;
892 case 'C':
893 /* move cursor right */
894 if (s->esc_params[0] == 0) {
895 s->esc_params[0] = 1;
897 set_cursor(s, s->x + s->esc_params[0], s->y);
898 break;
899 case 'D':
900 /* move cursor left */
901 if (s->esc_params[0] == 0) {
902 s->esc_params[0] = 1;
904 set_cursor(s, s->x - s->esc_params[0], s->y);
905 break;
906 case 'G':
907 /* move cursor to column */
908 set_cursor(s, s->esc_params[0] - 1, s->y);
909 break;
910 case 'f':
911 case 'H':
912 /* move cursor to row, column */
913 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
914 break;
915 case 'J':
916 switch (s->esc_params[0]) {
917 case 0:
918 /* clear to end of screen */
919 for (y = s->y; y < s->height; y++) {
920 for (x = 0; x < s->width; x++) {
921 if (y == s->y && x < s->x) {
922 continue;
924 console_clear_xy(s, x, y);
927 break;
928 case 1:
929 /* clear from beginning of screen */
930 for (y = 0; y <= s->y; y++) {
931 for (x = 0; x < s->width; x++) {
932 if (y == s->y && x > s->x) {
933 break;
935 console_clear_xy(s, x, y);
938 break;
939 case 2:
940 /* clear entire screen */
941 for (y = 0; y <= s->height; y++) {
942 for (x = 0; x < s->width; x++) {
943 console_clear_xy(s, x, y);
946 break;
948 break;
949 case 'K':
950 switch (s->esc_params[0]) {
951 case 0:
952 /* clear to eol */
953 for (x = s->x; x < s->width; x++) {
954 console_clear_xy(s, x, s->y);
956 break;
957 case 1:
958 /* clear from beginning of line */
959 for (x = 0; x <= s->x; x++) {
960 console_clear_xy(s, x, s->y);
962 break;
963 case 2:
964 /* clear entire line */
965 for(x = 0; x < s->width; x++) {
966 console_clear_xy(s, x, s->y);
968 break;
970 break;
971 case 'm':
972 console_handle_escape(s);
973 break;
974 case 'n':
975 switch (s->esc_params[0]) {
976 case 5:
977 /* report console status (always succeed)*/
978 console_respond_str(s, "\033[0n");
979 break;
980 case 6:
981 /* report cursor position */
982 sprintf(response, "\033[%d;%dR",
983 (s->y_base + s->y) % s->total_height + 1,
984 s->x + 1);
985 console_respond_str(s, response);
986 break;
988 break;
989 case 's':
990 /* save cursor position */
991 s->x_saved = s->x;
992 s->y_saved = s->y;
993 break;
994 case 'u':
995 /* restore cursor position */
996 s->x = s->x_saved;
997 s->y = s->y_saved;
998 break;
999 default:
1000 trace_console_putchar_unhandled(ch);
1001 break;
1003 break;
1008 void console_select(unsigned int index)
1010 DisplayChangeListener *dcl;
1011 QemuConsole *s;
1013 trace_console_select(index);
1014 s = qemu_console_lookup_by_index(index);
1015 if (s) {
1016 DisplayState *ds = s->ds;
1018 active_console = s;
1019 if (ds->have_gfx) {
1020 QLIST_FOREACH(dcl, &ds->listeners, next) {
1021 if (dcl->con != NULL) {
1022 continue;
1024 if (dcl->ops->dpy_gfx_switch) {
1025 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1028 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1029 surface_height(s->surface));
1031 if (ds->have_text) {
1032 dpy_text_resize(s, s->width, s->height);
1034 text_console_update_cursor(NULL);
1038 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1040 QemuConsole *s = chr->opaque;
1041 int i;
1043 s->update_x0 = s->width * FONT_WIDTH;
1044 s->update_y0 = s->height * FONT_HEIGHT;
1045 s->update_x1 = 0;
1046 s->update_y1 = 0;
1047 console_show_cursor(s, 0);
1048 for(i = 0; i < len; i++) {
1049 console_putchar(s, buf[i]);
1051 console_show_cursor(s, 1);
1052 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1053 dpy_gfx_update(s, s->update_x0, s->update_y0,
1054 s->update_x1 - s->update_x0,
1055 s->update_y1 - s->update_y0);
1057 return len;
1060 static void kbd_send_chars(void *opaque)
1062 QemuConsole *s = opaque;
1063 int len;
1064 uint8_t buf[16];
1066 len = qemu_chr_be_can_write(s->chr);
1067 if (len > s->out_fifo.count)
1068 len = s->out_fifo.count;
1069 if (len > 0) {
1070 if (len > sizeof(buf))
1071 len = sizeof(buf);
1072 qemu_fifo_read(&s->out_fifo, buf, len);
1073 qemu_chr_be_write(s->chr, buf, len);
1075 /* characters are pending: we send them a bit later (XXX:
1076 horrible, should change char device API) */
1077 if (s->out_fifo.count > 0) {
1078 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1082 /* called when an ascii key is pressed */
1083 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1085 uint8_t buf[16], *q;
1086 int c;
1088 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1089 return;
1091 switch(keysym) {
1092 case QEMU_KEY_CTRL_UP:
1093 console_scroll(s, -1);
1094 break;
1095 case QEMU_KEY_CTRL_DOWN:
1096 console_scroll(s, 1);
1097 break;
1098 case QEMU_KEY_CTRL_PAGEUP:
1099 console_scroll(s, -10);
1100 break;
1101 case QEMU_KEY_CTRL_PAGEDOWN:
1102 console_scroll(s, 10);
1103 break;
1104 default:
1105 /* convert the QEMU keysym to VT100 key string */
1106 q = buf;
1107 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1108 *q++ = '\033';
1109 *q++ = '[';
1110 c = keysym - 0xe100;
1111 if (c >= 10)
1112 *q++ = '0' + (c / 10);
1113 *q++ = '0' + (c % 10);
1114 *q++ = '~';
1115 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1116 *q++ = '\033';
1117 *q++ = '[';
1118 *q++ = keysym & 0xff;
1119 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1120 console_puts(s->chr, (const uint8_t *) "\r", 1);
1121 *q++ = '\n';
1122 } else {
1123 *q++ = keysym;
1125 if (s->echo) {
1126 console_puts(s->chr, buf, q - buf);
1128 if (s->chr->chr_read) {
1129 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1130 kbd_send_chars(s);
1132 break;
1136 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1137 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1138 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1139 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1140 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1141 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1142 [Q_KEY_CODE_END] = QEMU_KEY_END,
1143 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1144 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1145 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1148 bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1150 int keysym;
1152 keysym = qcode_to_keysym[qcode];
1153 if (keysym == 0) {
1154 return false;
1156 kbd_put_keysym_console(s, keysym);
1157 return true;
1160 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1162 int i;
1164 for (i = 0; i < len && str[i]; i++) {
1165 kbd_put_keysym_console(s, str[i]);
1169 void kbd_put_keysym(int keysym)
1171 kbd_put_keysym_console(active_console, keysym);
1174 static void text_console_invalidate(void *opaque)
1176 QemuConsole *s = (QemuConsole *) opaque;
1178 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1179 text_console_resize(s);
1181 console_refresh(s);
1184 static void text_console_update(void *opaque, console_ch_t *chardata)
1186 QemuConsole *s = (QemuConsole *) opaque;
1187 int i, j, src;
1189 if (s->text_x[0] <= s->text_x[1]) {
1190 src = (s->y_base + s->text_y[0]) * s->width;
1191 chardata += s->text_y[0] * s->width;
1192 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1193 for (j = 0; j < s->width; j++, src++) {
1194 console_write_ch(chardata ++,
1195 ATTR2CHTYPE(s->cells[src].ch,
1196 s->cells[src].t_attrib.fgcol,
1197 s->cells[src].t_attrib.bgcol,
1198 s->cells[src].t_attrib.bold));
1200 dpy_text_update(s, s->text_x[0], s->text_y[0],
1201 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1202 s->text_x[0] = s->width;
1203 s->text_y[0] = s->height;
1204 s->text_x[1] = 0;
1205 s->text_y[1] = 0;
1207 if (s->cursor_invalidate) {
1208 dpy_text_cursor(s, s->x, s->y);
1209 s->cursor_invalidate = 0;
1213 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1214 uint32_t head)
1216 Object *obj;
1217 QemuConsole *s;
1218 int i;
1220 obj = object_new(TYPE_QEMU_CONSOLE);
1221 s = QEMU_CONSOLE(obj);
1222 s->head = head;
1223 object_property_add_link(obj, "device", TYPE_DEVICE,
1224 (Object **)&s->device,
1225 object_property_allow_set_link,
1226 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1227 &error_abort);
1228 object_property_add_uint32_ptr(obj, "head",
1229 &s->head, &error_abort);
1231 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1232 (console_type == GRAPHIC_CONSOLE))) {
1233 active_console = s;
1235 s->ds = ds;
1236 s->console_type = console_type;
1238 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1239 if (console_type != GRAPHIC_CONSOLE) {
1240 s->index = nb_consoles;
1241 consoles[nb_consoles++] = s;
1242 } else {
1243 /* HACK: Put graphical consoles before text consoles. */
1244 for (i = nb_consoles; i > 0; i--) {
1245 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1246 break;
1247 consoles[i] = consoles[i - 1];
1248 consoles[i]->index = i;
1250 s->index = i;
1251 consoles[i] = s;
1252 nb_consoles++;
1254 return s;
1257 static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1259 qemu_pixman_image_unref(surface->image);
1260 surface->image = NULL;
1262 surface->format = PIXMAN_x8r8g8b8;
1263 surface->image = pixman_image_create_bits(surface->format,
1264 width, height,
1265 NULL, width * 4);
1266 assert(surface->image != NULL);
1268 surface->flags = QEMU_ALLOCATED_FLAG;
1271 DisplaySurface *qemu_create_displaysurface(int width, int height)
1273 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1275 trace_displaysurface_create(surface, width, height);
1276 qemu_alloc_display(surface, width, height);
1277 return surface;
1280 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1281 pixman_format_code_t format,
1282 int linesize, uint8_t *data)
1284 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1286 trace_displaysurface_create_from(surface, width, height, format);
1287 surface->format = format;
1288 surface->image = pixman_image_create_bits(surface->format,
1289 width, height,
1290 (void *)data, linesize);
1291 assert(surface->image != NULL);
1293 return surface;
1296 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1298 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1300 trace_displaysurface_create_pixman(surface);
1301 surface->format = pixman_image_get_format(image);
1302 surface->image = pixman_image_ref(image);
1304 return surface;
1307 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1308 void *unused)
1310 void *data = pixman_image_get_data(image);
1311 uint32_t size = pixman_image_get_stride(image) *
1312 pixman_image_get_height(image);
1313 cpu_physical_memory_unmap(data, size, 0, 0);
1316 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1317 pixman_format_code_t format,
1318 int linesize, uint64_t addr)
1320 DisplaySurface *surface;
1321 hwaddr size;
1322 void *data;
1324 if (linesize == 0) {
1325 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1328 size = (hwaddr)linesize * height;
1329 data = cpu_physical_memory_map(addr, &size, 0);
1330 if (size != (hwaddr)linesize * height) {
1331 cpu_physical_memory_unmap(data, size, 0, 0);
1332 return NULL;
1335 surface = qemu_create_displaysurface_from
1336 (width, height, format, linesize, data);
1337 pixman_image_set_destroy_function
1338 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1340 return surface;
1343 static DisplaySurface *qemu_create_message_surface(int w, int h,
1344 const char *msg)
1346 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1347 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1348 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1349 pixman_image_t *glyph;
1350 int len, x, y, i;
1352 len = strlen(msg);
1353 x = (w / FONT_WIDTH - len) / 2;
1354 y = (h / FONT_HEIGHT - 1) / 2;
1355 for (i = 0; i < len; i++) {
1356 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1357 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1358 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1359 qemu_pixman_image_unref(glyph);
1361 return surface;
1364 void qemu_free_displaysurface(DisplaySurface *surface)
1366 if (surface == NULL) {
1367 return;
1369 trace_displaysurface_free(surface);
1370 qemu_pixman_image_unref(surface->image);
1371 g_free(surface);
1374 bool console_has_gl(QemuConsole *con)
1376 return con->gl != NULL;
1379 void register_displaychangelistener(DisplayChangeListener *dcl)
1381 static const char nodev[] =
1382 "This VM has no graphic display device.";
1383 static DisplaySurface *dummy;
1384 QemuConsole *con;
1386 if (dcl->ops->dpy_gl_ctx_create) {
1387 /* display has opengl support */
1388 assert(dcl->con);
1389 if (dcl->con->gl) {
1390 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1391 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1392 exit(1);
1394 dcl->con->gl = dcl;
1397 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1398 dcl->ds = get_alloc_displaystate();
1399 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1400 gui_setup_refresh(dcl->ds);
1401 if (dcl->con) {
1402 dcl->con->dcls++;
1403 con = dcl->con;
1404 } else {
1405 con = active_console;
1407 if (dcl->ops->dpy_gfx_switch) {
1408 if (con) {
1409 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1410 } else {
1411 if (!dummy) {
1412 dummy = qemu_create_message_surface(640, 480, nodev);
1414 dcl->ops->dpy_gfx_switch(dcl, dummy);
1417 text_console_update_cursor(NULL);
1420 void update_displaychangelistener(DisplayChangeListener *dcl,
1421 uint64_t interval)
1423 DisplayState *ds = dcl->ds;
1425 dcl->update_interval = interval;
1426 if (!ds->refreshing && ds->update_interval > interval) {
1427 timer_mod(ds->gui_timer, ds->last_update + interval);
1431 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1433 DisplayState *ds = dcl->ds;
1434 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1435 if (dcl->con) {
1436 dcl->con->dcls--;
1438 QLIST_REMOVE(dcl, next);
1439 gui_setup_refresh(ds);
1442 static void dpy_set_ui_info_timer(void *opaque)
1444 QemuConsole *con = opaque;
1446 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1449 bool dpy_ui_info_supported(QemuConsole *con)
1451 return con->hw_ops->ui_info != NULL;
1454 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1456 assert(con != NULL);
1458 if (!dpy_ui_info_supported(con)) {
1459 return -1;
1461 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1462 /* nothing changed -- ignore */
1463 return 0;
1467 * Typically we get a flood of these as the user resizes the window.
1468 * Wait until the dust has settled (one second without updates), then
1469 * go notify the guest.
1471 con->ui_info = *info;
1472 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1473 return 0;
1476 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1478 DisplayState *s = con->ds;
1479 DisplayChangeListener *dcl;
1480 int width = w;
1481 int height = h;
1483 if (con->surface) {
1484 width = surface_width(con->surface);
1485 height = surface_height(con->surface);
1487 x = MAX(x, 0);
1488 y = MAX(y, 0);
1489 x = MIN(x, width);
1490 y = MIN(y, height);
1491 w = MIN(w, width - x);
1492 h = MIN(h, height - y);
1494 if (!qemu_console_is_visible(con)) {
1495 return;
1497 QLIST_FOREACH(dcl, &s->listeners, next) {
1498 if (con != (dcl->con ? dcl->con : active_console)) {
1499 continue;
1501 if (dcl->ops->dpy_gfx_update) {
1502 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1507 void dpy_gfx_replace_surface(QemuConsole *con,
1508 DisplaySurface *surface)
1510 DisplayState *s = con->ds;
1511 DisplaySurface *old_surface = con->surface;
1512 DisplayChangeListener *dcl;
1514 con->surface = surface;
1515 QLIST_FOREACH(dcl, &s->listeners, next) {
1516 if (con != (dcl->con ? dcl->con : active_console)) {
1517 continue;
1519 if (dcl->ops->dpy_gfx_switch) {
1520 dcl->ops->dpy_gfx_switch(dcl, surface);
1523 qemu_free_displaysurface(old_surface);
1526 bool dpy_gfx_check_format(QemuConsole *con,
1527 pixman_format_code_t format)
1529 DisplayChangeListener *dcl;
1530 DisplayState *s = con->ds;
1532 QLIST_FOREACH(dcl, &s->listeners, next) {
1533 if (dcl->con && dcl->con != con) {
1534 /* dcl bound to another console -> skip */
1535 continue;
1537 if (dcl->ops->dpy_gfx_check_format) {
1538 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1539 return false;
1541 } else {
1542 /* default is to whitelist native 32 bpp only */
1543 if (format != qemu_default_pixman_format(32, true)) {
1544 return false;
1548 return true;
1551 static void dpy_refresh(DisplayState *s)
1553 DisplayChangeListener *dcl;
1555 QLIST_FOREACH(dcl, &s->listeners, next) {
1556 if (dcl->ops->dpy_refresh) {
1557 dcl->ops->dpy_refresh(dcl);
1562 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1563 int dst_x, int dst_y, int w, int h)
1565 DisplayState *s = con->ds;
1566 DisplayChangeListener *dcl;
1568 if (!qemu_console_is_visible(con)) {
1569 return;
1571 QLIST_FOREACH(dcl, &s->listeners, next) {
1572 if (con != (dcl->con ? dcl->con : active_console)) {
1573 continue;
1575 if (dcl->ops->dpy_gfx_copy) {
1576 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1577 } else { /* TODO */
1578 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1583 void dpy_text_cursor(QemuConsole *con, int x, int y)
1585 DisplayState *s = con->ds;
1586 DisplayChangeListener *dcl;
1588 if (!qemu_console_is_visible(con)) {
1589 return;
1591 QLIST_FOREACH(dcl, &s->listeners, next) {
1592 if (con != (dcl->con ? dcl->con : active_console)) {
1593 continue;
1595 if (dcl->ops->dpy_text_cursor) {
1596 dcl->ops->dpy_text_cursor(dcl, x, y);
1601 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1603 DisplayState *s = con->ds;
1604 DisplayChangeListener *dcl;
1606 if (!qemu_console_is_visible(con)) {
1607 return;
1609 QLIST_FOREACH(dcl, &s->listeners, next) {
1610 if (con != (dcl->con ? dcl->con : active_console)) {
1611 continue;
1613 if (dcl->ops->dpy_text_update) {
1614 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1619 void dpy_text_resize(QemuConsole *con, int w, int h)
1621 DisplayState *s = con->ds;
1622 DisplayChangeListener *dcl;
1624 if (!qemu_console_is_visible(con)) {
1625 return;
1627 QLIST_FOREACH(dcl, &s->listeners, next) {
1628 if (con != (dcl->con ? dcl->con : active_console)) {
1629 continue;
1631 if (dcl->ops->dpy_text_resize) {
1632 dcl->ops->dpy_text_resize(dcl, w, h);
1637 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1639 DisplayState *s = con->ds;
1640 DisplayChangeListener *dcl;
1642 if (!qemu_console_is_visible(con)) {
1643 return;
1645 QLIST_FOREACH(dcl, &s->listeners, next) {
1646 if (con != (dcl->con ? dcl->con : active_console)) {
1647 continue;
1649 if (dcl->ops->dpy_mouse_set) {
1650 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1655 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1657 DisplayState *s = con->ds;
1658 DisplayChangeListener *dcl;
1660 if (!qemu_console_is_visible(con)) {
1661 return;
1663 QLIST_FOREACH(dcl, &s->listeners, next) {
1664 if (con != (dcl->con ? dcl->con : active_console)) {
1665 continue;
1667 if (dcl->ops->dpy_cursor_define) {
1668 dcl->ops->dpy_cursor_define(dcl, cursor);
1673 bool dpy_cursor_define_supported(QemuConsole *con)
1675 DisplayState *s = con->ds;
1676 DisplayChangeListener *dcl;
1678 QLIST_FOREACH(dcl, &s->listeners, next) {
1679 if (dcl->ops->dpy_cursor_define) {
1680 return true;
1683 return false;
1686 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1687 struct QEMUGLParams *qparams)
1689 assert(con->gl);
1690 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1693 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1695 assert(con->gl);
1696 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1699 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1701 assert(con->gl);
1702 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1705 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1707 assert(con->gl);
1708 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1711 void dpy_gl_scanout(QemuConsole *con,
1712 uint32_t backing_id, bool backing_y_0_top,
1713 uint32_t backing_width, uint32_t backing_height,
1714 uint32_t x, uint32_t y, uint32_t width, uint32_t height)
1716 assert(con->gl);
1717 con->gl->ops->dpy_gl_scanout(con->gl, backing_id,
1718 backing_y_0_top,
1719 backing_width, backing_height,
1720 x, y, width, height);
1723 void dpy_gl_update(QemuConsole *con,
1724 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1726 assert(con->gl);
1727 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1730 /***********************************************************/
1731 /* register display */
1733 /* console.c internal use only */
1734 static DisplayState *get_alloc_displaystate(void)
1736 if (!display_state) {
1737 display_state = g_new0(DisplayState, 1);
1738 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1739 text_console_update_cursor, NULL);
1741 return display_state;
1745 * Called by main(), after creating QemuConsoles
1746 * and before initializing ui (sdl/vnc/...).
1748 DisplayState *init_displaystate(void)
1750 gchar *name;
1751 int i;
1753 get_alloc_displaystate();
1754 for (i = 0; i < nb_consoles; i++) {
1755 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1756 consoles[i]->ds == NULL) {
1757 text_console_do_init(consoles[i]->chr, display_state);
1760 /* Hook up into the qom tree here (not in new_console()), once
1761 * all QemuConsoles are created and the order / numbering
1762 * doesn't change any more */
1763 name = g_strdup_printf("console[%d]", i);
1764 object_property_add_child(container_get(object_get_root(), "/backend"),
1765 name, OBJECT(consoles[i]), &error_abort);
1766 g_free(name);
1769 return display_state;
1772 void graphic_console_set_hwops(QemuConsole *con,
1773 const GraphicHwOps *hw_ops,
1774 void *opaque)
1776 con->hw_ops = hw_ops;
1777 con->hw = opaque;
1780 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1781 const GraphicHwOps *hw_ops,
1782 void *opaque)
1784 static const char noinit[] =
1785 "Guest has not initialized the display (yet).";
1786 int width = 640;
1787 int height = 480;
1788 QemuConsole *s;
1789 DisplayState *ds;
1791 ds = get_alloc_displaystate();
1792 trace_console_gfx_new();
1793 s = new_console(ds, GRAPHIC_CONSOLE, head);
1794 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1795 graphic_console_set_hwops(s, hw_ops, opaque);
1796 if (dev) {
1797 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1798 &error_abort);
1801 s->surface = qemu_create_message_surface(width, height, noinit);
1802 return s;
1805 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1807 if (index >= nb_consoles) {
1808 return NULL;
1810 return consoles[index];
1813 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1815 Object *obj;
1816 uint32_t h;
1817 int i;
1819 for (i = 0; i < nb_consoles; i++) {
1820 if (!consoles[i]) {
1821 continue;
1823 obj = object_property_get_link(OBJECT(consoles[i]),
1824 "device", &error_abort);
1825 if (DEVICE(obj) != dev) {
1826 continue;
1828 h = object_property_get_int(OBJECT(consoles[i]),
1829 "head", &error_abort);
1830 if (h != head) {
1831 continue;
1833 return consoles[i];
1835 return NULL;
1838 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1839 uint32_t head, Error **errp)
1841 DeviceState *dev;
1842 QemuConsole *con;
1844 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1845 if (dev == NULL) {
1846 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1847 "Device '%s' not found", device_id);
1848 return NULL;
1851 con = qemu_console_lookup_by_device(dev, head);
1852 if (con == NULL) {
1853 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1854 device_id, head);
1855 return NULL;
1858 return con;
1861 bool qemu_console_is_visible(QemuConsole *con)
1863 return (con == active_console) || (con->dcls > 0);
1866 bool qemu_console_is_graphic(QemuConsole *con)
1868 if (con == NULL) {
1869 con = active_console;
1871 return con && (con->console_type == GRAPHIC_CONSOLE);
1874 bool qemu_console_is_fixedsize(QemuConsole *con)
1876 if (con == NULL) {
1877 con = active_console;
1879 return con && (con->console_type != TEXT_CONSOLE);
1882 char *qemu_console_get_label(QemuConsole *con)
1884 if (con->console_type == GRAPHIC_CONSOLE) {
1885 if (con->device) {
1886 return g_strdup(object_get_typename(con->device));
1888 return g_strdup("VGA");
1889 } else {
1890 if (con->chr && con->chr->label) {
1891 return g_strdup(con->chr->label);
1893 return g_strdup_printf("vc%d", con->index);
1897 int qemu_console_get_index(QemuConsole *con)
1899 if (con == NULL) {
1900 con = active_console;
1902 return con ? con->index : -1;
1905 uint32_t qemu_console_get_head(QemuConsole *con)
1907 if (con == NULL) {
1908 con = active_console;
1910 return con ? con->head : -1;
1913 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1915 assert(con != NULL);
1916 return &con->ui_info;
1919 int qemu_console_get_width(QemuConsole *con, int fallback)
1921 if (con == NULL) {
1922 con = active_console;
1924 return con ? surface_width(con->surface) : fallback;
1927 int qemu_console_get_height(QemuConsole *con, int fallback)
1929 if (con == NULL) {
1930 con = active_console;
1932 return con ? surface_height(con->surface) : fallback;
1935 static void text_console_set_echo(CharDriverState *chr, bool echo)
1937 QemuConsole *s = chr->opaque;
1939 s->echo = echo;
1942 static void text_console_update_cursor_timer(void)
1944 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1945 + CONSOLE_CURSOR_PERIOD / 2);
1948 static void text_console_update_cursor(void *opaque)
1950 QemuConsole *s;
1951 int i, count = 0;
1953 cursor_visible_phase = !cursor_visible_phase;
1955 for (i = 0; i < nb_consoles; i++) {
1956 s = consoles[i];
1957 if (qemu_console_is_graphic(s) ||
1958 !qemu_console_is_visible(s)) {
1959 continue;
1961 count++;
1962 graphic_hw_invalidate(s);
1965 if (count) {
1966 text_console_update_cursor_timer();
1970 static const GraphicHwOps text_console_ops = {
1971 .invalidate = text_console_invalidate,
1972 .text_update = text_console_update,
1975 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1977 QemuConsole *s;
1978 int g_width = 80 * FONT_WIDTH;
1979 int g_height = 24 * FONT_HEIGHT;
1981 s = chr->opaque;
1983 chr->chr_write = console_puts;
1985 s->out_fifo.buf = s->out_fifo_buf;
1986 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1987 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1988 s->ds = ds;
1990 s->y_displayed = 0;
1991 s->y_base = 0;
1992 s->total_height = DEFAULT_BACKSCROLL;
1993 s->x = 0;
1994 s->y = 0;
1995 if (!s->surface) {
1996 if (active_console && active_console->surface) {
1997 g_width = surface_width(active_console->surface);
1998 g_height = surface_height(active_console->surface);
2000 s->surface = qemu_create_displaysurface(g_width, g_height);
2003 s->hw_ops = &text_console_ops;
2004 s->hw = s;
2006 /* Set text attribute defaults */
2007 s->t_attrib_default.bold = 0;
2008 s->t_attrib_default.uline = 0;
2009 s->t_attrib_default.blink = 0;
2010 s->t_attrib_default.invers = 0;
2011 s->t_attrib_default.unvisible = 0;
2012 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2013 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2014 /* set current text attributes to default */
2015 s->t_attrib = s->t_attrib_default;
2016 text_console_resize(s);
2018 if (chr->label) {
2019 char msg[128];
2020 int len;
2022 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2023 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2024 console_puts(chr, (uint8_t*)msg, len);
2025 s->t_attrib = s->t_attrib_default;
2028 qemu_chr_be_generic_open(chr);
2029 if (chr->init)
2030 chr->init(chr);
2033 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
2035 ChardevCommon *common = qapi_ChardevVC_base(vc);
2036 CharDriverState *chr;
2037 QemuConsole *s;
2038 unsigned width = 0;
2039 unsigned height = 0;
2041 chr = qemu_chr_alloc(common, errp);
2042 if (!chr) {
2043 return NULL;
2046 if (vc->has_width) {
2047 width = vc->width;
2048 } else if (vc->has_cols) {
2049 width = vc->cols * FONT_WIDTH;
2052 if (vc->has_height) {
2053 height = vc->height;
2054 } else if (vc->has_rows) {
2055 height = vc->rows * FONT_HEIGHT;
2058 trace_console_txt_new(width, height);
2059 if (width == 0 || height == 0) {
2060 s = new_console(NULL, TEXT_CONSOLE, 0);
2061 } else {
2062 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2063 s->surface = qemu_create_displaysurface(width, height);
2066 if (!s) {
2067 g_free(chr);
2068 error_setg(errp, "cannot create text console");
2069 return NULL;
2072 s->chr = chr;
2073 chr->opaque = s;
2074 chr->chr_set_echo = text_console_set_echo;
2075 /* console/chardev init sometimes completes elsewhere in a 2nd
2076 * stage, so defer OPENED events until they are fully initialized
2078 chr->explicit_be_open = true;
2080 if (display_state) {
2081 text_console_do_init(chr, display_state);
2083 return chr;
2086 static VcHandler *vc_handler = text_console_init;
2088 static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
2089 ChardevReturn *ret, Error **errp)
2091 return vc_handler(backend->u.vc.data, errp);
2094 void register_vc_handler(VcHandler *handler)
2096 vc_handler = handler;
2099 void qemu_console_resize(QemuConsole *s, int width, int height)
2101 DisplaySurface *surface;
2103 assert(s->console_type == GRAPHIC_CONSOLE);
2104 surface = qemu_create_displaysurface(width, height);
2105 dpy_gfx_replace_surface(s, surface);
2108 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
2109 int dst_x, int dst_y, int w, int h)
2111 assert(con->console_type == GRAPHIC_CONSOLE);
2112 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
2115 DisplaySurface *qemu_console_surface(QemuConsole *console)
2117 return console->surface;
2120 PixelFormat qemu_default_pixelformat(int bpp)
2122 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2123 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2124 return pf;
2127 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2128 Error **errp)
2130 int val;
2131 ChardevVC *vc;
2133 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2134 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2136 val = qemu_opt_get_number(opts, "width", 0);
2137 if (val != 0) {
2138 vc->has_width = true;
2139 vc->width = val;
2142 val = qemu_opt_get_number(opts, "height", 0);
2143 if (val != 0) {
2144 vc->has_height = true;
2145 vc->height = val;
2148 val = qemu_opt_get_number(opts, "cols", 0);
2149 if (val != 0) {
2150 vc->has_cols = true;
2151 vc->cols = val;
2154 val = qemu_opt_get_number(opts, "rows", 0);
2155 if (val != 0) {
2156 vc->has_rows = true;
2157 vc->rows = val;
2161 static const TypeInfo qemu_console_info = {
2162 .name = TYPE_QEMU_CONSOLE,
2163 .parent = TYPE_OBJECT,
2164 .instance_size = sizeof(QemuConsole),
2165 .class_size = sizeof(QemuConsoleClass),
2169 static void register_types(void)
2171 type_register_static(&qemu_console_info);
2172 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc,
2173 vc_init);
2176 type_init(register_types);