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-common.h"
25 #include "ui/console.h"
26 #include "hw/qdev-core.h"
27 #include "qemu/timer.h"
28 #include "qmp-commands.h"
29 #include "sysemu/char.h"
31 #include "exec/memory.h"
33 #define DEFAULT_BACKSCROLL 512
34 #define CONSOLE_CURSOR_PERIOD 500
36 typedef struct TextAttributes
{
46 typedef struct TextCell
{
48 TextAttributes t_attrib
;
51 #define MAX_ESC_PARAMS 3
59 typedef struct QEMUFIFO
{
62 int count
, wptr
, rptr
;
65 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
69 l
= f
->buf_size
- f
->count
;
74 l
= f
->buf_size
- f
->wptr
;
77 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
79 if (f
->wptr
>= f
->buf_size
)
88 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
96 l
= f
->buf_size
- f
->rptr
;
99 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
101 if (f
->rptr
>= f
->buf_size
)
113 TEXT_CONSOLE_FIXED_SIZE
120 console_type_t console_type
;
122 DisplaySurface
*surface
;
125 /* Graphic console state. */
129 const GraphicHwOps
*hw_ops
;
132 /* Text console state */
136 int backscroll_height
;
138 int x_saved
, y_saved
;
141 TextAttributes t_attrib_default
; /* default text attributes */
142 TextAttributes t_attrib
; /* currently active text attributes */
144 int text_x
[2], text_y
[2], cursor_invalidate
;
153 int esc_params
[MAX_ESC_PARAMS
];
156 CharDriverState
*chr
;
157 /* fifo for key pressed */
159 uint8_t out_fifo_buf
[16];
160 QEMUTimer
*kbd_timer
;
163 struct DisplayState
{
164 QEMUTimer
*gui_timer
;
165 uint64_t last_update
;
166 uint64_t update_interval
;
171 QLIST_HEAD(, DisplayChangeListener
) listeners
;
174 static DisplayState
*display_state
;
175 static QemuConsole
*active_console
;
176 static QemuConsole
**consoles
;
177 static int nb_consoles
= 0;
178 static bool cursor_visible_phase
;
179 static QEMUTimer
*cursor_timer
;
181 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
182 static void dpy_refresh(DisplayState
*s
);
183 static DisplayState
*get_alloc_displaystate(void);
184 static void text_console_update_cursor_timer(void);
185 static void text_console_update_cursor(void *opaque
);
187 static void gui_update(void *opaque
)
189 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
190 uint64_t dcl_interval
;
191 DisplayState
*ds
= opaque
;
192 DisplayChangeListener
*dcl
;
195 ds
->refreshing
= true;
197 ds
->refreshing
= false;
199 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
200 dcl_interval
= dcl
->update_interval
?
201 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
202 if (interval
> dcl_interval
) {
203 interval
= dcl_interval
;
206 if (ds
->update_interval
!= interval
) {
207 ds
->update_interval
= interval
;
208 for (i
= 0; i
< nb_consoles
; i
++) {
209 if (consoles
[i
]->hw_ops
->update_interval
) {
210 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
213 trace_console_refresh(interval
);
215 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
216 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
219 static void gui_setup_refresh(DisplayState
*ds
)
221 DisplayChangeListener
*dcl
;
222 bool need_timer
= false;
223 bool have_gfx
= false;
224 bool have_text
= false;
226 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
227 if (dcl
->ops
->dpy_refresh
!= NULL
) {
230 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
233 if (dcl
->ops
->dpy_text_update
!= NULL
) {
238 if (need_timer
&& ds
->gui_timer
== NULL
) {
239 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
240 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
242 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
243 timer_del(ds
->gui_timer
);
244 timer_free(ds
->gui_timer
);
245 ds
->gui_timer
= NULL
;
248 ds
->have_gfx
= have_gfx
;
249 ds
->have_text
= have_text
;
252 void graphic_hw_update(QemuConsole
*con
)
255 con
= active_console
;
257 if (con
&& con
->hw_ops
->gfx_update
) {
258 con
->hw_ops
->gfx_update(con
->hw
);
262 void graphic_hw_invalidate(QemuConsole
*con
)
265 con
= active_console
;
267 if (con
&& con
->hw_ops
->invalidate
) {
268 con
->hw_ops
->invalidate(con
->hw
);
272 static void ppm_save(const char *filename
, struct DisplaySurface
*ds
,
275 int width
= pixman_image_get_width(ds
->image
);
276 int height
= pixman_image_get_height(ds
->image
);
281 pixman_image_t
*linebuf
;
283 trace_ppm_save(filename
, ds
);
284 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
286 error_setg(errp
, "failed to open file '%s': %s", filename
,
290 f
= fdopen(fd
, "wb");
291 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
296 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
297 for (y
= 0; y
< height
; y
++) {
298 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
300 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
301 pixman_image_get_stride(linebuf
), f
);
309 qemu_pixman_image_unref(linebuf
);
314 error_setg(errp
, "failed to write to file '%s': %s", filename
,
320 void qmp_screendump(const char *filename
, Error
**errp
)
322 QemuConsole
*con
= qemu_console_lookup_by_index(0);
323 DisplaySurface
*surface
;
326 error_setg(errp
, "There is no QemuConsole I can screendump from.");
330 graphic_hw_update(con
);
331 surface
= qemu_console_surface(con
);
332 ppm_save(filename
, surface
, errp
);
335 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
338 con
= active_console
;
340 if (con
&& con
->hw_ops
->text_update
) {
341 con
->hw_ops
->text_update(con
->hw
, chardata
);
345 static void vga_fill_rect(QemuConsole
*con
,
346 int posx
, int posy
, int width
, int height
,
347 pixman_color_t color
)
349 DisplaySurface
*surface
= qemu_console_surface(con
);
350 pixman_rectangle16_t rect
= {
351 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
354 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
358 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
359 static void vga_bitblt(QemuConsole
*con
,
360 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
362 DisplaySurface
*surface
= qemu_console_surface(con
);
364 pixman_image_composite(PIXMAN_OP_SRC
,
365 surface
->image
, NULL
, surface
->image
,
366 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
369 /***********************************************************/
370 /* basic char display */
372 #define FONT_HEIGHT 16
377 #ifndef CONFIG_CURSES
390 #define QEMU_RGB(r, g, b) \
391 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
393 static const pixman_color_t color_table_rgb
[2][8] = {
395 QEMU_RGB(0x00, 0x00, 0x00), /* black */
396 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
397 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
398 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
399 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
400 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
401 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
402 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
405 QEMU_RGB(0x00, 0x00, 0x00), /* black */
406 QEMU_RGB(0xff, 0x00, 0x00), /* red */
407 QEMU_RGB(0x00, 0xff, 0x00), /* green */
408 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
409 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
410 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
411 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
412 QEMU_RGB(0xff, 0xff, 0xff), /* white */
416 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
417 TextAttributes
*t_attrib
)
419 static pixman_image_t
*glyphs
[256];
420 DisplaySurface
*surface
= qemu_console_surface(s
);
421 pixman_color_t fgcol
, bgcol
;
423 if (t_attrib
->invers
) {
424 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
425 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
427 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
428 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
432 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
434 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
435 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
438 static void text_console_resize(QemuConsole
*s
)
440 TextCell
*cells
, *c
, *c1
;
441 int w1
, x
, y
, last_width
;
443 last_width
= s
->width
;
444 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
445 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
451 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
452 for(y
= 0; y
< s
->total_height
; y
++) {
453 c
= &cells
[y
* s
->width
];
455 c1
= &s
->cells
[y
* last_width
];
456 for(x
= 0; x
< w1
; x
++) {
460 for(x
= w1
; x
< s
->width
; x
++) {
462 c
->t_attrib
= s
->t_attrib_default
;
470 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
472 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
473 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
474 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
475 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
478 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
480 if (!qemu_console_is_visible(s
)) {
483 if (s
->update_x0
> x
* FONT_WIDTH
)
484 s
->update_x0
= x
* FONT_WIDTH
;
485 if (s
->update_y0
> y
* FONT_HEIGHT
)
486 s
->update_y0
= y
* FONT_HEIGHT
;
487 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
488 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
489 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
490 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
493 static void update_xy(QemuConsole
*s
, int x
, int y
)
498 if (s
->ds
->have_text
) {
499 text_update_xy(s
, x
, y
);
502 y1
= (s
->y_base
+ y
) % s
->total_height
;
503 y2
= y1
- s
->y_displayed
;
505 y2
+= s
->total_height
;
507 if (y2
< s
->height
) {
508 c
= &s
->cells
[y1
* s
->width
+ x
];
509 vga_putcharxy(s
, x
, y2
, c
->ch
,
511 invalidate_xy(s
, x
, y2
);
515 static void console_show_cursor(QemuConsole
*s
, int show
)
521 if (s
->ds
->have_text
) {
522 s
->cursor_invalidate
= 1;
528 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
529 y
= y1
- s
->y_displayed
;
531 y
+= s
->total_height
;
534 c
= &s
->cells
[y1
* s
->width
+ x
];
535 if (show
&& cursor_visible_phase
) {
536 TextAttributes t_attrib
= s
->t_attrib_default
;
537 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
538 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
540 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
542 invalidate_xy(s
, x
, y
);
546 static void console_refresh(QemuConsole
*s
)
548 DisplaySurface
*surface
= qemu_console_surface(s
);
552 if (s
->ds
->have_text
) {
555 s
->text_x
[1] = s
->width
- 1;
556 s
->text_y
[1] = s
->height
- 1;
557 s
->cursor_invalidate
= 1;
560 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
561 color_table_rgb
[0][COLOR_BLACK
]);
563 for (y
= 0; y
< s
->height
; y
++) {
564 c
= s
->cells
+ y1
* s
->width
;
565 for (x
= 0; x
< s
->width
; x
++) {
566 vga_putcharxy(s
, x
, y
, c
->ch
,
570 if (++y1
== s
->total_height
) {
574 console_show_cursor(s
, 1);
575 dpy_gfx_update(s
, 0, 0,
576 surface_width(surface
), surface_height(surface
));
579 static void console_scroll(QemuConsole
*s
, int ydelta
)
584 for(i
= 0; i
< ydelta
; i
++) {
585 if (s
->y_displayed
== s
->y_base
)
587 if (++s
->y_displayed
== s
->total_height
)
592 i
= s
->backscroll_height
;
593 if (i
> s
->total_height
- s
->height
)
594 i
= s
->total_height
- s
->height
;
597 y1
+= s
->total_height
;
598 for(i
= 0; i
< ydelta
; i
++) {
599 if (s
->y_displayed
== y1
)
601 if (--s
->y_displayed
< 0)
602 s
->y_displayed
= s
->total_height
- 1;
608 static void console_put_lf(QemuConsole
*s
)
614 if (s
->y
>= s
->height
) {
615 s
->y
= s
->height
- 1;
617 if (s
->y_displayed
== s
->y_base
) {
618 if (++s
->y_displayed
== s
->total_height
)
621 if (++s
->y_base
== s
->total_height
)
623 if (s
->backscroll_height
< s
->total_height
)
624 s
->backscroll_height
++;
625 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
626 c
= &s
->cells
[y1
* s
->width
];
627 for(x
= 0; x
< s
->width
; x
++) {
629 c
->t_attrib
= s
->t_attrib_default
;
632 if (s
->y_displayed
== s
->y_base
) {
633 if (s
->ds
->have_text
) {
636 s
->text_x
[1] = s
->width
- 1;
637 s
->text_y
[1] = s
->height
- 1;
640 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
641 s
->width
* FONT_WIDTH
,
642 (s
->height
- 1) * FONT_HEIGHT
);
643 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
644 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
645 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
648 s
->update_x1
= s
->width
* FONT_WIDTH
;
649 s
->update_y1
= s
->height
* FONT_HEIGHT
;
654 /* Set console attributes depending on the current escape codes.
655 * NOTE: I know this code is not very efficient (checking every color for it
656 * self) but it is more readable and better maintainable.
658 static void console_handle_escape(QemuConsole
*s
)
662 for (i
=0; i
<s
->nb_esc_params
; i
++) {
663 switch (s
->esc_params
[i
]) {
664 case 0: /* reset all console attributes to default */
665 s
->t_attrib
= s
->t_attrib_default
;
668 s
->t_attrib
.bold
= 1;
671 s
->t_attrib
.uline
= 1;
674 s
->t_attrib
.blink
= 1;
677 s
->t_attrib
.invers
= 1;
680 s
->t_attrib
.unvisible
= 1;
683 s
->t_attrib
.bold
= 0;
686 s
->t_attrib
.uline
= 0;
689 s
->t_attrib
.blink
= 0;
692 s
->t_attrib
.invers
= 0;
695 s
->t_attrib
.unvisible
= 0;
697 /* set foreground color */
699 s
->t_attrib
.fgcol
=COLOR_BLACK
;
702 s
->t_attrib
.fgcol
=COLOR_RED
;
705 s
->t_attrib
.fgcol
=COLOR_GREEN
;
708 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
711 s
->t_attrib
.fgcol
=COLOR_BLUE
;
714 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
717 s
->t_attrib
.fgcol
=COLOR_CYAN
;
720 s
->t_attrib
.fgcol
=COLOR_WHITE
;
722 /* set background color */
724 s
->t_attrib
.bgcol
=COLOR_BLACK
;
727 s
->t_attrib
.bgcol
=COLOR_RED
;
730 s
->t_attrib
.bgcol
=COLOR_GREEN
;
733 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
736 s
->t_attrib
.bgcol
=COLOR_BLUE
;
739 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
742 s
->t_attrib
.bgcol
=COLOR_CYAN
;
745 s
->t_attrib
.bgcol
=COLOR_WHITE
;
751 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
753 int y1
= (s
->y_base
+ y
) % s
->total_height
;
754 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
756 c
->t_attrib
= s
->t_attrib_default
;
760 /* set cursor, checking bounds */
761 static void set_cursor(QemuConsole
*s
, int x
, int y
)
769 if (y
>= s
->height
) {
780 static void console_putchar(QemuConsole
*s
, int ch
)
789 case '\r': /* carriage return */
792 case '\n': /* newline */
795 case '\b': /* backspace */
799 case '\t': /* tabspace */
800 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
804 s
->x
= s
->x
+ (8 - (s
->x
% 8));
807 case '\a': /* alert aka. bell */
808 /* TODO: has to be implemented */
811 /* SI (shift in), character set 0 (ignored) */
814 /* SO (shift out), character set 1 (ignored) */
816 case 27: /* esc (introducing an escape sequence) */
817 s
->state
= TTY_STATE_ESC
;
820 if (s
->x
>= s
->width
) {
825 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
826 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
828 c
->t_attrib
= s
->t_attrib
;
829 update_xy(s
, s
->x
, s
->y
);
834 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
836 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
837 s
->esc_params
[i
] = 0;
838 s
->nb_esc_params
= 0;
839 s
->state
= TTY_STATE_CSI
;
841 s
->state
= TTY_STATE_NORM
;
844 case TTY_STATE_CSI
: /* handle escape sequence parameters */
845 if (ch
>= '0' && ch
<= '9') {
846 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
847 int *param
= &s
->esc_params
[s
->nb_esc_params
];
848 int digit
= (ch
- '0');
850 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
851 *param
* 10 + digit
: INT_MAX
;
854 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
858 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
859 ch
, s
->nb_esc_params
);
860 s
->state
= TTY_STATE_NORM
;
864 if (s
->esc_params
[0] == 0) {
865 s
->esc_params
[0] = 1;
867 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
870 /* move cursor down */
871 if (s
->esc_params
[0] == 0) {
872 s
->esc_params
[0] = 1;
874 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
877 /* move cursor right */
878 if (s
->esc_params
[0] == 0) {
879 s
->esc_params
[0] = 1;
881 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
884 /* move cursor left */
885 if (s
->esc_params
[0] == 0) {
886 s
->esc_params
[0] = 1;
888 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
891 /* move cursor to column */
892 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
896 /* move cursor to row, column */
897 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
900 switch (s
->esc_params
[0]) {
902 /* clear to end of screen */
903 for (y
= s
->y
; y
< s
->height
; y
++) {
904 for (x
= 0; x
< s
->width
; x
++) {
905 if (y
== s
->y
&& x
< s
->x
) {
908 console_clear_xy(s
, x
, y
);
913 /* clear from beginning of screen */
914 for (y
= 0; y
<= s
->y
; y
++) {
915 for (x
= 0; x
< s
->width
; x
++) {
916 if (y
== s
->y
&& x
> s
->x
) {
919 console_clear_xy(s
, x
, y
);
924 /* clear entire screen */
925 for (y
= 0; y
<= s
->height
; y
++) {
926 for (x
= 0; x
< s
->width
; x
++) {
927 console_clear_xy(s
, x
, y
);
934 switch (s
->esc_params
[0]) {
937 for(x
= s
->x
; x
< s
->width
; x
++) {
938 console_clear_xy(s
, x
, s
->y
);
942 /* clear from beginning of line */
943 for (x
= 0; x
<= s
->x
; x
++) {
944 console_clear_xy(s
, x
, s
->y
);
948 /* clear entire line */
949 for(x
= 0; x
< s
->width
; x
++) {
950 console_clear_xy(s
, x
, s
->y
);
956 console_handle_escape(s
);
959 /* report cursor position */
960 /* TODO: send ESC[row;colR */
963 /* save cursor position */
968 /* restore cursor position */
973 trace_console_putchar_unhandled(ch
);
981 void console_select(unsigned int index
)
983 DisplayChangeListener
*dcl
;
986 trace_console_select(index
);
987 s
= qemu_console_lookup_by_index(index
);
989 DisplayState
*ds
= s
->ds
;
993 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
994 if (dcl
->con
!= NULL
) {
997 if (dcl
->ops
->dpy_gfx_switch
) {
998 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1001 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1002 surface_height(s
->surface
));
1004 if (ds
->have_text
) {
1005 dpy_text_resize(s
, s
->width
, s
->height
);
1007 text_console_update_cursor(NULL
);
1011 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1013 QemuConsole
*s
= chr
->opaque
;
1016 s
->update_x0
= s
->width
* FONT_WIDTH
;
1017 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1020 console_show_cursor(s
, 0);
1021 for(i
= 0; i
< len
; i
++) {
1022 console_putchar(s
, buf
[i
]);
1024 console_show_cursor(s
, 1);
1025 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1026 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1027 s
->update_x1
- s
->update_x0
,
1028 s
->update_y1
- s
->update_y0
);
1033 static void kbd_send_chars(void *opaque
)
1035 QemuConsole
*s
= opaque
;
1039 len
= qemu_chr_be_can_write(s
->chr
);
1040 if (len
> s
->out_fifo
.count
)
1041 len
= s
->out_fifo
.count
;
1043 if (len
> sizeof(buf
))
1045 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1046 qemu_chr_be_write(s
->chr
, buf
, len
);
1048 /* characters are pending: we send them a bit later (XXX:
1049 horrible, should change char device API) */
1050 if (s
->out_fifo
.count
> 0) {
1051 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1055 /* called when an ascii key is pressed */
1056 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1058 uint8_t buf
[16], *q
;
1061 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1065 case QEMU_KEY_CTRL_UP
:
1066 console_scroll(s
, -1);
1068 case QEMU_KEY_CTRL_DOWN
:
1069 console_scroll(s
, 1);
1071 case QEMU_KEY_CTRL_PAGEUP
:
1072 console_scroll(s
, -10);
1074 case QEMU_KEY_CTRL_PAGEDOWN
:
1075 console_scroll(s
, 10);
1078 /* convert the QEMU keysym to VT100 key string */
1080 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1083 c
= keysym
- 0xe100;
1085 *q
++ = '0' + (c
/ 10);
1086 *q
++ = '0' + (c
% 10);
1088 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1091 *q
++ = keysym
& 0xff;
1092 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1093 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1099 console_puts(s
->chr
, buf
, q
- buf
);
1101 if (s
->chr
->chr_read
) {
1102 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1109 static const int qcode_to_keysym
[Q_KEY_CODE_MAX
] = {
1110 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1111 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1112 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1113 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1114 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1115 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1116 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1117 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1118 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1121 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1125 keysym
= qcode_to_keysym
[qcode
];
1129 kbd_put_keysym_console(s
, keysym
);
1133 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1137 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1138 kbd_put_keysym_console(s
, str
[i
]);
1142 void kbd_put_keysym(int keysym
)
1144 kbd_put_keysym_console(active_console
, keysym
);
1147 static void text_console_invalidate(void *opaque
)
1149 QemuConsole
*s
= (QemuConsole
*) opaque
;
1151 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1152 text_console_resize(s
);
1157 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1159 QemuConsole
*s
= (QemuConsole
*) opaque
;
1162 if (s
->text_x
[0] <= s
->text_x
[1]) {
1163 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1164 chardata
+= s
->text_y
[0] * s
->width
;
1165 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1166 for (j
= 0; j
< s
->width
; j
++, src
++)
1167 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1168 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1169 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1170 (s
->cells
[src
].t_attrib
.bold
<< 21));
1171 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1172 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1173 s
->text_x
[0] = s
->width
;
1174 s
->text_y
[0] = s
->height
;
1178 if (s
->cursor_invalidate
) {
1179 dpy_text_cursor(s
, s
->x
, s
->y
);
1180 s
->cursor_invalidate
= 0;
1184 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1191 obj
= object_new(TYPE_QEMU_CONSOLE
);
1192 s
= QEMU_CONSOLE(obj
);
1194 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1195 (Object
**)&s
->device
,
1196 object_property_allow_set_link
,
1197 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1199 object_property_add_uint32_ptr(obj
, "head",
1200 &s
->head
, &error_abort
);
1202 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1203 (console_type
== GRAPHIC_CONSOLE
))) {
1207 s
->console_type
= console_type
;
1209 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1210 if (console_type
!= GRAPHIC_CONSOLE
) {
1211 s
->index
= nb_consoles
;
1212 consoles
[nb_consoles
++] = s
;
1214 /* HACK: Put graphical consoles before text consoles. */
1215 for (i
= nb_consoles
; i
> 0; i
--) {
1216 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1218 consoles
[i
] = consoles
[i
- 1];
1219 consoles
[i
]->index
= i
;
1228 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1230 qemu_pixman_image_unref(surface
->image
);
1231 surface
->image
= NULL
;
1233 surface
->format
= PIXMAN_x8r8g8b8
;
1234 surface
->image
= pixman_image_create_bits(surface
->format
,
1237 assert(surface
->image
!= NULL
);
1239 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1242 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1244 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1246 trace_displaysurface_create(surface
, width
, height
);
1247 qemu_alloc_display(surface
, width
, height
);
1251 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1252 pixman_format_code_t format
,
1253 int linesize
, uint8_t *data
)
1255 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1257 trace_displaysurface_create_from(surface
, width
, height
, format
);
1258 surface
->format
= format
;
1259 surface
->image
= pixman_image_create_bits(surface
->format
,
1261 (void *)data
, linesize
);
1262 assert(surface
->image
!= NULL
);
1267 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1270 void *data
= pixman_image_get_data(image
);
1271 uint32_t size
= pixman_image_get_stride(image
) *
1272 pixman_image_get_height(image
);
1273 cpu_physical_memory_unmap(data
, size
, 0, 0);
1276 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1277 pixman_format_code_t format
,
1278 int linesize
, uint64_t addr
)
1280 DisplaySurface
*surface
;
1284 if (linesize
== 0) {
1285 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1288 size
= linesize
* height
;
1289 data
= cpu_physical_memory_map(addr
, &size
, 0);
1290 if (size
!= linesize
* height
) {
1291 cpu_physical_memory_unmap(data
, size
, 0, 0);
1295 surface
= qemu_create_displaysurface_from
1296 (width
, height
, format
, linesize
, data
);
1297 pixman_image_set_destroy_function
1298 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1303 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1306 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1307 pixman_color_t bg
= color_table_rgb
[0][COLOR_BLACK
];
1308 pixman_color_t fg
= color_table_rgb
[0][COLOR_WHITE
];
1309 pixman_image_t
*glyph
;
1313 x
= (w
/ FONT_WIDTH
- len
) / 2;
1314 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1315 for (i
= 0; i
< len
; i
++) {
1316 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1317 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1318 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1319 qemu_pixman_image_unref(glyph
);
1324 void qemu_free_displaysurface(DisplaySurface
*surface
)
1326 if (surface
== NULL
) {
1329 trace_displaysurface_free(surface
);
1330 qemu_pixman_image_unref(surface
->image
);
1334 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1336 static const char nodev
[] =
1337 "This VM has no graphic display device.";
1338 static DisplaySurface
*dummy
;
1341 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1342 dcl
->ds
= get_alloc_displaystate();
1343 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1344 gui_setup_refresh(dcl
->ds
);
1349 con
= active_console
;
1351 if (dcl
->ops
->dpy_gfx_switch
) {
1353 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1356 dummy
= qemu_create_message_surface(640, 480, nodev
);
1358 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1361 text_console_update_cursor(NULL
);
1364 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1367 DisplayState
*ds
= dcl
->ds
;
1369 dcl
->update_interval
= interval
;
1370 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1371 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1375 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1377 DisplayState
*ds
= dcl
->ds
;
1378 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1382 QLIST_REMOVE(dcl
, next
);
1383 gui_setup_refresh(ds
);
1386 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1388 assert(con
!= NULL
);
1389 con
->ui_info
= *info
;
1390 if (con
->hw_ops
->ui_info
) {
1391 return con
->hw_ops
->ui_info(con
->hw
, con
->head
, info
);
1396 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1398 DisplayState
*s
= con
->ds
;
1399 DisplayChangeListener
*dcl
;
1400 int width
= surface_width(con
->surface
);
1401 int height
= surface_height(con
->surface
);
1407 w
= MIN(w
, width
- x
);
1408 h
= MIN(h
, height
- y
);
1410 if (!qemu_console_is_visible(con
)) {
1413 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1414 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1417 if (dcl
->ops
->dpy_gfx_update
) {
1418 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1423 void dpy_gfx_replace_surface(QemuConsole
*con
,
1424 DisplaySurface
*surface
)
1426 DisplayState
*s
= con
->ds
;
1427 DisplaySurface
*old_surface
= con
->surface
;
1428 DisplayChangeListener
*dcl
;
1430 con
->surface
= surface
;
1431 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1432 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1435 if (dcl
->ops
->dpy_gfx_switch
) {
1436 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1439 qemu_free_displaysurface(old_surface
);
1442 static void dpy_refresh(DisplayState
*s
)
1444 DisplayChangeListener
*dcl
;
1446 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1447 if (dcl
->ops
->dpy_refresh
) {
1448 dcl
->ops
->dpy_refresh(dcl
);
1453 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1454 int dst_x
, int dst_y
, int w
, int h
)
1456 DisplayState
*s
= con
->ds
;
1457 DisplayChangeListener
*dcl
;
1459 if (!qemu_console_is_visible(con
)) {
1462 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1463 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1466 if (dcl
->ops
->dpy_gfx_copy
) {
1467 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1469 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1474 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1476 DisplayState
*s
= con
->ds
;
1477 DisplayChangeListener
*dcl
;
1479 if (!qemu_console_is_visible(con
)) {
1482 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1483 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1486 if (dcl
->ops
->dpy_text_cursor
) {
1487 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1492 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1494 DisplayState
*s
= con
->ds
;
1495 DisplayChangeListener
*dcl
;
1497 if (!qemu_console_is_visible(con
)) {
1500 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1501 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1504 if (dcl
->ops
->dpy_text_update
) {
1505 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1510 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1512 DisplayState
*s
= con
->ds
;
1513 struct DisplayChangeListener
*dcl
;
1515 if (!qemu_console_is_visible(con
)) {
1518 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1519 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1522 if (dcl
->ops
->dpy_text_resize
) {
1523 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1528 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1530 DisplayState
*s
= con
->ds
;
1531 DisplayChangeListener
*dcl
;
1533 if (!qemu_console_is_visible(con
)) {
1536 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1537 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1540 if (dcl
->ops
->dpy_mouse_set
) {
1541 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1546 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1548 DisplayState
*s
= con
->ds
;
1549 DisplayChangeListener
*dcl
;
1551 if (!qemu_console_is_visible(con
)) {
1554 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1555 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1558 if (dcl
->ops
->dpy_cursor_define
) {
1559 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1564 bool dpy_cursor_define_supported(QemuConsole
*con
)
1566 DisplayState
*s
= con
->ds
;
1567 DisplayChangeListener
*dcl
;
1569 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1570 if (dcl
->ops
->dpy_cursor_define
) {
1578 * Call dpy_gfx_update for all dirity scanlines. Works for
1579 * DisplaySurfaces backed by guest memory (i.e. the ones created
1580 * using qemu_create_displaysurface_guestmem).
1582 void dpy_gfx_update_dirty(QemuConsole
*con
,
1583 MemoryRegion
*address_space
,
1587 DisplaySurface
*ds
= qemu_console_surface(con
);
1588 int width
= surface_stride(ds
);
1589 int height
= surface_height(ds
);
1590 hwaddr size
= width
* height
;
1591 MemoryRegionSection mem_section
;
1597 mem_section
= memory_region_find(address_space
, base
, size
);
1598 mem
= mem_section
.mr
;
1599 if (int128_get64(mem_section
.size
) != size
||
1600 !memory_region_is_ram(mem_section
.mr
)) {
1605 memory_region_sync_dirty_bitmap(mem
);
1606 addr
= mem_section
.offset_within_region
;
1610 for (i
= 0; i
< height
; i
++, addr
+= width
) {
1611 dirty
= invalidate
||
1612 memory_region_get_dirty(mem
, addr
, width
, DIRTY_MEMORY_VGA
);
1619 if (first
!= -1 && !dirty
) {
1620 assert(last
!= -1 && last
>= first
);
1621 dpy_gfx_update(con
, 0, first
, surface_width(ds
),
1627 assert(last
!= -1 && last
>= first
);
1628 dpy_gfx_update(con
, 0, first
, surface_width(ds
),
1632 memory_region_reset_dirty(mem
, mem_section
.offset_within_region
, size
,
1635 memory_region_unref(mem
);
1638 /***********************************************************/
1639 /* register display */
1641 /* console.c internal use only */
1642 static DisplayState
*get_alloc_displaystate(void)
1644 if (!display_state
) {
1645 display_state
= g_new0(DisplayState
, 1);
1646 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1647 text_console_update_cursor
, NULL
);
1649 return display_state
;
1653 * Called by main(), after creating QemuConsoles
1654 * and before initializing ui (sdl/vnc/...).
1656 DisplayState
*init_displaystate(void)
1661 get_alloc_displaystate();
1662 for (i
= 0; i
< nb_consoles
; i
++) {
1663 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1664 consoles
[i
]->ds
== NULL
) {
1665 text_console_do_init(consoles
[i
]->chr
, display_state
);
1668 /* Hook up into the qom tree here (not in new_console()), once
1669 * all QemuConsoles are created and the order / numbering
1670 * doesn't change any more */
1671 name
= g_strdup_printf("console[%d]", i
);
1672 object_property_add_child(container_get(object_get_root(), "/backend"),
1673 name
, OBJECT(consoles
[i
]), &error_abort
);
1677 return display_state
;
1680 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1681 const GraphicHwOps
*hw_ops
,
1684 static const char noinit
[] =
1685 "Guest has not initialized the display (yet).";
1691 ds
= get_alloc_displaystate();
1692 trace_console_gfx_new();
1693 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1697 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1701 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1705 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1707 if (index
>= nb_consoles
) {
1710 return consoles
[index
];
1713 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1719 for (i
= 0; i
< nb_consoles
; i
++) {
1723 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1724 "device", &error_abort
);
1725 if (DEVICE(obj
) != dev
) {
1728 h
= object_property_get_int(OBJECT(consoles
[i
]),
1729 "head", &error_abort
);
1738 bool qemu_console_is_visible(QemuConsole
*con
)
1740 return (con
== active_console
) || (con
->dcls
> 0);
1743 bool qemu_console_is_graphic(QemuConsole
*con
)
1746 con
= active_console
;
1748 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1751 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1754 con
= active_console
;
1756 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1759 int qemu_console_get_index(QemuConsole
*con
)
1762 con
= active_console
;
1764 return con
? con
->index
: -1;
1767 uint32_t qemu_console_get_head(QemuConsole
*con
)
1770 con
= active_console
;
1772 return con
? con
->head
: -1;
1775 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1777 assert(con
!= NULL
);
1778 return &con
->ui_info
;
1781 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1784 con
= active_console
;
1786 return con
? surface_width(con
->surface
) : fallback
;
1789 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1792 con
= active_console
;
1794 return con
? surface_height(con
->surface
) : fallback
;
1797 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1799 QemuConsole
*s
= chr
->opaque
;
1804 static void text_console_update_cursor_timer(void)
1806 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1807 + CONSOLE_CURSOR_PERIOD
/ 2);
1810 static void text_console_update_cursor(void *opaque
)
1815 cursor_visible_phase
= !cursor_visible_phase
;
1817 for (i
= 0; i
< nb_consoles
; i
++) {
1819 if (qemu_console_is_graphic(s
) ||
1820 !qemu_console_is_visible(s
)) {
1824 graphic_hw_invalidate(s
);
1828 text_console_update_cursor_timer();
1832 static const GraphicHwOps text_console_ops
= {
1833 .invalidate
= text_console_invalidate
,
1834 .text_update
= text_console_update
,
1837 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1840 int g_width
= 80 * FONT_WIDTH
;
1841 int g_height
= 24 * FONT_HEIGHT
;
1845 chr
->chr_write
= console_puts
;
1847 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1848 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1849 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
1854 s
->total_height
= DEFAULT_BACKSCROLL
;
1858 if (active_console
&& active_console
->surface
) {
1859 g_width
= surface_width(active_console
->surface
);
1860 g_height
= surface_height(active_console
->surface
);
1862 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
1865 s
->hw_ops
= &text_console_ops
;
1868 /* Set text attribute defaults */
1869 s
->t_attrib_default
.bold
= 0;
1870 s
->t_attrib_default
.uline
= 0;
1871 s
->t_attrib_default
.blink
= 0;
1872 s
->t_attrib_default
.invers
= 0;
1873 s
->t_attrib_default
.unvisible
= 0;
1874 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1875 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1876 /* set current text attributes to default */
1877 s
->t_attrib
= s
->t_attrib_default
;
1878 text_console_resize(s
);
1884 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1885 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1886 console_puts(chr
, (uint8_t*)msg
, len
);
1887 s
->t_attrib
= s
->t_attrib_default
;
1890 qemu_chr_be_generic_open(chr
);
1895 static CharDriverState
*text_console_init(ChardevVC
*vc
)
1897 CharDriverState
*chr
;
1900 unsigned height
= 0;
1902 chr
= qemu_chr_alloc();
1904 if (vc
->has_width
) {
1906 } else if (vc
->has_cols
) {
1907 width
= vc
->cols
* FONT_WIDTH
;
1910 if (vc
->has_height
) {
1911 height
= vc
->height
;
1912 } else if (vc
->has_rows
) {
1913 height
= vc
->rows
* FONT_HEIGHT
;
1916 trace_console_txt_new(width
, height
);
1917 if (width
== 0 || height
== 0) {
1918 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
1920 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
1921 s
->surface
= qemu_create_displaysurface(width
, height
);
1931 chr
->chr_set_echo
= text_console_set_echo
;
1932 /* console/chardev init sometimes completes elsewhere in a 2nd
1933 * stage, so defer OPENED events until they are fully initialized
1935 chr
->explicit_be_open
= true;
1937 if (display_state
) {
1938 text_console_do_init(chr
, display_state
);
1943 static VcHandler
*vc_handler
= text_console_init
;
1945 CharDriverState
*vc_init(ChardevVC
*vc
)
1947 return vc_handler(vc
);
1950 void register_vc_handler(VcHandler
*handler
)
1952 vc_handler
= handler
;
1955 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
1957 DisplaySurface
*surface
;
1959 assert(s
->console_type
== GRAPHIC_CONSOLE
);
1960 surface
= qemu_create_displaysurface(width
, height
);
1961 dpy_gfx_replace_surface(s
, surface
);
1964 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
1965 int dst_x
, int dst_y
, int w
, int h
)
1967 assert(con
->console_type
== GRAPHIC_CONSOLE
);
1968 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1971 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
1973 return console
->surface
;
1976 DisplayState
*qemu_console_displaystate(QemuConsole
*console
)
1981 PixelFormat
qemu_different_endianness_pixelformat(int bpp
)
1983 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, false);
1984 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
1988 PixelFormat
qemu_default_pixelformat(int bpp
)
1990 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
1991 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
1995 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
2000 backend
->vc
= g_new0(ChardevVC
, 1);
2002 val
= qemu_opt_get_number(opts
, "width", 0);
2004 backend
->vc
->has_width
= true;
2005 backend
->vc
->width
= val
;
2008 val
= qemu_opt_get_number(opts
, "height", 0);
2010 backend
->vc
->has_height
= true;
2011 backend
->vc
->height
= val
;
2014 val
= qemu_opt_get_number(opts
, "cols", 0);
2016 backend
->vc
->has_cols
= true;
2017 backend
->vc
->cols
= val
;
2020 val
= qemu_opt_get_number(opts
, "rows", 0);
2022 backend
->vc
->has_rows
= true;
2023 backend
->vc
->rows
= val
;
2027 static const TypeInfo qemu_console_info
= {
2028 .name
= TYPE_QEMU_CONSOLE
,
2029 .parent
= TYPE_OBJECT
,
2030 .instance_size
= sizeof(QemuConsole
),
2031 .class_size
= sizeof(QemuConsoleClass
),
2035 static void register_types(void)
2037 type_register_static(&qemu_console_info
);
2038 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC
, qemu_chr_parse_vc
);
2041 type_init(register_types
);