hid.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * QEMU HID devices
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com)
  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 "qemu/osdep.h"
  26. #include "ui/console.h"
  27. #include "qemu/timer.h"
  28. #include "hw/input/hid.h"
  29. #include "migration/vmstate.h"
  30. #include "trace.h"
  31. #define HID_USAGE_ERROR_ROLLOVER 0x01
  32. #define HID_USAGE_POSTFAIL 0x02
  33. #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, 0x39, 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, 0x46, 0x00, 0x64, 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. 0x88, 0x00, 0x00, 0x87, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x8a, 0x00, 0x8b, 0x00, 0x89, 0xe7, 0x65,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
  57. 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00,
  59. 0x80, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
  60. 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x48, 0x4a,
  62. 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
  63. 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x66, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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. };
  70. bool hid_has_events(HIDState *hs)
  71. {
  72. return hs->n > 0 || hs->idle_pending;
  73. }
  74. static void hid_idle_timer(void *opaque)
  75. {
  76. HIDState *hs = opaque;
  77. hs->idle_pending = true;
  78. hs->event(hs);
  79. }
  80. static void hid_del_idle_timer(HIDState *hs)
  81. {
  82. if (hs->idle_timer) {
  83. timer_free(hs->idle_timer);
  84. hs->idle_timer = NULL;
  85. }
  86. }
  87. void hid_set_next_idle(HIDState *hs)
  88. {
  89. if (hs->idle) {
  90. uint64_t expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  91. NANOSECONDS_PER_SECOND * hs->idle * 4 / 1000;
  92. if (!hs->idle_timer) {
  93. hs->idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hid_idle_timer, hs);
  94. }
  95. timer_mod_ns(hs->idle_timer, expire_time);
  96. } else {
  97. hid_del_idle_timer(hs);
  98. }
  99. }
  100. static void hid_pointer_event(DeviceState *dev, QemuConsole *src,
  101. InputEvent *evt)
  102. {
  103. static const int bmap[INPUT_BUTTON__MAX] = {
  104. [INPUT_BUTTON_LEFT] = 0x01,
  105. [INPUT_BUTTON_RIGHT] = 0x02,
  106. [INPUT_BUTTON_MIDDLE] = 0x04,
  107. [INPUT_BUTTON_SIDE] = 0x08,
  108. [INPUT_BUTTON_EXTRA] = 0x10,
  109. };
  110. HIDState *hs = (HIDState *)dev;
  111. HIDPointerEvent *e;
  112. InputMoveEvent *move;
  113. InputBtnEvent *btn;
  114. assert(hs->n < QUEUE_LENGTH);
  115. e = &hs->ptr.queue[(hs->head + hs->n) & QUEUE_MASK];
  116. switch (evt->type) {
  117. case INPUT_EVENT_KIND_REL:
  118. move = evt->u.rel.data;
  119. if (move->axis == INPUT_AXIS_X) {
  120. e->xdx += move->value;
  121. } else if (move->axis == INPUT_AXIS_Y) {
  122. e->ydy += move->value;
  123. }
  124. break;
  125. case INPUT_EVENT_KIND_ABS:
  126. move = evt->u.abs.data;
  127. if (move->axis == INPUT_AXIS_X) {
  128. e->xdx = move->value;
  129. } else if (move->axis == INPUT_AXIS_Y) {
  130. e->ydy = move->value;
  131. }
  132. break;
  133. case INPUT_EVENT_KIND_BTN:
  134. btn = evt->u.btn.data;
  135. if (btn->down) {
  136. e->buttons_state |= bmap[btn->button];
  137. if (btn->button == INPUT_BUTTON_WHEEL_UP) {
  138. e->dz--;
  139. } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) {
  140. e->dz++;
  141. }
  142. } else {
  143. e->buttons_state &= ~bmap[btn->button];
  144. }
  145. break;
  146. default:
  147. /* keep gcc happy */
  148. break;
  149. }
  150. }
  151. static void hid_pointer_sync(DeviceState *dev)
  152. {
  153. HIDState *hs = (HIDState *)dev;
  154. HIDPointerEvent *prev, *curr, *next;
  155. bool event_compression = false;
  156. if (hs->n == QUEUE_LENGTH-1) {
  157. /*
  158. * Queue full. We are losing information, but we at least
  159. * keep track of most recent button state.
  160. */
  161. return;
  162. }
  163. prev = &hs->ptr.queue[(hs->head + hs->n - 1) & QUEUE_MASK];
  164. curr = &hs->ptr.queue[(hs->head + hs->n) & QUEUE_MASK];
  165. next = &hs->ptr.queue[(hs->head + hs->n + 1) & QUEUE_MASK];
  166. if (hs->n > 0) {
  167. /*
  168. * No button state change between previous and current event
  169. * (and previous wasn't seen by the guest yet), so there is
  170. * motion information only and we can combine the two event
  171. * into one.
  172. */
  173. if (curr->buttons_state == prev->buttons_state) {
  174. event_compression = true;
  175. }
  176. }
  177. if (event_compression) {
  178. /* add current motion to previous, clear current */
  179. if (hs->kind == HID_MOUSE) {
  180. prev->xdx += curr->xdx;
  181. curr->xdx = 0;
  182. prev->ydy += curr->ydy;
  183. curr->ydy = 0;
  184. } else {
  185. prev->xdx = curr->xdx;
  186. prev->ydy = curr->ydy;
  187. }
  188. prev->dz += curr->dz;
  189. curr->dz = 0;
  190. } else {
  191. /* prepate next (clear rel, copy abs + btns) */
  192. if (hs->kind == HID_MOUSE) {
  193. next->xdx = 0;
  194. next->ydy = 0;
  195. } else {
  196. next->xdx = curr->xdx;
  197. next->ydy = curr->ydy;
  198. }
  199. next->dz = 0;
  200. next->buttons_state = curr->buttons_state;
  201. /* make current guest visible, notify guest */
  202. hs->n++;
  203. hs->event(hs);
  204. }
  205. }
  206. static void hid_keyboard_event(DeviceState *dev, QemuConsole *src,
  207. InputEvent *evt)
  208. {
  209. HIDState *hs = (HIDState *)dev;
  210. int scancodes[3], i, count;
  211. int slot;
  212. InputKeyEvent *key = evt->u.key.data;
  213. count = qemu_input_key_value_to_scancode(key->key,
  214. key->down,
  215. scancodes);
  216. if (hs->n + count > QUEUE_LENGTH) {
  217. trace_hid_kbd_queue_full();
  218. return;
  219. }
  220. for (i = 0; i < count; i++) {
  221. slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
  222. hs->kbd.keycodes[slot] = scancodes[i];
  223. }
  224. hs->event(hs);
  225. }
  226. static void hid_keyboard_process_keycode(HIDState *hs)
  227. {
  228. uint8_t hid_code, index, key;
  229. int i, keycode, slot;
  230. if (hs->n == 0) {
  231. return;
  232. }
  233. slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
  234. keycode = hs->kbd.keycodes[slot];
  235. if (!hs->n) {
  236. trace_hid_kbd_queue_empty();
  237. }
  238. key = keycode & 0x7f;
  239. index = key | ((hs->kbd.modifiers & (1 << 8)) >> 1);
  240. hid_code = hid_usage_keys[index];
  241. hs->kbd.modifiers &= ~(1 << 8);
  242. switch (hid_code) {
  243. case 0x00:
  244. return;
  245. case 0xe0:
  246. assert(key == 0x1d);
  247. if (hs->kbd.modifiers & (1 << 9)) {
  248. /* The hid_codes for the 0xe1/0x1d scancode sequence are 0xe9/0xe0.
  249. * Here we're processing the second hid_code. By dropping bit 9
  250. * and setting bit 8, the scancode after 0x1d will access the
  251. * second half of the table.
  252. */
  253. hs->kbd.modifiers ^= (1 << 8) | (1 << 9);
  254. return;
  255. }
  256. /* fall through to process Ctrl_L */
  257. case 0xe1 ... 0xe7:
  258. /* Ctrl_L/Ctrl_R, Shift_L/Shift_R, Alt_L/Alt_R, Win_L/Win_R.
  259. * Handle releases here, or fall through to process presses.
  260. */
  261. if (keycode & (1 << 7)) {
  262. hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
  263. return;
  264. }
  265. /* fall through */
  266. case 0xe8 ... 0xe9:
  267. /* USB modifiers are just 1 byte long. Bits 8 and 9 of
  268. * hs->kbd.modifiers implement a state machine that detects the
  269. * 0xe0 and 0xe1/0x1d sequences. These bits do not follow the
  270. * usual rules where bit 7 marks released keys; they are cleared
  271. * elsewhere in the function as the state machine dictates.
  272. */
  273. hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
  274. return;
  275. case 0xea ... 0xef:
  276. abort();
  277. default:
  278. break;
  279. }
  280. if (keycode & (1 << 7)) {
  281. for (i = hs->kbd.keys - 1; i >= 0; i--) {
  282. if (hs->kbd.key[i] == hid_code) {
  283. hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
  284. hs->kbd.key[hs->kbd.keys] = 0x00;
  285. break;
  286. }
  287. }
  288. if (i < 0) {
  289. return;
  290. }
  291. } else {
  292. for (i = hs->kbd.keys - 1; i >= 0; i--) {
  293. if (hs->kbd.key[i] == hid_code) {
  294. break;
  295. }
  296. }
  297. if (i < 0) {
  298. if (hs->kbd.keys < sizeof(hs->kbd.key)) {
  299. hs->kbd.key[hs->kbd.keys++] = hid_code;
  300. }
  301. } else {
  302. return;
  303. }
  304. }
  305. }
  306. static inline int int_clamp(int val, int vmin, int vmax)
  307. {
  308. if (val < vmin) {
  309. return vmin;
  310. } else if (val > vmax) {
  311. return vmax;
  312. } else {
  313. return val;
  314. }
  315. }
  316. void hid_pointer_activate(HIDState *hs)
  317. {
  318. if (!hs->ptr.mouse_grabbed) {
  319. qemu_input_handler_activate(hs->s);
  320. hs->ptr.mouse_grabbed = 1;
  321. }
  322. }
  323. int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
  324. {
  325. int dx, dy, dz, l;
  326. int index;
  327. HIDPointerEvent *e;
  328. hs->idle_pending = false;
  329. hid_pointer_activate(hs);
  330. /* When the buffer is empty, return the last event. Relative
  331. movements will all be zero. */
  332. index = (hs->n ? hs->head : hs->head - 1);
  333. e = &hs->ptr.queue[index & QUEUE_MASK];
  334. if (hs->kind == HID_MOUSE) {
  335. dx = int_clamp(e->xdx, -127, 127);
  336. dy = int_clamp(e->ydy, -127, 127);
  337. e->xdx -= dx;
  338. e->ydy -= dy;
  339. } else {
  340. dx = e->xdx;
  341. dy = e->ydy;
  342. }
  343. dz = int_clamp(e->dz, -127, 127);
  344. e->dz -= dz;
  345. if (hs->n &&
  346. !e->dz &&
  347. (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
  348. /* that deals with this event */
  349. QUEUE_INCR(hs->head);
  350. hs->n--;
  351. }
  352. /* Appears we have to invert the wheel direction */
  353. dz = 0 - dz;
  354. l = 0;
  355. switch (hs->kind) {
  356. case HID_MOUSE:
  357. if (len > l) {
  358. buf[l++] = e->buttons_state;
  359. }
  360. if (len > l) {
  361. buf[l++] = dx;
  362. }
  363. if (len > l) {
  364. buf[l++] = dy;
  365. }
  366. if (len > l) {
  367. buf[l++] = dz;
  368. }
  369. break;
  370. case HID_TABLET:
  371. if (len > l) {
  372. buf[l++] = e->buttons_state;
  373. }
  374. if (len > l) {
  375. buf[l++] = dx & 0xff;
  376. }
  377. if (len > l) {
  378. buf[l++] = dx >> 8;
  379. }
  380. if (len > l) {
  381. buf[l++] = dy & 0xff;
  382. }
  383. if (len > l) {
  384. buf[l++] = dy >> 8;
  385. }
  386. if (len > l) {
  387. buf[l++] = dz;
  388. }
  389. break;
  390. default:
  391. abort();
  392. }
  393. return l;
  394. }
  395. int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
  396. {
  397. hs->idle_pending = false;
  398. if (len < 2) {
  399. return 0;
  400. }
  401. hid_keyboard_process_keycode(hs);
  402. buf[0] = hs->kbd.modifiers & 0xff;
  403. buf[1] = 0;
  404. if (hs->kbd.keys > 6) {
  405. memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
  406. } else {
  407. memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
  408. }
  409. return MIN(8, len);
  410. }
  411. int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
  412. {
  413. if (len > 0) {
  414. int ledstate = 0;
  415. /* 0x01: Num Lock LED
  416. * 0x02: Caps Lock LED
  417. * 0x04: Scroll Lock LED
  418. * 0x08: Compose LED
  419. * 0x10: Kana LED */
  420. hs->kbd.leds = buf[0];
  421. if (hs->kbd.leds & 0x04) {
  422. ledstate |= QEMU_SCROLL_LOCK_LED;
  423. }
  424. if (hs->kbd.leds & 0x01) {
  425. ledstate |= QEMU_NUM_LOCK_LED;
  426. }
  427. if (hs->kbd.leds & 0x02) {
  428. ledstate |= QEMU_CAPS_LOCK_LED;
  429. }
  430. kbd_put_ledstate(ledstate);
  431. }
  432. return 0;
  433. }
  434. void hid_reset(HIDState *hs)
  435. {
  436. switch (hs->kind) {
  437. case HID_KEYBOARD:
  438. memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
  439. memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
  440. hs->kbd.keys = 0;
  441. hs->kbd.modifiers = 0;
  442. break;
  443. case HID_MOUSE:
  444. case HID_TABLET:
  445. memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
  446. break;
  447. }
  448. hs->head = 0;
  449. hs->n = 0;
  450. hs->protocol = 1;
  451. hs->idle = 0;
  452. hs->idle_pending = false;
  453. hid_del_idle_timer(hs);
  454. }
  455. void hid_free(HIDState *hs)
  456. {
  457. qemu_input_handler_unregister(hs->s);
  458. hid_del_idle_timer(hs);
  459. }
  460. static QemuInputHandler hid_keyboard_handler = {
  461. .name = "QEMU HID Keyboard",
  462. .mask = INPUT_EVENT_MASK_KEY,
  463. .event = hid_keyboard_event,
  464. };
  465. static QemuInputHandler hid_mouse_handler = {
  466. .name = "QEMU HID Mouse",
  467. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
  468. .event = hid_pointer_event,
  469. .sync = hid_pointer_sync,
  470. };
  471. static QemuInputHandler hid_tablet_handler = {
  472. .name = "QEMU HID Tablet",
  473. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
  474. .event = hid_pointer_event,
  475. .sync = hid_pointer_sync,
  476. };
  477. void hid_init(HIDState *hs, int kind, HIDEventFunc event)
  478. {
  479. hs->kind = kind;
  480. hs->event = event;
  481. if (hs->kind == HID_KEYBOARD) {
  482. hs->s = qemu_input_handler_register((DeviceState *)hs,
  483. &hid_keyboard_handler);
  484. qemu_input_handler_activate(hs->s);
  485. } else if (hs->kind == HID_MOUSE) {
  486. hs->s = qemu_input_handler_register((DeviceState *)hs,
  487. &hid_mouse_handler);
  488. } else if (hs->kind == HID_TABLET) {
  489. hs->s = qemu_input_handler_register((DeviceState *)hs,
  490. &hid_tablet_handler);
  491. }
  492. }
  493. static int hid_post_load(void *opaque, int version_id)
  494. {
  495. HIDState *s = opaque;
  496. hid_set_next_idle(s);
  497. if (s->n == QUEUE_LENGTH && (s->kind == HID_TABLET ||
  498. s->kind == HID_MOUSE)) {
  499. /*
  500. * Handle ptr device migration from old qemu with full queue.
  501. *
  502. * Throw away everything but the last event, so we propagate
  503. * at least the current button state to the guest. Also keep
  504. * current position for the tablet, signal "no motion" for the
  505. * mouse.
  506. */
  507. HIDPointerEvent evt;
  508. evt = s->ptr.queue[(s->head+s->n) & QUEUE_MASK];
  509. if (s->kind == HID_MOUSE) {
  510. evt.xdx = 0;
  511. evt.ydy = 0;
  512. }
  513. s->ptr.queue[0] = evt;
  514. s->head = 0;
  515. s->n = 1;
  516. }
  517. return 0;
  518. }
  519. static const VMStateDescription vmstate_hid_ptr_queue = {
  520. .name = "HIDPointerEventQueue",
  521. .version_id = 1,
  522. .minimum_version_id = 1,
  523. .fields = (VMStateField[]) {
  524. VMSTATE_INT32(xdx, HIDPointerEvent),
  525. VMSTATE_INT32(ydy, HIDPointerEvent),
  526. VMSTATE_INT32(dz, HIDPointerEvent),
  527. VMSTATE_INT32(buttons_state, HIDPointerEvent),
  528. VMSTATE_END_OF_LIST()
  529. }
  530. };
  531. const VMStateDescription vmstate_hid_ptr_device = {
  532. .name = "HIDPointerDevice",
  533. .version_id = 1,
  534. .minimum_version_id = 1,
  535. .post_load = hid_post_load,
  536. .fields = (VMStateField[]) {
  537. VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
  538. vmstate_hid_ptr_queue, HIDPointerEvent),
  539. VMSTATE_UINT32(head, HIDState),
  540. VMSTATE_UINT32(n, HIDState),
  541. VMSTATE_INT32(protocol, HIDState),
  542. VMSTATE_UINT8(idle, HIDState),
  543. VMSTATE_END_OF_LIST(),
  544. }
  545. };
  546. const VMStateDescription vmstate_hid_keyboard_device = {
  547. .name = "HIDKeyboardDevice",
  548. .version_id = 1,
  549. .minimum_version_id = 1,
  550. .post_load = hid_post_load,
  551. .fields = (VMStateField[]) {
  552. VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
  553. VMSTATE_UINT32(head, HIDState),
  554. VMSTATE_UINT32(n, HIDState),
  555. VMSTATE_UINT16(kbd.modifiers, HIDState),
  556. VMSTATE_UINT8(kbd.leds, HIDState),
  557. VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
  558. VMSTATE_INT32(kbd.keys, HIDState),
  559. VMSTATE_INT32(protocol, HIDState),
  560. VMSTATE_UINT8(idle, HIDState),
  561. VMSTATE_END_OF_LIST(),
  562. }
  563. };