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"
26 #include "qemu-timer.h"
27 #include "qmp-commands.h"
29 //#define DEBUG_CONSOLE
30 #define DEFAULT_BACKSCROLL 512
31 #define MAX_CONSOLES 12
32 #define CONSOLE_CURSOR_PERIOD 500
34 #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
35 #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
37 typedef struct TextAttributes {
47 typedef struct TextCell {
49 TextAttributes t_attrib;
52 #define MAX_ESC_PARAMS 3
60 typedef struct QEMUFIFO {
63 int count, wptr, rptr;
66 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
70 l = f->buf_size - f->count;
75 l = f->buf_size - f->wptr;
78 memcpy(f->buf + f->wptr, buf, l);
80 if (f->wptr >= f->buf_size)
89 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
97 l = f->buf_size - f->rptr;
100 memcpy(buf, f->buf + f->rptr, l);
102 if (f->rptr >= f->buf_size)
114 TEXT_CONSOLE_FIXED_SIZE
117 /* ??? This is mis-named.
118 It is used for both text and graphical consoles. */
121 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;
130 int g_width, g_height;
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 static DisplayState *display_state;
164 static TextConsole *active_console;
165 static TextConsole *consoles[MAX_CONSOLES];
166 static int nb_consoles = 0;
168 void vga_hw_update(void)
170 if (active_console && active_console->hw_update)
171 active_console->hw_update(active_console->hw);
174 void vga_hw_invalidate(void)
176 if (active_console && active_console->hw_invalidate)
177 active_console->hw_invalidate(active_console->hw);
180 void qmp_screendump(const char *filename, Error **errp)
182 TextConsole *previous_active_console;
185 previous_active_console = active_console;
186 cswitch = previous_active_console && previous_active_console->index != 0;
188 /* There is currently no way of specifying which screen we want to dump,
189 so always dump the first one. */
193 if (consoles[0] && consoles[0]->hw_screen_dump) {
194 consoles[0]->hw_screen_dump(consoles[0]->hw, filename, cswitch, errp);
196 error_setg(errp, "device doesn't support screendump\n");
200 console_select(previous_active_console->index);
204 void vga_hw_text_update(console_ch_t *chardata)
206 if (active_console && active_console->hw_text_update)
207 active_console->hw_text_update(active_console->hw, chardata);
210 /* convert a RGBA color to a color index usable in graphic primitives */
211 static unsigned int vga_get_color(DisplayState *ds, unsigned int rgba)
213 unsigned int r, g, b, color;
215 switch(ds_get_bits_per_pixel(ds)) {
218 r = (rgba >> 16) & 0xff;
219 g = (rgba >> 8) & 0xff;
221 color = (rgb_to_index[r] * 6 * 6) +
222 (rgb_to_index[g] * 6) +
227 r = (rgba >> 16) & 0xff;
228 g = (rgba >> 8) & 0xff;
230 color = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
233 r = (rgba >> 16) & 0xff;
234 g = (rgba >> 8) & 0xff;
236 color = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
246 static void vga_fill_rect (DisplayState *ds,
247 int posx, int posy, int width, int height, uint32_t color)
252 bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
253 d1 = ds_get_data(ds) +
254 ds_get_linesize(ds) * posy + bpp * posx;
255 for (y = 0; y < height; y++) {
259 for (x = 0; x < width; x++) {
260 *((uint8_t *)d) = color;
265 for (x = 0; x < width; x++) {
266 *((uint16_t *)d) = color;
271 for (x = 0; x < width; x++) {
272 *((uint32_t *)d) = color;
277 d1 += ds_get_linesize(ds);
281 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
282 static void vga_bitblt(DisplayState *ds, int xs, int ys, int xd, int yd, int w, int h)
288 bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
291 s = ds_get_data(ds) +
292 ds_get_linesize(ds) * ys + bpp * xs;
293 d = ds_get_data(ds) +
294 ds_get_linesize(ds) * yd + bpp * xd;
295 for (y = 0; y < h; y++) {
297 d += ds_get_linesize(ds);
298 s += ds_get_linesize(ds);
301 s = ds_get_data(ds) +
302 ds_get_linesize(ds) * (ys + h - 1) + bpp * xs;
303 d = ds_get_data(ds) +
304 ds_get_linesize(ds) * (yd + h - 1) + bpp * xd;
305 for (y = 0; y < h; y++) {
307 d -= ds_get_linesize(ds);
308 s -= ds_get_linesize(ds);
313 /***********************************************************/
314 /* basic char display */
316 #define FONT_HEIGHT 16
321 #define cbswap_32(__x) \
323 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
324 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
325 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
326 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
328 #ifdef HOST_WORDS_BIGENDIAN
331 #define PAT(x) cbswap_32(x)
334 static const uint32_t dmask16[16] = {
353 static const uint32_t dmask4[4] = {
360 static uint32_t color_table[2][8];
362 #ifndef CONFIG_CURSES
375 static const uint32_t color_table_rgb[2][8] = {
377 QEMU_RGB(0x00, 0x00, 0x00), /* black */
378 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
379 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
380 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
381 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
382 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
383 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
384 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
387 QEMU_RGB(0x00, 0x00, 0x00), /* black */
388 QEMU_RGB(0xff, 0x00, 0x00), /* red */
389 QEMU_RGB(0x00, 0xff, 0x00), /* green */
390 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
391 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
392 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
393 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
394 QEMU_RGB(0xff, 0xff, 0xff), /* white */
398 static inline unsigned int col_expand(DisplayState *ds, unsigned int col)
400 switch(ds_get_bits_per_pixel(ds)) {
416 static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
418 if (t_attrib->bold) {
423 if (t_attrib->uline) {
428 if (t_attrib->blink) {
433 if (t_attrib->invers) {
438 if (t_attrib->unvisible) {
444 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
448 static void vga_putcharxy(DisplayState *ds, int x, int y, int ch,
449 TextAttributes *t_attrib)
452 const uint8_t *font_ptr;
453 unsigned int font_data, linesize, xorcol, bpp;
455 unsigned int fgcol, bgcol;
458 printf("x: %2i y: %2i", x, y);
459 console_print_text_attributes(t_attrib, ch);
462 if (t_attrib->invers) {
463 bgcol = color_table[t_attrib->bold][t_attrib->fgcol];
464 fgcol = color_table[t_attrib->bold][t_attrib->bgcol];
466 fgcol = color_table[t_attrib->bold][t_attrib->fgcol];
467 bgcol = color_table[t_attrib->bold][t_attrib->bgcol];
470 bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
471 d = ds_get_data(ds) +
472 ds_get_linesize(ds) * y * FONT_HEIGHT + bpp * x * FONT_WIDTH;
473 linesize = ds_get_linesize(ds);
474 font_ptr = vgafont16 + FONT_HEIGHT * ch;
475 xorcol = bgcol ^ fgcol;
476 switch(ds_get_bits_per_pixel(ds)) {
478 for(i = 0; i < FONT_HEIGHT; i++) {
479 font_data = *font_ptr++;
481 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
484 ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
485 ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
491 for(i = 0; i < FONT_HEIGHT; i++) {
492 font_data = *font_ptr++;
494 && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
497 ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
498 ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
499 ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
500 ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
505 for(i = 0; i < FONT_HEIGHT; i++) {
506 font_data = *font_ptr++;
507 if (t_attrib->uline && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
510 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
511 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
512 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
513 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
514 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
515 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
516 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
517 ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
524 static void text_console_resize(TextConsole *s)
526 TextCell *cells, *c, *c1;
527 int w1, x, y, last_width;
529 last_width = s->width;
530 s->width = s->g_width / FONT_WIDTH;
531 s->height = s->g_height / FONT_HEIGHT;
537 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
538 for(y = 0; y < s->total_height; y++) {
539 c = &cells[y * s->width];
541 c1 = &s->cells[y * last_width];
542 for(x = 0; x < w1; x++) {
546 for(x = w1; x < s->width; x++) {
548 c->t_attrib = s->t_attrib_default;
556 static inline void text_update_xy(TextConsole *s, int x, int y)
558 s->text_x[0] = MIN(s->text_x[0], x);
559 s->text_x[1] = MAX(s->text_x[1], x);
560 s->text_y[0] = MIN(s->text_y[0], y);
561 s->text_y[1] = MAX(s->text_y[1], y);
564 static void invalidate_xy(TextConsole *s, int x, int y)
566 if (s->update_x0 > x * FONT_WIDTH)
567 s->update_x0 = x * FONT_WIDTH;
568 if (s->update_y0 > y * FONT_HEIGHT)
569 s->update_y0 = y * FONT_HEIGHT;
570 if (s->update_x1 < (x + 1) * FONT_WIDTH)
571 s->update_x1 = (x + 1) * FONT_WIDTH;
572 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
573 s->update_y1 = (y + 1) * FONT_HEIGHT;
576 static void update_xy(TextConsole *s, int x, int y)
581 if (s == active_console) {
582 if (!ds_get_bits_per_pixel(s->ds)) {
583 text_update_xy(s, x, y);
587 y1 = (s->y_base + y) % s->total_height;
588 y2 = y1 - s->y_displayed;
590 y2 += s->total_height;
591 if (y2 < s->height) {
592 c = &s->cells[y1 * s->width + x];
593 vga_putcharxy(s->ds, x, y2, c->ch,
595 invalidate_xy(s, x, y2);
600 static void console_show_cursor(TextConsole *s, int show)
605 if (s == active_console) {
608 if (!ds_get_bits_per_pixel(s->ds)) {
609 s->cursor_invalidate = 1;
616 y1 = (s->y_base + s->y) % s->total_height;
617 y = y1 - s->y_displayed;
619 y += s->total_height;
621 c = &s->cells[y1 * s->width + x];
622 if (show && s->cursor_visible_phase) {
623 TextAttributes t_attrib = s->t_attrib_default;
624 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
625 vga_putcharxy(s->ds, x, y, c->ch, &t_attrib);
627 vga_putcharxy(s->ds, x, y, c->ch, &(c->t_attrib));
629 invalidate_xy(s, x, y);
634 static void console_refresh(TextConsole *s)
639 if (s != active_console)
641 if (!ds_get_bits_per_pixel(s->ds)) {
644 s->text_x[1] = s->width - 1;
645 s->text_y[1] = s->height - 1;
646 s->cursor_invalidate = 1;
650 vga_fill_rect(s->ds, 0, 0, ds_get_width(s->ds), ds_get_height(s->ds),
651 color_table[0][COLOR_BLACK]);
653 for(y = 0; y < s->height; y++) {
654 c = s->cells + y1 * s->width;
655 for(x = 0; x < s->width; x++) {
656 vga_putcharxy(s->ds, x, y, c->ch,
660 if (++y1 == s->total_height)
663 console_show_cursor(s, 1);
664 dpy_update(s->ds, 0, 0, ds_get_width(s->ds), ds_get_height(s->ds));
667 static void console_scroll(int ydelta)
673 if (!s || (s->console_type == GRAPHIC_CONSOLE))
677 for(i = 0; i < ydelta; i++) {
678 if (s->y_displayed == s->y_base)
680 if (++s->y_displayed == s->total_height)
685 i = s->backscroll_height;
686 if (i > s->total_height - s->height)
687 i = s->total_height - s->height;
690 y1 += s->total_height;
691 for(i = 0; i < ydelta; i++) {
692 if (s->y_displayed == y1)
694 if (--s->y_displayed < 0)
695 s->y_displayed = s->total_height - 1;
701 static void console_put_lf(TextConsole *s)
707 if (s->y >= s->height) {
708 s->y = s->height - 1;
710 if (s->y_displayed == s->y_base) {
711 if (++s->y_displayed == s->total_height)
714 if (++s->y_base == s->total_height)
716 if (s->backscroll_height < s->total_height)
717 s->backscroll_height++;
718 y1 = (s->y_base + s->height - 1) % s->total_height;
719 c = &s->cells[y1 * s->width];
720 for(x = 0; x < s->width; x++) {
722 c->t_attrib = s->t_attrib_default;
725 if (s == active_console && s->y_displayed == s->y_base) {
726 if (!ds_get_bits_per_pixel(s->ds)) {
729 s->text_x[1] = s->width - 1;
730 s->text_y[1] = s->height - 1;
734 vga_bitblt(s->ds, 0, FONT_HEIGHT, 0, 0,
735 s->width * FONT_WIDTH,
736 (s->height - 1) * FONT_HEIGHT);
737 vga_fill_rect(s->ds, 0, (s->height - 1) * FONT_HEIGHT,
738 s->width * FONT_WIDTH, FONT_HEIGHT,
739 color_table[0][s->t_attrib_default.bgcol]);
742 s->update_x1 = s->width * FONT_WIDTH;
743 s->update_y1 = s->height * FONT_HEIGHT;
748 /* Set console attributes depending on the current escape codes.
749 * NOTE: I know this code is not very efficient (checking every color for it
750 * self) but it is more readable and better maintainable.
752 static void console_handle_escape(TextConsole *s)
756 for (i=0; i<s->nb_esc_params; i++) {
757 switch (s->esc_params[i]) {
758 case 0: /* reset all console attributes to default */
759 s->t_attrib = s->t_attrib_default;
762 s->t_attrib.bold = 1;
765 s->t_attrib.uline = 1;
768 s->t_attrib.blink = 1;
771 s->t_attrib.invers = 1;
774 s->t_attrib.unvisible = 1;
777 s->t_attrib.bold = 0;
780 s->t_attrib.uline = 0;
783 s->t_attrib.blink = 0;
786 s->t_attrib.invers = 0;
789 s->t_attrib.unvisible = 0;
791 /* set foreground color */
793 s->t_attrib.fgcol=COLOR_BLACK;
796 s->t_attrib.fgcol=COLOR_RED;
799 s->t_attrib.fgcol=COLOR_GREEN;
802 s->t_attrib.fgcol=COLOR_YELLOW;
805 s->t_attrib.fgcol=COLOR_BLUE;
808 s->t_attrib.fgcol=COLOR_MAGENTA;
811 s->t_attrib.fgcol=COLOR_CYAN;
814 s->t_attrib.fgcol=COLOR_WHITE;
816 /* set background color */
818 s->t_attrib.bgcol=COLOR_BLACK;
821 s->t_attrib.bgcol=COLOR_RED;
824 s->t_attrib.bgcol=COLOR_GREEN;
827 s->t_attrib.bgcol=COLOR_YELLOW;
830 s->t_attrib.bgcol=COLOR_BLUE;
833 s->t_attrib.bgcol=COLOR_MAGENTA;
836 s->t_attrib.bgcol=COLOR_CYAN;
839 s->t_attrib.bgcol=COLOR_WHITE;
845 static void console_clear_xy(TextConsole *s, int x, int y)
847 int y1 = (s->y_base + y) % s->total_height;
848 TextCell *c = &s->cells[y1 * s->width + x];
850 c->t_attrib = s->t_attrib_default;
854 /* set cursor, checking bounds */
855 static void set_cursor(TextConsole *s, int x, int y)
863 if (y >= s->height) {
874 static void console_putchar(TextConsole *s, int ch)
883 case '\r': /* carriage return */
886 case '\n': /* newline */
889 case '\b': /* backspace */
893 case '\t': /* tabspace */
894 if (s->x + (8 - (s->x % 8)) > s->width) {
898 s->x = s->x + (8 - (s->x % 8));
901 case '\a': /* alert aka. bell */
902 /* TODO: has to be implemented */
905 /* SI (shift in), character set 0 (ignored) */
908 /* SO (shift out), character set 1 (ignored) */
910 case 27: /* esc (introducing an escape sequence) */
911 s->state = TTY_STATE_ESC;
914 if (s->x >= s->width) {
919 y1 = (s->y_base + s->y) % s->total_height;
920 c = &s->cells[y1 * s->width + s->x];
922 c->t_attrib = s->t_attrib;
923 update_xy(s, s->x, s->y);
928 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
930 for(i=0;i<MAX_ESC_PARAMS;i++)
931 s->esc_params[i] = 0;
932 s->nb_esc_params = 0;
933 s->state = TTY_STATE_CSI;
935 s->state = TTY_STATE_NORM;
938 case TTY_STATE_CSI: /* handle escape sequence parameters */
939 if (ch >= '0' && ch <= '9') {
940 if (s->nb_esc_params < MAX_ESC_PARAMS) {
941 int *param = &s->esc_params[s->nb_esc_params];
942 int digit = (ch - '0');
944 *param = (*param <= (INT_MAX - digit) / 10) ?
945 *param * 10 + digit : INT_MAX;
948 if (s->nb_esc_params < MAX_ESC_PARAMS)
953 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
954 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
956 s->state = TTY_STATE_NORM;
960 if (s->esc_params[0] == 0) {
961 s->esc_params[0] = 1;
963 set_cursor(s, s->x, s->y - s->esc_params[0]);
966 /* move cursor down */
967 if (s->esc_params[0] == 0) {
968 s->esc_params[0] = 1;
970 set_cursor(s, s->x, s->y + s->esc_params[0]);
973 /* move cursor right */
974 if (s->esc_params[0] == 0) {
975 s->esc_params[0] = 1;
977 set_cursor(s, s->x + s->esc_params[0], s->y);
980 /* move cursor left */
981 if (s->esc_params[0] == 0) {
982 s->esc_params[0] = 1;
984 set_cursor(s, s->x - s->esc_params[0], s->y);
987 /* move cursor to column */
988 set_cursor(s, s->esc_params[0] - 1, s->y);
992 /* move cursor to row, column */
993 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
996 switch (s->esc_params[0]) {
998 /* clear to end of screen */
999 for (y = s->y; y < s->height; y++) {
1000 for (x = 0; x < s->width; x++) {
1001 if (y == s->y && x < s->x) {
1004 console_clear_xy(s, x, y);
1009 /* clear from beginning of screen */
1010 for (y = 0; y <= s->y; y++) {
1011 for (x = 0; x < s->width; x++) {
1012 if (y == s->y && x > s->x) {
1015 console_clear_xy(s, x, y);
1020 /* clear entire screen */
1021 for (y = 0; y <= s->height; y++) {
1022 for (x = 0; x < s->width; x++) {
1023 console_clear_xy(s, x, y);
1030 switch (s->esc_params[0]) {
1033 for(x = s->x; x < s->width; x++) {
1034 console_clear_xy(s, x, s->y);
1038 /* clear from beginning of line */
1039 for (x = 0; x <= s->x; x++) {
1040 console_clear_xy(s, x, s->y);
1044 /* clear entire line */
1045 for(x = 0; x < s->width; x++) {
1046 console_clear_xy(s, x, s->y);
1052 console_handle_escape(s);
1055 /* report cursor position */
1056 /* TODO: send ESC[row;colR */
1059 /* save cursor position */
1064 /* restore cursor position */
1069 #ifdef DEBUG_CONSOLE
1070 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1079 void console_select(unsigned int index)
1083 if (index >= MAX_CONSOLES)
1085 if (active_console) {
1086 active_console->g_width = ds_get_width(active_console->ds);
1087 active_console->g_height = ds_get_height(active_console->ds);
1089 s = consoles[index];
1091 DisplayState *ds = s->ds;
1093 if (active_console && active_console->cursor_timer) {
1094 qemu_del_timer(active_console->cursor_timer);
1097 if (ds_get_bits_per_pixel(s->ds)) {
1098 ds->surface = qemu_resize_displaysurface(ds, s->g_width, s->g_height);
1100 s->ds->surface->width = s->width;
1101 s->ds->surface->height = s->height;
1103 if (s->cursor_timer) {
1104 qemu_mod_timer(s->cursor_timer,
1105 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1108 vga_hw_invalidate();
1112 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1114 TextConsole *s = chr->opaque;
1117 s->update_x0 = s->width * FONT_WIDTH;
1118 s->update_y0 = s->height * FONT_HEIGHT;
1121 console_show_cursor(s, 0);
1122 for(i = 0; i < len; i++) {
1123 console_putchar(s, buf[i]);
1125 console_show_cursor(s, 1);
1126 if (ds_get_bits_per_pixel(s->ds) && s->update_x0 < s->update_x1) {
1127 dpy_update(s->ds, s->update_x0, s->update_y0,
1128 s->update_x1 - s->update_x0,
1129 s->update_y1 - s->update_y0);
1134 static void kbd_send_chars(void *opaque)
1136 TextConsole *s = opaque;
1140 len = qemu_chr_be_can_write(s->chr);
1141 if (len > s->out_fifo.count)
1142 len = s->out_fifo.count;
1144 if (len > sizeof(buf))
1146 qemu_fifo_read(&s->out_fifo, buf, len);
1147 qemu_chr_be_write(s->chr, buf, len);
1149 /* characters are pending: we send them a bit later (XXX:
1150 horrible, should change char device API) */
1151 if (s->out_fifo.count > 0) {
1152 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
1156 /* called when an ascii key is pressed */
1157 void kbd_put_keysym(int keysym)
1160 uint8_t buf[16], *q;
1164 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1168 case QEMU_KEY_CTRL_UP:
1171 case QEMU_KEY_CTRL_DOWN:
1174 case QEMU_KEY_CTRL_PAGEUP:
1175 console_scroll(-10);
1177 case QEMU_KEY_CTRL_PAGEDOWN:
1181 /* convert the QEMU keysym to VT100 key string */
1183 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1186 c = keysym - 0xe100;
1188 *q++ = '0' + (c / 10);
1189 *q++ = '0' + (c % 10);
1191 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1194 *q++ = keysym & 0xff;
1195 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1196 console_puts(s->chr, (const uint8_t *) "\r", 1);
1202 console_puts(s->chr, buf, q - buf);
1204 if (s->chr->chr_read) {
1205 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1212 static void text_console_invalidate(void *opaque)
1214 TextConsole *s = (TextConsole *) opaque;
1215 if (!ds_get_bits_per_pixel(s->ds) && s->console_type == TEXT_CONSOLE) {
1216 s->g_width = ds_get_width(s->ds);
1217 s->g_height = ds_get_height(s->ds);
1218 text_console_resize(s);
1223 static void text_console_update(void *opaque, console_ch_t *chardata)
1225 TextConsole *s = (TextConsole *) opaque;
1228 if (s->text_x[0] <= s->text_x[1]) {
1229 src = (s->y_base + s->text_y[0]) * s->width;
1230 chardata += s->text_y[0] * s->width;
1231 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1232 for (j = 0; j < s->width; j ++, src ++)
1233 console_write_ch(chardata ++, s->cells[src].ch |
1234 (s->cells[src].t_attrib.fgcol << 12) |
1235 (s->cells[src].t_attrib.bgcol << 8) |
1236 (s->cells[src].t_attrib.bold << 21));
1237 dpy_update(s->ds, s->text_x[0], s->text_y[0],
1238 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1239 s->text_x[0] = s->width;
1240 s->text_y[0] = s->height;
1244 if (s->cursor_invalidate) {
1245 dpy_cursor(s->ds, s->x, s->y);
1246 s->cursor_invalidate = 0;
1250 static TextConsole *get_graphic_console(DisplayState *ds)
1254 for (i = 0; i < nb_consoles; i++) {
1256 if (s->console_type == GRAPHIC_CONSOLE && s->ds == ds)
1262 static TextConsole *new_console(DisplayState *ds, console_type_t console_type)
1267 if (nb_consoles >= MAX_CONSOLES)
1269 s = g_malloc0(sizeof(TextConsole));
1270 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1271 (console_type == GRAPHIC_CONSOLE))) {
1275 s->console_type = console_type;
1276 if (console_type != GRAPHIC_CONSOLE) {
1277 s->index = nb_consoles;
1278 consoles[nb_consoles++] = s;
1280 /* HACK: Put graphical consoles before text consoles. */
1281 for (i = nb_consoles; i > 0; i--) {
1282 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1284 consoles[i] = consoles[i - 1];
1285 consoles[i]->index = i;
1294 static DisplaySurface* defaultallocator_create_displaysurface(int width, int height)
1296 DisplaySurface *surface = (DisplaySurface*) g_malloc0(sizeof(DisplaySurface));
1298 int linesize = width * 4;
1299 qemu_alloc_display(surface, width, height, linesize,
1300 qemu_default_pixelformat(32), 0);
1304 static DisplaySurface* defaultallocator_resize_displaysurface(DisplaySurface *surface,
1305 int width, int height)
1307 int linesize = width * 4;
1308 qemu_alloc_display(surface, width, height, linesize,
1309 qemu_default_pixelformat(32), 0);
1313 void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1314 int linesize, PixelFormat pf, int newflags)
1317 surface->width = width;
1318 surface->height = height;
1319 surface->linesize = linesize;
1321 if (surface->flags & QEMU_ALLOCATED_FLAG) {
1322 data = g_realloc(surface->data,
1323 surface->linesize * surface->height);
1325 data = g_malloc(surface->linesize * surface->height);
1327 surface->data = (uint8_t *)data;
1328 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1329 #ifdef HOST_WORDS_BIGENDIAN
1330 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1334 DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp,
1335 int linesize, uint8_t *data)
1337 DisplaySurface *surface = (DisplaySurface*) g_malloc0(sizeof(DisplaySurface));
1339 surface->width = width;
1340 surface->height = height;
1341 surface->linesize = linesize;
1342 surface->pf = qemu_default_pixelformat(bpp);
1343 #ifdef HOST_WORDS_BIGENDIAN
1344 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1346 surface->data = data;
1351 static void defaultallocator_free_displaysurface(DisplaySurface *surface)
1353 if (surface == NULL)
1355 if (surface->flags & QEMU_ALLOCATED_FLAG)
1356 g_free(surface->data);
1360 static struct DisplayAllocator default_allocator = {
1361 defaultallocator_create_displaysurface,
1362 defaultallocator_resize_displaysurface,
1363 defaultallocator_free_displaysurface
1366 static void dumb_display_init(void)
1368 DisplayState *ds = g_malloc0(sizeof(DisplayState));
1372 ds->allocator = &default_allocator;
1373 if (is_fixedsize_console()) {
1374 width = active_console->g_width;
1375 height = active_console->g_height;
1377 ds->surface = qemu_create_displaysurface(ds, width, height);
1378 register_displaystate(ds);
1381 /***********************************************************/
1382 /* register display */
1384 void register_displaystate(DisplayState *ds)
1394 DisplayState *get_displaystate(void)
1396 if (!display_state) {
1397 dumb_display_init ();
1399 return display_state;
1402 DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
1404 if(ds->allocator == &default_allocator) {
1405 DisplaySurface *surf;
1406 surf = da->create_displaysurface(ds_get_width(ds), ds_get_height(ds));
1407 defaultallocator_free_displaysurface(ds->surface);
1411 return ds->allocator;
1414 DisplayState *graphic_console_init(vga_hw_update_ptr update,
1415 vga_hw_invalidate_ptr invalidate,
1416 vga_hw_screen_dump_ptr screen_dump,
1417 vga_hw_text_update_ptr text_update,
1423 ds = (DisplayState *) g_malloc0(sizeof(DisplayState));
1424 ds->allocator = &default_allocator;
1425 ds->surface = qemu_create_displaysurface(ds, 640, 480);
1427 s = new_console(ds, GRAPHIC_CONSOLE);
1429 qemu_free_displaysurface(ds);
1433 s->hw_update = update;
1434 s->hw_invalidate = invalidate;
1435 s->hw_screen_dump = screen_dump;
1436 s->hw_text_update = text_update;
1439 register_displaystate(ds);
1443 int is_graphic_console(void)
1445 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
1448 int is_fixedsize_console(void)
1450 return active_console && active_console->console_type != TEXT_CONSOLE;
1453 void console_color_init(DisplayState *ds)
1456 for (j = 0; j < 2; j++) {
1457 for (i = 0; i < 8; i++) {
1458 color_table[j][i] = col_expand(ds,
1459 vga_get_color(ds, color_table_rgb[j][i]));
1464 static void text_console_set_echo(CharDriverState *chr, bool echo)
1466 TextConsole *s = chr->opaque;
1471 static void text_console_update_cursor(void *opaque)
1473 TextConsole *s = opaque;
1475 s->cursor_visible_phase = !s->cursor_visible_phase;
1476 vga_hw_invalidate();
1477 qemu_mod_timer(s->cursor_timer,
1478 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1481 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1484 static int color_inited;
1488 chr->chr_write = console_puts;
1490 s->out_fifo.buf = s->out_fifo_buf;
1491 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1492 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
1495 if (!color_inited) {
1497 console_color_init(s->ds);
1501 s->total_height = DEFAULT_BACKSCROLL;
1504 if (s->console_type == TEXT_CONSOLE) {
1505 s->g_width = ds_get_width(s->ds);
1506 s->g_height = ds_get_height(s->ds);
1510 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1512 s->hw_invalidate = text_console_invalidate;
1513 s->hw_text_update = text_console_update;
1516 /* Set text attribute defaults */
1517 s->t_attrib_default.bold = 0;
1518 s->t_attrib_default.uline = 0;
1519 s->t_attrib_default.blink = 0;
1520 s->t_attrib_default.invers = 0;
1521 s->t_attrib_default.unvisible = 0;
1522 s->t_attrib_default.fgcol = COLOR_WHITE;
1523 s->t_attrib_default.bgcol = COLOR_BLACK;
1524 /* set current text attributes to default */
1525 s->t_attrib = s->t_attrib_default;
1526 text_console_resize(s);
1532 s->t_attrib.bgcol = COLOR_BLUE;
1533 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1534 console_puts(chr, (uint8_t*)msg, len);
1535 s->t_attrib = s->t_attrib_default;
1538 qemu_chr_generic_open(chr);
1543 CharDriverState *text_console_init(QemuOpts *opts)
1545 CharDriverState *chr;
1550 chr = g_malloc0(sizeof(CharDriverState));
1552 width = qemu_opt_get_number(opts, "width", 0);
1554 width = qemu_opt_get_number(opts, "cols", 0) * FONT_WIDTH;
1556 height = qemu_opt_get_number(opts, "height", 0);
1558 height = qemu_opt_get_number(opts, "rows", 0) * FONT_HEIGHT;
1560 if (width == 0 || height == 0) {
1561 s = new_console(NULL, TEXT_CONSOLE);
1563 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1573 s->g_height = height;
1575 chr->chr_set_echo = text_console_set_echo;
1579 void text_consoles_set_display(DisplayState *ds)
1583 for (i = 0; i < nb_consoles; i++) {
1584 if (consoles[i]->console_type != GRAPHIC_CONSOLE) {
1585 text_console_do_init(consoles[i]->chr, ds);
1590 void qemu_console_resize(DisplayState *ds, int width, int height)
1592 TextConsole *s = get_graphic_console(ds);
1596 s->g_height = height;
1597 if (is_graphic_console()) {
1598 ds->surface = qemu_resize_displaysurface(ds, width, height);
1603 void qemu_console_copy(DisplayState *ds, int src_x, int src_y,
1604 int dst_x, int dst_y, int w, int h)
1606 if (is_graphic_console()) {
1607 dpy_copy(ds, src_x, src_y, dst_x, dst_y, w, h);
1611 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1615 memset(&pf, 0x00, sizeof(PixelFormat));
1617 pf.bits_per_pixel = bpp;
1618 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1619 pf.depth = bpp == 32 ? 24 : bpp;
1623 pf.rmask = 0x000000FF;
1624 pf.gmask = 0x0000FF00;
1625 pf.bmask = 0x00FF0000;
1637 pf.rmask = 0x0000FF00;
1638 pf.gmask = 0x00FF0000;
1639 pf.bmask = 0xFF000000;
1640 pf.amask = 0x00000000;
1660 PixelFormat qemu_default_pixelformat(int bpp)
1664 memset(&pf, 0x00, sizeof(PixelFormat));
1666 pf.bits_per_pixel = bpp;
1667 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1668 pf.depth = bpp == 32 ? 24 : bpp;
1672 pf.bits_per_pixel = 16;
1673 pf.rmask = 0x00007c00;
1674 pf.gmask = 0x000003E0;
1675 pf.bmask = 0x0000001F;
1687 pf.rmask = 0x0000F800;
1688 pf.gmask = 0x000007E0;
1689 pf.bmask = 0x0000001F;
1701 pf.rmask = 0x00FF0000;
1702 pf.gmask = 0x0000FF00;
1703 pf.bmask = 0x000000FF;
1715 pf.rmask = 0x00FF0000;
1716 pf.gmask = 0x0000FF00;
1717 pf.bmask = 0x000000FF;