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
24 #include "qemu-common.h"
25 #include "ui/console.h"
26 #include "hw/qdev-core.h"
27 #include "qemu/timer.h"
28 #include "qmp-commands.h"
29 #include "sysemu/char.h"
31 //#define DEBUG_CONSOLE
32 #define DEFAULT_BACKSCROLL 512
33 #define MAX_CONSOLES 12
34 #define CONSOLE_CURSOR_PERIOD 500
36 typedef struct TextAttributes {
46 typedef struct TextCell {
48 TextAttributes t_attrib;
51 #define MAX_ESC_PARAMS 3
59 typedef struct QEMUFIFO {
62 int count, wptr, rptr;
65 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
69 l = f->buf_size - f->count;
74 l = f->buf_size - f->wptr;
77 memcpy(f->buf + f->wptr, buf, l);
79 if (f->wptr >= f->buf_size)
88 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
96 l = f->buf_size - f->rptr;
99 memcpy(buf, f->buf + f->rptr, l);
101 if (f->rptr >= f->buf_size)
113 TEXT_CONSOLE_FIXED_SIZE
120 console_type_t console_type;
122 DisplaySurface *surface;
125 /* Graphic console state. */
127 const GraphicHwOps *hw_ops;
130 /* Text console state */
134 int backscroll_height;
136 int x_saved, y_saved;
139 TextAttributes t_attrib_default; /* default text attributes */
140 TextAttributes t_attrib; /* currently active text attributes */
142 int text_x[2], text_y[2], cursor_invalidate;
144 bool cursor_visible_phase;
145 QEMUTimer *cursor_timer;
153 int esc_params[MAX_ESC_PARAMS];
156 CharDriverState *chr;
157 /* fifo for key pressed */
159 uint8_t out_fifo_buf[16];
160 QEMUTimer *kbd_timer;
163 struct DisplayState {
164 struct QEMUTimer *gui_timer;
165 uint64_t last_update;
166 uint64_t update_interval;
171 QLIST_HEAD(, DisplayChangeListener) listeners;
174 static DisplayState *display_state;
175 static QemuConsole *active_console;
176 static QemuConsole *consoles[MAX_CONSOLES];
177 static int nb_consoles = 0;
179 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
180 static void dpy_refresh(DisplayState *s);
181 static DisplayState *get_alloc_displaystate(void);
183 static void gui_update(void *opaque)
185 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
186 uint64_t dcl_interval;
187 DisplayState *ds = opaque;
188 DisplayChangeListener *dcl;
191 ds->refreshing = true;
193 ds->refreshing = false;
195 QLIST_FOREACH(dcl, &ds->listeners, next) {
196 dcl_interval = dcl->update_interval ?
197 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
198 if (interval > dcl_interval) {
199 interval = dcl_interval;
202 if (ds->update_interval != interval) {
203 ds->update_interval = interval;
204 for (i = 0; i < nb_consoles; i++) {
205 if (consoles[i]->hw_ops->update_interval) {
206 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
209 trace_console_refresh(interval);
211 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
212 timer_mod(ds->gui_timer, ds->last_update + interval);
215 static void gui_setup_refresh(DisplayState *ds)
217 DisplayChangeListener *dcl;
218 bool need_timer = false;
219 bool have_gfx = false;
220 bool have_text = false;
222 QLIST_FOREACH(dcl, &ds->listeners, next) {
223 if (dcl->ops->dpy_refresh != NULL) {
226 if (dcl->ops->dpy_gfx_update != NULL) {
229 if (dcl->ops->dpy_text_update != NULL) {
234 if (need_timer && ds->gui_timer == NULL) {
235 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
236 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
238 if (!need_timer && ds->gui_timer != NULL) {
239 timer_del(ds->gui_timer);
240 timer_free(ds->gui_timer);
241 ds->gui_timer = NULL;
244 ds->have_gfx = have_gfx;
245 ds->have_text = have_text;
248 void graphic_hw_update(QemuConsole *con)
251 con = active_console;
253 if (con && con->hw_ops->gfx_update) {
254 con->hw_ops->gfx_update(con->hw);
258 void graphic_hw_invalidate(QemuConsole *con)
261 con = active_console;
263 if (con && con->hw_ops->invalidate) {
264 con->hw_ops->invalidate(con->hw);
268 static void ppm_save(const char *filename, struct DisplaySurface *ds,
271 int width = pixman_image_get_width(ds->image);
272 int height = pixman_image_get_height(ds->image);
277 pixman_image_t *linebuf;
279 trace_ppm_save(filename, ds);
280 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
282 error_setg(errp, "failed to open file '%s': %s", filename,
286 f = fdopen(fd, "wb");
287 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
292 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
293 for (y = 0; y < height; y++) {
294 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
296 ret = fwrite(pixman_image_get_data(linebuf), 1,
297 pixman_image_get_stride(linebuf), f);
305 qemu_pixman_image_unref(linebuf);
310 error_setg(errp, "failed to write to file '%s': %s", filename,
316 void qmp_screendump(const char *filename, Error **errp)
318 QemuConsole *con = qemu_console_lookup_by_index(0);
319 DisplaySurface *surface;
322 error_setg(errp, "There is no QemuConsole I can screendump from.");
326 graphic_hw_update(con);
327 surface = qemu_console_surface(con);
328 ppm_save(filename, surface, errp);
331 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
334 con = active_console;
336 if (con && con->hw_ops->text_update) {
337 con->hw_ops->text_update(con->hw, chardata);
341 static void vga_fill_rect(QemuConsole *con,
342 int posx, int posy, int width, int height,
343 pixman_color_t color)
345 DisplaySurface *surface = qemu_console_surface(con);
346 pixman_rectangle16_t rect = {
347 .x = posx, .y = posy, .width = width, .height = height
350 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
354 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
355 static void vga_bitblt(QemuConsole *con,
356 int xs, int ys, int xd, int yd, int w, int h)
358 DisplaySurface *surface = qemu_console_surface(con);
360 pixman_image_composite(PIXMAN_OP_SRC,
361 surface->image, NULL, surface->image,
362 xs, ys, 0, 0, xd, yd, w, h);
365 /***********************************************************/
366 /* basic char display */
368 #define FONT_HEIGHT 16
373 #ifndef CONFIG_CURSES
386 #define QEMU_RGB(r, g, b) \
387 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
389 static const pixman_color_t color_table_rgb[2][8] = {
391 QEMU_RGB(0x00, 0x00, 0x00), /* black */
392 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
393 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
394 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
395 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
396 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
397 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
398 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
401 QEMU_RGB(0x00, 0x00, 0x00), /* black */
402 QEMU_RGB(0xff, 0x00, 0x00), /* red */
403 QEMU_RGB(0x00, 0xff, 0x00), /* green */
404 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
405 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
406 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
407 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
408 QEMU_RGB(0xff, 0xff, 0xff), /* white */
412 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
413 TextAttributes *t_attrib)
415 static pixman_image_t *glyphs[256];
416 DisplaySurface *surface = qemu_console_surface(s);
417 pixman_color_t fgcol, bgcol;
419 if (t_attrib->invers) {
420 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
421 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
423 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
424 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
428 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
430 qemu_pixman_glyph_render(glyphs[ch], surface->image,
431 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
434 static void text_console_resize(QemuConsole *s)
436 TextCell *cells, *c, *c1;
437 int w1, x, y, last_width;
439 last_width = s->width;
440 s->width = surface_width(s->surface) / FONT_WIDTH;
441 s->height = surface_height(s->surface) / FONT_HEIGHT;
447 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
448 for(y = 0; y < s->total_height; y++) {
449 c = &cells[y * s->width];
451 c1 = &s->cells[y * last_width];
452 for(x = 0; x < w1; x++) {
456 for(x = w1; x < s->width; x++) {
458 c->t_attrib = s->t_attrib_default;
466 static inline void text_update_xy(QemuConsole *s, int x, int y)
468 s->text_x[0] = MIN(s->text_x[0], x);
469 s->text_x[1] = MAX(s->text_x[1], x);
470 s->text_y[0] = MIN(s->text_y[0], y);
471 s->text_y[1] = MAX(s->text_y[1], y);
474 static void invalidate_xy(QemuConsole *s, int x, int y)
476 if (s->update_x0 > x * FONT_WIDTH)
477 s->update_x0 = x * FONT_WIDTH;
478 if (s->update_y0 > y * FONT_HEIGHT)
479 s->update_y0 = y * FONT_HEIGHT;
480 if (s->update_x1 < (x + 1) * FONT_WIDTH)
481 s->update_x1 = (x + 1) * FONT_WIDTH;
482 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
483 s->update_y1 = (y + 1) * FONT_HEIGHT;
486 static void update_xy(QemuConsole *s, int x, int y)
491 if (!qemu_console_is_visible(s)) {
495 if (s->ds->have_text) {
496 text_update_xy(s, x, y);
499 if (s->ds->have_gfx) {
500 y1 = (s->y_base + y) % s->total_height;
501 y2 = y1 - s->y_displayed;
503 y2 += s->total_height;
504 if (y2 < s->height) {
505 c = &s->cells[y1 * s->width + x];
506 vga_putcharxy(s, x, y2, c->ch,
508 invalidate_xy(s, x, y2);
513 static void console_show_cursor(QemuConsole *s, int show)
519 if (!qemu_console_is_visible(s)) {
523 if (s->ds->have_text) {
524 s->cursor_invalidate = 1;
527 if (s->ds->have_gfx) {
531 y1 = (s->y_base + s->y) % s->total_height;
532 y = y1 - s->y_displayed;
534 y += s->total_height;
536 c = &s->cells[y1 * s->width + x];
537 if (show && s->cursor_visible_phase) {
538 TextAttributes t_attrib = s->t_attrib_default;
539 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
540 vga_putcharxy(s, x, y, c->ch, &t_attrib);
542 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
544 invalidate_xy(s, x, y);
549 static void console_refresh(QemuConsole *s)
551 DisplaySurface *surface = qemu_console_surface(s);
555 if (!qemu_console_is_visible(s)) {
559 if (s->ds->have_text) {
562 s->text_x[1] = s->width - 1;
563 s->text_y[1] = s->height - 1;
564 s->cursor_invalidate = 1;
567 if (s->ds->have_gfx) {
568 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
569 color_table_rgb[0][COLOR_BLACK]);
571 for (y = 0; y < s->height; y++) {
572 c = s->cells + y1 * s->width;
573 for (x = 0; x < s->width; x++) {
574 vga_putcharxy(s, x, y, c->ch,
578 if (++y1 == s->total_height) {
582 console_show_cursor(s, 1);
583 dpy_gfx_update(s, 0, 0,
584 surface_width(surface), surface_height(surface));
588 static void console_scroll(QemuConsole *s, int ydelta)
593 for(i = 0; i < ydelta; i++) {
594 if (s->y_displayed == s->y_base)
596 if (++s->y_displayed == s->total_height)
601 i = s->backscroll_height;
602 if (i > s->total_height - s->height)
603 i = s->total_height - s->height;
606 y1 += s->total_height;
607 for(i = 0; i < ydelta; i++) {
608 if (s->y_displayed == y1)
610 if (--s->y_displayed < 0)
611 s->y_displayed = s->total_height - 1;
617 static void console_put_lf(QemuConsole *s)
623 if (s->y >= s->height) {
624 s->y = s->height - 1;
626 if (s->y_displayed == s->y_base) {
627 if (++s->y_displayed == s->total_height)
630 if (++s->y_base == s->total_height)
632 if (s->backscroll_height < s->total_height)
633 s->backscroll_height++;
634 y1 = (s->y_base + s->height - 1) % s->total_height;
635 c = &s->cells[y1 * s->width];
636 for(x = 0; x < s->width; x++) {
638 c->t_attrib = s->t_attrib_default;
641 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
642 if (s->ds->have_text) {
645 s->text_x[1] = s->width - 1;
646 s->text_y[1] = s->height - 1;
649 if (s->ds->have_gfx) {
650 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
651 s->width * FONT_WIDTH,
652 (s->height - 1) * FONT_HEIGHT);
653 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
654 s->width * FONT_WIDTH, FONT_HEIGHT,
655 color_table_rgb[0][s->t_attrib_default.bgcol]);
658 s->update_x1 = s->width * FONT_WIDTH;
659 s->update_y1 = s->height * FONT_HEIGHT;
665 /* Set console attributes depending on the current escape codes.
666 * NOTE: I know this code is not very efficient (checking every color for it
667 * self) but it is more readable and better maintainable.
669 static void console_handle_escape(QemuConsole *s)
673 for (i=0; i<s->nb_esc_params; i++) {
674 switch (s->esc_params[i]) {
675 case 0: /* reset all console attributes to default */
676 s->t_attrib = s->t_attrib_default;
679 s->t_attrib.bold = 1;
682 s->t_attrib.uline = 1;
685 s->t_attrib.blink = 1;
688 s->t_attrib.invers = 1;
691 s->t_attrib.unvisible = 1;
694 s->t_attrib.bold = 0;
697 s->t_attrib.uline = 0;
700 s->t_attrib.blink = 0;
703 s->t_attrib.invers = 0;
706 s->t_attrib.unvisible = 0;
708 /* set foreground color */
710 s->t_attrib.fgcol=COLOR_BLACK;
713 s->t_attrib.fgcol=COLOR_RED;
716 s->t_attrib.fgcol=COLOR_GREEN;
719 s->t_attrib.fgcol=COLOR_YELLOW;
722 s->t_attrib.fgcol=COLOR_BLUE;
725 s->t_attrib.fgcol=COLOR_MAGENTA;
728 s->t_attrib.fgcol=COLOR_CYAN;
731 s->t_attrib.fgcol=COLOR_WHITE;
733 /* set background color */
735 s->t_attrib.bgcol=COLOR_BLACK;
738 s->t_attrib.bgcol=COLOR_RED;
741 s->t_attrib.bgcol=COLOR_GREEN;
744 s->t_attrib.bgcol=COLOR_YELLOW;
747 s->t_attrib.bgcol=COLOR_BLUE;
750 s->t_attrib.bgcol=COLOR_MAGENTA;
753 s->t_attrib.bgcol=COLOR_CYAN;
756 s->t_attrib.bgcol=COLOR_WHITE;
762 static void console_clear_xy(QemuConsole *s, int x, int y)
764 int y1 = (s->y_base + y) % s->total_height;
765 TextCell *c = &s->cells[y1 * s->width + x];
767 c->t_attrib = s->t_attrib_default;
771 /* set cursor, checking bounds */
772 static void set_cursor(QemuConsole *s, int x, int y)
780 if (y >= s->height) {
791 static void console_putchar(QemuConsole *s, int ch)
800 case '\r': /* carriage return */
803 case '\n': /* newline */
806 case '\b': /* backspace */
810 case '\t': /* tabspace */
811 if (s->x + (8 - (s->x % 8)) > s->width) {
815 s->x = s->x + (8 - (s->x % 8));
818 case '\a': /* alert aka. bell */
819 /* TODO: has to be implemented */
822 /* SI (shift in), character set 0 (ignored) */
825 /* SO (shift out), character set 1 (ignored) */
827 case 27: /* esc (introducing an escape sequence) */
828 s->state = TTY_STATE_ESC;
831 if (s->x >= s->width) {
836 y1 = (s->y_base + s->y) % s->total_height;
837 c = &s->cells[y1 * s->width + s->x];
839 c->t_attrib = s->t_attrib;
840 update_xy(s, s->x, s->y);
845 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
847 for(i=0;i<MAX_ESC_PARAMS;i++)
848 s->esc_params[i] = 0;
849 s->nb_esc_params = 0;
850 s->state = TTY_STATE_CSI;
852 s->state = TTY_STATE_NORM;
855 case TTY_STATE_CSI: /* handle escape sequence parameters */
856 if (ch >= '0' && ch <= '9') {
857 if (s->nb_esc_params < MAX_ESC_PARAMS) {
858 int *param = &s->esc_params[s->nb_esc_params];
859 int digit = (ch - '0');
861 *param = (*param <= (INT_MAX - digit) / 10) ?
862 *param * 10 + digit : INT_MAX;
865 if (s->nb_esc_params < MAX_ESC_PARAMS)
870 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
871 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
873 s->state = TTY_STATE_NORM;
877 if (s->esc_params[0] == 0) {
878 s->esc_params[0] = 1;
880 set_cursor(s, s->x, s->y - s->esc_params[0]);
883 /* move cursor down */
884 if (s->esc_params[0] == 0) {
885 s->esc_params[0] = 1;
887 set_cursor(s, s->x, s->y + s->esc_params[0]);
890 /* move cursor right */
891 if (s->esc_params[0] == 0) {
892 s->esc_params[0] = 1;
894 set_cursor(s, s->x + s->esc_params[0], s->y);
897 /* move cursor left */
898 if (s->esc_params[0] == 0) {
899 s->esc_params[0] = 1;
901 set_cursor(s, s->x - s->esc_params[0], s->y);
904 /* move cursor to column */
905 set_cursor(s, s->esc_params[0] - 1, s->y);
909 /* move cursor to row, column */
910 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
913 switch (s->esc_params[0]) {
915 /* clear to end of screen */
916 for (y = s->y; y < s->height; y++) {
917 for (x = 0; x < s->width; x++) {
918 if (y == s->y && x < s->x) {
921 console_clear_xy(s, x, y);
926 /* clear from beginning of screen */
927 for (y = 0; y <= s->y; y++) {
928 for (x = 0; x < s->width; x++) {
929 if (y == s->y && x > s->x) {
932 console_clear_xy(s, x, y);
937 /* clear entire screen */
938 for (y = 0; y <= s->height; y++) {
939 for (x = 0; x < s->width; x++) {
940 console_clear_xy(s, x, y);
947 switch (s->esc_params[0]) {
950 for(x = s->x; x < s->width; x++) {
951 console_clear_xy(s, x, s->y);
955 /* clear from beginning of line */
956 for (x = 0; x <= s->x; x++) {
957 console_clear_xy(s, x, s->y);
961 /* clear entire line */
962 for(x = 0; x < s->width; x++) {
963 console_clear_xy(s, x, s->y);
969 console_handle_escape(s);
972 /* report cursor position */
973 /* TODO: send ESC[row;colR */
976 /* save cursor position */
981 /* restore cursor position */
987 fprintf(stderr, "unhandled escape character '%c'\n", ch);
996 void console_select(unsigned int index)
998 DisplayChangeListener *dcl;
1001 if (index >= MAX_CONSOLES)
1004 trace_console_select(index);
1005 s = qemu_console_lookup_by_index(index);
1007 DisplayState *ds = s->ds;
1009 if (active_console && active_console->cursor_timer) {
1010 timer_del(active_console->cursor_timer);
1014 QLIST_FOREACH(dcl, &ds->listeners, next) {
1015 if (dcl->con != NULL) {
1018 if (dcl->ops->dpy_gfx_switch) {
1019 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1022 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1023 surface_height(s->surface));
1025 if (ds->have_text) {
1026 dpy_text_resize(s, s->width, s->height);
1028 if (s->cursor_timer) {
1029 timer_mod(s->cursor_timer,
1030 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1035 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1037 QemuConsole *s = chr->opaque;
1040 s->update_x0 = s->width * FONT_WIDTH;
1041 s->update_y0 = s->height * FONT_HEIGHT;
1044 console_show_cursor(s, 0);
1045 for(i = 0; i < len; i++) {
1046 console_putchar(s, buf[i]);
1048 console_show_cursor(s, 1);
1049 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1050 dpy_gfx_update(s, s->update_x0, s->update_y0,
1051 s->update_x1 - s->update_x0,
1052 s->update_y1 - s->update_y0);
1057 static void kbd_send_chars(void *opaque)
1059 QemuConsole *s = opaque;
1063 len = qemu_chr_be_can_write(s->chr);
1064 if (len > s->out_fifo.count)
1065 len = s->out_fifo.count;
1067 if (len > sizeof(buf))
1069 qemu_fifo_read(&s->out_fifo, buf, len);
1070 qemu_chr_be_write(s->chr, buf, len);
1072 /* characters are pending: we send them a bit later (XXX:
1073 horrible, should change char device API) */
1074 if (s->out_fifo.count > 0) {
1075 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1079 /* called when an ascii key is pressed */
1080 void kbd_put_keysym(int keysym)
1083 uint8_t buf[16], *q;
1087 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1091 case QEMU_KEY_CTRL_UP:
1092 console_scroll(s, -1);
1094 case QEMU_KEY_CTRL_DOWN:
1095 console_scroll(s, 1);
1097 case QEMU_KEY_CTRL_PAGEUP:
1098 console_scroll(s, -10);
1100 case QEMU_KEY_CTRL_PAGEDOWN:
1101 console_scroll(s, 10);
1104 /* convert the QEMU keysym to VT100 key string */
1106 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1109 c = keysym - 0xe100;
1111 *q++ = '0' + (c / 10);
1112 *q++ = '0' + (c % 10);
1114 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1117 *q++ = keysym & 0xff;
1118 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1119 console_puts(s->chr, (const uint8_t *) "\r", 1);
1125 console_puts(s->chr, buf, q - buf);
1127 if (s->chr->chr_read) {
1128 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1135 static void text_console_invalidate(void *opaque)
1137 QemuConsole *s = (QemuConsole *) opaque;
1139 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1140 text_console_resize(s);
1145 static void text_console_update(void *opaque, console_ch_t *chardata)
1147 QemuConsole *s = (QemuConsole *) opaque;
1150 if (s->text_x[0] <= s->text_x[1]) {
1151 src = (s->y_base + s->text_y[0]) * s->width;
1152 chardata += s->text_y[0] * s->width;
1153 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1154 for (j = 0; j < s->width; j ++, src ++)
1155 console_write_ch(chardata ++, s->cells[src].ch |
1156 (s->cells[src].t_attrib.fgcol << 12) |
1157 (s->cells[src].t_attrib.bgcol << 8) |
1158 (s->cells[src].t_attrib.bold << 21));
1159 dpy_text_update(s, s->text_x[0], s->text_y[0],
1160 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1161 s->text_x[0] = s->width;
1162 s->text_y[0] = s->height;
1166 if (s->cursor_invalidate) {
1167 dpy_text_cursor(s, s->x, s->y);
1168 s->cursor_invalidate = 0;
1172 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1174 Error *local_err = NULL;
1179 if (nb_consoles >= MAX_CONSOLES)
1182 obj = object_new(TYPE_QEMU_CONSOLE);
1183 s = QEMU_CONSOLE(obj);
1184 object_property_add_link(obj, "device", TYPE_DEVICE,
1185 (Object **)&s->device, &local_err);
1187 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1188 (console_type == GRAPHIC_CONSOLE))) {
1192 s->console_type = console_type;
1193 if (console_type != GRAPHIC_CONSOLE) {
1194 s->index = nb_consoles;
1195 consoles[nb_consoles++] = s;
1197 /* HACK: Put graphical consoles before text consoles. */
1198 for (i = nb_consoles; i > 0; i--) {
1199 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1201 consoles[i] = consoles[i - 1];
1202 consoles[i]->index = i;
1211 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1212 int linesize, PixelFormat pf, int newflags)
1216 qemu_pixman_image_unref(surface->image);
1217 surface->image = NULL;
1219 surface->format = qemu_pixman_get_format(&pf);
1220 assert(surface->format != 0);
1221 surface->image = pixman_image_create_bits(surface->format,
1224 assert(surface->image != NULL);
1226 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1227 #ifdef HOST_WORDS_BIGENDIAN
1228 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1232 DisplaySurface *qemu_create_displaysurface(int width, int height)
1234 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1235 int linesize = width * 4;
1237 trace_displaysurface_create(surface, width, height);
1238 qemu_alloc_display(surface, width, height, linesize,
1239 qemu_default_pixelformat(32), 0);
1243 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1244 int linesize, uint8_t *data,
1247 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1249 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1251 surface->pf = qemu_different_endianness_pixelformat(bpp);
1253 surface->pf = qemu_default_pixelformat(bpp);
1256 surface->format = qemu_pixman_get_format(&surface->pf);
1257 assert(surface->format != 0);
1258 surface->image = pixman_image_create_bits(surface->format,
1260 (void *)data, linesize);
1261 assert(surface->image != NULL);
1263 #ifdef HOST_WORDS_BIGENDIAN
1264 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1270 static DisplaySurface *qemu_create_dummy_surface(void)
1272 static const char msg[] =
1273 "This VM has no graphic display device.";
1274 DisplaySurface *surface = qemu_create_displaysurface(640, 480);
1275 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1276 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1277 pixman_image_t *glyph;
1281 x = (640/FONT_WIDTH - len) / 2;
1282 y = (480/FONT_HEIGHT - 1) / 2;
1283 for (i = 0; i < len; i++) {
1284 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1285 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1286 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1287 qemu_pixman_image_unref(glyph);
1292 void qemu_free_displaysurface(DisplaySurface *surface)
1294 if (surface == NULL) {
1297 trace_displaysurface_free(surface);
1298 qemu_pixman_image_unref(surface->image);
1302 void register_displaychangelistener(DisplayChangeListener *dcl)
1304 static DisplaySurface *dummy;
1307 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1308 dcl->ds = get_alloc_displaystate();
1309 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1310 gui_setup_refresh(dcl->ds);
1315 con = active_console;
1317 if (dcl->ops->dpy_gfx_switch) {
1319 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1322 dummy = qemu_create_dummy_surface();
1324 dcl->ops->dpy_gfx_switch(dcl, dummy);
1329 void update_displaychangelistener(DisplayChangeListener *dcl,
1332 DisplayState *ds = dcl->ds;
1334 dcl->update_interval = interval;
1335 if (!ds->refreshing && ds->update_interval > interval) {
1336 timer_mod(ds->gui_timer, ds->last_update + interval);
1340 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1342 DisplayState *ds = dcl->ds;
1343 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1347 QLIST_REMOVE(dcl, next);
1348 gui_setup_refresh(ds);
1351 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1353 DisplayState *s = con->ds;
1354 DisplayChangeListener *dcl;
1355 int width = surface_width(con->surface);
1356 int height = surface_height(con->surface);
1362 w = MIN(w, width - x);
1363 h = MIN(h, height - y);
1365 if (!qemu_console_is_visible(con)) {
1368 QLIST_FOREACH(dcl, &s->listeners, next) {
1369 if (con != (dcl->con ? dcl->con : active_console)) {
1372 if (dcl->ops->dpy_gfx_update) {
1373 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1378 void dpy_gfx_replace_surface(QemuConsole *con,
1379 DisplaySurface *surface)
1381 DisplayState *s = con->ds;
1382 DisplaySurface *old_surface = con->surface;
1383 DisplayChangeListener *dcl;
1385 con->surface = surface;
1386 QLIST_FOREACH(dcl, &s->listeners, next) {
1387 if (con != (dcl->con ? dcl->con : active_console)) {
1390 if (dcl->ops->dpy_gfx_switch) {
1391 dcl->ops->dpy_gfx_switch(dcl, surface);
1394 qemu_free_displaysurface(old_surface);
1397 void dpy_refresh(DisplayState *s)
1399 DisplayChangeListener *dcl;
1401 QLIST_FOREACH(dcl, &s->listeners, next) {
1402 if (dcl->ops->dpy_refresh) {
1403 dcl->ops->dpy_refresh(dcl);
1408 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1409 int dst_x, int dst_y, int w, int h)
1411 DisplayState *s = con->ds;
1412 DisplayChangeListener *dcl;
1414 if (!qemu_console_is_visible(con)) {
1417 QLIST_FOREACH(dcl, &s->listeners, next) {
1418 if (con != (dcl->con ? dcl->con : active_console)) {
1421 if (dcl->ops->dpy_gfx_copy) {
1422 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1424 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1429 void dpy_text_cursor(QemuConsole *con, int x, int y)
1431 DisplayState *s = con->ds;
1432 DisplayChangeListener *dcl;
1434 if (!qemu_console_is_visible(con)) {
1437 QLIST_FOREACH(dcl, &s->listeners, next) {
1438 if (con != (dcl->con ? dcl->con : active_console)) {
1441 if (dcl->ops->dpy_text_cursor) {
1442 dcl->ops->dpy_text_cursor(dcl, x, y);
1447 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1449 DisplayState *s = con->ds;
1450 DisplayChangeListener *dcl;
1452 if (!qemu_console_is_visible(con)) {
1455 QLIST_FOREACH(dcl, &s->listeners, next) {
1456 if (con != (dcl->con ? dcl->con : active_console)) {
1459 if (dcl->ops->dpy_text_update) {
1460 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1465 void dpy_text_resize(QemuConsole *con, int w, int h)
1467 DisplayState *s = con->ds;
1468 struct DisplayChangeListener *dcl;
1470 if (!qemu_console_is_visible(con)) {
1473 QLIST_FOREACH(dcl, &s->listeners, next) {
1474 if (con != (dcl->con ? dcl->con : active_console)) {
1477 if (dcl->ops->dpy_text_resize) {
1478 dcl->ops->dpy_text_resize(dcl, w, h);
1483 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1485 DisplayState *s = con->ds;
1486 DisplayChangeListener *dcl;
1488 if (!qemu_console_is_visible(con)) {
1491 QLIST_FOREACH(dcl, &s->listeners, next) {
1492 if (con != (dcl->con ? dcl->con : active_console)) {
1495 if (dcl->ops->dpy_mouse_set) {
1496 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1501 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1503 DisplayState *s = con->ds;
1504 DisplayChangeListener *dcl;
1506 if (!qemu_console_is_visible(con)) {
1509 QLIST_FOREACH(dcl, &s->listeners, next) {
1510 if (con != (dcl->con ? dcl->con : active_console)) {
1513 if (dcl->ops->dpy_cursor_define) {
1514 dcl->ops->dpy_cursor_define(dcl, cursor);
1519 bool dpy_cursor_define_supported(QemuConsole *con)
1521 DisplayState *s = con->ds;
1522 DisplayChangeListener *dcl;
1524 QLIST_FOREACH(dcl, &s->listeners, next) {
1525 if (dcl->ops->dpy_cursor_define) {
1532 /***********************************************************/
1533 /* register display */
1535 /* console.c internal use only */
1536 static DisplayState *get_alloc_displaystate(void)
1538 if (!display_state) {
1539 display_state = g_new0(DisplayState, 1);
1541 return display_state;
1545 * Called by main(), after creating QemuConsoles
1546 * and before initializing ui (sdl/vnc/...).
1548 DisplayState *init_displaystate(void)
1550 Error *local_err = NULL;
1554 if (!display_state) {
1555 display_state = g_new0(DisplayState, 1);
1558 for (i = 0; i < nb_consoles; i++) {
1559 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1560 consoles[i]->ds == NULL) {
1561 text_console_do_init(consoles[i]->chr, display_state);
1564 /* Hook up into the qom tree here (not in new_console()), once
1565 * all QemuConsoles are created and the order / numbering
1566 * doesn't change any more */
1567 name = g_strdup_printf("console[%d]", i);
1568 object_property_add_child(container_get(object_get_root(), "/backend"),
1569 name, OBJECT(consoles[i]), &local_err);
1573 return display_state;
1576 QemuConsole *graphic_console_init(DeviceState *dev,
1577 const GraphicHwOps *hw_ops,
1580 Error *local_err = NULL;
1586 ds = get_alloc_displaystate();
1587 trace_console_gfx_new();
1588 s = new_console(ds, GRAPHIC_CONSOLE);
1592 object_property_set_link(OBJECT(s), OBJECT(dev),
1593 "device", &local_err);
1596 s->surface = qemu_create_displaysurface(width, height);
1600 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1602 if (index >= MAX_CONSOLES) {
1605 return consoles[index];
1608 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev)
1610 Error *local_err = NULL;
1614 for (i = 0; i < nb_consoles; i++) {
1618 obj = object_property_get_link(OBJECT(consoles[i]),
1619 "device", &local_err);
1620 if (DEVICE(obj) == dev) {
1627 bool qemu_console_is_visible(QemuConsole *con)
1629 return (con == active_console) || (con->dcls > 0);
1632 bool qemu_console_is_graphic(QemuConsole *con)
1635 con = active_console;
1637 return con && (con->console_type == GRAPHIC_CONSOLE);
1640 bool qemu_console_is_fixedsize(QemuConsole *con)
1643 con = active_console;
1645 return con && (con->console_type != TEXT_CONSOLE);
1648 static void text_console_set_echo(CharDriverState *chr, bool echo)
1650 QemuConsole *s = chr->opaque;
1655 static void text_console_update_cursor(void *opaque)
1657 QemuConsole *s = opaque;
1659 s->cursor_visible_phase = !s->cursor_visible_phase;
1660 graphic_hw_invalidate(s);
1661 timer_mod(s->cursor_timer,
1662 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1665 static const GraphicHwOps text_console_ops = {
1666 .invalidate = text_console_invalidate,
1667 .text_update = text_console_update,
1670 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1673 int g_width = 80 * FONT_WIDTH;
1674 int g_height = 24 * FONT_HEIGHT;
1678 chr->chr_write = console_puts;
1680 s->out_fifo.buf = s->out_fifo_buf;
1681 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1682 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1687 s->total_height = DEFAULT_BACKSCROLL;
1691 if (active_console && active_console->surface) {
1692 g_width = surface_width(active_console->surface);
1693 g_height = surface_height(active_console->surface);
1695 s->surface = qemu_create_displaysurface(g_width, g_height);
1699 timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
1701 s->hw_ops = &text_console_ops;
1704 /* Set text attribute defaults */
1705 s->t_attrib_default.bold = 0;
1706 s->t_attrib_default.uline = 0;
1707 s->t_attrib_default.blink = 0;
1708 s->t_attrib_default.invers = 0;
1709 s->t_attrib_default.unvisible = 0;
1710 s->t_attrib_default.fgcol = COLOR_WHITE;
1711 s->t_attrib_default.bgcol = COLOR_BLACK;
1712 /* set current text attributes to default */
1713 s->t_attrib = s->t_attrib_default;
1714 text_console_resize(s);
1720 s->t_attrib.bgcol = COLOR_BLUE;
1721 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1722 console_puts(chr, (uint8_t*)msg, len);
1723 s->t_attrib = s->t_attrib_default;
1726 qemu_chr_be_generic_open(chr);
1731 static CharDriverState *text_console_init(ChardevVC *vc)
1733 CharDriverState *chr;
1736 unsigned height = 0;
1738 chr = g_malloc0(sizeof(CharDriverState));
1740 if (vc->has_width) {
1742 } else if (vc->has_cols) {
1743 width = vc->cols * FONT_WIDTH;
1746 if (vc->has_height) {
1747 height = vc->height;
1748 } else if (vc->has_rows) {
1749 height = vc->rows * FONT_HEIGHT;
1752 trace_console_txt_new(width, height);
1753 if (width == 0 || height == 0) {
1754 s = new_console(NULL, TEXT_CONSOLE);
1756 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1757 s->surface = qemu_create_displaysurface(width, height);
1767 chr->chr_set_echo = text_console_set_echo;
1768 /* console/chardev init sometimes completes elsewhere in a 2nd
1769 * stage, so defer OPENED events until they are fully initialized
1771 chr->explicit_be_open = true;
1773 if (display_state) {
1774 text_console_do_init(chr, display_state);
1779 static VcHandler *vc_handler = text_console_init;
1781 CharDriverState *vc_init(ChardevVC *vc)
1783 return vc_handler(vc);
1786 void register_vc_handler(VcHandler *handler)
1788 vc_handler = handler;
1791 void qemu_console_resize(QemuConsole *s, int width, int height)
1793 DisplaySurface *surface;
1795 assert(s->console_type == GRAPHIC_CONSOLE);
1796 surface = qemu_create_displaysurface(width, height);
1797 dpy_gfx_replace_surface(s, surface);
1800 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1801 int dst_x, int dst_y, int w, int h)
1803 assert(con->console_type == GRAPHIC_CONSOLE);
1804 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1807 DisplaySurface *qemu_console_surface(QemuConsole *console)
1809 return console->surface;
1812 DisplayState *qemu_console_displaystate(QemuConsole *console)
1817 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1821 memset(&pf, 0x00, sizeof(PixelFormat));
1823 pf.bits_per_pixel = bpp;
1824 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1825 pf.depth = bpp == 32 ? 24 : bpp;
1829 pf.rmask = 0x000000FF;
1830 pf.gmask = 0x0000FF00;
1831 pf.bmask = 0x00FF0000;
1843 pf.rmask = 0x0000FF00;
1844 pf.gmask = 0x00FF0000;
1845 pf.bmask = 0xFF000000;
1846 pf.amask = 0x00000000;
1866 PixelFormat qemu_default_pixelformat(int bpp)
1870 memset(&pf, 0x00, sizeof(PixelFormat));
1872 pf.bits_per_pixel = bpp;
1873 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1874 pf.depth = bpp == 32 ? 24 : bpp;
1878 pf.bits_per_pixel = 16;
1879 pf.rmask = 0x00007c00;
1880 pf.gmask = 0x000003E0;
1881 pf.bmask = 0x0000001F;
1893 pf.rmask = 0x0000F800;
1894 pf.gmask = 0x000007E0;
1895 pf.bmask = 0x0000001F;
1907 pf.rmask = 0x00FF0000;
1908 pf.gmask = 0x0000FF00;
1909 pf.bmask = 0x000000FF;
1921 pf.rmask = 0x00FF0000;
1922 pf.gmask = 0x0000FF00;
1923 pf.bmask = 0x000000FF;
1940 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1945 backend->vc = g_new0(ChardevVC, 1);
1947 val = qemu_opt_get_number(opts, "width", 0);
1949 backend->vc->has_width = true;
1950 backend->vc->width = val;
1953 val = qemu_opt_get_number(opts, "height", 0);
1955 backend->vc->has_height = true;
1956 backend->vc->height = val;
1959 val = qemu_opt_get_number(opts, "cols", 0);
1961 backend->vc->has_cols = true;
1962 backend->vc->cols = val;
1965 val = qemu_opt_get_number(opts, "rows", 0);
1967 backend->vc->has_rows = true;
1968 backend->vc->rows = val;
1972 static const TypeInfo qemu_console_info = {
1973 .name = TYPE_QEMU_CONSOLE,
1974 .parent = TYPE_OBJECT,
1975 .instance_size = sizeof(QemuConsole),
1976 .class_size = sizeof(QemuConsoleClass),
1980 static void register_types(void)
1982 type_register_static(&qemu_console_info);
1983 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1987 type_init(register_types);