You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 8 година
пре 11 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 & 0x3FFF;
  109. if (cpoint == 11) return KEY_TAB & 0x3FFF;
  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. void usb_keyboard_press_keycode(uint16_t n)
  265. {
  266. uint8_t key, mod, msb, modrestore=0;
  267. KEYCODE_TYPE keycode;
  268. #ifdef DEADKEYS_MASK
  269. KEYCODE_TYPE deadkeycode;
  270. #endif
  271. msb = n >> 8;
  272. if (msb >= 0xC2 && msb <= 0xDF) {
  273. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  274. } else
  275. if (msb == 0x80) {
  276. usb_keyboard_press_key(0, n);
  277. return;
  278. } else
  279. if (msb == 0x40) {
  280. usb_keyboard_press_key(n, 0);
  281. return;
  282. }
  283. keycode = unicode_to_keycode(n);
  284. if (!keycode) return;
  285. #ifdef DEADKEYS_MASK
  286. deadkeycode = deadkey_to_keycode(keycode);
  287. if (deadkeycode) {
  288. modrestore = keyboard_modifier_keys;
  289. if (modrestore) {
  290. keyboard_modifier_keys = 0;
  291. usb_keyboard_send();
  292. }
  293. // TODO: test if operating systems recognize
  294. // deadkey sequences when other keys are held
  295. mod = keycode_to_modifier(deadkeycode);
  296. key = keycode_to_key(deadkeycode);
  297. usb_keyboard_press_key(key, mod);
  298. usb_keyboard_release_key(key, mod);
  299. }
  300. #endif
  301. mod = keycode_to_modifier(keycode);
  302. key = keycode_to_key(keycode);
  303. usb_keyboard_press_key(key, mod | modrestore);
  304. }
  305. void usb_keyboard_release_keycode(uint16_t n)
  306. {
  307. uint8_t key, mod, msb;
  308. msb = n >> 8;
  309. if (msb >= 0xC2 && msb <= 0xDF) {
  310. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  311. } else
  312. if (msb == 0x80) {
  313. usb_keyboard_release_key(0, n);
  314. return;
  315. } else
  316. if (msb == 0x40) {
  317. usb_keyboard_release_key(n, 0);
  318. return;
  319. }
  320. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  321. if (!keycode) return;
  322. mod = keycode_to_modifier(keycode);
  323. key = keycode_to_key(keycode);
  324. usb_keyboard_release_key(key, mod);
  325. }
  326. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
  327. {
  328. int i, send_required = 0;
  329. if (modifier) {
  330. if ((keyboard_modifier_keys & modifier) != modifier) {
  331. keyboard_modifier_keys |= modifier;
  332. send_required = 1;
  333. }
  334. }
  335. if (key) {
  336. for (i=0; i < 6; i++) {
  337. if (keyboard_keys[i] == key) goto end;
  338. }
  339. for (i=0; i < 6; i++) {
  340. if (keyboard_keys[i] == 0) {
  341. keyboard_keys[i] = key;
  342. send_required = 1;
  343. goto end;
  344. }
  345. }
  346. }
  347. end:
  348. if (send_required) usb_keyboard_send();
  349. }
  350. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
  351. {
  352. int i, send_required = 0;
  353. if (modifier) {
  354. if ((keyboard_modifier_keys & modifier) != 0) {
  355. keyboard_modifier_keys &= ~modifier;
  356. send_required = 1;
  357. }
  358. }
  359. if (key) {
  360. for (i=0; i < 6; i++) {
  361. if (keyboard_keys[i] == key) {
  362. keyboard_keys[i] = 0;
  363. send_required = 1;
  364. }
  365. }
  366. }
  367. if (send_required) usb_keyboard_send();
  368. }
  369. void usb_keyboard_release_all(void)
  370. {
  371. uint8_t i, anybits;
  372. anybits = keyboard_modifier_keys;
  373. keyboard_modifier_keys = 0;
  374. anybits |= keyboard_media_keys;
  375. keyboard_media_keys = 0;
  376. for (i=0; i < 6; i++) {
  377. anybits |= keyboard_keys[i];
  378. keyboard_keys[i] = 0;
  379. }
  380. if (anybits) usb_keyboard_send();
  381. }
  382. int usb_keyboard_press(uint8_t key, uint8_t modifier)
  383. {
  384. int r;
  385. keyboard_modifier_keys = modifier;
  386. keyboard_keys[0] = key;
  387. keyboard_keys[1] = 0;
  388. keyboard_keys[2] = 0;
  389. keyboard_keys[3] = 0;
  390. keyboard_keys[4] = 0;
  391. keyboard_keys[5] = 0;
  392. r = usb_keyboard_send();
  393. if (r) return r;
  394. keyboard_modifier_keys = 0;
  395. keyboard_keys[0] = 0;
  396. return usb_keyboard_send();
  397. }
  398. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  399. #define TX_PACKET_LIMIT 4
  400. static uint8_t transmit_previous_timeout=0;
  401. // When the PC isn't listening, how long do we wait before discarding data?
  402. #define TX_TIMEOUT_MSEC 50
  403. #if F_CPU == 192000000
  404. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  405. #elif F_CPU == 180000000
  406. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  407. #elif F_CPU == 168000000
  408. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  409. #elif F_CPU == 144000000
  410. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  411. #elif F_CPU == 120000000
  412. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  413. #elif F_CPU == 96000000
  414. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  415. #elif F_CPU == 72000000
  416. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  417. #elif F_CPU == 48000000
  418. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  419. #elif F_CPU == 24000000
  420. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  421. #endif
  422. // send the contents of keyboard_keys and keyboard_modifier_keys
  423. int usb_keyboard_send(void)
  424. {
  425. #if 0
  426. serial_print("Send:");
  427. serial_phex(keyboard_modifier_keys);
  428. serial_phex(keyboard_keys[0]);
  429. serial_phex(keyboard_keys[1]);
  430. serial_phex(keyboard_keys[2]);
  431. serial_phex(keyboard_keys[3]);
  432. serial_phex(keyboard_keys[4]);
  433. serial_phex(keyboard_keys[5]);
  434. serial_print("\n");
  435. #endif
  436. #if 1
  437. uint32_t wait_count=0;
  438. usb_packet_t *tx_packet;
  439. while (1) {
  440. if (!usb_configuration) {
  441. return -1;
  442. }
  443. if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
  444. tx_packet = usb_malloc();
  445. if (tx_packet) break;
  446. }
  447. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  448. transmit_previous_timeout = 1;
  449. return -1;
  450. }
  451. yield();
  452. }
  453. *(tx_packet->buf) = keyboard_modifier_keys;
  454. *(tx_packet->buf + 1) = keyboard_media_keys;
  455. memcpy(tx_packet->buf + 2, keyboard_keys, 6);
  456. tx_packet->len = 8;
  457. usb_tx(KEYBOARD_ENDPOINT, tx_packet);
  458. #endif
  459. return 0;
  460. }
  461. #endif // F_CPU
  462. #endif // KEYBOARD_INTERFACE