4 #include "ui/qemu-pixman.h"
5 #include "qom/object.h"
6 #include "qemu/notify.h"
7 #include "qapi/qapi-types-ui.h"
11 # include <epoxy/gl.h>
12 # include "ui/shader.h"
15 #define TYPE_QEMU_CONSOLE "qemu-console"
16 OBJECT_DECLARE_TYPE(QemuConsole
, QemuConsoleClass
, QEMU_CONSOLE
)
18 #define TYPE_QEMU_GRAPHIC_CONSOLE "qemu-graphic-console"
19 OBJECT_DECLARE_SIMPLE_TYPE(QemuGraphicConsole
, QEMU_GRAPHIC_CONSOLE
)
21 #define TYPE_QEMU_TEXT_CONSOLE "qemu-text-console"
22 OBJECT_DECLARE_SIMPLE_TYPE(QemuTextConsole
, QEMU_TEXT_CONSOLE
)
24 #define TYPE_QEMU_FIXED_TEXT_CONSOLE "qemu-fixed-text-console"
25 OBJECT_DECLARE_SIMPLE_TYPE(QemuFixedTextConsole
, QEMU_FIXED_TEXT_CONSOLE
)
27 #define QEMU_IS_GRAPHIC_CONSOLE(c) \
28 object_dynamic_cast(OBJECT(c), TYPE_QEMU_GRAPHIC_CONSOLE)
30 #define QEMU_IS_TEXT_CONSOLE(c) \
31 object_dynamic_cast(OBJECT(c), TYPE_QEMU_TEXT_CONSOLE)
33 #define QEMU_IS_FIXED_TEXT_CONSOLE(c) \
34 object_dynamic_cast(OBJECT(c), TYPE_QEMU_FIXED_TEXT_CONSOLE)
36 /* keyboard/mouse support */
38 #define MOUSE_EVENT_LBUTTON 0x01
39 #define MOUSE_EVENT_RBUTTON 0x02
40 #define MOUSE_EVENT_MBUTTON 0x04
41 #define MOUSE_EVENT_WHEELUP 0x08
42 #define MOUSE_EVENT_WHEELDN 0x10
44 /* identical to the ps/2 keyboard bits */
45 #define QEMU_SCROLL_LOCK_LED (1 << 0)
46 #define QEMU_NUM_LOCK_LED (1 << 1)
47 #define QEMU_CAPS_LOCK_LED (1 << 2)
50 #define GUI_REFRESH_INTERVAL_DEFAULT 30
51 #define GUI_REFRESH_INTERVAL_IDLE 3000
53 /* Color number is match to standard vga palette */
54 enum qemu_color_names
{
60 QEMU_COLOR_MAGENTA
= 5,
61 QEMU_COLOR_YELLOW
= 6,
64 /* Convert to curses char attributes */
65 #define ATTR2CHTYPE(c, fg, bg, bold) \
66 ((bold) << 21 | (bg) << 11 | (fg) << 8 | (c))
68 typedef void QEMUPutKBDEvent(void *opaque
, int keycode
);
69 typedef void QEMUPutLEDEvent(void *opaque
, int ledstate
);
70 typedef void QEMUPutMouseEvent(void *opaque
, int dx
, int dy
, int dz
, int buttons_state
);
72 typedef struct QEMUPutMouseEntry QEMUPutMouseEntry
;
73 typedef struct QEMUPutKbdEntry QEMUPutKbdEntry
;
74 typedef struct QEMUPutLEDEntry QEMUPutLEDEntry
;
76 QEMUPutKbdEntry
*qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
,
78 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
79 void *opaque
, int absolute
,
81 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
);
82 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry
*entry
);
84 QEMUPutLEDEntry
*qemu_add_led_event_handler(QEMUPutLEDEvent
*func
, void *opaque
);
85 void qemu_remove_led_event_handler(QEMUPutLEDEntry
*entry
);
87 void kbd_put_ledstate(int ledstate
);
89 bool qemu_mouse_set(int index
, Error
**errp
);
91 /* keysym is a unicode code except for special keys (see QEMU_KEY_xxx
93 #define QEMU_KEY_ESC1(c) ((c) | 0xe100)
94 #define QEMU_KEY_TAB 0x0009
95 #define QEMU_KEY_BACKSPACE 0x007f
96 #define QEMU_KEY_UP QEMU_KEY_ESC1('A')
97 #define QEMU_KEY_DOWN QEMU_KEY_ESC1('B')
98 #define QEMU_KEY_RIGHT QEMU_KEY_ESC1('C')
99 #define QEMU_KEY_LEFT QEMU_KEY_ESC1('D')
100 #define QEMU_KEY_HOME QEMU_KEY_ESC1(1)
101 #define QEMU_KEY_END QEMU_KEY_ESC1(4)
102 #define QEMU_KEY_PAGEUP QEMU_KEY_ESC1(5)
103 #define QEMU_KEY_PAGEDOWN QEMU_KEY_ESC1(6)
104 #define QEMU_KEY_DELETE QEMU_KEY_ESC1(3)
106 #define QEMU_KEY_CTRL_UP 0xe400
107 #define QEMU_KEY_CTRL_DOWN 0xe401
108 #define QEMU_KEY_CTRL_LEFT 0xe402
109 #define QEMU_KEY_CTRL_RIGHT 0xe403
110 #define QEMU_KEY_CTRL_HOME 0xe404
111 #define QEMU_KEY_CTRL_END 0xe405
112 #define QEMU_KEY_CTRL_PAGEUP 0xe406
113 #define QEMU_KEY_CTRL_PAGEDOWN 0xe407
115 void kbd_put_keysym_console(QemuConsole
*s
, int keysym
);
116 bool kbd_put_qcode_console(QemuConsole
*s
, int qcode
, bool ctrl
);
117 void kbd_put_string_console(QemuConsole
*s
, const char *str
, int len
);
118 void kbd_put_keysym(int keysym
);
121 typedef struct touch_slot
{
127 void console_handle_touch_event(QemuConsole
*con
,
128 struct touch_slot touch_slots
[INPUT_EVENT_SLOTS_MAX
],
130 int width
, int height
,
132 InputMultiTouchType type
,
136 struct QemuConsoleClass
{
137 ObjectClass parent_class
;
140 #define QEMU_ALLOCATED_FLAG 0x01
141 #define QEMU_PLACEHOLDER_FLAG 0x02
143 typedef struct ScanoutTexture
{
145 bool backing_y_0_top
;
146 uint32_t backing_width
;
147 uint32_t backing_height
;
155 typedef struct DisplaySurface
{
156 pixman_format_code_t format
;
157 pixman_image_t
*image
;
166 uint32_t handle_offset
;
170 typedef struct QemuUIInfo
{
171 /* physical dimension */
179 uint32_t refresh_rate
;
182 /* cursor data format is 32bit RGBA */
183 typedef struct QEMUCursor
{
184 uint16_t width
, height
;
190 QEMUCursor
*cursor_alloc(uint16_t width
, uint16_t height
);
191 QEMUCursor
*cursor_ref(QEMUCursor
*c
);
192 void cursor_unref(QEMUCursor
*c
);
193 QEMUCursor
*cursor_builtin_hidden(void);
194 QEMUCursor
*cursor_builtin_left_ptr(void);
195 void cursor_print_ascii_art(QEMUCursor
*c
, const char *prefix
);
196 int cursor_get_mono_bpl(QEMUCursor
*c
);
197 void cursor_set_mono(QEMUCursor
*c
,
198 uint32_t foreground
, uint32_t background
, uint8_t *image
,
199 int transparent
, uint8_t *mask
);
200 void cursor_get_mono_image(QEMUCursor
*c
, int foreground
, uint8_t *mask
);
201 void cursor_get_mono_mask(QEMUCursor
*c
, int transparent
, uint8_t *mask
);
203 typedef void *QEMUGLContext
;
204 typedef struct QEMUGLParams QEMUGLParams
;
206 struct QEMUGLParams
{
211 typedef struct QemuDmaBuf
{
221 uint32_t backing_width
;
222 uint32_t backing_height
;
230 enum display_scanout
{
237 typedef struct DisplayScanout
{
238 enum display_scanout kind
;
240 /* DisplaySurface *surface; is kept in QemuConsole */
241 ScanoutTexture texture
;
246 typedef struct DisplayState DisplayState
;
247 typedef struct DisplayGLCtx DisplayGLCtx
;
249 typedef struct DisplayChangeListenerOps
{
250 const char *dpy_name
;
253 void (*dpy_refresh
)(DisplayChangeListener
*dcl
);
256 void (*dpy_gfx_update
)(DisplayChangeListener
*dcl
,
257 int x
, int y
, int w
, int h
);
259 void (*dpy_gfx_switch
)(DisplayChangeListener
*dcl
,
260 struct DisplaySurface
*new_surface
);
262 bool (*dpy_gfx_check_format
)(DisplayChangeListener
*dcl
,
263 pixman_format_code_t format
);
266 void (*dpy_text_cursor
)(DisplayChangeListener
*dcl
,
269 void (*dpy_text_resize
)(DisplayChangeListener
*dcl
,
272 void (*dpy_text_update
)(DisplayChangeListener
*dcl
,
273 int x
, int y
, int w
, int h
);
276 void (*dpy_mouse_set
)(DisplayChangeListener
*dcl
,
277 int x
, int y
, int on
);
279 void (*dpy_cursor_define
)(DisplayChangeListener
*dcl
,
283 void (*dpy_gl_scanout_disable
)(DisplayChangeListener
*dcl
);
285 void (*dpy_gl_scanout_texture
)(DisplayChangeListener
*dcl
,
287 bool backing_y_0_top
,
288 uint32_t backing_width
,
289 uint32_t backing_height
,
290 uint32_t x
, uint32_t y
,
291 uint32_t w
, uint32_t h
,
293 /* optional (default to true if has dpy_gl_scanout_dmabuf) */
294 bool (*dpy_has_dmabuf
)(DisplayChangeListener
*dcl
);
296 void (*dpy_gl_scanout_dmabuf
)(DisplayChangeListener
*dcl
,
299 void (*dpy_gl_cursor_dmabuf
)(DisplayChangeListener
*dcl
,
300 QemuDmaBuf
*dmabuf
, bool have_hot
,
301 uint32_t hot_x
, uint32_t hot_y
);
303 void (*dpy_gl_cursor_position
)(DisplayChangeListener
*dcl
,
304 uint32_t pos_x
, uint32_t pos_y
);
306 void (*dpy_gl_release_dmabuf
)(DisplayChangeListener
*dcl
,
309 void (*dpy_gl_update
)(DisplayChangeListener
*dcl
,
310 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
);
312 } DisplayChangeListenerOps
;
314 struct DisplayChangeListener
{
315 uint64_t update_interval
;
316 const DisplayChangeListenerOps
*ops
;
320 QLIST_ENTRY(DisplayChangeListener
) next
;
323 typedef struct DisplayGLCtxOps
{
324 bool (*dpy_gl_ctx_is_compatible_dcl
)(DisplayGLCtx
*dgc
,
325 DisplayChangeListener
*dcl
);
326 QEMUGLContext (*dpy_gl_ctx_create
)(DisplayGLCtx
*dgc
,
327 QEMUGLParams
*params
);
328 void (*dpy_gl_ctx_destroy
)(DisplayGLCtx
*dgc
,
330 int (*dpy_gl_ctx_make_current
)(DisplayGLCtx
*dgc
,
332 void (*dpy_gl_ctx_create_texture
)(DisplayGLCtx
*dgc
,
333 DisplaySurface
*surface
);
334 void (*dpy_gl_ctx_destroy_texture
)(DisplayGLCtx
*dgc
,
335 DisplaySurface
*surface
);
336 void (*dpy_gl_ctx_update_texture
)(DisplayGLCtx
*dgc
,
337 DisplaySurface
*surface
,
338 int x
, int y
, int w
, int h
);
341 struct DisplayGLCtx
{
342 const DisplayGLCtxOps
*ops
;
344 QemuGLShader
*gls
; /* optional shared shader */
348 DisplayState
*init_displaystate(void);
349 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
350 pixman_format_code_t format
,
351 int linesize
, uint8_t *data
);
352 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
);
353 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
356 void qemu_displaysurface_win32_set_handle(DisplaySurface
*surface
,
357 HANDLE h
, uint32_t offset
);
359 PixelFormat
qemu_default_pixelformat(int bpp
);
361 DisplaySurface
*qemu_create_displaysurface(int width
, int height
);
362 void qemu_free_displaysurface(DisplaySurface
*surface
);
364 static inline int is_buffer_shared(DisplaySurface
*surface
)
366 return !(surface
->flags
& QEMU_ALLOCATED_FLAG
);
369 static inline int is_placeholder(DisplaySurface
*surface
)
371 return surface
->flags
& QEMU_PLACEHOLDER_FLAG
;
374 void register_displaychangelistener(DisplayChangeListener
*dcl
);
375 void update_displaychangelistener(DisplayChangeListener
*dcl
,
377 void unregister_displaychangelistener(DisplayChangeListener
*dcl
);
379 bool dpy_ui_info_supported(QemuConsole
*con
);
380 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
);
381 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
, bool delay
);
383 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
);
384 void dpy_gfx_update_full(QemuConsole
*con
);
385 void dpy_gfx_replace_surface(QemuConsole
*con
,
386 DisplaySurface
*surface
);
387 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
);
388 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
);
389 void dpy_text_resize(QemuConsole
*con
, int w
, int h
);
390 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
);
391 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
);
392 bool dpy_cursor_define_supported(QemuConsole
*con
);
393 bool dpy_gfx_check_format(QemuConsole
*con
,
394 pixman_format_code_t format
);
396 void dpy_gl_scanout_disable(QemuConsole
*con
);
397 void dpy_gl_scanout_texture(QemuConsole
*con
,
398 uint32_t backing_id
, bool backing_y_0_top
,
399 uint32_t backing_width
, uint32_t backing_height
,
400 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
,
402 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
404 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
405 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
);
406 void dpy_gl_cursor_position(QemuConsole
*con
,
407 uint32_t pos_x
, uint32_t pos_y
);
408 void dpy_gl_release_dmabuf(QemuConsole
*con
,
410 void dpy_gl_update(QemuConsole
*con
,
411 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
);
413 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
414 QEMUGLParams
*params
);
415 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
);
416 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
);
418 bool console_has_gl(QemuConsole
*con
);
420 static inline int surface_stride(DisplaySurface
*s
)
422 return pixman_image_get_stride(s
->image
);
425 static inline void *surface_data(DisplaySurface
*s
)
427 return pixman_image_get_data(s
->image
);
430 static inline int surface_width(DisplaySurface
*s
)
432 return pixman_image_get_width(s
->image
);
435 static inline int surface_height(DisplaySurface
*s
)
437 return pixman_image_get_height(s
->image
);
440 static inline int surface_bits_per_pixel(DisplaySurface
*s
)
442 int bits
= PIXMAN_FORMAT_BPP(s
->format
);
446 static inline int surface_bytes_per_pixel(DisplaySurface
*s
)
448 int bits
= PIXMAN_FORMAT_BPP(s
->format
);
449 return DIV_ROUND_UP(bits
, 8);
452 static inline pixman_format_code_t
surface_format(DisplaySurface
*s
)
457 typedef uint32_t console_ch_t
;
459 static inline void console_write_ch(console_ch_t
*dest
, uint32_t ch
)
465 GRAPHIC_FLAGS_NONE
= 0,
466 /* require a console/display with GL callbacks */
467 GRAPHIC_FLAGS_GL
= 1 << 0,
468 /* require a console/display with DMABUF import */
469 GRAPHIC_FLAGS_DMABUF
= 1 << 1,
472 typedef struct GraphicHwOps
{
473 int (*get_flags
)(void *opaque
); /* optional, default 0 */
474 void (*invalidate
)(void *opaque
);
475 void (*gfx_update
)(void *opaque
);
476 bool gfx_update_async
; /* if true, calls graphic_hw_update_done() */
477 void (*text_update
)(void *opaque
, console_ch_t
*text
);
478 void (*ui_info
)(void *opaque
, uint32_t head
, QemuUIInfo
*info
);
479 void (*gl_block
)(void *opaque
, bool block
);
482 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
483 const GraphicHwOps
*ops
,
485 void graphic_console_set_hwops(QemuConsole
*con
,
486 const GraphicHwOps
*hw_ops
,
488 void graphic_console_close(QemuConsole
*con
);
490 void graphic_hw_update(QemuConsole
*con
);
491 void graphic_hw_update_done(QemuConsole
*con
);
492 void graphic_hw_invalidate(QemuConsole
*con
);
493 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
);
494 void graphic_hw_gl_block(QemuConsole
*con
, bool block
);
496 void qemu_console_early_init(void);
498 void qemu_console_set_display_gl_ctx(QemuConsole
*con
, DisplayGLCtx
*ctx
);
500 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
);
501 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
);
502 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
503 uint32_t head
, Error
**errp
);
504 QEMUCursor
*qemu_console_get_cursor(QemuConsole
*con
);
505 bool qemu_console_is_visible(QemuConsole
*con
);
506 bool qemu_console_is_graphic(QemuConsole
*con
);
507 bool qemu_console_is_fixedsize(QemuConsole
*con
);
508 bool qemu_console_is_gl_blocked(QemuConsole
*con
);
509 bool qemu_console_is_multihead(DeviceState
*dev
);
510 char *qemu_console_get_label(QemuConsole
*con
);
511 int qemu_console_get_index(QemuConsole
*con
);
512 uint32_t qemu_console_get_head(QemuConsole
*con
);
513 int qemu_console_get_width(QemuConsole
*con
, int fallback
);
514 int qemu_console_get_height(QemuConsole
*con
, int fallback
);
515 /* Return the low-level window id for the console */
516 int qemu_console_get_window_id(QemuConsole
*con
);
517 /* Set the low-level window id for the console */
518 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
);
520 void console_select(unsigned int index
);
521 void qemu_console_resize(QemuConsole
*con
, int width
, int height
);
522 DisplaySurface
*qemu_console_surface(QemuConsole
*con
);
523 void coroutine_fn
qemu_console_co_wait_update(QemuConsole
*con
);
524 int qemu_invalidate_text_consoles(void);
528 bool console_gl_check_format(DisplayChangeListener
*dcl
,
529 pixman_format_code_t format
);
530 void surface_gl_create_texture(QemuGLShader
*gls
,
531 DisplaySurface
*surface
);
532 void surface_gl_update_texture(QemuGLShader
*gls
,
533 DisplaySurface
*surface
,
534 int x
, int y
, int w
, int h
);
535 void surface_gl_render_texture(QemuGLShader
*gls
,
536 DisplaySurface
*surface
);
537 void surface_gl_destroy_texture(QemuGLShader
*gls
,
538 DisplaySurface
*surface
);
539 void surface_gl_setup_viewport(QemuGLShader
*gls
,
540 DisplaySurface
*surface
,
544 typedef struct QemuDisplay QemuDisplay
;
548 void (*early_init
)(DisplayOptions
*opts
);
549 void (*init
)(DisplayState
*ds
, DisplayOptions
*opts
);
552 void qemu_display_register(QemuDisplay
*ui
);
553 bool qemu_display_find_default(DisplayOptions
*opts
);
554 void qemu_display_early_init(DisplayOptions
*opts
);
555 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
);
556 void qemu_display_help(void);
559 void vnc_display_init(const char *id
, Error
**errp
);
560 void vnc_display_open(const char *id
, Error
**errp
);
561 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
);
562 int vnc_display_password(const char *id
, const char *password
);
563 int vnc_display_pw_expire(const char *id
, time_t expires
);
564 void vnc_parse(const char *str
);
565 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
);
566 bool vnc_display_reload_certs(const char *id
, Error
**errp
);
567 bool vnc_display_update(DisplayUpdateOptionsVNC
*arg
, Error
**errp
);
570 int index_from_key(const char *key
, size_t key_length
);
574 int udmabuf_fd(void);
578 bool qemu_console_fill_device_address(QemuConsole
*con
,
579 char *device_address
,