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