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"
36 #include "io/channel-file.h"
38 #define DEFAULT_BACKSCROLL 512
39 #define CONSOLE_CURSOR_PERIOD 500
41 typedef struct TextAttributes
{
51 typedef struct TextCell
{
53 TextAttributes t_attrib
;
56 #define MAX_ESC_PARAMS 3
64 typedef struct QEMUFIFO
{
67 int count
, wptr
, rptr
;
70 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
74 l
= f
->buf_size
- f
->count
;
79 l
= f
->buf_size
- f
->wptr
;
82 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
84 if (f
->wptr
>= f
->buf_size
)
93 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
101 l
= f
->buf_size
- f
->rptr
;
104 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
106 if (f
->rptr
>= f
->buf_size
)
118 TEXT_CONSOLE_FIXED_SIZE
125 console_type_t console_type
;
127 DisplaySurface
*surface
;
129 DisplayChangeListener
*gl
;
133 /* Graphic console state. */
138 const GraphicHwOps
*hw_ops
;
141 /* Text console state */
145 int backscroll_height
;
147 int x_saved
, y_saved
;
150 TextAttributes t_attrib_default
; /* default text attributes */
151 TextAttributes t_attrib
; /* currently active text attributes */
153 int text_x
[2], text_y
[2], cursor_invalidate
;
162 int esc_params
[MAX_ESC_PARAMS
];
166 /* fifo for key pressed */
168 uint8_t out_fifo_buf
[16];
169 QEMUTimer
*kbd_timer
;
171 QTAILQ_ENTRY(QemuConsole
) next
;
174 struct DisplayState
{
175 QEMUTimer
*gui_timer
;
176 uint64_t last_update
;
177 uint64_t update_interval
;
182 QLIST_HEAD(, DisplayChangeListener
) listeners
;
185 static DisplayState
*display_state
;
186 static QemuConsole
*active_console
;
187 static QTAILQ_HEAD(, QemuConsole
) consoles
=
188 QTAILQ_HEAD_INITIALIZER(consoles
);
189 static bool cursor_visible_phase
;
190 static QEMUTimer
*cursor_timer
;
192 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
193 static void dpy_refresh(DisplayState
*s
);
194 static DisplayState
*get_alloc_displaystate(void);
195 static void text_console_update_cursor_timer(void);
196 static void text_console_update_cursor(void *opaque
);
197 static bool ppm_save(int fd
, DisplaySurface
*ds
, Error
**errp
);
199 static void gui_update(void *opaque
)
201 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
202 uint64_t dcl_interval
;
203 DisplayState
*ds
= opaque
;
204 DisplayChangeListener
*dcl
;
207 ds
->refreshing
= true;
209 ds
->refreshing
= false;
211 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
212 dcl_interval
= dcl
->update_interval
?
213 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
214 if (interval
> dcl_interval
) {
215 interval
= dcl_interval
;
218 if (ds
->update_interval
!= interval
) {
219 ds
->update_interval
= interval
;
220 QTAILQ_FOREACH(con
, &consoles
, next
) {
221 if (con
->hw_ops
->update_interval
) {
222 con
->hw_ops
->update_interval(con
->hw
, interval
);
225 trace_console_refresh(interval
);
227 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
228 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
231 static void gui_setup_refresh(DisplayState
*ds
)
233 DisplayChangeListener
*dcl
;
234 bool need_timer
= false;
235 bool have_gfx
= false;
236 bool have_text
= false;
238 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
239 if (dcl
->ops
->dpy_refresh
!= NULL
) {
242 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
245 if (dcl
->ops
->dpy_text_update
!= NULL
) {
250 if (need_timer
&& ds
->gui_timer
== NULL
) {
251 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
252 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
254 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
255 timer_del(ds
->gui_timer
);
256 timer_free(ds
->gui_timer
);
257 ds
->gui_timer
= NULL
;
260 ds
->have_gfx
= have_gfx
;
261 ds
->have_text
= have_text
;
264 void graphic_hw_update_done(QemuConsole
*con
)
268 void graphic_hw_update(QemuConsole
*con
)
272 con
= active_console
;
274 if (con
&& con
->hw_ops
->gfx_update
) {
275 con
->hw_ops
->gfx_update(con
->hw
);
276 async
= con
->hw_ops
->gfx_update_async
;
279 graphic_hw_update_done(con
);
283 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
287 con
->gl_block
= block
;
288 if (con
->hw_ops
->gl_block
) {
289 con
->hw_ops
->gl_block(con
->hw
, block
);
293 int qemu_console_get_window_id(QemuConsole
*con
)
295 return con
->window_id
;
298 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
300 con
->window_id
= window_id
;
303 void graphic_hw_invalidate(QemuConsole
*con
)
306 con
= active_console
;
308 if (con
&& con
->hw_ops
->invalidate
) {
309 con
->hw_ops
->invalidate(con
->hw
);
313 static bool ppm_save(int fd
, DisplaySurface
*ds
, Error
**errp
)
315 int width
= pixman_image_get_width(ds
->image
);
316 int height
= pixman_image_get_height(ds
->image
);
317 g_autoptr(Object
) ioc
= OBJECT(qio_channel_file_new_fd(fd
));
318 g_autofree
char *header
= NULL
;
319 g_autoptr(pixman_image_t
) linebuf
= NULL
;
322 trace_ppm_save(fd
, ds
);
324 header
= g_strdup_printf("P6\n%d %d\n%d\n", width
, height
, 255);
325 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
326 header
, strlen(header
), errp
) < 0) {
330 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
331 for (y
= 0; y
< height
; y
++) {
332 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
333 if (qio_channel_write_all(QIO_CHANNEL(ioc
),
334 (char *)pixman_image_get_data(linebuf
),
335 pixman_image_get_stride(linebuf
), errp
) < 0) {
343 void qmp_screendump(const char *filename
, bool has_device
, const char *device
,
344 bool has_head
, int64_t head
, Error
**errp
)
347 DisplaySurface
*surface
;
351 con
= qemu_console_lookup_by_device_name(device
, has_head
? head
: 0,
358 error_setg(errp
, "'head' must be specified together with 'device'");
361 con
= qemu_console_lookup_by_index(0);
363 error_setg(errp
, "There is no console to take a screendump from");
368 graphic_hw_update(con
);
369 surface
= qemu_console_surface(con
);
371 error_setg(errp
, "no surface");
375 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
377 error_setg(errp
, "failed to open file '%s': %s", filename
,
382 if (!ppm_save(fd
, surface
, errp
)) {
383 qemu_unlink(filename
);
387 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
390 con
= active_console
;
392 if (con
&& con
->hw_ops
->text_update
) {
393 con
->hw_ops
->text_update(con
->hw
, chardata
);
397 static void vga_fill_rect(QemuConsole
*con
,
398 int posx
, int posy
, int width
, int height
,
399 pixman_color_t color
)
401 DisplaySurface
*surface
= qemu_console_surface(con
);
402 pixman_rectangle16_t rect
= {
403 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
406 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
410 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
411 static void vga_bitblt(QemuConsole
*con
,
412 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
414 DisplaySurface
*surface
= qemu_console_surface(con
);
416 pixman_image_composite(PIXMAN_OP_SRC
,
417 surface
->image
, NULL
, surface
->image
,
418 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
421 /***********************************************************/
422 /* basic char display */
424 #define FONT_HEIGHT 16
429 #define QEMU_RGB(r, g, b) \
430 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
432 static const pixman_color_t color_table_rgb
[2][8] = {
434 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
435 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
436 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
437 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
438 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
439 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
440 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
441 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
444 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
445 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
446 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
447 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
448 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
449 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
450 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
451 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
455 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
456 TextAttributes
*t_attrib
)
458 static pixman_image_t
*glyphs
[256];
459 DisplaySurface
*surface
= qemu_console_surface(s
);
460 pixman_color_t fgcol
, bgcol
;
462 if (t_attrib
->invers
) {
463 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
464 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
466 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
467 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
471 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
473 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
474 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
477 static void text_console_resize(QemuConsole
*s
)
479 TextCell
*cells
, *c
, *c1
;
480 int w1
, x
, y
, last_width
;
482 last_width
= s
->width
;
483 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
484 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
490 cells
= g_new(TextCell
, s
->width
* s
->total_height
+ 1);
491 for(y
= 0; y
< s
->total_height
; y
++) {
492 c
= &cells
[y
* s
->width
];
494 c1
= &s
->cells
[y
* last_width
];
495 for(x
= 0; x
< w1
; x
++) {
499 for(x
= w1
; x
< s
->width
; x
++) {
501 c
->t_attrib
= s
->t_attrib_default
;
509 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
511 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
512 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
513 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
514 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
517 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
519 if (!qemu_console_is_visible(s
)) {
522 if (s
->update_x0
> x
* FONT_WIDTH
)
523 s
->update_x0
= x
* FONT_WIDTH
;
524 if (s
->update_y0
> y
* FONT_HEIGHT
)
525 s
->update_y0
= y
* FONT_HEIGHT
;
526 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
527 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
528 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
529 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
532 static void update_xy(QemuConsole
*s
, int x
, int y
)
537 if (s
->ds
->have_text
) {
538 text_update_xy(s
, x
, y
);
541 y1
= (s
->y_base
+ y
) % s
->total_height
;
542 y2
= y1
- s
->y_displayed
;
544 y2
+= s
->total_height
;
546 if (y2
< s
->height
) {
550 c
= &s
->cells
[y1
* s
->width
+ x
];
551 vga_putcharxy(s
, x
, y2
, c
->ch
,
553 invalidate_xy(s
, x
, y2
);
557 static void console_show_cursor(QemuConsole
*s
, int show
)
563 if (s
->ds
->have_text
) {
564 s
->cursor_invalidate
= 1;
570 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
571 y
= y1
- s
->y_displayed
;
573 y
+= s
->total_height
;
576 c
= &s
->cells
[y1
* s
->width
+ x
];
577 if (show
&& cursor_visible_phase
) {
578 TextAttributes t_attrib
= s
->t_attrib_default
;
579 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
580 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
582 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
584 invalidate_xy(s
, x
, y
);
588 static void console_refresh(QemuConsole
*s
)
590 DisplaySurface
*surface
= qemu_console_surface(s
);
594 if (s
->ds
->have_text
) {
597 s
->text_x
[1] = s
->width
- 1;
598 s
->text_y
[1] = s
->height
- 1;
599 s
->cursor_invalidate
= 1;
602 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
603 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
605 for (y
= 0; y
< s
->height
; y
++) {
606 c
= s
->cells
+ y1
* s
->width
;
607 for (x
= 0; x
< s
->width
; x
++) {
608 vga_putcharxy(s
, x
, y
, c
->ch
,
612 if (++y1
== s
->total_height
) {
616 console_show_cursor(s
, 1);
617 dpy_gfx_update(s
, 0, 0,
618 surface_width(surface
), surface_height(surface
));
621 static void console_scroll(QemuConsole
*s
, int ydelta
)
626 for(i
= 0; i
< ydelta
; i
++) {
627 if (s
->y_displayed
== s
->y_base
)
629 if (++s
->y_displayed
== s
->total_height
)
634 i
= s
->backscroll_height
;
635 if (i
> s
->total_height
- s
->height
)
636 i
= s
->total_height
- s
->height
;
639 y1
+= s
->total_height
;
640 for(i
= 0; i
< ydelta
; i
++) {
641 if (s
->y_displayed
== y1
)
643 if (--s
->y_displayed
< 0)
644 s
->y_displayed
= s
->total_height
- 1;
650 static void console_put_lf(QemuConsole
*s
)
656 if (s
->y
>= s
->height
) {
657 s
->y
= s
->height
- 1;
659 if (s
->y_displayed
== s
->y_base
) {
660 if (++s
->y_displayed
== s
->total_height
)
663 if (++s
->y_base
== s
->total_height
)
665 if (s
->backscroll_height
< s
->total_height
)
666 s
->backscroll_height
++;
667 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
668 c
= &s
->cells
[y1
* s
->width
];
669 for(x
= 0; x
< s
->width
; x
++) {
671 c
->t_attrib
= s
->t_attrib_default
;
674 if (s
->y_displayed
== s
->y_base
) {
675 if (s
->ds
->have_text
) {
678 s
->text_x
[1] = s
->width
- 1;
679 s
->text_y
[1] = s
->height
- 1;
682 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
683 s
->width
* FONT_WIDTH
,
684 (s
->height
- 1) * FONT_HEIGHT
);
685 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
686 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
687 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
690 s
->update_x1
= s
->width
* FONT_WIDTH
;
691 s
->update_y1
= s
->height
* FONT_HEIGHT
;
696 /* Set console attributes depending on the current escape codes.
697 * NOTE: I know this code is not very efficient (checking every color for it
698 * self) but it is more readable and better maintainable.
700 static void console_handle_escape(QemuConsole
*s
)
704 for (i
=0; i
<s
->nb_esc_params
; i
++) {
705 switch (s
->esc_params
[i
]) {
706 case 0: /* reset all console attributes to default */
707 s
->t_attrib
= s
->t_attrib_default
;
710 s
->t_attrib
.bold
= 1;
713 s
->t_attrib
.uline
= 1;
716 s
->t_attrib
.blink
= 1;
719 s
->t_attrib
.invers
= 1;
722 s
->t_attrib
.unvisible
= 1;
725 s
->t_attrib
.bold
= 0;
728 s
->t_attrib
.uline
= 0;
731 s
->t_attrib
.blink
= 0;
734 s
->t_attrib
.invers
= 0;
737 s
->t_attrib
.unvisible
= 0;
739 /* set foreground color */
741 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
744 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
747 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
750 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
753 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
756 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
759 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
762 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
764 /* set background color */
766 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
769 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
772 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
775 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
778 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
781 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
784 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
787 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
793 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
795 int y1
= (s
->y_base
+ y
) % s
->total_height
;
799 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
801 c
->t_attrib
= s
->t_attrib_default
;
805 static void console_put_one(QemuConsole
*s
, int ch
)
809 if (s
->x
>= s
->width
) {
814 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
815 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
817 c
->t_attrib
= s
->t_attrib
;
818 update_xy(s
, s
->x
, s
->y
);
822 static void console_respond_str(QemuConsole
*s
, const char *buf
)
825 console_put_one(s
, *buf
);
830 /* set cursor, checking bounds */
831 static void set_cursor(QemuConsole
*s
, int x
, int y
)
839 if (y
>= s
->height
) {
850 static void console_putchar(QemuConsole
*s
, int ch
)
859 case '\r': /* carriage return */
862 case '\n': /* newline */
865 case '\b': /* backspace */
869 case '\t': /* tabspace */
870 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
874 s
->x
= s
->x
+ (8 - (s
->x
% 8));
877 case '\a': /* alert aka. bell */
878 /* TODO: has to be implemented */
881 /* SI (shift in), character set 0 (ignored) */
884 /* SO (shift out), character set 1 (ignored) */
886 case 27: /* esc (introducing an escape sequence) */
887 s
->state
= TTY_STATE_ESC
;
890 console_put_one(s
, ch
);
894 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
896 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
897 s
->esc_params
[i
] = 0;
898 s
->nb_esc_params
= 0;
899 s
->state
= TTY_STATE_CSI
;
901 s
->state
= TTY_STATE_NORM
;
904 case TTY_STATE_CSI
: /* handle escape sequence parameters */
905 if (ch
>= '0' && ch
<= '9') {
906 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
907 int *param
= &s
->esc_params
[s
->nb_esc_params
];
908 int digit
= (ch
- '0');
910 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
911 *param
* 10 + digit
: INT_MAX
;
914 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
916 if (ch
== ';' || ch
== '?') {
919 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
920 ch
, s
->nb_esc_params
);
921 s
->state
= TTY_STATE_NORM
;
925 if (s
->esc_params
[0] == 0) {
926 s
->esc_params
[0] = 1;
928 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
931 /* move cursor down */
932 if (s
->esc_params
[0] == 0) {
933 s
->esc_params
[0] = 1;
935 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
938 /* move cursor right */
939 if (s
->esc_params
[0] == 0) {
940 s
->esc_params
[0] = 1;
942 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
945 /* move cursor left */
946 if (s
->esc_params
[0] == 0) {
947 s
->esc_params
[0] = 1;
949 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
952 /* move cursor to column */
953 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
957 /* move cursor to row, column */
958 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
961 switch (s
->esc_params
[0]) {
963 /* clear to end of screen */
964 for (y
= s
->y
; y
< s
->height
; y
++) {
965 for (x
= 0; x
< s
->width
; x
++) {
966 if (y
== s
->y
&& x
< s
->x
) {
969 console_clear_xy(s
, x
, y
);
974 /* clear from beginning of screen */
975 for (y
= 0; y
<= s
->y
; y
++) {
976 for (x
= 0; x
< s
->width
; x
++) {
977 if (y
== s
->y
&& x
> s
->x
) {
980 console_clear_xy(s
, x
, y
);
985 /* clear entire screen */
986 for (y
= 0; y
<= s
->height
; y
++) {
987 for (x
= 0; x
< s
->width
; x
++) {
988 console_clear_xy(s
, x
, y
);
995 switch (s
->esc_params
[0]) {
998 for(x
= s
->x
; x
< s
->width
; x
++) {
999 console_clear_xy(s
, x
, s
->y
);
1003 /* clear from beginning of line */
1004 for (x
= 0; x
<= s
->x
&& x
< s
->width
; x
++) {
1005 console_clear_xy(s
, x
, s
->y
);
1009 /* clear entire line */
1010 for(x
= 0; x
< s
->width
; x
++) {
1011 console_clear_xy(s
, x
, s
->y
);
1017 console_handle_escape(s
);
1020 switch (s
->esc_params
[0]) {
1022 /* report console status (always succeed)*/
1023 console_respond_str(s
, "\033[0n");
1026 /* report cursor position */
1027 sprintf(response
, "\033[%d;%dR",
1028 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
1030 console_respond_str(s
, response
);
1035 /* save cursor position */
1040 /* restore cursor position */
1045 trace_console_putchar_unhandled(ch
);
1053 void console_select(unsigned int index
)
1055 DisplayChangeListener
*dcl
;
1058 trace_console_select(index
);
1059 s
= qemu_console_lookup_by_index(index
);
1061 DisplayState
*ds
= s
->ds
;
1065 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1066 if (dcl
->con
!= NULL
) {
1069 if (dcl
->ops
->dpy_gfx_switch
) {
1070 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1074 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1075 surface_height(s
->surface
));
1078 if (ds
->have_text
) {
1079 dpy_text_resize(s
, s
->width
, s
->height
);
1081 text_console_update_cursor(NULL
);
1085 typedef struct VCChardev
{
1087 QemuConsole
*console
;
1090 #define TYPE_CHARDEV_VC "chardev-vc"
1091 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1093 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1095 VCChardev
*drv
= VC_CHARDEV(chr
);
1096 QemuConsole
*s
= drv
->console
;
1103 s
->update_x0
= s
->width
* FONT_WIDTH
;
1104 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1107 console_show_cursor(s
, 0);
1108 for(i
= 0; i
< len
; i
++) {
1109 console_putchar(s
, buf
[i
]);
1111 console_show_cursor(s
, 1);
1112 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1113 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1114 s
->update_x1
- s
->update_x0
,
1115 s
->update_y1
- s
->update_y0
);
1120 static void kbd_send_chars(void *opaque
)
1122 QemuConsole
*s
= opaque
;
1126 len
= qemu_chr_be_can_write(s
->chr
);
1127 if (len
> s
->out_fifo
.count
)
1128 len
= s
->out_fifo
.count
;
1130 if (len
> sizeof(buf
))
1132 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1133 qemu_chr_be_write(s
->chr
, buf
, len
);
1135 /* characters are pending: we send them a bit later (XXX:
1136 horrible, should change char device API) */
1137 if (s
->out_fifo
.count
> 0) {
1138 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1142 /* called when an ascii key is pressed */
1143 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1145 uint8_t buf
[16], *q
;
1149 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1153 case QEMU_KEY_CTRL_UP
:
1154 console_scroll(s
, -1);
1156 case QEMU_KEY_CTRL_DOWN
:
1157 console_scroll(s
, 1);
1159 case QEMU_KEY_CTRL_PAGEUP
:
1160 console_scroll(s
, -10);
1162 case QEMU_KEY_CTRL_PAGEDOWN
:
1163 console_scroll(s
, 10);
1166 /* convert the QEMU keysym to VT100 key string */
1168 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1171 c
= keysym
- 0xe100;
1173 *q
++ = '0' + (c
/ 10);
1174 *q
++ = '0' + (c
% 10);
1176 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1179 *q
++ = keysym
& 0xff;
1180 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1181 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1187 vc_chr_write(s
->chr
, buf
, q
- buf
);
1190 if (be
&& be
->chr_read
) {
1191 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1198 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1199 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1200 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1201 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1202 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1203 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1204 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1205 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1206 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1207 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1208 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1211 static const int ctrl_qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1212 [Q_KEY_CODE_UP
] = QEMU_KEY_CTRL_UP
,
1213 [Q_KEY_CODE_DOWN
] = QEMU_KEY_CTRL_DOWN
,
1214 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_CTRL_RIGHT
,
1215 [Q_KEY_CODE_LEFT
] = QEMU_KEY_CTRL_LEFT
,
1216 [Q_KEY_CODE_HOME
] = QEMU_KEY_CTRL_HOME
,
1217 [Q_KEY_CODE_END
] = QEMU_KEY_CTRL_END
,
1218 [Q_KEY_CODE_PGUP
] = QEMU_KEY_CTRL_PAGEUP
,
1219 [Q_KEY_CODE_PGDN
] = QEMU_KEY_CTRL_PAGEDOWN
,
1222 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
)
1226 keysym
= ctrl
? ctrl_qcode_to_keysym
[qcode
] : qcode_to_keysym
[qcode
];
1230 kbd_put_keysym_console(s
, keysym
);
1234 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1238 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1239 kbd_put_keysym_console(s
, str
[i
]);
1243 void kbd_put_keysym(int keysym
)
1245 kbd_put_keysym_console(active_console
, keysym
);
1248 static void text_console_invalidate(void *opaque
)
1250 QemuConsole
*s
= (QemuConsole
*) opaque
;
1252 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1253 text_console_resize(s
);
1258 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1260 QemuConsole
*s
= (QemuConsole
*) opaque
;
1263 if (s
->text_x
[0] <= s
->text_x
[1]) {
1264 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1265 chardata
+= s
->text_y
[0] * s
->width
;
1266 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1267 for (j
= 0; j
< s
->width
; j
++, src
++) {
1268 console_write_ch(chardata
++,
1269 ATTR2CHTYPE(s
->cells
[src
].ch
,
1270 s
->cells
[src
].t_attrib
.fgcol
,
1271 s
->cells
[src
].t_attrib
.bgcol
,
1272 s
->cells
[src
].t_attrib
.bold
));
1274 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1275 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1276 s
->text_x
[0] = s
->width
;
1277 s
->text_y
[0] = s
->height
;
1281 if (s
->cursor_invalidate
) {
1282 dpy_text_cursor(s
, s
->x
, s
->y
);
1283 s
->cursor_invalidate
= 0;
1287 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1294 obj
= object_new(TYPE_QEMU_CONSOLE
);
1295 s
= QEMU_CONSOLE(obj
);
1297 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1298 (Object
**)&s
->device
,
1299 object_property_allow_set_link
,
1300 OBJ_PROP_LINK_STRONG
,
1302 object_property_add_uint32_ptr(obj
, "head",
1303 &s
->head
, &error_abort
);
1305 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1306 (console_type
== GRAPHIC_CONSOLE
))) {
1310 s
->console_type
= console_type
;
1312 if (QTAILQ_EMPTY(&consoles
)) {
1314 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1315 } else if (console_type
!= GRAPHIC_CONSOLE
|| qdev_hotplug
) {
1316 QemuConsole
*last
= QTAILQ_LAST(&consoles
);
1317 s
->index
= last
->index
+ 1;
1318 QTAILQ_INSERT_TAIL(&consoles
, s
, next
);
1321 * HACK: Put graphical consoles before text consoles.
1323 * Only do that for coldplugged devices. After initial device
1324 * initialization we will not renumber the consoles any more.
1326 QemuConsole
*c
= QTAILQ_FIRST(&consoles
);
1328 while (QTAILQ_NEXT(c
, next
) != NULL
&&
1329 c
->console_type
== GRAPHIC_CONSOLE
) {
1330 c
= QTAILQ_NEXT(c
, next
);
1332 if (c
->console_type
== GRAPHIC_CONSOLE
) {
1333 /* have no text consoles */
1334 s
->index
= c
->index
+ 1;
1335 QTAILQ_INSERT_AFTER(&consoles
, c
, s
, next
);
1337 s
->index
= c
->index
;
1338 QTAILQ_INSERT_BEFORE(c
, s
, next
);
1339 /* renumber text consoles */
1340 for (i
= s
->index
+ 1; c
!= NULL
; c
= QTAILQ_NEXT(c
, next
), i
++) {
1348 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1350 qemu_pixman_image_unref(surface
->image
);
1351 surface
->image
= NULL
;
1353 surface
->format
= PIXMAN_x8r8g8b8
;
1354 surface
->image
= pixman_image_create_bits(surface
->format
,
1357 assert(surface
->image
!= NULL
);
1359 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1362 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1364 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1366 trace_displaysurface_create(surface
, width
, height
);
1367 qemu_alloc_display(surface
, width
, height
);
1371 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1372 pixman_format_code_t format
,
1373 int linesize
, uint8_t *data
)
1375 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1377 trace_displaysurface_create_from(surface
, width
, height
, format
);
1378 surface
->format
= format
;
1379 surface
->image
= pixman_image_create_bits(surface
->format
,
1381 (void *)data
, linesize
);
1382 assert(surface
->image
!= NULL
);
1387 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1389 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1391 trace_displaysurface_create_pixman(surface
);
1392 surface
->format
= pixman_image_get_format(image
);
1393 surface
->image
= pixman_image_ref(image
);
1398 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1401 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1402 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1403 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1404 pixman_image_t
*glyph
;
1408 x
= (w
/ FONT_WIDTH
- len
) / 2;
1409 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1410 for (i
= 0; i
< len
; i
++) {
1411 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1412 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1413 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1414 qemu_pixman_image_unref(glyph
);
1419 void qemu_free_displaysurface(DisplaySurface
*surface
)
1421 if (surface
== NULL
) {
1424 trace_displaysurface_free(surface
);
1425 qemu_pixman_image_unref(surface
->image
);
1429 bool console_has_gl(QemuConsole
*con
)
1431 return con
->gl
!= NULL
;
1434 bool console_has_gl_dmabuf(QemuConsole
*con
)
1436 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1439 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1441 static const char nodev
[] =
1442 "This VM has no graphic display device.";
1443 static DisplaySurface
*dummy
;
1448 if (dcl
->ops
->dpy_gl_ctx_create
) {
1449 /* display has opengl support */
1452 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1453 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1459 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1460 dcl
->ds
= get_alloc_displaystate();
1461 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1462 gui_setup_refresh(dcl
->ds
);
1467 con
= active_console
;
1469 if (dcl
->ops
->dpy_gfx_switch
) {
1471 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1474 dummy
= qemu_create_message_surface(640, 480, nodev
);
1476 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1479 text_console_update_cursor(NULL
);
1482 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1485 DisplayState
*ds
= dcl
->ds
;
1487 dcl
->update_interval
= interval
;
1488 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1489 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1493 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1495 DisplayState
*ds
= dcl
->ds
;
1496 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1500 QLIST_REMOVE(dcl
, next
);
1502 gui_setup_refresh(ds
);
1505 static void dpy_set_ui_info_timer(void *opaque
)
1507 QemuConsole
*con
= opaque
;
1509 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1512 bool dpy_ui_info_supported(QemuConsole
*con
)
1514 return con
->hw_ops
->ui_info
!= NULL
;
1517 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1519 assert(con
!= NULL
);
1521 if (!dpy_ui_info_supported(con
)) {
1524 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1525 /* nothing changed -- ignore */
1530 * Typically we get a flood of these as the user resizes the window.
1531 * Wait until the dust has settled (one second without updates), then
1532 * go notify the guest.
1534 con
->ui_info
= *info
;
1535 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1539 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1541 DisplayState
*s
= con
->ds
;
1542 DisplayChangeListener
*dcl
;
1547 width
= surface_width(con
->surface
);
1548 height
= surface_height(con
->surface
);
1554 w
= MIN(w
, width
- x
);
1555 h
= MIN(h
, height
- y
);
1557 if (!qemu_console_is_visible(con
)) {
1560 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1561 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1564 if (dcl
->ops
->dpy_gfx_update
) {
1565 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1570 void dpy_gfx_update_full(QemuConsole
*con
)
1572 if (!con
->surface
) {
1575 dpy_gfx_update(con
, 0, 0,
1576 surface_width(con
->surface
),
1577 surface_height(con
->surface
));
1580 void dpy_gfx_replace_surface(QemuConsole
*con
,
1581 DisplaySurface
*surface
)
1583 DisplayState
*s
= con
->ds
;
1584 DisplaySurface
*old_surface
= con
->surface
;
1585 DisplayChangeListener
*dcl
;
1587 assert(old_surface
!= surface
|| surface
== NULL
);
1589 con
->surface
= surface
;
1590 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1591 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1594 if (dcl
->ops
->dpy_gfx_switch
) {
1595 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1598 qemu_free_displaysurface(old_surface
);
1601 bool dpy_gfx_check_format(QemuConsole
*con
,
1602 pixman_format_code_t format
)
1604 DisplayChangeListener
*dcl
;
1605 DisplayState
*s
= con
->ds
;
1607 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1608 if (dcl
->con
&& dcl
->con
!= con
) {
1609 /* dcl bound to another console -> skip */
1612 if (dcl
->ops
->dpy_gfx_check_format
) {
1613 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1617 /* default is to whitelist native 32 bpp only */
1618 if (format
!= qemu_default_pixman_format(32, true)) {
1626 static void dpy_refresh(DisplayState
*s
)
1628 DisplayChangeListener
*dcl
;
1630 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1631 if (dcl
->ops
->dpy_refresh
) {
1632 dcl
->ops
->dpy_refresh(dcl
);
1637 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1639 DisplayState
*s
= con
->ds
;
1640 DisplayChangeListener
*dcl
;
1642 if (!qemu_console_is_visible(con
)) {
1645 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1646 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1649 if (dcl
->ops
->dpy_text_cursor
) {
1650 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1655 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1657 DisplayState
*s
= con
->ds
;
1658 DisplayChangeListener
*dcl
;
1660 if (!qemu_console_is_visible(con
)) {
1663 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1664 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1667 if (dcl
->ops
->dpy_text_update
) {
1668 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1673 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1675 DisplayState
*s
= con
->ds
;
1676 DisplayChangeListener
*dcl
;
1678 if (!qemu_console_is_visible(con
)) {
1681 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1682 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1685 if (dcl
->ops
->dpy_text_resize
) {
1686 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1691 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1693 DisplayState
*s
= con
->ds
;
1694 DisplayChangeListener
*dcl
;
1696 if (!qemu_console_is_visible(con
)) {
1699 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1700 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1703 if (dcl
->ops
->dpy_mouse_set
) {
1704 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1709 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1711 DisplayState
*s
= con
->ds
;
1712 DisplayChangeListener
*dcl
;
1714 if (!qemu_console_is_visible(con
)) {
1717 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1718 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1721 if (dcl
->ops
->dpy_cursor_define
) {
1722 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1727 bool dpy_cursor_define_supported(QemuConsole
*con
)
1729 DisplayState
*s
= con
->ds
;
1730 DisplayChangeListener
*dcl
;
1732 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1733 if (dcl
->ops
->dpy_cursor_define
) {
1740 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1741 struct QEMUGLParams
*qparams
)
1744 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1747 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1750 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1753 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1756 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1759 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1762 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1765 void dpy_gl_scanout_disable(QemuConsole
*con
)
1768 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1769 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1771 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1776 void dpy_gl_scanout_texture(QemuConsole
*con
,
1777 uint32_t backing_id
,
1778 bool backing_y_0_top
,
1779 uint32_t backing_width
,
1780 uint32_t backing_height
,
1781 uint32_t x
, uint32_t y
,
1782 uint32_t width
, uint32_t height
)
1785 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1787 backing_width
, backing_height
,
1788 x
, y
, width
, height
);
1791 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1795 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1798 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1799 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1803 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1804 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1805 have_hot
, hot_x
, hot_y
);
1809 void dpy_gl_cursor_position(QemuConsole
*con
,
1810 uint32_t pos_x
, uint32_t pos_y
)
1814 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1815 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1819 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1824 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1825 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1829 void dpy_gl_update(QemuConsole
*con
,
1830 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1833 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1836 /***********************************************************/
1837 /* register display */
1839 /* console.c internal use only */
1840 static DisplayState
*get_alloc_displaystate(void)
1842 if (!display_state
) {
1843 display_state
= g_new0(DisplayState
, 1);
1844 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1845 text_console_update_cursor
, NULL
);
1847 return display_state
;
1851 * Called by main(), after creating QemuConsoles
1852 * and before initializing ui (sdl/vnc/...).
1854 DisplayState
*init_displaystate(void)
1859 get_alloc_displaystate();
1860 QTAILQ_FOREACH(con
, &consoles
, next
) {
1861 if (con
->console_type
!= GRAPHIC_CONSOLE
&&
1863 text_console_do_init(con
->chr
, display_state
);
1866 /* Hook up into the qom tree here (not in new_console()), once
1867 * all QemuConsoles are created and the order / numbering
1868 * doesn't change any more */
1869 name
= g_strdup_printf("console[%d]", con
->index
);
1870 object_property_add_child(container_get(object_get_root(), "/backend"),
1871 name
, OBJECT(con
), &error_abort
);
1875 return display_state
;
1878 void graphic_console_set_hwops(QemuConsole
*con
,
1879 const GraphicHwOps
*hw_ops
,
1882 con
->hw_ops
= hw_ops
;
1886 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1887 const GraphicHwOps
*hw_ops
,
1890 static const char noinit
[] =
1891 "Guest has not initialized the display (yet).";
1896 DisplaySurface
*surface
;
1898 ds
= get_alloc_displaystate();
1899 s
= qemu_console_lookup_unused();
1901 trace_console_gfx_reuse(s
->index
);
1903 width
= surface_width(s
->surface
);
1904 height
= surface_height(s
->surface
);
1907 trace_console_gfx_new();
1908 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1909 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1910 dpy_set_ui_info_timer
, s
);
1912 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1914 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1918 surface
= qemu_create_message_surface(width
, height
, noinit
);
1919 dpy_gfx_replace_surface(s
, surface
);
1923 static const GraphicHwOps unused_ops
= {
1927 void graphic_console_close(QemuConsole
*con
)
1929 static const char unplugged
[] =
1930 "Guest display has been unplugged";
1931 DisplaySurface
*surface
;
1936 width
= surface_width(con
->surface
);
1937 height
= surface_height(con
->surface
);
1940 trace_console_gfx_close(con
->index
);
1941 object_property_set_link(OBJECT(con
), NULL
, "device", &error_abort
);
1942 graphic_console_set_hwops(con
, &unused_ops
, NULL
);
1945 dpy_gl_scanout_disable(con
);
1947 surface
= qemu_create_message_surface(width
, height
, unplugged
);
1948 dpy_gfx_replace_surface(con
, surface
);
1951 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1955 QTAILQ_FOREACH(con
, &consoles
, next
) {
1956 if (con
->index
== index
) {
1963 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1969 QTAILQ_FOREACH(con
, &consoles
, next
) {
1970 obj
= object_property_get_link(OBJECT(con
),
1971 "device", &error_abort
);
1972 if (DEVICE(obj
) != dev
) {
1975 h
= object_property_get_uint(OBJECT(con
),
1976 "head", &error_abort
);
1985 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1986 uint32_t head
, Error
**errp
)
1991 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1993 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1994 "Device '%s' not found", device_id
);
1998 con
= qemu_console_lookup_by_device(dev
, head
);
2000 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
2008 QemuConsole
*qemu_console_lookup_unused(void)
2013 QTAILQ_FOREACH(con
, &consoles
, next
) {
2014 if (con
->hw_ops
!= &unused_ops
) {
2017 obj
= object_property_get_link(OBJECT(con
),
2018 "device", &error_abort
);
2027 bool qemu_console_is_visible(QemuConsole
*con
)
2029 return (con
== active_console
) || (con
->dcls
> 0);
2032 bool qemu_console_is_graphic(QemuConsole
*con
)
2035 con
= active_console
;
2037 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
2040 bool qemu_console_is_fixedsize(QemuConsole
*con
)
2043 con
= active_console
;
2045 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
2048 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
2050 assert(con
!= NULL
);
2051 return con
->gl_block
;
2054 char *qemu_console_get_label(QemuConsole
*con
)
2056 if (con
->console_type
== GRAPHIC_CONSOLE
) {
2058 return g_strdup(object_get_typename(con
->device
));
2060 return g_strdup("VGA");
2062 if (con
->chr
&& con
->chr
->label
) {
2063 return g_strdup(con
->chr
->label
);
2065 return g_strdup_printf("vc%d", con
->index
);
2069 int qemu_console_get_index(QemuConsole
*con
)
2072 con
= active_console
;
2074 return con
? con
->index
: -1;
2077 uint32_t qemu_console_get_head(QemuConsole
*con
)
2080 con
= active_console
;
2082 return con
? con
->head
: -1;
2085 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
2087 assert(con
!= NULL
);
2088 return &con
->ui_info
;
2091 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2094 con
= active_console
;
2096 return con
? surface_width(con
->surface
) : fallback
;
2099 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2102 con
= active_console
;
2104 return con
? surface_height(con
->surface
) : fallback
;
2107 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2109 VCChardev
*drv
= VC_CHARDEV(chr
);
2110 QemuConsole
*s
= drv
->console
;
2115 static void text_console_update_cursor_timer(void)
2117 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2118 + CONSOLE_CURSOR_PERIOD
/ 2);
2121 static void text_console_update_cursor(void *opaque
)
2126 cursor_visible_phase
= !cursor_visible_phase
;
2128 QTAILQ_FOREACH(s
, &consoles
, next
) {
2129 if (qemu_console_is_graphic(s
) ||
2130 !qemu_console_is_visible(s
)) {
2134 graphic_hw_invalidate(s
);
2138 text_console_update_cursor_timer();
2142 static const GraphicHwOps text_console_ops
= {
2143 .invalidate
= text_console_invalidate
,
2144 .text_update
= text_console_update
,
2147 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2149 VCChardev
*drv
= VC_CHARDEV(chr
);
2150 QemuConsole
*s
= drv
->console
;
2151 int g_width
= 80 * FONT_WIDTH
;
2152 int g_height
= 24 * FONT_HEIGHT
;
2154 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2155 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2156 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2161 s
->total_height
= DEFAULT_BACKSCROLL
;
2165 if (active_console
&& active_console
->surface
) {
2166 g_width
= surface_width(active_console
->surface
);
2167 g_height
= surface_height(active_console
->surface
);
2169 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2172 s
->hw_ops
= &text_console_ops
;
2175 /* Set text attribute defaults */
2176 s
->t_attrib_default
.bold
= 0;
2177 s
->t_attrib_default
.uline
= 0;
2178 s
->t_attrib_default
.blink
= 0;
2179 s
->t_attrib_default
.invers
= 0;
2180 s
->t_attrib_default
.unvisible
= 0;
2181 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2182 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2183 /* set current text attributes to default */
2184 s
->t_attrib
= s
->t_attrib_default
;
2185 text_console_resize(s
);
2191 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2192 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2193 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2194 s
->t_attrib
= s
->t_attrib_default
;
2197 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2200 static void vc_chr_open(Chardev
*chr
,
2201 ChardevBackend
*backend
,
2205 ChardevVC
*vc
= backend
->u
.vc
.data
;
2206 VCChardev
*drv
= VC_CHARDEV(chr
);
2209 unsigned height
= 0;
2211 if (vc
->has_width
) {
2213 } else if (vc
->has_cols
) {
2214 width
= vc
->cols
* FONT_WIDTH
;
2217 if (vc
->has_height
) {
2218 height
= vc
->height
;
2219 } else if (vc
->has_rows
) {
2220 height
= vc
->rows
* FONT_HEIGHT
;
2223 trace_console_txt_new(width
, height
);
2224 if (width
== 0 || height
== 0) {
2225 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2227 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2228 s
->surface
= qemu_create_displaysurface(width
, height
);
2232 error_setg(errp
, "cannot create text console");
2239 if (display_state
) {
2240 text_console_do_init(chr
, display_state
);
2243 /* console/chardev init sometimes completes elsewhere in a 2nd
2244 * stage, so defer OPENED events until they are fully initialized
2249 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2251 DisplaySurface
*surface
;
2253 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2255 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2256 pixman_image_get_width(s
->surface
->image
) == width
&&
2257 pixman_image_get_height(s
->surface
->image
) == height
) {
2261 surface
= qemu_create_displaysurface(width
, height
);
2262 dpy_gfx_replace_surface(s
, surface
);
2265 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2267 return console
->surface
;
2270 PixelFormat
qemu_default_pixelformat(int bpp
)
2272 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2273 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2277 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2279 void qemu_display_register(QemuDisplay
*ui
)
2281 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2282 dpys
[ui
->type
] = ui
;
2285 bool qemu_display_find_default(DisplayOptions
*opts
)
2287 static DisplayType prio
[] = {
2294 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2295 if (dpys
[prio
[i
]] == NULL
) {
2296 ui_module_load_one(DisplayType_str(prio
[i
]));
2298 if (dpys
[prio
[i
]] == NULL
) {
2301 opts
->type
= prio
[i
];
2307 void qemu_display_early_init(DisplayOptions
*opts
)
2309 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2310 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2313 if (dpys
[opts
->type
] == NULL
) {
2314 ui_module_load_one(DisplayType_str(opts
->type
));
2316 if (dpys
[opts
->type
] == NULL
) {
2317 error_report("Display '%s' is not available.",
2318 DisplayType_str(opts
->type
));
2321 if (dpys
[opts
->type
]->early_init
) {
2322 dpys
[opts
->type
]->early_init(opts
);
2326 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2328 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2329 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2332 assert(dpys
[opts
->type
] != NULL
);
2333 dpys
[opts
->type
]->init(ds
, opts
);
2336 void qemu_display_help(void)
2340 printf("Available display backend types:\n");
2342 for (idx
= DISPLAY_TYPE_NONE
; idx
< DISPLAY_TYPE__MAX
; idx
++) {
2344 ui_module_load_one(DisplayType_str(idx
));
2347 printf("%s\n", DisplayType_str(dpys
[idx
]->type
));
2352 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2357 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2358 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2359 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2361 val
= qemu_opt_get_number(opts
, "width", 0);
2363 vc
->has_width
= true;
2367 val
= qemu_opt_get_number(opts
, "height", 0);
2369 vc
->has_height
= true;
2373 val
= qemu_opt_get_number(opts
, "cols", 0);
2375 vc
->has_cols
= true;
2379 val
= qemu_opt_get_number(opts
, "rows", 0);
2381 vc
->has_rows
= true;
2386 static const TypeInfo qemu_console_info
= {
2387 .name
= TYPE_QEMU_CONSOLE
,
2388 .parent
= TYPE_OBJECT
,
2389 .instance_size
= sizeof(QemuConsole
),
2390 .class_size
= sizeof(QemuConsoleClass
),
2393 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2395 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2397 cc
->parse
= qemu_chr_parse_vc
;
2398 cc
->open
= vc_chr_open
;
2399 cc
->chr_write
= vc_chr_write
;
2400 cc
->chr_set_echo
= vc_chr_set_echo
;
2403 static const TypeInfo char_vc_type_info
= {
2404 .name
= TYPE_CHARDEV_VC
,
2405 .parent
= TYPE_CHARDEV
,
2406 .instance_size
= sizeof(VCChardev
),
2407 .class_init
= char_vc_class_init
,
2410 void qemu_console_early_init(void)
2412 /* set the default vc driver */
2413 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2414 type_register(&char_vc_type_info
);
2418 static void register_types(void)
2420 type_register_static(&qemu_console_info
);
2423 type_init(register_types
);