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"
27 #include "ui/console.h"
28 #include "hw/qdev-core.h"
29 #include "qemu/timer.h"
30 #include "qmp-commands.h"
31 #include "chardev/char-fe.h"
33 #include "exec/memory.h"
35 #define DEFAULT_BACKSCROLL 512
36 #define CONSOLE_CURSOR_PERIOD 500
38 typedef struct TextAttributes
{
48 typedef struct TextCell
{
50 TextAttributes t_attrib
;
53 #define MAX_ESC_PARAMS 3
61 typedef struct QEMUFIFO
{
64 int count
, wptr
, rptr
;
67 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
71 l
= f
->buf_size
- f
->count
;
76 l
= f
->buf_size
- f
->wptr
;
79 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
81 if (f
->wptr
>= f
->buf_size
)
90 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
98 l
= f
->buf_size
- f
->rptr
;
101 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
103 if (f
->rptr
>= f
->buf_size
)
115 TEXT_CONSOLE_FIXED_SIZE
122 console_type_t console_type
;
124 DisplaySurface
*surface
;
126 DisplayChangeListener
*gl
;
130 /* Graphic console state. */
135 const GraphicHwOps
*hw_ops
;
138 /* Text console state */
142 int backscroll_height
;
144 int x_saved
, y_saved
;
147 TextAttributes t_attrib_default
; /* default text attributes */
148 TextAttributes t_attrib
; /* currently active text attributes */
150 int text_x
[2], text_y
[2], cursor_invalidate
;
159 int esc_params
[MAX_ESC_PARAMS
];
163 /* fifo for key pressed */
165 uint8_t out_fifo_buf
[16];
166 QEMUTimer
*kbd_timer
;
169 struct DisplayState
{
170 QEMUTimer
*gui_timer
;
171 uint64_t last_update
;
172 uint64_t update_interval
;
177 QLIST_HEAD(, DisplayChangeListener
) listeners
;
180 static DisplayState
*display_state
;
181 static QemuConsole
*active_console
;
182 static QemuConsole
**consoles
;
183 static int nb_consoles
= 0;
184 static bool cursor_visible_phase
;
185 static QEMUTimer
*cursor_timer
;
187 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
);
188 static void dpy_refresh(DisplayState
*s
);
189 static DisplayState
*get_alloc_displaystate(void);
190 static void text_console_update_cursor_timer(void);
191 static void text_console_update_cursor(void *opaque
);
193 static void gui_update(void *opaque
)
195 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
196 uint64_t dcl_interval
;
197 DisplayState
*ds
= opaque
;
198 DisplayChangeListener
*dcl
;
201 ds
->refreshing
= true;
203 ds
->refreshing
= false;
205 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
206 dcl_interval
= dcl
->update_interval
?
207 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
208 if (interval
> dcl_interval
) {
209 interval
= dcl_interval
;
212 if (ds
->update_interval
!= interval
) {
213 ds
->update_interval
= interval
;
214 for (i
= 0; i
< nb_consoles
; i
++) {
215 if (consoles
[i
]->hw_ops
->update_interval
) {
216 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
219 trace_console_refresh(interval
);
221 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
222 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
225 static void gui_setup_refresh(DisplayState
*ds
)
227 DisplayChangeListener
*dcl
;
228 bool need_timer
= false;
229 bool have_gfx
= false;
230 bool have_text
= false;
232 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
233 if (dcl
->ops
->dpy_refresh
!= NULL
) {
236 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
239 if (dcl
->ops
->dpy_text_update
!= NULL
) {
244 if (need_timer
&& ds
->gui_timer
== NULL
) {
245 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
246 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
248 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
249 timer_del(ds
->gui_timer
);
250 timer_free(ds
->gui_timer
);
251 ds
->gui_timer
= NULL
;
254 ds
->have_gfx
= have_gfx
;
255 ds
->have_text
= have_text
;
258 void graphic_hw_update(QemuConsole
*con
)
261 con
= active_console
;
263 if (con
&& con
->hw_ops
->gfx_update
) {
264 con
->hw_ops
->gfx_update(con
->hw
);
268 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
272 con
->gl_block
= block
;
273 if (con
->hw_ops
->gl_block
) {
274 con
->hw_ops
->gl_block(con
->hw
, block
);
278 int qemu_console_get_window_id(QemuConsole
*con
)
280 return con
->window_id
;
283 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
)
285 con
->window_id
= window_id
;
288 void graphic_hw_invalidate(QemuConsole
*con
)
291 con
= active_console
;
293 if (con
&& con
->hw_ops
->invalidate
) {
294 con
->hw_ops
->invalidate(con
->hw
);
298 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
301 int width
= pixman_image_get_width(ds
->image
);
302 int height
= pixman_image_get_height(ds
->image
);
307 pixman_image_t
*linebuf
;
309 trace_ppm_save(filename
, ds
);
310 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
312 error_setg(errp
, "failed to open file '%s': %s", filename
,
316 f
= fdopen(fd
, "wb");
317 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
322 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
323 for (y
= 0; y
< height
; y
++) {
324 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
326 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
327 pixman_image_get_stride(linebuf
), f
);
335 qemu_pixman_image_unref(linebuf
);
340 error_setg(errp
, "failed to write to file '%s': %s", filename
,
346 void qmp_screendump(const char *filename
, Error
**errp
)
348 QemuConsole
*con
= qemu_console_lookup_by_index(0);
349 DisplaySurface
*surface
;
352 error_setg(errp
, "There is no QemuConsole I can screendump from.");
356 graphic_hw_update(con
);
357 surface
= qemu_console_surface(con
);
358 ppm_save(filename
, surface
, errp
);
361 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
364 con
= active_console
;
366 if (con
&& con
->hw_ops
->text_update
) {
367 con
->hw_ops
->text_update(con
->hw
, chardata
);
371 static void vga_fill_rect(QemuConsole
*con
,
372 int posx
, int posy
, int width
, int height
,
373 pixman_color_t color
)
375 DisplaySurface
*surface
= qemu_console_surface(con
);
376 pixman_rectangle16_t rect
= {
377 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
380 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
384 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
385 static void vga_bitblt(QemuConsole
*con
,
386 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
388 DisplaySurface
*surface
= qemu_console_surface(con
);
390 pixman_image_composite(PIXMAN_OP_SRC
,
391 surface
->image
, NULL
, surface
->image
,
392 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
395 /***********************************************************/
396 /* basic char display */
398 #define FONT_HEIGHT 16
403 #define QEMU_RGB(r, g, b) \
404 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
406 static const pixman_color_t color_table_rgb
[2][8] = {
408 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
409 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
410 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
411 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
412 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
413 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
414 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
415 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
418 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
419 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
420 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
421 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
422 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
423 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
424 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
425 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
429 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
430 TextAttributes
*t_attrib
)
432 static pixman_image_t
*glyphs
[256];
433 DisplaySurface
*surface
= qemu_console_surface(s
);
434 pixman_color_t fgcol
, bgcol
;
436 if (t_attrib
->invers
) {
437 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
438 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
440 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
441 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
445 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
447 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
448 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
451 static void text_console_resize(QemuConsole
*s
)
453 TextCell
*cells
, *c
, *c1
;
454 int w1
, x
, y
, last_width
;
456 last_width
= s
->width
;
457 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
458 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
464 cells
= g_new(TextCell
, s
->width
* s
->total_height
);
465 for(y
= 0; y
< s
->total_height
; y
++) {
466 c
= &cells
[y
* s
->width
];
468 c1
= &s
->cells
[y
* last_width
];
469 for(x
= 0; x
< w1
; x
++) {
473 for(x
= w1
; x
< s
->width
; x
++) {
475 c
->t_attrib
= s
->t_attrib_default
;
483 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
485 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
486 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
487 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
488 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
491 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
493 if (!qemu_console_is_visible(s
)) {
496 if (s
->update_x0
> x
* FONT_WIDTH
)
497 s
->update_x0
= x
* FONT_WIDTH
;
498 if (s
->update_y0
> y
* FONT_HEIGHT
)
499 s
->update_y0
= y
* FONT_HEIGHT
;
500 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
501 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
502 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
503 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
506 static void update_xy(QemuConsole
*s
, int x
, int y
)
511 if (s
->ds
->have_text
) {
512 text_update_xy(s
, x
, y
);
515 y1
= (s
->y_base
+ y
) % s
->total_height
;
516 y2
= y1
- s
->y_displayed
;
518 y2
+= s
->total_height
;
520 if (y2
< s
->height
) {
521 c
= &s
->cells
[y1
* s
->width
+ x
];
522 vga_putcharxy(s
, x
, y2
, c
->ch
,
524 invalidate_xy(s
, x
, y2
);
528 static void console_show_cursor(QemuConsole
*s
, int show
)
534 if (s
->ds
->have_text
) {
535 s
->cursor_invalidate
= 1;
541 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
542 y
= y1
- s
->y_displayed
;
544 y
+= s
->total_height
;
547 c
= &s
->cells
[y1
* s
->width
+ x
];
548 if (show
&& cursor_visible_phase
) {
549 TextAttributes t_attrib
= s
->t_attrib_default
;
550 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
551 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
553 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
555 invalidate_xy(s
, x
, y
);
559 static void console_refresh(QemuConsole
*s
)
561 DisplaySurface
*surface
= qemu_console_surface(s
);
565 if (s
->ds
->have_text
) {
568 s
->text_x
[1] = s
->width
- 1;
569 s
->text_y
[1] = s
->height
- 1;
570 s
->cursor_invalidate
= 1;
573 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
574 color_table_rgb
[0][QEMU_COLOR_BLACK
]);
576 for (y
= 0; y
< s
->height
; y
++) {
577 c
= s
->cells
+ y1
* s
->width
;
578 for (x
= 0; x
< s
->width
; x
++) {
579 vga_putcharxy(s
, x
, y
, c
->ch
,
583 if (++y1
== s
->total_height
) {
587 console_show_cursor(s
, 1);
588 dpy_gfx_update(s
, 0, 0,
589 surface_width(surface
), surface_height(surface
));
592 static void console_scroll(QemuConsole
*s
, int ydelta
)
597 for(i
= 0; i
< ydelta
; i
++) {
598 if (s
->y_displayed
== s
->y_base
)
600 if (++s
->y_displayed
== s
->total_height
)
605 i
= s
->backscroll_height
;
606 if (i
> s
->total_height
- s
->height
)
607 i
= s
->total_height
- s
->height
;
610 y1
+= s
->total_height
;
611 for(i
= 0; i
< ydelta
; i
++) {
612 if (s
->y_displayed
== y1
)
614 if (--s
->y_displayed
< 0)
615 s
->y_displayed
= s
->total_height
- 1;
621 static void console_put_lf(QemuConsole
*s
)
627 if (s
->y
>= s
->height
) {
628 s
->y
= s
->height
- 1;
630 if (s
->y_displayed
== s
->y_base
) {
631 if (++s
->y_displayed
== s
->total_height
)
634 if (++s
->y_base
== s
->total_height
)
636 if (s
->backscroll_height
< s
->total_height
)
637 s
->backscroll_height
++;
638 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
639 c
= &s
->cells
[y1
* s
->width
];
640 for(x
= 0; x
< s
->width
; x
++) {
642 c
->t_attrib
= s
->t_attrib_default
;
645 if (s
->y_displayed
== s
->y_base
) {
646 if (s
->ds
->have_text
) {
649 s
->text_x
[1] = s
->width
- 1;
650 s
->text_y
[1] = s
->height
- 1;
653 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
654 s
->width
* FONT_WIDTH
,
655 (s
->height
- 1) * FONT_HEIGHT
);
656 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
657 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
658 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
661 s
->update_x1
= s
->width
* FONT_WIDTH
;
662 s
->update_y1
= s
->height
* FONT_HEIGHT
;
667 /* Set console attributes depending on the current escape codes.
668 * NOTE: I know this code is not very efficient (checking every color for it
669 * self) but it is more readable and better maintainable.
671 static void console_handle_escape(QemuConsole
*s
)
675 for (i
=0; i
<s
->nb_esc_params
; i
++) {
676 switch (s
->esc_params
[i
]) {
677 case 0: /* reset all console attributes to default */
678 s
->t_attrib
= s
->t_attrib_default
;
681 s
->t_attrib
.bold
= 1;
684 s
->t_attrib
.uline
= 1;
687 s
->t_attrib
.blink
= 1;
690 s
->t_attrib
.invers
= 1;
693 s
->t_attrib
.unvisible
= 1;
696 s
->t_attrib
.bold
= 0;
699 s
->t_attrib
.uline
= 0;
702 s
->t_attrib
.blink
= 0;
705 s
->t_attrib
.invers
= 0;
708 s
->t_attrib
.unvisible
= 0;
710 /* set foreground color */
712 s
->t_attrib
.fgcol
= QEMU_COLOR_BLACK
;
715 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
718 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
721 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
724 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
727 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
730 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
733 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
735 /* set background color */
737 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
740 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
743 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
746 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
749 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
752 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
755 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
758 s
->t_attrib
.bgcol
= QEMU_COLOR_WHITE
;
764 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
766 int y1
= (s
->y_base
+ y
) % s
->total_height
;
767 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
769 c
->t_attrib
= s
->t_attrib_default
;
773 static void console_put_one(QemuConsole
*s
, int ch
)
777 if (s
->x
>= s
->width
) {
782 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
783 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
785 c
->t_attrib
= s
->t_attrib
;
786 update_xy(s
, s
->x
, s
->y
);
790 static void console_respond_str(QemuConsole
*s
, const char *buf
)
793 console_put_one(s
, *buf
);
798 /* set cursor, checking bounds */
799 static void set_cursor(QemuConsole
*s
, int x
, int y
)
807 if (y
>= s
->height
) {
818 static void console_putchar(QemuConsole
*s
, int ch
)
827 case '\r': /* carriage return */
830 case '\n': /* newline */
833 case '\b': /* backspace */
837 case '\t': /* tabspace */
838 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
842 s
->x
= s
->x
+ (8 - (s
->x
% 8));
845 case '\a': /* alert aka. bell */
846 /* TODO: has to be implemented */
849 /* SI (shift in), character set 0 (ignored) */
852 /* SO (shift out), character set 1 (ignored) */
854 case 27: /* esc (introducing an escape sequence) */
855 s
->state
= TTY_STATE_ESC
;
858 console_put_one(s
, ch
);
862 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
864 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
865 s
->esc_params
[i
] = 0;
866 s
->nb_esc_params
= 0;
867 s
->state
= TTY_STATE_CSI
;
869 s
->state
= TTY_STATE_NORM
;
872 case TTY_STATE_CSI
: /* handle escape sequence parameters */
873 if (ch
>= '0' && ch
<= '9') {
874 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
875 int *param
= &s
->esc_params
[s
->nb_esc_params
];
876 int digit
= (ch
- '0');
878 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
879 *param
* 10 + digit
: INT_MAX
;
882 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
884 if (ch
== ';' || ch
== '?') {
887 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
888 ch
, s
->nb_esc_params
);
889 s
->state
= TTY_STATE_NORM
;
893 if (s
->esc_params
[0] == 0) {
894 s
->esc_params
[0] = 1;
896 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
899 /* move cursor down */
900 if (s
->esc_params
[0] == 0) {
901 s
->esc_params
[0] = 1;
903 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
906 /* move cursor right */
907 if (s
->esc_params
[0] == 0) {
908 s
->esc_params
[0] = 1;
910 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
913 /* move cursor left */
914 if (s
->esc_params
[0] == 0) {
915 s
->esc_params
[0] = 1;
917 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
920 /* move cursor to column */
921 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
925 /* move cursor to row, column */
926 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
929 switch (s
->esc_params
[0]) {
931 /* clear to end of screen */
932 for (y
= s
->y
; y
< s
->height
; y
++) {
933 for (x
= 0; x
< s
->width
; x
++) {
934 if (y
== s
->y
&& x
< s
->x
) {
937 console_clear_xy(s
, x
, y
);
942 /* clear from beginning of screen */
943 for (y
= 0; y
<= s
->y
; y
++) {
944 for (x
= 0; x
< s
->width
; x
++) {
945 if (y
== s
->y
&& x
> s
->x
) {
948 console_clear_xy(s
, x
, y
);
953 /* clear entire screen */
954 for (y
= 0; y
<= s
->height
; y
++) {
955 for (x
= 0; x
< s
->width
; x
++) {
956 console_clear_xy(s
, x
, y
);
963 switch (s
->esc_params
[0]) {
966 for (x
= s
->x
; x
< s
->width
; x
++) {
967 console_clear_xy(s
, x
, s
->y
);
971 /* clear from beginning of line */
972 for (x
= 0; x
<= s
->x
; x
++) {
973 console_clear_xy(s
, x
, s
->y
);
977 /* clear entire line */
978 for(x
= 0; x
< s
->width
; x
++) {
979 console_clear_xy(s
, x
, s
->y
);
985 console_handle_escape(s
);
988 switch (s
->esc_params
[0]) {
990 /* report console status (always succeed)*/
991 console_respond_str(s
, "\033[0n");
994 /* report cursor position */
995 sprintf(response
, "\033[%d;%dR",
996 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
998 console_respond_str(s
, response
);
1003 /* save cursor position */
1008 /* restore cursor position */
1013 trace_console_putchar_unhandled(ch
);
1021 void console_select(unsigned int index
)
1023 DisplayChangeListener
*dcl
;
1026 trace_console_select(index
);
1027 s
= qemu_console_lookup_by_index(index
);
1029 DisplayState
*ds
= s
->ds
;
1033 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1034 if (dcl
->con
!= NULL
) {
1037 if (dcl
->ops
->dpy_gfx_switch
) {
1038 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1041 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1042 surface_height(s
->surface
));
1044 if (ds
->have_text
) {
1045 dpy_text_resize(s
, s
->width
, s
->height
);
1047 text_console_update_cursor(NULL
);
1051 typedef struct VCChardev
{
1053 QemuConsole
*console
;
1056 #define TYPE_CHARDEV_VC "chardev-vc"
1057 #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1059 static int vc_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
1061 VCChardev
*drv
= VC_CHARDEV(chr
);
1062 QemuConsole
*s
= drv
->console
;
1069 s
->update_x0
= s
->width
* FONT_WIDTH
;
1070 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1073 console_show_cursor(s
, 0);
1074 for(i
= 0; i
< len
; i
++) {
1075 console_putchar(s
, buf
[i
]);
1077 console_show_cursor(s
, 1);
1078 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1079 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1080 s
->update_x1
- s
->update_x0
,
1081 s
->update_y1
- s
->update_y0
);
1086 static void kbd_send_chars(void *opaque
)
1088 QemuConsole
*s
= opaque
;
1092 len
= qemu_chr_be_can_write(s
->chr
);
1093 if (len
> s
->out_fifo
.count
)
1094 len
= s
->out_fifo
.count
;
1096 if (len
> sizeof(buf
))
1098 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1099 qemu_chr_be_write(s
->chr
, buf
, len
);
1101 /* characters are pending: we send them a bit later (XXX:
1102 horrible, should change char device API) */
1103 if (s
->out_fifo
.count
> 0) {
1104 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1108 /* called when an ascii key is pressed */
1109 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1111 uint8_t buf
[16], *q
;
1115 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1119 case QEMU_KEY_CTRL_UP
:
1120 console_scroll(s
, -1);
1122 case QEMU_KEY_CTRL_DOWN
:
1123 console_scroll(s
, 1);
1125 case QEMU_KEY_CTRL_PAGEUP
:
1126 console_scroll(s
, -10);
1128 case QEMU_KEY_CTRL_PAGEDOWN
:
1129 console_scroll(s
, 10);
1132 /* convert the QEMU keysym to VT100 key string */
1134 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1137 c
= keysym
- 0xe100;
1139 *q
++ = '0' + (c
/ 10);
1140 *q
++ = '0' + (c
% 10);
1142 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1145 *q
++ = keysym
& 0xff;
1146 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1147 vc_chr_write(s
->chr
, (const uint8_t *) "\r", 1);
1153 vc_chr_write(s
->chr
, buf
, q
- buf
);
1156 if (be
&& be
->chr_read
) {
1157 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1164 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1165 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1166 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1167 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1168 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1169 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1170 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1171 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1172 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1173 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1174 [Q_KEY_CODE_BACKSPACE
] = QEMU_KEY_BACKSPACE
,
1177 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1181 keysym
= qcode_to_keysym
[qcode
];
1185 kbd_put_keysym_console(s
, keysym
);
1189 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1193 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1194 kbd_put_keysym_console(s
, str
[i
]);
1198 void kbd_put_keysym(int keysym
)
1200 kbd_put_keysym_console(active_console
, keysym
);
1203 static void text_console_invalidate(void *opaque
)
1205 QemuConsole
*s
= (QemuConsole
*) opaque
;
1207 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1208 text_console_resize(s
);
1213 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1215 QemuConsole
*s
= (QemuConsole
*) opaque
;
1218 if (s
->text_x
[0] <= s
->text_x
[1]) {
1219 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1220 chardata
+= s
->text_y
[0] * s
->width
;
1221 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1222 for (j
= 0; j
< s
->width
; j
++, src
++) {
1223 console_write_ch(chardata
++,
1224 ATTR2CHTYPE(s
->cells
[src
].ch
,
1225 s
->cells
[src
].t_attrib
.fgcol
,
1226 s
->cells
[src
].t_attrib
.bgcol
,
1227 s
->cells
[src
].t_attrib
.bold
));
1229 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1230 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1231 s
->text_x
[0] = s
->width
;
1232 s
->text_y
[0] = s
->height
;
1236 if (s
->cursor_invalidate
) {
1237 dpy_text_cursor(s
, s
->x
, s
->y
);
1238 s
->cursor_invalidate
= 0;
1242 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1249 obj
= object_new(TYPE_QEMU_CONSOLE
);
1250 s
= QEMU_CONSOLE(obj
);
1252 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1253 (Object
**)&s
->device
,
1254 object_property_allow_set_link
,
1255 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1257 object_property_add_uint32_ptr(obj
, "head",
1258 &s
->head
, &error_abort
);
1260 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1261 (console_type
== GRAPHIC_CONSOLE
))) {
1265 s
->console_type
= console_type
;
1267 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1268 if (console_type
!= GRAPHIC_CONSOLE
) {
1269 s
->index
= nb_consoles
;
1270 consoles
[nb_consoles
++] = s
;
1272 /* HACK: Put graphical consoles before text consoles. */
1273 for (i
= nb_consoles
; i
> 0; i
--) {
1274 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1276 consoles
[i
] = consoles
[i
- 1];
1277 consoles
[i
]->index
= i
;
1286 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1288 qemu_pixman_image_unref(surface
->image
);
1289 surface
->image
= NULL
;
1291 surface
->format
= PIXMAN_x8r8g8b8
;
1292 surface
->image
= pixman_image_create_bits(surface
->format
,
1295 assert(surface
->image
!= NULL
);
1297 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1300 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1302 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1304 trace_displaysurface_create(surface
, width
, height
);
1305 qemu_alloc_display(surface
, width
, height
);
1309 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1310 pixman_format_code_t format
,
1311 int linesize
, uint8_t *data
)
1313 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1315 trace_displaysurface_create_from(surface
, width
, height
, format
);
1316 surface
->format
= format
;
1317 surface
->image
= pixman_image_create_bits(surface
->format
,
1319 (void *)data
, linesize
);
1320 assert(surface
->image
!= NULL
);
1325 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
)
1327 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1329 trace_displaysurface_create_pixman(surface
);
1330 surface
->format
= pixman_image_get_format(image
);
1331 surface
->image
= pixman_image_ref(image
);
1336 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1339 void *data
= pixman_image_get_data(image
);
1340 uint32_t size
= pixman_image_get_stride(image
) *
1341 pixman_image_get_height(image
);
1342 cpu_physical_memory_unmap(data
, size
, 0, 0);
1345 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1346 pixman_format_code_t format
,
1347 int linesize
, uint64_t addr
)
1349 DisplaySurface
*surface
;
1353 if (linesize
== 0) {
1354 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1357 size
= (hwaddr
)linesize
* height
;
1358 data
= cpu_physical_memory_map(addr
, &size
, 0);
1359 if (size
!= (hwaddr
)linesize
* height
) {
1360 cpu_physical_memory_unmap(data
, size
, 0, 0);
1364 surface
= qemu_create_displaysurface_from
1365 (width
, height
, format
, linesize
, data
);
1366 pixman_image_set_destroy_function
1367 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1372 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1375 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1376 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1377 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1378 pixman_image_t
*glyph
;
1382 x
= (w
/ FONT_WIDTH
- len
) / 2;
1383 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1384 for (i
= 0; i
< len
; i
++) {
1385 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1386 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1387 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1388 qemu_pixman_image_unref(glyph
);
1393 void qemu_free_displaysurface(DisplaySurface
*surface
)
1395 if (surface
== NULL
) {
1398 trace_displaysurface_free(surface
);
1399 qemu_pixman_image_unref(surface
->image
);
1403 bool console_has_gl(QemuConsole
*con
)
1405 return con
->gl
!= NULL
;
1408 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1410 static const char nodev
[] =
1411 "This VM has no graphic display device.";
1412 static DisplaySurface
*dummy
;
1417 if (dcl
->ops
->dpy_gl_ctx_create
) {
1418 /* display has opengl support */
1421 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1422 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1428 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1429 dcl
->ds
= get_alloc_displaystate();
1430 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1431 gui_setup_refresh(dcl
->ds
);
1436 con
= active_console
;
1438 if (dcl
->ops
->dpy_gfx_switch
) {
1440 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1443 dummy
= qemu_create_message_surface(640, 480, nodev
);
1445 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1448 text_console_update_cursor(NULL
);
1451 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1454 DisplayState
*ds
= dcl
->ds
;
1456 dcl
->update_interval
= interval
;
1457 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1458 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1462 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1464 DisplayState
*ds
= dcl
->ds
;
1465 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1469 QLIST_REMOVE(dcl
, next
);
1470 gui_setup_refresh(ds
);
1473 static void dpy_set_ui_info_timer(void *opaque
)
1475 QemuConsole
*con
= opaque
;
1477 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1480 bool dpy_ui_info_supported(QemuConsole
*con
)
1482 return con
->hw_ops
->ui_info
!= NULL
;
1485 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1487 assert(con
!= NULL
);
1489 if (!dpy_ui_info_supported(con
)) {
1492 if (memcmp(&con
->ui_info
, info
, sizeof(con
->ui_info
)) == 0) {
1493 /* nothing changed -- ignore */
1498 * Typically we get a flood of these as the user resizes the window.
1499 * Wait until the dust has settled (one second without updates), then
1500 * go notify the guest.
1502 con
->ui_info
= *info
;
1503 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1507 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1509 DisplayState
*s
= con
->ds
;
1510 DisplayChangeListener
*dcl
;
1515 width
= surface_width(con
->surface
);
1516 height
= surface_height(con
->surface
);
1522 w
= MIN(w
, width
- x
);
1523 h
= MIN(h
, height
- y
);
1525 if (!qemu_console_is_visible(con
)) {
1528 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1529 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1532 if (dcl
->ops
->dpy_gfx_update
) {
1533 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1538 void dpy_gfx_replace_surface(QemuConsole
*con
,
1539 DisplaySurface
*surface
)
1541 DisplayState
*s
= con
->ds
;
1542 DisplaySurface
*old_surface
= con
->surface
;
1543 DisplayChangeListener
*dcl
;
1545 assert(old_surface
!= surface
|| surface
== NULL
);
1547 con
->surface
= surface
;
1548 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1549 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1552 if (dcl
->ops
->dpy_gfx_switch
) {
1553 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1556 qemu_free_displaysurface(old_surface
);
1559 bool dpy_gfx_check_format(QemuConsole
*con
,
1560 pixman_format_code_t format
)
1562 DisplayChangeListener
*dcl
;
1563 DisplayState
*s
= con
->ds
;
1565 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1566 if (dcl
->con
&& dcl
->con
!= con
) {
1567 /* dcl bound to another console -> skip */
1570 if (dcl
->ops
->dpy_gfx_check_format
) {
1571 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1575 /* default is to whitelist native 32 bpp only */
1576 if (format
!= qemu_default_pixman_format(32, true)) {
1584 static void dpy_refresh(DisplayState
*s
)
1586 DisplayChangeListener
*dcl
;
1588 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1589 if (dcl
->ops
->dpy_refresh
) {
1590 dcl
->ops
->dpy_refresh(dcl
);
1595 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1597 DisplayState
*s
= con
->ds
;
1598 DisplayChangeListener
*dcl
;
1600 if (!qemu_console_is_visible(con
)) {
1603 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1604 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1607 if (dcl
->ops
->dpy_text_cursor
) {
1608 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1613 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1615 DisplayState
*s
= con
->ds
;
1616 DisplayChangeListener
*dcl
;
1618 if (!qemu_console_is_visible(con
)) {
1621 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1622 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1625 if (dcl
->ops
->dpy_text_update
) {
1626 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1631 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1633 DisplayState
*s
= con
->ds
;
1634 DisplayChangeListener
*dcl
;
1636 if (!qemu_console_is_visible(con
)) {
1639 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1640 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1643 if (dcl
->ops
->dpy_text_resize
) {
1644 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1649 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1651 DisplayState
*s
= con
->ds
;
1652 DisplayChangeListener
*dcl
;
1654 if (!qemu_console_is_visible(con
)) {
1657 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1658 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1661 if (dcl
->ops
->dpy_mouse_set
) {
1662 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1667 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1669 DisplayState
*s
= con
->ds
;
1670 DisplayChangeListener
*dcl
;
1672 if (!qemu_console_is_visible(con
)) {
1675 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1676 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1679 if (dcl
->ops
->dpy_cursor_define
) {
1680 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1685 bool dpy_cursor_define_supported(QemuConsole
*con
)
1687 DisplayState
*s
= con
->ds
;
1688 DisplayChangeListener
*dcl
;
1690 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1691 if (dcl
->ops
->dpy_cursor_define
) {
1698 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1699 struct QEMUGLParams
*qparams
)
1702 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1705 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1708 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1711 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1714 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1717 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1720 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1723 void dpy_gl_scanout_disable(QemuConsole
*con
)
1726 if (con
->gl
->ops
->dpy_gl_scanout_disable
) {
1727 con
->gl
->ops
->dpy_gl_scanout_disable(con
->gl
);
1729 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, 0, false, 0, 0,
1734 void dpy_gl_scanout_texture(QemuConsole
*con
,
1735 uint32_t backing_id
,
1736 bool backing_y_0_top
,
1737 uint32_t backing_width
,
1738 uint32_t backing_height
,
1739 uint32_t x
, uint32_t y
,
1740 uint32_t width
, uint32_t height
)
1743 con
->gl
->ops
->dpy_gl_scanout_texture(con
->gl
, backing_id
,
1745 backing_width
, backing_height
,
1746 x
, y
, width
, height
);
1749 void dpy_gl_update(QemuConsole
*con
,
1750 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1753 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1756 /***********************************************************/
1757 /* register display */
1759 /* console.c internal use only */
1760 static DisplayState
*get_alloc_displaystate(void)
1762 if (!display_state
) {
1763 display_state
= g_new0(DisplayState
, 1);
1764 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1765 text_console_update_cursor
, NULL
);
1767 return display_state
;
1771 * Called by main(), after creating QemuConsoles
1772 * and before initializing ui (sdl/vnc/...).
1774 DisplayState
*init_displaystate(void)
1779 get_alloc_displaystate();
1780 for (i
= 0; i
< nb_consoles
; i
++) {
1781 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1782 consoles
[i
]->ds
== NULL
) {
1783 text_console_do_init(consoles
[i
]->chr
, display_state
);
1786 /* Hook up into the qom tree here (not in new_console()), once
1787 * all QemuConsoles are created and the order / numbering
1788 * doesn't change any more */
1789 name
= g_strdup_printf("console[%d]", i
);
1790 object_property_add_child(container_get(object_get_root(), "/backend"),
1791 name
, OBJECT(consoles
[i
]), &error_abort
);
1795 return display_state
;
1798 void graphic_console_set_hwops(QemuConsole
*con
,
1799 const GraphicHwOps
*hw_ops
,
1802 con
->hw_ops
= hw_ops
;
1806 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1807 const GraphicHwOps
*hw_ops
,
1810 static const char noinit
[] =
1811 "Guest has not initialized the display (yet).";
1817 ds
= get_alloc_displaystate();
1818 trace_console_gfx_new();
1819 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1820 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1821 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1823 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1827 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1831 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1833 if (index
>= nb_consoles
) {
1836 return consoles
[index
];
1839 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1845 for (i
= 0; i
< nb_consoles
; i
++) {
1849 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1850 "device", &error_abort
);
1851 if (DEVICE(obj
) != dev
) {
1854 h
= object_property_get_uint(OBJECT(consoles
[i
]),
1855 "head", &error_abort
);
1864 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1865 uint32_t head
, Error
**errp
)
1870 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1872 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1873 "Device '%s' not found", device_id
);
1877 con
= qemu_console_lookup_by_device(dev
, head
);
1879 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1887 bool qemu_console_is_visible(QemuConsole
*con
)
1889 return (con
== active_console
) || (con
->dcls
> 0);
1892 bool qemu_console_is_graphic(QemuConsole
*con
)
1895 con
= active_console
;
1897 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1900 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1903 con
= active_console
;
1905 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1908 bool qemu_console_is_gl_blocked(QemuConsole
*con
)
1910 assert(con
!= NULL
);
1911 return con
->gl_block
;
1914 char *qemu_console_get_label(QemuConsole
*con
)
1916 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1918 return g_strdup(object_get_typename(con
->device
));
1920 return g_strdup("VGA");
1922 if (con
->chr
&& con
->chr
->label
) {
1923 return g_strdup(con
->chr
->label
);
1925 return g_strdup_printf("vc%d", con
->index
);
1929 int qemu_console_get_index(QemuConsole
*con
)
1932 con
= active_console
;
1934 return con
? con
->index
: -1;
1937 uint32_t qemu_console_get_head(QemuConsole
*con
)
1940 con
= active_console
;
1942 return con
? con
->head
: -1;
1945 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1947 assert(con
!= NULL
);
1948 return &con
->ui_info
;
1951 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1954 con
= active_console
;
1956 return con
? surface_width(con
->surface
) : fallback
;
1959 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1962 con
= active_console
;
1964 return con
? surface_height(con
->surface
) : fallback
;
1967 static void vc_chr_set_echo(Chardev
*chr
, bool echo
)
1969 VCChardev
*drv
= VC_CHARDEV(chr
);
1970 QemuConsole
*s
= drv
->console
;
1975 static void text_console_update_cursor_timer(void)
1977 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1978 + CONSOLE_CURSOR_PERIOD
/ 2);
1981 static void text_console_update_cursor(void *opaque
)
1986 cursor_visible_phase
= !cursor_visible_phase
;
1988 for (i
= 0; i
< nb_consoles
; i
++) {
1990 if (qemu_console_is_graphic(s
) ||
1991 !qemu_console_is_visible(s
)) {
1995 graphic_hw_invalidate(s
);
1999 text_console_update_cursor_timer();
2003 static const GraphicHwOps text_console_ops
= {
2004 .invalidate
= text_console_invalidate
,
2005 .text_update
= text_console_update
,
2008 static void text_console_do_init(Chardev
*chr
, DisplayState
*ds
)
2010 VCChardev
*drv
= VC_CHARDEV(chr
);
2011 QemuConsole
*s
= drv
->console
;
2012 int g_width
= 80 * FONT_WIDTH
;
2013 int g_height
= 24 * FONT_HEIGHT
;
2015 s
->out_fifo
.buf
= s
->out_fifo_buf
;
2016 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
2017 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
2022 s
->total_height
= DEFAULT_BACKSCROLL
;
2026 if (active_console
&& active_console
->surface
) {
2027 g_width
= surface_width(active_console
->surface
);
2028 g_height
= surface_height(active_console
->surface
);
2030 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
2033 s
->hw_ops
= &text_console_ops
;
2036 /* Set text attribute defaults */
2037 s
->t_attrib_default
.bold
= 0;
2038 s
->t_attrib_default
.uline
= 0;
2039 s
->t_attrib_default
.blink
= 0;
2040 s
->t_attrib_default
.invers
= 0;
2041 s
->t_attrib_default
.unvisible
= 0;
2042 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
2043 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
2044 /* set current text attributes to default */
2045 s
->t_attrib
= s
->t_attrib_default
;
2046 text_console_resize(s
);
2052 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2053 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2054 vc_chr_write(chr
, (uint8_t *)msg
, len
);
2055 s
->t_attrib
= s
->t_attrib_default
;
2058 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
2061 static void vc_chr_open(Chardev
*chr
,
2062 ChardevBackend
*backend
,
2066 ChardevVC
*vc
= backend
->u
.vc
.data
;
2067 VCChardev
*drv
= VC_CHARDEV(chr
);
2070 unsigned height
= 0;
2072 if (vc
->has_width
) {
2074 } else if (vc
->has_cols
) {
2075 width
= vc
->cols
* FONT_WIDTH
;
2078 if (vc
->has_height
) {
2079 height
= vc
->height
;
2080 } else if (vc
->has_rows
) {
2081 height
= vc
->rows
* FONT_HEIGHT
;
2084 trace_console_txt_new(width
, height
);
2085 if (width
== 0 || height
== 0) {
2086 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2088 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2089 s
->surface
= qemu_create_displaysurface(width
, height
);
2093 error_setg(errp
, "cannot create text console");
2100 if (display_state
) {
2101 text_console_do_init(chr
, display_state
);
2104 /* console/chardev init sometimes completes elsewhere in a 2nd
2105 * stage, so defer OPENED events until they are fully initialized
2110 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2112 DisplaySurface
*surface
;
2114 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2116 if (s
->surface
&& (s
->surface
->flags
& QEMU_ALLOCATED_FLAG
) &&
2117 pixman_image_get_width(s
->surface
->image
) == width
&&
2118 pixman_image_get_height(s
->surface
->image
) == height
) {
2122 surface
= qemu_create_displaysurface(width
, height
);
2123 dpy_gfx_replace_surface(s
, surface
);
2126 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2128 return console
->surface
;
2131 PixelFormat
qemu_default_pixelformat(int bpp
)
2133 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2134 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2138 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
)
2143 backend
->type
= CHARDEV_BACKEND_KIND_VC
;
2144 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2145 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2147 val
= qemu_opt_get_number(opts
, "width", 0);
2149 vc
->has_width
= true;
2153 val
= qemu_opt_get_number(opts
, "height", 0);
2155 vc
->has_height
= true;
2159 val
= qemu_opt_get_number(opts
, "cols", 0);
2161 vc
->has_cols
= true;
2165 val
= qemu_opt_get_number(opts
, "rows", 0);
2167 vc
->has_rows
= true;
2172 static const TypeInfo qemu_console_info
= {
2173 .name
= TYPE_QEMU_CONSOLE
,
2174 .parent
= TYPE_OBJECT
,
2175 .instance_size
= sizeof(QemuConsole
),
2176 .class_size
= sizeof(QemuConsoleClass
),
2179 static void char_vc_class_init(ObjectClass
*oc
, void *data
)
2181 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2183 cc
->parse
= qemu_chr_parse_vc
;
2184 cc
->open
= vc_chr_open
;
2185 cc
->chr_write
= vc_chr_write
;
2186 cc
->chr_set_echo
= vc_chr_set_echo
;
2189 static const TypeInfo char_vc_type_info
= {
2190 .name
= TYPE_CHARDEV_VC
,
2191 .parent
= TYPE_CHARDEV
,
2192 .instance_size
= sizeof(VCChardev
),
2193 .class_init
= char_vc_class_init
,
2196 void qemu_console_early_init(void)
2198 /* set the default vc driver */
2199 if (!object_class_by_name(TYPE_CHARDEV_VC
)) {
2200 type_register(&char_vc_type_info
);
2204 static void register_types(void)
2206 type_register_static(&qemu_console_info
);
2209 type_init(register_types
);