2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "ui/console.h"
26 #include "hw/qdev-core.h"
27 #include "qemu/timer.h"
28 #include "qmp-commands.h"
29 #include "sysemu/char.h"
31 #include "exec/memory.h"
33 #define DEFAULT_BACKSCROLL 512
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
;
124 DisplayChangeListener
*gl
;
126 /* Graphic console state. */
131 const GraphicHwOps
*hw_ops
;
134 /* Text console state */
138 int backscroll_height
;
140 int x_saved
, y_saved
;
143 TextAttributes t_attrib_default
; /* default text attributes */
144 TextAttributes t_attrib
; /* currently active text attributes */
146 int text_x
[2], text_y
[2], cursor_invalidate
;
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
;
179 static int nb_consoles
= 0;
180 static bool cursor_visible_phase
;
181 static QEMUTimer
*cursor_timer
;
183 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
184 static void dpy_refresh(DisplayState
*s
);
185 static DisplayState
*get_alloc_displaystate(void);
186 static void text_console_update_cursor_timer(void);
187 static void text_console_update_cursor(void *opaque
);
189 static void gui_update(void *opaque
)
191 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
192 uint64_t dcl_interval
;
193 DisplayState
*ds
= opaque
;
194 DisplayChangeListener
*dcl
;
197 ds
->refreshing
= true;
199 ds
->refreshing
= false;
201 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
202 dcl_interval
= dcl
->update_interval
?
203 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
204 if (interval
> dcl_interval
) {
205 interval
= dcl_interval
;
208 if (ds
->update_interval
!= interval
) {
209 ds
->update_interval
= interval
;
210 for (i
= 0; i
< nb_consoles
; i
++) {
211 if (consoles
[i
]->hw_ops
->update_interval
) {
212 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
215 trace_console_refresh(interval
);
217 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
218 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
221 static void gui_setup_refresh(DisplayState
*ds
)
223 DisplayChangeListener
*dcl
;
224 bool need_timer
= false;
225 bool have_gfx
= false;
226 bool have_text
= false;
228 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
229 if (dcl
->ops
->dpy_refresh
!= NULL
) {
232 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
235 if (dcl
->ops
->dpy_text_update
!= NULL
) {
240 if (need_timer
&& ds
->gui_timer
== NULL
) {
241 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
242 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
244 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
245 timer_del(ds
->gui_timer
);
246 timer_free(ds
->gui_timer
);
247 ds
->gui_timer
= NULL
;
250 ds
->have_gfx
= have_gfx
;
251 ds
->have_text
= have_text
;
254 void graphic_hw_update(QemuConsole
*con
)
257 con
= active_console
;
259 if (con
&& con
->hw_ops
->gfx_update
) {
260 con
->hw_ops
->gfx_update(con
->hw
);
264 void graphic_hw_invalidate(QemuConsole
*con
)
267 con
= active_console
;
269 if (con
&& con
->hw_ops
->invalidate
) {
270 con
->hw_ops
->invalidate(con
->hw
);
274 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
277 int width
= pixman_image_get_width(ds
->image
);
278 int height
= pixman_image_get_height(ds
->image
);
283 pixman_image_t
*linebuf
;
285 trace_ppm_save(filename
, ds
);
286 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
288 error_setg(errp
, "failed to open file '%s': %s", filename
,
292 f
= fdopen(fd
, "wb");
293 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
298 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
299 for (y
= 0; y
< height
; y
++) {
300 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
302 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
303 pixman_image_get_stride(linebuf
), f
);
311 qemu_pixman_image_unref(linebuf
);
316 error_setg(errp
, "failed to write to file '%s': %s", filename
,
322 void qmp_screendump(const char *filename
, Error
**errp
)
324 QemuConsole
*con
= qemu_console_lookup_by_index(0);
325 DisplaySurface
*surface
;
328 error_setg(errp
, "There is no QemuConsole I can screendump from.");
332 graphic_hw_update(con
);
333 surface
= qemu_console_surface(con
);
334 ppm_save(filename
, surface
, errp
);
337 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
340 con
= active_console
;
342 if (con
&& con
->hw_ops
->text_update
) {
343 con
->hw_ops
->text_update(con
->hw
, chardata
);
347 static void vga_fill_rect(QemuConsole
*con
,
348 int posx
, int posy
, int width
, int height
,
349 pixman_color_t color
)
351 DisplaySurface
*surface
= qemu_console_surface(con
);
352 pixman_rectangle16_t rect
= {
353 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
356 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
360 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
361 static void vga_bitblt(QemuConsole
*con
,
362 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
364 DisplaySurface
*surface
= qemu_console_surface(con
);
366 pixman_image_composite(PIXMAN_OP_SRC
,
367 surface
->image
, NULL
, surface
->image
,
368 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
371 /***********************************************************/
372 /* basic char display */
374 #define FONT_HEIGHT 16
379 #define QEMU_RGB(r, g, b) \
380 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
382 static const pixman_color_t color_table_rgb
[2][8] = {
384 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
385 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
386 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
387 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
388 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
389 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
390 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
391 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
394 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
395 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
396 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
397 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
398 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
399 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
400 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
401 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
405 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
406 TextAttributes
*t_attrib
)
408 static pixman_image_t
*glyphs
[256];
409 DisplaySurface
*surface
= qemu_console_surface(s
);
410 pixman_color_t fgcol
, bgcol
;
412 if (t_attrib
->invers
) {
413 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
414 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
416 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
417 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
421 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
423 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
424 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
427 static void text_console_resize(QemuConsole
*s
)
429 TextCell
*cells
, *c
, *c1
;
430 int w1
, x
, y
, last_width
;
432 last_width
= s
->width
;
433 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
434 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
440 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
441 for(y
= 0; y
< s
->total_height
; y
++) {
442 c
= &cells
[y
* s
->width
];
444 c1
= &s
->cells
[y
* last_width
];
445 for(x
= 0; x
< w1
; x
++) {
449 for(x
= w1
; x
< s
->width
; x
++) {
451 c
->t_attrib
= s
->t_attrib_default
;
459 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
461 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
462 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
463 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
464 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
467 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
469 if (!qemu_console_is_visible(s
)) {
472 if (s
->update_x0
> x
* FONT_WIDTH
)
473 s
->update_x0
= x
* FONT_WIDTH
;
474 if (s
->update_y0
> y
* FONT_HEIGHT
)
475 s
->update_y0
= y
* FONT_HEIGHT
;
476 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
477 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
478 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
479 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
482 static void update_xy(QemuConsole
*s
, int x
, int y
)
487 if (s
->ds
->have_text
) {
488 text_update_xy(s
, x
, y
);
491 y1
= (s
->y_base
+ y
) % s
->total_height
;
492 y2
= y1
- s
->y_displayed
;
494 y2
+= s
->total_height
;
496 if (y2
< s
->height
) {
497 c
= &s
->cells
[y1
* s
->width
+ x
];
498 vga_putcharxy(s
, x
, y2
, c
->ch
,
500 invalidate_xy(s
, x
, y2
);
504 static void console_show_cursor(QemuConsole
*s
, int show
)
510 if (s
->ds
->have_text
) {
511 s
->cursor_invalidate
= 1;
517 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
518 y
= y1
- s
->y_displayed
;
520 y
+= s
->total_height
;
523 c
= &s
->cells
[y1
* s
->width
+ x
];
524 if (show
&& cursor_visible_phase
) {
525 TextAttributes t_attrib
= s
->t_attrib_default
;
526 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
527 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
529 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
531 invalidate_xy(s
, x
, y
);
535 static void console_refresh(QemuConsole
*s
)
537 DisplaySurface
*surface
= qemu_console_surface(s
);
541 if (s
->ds
->have_text
) {
544 s
->text_x
[1] = s
->width
- 1;
545 s
->text_y
[1] = s
->height
- 1;
546 s
->cursor_invalidate
= 1;
549 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
550 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
552 for (y
= 0; y
< s
->height
; y
++) {
553 c
= s
->cells
+ y1
* s
->width
;
554 for (x
= 0; x
< s
->width
; x
++) {
555 vga_putcharxy(s
, x
, y
, c
->ch
,
559 if (++y1
== s
->total_height
) {
563 console_show_cursor(s
, 1);
564 dpy_gfx_update(s
, 0, 0,
565 surface_width(surface
), surface_height(surface
));
568 static void console_scroll(QemuConsole
*s
, int ydelta
)
573 for(i
= 0; i
< ydelta
; i
++) {
574 if (s
->y_displayed
== s
->y_base
)
576 if (++s
->y_displayed
== s
->total_height
)
581 i
= s
->backscroll_height
;
582 if (i
> s
->total_height
- s
->height
)
583 i
= s
->total_height
- s
->height
;
586 y1
+= s
->total_height
;
587 for(i
= 0; i
< ydelta
; i
++) {
588 if (s
->y_displayed
== y1
)
590 if (--s
->y_displayed
< 0)
591 s
->y_displayed
= s
->total_height
- 1;
597 static void console_put_lf(QemuConsole
*s
)
603 if (s
->y
>= s
->height
) {
604 s
->y
= s
->height
- 1;
606 if (s
->y_displayed
== s
->y_base
) {
607 if (++s
->y_displayed
== s
->total_height
)
610 if (++s
->y_base
== s
->total_height
)
612 if (s
->backscroll_height
< s
->total_height
)
613 s
->backscroll_height
++;
614 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
615 c
= &s
->cells
[y1
* s
->width
];
616 for(x
= 0; x
< s
->width
; x
++) {
618 c
->t_attrib
= s
->t_attrib_default
;
621 if (s
->y_displayed
== s
->y_base
) {
622 if (s
->ds
->have_text
) {
625 s
->text_x
[1] = s
->width
- 1;
626 s
->text_y
[1] = s
->height
- 1;
629 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
630 s
->width
* FONT_WIDTH
,
631 (s
->height
- 1) * FONT_HEIGHT
);
632 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
633 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
634 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
637 s
->update_x1
= s
->width
* FONT_WIDTH
;
638 s
->update_y1
= s
->height
* FONT_HEIGHT
;
643 /* Set console attributes depending on the current escape codes.
644 * NOTE: I know this code is not very efficient (checking every color for it
645 * self) but it is more readable and better maintainable.
647 static void console_handle_escape(QemuConsole
*s
)
651 for (i
=0; i
<s
->nb_esc_params
; i
++) {
652 switch (s
->esc_params
[i
]) {
653 case 0: /* reset all console attributes to default */
654 s
->t_attrib
= s
->t_attrib_default
;
657 s
->t_attrib
.bold
= 1;
660 s
->t_attrib
.uline
= 1;
663 s
->t_attrib
.blink
= 1;
666 s
->t_attrib
.invers
= 1;
669 s
->t_attrib
.unvisible
= 1;
672 s
->t_attrib
.bold
= 0;
675 s
->t_attrib
.uline
= 0;
678 s
->t_attrib
.blink
= 0;
681 s
->t_attrib
.invers
= 0;
684 s
->t_attrib
.unvisible
= 0;
686 /* set foreground color */
688 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
691 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
694 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
697 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
700 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
703 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
706 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
709 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
711 /* set background color */
713 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
716 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
719 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
722 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
725 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
728 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
731 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
734 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
740 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
742 int y1
= (s
->y_base
+ y
) % s
->total_height
;
743 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
745 c
->t_attrib
= s
->t_attrib_default
;
749 /* set cursor, checking bounds */
750 static void set_cursor(QemuConsole
*s
, int x
, int y
)
758 if (y
>= s
->height
) {
769 static void console_putchar(QemuConsole
*s
, int ch
)
778 case '\r': /* carriage return */
781 case '\n': /* newline */
784 case '\b': /* backspace */
788 case '\t': /* tabspace */
789 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
793 s
->x
= s
->x
+ (8 - (s
->x
% 8));
796 case '\a': /* alert aka. bell */
797 /* TODO: has to be implemented */
800 /* SI (shift in), character set 0 (ignored) */
803 /* SO (shift out), character set 1 (ignored) */
805 case 27: /* esc (introducing an escape sequence) */
806 s
->state
= TTY_STATE_ESC
;
809 if (s
->x
>= s
->width
) {
814 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
815 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
817 c
->t_attrib
= s
->t_attrib
;
818 update_xy(s
, s
->x
, s
->y
);
823 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
825 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
826 s
->esc_params
[i
] = 0;
827 s
->nb_esc_params
= 0;
828 s
->state
= TTY_STATE_CSI
;
830 s
->state
= TTY_STATE_NORM
;
833 case TTY_STATE_CSI
: /* handle escape sequence parameters */
834 if (ch
>= '0' && ch
<= '9') {
835 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
836 int *param
= &s
->esc_params
[s
->nb_esc_params
];
837 int digit
= (ch
- '0');
839 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
840 *param
* 10 + digit
: INT_MAX
;
843 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
847 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
848 ch
, s
->nb_esc_params
);
849 s
->state
= TTY_STATE_NORM
;
853 if (s
->esc_params
[0] == 0) {
854 s
->esc_params
[0] = 1;
856 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
859 /* move cursor down */
860 if (s
->esc_params
[0] == 0) {
861 s
->esc_params
[0] = 1;
863 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
866 /* move cursor right */
867 if (s
->esc_params
[0] == 0) {
868 s
->esc_params
[0] = 1;
870 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
873 /* move cursor left */
874 if (s
->esc_params
[0] == 0) {
875 s
->esc_params
[0] = 1;
877 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
880 /* move cursor to column */
881 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
885 /* move cursor to row, column */
886 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
889 switch (s
->esc_params
[0]) {
891 /* clear to end of screen */
892 for (y
= s
->y
; y
< s
->height
; y
++) {
893 for (x
= 0; x
< s
->width
; x
++) {
894 if (y
== s
->y
&& x
< s
->x
) {
897 console_clear_xy(s
, x
, y
);
902 /* clear from beginning of screen */
903 for (y
= 0; y
<= s
->y
; y
++) {
904 for (x
= 0; x
< s
->width
; x
++) {
905 if (y
== s
->y
&& x
> s
->x
) {
908 console_clear_xy(s
, x
, y
);
913 /* clear entire screen */
914 for (y
= 0; y
<= s
->height
; y
++) {
915 for (x
= 0; x
< s
->width
; x
++) {
916 console_clear_xy(s
, x
, y
);
923 switch (s
->esc_params
[0]) {
926 for(x
= s
->x
; x
< s
->width
; x
++) {
927 console_clear_xy(s
, x
, s
->y
);
931 /* clear from beginning of line */
932 for (x
= 0; x
<= s
->x
; x
++) {
933 console_clear_xy(s
, x
, s
->y
);
937 /* clear entire line */
938 for(x
= 0; x
< s
->width
; x
++) {
939 console_clear_xy(s
, x
, s
->y
);
945 console_handle_escape(s
);
948 /* report cursor position */
949 /* TODO: send ESC[row;colR */
952 /* save cursor position */
957 /* restore cursor position */
962 trace_console_putchar_unhandled(ch
);
970 void console_select(unsigned int index
)
972 DisplayChangeListener
*dcl
;
975 trace_console_select(index
);
976 s
= qemu_console_lookup_by_index(index
);
978 DisplayState
*ds
= s
->ds
;
982 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
983 if (dcl
->con
!= NULL
) {
986 if (dcl
->ops
->dpy_gfx_switch
) {
987 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
990 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
991 surface_height(s
->surface
));
994 dpy_text_resize(s
, s
->width
, s
->height
);
996 text_console_update_cursor(NULL
);
1000 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1002 QemuConsole
*s
= chr
->opaque
;
1005 s
->update_x0
= s
->width
* FONT_WIDTH
;
1006 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1009 console_show_cursor(s
, 0);
1010 for(i
= 0; i
< len
; i
++) {
1011 console_putchar(s
, buf
[i
]);
1013 console_show_cursor(s
, 1);
1014 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1015 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1016 s
->update_x1
- s
->update_x0
,
1017 s
->update_y1
- s
->update_y0
);
1022 static void kbd_send_chars(void *opaque
)
1024 QemuConsole
*s
= opaque
;
1028 len
= qemu_chr_be_can_write(s
->chr
);
1029 if (len
> s
->out_fifo
.count
)
1030 len
= s
->out_fifo
.count
;
1032 if (len
> sizeof(buf
))
1034 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1035 qemu_chr_be_write(s
->chr
, buf
, len
);
1037 /* characters are pending: we send them a bit later (XXX:
1038 horrible, should change char device API) */
1039 if (s
->out_fifo
.count
> 0) {
1040 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1044 /* called when an ascii key is pressed */
1045 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1047 uint8_t buf
[16], *q
;
1050 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1054 case QEMU_KEY_CTRL_UP
:
1055 console_scroll(s
, -1);
1057 case QEMU_KEY_CTRL_DOWN
:
1058 console_scroll(s
, 1);
1060 case QEMU_KEY_CTRL_PAGEUP
:
1061 console_scroll(s
, -10);
1063 case QEMU_KEY_CTRL_PAGEDOWN
:
1064 console_scroll(s
, 10);
1067 /* convert the QEMU keysym to VT100 key string */
1069 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1072 c
= keysym
- 0xe100;
1074 *q
++ = '0' + (c
/ 10);
1075 *q
++ = '0' + (c
% 10);
1077 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1080 *q
++ = keysym
& 0xff;
1081 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1082 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1088 console_puts(s
->chr
, buf
, q
- buf
);
1090 if (s
->chr
->chr_read
) {
1091 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1098 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1099 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1100 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1101 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1102 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1103 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1104 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1105 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1106 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1107 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1110 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1114 keysym
= qcode_to_keysym
[qcode
];
1118 kbd_put_keysym_console(s
, keysym
);
1122 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1126 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1127 kbd_put_keysym_console(s
, str
[i
]);
1131 void kbd_put_keysym(int keysym
)
1133 kbd_put_keysym_console(active_console
, keysym
);
1136 static void text_console_invalidate(void *opaque
)
1138 QemuConsole
*s
= (QemuConsole
*) opaque
;
1140 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1141 text_console_resize(s
);
1146 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1148 QemuConsole
*s
= (QemuConsole
*) opaque
;
1151 if (s
->text_x
[0] <= s
->text_x
[1]) {
1152 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1153 chardata
+= s
->text_y
[0] * s
->width
;
1154 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1155 for (j
= 0; j
< s
->width
; j
++, src
++) {
1156 console_write_ch(chardata
++,
1157 ATTR2CHTYPE(s
->cells
[src
].ch
,
1158 s
->cells
[src
].t_attrib
.fgcol
,
1159 s
->cells
[src
].t_attrib
.bgcol
,
1160 s
->cells
[src
].t_attrib
.bold
));
1162 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1163 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1164 s
->text_x
[0] = s
->width
;
1165 s
->text_y
[0] = s
->height
;
1169 if (s
->cursor_invalidate
) {
1170 dpy_text_cursor(s
, s
->x
, s
->y
);
1171 s
->cursor_invalidate
= 0;
1175 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1182 obj
= object_new(TYPE_QEMU_CONSOLE
);
1183 s
= QEMU_CONSOLE(obj
);
1185 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1186 (Object
**)&s
->device
,
1187 object_property_allow_set_link
,
1188 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1190 object_property_add_uint32_ptr(obj
, "head",
1191 &s
->head
, &error_abort
);
1193 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1194 (console_type
== GRAPHIC_CONSOLE
))) {
1198 s
->console_type
= console_type
;
1200 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1201 if (console_type
!= GRAPHIC_CONSOLE
) {
1202 s
->index
= nb_consoles
;
1203 consoles
[nb_consoles
++] = s
;
1205 /* HACK: Put graphical consoles before text consoles. */
1206 for (i
= nb_consoles
; i
> 0; i
--) {
1207 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1209 consoles
[i
] = consoles
[i
- 1];
1210 consoles
[i
]->index
= i
;
1219 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1221 qemu_pixman_image_unref(surface
->image
);
1222 surface
->image
= NULL
;
1224 surface
->format
= PIXMAN_x8r8g8b8
;
1225 surface
->image
= pixman_image_create_bits(surface
->format
,
1228 assert(surface
->image
!= NULL
);
1230 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1233 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1235 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1237 trace_displaysurface_create(surface
, width
, height
);
1238 qemu_alloc_display(surface
, width
, height
);
1242 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1243 pixman_format_code_t format
,
1244 int linesize
, uint8_t *data
)
1246 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1248 trace_displaysurface_create_from(surface
, width
, height
, format
);
1249 surface
->format
= format
;
1250 surface
->image
= pixman_image_create_bits(surface
->format
,
1252 (void *)data
, linesize
);
1253 assert(surface
->image
!= NULL
);
1258 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1261 void *data
= pixman_image_get_data(image
);
1262 uint32_t size
= pixman_image_get_stride(image
) *
1263 pixman_image_get_height(image
);
1264 cpu_physical_memory_unmap(data
, size
, 0, 0);
1267 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1268 pixman_format_code_t format
,
1269 int linesize
, uint64_t addr
)
1271 DisplaySurface
*surface
;
1275 if (linesize
== 0) {
1276 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1279 size
= (hwaddr
)linesize
* height
;
1280 data
= cpu_physical_memory_map(addr
, &size
, 0);
1281 if (size
!= (hwaddr
)linesize
* height
) {
1282 cpu_physical_memory_unmap(data
, size
, 0, 0);
1286 surface
= qemu_create_displaysurface_from
1287 (width
, height
, format
, linesize
, data
);
1288 pixman_image_set_destroy_function
1289 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1294 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1297 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1298 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1299 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1300 pixman_image_t
*glyph
;
1304 x
= (w
/ FONT_WIDTH
- len
) / 2;
1305 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1306 for (i
= 0; i
< len
; i
++) {
1307 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1308 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1309 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1310 qemu_pixman_image_unref(glyph
);
1315 void qemu_free_displaysurface(DisplaySurface
*surface
)
1317 if (surface
== NULL
) {
1320 trace_displaysurface_free(surface
);
1321 qemu_pixman_image_unref(surface
->image
);
1325 bool console_has_gl(QemuConsole
*con
)
1327 return con
->gl
!= NULL
;
1330 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1332 static const char nodev
[] =
1333 "This VM has no graphic display device.";
1334 static DisplaySurface
*dummy
;
1337 if (dcl
->ops
->dpy_gl_ctx_create
) {
1338 /* display has opengl support */
1341 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1342 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1348 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1349 dcl
->ds
= get_alloc_displaystate();
1350 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1351 gui_setup_refresh(dcl
->ds
);
1356 con
= active_console
;
1358 if (dcl
->ops
->dpy_gfx_switch
) {
1360 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1363 dummy
= qemu_create_message_surface(640, 480, nodev
);
1365 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1368 text_console_update_cursor(NULL
);
1371 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1374 DisplayState
*ds
= dcl
->ds
;
1376 dcl
->update_interval
= interval
;
1377 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1378 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1382 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1384 DisplayState
*ds
= dcl
->ds
;
1385 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1389 QLIST_REMOVE(dcl
, next
);
1390 gui_setup_refresh(ds
);
1393 static void dpy_set_ui_info_timer(void *opaque
)
1395 QemuConsole
*con
= opaque
;
1397 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1400 bool dpy_ui_info_supported(QemuConsole
*con
)
1402 return con
->hw_ops
->ui_info
!= NULL
;
1405 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1407 assert(con
!= NULL
);
1408 con
->ui_info
= *info
;
1409 if (!dpy_ui_info_supported(con
)) {
1414 * Typically we get a flood of these as the user resizes the window.
1415 * Wait until the dust has settled (one second without updates), then
1416 * go notify the guest.
1418 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1422 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1424 DisplayState
*s
= con
->ds
;
1425 DisplayChangeListener
*dcl
;
1430 width
= surface_width(con
->surface
);
1431 height
= surface_height(con
->surface
);
1437 w
= MIN(w
, width
- x
);
1438 h
= MIN(h
, height
- y
);
1440 if (!qemu_console_is_visible(con
)) {
1443 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1444 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1447 if (dcl
->ops
->dpy_gfx_update
) {
1448 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1453 void dpy_gfx_replace_surface(QemuConsole
*con
,
1454 DisplaySurface
*surface
)
1456 DisplayState
*s
= con
->ds
;
1457 DisplaySurface
*old_surface
= con
->surface
;
1458 DisplayChangeListener
*dcl
;
1460 con
->surface
= surface
;
1461 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1462 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1465 if (dcl
->ops
->dpy_gfx_switch
) {
1466 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1469 qemu_free_displaysurface(old_surface
);
1472 bool dpy_gfx_check_format(QemuConsole
*con
,
1473 pixman_format_code_t format
)
1475 DisplayChangeListener
*dcl
;
1476 DisplayState
*s
= con
->ds
;
1478 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1479 if (dcl
->con
&& dcl
->con
!= con
) {
1480 /* dcl bound to another console -> skip */
1483 if (dcl
->ops
->dpy_gfx_check_format
) {
1484 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1488 /* default is to whitelist native 32 bpp only */
1489 if (format
!= qemu_default_pixman_format(32, true)) {
1497 static void dpy_refresh(DisplayState
*s
)
1499 DisplayChangeListener
*dcl
;
1501 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1502 if (dcl
->ops
->dpy_refresh
) {
1503 dcl
->ops
->dpy_refresh(dcl
);
1508 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1509 int dst_x
, int dst_y
, int w
, int h
)
1511 DisplayState
*s
= con
->ds
;
1512 DisplayChangeListener
*dcl
;
1514 if (!qemu_console_is_visible(con
)) {
1517 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1518 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1521 if (dcl
->ops
->dpy_gfx_copy
) {
1522 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1524 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1529 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1531 DisplayState
*s
= con
->ds
;
1532 DisplayChangeListener
*dcl
;
1534 if (!qemu_console_is_visible(con
)) {
1537 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1538 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1541 if (dcl
->ops
->dpy_text_cursor
) {
1542 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1547 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1549 DisplayState
*s
= con
->ds
;
1550 DisplayChangeListener
*dcl
;
1552 if (!qemu_console_is_visible(con
)) {
1555 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1556 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1559 if (dcl
->ops
->dpy_text_update
) {
1560 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1565 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1567 DisplayState
*s
= con
->ds
;
1568 DisplayChangeListener
*dcl
;
1570 if (!qemu_console_is_visible(con
)) {
1573 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1574 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1577 if (dcl
->ops
->dpy_text_resize
) {
1578 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1583 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1585 DisplayState
*s
= con
->ds
;
1586 DisplayChangeListener
*dcl
;
1588 if (!qemu_console_is_visible(con
)) {
1591 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1592 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1595 if (dcl
->ops
->dpy_mouse_set
) {
1596 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1601 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1603 DisplayState
*s
= con
->ds
;
1604 DisplayChangeListener
*dcl
;
1606 if (!qemu_console_is_visible(con
)) {
1609 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1610 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1613 if (dcl
->ops
->dpy_cursor_define
) {
1614 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1619 bool dpy_cursor_define_supported(QemuConsole
*con
)
1621 DisplayState
*s
= con
->ds
;
1622 DisplayChangeListener
*dcl
;
1624 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1625 if (dcl
->ops
->dpy_cursor_define
) {
1632 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1633 struct QEMUGLParams
*qparams
)
1636 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1639 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1642 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1645 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1648 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1651 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1654 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1657 void dpy_gl_scanout(QemuConsole
*con
,
1658 uint32_t backing_id
, bool backing_y_0_top
,
1659 uint32_t x
, uint32_t y
, uint32_t width
, uint32_t height
)
1662 con
->gl
->ops
->dpy_gl_scanout(con
->gl
, backing_id
,
1664 x
, y
, width
, height
);
1667 void dpy_gl_update(QemuConsole
*con
,
1668 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1671 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1674 /***********************************************************/
1675 /* register display */
1677 /* console.c internal use only */
1678 static DisplayState
*get_alloc_displaystate(void)
1680 if (!display_state
) {
1681 display_state
= g_new0(DisplayState
, 1);
1682 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1683 text_console_update_cursor
, NULL
);
1685 return display_state
;
1689 * Called by main(), after creating QemuConsoles
1690 * and before initializing ui (sdl/vnc/...).
1692 DisplayState
*init_displaystate(void)
1697 get_alloc_displaystate();
1698 for (i
= 0; i
< nb_consoles
; i
++) {
1699 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1700 consoles
[i
]->ds
== NULL
) {
1701 text_console_do_init(consoles
[i
]->chr
, display_state
);
1704 /* Hook up into the qom tree here (not in new_console()), once
1705 * all QemuConsoles are created and the order / numbering
1706 * doesn't change any more */
1707 name
= g_strdup_printf("console[%d]", i
);
1708 object_property_add_child(container_get(object_get_root(), "/backend"),
1709 name
, OBJECT(consoles
[i
]), &error_abort
);
1713 return display_state
;
1716 void graphic_console_set_hwops(QemuConsole
*con
,
1717 const GraphicHwOps
*hw_ops
,
1720 con
->hw_ops
= hw_ops
;
1724 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1725 const GraphicHwOps
*hw_ops
,
1728 static const char noinit
[] =
1729 "Guest has not initialized the display (yet).";
1735 ds
= get_alloc_displaystate();
1736 trace_console_gfx_new();
1737 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1738 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1739 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1741 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1745 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1749 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1751 if (index
>= nb_consoles
) {
1754 return consoles
[index
];
1757 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1763 for (i
= 0; i
< nb_consoles
; i
++) {
1767 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1768 "device", &error_abort
);
1769 if (DEVICE(obj
) != dev
) {
1772 h
= object_property_get_int(OBJECT(consoles
[i
]),
1773 "head", &error_abort
);
1782 bool qemu_console_is_visible(QemuConsole
*con
)
1784 return (con
== active_console
) || (con
->dcls
> 0);
1787 bool qemu_console_is_graphic(QemuConsole
*con
)
1790 con
= active_console
;
1792 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1795 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1798 con
= active_console
;
1800 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1803 char *qemu_console_get_label(QemuConsole
*con
)
1805 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1807 return g_strdup(object_get_typename(con
->device
));
1809 return g_strdup("VGA");
1811 if (con
->chr
&& con
->chr
->label
) {
1812 return g_strdup(con
->chr
->label
);
1814 return g_strdup_printf("vc%d", con
->index
);
1818 int qemu_console_get_index(QemuConsole
*con
)
1821 con
= active_console
;
1823 return con
? con
->index
: -1;
1826 uint32_t qemu_console_get_head(QemuConsole
*con
)
1829 con
= active_console
;
1831 return con
? con
->head
: -1;
1834 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1836 assert(con
!= NULL
);
1837 return &con
->ui_info
;
1840 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1843 con
= active_console
;
1845 return con
? surface_width(con
->surface
) : fallback
;
1848 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1851 con
= active_console
;
1853 return con
? surface_height(con
->surface
) : fallback
;
1856 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1858 QemuConsole
*s
= chr
->opaque
;
1863 static void text_console_update_cursor_timer(void)
1865 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1866 + CONSOLE_CURSOR_PERIOD
/ 2);
1869 static void text_console_update_cursor(void *opaque
)
1874 cursor_visible_phase
= !cursor_visible_phase
;
1876 for (i
= 0; i
< nb_consoles
; i
++) {
1878 if (qemu_console_is_graphic(s
) ||
1879 !qemu_console_is_visible(s
)) {
1883 graphic_hw_invalidate(s
);
1887 text_console_update_cursor_timer();
1891 static const GraphicHwOps text_console_ops
= {
1892 .invalidate
= text_console_invalidate
,
1893 .text_update
= text_console_update
,
1896 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1899 int g_width
= 80 * FONT_WIDTH
;
1900 int g_height
= 24 * FONT_HEIGHT
;
1904 chr
->chr_write
= console_puts
;
1906 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1907 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1908 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
1913 s
->total_height
= DEFAULT_BACKSCROLL
;
1917 if (active_console
&& active_console
->surface
) {
1918 g_width
= surface_width(active_console
->surface
);
1919 g_height
= surface_height(active_console
->surface
);
1921 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
1924 s
->hw_ops
= &text_console_ops
;
1927 /* Set text attribute defaults */
1928 s
->t_attrib_default
.bold
= 0;
1929 s
->t_attrib_default
.uline
= 0;
1930 s
->t_attrib_default
.blink
= 0;
1931 s
->t_attrib_default
.invers
= 0;
1932 s
->t_attrib_default
.unvisible
= 0;
1933 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
1934 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
1935 /* set current text attributes to default */
1936 s
->t_attrib
= s
->t_attrib_default
;
1937 text_console_resize(s
);
1943 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
1944 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1945 console_puts(chr
, (uint8_t*)msg
, len
);
1946 s
->t_attrib
= s
->t_attrib_default
;
1949 qemu_chr_be_generic_open(chr
);
1954 static CharDriverState
*text_console_init(ChardevVC
*vc
, Error
**errp
)
1956 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
1957 CharDriverState
*chr
;
1960 unsigned height
= 0;
1962 chr
= qemu_chr_alloc(common
, errp
);
1967 if (vc
->has_width
) {
1969 } else if (vc
->has_cols
) {
1970 width
= vc
->cols
* FONT_WIDTH
;
1973 if (vc
->has_height
) {
1974 height
= vc
->height
;
1975 } else if (vc
->has_rows
) {
1976 height
= vc
->rows
* FONT_HEIGHT
;
1979 trace_console_txt_new(width
, height
);
1980 if (width
== 0 || height
== 0) {
1981 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
1983 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
1984 s
->surface
= qemu_create_displaysurface(width
, height
);
1989 error_setg(errp
, "cannot create text console");
1995 chr
->chr_set_echo
= text_console_set_echo
;
1996 /* console/chardev init sometimes completes elsewhere in a 2nd
1997 * stage, so defer OPENED events until they are fully initialized
1999 chr
->explicit_be_open
= true;
2001 if (display_state
) {
2002 text_console_do_init(chr
, display_state
);
2007 static VcHandler
*vc_handler
= text_console_init
;
2009 static CharDriverState
*vc_init(const char *id
, ChardevBackend
*backend
,
2010 ChardevReturn
*ret
, Error
**errp
)
2012 return vc_handler(backend
->u
.vc
, errp
);
2015 void register_vc_handler(VcHandler
*handler
)
2017 vc_handler
= handler
;
2020 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2022 DisplaySurface
*surface
;
2024 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2025 surface
= qemu_create_displaysurface(width
, height
);
2026 dpy_gfx_replace_surface(s
, surface
);
2029 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
2030 int dst_x
, int dst_y
, int w
, int h
)
2032 assert(con
->console_type
== GRAPHIC_CONSOLE
);
2033 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
2036 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2038 return console
->surface
;
2041 PixelFormat
qemu_default_pixelformat(int bpp
)
2043 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2044 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2048 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
2053 backend
->u
.vc
= g_new0(ChardevVC
, 1);
2055 val
= qemu_opt_get_number(opts
, "width", 0);
2057 backend
->u
.vc
->has_width
= true;
2058 backend
->u
.vc
->width
= val
;
2061 val
= qemu_opt_get_number(opts
, "height", 0);
2063 backend
->u
.vc
->has_height
= true;
2064 backend
->u
.vc
->height
= val
;
2067 val
= qemu_opt_get_number(opts
, "cols", 0);
2069 backend
->u
.vc
->has_cols
= true;
2070 backend
->u
.vc
->cols
= val
;
2073 val
= qemu_opt_get_number(opts
, "rows", 0);
2075 backend
->u
.vc
->has_rows
= true;
2076 backend
->u
.vc
->rows
= val
;
2080 static const TypeInfo qemu_console_info
= {
2081 .name
= TYPE_QEMU_CONSOLE
,
2082 .parent
= TYPE_OBJECT
,
2083 .instance_size
= sizeof(QemuConsole
),
2084 .class_size
= sizeof(QemuConsoleClass
),
2088 static void register_types(void)
2090 type_register_static(&qemu_console_info
);
2091 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC
, qemu_chr_parse_vc
,
2095 type_init(register_types
);