2 * QEMU graphical console
4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
27 #include "ui/console.h"
28 #include "hw/qdev-core.h"
29 #include "qemu/timer.h"
30 #include "qmp-commands.h"
31 #include "sysemu/char.h"
33 #include "exec/memory.h"
35 #define DEFAULT_BACKSCROLL 512
36 #define CONSOLE_CURSOR_PERIOD 500
38 typedef struct TextAttributes
{
48 typedef struct TextCell
{
50 TextAttributes t_attrib
;
53 #define MAX_ESC_PARAMS 3
61 typedef struct QEMUFIFO
{
64 int count
, wptr
, rptr
;
67 static int qemu_fifo_write(QEMUFIFO
*f
, const uint8_t *buf
, int len1
)
71 l
= f
->buf_size
- f
->count
;
76 l
= f
->buf_size
- f
->wptr
;
79 memcpy(f
->buf
+ f
->wptr
, buf
, l
);
81 if (f
->wptr
>= f
->buf_size
)
90 static int qemu_fifo_read(QEMUFIFO
*f
, uint8_t *buf
, int len1
)
98 l
= f
->buf_size
- f
->rptr
;
101 memcpy(buf
, f
->buf
+ f
->rptr
, l
);
103 if (f
->rptr
>= f
->buf_size
)
115 TEXT_CONSOLE_FIXED_SIZE
122 console_type_t console_type
;
124 DisplaySurface
*surface
;
126 DisplayChangeListener
*gl
;
128 /* Graphic console state. */
133 const GraphicHwOps
*hw_ops
;
136 /* Text console state */
140 int backscroll_height
;
142 int x_saved
, y_saved
;
145 TextAttributes t_attrib_default
; /* default text attributes */
146 TextAttributes t_attrib
; /* currently active text attributes */
148 int text_x
[2], text_y
[2], cursor_invalidate
;
157 int esc_params
[MAX_ESC_PARAMS
];
160 CharDriverState
*chr
;
161 /* fifo for key pressed */
163 uint8_t out_fifo_buf
[16];
164 QEMUTimer
*kbd_timer
;
167 struct DisplayState
{
168 QEMUTimer
*gui_timer
;
169 uint64_t last_update
;
170 uint64_t update_interval
;
175 QLIST_HEAD(, DisplayChangeListener
) listeners
;
178 static DisplayState
*display_state
;
179 static QemuConsole
*active_console
;
180 static QemuConsole
**consoles
;
181 static int nb_consoles
= 0;
182 static bool cursor_visible_phase
;
183 static QEMUTimer
*cursor_timer
;
185 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
);
186 static void dpy_refresh(DisplayState
*s
);
187 static DisplayState
*get_alloc_displaystate(void);
188 static void text_console_update_cursor_timer(void);
189 static void text_console_update_cursor(void *opaque
);
191 static void gui_update(void *opaque
)
193 uint64_t interval
= GUI_REFRESH_INTERVAL_IDLE
;
194 uint64_t dcl_interval
;
195 DisplayState
*ds
= opaque
;
196 DisplayChangeListener
*dcl
;
199 ds
->refreshing
= true;
201 ds
->refreshing
= false;
203 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
204 dcl_interval
= dcl
->update_interval
?
205 dcl
->update_interval
: GUI_REFRESH_INTERVAL_DEFAULT
;
206 if (interval
> dcl_interval
) {
207 interval
= dcl_interval
;
210 if (ds
->update_interval
!= interval
) {
211 ds
->update_interval
= interval
;
212 for (i
= 0; i
< nb_consoles
; i
++) {
213 if (consoles
[i
]->hw_ops
->update_interval
) {
214 consoles
[i
]->hw_ops
->update_interval(consoles
[i
]->hw
, interval
);
217 trace_console_refresh(interval
);
219 ds
->last_update
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
220 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
223 static void gui_setup_refresh(DisplayState
*ds
)
225 DisplayChangeListener
*dcl
;
226 bool need_timer
= false;
227 bool have_gfx
= false;
228 bool have_text
= false;
230 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
231 if (dcl
->ops
->dpy_refresh
!= NULL
) {
234 if (dcl
->ops
->dpy_gfx_update
!= NULL
) {
237 if (dcl
->ops
->dpy_text_update
!= NULL
) {
242 if (need_timer
&& ds
->gui_timer
== NULL
) {
243 ds
->gui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, gui_update
, ds
);
244 timer_mod(ds
->gui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
246 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
247 timer_del(ds
->gui_timer
);
248 timer_free(ds
->gui_timer
);
249 ds
->gui_timer
= NULL
;
252 ds
->have_gfx
= have_gfx
;
253 ds
->have_text
= have_text
;
256 void graphic_hw_update(QemuConsole
*con
)
259 con
= active_console
;
261 if (con
&& con
->hw_ops
->gfx_update
) {
262 con
->hw_ops
->gfx_update(con
->hw
);
266 void graphic_hw_gl_block(QemuConsole
*con
, bool block
)
269 con
= active_console
;
271 if (con
&& con
->hw_ops
->gl_block
) {
272 con
->hw_ops
->gl_block(con
->hw
, block
);
276 void graphic_hw_invalidate(QemuConsole
*con
)
279 con
= active_console
;
281 if (con
&& con
->hw_ops
->invalidate
) {
282 con
->hw_ops
->invalidate(con
->hw
);
286 static void ppm_save(const char *filename
, DisplaySurface
*ds
,
289 int width
= pixman_image_get_width(ds
->image
);
290 int height
= pixman_image_get_height(ds
->image
);
295 pixman_image_t
*linebuf
;
297 trace_ppm_save(filename
, ds
);
298 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
300 error_setg(errp
, "failed to open file '%s': %s", filename
,
304 f
= fdopen(fd
, "wb");
305 ret
= fprintf(f
, "P6\n%d %d\n%d\n", width
, height
, 255);
310 linebuf
= qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8
, width
);
311 for (y
= 0; y
< height
; y
++) {
312 qemu_pixman_linebuf_fill(linebuf
, ds
->image
, width
, 0, y
);
314 ret
= fwrite(pixman_image_get_data(linebuf
), 1,
315 pixman_image_get_stride(linebuf
), f
);
323 qemu_pixman_image_unref(linebuf
);
328 error_setg(errp
, "failed to write to file '%s': %s", filename
,
334 void qmp_screendump(const char *filename
, Error
**errp
)
336 QemuConsole
*con
= qemu_console_lookup_by_index(0);
337 DisplaySurface
*surface
;
340 error_setg(errp
, "There is no QemuConsole I can screendump from.");
344 graphic_hw_update(con
);
345 surface
= qemu_console_surface(con
);
346 ppm_save(filename
, surface
, errp
);
349 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
)
352 con
= active_console
;
354 if (con
&& con
->hw_ops
->text_update
) {
355 con
->hw_ops
->text_update(con
->hw
, chardata
);
359 static void vga_fill_rect(QemuConsole
*con
,
360 int posx
, int posy
, int width
, int height
,
361 pixman_color_t color
)
363 DisplaySurface
*surface
= qemu_console_surface(con
);
364 pixman_rectangle16_t rect
= {
365 .x
= posx
, .y
= posy
, .width
= width
, .height
= height
368 pixman_image_fill_rectangles(PIXMAN_OP_SRC
, surface
->image
,
372 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
373 static void vga_bitblt(QemuConsole
*con
,
374 int xs
, int ys
, int xd
, int yd
, int w
, int h
)
376 DisplaySurface
*surface
= qemu_console_surface(con
);
378 pixman_image_composite(PIXMAN_OP_SRC
,
379 surface
->image
, NULL
, surface
->image
,
380 xs
, ys
, 0, 0, xd
, yd
, w
, h
);
383 /***********************************************************/
384 /* basic char display */
386 #define FONT_HEIGHT 16
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_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
397 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
398 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
399 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
400 [QEMU_COLOR_RED
] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
401 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
402 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
403 [QEMU_COLOR_WHITE
] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
406 [QEMU_COLOR_BLACK
] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
407 [QEMU_COLOR_BLUE
] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
408 [QEMU_COLOR_GREEN
] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
409 [QEMU_COLOR_CYAN
] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
410 [QEMU_COLOR_RED
] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
411 [QEMU_COLOR_MAGENTA
] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
412 [QEMU_COLOR_YELLOW
] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
413 [QEMU_COLOR_WHITE
] = 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_new(TextCell
, s
->width
* s
->total_height
);
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][QEMU_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
= QEMU_COLOR_BLACK
;
703 s
->t_attrib
.fgcol
= QEMU_COLOR_RED
;
706 s
->t_attrib
.fgcol
= QEMU_COLOR_GREEN
;
709 s
->t_attrib
.fgcol
= QEMU_COLOR_YELLOW
;
712 s
->t_attrib
.fgcol
= QEMU_COLOR_BLUE
;
715 s
->t_attrib
.fgcol
= QEMU_COLOR_MAGENTA
;
718 s
->t_attrib
.fgcol
= QEMU_COLOR_CYAN
;
721 s
->t_attrib
.fgcol
= QEMU_COLOR_WHITE
;
723 /* set background color */
725 s
->t_attrib
.bgcol
= QEMU_COLOR_BLACK
;
728 s
->t_attrib
.bgcol
= QEMU_COLOR_RED
;
731 s
->t_attrib
.bgcol
= QEMU_COLOR_GREEN
;
734 s
->t_attrib
.bgcol
= QEMU_COLOR_YELLOW
;
737 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
740 s
->t_attrib
.bgcol
= QEMU_COLOR_MAGENTA
;
743 s
->t_attrib
.bgcol
= QEMU_COLOR_CYAN
;
746 s
->t_attrib
.bgcol
= QEMU_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 static void console_put_one(QemuConsole
*s
, int ch
)
765 if (s
->x
>= s
->width
) {
770 y1
= (s
->y_base
+ s
->y
) % s
->total_height
;
771 c
= &s
->cells
[y1
* s
->width
+ s
->x
];
773 c
->t_attrib
= s
->t_attrib
;
774 update_xy(s
, s
->x
, s
->y
);
778 static void console_respond_str(QemuConsole
*s
, const char *buf
)
781 console_put_one(s
, *buf
);
786 /* set cursor, checking bounds */
787 static void set_cursor(QemuConsole
*s
, int x
, int y
)
795 if (y
>= s
->height
) {
806 static void console_putchar(QemuConsole
*s
, int ch
)
815 case '\r': /* carriage return */
818 case '\n': /* newline */
821 case '\b': /* backspace */
825 case '\t': /* tabspace */
826 if (s
->x
+ (8 - (s
->x
% 8)) > s
->width
) {
830 s
->x
= s
->x
+ (8 - (s
->x
% 8));
833 case '\a': /* alert aka. bell */
834 /* TODO: has to be implemented */
837 /* SI (shift in), character set 0 (ignored) */
840 /* SO (shift out), character set 1 (ignored) */
842 case 27: /* esc (introducing an escape sequence) */
843 s
->state
= TTY_STATE_ESC
;
846 console_put_one(s
, ch
);
850 case TTY_STATE_ESC
: /* check if it is a terminal escape sequence */
852 for(i
=0;i
<MAX_ESC_PARAMS
;i
++)
853 s
->esc_params
[i
] = 0;
854 s
->nb_esc_params
= 0;
855 s
->state
= TTY_STATE_CSI
;
857 s
->state
= TTY_STATE_NORM
;
860 case TTY_STATE_CSI
: /* handle escape sequence parameters */
861 if (ch
>= '0' && ch
<= '9') {
862 if (s
->nb_esc_params
< MAX_ESC_PARAMS
) {
863 int *param
= &s
->esc_params
[s
->nb_esc_params
];
864 int digit
= (ch
- '0');
866 *param
= (*param
<= (INT_MAX
- digit
) / 10) ?
867 *param
* 10 + digit
: INT_MAX
;
870 if (s
->nb_esc_params
< MAX_ESC_PARAMS
)
874 trace_console_putchar_csi(s
->esc_params
[0], s
->esc_params
[1],
875 ch
, s
->nb_esc_params
);
876 s
->state
= TTY_STATE_NORM
;
880 if (s
->esc_params
[0] == 0) {
881 s
->esc_params
[0] = 1;
883 set_cursor(s
, s
->x
, s
->y
- s
->esc_params
[0]);
886 /* move cursor down */
887 if (s
->esc_params
[0] == 0) {
888 s
->esc_params
[0] = 1;
890 set_cursor(s
, s
->x
, s
->y
+ s
->esc_params
[0]);
893 /* move cursor right */
894 if (s
->esc_params
[0] == 0) {
895 s
->esc_params
[0] = 1;
897 set_cursor(s
, s
->x
+ s
->esc_params
[0], s
->y
);
900 /* move cursor left */
901 if (s
->esc_params
[0] == 0) {
902 s
->esc_params
[0] = 1;
904 set_cursor(s
, s
->x
- s
->esc_params
[0], s
->y
);
907 /* move cursor to column */
908 set_cursor(s
, s
->esc_params
[0] - 1, s
->y
);
912 /* move cursor to row, column */
913 set_cursor(s
, s
->esc_params
[1] - 1, s
->esc_params
[0] - 1);
916 switch (s
->esc_params
[0]) {
918 /* clear to end of screen */
919 for (y
= s
->y
; y
< s
->height
; y
++) {
920 for (x
= 0; x
< s
->width
; x
++) {
921 if (y
== s
->y
&& x
< s
->x
) {
924 console_clear_xy(s
, x
, y
);
929 /* clear from beginning of screen */
930 for (y
= 0; y
<= s
->y
; y
++) {
931 for (x
= 0; x
< s
->width
; x
++) {
932 if (y
== s
->y
&& x
> s
->x
) {
935 console_clear_xy(s
, x
, y
);
940 /* clear entire screen */
941 for (y
= 0; y
<= s
->height
; y
++) {
942 for (x
= 0; x
< s
->width
; x
++) {
943 console_clear_xy(s
, x
, y
);
950 switch (s
->esc_params
[0]) {
953 for (x
= s
->x
; x
< s
->width
; x
++) {
954 console_clear_xy(s
, x
, s
->y
);
958 /* clear from beginning of line */
959 for (x
= 0; x
<= s
->x
; x
++) {
960 console_clear_xy(s
, x
, s
->y
);
964 /* clear entire line */
965 for(x
= 0; x
< s
->width
; x
++) {
966 console_clear_xy(s
, x
, s
->y
);
972 console_handle_escape(s
);
975 switch (s
->esc_params
[0]) {
977 /* report console status (always succeed)*/
978 console_respond_str(s
, "\033[0n");
981 /* report cursor position */
982 sprintf(response
, "\033[%d;%dR",
983 (s
->y_base
+ s
->y
) % s
->total_height
+ 1,
985 console_respond_str(s
, response
);
990 /* save cursor position */
995 /* restore cursor position */
1000 trace_console_putchar_unhandled(ch
);
1008 void console_select(unsigned int index
)
1010 DisplayChangeListener
*dcl
;
1013 trace_console_select(index
);
1014 s
= qemu_console_lookup_by_index(index
);
1016 DisplayState
*ds
= s
->ds
;
1020 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1021 if (dcl
->con
!= NULL
) {
1024 if (dcl
->ops
->dpy_gfx_switch
) {
1025 dcl
->ops
->dpy_gfx_switch(dcl
, s
->surface
);
1028 dpy_gfx_update(s
, 0, 0, surface_width(s
->surface
),
1029 surface_height(s
->surface
));
1031 if (ds
->have_text
) {
1032 dpy_text_resize(s
, s
->width
, s
->height
);
1034 text_console_update_cursor(NULL
);
1038 static int console_puts(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1040 QemuConsole
*s
= chr
->opaque
;
1043 s
->update_x0
= s
->width
* FONT_WIDTH
;
1044 s
->update_y0
= s
->height
* FONT_HEIGHT
;
1047 console_show_cursor(s
, 0);
1048 for(i
= 0; i
< len
; i
++) {
1049 console_putchar(s
, buf
[i
]);
1051 console_show_cursor(s
, 1);
1052 if (s
->ds
->have_gfx
&& s
->update_x0
< s
->update_x1
) {
1053 dpy_gfx_update(s
, s
->update_x0
, s
->update_y0
,
1054 s
->update_x1
- s
->update_x0
,
1055 s
->update_y1
- s
->update_y0
);
1060 static void kbd_send_chars(void *opaque
)
1062 QemuConsole
*s
= opaque
;
1066 len
= qemu_chr_be_can_write(s
->chr
);
1067 if (len
> s
->out_fifo
.count
)
1068 len
= s
->out_fifo
.count
;
1070 if (len
> sizeof(buf
))
1072 qemu_fifo_read(&s
->out_fifo
, buf
, len
);
1073 qemu_chr_be_write(s
->chr
, buf
, len
);
1075 /* characters are pending: we send them a bit later (XXX:
1076 horrible, should change char device API) */
1077 if (s
->out_fifo
.count
> 0) {
1078 timer_mod(s
->kbd_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1);
1082 /* called when an ascii key is pressed */
1083 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
)
1085 uint8_t buf
[16], *q
;
1088 if (!s
|| (s
->console_type
== GRAPHIC_CONSOLE
))
1092 case QEMU_KEY_CTRL_UP
:
1093 console_scroll(s
, -1);
1095 case QEMU_KEY_CTRL_DOWN
:
1096 console_scroll(s
, 1);
1098 case QEMU_KEY_CTRL_PAGEUP
:
1099 console_scroll(s
, -10);
1101 case QEMU_KEY_CTRL_PAGEDOWN
:
1102 console_scroll(s
, 10);
1105 /* convert the QEMU keysym to VT100 key string */
1107 if (keysym
>= 0xe100 && keysym
<= 0xe11f) {
1110 c
= keysym
- 0xe100;
1112 *q
++ = '0' + (c
/ 10);
1113 *q
++ = '0' + (c
% 10);
1115 } else if (keysym
>= 0xe120 && keysym
<= 0xe17f) {
1118 *q
++ = keysym
& 0xff;
1119 } else if (s
->echo
&& (keysym
== '\r' || keysym
== '\n')) {
1120 console_puts(s
->chr
, (const uint8_t *) "\r", 1);
1126 console_puts(s
->chr
, buf
, q
- buf
);
1128 if (s
->chr
->chr_read
) {
1129 qemu_fifo_write(&s
->out_fifo
, buf
, q
- buf
);
1136 static const int qcode_to_keysym
[Q_KEY_CODE__MAX
] = {
1137 [Q_KEY_CODE_UP
] = QEMU_KEY_UP
,
1138 [Q_KEY_CODE_DOWN
] = QEMU_KEY_DOWN
,
1139 [Q_KEY_CODE_RIGHT
] = QEMU_KEY_RIGHT
,
1140 [Q_KEY_CODE_LEFT
] = QEMU_KEY_LEFT
,
1141 [Q_KEY_CODE_HOME
] = QEMU_KEY_HOME
,
1142 [Q_KEY_CODE_END
] = QEMU_KEY_END
,
1143 [Q_KEY_CODE_PGUP
] = QEMU_KEY_PAGEUP
,
1144 [Q_KEY_CODE_PGDN
] = QEMU_KEY_PAGEDOWN
,
1145 [Q_KEY_CODE_DELETE
] = QEMU_KEY_DELETE
,
1148 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
)
1152 keysym
= qcode_to_keysym
[qcode
];
1156 kbd_put_keysym_console(s
, keysym
);
1160 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
)
1164 for (i
= 0; i
< len
&& str
[i
]; i
++) {
1165 kbd_put_keysym_console(s
, str
[i
]);
1169 void kbd_put_keysym(int keysym
)
1171 kbd_put_keysym_console(active_console
, keysym
);
1174 static void text_console_invalidate(void *opaque
)
1176 QemuConsole
*s
= (QemuConsole
*) opaque
;
1178 if (s
->ds
->have_text
&& s
->console_type
== TEXT_CONSOLE
) {
1179 text_console_resize(s
);
1184 static void text_console_update(void *opaque
, console_ch_t
*chardata
)
1186 QemuConsole
*s
= (QemuConsole
*) opaque
;
1189 if (s
->text_x
[0] <= s
->text_x
[1]) {
1190 src
= (s
->y_base
+ s
->text_y
[0]) * s
->width
;
1191 chardata
+= s
->text_y
[0] * s
->width
;
1192 for (i
= s
->text_y
[0]; i
<= s
->text_y
[1]; i
++)
1193 for (j
= 0; j
< s
->width
; j
++, src
++) {
1194 console_write_ch(chardata
++,
1195 ATTR2CHTYPE(s
->cells
[src
].ch
,
1196 s
->cells
[src
].t_attrib
.fgcol
,
1197 s
->cells
[src
].t_attrib
.bgcol
,
1198 s
->cells
[src
].t_attrib
.bold
));
1200 dpy_text_update(s
, s
->text_x
[0], s
->text_y
[0],
1201 s
->text_x
[1] - s
->text_x
[0], i
- s
->text_y
[0]);
1202 s
->text_x
[0] = s
->width
;
1203 s
->text_y
[0] = s
->height
;
1207 if (s
->cursor_invalidate
) {
1208 dpy_text_cursor(s
, s
->x
, s
->y
);
1209 s
->cursor_invalidate
= 0;
1213 static QemuConsole
*new_console(DisplayState
*ds
, console_type_t console_type
,
1220 obj
= object_new(TYPE_QEMU_CONSOLE
);
1221 s
= QEMU_CONSOLE(obj
);
1223 object_property_add_link(obj
, "device", TYPE_DEVICE
,
1224 (Object
**)&s
->device
,
1225 object_property_allow_set_link
,
1226 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1228 object_property_add_uint32_ptr(obj
, "head",
1229 &s
->head
, &error_abort
);
1231 if (!active_console
|| ((active_console
->console_type
!= GRAPHIC_CONSOLE
) &&
1232 (console_type
== GRAPHIC_CONSOLE
))) {
1236 s
->console_type
= console_type
;
1238 consoles
= g_realloc(consoles
, sizeof(*consoles
) * (nb_consoles
+1));
1239 if (console_type
!= GRAPHIC_CONSOLE
) {
1240 s
->index
= nb_consoles
;
1241 consoles
[nb_consoles
++] = s
;
1243 /* HACK: Put graphical consoles before text consoles. */
1244 for (i
= nb_consoles
; i
> 0; i
--) {
1245 if (consoles
[i
- 1]->console_type
== GRAPHIC_CONSOLE
)
1247 consoles
[i
] = consoles
[i
- 1];
1248 consoles
[i
]->index
= i
;
1257 static void qemu_alloc_display(DisplaySurface
*surface
, int width
, int height
)
1259 qemu_pixman_image_unref(surface
->image
);
1260 surface
->image
= NULL
;
1262 surface
->format
= PIXMAN_x8r8g8b8
;
1263 surface
->image
= pixman_image_create_bits(surface
->format
,
1266 assert(surface
->image
!= NULL
);
1268 surface
->flags
= QEMU_ALLOCATED_FLAG
;
1271 DisplaySurface
*qemu_create_displaysurface(int width
, int height
)
1273 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1275 trace_displaysurface_create(surface
, width
, height
);
1276 qemu_alloc_display(surface
, width
, height
);
1280 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
1281 pixman_format_code_t format
,
1282 int linesize
, uint8_t *data
)
1284 DisplaySurface
*surface
= g_new0(DisplaySurface
, 1);
1286 trace_displaysurface_create_from(surface
, width
, height
, format
);
1287 surface
->format
= format
;
1288 surface
->image
= pixman_image_create_bits(surface
->format
,
1290 (void *)data
, linesize
);
1291 assert(surface
->image
!= NULL
);
1296 static void qemu_unmap_displaysurface_guestmem(pixman_image_t
*image
,
1299 void *data
= pixman_image_get_data(image
);
1300 uint32_t size
= pixman_image_get_stride(image
) *
1301 pixman_image_get_height(image
);
1302 cpu_physical_memory_unmap(data
, size
, 0, 0);
1305 DisplaySurface
*qemu_create_displaysurface_guestmem(int width
, int height
,
1306 pixman_format_code_t format
,
1307 int linesize
, uint64_t addr
)
1309 DisplaySurface
*surface
;
1313 if (linesize
== 0) {
1314 linesize
= width
* PIXMAN_FORMAT_BPP(format
) / 8;
1317 size
= (hwaddr
)linesize
* height
;
1318 data
= cpu_physical_memory_map(addr
, &size
, 0);
1319 if (size
!= (hwaddr
)linesize
* height
) {
1320 cpu_physical_memory_unmap(data
, size
, 0, 0);
1324 surface
= qemu_create_displaysurface_from
1325 (width
, height
, format
, linesize
, data
);
1326 pixman_image_set_destroy_function
1327 (surface
->image
, qemu_unmap_displaysurface_guestmem
, NULL
);
1332 static DisplaySurface
*qemu_create_message_surface(int w
, int h
,
1335 DisplaySurface
*surface
= qemu_create_displaysurface(w
, h
);
1336 pixman_color_t bg
= color_table_rgb
[0][QEMU_COLOR_BLACK
];
1337 pixman_color_t fg
= color_table_rgb
[0][QEMU_COLOR_WHITE
];
1338 pixman_image_t
*glyph
;
1342 x
= (w
/ FONT_WIDTH
- len
) / 2;
1343 y
= (h
/ FONT_HEIGHT
- 1) / 2;
1344 for (i
= 0; i
< len
; i
++) {
1345 glyph
= qemu_pixman_glyph_from_vgafont(FONT_HEIGHT
, vgafont16
, msg
[i
]);
1346 qemu_pixman_glyph_render(glyph
, surface
->image
, &fg
, &bg
,
1347 x
+i
, y
, FONT_WIDTH
, FONT_HEIGHT
);
1348 qemu_pixman_image_unref(glyph
);
1353 void qemu_free_displaysurface(DisplaySurface
*surface
)
1355 if (surface
== NULL
) {
1358 trace_displaysurface_free(surface
);
1359 qemu_pixman_image_unref(surface
->image
);
1363 bool console_has_gl(QemuConsole
*con
)
1365 return con
->gl
!= NULL
;
1368 void register_displaychangelistener(DisplayChangeListener
*dcl
)
1370 static const char nodev
[] =
1371 "This VM has no graphic display device.";
1372 static DisplaySurface
*dummy
;
1375 if (dcl
->ops
->dpy_gl_ctx_create
) {
1376 /* display has opengl support */
1379 fprintf(stderr
, "can't register two opengl displays (%s, %s)\n",
1380 dcl
->ops
->dpy_name
, dcl
->con
->gl
->ops
->dpy_name
);
1386 trace_displaychangelistener_register(dcl
, dcl
->ops
->dpy_name
);
1387 dcl
->ds
= get_alloc_displaystate();
1388 QLIST_INSERT_HEAD(&dcl
->ds
->listeners
, dcl
, next
);
1389 gui_setup_refresh(dcl
->ds
);
1394 con
= active_console
;
1396 if (dcl
->ops
->dpy_gfx_switch
) {
1398 dcl
->ops
->dpy_gfx_switch(dcl
, con
->surface
);
1401 dummy
= qemu_create_message_surface(640, 480, nodev
);
1403 dcl
->ops
->dpy_gfx_switch(dcl
, dummy
);
1406 text_console_update_cursor(NULL
);
1409 void update_displaychangelistener(DisplayChangeListener
*dcl
,
1412 DisplayState
*ds
= dcl
->ds
;
1414 dcl
->update_interval
= interval
;
1415 if (!ds
->refreshing
&& ds
->update_interval
> interval
) {
1416 timer_mod(ds
->gui_timer
, ds
->last_update
+ interval
);
1420 void unregister_displaychangelistener(DisplayChangeListener
*dcl
)
1422 DisplayState
*ds
= dcl
->ds
;
1423 trace_displaychangelistener_unregister(dcl
, dcl
->ops
->dpy_name
);
1427 QLIST_REMOVE(dcl
, next
);
1428 gui_setup_refresh(ds
);
1431 static void dpy_set_ui_info_timer(void *opaque
)
1433 QemuConsole
*con
= opaque
;
1435 con
->hw_ops
->ui_info(con
->hw
, con
->head
, &con
->ui_info
);
1438 bool dpy_ui_info_supported(QemuConsole
*con
)
1440 return con
->hw_ops
->ui_info
!= NULL
;
1443 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
)
1445 assert(con
!= NULL
);
1446 con
->ui_info
= *info
;
1447 if (!dpy_ui_info_supported(con
)) {
1452 * Typically we get a flood of these as the user resizes the window.
1453 * Wait until the dust has settled (one second without updates), then
1454 * go notify the guest.
1456 timer_mod(con
->ui_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
1460 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1462 DisplayState
*s
= con
->ds
;
1463 DisplayChangeListener
*dcl
;
1468 width
= surface_width(con
->surface
);
1469 height
= surface_height(con
->surface
);
1475 w
= MIN(w
, width
- x
);
1476 h
= MIN(h
, height
- y
);
1478 if (!qemu_console_is_visible(con
)) {
1481 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1482 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1485 if (dcl
->ops
->dpy_gfx_update
) {
1486 dcl
->ops
->dpy_gfx_update(dcl
, x
, y
, w
, h
);
1491 void dpy_gfx_replace_surface(QemuConsole
*con
,
1492 DisplaySurface
*surface
)
1494 DisplayState
*s
= con
->ds
;
1495 DisplaySurface
*old_surface
= con
->surface
;
1496 DisplayChangeListener
*dcl
;
1498 con
->surface
= surface
;
1499 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1500 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1503 if (dcl
->ops
->dpy_gfx_switch
) {
1504 dcl
->ops
->dpy_gfx_switch(dcl
, surface
);
1507 qemu_free_displaysurface(old_surface
);
1510 bool dpy_gfx_check_format(QemuConsole
*con
,
1511 pixman_format_code_t format
)
1513 DisplayChangeListener
*dcl
;
1514 DisplayState
*s
= con
->ds
;
1516 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1517 if (dcl
->con
&& dcl
->con
!= con
) {
1518 /* dcl bound to another console -> skip */
1521 if (dcl
->ops
->dpy_gfx_check_format
) {
1522 if (!dcl
->ops
->dpy_gfx_check_format(dcl
, format
)) {
1526 /* default is to whitelist native 32 bpp only */
1527 if (format
!= qemu_default_pixman_format(32, true)) {
1535 static void dpy_refresh(DisplayState
*s
)
1537 DisplayChangeListener
*dcl
;
1539 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1540 if (dcl
->ops
->dpy_refresh
) {
1541 dcl
->ops
->dpy_refresh(dcl
);
1546 void dpy_gfx_copy(QemuConsole
*con
, int src_x
, int src_y
,
1547 int dst_x
, int dst_y
, int w
, int h
)
1549 DisplayState
*s
= con
->ds
;
1550 DisplayChangeListener
*dcl
;
1552 if (!qemu_console_is_visible(con
)) {
1555 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1556 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1559 if (dcl
->ops
->dpy_gfx_copy
) {
1560 dcl
->ops
->dpy_gfx_copy(dcl
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
1562 dcl
->ops
->dpy_gfx_update(dcl
, dst_x
, dst_y
, w
, h
);
1567 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
)
1569 DisplayState
*s
= con
->ds
;
1570 DisplayChangeListener
*dcl
;
1572 if (!qemu_console_is_visible(con
)) {
1575 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1576 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1579 if (dcl
->ops
->dpy_text_cursor
) {
1580 dcl
->ops
->dpy_text_cursor(dcl
, x
, y
);
1585 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
)
1587 DisplayState
*s
= con
->ds
;
1588 DisplayChangeListener
*dcl
;
1590 if (!qemu_console_is_visible(con
)) {
1593 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1594 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1597 if (dcl
->ops
->dpy_text_update
) {
1598 dcl
->ops
->dpy_text_update(dcl
, x
, y
, w
, h
);
1603 void dpy_text_resize(QemuConsole
*con
, int w
, int h
)
1605 DisplayState
*s
= con
->ds
;
1606 DisplayChangeListener
*dcl
;
1608 if (!qemu_console_is_visible(con
)) {
1611 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1612 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1615 if (dcl
->ops
->dpy_text_resize
) {
1616 dcl
->ops
->dpy_text_resize(dcl
, w
, h
);
1621 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
)
1623 DisplayState
*s
= con
->ds
;
1624 DisplayChangeListener
*dcl
;
1626 if (!qemu_console_is_visible(con
)) {
1629 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1630 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1633 if (dcl
->ops
->dpy_mouse_set
) {
1634 dcl
->ops
->dpy_mouse_set(dcl
, x
, y
, on
);
1639 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
)
1641 DisplayState
*s
= con
->ds
;
1642 DisplayChangeListener
*dcl
;
1644 if (!qemu_console_is_visible(con
)) {
1647 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1648 if (con
!= (dcl
->con
? dcl
->con
: active_console
)) {
1651 if (dcl
->ops
->dpy_cursor_define
) {
1652 dcl
->ops
->dpy_cursor_define(dcl
, cursor
);
1657 bool dpy_cursor_define_supported(QemuConsole
*con
)
1659 DisplayState
*s
= con
->ds
;
1660 DisplayChangeListener
*dcl
;
1662 QLIST_FOREACH(dcl
, &s
->listeners
, next
) {
1663 if (dcl
->ops
->dpy_cursor_define
) {
1670 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
1671 struct QEMUGLParams
*qparams
)
1674 return con
->gl
->ops
->dpy_gl_ctx_create(con
->gl
, qparams
);
1677 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
)
1680 con
->gl
->ops
->dpy_gl_ctx_destroy(con
->gl
, ctx
);
1683 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
)
1686 return con
->gl
->ops
->dpy_gl_ctx_make_current(con
->gl
, ctx
);
1689 QEMUGLContext
dpy_gl_ctx_get_current(QemuConsole
*con
)
1692 return con
->gl
->ops
->dpy_gl_ctx_get_current(con
->gl
);
1695 void dpy_gl_scanout(QemuConsole
*con
,
1696 uint32_t backing_id
, bool backing_y_0_top
,
1697 uint32_t x
, uint32_t y
, uint32_t width
, uint32_t height
)
1700 con
->gl
->ops
->dpy_gl_scanout(con
->gl
, backing_id
,
1702 x
, y
, width
, height
);
1705 void dpy_gl_update(QemuConsole
*con
,
1706 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1709 con
->gl
->ops
->dpy_gl_update(con
->gl
, x
, y
, w
, h
);
1712 /***********************************************************/
1713 /* register display */
1715 /* console.c internal use only */
1716 static DisplayState
*get_alloc_displaystate(void)
1718 if (!display_state
) {
1719 display_state
= g_new0(DisplayState
, 1);
1720 cursor_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1721 text_console_update_cursor
, NULL
);
1723 return display_state
;
1727 * Called by main(), after creating QemuConsoles
1728 * and before initializing ui (sdl/vnc/...).
1730 DisplayState
*init_displaystate(void)
1735 get_alloc_displaystate();
1736 for (i
= 0; i
< nb_consoles
; i
++) {
1737 if (consoles
[i
]->console_type
!= GRAPHIC_CONSOLE
&&
1738 consoles
[i
]->ds
== NULL
) {
1739 text_console_do_init(consoles
[i
]->chr
, display_state
);
1742 /* Hook up into the qom tree here (not in new_console()), once
1743 * all QemuConsoles are created and the order / numbering
1744 * doesn't change any more */
1745 name
= g_strdup_printf("console[%d]", i
);
1746 object_property_add_child(container_get(object_get_root(), "/backend"),
1747 name
, OBJECT(consoles
[i
]), &error_abort
);
1751 return display_state
;
1754 void graphic_console_set_hwops(QemuConsole
*con
,
1755 const GraphicHwOps
*hw_ops
,
1758 con
->hw_ops
= hw_ops
;
1762 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
1763 const GraphicHwOps
*hw_ops
,
1766 static const char noinit
[] =
1767 "Guest has not initialized the display (yet).";
1773 ds
= get_alloc_displaystate();
1774 trace_console_gfx_new();
1775 s
= new_console(ds
, GRAPHIC_CONSOLE
, head
);
1776 s
->ui_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, dpy_set_ui_info_timer
, s
);
1777 graphic_console_set_hwops(s
, hw_ops
, opaque
);
1779 object_property_set_link(OBJECT(s
), OBJECT(dev
), "device",
1783 s
->surface
= qemu_create_message_surface(width
, height
, noinit
);
1787 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
)
1789 if (index
>= nb_consoles
) {
1792 return consoles
[index
];
1795 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
)
1801 for (i
= 0; i
< nb_consoles
; i
++) {
1805 obj
= object_property_get_link(OBJECT(consoles
[i
]),
1806 "device", &error_abort
);
1807 if (DEVICE(obj
) != dev
) {
1810 h
= object_property_get_int(OBJECT(consoles
[i
]),
1811 "head", &error_abort
);
1820 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
1821 uint32_t head
, Error
**errp
)
1826 dev
= qdev_find_recursive(sysbus_get_default(), device_id
);
1828 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1829 "Device '%s' not found", device_id
);
1833 con
= qemu_console_lookup_by_device(dev
, head
);
1835 error_setg(errp
, "Device %s (head %d) is not bound to a QemuConsole",
1843 bool qemu_console_is_visible(QemuConsole
*con
)
1845 return (con
== active_console
) || (con
->dcls
> 0);
1848 bool qemu_console_is_graphic(QemuConsole
*con
)
1851 con
= active_console
;
1853 return con
&& (con
->console_type
== GRAPHIC_CONSOLE
);
1856 bool qemu_console_is_fixedsize(QemuConsole
*con
)
1859 con
= active_console
;
1861 return con
&& (con
->console_type
!= TEXT_CONSOLE
);
1864 char *qemu_console_get_label(QemuConsole
*con
)
1866 if (con
->console_type
== GRAPHIC_CONSOLE
) {
1868 return g_strdup(object_get_typename(con
->device
));
1870 return g_strdup("VGA");
1872 if (con
->chr
&& con
->chr
->label
) {
1873 return g_strdup(con
->chr
->label
);
1875 return g_strdup_printf("vc%d", con
->index
);
1879 int qemu_console_get_index(QemuConsole
*con
)
1882 con
= active_console
;
1884 return con
? con
->index
: -1;
1887 uint32_t qemu_console_get_head(QemuConsole
*con
)
1890 con
= active_console
;
1892 return con
? con
->head
: -1;
1895 QemuUIInfo
*qemu_console_get_ui_info(QemuConsole
*con
)
1897 assert(con
!= NULL
);
1898 return &con
->ui_info
;
1901 int qemu_console_get_width(QemuConsole
*con
, int fallback
)
1904 con
= active_console
;
1906 return con
? surface_width(con
->surface
) : fallback
;
1909 int qemu_console_get_height(QemuConsole
*con
, int fallback
)
1912 con
= active_console
;
1914 return con
? surface_height(con
->surface
) : fallback
;
1917 static void text_console_set_echo(CharDriverState
*chr
, bool echo
)
1919 QemuConsole
*s
= chr
->opaque
;
1924 static void text_console_update_cursor_timer(void)
1926 timer_mod(cursor_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
1927 + CONSOLE_CURSOR_PERIOD
/ 2);
1930 static void text_console_update_cursor(void *opaque
)
1935 cursor_visible_phase
= !cursor_visible_phase
;
1937 for (i
= 0; i
< nb_consoles
; i
++) {
1939 if (qemu_console_is_graphic(s
) ||
1940 !qemu_console_is_visible(s
)) {
1944 graphic_hw_invalidate(s
);
1948 text_console_update_cursor_timer();
1952 static const GraphicHwOps text_console_ops
= {
1953 .invalidate
= text_console_invalidate
,
1954 .text_update
= text_console_update
,
1957 static void text_console_do_init(CharDriverState
*chr
, DisplayState
*ds
)
1960 int g_width
= 80 * FONT_WIDTH
;
1961 int g_height
= 24 * FONT_HEIGHT
;
1965 chr
->chr_write
= console_puts
;
1967 s
->out_fifo
.buf
= s
->out_fifo_buf
;
1968 s
->out_fifo
.buf_size
= sizeof(s
->out_fifo_buf
);
1969 s
->kbd_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, kbd_send_chars
, s
);
1974 s
->total_height
= DEFAULT_BACKSCROLL
;
1978 if (active_console
&& active_console
->surface
) {
1979 g_width
= surface_width(active_console
->surface
);
1980 g_height
= surface_height(active_console
->surface
);
1982 s
->surface
= qemu_create_displaysurface(g_width
, g_height
);
1985 s
->hw_ops
= &text_console_ops
;
1988 /* Set text attribute defaults */
1989 s
->t_attrib_default
.bold
= 0;
1990 s
->t_attrib_default
.uline
= 0;
1991 s
->t_attrib_default
.blink
= 0;
1992 s
->t_attrib_default
.invers
= 0;
1993 s
->t_attrib_default
.unvisible
= 0;
1994 s
->t_attrib_default
.fgcol
= QEMU_COLOR_WHITE
;
1995 s
->t_attrib_default
.bgcol
= QEMU_COLOR_BLACK
;
1996 /* set current text attributes to default */
1997 s
->t_attrib
= s
->t_attrib_default
;
1998 text_console_resize(s
);
2004 s
->t_attrib
.bgcol
= QEMU_COLOR_BLUE
;
2005 len
= snprintf(msg
, sizeof(msg
), "%s console\r\n", chr
->label
);
2006 console_puts(chr
, (uint8_t*)msg
, len
);
2007 s
->t_attrib
= s
->t_attrib_default
;
2010 qemu_chr_be_generic_open(chr
);
2015 static CharDriverState
*text_console_init(ChardevVC
*vc
, Error
**errp
)
2017 ChardevCommon
*common
= qapi_ChardevVC_base(vc
);
2018 CharDriverState
*chr
;
2021 unsigned height
= 0;
2023 chr
= qemu_chr_alloc(common
, errp
);
2028 if (vc
->has_width
) {
2030 } else if (vc
->has_cols
) {
2031 width
= vc
->cols
* FONT_WIDTH
;
2034 if (vc
->has_height
) {
2035 height
= vc
->height
;
2036 } else if (vc
->has_rows
) {
2037 height
= vc
->rows
* FONT_HEIGHT
;
2040 trace_console_txt_new(width
, height
);
2041 if (width
== 0 || height
== 0) {
2042 s
= new_console(NULL
, TEXT_CONSOLE
, 0);
2044 s
= new_console(NULL
, TEXT_CONSOLE_FIXED_SIZE
, 0);
2045 s
->surface
= qemu_create_displaysurface(width
, height
);
2050 error_setg(errp
, "cannot create text console");
2056 chr
->chr_set_echo
= text_console_set_echo
;
2057 /* console/chardev init sometimes completes elsewhere in a 2nd
2058 * stage, so defer OPENED events until they are fully initialized
2060 chr
->explicit_be_open
= true;
2062 if (display_state
) {
2063 text_console_do_init(chr
, display_state
);
2068 static VcHandler
*vc_handler
= text_console_init
;
2070 static CharDriverState
*vc_init(const char *id
, ChardevBackend
*backend
,
2071 ChardevReturn
*ret
, Error
**errp
)
2073 return vc_handler(backend
->u
.vc
.data
, errp
);
2076 void register_vc_handler(VcHandler
*handler
)
2078 vc_handler
= handler
;
2081 void qemu_console_resize(QemuConsole
*s
, int width
, int height
)
2083 DisplaySurface
*surface
;
2085 assert(s
->console_type
== GRAPHIC_CONSOLE
);
2086 surface
= qemu_create_displaysurface(width
, height
);
2087 dpy_gfx_replace_surface(s
, surface
);
2090 void qemu_console_copy(QemuConsole
*con
, int src_x
, int src_y
,
2091 int dst_x
, int dst_y
, int w
, int h
)
2093 assert(con
->console_type
== GRAPHIC_CONSOLE
);
2094 dpy_gfx_copy(con
, src_x
, src_y
, dst_x
, dst_y
, w
, h
);
2097 DisplaySurface
*qemu_console_surface(QemuConsole
*console
)
2099 return console
->surface
;
2102 PixelFormat
qemu_default_pixelformat(int bpp
)
2104 pixman_format_code_t fmt
= qemu_default_pixman_format(bpp
, true);
2105 PixelFormat pf
= qemu_pixelformat_from_pixman(fmt
);
2109 static void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
,
2115 vc
= backend
->u
.vc
.data
= g_new0(ChardevVC
, 1);
2116 qemu_chr_parse_common(opts
, qapi_ChardevVC_base(vc
));
2118 val
= qemu_opt_get_number(opts
, "width", 0);
2120 vc
->has_width
= true;
2124 val
= qemu_opt_get_number(opts
, "height", 0);
2126 vc
->has_height
= true;
2130 val
= qemu_opt_get_number(opts
, "cols", 0);
2132 vc
->has_cols
= true;
2136 val
= qemu_opt_get_number(opts
, "rows", 0);
2138 vc
->has_rows
= true;
2143 static const TypeInfo qemu_console_info
= {
2144 .name
= TYPE_QEMU_CONSOLE
,
2145 .parent
= TYPE_OBJECT
,
2146 .instance_size
= sizeof(QemuConsole
),
2147 .class_size
= sizeof(QemuConsoleClass
),
2151 static void register_types(void)
2153 type_register_static(&qemu_console_info
);
2154 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC
, qemu_chr_parse_vc
,
2158 type_init(register_types
);