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 */
413 static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
415 if (t_attrib->bold) {
420 if (t_attrib->uline) {
425 if (t_attrib->blink) {
430 if (t_attrib->invers) {
435 if (t_attrib->unvisible) {
441 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
445 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
446 TextAttributes *t_attrib)
448 static pixman_image_t *glyphs[256];
449 DisplaySurface *surface = qemu_console_surface(s);
450 pixman_color_t fgcol, bgcol;
452 if (t_attrib->invers) {
453 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
454 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
456 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
457 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
461 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
463 qemu_pixman_glyph_render(glyphs[ch], surface->image,
464 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
467 static void text_console_resize(QemuConsole *s)
469 TextCell *cells, *c, *c1;
470 int w1, x, y, last_width;
472 last_width = s->width;
473 s->width = surface_width(s->surface) / FONT_WIDTH;
474 s->height = surface_height(s->surface) / FONT_HEIGHT;
480 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
481 for(y = 0; y < s->total_height; y++) {
482 c = &cells[y * s->width];
484 c1 = &s->cells[y * last_width];
485 for(x = 0; x < w1; x++) {
489 for(x = w1; x < s->width; x++) {
491 c->t_attrib = s->t_attrib_default;
499 static inline void text_update_xy(QemuConsole *s, int x, int y)
501 s->text_x[0] = MIN(s->text_x[0], x);
502 s->text_x[1] = MAX(s->text_x[1], x);
503 s->text_y[0] = MIN(s->text_y[0], y);
504 s->text_y[1] = MAX(s->text_y[1], y);
507 static void invalidate_xy(QemuConsole *s, int x, int y)
509 if (s->update_x0 > x * FONT_WIDTH)
510 s->update_x0 = x * FONT_WIDTH;
511 if (s->update_y0 > y * FONT_HEIGHT)
512 s->update_y0 = y * FONT_HEIGHT;
513 if (s->update_x1 < (x + 1) * FONT_WIDTH)
514 s->update_x1 = (x + 1) * FONT_WIDTH;
515 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
516 s->update_y1 = (y + 1) * FONT_HEIGHT;
519 static void update_xy(QemuConsole *s, int x, int y)
524 if (!qemu_console_is_visible(s)) {
528 if (s->ds->have_text) {
529 text_update_xy(s, x, y);
532 if (s->ds->have_gfx) {
533 y1 = (s->y_base + y) % s->total_height;
534 y2 = y1 - s->y_displayed;
536 y2 += s->total_height;
537 if (y2 < s->height) {
538 c = &s->cells[y1 * s->width + x];
539 vga_putcharxy(s, x, y2, c->ch,
541 invalidate_xy(s, x, y2);
546 static void console_show_cursor(QemuConsole *s, int show)
552 if (!qemu_console_is_visible(s)) {
556 if (s->ds->have_text) {
557 s->cursor_invalidate = 1;
560 if (s->ds->have_gfx) {
564 y1 = (s->y_base + s->y) % s->total_height;
565 y = y1 - s->y_displayed;
567 y += s->total_height;
569 c = &s->cells[y1 * s->width + x];
570 if (show && s->cursor_visible_phase) {
571 TextAttributes t_attrib = s->t_attrib_default;
572 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
573 vga_putcharxy(s, x, y, c->ch, &t_attrib);
575 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
577 invalidate_xy(s, x, y);
582 static void console_refresh(QemuConsole *s)
584 DisplaySurface *surface = qemu_console_surface(s);
588 if (!qemu_console_is_visible(s)) {
592 if (s->ds->have_text) {
595 s->text_x[1] = s->width - 1;
596 s->text_y[1] = s->height - 1;
597 s->cursor_invalidate = 1;
600 if (s->ds->have_gfx) {
601 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
602 color_table_rgb[0][COLOR_BLACK]);
604 for (y = 0; y < s->height; y++) {
605 c = s->cells + y1 * s->width;
606 for (x = 0; x < s->width; x++) {
607 vga_putcharxy(s, x, y, c->ch,
611 if (++y1 == s->total_height) {
615 console_show_cursor(s, 1);
616 dpy_gfx_update(s, 0, 0,
617 surface_width(surface), surface_height(surface));
621 static void console_scroll(QemuConsole *s, int ydelta)
626 for(i = 0; i < ydelta; i++) {
627 if (s->y_displayed == s->y_base)
629 if (++s->y_displayed == s->total_height)
634 i = s->backscroll_height;
635 if (i > s->total_height - s->height)
636 i = s->total_height - s->height;
639 y1 += s->total_height;
640 for(i = 0; i < ydelta; i++) {
641 if (s->y_displayed == y1)
643 if (--s->y_displayed < 0)
644 s->y_displayed = s->total_height - 1;
650 static void console_put_lf(QemuConsole *s)
656 if (s->y >= s->height) {
657 s->y = s->height - 1;
659 if (s->y_displayed == s->y_base) {
660 if (++s->y_displayed == s->total_height)
663 if (++s->y_base == s->total_height)
665 if (s->backscroll_height < s->total_height)
666 s->backscroll_height++;
667 y1 = (s->y_base + s->height - 1) % s->total_height;
668 c = &s->cells[y1 * s->width];
669 for(x = 0; x < s->width; x++) {
671 c->t_attrib = s->t_attrib_default;
674 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
675 if (s->ds->have_text) {
678 s->text_x[1] = s->width - 1;
679 s->text_y[1] = s->height - 1;
682 if (s->ds->have_gfx) {
683 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
684 s->width * FONT_WIDTH,
685 (s->height - 1) * FONT_HEIGHT);
686 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
687 s->width * FONT_WIDTH, FONT_HEIGHT,
688 color_table_rgb[0][s->t_attrib_default.bgcol]);
691 s->update_x1 = s->width * FONT_WIDTH;
692 s->update_y1 = s->height * FONT_HEIGHT;
698 /* Set console attributes depending on the current escape codes.
699 * NOTE: I know this code is not very efficient (checking every color for it
700 * self) but it is more readable and better maintainable.
702 static void console_handle_escape(QemuConsole *s)
706 for (i=0; i<s->nb_esc_params; i++) {
707 switch (s->esc_params[i]) {
708 case 0: /* reset all console attributes to default */
709 s->t_attrib = s->t_attrib_default;
712 s->t_attrib.bold = 1;
715 s->t_attrib.uline = 1;
718 s->t_attrib.blink = 1;
721 s->t_attrib.invers = 1;
724 s->t_attrib.unvisible = 1;
727 s->t_attrib.bold = 0;
730 s->t_attrib.uline = 0;
733 s->t_attrib.blink = 0;
736 s->t_attrib.invers = 0;
739 s->t_attrib.unvisible = 0;
741 /* set foreground color */
743 s->t_attrib.fgcol=COLOR_BLACK;
746 s->t_attrib.fgcol=COLOR_RED;
749 s->t_attrib.fgcol=COLOR_GREEN;
752 s->t_attrib.fgcol=COLOR_YELLOW;
755 s->t_attrib.fgcol=COLOR_BLUE;
758 s->t_attrib.fgcol=COLOR_MAGENTA;
761 s->t_attrib.fgcol=COLOR_CYAN;
764 s->t_attrib.fgcol=COLOR_WHITE;
766 /* set background color */
768 s->t_attrib.bgcol=COLOR_BLACK;
771 s->t_attrib.bgcol=COLOR_RED;
774 s->t_attrib.bgcol=COLOR_GREEN;
777 s->t_attrib.bgcol=COLOR_YELLOW;
780 s->t_attrib.bgcol=COLOR_BLUE;
783 s->t_attrib.bgcol=COLOR_MAGENTA;
786 s->t_attrib.bgcol=COLOR_CYAN;
789 s->t_attrib.bgcol=COLOR_WHITE;
795 static void console_clear_xy(QemuConsole *s, int x, int y)
797 int y1 = (s->y_base + y) % s->total_height;
798 TextCell *c = &s->cells[y1 * s->width + x];
800 c->t_attrib = s->t_attrib_default;
804 /* set cursor, checking bounds */
805 static void set_cursor(QemuConsole *s, int x, int y)
813 if (y >= s->height) {
824 static void console_putchar(QemuConsole *s, int ch)
833 case '\r': /* carriage return */
836 case '\n': /* newline */
839 case '\b': /* backspace */
843 case '\t': /* tabspace */
844 if (s->x + (8 - (s->x % 8)) > s->width) {
848 s->x = s->x + (8 - (s->x % 8));
851 case '\a': /* alert aka. bell */
852 /* TODO: has to be implemented */
855 /* SI (shift in), character set 0 (ignored) */
858 /* SO (shift out), character set 1 (ignored) */
860 case 27: /* esc (introducing an escape sequence) */
861 s->state = TTY_STATE_ESC;
864 if (s->x >= s->width) {
869 y1 = (s->y_base + s->y) % s->total_height;
870 c = &s->cells[y1 * s->width + s->x];
872 c->t_attrib = s->t_attrib;
873 update_xy(s, s->x, s->y);
878 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
880 for(i=0;i<MAX_ESC_PARAMS;i++)
881 s->esc_params[i] = 0;
882 s->nb_esc_params = 0;
883 s->state = TTY_STATE_CSI;
885 s->state = TTY_STATE_NORM;
888 case TTY_STATE_CSI: /* handle escape sequence parameters */
889 if (ch >= '0' && ch <= '9') {
890 if (s->nb_esc_params < MAX_ESC_PARAMS) {
891 int *param = &s->esc_params[s->nb_esc_params];
892 int digit = (ch - '0');
894 *param = (*param <= (INT_MAX - digit) / 10) ?
895 *param * 10 + digit : INT_MAX;
898 if (s->nb_esc_params < MAX_ESC_PARAMS)
903 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
904 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
906 s->state = TTY_STATE_NORM;
910 if (s->esc_params[0] == 0) {
911 s->esc_params[0] = 1;
913 set_cursor(s, s->x, s->y - s->esc_params[0]);
916 /* move cursor down */
917 if (s->esc_params[0] == 0) {
918 s->esc_params[0] = 1;
920 set_cursor(s, s->x, s->y + s->esc_params[0]);
923 /* move cursor right */
924 if (s->esc_params[0] == 0) {
925 s->esc_params[0] = 1;
927 set_cursor(s, s->x + s->esc_params[0], s->y);
930 /* move cursor left */
931 if (s->esc_params[0] == 0) {
932 s->esc_params[0] = 1;
934 set_cursor(s, s->x - s->esc_params[0], s->y);
937 /* move cursor to column */
938 set_cursor(s, s->esc_params[0] - 1, s->y);
942 /* move cursor to row, column */
943 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
946 switch (s->esc_params[0]) {
948 /* clear to end of screen */
949 for (y = s->y; y < s->height; y++) {
950 for (x = 0; x < s->width; x++) {
951 if (y == s->y && x < s->x) {
954 console_clear_xy(s, x, y);
959 /* clear from beginning of screen */
960 for (y = 0; y <= s->y; y++) {
961 for (x = 0; x < s->width; x++) {
962 if (y == s->y && x > s->x) {
965 console_clear_xy(s, x, y);
970 /* clear entire screen */
971 for (y = 0; y <= s->height; y++) {
972 for (x = 0; x < s->width; x++) {
973 console_clear_xy(s, x, y);
980 switch (s->esc_params[0]) {
983 for(x = s->x; x < s->width; x++) {
984 console_clear_xy(s, x, s->y);
988 /* clear from beginning of line */
989 for (x = 0; x <= s->x; x++) {
990 console_clear_xy(s, x, s->y);
994 /* clear entire line */
995 for(x = 0; x < s->width; x++) {
996 console_clear_xy(s, x, s->y);
1002 console_handle_escape(s);
1005 /* report cursor position */
1006 /* TODO: send ESC[row;colR */
1009 /* save cursor position */
1014 /* restore cursor position */
1019 #ifdef DEBUG_CONSOLE
1020 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1029 void console_select(unsigned int index)
1031 DisplayChangeListener *dcl;
1034 if (index >= MAX_CONSOLES)
1037 trace_console_select(index);
1038 s = qemu_console_lookup_by_index(index);
1040 DisplayState *ds = s->ds;
1042 if (active_console && active_console->cursor_timer) {
1043 timer_del(active_console->cursor_timer);
1047 QLIST_FOREACH(dcl, &ds->listeners, next) {
1048 if (dcl->con != NULL) {
1051 if (dcl->ops->dpy_gfx_switch) {
1052 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1055 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1056 surface_height(s->surface));
1058 if (ds->have_text) {
1059 dpy_text_resize(s, s->width, s->height);
1061 if (s->cursor_timer) {
1062 timer_mod(s->cursor_timer,
1063 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1068 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1070 QemuConsole *s = chr->opaque;
1073 s->update_x0 = s->width * FONT_WIDTH;
1074 s->update_y0 = s->height * FONT_HEIGHT;
1077 console_show_cursor(s, 0);
1078 for(i = 0; i < len; i++) {
1079 console_putchar(s, buf[i]);
1081 console_show_cursor(s, 1);
1082 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1083 dpy_gfx_update(s, s->update_x0, s->update_y0,
1084 s->update_x1 - s->update_x0,
1085 s->update_y1 - s->update_y0);
1090 static void kbd_send_chars(void *opaque)
1092 QemuConsole *s = opaque;
1096 len = qemu_chr_be_can_write(s->chr);
1097 if (len > s->out_fifo.count)
1098 len = s->out_fifo.count;
1100 if (len > sizeof(buf))
1102 qemu_fifo_read(&s->out_fifo, buf, len);
1103 qemu_chr_be_write(s->chr, buf, len);
1105 /* characters are pending: we send them a bit later (XXX:
1106 horrible, should change char device API) */
1107 if (s->out_fifo.count > 0) {
1108 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1112 /* called when an ascii key is pressed */
1113 void kbd_put_keysym(int keysym)
1116 uint8_t buf[16], *q;
1120 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1124 case QEMU_KEY_CTRL_UP:
1125 console_scroll(s, -1);
1127 case QEMU_KEY_CTRL_DOWN:
1128 console_scroll(s, 1);
1130 case QEMU_KEY_CTRL_PAGEUP:
1131 console_scroll(s, -10);
1133 case QEMU_KEY_CTRL_PAGEDOWN:
1134 console_scroll(s, 10);
1137 /* convert the QEMU keysym to VT100 key string */
1139 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1142 c = keysym - 0xe100;
1144 *q++ = '0' + (c / 10);
1145 *q++ = '0' + (c % 10);
1147 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1150 *q++ = keysym & 0xff;
1151 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1152 console_puts(s->chr, (const uint8_t *) "\r", 1);
1158 console_puts(s->chr, buf, q - buf);
1160 if (s->chr->chr_read) {
1161 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1168 static void text_console_invalidate(void *opaque)
1170 QemuConsole *s = (QemuConsole *) opaque;
1172 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1173 text_console_resize(s);
1178 static void text_console_update(void *opaque, console_ch_t *chardata)
1180 QemuConsole *s = (QemuConsole *) opaque;
1183 if (s->text_x[0] <= s->text_x[1]) {
1184 src = (s->y_base + s->text_y[0]) * s->width;
1185 chardata += s->text_y[0] * s->width;
1186 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1187 for (j = 0; j < s->width; j ++, src ++)
1188 console_write_ch(chardata ++, s->cells[src].ch |
1189 (s->cells[src].t_attrib.fgcol << 12) |
1190 (s->cells[src].t_attrib.bgcol << 8) |
1191 (s->cells[src].t_attrib.bold << 21));
1192 dpy_text_update(s, s->text_x[0], s->text_y[0],
1193 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1194 s->text_x[0] = s->width;
1195 s->text_y[0] = s->height;
1199 if (s->cursor_invalidate) {
1200 dpy_text_cursor(s, s->x, s->y);
1201 s->cursor_invalidate = 0;
1205 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1207 Error *local_err = NULL;
1212 if (nb_consoles >= MAX_CONSOLES)
1215 obj = object_new(TYPE_QEMU_CONSOLE);
1216 s = QEMU_CONSOLE(obj);
1217 object_property_add_link(obj, "device", TYPE_DEVICE,
1218 (Object **)&s->device, &local_err);
1220 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1221 (console_type == GRAPHIC_CONSOLE))) {
1225 s->console_type = console_type;
1226 if (console_type != GRAPHIC_CONSOLE) {
1227 s->index = nb_consoles;
1228 consoles[nb_consoles++] = s;
1230 /* HACK: Put graphical consoles before text consoles. */
1231 for (i = nb_consoles; i > 0; i--) {
1232 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1234 consoles[i] = consoles[i - 1];
1235 consoles[i]->index = i;
1244 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1245 int linesize, PixelFormat pf, int newflags)
1249 qemu_pixman_image_unref(surface->image);
1250 surface->image = NULL;
1252 surface->format = qemu_pixman_get_format(&pf);
1253 assert(surface->format != 0);
1254 surface->image = pixman_image_create_bits(surface->format,
1257 assert(surface->image != NULL);
1259 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1260 #ifdef HOST_WORDS_BIGENDIAN
1261 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1265 DisplaySurface *qemu_create_displaysurface(int width, int height)
1267 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1268 int linesize = width * 4;
1270 trace_displaysurface_create(surface, width, height);
1271 qemu_alloc_display(surface, width, height, linesize,
1272 qemu_default_pixelformat(32), 0);
1276 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1277 int linesize, uint8_t *data,
1280 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1282 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1284 surface->pf = qemu_different_endianness_pixelformat(bpp);
1286 surface->pf = qemu_default_pixelformat(bpp);
1289 surface->format = qemu_pixman_get_format(&surface->pf);
1290 assert(surface->format != 0);
1291 surface->image = pixman_image_create_bits(surface->format,
1293 (void *)data, linesize);
1294 assert(surface->image != NULL);
1296 #ifdef HOST_WORDS_BIGENDIAN
1297 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1303 static DisplaySurface *qemu_create_dummy_surface(void)
1305 static const char msg[] =
1306 "This VM has no graphic display device.";
1307 DisplaySurface *surface = qemu_create_displaysurface(640, 480);
1308 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1309 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1310 pixman_image_t *glyph;
1314 x = (640/FONT_WIDTH - len) / 2;
1315 y = (480/FONT_HEIGHT - 1) / 2;
1316 for (i = 0; i < len; i++) {
1317 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1318 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1319 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1320 qemu_pixman_image_unref(glyph);
1325 void qemu_free_displaysurface(DisplaySurface *surface)
1327 if (surface == NULL) {
1330 trace_displaysurface_free(surface);
1331 qemu_pixman_image_unref(surface->image);
1335 void register_displaychangelistener(DisplayChangeListener *dcl)
1337 static DisplaySurface *dummy;
1340 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1341 dcl->ds = get_alloc_displaystate();
1342 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1343 gui_setup_refresh(dcl->ds);
1348 con = active_console;
1350 if (dcl->ops->dpy_gfx_switch) {
1352 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1355 dummy = qemu_create_dummy_surface();
1357 dcl->ops->dpy_gfx_switch(dcl, dummy);
1362 void update_displaychangelistener(DisplayChangeListener *dcl,
1365 DisplayState *ds = dcl->ds;
1367 dcl->update_interval = interval;
1368 if (!ds->refreshing && ds->update_interval > interval) {
1369 timer_mod(ds->gui_timer, ds->last_update + interval);
1373 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1375 DisplayState *ds = dcl->ds;
1376 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1380 QLIST_REMOVE(dcl, next);
1381 gui_setup_refresh(ds);
1384 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1386 DisplayState *s = con->ds;
1387 DisplayChangeListener *dcl;
1388 int width = surface_width(con->surface);
1389 int height = surface_height(con->surface);
1395 w = MIN(w, width - x);
1396 h = MIN(h, height - y);
1398 if (!qemu_console_is_visible(con)) {
1401 QLIST_FOREACH(dcl, &s->listeners, next) {
1402 if (con != (dcl->con ? dcl->con : active_console)) {
1405 if (dcl->ops->dpy_gfx_update) {
1406 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1411 void dpy_gfx_replace_surface(QemuConsole *con,
1412 DisplaySurface *surface)
1414 DisplayState *s = con->ds;
1415 DisplaySurface *old_surface = con->surface;
1416 DisplayChangeListener *dcl;
1418 con->surface = surface;
1419 QLIST_FOREACH(dcl, &s->listeners, next) {
1420 if (con != (dcl->con ? dcl->con : active_console)) {
1423 if (dcl->ops->dpy_gfx_switch) {
1424 dcl->ops->dpy_gfx_switch(dcl, surface);
1427 qemu_free_displaysurface(old_surface);
1430 void dpy_refresh(DisplayState *s)
1432 DisplayChangeListener *dcl;
1434 QLIST_FOREACH(dcl, &s->listeners, next) {
1435 if (dcl->ops->dpy_refresh) {
1436 dcl->ops->dpy_refresh(dcl);
1441 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1442 int dst_x, int dst_y, int w, int h)
1444 DisplayState *s = con->ds;
1445 DisplayChangeListener *dcl;
1447 if (!qemu_console_is_visible(con)) {
1450 QLIST_FOREACH(dcl, &s->listeners, next) {
1451 if (con != (dcl->con ? dcl->con : active_console)) {
1454 if (dcl->ops->dpy_gfx_copy) {
1455 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1457 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1462 void dpy_text_cursor(QemuConsole *con, int x, int y)
1464 DisplayState *s = con->ds;
1465 DisplayChangeListener *dcl;
1467 if (!qemu_console_is_visible(con)) {
1470 QLIST_FOREACH(dcl, &s->listeners, next) {
1471 if (con != (dcl->con ? dcl->con : active_console)) {
1474 if (dcl->ops->dpy_text_cursor) {
1475 dcl->ops->dpy_text_cursor(dcl, x, y);
1480 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1482 DisplayState *s = con->ds;
1483 DisplayChangeListener *dcl;
1485 if (!qemu_console_is_visible(con)) {
1488 QLIST_FOREACH(dcl, &s->listeners, next) {
1489 if (con != (dcl->con ? dcl->con : active_console)) {
1492 if (dcl->ops->dpy_text_update) {
1493 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1498 void dpy_text_resize(QemuConsole *con, int w, int h)
1500 DisplayState *s = con->ds;
1501 struct DisplayChangeListener *dcl;
1503 if (!qemu_console_is_visible(con)) {
1506 QLIST_FOREACH(dcl, &s->listeners, next) {
1507 if (con != (dcl->con ? dcl->con : active_console)) {
1510 if (dcl->ops->dpy_text_resize) {
1511 dcl->ops->dpy_text_resize(dcl, w, h);
1516 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1518 DisplayState *s = con->ds;
1519 DisplayChangeListener *dcl;
1521 if (!qemu_console_is_visible(con)) {
1524 QLIST_FOREACH(dcl, &s->listeners, next) {
1525 if (con != (dcl->con ? dcl->con : active_console)) {
1528 if (dcl->ops->dpy_mouse_set) {
1529 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1534 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1536 DisplayState *s = con->ds;
1537 DisplayChangeListener *dcl;
1539 if (!qemu_console_is_visible(con)) {
1542 QLIST_FOREACH(dcl, &s->listeners, next) {
1543 if (con != (dcl->con ? dcl->con : active_console)) {
1546 if (dcl->ops->dpy_cursor_define) {
1547 dcl->ops->dpy_cursor_define(dcl, cursor);
1552 bool dpy_cursor_define_supported(QemuConsole *con)
1554 DisplayState *s = con->ds;
1555 DisplayChangeListener *dcl;
1557 QLIST_FOREACH(dcl, &s->listeners, next) {
1558 if (dcl->ops->dpy_cursor_define) {
1565 /***********************************************************/
1566 /* register display */
1568 /* console.c internal use only */
1569 static DisplayState *get_alloc_displaystate(void)
1571 if (!display_state) {
1572 display_state = g_new0(DisplayState, 1);
1574 return display_state;
1578 * Called by main(), after creating QemuConsoles
1579 * and before initializing ui (sdl/vnc/...).
1581 DisplayState *init_displaystate(void)
1583 Error *local_err = NULL;
1587 if (!display_state) {
1588 display_state = g_new0(DisplayState, 1);
1591 for (i = 0; i < nb_consoles; i++) {
1592 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1593 consoles[i]->ds == NULL) {
1594 text_console_do_init(consoles[i]->chr, display_state);
1597 /* Hook up into the qom tree here (not in new_console()), once
1598 * all QemuConsoles are created and the order / numbering
1599 * doesn't change any more */
1600 name = g_strdup_printf("console[%d]", i);
1601 object_property_add_child(container_get(object_get_root(), "/backend"),
1602 name, OBJECT(consoles[i]), &local_err);
1606 return display_state;
1609 QemuConsole *graphic_console_init(DeviceState *dev,
1610 const GraphicHwOps *hw_ops,
1613 Error *local_err = NULL;
1619 ds = get_alloc_displaystate();
1620 trace_console_gfx_new();
1621 s = new_console(ds, GRAPHIC_CONSOLE);
1625 object_property_set_link(OBJECT(s), OBJECT(dev),
1626 "device", &local_err);
1629 s->surface = qemu_create_displaysurface(width, height);
1633 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1635 if (index >= MAX_CONSOLES) {
1638 return consoles[index];
1641 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev)
1643 Error *local_err = NULL;
1647 for (i = 0; i < nb_consoles; i++) {
1651 obj = object_property_get_link(OBJECT(consoles[i]),
1652 "device", &local_err);
1653 if (DEVICE(obj) == dev) {
1660 bool qemu_console_is_visible(QemuConsole *con)
1662 return (con == active_console) || (con->dcls > 0);
1665 bool qemu_console_is_graphic(QemuConsole *con)
1668 con = active_console;
1670 return con && (con->console_type == GRAPHIC_CONSOLE);
1673 bool qemu_console_is_fixedsize(QemuConsole *con)
1676 con = active_console;
1678 return con && (con->console_type != TEXT_CONSOLE);
1681 static void text_console_set_echo(CharDriverState *chr, bool echo)
1683 QemuConsole *s = chr->opaque;
1688 static void text_console_update_cursor(void *opaque)
1690 QemuConsole *s = opaque;
1692 s->cursor_visible_phase = !s->cursor_visible_phase;
1693 graphic_hw_invalidate(s);
1694 timer_mod(s->cursor_timer,
1695 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1698 static const GraphicHwOps text_console_ops = {
1699 .invalidate = text_console_invalidate,
1700 .text_update = text_console_update,
1703 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1706 int g_width = 80 * FONT_WIDTH;
1707 int g_height = 24 * FONT_HEIGHT;
1711 chr->chr_write = console_puts;
1713 s->out_fifo.buf = s->out_fifo_buf;
1714 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1715 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1720 s->total_height = DEFAULT_BACKSCROLL;
1724 if (active_console && active_console->surface) {
1725 g_width = surface_width(active_console->surface);
1726 g_height = surface_height(active_console->surface);
1728 s->surface = qemu_create_displaysurface(g_width, g_height);
1732 timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
1734 s->hw_ops = &text_console_ops;
1737 /* Set text attribute defaults */
1738 s->t_attrib_default.bold = 0;
1739 s->t_attrib_default.uline = 0;
1740 s->t_attrib_default.blink = 0;
1741 s->t_attrib_default.invers = 0;
1742 s->t_attrib_default.unvisible = 0;
1743 s->t_attrib_default.fgcol = COLOR_WHITE;
1744 s->t_attrib_default.bgcol = COLOR_BLACK;
1745 /* set current text attributes to default */
1746 s->t_attrib = s->t_attrib_default;
1747 text_console_resize(s);
1753 s->t_attrib.bgcol = COLOR_BLUE;
1754 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1755 console_puts(chr, (uint8_t*)msg, len);
1756 s->t_attrib = s->t_attrib_default;
1759 qemu_chr_be_generic_open(chr);
1764 static CharDriverState *text_console_init(ChardevVC *vc)
1766 CharDriverState *chr;
1769 unsigned height = 0;
1771 chr = g_malloc0(sizeof(CharDriverState));
1773 if (vc->has_width) {
1775 } else if (vc->has_cols) {
1776 width = vc->cols * FONT_WIDTH;
1779 if (vc->has_height) {
1780 height = vc->height;
1781 } else if (vc->has_rows) {
1782 height = vc->rows * FONT_HEIGHT;
1785 trace_console_txt_new(width, height);
1786 if (width == 0 || height == 0) {
1787 s = new_console(NULL, TEXT_CONSOLE);
1789 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1790 s->surface = qemu_create_displaysurface(width, height);
1800 chr->chr_set_echo = text_console_set_echo;
1801 /* console/chardev init sometimes completes elsewhere in a 2nd
1802 * stage, so defer OPENED events until they are fully initialized
1804 chr->explicit_be_open = true;
1806 if (display_state) {
1807 text_console_do_init(chr, display_state);
1812 static VcHandler *vc_handler = text_console_init;
1814 CharDriverState *vc_init(ChardevVC *vc)
1816 return vc_handler(vc);
1819 void register_vc_handler(VcHandler *handler)
1821 vc_handler = handler;
1824 void qemu_console_resize(QemuConsole *s, int width, int height)
1826 DisplaySurface *surface;
1828 assert(s->console_type == GRAPHIC_CONSOLE);
1829 surface = qemu_create_displaysurface(width, height);
1830 dpy_gfx_replace_surface(s, surface);
1833 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1834 int dst_x, int dst_y, int w, int h)
1836 assert(con->console_type == GRAPHIC_CONSOLE);
1837 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1840 DisplaySurface *qemu_console_surface(QemuConsole *console)
1842 return console->surface;
1845 DisplayState *qemu_console_displaystate(QemuConsole *console)
1850 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1854 memset(&pf, 0x00, sizeof(PixelFormat));
1856 pf.bits_per_pixel = bpp;
1857 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1858 pf.depth = bpp == 32 ? 24 : bpp;
1862 pf.rmask = 0x000000FF;
1863 pf.gmask = 0x0000FF00;
1864 pf.bmask = 0x00FF0000;
1876 pf.rmask = 0x0000FF00;
1877 pf.gmask = 0x00FF0000;
1878 pf.bmask = 0xFF000000;
1879 pf.amask = 0x00000000;
1899 PixelFormat qemu_default_pixelformat(int bpp)
1903 memset(&pf, 0x00, sizeof(PixelFormat));
1905 pf.bits_per_pixel = bpp;
1906 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1907 pf.depth = bpp == 32 ? 24 : bpp;
1911 pf.bits_per_pixel = 16;
1912 pf.rmask = 0x00007c00;
1913 pf.gmask = 0x000003E0;
1914 pf.bmask = 0x0000001F;
1926 pf.rmask = 0x0000F800;
1927 pf.gmask = 0x000007E0;
1928 pf.bmask = 0x0000001F;
1940 pf.rmask = 0x00FF0000;
1941 pf.gmask = 0x0000FF00;
1942 pf.bmask = 0x000000FF;
1954 pf.rmask = 0x00FF0000;
1955 pf.gmask = 0x0000FF00;
1956 pf.bmask = 0x000000FF;
1973 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1978 backend->vc = g_new0(ChardevVC, 1);
1980 val = qemu_opt_get_number(opts, "width", 0);
1982 backend->vc->has_width = true;
1983 backend->vc->width = val;
1986 val = qemu_opt_get_number(opts, "height", 0);
1988 backend->vc->has_height = true;
1989 backend->vc->height = val;
1992 val = qemu_opt_get_number(opts, "cols", 0);
1994 backend->vc->has_cols = true;
1995 backend->vc->cols = val;
1998 val = qemu_opt_get_number(opts, "rows", 0);
2000 backend->vc->has_rows = true;
2001 backend->vc->rows = val;
2005 static const TypeInfo qemu_console_info = {
2006 .name = TYPE_QEMU_CONSOLE,
2007 .parent = TYPE_OBJECT,
2008 .instance_size = sizeof(QemuConsole),
2009 .class_size = sizeof(QemuConsoleClass),
2013 static void register_types(void)
2015 type_register_static(&qemu_console_info);
2016 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
2020 type_init(register_types);