2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-ui.h"
30 #include "qemu/option.h"
31 #include "qemu/timer.h"
32 #include "chardev/char-fe.h"
34 #include "exec/memory.h"
36 #define DEFAULT_BACKSCROLL 512
37 #define CONSOLE_CURSOR_PERIOD 500
39 typedef struct TextAttributes
{
49 typedef struct TextCell
{
51 TextAttributes t_attrib
;
54 #define MAX_ESC_PARAMS 3
62 typedef struct QEMUFIFO
{
65 int count
, wptr
, rptr
;
68 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
72 l
= f
->buf_size
- f
->count
;
77 l
= f
->buf_size
- f
->wptr
;
80 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
82 if (f
->wptr
>= f
->buf_size
)
91 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
99 l
= f
->buf_size
- f
->rptr
;
102 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
104 if (f
->rptr
>= f
->buf_size
)
116 TEXT_CONSOLE_FIXED_SIZE
123 console_type_t console_type
;
125 DisplaySurface
*surface
;
127 DisplayChangeListener
*gl
;
131 /* Graphic console state. */
136 const GraphicHwOps
*hw_ops
;
139 /* Text console state */
143 int backscroll_height
;
145 int x_saved
, y_saved
;
148 TextAttributes t_attrib_default
; /* default text attributes */
149 TextAttributes t_attrib
; /* currently active text attributes */
151 int text_x
[2], text_y
[2], cursor_invalidate
;
160 int esc_params
[MAX_ESC_PARAMS
];
164 /* fifo for key pressed */
166 uint8_t out_fifo_buf
[16];
167 QEMUTimer
*kbd_timer
;
170 struct DisplayState
{
171 QEMUTimer
*gui_timer
;
172 uint64_t last_update
;
173 uint64_t update_interval
;
178 QLIST_HEAD(, DisplayChangeListener
) listeners
;
181 static DisplayState
*display_state
;
182 static QemuConsole
*active_console
;
183 static QemuConsole
**consoles
;
184 static int nb_consoles
= 0;
185 static bool cursor_visible_phase
;
186 static QEMUTimer
*cursor_timer
;
188 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
189 static void dpy_refresh(DisplayState
*s
);
190 static DisplayState
*get_alloc_displaystate(void);
191 static void text_console_update_cursor_timer(void);
192 static void text_console_update_cursor(void *opaque
);
194 static void gui_update(void *opaque
)
196 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
197 uint64_t dcl_interval
;
198 DisplayState
*ds
= opaque
;
199 DisplayChangeListener
*dcl
;
202 ds
->refreshing
= true;
204 ds
->refreshing
= false;
206 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
207 dcl_interval
= dcl
->update_interval
?
208 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
209 if (interval
> dcl_interval
) {
210 interval
= dcl_interval
;
213 if (ds
->update_interval
!= interval
) {
214 ds
->update_interval
= interval
;
215 for (i
= 0; i
< nb_consoles
; i
++) {
216 if (consoles
[i
]->hw_ops
->update_interval
) {
217 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
220 trace_console_refresh(interval
);
222 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
223 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
226 static void gui_setup_refresh(DisplayState
*ds
)
228 DisplayChangeListener
*dcl
;
229 bool need_timer
= false;
230 bool have_gfx
= false;
231 bool have_text
= false;
233 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
234 if (dcl
->ops
->dpy_refresh
!= NULL
) {
237 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
240 if (dcl
->ops
->dpy_text_update
!= NULL
) {
245 if (need_timer
&& ds
->gui_timer
== NULL
) {
246 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
247 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
249 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
250 timer_del(ds
->gui_timer
);
251 timer_free(ds
->gui_timer
);
252 ds
->gui_timer
= NULL
;
255 ds
->have_gfx
= have_gfx
;
256 ds
->have_text
= have_text
;
259 void graphic_hw_update(QemuConsole
*con
)
262 con
= active_console
;
264 if (con
&& con
->hw_ops
->gfx_update
) {
265 con
->hw_ops
->gfx_update(con
->hw
);
269 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
273 con
->gl_block
= block
;
274 if (con
->hw_ops
->gl_block
) {
275 con
->hw_ops
->gl_block(con
->hw
, block
);
279 int qemu_console_get_window_id(QemuConsole
*con
)
281 return con
->window_id
;
284 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
286 con
->window_id
= window_id
;
289 void graphic_hw_invalidate(QemuConsole
*con
)
292 con
= active_console
;
294 if (con
&& con
->hw_ops
->invalidate
) {
295 con
->hw_ops
->invalidate(con
->hw
);
299 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
302 int width
= pixman_image_get_width(ds
->image
);
303 int height
= pixman_image_get_height(ds
->image
);
308 pixman_image_t
*linebuf
;
310 trace_ppm_save(filename
, ds
);
311 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
313 error_setg(errp
, "failed to open file '%s': %s", filename
,
317 f
= fdopen(fd
, "wb");
318 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
323 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
324 for (y
= 0; y
< height
; y
++) {
325 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
327 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
328 pixman_image_get_stride(linebuf
), f
);
336 qemu_pixman_image_unref(linebuf
);
341 error_setg(errp
, "failed to write to file '%s': %s", filename
,
347 void qmp_screendump(const char *filename
, Error
**errp
)
349 QemuConsole
*con
= qemu_console_lookup_by_index(0);
350 DisplaySurface
*surface
;
353 error_setg(errp
, "There is no QemuConsole I can screendump from.");
357 graphic_hw_update(con
);
358 surface
= qemu_console_surface(con
);
359 ppm_save(filename
, surface
, errp
);
362 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
365 con
= active_console
;
367 if (con
&& con
->hw_ops
->text_update
) {
368 con
->hw_ops
->text_update(con
->hw
, chardata
);
372 static void vga_fill_rect(QemuConsole
*con
,
373 int posx
, int posy
, int width
, int height
,
374 pixman_color_t color
)
376 DisplaySurface
*surface
= qemu_console_surface(con
);
377 pixman_rectangle16_t rect
= {
378 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
381 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
385 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
386 static void vga_bitblt(QemuConsole
*con
,
387 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
389 DisplaySurface
*surface
= qemu_console_surface(con
);
391 pixman_image_composite(PIXMAN_OP_SRC
,
392 surface
->image
, NULL
, surface
->image
,
393 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
396 /***********************************************************/
397 /* basic char display */
399 #define FONT_HEIGHT 16
404 #define QEMU_RGB(r, g, b) \
405 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
407 static const pixman_color_t color_table_rgb
[2][8] = {
409 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
410 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
411 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
412 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
413 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
414 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
415 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
416 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
419 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
420 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
421 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
422 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
423 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
424 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
425 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
426 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
430 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
431 TextAttributes
*t_attrib
)
433 static pixman_image_t
*glyphs
[256];
434 DisplaySurface
*surface
= qemu_console_surface(s
);
435 pixman_color_t fgcol
, bgcol
;
437 if (t_attrib
->invers
) {
438 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
439 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
441 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
442 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
446 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
448 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
449 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
452 static void text_console_resize(QemuConsole
*s
)
454 TextCell
*cells
, *c
, *c1
;
455 int w1
, x
, y
, last_width
;
457 last_width
= s
->width
;
458 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
459 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
465 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
466 for(y
= 0; y
< s
->total_height
; y
++) {
467 c
= &cells
[y
* s
->width
];
469 c1
= &s
->cells
[y
* last_width
];
470 for(x
= 0; x
< w1
; x
++) {
474 for(x
= w1
; x
< s
->width
; x
++) {
476 c
->t_attrib
= s
->t_attrib_default
;
484 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
486 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
487 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
488 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
489 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
492 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
494 if (!qemu_console_is_visible(s
)) {
497 if (s
->update_x0
> x
* FONT_WIDTH
)
498 s
->update_x0
= x
* FONT_WIDTH
;
499 if (s
->update_y0
> y
* FONT_HEIGHT
)
500 s
->update_y0
= y
* FONT_HEIGHT
;
501 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
502 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
503 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
504 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
507 static void update_xy(QemuConsole
*s
, int x
, int y
)
512 if (s
->ds
->have_text
) {
513 text_update_xy(s
, x
, y
);
516 y1
= (s
->y_base
+ y
) % s
->total_height
;
517 y2
= y1
- s
->y_displayed
;
519 y2
+= s
->total_height
;
521 if (y2
< s
->height
) {
522 c
= &s
->cells
[y1
* s
->width
+ x
];
523 vga_putcharxy(s
, x
, y2
, c
->ch
,
525 invalidate_xy(s
, x
, y2
);
529 static void console_show_cursor(QemuConsole
*s
, int show
)
535 if (s
->ds
->have_text
) {
536 s
->cursor_invalidate
= 1;
542 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
543 y
= y1
- s
->y_displayed
;
545 y
+= s
->total_height
;
548 c
= &s
->cells
[y1
* s
->width
+ x
];
549 if (show
&& cursor_visible_phase
) {
550 TextAttributes t_attrib
= s
->t_attrib_default
;
551 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
552 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
554 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
556 invalidate_xy(s
, x
, y
);
560 static void console_refresh(QemuConsole
*s
)
562 DisplaySurface
*surface
= qemu_console_surface(s
);
566 if (s
->ds
->have_text
) {
569 s
->text_x
[1] = s
->width
- 1;
570 s
->text_y
[1] = s
->height
- 1;
571 s
->cursor_invalidate
= 1;
574 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
575 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
577 for (y
= 0; y
< s
->height
; y
++) {
578 c
= s
->cells
+ y1
* s
->width
;
579 for (x
= 0; x
< s
->width
; x
++) {
580 vga_putcharxy(s
, x
, y
, c
->ch
,
584 if (++y1
== s
->total_height
) {
588 console_show_cursor(s
, 1);
589 dpy_gfx_update(s
, 0, 0,
590 surface_width(surface
), surface_height(surface
));
593 static void console_scroll(QemuConsole
*s
, int ydelta
)
598 for(i
= 0; i
< ydelta
; i
++) {
599 if (s
->y_displayed
== s
->y_base
)
601 if (++s
->y_displayed
== s
->total_height
)
606 i
= s
->backscroll_height
;
607 if (i
> s
->total_height
- s
->height
)
608 i
= s
->total_height
- s
->height
;
611 y1
+= s
->total_height
;
612 for(i
= 0; i
< ydelta
; i
++) {
613 if (s
->y_displayed
== y1
)
615 if (--s
->y_displayed
< 0)
616 s
->y_displayed
= s
->total_height
- 1;
622 static void console_put_lf(QemuConsole
*s
)
628 if (s
->y
>= s
->height
) {
629 s
->y
= s
->height
- 1;
631 if (s
->y_displayed
== s
->y_base
) {
632 if (++s
->y_displayed
== s
->total_height
)
635 if (++s
->y_base
== s
->total_height
)
637 if (s
->backscroll_height
< s
->total_height
)
638 s
->backscroll_height
++;
639 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
640 c
= &s
->cells
[y1
* s
->width
];
641 for(x
= 0; x
< s
->width
; x
++) {
643 c
->t_attrib
= s
->t_attrib_default
;
646 if (s
->y_displayed
== s
->y_base
) {
647 if (s
->ds
->have_text
) {
650 s
->text_x
[1] = s
->width
- 1;
651 s
->text_y
[1] = s
->height
- 1;
654 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
655 s
->width
* FONT_WIDTH
,
656 (s
->height
- 1) * FONT_HEIGHT
);
657 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
658 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
659 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
662 s
->update_x1
= s
->width
* FONT_WIDTH
;
663 s
->update_y1
= s
->height
* FONT_HEIGHT
;
668 /* Set console attributes depending on the current escape codes.
669 * NOTE: I know this code is not very efficient (checking every color for it
670 * self) but it is more readable and better maintainable.
672 static void console_handle_escape(QemuConsole
*s
)
676 for (i
=0; i
<s
->nb_esc_params
; i
++) {
677 switch (s
->esc_params
[i
]) {
678 case 0: /* reset all console attributes to default */
679 s
->t_attrib
= s
->t_attrib_default
;
682 s
->t_attrib
.bold
= 1;
685 s
->t_attrib
.uline
= 1;
688 s
->t_attrib
.blink
= 1;
691 s
->t_attrib
.invers
= 1;
694 s
->t_attrib
.unvisible
= 1;
697 s
->t_attrib
.bold
= 0;
700 s
->t_attrib
.uline
= 0;
703 s
->t_attrib
.blink
= 0;
706 s
->t_attrib
.invers
= 0;
709 s
->t_attrib
.unvisible
= 0;
711 /* set foreground color */
713 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
716 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
719 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
722 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
725 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
728 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
731 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
734 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
736 /* set background color */
738 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
741 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
744 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
747 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
750 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
753 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
756 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
759 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
765 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
767 int y1
= (s
->y_base
+ y
) % s
->total_height
;
768 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
770 c
->t_attrib
= s
->t_attrib_default
;
774 static void console_put_one(QemuConsole
*s
, int ch
)
778 if (s
->x
>= s
->width
) {
783 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
784 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
786 c
->t_attrib
= s
->t_attrib
;
787 update_xy(s
, s
->x
, s
->y
);
791 static void console_respond_str(QemuConsole
*s
, const char *buf
)
794 console_put_one(s
, *buf
);
799 /* set cursor, checking bounds */
800 static void set_cursor(QemuConsole
*s
, int x
, int y
)
808 if (y
>= s
->height
) {
819 static void console_putchar(QemuConsole
*s
, int ch
)
828 case '\r': /* carriage return */
831 case '\n': /* newline */
834 case '\b': /* backspace */
838 case '\t': /* tabspace */
839 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
843 s
->x
= s
->x
+ (8 - (s
->x
% 8));
846 case '\a': /* alert aka. bell */
847 /* TODO: has to be implemented */
850 /* SI (shift in), character set 0 (ignored) */
853 /* SO (shift out), character set 1 (ignored) */
855 case 27: /* esc (introducing an escape sequence) */
856 s
->state
= TTY_STATE_ESC
;
859 console_put_one(s
, ch
);
863 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
865 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
866 s
->esc_params
[i
] = 0;
867 s
->nb_esc_params
= 0;
868 s
->state
= TTY_STATE_CSI
;
870 s
->state
= TTY_STATE_NORM
;
873 case TTY_STATE_CSI
: /* handle escape sequence parameters */
874 if (ch
>= '0' && ch
<= '9') {
875 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
876 int *param
= &s
->esc_params
[s
->nb_esc_params
];
877 int digit
= (ch
- '0');
879 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
880 *param
* 10 + digit
: INT_MAX
;
883 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
885 if (ch
== ';' || ch
== '?') {
888 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
889 ch
, s
->nb_esc_params
);
890 s
->state
= TTY_STATE_NORM
;
894 if (s
->esc_params
[0] == 0) {
895 s
->esc_params
[0] = 1;
897 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
900 /* move cursor down */
901 if (s
->esc_params
[0] == 0) {
902 s
->esc_params
[0] = 1;
904 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
907 /* move cursor right */
908 if (s
->esc_params
[0] == 0) {
909 s
->esc_params
[0] = 1;
911 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
914 /* move cursor left */
915 if (s
->esc_params
[0] == 0) {
916 s
->esc_params
[0] = 1;
918 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
921 /* move cursor to column */
922 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
926 /* move cursor to row, column */
927 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
930 switch (s
->esc_params
[0]) {
932 /* clear to end of screen */
933 for (y
= s
->y
; y
< s
->height
; y
++) {
934 for (x
= 0; x
< s
->width
; x
++) {
935 if (y
== s
->y
&& x
< s
->x
) {
938 console_clear_xy(s
, x
, y
);
943 /* clear from beginning of screen */
944 for (y
= 0; y
<= s
->y
; y
++) {
945 for (x
= 0; x
< s
->width
; x
++) {
946 if (y
== s
->y
&& x
> s
->x
) {
949 console_clear_xy(s
, x
, y
);
954 /* clear entire screen */
955 for (y
= 0; y
<= s
->height
; y
++) {
956 for (x
= 0; x
< s
->width
; x
++) {
957 console_clear_xy(s
, x
, y
);
964 switch (s
->esc_params
[0]) {
967 for(x
= s
->x
; x
< s
->width
; x
++) {
968 console_clear_xy(s
, x
, s
->y
);
972 /* clear from beginning of line */
973 for (x
= 0; x
<= s
->x
; x
++) {
974 console_clear_xy(s
, x
, s
->y
);
978 /* clear entire line */
979 for(x
= 0; x
< s
->width
; x
++) {
980 console_clear_xy(s
, x
, s
->y
);
986 console_handle_escape(s
);
989 switch (s
->esc_params
[0]) {
991 /* report console status (always succeed)*/
992 console_respond_str(s
, "\033[0n");
995 /* report cursor position */
996 sprintf(response
, "\033[%d;%dR",
997 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
999 console_respond_str(s
, response
);
1004 /* save cursor position */
1009 /* restore cursor position */
1014 trace_console_putchar_unhandled(ch
);
1022 void console_select(unsigned int index
)
1024 DisplayChangeListener
*dcl
;
1027 trace_console_select(index
);
1028 s
= qemu_console_lookup_by_index(index
);
1030 DisplayState
*ds
= s
->ds
;
1034 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1035 if (dcl
->con
!= NULL
) {
1038 if (dcl
->ops
->dpy_gfx_switch
) {
1039 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1043 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1044 surface_height(s
->surface
));
1047 if (ds
->have_text
) {
1048 dpy_text_resize(s
, s
->width
, s
->height
);
1050 text_console_update_cursor(NULL
);
1054 typedef struct VCChardev
{
1056 QemuConsole
*console
;
1059 #define TYPE_CHARDEV_VC "chardev-vc"
1060 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1062 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1064 VCChardev
*drv
= VC_CHARDEV(chr
);
1065 QemuConsole
*s
= drv
->console
;
1072 s
->update_x0
= s
->width
* FONT_WIDTH
;
1073 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1076 console_show_cursor(s
, 0);
1077 for(i
= 0; i
< len
; i
++) {
1078 console_putchar(s
, buf
[i
]);
1080 console_show_cursor(s
, 1);
1081 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1082 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1083 s
->update_x1
- s
->update_x0
,
1084 s
->update_y1
- s
->update_y0
);
1089 static void kbd_send_chars(void *opaque
)
1091 QemuConsole
*s
= opaque
;
1095 len
= qemu_chr_be_can_write(s
->chr
);
1096 if (len
> s
->out_fifo
.count
)
1097 len
= s
->out_fifo
.count
;
1099 if (len
> sizeof(buf
))
1101 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1102 qemu_chr_be_write(s
->chr
, buf
, len
);
1104 /* characters are pending: we send them a bit later (XXX:
1105 horrible, should change char device API) */
1106 if (s
->out_fifo
.count
> 0) {
1107 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1111 /* called when an ascii key is pressed */
1112 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1114 uint8_t buf
[16], *q
;
1118 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1122 case QEMU_KEY_CTRL_UP
:
1123 console_scroll(s
, -1);
1125 case QEMU_KEY_CTRL_DOWN
:
1126 console_scroll(s
, 1);
1128 case QEMU_KEY_CTRL_PAGEUP
:
1129 console_scroll(s
, -10);
1131 case QEMU_KEY_CTRL_PAGEDOWN
:
1132 console_scroll(s
, 10);
1135 /* convert the QEMU keysym to VT100 key string */
1137 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1140 c
= keysym
- 0xe100;
1142 *q
++ = '0' + (c
/ 10);
1143 *q
++ = '0' + (c
% 10);
1145 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1148 *q
++ = keysym
& 0xff;
1149 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1150 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1156 vc_chr_write(s
->chr
, buf
, q
- buf
);
1159 if (be
&& be
->chr_read
) {
1160 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1167 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1168 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1169 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1170 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1171 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1172 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1173 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1174 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1175 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1176 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1177 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1180 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1184 keysym
= qcode_to_keysym
[qcode
];
1188 kbd_put_keysym_console(s
, keysym
);
1192 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1196 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1197 kbd_put_keysym_console(s
, str
[i
]);
1201 void kbd_put_keysym(int keysym
)
1203 kbd_put_keysym_console(active_console
, keysym
);
1206 static void text_console_invalidate(void *opaque
)
1208 QemuConsole
*s
= (QemuConsole
*) opaque
;
1210 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1211 text_console_resize(s
);
1216 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1218 QemuConsole
*s
= (QemuConsole
*) opaque
;
1221 if (s
->text_x
[0] <= s
->text_x
[1]) {
1222 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1223 chardata
+= s
->text_y
[0] * s
->width
;
1224 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1225 for (j
= 0; j
< s
->width
; j
++, src
++) {
1226 console_write_ch(chardata
++,
1227 ATTR2CHTYPE(s
->cells
[src
].ch
,
1228 s
->cells
[src
].t_attrib
.fgcol
,
1229 s
->cells
[src
].t_attrib
.bgcol
,
1230 s
->cells
[src
].t_attrib
.bold
));
1232 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1233 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1234 s
->text_x
[0] = s
->width
;
1235 s
->text_y
[0] = s
->height
;
1239 if (s
->cursor_invalidate
) {
1240 dpy_text_cursor(s
, s
->x
, s
->y
);
1241 s
->cursor_invalidate
= 0;
1245 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1252 obj
= object_new(TYPE_QEMU_CONSOLE
);
1253 s
= QEMU_CONSOLE(obj
);
1255 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1256 (Object
**)&s
->device
,
1257 object_property_allow_set_link
,
1258 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1260 object_property_add_uint32_ptr(obj
, "head",
1261 &s
->head
, &error_abort
);
1263 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1264 (console_type
== GRAPHIC_CONSOLE
))) {
1268 s
->console_type
= console_type
;
1270 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1271 if (console_type
!= GRAPHIC_CONSOLE
) {
1272 s
->index
= nb_consoles
;
1273 consoles
[nb_consoles
++] = s
;
1275 /* HACK: Put graphical consoles before text consoles. */
1276 for (i
= nb_consoles
; i
> 0; i
--) {
1277 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1279 consoles
[i
] = consoles
[i
- 1];
1280 consoles
[i
]->index
= i
;
1289 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1291 qemu_pixman_image_unref(surface
->image
);
1292 surface
->image
= NULL
;
1294 surface
->format
= PIXMAN_x8r8g8b8
;
1295 surface
->image
= pixman_image_create_bits(surface
->format
,
1298 assert(surface
->image
!= NULL
);
1300 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1303 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1305 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1307 trace_displaysurface_create(surface
, width
, height
);
1308 qemu_alloc_display(surface
, width
, height
);
1312 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1313 pixman_format_code_t format
,
1314 int linesize
, uint8_t *data
)
1316 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1318 trace_displaysurface_create_from(surface
, width
, height
, format
);
1319 surface
->format
= format
;
1320 surface
->image
= pixman_image_create_bits(surface
->format
,
1322 (void *)data
, linesize
);
1323 assert(surface
->image
!= NULL
);
1328 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1330 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1332 trace_displaysurface_create_pixman(surface
);
1333 surface
->format
= pixman_image_get_format(image
);
1334 surface
->image
= pixman_image_ref(image
);
1339 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1342 void *data
= pixman_image_get_data(image
);
1343 uint32_t size
= pixman_image_get_stride(image
) *
1344 pixman_image_get_height(image
);
1345 cpu_physical_memory_unmap(data
, size
, 0, 0);
1348 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1349 pixman_format_code_t format
,
1350 int linesize
, uint64_t addr
)
1352 DisplaySurface
*surface
;
1356 if (linesize
== 0) {
1357 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1360 size
= (hwaddr
)linesize
* height
;
1361 data
= cpu_physical_memory_map(addr
, &size
, 0);
1362 if (size
!= (hwaddr
)linesize
* height
) {
1363 cpu_physical_memory_unmap(data
, size
, 0, 0);
1367 surface
= qemu_create_displaysurface_from
1368 (width
, height
, format
, linesize
, data
);
1369 pixman_image_set_destroy_function
1370 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1375 DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1378 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1379 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1380 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1381 pixman_image_t
*glyph
;
1385 x
= (w
/ FONT_WIDTH
- len
) / 2;
1386 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1387 for (i
= 0; i
< len
; i
++) {
1388 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1389 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1390 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1391 qemu_pixman_image_unref(glyph
);
1396 void qemu_free_displaysurface(DisplaySurface
*surface
)
1398 if (surface
== NULL
) {
1401 trace_displaysurface_free(surface
);
1402 qemu_pixman_image_unref(surface
->image
);
1406 bool console_has_gl(QemuConsole
*con
)
1408 return con
->gl
!= NULL
;
1411 bool console_has_gl_dmabuf(QemuConsole
*con
)
1413 return con
->gl
!= NULL
&& con
->gl
->ops
->dpy_gl_scanout_dmabuf
!= NULL
;
1416 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1418 static const char nodev
[] =
1419 "This VM has no graphic display device.";
1420 static DisplaySurface
*dummy
;
1425 if (dcl
->ops
->dpy_gl_ctx_create
) {
1426 /* display has opengl support */
1429 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1430 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1436 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1437 dcl
->ds
= get_alloc_displaystate();
1438 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1439 gui_setup_refresh(dcl
->ds
);
1444 con
= active_console
;
1446 if (dcl
->ops
->dpy_gfx_switch
) {
1448 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1451 dummy
= qemu_create_message_surface(640, 480, nodev
);
1453 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1456 text_console_update_cursor(NULL
);
1459 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1462 DisplayState
*ds
= dcl
->ds
;
1464 dcl
->update_interval
= interval
;
1465 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1466 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1470 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1472 DisplayState
*ds
= dcl
->ds
;
1473 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1477 QLIST_REMOVE(dcl
, next
);
1479 gui_setup_refresh(ds
);
1482 static void dpy_set_ui_info_timer(void *opaque
)
1484 QemuConsole
*con
= opaque
;
1486 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1489 bool dpy_ui_info_supported(QemuConsole
*con
)
1491 return con
->hw_ops
->ui_info
!= NULL
;
1494 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1496 assert(con
!= NULL
);
1498 if (!dpy_ui_info_supported(con
)) {
1501 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1502 /* nothing changed -- ignore */
1507 * Typically we get a flood of these as the user resizes the window.
1508 * Wait until the dust has settled (one second without updates), then
1509 * go notify the guest.
1511 con
->ui_info
= *info
;
1512 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1516 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1518 DisplayState
*s
= con
->ds
;
1519 DisplayChangeListener
*dcl
;
1524 width
= surface_width(con
->surface
);
1525 height
= surface_height(con
->surface
);
1531 w
= MIN(w
, width
- x
);
1532 h
= MIN(h
, height
- y
);
1534 if (!qemu_console_is_visible(con
)) {
1537 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1538 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1541 if (dcl
->ops
->dpy_gfx_update
) {
1542 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1547 void dpy_gfx_replace_surface(QemuConsole
*con
,
1548 DisplaySurface
*surface
)
1550 DisplayState
*s
= con
->ds
;
1551 DisplaySurface
*old_surface
= con
->surface
;
1552 DisplayChangeListener
*dcl
;
1554 assert(old_surface
!= surface
|| surface
== NULL
);
1556 con
->surface
= surface
;
1557 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1558 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1561 if (dcl
->ops
->dpy_gfx_switch
) {
1562 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1565 qemu_free_displaysurface(old_surface
);
1568 bool dpy_gfx_check_format(QemuConsole
*con
,
1569 pixman_format_code_t format
)
1571 DisplayChangeListener
*dcl
;
1572 DisplayState
*s
= con
->ds
;
1574 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1575 if (dcl
->con
&& dcl
->con
!= con
) {
1576 /* dcl bound to another console -> skip */
1579 if (dcl
->ops
->dpy_gfx_check_format
) {
1580 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1584 /* default is to whitelist native 32 bpp only */
1585 if (format
!= qemu_default_pixman_format(32, true)) {
1593 static void dpy_refresh(DisplayState
*s
)
1595 DisplayChangeListener
*dcl
;
1597 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1598 if (dcl
->ops
->dpy_refresh
) {
1599 dcl
->ops
->dpy_refresh(dcl
);
1604 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1606 DisplayState
*s
= con
->ds
;
1607 DisplayChangeListener
*dcl
;
1609 if (!qemu_console_is_visible(con
)) {
1612 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1613 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1616 if (dcl
->ops
->dpy_text_cursor
) {
1617 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1622 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1624 DisplayState
*s
= con
->ds
;
1625 DisplayChangeListener
*dcl
;
1627 if (!qemu_console_is_visible(con
)) {
1630 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1631 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1634 if (dcl
->ops
->dpy_text_update
) {
1635 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1640 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1642 DisplayState
*s
= con
->ds
;
1643 DisplayChangeListener
*dcl
;
1645 if (!qemu_console_is_visible(con
)) {
1648 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1649 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1652 if (dcl
->ops
->dpy_text_resize
) {
1653 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1658 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1660 DisplayState
*s
= con
->ds
;
1661 DisplayChangeListener
*dcl
;
1663 if (!qemu_console_is_visible(con
)) {
1666 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1667 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1670 if (dcl
->ops
->dpy_mouse_set
) {
1671 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1676 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1678 DisplayState
*s
= con
->ds
;
1679 DisplayChangeListener
*dcl
;
1681 if (!qemu_console_is_visible(con
)) {
1684 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1685 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1688 if (dcl
->ops
->dpy_cursor_define
) {
1689 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1694 bool dpy_cursor_define_supported(QemuConsole
*con
)
1696 DisplayState
*s
= con
->ds
;
1697 DisplayChangeListener
*dcl
;
1699 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1700 if (dcl
->ops
->dpy_cursor_define
) {
1707 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1708 struct QEMUGLParams
*qparams
)
1711 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1714 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1717 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1720 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1723 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1726 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1729 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1732 void dpy_gl_scanout_disable(QemuConsole
*con
)
1735 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1736 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1738 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1743 void dpy_gl_scanout_texture(QemuConsole
*con
,
1744 uint32_t backing_id
,
1745 bool backing_y_0_top
,
1746 uint32_t backing_width
,
1747 uint32_t backing_height
,
1748 uint32_t x
, uint32_t y
,
1749 uint32_t width
, uint32_t height
)
1752 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1754 backing_width
, backing_height
,
1755 x
, y
, width
, height
);
1758 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
1762 con
->gl
->ops
->dpy_gl_scanout_dmabuf(con
->gl
, dmabuf
);
1765 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
1766 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
)
1770 if (con
->gl
->ops
->dpy_gl_cursor_dmabuf
) {
1771 con
->gl
->ops
->dpy_gl_cursor_dmabuf(con
->gl
, dmabuf
,
1772 have_hot
, hot_x
, hot_y
);
1776 void dpy_gl_cursor_position(QemuConsole
*con
,
1777 uint32_t pos_x
, uint32_t pos_y
)
1781 if (con
->gl
->ops
->dpy_gl_cursor_position
) {
1782 con
->gl
->ops
->dpy_gl_cursor_position(con
->gl
, pos_x
, pos_y
);
1786 void dpy_gl_release_dmabuf(QemuConsole
*con
,
1791 if (con
->gl
->ops
->dpy_gl_release_dmabuf
) {
1792 con
->gl
->ops
->dpy_gl_release_dmabuf(con
->gl
, dmabuf
);
1796 void dpy_gl_update(QemuConsole
*con
,
1797 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1800 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1803 /***********************************************************/
1804 /* register display */
1806 /* console.c internal use only */
1807 static DisplayState
*get_alloc_displaystate(void)
1809 if (!display_state
) {
1810 display_state
= g_new0(DisplayState
, 1);
1811 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1812 text_console_update_cursor
, NULL
);
1814 return display_state
;
1818 * Called by main(), after creating QemuConsoles
1819 * and before initializing ui (sdl/vnc/...).
1821 DisplayState
*init_displaystate(void)
1826 get_alloc_displaystate();
1827 for (i
= 0; i
< nb_consoles
; i
++) {
1828 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1829 consoles
[i
]->ds
== NULL
) {
1830 text_console_do_init(consoles
[i
]->chr
, display_state
);
1833 /* Hook up into the qom tree here (not in new_console()), once
1834 * all QemuConsoles are created and the order / numbering
1835 * doesn't change any more */
1836 name
= g_strdup_printf("console[%d]", i
);
1837 object_property_add_child(container_get(object_get_root(), "/backend"),
1838 name
, OBJECT(consoles
[i
]), &error_abort
);
1842 return display_state
;
1845 void graphic_console_set_hwops(QemuConsole
*con
,
1846 const GraphicHwOps
*hw_ops
,
1849 con
->hw_ops
= hw_ops
;
1853 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1854 const GraphicHwOps
*hw_ops
,
1857 static const char noinit
[] =
1858 "Guest has not initialized the display (yet).";
1864 ds
= get_alloc_displaystate();
1865 trace_console_gfx_new();
1866 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1867 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1868 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1870 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1874 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1878 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1880 if (index
>= nb_consoles
) {
1883 return consoles
[index
];
1886 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1892 for (i
= 0; i
< nb_consoles
; i
++) {
1896 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1897 "device", &error_abort
);
1898 if (DEVICE(obj
) != dev
) {
1901 h
= object_property_get_uint(OBJECT(consoles
[i
]),
1902 "head", &error_abort
);
1911 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1912 uint32_t head
, Error
**errp
)
1917 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1919 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1920 "Device '%s' not found", device_id
);
1924 con
= qemu_console_lookup_by_device(dev
, head
);
1926 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1934 bool qemu_console_is_visible(QemuConsole
*con
)
1936 return (con
== active_console
) || (con
->dcls
> 0);
1939 bool qemu_console_is_graphic(QemuConsole
*con
)
1942 con
= active_console
;
1944 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1947 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1950 con
= active_console
;
1952 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1955 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
1957 assert(con
!= NULL
);
1958 return con
->gl_block
;
1961 char *qemu_console_get_label(QemuConsole
*con
)
1963 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1965 return g_strdup(object_get_typename(con
->device
));
1967 return g_strdup("VGA");
1969 if (con
->chr
&& con
->chr
->label
) {
1970 return g_strdup(con
->chr
->label
);
1972 return g_strdup_printf("vc%d", con
->index
);
1976 int qemu_console_get_index(QemuConsole
*con
)
1979 con
= active_console
;
1981 return con
? con
->index
: -1;
1984 uint32_t qemu_console_get_head(QemuConsole
*con
)
1987 con
= active_console
;
1989 return con
? con
->head
: -1;
1992 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1994 assert(con
!= NULL
);
1995 return &con
->ui_info
;
1998 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
2001 con
= active_console
;
2003 return con
? surface_width(con
->surface
) : fallback
;
2006 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
2009 con
= active_console
;
2011 return con
? surface_height(con
->surface
) : fallback
;
2014 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
2016 VCChardev
*drv
= VC_CHARDEV(chr
);
2017 QemuConsole
*s
= drv
->console
;
2022 static void text_console_update_cursor_timer(void)
2024 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
2025 + CONSOLE_CURSOR_PERIOD
/ 2);
2028 static void text_console_update_cursor(void *opaque
)
2033 cursor_visible_phase
= !cursor_visible_phase
;
2035 for (i
= 0; i
< nb_consoles
; i
++) {
2037 if (qemu_console_is_graphic(s
) ||
2038 !qemu_console_is_visible(s
)) {
2042 graphic_hw_invalidate(s
);
2046 text_console_update_cursor_timer();
2050 static const GraphicHwOps text_console_ops
= {
2051 .invalidate
= text_console_invalidate
,
2052 .text_update
= text_console_update
,
2055 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2057 VCChardev
*drv
= VC_CHARDEV(chr
);
2058 QemuConsole
*s
= drv
->console
;
2059 int g_width
= 80 * FONT_WIDTH
;
2060 int g_height
= 24 * FONT_HEIGHT
;
2062 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2063 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2064 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2069 s
->total_height
= DEFAULT_BACKSCROLL
;
2073 if (active_console
&& active_console
->surface
) {
2074 g_width
= surface_width(active_console
->surface
);
2075 g_height
= surface_height(active_console
->surface
);
2077 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2080 s
->hw_ops
= &text_console_ops
;
2083 /* Set text attribute defaults */
2084 s
->t_attrib_default
.bold
= 0;
2085 s
->t_attrib_default
.uline
= 0;
2086 s
->t_attrib_default
.blink
= 0;
2087 s
->t_attrib_default
.invers
= 0;
2088 s
->t_attrib_default
.unvisible
= 0;
2089 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2090 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2091 /* set current text attributes to default */
2092 s
->t_attrib
= s
->t_attrib_default
;
2093 text_console_resize(s
);
2099 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2100 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2101 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2102 s
->t_attrib
= s
->t_attrib_default
;
2105 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2108 static void vc_chr_open(Chardev
*chr
,
2109 ChardevBackend
*backend
,
2113 ChardevVC
*vc
= backend
->u
.vc
.data
;
2114 VCChardev
*drv
= VC_CHARDEV(chr
);
2117 unsigned height
= 0;
2119 if (vc
->has_width
) {
2121 } else if (vc
->has_cols
) {
2122 width
= vc
->cols
* FONT_WIDTH
;
2125 if (vc
->has_height
) {
2126 height
= vc
->height
;
2127 } else if (vc
->has_rows
) {
2128 height
= vc
->rows
* FONT_HEIGHT
;
2131 trace_console_txt_new(width
, height
);
2132 if (width
== 0 || height
== 0) {
2133 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2135 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2136 s
->surface
= qemu_create_displaysurface(width
, height
);
2140 error_setg(errp
, "cannot create text console");
2147 if (display_state
) {
2148 text_console_do_init(chr
, display_state
);
2151 /* console/chardev init sometimes completes elsewhere in a 2nd
2152 * stage, so defer OPENED events until they are fully initialized
2157 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2159 DisplaySurface
*surface
;
2161 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2163 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2164 pixman_image_get_width(s
->surface
->image
) == width
&&
2165 pixman_image_get_height(s
->surface
->image
) == height
) {
2169 surface
= qemu_create_displaysurface(width
, height
);
2170 dpy_gfx_replace_surface(s
, surface
);
2173 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2175 return console
->surface
;
2178 PixelFormat
qemu_default_pixelformat(int bpp
)
2180 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2181 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2185 static QemuDisplay
*dpys
[DISPLAY_TYPE__MAX
];
2187 void qemu_display_register(QemuDisplay
*ui
)
2189 assert(ui
->type
< DISPLAY_TYPE__MAX
);
2190 dpys
[ui
->type
] = ui
;
2193 bool qemu_display_find_default(DisplayOptions
*opts
)
2195 static DisplayType prio
[] = {
2202 for (i
= 0; i
< ARRAY_SIZE(prio
); i
++) {
2203 if (dpys
[prio
[i
]] == NULL
) {
2204 ui_module_load_one(DisplayType_lookup
.array
[prio
[i
]]);
2206 if (dpys
[prio
[i
]] == NULL
) {
2209 opts
->type
= prio
[i
];
2215 void qemu_display_early_init(DisplayOptions
*opts
)
2217 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2218 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2221 if (dpys
[opts
->type
] == NULL
) {
2222 ui_module_load_one(DisplayType_lookup
.array
[opts
->type
]);
2224 if (dpys
[opts
->type
] == NULL
) {
2225 error_report("Display '%s' is not available.",
2226 DisplayType_lookup
.array
[opts
->type
]);
2229 if (dpys
[opts
->type
]->early_init
) {
2230 dpys
[opts
->type
]->early_init(opts
);
2234 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
2236 assert(opts
->type
< DISPLAY_TYPE__MAX
);
2237 if (opts
->type
== DISPLAY_TYPE_NONE
) {
2240 assert(dpys
[opts
->type
] != NULL
);
2241 dpys
[opts
->type
]->init(ds
, opts
);
2244 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2249 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2250 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2251 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2253 val
= qemu_opt_get_number(opts
, "width", 0);
2255 vc
->has_width
= true;
2259 val
= qemu_opt_get_number(opts
, "height", 0);
2261 vc
->has_height
= true;
2265 val
= qemu_opt_get_number(opts
, "cols", 0);
2267 vc
->has_cols
= true;
2271 val
= qemu_opt_get_number(opts
, "rows", 0);
2273 vc
->has_rows
= true;
2278 static const TypeInfo qemu_console_info
= {
2279 .name
= TYPE_QEMU_CONSOLE
,
2280 .parent
= TYPE_OBJECT
,
2281 .instance_size
= sizeof(QemuConsole
),
2282 .class_size
= sizeof(QemuConsoleClass
),
2285 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2287 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2289 cc
->parse
= qemu_chr_parse_vc
;
2290 cc
->open
= vc_chr_open
;
2291 cc
->chr_write
= vc_chr_write
;
2292 cc
->chr_set_echo
= vc_chr_set_echo
;
2295 static const TypeInfo char_vc_type_info
= {
2296 .name
= TYPE_CHARDEV_VC
,
2297 .parent
= TYPE_CHARDEV
,
2298 .instance_size
= sizeof(VCChardev
),
2299 .class_init
= char_vc_class_init
,
2302 void qemu_console_early_init(void)
2304 /* set the default vc driver */
2305 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2306 type_register(&char_vc_type_info
);
2310 static void register_types(void)
2312 type_register_static(&qemu_console_info
);
2315 type_init(register_types
);