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/option.h"
31 #include "qemu/timer.h"
32 #include "chardev/char-fe.h"
34 #include "exec/memory.h"
36 #define DEFAULT_BACKSCROLL 512
37 #define CONSOLE_CURSOR_PERIOD 500
39 typedef struct TextAttributes
{
49 typedef struct TextCell
{
51 TextAttributes t_attrib
;
54 #define MAX_ESC_PARAMS 3
62 typedef struct QEMUFIFO
{
65 int count
, wptr
, rptr
;
68 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
72 l
= f
->buf_size
- f
->count
;
77 l
= f
->buf_size
- f
->wptr
;
80 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
82 if (f
->wptr
>= f
->buf_size
)
91 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
99 l
= f
->buf_size
- f
->rptr
;
102 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
104 if (f
->rptr
>= f
->buf_size
)
116 TEXT_CONSOLE_FIXED_SIZE
123 console_type_t console_type
;
125 DisplaySurface
*surface
;
127 DisplayChangeListener
*gl
;
131 /* Graphic console state. */
136 const GraphicHwOps
*hw_ops
;
139 /* Text console state */
143 int backscroll_height
;
145 int x_saved
, y_saved
;
148 TextAttributes t_attrib_default
; /* default text attributes */
149 TextAttributes t_attrib
; /* currently active text attributes */
151 int text_x
[2], text_y
[2], cursor_invalidate
;
160 int esc_params
[MAX_ESC_PARAMS
];
164 /* fifo for key pressed */
166 uint8_t out_fifo_buf
[16];
167 QEMUTimer
*kbd_timer
;
170 struct DisplayState
{
171 QEMUTimer
*gui_timer
;
172 uint64_t last_update
;
173 uint64_t update_interval
;
178 QLIST_HEAD(, DisplayChangeListener
) listeners
;
181 static DisplayState
*display_state
;
182 static QemuConsole
*active_console
;
183 static QemuConsole
**consoles
;
184 static int nb_consoles
= 0;
185 static bool cursor_visible_phase
;
186 static QEMUTimer
*cursor_timer
;
188 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
189 static void dpy_refresh(DisplayState
*s
);
190 static DisplayState
*get_alloc_displaystate(void);
191 static void text_console_update_cursor_timer(void);
192 static void text_console_update_cursor(void *opaque
);
194 static void gui_update(void *opaque
)
196 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
197 uint64_t dcl_interval
;
198 DisplayState
*ds
= opaque
;
199 DisplayChangeListener
*dcl
;
202 ds
->refreshing
= true;
204 ds
->refreshing
= false;
206 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
207 dcl_interval
= dcl
->update_interval
?
208 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
209 if (interval
> dcl_interval
) {
210 interval
= dcl_interval
;
213 if (ds
->update_interval
!= interval
) {
214 ds
->update_interval
= interval
;
215 for (i
= 0; i
< nb_consoles
; i
++) {
216 if (consoles
[i
]->hw_ops
->update_interval
) {
217 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
220 trace_console_refresh(interval
);
222 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
223 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
226 static void gui_setup_refresh(DisplayState
*ds
)
228 DisplayChangeListener
*dcl
;
229 bool need_timer
= false;
230 bool have_gfx
= false;
231 bool have_text
= false;
233 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
234 if (dcl
->ops
->dpy_refresh
!= NULL
) {
237 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
240 if (dcl
->ops
->dpy_text_update
!= NULL
) {
245 if (need_timer
&& ds
->gui_timer
== NULL
) {
246 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
247 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
249 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
250 timer_del(ds
->gui_timer
);
251 timer_free(ds
->gui_timer
);
252 ds
->gui_timer
= NULL
;
255 ds
->have_gfx
= have_gfx
;
256 ds
->have_text
= have_text
;
259 void graphic_hw_update(QemuConsole
*con
)
262 con
= active_console
;
264 if (con
&& con
->hw_ops
->gfx_update
) {
265 con
->hw_ops
->gfx_update(con
->hw
);
269 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
273 con
->gl_block
= block
;
274 if (con
->hw_ops
->gl_block
) {
275 con
->hw_ops
->gl_block(con
->hw
, block
);
279 int qemu_console_get_window_id(QemuConsole
*con
)
281 return con
->window_id
;
284 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
286 con
->window_id
= window_id
;
289 void graphic_hw_invalidate(QemuConsole
*con
)
292 con
= active_console
;
294 if (con
&& con
->hw_ops
->invalidate
) {
295 con
->hw_ops
->invalidate(con
->hw
);
299 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
302 int width
= pixman_image_get_width(ds
->image
);
303 int height
= pixman_image_get_height(ds
->image
);
308 pixman_image_t
*linebuf
;
310 trace_ppm_save(filename
, ds
);
311 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
313 error_setg(errp
, "failed to open file '%s': %s", filename
,
317 f
= fdopen(fd
, "wb");
318 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
323 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
324 for (y
= 0; y
< height
; y
++) {
325 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
327 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
328 pixman_image_get_stride(linebuf
), f
);
336 qemu_pixman_image_unref(linebuf
);
341 error_setg(errp
, "failed to write to file '%s': %s", filename
,
347 void qmp_screendump(const char *filename
, bool has_device
, const char *device
,
348 bool has_head
, int64_t head
, Error
**errp
)
351 DisplaySurface
*surface
;
354 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
361 error_setg(errp
, "'head' must be specified together with 'device'");
364 con
= qemu_console_lookup_by_index(0);
366 error_setg(errp
, "There is no console to take a screendump from");
371 graphic_hw_update(con
);
372 surface
= qemu_console_surface(con
);
373 ppm_save(filename
, surface
, errp
);
376 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
379 con
= active_console
;
381 if (con
&& con
->hw_ops
->text_update
) {
382 con
->hw_ops
->text_update(con
->hw
, chardata
);
386 static void vga_fill_rect(QemuConsole
*con
,
387 int posx
, int posy
, int width
, int height
,
388 pixman_color_t color
)
390 DisplaySurface
*surface
= qemu_console_surface(con
);
391 pixman_rectangle16_t rect
= {
392 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
395 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
399 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
400 static void vga_bitblt(QemuConsole
*con
,
401 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
403 DisplaySurface
*surface
= qemu_console_surface(con
);
405 pixman_image_composite(PIXMAN_OP_SRC
,
406 surface
->image
, NULL
, surface
->image
,
407 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
410 /***********************************************************/
411 /* basic char display */
413 #define FONT_HEIGHT 16
418 #define QEMU_RGB(r, g, b) \
419 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
421 static const pixman_color_t color_table_rgb
[2][8] = {
423 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
424 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
425 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
426 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
427 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
428 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
429 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
430 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
433 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
434 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
435 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
436 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
437 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
438 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
439 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
440 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
444 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
445 TextAttributes
*t_attrib
)
447 static pixman_image_t
*glyphs
[256];
448 DisplaySurface
*surface
= qemu_console_surface(s
);
449 pixman_color_t fgcol
, bgcol
;
451 if (t_attrib
->invers
) {
452 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
453 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
455 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
456 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
460 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
462 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
463 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
466 static void text_console_resize(QemuConsole
*s
)
468 TextCell
*cells
, *c
, *c1
;
469 int w1
, x
, y
, last_width
;
471 last_width
= s
->width
;
472 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
473 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
479 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
480 for(y
= 0; y
< s
->total_height
; y
++) {
481 c
= &cells
[y
* s
->width
];
483 c1
= &s
->cells
[y
* last_width
];
484 for(x
= 0; x
< w1
; x
++) {
488 for(x
= w1
; x
< s
->width
; x
++) {
490 c
->t_attrib
= s
->t_attrib_default
;
498 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
500 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
501 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
502 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
503 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
506 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
508 if (!qemu_console_is_visible(s
)) {
511 if (s
->update_x0
> x
* FONT_WIDTH
)
512 s
->update_x0
= x
* FONT_WIDTH
;
513 if (s
->update_y0
> y
* FONT_HEIGHT
)
514 s
->update_y0
= y
* FONT_HEIGHT
;
515 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
516 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
517 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
518 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
521 static void update_xy(QemuConsole
*s
, int x
, int y
)
526 if (s
->ds
->have_text
) {
527 text_update_xy(s
, x
, y
);
530 y1
= (s
->y_base
+ y
) % s
->total_height
;
531 y2
= y1
- s
->y_displayed
;
533 y2
+= s
->total_height
;
535 if (y2
< s
->height
) {
536 c
= &s
->cells
[y1
* s
->width
+ x
];
537 vga_putcharxy(s
, x
, y2
, c
->ch
,
539 invalidate_xy(s
, x
, y2
);
543 static void console_show_cursor(QemuConsole
*s
, int show
)
549 if (s
->ds
->have_text
) {
550 s
->cursor_invalidate
= 1;
556 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
557 y
= y1
- s
->y_displayed
;
559 y
+= s
->total_height
;
562 c
= &s
->cells
[y1
* s
->width
+ x
];
563 if (show
&& cursor_visible_phase
) {
564 TextAttributes t_attrib
= s
->t_attrib_default
;
565 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
566 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
568 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
570 invalidate_xy(s
, x
, y
);
574 static void console_refresh(QemuConsole
*s
)
576 DisplaySurface
*surface
= qemu_console_surface(s
);
580 if (s
->ds
->have_text
) {
583 s
->text_x
[1] = s
->width
- 1;
584 s
->text_y
[1] = s
->height
- 1;
585 s
->cursor_invalidate
= 1;
588 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
589 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
591 for (y
= 0; y
< s
->height
; y
++) {
592 c
= s
->cells
+ y1
* s
->width
;
593 for (x
= 0; x
< s
->width
; x
++) {
594 vga_putcharxy(s
, x
, y
, c
->ch
,
598 if (++y1
== s
->total_height
) {
602 console_show_cursor(s
, 1);
603 dpy_gfx_update(s
, 0, 0,
604 surface_width(surface
), surface_height(surface
));
607 static void console_scroll(QemuConsole
*s
, int ydelta
)
612 for(i
= 0; i
< ydelta
; i
++) {
613 if (s
->y_displayed
== s
->y_base
)
615 if (++s
->y_displayed
== s
->total_height
)
620 i
= s
->backscroll_height
;
621 if (i
> s
->total_height
- s
->height
)
622 i
= s
->total_height
- s
->height
;
625 y1
+= s
->total_height
;
626 for(i
= 0; i
< ydelta
; i
++) {
627 if (s
->y_displayed
== y1
)
629 if (--s
->y_displayed
< 0)
630 s
->y_displayed
= s
->total_height
- 1;
636 static void console_put_lf(QemuConsole
*s
)
642 if (s
->y
>= s
->height
) {
643 s
->y
= s
->height
- 1;
645 if (s
->y_displayed
== s
->y_base
) {
646 if (++s
->y_displayed
== s
->total_height
)
649 if (++s
->y_base
== s
->total_height
)
651 if (s
->backscroll_height
< s
->total_height
)
652 s
->backscroll_height
++;
653 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
654 c
= &s
->cells
[y1
* s
->width
];
655 for(x
= 0; x
< s
->width
; x
++) {
657 c
->t_attrib
= s
->t_attrib_default
;
660 if (s
->y_displayed
== s
->y_base
) {
661 if (s
->ds
->have_text
) {
664 s
->text_x
[1] = s
->width
- 1;
665 s
->text_y
[1] = s
->height
- 1;
668 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
669 s
->width
* FONT_WIDTH
,
670 (s
->height
- 1) * FONT_HEIGHT
);
671 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
672 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
673 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
676 s
->update_x1
= s
->width
* FONT_WIDTH
;
677 s
->update_y1
= s
->height
* FONT_HEIGHT
;
682 /* Set console attributes depending on the current escape codes.
683 * NOTE: I know this code is not very efficient (checking every color for it
684 * self) but it is more readable and better maintainable.
686 static void console_handle_escape(QemuConsole
*s
)
690 for (i
=0; i
<s
->nb_esc_params
; i
++) {
691 switch (s
->esc_params
[i
]) {
692 case 0: /* reset all console attributes to default */
693 s
->t_attrib
= s
->t_attrib_default
;
696 s
->t_attrib
.bold
= 1;
699 s
->t_attrib
.uline
= 1;
702 s
->t_attrib
.blink
= 1;
705 s
->t_attrib
.invers
= 1;
708 s
->t_attrib
.unvisible
= 1;
711 s
->t_attrib
.bold
= 0;
714 s
->t_attrib
.uline
= 0;
717 s
->t_attrib
.blink
= 0;
720 s
->t_attrib
.invers
= 0;
723 s
->t_attrib
.unvisible
= 0;
725 /* set foreground color */
727 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
730 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
733 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
736 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
739 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
742 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
745 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
748 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
750 /* set background color */
752 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
755 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
758 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
761 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
764 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
767 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
770 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
773 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
779 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
781 int y1
= (s
->y_base
+ y
) % s
->total_height
;
782 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
784 c
->t_attrib
= s
->t_attrib_default
;
788 static void console_put_one(QemuConsole
*s
, int ch
)
792 if (s
->x
>= s
->width
) {
797 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
798 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
800 c
->t_attrib
= s
->t_attrib
;
801 update_xy(s
, s
->x
, s
->y
);
805 static void console_respond_str(QemuConsole
*s
, const char *buf
)
808 console_put_one(s
, *buf
);
813 /* set cursor, checking bounds */
814 static void set_cursor(QemuConsole
*s
, int x
, int y
)
822 if (y
>= s
->height
) {
833 static void console_putchar(QemuConsole
*s
, int ch
)
842 case '\r': /* carriage return */
845 case '\n': /* newline */
848 case '\b': /* backspace */
852 case '\t': /* tabspace */
853 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
857 s
->x
= s
->x
+ (8 - (s
->x
% 8));
860 case '\a': /* alert aka. bell */
861 /* TODO: has to be implemented */
864 /* SI (shift in), character set 0 (ignored) */
867 /* SO (shift out), character set 1 (ignored) */
869 case 27: /* esc (introducing an escape sequence) */
870 s
->state
= TTY_STATE_ESC
;
873 console_put_one(s
, ch
);
877 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
879 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
880 s
->esc_params
[i
] = 0;
881 s
->nb_esc_params
= 0;
882 s
->state
= TTY_STATE_CSI
;
884 s
->state
= TTY_STATE_NORM
;
887 case TTY_STATE_CSI
: /* handle escape sequence parameters */
888 if (ch
>= '0' && ch
<= '9') {
889 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
890 int *param
= &s
->esc_params
[s
->nb_esc_params
];
891 int digit
= (ch
- '0');
893 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
894 *param
* 10 + digit
: INT_MAX
;
897 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
899 if (ch
== ';' || ch
== '?') {
902 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
903 ch
, s
->nb_esc_params
);
904 s
->state
= TTY_STATE_NORM
;
908 if (s
->esc_params
[0] == 0) {
909 s
->esc_params
[0] = 1;
911 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
914 /* move cursor down */
915 if (s
->esc_params
[0] == 0) {
916 s
->esc_params
[0] = 1;
918 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
921 /* move cursor right */
922 if (s
->esc_params
[0] == 0) {
923 s
->esc_params
[0] = 1;
925 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
928 /* move cursor left */
929 if (s
->esc_params
[0] == 0) {
930 s
->esc_params
[0] = 1;
932 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
935 /* move cursor to column */
936 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
940 /* move cursor to row, column */
941 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
944 switch (s
->esc_params
[0]) {
946 /* clear to end of screen */
947 for (y
= s
->y
; y
< s
->height
; y
++) {
948 for (x
= 0; x
< s
->width
; x
++) {
949 if (y
== s
->y
&& x
< s
->x
) {
952 console_clear_xy(s
, x
, y
);
957 /* clear from beginning of screen */
958 for (y
= 0; y
<= s
->y
; y
++) {
959 for (x
= 0; x
< s
->width
; x
++) {
960 if (y
== s
->y
&& x
> s
->x
) {
963 console_clear_xy(s
, x
, y
);
968 /* clear entire screen */
969 for (y
= 0; y
<= s
->height
; y
++) {
970 for (x
= 0; x
< s
->width
; x
++) {
971 console_clear_xy(s
, x
, y
);
978 switch (s
->esc_params
[0]) {
981 for(x
= s
->x
; x
< s
->width
; x
++) {
982 console_clear_xy(s
, x
, s
->y
);
986 /* clear from beginning of line */
987 for (x
= 0; x
<= s
->x
; x
++) {
988 console_clear_xy(s
, x
, s
->y
);
992 /* clear entire line */
993 for(x
= 0; x
< s
->width
; x
++) {
994 console_clear_xy(s
, x
, s
->y
);
1000 console_handle_escape(s
);
1003 switch (s
->esc_params
[0]) {
1005 /* report console status (always succeed)*/
1006 console_respond_str(s
, "\033[0n");
1009 /* report cursor position */
1010 sprintf(response
, "\033[%d;%dR",
1011 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1013 console_respond_str(s
, response
);
1018 /* save cursor position */
1023 /* restore cursor position */
1028 trace_console_putchar_unhandled(ch
);
1036 void console_select(unsigned int index
)
1038 DisplayChangeListener
*dcl
;
1041 trace_console_select(index
);
1042 s
= qemu_console_lookup_by_index(index
);
1044 DisplayState
*ds
= s
->ds
;
1048 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1049 if (dcl
->con
!= NULL
) {
1052 if (dcl
->ops
->dpy_gfx_switch
) {
1053 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1057 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1058 surface_height(s
->surface
));
1061 if (ds
->have_text
) {
1062 dpy_text_resize(s
, s
->width
, s
->height
);
1064 text_console_update_cursor(NULL
);
1068 typedef struct VCChardev
{
1070 QemuConsole
*console
;
1073 #define TYPE_CHARDEV_VC "chardev-vc"
1074 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1076 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1078 VCChardev
*drv
= VC_CHARDEV(chr
);
1079 QemuConsole
*s
= drv
->console
;
1086 s
->update_x0
= s
->width
* FONT_WIDTH
;
1087 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1090 console_show_cursor(s
, 0);
1091 for(i
= 0; i
< len
; i
++) {
1092 console_putchar(s
, buf
[i
]);
1094 console_show_cursor(s
, 1);
1095 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1096 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1097 s
->update_x1
- s
->update_x0
,
1098 s
->update_y1
- s
->update_y0
);
1103 static void kbd_send_chars(void *opaque
)
1105 QemuConsole
*s
= opaque
;
1109 len
= qemu_chr_be_can_write(s
->chr
);
1110 if (len
> s
->out_fifo
.count
)
1111 len
= s
->out_fifo
.count
;
1113 if (len
> sizeof(buf
))
1115 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1116 qemu_chr_be_write(s
->chr
, buf
, len
);
1118 /* characters are pending: we send them a bit later (XXX:
1119 horrible, should change char device API) */
1120 if (s
->out_fifo
.count
> 0) {
1121 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1125 /* called when an ascii key is pressed */
1126 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1128 uint8_t buf
[16], *q
;
1132 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1136 case QEMU_KEY_CTRL_UP
:
1137 console_scroll(s
, -1);
1139 case QEMU_KEY_CTRL_DOWN
:
1140 console_scroll(s
, 1);
1142 case QEMU_KEY_CTRL_PAGEUP
:
1143 console_scroll(s
, -10);
1145 case QEMU_KEY_CTRL_PAGEDOWN
:
1146 console_scroll(s
, 10);
1149 /* convert the QEMU keysym to VT100 key string */
1151 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1154 c
= keysym
- 0xe100;
1156 *q
++ = '0' + (c
/ 10);
1157 *q
++ = '0' + (c
% 10);
1159 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1162 *q
++ = keysym
& 0xff;
1163 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1164 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1170 vc_chr_write(s
->chr
, buf
, q
- buf
);
1173 if (be
&& be
->chr_read
) {
1174 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1181 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1182 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1183 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1184 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1185 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1186 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1187 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1188 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1189 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1190 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1191 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1194 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1195 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1196 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1197 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1198 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1199 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1200 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1201 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1202 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1205 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1209 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1213 kbd_put_keysym_console(s
, keysym
);
1217 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1221 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1222 kbd_put_keysym_console(s
, str
[i
]);
1226 void kbd_put_keysym(int keysym
)
1228 kbd_put_keysym_console(active_console
, keysym
);
1231 static void text_console_invalidate(void *opaque
)
1233 QemuConsole
*s
= (QemuConsole
*) opaque
;
1235 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1236 text_console_resize(s
);
1241 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1243 QemuConsole
*s
= (QemuConsole
*) opaque
;
1246 if (s
->text_x
[0] <= s
->text_x
[1]) {
1247 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1248 chardata
+= s
->text_y
[0] * s
->width
;
1249 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1250 for (j
= 0; j
< s
->width
; j
++, src
++) {
1251 console_write_ch(chardata
++,
1252 ATTR2CHTYPE(s
->cells
[src
].ch
,
1253 s
->cells
[src
].t_attrib
.fgcol
,
1254 s
->cells
[src
].t_attrib
.bgcol
,
1255 s
->cells
[src
].t_attrib
.bold
));
1257 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1258 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1259 s
->text_x
[0] = s
->width
;
1260 s
->text_y
[0] = s
->height
;
1264 if (s
->cursor_invalidate
) {
1265 dpy_text_cursor(s
, s
->x
, s
->y
);
1266 s
->cursor_invalidate
= 0;
1270 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1277 obj
= object_new(TYPE_QEMU_CONSOLE
);
1278 s
= QEMU_CONSOLE(obj
);
1280 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1281 (Object
**)&s
->device
,
1282 object_property_allow_set_link
,
1283 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1285 object_property_add_uint32_ptr(obj
, "head",
1286 &s
->head
, &error_abort
);
1288 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1289 (console_type
== GRAPHIC_CONSOLE
))) {
1293 s
->console_type
= console_type
;
1295 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1296 if (console_type
!= GRAPHIC_CONSOLE
|| qdev_hotplug
) {
1297 s
->index
= nb_consoles
;
1298 consoles
[nb_consoles
++] = s
;
1301 * HACK: Put graphical consoles before text consoles.
1303 * Only do that for coldplugged devices. After initial device
1304 * initialization we will not renumber the consoles any more.
1306 for (i
= nb_consoles
; i
> 0; i
--) {
1307 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1309 consoles
[i
] = consoles
[i
- 1];
1310 consoles
[i
]->index
= i
;
1319 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1321 qemu_pixman_image_unref(surface
->image
);
1322 surface
->image
= NULL
;
1324 surface
->format
= PIXMAN_x8r8g8b8
;
1325 surface
->image
= pixman_image_create_bits(surface
->format
,
1328 assert(surface
->image
!= NULL
);
1330 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1333 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1335 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1337 trace_displaysurface_create(surface
, width
, height
);
1338 qemu_alloc_display(surface
, width
, height
);
1342 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1343 pixman_format_code_t format
,
1344 int linesize
, uint8_t *data
)
1346 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1348 trace_displaysurface_create_from(surface
, width
, height
, format
);
1349 surface
->format
= format
;
1350 surface
->image
= pixman_image_create_bits(surface
->format
,
1352 (void *)data
, linesize
);
1353 assert(surface
->image
!= NULL
);
1358 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1360 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1362 trace_displaysurface_create_pixman(surface
);
1363 surface
->format
= pixman_image_get_format(image
);
1364 surface
->image
= pixman_image_ref(image
);
1369 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1372 void *data
= pixman_image_get_data(image
);
1373 uint32_t size
= pixman_image_get_stride(image
) *
1374 pixman_image_get_height(image
);
1375 cpu_physical_memory_unmap(data
, size
, 0, 0);
1378 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1379 pixman_format_code_t format
,
1380 int linesize
, uint64_t addr
)
1382 DisplaySurface
*surface
;
1386 if (linesize
== 0) {
1387 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1390 size
= (hwaddr
)linesize
* height
;
1391 data
= cpu_physical_memory_map(addr
, &size
, 0);
1392 if (size
!= (hwaddr
)linesize
* height
) {
1393 cpu_physical_memory_unmap(data
, size
, 0, 0);
1397 surface
= qemu_create_displaysurface_from
1398 (width
, height
, format
, linesize
, data
);
1399 pixman_image_set_destroy_function
1400 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1405 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1408 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1409 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1410 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1411 pixman_image_t
*glyph
;
1415 x
= (w
/ FONT_WIDTH
- len
) / 2;
1416 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1417 for (i
= 0; i
< len
; i
++) {
1418 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1419 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1420 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1421 qemu_pixman_image_unref(glyph
);
1426 void qemu_free_displaysurface(DisplaySurface
*surface
)
1428 if (surface
== NULL
) {
1431 trace_displaysurface_free(surface
);
1432 qemu_pixman_image_unref(surface
->image
);
1436 bool console_has_gl(QemuConsole
*con
)
1438 return con
->gl
!= NULL
;
1441 bool console_has_gl_dmabuf(QemuConsole
*con
)
1443 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1446 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1448 static const char nodev
[] =
1449 "This VM has no graphic display device.";
1450 static DisplaySurface
*dummy
;
1455 if (dcl
->ops
->dpy_gl_ctx_create
) {
1456 /* display has opengl support */
1459 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1460 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1466 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1467 dcl
->ds
= get_alloc_displaystate();
1468 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1469 gui_setup_refresh(dcl
->ds
);
1474 con
= active_console
;
1476 if (dcl
->ops
->dpy_gfx_switch
) {
1478 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1481 dummy
= qemu_create_message_surface(640, 480, nodev
);
1483 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1486 text_console_update_cursor(NULL
);
1489 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1492 DisplayState
*ds
= dcl
->ds
;
1494 dcl
->update_interval
= interval
;
1495 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1496 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1500 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1502 DisplayState
*ds
= dcl
->ds
;
1503 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1507 QLIST_REMOVE(dcl
, next
);
1509 gui_setup_refresh(ds
);
1512 static void dpy_set_ui_info_timer(void *opaque
)
1514 QemuConsole
*con
= opaque
;
1516 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1519 bool dpy_ui_info_supported(QemuConsole
*con
)
1521 return con
->hw_ops
->ui_info
!= NULL
;
1524 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1526 assert(con
!= NULL
);
1528 if (!dpy_ui_info_supported(con
)) {
1531 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1532 /* nothing changed -- ignore */
1537 * Typically we get a flood of these as the user resizes the window.
1538 * Wait until the dust has settled (one second without updates), then
1539 * go notify the guest.
1541 con
->ui_info
= *info
;
1542 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1546 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1548 DisplayState
*s
= con
->ds
;
1549 DisplayChangeListener
*dcl
;
1554 width
= surface_width(con
->surface
);
1555 height
= surface_height(con
->surface
);
1561 w
= MIN(w
, width
- x
);
1562 h
= MIN(h
, height
- y
);
1564 if (!qemu_console_is_visible(con
)) {
1567 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1568 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1571 if (dcl
->ops
->dpy_gfx_update
) {
1572 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
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 for (i
= 0; i
< nb_consoles
; i
++) {
1858 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1859 consoles
[i
]->ds
== NULL
) {
1860 text_console_do_init(consoles
[i
]->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]", i
);
1867 object_property_add_child(container_get(object_get_root(), "/backend"),
1868 name
, OBJECT(consoles
[i
]), &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
)
1950 if (index
>= nb_consoles
) {
1953 return consoles
[index
];
1956 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1962 for (i
= 0; i
< nb_consoles
; i
++) {
1966 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1967 "device", &error_abort
);
1968 if (DEVICE(obj
) != dev
) {
1971 h
= object_property_get_uint(OBJECT(consoles
[i
]),
1972 "head", &error_abort
);
1981 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1982 uint32_t head
, Error
**errp
)
1987 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1989 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1990 "Device '%s' not found", device_id
);
1994 con
= qemu_console_lookup_by_device(dev
, head
);
1996 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2004 QemuConsole
*qemu_console_lookup_unused(void)
2009 for (i
= 0; i
< nb_consoles
; i
++) {
2013 if (consoles
[i
]->hw_ops
!= &unused_ops
) {
2016 obj
= object_property_get_link(OBJECT(consoles
[i
]),
2017 "device", &error_abort
);
2026 bool qemu_console_is_visible(QemuConsole
*con
)
2028 return (con
== active_console
) || (con
->dcls
> 0);
2031 bool qemu_console_is_graphic(QemuConsole
*con
)
2034 con
= active_console
;
2036 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2039 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2042 con
= active_console
;
2044 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2047 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2049 assert(con
!= NULL
);
2050 return con
->gl_block
;
2053 char *qemu_console_get_label(QemuConsole
*con
)
2055 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2057 return g_strdup(object_get_typename(con
->device
));
2059 return g_strdup("VGA");
2061 if (con
->chr
&& con
->chr
->label
) {
2062 return g_strdup(con
->chr
->label
);
2064 return g_strdup_printf("vc%d", con
->index
);
2068 int qemu_console_get_index(QemuConsole
*con
)
2071 con
= active_console
;
2073 return con
? con
->index
: -1;
2076 uint32_t qemu_console_get_head(QemuConsole
*con
)
2079 con
= active_console
;
2081 return con
? con
->head
: -1;
2084 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
2086 assert(con
!= NULL
);
2087 return &con
->ui_info
;
2090 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2093 con
= active_console
;
2095 return con
? surface_width(con
->surface
) : fallback
;
2098 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2101 con
= active_console
;
2103 return con
? surface_height(con
->surface
) : fallback
;
2106 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2108 VCChardev
*drv
= VC_CHARDEV(chr
);
2109 QemuConsole
*s
= drv
->console
;
2114 static void text_console_update_cursor_timer(void)
2116 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2117 + CONSOLE_CURSOR_PERIOD
/ 2);
2120 static void text_console_update_cursor(void *opaque
)
2125 cursor_visible_phase
= !cursor_visible_phase
;
2127 for (i
= 0; i
< nb_consoles
; i
++) {
2129 if (qemu_console_is_graphic(s
) ||
2130 !qemu_console_is_visible(s
)) {
2134 graphic_hw_invalidate(s
);
2138 text_console_update_cursor_timer();
2142 static const GraphicHwOps text_console_ops
= {
2143 .invalidate
= text_console_invalidate
,
2144 .text_update
= text_console_update
,
2147 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2149 VCChardev
*drv
= VC_CHARDEV(chr
);
2150 QemuConsole
*s
= drv
->console
;
2151 int g_width
= 80 * FONT_WIDTH
;
2152 int g_height
= 24 * FONT_HEIGHT
;
2154 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2155 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2156 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2161 s
->total_height
= DEFAULT_BACKSCROLL
;
2165 if (active_console
&& active_console
->surface
) {
2166 g_width
= surface_width(active_console
->surface
);
2167 g_height
= surface_height(active_console
->surface
);
2169 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2172 s
->hw_ops
= &text_console_ops
;
2175 /* Set text attribute defaults */
2176 s
->t_attrib_default
.bold
= 0;
2177 s
->t_attrib_default
.uline
= 0;
2178 s
->t_attrib_default
.blink
= 0;
2179 s
->t_attrib_default
.invers
= 0;
2180 s
->t_attrib_default
.unvisible
= 0;
2181 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2182 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2183 /* set current text attributes to default */
2184 s
->t_attrib
= s
->t_attrib_default
;
2185 text_console_resize(s
);
2191 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2192 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2193 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2194 s
->t_attrib
= s
->t_attrib_default
;
2197 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2200 static void vc_chr_open(Chardev
*chr
,
2201 ChardevBackend
*backend
,
2205 ChardevVC
*vc
= backend
->u
.vc
.data
;
2206 VCChardev
*drv
= VC_CHARDEV(chr
);
2209 unsigned height
= 0;
2211 if (vc
->has_width
) {
2213 } else if (vc
->has_cols
) {
2214 width
= vc
->cols
* FONT_WIDTH
;
2217 if (vc
->has_height
) {
2218 height
= vc
->height
;
2219 } else if (vc
->has_rows
) {
2220 height
= vc
->rows
* FONT_HEIGHT
;
2223 trace_console_txt_new(width
, height
);
2224 if (width
== 0 || height
== 0) {
2225 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2227 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2228 s
->surface
= qemu_create_displaysurface(width
, height
);
2232 error_setg(errp
, "cannot create text console");
2239 if (display_state
) {
2240 text_console_do_init(chr
, display_state
);
2243 /* console/chardev init sometimes completes elsewhere in a 2nd
2244 * stage, so defer OPENED events until they are fully initialized
2249 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2251 DisplaySurface
*surface
;
2253 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2255 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2256 pixman_image_get_width(s
->surface
->image
) == width
&&
2257 pixman_image_get_height(s
->surface
->image
) == height
) {
2261 surface
= qemu_create_displaysurface(width
, height
);
2262 dpy_gfx_replace_surface(s
, surface
);
2265 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2267 return console
->surface
;
2270 PixelFormat
qemu_default_pixelformat(int bpp
)
2272 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2273 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2277 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2279 void qemu_display_register(QemuDisplay
*ui
)
2281 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2282 dpys
[ui
->type
] = ui
;
2285 bool qemu_display_find_default(DisplayOptions
*opts
)
2287 static DisplayType prio
[] = {
2294 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2295 if (dpys
[prio
[i
]] == NULL
) {
2296 ui_module_load_one(DisplayType_lookup
.array
[prio
[i
]]);
2298 if (dpys
[prio
[i
]] == NULL
) {
2301 opts
->type
= prio
[i
];
2307 void qemu_display_early_init(DisplayOptions
*opts
)
2309 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2310 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2313 if (dpys
[opts
->type
] == NULL
) {
2314 ui_module_load_one(DisplayType_lookup
.array
[opts
->type
]);
2316 if (dpys
[opts
->type
] == NULL
) {
2317 error_report("Display '%s' is not available.",
2318 DisplayType_lookup
.array
[opts
->type
]);
2321 if (dpys
[opts
->type
]->early_init
) {
2322 dpys
[opts
->type
]->early_init(opts
);
2326 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2328 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2329 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2332 assert(dpys
[opts
->type
] != NULL
);
2333 dpys
[opts
->type
]->init(ds
, opts
);
2336 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2341 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2342 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2343 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2345 val
= qemu_opt_get_number(opts
, "width", 0);
2347 vc
->has_width
= true;
2351 val
= qemu_opt_get_number(opts
, "height", 0);
2353 vc
->has_height
= true;
2357 val
= qemu_opt_get_number(opts
, "cols", 0);
2359 vc
->has_cols
= true;
2363 val
= qemu_opt_get_number(opts
, "rows", 0);
2365 vc
->has_rows
= true;
2370 static const TypeInfo qemu_console_info
= {
2371 .name
= TYPE_QEMU_CONSOLE
,
2372 .parent
= TYPE_OBJECT
,
2373 .instance_size
= sizeof(QemuConsole
),
2374 .class_size
= sizeof(QemuConsoleClass
),
2377 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2379 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2381 cc
->parse
= qemu_chr_parse_vc
;
2382 cc
->open
= vc_chr_open
;
2383 cc
->chr_write
= vc_chr_write
;
2384 cc
->chr_set_echo
= vc_chr_set_echo
;
2387 static const TypeInfo char_vc_type_info
= {
2388 .name
= TYPE_CHARDEV_VC
,
2389 .parent
= TYPE_CHARDEV
,
2390 .instance_size
= sizeof(VCChardev
),
2391 .class_init
= char_vc_class_init
,
2394 void qemu_console_early_init(void)
2396 /* set the default vc driver */
2397 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2398 type_register(&char_vc_type_info
);
2402 static void register_types(void)
2404 type_register_static(&qemu_console_info
);
2407 type_init(register_types
);