您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

546 行
14KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_keyboard.h"
  32. #include "core_pins.h" // for yield()
  33. #include "keylayouts.h"
  34. //#include "HardwareSerial.h"
  35. #include <string.h> // for memcpy()
  36. #ifdef KEYBOARD_INTERFACE // defined by usb_dev.h -> usb_desc.h
  37. #if F_CPU >= 20000000
  38. // which modifier keys are currently pressed
  39. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  40. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  41. uint8_t keyboard_modifier_keys=0;
  42. // which media keys are currently pressed
  43. uint8_t keyboard_media_keys=0;
  44. // which keys are currently pressed, up to 6 keys may be down at once
  45. uint8_t keyboard_keys[6]={0,0,0,0,0,0};
  46. // protocol setting from the host. We use exactly the same report
  47. // either way, so this variable only stores the setting since we
  48. // are required to be able to report which setting is in use.
  49. uint8_t keyboard_protocol=1;
  50. // the idle configuration, how often we send the report to the
  51. // host (ms * 4) even when it hasn't changed
  52. uint8_t keyboard_idle_config=125;
  53. // count until idle timeout
  54. uint8_t keyboard_idle_count=0;
  55. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  56. volatile uint8_t keyboard_leds=0;
  57. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint);
  58. static void write_key(KEYCODE_TYPE keycode);
  59. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
  60. static uint8_t keycode_to_key(KEYCODE_TYPE keycode);
  61. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier);
  62. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier);
  63. #ifdef DEADKEYS_MASK
  64. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
  65. #endif
  66. // Step #1, decode UTF8 to Unicode code points
  67. //
  68. void usb_keyboard_write(uint8_t c)
  69. {
  70. static int utf8_state=0;
  71. static uint16_t unicode_wchar=0;
  72. if (c < 0x80) {
  73. // single byte encoded, 0x00 to 0x7F
  74. utf8_state = 0;
  75. usb_keyboard_write_unicode(c);
  76. } else if (c < 0xC0) {
  77. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  78. c &= 0x3F;
  79. if (utf8_state == 1) {
  80. utf8_state = 0;
  81. usb_keyboard_write_unicode(unicode_wchar | c);
  82. } else if (utf8_state == 2) {
  83. unicode_wchar |= ((uint16_t)c << 6);
  84. utf8_state = 1;
  85. }
  86. } else if (c < 0xE0) {
  87. // begin 2 byte sequence, 0xC2 to 0xDF
  88. // or illegal 2 byte sequence, 0xC0 to 0xC1
  89. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  90. utf8_state = 1;
  91. } else if (c < 0xF0) {
  92. // begin 3 byte sequence, 0xE0 to 0xEF
  93. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  94. utf8_state = 2;
  95. } else {
  96. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  97. // or illegal, 0xF5 to 0xFF
  98. utf8_state = 255;
  99. }
  100. }
  101. // Step #2: translate Unicode code point to keystroke sequence
  102. //
  103. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
  104. {
  105. // Unicode code points beyond U+FFFF are not supported
  106. // technically this input should probably be called UCS-2
  107. if (cpoint < 32) {
  108. if (cpoint == 10) return KEY_ENTER & KEYCODE_MASK;
  109. if (cpoint == 11) return KEY_TAB & KEYCODE_MASK;
  110. return 0;
  111. }
  112. if (cpoint < 128) {
  113. return keycodes_ascii[cpoint - 0x20];
  114. }
  115. #ifdef ISO_8859_1_A0
  116. if (cpoint >= 0xA0 && cpoint < 0x100) {
  117. return keycodes_iso_8859_1[cpoint - 0xA0];
  118. }
  119. #endif
  120. //#ifdef UNICODE_20AC
  121. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  122. //#endif
  123. #ifdef KEYCODE_EXTRA00
  124. if (cpoint == UNICODE_EXTRA00) return (KEYCODE_EXTRA00) & 0x3FFF;
  125. #endif
  126. #ifdef KEYCODE_EXTRA01
  127. if (cpoint == UNICODE_EXTRA01) return (KEYCODE_EXTRA01) & 0x3FFF;
  128. #endif
  129. #ifdef KEYCODE_EXTRA02
  130. if (cpoint == UNICODE_EXTRA02) return (KEYCODE_EXTRA02) & 0x3FFF;
  131. #endif
  132. #ifdef KEYCODE_EXTRA03
  133. if (cpoint == UNICODE_EXTRA03) return (KEYCODE_EXTRA03) & 0x3FFF;
  134. #endif
  135. #ifdef KEYCODE_EXTRA04
  136. if (cpoint == UNICODE_EXTRA04) return (KEYCODE_EXTRA04) & 0x3FFF;
  137. #endif
  138. #ifdef KEYCODE_EXTRA05
  139. if (cpoint == UNICODE_EXTRA05) return (KEYCODE_EXTRA05) & 0x3FFF;
  140. #endif
  141. #ifdef KEYCODE_EXTRA06
  142. if (cpoint == UNICODE_EXTRA06) return (KEYCODE_EXTRA06) & 0x3FFF;
  143. #endif
  144. #ifdef KEYCODE_EXTRA07
  145. if (cpoint == UNICODE_EXTRA07) return (KEYCODE_EXTRA07) & 0x3FFF;
  146. #endif
  147. #ifdef KEYCODE_EXTRA08
  148. if (cpoint == UNICODE_EXTRA08) return (KEYCODE_EXTRA08) & 0x3FFF;
  149. #endif
  150. #ifdef KEYCODE_EXTRA09
  151. if (cpoint == UNICODE_EXTRA09) return (KEYCODE_EXTRA09) & 0x3FFF;
  152. #endif
  153. #ifdef KEYCODE_EXTRA0A
  154. if (cpoint == UNICODE_EXTRA0A) return (KEYCODE_EXTRA0A) & 0x3FFF;
  155. #endif
  156. return 0;
  157. }
  158. // Step #3: execute keystroke sequence
  159. //
  160. #ifdef DEADKEYS_MASK
  161. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode)
  162. {
  163. keycode &= DEADKEYS_MASK;
  164. if (keycode == 0) return 0;
  165. #ifdef ACUTE_ACCENT_BITS
  166. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  167. #endif
  168. #ifdef CEDILLA_BITS
  169. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  170. #endif
  171. #ifdef CIRCUMFLEX_BITS
  172. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  173. #endif
  174. #ifdef DIAERESIS_BITS
  175. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  176. #endif
  177. #ifdef GRAVE_ACCENT_BITS
  178. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  179. #endif
  180. #ifdef TILDE_BITS
  181. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  182. #endif
  183. #ifdef RING_ABOVE_BITS
  184. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  185. #endif
  186. #ifdef DEGREE_SIGN_BITS
  187. if (keycode == DEGREE_SIGN_BITS) return DEADKEY_DEGREE_SIGN;
  188. #endif
  189. #ifdef CARON_BITS
  190. if (keycode == CARON_BITS) return DEADKEY_CARON;
  191. #endif
  192. #ifdef BREVE_BITS
  193. if (keycode == BREVE_BITS) return DEADKEY_BREVE;
  194. #endif
  195. #ifdef OGONEK_BITS
  196. if (keycode == OGONEK_BITS) return DEADKEY_OGONEK;
  197. #endif
  198. #ifdef DOT_ABOVE_BITS
  199. if (keycode == DOT_ABOVE_BITS) return DEADKEY_DOT_ABOVE;
  200. #endif
  201. #ifdef DOUBLE_ACUTE_BITS
  202. if (keycode == DOUBLE_ACUTE_BITS) return DEADKEY_DOUBLE_ACUTE;
  203. #endif
  204. return 0;
  205. }
  206. #endif
  207. void usb_keyboard_write_unicode(uint16_t cpoint)
  208. {
  209. KEYCODE_TYPE keycode;
  210. keycode = unicode_to_keycode(cpoint);
  211. if (keycode) {
  212. #ifdef DEADKEYS_MASK
  213. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  214. if (deadkeycode) write_key(deadkeycode);
  215. #endif
  216. write_key(keycode);
  217. }
  218. }
  219. // Step #4: do each keystroke
  220. //
  221. static void write_key(KEYCODE_TYPE keycode)
  222. {
  223. /*
  224. uint8_t key, modifier=0;
  225. #ifdef SHIFT_MASK
  226. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  227. #endif
  228. #ifdef ALTGR_MASK
  229. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  230. #endif
  231. #ifdef RCTRL_MASK
  232. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  233. #endif
  234. key = keycode & 0x3F;
  235. #ifdef KEY_NON_US_100
  236. if (key == KEY_NON_US_100) key = 100;
  237. #endif
  238. usb_keyboard_press(key, modifier);
  239. */
  240. usb_keyboard_press(keycode_to_key(keycode), keycode_to_modifier(keycode));
  241. }
  242. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode)
  243. {
  244. uint8_t modifier=0;
  245. #ifdef SHIFT_MASK
  246. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  247. #endif
  248. #ifdef ALTGR_MASK
  249. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  250. #endif
  251. #ifdef RCTRL_MASK
  252. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  253. #endif
  254. return modifier;
  255. }
  256. static uint8_t keycode_to_key(KEYCODE_TYPE keycode)
  257. {
  258. uint8_t key = keycode & 0x3F;
  259. #ifdef KEY_NON_US_100
  260. if (key == KEY_NON_US_100) key = 100;
  261. #endif
  262. return key;
  263. }
  264. // Input can be:
  265. // 32 - 127 ASCII direct (U+0020 to U+007F) <-- uses layout
  266. // 128 - 0xC1FF Unicode direct (U+0080 to U+C1FF) <-- uses layout
  267. // 0xC200 - 0xDFFF Unicode UTF8 packed (U+0080 to U+07FF) <-- uses layout
  268. // 0xE000 - 0xE0FF Modifier key (bitmap, 8 keys, shift/ctrl/alt/gui)
  269. // 0xE200 - 0xE2FF System key (HID usage code, within usage page 1)
  270. // 0xE400 - 0xE7FF Media/Consumer key (HID usage code, within usage page 12)
  271. // 0xF000 - 0xFFFF Normal key (HID usage code, within usage page 7)
  272. void usb_keyboard_press_keycode(uint16_t n)
  273. {
  274. uint8_t key, mod, msb, modrestore=0;
  275. KEYCODE_TYPE keycode;
  276. #ifdef DEADKEYS_MASK
  277. KEYCODE_TYPE deadkeycode;
  278. #endif
  279. msb = n >> 8;
  280. if (msb >= 0xC2) {
  281. if (msb <= 0xDF) {
  282. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  283. } else if (msb == 0xF0) {
  284. usb_keyboard_press_key(n, 0);
  285. return;
  286. } else if (msb == 0xE0) {
  287. usb_keyboard_press_key(0, n);
  288. return;
  289. #ifdef KEYMEDIA_INTERFACE
  290. } else if (msb == 0xE2) {
  291. // TODO: system keys
  292. return;
  293. } else if (msb >= 0xE4 && msb <= 0xE7) {
  294. // TODO: media/consumer keys
  295. return;
  296. #endif
  297. } else {
  298. return;
  299. }
  300. }
  301. keycode = unicode_to_keycode(n);
  302. if (!keycode) return;
  303. #ifdef DEADKEYS_MASK
  304. deadkeycode = deadkey_to_keycode(keycode);
  305. if (deadkeycode) {
  306. modrestore = keyboard_modifier_keys;
  307. if (modrestore) {
  308. keyboard_modifier_keys = 0;
  309. usb_keyboard_send();
  310. }
  311. // TODO: test if operating systems recognize
  312. // deadkey sequences when other keys are held
  313. mod = keycode_to_modifier(deadkeycode);
  314. key = keycode_to_key(deadkeycode);
  315. usb_keyboard_press_key(key, mod);
  316. usb_keyboard_release_key(key, mod);
  317. }
  318. #endif
  319. mod = keycode_to_modifier(keycode);
  320. key = keycode_to_key(keycode);
  321. usb_keyboard_press_key(key, mod | modrestore);
  322. }
  323. void usb_keyboard_release_keycode(uint16_t n)
  324. {
  325. uint8_t key, mod, msb;
  326. msb = n >> 8;
  327. if (msb >= 0xC2) {
  328. if (msb <= 0xDF) {
  329. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  330. } else if (msb == 0xF0) {
  331. usb_keyboard_release_key(n, 0);
  332. return;
  333. } else if (msb == 0xE0) {
  334. usb_keyboard_release_key(0, n);
  335. return;
  336. #ifdef KEYMEDIA_INTERFACE
  337. } else if (msb == 0xE2) {
  338. // TODO: system keys
  339. return;
  340. } else if (msb >= 0xE4 && msb <= 0xE7) {
  341. // TODO: media/consumer keys
  342. return;
  343. #endif
  344. } else {
  345. return;
  346. }
  347. }
  348. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  349. if (!keycode) return;
  350. mod = keycode_to_modifier(keycode);
  351. key = keycode_to_key(keycode);
  352. usb_keyboard_release_key(key, mod);
  353. }
  354. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
  355. {
  356. int i, send_required = 0;
  357. if (modifier) {
  358. if ((keyboard_modifier_keys & modifier) != modifier) {
  359. keyboard_modifier_keys |= modifier;
  360. send_required = 1;
  361. }
  362. }
  363. if (key) {
  364. for (i=0; i < 6; i++) {
  365. if (keyboard_keys[i] == key) goto end;
  366. }
  367. for (i=0; i < 6; i++) {
  368. if (keyboard_keys[i] == 0) {
  369. keyboard_keys[i] = key;
  370. send_required = 1;
  371. goto end;
  372. }
  373. }
  374. }
  375. end:
  376. if (send_required) usb_keyboard_send();
  377. }
  378. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
  379. {
  380. int i, send_required = 0;
  381. if (modifier) {
  382. if ((keyboard_modifier_keys & modifier) != 0) {
  383. keyboard_modifier_keys &= ~modifier;
  384. send_required = 1;
  385. }
  386. }
  387. if (key) {
  388. for (i=0; i < 6; i++) {
  389. if (keyboard_keys[i] == key) {
  390. keyboard_keys[i] = 0;
  391. send_required = 1;
  392. }
  393. }
  394. }
  395. if (send_required) usb_keyboard_send();
  396. }
  397. void usb_keyboard_release_all(void)
  398. {
  399. uint8_t i, anybits;
  400. anybits = keyboard_modifier_keys;
  401. keyboard_modifier_keys = 0;
  402. anybits |= keyboard_media_keys;
  403. keyboard_media_keys = 0;
  404. for (i=0; i < 6; i++) {
  405. anybits |= keyboard_keys[i];
  406. keyboard_keys[i] = 0;
  407. }
  408. if (anybits) usb_keyboard_send();
  409. }
  410. int usb_keyboard_press(uint8_t key, uint8_t modifier)
  411. {
  412. int r;
  413. keyboard_modifier_keys = modifier;
  414. keyboard_keys[0] = key;
  415. keyboard_keys[1] = 0;
  416. keyboard_keys[2] = 0;
  417. keyboard_keys[3] = 0;
  418. keyboard_keys[4] = 0;
  419. keyboard_keys[5] = 0;
  420. r = usb_keyboard_send();
  421. if (r) return r;
  422. keyboard_modifier_keys = 0;
  423. keyboard_keys[0] = 0;
  424. return usb_keyboard_send();
  425. }
  426. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  427. #define TX_PACKET_LIMIT 4
  428. static uint8_t transmit_previous_timeout=0;
  429. // When the PC isn't listening, how long do we wait before discarding data?
  430. #define TX_TIMEOUT_MSEC 50
  431. #if F_CPU == 192000000
  432. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  433. #elif F_CPU == 180000000
  434. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  435. #elif F_CPU == 168000000
  436. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  437. #elif F_CPU == 144000000
  438. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  439. #elif F_CPU == 120000000
  440. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  441. #elif F_CPU == 96000000
  442. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  443. #elif F_CPU == 72000000
  444. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  445. #elif F_CPU == 48000000
  446. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  447. #elif F_CPU == 24000000
  448. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  449. #endif
  450. // send the contents of keyboard_keys and keyboard_modifier_keys
  451. int usb_keyboard_send(void)
  452. {
  453. #if 0
  454. serial_print("Send:");
  455. serial_phex(keyboard_modifier_keys);
  456. serial_phex(keyboard_keys[0]);
  457. serial_phex(keyboard_keys[1]);
  458. serial_phex(keyboard_keys[2]);
  459. serial_phex(keyboard_keys[3]);
  460. serial_phex(keyboard_keys[4]);
  461. serial_phex(keyboard_keys[5]);
  462. serial_print("\n");
  463. #endif
  464. #if 1
  465. uint32_t wait_count=0;
  466. usb_packet_t *tx_packet;
  467. while (1) {
  468. if (!usb_configuration) {
  469. return -1;
  470. }
  471. if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
  472. tx_packet = usb_malloc();
  473. if (tx_packet) break;
  474. }
  475. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  476. transmit_previous_timeout = 1;
  477. return -1;
  478. }
  479. yield();
  480. }
  481. *(tx_packet->buf) = keyboard_modifier_keys;
  482. *(tx_packet->buf + 1) = keyboard_media_keys;
  483. memcpy(tx_packet->buf + 2, keyboard_keys, 6);
  484. tx_packet->len = 8;
  485. usb_tx(KEYBOARD_ENDPOINT, tx_packet);
  486. #endif
  487. return 0;
  488. }
  489. #endif // F_CPU
  490. #endif // KEYBOARD_INTERFACE