4 * Copyright (c) 2005 Fabrice Bellard
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
27 #include "qemu-timer.h"
30 #define HID_USAGE_ERROR_ROLLOVER 0x01
31 #define HID_USAGE_POSTFAIL 0x02
32 #define HID_USAGE_ERROR_UNDEFINED 0x03
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,
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,
72 bool hid_has_events(HIDState *hs)
77 void hid_set_next_idle(HIDState *hs, int64_t curtime)
79 hs->next_idle_clock = curtime + (get_ticks_per_sec() * hs->idle * 4) / 1000;
82 static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
84 e->xdx = e->ydy = e->dz = 0;
85 e->buttons_state = buttons;
88 static void hid_pointer_event_combine(HIDPointerEvent *e, int xyrel,
89 int x1, int y1, int z1) {
96 /* Windows drivers do not like the 0/0 position and ignore such
105 static void hid_pointer_event(void *opaque,
106 int x1, int y1, int z1, int buttons_state)
108 HIDState *hs = opaque;
109 unsigned use_slot = (hs->head + hs->n - 1) & QUEUE_MASK;
110 unsigned previous_slot = (use_slot - 1) & QUEUE_MASK;
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);
127 hid_pointer_event_clear(&hs->ptr.queue[use_slot], buttons_state);
129 hid_pointer_event_combine(&hs->ptr.queue[use_slot],
130 hs->kind == HID_MOUSE,
135 static void hid_keyboard_event(void *opaque, int keycode)
137 HIDState *hs = opaque;
140 if (hs->n == QUEUE_LENGTH) {
141 fprintf(stderr, "usb-kbd: warning: key event queue full\n");
144 slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
145 hs->kbd.keycodes[slot] = keycode;
149 static void hid_keyboard_process_keycode(HIDState *hs)
151 uint8_t hid_code, key;
152 int i, keycode, slot;
157 slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
158 keycode = hs->kbd.keycodes[slot];
160 key = keycode & 0x7f;
161 hid_code = hid_usage_keys[key | ((hs->kbd.modifiers >> 1) & (1 << 7))];
162 hs->kbd.modifiers &= ~(1 << 8);
169 if (hs->kbd.modifiers & (1 << 9)) {
170 hs->kbd.modifiers ^= 3 << 8;
174 if (keycode & (1 << 7)) {
175 hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
179 hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
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;
195 for (i = hs->kbd.keys - 1; i >= 0; i--) {
196 if (hs->kbd.key[i] == hid_code) {
201 if (hs->kbd.keys < sizeof(hs->kbd.key)) {
202 hs->kbd.key[hs->kbd.keys++] = hid_code;
210 static inline int int_clamp(int val, int vmin, int vmax)
214 } else if (val > vmax) {
221 void hid_pointer_activate(HIDState *hs)
223 if (!hs->ptr.mouse_grabbed) {
224 qemu_activate_mouse_event_handler(hs->ptr.eh_entry);
225 hs->ptr.mouse_grabbed = 1;
229 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
231 int dx, dy, dz, b, l;
235 hid_pointer_activate(hs);
237 /* When the buffer is empty, return the last event. Relative
238 movements will all be zero. */
239 index = (hs->n ? hs->head : hs->head - 1);
240 e = &hs->ptr.queue[index & QUEUE_MASK];
242 if (hs->kind == HID_MOUSE) {
243 dx = int_clamp(e->xdx, -127, 127);
244 dy = int_clamp(e->ydy, -127, 127);
251 dz = int_clamp(e->dz, -127, 127);
255 if (e->buttons_state & MOUSE_EVENT_LBUTTON) {
258 if (e->buttons_state & MOUSE_EVENT_RBUTTON) {
261 if (e->buttons_state & MOUSE_EVENT_MBUTTON) {
267 (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
268 /* that deals with this event */
269 QUEUE_INCR(hs->head);
273 /* Appears we have to invert the wheel direction */
297 buf[l++] = dx & 0xff;
303 buf[l++] = dy & 0xff;
320 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
326 hid_keyboard_process_keycode(hs);
328 buf[0] = hs->kbd.modifiers & 0xff;
330 if (hs->kbd.keys > 6) {
331 memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
333 memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
339 int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
343 /* 0x01: Num Lock LED
344 * 0x02: Caps Lock LED
345 * 0x04: Scroll Lock LED
348 hs->kbd.leds = buf[0];
349 if (hs->kbd.leds & 0x04) {
350 ledstate |= QEMU_SCROLL_LOCK_LED;
352 if (hs->kbd.leds & 0x01) {
353 ledstate |= QEMU_NUM_LOCK_LED;
355 if (hs->kbd.leds & 0x02) {
356 ledstate |= QEMU_CAPS_LOCK_LED;
358 kbd_put_ledstate(ledstate);
363 void hid_reset(HIDState *hs)
367 memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
368 memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
373 memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
382 void hid_free(HIDState *hs)
386 qemu_remove_kbd_event_handler();
390 qemu_remove_mouse_event_handler(hs->ptr.eh_entry);
395 void hid_init(HIDState *hs, int kind, HIDEventFunc event)
400 if (hs->kind == HID_KEYBOARD) {
401 qemu_add_kbd_event_handler(hid_keyboard_event, hs);
402 } else if (hs->kind == HID_MOUSE) {
403 hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
404 0, "QEMU HID Mouse");
405 } else if (hs->kind == HID_TABLET) {
406 hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
407 1, "QEMU HID Tablet");
411 static int hid_post_load(void *opaque, int version_id)
413 HIDState *s = opaque;
416 hid_set_next_idle(s, qemu_get_clock_ns(vm_clock));
421 static const VMStateDescription vmstate_hid_ptr_queue = {
422 .name = "HIDPointerEventQueue",
424 .minimum_version_id = 1,
425 .fields = (VMStateField[]) {
426 VMSTATE_INT32(xdx, HIDPointerEvent),
427 VMSTATE_INT32(ydy, HIDPointerEvent),
428 VMSTATE_INT32(dz, HIDPointerEvent),
429 VMSTATE_INT32(buttons_state, HIDPointerEvent),
430 VMSTATE_END_OF_LIST()
434 const VMStateDescription vmstate_hid_ptr_device = {
435 .name = "HIDPointerDevice",
437 .minimum_version_id = 1,
438 .post_load = hid_post_load,
439 .fields = (VMStateField[]) {
440 VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
441 vmstate_hid_ptr_queue, HIDPointerEvent),
442 VMSTATE_UINT32(head, HIDState),
443 VMSTATE_UINT32(n, HIDState),
444 VMSTATE_INT32(protocol, HIDState),
445 VMSTATE_UINT8(idle, HIDState),
446 VMSTATE_END_OF_LIST(),
450 const VMStateDescription vmstate_hid_keyboard_device = {
451 .name = "HIDKeyboardDevice",
453 .minimum_version_id = 1,
454 .post_load = hid_post_load,
455 .fields = (VMStateField[]) {
456 VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
457 VMSTATE_UINT32(head, HIDState),
458 VMSTATE_UINT32(n, HIDState),
459 VMSTATE_UINT16(kbd.modifiers, HIDState),
460 VMSTATE_UINT8(kbd.leds, HIDState),
461 VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
462 VMSTATE_INT32(kbd.keys, HIDState),
463 VMSTATE_INT32(protocol, HIDState),
464 VMSTATE_UINT8(idle, HIDState),
465 VMSTATE_END_OF_LIST(),