Home | History | Annotate | Download | only in ui
      1 #ifndef CONSOLE_H
      2 #define CONSOLE_H
      3 
      4 #include "sysemu/char.h"
      5 #include "qapi/qmp/qdict.h"
      6 #include "qemu/notify.h"
      7 
      8 /* keyboard/mouse support */
      9 
     10 #define MOUSE_EVENT_LBUTTON 0x01
     11 #define MOUSE_EVENT_RBUTTON 0x02
     12 #define MOUSE_EVENT_MBUTTON 0x04
     13 extern int multitouch_enabled;
     14 
     15 /* identical to the ps/2 keyboard bits */
     16 #define QEMU_SCROLL_LOCK_LED (1 << 0)
     17 #define QEMU_NUM_LOCK_LED    (1 << 1)
     18 #define QEMU_CAPS_LOCK_LED   (1 << 2)
     19 
     20 /* in ms */
     21 #ifdef CONFIG_ANDROID
     22 #define GUI_REFRESH_INTERVAL (1000/60)  /* 60 frames/s is better */
     23 #else
     24 #define GUI_REFRESH_INTERVAL 30
     25 #endif
     26 
     27 typedef int QEMUDisplayCloseCallback(void *opaque);
     28 void qemu_set_display_close_handler(QEMUDisplayCloseCallback *cb, void *opaque);
     29 int qemu_run_display_close_handler(void);
     30 
     31 typedef void QEMUPutKBDEvent(void *opaque, int keycode);
     32 typedef void QEMUPutLEDEvent(void *opaque, int ledstate);
     33 typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int buttons_state);
     34 
     35 typedef struct QEMUPutMouseEntry {
     36     QEMUPutMouseEvent *qemu_put_mouse_event;
     37     void *qemu_put_mouse_event_opaque;
     38     int qemu_put_mouse_event_absolute;
     39     char *qemu_put_mouse_event_name;
     40 
     41     int index;
     42 
     43     /* used internally by qemu for handling mice */
     44     QTAILQ_ENTRY(QEMUPutMouseEntry) node;
     45 } QEMUPutMouseEntry;
     46 
     47 typedef struct QEMUPutKBDEntry {
     48     QEMUPutKBDEvent *put_kbd_event;
     49     void *opaque;
     50 
     51     /* used internally by qemu for handling keyboards */
     52     QTAILQ_ENTRY(QEMUPutKBDEntry) next;
     53 } QEMUPutKBDEntry;
     54 
     55 typedef struct QEMUPutLEDEntry {
     56     QEMUPutLEDEvent *put_led;
     57     void *opaque;
     58     QTAILQ_ENTRY(QEMUPutLEDEntry) next;
     59 } QEMUPutLEDEntry;
     60 
     61 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque);
     62 void qemu_remove_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque);
     63 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
     64                                                 void *opaque, int absolute,
     65                                                 const char *name);
     66 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry);
     67 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry);
     68 
     69 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, void *opaque);
     70 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry);
     71 
     72 void kbd_put_keycode(int keycode);
     73 void kbd_put_ledstate(int ledstate);
     74 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
     75 
     76 /* Does the current mouse generate absolute events */
     77 int kbd_mouse_is_absolute(void);
     78 void qemu_add_mouse_mode_change_notifier(Notifier *notify);
     79 void qemu_remove_mouse_mode_change_notifier(Notifier *notify);
     80 
     81 /* Of all the mice, is there one that generates absolute events */
     82 int kbd_mouse_has_absolute(void);
     83 
     84 struct MouseTransformInfo {
     85     /* Touchscreen resolution */
     86     int x;
     87     int y;
     88     /* Calibration values as used/generated by tslib */
     89     int a[7];
     90 };
     91 
     92 void do_info_mice_print(Monitor *mon, const QObject *data);
     93 void do_info_mice(Monitor *mon, QObject **ret_data);
     94 void do_mouse_set(Monitor *mon, const QDict *qdict);
     95 
     96 /* keysym is a unicode code except for special keys (see QEMU_KEY_xxx
     97    constants) */
     98 #define QEMU_KEY_ESC1(c) ((c) | 0xe100)
     99 #define QEMU_KEY_BACKSPACE  0x007f
    100 #define QEMU_KEY_UP         QEMU_KEY_ESC1('A')
    101 #define QEMU_KEY_DOWN       QEMU_KEY_ESC1('B')
    102 #define QEMU_KEY_RIGHT      QEMU_KEY_ESC1('C')
    103 #define QEMU_KEY_LEFT       QEMU_KEY_ESC1('D')
    104 #define QEMU_KEY_HOME       QEMU_KEY_ESC1(1)
    105 #define QEMU_KEY_END        QEMU_KEY_ESC1(4)
    106 #define QEMU_KEY_PAGEUP     QEMU_KEY_ESC1(5)
    107 #define QEMU_KEY_PAGEDOWN   QEMU_KEY_ESC1(6)
    108 #define QEMU_KEY_DELETE     QEMU_KEY_ESC1(3)
    109 
    110 #define QEMU_KEY_CTRL_UP         0xe400
    111 #define QEMU_KEY_CTRL_DOWN       0xe401
    112 #define QEMU_KEY_CTRL_LEFT       0xe402
    113 #define QEMU_KEY_CTRL_RIGHT      0xe403
    114 #define QEMU_KEY_CTRL_HOME       0xe404
    115 #define QEMU_KEY_CTRL_END        0xe405
    116 #define QEMU_KEY_CTRL_PAGEUP     0xe406
    117 #define QEMU_KEY_CTRL_PAGEDOWN   0xe407
    118 
    119 void kbd_put_keysym(int keysym);
    120 
    121 /* consoles */
    122 
    123 #define QEMU_BIG_ENDIAN_FLAG    0x01
    124 #define QEMU_ALLOCATED_FLAG     0x02
    125 #define QEMU_REALPIXELS_FLAG    0x04
    126 
    127 struct PixelFormat {
    128     uint8_t bits_per_pixel;
    129     uint8_t bytes_per_pixel;
    130     uint8_t depth; /* color depth in bits */
    131     uint32_t rmask, gmask, bmask, amask;
    132     uint8_t rshift, gshift, bshift, ashift;
    133     uint8_t rmax, gmax, bmax, amax;
    134     uint8_t rbits, gbits, bbits, abits;
    135 };
    136 
    137 struct DisplaySurface {
    138     uint8_t flags;
    139     int width;
    140     int height;
    141     int linesize;        /* bytes per line */
    142     uint8_t *data;
    143 
    144     struct PixelFormat pf;
    145 };
    146 
    147 /* cursor data format is 32bit RGBA */
    148 typedef struct QEMUCursor {
    149     int                 width, height;
    150     int                 hot_x, hot_y;
    151     int                 refcount;
    152     uint32_t            data[];
    153 } QEMUCursor;
    154 
    155 QEMUCursor *cursor_alloc(int width, int height);
    156 void cursor_get(QEMUCursor *c);
    157 void cursor_put(QEMUCursor *c);
    158 QEMUCursor *cursor_builtin_hidden(void);
    159 QEMUCursor *cursor_builtin_left_ptr(void);
    160 void cursor_print_ascii_art(QEMUCursor *c, const char *prefix);
    161 int cursor_get_mono_bpl(QEMUCursor *c);
    162 void cursor_set_mono(QEMUCursor *c,
    163                      uint32_t foreground, uint32_t background, uint8_t *image,
    164                      int transparent, uint8_t *mask);
    165 void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *mask);
    166 void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask);
    167 
    168 struct DisplayChangeListener {
    169     int idle;
    170     uint64_t gui_timer_interval;
    171 
    172     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
    173     void (*dpy_resize)(struct DisplayState *s);
    174     void (*dpy_setdata)(struct DisplayState *s);
    175     void (*dpy_refresh)(struct DisplayState *s);
    176     void (*dpy_copy)(struct DisplayState *s, int src_x, int src_y,
    177                      int dst_x, int dst_y, int w, int h);
    178     void (*dpy_fill)(struct DisplayState *s, int x, int y,
    179                      int w, int h, uint32_t c);
    180     void (*dpy_text_cursor)(struct DisplayState *s, int x, int y);
    181 #ifdef CONFIG_GLES2
    182     void (*dpy_updatecaption)(void);
    183 #endif
    184 #ifdef CONFIG_SKINNING
    185     void (*dpy_enablezoom)(struct DisplayState *s, int width, int height);
    186     void (*dpy_getresolution)(int *width, int *height);
    187 #endif
    188     struct DisplayChangeListener *next;
    189 };
    190 
    191 #ifdef CONFIG_ANDROID
    192 /* The problem with DisplayChangeListener is that the callbacks can't
    193  * differentiate between different DisplayChangeListeners. Instead of
    194  * modifying the type above, which is going to generate conflicts with
    195  * upstream changes, we define our own listener type here.
    196  */
    197 typedef struct DisplayUpdateListener {
    198     void* opaque;
    199     void (*dpy_update)(void* opaque, int x, int y, int w, int h);
    200     struct DisplayUpdateListener *next;
    201 } DisplayUpdateListener;
    202 #endif
    203 
    204 struct DisplayAllocator {
    205     DisplaySurface* (*create_displaysurface)(int width, int height);
    206     DisplaySurface* (*resize_displaysurface)(DisplaySurface *surface, int width, int height);
    207     void (*free_displaysurface)(DisplaySurface *surface);
    208 };
    209 
    210 struct DisplayState {
    211     struct DisplaySurface *surface;
    212     void *opaque;
    213     struct QEMUTimer *gui_timer;
    214 
    215     struct DisplayAllocator* allocator;
    216     struct DisplayChangeListener* listeners;
    217 #ifdef CONFIG_ANDROID
    218     struct DisplayUpdateListener* update_listeners;
    219 #endif
    220     void (*mouse_set)(int x, int y, int on);
    221     void (*cursor_define)(QEMUCursor *cursor);
    222 
    223     struct DisplayState *next;
    224 };
    225 
    226 void vga_fill_rect (DisplayState *ds, int posx, int posy,
    227     int width, int height, uint32_t color);
    228 void register_displaystate(DisplayState *ds);
    229 DisplayState *get_displaystate(void);
    230 DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp,
    231                                                 int linesize, uint8_t *data);
    232 void qemu_alloc_display(DisplaySurface *surface, int width, int height,
    233                         int linesize, PixelFormat pf, int newflags);
    234 PixelFormat qemu_different_endianness_pixelformat(int bpp);
    235 PixelFormat qemu_default_pixelformat(int bpp);
    236 
    237 DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da);
    238 
    239 static inline DisplaySurface* qemu_create_displaysurface(DisplayState *ds, int width, int height)
    240 {
    241     return ds->allocator->create_displaysurface(width, height);
    242 }
    243 
    244 static inline DisplaySurface* qemu_resize_displaysurface(DisplayState *ds, int width, int height)
    245 {
    246     return ds->allocator->resize_displaysurface(ds->surface, width, height);
    247 }
    248 
    249 static inline void qemu_free_displaysurface(DisplayState *ds)
    250 {
    251     ds->allocator->free_displaysurface(ds->surface);
    252 }
    253 
    254 static inline int is_surface_bgr(DisplaySurface *surface)
    255 {
    256     if (surface->pf.bits_per_pixel == 32 && surface->pf.rshift == 0)
    257         return 1;
    258     else
    259         return 0;
    260 }
    261 
    262 static inline int is_buffer_shared(DisplaySurface *surface)
    263 {
    264     return (!(surface->flags & QEMU_ALLOCATED_FLAG) &&
    265             !(surface->flags & QEMU_REALPIXELS_FLAG));
    266 }
    267 
    268 static inline void register_displaychangelistener(DisplayState *ds, DisplayChangeListener *dcl)
    269 {
    270     dcl->next = ds->listeners;
    271     ds->listeners = dcl;
    272 }
    273 
    274 #ifdef CONFIG_ANDROID
    275 static inline void register_displayupdatelistener(DisplayState *ds, DisplayUpdateListener *dul)
    276 {
    277     dul->next = ds->update_listeners;
    278     ds->update_listeners = dul;
    279 }
    280 
    281 void unregister_displayupdatelistener(DisplayState *ds, DisplayUpdateListener *dul);
    282 #endif
    283 
    284 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
    285 {
    286     struct DisplayChangeListener *dcl = s->listeners;
    287     while (dcl != NULL) {
    288         dcl->dpy_update(s, x, y, w, h);
    289         dcl = dcl->next;
    290     }
    291 #ifdef CONFIG_ANDROID
    292     DisplayUpdateListener* dul = s->update_listeners;
    293     while (dul != NULL) {
    294         dul->dpy_update(dul->opaque, x, y, w, h);
    295         dul = dul->next;
    296     }
    297 #endif
    298 }
    299 
    300 #ifdef CONFIG_GLES2
    301 static inline void dpy_updatecaption(DisplayState *s)
    302 {
    303     struct DisplayChangeListener *dcl = s->listeners;
    304     while (dcl != NULL) {
    305         if(dcl->dpy_updatecaption != NULL)
    306         {
    307             dcl->dpy_updatecaption();
    308         }
    309         dcl = dcl->next;
    310     }
    311 }
    312 #endif
    313 
    314 static inline void dpy_resize(DisplayState *s)
    315 {
    316     struct DisplayChangeListener *dcl = s->listeners;
    317     while (dcl != NULL) {
    318         dcl->dpy_resize(s);
    319         dcl = dcl->next;
    320     }
    321 }
    322 
    323 static inline void dpy_setdata(DisplayState *s)
    324 {
    325     struct DisplayChangeListener *dcl = s->listeners;
    326     while (dcl != NULL) {
    327         if (dcl->dpy_setdata) dcl->dpy_setdata(s);
    328         dcl = dcl->next;
    329     }
    330 }
    331 
    332 static inline void dpy_refresh(DisplayState *s)
    333 {
    334     struct DisplayChangeListener *dcl = s->listeners;
    335     while (dcl != NULL) {
    336         if (dcl->dpy_refresh) dcl->dpy_refresh(s);
    337         dcl = dcl->next;
    338     }
    339 }
    340 
    341 static inline void dpy_copy(struct DisplayState *s, int src_x, int src_y,
    342                              int dst_x, int dst_y, int w, int h) {
    343     struct DisplayChangeListener *dcl = s->listeners;
    344     while (dcl != NULL) {
    345         if (dcl->dpy_copy)
    346             dcl->dpy_copy(s, src_x, src_y, dst_x, dst_y, w, h);
    347         else /* TODO */
    348             dcl->dpy_update(s, dst_x, dst_y, w, h);
    349         dcl = dcl->next;
    350     }
    351 }
    352 
    353 static inline void dpy_fill(struct DisplayState *s, int x, int y,
    354                              int w, int h, uint32_t c) {
    355     struct DisplayChangeListener *dcl = s->listeners;
    356     while (dcl != NULL) {
    357         if (dcl->dpy_fill) dcl->dpy_fill(s, x, y, w, h, c);
    358         dcl = dcl->next;
    359     }
    360 }
    361 
    362 static inline void dpy_cursor(struct DisplayState *s, int x, int y) {
    363     struct DisplayChangeListener *dcl = s->listeners;
    364     while (dcl != NULL) {
    365         if (dcl->dpy_text_cursor) dcl->dpy_text_cursor(s, x, y);
    366         dcl = dcl->next;
    367     }
    368 }
    369 
    370 #ifdef CONFIG_SKINNING
    371 static inline void dpy_enablezoom(struct DisplayState *s, int width, int height)
    372 {
    373     struct DisplayChangeListener *dcl = s->listeners;
    374     while (dcl != NULL) {
    375         if (dcl->dpy_enablezoom) dcl->dpy_enablezoom(s, width, height);
    376         dcl = dcl->next;
    377     }
    378 }
    379 
    380 static inline void dpy_getresolution(struct DisplayState *s, int *width, int *height)
    381 {
    382     struct DisplayChangeListener *dcl = s->listeners;
    383     while (dcl != NULL) {
    384         if (dcl->dpy_getresolution) dcl->dpy_getresolution(width, height);
    385         dcl = dcl->next;
    386     }
    387 }
    388 #endif
    389 
    390 static inline int ds_get_linesize(DisplayState *ds)
    391 {
    392     return ds->surface->linesize;
    393 }
    394 
    395 static inline uint8_t* ds_get_data(DisplayState *ds)
    396 {
    397     return ds->surface->data;
    398 }
    399 
    400 static inline int ds_get_width(DisplayState *ds)
    401 {
    402     return ds->surface->width;
    403 }
    404 
    405 static inline int ds_get_height(DisplayState *ds)
    406 {
    407     return ds->surface->height;
    408 }
    409 
    410 static inline int ds_get_bits_per_pixel(DisplayState *ds)
    411 {
    412     return ds->surface->pf.bits_per_pixel;
    413 }
    414 
    415 static inline int ds_get_bytes_per_pixel(DisplayState *ds)
    416 {
    417     return ds->surface->pf.bytes_per_pixel;
    418 }
    419 
    420 typedef unsigned long console_ch_t;
    421 static inline void console_write_ch(console_ch_t *dest, uint32_t ch)
    422 {
    423     if (!(ch & 0xff))
    424         ch |= ' ';
    425     *dest = ch;
    426 }
    427 
    428 typedef void (*vga_hw_update_ptr)(void *);
    429 typedef void (*vga_hw_invalidate_ptr)(void *);
    430 typedef void (*vga_hw_screen_dump_ptr)(void *, const char *);
    431 typedef void (*vga_hw_text_update_ptr)(void *, console_ch_t *);
    432 
    433 DisplayState *graphic_console_init(vga_hw_update_ptr update,
    434                                    vga_hw_invalidate_ptr invalidate,
    435                                    vga_hw_screen_dump_ptr screen_dump,
    436                                    vga_hw_text_update_ptr text_update,
    437                                    void *opaque);
    438 
    439 void vga_hw_update(void);
    440 void vga_hw_invalidate(void);
    441 void vga_hw_screen_dump(const char *filename);
    442 void vga_hw_text_update(console_ch_t *chardata);
    443 
    444 int is_graphic_console(void);
    445 int is_fixedsize_console(void);
    446 CharDriverState *text_console_init(QemuOpts *opts);
    447 CharDriverState* text_console_init_compat(const char *label, const char *p);
    448 void text_consoles_set_display(DisplayState *ds);
    449 void console_select(unsigned int index);
    450 void console_color_init(DisplayState *ds);
    451 void qemu_console_resize(DisplayState *ds, int width, int height);
    452 void qemu_console_copy(DisplayState *ds, int src_x, int src_y,
    453                        int dst_x, int dst_y, int w, int h);
    454 
    455 /* sdl.c */
    456 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
    457 
    458 /* cocoa.m */
    459 void cocoa_display_init(DisplayState *ds, int full_screen);
    460 
    461 /* vnc.c */
    462 void vnc_display_init(DisplayState *ds);
    463 void vnc_display_close(DisplayState *ds);
    464 int vnc_display_open(DisplayState *ds, const char *display);
    465 int vnc_display_password(DisplayState *ds, const char *password);
    466 void do_info_vnc(Monitor *mon);
    467 char *vnc_display_local_addr(DisplayState *ds);
    468 
    469 /* curses.c */
    470 void curses_display_init(DisplayState *ds, int full_screen);
    471 
    472 #ifdef CONFIG_ANDROID
    473 void android_display_reset(DisplayState* ds, int width, int height, int bitspp);
    474 #endif
    475 
    476 #endif
    477