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(QemuTextConsole
*s
, int keysym
);
116 bool kbd_put_qcode_console(QemuTextConsole
*s
, int qcode
, bool ctrl
);
117 void kbd_put_string_console(QemuTextConsole
*s
, const char *str
, int len
);
120 typedef struct touch_slot
{
126 void console_handle_touch_event(QemuConsole
*con
,
127 struct touch_slot touch_slots
[INPUT_EVENT_SLOTS_MAX
],
129 int width
, int height
,
131 InputMultiTouchType type
,
135 struct QemuConsoleClass
{
136 ObjectClass parent_class
;
139 #define QEMU_ALLOCATED_FLAG 0x01
140 #define QEMU_PLACEHOLDER_FLAG 0x02
142 typedef struct ScanoutTexture
{
144 bool backing_y_0_top
;
145 uint32_t backing_width
;
146 uint32_t backing_height
;
154 typedef struct DisplaySurface
{
155 pixman_format_code_t format
;
156 pixman_image_t
*image
;
165 uint32_t handle_offset
;
169 typedef struct QemuUIInfo
{
170 /* physical dimension */
178 uint32_t refresh_rate
;
181 /* cursor data format is 32bit RGBA */
182 typedef struct QEMUCursor
{
183 uint16_t width
, height
;
189 QEMUCursor
*cursor_alloc(uint16_t width
, uint16_t height
);
190 QEMUCursor
*cursor_ref(QEMUCursor
*c
);
191 void cursor_unref(QEMUCursor
*c
);
192 QEMUCursor
*cursor_builtin_hidden(void);
193 QEMUCursor
*cursor_builtin_left_ptr(void);
194 void cursor_print_ascii_art(QEMUCursor
*c
, const char *prefix
);
195 int cursor_get_mono_bpl(QEMUCursor
*c
);
196 void cursor_set_mono(QEMUCursor
*c
,
197 uint32_t foreground
, uint32_t background
, uint8_t *image
,
198 int transparent
, uint8_t *mask
);
199 void cursor_get_mono_image(QEMUCursor
*c
, int foreground
, uint8_t *mask
);
200 void cursor_get_mono_mask(QEMUCursor
*c
, int transparent
, uint8_t *mask
);
202 typedef void *QEMUGLContext
;
203 typedef struct QEMUGLParams QEMUGLParams
;
205 struct QEMUGLParams
{
210 typedef struct QemuDmaBuf
{
220 uint32_t backing_width
;
221 uint32_t backing_height
;
229 enum display_scanout
{
236 typedef struct DisplayScanout
{
237 enum display_scanout kind
;
239 /* DisplaySurface *surface; is kept in QemuConsole */
240 ScanoutTexture texture
;
245 typedef struct DisplayState DisplayState
;
246 typedef struct DisplayGLCtx DisplayGLCtx
;
248 typedef struct DisplayChangeListenerOps
{
249 const char *dpy_name
;
252 void (*dpy_refresh
)(DisplayChangeListener
*dcl
);
255 void (*dpy_gfx_update
)(DisplayChangeListener
*dcl
,
256 int x
, int y
, int w
, int h
);
258 void (*dpy_gfx_switch
)(DisplayChangeListener
*dcl
,
259 struct DisplaySurface
*new_surface
);
261 bool (*dpy_gfx_check_format
)(DisplayChangeListener
*dcl
,
262 pixman_format_code_t format
);
265 void (*dpy_text_cursor
)(DisplayChangeListener
*dcl
,
268 void (*dpy_text_resize
)(DisplayChangeListener
*dcl
,
271 void (*dpy_text_update
)(DisplayChangeListener
*dcl
,
272 int x
, int y
, int w
, int h
);
275 void (*dpy_mouse_set
)(DisplayChangeListener
*dcl
,
276 int x
, int y
, int on
);
278 void (*dpy_cursor_define
)(DisplayChangeListener
*dcl
,
282 void (*dpy_gl_scanout_disable
)(DisplayChangeListener
*dcl
);
284 void (*dpy_gl_scanout_texture
)(DisplayChangeListener
*dcl
,
286 bool backing_y_0_top
,
287 uint32_t backing_width
,
288 uint32_t backing_height
,
289 uint32_t x
, uint32_t y
,
290 uint32_t w
, uint32_t h
,
292 /* optional (default to true if has dpy_gl_scanout_dmabuf) */
293 bool (*dpy_has_dmabuf
)(DisplayChangeListener
*dcl
);
295 void (*dpy_gl_scanout_dmabuf
)(DisplayChangeListener
*dcl
,
298 void (*dpy_gl_cursor_dmabuf
)(DisplayChangeListener
*dcl
,
299 QemuDmaBuf
*dmabuf
, bool have_hot
,
300 uint32_t hot_x
, uint32_t hot_y
);
302 void (*dpy_gl_cursor_position
)(DisplayChangeListener
*dcl
,
303 uint32_t pos_x
, uint32_t pos_y
);
305 void (*dpy_gl_release_dmabuf
)(DisplayChangeListener
*dcl
,
308 void (*dpy_gl_update
)(DisplayChangeListener
*dcl
,
309 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
);
311 } DisplayChangeListenerOps
;
313 struct DisplayChangeListener
{
314 uint64_t update_interval
;
315 const DisplayChangeListenerOps
*ops
;
319 QLIST_ENTRY(DisplayChangeListener
) next
;
322 typedef struct DisplayGLCtxOps
{
323 bool (*dpy_gl_ctx_is_compatible_dcl
)(DisplayGLCtx
*dgc
,
324 DisplayChangeListener
*dcl
);
325 QEMUGLContext (*dpy_gl_ctx_create
)(DisplayGLCtx
*dgc
,
326 QEMUGLParams
*params
);
327 void (*dpy_gl_ctx_destroy
)(DisplayGLCtx
*dgc
,
329 int (*dpy_gl_ctx_make_current
)(DisplayGLCtx
*dgc
,
331 void (*dpy_gl_ctx_create_texture
)(DisplayGLCtx
*dgc
,
332 DisplaySurface
*surface
);
333 void (*dpy_gl_ctx_destroy_texture
)(DisplayGLCtx
*dgc
,
334 DisplaySurface
*surface
);
335 void (*dpy_gl_ctx_update_texture
)(DisplayGLCtx
*dgc
,
336 DisplaySurface
*surface
,
337 int x
, int y
, int w
, int h
);
340 struct DisplayGLCtx
{
341 const DisplayGLCtxOps
*ops
;
343 QemuGLShader
*gls
; /* optional shared shader */
347 DisplayState
*init_displaystate(void);
348 DisplaySurface
*qemu_create_displaysurface_from(int width
, int height
,
349 pixman_format_code_t format
,
350 int linesize
, uint8_t *data
);
351 DisplaySurface
*qemu_create_displaysurface_pixman(pixman_image_t
*image
);
352 DisplaySurface
*qemu_create_placeholder_surface(int w
, int h
,
355 void qemu_displaysurface_win32_set_handle(DisplaySurface
*surface
,
356 HANDLE h
, uint32_t offset
);
358 PixelFormat
qemu_default_pixelformat(int bpp
);
360 DisplaySurface
*qemu_create_displaysurface(int width
, int height
);
361 void qemu_free_displaysurface(DisplaySurface
*surface
);
363 static inline int is_buffer_shared(DisplaySurface
*surface
)
365 return !(surface
->flags
& QEMU_ALLOCATED_FLAG
);
368 static inline int is_placeholder(DisplaySurface
*surface
)
370 return surface
->flags
& QEMU_PLACEHOLDER_FLAG
;
373 void register_displaychangelistener(DisplayChangeListener
*dcl
);
374 void update_displaychangelistener(DisplayChangeListener
*dcl
,
376 void unregister_displaychangelistener(DisplayChangeListener
*dcl
);
378 bool dpy_ui_info_supported(QemuConsole
*con
);
379 const QemuUIInfo
*dpy_get_ui_info(const QemuConsole
*con
);
380 int dpy_set_ui_info(QemuConsole
*con
, QemuUIInfo
*info
, bool delay
);
382 void dpy_gfx_update(QemuConsole
*con
, int x
, int y
, int w
, int h
);
383 void dpy_gfx_update_full(QemuConsole
*con
);
384 void dpy_gfx_replace_surface(QemuConsole
*con
,
385 DisplaySurface
*surface
);
386 void dpy_text_cursor(QemuConsole
*con
, int x
, int y
);
387 void dpy_text_update(QemuConsole
*con
, int x
, int y
, int w
, int h
);
388 void dpy_text_resize(QemuConsole
*con
, int w
, int h
);
389 void dpy_mouse_set(QemuConsole
*con
, int x
, int y
, int on
);
390 void dpy_cursor_define(QemuConsole
*con
, QEMUCursor
*cursor
);
391 bool dpy_cursor_define_supported(QemuConsole
*con
);
392 bool dpy_gfx_check_format(QemuConsole
*con
,
393 pixman_format_code_t format
);
395 void dpy_gl_scanout_disable(QemuConsole
*con
);
396 void dpy_gl_scanout_texture(QemuConsole
*con
,
397 uint32_t backing_id
, bool backing_y_0_top
,
398 uint32_t backing_width
, uint32_t backing_height
,
399 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
,
401 void dpy_gl_scanout_dmabuf(QemuConsole
*con
,
403 void dpy_gl_cursor_dmabuf(QemuConsole
*con
, QemuDmaBuf
*dmabuf
,
404 bool have_hot
, uint32_t hot_x
, uint32_t hot_y
);
405 void dpy_gl_cursor_position(QemuConsole
*con
,
406 uint32_t pos_x
, uint32_t pos_y
);
407 void dpy_gl_release_dmabuf(QemuConsole
*con
,
409 void dpy_gl_update(QemuConsole
*con
,
410 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
);
412 QEMUGLContext
dpy_gl_ctx_create(QemuConsole
*con
,
413 QEMUGLParams
*params
);
414 void dpy_gl_ctx_destroy(QemuConsole
*con
, QEMUGLContext ctx
);
415 int dpy_gl_ctx_make_current(QemuConsole
*con
, QEMUGLContext ctx
);
417 bool console_has_gl(QemuConsole
*con
);
419 static inline int surface_stride(DisplaySurface
*s
)
421 return pixman_image_get_stride(s
->image
);
424 static inline void *surface_data(DisplaySurface
*s
)
426 return pixman_image_get_data(s
->image
);
429 static inline int surface_width(DisplaySurface
*s
)
431 return pixman_image_get_width(s
->image
);
434 static inline int surface_height(DisplaySurface
*s
)
436 return pixman_image_get_height(s
->image
);
439 static inline int surface_bits_per_pixel(DisplaySurface
*s
)
441 int bits
= PIXMAN_FORMAT_BPP(s
->format
);
445 static inline int surface_bytes_per_pixel(DisplaySurface
*s
)
447 int bits
= PIXMAN_FORMAT_BPP(s
->format
);
448 return DIV_ROUND_UP(bits
, 8);
451 static inline pixman_format_code_t
surface_format(DisplaySurface
*s
)
456 typedef uint32_t console_ch_t
;
458 static inline void console_write_ch(console_ch_t
*dest
, uint32_t ch
)
464 GRAPHIC_FLAGS_NONE
= 0,
465 /* require a console/display with GL callbacks */
466 GRAPHIC_FLAGS_GL
= 1 << 0,
467 /* require a console/display with DMABUF import */
468 GRAPHIC_FLAGS_DMABUF
= 1 << 1,
471 typedef struct GraphicHwOps
{
472 int (*get_flags
)(void *opaque
); /* optional, default 0 */
473 void (*invalidate
)(void *opaque
);
474 void (*gfx_update
)(void *opaque
);
475 bool gfx_update_async
; /* if true, calls graphic_hw_update_done() */
476 void (*text_update
)(void *opaque
, console_ch_t
*text
);
477 void (*ui_info
)(void *opaque
, uint32_t head
, QemuUIInfo
*info
);
478 void (*gl_block
)(void *opaque
, bool block
);
481 QemuConsole
*graphic_console_init(DeviceState
*dev
, uint32_t head
,
482 const GraphicHwOps
*ops
,
484 void graphic_console_set_hwops(QemuConsole
*con
,
485 const GraphicHwOps
*hw_ops
,
487 void graphic_console_close(QemuConsole
*con
);
489 void graphic_hw_update(QemuConsole
*con
);
490 void graphic_hw_update_done(QemuConsole
*con
);
491 void graphic_hw_invalidate(QemuConsole
*con
);
492 void graphic_hw_text_update(QemuConsole
*con
, console_ch_t
*chardata
);
493 void graphic_hw_gl_block(QemuConsole
*con
, bool block
);
495 void qemu_console_early_init(void);
497 void qemu_console_set_display_gl_ctx(QemuConsole
*con
, DisplayGLCtx
*ctx
);
499 QemuConsole
*qemu_console_lookup_by_index(unsigned int index
);
500 QemuConsole
*qemu_console_lookup_by_device(DeviceState
*dev
, uint32_t head
);
501 QemuConsole
*qemu_console_lookup_by_device_name(const char *device_id
,
502 uint32_t head
, Error
**errp
);
503 QEMUCursor
*qemu_console_get_cursor(QemuConsole
*con
);
504 bool qemu_console_is_visible(QemuConsole
*con
);
505 bool qemu_console_is_graphic(QemuConsole
*con
);
506 bool qemu_console_is_fixedsize(QemuConsole
*con
);
507 bool qemu_console_is_gl_blocked(QemuConsole
*con
);
508 bool qemu_console_is_multihead(DeviceState
*dev
);
509 char *qemu_console_get_label(QemuConsole
*con
);
510 int qemu_console_get_index(QemuConsole
*con
);
511 uint32_t qemu_console_get_head(QemuConsole
*con
);
512 int qemu_console_get_width(QemuConsole
*con
, int fallback
);
513 int qemu_console_get_height(QemuConsole
*con
, int fallback
);
514 /* Return the low-level window id for the console */
515 int qemu_console_get_window_id(QemuConsole
*con
);
516 /* Set the low-level window id for the console */
517 void qemu_console_set_window_id(QemuConsole
*con
, int window_id
);
519 void console_select(unsigned int index
);
520 void qemu_console_resize(QemuConsole
*con
, int width
, int height
);
521 DisplaySurface
*qemu_console_surface(QemuConsole
*con
);
522 void coroutine_fn
qemu_console_co_wait_update(QemuConsole
*con
);
523 int qemu_invalidate_text_consoles(void);
527 bool console_gl_check_format(DisplayChangeListener
*dcl
,
528 pixman_format_code_t format
);
529 void surface_gl_create_texture(QemuGLShader
*gls
,
530 DisplaySurface
*surface
);
531 void surface_gl_update_texture(QemuGLShader
*gls
,
532 DisplaySurface
*surface
,
533 int x
, int y
, int w
, int h
);
534 void surface_gl_render_texture(QemuGLShader
*gls
,
535 DisplaySurface
*surface
);
536 void surface_gl_destroy_texture(QemuGLShader
*gls
,
537 DisplaySurface
*surface
);
538 void surface_gl_setup_viewport(QemuGLShader
*gls
,
539 DisplaySurface
*surface
,
543 typedef struct QemuDisplay QemuDisplay
;
547 void (*early_init
)(DisplayOptions
*opts
);
548 void (*init
)(DisplayState
*ds
, DisplayOptions
*opts
);
551 void qemu_display_register(QemuDisplay
*ui
);
552 bool qemu_display_find_default(DisplayOptions
*opts
);
553 void qemu_display_early_init(DisplayOptions
*opts
);
554 void qemu_display_init(DisplayState
*ds
, DisplayOptions
*opts
);
555 void qemu_display_help(void);
558 void vnc_display_init(const char *id
, Error
**errp
);
559 void vnc_display_open(const char *id
, Error
**errp
);
560 void vnc_display_add_client(const char *id
, int csock
, bool skipauth
);
561 int vnc_display_password(const char *id
, const char *password
);
562 int vnc_display_pw_expire(const char *id
, time_t expires
);
563 void vnc_parse(const char *str
);
564 int vnc_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
);
565 bool vnc_display_reload_certs(const char *id
, Error
**errp
);
566 bool vnc_display_update(DisplayUpdateOptionsVNC
*arg
, Error
**errp
);
569 int index_from_key(const char *key
, size_t key_length
);
573 int udmabuf_fd(void);
577 bool qemu_console_fill_device_address(QemuConsole
*con
,
578 char *device_address
,