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

usb_keyboard.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. // which modifier keys are currently pressed
  38. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  39. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  40. uint8_t keyboard_modifier_keys=0;
  41. // which media keys are currently pressed
  42. uint8_t keyboard_media_keys=0;
  43. // which keys are currently pressed, up to 6 keys may be down at once
  44. uint8_t keyboard_keys[6]={0,0,0,0,0,0};
  45. // protocol setting from the host. We use exactly the same report
  46. // either way, so this variable only stores the setting since we
  47. // are required to be able to report which setting is in use.
  48. uint8_t keyboard_protocol=1;
  49. // the idle configuration, how often we send the report to the
  50. // host (ms * 4) even when it hasn't changed
  51. uint8_t keyboard_idle_config=125;
  52. // count until idle timeout
  53. uint8_t keyboard_idle_count=0;
  54. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  55. volatile uint8_t keyboard_leds=0;
  56. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint);
  57. static void write_key(KEYCODE_TYPE keycode);
  58. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
  59. static uint8_t keycode_to_key(KEYCODE_TYPE keycode);
  60. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier);
  61. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier);
  62. #ifdef DEADKEYS_MASK
  63. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
  64. #endif
  65. // Step #1, decode UTF8 to Unicode code points
  66. //
  67. void usb_keyboard_write(uint8_t c)
  68. {
  69. static int utf8_state=0;
  70. static uint16_t unicode_wchar=0;
  71. if (c < 0x80) {
  72. // single byte encoded, 0x00 to 0x7F
  73. utf8_state = 0;
  74. usb_keyboard_write_unicode(c);
  75. } else if (c < 0xC0) {
  76. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  77. c &= 0x3F;
  78. if (utf8_state == 1) {
  79. utf8_state = 0;
  80. usb_keyboard_write_unicode(unicode_wchar | c);
  81. } else if (utf8_state == 2) {
  82. unicode_wchar |= ((uint16_t)c << 6);
  83. utf8_state = 1;
  84. }
  85. } else if (c < 0xE0) {
  86. // begin 2 byte sequence, 0xC2 to 0xDF
  87. // or illegal 2 byte sequence, 0xC0 to 0xC1
  88. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  89. utf8_state = 1;
  90. } else if (c < 0xF0) {
  91. // begin 3 byte sequence, 0xE0 to 0xEF
  92. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  93. utf8_state = 2;
  94. } else {
  95. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  96. // or illegal, 0xF5 to 0xFF
  97. utf8_state = 255;
  98. }
  99. }
  100. // Step #2: translate Unicode code point to keystroke sequence
  101. //
  102. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
  103. {
  104. // Unicode code points beyond U+FFFF are not supported
  105. // technically this input should probably be called UCS-2
  106. if (cpoint < 32) {
  107. if (cpoint == 10) return KEY_ENTER & 0x3FFF;
  108. if (cpoint == 11) return KEY_TAB & 0x3FFF;
  109. return 0;
  110. }
  111. if (cpoint < 128) {
  112. return keycodes_ascii[cpoint - 0x20];
  113. }
  114. #ifdef ISO_8859_1_A0
  115. if (cpoint >= 0xA0 && cpoint < 0x100) {
  116. return keycodes_iso_8859_1[cpoint - 0xA0];
  117. }
  118. #endif
  119. //#ifdef UNICODE_20AC
  120. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  121. //#endif
  122. #ifdef KEYCODE_EXTRA00
  123. if (cpoint == UNICODE_EXTRA00) return KEYCODE_EXTRA00 & 0x3FFF;
  124. #endif
  125. #ifdef KEYCODE_EXTRA01
  126. if (cpoint == UNICODE_EXTRA01) return KEYCODE_EXTRA01 & 0x3FFF;
  127. #endif
  128. #ifdef KEYCODE_EXTRA02
  129. if (cpoint == UNICODE_EXTRA02) return KEYCODE_EXTRA02 & 0x3FFF;
  130. #endif
  131. #ifdef KEYCODE_EXTRA03
  132. if (cpoint == UNICODE_EXTRA03) return KEYCODE_EXTRA03 & 0x3FFF;
  133. #endif
  134. #ifdef KEYCODE_EXTRA04
  135. if (cpoint == UNICODE_EXTRA04) return KEYCODE_EXTRA04 & 0x3FFF;
  136. #endif
  137. #ifdef KEYCODE_EXTRA05
  138. if (cpoint == UNICODE_EXTRA05) return KEYCODE_EXTRA05 & 0x3FFF;
  139. #endif
  140. #ifdef KEYCODE_EXTRA06
  141. if (cpoint == UNICODE_EXTRA06) return KEYCODE_EXTRA06 & 0x3FFF;
  142. #endif
  143. #ifdef KEYCODE_EXTRA07
  144. if (cpoint == UNICODE_EXTRA07) return KEYCODE_EXTRA07 & 0x3FFF;
  145. #endif
  146. #ifdef KEYCODE_EXTRA08
  147. if (cpoint == UNICODE_EXTRA08) return KEYCODE_EXTRA08 & 0x3FFF;
  148. #endif
  149. #ifdef KEYCODE_EXTRA09
  150. if (cpoint == UNICODE_EXTRA09) return KEYCODE_EXTRA09 & 0x3FFF;
  151. #endif
  152. return 0;
  153. }
  154. // Step #3: execute keystroke sequence
  155. //
  156. #ifdef DEADKEYS_MASK
  157. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode)
  158. {
  159. keycode &= DEADKEYS_MASK;
  160. if (keycode == 0) return 0;
  161. #ifdef ACUTE_ACCENT_BITS
  162. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  163. #endif
  164. #ifdef CEDILLA_BITS
  165. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  166. #endif
  167. #ifdef CIRCUMFLEX_BITS
  168. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  169. #endif
  170. #ifdef DIAERESIS_BITS
  171. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  172. #endif
  173. #ifdef GRAVE_ACCENT_BITS
  174. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  175. #endif
  176. #ifdef TILDE_BITS
  177. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  178. #endif
  179. #ifdef RING_ABOVE_BITS
  180. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  181. #endif
  182. return 0;
  183. }
  184. #endif
  185. void usb_keyboard_write_unicode(uint16_t cpoint)
  186. {
  187. KEYCODE_TYPE keycode;
  188. #ifdef DEADKEYS_MASK
  189. KEYCODE_TYPE deadkeycode;
  190. #endif
  191. keycode = unicode_to_keycode(cpoint);
  192. if (keycode) {
  193. #ifdef DEADKEYS_MASK
  194. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  195. if (deadkeycode) write_key(deadkeycode);
  196. #endif
  197. write_key(keycode);
  198. }
  199. }
  200. // Step #4: do each keystroke
  201. //
  202. static void write_key(KEYCODE_TYPE keycode)
  203. {
  204. /*
  205. uint8_t key, modifier=0;
  206. #ifdef SHIFT_MASK
  207. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  208. #endif
  209. #ifdef ALTGR_MASK
  210. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  211. #endif
  212. #ifdef RCTRL_MASK
  213. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  214. #endif
  215. key = keycode & 0x3F;
  216. #ifdef KEY_NON_US_100
  217. if (key == KEY_NON_US_100) key = 100;
  218. #endif
  219. usb_keyboard_press(key, modifier);
  220. */
  221. usb_keyboard_press(keycode_to_key(keycode), keycode_to_modifier(keycode));
  222. }
  223. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode)
  224. {
  225. uint8_t modifier=0;
  226. #ifdef SHIFT_MASK
  227. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  228. #endif
  229. #ifdef ALTGR_MASK
  230. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  231. #endif
  232. #ifdef RCTRL_MASK
  233. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  234. #endif
  235. return modifier;
  236. }
  237. static uint8_t keycode_to_key(KEYCODE_TYPE keycode)
  238. {
  239. uint8_t key = keycode & 0x3F;
  240. #ifdef KEY_NON_US_100
  241. if (key == KEY_NON_US_100) key = 100;
  242. #endif
  243. return key;
  244. }
  245. void usb_keyboard_press_keycode(uint16_t n)
  246. {
  247. uint8_t key, mod, msb, modrestore=0;
  248. KEYCODE_TYPE keycode;
  249. #ifdef DEADKEYS_MASK
  250. KEYCODE_TYPE deadkeycode;
  251. #endif
  252. msb = n >> 8;
  253. if (msb >= 0xC2 && msb <= 0xDF) {
  254. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  255. } else
  256. if (msb == 0x80) {
  257. usb_keyboard_press_key(0, n);
  258. return;
  259. } else
  260. if (msb == 0x40) {
  261. usb_keyboard_press_key(n, 0);
  262. return;
  263. }
  264. keycode = unicode_to_keycode(n);
  265. if (!keycode) return;
  266. #ifdef DEADKEYS_MASK
  267. deadkeycode = deadkey_to_keycode(keycode);
  268. if (deadkeycode) {
  269. modrestore = keyboard_modifier_keys;
  270. if (modrestore) {
  271. keyboard_modifier_keys = 0;
  272. send_now();
  273. }
  274. // TODO: test if operating systems recognize
  275. // deadkey sequences when other keys are held
  276. mod = keycode_to_modifier(deadkeycode);
  277. key = keycode_to_key(deadkeycode);
  278. usb_keyboard_press_key(key, mod);
  279. usb_keyboard_release_key(key, mod);
  280. }
  281. #endif
  282. mod = keycode_to_modifier(keycode);
  283. key = keycode_to_key(keycode);
  284. usb_keyboard_press_key(key, mod | modrestore);
  285. }
  286. void usb_keyboard_release_keycode(uint16_t n)
  287. {
  288. uint8_t key, mod, msb;
  289. msb = n >> 8;
  290. if (msb >= 0xC2 && msb <= 0xDF) {
  291. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  292. } else
  293. if (msb == 0x80) {
  294. usb_keyboard_release_key(0, n);
  295. return;
  296. } else
  297. if (msb == 0x40) {
  298. usb_keyboard_release_key(n, 0);
  299. return;
  300. }
  301. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  302. if (!keycode) return;
  303. mod = keycode_to_modifier(keycode);
  304. key = keycode_to_key(keycode);
  305. usb_keyboard_release_key(key, mod);
  306. }
  307. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
  308. {
  309. int i, send_required = 0;
  310. if (modifier) {
  311. if ((keyboard_modifier_keys & modifier) != modifier) {
  312. keyboard_modifier_keys |= modifier;
  313. send_required = 1;
  314. }
  315. }
  316. if (key) {
  317. for (i=0; i < 6; i++) {
  318. if (keyboard_keys[i] == key) goto end;
  319. }
  320. for (i=0; i < 6; i++) {
  321. if (keyboard_keys[i] == 0) {
  322. keyboard_keys[i] = key;
  323. send_required = 1;
  324. goto end;
  325. }
  326. }
  327. }
  328. end:
  329. if (send_required) usb_keyboard_send();
  330. }
  331. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
  332. {
  333. int i, send_required = 0;
  334. if (modifier) {
  335. if ((keyboard_modifier_keys & modifier) != 0) {
  336. keyboard_modifier_keys &= ~modifier;
  337. send_required = 1;
  338. }
  339. }
  340. if (key) {
  341. for (i=0; i < 6; i++) {
  342. if (keyboard_keys[i] == key) {
  343. keyboard_keys[i] = 0;
  344. send_required = 1;
  345. }
  346. }
  347. }
  348. if (send_required) usb_keyboard_send();
  349. }
  350. void usb_keyboard_release_all(void)
  351. {
  352. uint8_t i, anybits;
  353. anybits = keyboard_modifier_keys;
  354. keyboard_modifier_keys = 0;
  355. anybits |= keyboard_media_keys;
  356. keyboard_media_keys = 0;
  357. for (i=0; i < 6; i++) {
  358. anybits |= keyboard_keys[i];
  359. keyboard_keys[i] = 0;
  360. }
  361. if (anybits) usb_keyboard_send();
  362. }
  363. int usb_keyboard_press(uint8_t key, uint8_t modifier)
  364. {
  365. int r;
  366. keyboard_modifier_keys = modifier;
  367. keyboard_keys[0] = key;
  368. keyboard_keys[1] = 0;
  369. keyboard_keys[2] = 0;
  370. keyboard_keys[3] = 0;
  371. keyboard_keys[4] = 0;
  372. keyboard_keys[5] = 0;
  373. r = usb_keyboard_send();
  374. if (r) return r;
  375. keyboard_modifier_keys = 0;
  376. keyboard_keys[0] = 0;
  377. return usb_keyboard_send();
  378. }
  379. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  380. #define TX_PACKET_LIMIT 4
  381. static uint8_t transmit_previous_timeout=0;
  382. // When the PC isn't listening, how long do we wait before discarding data?
  383. #define TX_TIMEOUT_MSEC 50
  384. #if F_CPU == 96000000
  385. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  386. #elif F_CPU == 48000000
  387. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  388. #elif F_CPU == 24000000
  389. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  390. #endif
  391. // send the contents of keyboard_keys and keyboard_modifier_keys
  392. int usb_keyboard_send(void)
  393. {
  394. #if 0
  395. serial_print("Send:");
  396. serial_phex(keyboard_modifier_keys);
  397. serial_phex(keyboard_keys[0]);
  398. serial_phex(keyboard_keys[1]);
  399. serial_phex(keyboard_keys[2]);
  400. serial_phex(keyboard_keys[3]);
  401. serial_phex(keyboard_keys[4]);
  402. serial_phex(keyboard_keys[5]);
  403. serial_print("\n");
  404. #endif
  405. #if 1
  406. uint32_t wait_count=0;
  407. usb_packet_t *tx_packet;
  408. while (1) {
  409. if (!usb_configuration) {
  410. return -1;
  411. }
  412. if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
  413. tx_packet = usb_malloc();
  414. if (tx_packet) break;
  415. }
  416. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  417. transmit_previous_timeout = 1;
  418. return -1;
  419. }
  420. yield();
  421. }
  422. *(tx_packet->buf) = keyboard_modifier_keys;
  423. *(tx_packet->buf + 1) = keyboard_media_keys;
  424. memcpy(tx_packet->buf + 2, keyboard_keys, 6);
  425. tx_packet->len = 8;
  426. usb_tx(KEYBOARD_ENDPOINT, tx_packet);
  427. #endif
  428. return 0;
  429. }
  430. #endif // KEYBOARD_INTERFACE