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
25 #include "qemu/osdep.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-ui.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qemu/timer.h"
33 #include "chardev/char-fe.h"
35 #include "exec/memory.h"
37 #define DEFAULT_BACKSCROLL 512
38 #define CONSOLE_CURSOR_PERIOD 500
40 typedef struct TextAttributes
{
50 typedef struct TextCell
{
52 TextAttributes t_attrib
;
55 #define MAX_ESC_PARAMS 3
63 typedef struct QEMUFIFO
{
66 int count
, wptr
, rptr
;
69 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
73 l
= f
->buf_size
- f
->count
;
78 l
= f
->buf_size
- f
->wptr
;
81 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
83 if (f
->wptr
>= f
->buf_size
)
92 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
100 l
= f
->buf_size
- f
->rptr
;
103 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
105 if (f
->rptr
>= f
->buf_size
)
117 TEXT_CONSOLE_FIXED_SIZE
124 console_type_t console_type
;
126 DisplaySurface
*surface
;
128 DisplayChangeListener
*gl
;
132 /* Graphic console state. */
137 const GraphicHwOps
*hw_ops
;
140 /* Text console state */
144 int backscroll_height
;
146 int x_saved
, y_saved
;
149 TextAttributes t_attrib_default
; /* default text attributes */
150 TextAttributes t_attrib
; /* currently active text attributes */
152 int text_x
[2], text_y
[2], cursor_invalidate
;
161 int esc_params
[MAX_ESC_PARAMS
];
165 /* fifo for key pressed */
167 uint8_t out_fifo_buf
[16];
168 QEMUTimer
*kbd_timer
;
170 QTAILQ_ENTRY(QemuConsole
) next
;
173 struct DisplayState
{
174 QEMUTimer
*gui_timer
;
175 uint64_t last_update
;
176 uint64_t update_interval
;
181 QLIST_HEAD(, DisplayChangeListener
) listeners
;
184 static DisplayState
*display_state
;
185 static QemuConsole
*active_console
;
186 static QTAILQ_HEAD(, QemuConsole
) consoles
=
187 QTAILQ_HEAD_INITIALIZER(consoles
);
188 static bool cursor_visible_phase
;
189 static QEMUTimer
*cursor_timer
;
191 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
192 static void dpy_refresh(DisplayState
*s
);
193 static DisplayState
*get_alloc_displaystate(void);
194 static void text_console_update_cursor_timer(void);
195 static void text_console_update_cursor(void *opaque
);
197 static void gui_update(void *opaque
)
199 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
200 uint64_t dcl_interval
;
201 DisplayState
*ds
= opaque
;
202 DisplayChangeListener
*dcl
;
205 ds
->refreshing
= true;
207 ds
->refreshing
= false;
209 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
210 dcl_interval
= dcl
->update_interval
?
211 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
212 if (interval
> dcl_interval
) {
213 interval
= dcl_interval
;
216 if (ds
->update_interval
!= interval
) {
217 ds
->update_interval
= interval
;
218 QTAILQ_FOREACH(con
, &consoles
, next
) {
219 if (con
->hw_ops
->update_interval
) {
220 con
->hw_ops
->update_interval(con
->hw
, interval
);
223 trace_console_refresh(interval
);
225 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
226 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
229 static void gui_setup_refresh(DisplayState
*ds
)
231 DisplayChangeListener
*dcl
;
232 bool need_timer
= false;
233 bool have_gfx
= false;
234 bool have_text
= false;
236 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
237 if (dcl
->ops
->dpy_refresh
!= NULL
) {
240 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
243 if (dcl
->ops
->dpy_text_update
!= NULL
) {
248 if (need_timer
&& ds
->gui_timer
== NULL
) {
249 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
250 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
252 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
253 timer_del(ds
->gui_timer
);
254 timer_free(ds
->gui_timer
);
255 ds
->gui_timer
= NULL
;
258 ds
->have_gfx
= have_gfx
;
259 ds
->have_text
= have_text
;
262 void graphic_hw_update(QemuConsole
*con
)
265 con
= active_console
;
267 if (con
&& con
->hw_ops
->gfx_update
) {
268 con
->hw_ops
->gfx_update(con
->hw
);
272 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
276 con
->gl_block
= block
;
277 if (con
->hw_ops
->gl_block
) {
278 con
->hw_ops
->gl_block(con
->hw
, block
);
282 int qemu_console_get_window_id(QemuConsole
*con
)
284 return con
->window_id
;
287 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
289 con
->window_id
= window_id
;
292 void graphic_hw_invalidate(QemuConsole
*con
)
295 con
= active_console
;
297 if (con
&& con
->hw_ops
->invalidate
) {
298 con
->hw_ops
->invalidate(con
->hw
);
302 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
305 int width
= pixman_image_get_width(ds
->image
);
306 int height
= pixman_image_get_height(ds
->image
);
311 pixman_image_t
*linebuf
;
313 trace_ppm_save(filename
, ds
);
314 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
316 error_setg(errp
, "failed to open file '%s': %s", filename
,
320 f
= fdopen(fd
, "wb");
321 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
326 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
327 for (y
= 0; y
< height
; y
++) {
328 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
330 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
331 pixman_image_get_stride(linebuf
), f
);
339 qemu_pixman_image_unref(linebuf
);
344 error_setg(errp
, "failed to write to file '%s': %s", filename
,
350 void qmp_screendump(const char *filename
, bool has_device
, const char *device
,
351 bool has_head
, int64_t head
, Error
**errp
)
354 DisplaySurface
*surface
;
357 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
364 error_setg(errp
, "'head' must be specified together with 'device'");
367 con
= qemu_console_lookup_by_index(0);
369 error_setg(errp
, "There is no console to take a screendump from");
374 graphic_hw_update(con
);
375 surface
= qemu_console_surface(con
);
377 error_setg(errp
, "no surface");
381 ppm_save(filename
, surface
, errp
);
384 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
387 con
= active_console
;
389 if (con
&& con
->hw_ops
->text_update
) {
390 con
->hw_ops
->text_update(con
->hw
, chardata
);
394 static void vga_fill_rect(QemuConsole
*con
,
395 int posx
, int posy
, int width
, int height
,
396 pixman_color_t color
)
398 DisplaySurface
*surface
= qemu_console_surface(con
);
399 pixman_rectangle16_t rect
= {
400 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
403 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
407 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
408 static void vga_bitblt(QemuConsole
*con
,
409 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
411 DisplaySurface
*surface
= qemu_console_surface(con
);
413 pixman_image_composite(PIXMAN_OP_SRC
,
414 surface
->image
, NULL
, surface
->image
,
415 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
418 /***********************************************************/
419 /* basic char display */
421 #define FONT_HEIGHT 16
426 #define QEMU_RGB(r, g, b) \
427 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
429 static const pixman_color_t color_table_rgb
[2][8] = {
431 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
432 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
433 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
434 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
435 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
436 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
437 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
438 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
441 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
442 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
443 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
444 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
445 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
446 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
447 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
448 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
452 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
453 TextAttributes
*t_attrib
)
455 static pixman_image_t
*glyphs
[256];
456 DisplaySurface
*surface
= qemu_console_surface(s
);
457 pixman_color_t fgcol
, bgcol
;
459 if (t_attrib
->invers
) {
460 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
461 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
463 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
464 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
468 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
470 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
471 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
474 static void text_console_resize(QemuConsole
*s
)
476 TextCell
*cells
, *c
, *c1
;
477 int w1
, x
, y
, last_width
;
479 last_width
= s
->width
;
480 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
481 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
487 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
488 for(y
= 0; y
< s
->total_height
; y
++) {
489 c
= &cells
[y
* s
->width
];
491 c1
= &s
->cells
[y
* last_width
];
492 for(x
= 0; x
< w1
; x
++) {
496 for(x
= w1
; x
< s
->width
; x
++) {
498 c
->t_attrib
= s
->t_attrib_default
;
506 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
508 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
509 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
510 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
511 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
514 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
516 if (!qemu_console_is_visible(s
)) {
519 if (s
->update_x0
> x
* FONT_WIDTH
)
520 s
->update_x0
= x
* FONT_WIDTH
;
521 if (s
->update_y0
> y
* FONT_HEIGHT
)
522 s
->update_y0
= y
* FONT_HEIGHT
;
523 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
524 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
525 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
526 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
529 static void update_xy(QemuConsole
*s
, int x
, int y
)
534 if (s
->ds
->have_text
) {
535 text_update_xy(s
, x
, y
);
538 y1
= (s
->y_base
+ y
) % s
->total_height
;
539 y2
= y1
- s
->y_displayed
;
541 y2
+= s
->total_height
;
543 if (y2
< s
->height
) {
547 c
= &s
->cells
[y1
* s
->width
+ x
];
548 vga_putcharxy(s
, x
, y2
, c
->ch
,
550 invalidate_xy(s
, x
, y2
);
554 static void console_show_cursor(QemuConsole
*s
, int show
)
560 if (s
->ds
->have_text
) {
561 s
->cursor_invalidate
= 1;
567 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
568 y
= y1
- s
->y_displayed
;
570 y
+= s
->total_height
;
573 c
= &s
->cells
[y1
* s
->width
+ x
];
574 if (show
&& cursor_visible_phase
) {
575 TextAttributes t_attrib
= s
->t_attrib_default
;
576 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
577 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
579 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
581 invalidate_xy(s
, x
, y
);
585 static void console_refresh(QemuConsole
*s
)
587 DisplaySurface
*surface
= qemu_console_surface(s
);
591 if (s
->ds
->have_text
) {
594 s
->text_x
[1] = s
->width
- 1;
595 s
->text_y
[1] = s
->height
- 1;
596 s
->cursor_invalidate
= 1;
599 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
600 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
602 for (y
= 0; y
< s
->height
; y
++) {
603 c
= s
->cells
+ y1
* s
->width
;
604 for (x
= 0; x
< s
->width
; x
++) {
605 vga_putcharxy(s
, x
, y
, c
->ch
,
609 if (++y1
== s
->total_height
) {
613 console_show_cursor(s
, 1);
614 dpy_gfx_update(s
, 0, 0,
615 surface_width(surface
), surface_height(surface
));
618 static void console_scroll(QemuConsole
*s
, int ydelta
)
623 for(i
= 0; i
< ydelta
; i
++) {
624 if (s
->y_displayed
== s
->y_base
)
626 if (++s
->y_displayed
== s
->total_height
)
631 i
= s
->backscroll_height
;
632 if (i
> s
->total_height
- s
->height
)
633 i
= s
->total_height
- s
->height
;
636 y1
+= s
->total_height
;
637 for(i
= 0; i
< ydelta
; i
++) {
638 if (s
->y_displayed
== y1
)
640 if (--s
->y_displayed
< 0)
641 s
->y_displayed
= s
->total_height
- 1;
647 static void console_put_lf(QemuConsole
*s
)
653 if (s
->y
>= s
->height
) {
654 s
->y
= s
->height
- 1;
656 if (s
->y_displayed
== s
->y_base
) {
657 if (++s
->y_displayed
== s
->total_height
)
660 if (++s
->y_base
== s
->total_height
)
662 if (s
->backscroll_height
< s
->total_height
)
663 s
->backscroll_height
++;
664 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
665 c
= &s
->cells
[y1
* s
->width
];
666 for(x
= 0; x
< s
->width
; x
++) {
668 c
->t_attrib
= s
->t_attrib_default
;
671 if (s
->y_displayed
== s
->y_base
) {
672 if (s
->ds
->have_text
) {
675 s
->text_x
[1] = s
->width
- 1;
676 s
->text_y
[1] = s
->height
- 1;
679 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
680 s
->width
* FONT_WIDTH
,
681 (s
->height
- 1) * FONT_HEIGHT
);
682 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
683 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
684 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
687 s
->update_x1
= s
->width
* FONT_WIDTH
;
688 s
->update_y1
= s
->height
* FONT_HEIGHT
;
693 /* Set console attributes depending on the current escape codes.
694 * NOTE: I know this code is not very efficient (checking every color for it
695 * self) but it is more readable and better maintainable.
697 static void console_handle_escape(QemuConsole
*s
)
701 for (i
=0; i
<s
->nb_esc_params
; i
++) {
702 switch (s
->esc_params
[i
]) {
703 case 0: /* reset all console attributes to default */
704 s
->t_attrib
= s
->t_attrib_default
;
707 s
->t_attrib
.bold
= 1;
710 s
->t_attrib
.uline
= 1;
713 s
->t_attrib
.blink
= 1;
716 s
->t_attrib
.invers
= 1;
719 s
->t_attrib
.unvisible
= 1;
722 s
->t_attrib
.bold
= 0;
725 s
->t_attrib
.uline
= 0;
728 s
->t_attrib
.blink
= 0;
731 s
->t_attrib
.invers
= 0;
734 s
->t_attrib
.unvisible
= 0;
736 /* set foreground color */
738 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
741 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
744 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
747 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
750 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
753 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
756 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
759 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
761 /* set background color */
763 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
766 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
769 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
772 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
775 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
778 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
781 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
784 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
790 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
792 int y1
= (s
->y_base
+ y
) % s
->total_height
;
796 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
798 c
->t_attrib
= s
->t_attrib_default
;
802 static void console_put_one(QemuConsole
*s
, int ch
)
806 if (s
->x
>= s
->width
) {
811 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
812 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
814 c
->t_attrib
= s
->t_attrib
;
815 update_xy(s
, s
->x
, s
->y
);
819 static void console_respond_str(QemuConsole
*s
, const char *buf
)
822 console_put_one(s
, *buf
);
827 /* set cursor, checking bounds */
828 static void set_cursor(QemuConsole
*s
, int x
, int y
)
836 if (y
>= s
->height
) {
847 static void console_putchar(QemuConsole
*s
, int ch
)
856 case '\r': /* carriage return */
859 case '\n': /* newline */
862 case '\b': /* backspace */
866 case '\t': /* tabspace */
867 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
871 s
->x
= s
->x
+ (8 - (s
->x
% 8));
874 case '\a': /* alert aka. bell */
875 /* TODO: has to be implemented */
878 /* SI (shift in), character set 0 (ignored) */
881 /* SO (shift out), character set 1 (ignored) */
883 case 27: /* esc (introducing an escape sequence) */
884 s
->state
= TTY_STATE_ESC
;
887 console_put_one(s
, ch
);
891 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
893 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
894 s
->esc_params
[i
] = 0;
895 s
->nb_esc_params
= 0;
896 s
->state
= TTY_STATE_CSI
;
898 s
->state
= TTY_STATE_NORM
;
901 case TTY_STATE_CSI
: /* handle escape sequence parameters */
902 if (ch
>= '0' && ch
<= '9') {
903 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
904 int *param
= &s
->esc_params
[s
->nb_esc_params
];
905 int digit
= (ch
- '0');
907 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
908 *param
* 10 + digit
: INT_MAX
;
911 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
913 if (ch
== ';' || ch
== '?') {
916 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
917 ch
, s
->nb_esc_params
);
918 s
->state
= TTY_STATE_NORM
;
922 if (s
->esc_params
[0] == 0) {
923 s
->esc_params
[0] = 1;
925 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
928 /* move cursor down */
929 if (s
->esc_params
[0] == 0) {
930 s
->esc_params
[0] = 1;
932 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
935 /* move cursor right */
936 if (s
->esc_params
[0] == 0) {
937 s
->esc_params
[0] = 1;
939 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
942 /* move cursor left */
943 if (s
->esc_params
[0] == 0) {
944 s
->esc_params
[0] = 1;
946 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
949 /* move cursor to column */
950 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
954 /* move cursor to row, column */
955 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
958 switch (s
->esc_params
[0]) {
960 /* clear to end of screen */
961 for (y
= s
->y
; y
< s
->height
; y
++) {
962 for (x
= 0; x
< s
->width
; x
++) {
963 if (y
== s
->y
&& x
< s
->x
) {
966 console_clear_xy(s
, x
, y
);
971 /* clear from beginning of screen */
972 for (y
= 0; y
<= s
->y
; y
++) {
973 for (x
= 0; x
< s
->width
; x
++) {
974 if (y
== s
->y
&& x
> s
->x
) {
977 console_clear_xy(s
, x
, y
);
982 /* clear entire screen */
983 for (y
= 0; y
<= s
->height
; y
++) {
984 for (x
= 0; x
< s
->width
; x
++) {
985 console_clear_xy(s
, x
, y
);
992 switch (s
->esc_params
[0]) {
995 for(x
= s
->x
; x
< s
->width
; x
++) {
996 console_clear_xy(s
, x
, s
->y
);
1000 /* clear from beginning of line */
1001 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1002 console_clear_xy(s
, x
, s
->y
);
1006 /* clear entire line */
1007 for(x
= 0; x
< s
->width
; x
++) {
1008 console_clear_xy(s
, x
, s
->y
);
1014 console_handle_escape(s
);
1017 switch (s
->esc_params
[0]) {
1019 /* report console status (always succeed)*/
1020 console_respond_str(s
, "\033[0n");
1023 /* report cursor position */
1024 sprintf(response
, "\033[%d;%dR",
1025 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1027 console_respond_str(s
, response
);
1032 /* save cursor position */
1037 /* restore cursor position */
1042 trace_console_putchar_unhandled(ch
);
1050 void console_select(unsigned int index
)
1052 DisplayChangeListener
*dcl
;
1055 trace_console_select(index
);
1056 s
= qemu_console_lookup_by_index(index
);
1058 DisplayState
*ds
= s
->ds
;
1062 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1063 if (dcl
->con
!= NULL
) {
1066 if (dcl
->ops
->dpy_gfx_switch
) {
1067 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1071 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1072 surface_height(s
->surface
));
1075 if (ds
->have_text
) {
1076 dpy_text_resize(s
, s
->width
, s
->height
);
1078 text_console_update_cursor(NULL
);
1082 typedef struct VCChardev
{
1084 QemuConsole
*console
;
1087 #define TYPE_CHARDEV_VC "chardev-vc"
1088 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1090 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1092 VCChardev
*drv
= VC_CHARDEV(chr
);
1093 QemuConsole
*s
= drv
->console
;
1100 s
->update_x0
= s
->width
* FONT_WIDTH
;
1101 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1104 console_show_cursor(s
, 0);
1105 for(i
= 0; i
< len
; i
++) {
1106 console_putchar(s
, buf
[i
]);
1108 console_show_cursor(s
, 1);
1109 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1110 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1111 s
->update_x1
- s
->update_x0
,
1112 s
->update_y1
- s
->update_y0
);
1117 static void kbd_send_chars(void *opaque
)
1119 QemuConsole
*s
= opaque
;
1123 len
= qemu_chr_be_can_write(s
->chr
);
1124 if (len
> s
->out_fifo
.count
)
1125 len
= s
->out_fifo
.count
;
1127 if (len
> sizeof(buf
))
1129 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1130 qemu_chr_be_write(s
->chr
, buf
, len
);
1132 /* characters are pending: we send them a bit later (XXX:
1133 horrible, should change char device API) */
1134 if (s
->out_fifo
.count
> 0) {
1135 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1139 /* called when an ascii key is pressed */
1140 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1142 uint8_t buf
[16], *q
;
1146 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1150 case QEMU_KEY_CTRL_UP
:
1151 console_scroll(s
, -1);
1153 case QEMU_KEY_CTRL_DOWN
:
1154 console_scroll(s
, 1);
1156 case QEMU_KEY_CTRL_PAGEUP
:
1157 console_scroll(s
, -10);
1159 case QEMU_KEY_CTRL_PAGEDOWN
:
1160 console_scroll(s
, 10);
1163 /* convert the QEMU keysym to VT100 key string */
1165 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1168 c
= keysym
- 0xe100;
1170 *q
++ = '0' + (c
/ 10);
1171 *q
++ = '0' + (c
% 10);
1173 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1176 *q
++ = keysym
& 0xff;
1177 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1178 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1184 vc_chr_write(s
->chr
, buf
, q
- buf
);
1187 if (be
&& be
->chr_read
) {
1188 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1195 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1196 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1197 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1198 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1199 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1200 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1201 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1202 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1203 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1204 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1205 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1208 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1209 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1210 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1211 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1212 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1213 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1214 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1215 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1216 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1219 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1223 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1227 kbd_put_keysym_console(s
, keysym
);
1231 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1235 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1236 kbd_put_keysym_console(s
, str
[i
]);
1240 void kbd_put_keysym(int keysym
)
1242 kbd_put_keysym_console(active_console
, keysym
);
1245 static void text_console_invalidate(void *opaque
)
1247 QemuConsole
*s
= (QemuConsole
*) opaque
;
1249 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1250 text_console_resize(s
);
1255 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1257 QemuConsole
*s
= (QemuConsole
*) opaque
;
1260 if (s
->text_x
[0] <= s
->text_x
[1]) {
1261 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1262 chardata
+= s
->text_y
[0] * s
->width
;
1263 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1264 for (j
= 0; j
< s
->width
; j
++, src
++) {
1265 console_write_ch(chardata
++,
1266 ATTR2CHTYPE(s
->cells
[src
].ch
,
1267 s
->cells
[src
].t_attrib
.fgcol
,
1268 s
->cells
[src
].t_attrib
.bgcol
,
1269 s
->cells
[src
].t_attrib
.bold
));
1271 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1272 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1273 s
->text_x
[0] = s
->width
;
1274 s
->text_y
[0] = s
->height
;
1278 if (s
->cursor_invalidate
) {
1279 dpy_text_cursor(s
, s
->x
, s
->y
);
1280 s
->cursor_invalidate
= 0;
1284 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1291 obj
= object_new(TYPE_QEMU_CONSOLE
);
1292 s
= QEMU_CONSOLE(obj
);
1294 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1295 (Object
**)&s
->device
,
1296 object_property_allow_set_link
,
1297 OBJ_PROP_LINK_STRONG
,
1299 object_property_add_uint32_ptr(obj
, "head",
1300 &s
->head
, &error_abort
);
1302 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1303 (console_type
== GRAPHIC_CONSOLE
))) {
1307 s
->console_type
= console_type
;
1309 if (QTAILQ_EMPTY(&consoles
)) {
1311 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1312 } else if (console_type
!= GRAPHIC_CONSOLE
|| qdev_hotplug
) {
1313 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1314 s
->index
= last
->index
+ 1;
1315 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1318 * HACK: Put graphical consoles before text consoles.
1320 * Only do that for coldplugged devices. After initial device
1321 * initialization we will not renumber the consoles any more.
1323 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1325 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1326 c
->console_type
== GRAPHIC_CONSOLE
) {
1327 c
= QTAILQ_NEXT(c
, next
);
1329 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1330 /* have no text consoles */
1331 s
->index
= c
->index
+ 1;
1332 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1334 s
->index
= c
->index
;
1335 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1336 /* renumber text consoles */
1337 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1345 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1347 qemu_pixman_image_unref(surface
->image
);
1348 surface
->image
= NULL
;
1350 surface
->format
= PIXMAN_x8r8g8b8
;
1351 surface
->image
= pixman_image_create_bits(surface
->format
,
1354 assert(surface
->image
!= NULL
);
1356 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1359 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1361 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1363 trace_displaysurface_create(surface
, width
, height
);
1364 qemu_alloc_display(surface
, width
, height
);
1368 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1369 pixman_format_code_t format
,
1370 int linesize
, uint8_t *data
)
1372 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1374 trace_displaysurface_create_from(surface
, width
, height
, format
);
1375 surface
->format
= format
;
1376 surface
->image
= pixman_image_create_bits(surface
->format
,
1378 (void *)data
, linesize
);
1379 assert(surface
->image
!= NULL
);
1384 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1386 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1388 trace_displaysurface_create_pixman(surface
);
1389 surface
->format
= pixman_image_get_format(image
);
1390 surface
->image
= pixman_image_ref(image
);
1395 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1398 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1399 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1400 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1401 pixman_image_t
*glyph
;
1405 x
= (w
/ FONT_WIDTH
- len
) / 2;
1406 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1407 for (i
= 0; i
< len
; i
++) {
1408 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1409 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1410 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1411 qemu_pixman_image_unref(glyph
);
1416 void qemu_free_displaysurface(DisplaySurface
*surface
)
1418 if (surface
== NULL
) {
1421 trace_displaysurface_free(surface
);
1422 qemu_pixman_image_unref(surface
->image
);
1426 bool console_has_gl(QemuConsole
*con
)
1428 return con
->gl
!= NULL
;
1431 bool console_has_gl_dmabuf(QemuConsole
*con
)
1433 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1436 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1438 static const char nodev
[] =
1439 "This VM has no graphic display device.";
1440 static DisplaySurface
*dummy
;
1445 if (dcl
->ops
->dpy_gl_ctx_create
) {
1446 /* display has opengl support */
1449 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1450 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1456 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1457 dcl
->ds
= get_alloc_displaystate();
1458 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1459 gui_setup_refresh(dcl
->ds
);
1464 con
= active_console
;
1466 if (dcl
->ops
->dpy_gfx_switch
) {
1468 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1471 dummy
= qemu_create_message_surface(640, 480, nodev
);
1473 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1476 text_console_update_cursor(NULL
);
1479 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1482 DisplayState
*ds
= dcl
->ds
;
1484 dcl
->update_interval
= interval
;
1485 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1486 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1490 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1492 DisplayState
*ds
= dcl
->ds
;
1493 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1497 QLIST_REMOVE(dcl
, next
);
1499 gui_setup_refresh(ds
);
1502 static void dpy_set_ui_info_timer(void *opaque
)
1504 QemuConsole
*con
= opaque
;
1506 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1509 bool dpy_ui_info_supported(QemuConsole
*con
)
1511 return con
->hw_ops
->ui_info
!= NULL
;
1514 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1516 assert(con
!= NULL
);
1518 if (!dpy_ui_info_supported(con
)) {
1521 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1522 /* nothing changed -- ignore */
1527 * Typically we get a flood of these as the user resizes the window.
1528 * Wait until the dust has settled (one second without updates), then
1529 * go notify the guest.
1531 con
->ui_info
= *info
;
1532 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1536 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1538 DisplayState
*s
= con
->ds
;
1539 DisplayChangeListener
*dcl
;
1544 width
= surface_width(con
->surface
);
1545 height
= surface_height(con
->surface
);
1551 w
= MIN(w
, width
- x
);
1552 h
= MIN(h
, height
- y
);
1554 if (!qemu_console_is_visible(con
)) {
1557 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1558 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1561 if (dcl
->ops
->dpy_gfx_update
) {
1562 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1567 void dpy_gfx_update_full(QemuConsole
*con
)
1569 if (!con
->surface
) {
1572 dpy_gfx_update(con
, 0, 0,
1573 surface_width(con
->surface
),
1574 surface_height(con
->surface
));
1577 void dpy_gfx_replace_surface(QemuConsole
*con
,
1578 DisplaySurface
*surface
)
1580 DisplayState
*s
= con
->ds
;
1581 DisplaySurface
*old_surface
= con
->surface
;
1582 DisplayChangeListener
*dcl
;
1584 assert(old_surface
!= surface
|| surface
== NULL
);
1586 con
->surface
= surface
;
1587 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1588 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1591 if (dcl
->ops
->dpy_gfx_switch
) {
1592 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1595 qemu_free_displaysurface(old_surface
);
1598 bool dpy_gfx_check_format(QemuConsole
*con
,
1599 pixman_format_code_t format
)
1601 DisplayChangeListener
*dcl
;
1602 DisplayState
*s
= con
->ds
;
1604 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1605 if (dcl
->con
&& dcl
->con
!= con
) {
1606 /* dcl bound to another console -> skip */
1609 if (dcl
->ops
->dpy_gfx_check_format
) {
1610 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1614 /* default is to whitelist native 32 bpp only */
1615 if (format
!= qemu_default_pixman_format(32, true)) {
1623 static void dpy_refresh(DisplayState
*s
)
1625 DisplayChangeListener
*dcl
;
1627 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1628 if (dcl
->ops
->dpy_refresh
) {
1629 dcl
->ops
->dpy_refresh(dcl
);
1634 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1636 DisplayState
*s
= con
->ds
;
1637 DisplayChangeListener
*dcl
;
1639 if (!qemu_console_is_visible(con
)) {
1642 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1643 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1646 if (dcl
->ops
->dpy_text_cursor
) {
1647 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1652 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1654 DisplayState
*s
= con
->ds
;
1655 DisplayChangeListener
*dcl
;
1657 if (!qemu_console_is_visible(con
)) {
1660 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1661 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1664 if (dcl
->ops
->dpy_text_update
) {
1665 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1670 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1672 DisplayState
*s
= con
->ds
;
1673 DisplayChangeListener
*dcl
;
1675 if (!qemu_console_is_visible(con
)) {
1678 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1679 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1682 if (dcl
->ops
->dpy_text_resize
) {
1683 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1688 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1690 DisplayState
*s
= con
->ds
;
1691 DisplayChangeListener
*dcl
;
1693 if (!qemu_console_is_visible(con
)) {
1696 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1697 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1700 if (dcl
->ops
->dpy_mouse_set
) {
1701 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1706 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1708 DisplayState
*s
= con
->ds
;
1709 DisplayChangeListener
*dcl
;
1711 if (!qemu_console_is_visible(con
)) {
1714 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1715 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1718 if (dcl
->ops
->dpy_cursor_define
) {
1719 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1724 bool dpy_cursor_define_supported(QemuConsole
*con
)
1726 DisplayState
*s
= con
->ds
;
1727 DisplayChangeListener
*dcl
;
1729 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1730 if (dcl
->ops
->dpy_cursor_define
) {
1737 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1738 struct QEMUGLParams
*qparams
)
1741 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1744 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1747 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1750 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1753 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1756 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1759 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1762 void dpy_gl_scanout_disable(QemuConsole
*con
)
1765 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1766 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1768 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1773 void dpy_gl_scanout_texture(QemuConsole
*con
,
1774 uint32_t backing_id
,
1775 bool backing_y_0_top
,
1776 uint32_t backing_width
,
1777 uint32_t backing_height
,
1778 uint32_t x
, uint32_t y
,
1779 uint32_t width
, uint32_t height
)
1782 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1784 backing_width
, backing_height
,
1785 x
, y
, width
, height
);
1788 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1792 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1795 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1796 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1800 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1801 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1802 have_hot
, hot_x
, hot_y
);
1806 void dpy_gl_cursor_position(QemuConsole
*con
,
1807 uint32_t pos_x
, uint32_t pos_y
)
1811 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1812 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1816 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1821 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1822 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1826 void dpy_gl_update(QemuConsole
*con
,
1827 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1830 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1833 /***********************************************************/
1834 /* register display */
1836 /* console.c internal use only */
1837 static DisplayState
*get_alloc_displaystate(void)
1839 if (!display_state
) {
1840 display_state
= g_new0(DisplayState
, 1);
1841 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1842 text_console_update_cursor
, NULL
);
1844 return display_state
;
1848 * Called by main(), after creating QemuConsoles
1849 * and before initializing ui (sdl/vnc/...).
1851 DisplayState
*init_displaystate(void)
1856 get_alloc_displaystate();
1857 QTAILQ_FOREACH(con
, &consoles
, next
) {
1858 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1860 text_console_do_init(con
->chr
, display_state
);
1863 /* Hook up into the qom tree here (not in new_console()), once
1864 * all QemuConsoles are created and the order / numbering
1865 * doesn't change any more */
1866 name
= g_strdup_printf("console[%d]", con
->index
);
1867 object_property_add_child(container_get(object_get_root(), "/backend"),
1868 name
, OBJECT(con
), &error_abort
);
1872 return display_state
;
1875 void graphic_console_set_hwops(QemuConsole
*con
,
1876 const GraphicHwOps
*hw_ops
,
1879 con
->hw_ops
= hw_ops
;
1883 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1884 const GraphicHwOps
*hw_ops
,
1887 static const char noinit
[] =
1888 "Guest has not initialized the display (yet).";
1893 DisplaySurface
*surface
;
1895 ds
= get_alloc_displaystate();
1896 s
= qemu_console_lookup_unused();
1898 trace_console_gfx_reuse(s
->index
);
1900 width
= surface_width(s
->surface
);
1901 height
= surface_height(s
->surface
);
1904 trace_console_gfx_new();
1905 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1906 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1907 dpy_set_ui_info_timer
, s
);
1909 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1911 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1915 surface
= qemu_create_message_surface(width
, height
, noinit
);
1916 dpy_gfx_replace_surface(s
, surface
);
1920 static const GraphicHwOps unused_ops
= {
1924 void graphic_console_close(QemuConsole
*con
)
1926 static const char unplugged
[] =
1927 "Guest display has been unplugged";
1928 DisplaySurface
*surface
;
1933 width
= surface_width(con
->surface
);
1934 height
= surface_height(con
->surface
);
1937 trace_console_gfx_close(con
->index
);
1938 object_property_set_link(OBJECT(con
), NULL
, "device", &error_abort
);
1939 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1942 dpy_gl_scanout_disable(con
);
1944 surface
= qemu_create_message_surface(width
, height
, unplugged
);
1945 dpy_gfx_replace_surface(con
, surface
);
1948 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1952 QTAILQ_FOREACH(con
, &consoles
, next
) {
1953 if (con
->index
== index
) {
1960 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1966 QTAILQ_FOREACH(con
, &consoles
, next
) {
1967 obj
= object_property_get_link(OBJECT(con
),
1968 "device", &error_abort
);
1969 if (DEVICE(obj
) != dev
) {
1972 h
= object_property_get_uint(OBJECT(con
),
1973 "head", &error_abort
);
1982 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1983 uint32_t head
, Error
**errp
)
1988 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1990 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1991 "Device '%s' not found", device_id
);
1995 con
= qemu_console_lookup_by_device(dev
, head
);
1997 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2005 QemuConsole
*qemu_console_lookup_unused(void)
2010 QTAILQ_FOREACH(con
, &consoles
, next
) {
2011 if (con
->hw_ops
!= &unused_ops
) {
2014 obj
= object_property_get_link(OBJECT(con
),
2015 "device", &error_abort
);
2024 bool qemu_console_is_visible(QemuConsole
*con
)
2026 return (con
== active_console
) || (con
->dcls
> 0);
2029 bool qemu_console_is_graphic(QemuConsole
*con
)
2032 con
= active_console
;
2034 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2037 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2040 con
= active_console
;
2042 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2045 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2047 assert(con
!= NULL
);
2048 return con
->gl_block
;
2051 char *qemu_console_get_label(QemuConsole
*con
)
2053 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2055 return g_strdup(object_get_typename(con
->device
));
2057 return g_strdup("VGA");
2059 if (con
->chr
&& con
->chr
->label
) {
2060 return g_strdup(con
->chr
->label
);
2062 return g_strdup_printf("vc%d", con
->index
);
2066 int qemu_console_get_index(QemuConsole
*con
)
2069 con
= active_console
;
2071 return con
? con
->index
: -1;
2074 uint32_t qemu_console_get_head(QemuConsole
*con
)
2077 con
= active_console
;
2079 return con
? con
->head
: -1;
2082 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
2084 assert(con
!= NULL
);
2085 return &con
->ui_info
;
2088 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2091 con
= active_console
;
2093 return con
? surface_width(con
->surface
) : fallback
;
2096 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2099 con
= active_console
;
2101 return con
? surface_height(con
->surface
) : fallback
;
2104 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2106 VCChardev
*drv
= VC_CHARDEV(chr
);
2107 QemuConsole
*s
= drv
->console
;
2112 static void text_console_update_cursor_timer(void)
2114 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2115 + CONSOLE_CURSOR_PERIOD
/ 2);
2118 static void text_console_update_cursor(void *opaque
)
2123 cursor_visible_phase
= !cursor_visible_phase
;
2125 QTAILQ_FOREACH(s
, &consoles
, next
) {
2126 if (qemu_console_is_graphic(s
) ||
2127 !qemu_console_is_visible(s
)) {
2131 graphic_hw_invalidate(s
);
2135 text_console_update_cursor_timer();
2139 static const GraphicHwOps text_console_ops
= {
2140 .invalidate
= text_console_invalidate
,
2141 .text_update
= text_console_update
,
2144 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2146 VCChardev
*drv
= VC_CHARDEV(chr
);
2147 QemuConsole
*s
= drv
->console
;
2148 int g_width
= 80 * FONT_WIDTH
;
2149 int g_height
= 24 * FONT_HEIGHT
;
2151 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2152 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2153 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2158 s
->total_height
= DEFAULT_BACKSCROLL
;
2162 if (active_console
&& active_console
->surface
) {
2163 g_width
= surface_width(active_console
->surface
);
2164 g_height
= surface_height(active_console
->surface
);
2166 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2169 s
->hw_ops
= &text_console_ops
;
2172 /* Set text attribute defaults */
2173 s
->t_attrib_default
.bold
= 0;
2174 s
->t_attrib_default
.uline
= 0;
2175 s
->t_attrib_default
.blink
= 0;
2176 s
->t_attrib_default
.invers
= 0;
2177 s
->t_attrib_default
.unvisible
= 0;
2178 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2179 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2180 /* set current text attributes to default */
2181 s
->t_attrib
= s
->t_attrib_default
;
2182 text_console_resize(s
);
2188 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2189 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2190 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2191 s
->t_attrib
= s
->t_attrib_default
;
2194 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2197 static void vc_chr_open(Chardev
*chr
,
2198 ChardevBackend
*backend
,
2202 ChardevVC
*vc
= backend
->u
.vc
.data
;
2203 VCChardev
*drv
= VC_CHARDEV(chr
);
2206 unsigned height
= 0;
2208 if (vc
->has_width
) {
2210 } else if (vc
->has_cols
) {
2211 width
= vc
->cols
* FONT_WIDTH
;
2214 if (vc
->has_height
) {
2215 height
= vc
->height
;
2216 } else if (vc
->has_rows
) {
2217 height
= vc
->rows
* FONT_HEIGHT
;
2220 trace_console_txt_new(width
, height
);
2221 if (width
== 0 || height
== 0) {
2222 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2224 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2225 s
->surface
= qemu_create_displaysurface(width
, height
);
2229 error_setg(errp
, "cannot create text console");
2236 if (display_state
) {
2237 text_console_do_init(chr
, display_state
);
2240 /* console/chardev init sometimes completes elsewhere in a 2nd
2241 * stage, so defer OPENED events until they are fully initialized
2246 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2248 DisplaySurface
*surface
;
2250 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2252 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2253 pixman_image_get_width(s
->surface
->image
) == width
&&
2254 pixman_image_get_height(s
->surface
->image
) == height
) {
2258 surface
= qemu_create_displaysurface(width
, height
);
2259 dpy_gfx_replace_surface(s
, surface
);
2262 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2264 return console
->surface
;
2267 PixelFormat
qemu_default_pixelformat(int bpp
)
2269 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2270 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2274 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2276 void qemu_display_register(QemuDisplay
*ui
)
2278 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2279 dpys
[ui
->type
] = ui
;
2282 bool qemu_display_find_default(DisplayOptions
*opts
)
2284 static DisplayType prio
[] = {
2291 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2292 if (dpys
[prio
[i
]] == NULL
) {
2293 ui_module_load_one(DisplayType_str(prio
[i
]));
2295 if (dpys
[prio
[i
]] == NULL
) {
2298 opts
->type
= prio
[i
];
2304 void qemu_display_early_init(DisplayOptions
*opts
)
2306 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2307 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2310 if (dpys
[opts
->type
] == NULL
) {
2311 ui_module_load_one(DisplayType_str(opts
->type
));
2313 if (dpys
[opts
->type
] == NULL
) {
2314 error_report("Display '%s' is not available.",
2315 DisplayType_str(opts
->type
));
2318 if (dpys
[opts
->type
]->early_init
) {
2319 dpys
[opts
->type
]->early_init(opts
);
2323 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2325 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2326 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2329 assert(dpys
[opts
->type
] != NULL
);
2330 dpys
[opts
->type
]->init(ds
, opts
);
2333 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2338 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2339 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2340 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2342 val
= qemu_opt_get_number(opts
, "width", 0);
2344 vc
->has_width
= true;
2348 val
= qemu_opt_get_number(opts
, "height", 0);
2350 vc
->has_height
= true;
2354 val
= qemu_opt_get_number(opts
, "cols", 0);
2356 vc
->has_cols
= true;
2360 val
= qemu_opt_get_number(opts
, "rows", 0);
2362 vc
->has_rows
= true;
2367 static const TypeInfo qemu_console_info
= {
2368 .name
= TYPE_QEMU_CONSOLE
,
2369 .parent
= TYPE_OBJECT
,
2370 .instance_size
= sizeof(QemuConsole
),
2371 .class_size
= sizeof(QemuConsoleClass
),
2374 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2376 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2378 cc
->parse
= qemu_chr_parse_vc
;
2379 cc
->open
= vc_chr_open
;
2380 cc
->chr_write
= vc_chr_write
;
2381 cc
->chr_set_echo
= vc_chr_set_echo
;
2384 static const TypeInfo char_vc_type_info
= {
2385 .name
= TYPE_CHARDEV_VC
,
2386 .parent
= TYPE_CHARDEV
,
2387 .instance_size
= sizeof(VCChardev
),
2388 .class_init
= char_vc_class_init
,
2391 void qemu_console_early_init(void)
2393 /* set the default vc driver */
2394 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2395 type_register(&char_vc_type_info
);
2399 static void register_types(void)
2401 type_register_static(&qemu_console_info
);
2404 type_init(register_types
);