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 "qemu/timer.h"
27 #include "qmp-commands.h"
28 #include "char/char.h"
30 //#define DEBUG_CONSOLE
31 #define DEFAULT_BACKSCROLL 512
32 #define MAX_CONSOLES 12
33 #define CONSOLE_CURSOR_PERIOD 500
35 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
36 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
38 typedef struct TextAttributes {
48 typedef struct TextCell {
50 TextAttributes t_attrib;
53 #define MAX_ESC_PARAMS 3
61 typedef struct QEMUFIFO {
64 int count, wptr, rptr;
67 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
71 l = f->buf_size - f->count;
76 l = f->buf_size - f->wptr;
79 memcpy(f->buf + f->wptr, buf, l);
81 if (f->wptr >= f->buf_size)
90 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
98 l = f->buf_size - f->rptr;
101 memcpy(buf, f->buf + f->rptr, l);
103 if (f->rptr >= f->buf_size)
115 TEXT_CONSOLE_FIXED_SIZE
120 console_type_t console_type;
123 /* Graphic console state. */
124 vga_hw_update_ptr hw_update;
125 vga_hw_invalidate_ptr hw_invalidate;
126 vga_hw_screen_dump_ptr hw_screen_dump;
127 vga_hw_text_update_ptr hw_text_update;
129 int g_width, g_height;
131 /* Text console state */
135 int backscroll_height;
137 int x_saved, y_saved;
140 TextAttributes t_attrib_default; /* default text attributes */
141 TextAttributes t_attrib; /* currently active text attributes */
143 int text_x[2], text_y[2], cursor_invalidate;
145 bool cursor_visible_phase;
146 QEMUTimer *cursor_timer;
154 int esc_params[MAX_ESC_PARAMS];
157 CharDriverState *chr;
158 /* fifo for key pressed */
160 uint8_t out_fifo_buf[16];
161 QEMUTimer *kbd_timer;
164 static DisplayState *display_state;
165 static QemuConsole *active_console;
166 static QemuConsole *consoles[MAX_CONSOLES];
167 static int nb_consoles = 0;
169 void vga_hw_update(void)
171 if (active_console && active_console->hw_update)
172 active_console->hw_update(active_console->hw);
175 void vga_hw_invalidate(void)
177 if (active_console && active_console->hw_invalidate)
178 active_console->hw_invalidate(active_console->hw);
181 void qmp_screendump(const char *filename, Error **errp)
183 QemuConsole *previous_active_console;
186 previous_active_console = active_console;
187 cswitch = previous_active_console && previous_active_console->index != 0;
189 /* There is currently no way of specifying which screen we want to dump,
190 so always dump the first one. */
194 if (consoles[0] && consoles[0]->hw_screen_dump) {
195 consoles[0]->hw_screen_dump(consoles[0]->hw, filename, cswitch, errp);
197 error_setg(errp, "device doesn't support screendump");
201 console_select(previous_active_console->index);
205 void vga_hw_text_update(console_ch_t *chardata)
207 if (active_console && active_console->hw_text_update)
208 active_console->hw_text_update(active_console->hw, chardata);
211 static void vga_fill_rect(QemuConsole *con,
212 int posx, int posy, int width, int height,
215 DisplaySurface *surface = qemu_console_surface(con);
219 bpp = surface_bytes_per_pixel(surface);
220 d1 = surface_data(surface) +
221 surface_stride(surface) * posy + bpp * posx;
222 for (y = 0; y < height; y++) {
226 for (x = 0; x < width; x++) {
227 *((uint8_t *)d) = color;
232 for (x = 0; x < width; x++) {
233 *((uint16_t *)d) = color;
238 for (x = 0; x < width; x++) {
239 *((uint32_t *)d) = color;
244 d1 += surface_stride(surface);
248 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
249 static void vga_bitblt(QemuConsole *con,
250 int xs, int ys, int xd, int yd, int w, int h)
252 DisplaySurface *surface = qemu_console_surface(con);
257 bpp = surface_bytes_per_pixel(surface);
260 s = surface_data(surface) +
261 surface_stride(surface) * ys + bpp * xs;
262 d = surface_data(surface) +
263 surface_stride(surface) * yd + bpp * xd;
264 for (y = 0; y < h; y++) {
266 d += surface_stride(surface);
267 s += surface_stride(surface);
270 s = surface_data(surface) +
271 surface_stride(surface) * (ys + h - 1) + bpp * xs;
272 d = surface_data(surface) +
273 surface_stride(surface) * (yd + h - 1) + bpp * xd;
274 for (y = 0; y < h; y++) {
276 d -= surface_stride(surface);
277 s -= surface_stride(surface);
282 /***********************************************************/
283 /* basic char display */
285 #define FONT_HEIGHT 16
290 #define cbswap_32(__x) \
292 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
293 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
294 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
295 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
297 #ifdef HOST_WORDS_BIGENDIAN
300 #define PAT(x) cbswap_32(x)
303 static const uint32_t dmask16[16] = {
322 static const uint32_t dmask4[4] = {
329 #ifndef CONFIG_CURSES
342 static const uint32_t color_table_rgb[2][8] = {
344 QEMU_RGB(0x00, 0x00, 0x00), /* black */
345 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
346 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
347 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
348 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
349 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
350 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
351 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
354 QEMU_RGB(0x00, 0x00, 0x00), /* black */
355 QEMU_RGB(0xff, 0x00, 0x00), /* red */
356 QEMU_RGB(0x00, 0xff, 0x00), /* green */
357 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
358 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
359 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
360 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
361 QEMU_RGB(0xff, 0xff, 0xff), /* white */
366 static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
368 if (t_attrib->bold) {
373 if (t_attrib->uline) {
378 if (t_attrib->blink) {
383 if (t_attrib->invers) {
388 if (t_attrib->unvisible) {
394 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
398 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
399 TextAttributes *t_attrib)
401 DisplaySurface *surface = qemu_console_surface(s);
403 const uint8_t *font_ptr;
404 unsigned int font_data, linesize, xorcol, bpp;
406 unsigned int fgcol, bgcol;
409 printf("x: %2i y: %2i", x, y);
410 console_print_text_attributes(t_attrib, ch);
413 if (t_attrib->invers) {
414 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
415 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
417 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
418 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
421 bpp = surface_bytes_per_pixel(surface);
422 d = surface_data(surface) +
423 surface_stride(surface) * y * FONT_HEIGHT + bpp * x * FONT_WIDTH;
424 linesize = surface_stride(surface);
425 font_ptr = vgafont16 + FONT_HEIGHT * ch;
426 xorcol = bgcol ^ fgcol;
427 switch (surface_bits_per_pixel(surface)) {
429 for(i = 0; i < FONT_HEIGHT; i++) {
430 font_data = *font_ptr++;
432 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
435 ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
436 ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
442 for(i = 0; i < FONT_HEIGHT; i++) {
443 font_data = *font_ptr++;
445 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
448 ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
449 ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
450 ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
451 ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
456 for(i = 0; i < FONT_HEIGHT; i++) {
457 font_data = *font_ptr++;
458 if (t_attrib->uline && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
461 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
462 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
463 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
464 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
465 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
466 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
467 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
468 ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
475 static void text_console_resize(QemuConsole *s)
477 TextCell *cells, *c, *c1;
478 int w1, x, y, last_width;
480 last_width = s->width;
481 s->width = s->g_width / FONT_WIDTH;
482 s->height = s->g_height / FONT_HEIGHT;
488 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
489 for(y = 0; y < s->total_height; y++) {
490 c = &cells[y * s->width];
492 c1 = &s->cells[y * last_width];
493 for(x = 0; x < w1; x++) {
497 for(x = w1; x < s->width; x++) {
499 c->t_attrib = s->t_attrib_default;
507 static inline void text_update_xy(QemuConsole *s, int x, int y)
509 s->text_x[0] = MIN(s->text_x[0], x);
510 s->text_x[1] = MAX(s->text_x[1], x);
511 s->text_y[0] = MIN(s->text_y[0], y);
512 s->text_y[1] = MAX(s->text_y[1], y);
515 static void invalidate_xy(QemuConsole *s, int x, int y)
517 if (s->update_x0 > x * FONT_WIDTH)
518 s->update_x0 = x * FONT_WIDTH;
519 if (s->update_y0 > y * FONT_HEIGHT)
520 s->update_y0 = y * FONT_HEIGHT;
521 if (s->update_x1 < (x + 1) * FONT_WIDTH)
522 s->update_x1 = (x + 1) * FONT_WIDTH;
523 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
524 s->update_y1 = (y + 1) * FONT_HEIGHT;
527 static void update_xy(QemuConsole *s, int x, int y)
532 if (s != active_console) {
536 if (s->ds->have_text) {
537 text_update_xy(s, x, y);
540 if (s->ds->have_gfx) {
541 y1 = (s->y_base + y) % s->total_height;
542 y2 = y1 - s->y_displayed;
544 y2 += s->total_height;
545 if (y2 < s->height) {
546 c = &s->cells[y1 * s->width + x];
547 vga_putcharxy(s, x, y2, c->ch,
549 invalidate_xy(s, x, y2);
554 static void console_show_cursor(QemuConsole *s, int show)
560 if (s != active_console) {
564 if (s->ds->have_text) {
565 s->cursor_invalidate = 1;
568 if (s->ds->have_gfx) {
572 y1 = (s->y_base + s->y) % s->total_height;
573 y = y1 - s->y_displayed;
575 y += s->total_height;
577 c = &s->cells[y1 * s->width + x];
578 if (show && s->cursor_visible_phase) {
579 TextAttributes t_attrib = s->t_attrib_default;
580 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
581 vga_putcharxy(s, x, y, c->ch, &t_attrib);
583 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
585 invalidate_xy(s, x, y);
590 static void console_refresh(QemuConsole *s)
592 DisplaySurface *surface = qemu_console_surface(s);
596 if (s != active_console)
599 if (s->ds->have_text) {
602 s->text_x[1] = s->width - 1;
603 s->text_y[1] = s->height - 1;
604 s->cursor_invalidate = 1;
607 if (s->ds->have_gfx) {
608 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
609 color_table_rgb[0][COLOR_BLACK]);
611 for (y = 0; y < s->height; y++) {
612 c = s->cells + y1 * s->width;
613 for (x = 0; x < s->width; x++) {
614 vga_putcharxy(s, x, y, c->ch,
618 if (++y1 == s->total_height) {
622 console_show_cursor(s, 1);
623 dpy_gfx_update(s, 0, 0,
624 surface_width(surface), surface_height(surface));
628 static void console_scroll(int ydelta)
634 if (!s || (s->console_type == GRAPHIC_CONSOLE))
638 for(i = 0; i < ydelta; i++) {
639 if (s->y_displayed == s->y_base)
641 if (++s->y_displayed == s->total_height)
646 i = s->backscroll_height;
647 if (i > s->total_height - s->height)
648 i = s->total_height - s->height;
651 y1 += s->total_height;
652 for(i = 0; i < ydelta; i++) {
653 if (s->y_displayed == y1)
655 if (--s->y_displayed < 0)
656 s->y_displayed = s->total_height - 1;
662 static void console_put_lf(QemuConsole *s)
668 if (s->y >= s->height) {
669 s->y = s->height - 1;
671 if (s->y_displayed == s->y_base) {
672 if (++s->y_displayed == s->total_height)
675 if (++s->y_base == s->total_height)
677 if (s->backscroll_height < s->total_height)
678 s->backscroll_height++;
679 y1 = (s->y_base + s->height - 1) % s->total_height;
680 c = &s->cells[y1 * s->width];
681 for(x = 0; x < s->width; x++) {
683 c->t_attrib = s->t_attrib_default;
686 if (s == active_console && s->y_displayed == s->y_base) {
687 if (s->ds->have_text) {
690 s->text_x[1] = s->width - 1;
691 s->text_y[1] = s->height - 1;
694 if (s->ds->have_gfx) {
695 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
696 s->width * FONT_WIDTH,
697 (s->height - 1) * FONT_HEIGHT);
698 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
699 s->width * FONT_WIDTH, FONT_HEIGHT,
700 color_table_rgb[0][s->t_attrib_default.bgcol]);
703 s->update_x1 = s->width * FONT_WIDTH;
704 s->update_y1 = s->height * FONT_HEIGHT;
710 /* Set console attributes depending on the current escape codes.
711 * NOTE: I know this code is not very efficient (checking every color for it
712 * self) but it is more readable and better maintainable.
714 static void console_handle_escape(QemuConsole *s)
718 for (i=0; i<s->nb_esc_params; i++) {
719 switch (s->esc_params[i]) {
720 case 0: /* reset all console attributes to default */
721 s->t_attrib = s->t_attrib_default;
724 s->t_attrib.bold = 1;
727 s->t_attrib.uline = 1;
730 s->t_attrib.blink = 1;
733 s->t_attrib.invers = 1;
736 s->t_attrib.unvisible = 1;
739 s->t_attrib.bold = 0;
742 s->t_attrib.uline = 0;
745 s->t_attrib.blink = 0;
748 s->t_attrib.invers = 0;
751 s->t_attrib.unvisible = 0;
753 /* set foreground color */
755 s->t_attrib.fgcol=COLOR_BLACK;
758 s->t_attrib.fgcol=COLOR_RED;
761 s->t_attrib.fgcol=COLOR_GREEN;
764 s->t_attrib.fgcol=COLOR_YELLOW;
767 s->t_attrib.fgcol=COLOR_BLUE;
770 s->t_attrib.fgcol=COLOR_MAGENTA;
773 s->t_attrib.fgcol=COLOR_CYAN;
776 s->t_attrib.fgcol=COLOR_WHITE;
778 /* set background color */
780 s->t_attrib.bgcol=COLOR_BLACK;
783 s->t_attrib.bgcol=COLOR_RED;
786 s->t_attrib.bgcol=COLOR_GREEN;
789 s->t_attrib.bgcol=COLOR_YELLOW;
792 s->t_attrib.bgcol=COLOR_BLUE;
795 s->t_attrib.bgcol=COLOR_MAGENTA;
798 s->t_attrib.bgcol=COLOR_CYAN;
801 s->t_attrib.bgcol=COLOR_WHITE;
807 static void console_clear_xy(QemuConsole *s, int x, int y)
809 int y1 = (s->y_base + y) % s->total_height;
810 TextCell *c = &s->cells[y1 * s->width + x];
812 c->t_attrib = s->t_attrib_default;
816 /* set cursor, checking bounds */
817 static void set_cursor(QemuConsole *s, int x, int y)
825 if (y >= s->height) {
836 static void console_putchar(QemuConsole *s, int ch)
845 case '\r': /* carriage return */
848 case '\n': /* newline */
851 case '\b': /* backspace */
855 case '\t': /* tabspace */
856 if (s->x + (8 - (s->x % 8)) > s->width) {
860 s->x = s->x + (8 - (s->x % 8));
863 case '\a': /* alert aka. bell */
864 /* TODO: has to be implemented */
867 /* SI (shift in), character set 0 (ignored) */
870 /* SO (shift out), character set 1 (ignored) */
872 case 27: /* esc (introducing an escape sequence) */
873 s->state = TTY_STATE_ESC;
876 if (s->x >= s->width) {
881 y1 = (s->y_base + s->y) % s->total_height;
882 c = &s->cells[y1 * s->width + s->x];
884 c->t_attrib = s->t_attrib;
885 update_xy(s, s->x, s->y);
890 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
892 for(i=0;i<MAX_ESC_PARAMS;i++)
893 s->esc_params[i] = 0;
894 s->nb_esc_params = 0;
895 s->state = TTY_STATE_CSI;
897 s->state = TTY_STATE_NORM;
900 case TTY_STATE_CSI: /* handle escape sequence parameters */
901 if (ch >= '0' && ch <= '9') {
902 if (s->nb_esc_params < MAX_ESC_PARAMS) {
903 int *param = &s->esc_params[s->nb_esc_params];
904 int digit = (ch - '0');
906 *param = (*param <= (INT_MAX - digit) / 10) ?
907 *param * 10 + digit : INT_MAX;
910 if (s->nb_esc_params < MAX_ESC_PARAMS)
915 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
916 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
918 s->state = TTY_STATE_NORM;
922 if (s->esc_params[0] == 0) {
923 s->esc_params[0] = 1;
925 set_cursor(s, s->x, s->y - s->esc_params[0]);
928 /* move cursor down */
929 if (s->esc_params[0] == 0) {
930 s->esc_params[0] = 1;
932 set_cursor(s, s->x, s->y + s->esc_params[0]);
935 /* move cursor right */
936 if (s->esc_params[0] == 0) {
937 s->esc_params[0] = 1;
939 set_cursor(s, s->x + s->esc_params[0], s->y);
942 /* move cursor left */
943 if (s->esc_params[0] == 0) {
944 s->esc_params[0] = 1;
946 set_cursor(s, s->x - s->esc_params[0], s->y);
949 /* move cursor to column */
950 set_cursor(s, s->esc_params[0] - 1, s->y);
954 /* move cursor to row, column */
955 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
958 switch (s->esc_params[0]) {
960 /* clear to end of screen */
961 for (y = s->y; y < s->height; y++) {
962 for (x = 0; x < s->width; x++) {
963 if (y == s->y && x < s->x) {
966 console_clear_xy(s, x, y);
971 /* clear from beginning of screen */
972 for (y = 0; y <= s->y; y++) {
973 for (x = 0; x < s->width; x++) {
974 if (y == s->y && x > s->x) {
977 console_clear_xy(s, x, y);
982 /* clear entire screen */
983 for (y = 0; y <= s->height; y++) {
984 for (x = 0; x < s->width; x++) {
985 console_clear_xy(s, x, y);
992 switch (s->esc_params[0]) {
995 for(x = s->x; x < s->width; x++) {
996 console_clear_xy(s, x, s->y);
1000 /* clear from beginning of line */
1001 for (x = 0; x <= s->x; x++) {
1002 console_clear_xy(s, x, s->y);
1006 /* clear entire line */
1007 for(x = 0; x < s->width; x++) {
1008 console_clear_xy(s, x, s->y);
1014 console_handle_escape(s);
1017 /* report cursor position */
1018 /* TODO: send ESC[row;colR */
1021 /* save cursor position */
1026 /* restore cursor position */
1031 #ifdef DEBUG_CONSOLE
1032 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1041 void console_select(unsigned int index)
1043 DisplaySurface *surface;
1046 if (index >= MAX_CONSOLES)
1048 if (active_console) {
1049 surface = qemu_console_surface(active_console);
1050 active_console->g_width = surface_width(surface);
1051 active_console->g_height = surface_height(surface);
1053 s = consoles[index];
1055 DisplayState *ds = s->ds;
1057 if (active_console && active_console->cursor_timer) {
1058 qemu_del_timer(active_console->cursor_timer);
1062 surface = qemu_create_displaysurface(s->g_width, s->g_height);
1063 dpy_gfx_replace_surface(s, surface);
1065 if (ds->have_text) {
1066 dpy_text_resize(s, s->width, s->height);
1068 if (s->cursor_timer) {
1069 qemu_mod_timer(s->cursor_timer,
1070 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1072 vga_hw_invalidate();
1076 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1078 QemuConsole *s = chr->opaque;
1081 s->update_x0 = s->width * FONT_WIDTH;
1082 s->update_y0 = s->height * FONT_HEIGHT;
1085 console_show_cursor(s, 0);
1086 for(i = 0; i < len; i++) {
1087 console_putchar(s, buf[i]);
1089 console_show_cursor(s, 1);
1090 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1091 dpy_gfx_update(s, s->update_x0, s->update_y0,
1092 s->update_x1 - s->update_x0,
1093 s->update_y1 - s->update_y0);
1098 static void kbd_send_chars(void *opaque)
1100 QemuConsole *s = opaque;
1104 len = qemu_chr_be_can_write(s->chr);
1105 if (len > s->out_fifo.count)
1106 len = s->out_fifo.count;
1108 if (len > sizeof(buf))
1110 qemu_fifo_read(&s->out_fifo, buf, len);
1111 qemu_chr_be_write(s->chr, buf, len);
1113 /* characters are pending: we send them a bit later (XXX:
1114 horrible, should change char device API) */
1115 if (s->out_fifo.count > 0) {
1116 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
1120 /* called when an ascii key is pressed */
1121 void kbd_put_keysym(int keysym)
1124 uint8_t buf[16], *q;
1128 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1132 case QEMU_KEY_CTRL_UP:
1135 case QEMU_KEY_CTRL_DOWN:
1138 case QEMU_KEY_CTRL_PAGEUP:
1139 console_scroll(-10);
1141 case QEMU_KEY_CTRL_PAGEDOWN:
1145 /* convert the QEMU keysym to VT100 key string */
1147 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1150 c = keysym - 0xe100;
1152 *q++ = '0' + (c / 10);
1153 *q++ = '0' + (c % 10);
1155 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1158 *q++ = keysym & 0xff;
1159 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1160 console_puts(s->chr, (const uint8_t *) "\r", 1);
1166 console_puts(s->chr, buf, q - buf);
1168 if (s->chr->chr_read) {
1169 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1176 static void text_console_invalidate(void *opaque)
1178 QemuConsole *s = (QemuConsole *) opaque;
1179 DisplaySurface *surface = qemu_console_surface(s);
1181 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1182 s->g_width = surface_width(surface);
1183 s->g_height = surface_height(surface);
1184 text_console_resize(s);
1189 static void text_console_update(void *opaque, console_ch_t *chardata)
1191 QemuConsole *s = (QemuConsole *) opaque;
1194 if (s->text_x[0] <= s->text_x[1]) {
1195 src = (s->y_base + s->text_y[0]) * s->width;
1196 chardata += s->text_y[0] * s->width;
1197 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1198 for (j = 0; j < s->width; j ++, src ++)
1199 console_write_ch(chardata ++, s->cells[src].ch |
1200 (s->cells[src].t_attrib.fgcol << 12) |
1201 (s->cells[src].t_attrib.bgcol << 8) |
1202 (s->cells[src].t_attrib.bold << 21));
1203 dpy_text_update(s, s->text_x[0], s->text_y[0],
1204 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1205 s->text_x[0] = s->width;
1206 s->text_y[0] = s->height;
1210 if (s->cursor_invalidate) {
1211 dpy_text_cursor(s, s->x, s->y);
1212 s->cursor_invalidate = 0;
1216 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1221 if (nb_consoles >= MAX_CONSOLES)
1223 s = g_malloc0(sizeof(QemuConsole));
1224 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1225 (console_type == GRAPHIC_CONSOLE))) {
1229 s->console_type = console_type;
1230 if (console_type != GRAPHIC_CONSOLE) {
1231 s->index = nb_consoles;
1232 consoles[nb_consoles++] = s;
1234 /* HACK: Put graphical consoles before text consoles. */
1235 for (i = nb_consoles; i > 0; i--) {
1236 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1238 consoles[i] = consoles[i - 1];
1239 consoles[i]->index = i;
1248 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1249 int linesize, PixelFormat pf, int newflags)
1253 qemu_pixman_image_unref(surface->image);
1254 surface->image = NULL;
1256 surface->format = qemu_pixman_get_format(&pf);
1257 assert(surface->format != 0);
1258 surface->image = pixman_image_create_bits(surface->format,
1261 assert(surface->image != NULL);
1263 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1264 #ifdef HOST_WORDS_BIGENDIAN
1265 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1269 DisplaySurface *qemu_create_displaysurface(int width, int height)
1271 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1272 int linesize = width * 4;
1274 trace_displaysurface_create(surface, width, height);
1275 qemu_alloc_display(surface, width, height, linesize,
1276 qemu_default_pixelformat(32), 0);
1280 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1281 int linesize, uint8_t *data,
1284 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1286 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1288 surface->pf = qemu_different_endianness_pixelformat(bpp);
1290 surface->pf = qemu_default_pixelformat(bpp);
1293 surface->format = qemu_pixman_get_format(&surface->pf);
1294 assert(surface->format != 0);
1295 surface->image = pixman_image_create_bits(surface->format,
1297 (void *)data, linesize);
1298 assert(surface->image != NULL);
1300 #ifdef HOST_WORDS_BIGENDIAN
1301 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1307 void qemu_free_displaysurface(DisplaySurface *surface)
1309 if (surface == NULL) {
1312 trace_displaysurface_free(surface);
1313 qemu_pixman_image_unref(surface->image);
1317 void register_displaychangelistener(DisplayState *ds,
1318 DisplayChangeListener *dcl)
1320 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1322 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1323 gui_setup_refresh(ds);
1324 if (dcl->ops->dpy_gfx_switch) {
1325 dcl->ops->dpy_gfx_switch(dcl, ds->surface);
1329 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1331 DisplayState *ds = dcl->ds;
1332 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1333 QLIST_REMOVE(dcl, next);
1334 gui_setup_refresh(ds);
1337 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1339 DisplayState *s = con->ds;
1340 struct DisplayChangeListener *dcl;
1341 int width = pixman_image_get_width(s->surface->image);
1342 int height = pixman_image_get_height(s->surface->image);
1348 w = MIN(w, width - x);
1349 h = MIN(h, height - y);
1351 QLIST_FOREACH(dcl, &s->listeners, next) {
1352 if (dcl->ops->dpy_gfx_update) {
1353 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1358 void dpy_gfx_replace_surface(QemuConsole *con,
1359 DisplaySurface *surface)
1361 DisplayState *s = con->ds;
1362 DisplaySurface *old_surface = s->surface;
1363 struct DisplayChangeListener *dcl;
1365 s->surface = surface;
1366 QLIST_FOREACH(dcl, &s->listeners, next) {
1367 if (dcl->ops->dpy_gfx_switch) {
1368 dcl->ops->dpy_gfx_switch(dcl, surface);
1371 qemu_free_displaysurface(old_surface);
1374 void dpy_refresh(DisplayState *s)
1376 struct DisplayChangeListener *dcl;
1377 QLIST_FOREACH(dcl, &s->listeners, next) {
1378 if (dcl->ops->dpy_refresh) {
1379 dcl->ops->dpy_refresh(dcl);
1384 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1385 int dst_x, int dst_y, int w, int h)
1387 DisplayState *s = con->ds;
1388 struct DisplayChangeListener *dcl;
1389 QLIST_FOREACH(dcl, &s->listeners, next) {
1390 if (dcl->ops->dpy_gfx_copy) {
1391 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1393 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1398 void dpy_text_cursor(QemuConsole *con, int x, int y)
1400 DisplayState *s = con->ds;
1401 struct DisplayChangeListener *dcl;
1402 QLIST_FOREACH(dcl, &s->listeners, next) {
1403 if (dcl->ops->dpy_text_cursor) {
1404 dcl->ops->dpy_text_cursor(dcl, x, y);
1409 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1411 DisplayState *s = con->ds;
1412 struct DisplayChangeListener *dcl;
1413 QLIST_FOREACH(dcl, &s->listeners, next) {
1414 if (dcl->ops->dpy_text_update) {
1415 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1420 void dpy_text_resize(QemuConsole *con, int w, int h)
1422 DisplayState *s = con->ds;
1423 struct DisplayChangeListener *dcl;
1424 QLIST_FOREACH(dcl, &s->listeners, next) {
1425 if (dcl->ops->dpy_text_resize) {
1426 dcl->ops->dpy_text_resize(dcl, w, h);
1431 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1433 DisplayState *s = con->ds;
1434 struct DisplayChangeListener *dcl;
1435 QLIST_FOREACH(dcl, &s->listeners, next) {
1436 if (dcl->ops->dpy_mouse_set) {
1437 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1442 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1444 DisplayState *s = con->ds;
1445 struct DisplayChangeListener *dcl;
1446 QLIST_FOREACH(dcl, &s->listeners, next) {
1447 if (dcl->ops->dpy_cursor_define) {
1448 dcl->ops->dpy_cursor_define(dcl, cursor);
1453 bool dpy_cursor_define_supported(QemuConsole *con)
1455 DisplayState *s = con->ds;
1456 struct DisplayChangeListener *dcl;
1457 QLIST_FOREACH(dcl, &s->listeners, next) {
1458 if (dcl->ops->dpy_cursor_define) {
1465 static void dumb_display_init(void)
1467 DisplayState *ds = g_malloc0(sizeof(DisplayState));
1471 if (is_fixedsize_console()) {
1472 width = active_console->g_width;
1473 height = active_console->g_height;
1475 ds->surface = qemu_create_displaysurface(width, height);
1477 register_displaystate(ds);
1480 /***********************************************************/
1481 /* register display */
1483 void register_displaystate(DisplayState *ds)
1493 DisplayState *get_displaystate(void)
1495 if (!display_state) {
1496 dumb_display_init ();
1498 return display_state;
1501 QemuConsole *graphic_console_init(vga_hw_update_ptr update,
1502 vga_hw_invalidate_ptr invalidate,
1503 vga_hw_screen_dump_ptr screen_dump,
1504 vga_hw_text_update_ptr text_update,
1510 ds = (DisplayState *) g_malloc0(sizeof(DisplayState));
1511 s = new_console(ds, GRAPHIC_CONSOLE);
1512 s->hw_update = update;
1513 s->hw_invalidate = invalidate;
1514 s->hw_screen_dump = screen_dump;
1515 s->hw_text_update = text_update;
1518 ds->surface = qemu_create_displaysurface(640, 480);
1520 register_displaystate(ds);
1524 int is_graphic_console(void)
1526 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
1529 int is_fixedsize_console(void)
1531 return active_console && active_console->console_type != TEXT_CONSOLE;
1534 static void text_console_set_echo(CharDriverState *chr, bool echo)
1536 QemuConsole *s = chr->opaque;
1541 static void text_console_update_cursor(void *opaque)
1543 QemuConsole *s = opaque;
1545 s->cursor_visible_phase = !s->cursor_visible_phase;
1546 vga_hw_invalidate();
1547 qemu_mod_timer(s->cursor_timer,
1548 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1551 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1557 chr->chr_write = console_puts;
1559 s->out_fifo.buf = s->out_fifo_buf;
1560 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1561 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
1566 s->total_height = DEFAULT_BACKSCROLL;
1569 if (s->console_type == TEXT_CONSOLE) {
1570 s->g_width = surface_width(s->ds->surface);
1571 s->g_height = surface_height(s->ds->surface);
1575 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1577 s->hw_invalidate = text_console_invalidate;
1578 s->hw_text_update = text_console_update;
1581 /* Set text attribute defaults */
1582 s->t_attrib_default.bold = 0;
1583 s->t_attrib_default.uline = 0;
1584 s->t_attrib_default.blink = 0;
1585 s->t_attrib_default.invers = 0;
1586 s->t_attrib_default.unvisible = 0;
1587 s->t_attrib_default.fgcol = COLOR_WHITE;
1588 s->t_attrib_default.bgcol = COLOR_BLACK;
1589 /* set current text attributes to default */
1590 s->t_attrib = s->t_attrib_default;
1591 text_console_resize(s);
1597 s->t_attrib.bgcol = COLOR_BLUE;
1598 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1599 console_puts(chr, (uint8_t*)msg, len);
1600 s->t_attrib = s->t_attrib_default;
1603 qemu_chr_be_generic_open(chr);
1608 static CharDriverState *text_console_init(ChardevVC *vc)
1610 CharDriverState *chr;
1613 unsigned height = 0;
1615 chr = g_malloc0(sizeof(CharDriverState));
1617 if (vc->has_width) {
1619 } else if (vc->has_cols) {
1620 width = vc->cols * FONT_WIDTH;
1623 if (vc->has_height) {
1624 height = vc->height;
1625 } else if (vc->has_rows) {
1626 height = vc->rows * FONT_HEIGHT;
1629 if (width == 0 || height == 0) {
1630 s = new_console(NULL, TEXT_CONSOLE);
1632 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1642 s->g_height = height;
1644 chr->chr_set_echo = text_console_set_echo;
1648 static VcHandler *vc_handler = text_console_init;
1650 CharDriverState *vc_init(ChardevVC *vc)
1652 return vc_handler(vc);
1655 void register_vc_handler(VcHandler *handler)
1657 vc_handler = handler;
1660 void text_consoles_set_display(DisplayState *ds)
1664 for (i = 0; i < nb_consoles; i++) {
1665 if (consoles[i]->console_type != GRAPHIC_CONSOLE) {
1666 text_console_do_init(consoles[i]->chr, ds);
1671 void qemu_console_resize(QemuConsole *s, int width, int height)
1674 s->g_height = height;
1675 if (is_graphic_console()) {
1676 DisplaySurface *surface;
1677 surface = qemu_create_displaysurface(width, height);
1678 dpy_gfx_replace_surface(s, surface);
1682 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1683 int dst_x, int dst_y, int w, int h)
1685 if (is_graphic_console()) {
1686 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1690 DisplaySurface *qemu_console_surface(QemuConsole *console)
1692 return console->ds->surface;
1695 DisplayState *qemu_console_displaystate(QemuConsole *console)
1700 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1704 memset(&pf, 0x00, sizeof(PixelFormat));
1706 pf.bits_per_pixel = bpp;
1707 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1708 pf.depth = bpp == 32 ? 24 : bpp;
1712 pf.rmask = 0x000000FF;
1713 pf.gmask = 0x0000FF00;
1714 pf.bmask = 0x00FF0000;
1726 pf.rmask = 0x0000FF00;
1727 pf.gmask = 0x00FF0000;
1728 pf.bmask = 0xFF000000;
1729 pf.amask = 0x00000000;
1749 PixelFormat qemu_default_pixelformat(int bpp)
1753 memset(&pf, 0x00, sizeof(PixelFormat));
1755 pf.bits_per_pixel = bpp;
1756 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1757 pf.depth = bpp == 32 ? 24 : bpp;
1761 pf.bits_per_pixel = 16;
1762 pf.rmask = 0x00007c00;
1763 pf.gmask = 0x000003E0;
1764 pf.bmask = 0x0000001F;
1776 pf.rmask = 0x0000F800;
1777 pf.gmask = 0x000007E0;
1778 pf.bmask = 0x0000001F;
1790 pf.rmask = 0x00FF0000;
1791 pf.gmask = 0x0000FF00;
1792 pf.bmask = 0x000000FF;
1804 pf.rmask = 0x00FF0000;
1805 pf.gmask = 0x0000FF00;
1806 pf.bmask = 0x000000FF;
1823 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1828 backend->vc = g_new0(ChardevVC, 1);
1830 val = qemu_opt_get_number(opts, "width", 0);
1832 backend->vc->has_width = true;
1833 backend->vc->width = val;
1836 val = qemu_opt_get_number(opts, "height", 0);
1838 backend->vc->has_height = true;
1839 backend->vc->height = val;
1842 val = qemu_opt_get_number(opts, "cols", 0);
1844 backend->vc->has_cols = true;
1845 backend->vc->cols = val;
1848 val = qemu_opt_get_number(opts, "rows", 0);
1850 backend->vc->has_rows = true;
1851 backend->vc->rows = val;
1855 static void register_types(void)
1857 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1861 type_init(register_types);