xref: /openbmc/qemu/include/hw/input/hid.h (revision 4a09d0bb)
1 #ifndef QEMU_HID_H
2 #define QEMU_HID_H
3 
4 #include "migration/vmstate.h"
5 #include "ui/input.h"
6 
7 #define HID_MOUSE     1
8 #define HID_TABLET    2
9 #define HID_KEYBOARD  3
10 
11 typedef struct HIDPointerEvent {
12     int32_t xdx, ydy; /* relative iff it's a mouse, otherwise absolute */
13     int32_t dz, buttons_state;
14 } HIDPointerEvent;
15 
16 #define QUEUE_LENGTH    16 /* should be enough for a triple-click */
17 #define QUEUE_MASK      (QUEUE_LENGTH-1u)
18 #define QUEUE_INCR(v)   ((v)++, (v) &= QUEUE_MASK)
19 
20 typedef struct HIDState HIDState;
21 typedef void (*HIDEventFunc)(HIDState *s);
22 
23 typedef struct HIDMouseState {
24     HIDPointerEvent queue[QUEUE_LENGTH];
25     int mouse_grabbed;
26 } HIDMouseState;
27 
28 typedef struct HIDKeyboardState {
29     uint32_t keycodes[QUEUE_LENGTH];
30     uint16_t modifiers;
31     uint8_t leds;
32     uint8_t key[16];
33     int32_t keys;
34 } HIDKeyboardState;
35 
36 struct HIDState {
37     union {
38         HIDMouseState ptr;
39         HIDKeyboardState kbd;
40     };
41     uint32_t head; /* index into circular queue */
42     uint32_t n;
43     int kind;
44     int32_t protocol;
45     uint8_t idle;
46     bool idle_pending;
47     QEMUTimer *idle_timer;
48     HIDEventFunc event;
49     QemuInputHandlerState *s;
50 };
51 
52 void hid_init(HIDState *hs, int kind, HIDEventFunc event);
53 void hid_reset(HIDState *hs);
54 void hid_free(HIDState *hs);
55 
56 bool hid_has_events(HIDState *hs);
57 void hid_set_next_idle(HIDState *hs);
58 void hid_pointer_activate(HIDState *hs);
59 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len);
60 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len);
61 int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len);
62 
63 extern const VMStateDescription vmstate_hid_keyboard_device;
64 
65 #define VMSTATE_HID_KEYBOARD_DEVICE(_field, _state) {                \
66     .name       = (stringify(_field)),                               \
67     .size       = sizeof(HIDState),                                  \
68     .vmsd       = &vmstate_hid_keyboard_device,                      \
69     .flags      = VMS_STRUCT,                                        \
70     .offset     = vmstate_offset_value(_state, _field, HIDState),    \
71 }
72 
73 extern const VMStateDescription vmstate_hid_ptr_device;
74 
75 #define VMSTATE_HID_POINTER_DEVICE(_field, _state) {                 \
76     .name       = (stringify(_field)),                               \
77     .size       = sizeof(HIDState),                                  \
78     .vmsd       = &vmstate_hid_ptr_device,                           \
79     .flags      = VMS_STRUCT,                                        \
80     .offset     = vmstate_offset_value(_state, _field, HIDState),    \
81 }
82 
83 
84 #endif /* QEMU_HID_H */
85