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
;
169 QTAILQ_ENTRY(QemuConsole
) next
;
172 struct DisplayState
{
173 QEMUTimer
*gui_timer
;
174 uint64_t last_update
;
175 uint64_t update_interval
;
180 QLIST_HEAD(, DisplayChangeListener
) listeners
;
183 static DisplayState
*display_state
;
184 static QemuConsole
*active_console
;
185 static QTAILQ_HEAD(consoles_head
, QemuConsole
) consoles
=
186 QTAILQ_HEAD_INITIALIZER(consoles
);
187 static bool cursor_visible_phase
;
188 static QEMUTimer
*cursor_timer
;
190 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
191 static void dpy_refresh(DisplayState
*s
);
192 static DisplayState
*get_alloc_displaystate(void);
193 static void text_console_update_cursor_timer(void);
194 static void text_console_update_cursor(void *opaque
);
196 static void gui_update(void *opaque
)
198 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
199 uint64_t dcl_interval
;
200 DisplayState
*ds
= opaque
;
201 DisplayChangeListener
*dcl
;
204 ds
->refreshing
= true;
206 ds
->refreshing
= false;
208 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
209 dcl_interval
= dcl
->update_interval
?
210 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
211 if (interval
> dcl_interval
) {
212 interval
= dcl_interval
;
215 if (ds
->update_interval
!= interval
) {
216 ds
->update_interval
= interval
;
217 QTAILQ_FOREACH(con
, &consoles
, next
) {
218 if (con
->hw_ops
->update_interval
) {
219 con
->hw_ops
->update_interval(con
->hw
, interval
);
222 trace_console_refresh(interval
);
224 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
225 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
228 static void gui_setup_refresh(DisplayState
*ds
)
230 DisplayChangeListener
*dcl
;
231 bool need_timer
= false;
232 bool have_gfx
= false;
233 bool have_text
= false;
235 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
236 if (dcl
->ops
->dpy_refresh
!= NULL
) {
239 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
242 if (dcl
->ops
->dpy_text_update
!= NULL
) {
247 if (need_timer
&& ds
->gui_timer
== NULL
) {
248 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
249 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
251 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
252 timer_del(ds
->gui_timer
);
253 timer_free(ds
->gui_timer
);
254 ds
->gui_timer
= NULL
;
257 ds
->have_gfx
= have_gfx
;
258 ds
->have_text
= have_text
;
261 void graphic_hw_update(QemuConsole
*con
)
264 con
= active_console
;
266 if (con
&& con
->hw_ops
->gfx_update
) {
267 con
->hw_ops
->gfx_update(con
->hw
);
271 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
275 con
->gl_block
= block
;
276 if (con
->hw_ops
->gl_block
) {
277 con
->hw_ops
->gl_block(con
->hw
, block
);
281 int qemu_console_get_window_id(QemuConsole
*con
)
283 return con
->window_id
;
286 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
288 con
->window_id
= window_id
;
291 void graphic_hw_invalidate(QemuConsole
*con
)
294 con
= active_console
;
296 if (con
&& con
->hw_ops
->invalidate
) {
297 con
->hw_ops
->invalidate(con
->hw
);
301 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
304 int width
= pixman_image_get_width(ds
->image
);
305 int height
= pixman_image_get_height(ds
->image
);
310 pixman_image_t
*linebuf
;
312 trace_ppm_save(filename
, ds
);
313 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
315 error_setg(errp
, "failed to open file '%s': %s", filename
,
319 f
= fdopen(fd
, "wb");
320 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
325 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
326 for (y
= 0; y
< height
; y
++) {
327 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
329 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
330 pixman_image_get_stride(linebuf
), f
);
338 qemu_pixman_image_unref(linebuf
);
343 error_setg(errp
, "failed to write to file '%s': %s", filename
,
349 void qmp_screendump(const char *filename
, bool has_device
, const char *device
,
350 bool has_head
, int64_t head
, Error
**errp
)
353 DisplaySurface
*surface
;
356 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
363 error_setg(errp
, "'head' must be specified together with 'device'");
366 con
= qemu_console_lookup_by_index(0);
368 error_setg(errp
, "There is no console to take a screendump from");
373 graphic_hw_update(con
);
374 surface
= qemu_console_surface(con
);
376 error_setg(errp
, "no surface");
380 ppm_save(filename
, surface
, errp
);
383 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
386 con
= active_console
;
388 if (con
&& con
->hw_ops
->text_update
) {
389 con
->hw_ops
->text_update(con
->hw
, chardata
);
393 static void vga_fill_rect(QemuConsole
*con
,
394 int posx
, int posy
, int width
, int height
,
395 pixman_color_t color
)
397 DisplaySurface
*surface
= qemu_console_surface(con
);
398 pixman_rectangle16_t rect
= {
399 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
402 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
406 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
407 static void vga_bitblt(QemuConsole
*con
,
408 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
410 DisplaySurface
*surface
= qemu_console_surface(con
);
412 pixman_image_composite(PIXMAN_OP_SRC
,
413 surface
->image
, NULL
, surface
->image
,
414 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
417 /***********************************************************/
418 /* basic char display */
420 #define FONT_HEIGHT 16
425 #define QEMU_RGB(r, g, b) \
426 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
428 static const pixman_color_t color_table_rgb
[2][8] = {
430 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
431 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
432 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
433 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
434 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
435 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
436 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
437 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
440 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
441 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
442 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
443 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
444 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
445 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
446 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
447 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
451 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
452 TextAttributes
*t_attrib
)
454 static pixman_image_t
*glyphs
[256];
455 DisplaySurface
*surface
= qemu_console_surface(s
);
456 pixman_color_t fgcol
, bgcol
;
458 if (t_attrib
->invers
) {
459 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
460 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
462 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
463 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
467 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
469 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
470 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
473 static void text_console_resize(QemuConsole
*s
)
475 TextCell
*cells
, *c
, *c1
;
476 int w1
, x
, y
, last_width
;
478 last_width
= s
->width
;
479 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
480 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
486 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
487 for(y
= 0; y
< s
->total_height
; y
++) {
488 c
= &cells
[y
* s
->width
];
490 c1
= &s
->cells
[y
* last_width
];
491 for(x
= 0; x
< w1
; x
++) {
495 for(x
= w1
; x
< s
->width
; x
++) {
497 c
->t_attrib
= s
->t_attrib_default
;
505 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
507 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
508 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
509 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
510 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
513 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
515 if (!qemu_console_is_visible(s
)) {
518 if (s
->update_x0
> x
* FONT_WIDTH
)
519 s
->update_x0
= x
* FONT_WIDTH
;
520 if (s
->update_y0
> y
* FONT_HEIGHT
)
521 s
->update_y0
= y
* FONT_HEIGHT
;
522 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
523 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
524 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
525 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
528 static void update_xy(QemuConsole
*s
, int x
, int y
)
533 if (s
->ds
->have_text
) {
534 text_update_xy(s
, x
, y
);
537 y1
= (s
->y_base
+ y
) % s
->total_height
;
538 y2
= y1
- s
->y_displayed
;
540 y2
+= s
->total_height
;
542 if (y2
< s
->height
) {
543 c
= &s
->cells
[y1
* s
->width
+ x
];
544 vga_putcharxy(s
, x
, y2
, c
->ch
,
546 invalidate_xy(s
, x
, y2
);
550 static void console_show_cursor(QemuConsole
*s
, int show
)
556 if (s
->ds
->have_text
) {
557 s
->cursor_invalidate
= 1;
563 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
564 y
= y1
- s
->y_displayed
;
566 y
+= s
->total_height
;
569 c
= &s
->cells
[y1
* s
->width
+ x
];
570 if (show
&& cursor_visible_phase
) {
571 TextAttributes t_attrib
= s
->t_attrib_default
;
572 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
573 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
575 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
577 invalidate_xy(s
, x
, y
);
581 static void console_refresh(QemuConsole
*s
)
583 DisplaySurface
*surface
= qemu_console_surface(s
);
587 if (s
->ds
->have_text
) {
590 s
->text_x
[1] = s
->width
- 1;
591 s
->text_y
[1] = s
->height
- 1;
592 s
->cursor_invalidate
= 1;
595 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
596 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
598 for (y
= 0; y
< s
->height
; y
++) {
599 c
= s
->cells
+ y1
* s
->width
;
600 for (x
= 0; x
< s
->width
; x
++) {
601 vga_putcharxy(s
, x
, y
, c
->ch
,
605 if (++y1
== s
->total_height
) {
609 console_show_cursor(s
, 1);
610 dpy_gfx_update(s
, 0, 0,
611 surface_width(surface
), surface_height(surface
));
614 static void console_scroll(QemuConsole
*s
, int ydelta
)
619 for(i
= 0; i
< ydelta
; i
++) {
620 if (s
->y_displayed
== s
->y_base
)
622 if (++s
->y_displayed
== s
->total_height
)
627 i
= s
->backscroll_height
;
628 if (i
> s
->total_height
- s
->height
)
629 i
= s
->total_height
- s
->height
;
632 y1
+= s
->total_height
;
633 for(i
= 0; i
< ydelta
; i
++) {
634 if (s
->y_displayed
== y1
)
636 if (--s
->y_displayed
< 0)
637 s
->y_displayed
= s
->total_height
- 1;
643 static void console_put_lf(QemuConsole
*s
)
649 if (s
->y
>= s
->height
) {
650 s
->y
= s
->height
- 1;
652 if (s
->y_displayed
== s
->y_base
) {
653 if (++s
->y_displayed
== s
->total_height
)
656 if (++s
->y_base
== s
->total_height
)
658 if (s
->backscroll_height
< s
->total_height
)
659 s
->backscroll_height
++;
660 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
661 c
= &s
->cells
[y1
* s
->width
];
662 for(x
= 0; x
< s
->width
; x
++) {
664 c
->t_attrib
= s
->t_attrib_default
;
667 if (s
->y_displayed
== s
->y_base
) {
668 if (s
->ds
->have_text
) {
671 s
->text_x
[1] = s
->width
- 1;
672 s
->text_y
[1] = s
->height
- 1;
675 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
676 s
->width
* FONT_WIDTH
,
677 (s
->height
- 1) * FONT_HEIGHT
);
678 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
679 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
680 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
683 s
->update_x1
= s
->width
* FONT_WIDTH
;
684 s
->update_y1
= s
->height
* FONT_HEIGHT
;
689 /* Set console attributes depending on the current escape codes.
690 * NOTE: I know this code is not very efficient (checking every color for it
691 * self) but it is more readable and better maintainable.
693 static void console_handle_escape(QemuConsole
*s
)
697 for (i
=0; i
<s
->nb_esc_params
; i
++) {
698 switch (s
->esc_params
[i
]) {
699 case 0: /* reset all console attributes to default */
700 s
->t_attrib
= s
->t_attrib_default
;
703 s
->t_attrib
.bold
= 1;
706 s
->t_attrib
.uline
= 1;
709 s
->t_attrib
.blink
= 1;
712 s
->t_attrib
.invers
= 1;
715 s
->t_attrib
.unvisible
= 1;
718 s
->t_attrib
.bold
= 0;
721 s
->t_attrib
.uline
= 0;
724 s
->t_attrib
.blink
= 0;
727 s
->t_attrib
.invers
= 0;
730 s
->t_attrib
.unvisible
= 0;
732 /* set foreground color */
734 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
737 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
740 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
743 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
746 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
749 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
752 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
755 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
757 /* set background color */
759 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
762 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
765 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
768 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
771 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
774 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
777 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
780 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
786 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
788 int y1
= (s
->y_base
+ y
) % s
->total_height
;
789 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
791 c
->t_attrib
= s
->t_attrib_default
;
795 static void console_put_one(QemuConsole
*s
, int ch
)
799 if (s
->x
>= s
->width
) {
804 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
805 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
807 c
->t_attrib
= s
->t_attrib
;
808 update_xy(s
, s
->x
, s
->y
);
812 static void console_respond_str(QemuConsole
*s
, const char *buf
)
815 console_put_one(s
, *buf
);
820 /* set cursor, checking bounds */
821 static void set_cursor(QemuConsole
*s
, int x
, int y
)
829 if (y
>= s
->height
) {
840 static void console_putchar(QemuConsole
*s
, int ch
)
849 case '\r': /* carriage return */
852 case '\n': /* newline */
855 case '\b': /* backspace */
859 case '\t': /* tabspace */
860 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
864 s
->x
= s
->x
+ (8 - (s
->x
% 8));
867 case '\a': /* alert aka. bell */
868 /* TODO: has to be implemented */
871 /* SI (shift in), character set 0 (ignored) */
874 /* SO (shift out), character set 1 (ignored) */
876 case 27: /* esc (introducing an escape sequence) */
877 s
->state
= TTY_STATE_ESC
;
880 console_put_one(s
, ch
);
884 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
886 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
887 s
->esc_params
[i
] = 0;
888 s
->nb_esc_params
= 0;
889 s
->state
= TTY_STATE_CSI
;
891 s
->state
= TTY_STATE_NORM
;
894 case TTY_STATE_CSI
: /* handle escape sequence parameters */
895 if (ch
>= '0' && ch
<= '9') {
896 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
897 int *param
= &s
->esc_params
[s
->nb_esc_params
];
898 int digit
= (ch
- '0');
900 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
901 *param
* 10 + digit
: INT_MAX
;
904 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
906 if (ch
== ';' || ch
== '?') {
909 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
910 ch
, s
->nb_esc_params
);
911 s
->state
= TTY_STATE_NORM
;
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 down */
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 right */
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 left */
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 to column */
943 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
947 /* move cursor to row, column */
948 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
951 switch (s
->esc_params
[0]) {
953 /* clear to end of screen */
954 for (y
= s
->y
; y
< s
->height
; y
++) {
955 for (x
= 0; x
< s
->width
; x
++) {
956 if (y
== s
->y
&& x
< s
->x
) {
959 console_clear_xy(s
, x
, y
);
964 /* clear from beginning of screen */
965 for (y
= 0; y
<= s
->y
; y
++) {
966 for (x
= 0; x
< s
->width
; x
++) {
967 if (y
== s
->y
&& x
> s
->x
) {
970 console_clear_xy(s
, x
, y
);
975 /* clear entire screen */
976 for (y
= 0; y
<= s
->height
; y
++) {
977 for (x
= 0; x
< s
->width
; x
++) {
978 console_clear_xy(s
, x
, y
);
985 switch (s
->esc_params
[0]) {
988 for(x
= s
->x
; x
< s
->width
; x
++) {
989 console_clear_xy(s
, x
, s
->y
);
993 /* clear from beginning of line */
994 for (x
= 0; x
<= s
->x
; x
++) {
995 console_clear_xy(s
, x
, s
->y
);
999 /* clear entire line */
1000 for(x
= 0; x
< s
->width
; x
++) {
1001 console_clear_xy(s
, x
, s
->y
);
1007 console_handle_escape(s
);
1010 switch (s
->esc_params
[0]) {
1012 /* report console status (always succeed)*/
1013 console_respond_str(s
, "\033[0n");
1016 /* report cursor position */
1017 sprintf(response
, "\033[%d;%dR",
1018 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1020 console_respond_str(s
, response
);
1025 /* save cursor position */
1030 /* restore cursor position */
1035 trace_console_putchar_unhandled(ch
);
1043 void console_select(unsigned int index
)
1045 DisplayChangeListener
*dcl
;
1048 trace_console_select(index
);
1049 s
= qemu_console_lookup_by_index(index
);
1051 DisplayState
*ds
= s
->ds
;
1055 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1056 if (dcl
->con
!= NULL
) {
1059 if (dcl
->ops
->dpy_gfx_switch
) {
1060 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1064 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1065 surface_height(s
->surface
));
1068 if (ds
->have_text
) {
1069 dpy_text_resize(s
, s
->width
, s
->height
);
1071 text_console_update_cursor(NULL
);
1075 typedef struct VCChardev
{
1077 QemuConsole
*console
;
1080 #define TYPE_CHARDEV_VC "chardev-vc"
1081 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1083 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1085 VCChardev
*drv
= VC_CHARDEV(chr
);
1086 QemuConsole
*s
= drv
->console
;
1093 s
->update_x0
= s
->width
* FONT_WIDTH
;
1094 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1097 console_show_cursor(s
, 0);
1098 for(i
= 0; i
< len
; i
++) {
1099 console_putchar(s
, buf
[i
]);
1101 console_show_cursor(s
, 1);
1102 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1103 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1104 s
->update_x1
- s
->update_x0
,
1105 s
->update_y1
- s
->update_y0
);
1110 static void kbd_send_chars(void *opaque
)
1112 QemuConsole
*s
= opaque
;
1116 len
= qemu_chr_be_can_write(s
->chr
);
1117 if (len
> s
->out_fifo
.count
)
1118 len
= s
->out_fifo
.count
;
1120 if (len
> sizeof(buf
))
1122 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1123 qemu_chr_be_write(s
->chr
, buf
, len
);
1125 /* characters are pending: we send them a bit later (XXX:
1126 horrible, should change char device API) */
1127 if (s
->out_fifo
.count
> 0) {
1128 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1132 /* called when an ascii key is pressed */
1133 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1135 uint8_t buf
[16], *q
;
1139 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1143 case QEMU_KEY_CTRL_UP
:
1144 console_scroll(s
, -1);
1146 case QEMU_KEY_CTRL_DOWN
:
1147 console_scroll(s
, 1);
1149 case QEMU_KEY_CTRL_PAGEUP
:
1150 console_scroll(s
, -10);
1152 case QEMU_KEY_CTRL_PAGEDOWN
:
1153 console_scroll(s
, 10);
1156 /* convert the QEMU keysym to VT100 key string */
1158 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1161 c
= keysym
- 0xe100;
1163 *q
++ = '0' + (c
/ 10);
1164 *q
++ = '0' + (c
% 10);
1166 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1169 *q
++ = keysym
& 0xff;
1170 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1171 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1177 vc_chr_write(s
->chr
, buf
, q
- buf
);
1180 if (be
&& be
->chr_read
) {
1181 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1188 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1189 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1190 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1191 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1192 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1193 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1194 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1195 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1196 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1197 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1198 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1201 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1202 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1203 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1204 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1205 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1206 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1207 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1208 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1209 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1212 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1216 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1220 kbd_put_keysym_console(s
, keysym
);
1224 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1228 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1229 kbd_put_keysym_console(s
, str
[i
]);
1233 void kbd_put_keysym(int keysym
)
1235 kbd_put_keysym_console(active_console
, keysym
);
1238 static void text_console_invalidate(void *opaque
)
1240 QemuConsole
*s
= (QemuConsole
*) opaque
;
1242 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1243 text_console_resize(s
);
1248 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1250 QemuConsole
*s
= (QemuConsole
*) opaque
;
1253 if (s
->text_x
[0] <= s
->text_x
[1]) {
1254 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1255 chardata
+= s
->text_y
[0] * s
->width
;
1256 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1257 for (j
= 0; j
< s
->width
; j
++, src
++) {
1258 console_write_ch(chardata
++,
1259 ATTR2CHTYPE(s
->cells
[src
].ch
,
1260 s
->cells
[src
].t_attrib
.fgcol
,
1261 s
->cells
[src
].t_attrib
.bgcol
,
1262 s
->cells
[src
].t_attrib
.bold
));
1264 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1265 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1266 s
->text_x
[0] = s
->width
;
1267 s
->text_y
[0] = s
->height
;
1271 if (s
->cursor_invalidate
) {
1272 dpy_text_cursor(s
, s
->x
, s
->y
);
1273 s
->cursor_invalidate
= 0;
1277 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1284 obj
= object_new(TYPE_QEMU_CONSOLE
);
1285 s
= QEMU_CONSOLE(obj
);
1287 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1288 (Object
**)&s
->device
,
1289 object_property_allow_set_link
,
1290 OBJ_PROP_LINK_STRONG
,
1292 object_property_add_uint32_ptr(obj
, "head",
1293 &s
->head
, &error_abort
);
1295 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1296 (console_type
== GRAPHIC_CONSOLE
))) {
1300 s
->console_type
= console_type
;
1302 if (QTAILQ_EMPTY(&consoles
)) {
1304 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1305 } else if (console_type
!= GRAPHIC_CONSOLE
|| qdev_hotplug
) {
1306 QemuConsole
*last
= QTAILQ_LAST(&consoles
, consoles_head
);
1307 s
->index
= last
->index
+ 1;
1308 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1311 * HACK: Put graphical consoles before text consoles.
1313 * Only do that for coldplugged devices. After initial device
1314 * initialization we will not renumber the consoles any more.
1316 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1318 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1319 c
->console_type
== GRAPHIC_CONSOLE
) {
1320 c
= QTAILQ_NEXT(c
, next
);
1322 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1323 /* have no text consoles */
1324 s
->index
= c
->index
+ 1;
1325 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1327 s
->index
= c
->index
;
1328 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1329 /* renumber text consoles */
1330 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1338 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1340 qemu_pixman_image_unref(surface
->image
);
1341 surface
->image
= NULL
;
1343 surface
->format
= PIXMAN_x8r8g8b8
;
1344 surface
->image
= pixman_image_create_bits(surface
->format
,
1347 assert(surface
->image
!= NULL
);
1349 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1352 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1354 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1356 trace_displaysurface_create(surface
, width
, height
);
1357 qemu_alloc_display(surface
, width
, height
);
1361 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1362 pixman_format_code_t format
,
1363 int linesize
, uint8_t *data
)
1365 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1367 trace_displaysurface_create_from(surface
, width
, height
, format
);
1368 surface
->format
= format
;
1369 surface
->image
= pixman_image_create_bits(surface
->format
,
1371 (void *)data
, linesize
);
1372 assert(surface
->image
!= NULL
);
1377 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1379 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1381 trace_displaysurface_create_pixman(surface
);
1382 surface
->format
= pixman_image_get_format(image
);
1383 surface
->image
= pixman_image_ref(image
);
1388 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1391 void *data
= pixman_image_get_data(image
);
1392 uint32_t size
= pixman_image_get_stride(image
) *
1393 pixman_image_get_height(image
);
1394 cpu_physical_memory_unmap(data
, size
, 0, 0);
1397 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1398 pixman_format_code_t format
,
1399 int linesize
, uint64_t addr
)
1401 DisplaySurface
*surface
;
1405 if (linesize
== 0) {
1406 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1409 size
= (hwaddr
)linesize
* height
;
1410 data
= cpu_physical_memory_map(addr
, &size
, 0);
1411 if (size
!= (hwaddr
)linesize
* height
) {
1412 cpu_physical_memory_unmap(data
, size
, 0, 0);
1416 surface
= qemu_create_displaysurface_from
1417 (width
, height
, format
, linesize
, data
);
1418 pixman_image_set_destroy_function
1419 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1424 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1427 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1428 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1429 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1430 pixman_image_t
*glyph
;
1434 x
= (w
/ FONT_WIDTH
- len
) / 2;
1435 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1436 for (i
= 0; i
< len
; i
++) {
1437 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1438 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1439 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1440 qemu_pixman_image_unref(glyph
);
1445 void qemu_free_displaysurface(DisplaySurface
*surface
)
1447 if (surface
== NULL
) {
1450 trace_displaysurface_free(surface
);
1451 qemu_pixman_image_unref(surface
->image
);
1455 bool console_has_gl(QemuConsole
*con
)
1457 return con
->gl
!= NULL
;
1460 bool console_has_gl_dmabuf(QemuConsole
*con
)
1462 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1465 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1467 static const char nodev
[] =
1468 "This VM has no graphic display device.";
1469 static DisplaySurface
*dummy
;
1474 if (dcl
->ops
->dpy_gl_ctx_create
) {
1475 /* display has opengl support */
1478 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1479 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1485 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1486 dcl
->ds
= get_alloc_displaystate();
1487 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1488 gui_setup_refresh(dcl
->ds
);
1493 con
= active_console
;
1495 if (dcl
->ops
->dpy_gfx_switch
) {
1497 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1500 dummy
= qemu_create_message_surface(640, 480, nodev
);
1502 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1505 text_console_update_cursor(NULL
);
1508 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1511 DisplayState
*ds
= dcl
->ds
;
1513 dcl
->update_interval
= interval
;
1514 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1515 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1519 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1521 DisplayState
*ds
= dcl
->ds
;
1522 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1526 QLIST_REMOVE(dcl
, next
);
1528 gui_setup_refresh(ds
);
1531 static void dpy_set_ui_info_timer(void *opaque
)
1533 QemuConsole
*con
= opaque
;
1535 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1538 bool dpy_ui_info_supported(QemuConsole
*con
)
1540 return con
->hw_ops
->ui_info
!= NULL
;
1543 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1545 assert(con
!= NULL
);
1547 if (!dpy_ui_info_supported(con
)) {
1550 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1551 /* nothing changed -- ignore */
1556 * Typically we get a flood of these as the user resizes the window.
1557 * Wait until the dust has settled (one second without updates), then
1558 * go notify the guest.
1560 con
->ui_info
= *info
;
1561 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1565 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1567 DisplayState
*s
= con
->ds
;
1568 DisplayChangeListener
*dcl
;
1573 width
= surface_width(con
->surface
);
1574 height
= surface_height(con
->surface
);
1580 w
= MIN(w
, width
- x
);
1581 h
= MIN(h
, height
- y
);
1583 if (!qemu_console_is_visible(con
)) {
1586 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1587 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1590 if (dcl
->ops
->dpy_gfx_update
) {
1591 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1596 void dpy_gfx_update_full(QemuConsole
*con
)
1598 if (!con
->surface
) {
1601 dpy_gfx_update(con
, 0, 0,
1602 surface_width(con
->surface
),
1603 surface_height(con
->surface
));
1606 void dpy_gfx_replace_surface(QemuConsole
*con
,
1607 DisplaySurface
*surface
)
1609 DisplayState
*s
= con
->ds
;
1610 DisplaySurface
*old_surface
= con
->surface
;
1611 DisplayChangeListener
*dcl
;
1613 assert(old_surface
!= surface
|| surface
== NULL
);
1615 con
->surface
= surface
;
1616 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1617 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1620 if (dcl
->ops
->dpy_gfx_switch
) {
1621 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1624 qemu_free_displaysurface(old_surface
);
1627 bool dpy_gfx_check_format(QemuConsole
*con
,
1628 pixman_format_code_t format
)
1630 DisplayChangeListener
*dcl
;
1631 DisplayState
*s
= con
->ds
;
1633 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1634 if (dcl
->con
&& dcl
->con
!= con
) {
1635 /* dcl bound to another console -> skip */
1638 if (dcl
->ops
->dpy_gfx_check_format
) {
1639 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1643 /* default is to whitelist native 32 bpp only */
1644 if (format
!= qemu_default_pixman_format(32, true)) {
1652 static void dpy_refresh(DisplayState
*s
)
1654 DisplayChangeListener
*dcl
;
1656 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1657 if (dcl
->ops
->dpy_refresh
) {
1658 dcl
->ops
->dpy_refresh(dcl
);
1663 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1665 DisplayState
*s
= con
->ds
;
1666 DisplayChangeListener
*dcl
;
1668 if (!qemu_console_is_visible(con
)) {
1671 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1672 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1675 if (dcl
->ops
->dpy_text_cursor
) {
1676 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1681 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1683 DisplayState
*s
= con
->ds
;
1684 DisplayChangeListener
*dcl
;
1686 if (!qemu_console_is_visible(con
)) {
1689 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1690 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1693 if (dcl
->ops
->dpy_text_update
) {
1694 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1699 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1701 DisplayState
*s
= con
->ds
;
1702 DisplayChangeListener
*dcl
;
1704 if (!qemu_console_is_visible(con
)) {
1707 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1708 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1711 if (dcl
->ops
->dpy_text_resize
) {
1712 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1717 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1719 DisplayState
*s
= con
->ds
;
1720 DisplayChangeListener
*dcl
;
1722 if (!qemu_console_is_visible(con
)) {
1725 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1726 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1729 if (dcl
->ops
->dpy_mouse_set
) {
1730 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1735 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1737 DisplayState
*s
= con
->ds
;
1738 DisplayChangeListener
*dcl
;
1740 if (!qemu_console_is_visible(con
)) {
1743 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1744 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1747 if (dcl
->ops
->dpy_cursor_define
) {
1748 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1753 bool dpy_cursor_define_supported(QemuConsole
*con
)
1755 DisplayState
*s
= con
->ds
;
1756 DisplayChangeListener
*dcl
;
1758 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1759 if (dcl
->ops
->dpy_cursor_define
) {
1766 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1767 struct QEMUGLParams
*qparams
)
1770 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1773 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1776 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1779 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1782 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1785 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1788 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1791 void dpy_gl_scanout_disable(QemuConsole
*con
)
1794 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1795 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1797 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1802 void dpy_gl_scanout_texture(QemuConsole
*con
,
1803 uint32_t backing_id
,
1804 bool backing_y_0_top
,
1805 uint32_t backing_width
,
1806 uint32_t backing_height
,
1807 uint32_t x
, uint32_t y
,
1808 uint32_t width
, uint32_t height
)
1811 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1813 backing_width
, backing_height
,
1814 x
, y
, width
, height
);
1817 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1821 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1824 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1825 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1829 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1830 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1831 have_hot
, hot_x
, hot_y
);
1835 void dpy_gl_cursor_position(QemuConsole
*con
,
1836 uint32_t pos_x
, uint32_t pos_y
)
1840 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1841 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1845 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1850 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1851 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1855 void dpy_gl_update(QemuConsole
*con
,
1856 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1859 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1862 /***********************************************************/
1863 /* register display */
1865 /* console.c internal use only */
1866 static DisplayState
*get_alloc_displaystate(void)
1868 if (!display_state
) {
1869 display_state
= g_new0(DisplayState
, 1);
1870 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1871 text_console_update_cursor
, NULL
);
1873 return display_state
;
1877 * Called by main(), after creating QemuConsoles
1878 * and before initializing ui (sdl/vnc/...).
1880 DisplayState
*init_displaystate(void)
1885 get_alloc_displaystate();
1886 QTAILQ_FOREACH(con
, &consoles
, next
) {
1887 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1889 text_console_do_init(con
->chr
, display_state
);
1892 /* Hook up into the qom tree here (not in new_console()), once
1893 * all QemuConsoles are created and the order / numbering
1894 * doesn't change any more */
1895 name
= g_strdup_printf("console[%d]", con
->index
);
1896 object_property_add_child(container_get(object_get_root(), "/backend"),
1897 name
, OBJECT(con
), &error_abort
);
1901 return display_state
;
1904 void graphic_console_set_hwops(QemuConsole
*con
,
1905 const GraphicHwOps
*hw_ops
,
1908 con
->hw_ops
= hw_ops
;
1912 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1913 const GraphicHwOps
*hw_ops
,
1916 static const char noinit
[] =
1917 "Guest has not initialized the display (yet).";
1922 DisplaySurface
*surface
;
1924 ds
= get_alloc_displaystate();
1925 s
= qemu_console_lookup_unused();
1927 trace_console_gfx_reuse(s
->index
);
1929 width
= surface_width(s
->surface
);
1930 height
= surface_height(s
->surface
);
1933 trace_console_gfx_new();
1934 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1935 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1936 dpy_set_ui_info_timer
, s
);
1938 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1940 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1944 surface
= qemu_create_message_surface(width
, height
, noinit
);
1945 dpy_gfx_replace_surface(s
, surface
);
1949 static const GraphicHwOps unused_ops
= {
1953 void graphic_console_close(QemuConsole
*con
)
1955 static const char unplugged
[] =
1956 "Guest display has been unplugged";
1957 DisplaySurface
*surface
;
1962 width
= surface_width(con
->surface
);
1963 height
= surface_height(con
->surface
);
1966 trace_console_gfx_close(con
->index
);
1967 object_property_set_link(OBJECT(con
), NULL
, "device", &error_abort
);
1968 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1971 dpy_gl_scanout_disable(con
);
1973 surface
= qemu_create_message_surface(width
, height
, unplugged
);
1974 dpy_gfx_replace_surface(con
, surface
);
1977 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1981 QTAILQ_FOREACH(con
, &consoles
, next
) {
1982 if (con
->index
== index
) {
1989 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1995 QTAILQ_FOREACH(con
, &consoles
, next
) {
1996 obj
= object_property_get_link(OBJECT(con
),
1997 "device", &error_abort
);
1998 if (DEVICE(obj
) != dev
) {
2001 h
= object_property_get_uint(OBJECT(con
),
2002 "head", &error_abort
);
2011 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
2012 uint32_t head
, Error
**errp
)
2017 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
2019 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2020 "Device '%s' not found", device_id
);
2024 con
= qemu_console_lookup_by_device(dev
, head
);
2026 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2034 QemuConsole
*qemu_console_lookup_unused(void)
2039 QTAILQ_FOREACH(con
, &consoles
, next
) {
2040 if (con
->hw_ops
!= &unused_ops
) {
2043 obj
= object_property_get_link(OBJECT(con
),
2044 "device", &error_abort
);
2053 bool qemu_console_is_visible(QemuConsole
*con
)
2055 return (con
== active_console
) || (con
->dcls
> 0);
2058 bool qemu_console_is_graphic(QemuConsole
*con
)
2061 con
= active_console
;
2063 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2066 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2069 con
= active_console
;
2071 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2074 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2076 assert(con
!= NULL
);
2077 return con
->gl_block
;
2080 char *qemu_console_get_label(QemuConsole
*con
)
2082 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2084 return g_strdup(object_get_typename(con
->device
));
2086 return g_strdup("VGA");
2088 if (con
->chr
&& con
->chr
->label
) {
2089 return g_strdup(con
->chr
->label
);
2091 return g_strdup_printf("vc%d", con
->index
);
2095 int qemu_console_get_index(QemuConsole
*con
)
2098 con
= active_console
;
2100 return con
? con
->index
: -1;
2103 uint32_t qemu_console_get_head(QemuConsole
*con
)
2106 con
= active_console
;
2108 return con
? con
->head
: -1;
2111 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
2113 assert(con
!= NULL
);
2114 return &con
->ui_info
;
2117 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2120 con
= active_console
;
2122 return con
? surface_width(con
->surface
) : fallback
;
2125 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2128 con
= active_console
;
2130 return con
? surface_height(con
->surface
) : fallback
;
2133 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2135 VCChardev
*drv
= VC_CHARDEV(chr
);
2136 QemuConsole
*s
= drv
->console
;
2141 static void text_console_update_cursor_timer(void)
2143 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2144 + CONSOLE_CURSOR_PERIOD
/ 2);
2147 static void text_console_update_cursor(void *opaque
)
2152 cursor_visible_phase
= !cursor_visible_phase
;
2154 QTAILQ_FOREACH(s
, &consoles
, next
) {
2155 if (qemu_console_is_graphic(s
) ||
2156 !qemu_console_is_visible(s
)) {
2160 graphic_hw_invalidate(s
);
2164 text_console_update_cursor_timer();
2168 static const GraphicHwOps text_console_ops
= {
2169 .invalidate
= text_console_invalidate
,
2170 .text_update
= text_console_update
,
2173 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2175 VCChardev
*drv
= VC_CHARDEV(chr
);
2176 QemuConsole
*s
= drv
->console
;
2177 int g_width
= 80 * FONT_WIDTH
;
2178 int g_height
= 24 * FONT_HEIGHT
;
2180 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2181 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2182 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2187 s
->total_height
= DEFAULT_BACKSCROLL
;
2191 if (active_console
&& active_console
->surface
) {
2192 g_width
= surface_width(active_console
->surface
);
2193 g_height
= surface_height(active_console
->surface
);
2195 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2198 s
->hw_ops
= &text_console_ops
;
2201 /* Set text attribute defaults */
2202 s
->t_attrib_default
.bold
= 0;
2203 s
->t_attrib_default
.uline
= 0;
2204 s
->t_attrib_default
.blink
= 0;
2205 s
->t_attrib_default
.invers
= 0;
2206 s
->t_attrib_default
.unvisible
= 0;
2207 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2208 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2209 /* set current text attributes to default */
2210 s
->t_attrib
= s
->t_attrib_default
;
2211 text_console_resize(s
);
2217 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2218 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2219 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2220 s
->t_attrib
= s
->t_attrib_default
;
2223 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2226 static void vc_chr_open(Chardev
*chr
,
2227 ChardevBackend
*backend
,
2231 ChardevVC
*vc
= backend
->u
.vc
.data
;
2232 VCChardev
*drv
= VC_CHARDEV(chr
);
2235 unsigned height
= 0;
2237 if (vc
->has_width
) {
2239 } else if (vc
->has_cols
) {
2240 width
= vc
->cols
* FONT_WIDTH
;
2243 if (vc
->has_height
) {
2244 height
= vc
->height
;
2245 } else if (vc
->has_rows
) {
2246 height
= vc
->rows
* FONT_HEIGHT
;
2249 trace_console_txt_new(width
, height
);
2250 if (width
== 0 || height
== 0) {
2251 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2253 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2254 s
->surface
= qemu_create_displaysurface(width
, height
);
2258 error_setg(errp
, "cannot create text console");
2265 if (display_state
) {
2266 text_console_do_init(chr
, display_state
);
2269 /* console/chardev init sometimes completes elsewhere in a 2nd
2270 * stage, so defer OPENED events until they are fully initialized
2275 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2277 DisplaySurface
*surface
;
2279 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2281 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2282 pixman_image_get_width(s
->surface
->image
) == width
&&
2283 pixman_image_get_height(s
->surface
->image
) == height
) {
2287 surface
= qemu_create_displaysurface(width
, height
);
2288 dpy_gfx_replace_surface(s
, surface
);
2291 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2293 return console
->surface
;
2296 PixelFormat
qemu_default_pixelformat(int bpp
)
2298 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2299 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2303 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2305 void qemu_display_register(QemuDisplay
*ui
)
2307 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2308 dpys
[ui
->type
] = ui
;
2311 bool qemu_display_find_default(DisplayOptions
*opts
)
2313 static DisplayType prio
[] = {
2320 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2321 if (dpys
[prio
[i
]] == NULL
) {
2322 ui_module_load_one(DisplayType_str(prio
[i
]));
2324 if (dpys
[prio
[i
]] == NULL
) {
2327 opts
->type
= prio
[i
];
2333 void qemu_display_early_init(DisplayOptions
*opts
)
2335 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2336 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2339 if (dpys
[opts
->type
] == NULL
) {
2340 ui_module_load_one(DisplayType_str(opts
->type
));
2342 if (dpys
[opts
->type
] == NULL
) {
2343 error_report("Display '%s' is not available.",
2344 DisplayType_str(opts
->type
));
2347 if (dpys
[opts
->type
]->early_init
) {
2348 dpys
[opts
->type
]->early_init(opts
);
2352 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2354 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2355 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2358 assert(dpys
[opts
->type
] != NULL
);
2359 dpys
[opts
->type
]->init(ds
, opts
);
2362 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2367 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2368 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2369 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2371 val
= qemu_opt_get_number(opts
, "width", 0);
2373 vc
->has_width
= true;
2377 val
= qemu_opt_get_number(opts
, "height", 0);
2379 vc
->has_height
= true;
2383 val
= qemu_opt_get_number(opts
, "cols", 0);
2385 vc
->has_cols
= true;
2389 val
= qemu_opt_get_number(opts
, "rows", 0);
2391 vc
->has_rows
= true;
2396 static const TypeInfo qemu_console_info
= {
2397 .name
= TYPE_QEMU_CONSOLE
,
2398 .parent
= TYPE_OBJECT
,
2399 .instance_size
= sizeof(QemuConsole
),
2400 .class_size
= sizeof(QemuConsoleClass
),
2403 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2405 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2407 cc
->parse
= qemu_chr_parse_vc
;
2408 cc
->open
= vc_chr_open
;
2409 cc
->chr_write
= vc_chr_write
;
2410 cc
->chr_set_echo
= vc_chr_set_echo
;
2413 static const TypeInfo char_vc_type_info
= {
2414 .name
= TYPE_CHARDEV_VC
,
2415 .parent
= TYPE_CHARDEV
,
2416 .instance_size
= sizeof(VCChardev
),
2417 .class_init
= char_vc_class_init
,
2420 void qemu_console_early_init(void)
2422 /* set the default vc driver */
2423 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2424 type_register(&char_vc_type_info
);
2428 static void register_types(void)
2430 type_register_static(&qemu_console_info
);
2433 type_init(register_types
);