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