2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-ui.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qemu/timer.h"
33 #include "chardev/char-fe.h"
35 #include "exec/memory.h"
37 #define DEFAULT_BACKSCROLL 512
38 #define CONSOLE_CURSOR_PERIOD 500
40 typedef struct TextAttributes
{
50 typedef struct TextCell
{
52 TextAttributes t_attrib
;
55 #define MAX_ESC_PARAMS 3
63 typedef struct QEMUFIFO
{
66 int count
, wptr
, rptr
;
69 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
73 l
= f
->buf_size
- f
->count
;
78 l
= f
->buf_size
- f
->wptr
;
81 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
83 if (f
->wptr
>= f
->buf_size
)
92 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
100 l
= f
->buf_size
- f
->rptr
;
103 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
105 if (f
->rptr
>= f
->buf_size
)
117 TEXT_CONSOLE_FIXED_SIZE
124 console_type_t console_type
;
126 DisplaySurface
*surface
;
128 DisplayChangeListener
*gl
;
132 /* Graphic console state. */
137 const GraphicHwOps
*hw_ops
;
140 /* Text console state */
144 int backscroll_height
;
146 int x_saved
, y_saved
;
149 TextAttributes t_attrib_default
; /* default text attributes */
150 TextAttributes t_attrib
; /* currently active text attributes */
152 int text_x
[2], text_y
[2], cursor_invalidate
;
161 int esc_params
[MAX_ESC_PARAMS
];
165 /* fifo for key pressed */
167 uint8_t out_fifo_buf
[16];
168 QEMUTimer
*kbd_timer
;
170 QTAILQ_ENTRY(QemuConsole
) next
;
173 struct DisplayState
{
174 QEMUTimer
*gui_timer
;
175 uint64_t last_update
;
176 uint64_t update_interval
;
181 QLIST_HEAD(, DisplayChangeListener
) listeners
;
184 static DisplayState
*display_state
;
185 static QemuConsole
*active_console
;
186 static QTAILQ_HEAD(, QemuConsole
) consoles
=
187 QTAILQ_HEAD_INITIALIZER(consoles
);
188 static bool cursor_visible_phase
;
189 static QEMUTimer
*cursor_timer
;
191 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
192 static void dpy_refresh(DisplayState
*s
);
193 static DisplayState
*get_alloc_displaystate(void);
194 static void text_console_update_cursor_timer(void);
195 static void text_console_update_cursor(void *opaque
);
197 static void gui_update(void *opaque
)
199 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
200 uint64_t dcl_interval
;
201 DisplayState
*ds
= opaque
;
202 DisplayChangeListener
*dcl
;
205 ds
->refreshing
= true;
207 ds
->refreshing
= false;
209 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
210 dcl_interval
= dcl
->update_interval
?
211 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
212 if (interval
> dcl_interval
) {
213 interval
= dcl_interval
;
216 if (ds
->update_interval
!= interval
) {
217 ds
->update_interval
= interval
;
218 QTAILQ_FOREACH(con
, &consoles
, next
) {
219 if (con
->hw_ops
->update_interval
) {
220 con
->hw_ops
->update_interval(con
->hw
, interval
);
223 trace_console_refresh(interval
);
225 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
226 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
229 static void gui_setup_refresh(DisplayState
*ds
)
231 DisplayChangeListener
*dcl
;
232 bool need_timer
= false;
233 bool have_gfx
= false;
234 bool have_text
= false;
236 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
237 if (dcl
->ops
->dpy_refresh
!= NULL
) {
240 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
243 if (dcl
->ops
->dpy_text_update
!= NULL
) {
248 if (need_timer
&& ds
->gui_timer
== NULL
) {
249 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
250 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
252 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
253 timer_del(ds
->gui_timer
);
254 timer_free(ds
->gui_timer
);
255 ds
->gui_timer
= NULL
;
258 ds
->have_gfx
= have_gfx
;
259 ds
->have_text
= have_text
;
262 void graphic_hw_update(QemuConsole
*con
)
265 con
= active_console
;
267 if (con
&& con
->hw_ops
->gfx_update
) {
268 con
->hw_ops
->gfx_update(con
->hw
);
272 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
276 con
->gl_block
= block
;
277 if (con
->hw_ops
->gl_block
) {
278 con
->hw_ops
->gl_block(con
->hw
, block
);
282 int qemu_console_get_window_id(QemuConsole
*con
)
284 return con
->window_id
;
287 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
289 con
->window_id
= window_id
;
292 void graphic_hw_invalidate(QemuConsole
*con
)
295 con
= active_console
;
297 if (con
&& con
->hw_ops
->invalidate
) {
298 con
->hw_ops
->invalidate(con
->hw
);
302 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
305 int width
= pixman_image_get_width(ds
->image
);
306 int height
= pixman_image_get_height(ds
->image
);
311 pixman_image_t
*linebuf
;
313 trace_ppm_save(filename
, ds
);
314 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
316 error_setg(errp
, "failed to open file '%s': %s", filename
,
320 f
= fdopen(fd
, "wb");
321 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
326 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
327 for (y
= 0; y
< height
; y
++) {
328 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
330 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
331 pixman_image_get_stride(linebuf
), f
);
339 qemu_pixman_image_unref(linebuf
);
344 error_setg(errp
, "failed to write to file '%s': %s", filename
,
350 void qmp_screendump(const char *filename
, bool has_device
, const char *device
,
351 bool has_head
, int64_t head
, Error
**errp
)
354 DisplaySurface
*surface
;
357 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
364 error_setg(errp
, "'head' must be specified together with 'device'");
367 con
= qemu_console_lookup_by_index(0);
369 error_setg(errp
, "There is no console to take a screendump from");
374 graphic_hw_update(con
);
375 surface
= qemu_console_surface(con
);
377 error_setg(errp
, "no surface");
381 ppm_save(filename
, surface
, errp
);
384 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
387 con
= active_console
;
389 if (con
&& con
->hw_ops
->text_update
) {
390 con
->hw_ops
->text_update(con
->hw
, chardata
);
394 static void vga_fill_rect(QemuConsole
*con
,
395 int posx
, int posy
, int width
, int height
,
396 pixman_color_t color
)
398 DisplaySurface
*surface
= qemu_console_surface(con
);
399 pixman_rectangle16_t rect
= {
400 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
403 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
407 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
408 static void vga_bitblt(QemuConsole
*con
,
409 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
411 DisplaySurface
*surface
= qemu_console_surface(con
);
413 pixman_image_composite(PIXMAN_OP_SRC
,
414 surface
->image
, NULL
, surface
->image
,
415 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
418 /***********************************************************/
419 /* basic char display */
421 #define FONT_HEIGHT 16
426 #define QEMU_RGB(r, g, b) \
427 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
429 static const pixman_color_t color_table_rgb
[2][8] = {
431 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
432 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
433 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
434 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
435 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
436 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
437 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
438 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
441 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
442 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
443 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
444 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
445 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
446 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
447 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
448 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
452 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
453 TextAttributes
*t_attrib
)
455 static pixman_image_t
*glyphs
[256];
456 DisplaySurface
*surface
= qemu_console_surface(s
);
457 pixman_color_t fgcol
, bgcol
;
459 if (t_attrib
->invers
) {
460 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
461 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
463 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
464 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
468 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
470 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
471 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
474 static void text_console_resize(QemuConsole
*s
)
476 TextCell
*cells
, *c
, *c1
;
477 int w1
, x
, y
, last_width
;
479 last_width
= s
->width
;
480 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
481 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
487 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
488 for(y
= 0; y
< s
->total_height
; y
++) {
489 c
= &cells
[y
* s
->width
];
491 c1
= &s
->cells
[y
* last_width
];
492 for(x
= 0; x
< w1
; x
++) {
496 for(x
= w1
; x
< s
->width
; x
++) {
498 c
->t_attrib
= s
->t_attrib_default
;
506 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
508 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
509 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
510 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
511 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
514 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
516 if (!qemu_console_is_visible(s
)) {
519 if (s
->update_x0
> x
* FONT_WIDTH
)
520 s
->update_x0
= x
* FONT_WIDTH
;
521 if (s
->update_y0
> y
* FONT_HEIGHT
)
522 s
->update_y0
= y
* FONT_HEIGHT
;
523 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
524 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
525 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
526 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
529 static void update_xy(QemuConsole
*s
, int x
, int y
)
534 if (s
->ds
->have_text
) {
535 text_update_xy(s
, x
, y
);
538 y1
= (s
->y_base
+ y
) % s
->total_height
;
539 y2
= y1
- s
->y_displayed
;
541 y2
+= s
->total_height
;
543 if (y2
< s
->height
) {
544 c
= &s
->cells
[y1
* s
->width
+ x
];
545 vga_putcharxy(s
, x
, y2
, c
->ch
,
547 invalidate_xy(s
, x
, y2
);
551 static void console_show_cursor(QemuConsole
*s
, int show
)
557 if (s
->ds
->have_text
) {
558 s
->cursor_invalidate
= 1;
564 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
565 y
= y1
- s
->y_displayed
;
567 y
+= s
->total_height
;
570 c
= &s
->cells
[y1
* s
->width
+ x
];
571 if (show
&& cursor_visible_phase
) {
572 TextAttributes t_attrib
= s
->t_attrib_default
;
573 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
574 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
576 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
578 invalidate_xy(s
, x
, y
);
582 static void console_refresh(QemuConsole
*s
)
584 DisplaySurface
*surface
= qemu_console_surface(s
);
588 if (s
->ds
->have_text
) {
591 s
->text_x
[1] = s
->width
- 1;
592 s
->text_y
[1] = s
->height
- 1;
593 s
->cursor_invalidate
= 1;
596 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
597 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
599 for (y
= 0; y
< s
->height
; y
++) {
600 c
= s
->cells
+ y1
* s
->width
;
601 for (x
= 0; x
< s
->width
; x
++) {
602 vga_putcharxy(s
, x
, y
, c
->ch
,
606 if (++y1
== s
->total_height
) {
610 console_show_cursor(s
, 1);
611 dpy_gfx_update(s
, 0, 0,
612 surface_width(surface
), surface_height(surface
));
615 static void console_scroll(QemuConsole
*s
, int ydelta
)
620 for(i
= 0; i
< ydelta
; i
++) {
621 if (s
->y_displayed
== s
->y_base
)
623 if (++s
->y_displayed
== s
->total_height
)
628 i
= s
->backscroll_height
;
629 if (i
> s
->total_height
- s
->height
)
630 i
= s
->total_height
- s
->height
;
633 y1
+= s
->total_height
;
634 for(i
= 0; i
< ydelta
; i
++) {
635 if (s
->y_displayed
== y1
)
637 if (--s
->y_displayed
< 0)
638 s
->y_displayed
= s
->total_height
- 1;
644 static void console_put_lf(QemuConsole
*s
)
650 if (s
->y
>= s
->height
) {
651 s
->y
= s
->height
- 1;
653 if (s
->y_displayed
== s
->y_base
) {
654 if (++s
->y_displayed
== s
->total_height
)
657 if (++s
->y_base
== s
->total_height
)
659 if (s
->backscroll_height
< s
->total_height
)
660 s
->backscroll_height
++;
661 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
662 c
= &s
->cells
[y1
* s
->width
];
663 for(x
= 0; x
< s
->width
; x
++) {
665 c
->t_attrib
= s
->t_attrib_default
;
668 if (s
->y_displayed
== s
->y_base
) {
669 if (s
->ds
->have_text
) {
672 s
->text_x
[1] = s
->width
- 1;
673 s
->text_y
[1] = s
->height
- 1;
676 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
677 s
->width
* FONT_WIDTH
,
678 (s
->height
- 1) * FONT_HEIGHT
);
679 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
680 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
681 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
684 s
->update_x1
= s
->width
* FONT_WIDTH
;
685 s
->update_y1
= s
->height
* FONT_HEIGHT
;
690 /* Set console attributes depending on the current escape codes.
691 * NOTE: I know this code is not very efficient (checking every color for it
692 * self) but it is more readable and better maintainable.
694 static void console_handle_escape(QemuConsole
*s
)
698 for (i
=0; i
<s
->nb_esc_params
; i
++) {
699 switch (s
->esc_params
[i
]) {
700 case 0: /* reset all console attributes to default */
701 s
->t_attrib
= s
->t_attrib_default
;
704 s
->t_attrib
.bold
= 1;
707 s
->t_attrib
.uline
= 1;
710 s
->t_attrib
.blink
= 1;
713 s
->t_attrib
.invers
= 1;
716 s
->t_attrib
.unvisible
= 1;
719 s
->t_attrib
.bold
= 0;
722 s
->t_attrib
.uline
= 0;
725 s
->t_attrib
.blink
= 0;
728 s
->t_attrib
.invers
= 0;
731 s
->t_attrib
.unvisible
= 0;
733 /* set foreground color */
735 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
738 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
741 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
744 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
747 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
750 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
753 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
756 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
758 /* set background color */
760 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
763 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
766 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
769 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
772 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
775 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
778 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
781 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
787 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
789 int y1
= (s
->y_base
+ y
) % s
->total_height
;
790 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
792 c
->t_attrib
= s
->t_attrib_default
;
796 static void console_put_one(QemuConsole
*s
, int ch
)
800 if (s
->x
>= s
->width
) {
805 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
806 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
808 c
->t_attrib
= s
->t_attrib
;
809 update_xy(s
, s
->x
, s
->y
);
813 static void console_respond_str(QemuConsole
*s
, const char *buf
)
816 console_put_one(s
, *buf
);
821 /* set cursor, checking bounds */
822 static void set_cursor(QemuConsole
*s
, int x
, int y
)
830 if (y
>= s
->height
) {
841 static void console_putchar(QemuConsole
*s
, int ch
)
850 case '\r': /* carriage return */
853 case '\n': /* newline */
856 case '\b': /* backspace */
860 case '\t': /* tabspace */
861 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
865 s
->x
= s
->x
+ (8 - (s
->x
% 8));
868 case '\a': /* alert aka. bell */
869 /* TODO: has to be implemented */
872 /* SI (shift in), character set 0 (ignored) */
875 /* SO (shift out), character set 1 (ignored) */
877 case 27: /* esc (introducing an escape sequence) */
878 s
->state
= TTY_STATE_ESC
;
881 console_put_one(s
, ch
);
885 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
887 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
888 s
->esc_params
[i
] = 0;
889 s
->nb_esc_params
= 0;
890 s
->state
= TTY_STATE_CSI
;
892 s
->state
= TTY_STATE_NORM
;
895 case TTY_STATE_CSI
: /* handle escape sequence parameters */
896 if (ch
>= '0' && ch
<= '9') {
897 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
898 int *param
= &s
->esc_params
[s
->nb_esc_params
];
899 int digit
= (ch
- '0');
901 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
902 *param
* 10 + digit
: INT_MAX
;
905 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
907 if (ch
== ';' || ch
== '?') {
910 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
911 ch
, s
->nb_esc_params
);
912 s
->state
= TTY_STATE_NORM
;
916 if (s
->esc_params
[0] == 0) {
917 s
->esc_params
[0] = 1;
919 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
922 /* move cursor down */
923 if (s
->esc_params
[0] == 0) {
924 s
->esc_params
[0] = 1;
926 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
929 /* move cursor right */
930 if (s
->esc_params
[0] == 0) {
931 s
->esc_params
[0] = 1;
933 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
936 /* move cursor left */
937 if (s
->esc_params
[0] == 0) {
938 s
->esc_params
[0] = 1;
940 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
943 /* move cursor to column */
944 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
948 /* move cursor to row, column */
949 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
952 switch (s
->esc_params
[0]) {
954 /* clear to end of screen */
955 for (y
= s
->y
; y
< s
->height
; y
++) {
956 for (x
= 0; x
< s
->width
; x
++) {
957 if (y
== s
->y
&& x
< s
->x
) {
960 console_clear_xy(s
, x
, y
);
965 /* clear from beginning of screen */
966 for (y
= 0; y
<= s
->y
; y
++) {
967 for (x
= 0; x
< s
->width
; x
++) {
968 if (y
== s
->y
&& x
> s
->x
) {
971 console_clear_xy(s
, x
, y
);
976 /* clear entire screen */
977 for (y
= 0; y
<= s
->height
; y
++) {
978 for (x
= 0; x
< s
->width
; x
++) {
979 console_clear_xy(s
, x
, y
);
986 switch (s
->esc_params
[0]) {
989 for(x
= s
->x
; x
< s
->width
; x
++) {
990 console_clear_xy(s
, x
, s
->y
);
994 /* clear from beginning of line */
995 for (x
= 0; x
<= s
->x
; x
++) {
996 console_clear_xy(s
, x
, s
->y
);
1000 /* clear entire line */
1001 for(x
= 0; x
< s
->width
; x
++) {
1002 console_clear_xy(s
, x
, s
->y
);
1008 console_handle_escape(s
);
1011 switch (s
->esc_params
[0]) {
1013 /* report console status (always succeed)*/
1014 console_respond_str(s
, "\033[0n");
1017 /* report cursor position */
1018 sprintf(response
, "\033[%d;%dR",
1019 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1021 console_respond_str(s
, response
);
1026 /* save cursor position */
1031 /* restore cursor position */
1036 trace_console_putchar_unhandled(ch
);
1044 void console_select(unsigned int index
)
1046 DisplayChangeListener
*dcl
;
1049 trace_console_select(index
);
1050 s
= qemu_console_lookup_by_index(index
);
1052 DisplayState
*ds
= s
->ds
;
1056 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1057 if (dcl
->con
!= NULL
) {
1060 if (dcl
->ops
->dpy_gfx_switch
) {
1061 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1065 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1066 surface_height(s
->surface
));
1069 if (ds
->have_text
) {
1070 dpy_text_resize(s
, s
->width
, s
->height
);
1072 text_console_update_cursor(NULL
);
1076 typedef struct VCChardev
{
1078 QemuConsole
*console
;
1081 #define TYPE_CHARDEV_VC "chardev-vc"
1082 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1084 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1086 VCChardev
*drv
= VC_CHARDEV(chr
);
1087 QemuConsole
*s
= drv
->console
;
1094 s
->update_x0
= s
->width
* FONT_WIDTH
;
1095 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1098 console_show_cursor(s
, 0);
1099 for(i
= 0; i
< len
; i
++) {
1100 console_putchar(s
, buf
[i
]);
1102 console_show_cursor(s
, 1);
1103 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1104 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1105 s
->update_x1
- s
->update_x0
,
1106 s
->update_y1
- s
->update_y0
);
1111 static void kbd_send_chars(void *opaque
)
1113 QemuConsole
*s
= opaque
;
1117 len
= qemu_chr_be_can_write(s
->chr
);
1118 if (len
> s
->out_fifo
.count
)
1119 len
= s
->out_fifo
.count
;
1121 if (len
> sizeof(buf
))
1123 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1124 qemu_chr_be_write(s
->chr
, buf
, len
);
1126 /* characters are pending: we send them a bit later (XXX:
1127 horrible, should change char device API) */
1128 if (s
->out_fifo
.count
> 0) {
1129 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1133 /* called when an ascii key is pressed */
1134 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1136 uint8_t buf
[16], *q
;
1140 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1144 case QEMU_KEY_CTRL_UP
:
1145 console_scroll(s
, -1);
1147 case QEMU_KEY_CTRL_DOWN
:
1148 console_scroll(s
, 1);
1150 case QEMU_KEY_CTRL_PAGEUP
:
1151 console_scroll(s
, -10);
1153 case QEMU_KEY_CTRL_PAGEDOWN
:
1154 console_scroll(s
, 10);
1157 /* convert the QEMU keysym to VT100 key string */
1159 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1162 c
= keysym
- 0xe100;
1164 *q
++ = '0' + (c
/ 10);
1165 *q
++ = '0' + (c
% 10);
1167 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1170 *q
++ = keysym
& 0xff;
1171 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1172 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1178 vc_chr_write(s
->chr
, buf
, q
- buf
);
1181 if (be
&& be
->chr_read
) {
1182 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1189 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1190 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1191 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1192 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1193 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1194 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1195 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1196 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1197 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1198 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1199 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1202 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1203 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1204 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1205 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1206 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1207 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1208 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1209 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1210 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1213 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1217 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1221 kbd_put_keysym_console(s
, keysym
);
1225 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1229 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1230 kbd_put_keysym_console(s
, str
[i
]);
1234 void kbd_put_keysym(int keysym
)
1236 kbd_put_keysym_console(active_console
, keysym
);
1239 static void text_console_invalidate(void *opaque
)
1241 QemuConsole
*s
= (QemuConsole
*) opaque
;
1243 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1244 text_console_resize(s
);
1249 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1251 QemuConsole
*s
= (QemuConsole
*) opaque
;
1254 if (s
->text_x
[0] <= s
->text_x
[1]) {
1255 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1256 chardata
+= s
->text_y
[0] * s
->width
;
1257 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1258 for (j
= 0; j
< s
->width
; j
++, src
++) {
1259 console_write_ch(chardata
++,
1260 ATTR2CHTYPE(s
->cells
[src
].ch
,
1261 s
->cells
[src
].t_attrib
.fgcol
,
1262 s
->cells
[src
].t_attrib
.bgcol
,
1263 s
->cells
[src
].t_attrib
.bold
));
1265 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1266 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1267 s
->text_x
[0] = s
->width
;
1268 s
->text_y
[0] = s
->height
;
1272 if (s
->cursor_invalidate
) {
1273 dpy_text_cursor(s
, s
->x
, s
->y
);
1274 s
->cursor_invalidate
= 0;
1278 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1285 obj
= object_new(TYPE_QEMU_CONSOLE
);
1286 s
= QEMU_CONSOLE(obj
);
1288 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1289 (Object
**)&s
->device
,
1290 object_property_allow_set_link
,
1291 OBJ_PROP_LINK_STRONG
,
1293 object_property_add_uint32_ptr(obj
, "head",
1294 &s
->head
, &error_abort
);
1296 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1297 (console_type
== GRAPHIC_CONSOLE
))) {
1301 s
->console_type
= console_type
;
1303 if (QTAILQ_EMPTY(&consoles
)) {
1305 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1306 } else if (console_type
!= GRAPHIC_CONSOLE
|| qdev_hotplug
) {
1307 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1308 s
->index
= last
->index
+ 1;
1309 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1312 * HACK: Put graphical consoles before text consoles.
1314 * Only do that for coldplugged devices. After initial device
1315 * initialization we will not renumber the consoles any more.
1317 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1319 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1320 c
->console_type
== GRAPHIC_CONSOLE
) {
1321 c
= QTAILQ_NEXT(c
, next
);
1323 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1324 /* have no text consoles */
1325 s
->index
= c
->index
+ 1;
1326 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1328 s
->index
= c
->index
;
1329 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1330 /* renumber text consoles */
1331 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1339 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1341 qemu_pixman_image_unref(surface
->image
);
1342 surface
->image
= NULL
;
1344 surface
->format
= PIXMAN_x8r8g8b8
;
1345 surface
->image
= pixman_image_create_bits(surface
->format
,
1348 assert(surface
->image
!= NULL
);
1350 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1353 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1355 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1357 trace_displaysurface_create(surface
, width
, height
);
1358 qemu_alloc_display(surface
, width
, height
);
1362 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1363 pixman_format_code_t format
,
1364 int linesize
, uint8_t *data
)
1366 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1368 trace_displaysurface_create_from(surface
, width
, height
, format
);
1369 surface
->format
= format
;
1370 surface
->image
= pixman_image_create_bits(surface
->format
,
1372 (void *)data
, linesize
);
1373 assert(surface
->image
!= NULL
);
1378 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1380 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1382 trace_displaysurface_create_pixman(surface
);
1383 surface
->format
= pixman_image_get_format(image
);
1384 surface
->image
= pixman_image_ref(image
);
1389 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1392 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1393 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1394 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1395 pixman_image_t
*glyph
;
1399 x
= (w
/ FONT_WIDTH
- len
) / 2;
1400 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1401 for (i
= 0; i
< len
; i
++) {
1402 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1403 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1404 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1405 qemu_pixman_image_unref(glyph
);
1410 void qemu_free_displaysurface(DisplaySurface
*surface
)
1412 if (surface
== NULL
) {
1415 trace_displaysurface_free(surface
);
1416 qemu_pixman_image_unref(surface
->image
);
1420 bool console_has_gl(QemuConsole
*con
)
1422 return con
->gl
!= NULL
;
1425 bool console_has_gl_dmabuf(QemuConsole
*con
)
1427 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1430 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1432 static const char nodev
[] =
1433 "This VM has no graphic display device.";
1434 static DisplaySurface
*dummy
;
1439 if (dcl
->ops
->dpy_gl_ctx_create
) {
1440 /* display has opengl support */
1443 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1444 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1450 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1451 dcl
->ds
= get_alloc_displaystate();
1452 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1453 gui_setup_refresh(dcl
->ds
);
1458 con
= active_console
;
1460 if (dcl
->ops
->dpy_gfx_switch
) {
1462 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1465 dummy
= qemu_create_message_surface(640, 480, nodev
);
1467 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1470 text_console_update_cursor(NULL
);
1473 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1476 DisplayState
*ds
= dcl
->ds
;
1478 dcl
->update_interval
= interval
;
1479 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1480 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1484 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1486 DisplayState
*ds
= dcl
->ds
;
1487 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1491 QLIST_REMOVE(dcl
, next
);
1493 gui_setup_refresh(ds
);
1496 static void dpy_set_ui_info_timer(void *opaque
)
1498 QemuConsole
*con
= opaque
;
1500 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1503 bool dpy_ui_info_supported(QemuConsole
*con
)
1505 return con
->hw_ops
->ui_info
!= NULL
;
1508 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1510 assert(con
!= NULL
);
1512 if (!dpy_ui_info_supported(con
)) {
1515 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1516 /* nothing changed -- ignore */
1521 * Typically we get a flood of these as the user resizes the window.
1522 * Wait until the dust has settled (one second without updates), then
1523 * go notify the guest.
1525 con
->ui_info
= *info
;
1526 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1530 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1532 DisplayState
*s
= con
->ds
;
1533 DisplayChangeListener
*dcl
;
1538 width
= surface_width(con
->surface
);
1539 height
= surface_height(con
->surface
);
1545 w
= MIN(w
, width
- x
);
1546 h
= MIN(h
, height
- y
);
1548 if (!qemu_console_is_visible(con
)) {
1551 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1552 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1555 if (dcl
->ops
->dpy_gfx_update
) {
1556 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1561 void dpy_gfx_update_full(QemuConsole
*con
)
1563 if (!con
->surface
) {
1566 dpy_gfx_update(con
, 0, 0,
1567 surface_width(con
->surface
),
1568 surface_height(con
->surface
));
1571 void dpy_gfx_replace_surface(QemuConsole
*con
,
1572 DisplaySurface
*surface
)
1574 DisplayState
*s
= con
->ds
;
1575 DisplaySurface
*old_surface
= con
->surface
;
1576 DisplayChangeListener
*dcl
;
1578 assert(old_surface
!= surface
|| surface
== NULL
);
1580 con
->surface
= surface
;
1581 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1582 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1585 if (dcl
->ops
->dpy_gfx_switch
) {
1586 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1589 qemu_free_displaysurface(old_surface
);
1592 bool dpy_gfx_check_format(QemuConsole
*con
,
1593 pixman_format_code_t format
)
1595 DisplayChangeListener
*dcl
;
1596 DisplayState
*s
= con
->ds
;
1598 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1599 if (dcl
->con
&& dcl
->con
!= con
) {
1600 /* dcl bound to another console -> skip */
1603 if (dcl
->ops
->dpy_gfx_check_format
) {
1604 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1608 /* default is to whitelist native 32 bpp only */
1609 if (format
!= qemu_default_pixman_format(32, true)) {
1617 static void dpy_refresh(DisplayState
*s
)
1619 DisplayChangeListener
*dcl
;
1621 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1622 if (dcl
->ops
->dpy_refresh
) {
1623 dcl
->ops
->dpy_refresh(dcl
);
1628 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1630 DisplayState
*s
= con
->ds
;
1631 DisplayChangeListener
*dcl
;
1633 if (!qemu_console_is_visible(con
)) {
1636 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1637 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1640 if (dcl
->ops
->dpy_text_cursor
) {
1641 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1646 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1648 DisplayState
*s
= con
->ds
;
1649 DisplayChangeListener
*dcl
;
1651 if (!qemu_console_is_visible(con
)) {
1654 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1655 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1658 if (dcl
->ops
->dpy_text_update
) {
1659 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1664 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1666 DisplayState
*s
= con
->ds
;
1667 DisplayChangeListener
*dcl
;
1669 if (!qemu_console_is_visible(con
)) {
1672 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1673 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1676 if (dcl
->ops
->dpy_text_resize
) {
1677 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1682 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1684 DisplayState
*s
= con
->ds
;
1685 DisplayChangeListener
*dcl
;
1687 if (!qemu_console_is_visible(con
)) {
1690 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1691 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1694 if (dcl
->ops
->dpy_mouse_set
) {
1695 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1700 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1702 DisplayState
*s
= con
->ds
;
1703 DisplayChangeListener
*dcl
;
1705 if (!qemu_console_is_visible(con
)) {
1708 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1709 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1712 if (dcl
->ops
->dpy_cursor_define
) {
1713 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1718 bool dpy_cursor_define_supported(QemuConsole
*con
)
1720 DisplayState
*s
= con
->ds
;
1721 DisplayChangeListener
*dcl
;
1723 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1724 if (dcl
->ops
->dpy_cursor_define
) {
1731 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1732 struct QEMUGLParams
*qparams
)
1735 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1738 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1741 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1744 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1747 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1750 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1753 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1756 void dpy_gl_scanout_disable(QemuConsole
*con
)
1759 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1760 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1762 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1767 void dpy_gl_scanout_texture(QemuConsole
*con
,
1768 uint32_t backing_id
,
1769 bool backing_y_0_top
,
1770 uint32_t backing_width
,
1771 uint32_t backing_height
,
1772 uint32_t x
, uint32_t y
,
1773 uint32_t width
, uint32_t height
)
1776 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1778 backing_width
, backing_height
,
1779 x
, y
, width
, height
);
1782 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1786 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1789 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1790 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1794 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1795 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1796 have_hot
, hot_x
, hot_y
);
1800 void dpy_gl_cursor_position(QemuConsole
*con
,
1801 uint32_t pos_x
, uint32_t pos_y
)
1805 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1806 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1810 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1815 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1816 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1820 void dpy_gl_update(QemuConsole
*con
,
1821 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1824 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1827 /***********************************************************/
1828 /* register display */
1830 /* console.c internal use only */
1831 static DisplayState
*get_alloc_displaystate(void)
1833 if (!display_state
) {
1834 display_state
= g_new0(DisplayState
, 1);
1835 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1836 text_console_update_cursor
, NULL
);
1838 return display_state
;
1842 * Called by main(), after creating QemuConsoles
1843 * and before initializing ui (sdl/vnc/...).
1845 DisplayState
*init_displaystate(void)
1850 get_alloc_displaystate();
1851 QTAILQ_FOREACH(con
, &consoles
, next
) {
1852 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1854 text_console_do_init(con
->chr
, display_state
);
1857 /* Hook up into the qom tree here (not in new_console()), once
1858 * all QemuConsoles are created and the order / numbering
1859 * doesn't change any more */
1860 name
= g_strdup_printf("console[%d]", con
->index
);
1861 object_property_add_child(container_get(object_get_root(), "/backend"),
1862 name
, OBJECT(con
), &error_abort
);
1866 return display_state
;
1869 void graphic_console_set_hwops(QemuConsole
*con
,
1870 const GraphicHwOps
*hw_ops
,
1873 con
->hw_ops
= hw_ops
;
1877 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1878 const GraphicHwOps
*hw_ops
,
1881 static const char noinit
[] =
1882 "Guest has not initialized the display (yet).";
1887 DisplaySurface
*surface
;
1889 ds
= get_alloc_displaystate();
1890 s
= qemu_console_lookup_unused();
1892 trace_console_gfx_reuse(s
->index
);
1894 width
= surface_width(s
->surface
);
1895 height
= surface_height(s
->surface
);
1898 trace_console_gfx_new();
1899 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1900 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1901 dpy_set_ui_info_timer
, s
);
1903 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1905 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1909 surface
= qemu_create_message_surface(width
, height
, noinit
);
1910 dpy_gfx_replace_surface(s
, surface
);
1914 static const GraphicHwOps unused_ops
= {
1918 void graphic_console_close(QemuConsole
*con
)
1920 static const char unplugged
[] =
1921 "Guest display has been unplugged";
1922 DisplaySurface
*surface
;
1927 width
= surface_width(con
->surface
);
1928 height
= surface_height(con
->surface
);
1931 trace_console_gfx_close(con
->index
);
1932 object_property_set_link(OBJECT(con
), NULL
, "device", &error_abort
);
1933 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1936 dpy_gl_scanout_disable(con
);
1938 surface
= qemu_create_message_surface(width
, height
, unplugged
);
1939 dpy_gfx_replace_surface(con
, surface
);
1942 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1946 QTAILQ_FOREACH(con
, &consoles
, next
) {
1947 if (con
->index
== index
) {
1954 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1960 QTAILQ_FOREACH(con
, &consoles
, next
) {
1961 obj
= object_property_get_link(OBJECT(con
),
1962 "device", &error_abort
);
1963 if (DEVICE(obj
) != dev
) {
1966 h
= object_property_get_uint(OBJECT(con
),
1967 "head", &error_abort
);
1976 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1977 uint32_t head
, Error
**errp
)
1982 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1984 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1985 "Device '%s' not found", device_id
);
1989 con
= qemu_console_lookup_by_device(dev
, head
);
1991 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1999 QemuConsole
*qemu_console_lookup_unused(void)
2004 QTAILQ_FOREACH(con
, &consoles
, next
) {
2005 if (con
->hw_ops
!= &unused_ops
) {
2008 obj
= object_property_get_link(OBJECT(con
),
2009 "device", &error_abort
);
2018 bool qemu_console_is_visible(QemuConsole
*con
)
2020 return (con
== active_console
) || (con
->dcls
> 0);
2023 bool qemu_console_is_graphic(QemuConsole
*con
)
2026 con
= active_console
;
2028 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2031 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2034 con
= active_console
;
2036 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2039 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2041 assert(con
!= NULL
);
2042 return con
->gl_block
;
2045 char *qemu_console_get_label(QemuConsole
*con
)
2047 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2049 return g_strdup(object_get_typename(con
->device
));
2051 return g_strdup("VGA");
2053 if (con
->chr
&& con
->chr
->label
) {
2054 return g_strdup(con
->chr
->label
);
2056 return g_strdup_printf("vc%d", con
->index
);
2060 int qemu_console_get_index(QemuConsole
*con
)
2063 con
= active_console
;
2065 return con
? con
->index
: -1;
2068 uint32_t qemu_console_get_head(QemuConsole
*con
)
2071 con
= active_console
;
2073 return con
? con
->head
: -1;
2076 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
2078 assert(con
!= NULL
);
2079 return &con
->ui_info
;
2082 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2085 con
= active_console
;
2087 return con
? surface_width(con
->surface
) : fallback
;
2090 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2093 con
= active_console
;
2095 return con
? surface_height(con
->surface
) : fallback
;
2098 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2100 VCChardev
*drv
= VC_CHARDEV(chr
);
2101 QemuConsole
*s
= drv
->console
;
2106 static void text_console_update_cursor_timer(void)
2108 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2109 + CONSOLE_CURSOR_PERIOD
/ 2);
2112 static void text_console_update_cursor(void *opaque
)
2117 cursor_visible_phase
= !cursor_visible_phase
;
2119 QTAILQ_FOREACH(s
, &consoles
, next
) {
2120 if (qemu_console_is_graphic(s
) ||
2121 !qemu_console_is_visible(s
)) {
2125 graphic_hw_invalidate(s
);
2129 text_console_update_cursor_timer();
2133 static const GraphicHwOps text_console_ops
= {
2134 .invalidate
= text_console_invalidate
,
2135 .text_update
= text_console_update
,
2138 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2140 VCChardev
*drv
= VC_CHARDEV(chr
);
2141 QemuConsole
*s
= drv
->console
;
2142 int g_width
= 80 * FONT_WIDTH
;
2143 int g_height
= 24 * FONT_HEIGHT
;
2145 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2146 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2147 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2152 s
->total_height
= DEFAULT_BACKSCROLL
;
2156 if (active_console
&& active_console
->surface
) {
2157 g_width
= surface_width(active_console
->surface
);
2158 g_height
= surface_height(active_console
->surface
);
2160 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2163 s
->hw_ops
= &text_console_ops
;
2166 /* Set text attribute defaults */
2167 s
->t_attrib_default
.bold
= 0;
2168 s
->t_attrib_default
.uline
= 0;
2169 s
->t_attrib_default
.blink
= 0;
2170 s
->t_attrib_default
.invers
= 0;
2171 s
->t_attrib_default
.unvisible
= 0;
2172 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2173 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2174 /* set current text attributes to default */
2175 s
->t_attrib
= s
->t_attrib_default
;
2176 text_console_resize(s
);
2182 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2183 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2184 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2185 s
->t_attrib
= s
->t_attrib_default
;
2188 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2191 static void vc_chr_open(Chardev
*chr
,
2192 ChardevBackend
*backend
,
2196 ChardevVC
*vc
= backend
->u
.vc
.data
;
2197 VCChardev
*drv
= VC_CHARDEV(chr
);
2200 unsigned height
= 0;
2202 if (vc
->has_width
) {
2204 } else if (vc
->has_cols
) {
2205 width
= vc
->cols
* FONT_WIDTH
;
2208 if (vc
->has_height
) {
2209 height
= vc
->height
;
2210 } else if (vc
->has_rows
) {
2211 height
= vc
->rows
* FONT_HEIGHT
;
2214 trace_console_txt_new(width
, height
);
2215 if (width
== 0 || height
== 0) {
2216 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2218 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2219 s
->surface
= qemu_create_displaysurface(width
, height
);
2223 error_setg(errp
, "cannot create text console");
2230 if (display_state
) {
2231 text_console_do_init(chr
, display_state
);
2234 /* console/chardev init sometimes completes elsewhere in a 2nd
2235 * stage, so defer OPENED events until they are fully initialized
2240 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2242 DisplaySurface
*surface
;
2244 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2246 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2247 pixman_image_get_width(s
->surface
->image
) == width
&&
2248 pixman_image_get_height(s
->surface
->image
) == height
) {
2252 surface
= qemu_create_displaysurface(width
, height
);
2253 dpy_gfx_replace_surface(s
, surface
);
2256 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2258 return console
->surface
;
2261 PixelFormat
qemu_default_pixelformat(int bpp
)
2263 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2264 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2268 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2270 void qemu_display_register(QemuDisplay
*ui
)
2272 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2273 dpys
[ui
->type
] = ui
;
2276 bool qemu_display_find_default(DisplayOptions
*opts
)
2278 static DisplayType prio
[] = {
2285 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2286 if (dpys
[prio
[i
]] == NULL
) {
2287 ui_module_load_one(DisplayType_str(prio
[i
]));
2289 if (dpys
[prio
[i
]] == NULL
) {
2292 opts
->type
= prio
[i
];
2298 void qemu_display_early_init(DisplayOptions
*opts
)
2300 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2301 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2304 if (dpys
[opts
->type
] == NULL
) {
2305 ui_module_load_one(DisplayType_str(opts
->type
));
2307 if (dpys
[opts
->type
] == NULL
) {
2308 error_report("Display '%s' is not available.",
2309 DisplayType_str(opts
->type
));
2312 if (dpys
[opts
->type
]->early_init
) {
2313 dpys
[opts
->type
]->early_init(opts
);
2317 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2319 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2320 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2323 assert(dpys
[opts
->type
] != NULL
);
2324 dpys
[opts
->type
]->init(ds
, opts
);
2327 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2332 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2333 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2334 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2336 val
= qemu_opt_get_number(opts
, "width", 0);
2338 vc
->has_width
= true;
2342 val
= qemu_opt_get_number(opts
, "height", 0);
2344 vc
->has_height
= true;
2348 val
= qemu_opt_get_number(opts
, "cols", 0);
2350 vc
->has_cols
= true;
2354 val
= qemu_opt_get_number(opts
, "rows", 0);
2356 vc
->has_rows
= true;
2361 static const TypeInfo qemu_console_info
= {
2362 .name
= TYPE_QEMU_CONSOLE
,
2363 .parent
= TYPE_OBJECT
,
2364 .instance_size
= sizeof(QemuConsole
),
2365 .class_size
= sizeof(QemuConsoleClass
),
2368 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2370 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2372 cc
->parse
= qemu_chr_parse_vc
;
2373 cc
->open
= vc_chr_open
;
2374 cc
->chr_write
= vc_chr_write
;
2375 cc
->chr_set_echo
= vc_chr_set_echo
;
2378 static const TypeInfo char_vc_type_info
= {
2379 .name
= TYPE_CHARDEV_VC
,
2380 .parent
= TYPE_CHARDEV
,
2381 .instance_size
= sizeof(VCChardev
),
2382 .class_init
= char_vc_class_init
,
2385 void qemu_console_early_init(void)
2387 /* set the default vc driver */
2388 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2389 type_register(&char_vc_type_info
);
2393 static void register_types(void)
2395 type_register_static(&qemu_console_info
);
2398 type_init(register_types
);