2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "ui/console.h"
27 #include "hw/qdev-core.h"
28 #include "qemu/timer.h"
29 #include "qmp-commands.h"
30 #include "sysemu/char.h"
32 #include "exec/memory.h"
34 #define DEFAULT_BACKSCROLL 512
35 #define CONSOLE_CURSOR_PERIOD 500
37 typedef struct TextAttributes
{
47 typedef struct TextCell
{
49 TextAttributes t_attrib
;
52 #define MAX_ESC_PARAMS 3
60 typedef struct QEMUFIFO
{
63 int count
, wptr
, rptr
;
66 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
70 l
= f
->buf_size
- f
->count
;
75 l
= f
->buf_size
- f
->wptr
;
78 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
80 if (f
->wptr
>= f
->buf_size
)
89 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
97 l
= f
->buf_size
- f
->rptr
;
100 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
102 if (f
->rptr
>= f
->buf_size
)
114 TEXT_CONSOLE_FIXED_SIZE
121 console_type_t console_type
;
123 DisplaySurface
*surface
;
125 DisplayChangeListener
*gl
;
127 /* Graphic console state. */
132 const GraphicHwOps
*hw_ops
;
135 /* Text console state */
139 int backscroll_height
;
141 int x_saved
, y_saved
;
144 TextAttributes t_attrib_default
; /* default text attributes */
145 TextAttributes t_attrib
; /* currently active text attributes */
147 int text_x
[2], text_y
[2], cursor_invalidate
;
156 int esc_params
[MAX_ESC_PARAMS
];
159 CharDriverState
*chr
;
160 /* fifo for key pressed */
162 uint8_t out_fifo_buf
[16];
163 QEMUTimer
*kbd_timer
;
166 struct DisplayState
{
167 QEMUTimer
*gui_timer
;
168 uint64_t last_update
;
169 uint64_t update_interval
;
174 QLIST_HEAD(, DisplayChangeListener
) listeners
;
177 static DisplayState
*display_state
;
178 static QemuConsole
*active_console
;
179 static QemuConsole
**consoles
;
180 static int nb_consoles
= 0;
181 static bool cursor_visible_phase
;
182 static QEMUTimer
*cursor_timer
;
184 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
185 static void dpy_refresh(DisplayState
*s
);
186 static DisplayState
*get_alloc_displaystate(void);
187 static void text_console_update_cursor_timer(void);
188 static void text_console_update_cursor(void *opaque
);
190 static void gui_update(void *opaque
)
192 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
193 uint64_t dcl_interval
;
194 DisplayState
*ds
= opaque
;
195 DisplayChangeListener
*dcl
;
198 ds
->refreshing
= true;
200 ds
->refreshing
= false;
202 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
203 dcl_interval
= dcl
->update_interval
?
204 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
205 if (interval
> dcl_interval
) {
206 interval
= dcl_interval
;
209 if (ds
->update_interval
!= interval
) {
210 ds
->update_interval
= interval
;
211 for (i
= 0; i
< nb_consoles
; i
++) {
212 if (consoles
[i
]->hw_ops
->update_interval
) {
213 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
216 trace_console_refresh(interval
);
218 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
219 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
222 static void gui_setup_refresh(DisplayState
*ds
)
224 DisplayChangeListener
*dcl
;
225 bool need_timer
= false;
226 bool have_gfx
= false;
227 bool have_text
= false;
229 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
230 if (dcl
->ops
->dpy_refresh
!= NULL
) {
233 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
236 if (dcl
->ops
->dpy_text_update
!= NULL
) {
241 if (need_timer
&& ds
->gui_timer
== NULL
) {
242 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
243 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
245 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
246 timer_del(ds
->gui_timer
);
247 timer_free(ds
->gui_timer
);
248 ds
->gui_timer
= NULL
;
251 ds
->have_gfx
= have_gfx
;
252 ds
->have_text
= have_text
;
255 void graphic_hw_update(QemuConsole
*con
)
258 con
= active_console
;
260 if (con
&& con
->hw_ops
->gfx_update
) {
261 con
->hw_ops
->gfx_update(con
->hw
);
265 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
268 con
= active_console
;
270 if (con
&& con
->hw_ops
->gl_block
) {
271 con
->hw_ops
->gl_block(con
->hw
, block
);
275 void graphic_hw_invalidate(QemuConsole
*con
)
278 con
= active_console
;
280 if (con
&& con
->hw_ops
->invalidate
) {
281 con
->hw_ops
->invalidate(con
->hw
);
285 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
288 int width
= pixman_image_get_width(ds
->image
);
289 int height
= pixman_image_get_height(ds
->image
);
294 pixman_image_t
*linebuf
;
296 trace_ppm_save(filename
, ds
);
297 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
299 error_setg(errp
, "failed to open file '%s': %s", filename
,
303 f
= fdopen(fd
, "wb");
304 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
309 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
310 for (y
= 0; y
< height
; y
++) {
311 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
313 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
314 pixman_image_get_stride(linebuf
), f
);
322 qemu_pixman_image_unref(linebuf
);
327 error_setg(errp
, "failed to write to file '%s': %s", filename
,
333 void qmp_screendump(const char *filename
, Error
**errp
)
335 QemuConsole
*con
= qemu_console_lookup_by_index(0);
336 DisplaySurface
*surface
;
339 error_setg(errp
, "There is no QemuConsole I can screendump from.");
343 graphic_hw_update(con
);
344 surface
= qemu_console_surface(con
);
345 ppm_save(filename
, surface
, errp
);
348 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
351 con
= active_console
;
353 if (con
&& con
->hw_ops
->text_update
) {
354 con
->hw_ops
->text_update(con
->hw
, chardata
);
358 static void vga_fill_rect(QemuConsole
*con
,
359 int posx
, int posy
, int width
, int height
,
360 pixman_color_t color
)
362 DisplaySurface
*surface
= qemu_console_surface(con
);
363 pixman_rectangle16_t rect
= {
364 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
367 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
371 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
372 static void vga_bitblt(QemuConsole
*con
,
373 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
375 DisplaySurface
*surface
= qemu_console_surface(con
);
377 pixman_image_composite(PIXMAN_OP_SRC
,
378 surface
->image
, NULL
, surface
->image
,
379 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
382 /***********************************************************/
383 /* basic char display */
385 #define FONT_HEIGHT 16
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_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
396 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
397 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
398 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
399 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
400 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
401 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
402 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
405 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
406 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
407 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
408 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
409 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
410 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
411 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
412 [QEMU_COLOR_WHITE
] = 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_new(TextCell
, s
->width
* s
->total_height
);
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][QEMU_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
= QEMU_COLOR_BLACK
;
702 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
705 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
708 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
711 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
714 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
717 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
720 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
722 /* set background color */
724 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
727 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
730 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
733 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
736 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
739 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
742 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
745 s
->t_attrib
.bgcol
= QEMU_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 static void console_put_one(QemuConsole
*s
, int ch
)
764 if (s
->x
>= s
->width
) {
769 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
770 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
772 c
->t_attrib
= s
->t_attrib
;
773 update_xy(s
, s
->x
, s
->y
);
777 static void console_respond_str(QemuConsole
*s
, const char *buf
)
780 console_put_one(s
, *buf
);
785 /* set cursor, checking bounds */
786 static void set_cursor(QemuConsole
*s
, int x
, int y
)
794 if (y
>= s
->height
) {
805 static void console_putchar(QemuConsole
*s
, int ch
)
814 case '\r': /* carriage return */
817 case '\n': /* newline */
820 case '\b': /* backspace */
824 case '\t': /* tabspace */
825 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
829 s
->x
= s
->x
+ (8 - (s
->x
% 8));
832 case '\a': /* alert aka. bell */
833 /* TODO: has to be implemented */
836 /* SI (shift in), character set 0 (ignored) */
839 /* SO (shift out), character set 1 (ignored) */
841 case 27: /* esc (introducing an escape sequence) */
842 s
->state
= TTY_STATE_ESC
;
845 console_put_one(s
, ch
);
849 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
851 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
852 s
->esc_params
[i
] = 0;
853 s
->nb_esc_params
= 0;
854 s
->state
= TTY_STATE_CSI
;
856 s
->state
= TTY_STATE_NORM
;
859 case TTY_STATE_CSI
: /* handle escape sequence parameters */
860 if (ch
>= '0' && ch
<= '9') {
861 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
862 int *param
= &s
->esc_params
[s
->nb_esc_params
];
863 int digit
= (ch
- '0');
865 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
866 *param
* 10 + digit
: INT_MAX
;
869 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
873 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
874 ch
, s
->nb_esc_params
);
875 s
->state
= TTY_STATE_NORM
;
879 if (s
->esc_params
[0] == 0) {
880 s
->esc_params
[0] = 1;
882 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
885 /* move cursor down */
886 if (s
->esc_params
[0] == 0) {
887 s
->esc_params
[0] = 1;
889 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
892 /* move cursor right */
893 if (s
->esc_params
[0] == 0) {
894 s
->esc_params
[0] = 1;
896 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
899 /* move cursor left */
900 if (s
->esc_params
[0] == 0) {
901 s
->esc_params
[0] = 1;
903 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
906 /* move cursor to column */
907 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
911 /* move cursor to row, column */
912 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
915 switch (s
->esc_params
[0]) {
917 /* clear to end of screen */
918 for (y
= s
->y
; y
< s
->height
; y
++) {
919 for (x
= 0; x
< s
->width
; x
++) {
920 if (y
== s
->y
&& x
< s
->x
) {
923 console_clear_xy(s
, x
, y
);
928 /* clear from beginning of screen */
929 for (y
= 0; y
<= s
->y
; y
++) {
930 for (x
= 0; x
< s
->width
; x
++) {
931 if (y
== s
->y
&& x
> s
->x
) {
934 console_clear_xy(s
, x
, y
);
939 /* clear entire screen */
940 for (y
= 0; y
<= s
->height
; y
++) {
941 for (x
= 0; x
< s
->width
; x
++) {
942 console_clear_xy(s
, x
, y
);
949 switch (s
->esc_params
[0]) {
952 for(x
= s
->x
; x
< s
->width
; x
++) {
953 console_clear_xy(s
, x
, s
->y
);
957 /* clear from beginning of line */
958 for (x
= 0; x
<= s
->x
; x
++) {
959 console_clear_xy(s
, x
, s
->y
);
963 /* clear entire line */
964 for(x
= 0; x
< s
->width
; x
++) {
965 console_clear_xy(s
, x
, s
->y
);
971 console_handle_escape(s
);
974 switch (s
->esc_params
[0]) {
976 /* report console status (always succeed)*/
977 console_respond_str(s
, "\033[0n");
980 /* report cursor position */
981 sprintf(response
, "\033[%d;%dR",
982 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
984 console_respond_str(s
, response
);
989 /* save cursor position */
994 /* restore cursor position */
999 trace_console_putchar_unhandled(ch
);
1007 void console_select(unsigned int index
)
1009 DisplayChangeListener
*dcl
;
1012 trace_console_select(index
);
1013 s
= qemu_console_lookup_by_index(index
);
1015 DisplayState
*ds
= s
->ds
;
1019 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1020 if (dcl
->con
!= NULL
) {
1023 if (dcl
->ops
->dpy_gfx_switch
) {
1024 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1027 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1028 surface_height(s
->surface
));
1030 if (ds
->have_text
) {
1031 dpy_text_resize(s
, s
->width
, s
->height
);
1033 text_console_update_cursor(NULL
);
1037 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1039 QemuConsole
*s
= chr
->opaque
;
1042 s
->update_x0
= s
->width
* FONT_WIDTH
;
1043 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1046 console_show_cursor(s
, 0);
1047 for(i
= 0; i
< len
; i
++) {
1048 console_putchar(s
, buf
[i
]);
1050 console_show_cursor(s
, 1);
1051 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1052 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1053 s
->update_x1
- s
->update_x0
,
1054 s
->update_y1
- s
->update_y0
);
1059 static void kbd_send_chars(void *opaque
)
1061 QemuConsole
*s
= opaque
;
1065 len
= qemu_chr_be_can_write(s
->chr
);
1066 if (len
> s
->out_fifo
.count
)
1067 len
= s
->out_fifo
.count
;
1069 if (len
> sizeof(buf
))
1071 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1072 qemu_chr_be_write(s
->chr
, buf
, len
);
1074 /* characters are pending: we send them a bit later (XXX:
1075 horrible, should change char device API) */
1076 if (s
->out_fifo
.count
> 0) {
1077 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1081 /* called when an ascii key is pressed */
1082 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1084 uint8_t buf
[16], *q
;
1087 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1091 case QEMU_KEY_CTRL_UP
:
1092 console_scroll(s
, -1);
1094 case QEMU_KEY_CTRL_DOWN
:
1095 console_scroll(s
, 1);
1097 case QEMU_KEY_CTRL_PAGEUP
:
1098 console_scroll(s
, -10);
1100 case QEMU_KEY_CTRL_PAGEDOWN
:
1101 console_scroll(s
, 10);
1104 /* convert the QEMU keysym to VT100 key string */
1106 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1109 c
= keysym
- 0xe100;
1111 *q
++ = '0' + (c
/ 10);
1112 *q
++ = '0' + (c
% 10);
1114 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1117 *q
++ = keysym
& 0xff;
1118 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1119 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1125 console_puts(s
->chr
, buf
, q
- buf
);
1127 if (s
->chr
->chr_read
) {
1128 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1135 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1136 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1137 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1138 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1139 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1140 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1141 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1142 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1143 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1144 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1147 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1151 keysym
= qcode_to_keysym
[qcode
];
1155 kbd_put_keysym_console(s
, keysym
);
1159 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1163 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1164 kbd_put_keysym_console(s
, str
[i
]);
1168 void kbd_put_keysym(int keysym
)
1170 kbd_put_keysym_console(active_console
, keysym
);
1173 static void text_console_invalidate(void *opaque
)
1175 QemuConsole
*s
= (QemuConsole
*) opaque
;
1177 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1178 text_console_resize(s
);
1183 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1185 QemuConsole
*s
= (QemuConsole
*) opaque
;
1188 if (s
->text_x
[0] <= s
->text_x
[1]) {
1189 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1190 chardata
+= s
->text_y
[0] * s
->width
;
1191 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1192 for (j
= 0; j
< s
->width
; j
++, src
++) {
1193 console_write_ch(chardata
++,
1194 ATTR2CHTYPE(s
->cells
[src
].ch
,
1195 s
->cells
[src
].t_attrib
.fgcol
,
1196 s
->cells
[src
].t_attrib
.bgcol
,
1197 s
->cells
[src
].t_attrib
.bold
));
1199 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1200 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1201 s
->text_x
[0] = s
->width
;
1202 s
->text_y
[0] = s
->height
;
1206 if (s
->cursor_invalidate
) {
1207 dpy_text_cursor(s
, s
->x
, s
->y
);
1208 s
->cursor_invalidate
= 0;
1212 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1219 obj
= object_new(TYPE_QEMU_CONSOLE
);
1220 s
= QEMU_CONSOLE(obj
);
1222 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1223 (Object
**)&s
->device
,
1224 object_property_allow_set_link
,
1225 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1227 object_property_add_uint32_ptr(obj
, "head",
1228 &s
->head
, &error_abort
);
1230 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1231 (console_type
== GRAPHIC_CONSOLE
))) {
1235 s
->console_type
= console_type
;
1237 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1238 if (console_type
!= GRAPHIC_CONSOLE
) {
1239 s
->index
= nb_consoles
;
1240 consoles
[nb_consoles
++] = s
;
1242 /* HACK: Put graphical consoles before text consoles. */
1243 for (i
= nb_consoles
; i
> 0; i
--) {
1244 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1246 consoles
[i
] = consoles
[i
- 1];
1247 consoles
[i
]->index
= i
;
1256 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1258 qemu_pixman_image_unref(surface
->image
);
1259 surface
->image
= NULL
;
1261 surface
->format
= PIXMAN_x8r8g8b8
;
1262 surface
->image
= pixman_image_create_bits(surface
->format
,
1265 assert(surface
->image
!= NULL
);
1267 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1270 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1272 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1274 trace_displaysurface_create(surface
, width
, height
);
1275 qemu_alloc_display(surface
, width
, height
);
1279 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1280 pixman_format_code_t format
,
1281 int linesize
, uint8_t *data
)
1283 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1285 trace_displaysurface_create_from(surface
, width
, height
, format
);
1286 surface
->format
= format
;
1287 surface
->image
= pixman_image_create_bits(surface
->format
,
1289 (void *)data
, linesize
);
1290 assert(surface
->image
!= NULL
);
1295 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1298 void *data
= pixman_image_get_data(image
);
1299 uint32_t size
= pixman_image_get_stride(image
) *
1300 pixman_image_get_height(image
);
1301 cpu_physical_memory_unmap(data
, size
, 0, 0);
1304 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1305 pixman_format_code_t format
,
1306 int linesize
, uint64_t addr
)
1308 DisplaySurface
*surface
;
1312 if (linesize
== 0) {
1313 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1316 size
= (hwaddr
)linesize
* height
;
1317 data
= cpu_physical_memory_map(addr
, &size
, 0);
1318 if (size
!= (hwaddr
)linesize
* height
) {
1319 cpu_physical_memory_unmap(data
, size
, 0, 0);
1323 surface
= qemu_create_displaysurface_from
1324 (width
, height
, format
, linesize
, data
);
1325 pixman_image_set_destroy_function
1326 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1331 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1334 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1335 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1336 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1337 pixman_image_t
*glyph
;
1341 x
= (w
/ FONT_WIDTH
- len
) / 2;
1342 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1343 for (i
= 0; i
< len
; i
++) {
1344 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1345 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1346 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1347 qemu_pixman_image_unref(glyph
);
1352 void qemu_free_displaysurface(DisplaySurface
*surface
)
1354 if (surface
== NULL
) {
1357 trace_displaysurface_free(surface
);
1358 qemu_pixman_image_unref(surface
->image
);
1362 bool console_has_gl(QemuConsole
*con
)
1364 return con
->gl
!= NULL
;
1367 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1369 static const char nodev
[] =
1370 "This VM has no graphic display device.";
1371 static DisplaySurface
*dummy
;
1374 if (dcl
->ops
->dpy_gl_ctx_create
) {
1375 /* display has opengl support */
1378 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1379 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1385 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1386 dcl
->ds
= get_alloc_displaystate();
1387 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1388 gui_setup_refresh(dcl
->ds
);
1393 con
= active_console
;
1395 if (dcl
->ops
->dpy_gfx_switch
) {
1397 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1400 dummy
= qemu_create_message_surface(640, 480, nodev
);
1402 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1405 text_console_update_cursor(NULL
);
1408 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1411 DisplayState
*ds
= dcl
->ds
;
1413 dcl
->update_interval
= interval
;
1414 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1415 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1419 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1421 DisplayState
*ds
= dcl
->ds
;
1422 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1426 QLIST_REMOVE(dcl
, next
);
1427 gui_setup_refresh(ds
);
1430 static void dpy_set_ui_info_timer(void *opaque
)
1432 QemuConsole
*con
= opaque
;
1434 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1437 bool dpy_ui_info_supported(QemuConsole
*con
)
1439 return con
->hw_ops
->ui_info
!= NULL
;
1442 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1444 assert(con
!= NULL
);
1445 con
->ui_info
= *info
;
1446 if (!dpy_ui_info_supported(con
)) {
1451 * Typically we get a flood of these as the user resizes the window.
1452 * Wait until the dust has settled (one second without updates), then
1453 * go notify the guest.
1455 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1459 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1461 DisplayState
*s
= con
->ds
;
1462 DisplayChangeListener
*dcl
;
1467 width
= surface_width(con
->surface
);
1468 height
= surface_height(con
->surface
);
1474 w
= MIN(w
, width
- x
);
1475 h
= MIN(h
, height
- y
);
1477 if (!qemu_console_is_visible(con
)) {
1480 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1481 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1484 if (dcl
->ops
->dpy_gfx_update
) {
1485 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1490 void dpy_gfx_replace_surface(QemuConsole
*con
,
1491 DisplaySurface
*surface
)
1493 DisplayState
*s
= con
->ds
;
1494 DisplaySurface
*old_surface
= con
->surface
;
1495 DisplayChangeListener
*dcl
;
1497 con
->surface
= surface
;
1498 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1499 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1502 if (dcl
->ops
->dpy_gfx_switch
) {
1503 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1506 qemu_free_displaysurface(old_surface
);
1509 bool dpy_gfx_check_format(QemuConsole
*con
,
1510 pixman_format_code_t format
)
1512 DisplayChangeListener
*dcl
;
1513 DisplayState
*s
= con
->ds
;
1515 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1516 if (dcl
->con
&& dcl
->con
!= con
) {
1517 /* dcl bound to another console -> skip */
1520 if (dcl
->ops
->dpy_gfx_check_format
) {
1521 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1525 /* default is to whitelist native 32 bpp only */
1526 if (format
!= qemu_default_pixman_format(32, true)) {
1534 static void dpy_refresh(DisplayState
*s
)
1536 DisplayChangeListener
*dcl
;
1538 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1539 if (dcl
->ops
->dpy_refresh
) {
1540 dcl
->ops
->dpy_refresh(dcl
);
1545 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1546 int dst_x
, int dst_y
, int w
, int h
)
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_gfx_copy
) {
1559 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1561 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1566 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1568 DisplayState
*s
= con
->ds
;
1569 DisplayChangeListener
*dcl
;
1571 if (!qemu_console_is_visible(con
)) {
1574 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1575 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1578 if (dcl
->ops
->dpy_text_cursor
) {
1579 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1584 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1586 DisplayState
*s
= con
->ds
;
1587 DisplayChangeListener
*dcl
;
1589 if (!qemu_console_is_visible(con
)) {
1592 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1593 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1596 if (dcl
->ops
->dpy_text_update
) {
1597 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1602 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1604 DisplayState
*s
= con
->ds
;
1605 DisplayChangeListener
*dcl
;
1607 if (!qemu_console_is_visible(con
)) {
1610 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1611 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1614 if (dcl
->ops
->dpy_text_resize
) {
1615 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1620 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1622 DisplayState
*s
= con
->ds
;
1623 DisplayChangeListener
*dcl
;
1625 if (!qemu_console_is_visible(con
)) {
1628 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1629 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1632 if (dcl
->ops
->dpy_mouse_set
) {
1633 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1638 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1640 DisplayState
*s
= con
->ds
;
1641 DisplayChangeListener
*dcl
;
1643 if (!qemu_console_is_visible(con
)) {
1646 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1647 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1650 if (dcl
->ops
->dpy_cursor_define
) {
1651 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1656 bool dpy_cursor_define_supported(QemuConsole
*con
)
1658 DisplayState
*s
= con
->ds
;
1659 DisplayChangeListener
*dcl
;
1661 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1662 if (dcl
->ops
->dpy_cursor_define
) {
1669 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1670 struct QEMUGLParams
*qparams
)
1673 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1676 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1679 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1682 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1685 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1688 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1691 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1694 void dpy_gl_scanout(QemuConsole
*con
,
1695 uint32_t backing_id
, bool backing_y_0_top
,
1696 uint32_t x
, uint32_t y
, uint32_t width
, uint32_t height
)
1699 con
->gl
->ops
->dpy_gl_scanout(con
->gl
, backing_id
,
1701 x
, y
, width
, height
);
1704 void dpy_gl_update(QemuConsole
*con
,
1705 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1708 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1711 /***********************************************************/
1712 /* register display */
1714 /* console.c internal use only */
1715 static DisplayState
*get_alloc_displaystate(void)
1717 if (!display_state
) {
1718 display_state
= g_new0(DisplayState
, 1);
1719 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1720 text_console_update_cursor
, NULL
);
1722 return display_state
;
1726 * Called by main(), after creating QemuConsoles
1727 * and before initializing ui (sdl/vnc/...).
1729 DisplayState
*init_displaystate(void)
1734 get_alloc_displaystate();
1735 for (i
= 0; i
< nb_consoles
; i
++) {
1736 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1737 consoles
[i
]->ds
== NULL
) {
1738 text_console_do_init(consoles
[i
]->chr
, display_state
);
1741 /* Hook up into the qom tree here (not in new_console()), once
1742 * all QemuConsoles are created and the order / numbering
1743 * doesn't change any more */
1744 name
= g_strdup_printf("console[%d]", i
);
1745 object_property_add_child(container_get(object_get_root(), "/backend"),
1746 name
, OBJECT(consoles
[i
]), &error_abort
);
1750 return display_state
;
1753 void graphic_console_set_hwops(QemuConsole
*con
,
1754 const GraphicHwOps
*hw_ops
,
1757 con
->hw_ops
= hw_ops
;
1761 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1762 const GraphicHwOps
*hw_ops
,
1765 static const char noinit
[] =
1766 "Guest has not initialized the display (yet).";
1772 ds
= get_alloc_displaystate();
1773 trace_console_gfx_new();
1774 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1775 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1776 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1778 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1782 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1786 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1788 if (index
>= nb_consoles
) {
1791 return consoles
[index
];
1794 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1800 for (i
= 0; i
< nb_consoles
; i
++) {
1804 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1805 "device", &error_abort
);
1806 if (DEVICE(obj
) != dev
) {
1809 h
= object_property_get_int(OBJECT(consoles
[i
]),
1810 "head", &error_abort
);
1819 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1820 uint32_t head
, Error
**errp
)
1825 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1827 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1828 "Device '%s' not found", device_id
);
1832 con
= qemu_console_lookup_by_device(dev
, head
);
1834 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1842 bool qemu_console_is_visible(QemuConsole
*con
)
1844 return (con
== active_console
) || (con
->dcls
> 0);
1847 bool qemu_console_is_graphic(QemuConsole
*con
)
1850 con
= active_console
;
1852 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1855 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1858 con
= active_console
;
1860 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1863 char *qemu_console_get_label(QemuConsole
*con
)
1865 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1867 return g_strdup(object_get_typename(con
->device
));
1869 return g_strdup("VGA");
1871 if (con
->chr
&& con
->chr
->label
) {
1872 return g_strdup(con
->chr
->label
);
1874 return g_strdup_printf("vc%d", con
->index
);
1878 int qemu_console_get_index(QemuConsole
*con
)
1881 con
= active_console
;
1883 return con
? con
->index
: -1;
1886 uint32_t qemu_console_get_head(QemuConsole
*con
)
1889 con
= active_console
;
1891 return con
? con
->head
: -1;
1894 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1896 assert(con
!= NULL
);
1897 return &con
->ui_info
;
1900 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1903 con
= active_console
;
1905 return con
? surface_width(con
->surface
) : fallback
;
1908 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1911 con
= active_console
;
1913 return con
? surface_height(con
->surface
) : fallback
;
1916 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1918 QemuConsole
*s
= chr
->opaque
;
1923 static void text_console_update_cursor_timer(void)
1925 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1926 + CONSOLE_CURSOR_PERIOD
/ 2);
1929 static void text_console_update_cursor(void *opaque
)
1934 cursor_visible_phase
= !cursor_visible_phase
;
1936 for (i
= 0; i
< nb_consoles
; i
++) {
1938 if (qemu_console_is_graphic(s
) ||
1939 !qemu_console_is_visible(s
)) {
1943 graphic_hw_invalidate(s
);
1947 text_console_update_cursor_timer();
1951 static const GraphicHwOps text_console_ops
= {
1952 .invalidate
= text_console_invalidate
,
1953 .text_update
= text_console_update
,
1956 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1959 int g_width
= 80 * FONT_WIDTH
;
1960 int g_height
= 24 * FONT_HEIGHT
;
1964 chr
->chr_write
= console_puts
;
1966 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1967 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1968 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
1973 s
->total_height
= DEFAULT_BACKSCROLL
;
1977 if (active_console
&& active_console
->surface
) {
1978 g_width
= surface_width(active_console
->surface
);
1979 g_height
= surface_height(active_console
->surface
);
1981 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
1984 s
->hw_ops
= &text_console_ops
;
1987 /* Set text attribute defaults */
1988 s
->t_attrib_default
.bold
= 0;
1989 s
->t_attrib_default
.uline
= 0;
1990 s
->t_attrib_default
.blink
= 0;
1991 s
->t_attrib_default
.invers
= 0;
1992 s
->t_attrib_default
.unvisible
= 0;
1993 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
1994 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
1995 /* set current text attributes to default */
1996 s
->t_attrib
= s
->t_attrib_default
;
1997 text_console_resize(s
);
2003 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2004 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2005 console_puts(chr
, (uint8_t*)msg
, len
);
2006 s
->t_attrib
= s
->t_attrib_default
;
2009 qemu_chr_be_generic_open(chr
);
2014 static CharDriverState
*text_console_init(ChardevVC
*vc
, Error
**errp
)
2016 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
2017 CharDriverState
*chr
;
2020 unsigned height
= 0;
2022 chr
= qemu_chr_alloc(common
, errp
);
2027 if (vc
->has_width
) {
2029 } else if (vc
->has_cols
) {
2030 width
= vc
->cols
* FONT_WIDTH
;
2033 if (vc
->has_height
) {
2034 height
= vc
->height
;
2035 } else if (vc
->has_rows
) {
2036 height
= vc
->rows
* FONT_HEIGHT
;
2039 trace_console_txt_new(width
, height
);
2040 if (width
== 0 || height
== 0) {
2041 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2043 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2044 s
->surface
= qemu_create_displaysurface(width
, height
);
2049 error_setg(errp
, "cannot create text console");
2055 chr
->chr_set_echo
= text_console_set_echo
;
2056 /* console/chardev init sometimes completes elsewhere in a 2nd
2057 * stage, so defer OPENED events until they are fully initialized
2059 chr
->explicit_be_open
= true;
2061 if (display_state
) {
2062 text_console_do_init(chr
, display_state
);
2067 static VcHandler
*vc_handler
= text_console_init
;
2069 static CharDriverState
*vc_init(const char *id
, ChardevBackend
*backend
,
2070 ChardevReturn
*ret
, Error
**errp
)
2072 return vc_handler(backend
->u
.vc
, errp
);
2075 void register_vc_handler(VcHandler
*handler
)
2077 vc_handler
= handler
;
2080 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2082 DisplaySurface
*surface
;
2084 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2085 surface
= qemu_create_displaysurface(width
, height
);
2086 dpy_gfx_replace_surface(s
, surface
);
2089 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
2090 int dst_x
, int dst_y
, int w
, int h
)
2092 assert(con
->console_type
== GRAPHIC_CONSOLE
);
2093 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
2096 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2098 return console
->surface
;
2101 PixelFormat
qemu_default_pixelformat(int bpp
)
2103 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2104 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2108 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
2114 vc
= backend
->u
.vc
= g_new0(ChardevVC
, 1);
2115 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2117 val
= qemu_opt_get_number(opts
, "width", 0);
2119 vc
->has_width
= true;
2123 val
= qemu_opt_get_number(opts
, "height", 0);
2125 vc
->has_height
= true;
2129 val
= qemu_opt_get_number(opts
, "cols", 0);
2131 vc
->has_cols
= true;
2135 val
= qemu_opt_get_number(opts
, "rows", 0);
2137 vc
->has_rows
= true;
2142 static const TypeInfo qemu_console_info
= {
2143 .name
= TYPE_QEMU_CONSOLE
,
2144 .parent
= TYPE_OBJECT
,
2145 .instance_size
= sizeof(QemuConsole
),
2146 .class_size
= sizeof(QemuConsoleClass
),
2150 static void register_types(void)
2152 type_register_static(&qemu_console_info
);
2153 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC
, qemu_chr_parse_vc
,
2157 type_init(register_types
);