2
0

console.h 18 KB

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