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