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. */
130 const GraphicHwOps
*hw_ops
;
133 /* Text console state */
137 int backscroll_height
;
139 int x_saved
, y_saved
;
142 TextAttributes t_attrib_default
; /* default text attributes */
143 TextAttributes t_attrib
; /* currently active text attributes */
145 int text_x
[2], text_y
[2], cursor_invalidate
;
154 int esc_params
[MAX_ESC_PARAMS
];
157 CharDriverState
*chr
;
158 /* fifo for key pressed */
160 uint8_t out_fifo_buf
[16];
161 QEMUTimer
*kbd_timer
;
164 struct DisplayState
{
165 QEMUTimer
*gui_timer
;
166 uint64_t last_update
;
167 uint64_t update_interval
;
172 QLIST_HEAD(, DisplayChangeListener
) listeners
;
175 static DisplayState
*display_state
;
176 static QemuConsole
*active_console
;
177 static QemuConsole
**consoles
;
178 static int nb_consoles
= 0;
179 static bool cursor_visible_phase
;
180 static QEMUTimer
*cursor_timer
;
182 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
183 static void dpy_refresh(DisplayState
*s
);
184 static DisplayState
*get_alloc_displaystate(void);
185 static void text_console_update_cursor_timer(void);
186 static void text_console_update_cursor(void *opaque
);
188 static void gui_update(void *opaque
)
190 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
191 uint64_t dcl_interval
;
192 DisplayState
*ds
= opaque
;
193 DisplayChangeListener
*dcl
;
196 ds
->refreshing
= true;
198 ds
->refreshing
= false;
200 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
201 dcl_interval
= dcl
->update_interval
?
202 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
203 if (interval
> dcl_interval
) {
204 interval
= dcl_interval
;
207 if (ds
->update_interval
!= interval
) {
208 ds
->update_interval
= interval
;
209 for (i
= 0; i
< nb_consoles
; i
++) {
210 if (consoles
[i
]->hw_ops
->update_interval
) {
211 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
214 trace_console_refresh(interval
);
216 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
217 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
220 static void gui_setup_refresh(DisplayState
*ds
)
222 DisplayChangeListener
*dcl
;
223 bool need_timer
= false;
224 bool have_gfx
= false;
225 bool have_text
= false;
227 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
228 if (dcl
->ops
->dpy_refresh
!= NULL
) {
231 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
234 if (dcl
->ops
->dpy_text_update
!= NULL
) {
239 if (need_timer
&& ds
->gui_timer
== NULL
) {
240 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
241 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
243 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
244 timer_del(ds
->gui_timer
);
245 timer_free(ds
->gui_timer
);
246 ds
->gui_timer
= NULL
;
249 ds
->have_gfx
= have_gfx
;
250 ds
->have_text
= have_text
;
253 void graphic_hw_update(QemuConsole
*con
)
256 con
= active_console
;
258 if (con
&& con
->hw_ops
->gfx_update
) {
259 con
->hw_ops
->gfx_update(con
->hw
);
263 void graphic_hw_invalidate(QemuConsole
*con
)
266 con
= active_console
;
268 if (con
&& con
->hw_ops
->invalidate
) {
269 con
->hw_ops
->invalidate(con
->hw
);
273 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
276 int width
= pixman_image_get_width(ds
->image
);
277 int height
= pixman_image_get_height(ds
->image
);
282 pixman_image_t
*linebuf
;
284 trace_ppm_save(filename
, ds
);
285 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
287 error_setg(errp
, "failed to open file '%s': %s", filename
,
291 f
= fdopen(fd
, "wb");
292 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
297 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
298 for (y
= 0; y
< height
; y
++) {
299 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
301 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
302 pixman_image_get_stride(linebuf
), f
);
310 qemu_pixman_image_unref(linebuf
);
315 error_setg(errp
, "failed to write to file '%s': %s", filename
,
321 void qmp_screendump(const char *filename
, Error
**errp
)
323 QemuConsole
*con
= qemu_console_lookup_by_index(0);
324 DisplaySurface
*surface
;
327 error_setg(errp
, "There is no QemuConsole I can screendump from.");
331 graphic_hw_update(con
);
332 surface
= qemu_console_surface(con
);
333 ppm_save(filename
, surface
, errp
);
336 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
339 con
= active_console
;
341 if (con
&& con
->hw_ops
->text_update
) {
342 con
->hw_ops
->text_update(con
->hw
, chardata
);
346 static void vga_fill_rect(QemuConsole
*con
,
347 int posx
, int posy
, int width
, int height
,
348 pixman_color_t color
)
350 DisplaySurface
*surface
= qemu_console_surface(con
);
351 pixman_rectangle16_t rect
= {
352 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
355 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
359 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
360 static void vga_bitblt(QemuConsole
*con
,
361 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
363 DisplaySurface
*surface
= qemu_console_surface(con
);
365 pixman_image_composite(PIXMAN_OP_SRC
,
366 surface
->image
, NULL
, surface
->image
,
367 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
370 /***********************************************************/
371 /* basic char display */
373 #define FONT_HEIGHT 16
378 #ifndef CONFIG_CURSES
391 #define QEMU_RGB(r, g, b) \
392 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
394 static const pixman_color_t color_table_rgb
[2][8] = {
396 QEMU_RGB(0x00, 0x00, 0x00), /* black */
397 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
398 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
399 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
400 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
401 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
402 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
403 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
406 QEMU_RGB(0x00, 0x00, 0x00), /* black */
407 QEMU_RGB(0xff, 0x00, 0x00), /* red */
408 QEMU_RGB(0x00, 0xff, 0x00), /* green */
409 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
410 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
411 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
412 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
413 QEMU_RGB(0xff, 0xff, 0xff), /* white */
417 static void vga_putcharxy(QemuConsole
*s
, int x
, int y
, int ch
,
418 TextAttributes
*t_attrib
)
420 static pixman_image_t
*glyphs
[256];
421 DisplaySurface
*surface
= qemu_console_surface(s
);
422 pixman_color_t fgcol
, bgcol
;
424 if (t_attrib
->invers
) {
425 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
426 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
428 fgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->fgcol
];
429 bgcol
= color_table_rgb
[t_attrib
->bold
][t_attrib
->bgcol
];
433 glyphs
[ch
] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, ch
);
435 qemu_pixman_glyph_render(glyphs
[ch
], surface
->image
,
436 &fgcol
, &bgcol
, x
, y
, FONT_WIDTH
, FONT_HEIGHT
);
439 static void text_console_resize(QemuConsole
*s
)
441 TextCell
*cells
, *c
, *c1
;
442 int w1
, x
, y
, last_width
;
444 last_width
= s
->width
;
445 s
->width
= surface_width(s
->surface
) / FONT_WIDTH
;
446 s
->height
= surface_height(s
->surface
) / FONT_HEIGHT
;
452 cells
= g_malloc(s
->width
* s
->total_height
* sizeof(TextCell
));
453 for(y
= 0; y
< s
->total_height
; y
++) {
454 c
= &cells
[y
* s
->width
];
456 c1
= &s
->cells
[y
* last_width
];
457 for(x
= 0; x
< w1
; x
++) {
461 for(x
= w1
; x
< s
->width
; x
++) {
463 c
->t_attrib
= s
->t_attrib_default
;
471 static inline void text_update_xy(QemuConsole
*s
, int x
, int y
)
473 s
->text_x
[0] = MIN(s
->text_x
[0], x
);
474 s
->text_x
[1] = MAX(s
->text_x
[1], x
);
475 s
->text_y
[0] = MIN(s
->text_y
[0], y
);
476 s
->text_y
[1] = MAX(s
->text_y
[1], y
);
479 static void invalidate_xy(QemuConsole
*s
, int x
, int y
)
481 if (!qemu_console_is_visible(s
)) {
484 if (s
->update_x0
> x
* FONT_WIDTH
)
485 s
->update_x0
= x
* FONT_WIDTH
;
486 if (s
->update_y0
> y
* FONT_HEIGHT
)
487 s
->update_y0
= y
* FONT_HEIGHT
;
488 if (s
->update_x1
< (x
+ 1) * FONT_WIDTH
)
489 s
->update_x1
= (x
+ 1) * FONT_WIDTH
;
490 if (s
->update_y1
< (y
+ 1) * FONT_HEIGHT
)
491 s
->update_y1
= (y
+ 1) * FONT_HEIGHT
;
494 static void update_xy(QemuConsole
*s
, int x
, int y
)
499 if (s
->ds
->have_text
) {
500 text_update_xy(s
, x
, y
);
503 y1
= (s
->y_base
+ y
) % s
->total_height
;
504 y2
= y1
- s
->y_displayed
;
506 y2
+= s
->total_height
;
508 if (y2
< s
->height
) {
509 c
= &s
->cells
[y1
* s
->width
+ x
];
510 vga_putcharxy(s
, x
, y2
, c
->ch
,
512 invalidate_xy(s
, x
, y2
);
516 static void console_show_cursor(QemuConsole
*s
, int show
)
522 if (s
->ds
->have_text
) {
523 s
->cursor_invalidate
= 1;
529 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
530 y
= y1
- s
->y_displayed
;
532 y
+= s
->total_height
;
535 c
= &s
->cells
[y1
* s
->width
+ x
];
536 if (show
&& cursor_visible_phase
) {
537 TextAttributes t_attrib
= s
->t_attrib_default
;
538 t_attrib
.invers
= !(t_attrib
.invers
); /* invert fg and bg */
539 vga_putcharxy(s
, x
, y
, c
->ch
, &t_attrib
);
541 vga_putcharxy(s
, x
, y
, c
->ch
, &(c
->t_attrib
));
543 invalidate_xy(s
, x
, y
);
547 static void console_refresh(QemuConsole
*s
)
549 DisplaySurface
*surface
= qemu_console_surface(s
);
553 if (s
->ds
->have_text
) {
556 s
->text_x
[1] = s
->width
- 1;
557 s
->text_y
[1] = s
->height
- 1;
558 s
->cursor_invalidate
= 1;
561 vga_fill_rect(s
, 0, 0, surface_width(surface
), surface_height(surface
),
562 color_table_rgb
[0][COLOR_BLACK
]);
564 for (y
= 0; y
< s
->height
; y
++) {
565 c
= s
->cells
+ y1
* s
->width
;
566 for (x
= 0; x
< s
->width
; x
++) {
567 vga_putcharxy(s
, x
, y
, c
->ch
,
571 if (++y1
== s
->total_height
) {
575 console_show_cursor(s
, 1);
576 dpy_gfx_update(s
, 0, 0,
577 surface_width(surface
), surface_height(surface
));
580 static void console_scroll(QemuConsole
*s
, int ydelta
)
585 for(i
= 0; i
< ydelta
; i
++) {
586 if (s
->y_displayed
== s
->y_base
)
588 if (++s
->y_displayed
== s
->total_height
)
593 i
= s
->backscroll_height
;
594 if (i
> s
->total_height
- s
->height
)
595 i
= s
->total_height
- s
->height
;
598 y1
+= s
->total_height
;
599 for(i
= 0; i
< ydelta
; i
++) {
600 if (s
->y_displayed
== y1
)
602 if (--s
->y_displayed
< 0)
603 s
->y_displayed
= s
->total_height
- 1;
609 static void console_put_lf(QemuConsole
*s
)
615 if (s
->y
>= s
->height
) {
616 s
->y
= s
->height
- 1;
618 if (s
->y_displayed
== s
->y_base
) {
619 if (++s
->y_displayed
== s
->total_height
)
622 if (++s
->y_base
== s
->total_height
)
624 if (s
->backscroll_height
< s
->total_height
)
625 s
->backscroll_height
++;
626 y1
= (s
->y_base
+ s
->height
- 1) % s
->total_height
;
627 c
= &s
->cells
[y1
* s
->width
];
628 for(x
= 0; x
< s
->width
; x
++) {
630 c
->t_attrib
= s
->t_attrib_default
;
633 if (s
->y_displayed
== s
->y_base
) {
634 if (s
->ds
->have_text
) {
637 s
->text_x
[1] = s
->width
- 1;
638 s
->text_y
[1] = s
->height
- 1;
641 vga_bitblt(s
, 0, FONT_HEIGHT
, 0, 0,
642 s
->width
* FONT_WIDTH
,
643 (s
->height
- 1) * FONT_HEIGHT
);
644 vga_fill_rect(s
, 0, (s
->height
- 1) * FONT_HEIGHT
,
645 s
->width
* FONT_WIDTH
, FONT_HEIGHT
,
646 color_table_rgb
[0][s
->t_attrib_default
.bgcol
]);
649 s
->update_x1
= s
->width
* FONT_WIDTH
;
650 s
->update_y1
= s
->height
* FONT_HEIGHT
;
655 /* Set console attributes depending on the current escape codes.
656 * NOTE: I know this code is not very efficient (checking every color for it
657 * self) but it is more readable and better maintainable.
659 static void console_handle_escape(QemuConsole
*s
)
663 for (i
=0; i
<s
->nb_esc_params
; i
++) {
664 switch (s
->esc_params
[i
]) {
665 case 0: /* reset all console attributes to default */
666 s
->t_attrib
= s
->t_attrib_default
;
669 s
->t_attrib
.bold
= 1;
672 s
->t_attrib
.uline
= 1;
675 s
->t_attrib
.blink
= 1;
678 s
->t_attrib
.invers
= 1;
681 s
->t_attrib
.unvisible
= 1;
684 s
->t_attrib
.bold
= 0;
687 s
->t_attrib
.uline
= 0;
690 s
->t_attrib
.blink
= 0;
693 s
->t_attrib
.invers
= 0;
696 s
->t_attrib
.unvisible
= 0;
698 /* set foreground color */
700 s
->t_attrib
.fgcol
=COLOR_BLACK
;
703 s
->t_attrib
.fgcol
=COLOR_RED
;
706 s
->t_attrib
.fgcol
=COLOR_GREEN
;
709 s
->t_attrib
.fgcol
=COLOR_YELLOW
;
712 s
->t_attrib
.fgcol
=COLOR_BLUE
;
715 s
->t_attrib
.fgcol
=COLOR_MAGENTA
;
718 s
->t_attrib
.fgcol
=COLOR_CYAN
;
721 s
->t_attrib
.fgcol
=COLOR_WHITE
;
723 /* set background color */
725 s
->t_attrib
.bgcol
=COLOR_BLACK
;
728 s
->t_attrib
.bgcol
=COLOR_RED
;
731 s
->t_attrib
.bgcol
=COLOR_GREEN
;
734 s
->t_attrib
.bgcol
=COLOR_YELLOW
;
737 s
->t_attrib
.bgcol
=COLOR_BLUE
;
740 s
->t_attrib
.bgcol
=COLOR_MAGENTA
;
743 s
->t_attrib
.bgcol
=COLOR_CYAN
;
746 s
->t_attrib
.bgcol
=COLOR_WHITE
;
752 static void console_clear_xy(QemuConsole
*s
, int x
, int y
)
754 int y1
= (s
->y_base
+ y
) % s
->total_height
;
755 TextCell
*c
= &s
->cells
[y1
* s
->width
+ x
];
757 c
->t_attrib
= s
->t_attrib_default
;
761 /* set cursor, checking bounds */
762 static void set_cursor(QemuConsole
*s
, int x
, int y
)
770 if (y
>= s
->height
) {
781 static void console_putchar(QemuConsole
*s
, int ch
)
790 case '\r': /* carriage return */
793 case '\n': /* newline */
796 case '\b': /* backspace */
800 case '\t': /* tabspace */
801 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
805 s
->x
= s
->x
+ (8 - (s
->x
% 8));
808 case '\a': /* alert aka. bell */
809 /* TODO: has to be implemented */
812 /* SI (shift in), character set 0 (ignored) */
815 /* SO (shift out), character set 1 (ignored) */
817 case 27: /* esc (introducing an escape sequence) */
818 s
->state
= TTY_STATE_ESC
;
821 if (s
->x
>= s
->width
) {
826 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
827 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
829 c
->t_attrib
= s
->t_attrib
;
830 update_xy(s
, s
->x
, s
->y
);
835 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
837 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
838 s
->esc_params
[i
] = 0;
839 s
->nb_esc_params
= 0;
840 s
->state
= TTY_STATE_CSI
;
842 s
->state
= TTY_STATE_NORM
;
845 case TTY_STATE_CSI
: /* handle escape sequence parameters */
846 if (ch
>= '0' && ch
<= '9') {
847 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
848 int *param
= &s
->esc_params
[s
->nb_esc_params
];
849 int digit
= (ch
- '0');
851 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
852 *param
* 10 + digit
: INT_MAX
;
855 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
859 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
860 ch
, s
->nb_esc_params
);
861 s
->state
= TTY_STATE_NORM
;
865 if (s
->esc_params
[0] == 0) {
866 s
->esc_params
[0] = 1;
868 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
871 /* move cursor down */
872 if (s
->esc_params
[0] == 0) {
873 s
->esc_params
[0] = 1;
875 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
878 /* move cursor right */
879 if (s
->esc_params
[0] == 0) {
880 s
->esc_params
[0] = 1;
882 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
885 /* move cursor left */
886 if (s
->esc_params
[0] == 0) {
887 s
->esc_params
[0] = 1;
889 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
892 /* move cursor to column */
893 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
897 /* move cursor to row, column */
898 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
901 switch (s
->esc_params
[0]) {
903 /* clear to end of screen */
904 for (y
= s
->y
; y
< s
->height
; y
++) {
905 for (x
= 0; x
< s
->width
; x
++) {
906 if (y
== s
->y
&& x
< s
->x
) {
909 console_clear_xy(s
, x
, y
);
914 /* clear from beginning of screen */
915 for (y
= 0; y
<= s
->y
; y
++) {
916 for (x
= 0; x
< s
->width
; x
++) {
917 if (y
== s
->y
&& x
> s
->x
) {
920 console_clear_xy(s
, x
, y
);
925 /* clear entire screen */
926 for (y
= 0; y
<= s
->height
; y
++) {
927 for (x
= 0; x
< s
->width
; x
++) {
928 console_clear_xy(s
, x
, y
);
935 switch (s
->esc_params
[0]) {
938 for(x
= s
->x
; x
< s
->width
; x
++) {
939 console_clear_xy(s
, x
, s
->y
);
943 /* clear from beginning of line */
944 for (x
= 0; x
<= s
->x
; x
++) {
945 console_clear_xy(s
, x
, s
->y
);
949 /* clear entire line */
950 for(x
= 0; x
< s
->width
; x
++) {
951 console_clear_xy(s
, x
, s
->y
);
957 console_handle_escape(s
);
960 /* report cursor position */
961 /* TODO: send ESC[row;colR */
964 /* save cursor position */
969 /* restore cursor position */
974 trace_console_putchar_unhandled(ch
);
982 void console_select(unsigned int index
)
984 DisplayChangeListener
*dcl
;
987 trace_console_select(index
);
988 s
= qemu_console_lookup_by_index(index
);
990 DisplayState
*ds
= s
->ds
;
994 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
995 if (dcl
->con
!= NULL
) {
998 if (dcl
->ops
->dpy_gfx_switch
) {
999 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1002 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1003 surface_height(s
->surface
));
1005 if (ds
->have_text
) {
1006 dpy_text_resize(s
, s
->width
, s
->height
);
1008 text_console_update_cursor(NULL
);
1012 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1014 QemuConsole
*s
= chr
->opaque
;
1017 s
->update_x0
= s
->width
* FONT_WIDTH
;
1018 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1021 console_show_cursor(s
, 0);
1022 for(i
= 0; i
< len
; i
++) {
1023 console_putchar(s
, buf
[i
]);
1025 console_show_cursor(s
, 1);
1026 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1027 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1028 s
->update_x1
- s
->update_x0
,
1029 s
->update_y1
- s
->update_y0
);
1034 static void kbd_send_chars(void *opaque
)
1036 QemuConsole
*s
= opaque
;
1040 len
= qemu_chr_be_can_write(s
->chr
);
1041 if (len
> s
->out_fifo
.count
)
1042 len
= s
->out_fifo
.count
;
1044 if (len
> sizeof(buf
))
1046 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1047 qemu_chr_be_write(s
->chr
, buf
, len
);
1049 /* characters are pending: we send them a bit later (XXX:
1050 horrible, should change char device API) */
1051 if (s
->out_fifo
.count
> 0) {
1052 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1056 /* called when an ascii key is pressed */
1057 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1059 uint8_t buf
[16], *q
;
1062 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1066 case QEMU_KEY_CTRL_UP
:
1067 console_scroll(s
, -1);
1069 case QEMU_KEY_CTRL_DOWN
:
1070 console_scroll(s
, 1);
1072 case QEMU_KEY_CTRL_PAGEUP
:
1073 console_scroll(s
, -10);
1075 case QEMU_KEY_CTRL_PAGEDOWN
:
1076 console_scroll(s
, 10);
1079 /* convert the QEMU keysym to VT100 key string */
1081 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1084 c
= keysym
- 0xe100;
1086 *q
++ = '0' + (c
/ 10);
1087 *q
++ = '0' + (c
% 10);
1089 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1092 *q
++ = keysym
& 0xff;
1093 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1094 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1100 console_puts(s
->chr
, buf
, q
- buf
);
1102 if (s
->chr
->chr_read
) {
1103 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1110 static const int qcode_to_keysym
[Q_KEY_CODE_MAX
] = {
1111 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1112 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1113 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1114 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1115 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1116 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1117 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1118 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1119 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1122 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1126 keysym
= qcode_to_keysym
[qcode
];
1130 kbd_put_keysym_console(s
, keysym
);
1134 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1138 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1139 kbd_put_keysym_console(s
, str
[i
]);
1143 void kbd_put_keysym(int keysym
)
1145 kbd_put_keysym_console(active_console
, keysym
);
1148 static void text_console_invalidate(void *opaque
)
1150 QemuConsole
*s
= (QemuConsole
*) opaque
;
1152 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1153 text_console_resize(s
);
1158 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1160 QemuConsole
*s
= (QemuConsole
*) opaque
;
1163 if (s
->text_x
[0] <= s
->text_x
[1]) {
1164 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1165 chardata
+= s
->text_y
[0] * s
->width
;
1166 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1167 for (j
= 0; j
< s
->width
; j
++, src
++)
1168 console_write_ch(chardata
++, s
->cells
[src
].ch
|
1169 (s
->cells
[src
].t_attrib
.fgcol
<< 12) |
1170 (s
->cells
[src
].t_attrib
.bgcol
<< 8) |
1171 (s
->cells
[src
].t_attrib
.bold
<< 21));
1172 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1173 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1174 s
->text_x
[0] = s
->width
;
1175 s
->text_y
[0] = s
->height
;
1179 if (s
->cursor_invalidate
) {
1180 dpy_text_cursor(s
, s
->x
, s
->y
);
1181 s
->cursor_invalidate
= 0;
1185 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1192 obj
= object_new(TYPE_QEMU_CONSOLE
);
1193 s
= QEMU_CONSOLE(obj
);
1195 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1196 (Object
**)&s
->device
,
1197 object_property_allow_set_link
,
1198 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1200 object_property_add_uint32_ptr(obj
, "head",
1201 &s
->head
, &error_abort
);
1203 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1204 (console_type
== GRAPHIC_CONSOLE
))) {
1208 s
->console_type
= console_type
;
1210 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1211 if (console_type
!= GRAPHIC_CONSOLE
) {
1212 s
->index
= nb_consoles
;
1213 consoles
[nb_consoles
++] = s
;
1215 /* HACK: Put graphical consoles before text consoles. */
1216 for (i
= nb_consoles
; i
> 0; i
--) {
1217 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1219 consoles
[i
] = consoles
[i
- 1];
1220 consoles
[i
]->index
= i
;
1229 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1231 qemu_pixman_image_unref(surface
->image
);
1232 surface
->image
= NULL
;
1234 surface
->format
= PIXMAN_x8r8g8b8
;
1235 surface
->image
= pixman_image_create_bits(surface
->format
,
1238 assert(surface
->image
!= NULL
);
1240 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1243 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1245 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1247 trace_displaysurface_create(surface
, width
, height
);
1248 qemu_alloc_display(surface
, width
, height
);
1252 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1253 pixman_format_code_t format
,
1254 int linesize
, uint8_t *data
)
1256 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1258 trace_displaysurface_create_from(surface
, width
, height
, format
);
1259 surface
->format
= format
;
1260 surface
->image
= pixman_image_create_bits(surface
->format
,
1262 (void *)data
, linesize
);
1263 assert(surface
->image
!= NULL
);
1268 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1271 void *data
= pixman_image_get_data(image
);
1272 uint32_t size
= pixman_image_get_stride(image
) *
1273 pixman_image_get_height(image
);
1274 cpu_physical_memory_unmap(data
, size
, 0, 0);
1277 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1278 pixman_format_code_t format
,
1279 int linesize
, uint64_t addr
)
1281 DisplaySurface
*surface
;
1285 if (linesize
== 0) {
1286 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1289 size
= (hwaddr
)linesize
* height
;
1290 data
= cpu_physical_memory_map(addr
, &size
, 0);
1291 if (size
!= (hwaddr
)linesize
* height
) {
1292 cpu_physical_memory_unmap(data
, size
, 0, 0);
1296 surface
= qemu_create_displaysurface_from
1297 (width
, height
, format
, linesize
, data
);
1298 pixman_image_set_destroy_function
1299 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1304 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1307 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1308 pixman_color_t bg
= color_table_rgb
[0][COLOR_BLACK
];
1309 pixman_color_t fg
= color_table_rgb
[0][COLOR_WHITE
];
1310 pixman_image_t
*glyph
;
1314 x
= (w
/ FONT_WIDTH
- len
) / 2;
1315 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1316 for (i
= 0; i
< len
; i
++) {
1317 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1318 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1319 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1320 qemu_pixman_image_unref(glyph
);
1325 void qemu_free_displaysurface(DisplaySurface
*surface
)
1327 if (surface
== NULL
) {
1330 trace_displaysurface_free(surface
);
1331 qemu_pixman_image_unref(surface
->image
);
1335 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1337 static const char nodev
[] =
1338 "This VM has no graphic display device.";
1339 static DisplaySurface
*dummy
;
1342 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1343 dcl
->ds
= get_alloc_displaystate();
1344 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1345 gui_setup_refresh(dcl
->ds
);
1350 con
= active_console
;
1352 if (dcl
->ops
->dpy_gfx_switch
) {
1354 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1357 dummy
= qemu_create_message_surface(640, 480, nodev
);
1359 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1362 text_console_update_cursor(NULL
);
1365 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1368 DisplayState
*ds
= dcl
->ds
;
1370 dcl
->update_interval
= interval
;
1371 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1372 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1376 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1378 DisplayState
*ds
= dcl
->ds
;
1379 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1383 QLIST_REMOVE(dcl
, next
);
1384 gui_setup_refresh(ds
);
1387 static void dpy_set_ui_info_timer(void *opaque
)
1389 QemuConsole
*con
= opaque
;
1391 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1394 bool dpy_ui_info_supported(QemuConsole
*con
)
1396 return con
->hw_ops
->ui_info
!= NULL
;
1399 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1401 assert(con
!= NULL
);
1402 con
->ui_info
= *info
;
1403 if (!dpy_ui_info_supported(con
)) {
1408 * Typically we get a flood of these as the user resizes the window.
1409 * Wait until the dust has settled (one second without updates), then
1410 * go notify the guest.
1412 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1416 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1418 DisplayState
*s
= con
->ds
;
1419 DisplayChangeListener
*dcl
;
1420 int width
= surface_width(con
->surface
);
1421 int height
= surface_height(con
->surface
);
1427 w
= MIN(w
, width
- x
);
1428 h
= MIN(h
, height
- y
);
1430 if (!qemu_console_is_visible(con
)) {
1433 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1434 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1437 if (dcl
->ops
->dpy_gfx_update
) {
1438 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1443 void dpy_gfx_replace_surface(QemuConsole
*con
,
1444 DisplaySurface
*surface
)
1446 DisplayState
*s
= con
->ds
;
1447 DisplaySurface
*old_surface
= con
->surface
;
1448 DisplayChangeListener
*dcl
;
1450 con
->surface
= surface
;
1451 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1452 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1455 if (dcl
->ops
->dpy_gfx_switch
) {
1456 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1459 qemu_free_displaysurface(old_surface
);
1462 bool dpy_gfx_check_format(QemuConsole
*con
,
1463 pixman_format_code_t format
)
1465 DisplayChangeListener
*dcl
;
1466 DisplayState
*s
= con
->ds
;
1468 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1469 if (dcl
->con
&& dcl
->con
!= con
) {
1470 /* dcl bound to another console -> skip */
1473 if (dcl
->ops
->dpy_gfx_check_format
) {
1474 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1478 /* default is to whitelist native 32 bpp only */
1479 if (format
!= qemu_default_pixman_format(32, true)) {
1487 static void dpy_refresh(DisplayState
*s
)
1489 DisplayChangeListener
*dcl
;
1491 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1492 if (dcl
->ops
->dpy_refresh
) {
1493 dcl
->ops
->dpy_refresh(dcl
);
1498 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1499 int dst_x
, int dst_y
, int w
, int h
)
1501 DisplayState
*s
= con
->ds
;
1502 DisplayChangeListener
*dcl
;
1504 if (!qemu_console_is_visible(con
)) {
1507 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1508 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1511 if (dcl
->ops
->dpy_gfx_copy
) {
1512 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1514 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1519 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1521 DisplayState
*s
= con
->ds
;
1522 DisplayChangeListener
*dcl
;
1524 if (!qemu_console_is_visible(con
)) {
1527 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1528 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1531 if (dcl
->ops
->dpy_text_cursor
) {
1532 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1537 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1539 DisplayState
*s
= con
->ds
;
1540 DisplayChangeListener
*dcl
;
1542 if (!qemu_console_is_visible(con
)) {
1545 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1546 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1549 if (dcl
->ops
->dpy_text_update
) {
1550 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1555 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1557 DisplayState
*s
= con
->ds
;
1558 DisplayChangeListener
*dcl
;
1560 if (!qemu_console_is_visible(con
)) {
1563 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1564 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1567 if (dcl
->ops
->dpy_text_resize
) {
1568 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1573 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1575 DisplayState
*s
= con
->ds
;
1576 DisplayChangeListener
*dcl
;
1578 if (!qemu_console_is_visible(con
)) {
1581 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1582 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1585 if (dcl
->ops
->dpy_mouse_set
) {
1586 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1591 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1593 DisplayState
*s
= con
->ds
;
1594 DisplayChangeListener
*dcl
;
1596 if (!qemu_console_is_visible(con
)) {
1599 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1600 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1603 if (dcl
->ops
->dpy_cursor_define
) {
1604 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1609 bool dpy_cursor_define_supported(QemuConsole
*con
)
1611 DisplayState
*s
= con
->ds
;
1612 DisplayChangeListener
*dcl
;
1614 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1615 if (dcl
->ops
->dpy_cursor_define
) {
1623 * Call dpy_gfx_update for all dirity scanlines. Works for
1624 * DisplaySurfaces backed by guest memory (i.e. the ones created
1625 * using qemu_create_displaysurface_guestmem).
1627 void dpy_gfx_update_dirty(QemuConsole
*con
,
1628 MemoryRegion
*address_space
,
1632 DisplaySurface
*ds
= qemu_console_surface(con
);
1633 int width
= surface_stride(ds
);
1634 int height
= surface_height(ds
);
1635 hwaddr size
= width
* height
;
1636 MemoryRegionSection mem_section
;
1642 mem_section
= memory_region_find(address_space
, base
, size
);
1643 mem
= mem_section
.mr
;
1644 if (int128_get64(mem_section
.size
) != size
||
1645 !memory_region_is_ram(mem_section
.mr
)) {
1650 memory_region_sync_dirty_bitmap(mem
);
1651 addr
= mem_section
.offset_within_region
;
1655 for (i
= 0; i
< height
; i
++, addr
+= width
) {
1656 dirty
= invalidate
||
1657 memory_region_get_dirty(mem
, addr
, width
, DIRTY_MEMORY_VGA
);
1664 if (first
!= -1 && !dirty
) {
1665 assert(last
!= -1 && last
>= first
);
1666 dpy_gfx_update(con
, 0, first
, surface_width(ds
),
1672 assert(last
!= -1 && last
>= first
);
1673 dpy_gfx_update(con
, 0, first
, surface_width(ds
),
1677 memory_region_reset_dirty(mem
, mem_section
.offset_within_region
, size
,
1680 memory_region_unref(mem
);
1683 /***********************************************************/
1684 /* register display */
1686 /* console.c internal use only */
1687 static DisplayState
*get_alloc_displaystate(void)
1689 if (!display_state
) {
1690 display_state
= g_new0(DisplayState
, 1);
1691 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1692 text_console_update_cursor
, NULL
);
1694 return display_state
;
1698 * Called by main(), after creating QemuConsoles
1699 * and before initializing ui (sdl/vnc/...).
1701 DisplayState
*init_displaystate(void)
1706 get_alloc_displaystate();
1707 for (i
= 0; i
< nb_consoles
; i
++) {
1708 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1709 consoles
[i
]->ds
== NULL
) {
1710 text_console_do_init(consoles
[i
]->chr
, display_state
);
1713 /* Hook up into the qom tree here (not in new_console()), once
1714 * all QemuConsoles are created and the order / numbering
1715 * doesn't change any more */
1716 name
= g_strdup_printf("console[%d]", i
);
1717 object_property_add_child(container_get(object_get_root(), "/backend"),
1718 name
, OBJECT(consoles
[i
]), &error_abort
);
1722 return display_state
;
1725 void graphic_console_set_hwops(QemuConsole
*con
,
1726 const GraphicHwOps
*hw_ops
,
1729 con
->hw_ops
= hw_ops
;
1733 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1734 const GraphicHwOps
*hw_ops
,
1737 static const char noinit
[] =
1738 "Guest has not initialized the display (yet).";
1744 ds
= get_alloc_displaystate();
1745 trace_console_gfx_new();
1746 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1747 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1748 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1750 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1754 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1758 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1760 if (index
>= nb_consoles
) {
1763 return consoles
[index
];
1766 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1772 for (i
= 0; i
< nb_consoles
; i
++) {
1776 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1777 "device", &error_abort
);
1778 if (DEVICE(obj
) != dev
) {
1781 h
= object_property_get_int(OBJECT(consoles
[i
]),
1782 "head", &error_abort
);
1791 bool qemu_console_is_visible(QemuConsole
*con
)
1793 return (con
== active_console
) || (con
->dcls
> 0);
1796 bool qemu_console_is_graphic(QemuConsole
*con
)
1799 con
= active_console
;
1801 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1804 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1807 con
= active_console
;
1809 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1812 char *qemu_console_get_label(QemuConsole
*con
)
1814 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1816 return g_strdup(object_get_typename(con
->device
));
1818 return g_strdup("VGA");
1820 if (con
->chr
&& con
->chr
->label
) {
1821 return g_strdup(con
->chr
->label
);
1823 return g_strdup_printf("vc%d", con
->index
);
1827 int qemu_console_get_index(QemuConsole
*con
)
1830 con
= active_console
;
1832 return con
? con
->index
: -1;
1835 uint32_t qemu_console_get_head(QemuConsole
*con
)
1838 con
= active_console
;
1840 return con
? con
->head
: -1;
1843 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1845 assert(con
!= NULL
);
1846 return &con
->ui_info
;
1849 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1852 con
= active_console
;
1854 return con
? surface_width(con
->surface
) : fallback
;
1857 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1860 con
= active_console
;
1862 return con
? surface_height(con
->surface
) : fallback
;
1865 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1867 QemuConsole
*s
= chr
->opaque
;
1872 static void text_console_update_cursor_timer(void)
1874 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1875 + CONSOLE_CURSOR_PERIOD
/ 2);
1878 static void text_console_update_cursor(void *opaque
)
1883 cursor_visible_phase
= !cursor_visible_phase
;
1885 for (i
= 0; i
< nb_consoles
; i
++) {
1887 if (qemu_console_is_graphic(s
) ||
1888 !qemu_console_is_visible(s
)) {
1892 graphic_hw_invalidate(s
);
1896 text_console_update_cursor_timer();
1900 static const GraphicHwOps text_console_ops
= {
1901 .invalidate
= text_console_invalidate
,
1902 .text_update
= text_console_update
,
1905 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1908 int g_width
= 80 * FONT_WIDTH
;
1909 int g_height
= 24 * FONT_HEIGHT
;
1913 chr
->chr_write
= console_puts
;
1915 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1916 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1917 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
1922 s
->total_height
= DEFAULT_BACKSCROLL
;
1926 if (active_console
&& active_console
->surface
) {
1927 g_width
= surface_width(active_console
->surface
);
1928 g_height
= surface_height(active_console
->surface
);
1930 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
1933 s
->hw_ops
= &text_console_ops
;
1936 /* Set text attribute defaults */
1937 s
->t_attrib_default
.bold
= 0;
1938 s
->t_attrib_default
.uline
= 0;
1939 s
->t_attrib_default
.blink
= 0;
1940 s
->t_attrib_default
.invers
= 0;
1941 s
->t_attrib_default
.unvisible
= 0;
1942 s
->t_attrib_default
.fgcol
= COLOR_WHITE
;
1943 s
->t_attrib_default
.bgcol
= COLOR_BLACK
;
1944 /* set current text attributes to default */
1945 s
->t_attrib
= s
->t_attrib_default
;
1946 text_console_resize(s
);
1952 s
->t_attrib
.bgcol
= COLOR_BLUE
;
1953 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
1954 console_puts(chr
, (uint8_t*)msg
, len
);
1955 s
->t_attrib
= s
->t_attrib_default
;
1958 qemu_chr_be_generic_open(chr
);
1963 static CharDriverState
*text_console_init(ChardevVC
*vc
)
1965 CharDriverState
*chr
;
1968 unsigned height
= 0;
1970 chr
= qemu_chr_alloc();
1972 if (vc
->has_width
) {
1974 } else if (vc
->has_cols
) {
1975 width
= vc
->cols
* FONT_WIDTH
;
1978 if (vc
->has_height
) {
1979 height
= vc
->height
;
1980 } else if (vc
->has_rows
) {
1981 height
= vc
->rows
* FONT_HEIGHT
;
1984 trace_console_txt_new(width
, height
);
1985 if (width
== 0 || height
== 0) {
1986 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
1988 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
1989 s
->surface
= qemu_create_displaysurface(width
, height
);
1999 chr
->chr_set_echo
= text_console_set_echo
;
2000 /* console/chardev init sometimes completes elsewhere in a 2nd
2001 * stage, so defer OPENED events until they are fully initialized
2003 chr
->explicit_be_open
= true;
2005 if (display_state
) {
2006 text_console_do_init(chr
, display_state
);
2011 static VcHandler
*vc_handler
= text_console_init
;
2013 CharDriverState
*vc_init(ChardevVC
*vc
)
2015 return vc_handler(vc
);
2018 void register_vc_handler(VcHandler
*handler
)
2020 vc_handler
= handler
;
2023 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2025 DisplaySurface
*surface
;
2027 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2028 surface
= qemu_create_displaysurface(width
, height
);
2029 dpy_gfx_replace_surface(s
, surface
);
2032 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
2033 int dst_x
, int dst_y
, int w
, int h
)
2035 assert(con
->console_type
== GRAPHIC_CONSOLE
);
2036 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
2039 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2041 return console
->surface
;
2044 PixelFormat
qemu_default_pixelformat(int bpp
)
2046 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2047 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2051 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
2056 backend
->vc
= g_new0(ChardevVC
, 1);
2058 val
= qemu_opt_get_number(opts
, "width", 0);
2060 backend
->vc
->has_width
= true;
2061 backend
->vc
->width
= val
;
2064 val
= qemu_opt_get_number(opts
, "height", 0);
2066 backend
->vc
->has_height
= true;
2067 backend
->vc
->height
= val
;
2070 val
= qemu_opt_get_number(opts
, "cols", 0);
2072 backend
->vc
->has_cols
= true;
2073 backend
->vc
->cols
= val
;
2076 val
= qemu_opt_get_number(opts
, "rows", 0);
2078 backend
->vc
->has_rows
= true;
2079 backend
->vc
->rows
= val
;
2083 static const TypeInfo qemu_console_info
= {
2084 .name
= TYPE_QEMU_CONSOLE
,
2085 .parent
= TYPE_OBJECT
,
2086 .instance_size
= sizeof(QemuConsole
),
2087 .class_size
= sizeof(QemuConsoleClass
),
2091 static void register_types(void)
2093 type_register_static(&qemu_console_info
);
2094 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC
, qemu_chr_parse_vc
);
2097 type_init(register_types
);