Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / ui / console.c
blobf5d1a270fa8d87f8ece1d2a01eb6cd0ca3a29283
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 "chardev/char-fe.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;
128 int window_id;
130 /* Graphic console state. */
131 Object *device;
132 uint32_t head;
133 QemuUIInfo ui_info;
134 QEMUTimer *ui_timer;
135 const GraphicHwOps *hw_ops;
136 void *hw;
138 /* Text console state */
139 int width;
140 int height;
141 int total_height;
142 int backscroll_height;
143 int x, y;
144 int x_saved, y_saved;
145 int y_displayed;
146 int y_base;
147 TextAttributes t_attrib_default; /* default text attributes */
148 TextAttributes t_attrib; /* currently active text attributes */
149 TextCell *cells;
150 int text_x[2], text_y[2], cursor_invalidate;
151 int echo;
153 int update_x0;
154 int update_y0;
155 int update_x1;
156 int update_y1;
158 enum TTYState state;
159 int esc_params[MAX_ESC_PARAMS];
160 int nb_esc_params;
162 Chardev *chr;
163 /* fifo for key pressed */
164 QEMUFIFO out_fifo;
165 uint8_t out_fifo_buf[16];
166 QEMUTimer *kbd_timer;
169 struct DisplayState {
170 QEMUTimer *gui_timer;
171 uint64_t last_update;
172 uint64_t update_interval;
173 bool refreshing;
174 bool have_gfx;
175 bool have_text;
177 QLIST_HEAD(, DisplayChangeListener) listeners;
180 static DisplayState *display_state;
181 static QemuConsole *active_console;
182 static QemuConsole **consoles;
183 static int nb_consoles = 0;
184 static bool cursor_visible_phase;
185 static QEMUTimer *cursor_timer;
187 static void text_console_do_init(Chardev *chr, DisplayState *ds);
188 static void dpy_refresh(DisplayState *s);
189 static DisplayState *get_alloc_displaystate(void);
190 static void text_console_update_cursor_timer(void);
191 static void text_console_update_cursor(void *opaque);
193 static void gui_update(void *opaque)
195 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
196 uint64_t dcl_interval;
197 DisplayState *ds = opaque;
198 DisplayChangeListener *dcl;
199 int i;
201 ds->refreshing = true;
202 dpy_refresh(ds);
203 ds->refreshing = false;
205 QLIST_FOREACH(dcl, &ds->listeners, next) {
206 dcl_interval = dcl->update_interval ?
207 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
208 if (interval > dcl_interval) {
209 interval = dcl_interval;
212 if (ds->update_interval != interval) {
213 ds->update_interval = interval;
214 for (i = 0; i < nb_consoles; i++) {
215 if (consoles[i]->hw_ops->update_interval) {
216 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
219 trace_console_refresh(interval);
221 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
222 timer_mod(ds->gui_timer, ds->last_update + interval);
225 static void gui_setup_refresh(DisplayState *ds)
227 DisplayChangeListener *dcl;
228 bool need_timer = false;
229 bool have_gfx = false;
230 bool have_text = false;
232 QLIST_FOREACH(dcl, &ds->listeners, next) {
233 if (dcl->ops->dpy_refresh != NULL) {
234 need_timer = true;
236 if (dcl->ops->dpy_gfx_update != NULL) {
237 have_gfx = true;
239 if (dcl->ops->dpy_text_update != NULL) {
240 have_text = true;
244 if (need_timer && ds->gui_timer == NULL) {
245 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
246 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
248 if (!need_timer && ds->gui_timer != NULL) {
249 timer_del(ds->gui_timer);
250 timer_free(ds->gui_timer);
251 ds->gui_timer = NULL;
254 ds->have_gfx = have_gfx;
255 ds->have_text = have_text;
258 void graphic_hw_update(QemuConsole *con)
260 if (!con) {
261 con = active_console;
263 if (con && con->hw_ops->gfx_update) {
264 con->hw_ops->gfx_update(con->hw);
268 void graphic_hw_gl_block(QemuConsole *con, bool block)
270 assert(con != NULL);
272 con->gl_block = block;
273 if (con->hw_ops->gl_block) {
274 con->hw_ops->gl_block(con->hw, block);
278 int qemu_console_get_window_id(QemuConsole *con)
280 return con->window_id;
283 void qemu_console_set_window_id(QemuConsole *con, int window_id)
285 con->window_id = window_id;
288 void graphic_hw_invalidate(QemuConsole *con)
290 if (!con) {
291 con = active_console;
293 if (con && con->hw_ops->invalidate) {
294 con->hw_ops->invalidate(con->hw);
298 static void ppm_save(const char *filename, DisplaySurface *ds,
299 Error **errp)
301 int width = pixman_image_get_width(ds->image);
302 int height = pixman_image_get_height(ds->image);
303 int fd;
304 FILE *f;
305 int y;
306 int ret;
307 pixman_image_t *linebuf;
309 trace_ppm_save(filename, ds);
310 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
311 if (fd == -1) {
312 error_setg(errp, "failed to open file '%s': %s", filename,
313 strerror(errno));
314 return;
316 f = fdopen(fd, "wb");
317 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
318 if (ret < 0) {
319 linebuf = NULL;
320 goto write_err;
322 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
323 for (y = 0; y < height; y++) {
324 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
325 clearerr(f);
326 ret = fwrite(pixman_image_get_data(linebuf), 1,
327 pixman_image_get_stride(linebuf), f);
328 (void)ret;
329 if (ferror(f)) {
330 goto write_err;
334 out:
335 qemu_pixman_image_unref(linebuf);
336 fclose(f);
337 return;
339 write_err:
340 error_setg(errp, "failed to write to file '%s': %s", filename,
341 strerror(errno));
342 unlink(filename);
343 goto out;
346 void qmp_screendump(const char *filename, Error **errp)
348 QemuConsole *con = qemu_console_lookup_by_index(0);
349 DisplaySurface *surface;
351 if (con == NULL) {
352 error_setg(errp, "There is no QemuConsole I can screendump from.");
353 return;
356 graphic_hw_update(con);
357 surface = qemu_console_surface(con);
358 ppm_save(filename, surface, errp);
361 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
363 if (!con) {
364 con = active_console;
366 if (con && con->hw_ops->text_update) {
367 con->hw_ops->text_update(con->hw, chardata);
371 static void vga_fill_rect(QemuConsole *con,
372 int posx, int posy, int width, int height,
373 pixman_color_t color)
375 DisplaySurface *surface = qemu_console_surface(con);
376 pixman_rectangle16_t rect = {
377 .x = posx, .y = posy, .width = width, .height = height
380 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
381 &color, 1, &rect);
384 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
385 static void vga_bitblt(QemuConsole *con,
386 int xs, int ys, int xd, int yd, int w, int h)
388 DisplaySurface *surface = qemu_console_surface(con);
390 pixman_image_composite(PIXMAN_OP_SRC,
391 surface->image, NULL, surface->image,
392 xs, ys, 0, 0, xd, yd, w, h);
395 /***********************************************************/
396 /* basic char display */
398 #define FONT_HEIGHT 16
399 #define FONT_WIDTH 8
401 #include "vgafont.h"
403 #define QEMU_RGB(r, g, b) \
404 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
406 static const pixman_color_t color_table_rgb[2][8] = {
407 { /* dark */
408 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
409 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
410 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
411 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
412 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
413 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
414 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
415 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
417 { /* bright */
418 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
419 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
420 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
421 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
422 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
423 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
424 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
425 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
429 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
430 TextAttributes *t_attrib)
432 static pixman_image_t *glyphs[256];
433 DisplaySurface *surface = qemu_console_surface(s);
434 pixman_color_t fgcol, bgcol;
436 if (t_attrib->invers) {
437 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
438 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
439 } else {
440 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
441 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
444 if (!glyphs[ch]) {
445 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
447 qemu_pixman_glyph_render(glyphs[ch], surface->image,
448 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
451 static void text_console_resize(QemuConsole *s)
453 TextCell *cells, *c, *c1;
454 int w1, x, y, last_width;
456 last_width = s->width;
457 s->width = surface_width(s->surface) / FONT_WIDTH;
458 s->height = surface_height(s->surface) / FONT_HEIGHT;
460 w1 = last_width;
461 if (s->width < w1)
462 w1 = s->width;
464 cells = g_new(TextCell, s->width * s->total_height);
465 for(y = 0; y < s->total_height; y++) {
466 c = &cells[y * s->width];
467 if (w1 > 0) {
468 c1 = &s->cells[y * last_width];
469 for(x = 0; x < w1; x++) {
470 *c++ = *c1++;
473 for(x = w1; x < s->width; x++) {
474 c->ch = ' ';
475 c->t_attrib = s->t_attrib_default;
476 c++;
479 g_free(s->cells);
480 s->cells = cells;
483 static inline void text_update_xy(QemuConsole *s, int x, int y)
485 s->text_x[0] = MIN(s->text_x[0], x);
486 s->text_x[1] = MAX(s->text_x[1], x);
487 s->text_y[0] = MIN(s->text_y[0], y);
488 s->text_y[1] = MAX(s->text_y[1], y);
491 static void invalidate_xy(QemuConsole *s, int x, int y)
493 if (!qemu_console_is_visible(s)) {
494 return;
496 if (s->update_x0 > x * FONT_WIDTH)
497 s->update_x0 = x * FONT_WIDTH;
498 if (s->update_y0 > y * FONT_HEIGHT)
499 s->update_y0 = y * FONT_HEIGHT;
500 if (s->update_x1 < (x + 1) * FONT_WIDTH)
501 s->update_x1 = (x + 1) * FONT_WIDTH;
502 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
503 s->update_y1 = (y + 1) * FONT_HEIGHT;
506 static void update_xy(QemuConsole *s, int x, int y)
508 TextCell *c;
509 int y1, y2;
511 if (s->ds->have_text) {
512 text_update_xy(s, x, y);
515 y1 = (s->y_base + y) % s->total_height;
516 y2 = y1 - s->y_displayed;
517 if (y2 < 0) {
518 y2 += s->total_height;
520 if (y2 < s->height) {
521 c = &s->cells[y1 * s->width + x];
522 vga_putcharxy(s, x, y2, c->ch,
523 &(c->t_attrib));
524 invalidate_xy(s, x, y2);
528 static void console_show_cursor(QemuConsole *s, int show)
530 TextCell *c;
531 int y, y1;
532 int x = s->x;
534 if (s->ds->have_text) {
535 s->cursor_invalidate = 1;
538 if (x >= s->width) {
539 x = s->width - 1;
541 y1 = (s->y_base + s->y) % s->total_height;
542 y = y1 - s->y_displayed;
543 if (y < 0) {
544 y += s->total_height;
546 if (y < s->height) {
547 c = &s->cells[y1 * s->width + x];
548 if (show && cursor_visible_phase) {
549 TextAttributes t_attrib = s->t_attrib_default;
550 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
551 vga_putcharxy(s, x, y, c->ch, &t_attrib);
552 } else {
553 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
555 invalidate_xy(s, x, y);
559 static void console_refresh(QemuConsole *s)
561 DisplaySurface *surface = qemu_console_surface(s);
562 TextCell *c;
563 int x, y, y1;
565 if (s->ds->have_text) {
566 s->text_x[0] = 0;
567 s->text_y[0] = 0;
568 s->text_x[1] = s->width - 1;
569 s->text_y[1] = s->height - 1;
570 s->cursor_invalidate = 1;
573 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
574 color_table_rgb[0][QEMU_COLOR_BLACK]);
575 y1 = s->y_displayed;
576 for (y = 0; y < s->height; y++) {
577 c = s->cells + y1 * s->width;
578 for (x = 0; x < s->width; x++) {
579 vga_putcharxy(s, x, y, c->ch,
580 &(c->t_attrib));
581 c++;
583 if (++y1 == s->total_height) {
584 y1 = 0;
587 console_show_cursor(s, 1);
588 dpy_gfx_update(s, 0, 0,
589 surface_width(surface), surface_height(surface));
592 static void console_scroll(QemuConsole *s, int ydelta)
594 int i, y1;
596 if (ydelta > 0) {
597 for(i = 0; i < ydelta; i++) {
598 if (s->y_displayed == s->y_base)
599 break;
600 if (++s->y_displayed == s->total_height)
601 s->y_displayed = 0;
603 } else {
604 ydelta = -ydelta;
605 i = s->backscroll_height;
606 if (i > s->total_height - s->height)
607 i = s->total_height - s->height;
608 y1 = s->y_base - i;
609 if (y1 < 0)
610 y1 += s->total_height;
611 for(i = 0; i < ydelta; i++) {
612 if (s->y_displayed == y1)
613 break;
614 if (--s->y_displayed < 0)
615 s->y_displayed = s->total_height - 1;
618 console_refresh(s);
621 static void console_put_lf(QemuConsole *s)
623 TextCell *c;
624 int x, y1;
626 s->y++;
627 if (s->y >= s->height) {
628 s->y = s->height - 1;
630 if (s->y_displayed == s->y_base) {
631 if (++s->y_displayed == s->total_height)
632 s->y_displayed = 0;
634 if (++s->y_base == s->total_height)
635 s->y_base = 0;
636 if (s->backscroll_height < s->total_height)
637 s->backscroll_height++;
638 y1 = (s->y_base + s->height - 1) % s->total_height;
639 c = &s->cells[y1 * s->width];
640 for(x = 0; x < s->width; x++) {
641 c->ch = ' ';
642 c->t_attrib = s->t_attrib_default;
643 c++;
645 if (s->y_displayed == s->y_base) {
646 if (s->ds->have_text) {
647 s->text_x[0] = 0;
648 s->text_y[0] = 0;
649 s->text_x[1] = s->width - 1;
650 s->text_y[1] = s->height - 1;
653 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
654 s->width * FONT_WIDTH,
655 (s->height - 1) * FONT_HEIGHT);
656 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
657 s->width * FONT_WIDTH, FONT_HEIGHT,
658 color_table_rgb[0][s->t_attrib_default.bgcol]);
659 s->update_x0 = 0;
660 s->update_y0 = 0;
661 s->update_x1 = s->width * FONT_WIDTH;
662 s->update_y1 = s->height * FONT_HEIGHT;
667 /* Set console attributes depending on the current escape codes.
668 * NOTE: I know this code is not very efficient (checking every color for it
669 * self) but it is more readable and better maintainable.
671 static void console_handle_escape(QemuConsole *s)
673 int i;
675 for (i=0; i<s->nb_esc_params; i++) {
676 switch (s->esc_params[i]) {
677 case 0: /* reset all console attributes to default */
678 s->t_attrib = s->t_attrib_default;
679 break;
680 case 1:
681 s->t_attrib.bold = 1;
682 break;
683 case 4:
684 s->t_attrib.uline = 1;
685 break;
686 case 5:
687 s->t_attrib.blink = 1;
688 break;
689 case 7:
690 s->t_attrib.invers = 1;
691 break;
692 case 8:
693 s->t_attrib.unvisible = 1;
694 break;
695 case 22:
696 s->t_attrib.bold = 0;
697 break;
698 case 24:
699 s->t_attrib.uline = 0;
700 break;
701 case 25:
702 s->t_attrib.blink = 0;
703 break;
704 case 27:
705 s->t_attrib.invers = 0;
706 break;
707 case 28:
708 s->t_attrib.unvisible = 0;
709 break;
710 /* set foreground color */
711 case 30:
712 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
713 break;
714 case 31:
715 s->t_attrib.fgcol = QEMU_COLOR_RED;
716 break;
717 case 32:
718 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
719 break;
720 case 33:
721 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
722 break;
723 case 34:
724 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
725 break;
726 case 35:
727 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
728 break;
729 case 36:
730 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
731 break;
732 case 37:
733 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
734 break;
735 /* set background color */
736 case 40:
737 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
738 break;
739 case 41:
740 s->t_attrib.bgcol = QEMU_COLOR_RED;
741 break;
742 case 42:
743 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
744 break;
745 case 43:
746 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
747 break;
748 case 44:
749 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
750 break;
751 case 45:
752 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
753 break;
754 case 46:
755 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
756 break;
757 case 47:
758 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
759 break;
764 static void console_clear_xy(QemuConsole *s, int x, int y)
766 int y1 = (s->y_base + y) % s->total_height;
767 TextCell *c = &s->cells[y1 * s->width + x];
768 c->ch = ' ';
769 c->t_attrib = s->t_attrib_default;
770 update_xy(s, x, y);
773 static void console_put_one(QemuConsole *s, int ch)
775 TextCell *c;
776 int y1;
777 if (s->x >= s->width) {
778 /* line wrap */
779 s->x = 0;
780 console_put_lf(s);
782 y1 = (s->y_base + s->y) % s->total_height;
783 c = &s->cells[y1 * s->width + s->x];
784 c->ch = ch;
785 c->t_attrib = s->t_attrib;
786 update_xy(s, s->x, s->y);
787 s->x++;
790 static void console_respond_str(QemuConsole *s, const char *buf)
792 while (*buf) {
793 console_put_one(s, *buf);
794 buf++;
798 /* set cursor, checking bounds */
799 static void set_cursor(QemuConsole *s, int x, int y)
801 if (x < 0) {
802 x = 0;
804 if (y < 0) {
805 y = 0;
807 if (y >= s->height) {
808 y = s->height - 1;
810 if (x >= s->width) {
811 x = s->width - 1;
814 s->x = x;
815 s->y = y;
818 static void console_putchar(QemuConsole *s, int ch)
820 int i;
821 int x, y;
822 char response[40];
824 switch(s->state) {
825 case TTY_STATE_NORM:
826 switch(ch) {
827 case '\r': /* carriage return */
828 s->x = 0;
829 break;
830 case '\n': /* newline */
831 console_put_lf(s);
832 break;
833 case '\b': /* backspace */
834 if (s->x > 0)
835 s->x--;
836 break;
837 case '\t': /* tabspace */
838 if (s->x + (8 - (s->x % 8)) > s->width) {
839 s->x = 0;
840 console_put_lf(s);
841 } else {
842 s->x = s->x + (8 - (s->x % 8));
844 break;
845 case '\a': /* alert aka. bell */
846 /* TODO: has to be implemented */
847 break;
848 case 14:
849 /* SI (shift in), character set 0 (ignored) */
850 break;
851 case 15:
852 /* SO (shift out), character set 1 (ignored) */
853 break;
854 case 27: /* esc (introducing an escape sequence) */
855 s->state = TTY_STATE_ESC;
856 break;
857 default:
858 console_put_one(s, ch);
859 break;
861 break;
862 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
863 if (ch == '[') {
864 for(i=0;i<MAX_ESC_PARAMS;i++)
865 s->esc_params[i] = 0;
866 s->nb_esc_params = 0;
867 s->state = TTY_STATE_CSI;
868 } else {
869 s->state = TTY_STATE_NORM;
871 break;
872 case TTY_STATE_CSI: /* handle escape sequence parameters */
873 if (ch >= '0' && ch <= '9') {
874 if (s->nb_esc_params < MAX_ESC_PARAMS) {
875 int *param = &s->esc_params[s->nb_esc_params];
876 int digit = (ch - '0');
878 *param = (*param <= (INT_MAX - digit) / 10) ?
879 *param * 10 + digit : INT_MAX;
881 } else {
882 if (s->nb_esc_params < MAX_ESC_PARAMS)
883 s->nb_esc_params++;
884 if (ch == ';' || ch == '?') {
885 break;
887 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
888 ch, s->nb_esc_params);
889 s->state = TTY_STATE_NORM;
890 switch(ch) {
891 case 'A':
892 /* move cursor up */
893 if (s->esc_params[0] == 0) {
894 s->esc_params[0] = 1;
896 set_cursor(s, s->x, s->y - s->esc_params[0]);
897 break;
898 case 'B':
899 /* move cursor down */
900 if (s->esc_params[0] == 0) {
901 s->esc_params[0] = 1;
903 set_cursor(s, s->x, s->y + s->esc_params[0]);
904 break;
905 case 'C':
906 /* move cursor right */
907 if (s->esc_params[0] == 0) {
908 s->esc_params[0] = 1;
910 set_cursor(s, s->x + s->esc_params[0], s->y);
911 break;
912 case 'D':
913 /* move cursor left */
914 if (s->esc_params[0] == 0) {
915 s->esc_params[0] = 1;
917 set_cursor(s, s->x - s->esc_params[0], s->y);
918 break;
919 case 'G':
920 /* move cursor to column */
921 set_cursor(s, s->esc_params[0] - 1, s->y);
922 break;
923 case 'f':
924 case 'H':
925 /* move cursor to row, column */
926 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
927 break;
928 case 'J':
929 switch (s->esc_params[0]) {
930 case 0:
931 /* clear to end of screen */
932 for (y = s->y; y < s->height; y++) {
933 for (x = 0; x < s->width; x++) {
934 if (y == s->y && x < s->x) {
935 continue;
937 console_clear_xy(s, x, y);
940 break;
941 case 1:
942 /* clear from beginning of screen */
943 for (y = 0; y <= s->y; y++) {
944 for (x = 0; x < s->width; x++) {
945 if (y == s->y && x > s->x) {
946 break;
948 console_clear_xy(s, x, y);
951 break;
952 case 2:
953 /* clear entire screen */
954 for (y = 0; y <= s->height; y++) {
955 for (x = 0; x < s->width; x++) {
956 console_clear_xy(s, x, y);
959 break;
961 break;
962 case 'K':
963 switch (s->esc_params[0]) {
964 case 0:
965 /* clear to eol */
966 for (x = s->x; x < s->width; x++) {
967 console_clear_xy(s, x, s->y);
969 break;
970 case 1:
971 /* clear from beginning of line */
972 for (x = 0; x <= s->x; x++) {
973 console_clear_xy(s, x, s->y);
975 break;
976 case 2:
977 /* clear entire line */
978 for(x = 0; x < s->width; x++) {
979 console_clear_xy(s, x, s->y);
981 break;
983 break;
984 case 'm':
985 console_handle_escape(s);
986 break;
987 case 'n':
988 switch (s->esc_params[0]) {
989 case 5:
990 /* report console status (always succeed)*/
991 console_respond_str(s, "\033[0n");
992 break;
993 case 6:
994 /* report cursor position */
995 sprintf(response, "\033[%d;%dR",
996 (s->y_base + s->y) % s->total_height + 1,
997 s->x + 1);
998 console_respond_str(s, response);
999 break;
1001 break;
1002 case 's':
1003 /* save cursor position */
1004 s->x_saved = s->x;
1005 s->y_saved = s->y;
1006 break;
1007 case 'u':
1008 /* restore cursor position */
1009 s->x = s->x_saved;
1010 s->y = s->y_saved;
1011 break;
1012 default:
1013 trace_console_putchar_unhandled(ch);
1014 break;
1016 break;
1021 void console_select(unsigned int index)
1023 DisplayChangeListener *dcl;
1024 QemuConsole *s;
1026 trace_console_select(index);
1027 s = qemu_console_lookup_by_index(index);
1028 if (s) {
1029 DisplayState *ds = s->ds;
1031 active_console = s;
1032 if (ds->have_gfx) {
1033 QLIST_FOREACH(dcl, &ds->listeners, next) {
1034 if (dcl->con != NULL) {
1035 continue;
1037 if (dcl->ops->dpy_gfx_switch) {
1038 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1041 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1042 surface_height(s->surface));
1044 if (ds->have_text) {
1045 dpy_text_resize(s, s->width, s->height);
1047 text_console_update_cursor(NULL);
1051 typedef struct VCChardev {
1052 Chardev parent;
1053 QemuConsole *console;
1054 } VCChardev;
1056 #define TYPE_CHARDEV_VC "chardev-vc"
1057 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1059 static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
1061 VCChardev *drv = VC_CHARDEV(chr);
1062 QemuConsole *s = drv->console;
1063 int i;
1065 if (!s->ds) {
1066 return 0;
1069 s->update_x0 = s->width * FONT_WIDTH;
1070 s->update_y0 = s->height * FONT_HEIGHT;
1071 s->update_x1 = 0;
1072 s->update_y1 = 0;
1073 console_show_cursor(s, 0);
1074 for(i = 0; i < len; i++) {
1075 console_putchar(s, buf[i]);
1077 console_show_cursor(s, 1);
1078 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1079 dpy_gfx_update(s, s->update_x0, s->update_y0,
1080 s->update_x1 - s->update_x0,
1081 s->update_y1 - s->update_y0);
1083 return len;
1086 static void kbd_send_chars(void *opaque)
1088 QemuConsole *s = opaque;
1089 int len;
1090 uint8_t buf[16];
1092 len = qemu_chr_be_can_write(s->chr);
1093 if (len > s->out_fifo.count)
1094 len = s->out_fifo.count;
1095 if (len > 0) {
1096 if (len > sizeof(buf))
1097 len = sizeof(buf);
1098 qemu_fifo_read(&s->out_fifo, buf, len);
1099 qemu_chr_be_write(s->chr, buf, len);
1101 /* characters are pending: we send them a bit later (XXX:
1102 horrible, should change char device API) */
1103 if (s->out_fifo.count > 0) {
1104 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1108 /* called when an ascii key is pressed */
1109 void kbd_put_keysym_console(QemuConsole *s, int keysym)
1111 uint8_t buf[16], *q;
1112 CharBackend *be;
1113 int c;
1115 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1116 return;
1118 switch(keysym) {
1119 case QEMU_KEY_CTRL_UP:
1120 console_scroll(s, -1);
1121 break;
1122 case QEMU_KEY_CTRL_DOWN:
1123 console_scroll(s, 1);
1124 break;
1125 case QEMU_KEY_CTRL_PAGEUP:
1126 console_scroll(s, -10);
1127 break;
1128 case QEMU_KEY_CTRL_PAGEDOWN:
1129 console_scroll(s, 10);
1130 break;
1131 default:
1132 /* convert the QEMU keysym to VT100 key string */
1133 q = buf;
1134 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1135 *q++ = '\033';
1136 *q++ = '[';
1137 c = keysym - 0xe100;
1138 if (c >= 10)
1139 *q++ = '0' + (c / 10);
1140 *q++ = '0' + (c % 10);
1141 *q++ = '~';
1142 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1143 *q++ = '\033';
1144 *q++ = '[';
1145 *q++ = keysym & 0xff;
1146 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1147 vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
1148 *q++ = '\n';
1149 } else {
1150 *q++ = keysym;
1152 if (s->echo) {
1153 vc_chr_write(s->chr, buf, q - buf);
1155 be = s->chr->be;
1156 if (be && be->chr_read) {
1157 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1158 kbd_send_chars(s);
1160 break;
1164 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
1165 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1166 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1167 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1168 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1169 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1170 [Q_KEY_CODE_END] = QEMU_KEY_END,
1171 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1172 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1173 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1174 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
1177 bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1179 int keysym;
1181 keysym = qcode_to_keysym[qcode];
1182 if (keysym == 0) {
1183 return false;
1185 kbd_put_keysym_console(s, keysym);
1186 return true;
1189 void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1191 int i;
1193 for (i = 0; i < len && str[i]; i++) {
1194 kbd_put_keysym_console(s, str[i]);
1198 void kbd_put_keysym(int keysym)
1200 kbd_put_keysym_console(active_console, keysym);
1203 static void text_console_invalidate(void *opaque)
1205 QemuConsole *s = (QemuConsole *) opaque;
1207 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1208 text_console_resize(s);
1210 console_refresh(s);
1213 static void text_console_update(void *opaque, console_ch_t *chardata)
1215 QemuConsole *s = (QemuConsole *) opaque;
1216 int i, j, src;
1218 if (s->text_x[0] <= s->text_x[1]) {
1219 src = (s->y_base + s->text_y[0]) * s->width;
1220 chardata += s->text_y[0] * s->width;
1221 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1222 for (j = 0; j < s->width; j++, src++) {
1223 console_write_ch(chardata ++,
1224 ATTR2CHTYPE(s->cells[src].ch,
1225 s->cells[src].t_attrib.fgcol,
1226 s->cells[src].t_attrib.bgcol,
1227 s->cells[src].t_attrib.bold));
1229 dpy_text_update(s, s->text_x[0], s->text_y[0],
1230 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1231 s->text_x[0] = s->width;
1232 s->text_y[0] = s->height;
1233 s->text_x[1] = 0;
1234 s->text_y[1] = 0;
1236 if (s->cursor_invalidate) {
1237 dpy_text_cursor(s, s->x, s->y);
1238 s->cursor_invalidate = 0;
1242 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1243 uint32_t head)
1245 Object *obj;
1246 QemuConsole *s;
1247 int i;
1249 obj = object_new(TYPE_QEMU_CONSOLE);
1250 s = QEMU_CONSOLE(obj);
1251 s->head = head;
1252 object_property_add_link(obj, "device", TYPE_DEVICE,
1253 (Object **)&s->device,
1254 object_property_allow_set_link,
1255 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1256 &error_abort);
1257 object_property_add_uint32_ptr(obj, "head",
1258 &s->head, &error_abort);
1260 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1261 (console_type == GRAPHIC_CONSOLE))) {
1262 active_console = s;
1264 s->ds = ds;
1265 s->console_type = console_type;
1267 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
1268 if (console_type != GRAPHIC_CONSOLE) {
1269 s->index = nb_consoles;
1270 consoles[nb_consoles++] = s;
1271 } else {
1272 /* HACK: Put graphical consoles before text consoles. */
1273 for (i = nb_consoles; i > 0; i--) {
1274 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1275 break;
1276 consoles[i] = consoles[i - 1];
1277 consoles[i]->index = i;
1279 s->index = i;
1280 consoles[i] = s;
1281 nb_consoles++;
1283 return s;
1286 static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
1288 qemu_pixman_image_unref(surface->image);
1289 surface->image = NULL;
1291 surface->format = PIXMAN_x8r8g8b8;
1292 surface->image = pixman_image_create_bits(surface->format,
1293 width, height,
1294 NULL, width * 4);
1295 assert(surface->image != NULL);
1297 surface->flags = QEMU_ALLOCATED_FLAG;
1300 DisplaySurface *qemu_create_displaysurface(int width, int height)
1302 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1304 trace_displaysurface_create(surface, width, height);
1305 qemu_alloc_display(surface, width, height);
1306 return surface;
1309 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1310 pixman_format_code_t format,
1311 int linesize, uint8_t *data)
1313 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1315 trace_displaysurface_create_from(surface, width, height, format);
1316 surface->format = format;
1317 surface->image = pixman_image_create_bits(surface->format,
1318 width, height,
1319 (void *)data, linesize);
1320 assert(surface->image != NULL);
1322 return surface;
1325 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1327 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1329 trace_displaysurface_create_pixman(surface);
1330 surface->format = pixman_image_get_format(image);
1331 surface->image = pixman_image_ref(image);
1333 return surface;
1336 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1337 void *unused)
1339 void *data = pixman_image_get_data(image);
1340 uint32_t size = pixman_image_get_stride(image) *
1341 pixman_image_get_height(image);
1342 cpu_physical_memory_unmap(data, size, 0, 0);
1345 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1346 pixman_format_code_t format,
1347 int linesize, uint64_t addr)
1349 DisplaySurface *surface;
1350 hwaddr size;
1351 void *data;
1353 if (linesize == 0) {
1354 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1357 size = (hwaddr)linesize * height;
1358 data = cpu_physical_memory_map(addr, &size, 0);
1359 if (size != (hwaddr)linesize * height) {
1360 cpu_physical_memory_unmap(data, size, 0, 0);
1361 return NULL;
1364 surface = qemu_create_displaysurface_from
1365 (width, height, format, linesize, data);
1366 pixman_image_set_destroy_function
1367 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1369 return surface;
1372 static DisplaySurface *qemu_create_message_surface(int w, int h,
1373 const char *msg)
1375 DisplaySurface *surface = qemu_create_displaysurface(w, h);
1376 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1377 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
1378 pixman_image_t *glyph;
1379 int len, x, y, i;
1381 len = strlen(msg);
1382 x = (w / FONT_WIDTH - len) / 2;
1383 y = (h / FONT_HEIGHT - 1) / 2;
1384 for (i = 0; i < len; i++) {
1385 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1386 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1387 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1388 qemu_pixman_image_unref(glyph);
1390 return surface;
1393 void qemu_free_displaysurface(DisplaySurface *surface)
1395 if (surface == NULL) {
1396 return;
1398 trace_displaysurface_free(surface);
1399 qemu_pixman_image_unref(surface->image);
1400 g_free(surface);
1403 bool console_has_gl(QemuConsole *con)
1405 return con->gl != NULL;
1408 bool console_has_gl_dmabuf(QemuConsole *con)
1410 return con->gl != NULL && con->gl->ops->dpy_gl_scanout_dmabuf != NULL;
1413 void register_displaychangelistener(DisplayChangeListener *dcl)
1415 static const char nodev[] =
1416 "This VM has no graphic display device.";
1417 static DisplaySurface *dummy;
1418 QemuConsole *con;
1420 assert(!dcl->ds);
1422 if (dcl->ops->dpy_gl_ctx_create) {
1423 /* display has opengl support */
1424 assert(dcl->con);
1425 if (dcl->con->gl) {
1426 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1427 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1428 exit(1);
1430 dcl->con->gl = dcl;
1433 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1434 dcl->ds = get_alloc_displaystate();
1435 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1436 gui_setup_refresh(dcl->ds);
1437 if (dcl->con) {
1438 dcl->con->dcls++;
1439 con = dcl->con;
1440 } else {
1441 con = active_console;
1443 if (dcl->ops->dpy_gfx_switch) {
1444 if (con) {
1445 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1446 } else {
1447 if (!dummy) {
1448 dummy = qemu_create_message_surface(640, 480, nodev);
1450 dcl->ops->dpy_gfx_switch(dcl, dummy);
1453 text_console_update_cursor(NULL);
1456 void update_displaychangelistener(DisplayChangeListener *dcl,
1457 uint64_t interval)
1459 DisplayState *ds = dcl->ds;
1461 dcl->update_interval = interval;
1462 if (!ds->refreshing && ds->update_interval > interval) {
1463 timer_mod(ds->gui_timer, ds->last_update + interval);
1467 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1469 DisplayState *ds = dcl->ds;
1470 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1471 if (dcl->con) {
1472 dcl->con->dcls--;
1474 QLIST_REMOVE(dcl, next);
1475 dcl->ds = NULL;
1476 gui_setup_refresh(ds);
1479 static void dpy_set_ui_info_timer(void *opaque)
1481 QemuConsole *con = opaque;
1483 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1486 bool dpy_ui_info_supported(QemuConsole *con)
1488 return con->hw_ops->ui_info != NULL;
1491 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1493 assert(con != NULL);
1495 if (!dpy_ui_info_supported(con)) {
1496 return -1;
1498 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1499 /* nothing changed -- ignore */
1500 return 0;
1504 * Typically we get a flood of these as the user resizes the window.
1505 * Wait until the dust has settled (one second without updates), then
1506 * go notify the guest.
1508 con->ui_info = *info;
1509 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1510 return 0;
1513 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1515 DisplayState *s = con->ds;
1516 DisplayChangeListener *dcl;
1517 int width = w;
1518 int height = h;
1520 if (con->surface) {
1521 width = surface_width(con->surface);
1522 height = surface_height(con->surface);
1524 x = MAX(x, 0);
1525 y = MAX(y, 0);
1526 x = MIN(x, width);
1527 y = MIN(y, height);
1528 w = MIN(w, width - x);
1529 h = MIN(h, height - y);
1531 if (!qemu_console_is_visible(con)) {
1532 return;
1534 QLIST_FOREACH(dcl, &s->listeners, next) {
1535 if (con != (dcl->con ? dcl->con : active_console)) {
1536 continue;
1538 if (dcl->ops->dpy_gfx_update) {
1539 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1544 void dpy_gfx_replace_surface(QemuConsole *con,
1545 DisplaySurface *surface)
1547 DisplayState *s = con->ds;
1548 DisplaySurface *old_surface = con->surface;
1549 DisplayChangeListener *dcl;
1551 assert(old_surface != surface || surface == NULL);
1553 con->surface = surface;
1554 QLIST_FOREACH(dcl, &s->listeners, next) {
1555 if (con != (dcl->con ? dcl->con : active_console)) {
1556 continue;
1558 if (dcl->ops->dpy_gfx_switch) {
1559 dcl->ops->dpy_gfx_switch(dcl, surface);
1562 qemu_free_displaysurface(old_surface);
1565 bool dpy_gfx_check_format(QemuConsole *con,
1566 pixman_format_code_t format)
1568 DisplayChangeListener *dcl;
1569 DisplayState *s = con->ds;
1571 QLIST_FOREACH(dcl, &s->listeners, next) {
1572 if (dcl->con && dcl->con != con) {
1573 /* dcl bound to another console -> skip */
1574 continue;
1576 if (dcl->ops->dpy_gfx_check_format) {
1577 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1578 return false;
1580 } else {
1581 /* default is to whitelist native 32 bpp only */
1582 if (format != qemu_default_pixman_format(32, true)) {
1583 return false;
1587 return true;
1590 static void dpy_refresh(DisplayState *s)
1592 DisplayChangeListener *dcl;
1594 QLIST_FOREACH(dcl, &s->listeners, next) {
1595 if (dcl->ops->dpy_refresh) {
1596 dcl->ops->dpy_refresh(dcl);
1601 void dpy_text_cursor(QemuConsole *con, int x, int y)
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_cursor) {
1614 dcl->ops->dpy_text_cursor(dcl, x, y);
1619 void dpy_text_update(QemuConsole *con, int x, int y, 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_update) {
1632 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1637 void dpy_text_resize(QemuConsole *con, int w, int h)
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_text_resize) {
1650 dcl->ops->dpy_text_resize(dcl, w, h);
1655 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
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_mouse_set) {
1668 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1673 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1675 DisplayState *s = con->ds;
1676 DisplayChangeListener *dcl;
1678 if (!qemu_console_is_visible(con)) {
1679 return;
1681 QLIST_FOREACH(dcl, &s->listeners, next) {
1682 if (con != (dcl->con ? dcl->con : active_console)) {
1683 continue;
1685 if (dcl->ops->dpy_cursor_define) {
1686 dcl->ops->dpy_cursor_define(dcl, cursor);
1691 bool dpy_cursor_define_supported(QemuConsole *con)
1693 DisplayState *s = con->ds;
1694 DisplayChangeListener *dcl;
1696 QLIST_FOREACH(dcl, &s->listeners, next) {
1697 if (dcl->ops->dpy_cursor_define) {
1698 return true;
1701 return false;
1704 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1705 struct QEMUGLParams *qparams)
1707 assert(con->gl);
1708 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1711 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1713 assert(con->gl);
1714 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1717 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1719 assert(con->gl);
1720 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1723 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1725 assert(con->gl);
1726 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1729 void dpy_gl_scanout_disable(QemuConsole *con)
1731 assert(con->gl);
1732 if (con->gl->ops->dpy_gl_scanout_disable) {
1733 con->gl->ops->dpy_gl_scanout_disable(con->gl);
1734 } else {
1735 con->gl->ops->dpy_gl_scanout_texture(con->gl, 0, false, 0, 0,
1736 0, 0, 0, 0);
1740 void dpy_gl_scanout_texture(QemuConsole *con,
1741 uint32_t backing_id,
1742 bool backing_y_0_top,
1743 uint32_t backing_width,
1744 uint32_t backing_height,
1745 uint32_t x, uint32_t y,
1746 uint32_t width, uint32_t height)
1748 assert(con->gl);
1749 con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id,
1750 backing_y_0_top,
1751 backing_width, backing_height,
1752 x, y, width, height);
1755 void dpy_gl_scanout_dmabuf(QemuConsole *con,
1756 QemuDmaBuf *dmabuf)
1758 assert(con->gl);
1759 con->gl->ops->dpy_gl_scanout_dmabuf(con->gl, dmabuf);
1762 void dpy_gl_cursor_dmabuf(QemuConsole *con,
1763 QemuDmaBuf *dmabuf,
1764 uint32_t pos_x, uint32_t pos_y)
1766 assert(con->gl);
1768 if (con->gl->ops->dpy_gl_cursor_dmabuf) {
1769 con->gl->ops->dpy_gl_cursor_dmabuf(con->gl, dmabuf, pos_x, pos_y);
1773 void dpy_gl_release_dmabuf(QemuConsole *con,
1774 QemuDmaBuf *dmabuf)
1776 assert(con->gl);
1778 if (con->gl->ops->dpy_gl_release_dmabuf) {
1779 con->gl->ops->dpy_gl_release_dmabuf(con->gl, dmabuf);
1783 void dpy_gl_update(QemuConsole *con,
1784 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1786 assert(con->gl);
1787 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1790 /***********************************************************/
1791 /* register display */
1793 /* console.c internal use only */
1794 static DisplayState *get_alloc_displaystate(void)
1796 if (!display_state) {
1797 display_state = g_new0(DisplayState, 1);
1798 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1799 text_console_update_cursor, NULL);
1801 return display_state;
1805 * Called by main(), after creating QemuConsoles
1806 * and before initializing ui (sdl/vnc/...).
1808 DisplayState *init_displaystate(void)
1810 gchar *name;
1811 int i;
1813 get_alloc_displaystate();
1814 for (i = 0; i < nb_consoles; i++) {
1815 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1816 consoles[i]->ds == NULL) {
1817 text_console_do_init(consoles[i]->chr, display_state);
1820 /* Hook up into the qom tree here (not in new_console()), once
1821 * all QemuConsoles are created and the order / numbering
1822 * doesn't change any more */
1823 name = g_strdup_printf("console[%d]", i);
1824 object_property_add_child(container_get(object_get_root(), "/backend"),
1825 name, OBJECT(consoles[i]), &error_abort);
1826 g_free(name);
1829 return display_state;
1832 void graphic_console_set_hwops(QemuConsole *con,
1833 const GraphicHwOps *hw_ops,
1834 void *opaque)
1836 con->hw_ops = hw_ops;
1837 con->hw = opaque;
1840 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1841 const GraphicHwOps *hw_ops,
1842 void *opaque)
1844 static const char noinit[] =
1845 "Guest has not initialized the display (yet).";
1846 int width = 640;
1847 int height = 480;
1848 QemuConsole *s;
1849 DisplayState *ds;
1851 ds = get_alloc_displaystate();
1852 trace_console_gfx_new();
1853 s = new_console(ds, GRAPHIC_CONSOLE, head);
1854 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
1855 graphic_console_set_hwops(s, hw_ops, opaque);
1856 if (dev) {
1857 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1858 &error_abort);
1861 s->surface = qemu_create_message_surface(width, height, noinit);
1862 return s;
1865 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1867 if (index >= nb_consoles) {
1868 return NULL;
1870 return consoles[index];
1873 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1875 Object *obj;
1876 uint32_t h;
1877 int i;
1879 for (i = 0; i < nb_consoles; i++) {
1880 if (!consoles[i]) {
1881 continue;
1883 obj = object_property_get_link(OBJECT(consoles[i]),
1884 "device", &error_abort);
1885 if (DEVICE(obj) != dev) {
1886 continue;
1888 h = object_property_get_uint(OBJECT(consoles[i]),
1889 "head", &error_abort);
1890 if (h != head) {
1891 continue;
1893 return consoles[i];
1895 return NULL;
1898 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1899 uint32_t head, Error **errp)
1901 DeviceState *dev;
1902 QemuConsole *con;
1904 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1905 if (dev == NULL) {
1906 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1907 "Device '%s' not found", device_id);
1908 return NULL;
1911 con = qemu_console_lookup_by_device(dev, head);
1912 if (con == NULL) {
1913 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1914 device_id, head);
1915 return NULL;
1918 return con;
1921 bool qemu_console_is_visible(QemuConsole *con)
1923 return (con == active_console) || (con->dcls > 0);
1926 bool qemu_console_is_graphic(QemuConsole *con)
1928 if (con == NULL) {
1929 con = active_console;
1931 return con && (con->console_type == GRAPHIC_CONSOLE);
1934 bool qemu_console_is_fixedsize(QemuConsole *con)
1936 if (con == NULL) {
1937 con = active_console;
1939 return con && (con->console_type != TEXT_CONSOLE);
1942 bool qemu_console_is_gl_blocked(QemuConsole *con)
1944 assert(con != NULL);
1945 return con->gl_block;
1948 char *qemu_console_get_label(QemuConsole *con)
1950 if (con->console_type == GRAPHIC_CONSOLE) {
1951 if (con->device) {
1952 return g_strdup(object_get_typename(con->device));
1954 return g_strdup("VGA");
1955 } else {
1956 if (con->chr && con->chr->label) {
1957 return g_strdup(con->chr->label);
1959 return g_strdup_printf("vc%d", con->index);
1963 int qemu_console_get_index(QemuConsole *con)
1965 if (con == NULL) {
1966 con = active_console;
1968 return con ? con->index : -1;
1971 uint32_t qemu_console_get_head(QemuConsole *con)
1973 if (con == NULL) {
1974 con = active_console;
1976 return con ? con->head : -1;
1979 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1981 assert(con != NULL);
1982 return &con->ui_info;
1985 int qemu_console_get_width(QemuConsole *con, int fallback)
1987 if (con == NULL) {
1988 con = active_console;
1990 return con ? surface_width(con->surface) : fallback;
1993 int qemu_console_get_height(QemuConsole *con, int fallback)
1995 if (con == NULL) {
1996 con = active_console;
1998 return con ? surface_height(con->surface) : fallback;
2001 static void vc_chr_set_echo(Chardev *chr, bool echo)
2003 VCChardev *drv = VC_CHARDEV(chr);
2004 QemuConsole *s = drv->console;
2006 s->echo = echo;
2009 static void text_console_update_cursor_timer(void)
2011 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2012 + CONSOLE_CURSOR_PERIOD / 2);
2015 static void text_console_update_cursor(void *opaque)
2017 QemuConsole *s;
2018 int i, count = 0;
2020 cursor_visible_phase = !cursor_visible_phase;
2022 for (i = 0; i < nb_consoles; i++) {
2023 s = consoles[i];
2024 if (qemu_console_is_graphic(s) ||
2025 !qemu_console_is_visible(s)) {
2026 continue;
2028 count++;
2029 graphic_hw_invalidate(s);
2032 if (count) {
2033 text_console_update_cursor_timer();
2037 static const GraphicHwOps text_console_ops = {
2038 .invalidate = text_console_invalidate,
2039 .text_update = text_console_update,
2042 static void text_console_do_init(Chardev *chr, DisplayState *ds)
2044 VCChardev *drv = VC_CHARDEV(chr);
2045 QemuConsole *s = drv->console;
2046 int g_width = 80 * FONT_WIDTH;
2047 int g_height = 24 * FONT_HEIGHT;
2049 s->out_fifo.buf = s->out_fifo_buf;
2050 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
2051 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
2052 s->ds = ds;
2054 s->y_displayed = 0;
2055 s->y_base = 0;
2056 s->total_height = DEFAULT_BACKSCROLL;
2057 s->x = 0;
2058 s->y = 0;
2059 if (!s->surface) {
2060 if (active_console && active_console->surface) {
2061 g_width = surface_width(active_console->surface);
2062 g_height = surface_height(active_console->surface);
2064 s->surface = qemu_create_displaysurface(g_width, g_height);
2067 s->hw_ops = &text_console_ops;
2068 s->hw = s;
2070 /* Set text attribute defaults */
2071 s->t_attrib_default.bold = 0;
2072 s->t_attrib_default.uline = 0;
2073 s->t_attrib_default.blink = 0;
2074 s->t_attrib_default.invers = 0;
2075 s->t_attrib_default.unvisible = 0;
2076 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2077 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
2078 /* set current text attributes to default */
2079 s->t_attrib = s->t_attrib_default;
2080 text_console_resize(s);
2082 if (chr->label) {
2083 char msg[128];
2084 int len;
2086 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
2087 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
2088 vc_chr_write(chr, (uint8_t *)msg, len);
2089 s->t_attrib = s->t_attrib_default;
2092 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
2095 static void vc_chr_open(Chardev *chr,
2096 ChardevBackend *backend,
2097 bool *be_opened,
2098 Error **errp)
2100 ChardevVC *vc = backend->u.vc.data;
2101 VCChardev *drv = VC_CHARDEV(chr);
2102 QemuConsole *s;
2103 unsigned width = 0;
2104 unsigned height = 0;
2106 if (vc->has_width) {
2107 width = vc->width;
2108 } else if (vc->has_cols) {
2109 width = vc->cols * FONT_WIDTH;
2112 if (vc->has_height) {
2113 height = vc->height;
2114 } else if (vc->has_rows) {
2115 height = vc->rows * FONT_HEIGHT;
2118 trace_console_txt_new(width, height);
2119 if (width == 0 || height == 0) {
2120 s = new_console(NULL, TEXT_CONSOLE, 0);
2121 } else {
2122 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
2123 s->surface = qemu_create_displaysurface(width, height);
2126 if (!s) {
2127 error_setg(errp, "cannot create text console");
2128 return;
2131 s->chr = chr;
2132 drv->console = s;
2134 if (display_state) {
2135 text_console_do_init(chr, display_state);
2138 /* console/chardev init sometimes completes elsewhere in a 2nd
2139 * stage, so defer OPENED events until they are fully initialized
2141 *be_opened = false;
2144 void qemu_console_resize(QemuConsole *s, int width, int height)
2146 DisplaySurface *surface;
2148 assert(s->console_type == GRAPHIC_CONSOLE);
2150 if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) &&
2151 pixman_image_get_width(s->surface->image) == width &&
2152 pixman_image_get_height(s->surface->image) == height) {
2153 return;
2156 surface = qemu_create_displaysurface(width, height);
2157 dpy_gfx_replace_surface(s, surface);
2160 DisplaySurface *qemu_console_surface(QemuConsole *console)
2162 return console->surface;
2165 PixelFormat qemu_default_pixelformat(int bpp)
2167 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2168 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
2169 return pf;
2172 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
2174 int val;
2175 ChardevVC *vc;
2177 backend->type = CHARDEV_BACKEND_KIND_VC;
2178 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
2179 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
2181 val = qemu_opt_get_number(opts, "width", 0);
2182 if (val != 0) {
2183 vc->has_width = true;
2184 vc->width = val;
2187 val = qemu_opt_get_number(opts, "height", 0);
2188 if (val != 0) {
2189 vc->has_height = true;
2190 vc->height = val;
2193 val = qemu_opt_get_number(opts, "cols", 0);
2194 if (val != 0) {
2195 vc->has_cols = true;
2196 vc->cols = val;
2199 val = qemu_opt_get_number(opts, "rows", 0);
2200 if (val != 0) {
2201 vc->has_rows = true;
2202 vc->rows = val;
2206 static const TypeInfo qemu_console_info = {
2207 .name = TYPE_QEMU_CONSOLE,
2208 .parent = TYPE_OBJECT,
2209 .instance_size = sizeof(QemuConsole),
2210 .class_size = sizeof(QemuConsoleClass),
2213 static void char_vc_class_init(ObjectClass *oc, void *data)
2215 ChardevClass *cc = CHARDEV_CLASS(oc);
2217 cc->parse = qemu_chr_parse_vc;
2218 cc->open = vc_chr_open;
2219 cc->chr_write = vc_chr_write;
2220 cc->chr_set_echo = vc_chr_set_echo;
2223 static const TypeInfo char_vc_type_info = {
2224 .name = TYPE_CHARDEV_VC,
2225 .parent = TYPE_CHARDEV,
2226 .instance_size = sizeof(VCChardev),
2227 .class_init = char_vc_class_init,
2230 void qemu_console_early_init(void)
2232 /* set the default vc driver */
2233 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2234 type_register(&char_vc_type_info);
2238 static void register_types(void)
2240 type_register_static(&qemu_console_info);
2243 type_init(register_types);