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
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qemu/timer.h"
29 #include "qmp-commands.h"
30 #include "sysemu/char.h"
32 #include "exec/memory.h"
34 #define DEFAULT_BACKSCROLL 512
35 #define CONSOLE_CURSOR_PERIOD 500
37 typedef struct TextAttributes
{
47 typedef struct TextCell
{
49 TextAttributes t_attrib
;
52 #define MAX_ESC_PARAMS 3
60 typedef struct QEMUFIFO
{
63 int count
, wptr
, rptr
;
66 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
70 l
= f
->buf_size
- f
->count
;
75 l
= f
->buf_size
- f
->wptr
;
78 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
80 if (f
->wptr
>= f
->buf_size
)
89 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
97 l
= f
->buf_size
- f
->rptr
;
100 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
102 if (f
->rptr
>= f
->buf_size
)
114 TEXT_CONSOLE_FIXED_SIZE
121 console_type_t console_type
;
123 DisplaySurface
*surface
;
125 DisplayChangeListener
*gl
;
129 /* Graphic console state. */
134 const GraphicHwOps
*hw_ops
;
137 /* Text console state */
141 int backscroll_height
;
143 int x_saved
, y_saved
;
146 TextAttributes t_attrib_default
; /* default text attributes */
147 TextAttributes t_attrib
; /* currently active text attributes */
149 int text_x
[2], text_y
[2], cursor_invalidate
;
158 int esc_params
[MAX_ESC_PARAMS
];
161 CharDriverState
*chr
;
162 /* fifo for key pressed */
164 uint8_t out_fifo_buf
[16];
165 QEMUTimer
*kbd_timer
;
168 struct DisplayState
{
169 QEMUTimer
*gui_timer
;
170 uint64_t last_update
;
171 uint64_t update_interval
;
176 QLIST_HEAD(, DisplayChangeListener
) listeners
;
179 static DisplayState
*display_state
;
180 static QemuConsole
*active_console
;
181 static QemuConsole
**consoles
;
182 static int nb_consoles
= 0;
183 static bool cursor_visible_phase
;
184 static QEMUTimer
*cursor_timer
;
186 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
187 static void dpy_refresh(DisplayState
*s
);
188 static DisplayState
*get_alloc_displaystate(void);
189 static void text_console_update_cursor_timer(void);
190 static void text_console_update_cursor(void *opaque
);
192 static void gui_update(void *opaque
)
194 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
195 uint64_t dcl_interval
;
196 DisplayState
*ds
= opaque
;
197 DisplayChangeListener
*dcl
;
200 ds
->refreshing
= true;
202 ds
->refreshing
= false;
204 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
205 dcl_interval
= dcl
->update_interval
?
206 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
207 if (interval
> dcl_interval
) {
208 interval
= dcl_interval
;
211 if (ds
->update_interval
!= interval
) {
212 ds
->update_interval
= interval
;
213 for (i
= 0; i
< nb_consoles
; i
++) {
214 if (consoles
[i
]->hw_ops
->update_interval
) {
215 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
218 trace_console_refresh(interval
);
220 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
221 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
224 static void gui_setup_refresh(DisplayState
*ds
)
226 DisplayChangeListener
*dcl
;
227 bool need_timer
= false;
228 bool have_gfx
= false;
229 bool have_text
= false;
231 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
232 if (dcl
->ops
->dpy_refresh
!= NULL
) {
235 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
238 if (dcl
->ops
->dpy_text_update
!= NULL
) {
243 if (need_timer
&& ds
->gui_timer
== NULL
) {
244 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
245 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
247 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
248 timer_del(ds
->gui_timer
);
249 timer_free(ds
->gui_timer
);
250 ds
->gui_timer
= NULL
;
253 ds
->have_gfx
= have_gfx
;
254 ds
->have_text
= have_text
;
257 void graphic_hw_update(QemuConsole
*con
)
260 con
= active_console
;
262 if (con
&& con
->hw_ops
->gfx_update
) {
263 con
->hw_ops
->gfx_update(con
->hw
);
267 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
271 con
->gl_block
= block
;
272 if (con
->hw_ops
->gl_block
) {
273 con
->hw_ops
->gl_block(con
->hw
, block
);
277 int qemu_console_get_window_id(QemuConsole
*con
)
279 return con
->window_id
;
282 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
284 con
->window_id
= window_id
;
287 void graphic_hw_invalidate(QemuConsole
*con
)
290 con
= active_console
;
292 if (con
&& con
->hw_ops
->invalidate
) {
293 con
->hw_ops
->invalidate(con
->hw
);
297 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
300 int width
= pixman_image_get_width(ds
->image
);
301 int height
= pixman_image_get_height(ds
->image
);
306 pixman_image_t
*linebuf
;
308 trace_ppm_save(filename
, ds
);
309 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
311 error_setg(errp
, "failed to open file '%s': %s", filename
,
315 f
= fdopen(fd
, "wb");
316 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
321 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
322 for (y
= 0; y
< height
; y
++) {
323 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
325 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
326 pixman_image_get_stride(linebuf
), f
);
334 qemu_pixman_image_unref(linebuf
);
339 error_setg(errp
, "failed to write to file '%s': %s", filename
,
345 void qmp_screendump(const char *filename
, Error
**errp
)
347 QemuConsole
*con
= qemu_console_lookup_by_index(0);
348 DisplaySurface
*surface
;
351 error_setg(errp
, "There is no QemuConsole I can screendump from.");
355 graphic_hw_update(con
);
356 surface
= qemu_console_surface(con
);
357 ppm_save(filename
, surface
, errp
);
360 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
363 con
= active_console
;
365 if (con
&& con
->hw_ops
->text_update
) {
366 con
->hw_ops
->text_update(con
->hw
, chardata
);
370 static void vga_fill_rect(QemuConsole
*con
,
371 int posx
, int posy
, int width
, int height
,
372 pixman_color_t color
)
374 DisplaySurface
*surface
= qemu_console_surface(con
);
375 pixman_rectangle16_t rect
= {
376 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
379 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
383 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
384 static void vga_bitblt(QemuConsole
*con
,
385 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
387 DisplaySurface
*surface
= qemu_console_surface(con
);
389 pixman_image_composite(PIXMAN_OP_SRC
,
390 surface
->image
, NULL
, surface
->image
,
391 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
394 /***********************************************************/
395 /* basic char display */
397 #define FONT_HEIGHT 16
402 #define QEMU_RGB(r, g, b) \
403 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
405 static const pixman_color_t color_table_rgb
[2][8] = {
407 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
408 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
409 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
410 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
411 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
412 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
413 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
414 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
417 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
418 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
419 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
420 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
421 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
422 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
423 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
424 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
428 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
429 TextAttributes
*t_attrib
)
431 static pixman_image_t
*glyphs
[256];
432 DisplaySurface
*surface
= qemu_console_surface(s
);
433 pixman_color_t fgcol
, bgcol
;
435 if (t_attrib
->invers
) {
436 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
437 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
439 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
440 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
444 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
446 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
447 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
450 static void text_console_resize(QemuConsole
*s
)
452 TextCell
*cells
, *c
, *c1
;
453 int w1
, x
, y
, last_width
;
455 last_width
= s
->width
;
456 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
457 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
463 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
464 for(y
= 0; y
< s
->total_height
; y
++) {
465 c
= &cells
[y
* s
->width
];
467 c1
= &s
->cells
[y
* last_width
];
468 for(x
= 0; x
< w1
; x
++) {
472 for(x
= w1
; x
< s
->width
; x
++) {
474 c
->t_attrib
= s
->t_attrib_default
;
482 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
484 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
485 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
486 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
487 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
490 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
492 if (!qemu_console_is_visible(s
)) {
495 if (s
->update_x0
> x
* FONT_WIDTH
)
496 s
->update_x0
= x
* FONT_WIDTH
;
497 if (s
->update_y0
> y
* FONT_HEIGHT
)
498 s
->update_y0
= y
* FONT_HEIGHT
;
499 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
500 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
501 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
502 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
505 static void update_xy(QemuConsole
*s
, int x
, int y
)
510 if (s
->ds
->have_text
) {
511 text_update_xy(s
, x
, y
);
514 y1
= (s
->y_base
+ y
) % s
->total_height
;
515 y2
= y1
- s
->y_displayed
;
517 y2
+= s
->total_height
;
519 if (y2
< s
->height
) {
520 c
= &s
->cells
[y1
* s
->width
+ x
];
521 vga_putcharxy(s
, x
, y2
, c
->ch
,
523 invalidate_xy(s
, x
, y2
);
527 static void console_show_cursor(QemuConsole
*s
, int show
)
533 if (s
->ds
->have_text
) {
534 s
->cursor_invalidate
= 1;
540 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
541 y
= y1
- s
->y_displayed
;
543 y
+= s
->total_height
;
546 c
= &s
->cells
[y1
* s
->width
+ x
];
547 if (show
&& cursor_visible_phase
) {
548 TextAttributes t_attrib
= s
->t_attrib_default
;
549 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
550 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
552 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
554 invalidate_xy(s
, x
, y
);
558 static void console_refresh(QemuConsole
*s
)
560 DisplaySurface
*surface
= qemu_console_surface(s
);
564 if (s
->ds
->have_text
) {
567 s
->text_x
[1] = s
->width
- 1;
568 s
->text_y
[1] = s
->height
- 1;
569 s
->cursor_invalidate
= 1;
572 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
573 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
575 for (y
= 0; y
< s
->height
; y
++) {
576 c
= s
->cells
+ y1
* s
->width
;
577 for (x
= 0; x
< s
->width
; x
++) {
578 vga_putcharxy(s
, x
, y
, c
->ch
,
582 if (++y1
== s
->total_height
) {
586 console_show_cursor(s
, 1);
587 dpy_gfx_update(s
, 0, 0,
588 surface_width(surface
), surface_height(surface
));
591 static void console_scroll(QemuConsole
*s
, int ydelta
)
596 for(i
= 0; i
< ydelta
; i
++) {
597 if (s
->y_displayed
== s
->y_base
)
599 if (++s
->y_displayed
== s
->total_height
)
604 i
= s
->backscroll_height
;
605 if (i
> s
->total_height
- s
->height
)
606 i
= s
->total_height
- s
->height
;
609 y1
+= s
->total_height
;
610 for(i
= 0; i
< ydelta
; i
++) {
611 if (s
->y_displayed
== y1
)
613 if (--s
->y_displayed
< 0)
614 s
->y_displayed
= s
->total_height
- 1;
620 static void console_put_lf(QemuConsole
*s
)
626 if (s
->y
>= s
->height
) {
627 s
->y
= s
->height
- 1;
629 if (s
->y_displayed
== s
->y_base
) {
630 if (++s
->y_displayed
== s
->total_height
)
633 if (++s
->y_base
== s
->total_height
)
635 if (s
->backscroll_height
< s
->total_height
)
636 s
->backscroll_height
++;
637 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
638 c
= &s
->cells
[y1
* s
->width
];
639 for(x
= 0; x
< s
->width
; x
++) {
641 c
->t_attrib
= s
->t_attrib_default
;
644 if (s
->y_displayed
== s
->y_base
) {
645 if (s
->ds
->have_text
) {
648 s
->text_x
[1] = s
->width
- 1;
649 s
->text_y
[1] = s
->height
- 1;
652 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
653 s
->width
* FONT_WIDTH
,
654 (s
->height
- 1) * FONT_HEIGHT
);
655 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
656 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
657 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
660 s
->update_x1
= s
->width
* FONT_WIDTH
;
661 s
->update_y1
= s
->height
* FONT_HEIGHT
;
666 /* Set console attributes depending on the current escape codes.
667 * NOTE: I know this code is not very efficient (checking every color for it
668 * self) but it is more readable and better maintainable.
670 static void console_handle_escape(QemuConsole
*s
)
674 for (i
=0; i
<s
->nb_esc_params
; i
++) {
675 switch (s
->esc_params
[i
]) {
676 case 0: /* reset all console attributes to default */
677 s
->t_attrib
= s
->t_attrib_default
;
680 s
->t_attrib
.bold
= 1;
683 s
->t_attrib
.uline
= 1;
686 s
->t_attrib
.blink
= 1;
689 s
->t_attrib
.invers
= 1;
692 s
->t_attrib
.unvisible
= 1;
695 s
->t_attrib
.bold
= 0;
698 s
->t_attrib
.uline
= 0;
701 s
->t_attrib
.blink
= 0;
704 s
->t_attrib
.invers
= 0;
707 s
->t_attrib
.unvisible
= 0;
709 /* set foreground color */
711 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
714 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
717 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
720 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
723 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
726 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
729 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
732 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
734 /* set background color */
736 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
739 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
742 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
745 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
748 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
751 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
754 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
757 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
763 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
765 int y1
= (s
->y_base
+ y
) % s
->total_height
;
766 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
768 c
->t_attrib
= s
->t_attrib_default
;
772 static void console_put_one(QemuConsole
*s
, int ch
)
776 if (s
->x
>= s
->width
) {
781 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
782 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
784 c
->t_attrib
= s
->t_attrib
;
785 update_xy(s
, s
->x
, s
->y
);
789 static void console_respond_str(QemuConsole
*s
, const char *buf
)
792 console_put_one(s
, *buf
);
797 /* set cursor, checking bounds */
798 static void set_cursor(QemuConsole
*s
, int x
, int y
)
806 if (y
>= s
->height
) {
817 static void console_putchar(QemuConsole
*s
, int ch
)
826 case '\r': /* carriage return */
829 case '\n': /* newline */
832 case '\b': /* backspace */
836 case '\t': /* tabspace */
837 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
841 s
->x
= s
->x
+ (8 - (s
->x
% 8));
844 case '\a': /* alert aka. bell */
845 /* TODO: has to be implemented */
848 /* SI (shift in), character set 0 (ignored) */
851 /* SO (shift out), character set 1 (ignored) */
853 case 27: /* esc (introducing an escape sequence) */
854 s
->state
= TTY_STATE_ESC
;
857 console_put_one(s
, ch
);
861 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
863 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
864 s
->esc_params
[i
] = 0;
865 s
->nb_esc_params
= 0;
866 s
->state
= TTY_STATE_CSI
;
868 s
->state
= TTY_STATE_NORM
;
871 case TTY_STATE_CSI
: /* handle escape sequence parameters */
872 if (ch
>= '0' && ch
<= '9') {
873 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
874 int *param
= &s
->esc_params
[s
->nb_esc_params
];
875 int digit
= (ch
- '0');
877 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
878 *param
* 10 + digit
: INT_MAX
;
881 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
885 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
886 ch
, s
->nb_esc_params
);
887 s
->state
= TTY_STATE_NORM
;
891 if (s
->esc_params
[0] == 0) {
892 s
->esc_params
[0] = 1;
894 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
897 /* move cursor down */
898 if (s
->esc_params
[0] == 0) {
899 s
->esc_params
[0] = 1;
901 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
904 /* move cursor right */
905 if (s
->esc_params
[0] == 0) {
906 s
->esc_params
[0] = 1;
908 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
911 /* move cursor left */
912 if (s
->esc_params
[0] == 0) {
913 s
->esc_params
[0] = 1;
915 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
918 /* move cursor to column */
919 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
923 /* move cursor to row, column */
924 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
927 switch (s
->esc_params
[0]) {
929 /* clear to end of screen */
930 for (y
= s
->y
; y
< s
->height
; y
++) {
931 for (x
= 0; x
< s
->width
; x
++) {
932 if (y
== s
->y
&& x
< s
->x
) {
935 console_clear_xy(s
, x
, y
);
940 /* clear from beginning of screen */
941 for (y
= 0; y
<= s
->y
; y
++) {
942 for (x
= 0; x
< s
->width
; x
++) {
943 if (y
== s
->y
&& x
> s
->x
) {
946 console_clear_xy(s
, x
, y
);
951 /* clear entire screen */
952 for (y
= 0; y
<= s
->height
; y
++) {
953 for (x
= 0; x
< s
->width
; x
++) {
954 console_clear_xy(s
, x
, y
);
961 switch (s
->esc_params
[0]) {
964 for(x
= s
->x
; x
< s
->width
; x
++) {
965 console_clear_xy(s
, x
, s
->y
);
969 /* clear from beginning of line */
970 for (x
= 0; x
<= s
->x
; x
++) {
971 console_clear_xy(s
, x
, s
->y
);
975 /* clear entire line */
976 for(x
= 0; x
< s
->width
; x
++) {
977 console_clear_xy(s
, x
, s
->y
);
983 console_handle_escape(s
);
986 switch (s
->esc_params
[0]) {
988 /* report console status (always succeed)*/
989 console_respond_str(s
, "\033[0n");
992 /* report cursor position */
993 sprintf(response
, "\033[%d;%dR",
994 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
996 console_respond_str(s
, response
);
1001 /* save cursor position */
1006 /* restore cursor position */
1011 trace_console_putchar_unhandled(ch
);
1019 void console_select(unsigned int index
)
1021 DisplayChangeListener
*dcl
;
1024 trace_console_select(index
);
1025 s
= qemu_console_lookup_by_index(index
);
1027 DisplayState
*ds
= s
->ds
;
1031 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1032 if (dcl
->con
!= NULL
) {
1035 if (dcl
->ops
->dpy_gfx_switch
) {
1036 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1039 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1040 surface_height(s
->surface
));
1042 if (ds
->have_text
) {
1043 dpy_text_resize(s
, s
->width
, s
->height
);
1045 text_console_update_cursor(NULL
);
1049 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1051 QemuConsole
*s
= chr
->opaque
;
1054 s
->update_x0
= s
->width
* FONT_WIDTH
;
1055 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1058 console_show_cursor(s
, 0);
1059 for(i
= 0; i
< len
; i
++) {
1060 console_putchar(s
, buf
[i
]);
1062 console_show_cursor(s
, 1);
1063 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1064 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1065 s
->update_x1
- s
->update_x0
,
1066 s
->update_y1
- s
->update_y0
);
1071 static void kbd_send_chars(void *opaque
)
1073 QemuConsole
*s
= opaque
;
1077 len
= qemu_chr_be_can_write(s
->chr
);
1078 if (len
> s
->out_fifo
.count
)
1079 len
= s
->out_fifo
.count
;
1081 if (len
> sizeof(buf
))
1083 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1084 qemu_chr_be_write(s
->chr
, buf
, len
);
1086 /* characters are pending: we send them a bit later (XXX:
1087 horrible, should change char device API) */
1088 if (s
->out_fifo
.count
> 0) {
1089 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1093 /* called when an ascii key is pressed */
1094 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1096 uint8_t buf
[16], *q
;
1100 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1104 case QEMU_KEY_CTRL_UP
:
1105 console_scroll(s
, -1);
1107 case QEMU_KEY_CTRL_DOWN
:
1108 console_scroll(s
, 1);
1110 case QEMU_KEY_CTRL_PAGEUP
:
1111 console_scroll(s
, -10);
1113 case QEMU_KEY_CTRL_PAGEDOWN
:
1114 console_scroll(s
, 10);
1117 /* convert the QEMU keysym to VT100 key string */
1119 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1122 c
= keysym
- 0xe100;
1124 *q
++ = '0' + (c
/ 10);
1125 *q
++ = '0' + (c
% 10);
1127 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1130 *q
++ = keysym
& 0xff;
1131 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1132 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1138 console_puts(s
->chr
, buf
, q
- buf
);
1141 if (be
&& be
->chr_read
) {
1142 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1149 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1150 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1151 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1152 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1153 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1154 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1155 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1156 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1157 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1158 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1159 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1162 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1166 keysym
= qcode_to_keysym
[qcode
];
1170 kbd_put_keysym_console(s
, keysym
);
1174 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1178 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1179 kbd_put_keysym_console(s
, str
[i
]);
1183 void kbd_put_keysym(int keysym
)
1185 kbd_put_keysym_console(active_console
, keysym
);
1188 static void text_console_invalidate(void *opaque
)
1190 QemuConsole
*s
= (QemuConsole
*) opaque
;
1192 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1193 text_console_resize(s
);
1198 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1200 QemuConsole
*s
= (QemuConsole
*) opaque
;
1203 if (s
->text_x
[0] <= s
->text_x
[1]) {
1204 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1205 chardata
+= s
->text_y
[0] * s
->width
;
1206 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1207 for (j
= 0; j
< s
->width
; j
++, src
++) {
1208 console_write_ch(chardata
++,
1209 ATTR2CHTYPE(s
->cells
[src
].ch
,
1210 s
->cells
[src
].t_attrib
.fgcol
,
1211 s
->cells
[src
].t_attrib
.bgcol
,
1212 s
->cells
[src
].t_attrib
.bold
));
1214 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1215 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1216 s
->text_x
[0] = s
->width
;
1217 s
->text_y
[0] = s
->height
;
1221 if (s
->cursor_invalidate
) {
1222 dpy_text_cursor(s
, s
->x
, s
->y
);
1223 s
->cursor_invalidate
= 0;
1227 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1234 obj
= object_new(TYPE_QEMU_CONSOLE
);
1235 s
= QEMU_CONSOLE(obj
);
1237 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1238 (Object
**)&s
->device
,
1239 object_property_allow_set_link
,
1240 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1242 object_property_add_uint32_ptr(obj
, "head",
1243 &s
->head
, &error_abort
);
1245 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1246 (console_type
== GRAPHIC_CONSOLE
))) {
1250 s
->console_type
= console_type
;
1252 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1253 if (console_type
!= GRAPHIC_CONSOLE
) {
1254 s
->index
= nb_consoles
;
1255 consoles
[nb_consoles
++] = s
;
1257 /* HACK: Put graphical consoles before text consoles. */
1258 for (i
= nb_consoles
; i
> 0; i
--) {
1259 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1261 consoles
[i
] = consoles
[i
- 1];
1262 consoles
[i
]->index
= i
;
1271 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1273 qemu_pixman_image_unref(surface
->image
);
1274 surface
->image
= NULL
;
1276 surface
->format
= PIXMAN_x8r8g8b8
;
1277 surface
->image
= pixman_image_create_bits(surface
->format
,
1280 assert(surface
->image
!= NULL
);
1282 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1285 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1287 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1289 trace_displaysurface_create(surface
, width
, height
);
1290 qemu_alloc_display(surface
, width
, height
);
1294 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1295 pixman_format_code_t format
,
1296 int linesize
, uint8_t *data
)
1298 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1300 trace_displaysurface_create_from(surface
, width
, height
, format
);
1301 surface
->format
= format
;
1302 surface
->image
= pixman_image_create_bits(surface
->format
,
1304 (void *)data
, linesize
);
1305 assert(surface
->image
!= NULL
);
1310 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1312 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1314 trace_displaysurface_create_pixman(surface
);
1315 surface
->format
= pixman_image_get_format(image
);
1316 surface
->image
= pixman_image_ref(image
);
1321 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1324 void *data
= pixman_image_get_data(image
);
1325 uint32_t size
= pixman_image_get_stride(image
) *
1326 pixman_image_get_height(image
);
1327 cpu_physical_memory_unmap(data
, size
, 0, 0);
1330 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1331 pixman_format_code_t format
,
1332 int linesize
, uint64_t addr
)
1334 DisplaySurface
*surface
;
1338 if (linesize
== 0) {
1339 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1342 size
= (hwaddr
)linesize
* height
;
1343 data
= cpu_physical_memory_map(addr
, &size
, 0);
1344 if (size
!= (hwaddr
)linesize
* height
) {
1345 cpu_physical_memory_unmap(data
, size
, 0, 0);
1349 surface
= qemu_create_displaysurface_from
1350 (width
, height
, format
, linesize
, data
);
1351 pixman_image_set_destroy_function
1352 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1357 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1360 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1361 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1362 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1363 pixman_image_t
*glyph
;
1367 x
= (w
/ FONT_WIDTH
- len
) / 2;
1368 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1369 for (i
= 0; i
< len
; i
++) {
1370 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1371 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1372 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1373 qemu_pixman_image_unref(glyph
);
1378 void qemu_free_displaysurface(DisplaySurface
*surface
)
1380 if (surface
== NULL
) {
1383 trace_displaysurface_free(surface
);
1384 qemu_pixman_image_unref(surface
->image
);
1388 bool console_has_gl(QemuConsole
*con
)
1390 return con
->gl
!= NULL
;
1393 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1395 static const char nodev
[] =
1396 "This VM has no graphic display device.";
1397 static DisplaySurface
*dummy
;
1400 if (dcl
->ops
->dpy_gl_ctx_create
) {
1401 /* display has opengl support */
1404 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1405 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1411 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1412 dcl
->ds
= get_alloc_displaystate();
1413 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1414 gui_setup_refresh(dcl
->ds
);
1419 con
= active_console
;
1421 if (dcl
->ops
->dpy_gfx_switch
) {
1423 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1426 dummy
= qemu_create_message_surface(640, 480, nodev
);
1428 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1431 text_console_update_cursor(NULL
);
1434 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1437 DisplayState
*ds
= dcl
->ds
;
1439 dcl
->update_interval
= interval
;
1440 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1441 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1445 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1447 DisplayState
*ds
= dcl
->ds
;
1448 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1452 QLIST_REMOVE(dcl
, next
);
1453 gui_setup_refresh(ds
);
1456 static void dpy_set_ui_info_timer(void *opaque
)
1458 QemuConsole
*con
= opaque
;
1460 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1463 bool dpy_ui_info_supported(QemuConsole
*con
)
1465 return con
->hw_ops
->ui_info
!= NULL
;
1468 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1470 assert(con
!= NULL
);
1472 if (!dpy_ui_info_supported(con
)) {
1475 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1476 /* nothing changed -- ignore */
1481 * Typically we get a flood of these as the user resizes the window.
1482 * Wait until the dust has settled (one second without updates), then
1483 * go notify the guest.
1485 con
->ui_info
= *info
;
1486 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1490 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1492 DisplayState
*s
= con
->ds
;
1493 DisplayChangeListener
*dcl
;
1498 width
= surface_width(con
->surface
);
1499 height
= surface_height(con
->surface
);
1505 w
= MIN(w
, width
- x
);
1506 h
= MIN(h
, height
- y
);
1508 if (!qemu_console_is_visible(con
)) {
1511 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1512 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1515 if (dcl
->ops
->dpy_gfx_update
) {
1516 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1521 void dpy_gfx_replace_surface(QemuConsole
*con
,
1522 DisplaySurface
*surface
)
1524 DisplayState
*s
= con
->ds
;
1525 DisplaySurface
*old_surface
= con
->surface
;
1526 DisplayChangeListener
*dcl
;
1528 con
->surface
= surface
;
1529 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1530 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1533 if (dcl
->ops
->dpy_gfx_switch
) {
1534 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1537 qemu_free_displaysurface(old_surface
);
1540 bool dpy_gfx_check_format(QemuConsole
*con
,
1541 pixman_format_code_t format
)
1543 DisplayChangeListener
*dcl
;
1544 DisplayState
*s
= con
->ds
;
1546 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1547 if (dcl
->con
&& dcl
->con
!= con
) {
1548 /* dcl bound to another console -> skip */
1551 if (dcl
->ops
->dpy_gfx_check_format
) {
1552 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1556 /* default is to whitelist native 32 bpp only */
1557 if (format
!= qemu_default_pixman_format(32, true)) {
1565 static void dpy_refresh(DisplayState
*s
)
1567 DisplayChangeListener
*dcl
;
1569 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1570 if (dcl
->ops
->dpy_refresh
) {
1571 dcl
->ops
->dpy_refresh(dcl
);
1576 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1577 int dst_x
, int dst_y
, int w
, int h
)
1579 DisplayState
*s
= con
->ds
;
1580 DisplayChangeListener
*dcl
;
1582 if (!qemu_console_is_visible(con
)) {
1585 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1586 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1589 if (dcl
->ops
->dpy_gfx_copy
) {
1590 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1592 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1597 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1599 DisplayState
*s
= con
->ds
;
1600 DisplayChangeListener
*dcl
;
1602 if (!qemu_console_is_visible(con
)) {
1605 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1606 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1609 if (dcl
->ops
->dpy_text_cursor
) {
1610 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1615 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1617 DisplayState
*s
= con
->ds
;
1618 DisplayChangeListener
*dcl
;
1620 if (!qemu_console_is_visible(con
)) {
1623 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1624 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1627 if (dcl
->ops
->dpy_text_update
) {
1628 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1633 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1635 DisplayState
*s
= con
->ds
;
1636 DisplayChangeListener
*dcl
;
1638 if (!qemu_console_is_visible(con
)) {
1641 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1642 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1645 if (dcl
->ops
->dpy_text_resize
) {
1646 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1651 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1653 DisplayState
*s
= con
->ds
;
1654 DisplayChangeListener
*dcl
;
1656 if (!qemu_console_is_visible(con
)) {
1659 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1660 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1663 if (dcl
->ops
->dpy_mouse_set
) {
1664 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1669 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1671 DisplayState
*s
= con
->ds
;
1672 DisplayChangeListener
*dcl
;
1674 if (!qemu_console_is_visible(con
)) {
1677 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1678 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1681 if (dcl
->ops
->dpy_cursor_define
) {
1682 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1687 bool dpy_cursor_define_supported(QemuConsole
*con
)
1689 DisplayState
*s
= con
->ds
;
1690 DisplayChangeListener
*dcl
;
1692 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1693 if (dcl
->ops
->dpy_cursor_define
) {
1700 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1701 struct QEMUGLParams
*qparams
)
1704 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1707 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1710 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1713 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1716 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1719 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1722 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1725 void dpy_gl_scanout(QemuConsole
*con
,
1726 uint32_t backing_id
, bool backing_y_0_top
,
1727 uint32_t backing_width
, uint32_t backing_height
,
1728 uint32_t x
, uint32_t y
, uint32_t width
, uint32_t height
)
1731 con
->gl
->ops
->dpy_gl_scanout(con
->gl
, backing_id
,
1733 backing_width
, backing_height
,
1734 x
, y
, width
, height
);
1737 void dpy_gl_update(QemuConsole
*con
,
1738 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1741 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1744 /***********************************************************/
1745 /* register display */
1747 /* console.c internal use only */
1748 static DisplayState
*get_alloc_displaystate(void)
1750 if (!display_state
) {
1751 display_state
= g_new0(DisplayState
, 1);
1752 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1753 text_console_update_cursor
, NULL
);
1755 return display_state
;
1759 * Called by main(), after creating QemuConsoles
1760 * and before initializing ui (sdl/vnc/...).
1762 DisplayState
*init_displaystate(void)
1767 get_alloc_displaystate();
1768 for (i
= 0; i
< nb_consoles
; i
++) {
1769 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1770 consoles
[i
]->ds
== NULL
) {
1771 text_console_do_init(consoles
[i
]->chr
, display_state
);
1774 /* Hook up into the qom tree here (not in new_console()), once
1775 * all QemuConsoles are created and the order / numbering
1776 * doesn't change any more */
1777 name
= g_strdup_printf("console[%d]", i
);
1778 object_property_add_child(container_get(object_get_root(), "/backend"),
1779 name
, OBJECT(consoles
[i
]), &error_abort
);
1783 return display_state
;
1786 void graphic_console_set_hwops(QemuConsole
*con
,
1787 const GraphicHwOps
*hw_ops
,
1790 con
->hw_ops
= hw_ops
;
1794 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1795 const GraphicHwOps
*hw_ops
,
1798 static const char noinit
[] =
1799 "Guest has not initialized the display (yet).";
1805 ds
= get_alloc_displaystate();
1806 trace_console_gfx_new();
1807 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1808 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1809 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1811 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1815 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1819 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1821 if (index
>= nb_consoles
) {
1824 return consoles
[index
];
1827 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1833 for (i
= 0; i
< nb_consoles
; i
++) {
1837 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1838 "device", &error_abort
);
1839 if (DEVICE(obj
) != dev
) {
1842 h
= object_property_get_int(OBJECT(consoles
[i
]),
1843 "head", &error_abort
);
1852 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1853 uint32_t head
, Error
**errp
)
1858 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1860 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1861 "Device '%s' not found", device_id
);
1865 con
= qemu_console_lookup_by_device(dev
, head
);
1867 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1875 bool qemu_console_is_visible(QemuConsole
*con
)
1877 return (con
== active_console
) || (con
->dcls
> 0);
1880 bool qemu_console_is_graphic(QemuConsole
*con
)
1883 con
= active_console
;
1885 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1888 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1891 con
= active_console
;
1893 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1896 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
1898 assert(con
!= NULL
);
1899 return con
->gl_block
;
1902 char *qemu_console_get_label(QemuConsole
*con
)
1904 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1906 return g_strdup(object_get_typename(con
->device
));
1908 return g_strdup("VGA");
1910 if (con
->chr
&& con
->chr
->label
) {
1911 return g_strdup(con
->chr
->label
);
1913 return g_strdup_printf("vc%d", con
->index
);
1917 int qemu_console_get_index(QemuConsole
*con
)
1920 con
= active_console
;
1922 return con
? con
->index
: -1;
1925 uint32_t qemu_console_get_head(QemuConsole
*con
)
1928 con
= active_console
;
1930 return con
? con
->head
: -1;
1933 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1935 assert(con
!= NULL
);
1936 return &con
->ui_info
;
1939 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1942 con
= active_console
;
1944 return con
? surface_width(con
->surface
) : fallback
;
1947 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1950 con
= active_console
;
1952 return con
? surface_height(con
->surface
) : fallback
;
1955 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1957 QemuConsole
*s
= chr
->opaque
;
1962 static void text_console_update_cursor_timer(void)
1964 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1965 + CONSOLE_CURSOR_PERIOD
/ 2);
1968 static void text_console_update_cursor(void *opaque
)
1973 cursor_visible_phase
= !cursor_visible_phase
;
1975 for (i
= 0; i
< nb_consoles
; i
++) {
1977 if (qemu_console_is_graphic(s
) ||
1978 !qemu_console_is_visible(s
)) {
1982 graphic_hw_invalidate(s
);
1986 text_console_update_cursor_timer();
1990 static const GraphicHwOps text_console_ops
= {
1991 .invalidate
= text_console_invalidate
,
1992 .text_update
= text_console_update
,
1995 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1998 int g_width
= 80 * FONT_WIDTH
;
1999 int g_height
= 24 * FONT_HEIGHT
;
2003 chr
->chr_write
= console_puts
;
2005 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2006 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2007 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2012 s
->total_height
= DEFAULT_BACKSCROLL
;
2016 if (active_console
&& active_console
->surface
) {
2017 g_width
= surface_width(active_console
->surface
);
2018 g_height
= surface_height(active_console
->surface
);
2020 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2023 s
->hw_ops
= &text_console_ops
;
2026 /* Set text attribute defaults */
2027 s
->t_attrib_default
.bold
= 0;
2028 s
->t_attrib_default
.uline
= 0;
2029 s
->t_attrib_default
.blink
= 0;
2030 s
->t_attrib_default
.invers
= 0;
2031 s
->t_attrib_default
.unvisible
= 0;
2032 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2033 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2034 /* set current text attributes to default */
2035 s
->t_attrib
= s
->t_attrib_default
;
2036 text_console_resize(s
);
2042 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2043 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2044 console_puts(chr
, (uint8_t*)msg
, len
);
2045 s
->t_attrib
= s
->t_attrib_default
;
2048 qemu_chr_be_generic_open(chr
);
2051 static CharDriverState
*text_console_init(ChardevVC
*vc
, Error
**errp
)
2053 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
2054 CharDriverState
*chr
;
2057 unsigned height
= 0;
2059 chr
= qemu_chr_alloc(common
, errp
);
2064 if (vc
->has_width
) {
2066 } else if (vc
->has_cols
) {
2067 width
= vc
->cols
* FONT_WIDTH
;
2070 if (vc
->has_height
) {
2071 height
= vc
->height
;
2072 } else if (vc
->has_rows
) {
2073 height
= vc
->rows
* FONT_HEIGHT
;
2076 trace_console_txt_new(width
, height
);
2077 if (width
== 0 || height
== 0) {
2078 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2080 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2081 s
->surface
= qemu_create_displaysurface(width
, height
);
2086 error_setg(errp
, "cannot create text console");
2092 chr
->chr_set_echo
= text_console_set_echo
;
2094 if (display_state
) {
2095 text_console_do_init(chr
, display_state
);
2100 static VcHandler
*vc_handler
= text_console_init
;
2102 static CharDriverState
*vc_init(const char *id
, ChardevBackend
*backend
,
2103 ChardevReturn
*ret
, bool *be_opened
,
2106 /* console/chardev init sometimes completes elsewhere in a 2nd
2107 * stage, so defer OPENED events until they are fully initialized
2110 return vc_handler(backend
->u
.vc
.data
, errp
);
2113 void register_vc_handler(VcHandler
*handler
)
2115 vc_handler
= handler
;
2118 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2120 DisplaySurface
*surface
;
2122 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2125 pixman_image_get_width(s
->surface
->image
) == width
&&
2126 pixman_image_get_height(s
->surface
->image
) == height
) {
2130 surface
= qemu_create_displaysurface(width
, height
);
2131 dpy_gfx_replace_surface(s
, surface
);
2134 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
2135 int dst_x
, int dst_y
, int w
, int h
)
2137 assert(con
->console_type
== GRAPHIC_CONSOLE
);
2138 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
2141 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2143 return console
->surface
;
2146 PixelFormat
qemu_default_pixelformat(int bpp
)
2148 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2149 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2153 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
2159 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2160 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2162 val
= qemu_opt_get_number(opts
, "width", 0);
2164 vc
->has_width
= true;
2168 val
= qemu_opt_get_number(opts
, "height", 0);
2170 vc
->has_height
= true;
2174 val
= qemu_opt_get_number(opts
, "cols", 0);
2176 vc
->has_cols
= true;
2180 val
= qemu_opt_get_number(opts
, "rows", 0);
2182 vc
->has_rows
= true;
2187 static const TypeInfo qemu_console_info
= {
2188 .name
= TYPE_QEMU_CONSOLE
,
2189 .parent
= TYPE_OBJECT
,
2190 .instance_size
= sizeof(QemuConsole
),
2191 .class_size
= sizeof(QemuConsoleClass
),
2195 static void register_types(void)
2197 type_register_static(&qemu_console_info
);
2198 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC
, qemu_chr_parse_vc
,
2202 type_init(register_types
);