]> Git Repo - qemu.git/blob - hw/hid.c
Merge remote-tracking branch 'spice/spice.v40' into staging
[qemu.git] / hw / hid.c
1 /*
2  * QEMU HID devices
3  *
4  * Copyright (c) 2005 Fabrice Bellard
5  * Copyright (c) 2007 OpenMoko, Inc.  ([email protected])
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 #include "hw.h"
26 #include "console.h"
27 #include "qemu-timer.h"
28 #include "hid.h"
29
30 #define HID_USAGE_ERROR_ROLLOVER        0x01
31 #define HID_USAGE_POSTFAIL              0x02
32 #define HID_USAGE_ERROR_UNDEFINED       0x03
33
34 /* Indices are QEMU keycodes, values are from HID Usage Table.  Indices
35  * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d.  */
36 static const uint8_t hid_usage_keys[0x100] = {
37     0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
38     0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
39     0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
40     0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
41     0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
42     0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
43     0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
44     0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
45     0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
46     0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
47     0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
48     0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
49     0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
50     0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
53
54     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57     0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
58     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60     0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
61     0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
63     0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
64     0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
65     0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x00, 0x00,
66     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 };
71
72 bool hid_has_events(HIDState *hs)
73 {
74     return hs->n > 0;
75 }
76
77 void hid_set_next_idle(HIDState *hs, int64_t curtime)
78 {
79     hs->next_idle_clock = curtime + (get_ticks_per_sec() * hs->idle * 4) / 1000;
80 }
81
82 static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
83 {
84     e->xdx = e->ydy = e->dz = 0;
85     e->buttons_state = buttons;
86 }
87
88 static void hid_pointer_event_combine(HIDPointerEvent *e, int xyrel,
89                                       int x1, int y1, int z1) {
90     if (xyrel) {
91         e->xdx += x1;
92         e->ydy += y1;
93     } else {
94         e->xdx = x1;
95         e->ydy = y1;
96         /* Windows drivers do not like the 0/0 position and ignore such
97          * events. */
98         if (!(x1 | y1)) {
99             x1 = 1;
100         }
101     }
102     e->dz += z1;
103 }
104
105 static void hid_pointer_event(void *opaque,
106                               int x1, int y1, int z1, int buttons_state)
107 {
108     HIDState *hs = opaque;
109     unsigned use_slot = (hs->head + hs->n - 1) & QUEUE_MASK;
110     unsigned previous_slot = (use_slot - 1) & QUEUE_MASK;
111
112     /* We combine events where feasible to keep the queue small.  We shouldn't
113      * combine anything with the first event of a particular button state, as
114      * that would change the location of the button state change.  When the
115      * queue is empty, a second event is needed because we don't know if
116      * the first event changed the button state.  */
117     if (hs->n == QUEUE_LENGTH) {
118         /* Queue full.  Discard old button state, combine motion normally.  */
119         hs->ptr.queue[use_slot].buttons_state = buttons_state;
120     } else if (hs->n < 2 ||
121                hs->ptr.queue[use_slot].buttons_state != buttons_state ||
122                hs->ptr.queue[previous_slot].buttons_state !=
123                hs->ptr.queue[use_slot].buttons_state) {
124         /* Cannot or should not combine, so add an empty item to the queue.  */
125         QUEUE_INCR(use_slot);
126         hs->n++;
127         hid_pointer_event_clear(&hs->ptr.queue[use_slot], buttons_state);
128     }
129     hid_pointer_event_combine(&hs->ptr.queue[use_slot],
130                               hs->kind == HID_MOUSE,
131                               x1, y1, z1);
132     hs->event(hs);
133 }
134
135 static void hid_keyboard_event(void *opaque, int keycode)
136 {
137     HIDState *hs = opaque;
138     int slot;
139
140     if (hs->n == QUEUE_LENGTH) {
141         fprintf(stderr, "usb-kbd: warning: key event queue full\n");
142         return;
143     }
144     slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
145     hs->kbd.keycodes[slot] = keycode;
146     hs->event(hs);
147 }
148
149 static void hid_keyboard_process_keycode(HIDState *hs)
150 {
151     uint8_t hid_code, key;
152     int i, keycode, slot;
153
154     if (hs->n == 0) {
155         return;
156     }
157     slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
158     keycode = hs->kbd.keycodes[slot];
159
160     key = keycode & 0x7f;
161     hid_code = hid_usage_keys[key | ((hs->kbd.modifiers >> 1) & (1 << 7))];
162     hs->kbd.modifiers &= ~(1 << 8);
163
164     switch (hid_code) {
165     case 0x00:
166         return;
167
168     case 0xe0:
169         if (hs->kbd.modifiers & (1 << 9)) {
170             hs->kbd.modifiers ^= 3 << 8;
171             return;
172         }
173     case 0xe1 ... 0xe7:
174         if (keycode & (1 << 7)) {
175             hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
176             return;
177         }
178     case 0xe8 ... 0xef:
179         hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
180         return;
181     }
182
183     if (keycode & (1 << 7)) {
184         for (i = hs->kbd.keys - 1; i >= 0; i--) {
185             if (hs->kbd.key[i] == hid_code) {
186                 hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
187                 hs->kbd.key[hs->kbd.keys] = 0x00;
188                 break;
189             }
190         }
191         if (i < 0) {
192             return;
193         }
194     } else {
195         for (i = hs->kbd.keys - 1; i >= 0; i--) {
196             if (hs->kbd.key[i] == hid_code) {
197                 break;
198             }
199         }
200         if (i < 0) {
201             if (hs->kbd.keys < sizeof(hs->kbd.key)) {
202                 hs->kbd.key[hs->kbd.keys++] = hid_code;
203             }
204         } else {
205             return;
206         }
207     }
208 }
209
210 static inline int int_clamp(int val, int vmin, int vmax)
211 {
212     if (val < vmin) {
213         return vmin;
214     } else if (val > vmax) {
215         return vmax;
216     } else {
217         return val;
218     }
219 }
220
221 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
222 {
223     int dx, dy, dz, b, l;
224     int index;
225     HIDPointerEvent *e;
226
227     if (!hs->ptr.mouse_grabbed) {
228         qemu_activate_mouse_event_handler(hs->ptr.eh_entry);
229         hs->ptr.mouse_grabbed = 1;
230     }
231
232     /* When the buffer is empty, return the last event.  Relative
233        movements will all be zero.  */
234     index = (hs->n ? hs->head : hs->head - 1);
235     e = &hs->ptr.queue[index & QUEUE_MASK];
236
237     if (hs->kind == HID_MOUSE) {
238         dx = int_clamp(e->xdx, -127, 127);
239         dy = int_clamp(e->ydy, -127, 127);
240         e->xdx -= dx;
241         e->ydy -= dy;
242     } else {
243         dx = e->xdx;
244         dy = e->ydy;
245     }
246     dz = int_clamp(e->dz, -127, 127);
247     e->dz -= dz;
248
249     b = 0;
250     if (e->buttons_state & MOUSE_EVENT_LBUTTON) {
251         b |= 0x01;
252     }
253     if (e->buttons_state & MOUSE_EVENT_RBUTTON) {
254         b |= 0x02;
255     }
256     if (e->buttons_state & MOUSE_EVENT_MBUTTON) {
257         b |= 0x04;
258     }
259
260     if (hs->n &&
261         !e->dz &&
262         (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
263         /* that deals with this event */
264         QUEUE_INCR(hs->head);
265         hs->n--;
266     }
267
268     /* Appears we have to invert the wheel direction */
269     dz = 0 - dz;
270     l = 0;
271     switch (hs->kind) {
272     case HID_MOUSE:
273         if (len > l) {
274             buf[l++] = b;
275         }
276         if (len > l) {
277             buf[l++] = dx;
278         }
279         if (len > l) {
280             buf[l++] = dy;
281         }
282         if (len > l) {
283             buf[l++] = dz;
284         }
285         break;
286
287     case HID_TABLET:
288         if (len > l) {
289             buf[l++] = b;
290         }
291         if (len > l) {
292             buf[l++] = dx & 0xff;
293         }
294         if (len > l) {
295             buf[l++] = dx >> 8;
296         }
297         if (len > l) {
298             buf[l++] = dy & 0xff;
299         }
300         if (len > l) {
301             buf[l++] = dy >> 8;
302         }
303         if (len > l) {
304             buf[l++] = dz;
305         }
306         break;
307
308     default:
309         abort();
310     }
311
312     return l;
313 }
314
315 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
316 {
317     if (len < 2) {
318         return 0;
319     }
320
321     hid_keyboard_process_keycode(hs);
322
323     buf[0] = hs->kbd.modifiers & 0xff;
324     buf[1] = 0;
325     if (hs->kbd.keys > 6) {
326         memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
327     } else {
328         memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
329     }
330
331     return MIN(8, len);
332 }
333
334 int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
335 {
336     if (len > 0) {
337         int ledstate = 0;
338         /* 0x01: Num Lock LED
339          * 0x02: Caps Lock LED
340          * 0x04: Scroll Lock LED
341          * 0x08: Compose LED
342          * 0x10: Kana LED */
343         hs->kbd.leds = buf[0];
344         if (hs->kbd.leds & 0x04) {
345             ledstate |= QEMU_SCROLL_LOCK_LED;
346         }
347         if (hs->kbd.leds & 0x01) {
348             ledstate |= QEMU_NUM_LOCK_LED;
349         }
350         if (hs->kbd.leds & 0x02) {
351             ledstate |= QEMU_CAPS_LOCK_LED;
352         }
353         kbd_put_ledstate(ledstate);
354     }
355     return 0;
356 }
357
358 void hid_reset(HIDState *hs)
359 {
360     switch (hs->kind) {
361     case HID_KEYBOARD:
362         qemu_add_kbd_event_handler(hid_keyboard_event, hs);
363         memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
364         memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
365         hs->kbd.keys = 0;
366         break;
367     case HID_MOUSE:
368     case HID_TABLET:
369         memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
370         break;
371     }
372     hs->head = 0;
373     hs->n = 0;
374     hs->protocol = 1;
375     hs->idle = 0;
376 }
377
378 void hid_free(HIDState *hs)
379 {
380     switch (hs->kind) {
381     case HID_KEYBOARD:
382         qemu_remove_kbd_event_handler();
383         break;
384     case HID_MOUSE:
385     case HID_TABLET:
386         qemu_remove_mouse_event_handler(hs->ptr.eh_entry);
387         break;
388     }
389 }
390
391 void hid_init(HIDState *hs, int kind, HIDEventFunc event)
392 {
393     hs->kind = kind;
394     hs->event = event;
395
396     if (hs->kind == HID_MOUSE) {
397         hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
398                                                         0, "QEMU HID Mouse");
399     } else if (hs->kind == HID_TABLET) {
400         hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
401                                                         1, "QEMU HID Tablet");
402     }
403 }
This page took 0.049087 seconds and 4 git commands to generate.