console.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #ifndef CONSOLE_H
  2. #define CONSOLE_H
  3. #include "ui/qemu-pixman.h"
  4. #include "qom/object.h"
  5. #include "qemu/notify.h"
  6. #include "qapi/qapi-types-ui.h"
  7. #include "ui/input.h"
  8. #include "ui/surface.h"
  9. #include "ui/dmabuf.h"
  10. #define TYPE_QEMU_CONSOLE "qemu-console"
  11. OBJECT_DECLARE_TYPE(QemuConsole, QemuConsoleClass, QEMU_CONSOLE)
  12. #define TYPE_QEMU_GRAPHIC_CONSOLE "qemu-graphic-console"
  13. OBJECT_DECLARE_SIMPLE_TYPE(QemuGraphicConsole, QEMU_GRAPHIC_CONSOLE)
  14. #define TYPE_QEMU_TEXT_CONSOLE "qemu-text-console"
  15. OBJECT_DECLARE_SIMPLE_TYPE(QemuTextConsole, QEMU_TEXT_CONSOLE)
  16. #define TYPE_QEMU_FIXED_TEXT_CONSOLE "qemu-fixed-text-console"
  17. OBJECT_DECLARE_SIMPLE_TYPE(QemuFixedTextConsole, QEMU_FIXED_TEXT_CONSOLE)
  18. #define QEMU_IS_GRAPHIC_CONSOLE(c) \
  19. object_dynamic_cast(OBJECT(c), TYPE_QEMU_GRAPHIC_CONSOLE)
  20. #define QEMU_IS_TEXT_CONSOLE(c) \
  21. object_dynamic_cast(OBJECT(c), TYPE_QEMU_TEXT_CONSOLE)
  22. #define QEMU_IS_FIXED_TEXT_CONSOLE(c) \
  23. object_dynamic_cast(OBJECT(c), TYPE_QEMU_FIXED_TEXT_CONSOLE)
  24. /* keyboard/mouse support */
  25. #define MOUSE_EVENT_LBUTTON 0x01
  26. #define MOUSE_EVENT_RBUTTON 0x02
  27. #define MOUSE_EVENT_MBUTTON 0x04
  28. #define MOUSE_EVENT_WHEELUP 0x08
  29. #define MOUSE_EVENT_WHEELDN 0x10
  30. /* identical to the ps/2 keyboard bits */
  31. #define QEMU_SCROLL_LOCK_LED (1 << 0)
  32. #define QEMU_NUM_LOCK_LED (1 << 1)
  33. #define QEMU_CAPS_LOCK_LED (1 << 2)
  34. /* in ms */
  35. #define GUI_REFRESH_INTERVAL_DEFAULT 30
  36. #define GUI_REFRESH_INTERVAL_IDLE 3000
  37. /* Color number is match to standard vga palette */
  38. enum qemu_color_names {
  39. QEMU_COLOR_BLACK = 0,
  40. QEMU_COLOR_BLUE = 1,
  41. QEMU_COLOR_GREEN = 2,
  42. QEMU_COLOR_CYAN = 3,
  43. QEMU_COLOR_RED = 4,
  44. QEMU_COLOR_MAGENTA = 5,
  45. QEMU_COLOR_YELLOW = 6,
  46. QEMU_COLOR_WHITE = 7
  47. };
  48. /* Convert to curses char attributes */
  49. #define ATTR2CHTYPE(c, fg, bg, bold) \
  50. ((bold) << 21 | (bg) << 11 | (fg) << 8 | (c))
  51. typedef void QEMUPutKBDEvent(void *opaque, int keycode);
  52. typedef void QEMUPutLEDEvent(void *opaque, int ledstate);
  53. typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int buttons_state);
  54. typedef struct QEMUPutMouseEntry QEMUPutMouseEntry;
  55. typedef struct QEMUPutKbdEntry QEMUPutKbdEntry;
  56. typedef struct QEMUPutLEDEntry QEMUPutLEDEntry;
  57. QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
  58. void *opaque, int absolute,
  59. const char *name);
  60. void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry);
  61. void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry);
  62. QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, void *opaque);
  63. void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry);
  64. void kbd_put_ledstate(int ledstate);
  65. bool qemu_mouse_set(int index, Error **errp);
  66. /* keysym is a unicode code except for special keys (see QEMU_KEY_xxx
  67. constants) */
  68. #define QEMU_KEY_ESC1(c) ((c) | 0xe100)
  69. #define QEMU_KEY_TAB 0x0009
  70. #define QEMU_KEY_BACKSPACE 0x007f
  71. #define QEMU_KEY_UP QEMU_KEY_ESC1('A')
  72. #define QEMU_KEY_DOWN QEMU_KEY_ESC1('B')
  73. #define QEMU_KEY_RIGHT QEMU_KEY_ESC1('C')
  74. #define QEMU_KEY_LEFT QEMU_KEY_ESC1('D')
  75. #define QEMU_KEY_HOME QEMU_KEY_ESC1(1)
  76. #define QEMU_KEY_END QEMU_KEY_ESC1(4)
  77. #define QEMU_KEY_PAGEUP QEMU_KEY_ESC1(5)
  78. #define QEMU_KEY_PAGEDOWN QEMU_KEY_ESC1(6)
  79. #define QEMU_KEY_DELETE QEMU_KEY_ESC1(3)
  80. #define QEMU_KEY_CTRL_UP 0xe400
  81. #define QEMU_KEY_CTRL_DOWN 0xe401
  82. #define QEMU_KEY_CTRL_LEFT 0xe402
  83. #define QEMU_KEY_CTRL_RIGHT 0xe403
  84. #define QEMU_KEY_CTRL_HOME 0xe404
  85. #define QEMU_KEY_CTRL_END 0xe405
  86. #define QEMU_KEY_CTRL_PAGEUP 0xe406
  87. #define QEMU_KEY_CTRL_PAGEDOWN 0xe407
  88. void qemu_text_console_put_keysym(QemuTextConsole *s, int keysym);
  89. bool qemu_text_console_put_qcode(QemuTextConsole *s, int qcode, bool ctrl);
  90. void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len);
  91. /* Touch devices */
  92. typedef struct touch_slot {
  93. int x;
  94. int y;
  95. int tracking_id;
  96. } touch_slot;
  97. void console_handle_touch_event(QemuConsole *con,
  98. struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
  99. uint64_t num_slot,
  100. int width, int height,
  101. double x, double y,
  102. InputMultiTouchType type,
  103. Error **errp);
  104. /* consoles */
  105. struct QemuConsoleClass {
  106. ObjectClass parent_class;
  107. };
  108. typedef struct ScanoutTexture {
  109. uint32_t backing_id;
  110. bool backing_y_0_top;
  111. uint32_t backing_width;
  112. uint32_t backing_height;
  113. uint32_t x;
  114. uint32_t y;
  115. uint32_t width;
  116. uint32_t height;
  117. void *d3d_tex2d;
  118. } ScanoutTexture;
  119. typedef struct QemuUIInfo {
  120. /* physical dimension */
  121. uint16_t width_mm;
  122. uint16_t height_mm;
  123. /* geometry */
  124. int xoff;
  125. int yoff;
  126. uint32_t width;
  127. uint32_t height;
  128. uint32_t refresh_rate;
  129. } QemuUIInfo;
  130. /* cursor data format is 32bit RGBA */
  131. typedef struct QEMUCursor {
  132. uint16_t width, height;
  133. int hot_x, hot_y;
  134. int refcount;
  135. uint32_t data[];
  136. } QEMUCursor;
  137. QEMUCursor *cursor_alloc(uint16_t width, uint16_t height);
  138. QEMUCursor *cursor_ref(QEMUCursor *c);
  139. void cursor_unref(QEMUCursor *c);
  140. QEMUCursor *cursor_builtin_hidden(void);
  141. QEMUCursor *cursor_builtin_left_ptr(void);
  142. void cursor_print_ascii_art(QEMUCursor *c, const char *prefix);
  143. int cursor_get_mono_bpl(QEMUCursor *c);
  144. void cursor_set_mono(QEMUCursor *c,
  145. uint32_t foreground, uint32_t background, uint8_t *image,
  146. int transparent, uint8_t *mask);
  147. void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask);
  148. typedef void *QEMUGLContext;
  149. typedef struct QEMUGLParams QEMUGLParams;
  150. struct QEMUGLParams {
  151. int major_ver;
  152. int minor_ver;
  153. };
  154. enum display_scanout {
  155. SCANOUT_NONE,
  156. SCANOUT_SURFACE,
  157. SCANOUT_TEXTURE,
  158. SCANOUT_DMABUF,
  159. };
  160. typedef struct DisplayScanout {
  161. enum display_scanout kind;
  162. union {
  163. /* DisplaySurface *surface; is kept in QemuConsole */
  164. ScanoutTexture texture;
  165. QemuDmaBuf *dmabuf;
  166. };
  167. } DisplayScanout;
  168. typedef struct DisplayState DisplayState;
  169. typedef struct DisplayGLCtx DisplayGLCtx;
  170. typedef struct DisplayChangeListenerOps {
  171. const char *dpy_name;
  172. /* optional */
  173. void (*dpy_refresh)(DisplayChangeListener *dcl);
  174. /* optional */
  175. void (*dpy_gfx_update)(DisplayChangeListener *dcl,
  176. int x, int y, int w, int h);
  177. /* optional */
  178. void (*dpy_gfx_switch)(DisplayChangeListener *dcl,
  179. struct DisplaySurface *new_surface);
  180. /* optional */
  181. bool (*dpy_gfx_check_format)(DisplayChangeListener *dcl,
  182. pixman_format_code_t format);
  183. /* optional */
  184. void (*dpy_text_cursor)(DisplayChangeListener *dcl,
  185. int x, int y);
  186. /* optional */
  187. void (*dpy_text_resize)(DisplayChangeListener *dcl,
  188. int w, int h);
  189. /* optional */
  190. void (*dpy_text_update)(DisplayChangeListener *dcl,
  191. int x, int y, int w, int h);
  192. /* optional */
  193. void (*dpy_mouse_set)(DisplayChangeListener *dcl,
  194. int x, int y, bool on);
  195. /* optional */
  196. void (*dpy_cursor_define)(DisplayChangeListener *dcl,
  197. QEMUCursor *cursor);
  198. /* required if GL */
  199. void (*dpy_gl_scanout_disable)(DisplayChangeListener *dcl);
  200. /* required if GL */
  201. void (*dpy_gl_scanout_texture)(DisplayChangeListener *dcl,
  202. uint32_t backing_id,
  203. bool backing_y_0_top,
  204. uint32_t backing_width,
  205. uint32_t backing_height,
  206. uint32_t x, uint32_t y,
  207. uint32_t w, uint32_t h,
  208. void *d3d_tex2d);
  209. /* optional (default to true if has dpy_gl_scanout_dmabuf) */
  210. bool (*dpy_has_dmabuf)(DisplayChangeListener *dcl);
  211. /* optional */
  212. void (*dpy_gl_scanout_dmabuf)(DisplayChangeListener *dcl,
  213. QemuDmaBuf *dmabuf);
  214. /* optional */
  215. void (*dpy_gl_cursor_dmabuf)(DisplayChangeListener *dcl,
  216. QemuDmaBuf *dmabuf, bool have_hot,
  217. uint32_t hot_x, uint32_t hot_y);
  218. /* optional */
  219. void (*dpy_gl_cursor_position)(DisplayChangeListener *dcl,
  220. uint32_t pos_x, uint32_t pos_y);
  221. /* optional */
  222. void (*dpy_gl_release_dmabuf)(DisplayChangeListener *dcl,
  223. QemuDmaBuf *dmabuf);
  224. /* required if GL */
  225. void (*dpy_gl_update)(DisplayChangeListener *dcl,
  226. uint32_t x, uint32_t y, uint32_t w, uint32_t h);
  227. } DisplayChangeListenerOps;
  228. struct DisplayChangeListener {
  229. uint64_t update_interval;
  230. const DisplayChangeListenerOps *ops;
  231. DisplayState *ds;
  232. QemuConsole *con;
  233. QLIST_ENTRY(DisplayChangeListener) next;
  234. };
  235. typedef struct DisplayGLCtxOps {
  236. bool (*dpy_gl_ctx_is_compatible_dcl)(DisplayGLCtx *dgc,
  237. DisplayChangeListener *dcl);
  238. QEMUGLContext (*dpy_gl_ctx_create)(DisplayGLCtx *dgc,
  239. QEMUGLParams *params);
  240. void (*dpy_gl_ctx_destroy)(DisplayGLCtx *dgc,
  241. QEMUGLContext ctx);
  242. int (*dpy_gl_ctx_make_current)(DisplayGLCtx *dgc,
  243. QEMUGLContext ctx);
  244. void (*dpy_gl_ctx_create_texture)(DisplayGLCtx *dgc,
  245. DisplaySurface *surface);
  246. void (*dpy_gl_ctx_destroy_texture)(DisplayGLCtx *dgc,
  247. DisplaySurface *surface);
  248. void (*dpy_gl_ctx_update_texture)(DisplayGLCtx *dgc,
  249. DisplaySurface *surface,
  250. int x, int y, int w, int h);
  251. } DisplayGLCtxOps;
  252. struct DisplayGLCtx {
  253. const DisplayGLCtxOps *ops;
  254. #ifdef CONFIG_OPENGL
  255. QemuGLShader *gls; /* optional shared shader */
  256. #endif
  257. };
  258. DisplayState *init_displaystate(void);
  259. void register_displaychangelistener(DisplayChangeListener *dcl);
  260. void update_displaychangelistener(DisplayChangeListener *dcl,
  261. uint64_t interval);
  262. void unregister_displaychangelistener(DisplayChangeListener *dcl);
  263. bool dpy_ui_info_supported(const QemuConsole *con);
  264. const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con);
  265. int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay);
  266. void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h);
  267. void dpy_gfx_update_full(QemuConsole *con);
  268. void dpy_gfx_replace_surface(QemuConsole *con,
  269. DisplaySurface *surface);
  270. void dpy_text_cursor(QemuConsole *con, int x, int y);
  271. void dpy_text_update(QemuConsole *con, int x, int y, int w, int h);
  272. void dpy_text_resize(QemuConsole *con, int w, int h);
  273. void dpy_mouse_set(QemuConsole *con, int x, int y, bool on);
  274. void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor);
  275. bool dpy_gfx_check_format(QemuConsole *con,
  276. pixman_format_code_t format);
  277. void dpy_gl_scanout_disable(QemuConsole *con);
  278. void dpy_gl_scanout_texture(QemuConsole *con,
  279. uint32_t backing_id, bool backing_y_0_top,
  280. uint32_t backing_width, uint32_t backing_height,
  281. uint32_t x, uint32_t y, uint32_t w, uint32_t h,
  282. void *d3d_tex2d);
  283. void dpy_gl_scanout_dmabuf(QemuConsole *con,
  284. QemuDmaBuf *dmabuf);
  285. void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
  286. bool have_hot, uint32_t hot_x, uint32_t hot_y);
  287. void dpy_gl_cursor_position(QemuConsole *con,
  288. uint32_t pos_x, uint32_t pos_y);
  289. void dpy_gl_release_dmabuf(QemuConsole *con,
  290. QemuDmaBuf *dmabuf);
  291. void dpy_gl_update(QemuConsole *con,
  292. uint32_t x, uint32_t y, uint32_t w, uint32_t h);
  293. QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
  294. QEMUGLParams *params);
  295. void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx);
  296. int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx);
  297. bool console_has_gl(QemuConsole *con);
  298. typedef uint32_t console_ch_t;
  299. static inline void console_write_ch(console_ch_t *dest, uint32_t ch)
  300. {
  301. *dest = ch;
  302. }
  303. enum {
  304. GRAPHIC_FLAGS_NONE = 0,
  305. /* require a console/display with GL callbacks */
  306. GRAPHIC_FLAGS_GL = 1 << 0,
  307. /* require a console/display with DMABUF import */
  308. GRAPHIC_FLAGS_DMABUF = 1 << 1,
  309. };
  310. typedef struct GraphicHwOps {
  311. int (*get_flags)(void *opaque); /* optional, default 0 */
  312. void (*invalidate)(void *opaque);
  313. void (*gfx_update)(void *opaque);
  314. bool gfx_update_async; /* if true, calls graphic_hw_update_done() */
  315. void (*text_update)(void *opaque, console_ch_t *text);
  316. void (*ui_info)(void *opaque, uint32_t head, QemuUIInfo *info);
  317. void (*gl_block)(void *opaque, bool block);
  318. } GraphicHwOps;
  319. QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
  320. const GraphicHwOps *ops,
  321. void *opaque);
  322. void graphic_console_set_hwops(QemuConsole *con,
  323. const GraphicHwOps *hw_ops,
  324. void *opaque);
  325. void graphic_console_close(QemuConsole *con);
  326. void graphic_hw_update(QemuConsole *con);
  327. void graphic_hw_update_done(QemuConsole *con);
  328. void graphic_hw_invalidate(QemuConsole *con);
  329. void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata);
  330. void graphic_hw_gl_block(QemuConsole *con, bool block);
  331. void qemu_console_early_init(void);
  332. void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *ctx);
  333. QemuConsole *qemu_console_lookup_default(void);
  334. QemuConsole *qemu_console_lookup_by_index(unsigned int index);
  335. QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head);
  336. QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
  337. uint32_t head, Error **errp);
  338. QEMUCursor *qemu_console_get_cursor(QemuConsole *con);
  339. bool qemu_console_is_visible(QemuConsole *con);
  340. bool qemu_console_is_graphic(QemuConsole *con);
  341. bool qemu_console_is_fixedsize(QemuConsole *con);
  342. bool qemu_console_is_gl_blocked(QemuConsole *con);
  343. char *qemu_console_get_label(QemuConsole *con);
  344. int qemu_console_get_index(QemuConsole *con);
  345. uint32_t qemu_console_get_head(QemuConsole *con);
  346. int qemu_console_get_width(QemuConsole *con, int fallback);
  347. int qemu_console_get_height(QemuConsole *con, int fallback);
  348. /* Return the low-level window id for the console */
  349. int qemu_console_get_window_id(QemuConsole *con);
  350. /* Set the low-level window id for the console */
  351. void qemu_console_set_window_id(QemuConsole *con, int window_id);
  352. void qemu_console_resize(QemuConsole *con, int width, int height);
  353. DisplaySurface *qemu_console_surface(QemuConsole *con);
  354. void coroutine_fn qemu_console_co_wait_update(QemuConsole *con);
  355. int qemu_invalidate_text_consoles(void);
  356. /* console-gl.c */
  357. #ifdef CONFIG_OPENGL
  358. bool console_gl_check_format(DisplayChangeListener *dcl,
  359. pixman_format_code_t format);
  360. void surface_gl_create_texture(QemuGLShader *gls,
  361. DisplaySurface *surface);
  362. void surface_gl_update_texture(QemuGLShader *gls,
  363. DisplaySurface *surface,
  364. int x, int y, int w, int h);
  365. void surface_gl_render_texture(QemuGLShader *gls,
  366. DisplaySurface *surface);
  367. void surface_gl_destroy_texture(QemuGLShader *gls,
  368. DisplaySurface *surface);
  369. void surface_gl_setup_viewport(QemuGLShader *gls,
  370. DisplaySurface *surface,
  371. int ww, int wh);
  372. #endif
  373. typedef struct QemuDisplay QemuDisplay;
  374. struct QemuDisplay {
  375. DisplayType type;
  376. void (*early_init)(DisplayOptions *opts);
  377. void (*init)(DisplayState *ds, DisplayOptions *opts);
  378. const char *vc;
  379. };
  380. void qemu_display_register(QemuDisplay *ui);
  381. bool qemu_display_find_default(DisplayOptions *opts);
  382. void qemu_display_early_init(DisplayOptions *opts);
  383. void qemu_display_init(DisplayState *ds, DisplayOptions *opts);
  384. const char *qemu_display_get_vc(DisplayOptions *opts);
  385. void qemu_display_help(void);
  386. /* vnc.c */
  387. void vnc_display_init(const char *id, Error **errp);
  388. void vnc_display_open(const char *id, Error **errp);
  389. void vnc_display_add_client(const char *id, int csock, bool skipauth);
  390. int vnc_display_password(const char *id, const char *password);
  391. int vnc_display_pw_expire(const char *id, time_t expires);
  392. void vnc_parse(const char *str);
  393. int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp);
  394. bool vnc_display_reload_certs(const char *id, Error **errp);
  395. bool vnc_display_update(DisplayUpdateOptionsVNC *arg, Error **errp);
  396. /* input.c */
  397. int index_from_key(const char *key, size_t key_length);
  398. #ifdef CONFIG_LINUX
  399. /* udmabuf.c */
  400. int udmabuf_fd(void);
  401. #endif
  402. /* util.c */
  403. bool qemu_console_fill_device_address(QemuConsole *con,
  404. char *device_address,
  405. size_t size,
  406. Error **errp);
  407. #endif