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"
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. */
129 const GraphicHwOps *hw_ops;
132 /* Text console state */
136 int backscroll_height;
138 int x_saved, y_saved;
141 TextAttributes t_attrib_default; /* default text attributes */
142 TextAttributes t_attrib; /* currently active text attributes */
144 int text_x[2], text_y[2], cursor_invalidate;
146 bool cursor_visible_phase;
147 QEMUTimer *cursor_timer;
155 int esc_params[MAX_ESC_PARAMS];
158 CharDriverState *chr;
159 /* fifo for key pressed */
161 uint8_t out_fifo_buf[16];
162 QEMUTimer *kbd_timer;
165 struct DisplayState {
166 QEMUTimer *gui_timer;
167 uint64_t last_update;
168 uint64_t update_interval;
173 QLIST_HEAD(, DisplayChangeListener) listeners;
176 static DisplayState *display_state;
177 static QemuConsole *active_console;
178 static QemuConsole *consoles[MAX_CONSOLES];
179 static int nb_consoles = 0;
181 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
182 static void dpy_refresh(DisplayState *s);
183 static DisplayState *get_alloc_displaystate(void);
185 static void gui_update(void *opaque)
187 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
188 uint64_t dcl_interval;
189 DisplayState *ds = opaque;
190 DisplayChangeListener *dcl;
193 ds->refreshing = true;
195 ds->refreshing = false;
197 QLIST_FOREACH(dcl, &ds->listeners, next) {
198 dcl_interval = dcl->update_interval ?
199 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
200 if (interval > dcl_interval) {
201 interval = dcl_interval;
204 if (ds->update_interval != interval) {
205 ds->update_interval = interval;
206 for (i = 0; i < nb_consoles; i++) {
207 if (consoles[i]->hw_ops->update_interval) {
208 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
211 trace_console_refresh(interval);
213 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
214 timer_mod(ds->gui_timer, ds->last_update + interval);
217 static void gui_setup_refresh(DisplayState *ds)
219 DisplayChangeListener *dcl;
220 bool need_timer = false;
221 bool have_gfx = false;
222 bool have_text = false;
224 QLIST_FOREACH(dcl, &ds->listeners, next) {
225 if (dcl->ops->dpy_refresh != NULL) {
228 if (dcl->ops->dpy_gfx_update != NULL) {
231 if (dcl->ops->dpy_text_update != NULL) {
236 if (need_timer && ds->gui_timer == NULL) {
237 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
238 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
240 if (!need_timer && ds->gui_timer != NULL) {
241 timer_del(ds->gui_timer);
242 timer_free(ds->gui_timer);
243 ds->gui_timer = NULL;
246 ds->have_gfx = have_gfx;
247 ds->have_text = have_text;
250 void graphic_hw_update(QemuConsole *con)
253 con = active_console;
255 if (con && con->hw_ops->gfx_update) {
256 con->hw_ops->gfx_update(con->hw);
260 void graphic_hw_invalidate(QemuConsole *con)
263 con = active_console;
265 if (con && con->hw_ops->invalidate) {
266 con->hw_ops->invalidate(con->hw);
270 static void ppm_save(const char *filename, struct DisplaySurface *ds,
273 int width = pixman_image_get_width(ds->image);
274 int height = pixman_image_get_height(ds->image);
279 pixman_image_t *linebuf;
281 trace_ppm_save(filename, ds);
282 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
284 error_setg(errp, "failed to open file '%s': %s", filename,
288 f = fdopen(fd, "wb");
289 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
294 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
295 for (y = 0; y < height; y++) {
296 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
298 ret = fwrite(pixman_image_get_data(linebuf), 1,
299 pixman_image_get_stride(linebuf), f);
307 qemu_pixman_image_unref(linebuf);
312 error_setg(errp, "failed to write to file '%s': %s", filename,
318 void qmp_screendump(const char *filename, Error **errp)
320 QemuConsole *con = qemu_console_lookup_by_index(0);
321 DisplaySurface *surface;
324 error_setg(errp, "There is no QemuConsole I can screendump from.");
328 graphic_hw_update(con);
329 surface = qemu_console_surface(con);
330 ppm_save(filename, surface, errp);
333 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
336 con = active_console;
338 if (con && con->hw_ops->text_update) {
339 con->hw_ops->text_update(con->hw, chardata);
343 static void vga_fill_rect(QemuConsole *con,
344 int posx, int posy, int width, int height,
345 pixman_color_t color)
347 DisplaySurface *surface = qemu_console_surface(con);
348 pixman_rectangle16_t rect = {
349 .x = posx, .y = posy, .width = width, .height = height
352 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
356 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
357 static void vga_bitblt(QemuConsole *con,
358 int xs, int ys, int xd, int yd, int w, int h)
360 DisplaySurface *surface = qemu_console_surface(con);
362 pixman_image_composite(PIXMAN_OP_SRC,
363 surface->image, NULL, surface->image,
364 xs, ys, 0, 0, xd, yd, w, h);
367 /***********************************************************/
368 /* basic char display */
370 #define FONT_HEIGHT 16
375 #ifndef CONFIG_CURSES
388 #define QEMU_RGB(r, g, b) \
389 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
391 static const pixman_color_t color_table_rgb[2][8] = {
393 QEMU_RGB(0x00, 0x00, 0x00), /* black */
394 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
395 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
396 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
397 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
398 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
399 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
400 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
403 QEMU_RGB(0x00, 0x00, 0x00), /* black */
404 QEMU_RGB(0xff, 0x00, 0x00), /* red */
405 QEMU_RGB(0x00, 0xff, 0x00), /* green */
406 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
407 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
408 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
409 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
410 QEMU_RGB(0xff, 0xff, 0xff), /* white */
414 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
415 TextAttributes *t_attrib)
417 static pixman_image_t *glyphs[256];
418 DisplaySurface *surface = qemu_console_surface(s);
419 pixman_color_t fgcol, bgcol;
421 if (t_attrib->invers) {
422 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
423 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
425 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
426 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
430 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
432 qemu_pixman_glyph_render(glyphs[ch], surface->image,
433 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
436 static void text_console_resize(QemuConsole *s)
438 TextCell *cells, *c, *c1;
439 int w1, x, y, last_width;
441 last_width = s->width;
442 s->width = surface_width(s->surface) / FONT_WIDTH;
443 s->height = surface_height(s->surface) / FONT_HEIGHT;
449 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
450 for(y = 0; y < s->total_height; y++) {
451 c = &cells[y * s->width];
453 c1 = &s->cells[y * last_width];
454 for(x = 0; x < w1; x++) {
458 for(x = w1; x < s->width; x++) {
460 c->t_attrib = s->t_attrib_default;
468 static inline void text_update_xy(QemuConsole *s, int x, int y)
470 s->text_x[0] = MIN(s->text_x[0], x);
471 s->text_x[1] = MAX(s->text_x[1], x);
472 s->text_y[0] = MIN(s->text_y[0], y);
473 s->text_y[1] = MAX(s->text_y[1], y);
476 static void invalidate_xy(QemuConsole *s, int x, int y)
478 if (s->update_x0 > x * FONT_WIDTH)
479 s->update_x0 = x * FONT_WIDTH;
480 if (s->update_y0 > y * FONT_HEIGHT)
481 s->update_y0 = y * FONT_HEIGHT;
482 if (s->update_x1 < (x + 1) * FONT_WIDTH)
483 s->update_x1 = (x + 1) * FONT_WIDTH;
484 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
485 s->update_y1 = (y + 1) * FONT_HEIGHT;
488 static void update_xy(QemuConsole *s, int x, int y)
493 if (!qemu_console_is_visible(s)) {
497 if (s->ds->have_text) {
498 text_update_xy(s, x, y);
501 if (s->ds->have_gfx) {
502 y1 = (s->y_base + y) % s->total_height;
503 y2 = y1 - s->y_displayed;
505 y2 += s->total_height;
506 if (y2 < s->height) {
507 c = &s->cells[y1 * s->width + x];
508 vga_putcharxy(s, x, y2, c->ch,
510 invalidate_xy(s, x, y2);
515 static void console_show_cursor(QemuConsole *s, int show)
521 if (!qemu_console_is_visible(s)) {
525 if (s->ds->have_text) {
526 s->cursor_invalidate = 1;
529 if (s->ds->have_gfx) {
533 y1 = (s->y_base + s->y) % s->total_height;
534 y = y1 - s->y_displayed;
536 y += s->total_height;
538 c = &s->cells[y1 * s->width + x];
539 if (show && s->cursor_visible_phase) {
540 TextAttributes t_attrib = s->t_attrib_default;
541 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
542 vga_putcharxy(s, x, y, c->ch, &t_attrib);
544 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
546 invalidate_xy(s, x, y);
551 static void console_refresh(QemuConsole *s)
553 DisplaySurface *surface = qemu_console_surface(s);
557 if (!qemu_console_is_visible(s)) {
561 if (s->ds->have_text) {
564 s->text_x[1] = s->width - 1;
565 s->text_y[1] = s->height - 1;
566 s->cursor_invalidate = 1;
569 if (s->ds->have_gfx) {
570 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
571 color_table_rgb[0][COLOR_BLACK]);
573 for (y = 0; y < s->height; y++) {
574 c = s->cells + y1 * s->width;
575 for (x = 0; x < s->width; x++) {
576 vga_putcharxy(s, x, y, c->ch,
580 if (++y1 == s->total_height) {
584 console_show_cursor(s, 1);
585 dpy_gfx_update(s, 0, 0,
586 surface_width(surface), surface_height(surface));
590 static void console_scroll(QemuConsole *s, int ydelta)
595 for(i = 0; i < ydelta; i++) {
596 if (s->y_displayed == s->y_base)
598 if (++s->y_displayed == s->total_height)
603 i = s->backscroll_height;
604 if (i > s->total_height - s->height)
605 i = s->total_height - s->height;
608 y1 += s->total_height;
609 for(i = 0; i < ydelta; i++) {
610 if (s->y_displayed == y1)
612 if (--s->y_displayed < 0)
613 s->y_displayed = s->total_height - 1;
619 static void console_put_lf(QemuConsole *s)
625 if (s->y >= s->height) {
626 s->y = s->height - 1;
628 if (s->y_displayed == s->y_base) {
629 if (++s->y_displayed == s->total_height)
632 if (++s->y_base == s->total_height)
634 if (s->backscroll_height < s->total_height)
635 s->backscroll_height++;
636 y1 = (s->y_base + s->height - 1) % s->total_height;
637 c = &s->cells[y1 * s->width];
638 for(x = 0; x < s->width; x++) {
640 c->t_attrib = s->t_attrib_default;
643 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
644 if (s->ds->have_text) {
647 s->text_x[1] = s->width - 1;
648 s->text_y[1] = s->height - 1;
651 if (s->ds->have_gfx) {
652 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
653 s->width * FONT_WIDTH,
654 (s->height - 1) * FONT_HEIGHT);
655 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
656 s->width * FONT_WIDTH, FONT_HEIGHT,
657 color_table_rgb[0][s->t_attrib_default.bgcol]);
660 s->update_x1 = s->width * FONT_WIDTH;
661 s->update_y1 = s->height * FONT_HEIGHT;
667 /* Set console attributes depending on the current escape codes.
668 * NOTE: I know this code is not very efficient (checking every color for it
669 * self) but it is more readable and better maintainable.
671 static void console_handle_escape(QemuConsole *s)
675 for (i=0; i<s->nb_esc_params; i++) {
676 switch (s->esc_params[i]) {
677 case 0: /* reset all console attributes to default */
678 s->t_attrib = s->t_attrib_default;
681 s->t_attrib.bold = 1;
684 s->t_attrib.uline = 1;
687 s->t_attrib.blink = 1;
690 s->t_attrib.invers = 1;
693 s->t_attrib.unvisible = 1;
696 s->t_attrib.bold = 0;
699 s->t_attrib.uline = 0;
702 s->t_attrib.blink = 0;
705 s->t_attrib.invers = 0;
708 s->t_attrib.unvisible = 0;
710 /* set foreground color */
712 s->t_attrib.fgcol=COLOR_BLACK;
715 s->t_attrib.fgcol=COLOR_RED;
718 s->t_attrib.fgcol=COLOR_GREEN;
721 s->t_attrib.fgcol=COLOR_YELLOW;
724 s->t_attrib.fgcol=COLOR_BLUE;
727 s->t_attrib.fgcol=COLOR_MAGENTA;
730 s->t_attrib.fgcol=COLOR_CYAN;
733 s->t_attrib.fgcol=COLOR_WHITE;
735 /* set background color */
737 s->t_attrib.bgcol=COLOR_BLACK;
740 s->t_attrib.bgcol=COLOR_RED;
743 s->t_attrib.bgcol=COLOR_GREEN;
746 s->t_attrib.bgcol=COLOR_YELLOW;
749 s->t_attrib.bgcol=COLOR_BLUE;
752 s->t_attrib.bgcol=COLOR_MAGENTA;
755 s->t_attrib.bgcol=COLOR_CYAN;
758 s->t_attrib.bgcol=COLOR_WHITE;
764 static void console_clear_xy(QemuConsole *s, int x, int y)
766 int y1 = (s->y_base + y) % s->total_height;
767 TextCell *c = &s->cells[y1 * s->width + x];
769 c->t_attrib = s->t_attrib_default;
773 /* set cursor, checking bounds */
774 static void set_cursor(QemuConsole *s, int x, int y)
782 if (y >= s->height) {
793 static void console_putchar(QemuConsole *s, int ch)
802 case '\r': /* carriage return */
805 case '\n': /* newline */
808 case '\b': /* backspace */
812 case '\t': /* tabspace */
813 if (s->x + (8 - (s->x % 8)) > s->width) {
817 s->x = s->x + (8 - (s->x % 8));
820 case '\a': /* alert aka. bell */
821 /* TODO: has to be implemented */
824 /* SI (shift in), character set 0 (ignored) */
827 /* SO (shift out), character set 1 (ignored) */
829 case 27: /* esc (introducing an escape sequence) */
830 s->state = TTY_STATE_ESC;
833 if (s->x >= s->width) {
838 y1 = (s->y_base + s->y) % s->total_height;
839 c = &s->cells[y1 * s->width + s->x];
841 c->t_attrib = s->t_attrib;
842 update_xy(s, s->x, s->y);
847 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
849 for(i=0;i<MAX_ESC_PARAMS;i++)
850 s->esc_params[i] = 0;
851 s->nb_esc_params = 0;
852 s->state = TTY_STATE_CSI;
854 s->state = TTY_STATE_NORM;
857 case TTY_STATE_CSI: /* handle escape sequence parameters */
858 if (ch >= '0' && ch <= '9') {
859 if (s->nb_esc_params < MAX_ESC_PARAMS) {
860 int *param = &s->esc_params[s->nb_esc_params];
861 int digit = (ch - '0');
863 *param = (*param <= (INT_MAX - digit) / 10) ?
864 *param * 10 + digit : INT_MAX;
867 if (s->nb_esc_params < MAX_ESC_PARAMS)
871 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
872 ch, s->nb_esc_params);
873 s->state = TTY_STATE_NORM;
877 if (s->esc_params[0] == 0) {
878 s->esc_params[0] = 1;
880 set_cursor(s, s->x, s->y - s->esc_params[0]);
883 /* move cursor down */
884 if (s->esc_params[0] == 0) {
885 s->esc_params[0] = 1;
887 set_cursor(s, s->x, s->y + s->esc_params[0]);
890 /* move cursor right */
891 if (s->esc_params[0] == 0) {
892 s->esc_params[0] = 1;
894 set_cursor(s, s->x + s->esc_params[0], s->y);
897 /* move cursor left */
898 if (s->esc_params[0] == 0) {
899 s->esc_params[0] = 1;
901 set_cursor(s, s->x - s->esc_params[0], s->y);
904 /* move cursor to column */
905 set_cursor(s, s->esc_params[0] - 1, s->y);
909 /* move cursor to row, column */
910 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
913 switch (s->esc_params[0]) {
915 /* clear to end of screen */
916 for (y = s->y; y < s->height; y++) {
917 for (x = 0; x < s->width; x++) {
918 if (y == s->y && x < s->x) {
921 console_clear_xy(s, x, y);
926 /* clear from beginning of screen */
927 for (y = 0; y <= s->y; y++) {
928 for (x = 0; x < s->width; x++) {
929 if (y == s->y && x > s->x) {
932 console_clear_xy(s, x, y);
937 /* clear entire screen */
938 for (y = 0; y <= s->height; y++) {
939 for (x = 0; x < s->width; x++) {
940 console_clear_xy(s, x, y);
947 switch (s->esc_params[0]) {
950 for(x = s->x; x < s->width; x++) {
951 console_clear_xy(s, x, s->y);
955 /* clear from beginning of line */
956 for (x = 0; x <= s->x; x++) {
957 console_clear_xy(s, x, s->y);
961 /* clear entire line */
962 for(x = 0; x < s->width; x++) {
963 console_clear_xy(s, x, s->y);
969 console_handle_escape(s);
972 /* report cursor position */
973 /* TODO: send ESC[row;colR */
976 /* save cursor position */
981 /* restore cursor position */
986 trace_console_putchar_unhandled(ch);
994 void console_select(unsigned int index)
996 DisplayChangeListener *dcl;
999 if (index >= MAX_CONSOLES)
1002 trace_console_select(index);
1003 s = qemu_console_lookup_by_index(index);
1005 DisplayState *ds = s->ds;
1007 if (active_console && active_console->cursor_timer) {
1008 timer_del(active_console->cursor_timer);
1012 QLIST_FOREACH(dcl, &ds->listeners, next) {
1013 if (dcl->con != NULL) {
1016 if (dcl->ops->dpy_gfx_switch) {
1017 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1020 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1021 surface_height(s->surface));
1023 if (ds->have_text) {
1024 dpy_text_resize(s, s->width, s->height);
1026 if (s->cursor_timer) {
1027 timer_mod(s->cursor_timer,
1028 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1033 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1035 QemuConsole *s = chr->opaque;
1038 s->update_x0 = s->width * FONT_WIDTH;
1039 s->update_y0 = s->height * FONT_HEIGHT;
1042 console_show_cursor(s, 0);
1043 for(i = 0; i < len; i++) {
1044 console_putchar(s, buf[i]);
1046 console_show_cursor(s, 1);
1047 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
1048 dpy_gfx_update(s, s->update_x0, s->update_y0,
1049 s->update_x1 - s->update_x0,
1050 s->update_y1 - s->update_y0);
1055 static void kbd_send_chars(void *opaque)
1057 QemuConsole *s = opaque;
1061 len = qemu_chr_be_can_write(s->chr);
1062 if (len > s->out_fifo.count)
1063 len = s->out_fifo.count;
1065 if (len > sizeof(buf))
1067 qemu_fifo_read(&s->out_fifo, buf, len);
1068 qemu_chr_be_write(s->chr, buf, len);
1070 /* characters are pending: we send them a bit later (XXX:
1071 horrible, should change char device API) */
1072 if (s->out_fifo.count > 0) {
1073 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
1077 /* called when an ascii key is pressed */
1078 void kbd_put_keysym(int keysym)
1081 uint8_t buf[16], *q;
1085 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1089 case QEMU_KEY_CTRL_UP:
1090 console_scroll(s, -1);
1092 case QEMU_KEY_CTRL_DOWN:
1093 console_scroll(s, 1);
1095 case QEMU_KEY_CTRL_PAGEUP:
1096 console_scroll(s, -10);
1098 case QEMU_KEY_CTRL_PAGEDOWN:
1099 console_scroll(s, 10);
1102 /* convert the QEMU keysym to VT100 key string */
1104 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1107 c = keysym - 0xe100;
1109 *q++ = '0' + (c / 10);
1110 *q++ = '0' + (c % 10);
1112 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1115 *q++ = keysym & 0xff;
1116 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1117 console_puts(s->chr, (const uint8_t *) "\r", 1);
1123 console_puts(s->chr, buf, q - buf);
1125 if (s->chr->chr_read) {
1126 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1133 static void text_console_invalidate(void *opaque)
1135 QemuConsole *s = (QemuConsole *) opaque;
1137 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1138 text_console_resize(s);
1143 static void text_console_update(void *opaque, console_ch_t *chardata)
1145 QemuConsole *s = (QemuConsole *) opaque;
1148 if (s->text_x[0] <= s->text_x[1]) {
1149 src = (s->y_base + s->text_y[0]) * s->width;
1150 chardata += s->text_y[0] * s->width;
1151 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1152 for (j = 0; j < s->width; j ++, src ++)
1153 console_write_ch(chardata ++, s->cells[src].ch |
1154 (s->cells[src].t_attrib.fgcol << 12) |
1155 (s->cells[src].t_attrib.bgcol << 8) |
1156 (s->cells[src].t_attrib.bold << 21));
1157 dpy_text_update(s, s->text_x[0], s->text_y[0],
1158 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1159 s->text_x[0] = s->width;
1160 s->text_y[0] = s->height;
1164 if (s->cursor_invalidate) {
1165 dpy_text_cursor(s, s->x, s->y);
1166 s->cursor_invalidate = 0;
1170 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1172 Error *local_err = NULL;
1177 if (nb_consoles >= MAX_CONSOLES)
1180 obj = object_new(TYPE_QEMU_CONSOLE);
1181 s = QEMU_CONSOLE(obj);
1182 object_property_add_link(obj, "device", TYPE_DEVICE,
1183 (Object **)&s->device, &local_err);
1184 object_property_add_uint32_ptr(obj, "head",
1185 &s->head, &local_err);
1187 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1188 (console_type == GRAPHIC_CONSOLE))) {
1192 s->console_type = console_type;
1193 if (console_type != GRAPHIC_CONSOLE) {
1194 s->index = nb_consoles;
1195 consoles[nb_consoles++] = s;
1197 /* HACK: Put graphical consoles before text consoles. */
1198 for (i = nb_consoles; i > 0; i--) {
1199 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1201 consoles[i] = consoles[i - 1];
1202 consoles[i]->index = i;
1211 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1212 int linesize, PixelFormat pf, int newflags)
1216 qemu_pixman_image_unref(surface->image);
1217 surface->image = NULL;
1219 surface->format = qemu_pixman_get_format(&pf);
1220 assert(surface->format != 0);
1221 surface->image = pixman_image_create_bits(surface->format,
1224 assert(surface->image != NULL);
1226 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1227 #ifdef HOST_WORDS_BIGENDIAN
1228 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1232 DisplaySurface *qemu_create_displaysurface(int width, int height)
1234 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1235 int linesize = width * 4;
1237 trace_displaysurface_create(surface, width, height);
1238 qemu_alloc_display(surface, width, height, linesize,
1239 qemu_default_pixelformat(32), 0);
1243 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1244 int linesize, uint8_t *data,
1247 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1249 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1251 surface->pf = qemu_different_endianness_pixelformat(bpp);
1253 surface->pf = qemu_default_pixelformat(bpp);
1256 surface->format = qemu_pixman_get_format(&surface->pf);
1257 assert(surface->format != 0);
1258 surface->image = pixman_image_create_bits(surface->format,
1260 (void *)data, linesize);
1261 assert(surface->image != NULL);
1263 #ifdef HOST_WORDS_BIGENDIAN
1264 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1270 static DisplaySurface *qemu_create_dummy_surface(void)
1272 static const char msg[] =
1273 "This VM has no graphic display device.";
1274 DisplaySurface *surface = qemu_create_displaysurface(640, 480);
1275 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1276 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1277 pixman_image_t *glyph;
1281 x = (640/FONT_WIDTH - len) / 2;
1282 y = (480/FONT_HEIGHT - 1) / 2;
1283 for (i = 0; i < len; i++) {
1284 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1285 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1286 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1287 qemu_pixman_image_unref(glyph);
1292 void qemu_free_displaysurface(DisplaySurface *surface)
1294 if (surface == NULL) {
1297 trace_displaysurface_free(surface);
1298 qemu_pixman_image_unref(surface->image);
1302 void register_displaychangelistener(DisplayChangeListener *dcl)
1304 static DisplaySurface *dummy;
1307 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1308 dcl->ds = get_alloc_displaystate();
1309 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1310 gui_setup_refresh(dcl->ds);
1315 con = active_console;
1317 if (dcl->ops->dpy_gfx_switch) {
1319 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1322 dummy = qemu_create_dummy_surface();
1324 dcl->ops->dpy_gfx_switch(dcl, dummy);
1329 void update_displaychangelistener(DisplayChangeListener *dcl,
1332 DisplayState *ds = dcl->ds;
1334 dcl->update_interval = interval;
1335 if (!ds->refreshing && ds->update_interval > interval) {
1336 timer_mod(ds->gui_timer, ds->last_update + interval);
1340 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1342 DisplayState *ds = dcl->ds;
1343 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1347 QLIST_REMOVE(dcl, next);
1348 gui_setup_refresh(ds);
1351 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1353 assert(con != NULL);
1354 con->ui_info = *info;
1355 if (con->hw_ops->ui_info) {
1356 return con->hw_ops->ui_info(con->hw, con->head, info);
1361 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1363 DisplayState *s = con->ds;
1364 DisplayChangeListener *dcl;
1365 int width = surface_width(con->surface);
1366 int height = surface_height(con->surface);
1372 w = MIN(w, width - x);
1373 h = MIN(h, height - y);
1375 if (!qemu_console_is_visible(con)) {
1378 QLIST_FOREACH(dcl, &s->listeners, next) {
1379 if (con != (dcl->con ? dcl->con : active_console)) {
1382 if (dcl->ops->dpy_gfx_update) {
1383 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1388 void dpy_gfx_replace_surface(QemuConsole *con,
1389 DisplaySurface *surface)
1391 DisplayState *s = con->ds;
1392 DisplaySurface *old_surface = con->surface;
1393 DisplayChangeListener *dcl;
1395 con->surface = surface;
1396 QLIST_FOREACH(dcl, &s->listeners, next) {
1397 if (con != (dcl->con ? dcl->con : active_console)) {
1400 if (dcl->ops->dpy_gfx_switch) {
1401 dcl->ops->dpy_gfx_switch(dcl, surface);
1404 qemu_free_displaysurface(old_surface);
1407 void dpy_refresh(DisplayState *s)
1409 DisplayChangeListener *dcl;
1411 QLIST_FOREACH(dcl, &s->listeners, next) {
1412 if (dcl->ops->dpy_refresh) {
1413 dcl->ops->dpy_refresh(dcl);
1418 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1419 int dst_x, int dst_y, int w, int h)
1421 DisplayState *s = con->ds;
1422 DisplayChangeListener *dcl;
1424 if (!qemu_console_is_visible(con)) {
1427 QLIST_FOREACH(dcl, &s->listeners, next) {
1428 if (con != (dcl->con ? dcl->con : active_console)) {
1431 if (dcl->ops->dpy_gfx_copy) {
1432 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1434 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1439 void dpy_text_cursor(QemuConsole *con, int x, int y)
1441 DisplayState *s = con->ds;
1442 DisplayChangeListener *dcl;
1444 if (!qemu_console_is_visible(con)) {
1447 QLIST_FOREACH(dcl, &s->listeners, next) {
1448 if (con != (dcl->con ? dcl->con : active_console)) {
1451 if (dcl->ops->dpy_text_cursor) {
1452 dcl->ops->dpy_text_cursor(dcl, x, y);
1457 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1459 DisplayState *s = con->ds;
1460 DisplayChangeListener *dcl;
1462 if (!qemu_console_is_visible(con)) {
1465 QLIST_FOREACH(dcl, &s->listeners, next) {
1466 if (con != (dcl->con ? dcl->con : active_console)) {
1469 if (dcl->ops->dpy_text_update) {
1470 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1475 void dpy_text_resize(QemuConsole *con, int w, int h)
1477 DisplayState *s = con->ds;
1478 struct DisplayChangeListener *dcl;
1480 if (!qemu_console_is_visible(con)) {
1483 QLIST_FOREACH(dcl, &s->listeners, next) {
1484 if (con != (dcl->con ? dcl->con : active_console)) {
1487 if (dcl->ops->dpy_text_resize) {
1488 dcl->ops->dpy_text_resize(dcl, w, h);
1493 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1495 DisplayState *s = con->ds;
1496 DisplayChangeListener *dcl;
1498 if (!qemu_console_is_visible(con)) {
1501 QLIST_FOREACH(dcl, &s->listeners, next) {
1502 if (con != (dcl->con ? dcl->con : active_console)) {
1505 if (dcl->ops->dpy_mouse_set) {
1506 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1511 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1513 DisplayState *s = con->ds;
1514 DisplayChangeListener *dcl;
1516 if (!qemu_console_is_visible(con)) {
1519 QLIST_FOREACH(dcl, &s->listeners, next) {
1520 if (con != (dcl->con ? dcl->con : active_console)) {
1523 if (dcl->ops->dpy_cursor_define) {
1524 dcl->ops->dpy_cursor_define(dcl, cursor);
1529 bool dpy_cursor_define_supported(QemuConsole *con)
1531 DisplayState *s = con->ds;
1532 DisplayChangeListener *dcl;
1534 QLIST_FOREACH(dcl, &s->listeners, next) {
1535 if (dcl->ops->dpy_cursor_define) {
1542 /***********************************************************/
1543 /* register display */
1545 /* console.c internal use only */
1546 static DisplayState *get_alloc_displaystate(void)
1548 if (!display_state) {
1549 display_state = g_new0(DisplayState, 1);
1551 return display_state;
1555 * Called by main(), after creating QemuConsoles
1556 * and before initializing ui (sdl/vnc/...).
1558 DisplayState *init_displaystate(void)
1560 Error *local_err = NULL;
1564 if (!display_state) {
1565 display_state = g_new0(DisplayState, 1);
1568 for (i = 0; i < nb_consoles; i++) {
1569 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1570 consoles[i]->ds == NULL) {
1571 text_console_do_init(consoles[i]->chr, display_state);
1574 /* Hook up into the qom tree here (not in new_console()), once
1575 * all QemuConsoles are created and the order / numbering
1576 * doesn't change any more */
1577 name = g_strdup_printf("console[%d]", i);
1578 object_property_add_child(container_get(object_get_root(), "/backend"),
1579 name, OBJECT(consoles[i]), &local_err);
1583 return display_state;
1586 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
1587 const GraphicHwOps *hw_ops,
1590 Error *local_err = NULL;
1596 ds = get_alloc_displaystate();
1597 trace_console_gfx_new();
1598 s = new_console(ds, GRAPHIC_CONSOLE);
1602 object_property_set_link(OBJECT(s), OBJECT(dev),
1603 "device", &local_err);
1604 object_property_set_int(OBJECT(s), head,
1605 "head", &local_err);
1608 s->surface = qemu_create_displaysurface(width, height);
1612 QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1614 if (index >= MAX_CONSOLES) {
1617 return consoles[index];
1620 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
1622 Error *local_err = NULL;
1627 for (i = 0; i < nb_consoles; i++) {
1631 obj = object_property_get_link(OBJECT(consoles[i]),
1632 "device", &local_err);
1633 if (DEVICE(obj) != dev) {
1636 h = object_property_get_int(OBJECT(consoles[i]),
1637 "head", &local_err);
1646 bool qemu_console_is_visible(QemuConsole *con)
1648 return (con == active_console) || (con->dcls > 0);
1651 bool qemu_console_is_graphic(QemuConsole *con)
1654 con = active_console;
1656 return con && (con->console_type == GRAPHIC_CONSOLE);
1659 bool qemu_console_is_fixedsize(QemuConsole *con)
1662 con = active_console;
1664 return con && (con->console_type != TEXT_CONSOLE);
1667 int qemu_console_get_index(QemuConsole *con)
1670 con = active_console;
1672 return con ? con->index : -1;
1675 uint32_t qemu_console_get_head(QemuConsole *con)
1678 con = active_console;
1680 return con ? con->head : -1;
1683 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1685 assert(con != NULL);
1686 return &con->ui_info;
1689 int qemu_console_get_width(QemuConsole *con, int fallback)
1692 con = active_console;
1694 return con ? surface_width(con->surface) : fallback;
1697 int qemu_console_get_height(QemuConsole *con, int fallback)
1700 con = active_console;
1702 return con ? surface_height(con->surface) : fallback;
1705 static void text_console_set_echo(CharDriverState *chr, bool echo)
1707 QemuConsole *s = chr->opaque;
1712 static void text_console_update_cursor(void *opaque)
1714 QemuConsole *s = opaque;
1716 s->cursor_visible_phase = !s->cursor_visible_phase;
1717 graphic_hw_invalidate(s);
1718 timer_mod(s->cursor_timer,
1719 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
1722 static const GraphicHwOps text_console_ops = {
1723 .invalidate = text_console_invalidate,
1724 .text_update = text_console_update,
1727 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1730 int g_width = 80 * FONT_WIDTH;
1731 int g_height = 24 * FONT_HEIGHT;
1735 chr->chr_write = console_puts;
1737 s->out_fifo.buf = s->out_fifo_buf;
1738 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1739 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
1744 s->total_height = DEFAULT_BACKSCROLL;
1748 if (active_console && active_console->surface) {
1749 g_width = surface_width(active_console->surface);
1750 g_height = surface_height(active_console->surface);
1752 s->surface = qemu_create_displaysurface(g_width, g_height);
1756 timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
1758 s->hw_ops = &text_console_ops;
1761 /* Set text attribute defaults */
1762 s->t_attrib_default.bold = 0;
1763 s->t_attrib_default.uline = 0;
1764 s->t_attrib_default.blink = 0;
1765 s->t_attrib_default.invers = 0;
1766 s->t_attrib_default.unvisible = 0;
1767 s->t_attrib_default.fgcol = COLOR_WHITE;
1768 s->t_attrib_default.bgcol = COLOR_BLACK;
1769 /* set current text attributes to default */
1770 s->t_attrib = s->t_attrib_default;
1771 text_console_resize(s);
1777 s->t_attrib.bgcol = COLOR_BLUE;
1778 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1779 console_puts(chr, (uint8_t*)msg, len);
1780 s->t_attrib = s->t_attrib_default;
1783 qemu_chr_be_generic_open(chr);
1788 static CharDriverState *text_console_init(ChardevVC *vc)
1790 CharDriverState *chr;
1793 unsigned height = 0;
1795 chr = g_malloc0(sizeof(CharDriverState));
1797 if (vc->has_width) {
1799 } else if (vc->has_cols) {
1800 width = vc->cols * FONT_WIDTH;
1803 if (vc->has_height) {
1804 height = vc->height;
1805 } else if (vc->has_rows) {
1806 height = vc->rows * FONT_HEIGHT;
1809 trace_console_txt_new(width, height);
1810 if (width == 0 || height == 0) {
1811 s = new_console(NULL, TEXT_CONSOLE);
1813 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1814 s->surface = qemu_create_displaysurface(width, height);
1824 chr->chr_set_echo = text_console_set_echo;
1825 /* console/chardev init sometimes completes elsewhere in a 2nd
1826 * stage, so defer OPENED events until they are fully initialized
1828 chr->explicit_be_open = true;
1830 if (display_state) {
1831 text_console_do_init(chr, display_state);
1836 static VcHandler *vc_handler = text_console_init;
1838 CharDriverState *vc_init(ChardevVC *vc)
1840 return vc_handler(vc);
1843 void register_vc_handler(VcHandler *handler)
1845 vc_handler = handler;
1848 void qemu_console_resize(QemuConsole *s, int width, int height)
1850 DisplaySurface *surface;
1852 assert(s->console_type == GRAPHIC_CONSOLE);
1853 surface = qemu_create_displaysurface(width, height);
1854 dpy_gfx_replace_surface(s, surface);
1857 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1858 int dst_x, int dst_y, int w, int h)
1860 assert(con->console_type == GRAPHIC_CONSOLE);
1861 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1864 DisplaySurface *qemu_console_surface(QemuConsole *console)
1866 return console->surface;
1869 DisplayState *qemu_console_displaystate(QemuConsole *console)
1874 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1878 memset(&pf, 0x00, sizeof(PixelFormat));
1880 pf.bits_per_pixel = bpp;
1881 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1882 pf.depth = bpp == 32 ? 24 : bpp;
1886 pf.rmask = 0x000000FF;
1887 pf.gmask = 0x0000FF00;
1888 pf.bmask = 0x00FF0000;
1900 pf.rmask = 0x0000FF00;
1901 pf.gmask = 0x00FF0000;
1902 pf.bmask = 0xFF000000;
1903 pf.amask = 0x00000000;
1923 PixelFormat qemu_default_pixelformat(int bpp)
1927 memset(&pf, 0x00, sizeof(PixelFormat));
1929 pf.bits_per_pixel = bpp;
1930 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1931 pf.depth = bpp == 32 ? 24 : bpp;
1935 pf.bits_per_pixel = 16;
1936 pf.rmask = 0x00007c00;
1937 pf.gmask = 0x000003E0;
1938 pf.bmask = 0x0000001F;
1950 pf.rmask = 0x0000F800;
1951 pf.gmask = 0x000007E0;
1952 pf.bmask = 0x0000001F;
1964 pf.rmask = 0x00FF0000;
1965 pf.gmask = 0x0000FF00;
1966 pf.bmask = 0x000000FF;
1978 pf.rmask = 0x00FF0000;
1979 pf.gmask = 0x0000FF00;
1980 pf.bmask = 0x000000FF;
1997 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2002 backend->vc = g_new0(ChardevVC, 1);
2004 val = qemu_opt_get_number(opts, "width", 0);
2006 backend->vc->has_width = true;
2007 backend->vc->width = val;
2010 val = qemu_opt_get_number(opts, "height", 0);
2012 backend->vc->has_height = true;
2013 backend->vc->height = val;
2016 val = qemu_opt_get_number(opts, "cols", 0);
2018 backend->vc->has_cols = true;
2019 backend->vc->cols = val;
2022 val = qemu_opt_get_number(opts, "rows", 0);
2024 backend->vc->has_rows = true;
2025 backend->vc->rows = val;
2029 static const TypeInfo qemu_console_info = {
2030 .name = TYPE_QEMU_CONSOLE,
2031 .parent = TYPE_OBJECT,
2032 .instance_size = sizeof(QemuConsole),
2033 .class_size = sizeof(QemuConsoleClass),
2037 static void register_types(void)
2039 type_register_static(&qemu_console_info);
2040 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
2044 type_init(register_types);