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

11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. Copyright 2012,2013 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "host.h"
  15. #include "timer.h"
  16. #include "keymap.h"
  17. #include "keycode.h"
  18. #include "keyboard.h"
  19. #include "mousekey.h"
  20. #include "command.h"
  21. #include "util.h"
  22. #include "debug.h"
  23. #include "led.h"
  24. #include "layer_switch.h"
  25. #include "action_macro.h"
  26. #include "action.h"
  27. static void process_action(keyrecord_t *record);
  28. #ifndef NO_ACTION_TAPPING
  29. static bool process_tapping(keyrecord_t *record);
  30. static void waiting_buffer_scan_tap(void);
  31. #endif
  32. static void debug_event(keyevent_t event);
  33. static void debug_record(keyrecord_t record);
  34. static void debug_action(action_t action);
  35. #ifndef NO_ACTION_TAPPING
  36. static void debug_tapping_key(void);
  37. static void debug_waiting_buffer(void);
  38. #endif
  39. /*
  40. * Tapping
  41. */
  42. /* period of tapping(ms) */
  43. #ifndef TAPPING_TERM
  44. #define TAPPING_TERM 200
  45. #endif
  46. /* tap count needed for toggling a feature */
  47. #ifndef TAPPING_TOGGLE
  48. #define TAPPING_TOGGLE 5
  49. #endif
  50. #ifndef NO_ACTION_TAPPING
  51. /* stores a key event of current tap. */
  52. static keyrecord_t tapping_key = {};
  53. #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
  54. #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  55. #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  56. #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  57. #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
  58. /*
  59. * Waiting buffer
  60. *
  61. * stores key events waiting for settling current tap.
  62. */
  63. #define WAITING_BUFFER_SIZE 8
  64. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  65. /* point to empty cell to enq */
  66. static uint8_t waiting_buffer_head = 0;
  67. /* point to the oldest data cell to deq */
  68. static uint8_t waiting_buffer_tail = 0;
  69. static bool waiting_buffer_enq(keyrecord_t record)
  70. {
  71. if (IS_NOEVENT(record.event)) {
  72. return true;
  73. }
  74. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  75. debug("waiting_buffer_enq: Over flow.\n");
  76. return false;
  77. }
  78. waiting_buffer[waiting_buffer_head] = record;
  79. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  80. debug("waiting_buffer_enq: "); debug_waiting_buffer();
  81. return true;
  82. }
  83. static void waiting_buffer_clear(void)
  84. {
  85. waiting_buffer_head = 0;
  86. waiting_buffer_tail = 0;
  87. }
  88. #if TAPPING_TERM >= 500
  89. static bool waiting_buffer_typed(keyevent_t event)
  90. {
  91. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  92. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. #endif
  99. bool waiting_buffer_has_anykey_pressed(void)
  100. {
  101. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  102. if (waiting_buffer[i].event.pressed) return true;
  103. }
  104. return false;
  105. }
  106. /* Oneshot modifier
  107. *
  108. * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
  109. * Solution: Oneshot modifier have its effect on only one key coming next.
  110. * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
  111. *
  112. * Hold: works as normal modifier.
  113. * Tap: one shot modifier.
  114. * 2 Tap: cancel one shot modifier.
  115. * 5-Tap: toggles enable/disable oneshot feature.
  116. */
  117. static struct {
  118. uint8_t mods;
  119. uint8_t time;
  120. bool ready;
  121. bool disabled;
  122. } oneshot_state;
  123. static void oneshot_start(uint8_t mods, uint16_t time)
  124. {
  125. oneshot_state.mods = mods;
  126. oneshot_state.time = time;
  127. oneshot_state.ready = true;
  128. }
  129. static void oneshot_cancel(void)
  130. {
  131. oneshot_state.mods = 0;
  132. oneshot_state.time = 0;
  133. oneshot_state.ready = false;
  134. }
  135. static void oneshot_toggle(void)
  136. {
  137. oneshot_state.disabled = !oneshot_state.disabled;
  138. }
  139. #endif
  140. void action_exec(keyevent_t event)
  141. {
  142. if (!IS_NOEVENT(event)) {
  143. debug("\n---- action_exec: start -----\n");
  144. debug("EVENT: "); debug_event(event); debug("\n");
  145. }
  146. keyrecord_t record = { .event = event };
  147. #ifndef NO_ACTION_TAPPING
  148. // pre-process on tapping
  149. if (process_tapping(&record)) {
  150. if (!IS_NOEVENT(record.event)) {
  151. debug("processed: "); debug_record(record); debug("\n");
  152. }
  153. } else {
  154. // enqueue
  155. if (!waiting_buffer_enq(record)) {
  156. // clear all in case of overflow.
  157. debug("OVERFLOW: CLEAR ALL STATES\n");
  158. clear_keyboard();
  159. waiting_buffer_clear();
  160. tapping_key = (keyrecord_t){};
  161. }
  162. }
  163. // process waiting_buffer
  164. if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
  165. debug("---- action_exec: process waiting_buffer -----\n");
  166. }
  167. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  168. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  169. debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
  170. debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
  171. } else {
  172. break;
  173. }
  174. }
  175. if (!IS_NOEVENT(event)) {
  176. debug("\n");
  177. }
  178. #else
  179. process_action(&record);
  180. if (!IS_NOEVENT(record.event)) {
  181. debug("processed: "); debug_record(record); debug("\n");
  182. }
  183. #endif
  184. }
  185. static void process_action(keyrecord_t *record)
  186. {
  187. keyevent_t event = record->event;
  188. uint8_t tap_count = record->tap.count;
  189. if (IS_NOEVENT(event)) { return; }
  190. action_t action = layer_switch_get_action(event.key);
  191. debug("ACTION: "); debug_action(action);
  192. debug(" overlays: "); overlay_debug();
  193. debug(" keymaps: "); keymap_debug();
  194. debug(" default_layer: "); debug_dec(default_layer); debug("\n");
  195. switch (action.kind.id) {
  196. /* Key and Mods */
  197. case ACT_LMODS:
  198. case ACT_RMODS:
  199. {
  200. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  201. action.key.mods<<4;
  202. if (event.pressed) {
  203. uint8_t tmp_mods = host_get_mods();
  204. if (mods) {
  205. host_add_mods(mods);
  206. host_send_keyboard_report();
  207. }
  208. register_code(action.key.code);
  209. if (mods && action.key.code) {
  210. host_set_mods(tmp_mods);
  211. host_send_keyboard_report();
  212. }
  213. } else {
  214. if (mods && !action.key.code) {
  215. host_del_mods(mods);
  216. host_send_keyboard_report();
  217. }
  218. unregister_code(action.key.code);
  219. }
  220. }
  221. break;
  222. #ifndef NO_ACTION_TAPPING
  223. case ACT_LMODS_TAP:
  224. case ACT_RMODS_TAP:
  225. {
  226. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  227. action.key.mods<<4;
  228. switch (action.layer.code) {
  229. case 0x00:
  230. // Oneshot modifier
  231. if (event.pressed) {
  232. if (tap_count == 0) {
  233. debug("MODS_TAP: Oneshot: add_mods\n");
  234. add_mods(mods);
  235. }
  236. else if (tap_count == 1) {
  237. debug("MODS_TAP: Oneshot: start\n");
  238. oneshot_start(mods, event.time);
  239. }
  240. else if (tap_count == TAPPING_TOGGLE) {
  241. debug("MODS_TAP: Oneshot: toggle\n");
  242. oneshot_toggle();
  243. }
  244. else {
  245. debug("MODS_TAP: Oneshot: cancel&add_mods\n");
  246. // double tap cancels oneshot and works as normal modifier.
  247. oneshot_cancel();
  248. add_mods(mods);
  249. }
  250. } else {
  251. if (tap_count == 0) {
  252. debug("MODS_TAP: Oneshot: cancel/del_mods\n");
  253. // cancel oneshot on hold
  254. oneshot_cancel();
  255. del_mods(mods);
  256. }
  257. else if (tap_count == 1) {
  258. debug("MODS_TAP: Oneshot: del_mods\n");
  259. // retain Oneshot
  260. del_mods(mods);
  261. }
  262. else {
  263. debug("MODS_TAP: Oneshot: del_mods\n");
  264. // cancel Mods
  265. del_mods(mods);
  266. }
  267. }
  268. break;
  269. default:
  270. if (event.pressed) {
  271. if (tap_count > 0) {
  272. if (waiting_buffer_has_anykey_pressed()) {
  273. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  274. // ad hoc: set 0 to cancel tap
  275. record->tap.count = 0;
  276. add_mods(mods);
  277. } else {
  278. debug("MODS_TAP: Tap: register_code\n");
  279. register_code(action.key.code);
  280. }
  281. } else {
  282. debug("MODS_TAP: No tap: add_mods\n");
  283. add_mods(mods);
  284. }
  285. } else {
  286. if (tap_count > 0) {
  287. debug("MODS_TAP: Tap: unregister_code\n");
  288. unregister_code(action.key.code);
  289. } else {
  290. debug("MODS_TAP: No tap: add_mods\n");
  291. del_mods(mods);
  292. }
  293. }
  294. break;
  295. }
  296. }
  297. break;
  298. #endif
  299. #ifdef EXTRAKEY_ENABLE
  300. /* other HID usage */
  301. case ACT_USAGE:
  302. switch (action.usage.page) {
  303. case PAGE_SYSTEM:
  304. if (event.pressed) {
  305. host_system_send(action.usage.code);
  306. } else {
  307. host_system_send(0);
  308. }
  309. break;
  310. case PAGE_CONSUMER:
  311. if (event.pressed) {
  312. host_consumer_send(action.usage.code);
  313. } else {
  314. host_consumer_send(0);
  315. }
  316. break;
  317. }
  318. break;
  319. #endif
  320. #ifdef MOUSEKEY_ENABLE
  321. /* Mouse key */
  322. case ACT_MOUSEKEY:
  323. if (event.pressed) {
  324. mousekey_on(action.key.code);
  325. mousekey_send();
  326. } else {
  327. mousekey_off(action.key.code);
  328. mousekey_send();
  329. }
  330. break;
  331. #endif
  332. #ifndef NO_ACTION_KEYMAP
  333. case ACT_KEYMAP:
  334. switch (action.layer.code) {
  335. /* Keymap clear */
  336. case OP_RESET:
  337. switch (action.layer.val & 0x03) {
  338. case 0:
  339. // NOTE: reserved
  340. overlay_clear();
  341. keymap_clear();
  342. break;
  343. case ON_PRESS:
  344. if (event.pressed) {
  345. overlay_clear();
  346. keymap_clear();
  347. }
  348. break;
  349. case ON_RELEASE:
  350. if (!event.pressed) {
  351. overlay_clear();
  352. keymap_clear();
  353. }
  354. break;
  355. case ON_BOTH:
  356. overlay_clear();
  357. keymap_clear();
  358. break;
  359. /* NOTE: 4-7 rserved */
  360. }
  361. break;
  362. /* Keymap Reset default layer */
  363. case (OP_RESET | ON_PRESS):
  364. if (event.pressed) {
  365. default_layer_set(action.layer.val);
  366. }
  367. break;
  368. case (OP_RESET | ON_RELEASE):
  369. if (!event.pressed) {
  370. default_layer_set(action.layer.val);
  371. }
  372. break;
  373. case (OP_RESET | ON_BOTH):
  374. default_layer_set(action.layer.val);
  375. break;
  376. /* Keymap Bit invert */
  377. case OP_INV:
  378. /* with tap toggle */
  379. if (event.pressed) {
  380. if (tap_count < TAPPING_TOGGLE) {
  381. debug("KEYMAP_INV: tap toggle(press).\n");
  382. keymap_invert(action.layer.val);
  383. }
  384. } else {
  385. if (tap_count <= TAPPING_TOGGLE) {
  386. debug("KEYMAP_INV: tap toggle(release).\n");
  387. keymap_invert(action.layer.val);
  388. }
  389. }
  390. break;
  391. case (OP_INV | ON_PRESS):
  392. if (event.pressed) {
  393. keymap_invert(action.layer.val);
  394. }
  395. break;
  396. case (OP_INV | ON_RELEASE):
  397. if (!event.pressed) {
  398. keymap_invert(action.layer.val);
  399. }
  400. break;
  401. case (OP_INV | ON_BOTH):
  402. keymap_invert(action.layer.val);
  403. break;
  404. /* Keymap Bit on */
  405. case OP_ON:
  406. if (event.pressed) {
  407. keymap_on(action.layer.val);
  408. } else {
  409. keymap_off(action.layer.val);
  410. }
  411. break;
  412. case (OP_ON | ON_PRESS):
  413. if (event.pressed) {
  414. keymap_on(action.layer.val);
  415. }
  416. break;
  417. case (OP_ON | ON_RELEASE):
  418. if (!event.pressed) {
  419. keymap_on(action.layer.val);
  420. }
  421. break;
  422. case (OP_ON | ON_BOTH):
  423. keymap_on(action.layer.val);
  424. break;
  425. /* Keymap Bit off */
  426. case OP_OFF:
  427. if (event.pressed) {
  428. keymap_off(action.layer.val);
  429. } else {
  430. keymap_on(action.layer.val);
  431. }
  432. break;
  433. case (OP_OFF | ON_PRESS):
  434. if (event.pressed) {
  435. keymap_off(action.layer.val);
  436. }
  437. break;
  438. case (OP_OFF | ON_RELEASE):
  439. if (!event.pressed) {
  440. keymap_off(action.layer.val);
  441. }
  442. break;
  443. case (OP_OFF | ON_BOTH):
  444. keymap_off(action.layer.val);
  445. break;
  446. /* Keymap Bit set */
  447. case OP_SET:
  448. if (event.pressed) {
  449. keymap_set(action.layer.val);
  450. } else {
  451. keymap_clear();
  452. }
  453. break;
  454. case (OP_SET | ON_PRESS):
  455. if (event.pressed) {
  456. keymap_set(action.layer.val);
  457. }
  458. break;
  459. case (OP_SET | ON_RELEASE):
  460. if (!event.pressed) {
  461. keymap_set(action.layer.val);
  462. }
  463. break;
  464. case (OP_SET | ON_BOTH):
  465. keymap_set(action.layer.val);
  466. break;
  467. /* Keymap Bit invert with tap key */
  468. default:
  469. if (event.pressed) {
  470. if (tap_count > 0) {
  471. debug("KEYMAP_TAP_KEY: Tap: register_code\n");
  472. register_code(action.layer.code);
  473. } else {
  474. debug("KEYMAP_TAP_KEY: No tap: On on press\n");
  475. keymap_on(action.layer.val);
  476. }
  477. } else {
  478. if (tap_count > 0) {
  479. debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  480. unregister_code(action.layer.code);
  481. } else {
  482. debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
  483. keymap_off(action.layer.val);
  484. }
  485. }
  486. break;
  487. }
  488. break;
  489. #endif
  490. #ifndef NO_ACTION_OVERLAY
  491. case ACT_OVERLAY:
  492. switch (action.layer.code) {
  493. // Overlay Invert bit4
  494. case OP_INV4 | 0:
  495. if (action.layer.val == 0) {
  496. // NOTE: reserved for future use
  497. overlay_clear();
  498. } else {
  499. overlay_set(overlay_stat ^ action.layer.val);
  500. }
  501. break;
  502. case OP_INV4 | 1:
  503. if (action.layer.val == 0) {
  504. // on pressed
  505. if (event.pressed) overlay_clear();
  506. } else {
  507. overlay_set(overlay_stat ^ action.layer.val<<4);
  508. }
  509. break;
  510. case OP_INV4 | 2:
  511. if (action.layer.val == 0) {
  512. // on released
  513. if (!event.pressed) overlay_clear();
  514. } else {
  515. overlay_set(overlay_stat ^ action.layer.val<<8);
  516. }
  517. break;
  518. case OP_INV4 | 3:
  519. if (action.layer.val == 0) {
  520. // on both
  521. overlay_clear();
  522. } else {
  523. overlay_set(overlay_stat ^ action.layer.val<<12);
  524. }
  525. break;
  526. /* Overlay Bit invert */
  527. case OP_INV:
  528. /* with tap toggle */
  529. if (event.pressed) {
  530. if (tap_count < TAPPING_TOGGLE) {
  531. debug("OVERLAY_INV: tap toggle(press).\n");
  532. overlay_invert(action.layer.val);
  533. }
  534. } else {
  535. if (tap_count <= TAPPING_TOGGLE) {
  536. debug("OVERLAY_INV: tap toggle(release).\n");
  537. overlay_invert(action.layer.val);
  538. }
  539. }
  540. break;
  541. case (OP_INV | ON_PRESS):
  542. if (event.pressed) {
  543. overlay_invert(action.layer.val);
  544. }
  545. break;
  546. case (OP_INV | ON_RELEASE):
  547. if (!event.pressed) {
  548. overlay_invert(action.layer.val);
  549. }
  550. break;
  551. case (OP_INV | ON_BOTH):
  552. overlay_invert(action.layer.val);
  553. break;
  554. /* Overlay Bit on */
  555. case OP_ON:
  556. if (event.pressed) {
  557. overlay_on(action.layer.val);
  558. } else {
  559. overlay_off(action.layer.val);
  560. }
  561. break;
  562. case (OP_ON | ON_PRESS):
  563. if (event.pressed) {
  564. overlay_on(action.layer.val);
  565. }
  566. break;
  567. case (OP_ON | ON_RELEASE):
  568. if (!event.pressed) {
  569. overlay_on(action.layer.val);
  570. }
  571. break;
  572. case (OP_ON | ON_BOTH):
  573. overlay_on(action.layer.val);
  574. break;
  575. /* Overlay Bit off */
  576. case OP_OFF:
  577. if (event.pressed) {
  578. overlay_off(action.layer.val);
  579. } else {
  580. overlay_on(action.layer.val);
  581. }
  582. break;
  583. case (OP_OFF | ON_PRESS):
  584. if (event.pressed) {
  585. overlay_off(action.layer.val);
  586. }
  587. break;
  588. case (OP_OFF | ON_RELEASE):
  589. if (!event.pressed) {
  590. overlay_off(action.layer.val);
  591. }
  592. break;
  593. case (OP_OFF | ON_BOTH):
  594. overlay_off(action.layer.val);
  595. break;
  596. /* Overlay Bit set */
  597. case OP_SET:
  598. if (event.pressed) {
  599. overlay_move(action.layer.val);
  600. } else {
  601. overlay_clear();
  602. }
  603. break;
  604. case (OP_SET | ON_PRESS):
  605. if (event.pressed) {
  606. overlay_move(action.layer.val);
  607. }
  608. break;
  609. case (OP_SET | ON_RELEASE):
  610. if (!event.pressed) {
  611. overlay_move(action.layer.val);
  612. }
  613. break;
  614. case (OP_SET | ON_BOTH):
  615. overlay_move(action.layer.val);
  616. break;
  617. /* Overlay Bit invert with tap key */
  618. default:
  619. if (event.pressed) {
  620. if (tap_count > 0) {
  621. debug("OVERLAY_TAP_KEY: Tap: register_code\n");
  622. register_code(action.layer.code);
  623. } else {
  624. debug("OVERLAY_TAP_KEY: No tap: On on press\n");
  625. overlay_on(action.layer.val);
  626. }
  627. } else {
  628. if (tap_count > 0) {
  629. debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
  630. unregister_code(action.layer.code);
  631. } else {
  632. debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
  633. overlay_off(action.layer.val);
  634. }
  635. }
  636. break;
  637. }
  638. break;
  639. #endif
  640. /* Extentions */
  641. #ifndef NO_ACTION_MACRO
  642. case ACT_MACRO:
  643. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  644. break;
  645. #endif
  646. case ACT_COMMAND:
  647. break;
  648. #ifndef NO_ACTION_FUNCTION
  649. case ACT_FUNCTION:
  650. action_function(record, action.func.id, action.func.opt);
  651. break;
  652. #endif
  653. default:
  654. break;
  655. }
  656. }
  657. #ifndef NO_ACTION_TAPPING
  658. /* Tapping
  659. *
  660. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  661. * (without interfering by typing other key)
  662. */
  663. /* return true when key event is processed or consumed. */
  664. static bool process_tapping(keyrecord_t *keyp)
  665. {
  666. keyevent_t event = keyp->event;
  667. // if tapping
  668. if (IS_TAPPING_PRESSED()) {
  669. if (WITHIN_TAPPING_TERM(event)) {
  670. if (tapping_key.tap.count == 0) {
  671. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  672. // first tap!
  673. debug("Tapping: First tap(0->1).\n");
  674. tapping_key.tap.count = 1;
  675. tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
  676. debug_tapping_key();
  677. process_action(&tapping_key);
  678. // enqueue
  679. keyp->tap = tapping_key.tap;
  680. return false;
  681. }
  682. #if TAPPING_TERM >= 500
  683. /* This can prevent from typing some tap keys in a row at a time. */
  684. else if (!event.pressed && waiting_buffer_typed(event)) {
  685. // other key typed. not tap.
  686. debug("Tapping: End. No tap. Interfered by typing key\n");
  687. process_action(&tapping_key);
  688. tapping_key = (keyrecord_t){};
  689. debug_tapping_key();
  690. // enqueue
  691. return false;
  692. }
  693. #endif
  694. else {
  695. // other key events shall be enq'd till tapping state settles.
  696. return false;
  697. }
  698. }
  699. // tap_count > 0
  700. else {
  701. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  702. debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
  703. keyp->tap = tapping_key.tap;
  704. process_action(keyp);
  705. tapping_key = *keyp;
  706. debug_tapping_key();
  707. return true;
  708. }
  709. else if (is_tap_key(keyp->event.key) && event.pressed) {
  710. if (tapping_key.tap.count > 1) {
  711. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  712. // unregister key
  713. process_action(&(keyrecord_t){
  714. .tap = tapping_key.tap,
  715. .event.key = tapping_key.event.key,
  716. .event.time = event.time,
  717. .event.pressed = false
  718. });
  719. } else {
  720. debug("Tapping: Start while last tap(1).\n");
  721. }
  722. tapping_key = *keyp;
  723. waiting_buffer_scan_tap();
  724. debug_tapping_key();
  725. return true;
  726. }
  727. else {
  728. if (!IS_NOEVENT(keyp->event)) {
  729. debug("Tapping: key event while last tap(>0).\n");
  730. }
  731. process_action(keyp);
  732. return true;
  733. }
  734. }
  735. }
  736. // after TAPPING_TERM
  737. else {
  738. if (tapping_key.tap.count == 0) {
  739. debug("Tapping: End. Timeout. Not tap(0): ");
  740. debug_event(event); debug("\n");
  741. process_action(&tapping_key);
  742. tapping_key = (keyrecord_t){};
  743. debug_tapping_key();
  744. return false;
  745. } else {
  746. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  747. debug("Tapping: End. last timeout tap release(>0).");
  748. keyp->tap = tapping_key.tap;
  749. process_action(keyp);
  750. tapping_key = (keyrecord_t){};
  751. return true;
  752. }
  753. else if (is_tap_key(keyp->event.key) && event.pressed) {
  754. if (tapping_key.tap.count > 1) {
  755. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  756. // unregister key
  757. process_action(&(keyrecord_t){
  758. .tap = tapping_key.tap,
  759. .event.key = tapping_key.event.key,
  760. .event.time = event.time,
  761. .event.pressed = false
  762. });
  763. } else {
  764. debug("Tapping: Start while last timeout tap(1).\n");
  765. }
  766. tapping_key = *keyp;
  767. waiting_buffer_scan_tap();
  768. debug_tapping_key();
  769. return true;
  770. }
  771. else {
  772. if (!IS_NOEVENT(keyp->event)) {
  773. debug("Tapping: key event while last timeout tap(>0).\n");
  774. }
  775. process_action(keyp);
  776. return true;
  777. }
  778. }
  779. }
  780. } else if (IS_TAPPING_RELEASED()) {
  781. if (WITHIN_TAPPING_TERM(event)) {
  782. if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  783. // sequential tap.
  784. keyp->tap = tapping_key.tap;
  785. keyp->tap.count += 1;
  786. debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
  787. process_action(keyp);
  788. tapping_key = *keyp;
  789. debug_tapping_key();
  790. return true;
  791. } else if (event.pressed && is_tap_key(event.key)) {
  792. // Sequential tap can be interfered with other tap key.
  793. debug("Tapping: Start with interfering other tap.\n");
  794. tapping_key = *keyp;
  795. waiting_buffer_scan_tap();
  796. debug_tapping_key();
  797. return true;
  798. } else {
  799. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  800. process_action(keyp);
  801. return true;
  802. }
  803. } else {
  804. // timeout. no sequential tap.
  805. debug("Tapping: End(Timeout after releasing last tap): ");
  806. debug_event(event); debug("\n");
  807. tapping_key = (keyrecord_t){};
  808. debug_tapping_key();
  809. return false;
  810. }
  811. }
  812. // not tapping satate
  813. else {
  814. if (event.pressed && is_tap_key(event.key)) {
  815. debug("Tapping: Start(Press tap key).\n");
  816. tapping_key = *keyp;
  817. waiting_buffer_scan_tap();
  818. debug_tapping_key();
  819. return true;
  820. } else {
  821. process_action(keyp);
  822. return true;
  823. }
  824. }
  825. }
  826. /* scan buffer for tapping */
  827. static void waiting_buffer_scan_tap(void)
  828. {
  829. // tapping already is settled
  830. if (tapping_key.tap.count > 0) return;
  831. // invalid state: tapping_key released && tap.count == 0
  832. if (!tapping_key.event.pressed) return;
  833. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  834. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  835. !waiting_buffer[i].event.pressed &&
  836. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  837. tapping_key.tap.count = 1;
  838. waiting_buffer[i].tap.count = 1;
  839. process_action(&tapping_key);
  840. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  841. debug_waiting_buffer();
  842. return;
  843. }
  844. }
  845. }
  846. #endif
  847. /*
  848. * Utilities for actions.
  849. */
  850. void register_code(uint8_t code)
  851. {
  852. if (code == KC_NO) {
  853. return;
  854. }
  855. #ifdef CAPSLOCK_LOCKING_ENABLE
  856. else if (KC_LOCKING_CAPS == code) {
  857. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  858. // Resync: ignore if caps lock already is on
  859. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  860. #endif
  861. host_add_key(KC_CAPSLOCK);
  862. host_send_keyboard_report();
  863. host_del_key(KC_CAPSLOCK);
  864. host_send_keyboard_report();
  865. }
  866. #endif
  867. else if IS_KEY(code) {
  868. // TODO: should push command_proc out of this block?
  869. if (command_proc(code)) return;
  870. #ifndef NO_ACTION_TAPPING
  871. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  872. uint8_t tmp_mods = host_get_mods();
  873. host_add_mods(oneshot_state.mods);
  874. host_add_key(code);
  875. host_send_keyboard_report();
  876. host_set_mods(tmp_mods);
  877. oneshot_state.ready = false;
  878. } else
  879. #endif
  880. {
  881. host_add_key(code);
  882. host_send_keyboard_report();
  883. }
  884. }
  885. else if IS_MOD(code) {
  886. host_add_mods(MOD_BIT(code));
  887. host_send_keyboard_report();
  888. }
  889. }
  890. void unregister_code(uint8_t code)
  891. {
  892. if (code == KC_NO) {
  893. return;
  894. }
  895. #ifdef CAPSLOCK_LOCKING_ENABLE
  896. else if (KC_LOCKING_CAPS == code) {
  897. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  898. // Resync: ignore if caps lock already is off
  899. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  900. #endif
  901. host_add_key(KC_CAPSLOCK);
  902. host_send_keyboard_report();
  903. host_del_key(KC_CAPSLOCK);
  904. host_send_keyboard_report();
  905. }
  906. #endif
  907. else if IS_KEY(code) {
  908. host_del_key(code);
  909. host_send_keyboard_report();
  910. }
  911. else if IS_MOD(code) {
  912. host_del_mods(MOD_BIT(code));
  913. host_send_keyboard_report();
  914. }
  915. }
  916. void add_mods(uint8_t mods)
  917. {
  918. if (mods) {
  919. host_add_mods(mods);
  920. host_send_keyboard_report();
  921. }
  922. }
  923. void del_mods(uint8_t mods)
  924. {
  925. if (mods) {
  926. host_del_mods(mods);
  927. host_send_keyboard_report();
  928. }
  929. }
  930. void set_mods(uint8_t mods)
  931. {
  932. host_set_mods(mods);
  933. host_send_keyboard_report();
  934. }
  935. void clear_keyboard(void)
  936. {
  937. host_clear_mods();
  938. clear_keyboard_but_mods();
  939. }
  940. void clear_keyboard_but_mods(void)
  941. {
  942. host_clear_keys();
  943. host_send_keyboard_report();
  944. #ifdef MOUSEKEY_ENABLE
  945. mousekey_clear();
  946. mousekey_send();
  947. #endif
  948. #ifdef EXTRAKEY_ENABLE
  949. host_system_send(0);
  950. host_consumer_send(0);
  951. #endif
  952. }
  953. bool sending_anykey(void)
  954. {
  955. return (host_has_anykey() || host_mouse_in_use() ||
  956. host_last_sysytem_report() || host_last_consumer_report());
  957. }
  958. bool is_tap_key(key_t key)
  959. {
  960. action_t action = layer_switch_get_action(key);
  961. switch (action.kind.id) {
  962. case ACT_LMODS_TAP:
  963. case ACT_RMODS_TAP:
  964. return true;
  965. case ACT_KEYMAP:
  966. case ACT_OVERLAY:
  967. switch (action.layer.code) {
  968. case 0x04 ... 0xEF: /* tap key */
  969. case OP_INV:
  970. return true;
  971. default:
  972. return false;
  973. }
  974. case ACT_MACRO:
  975. case ACT_FUNCTION:
  976. if (action.func.opt & FUNC_TAP) { return true; }
  977. return false;
  978. }
  979. return false;
  980. }
  981. /*
  982. * debug print
  983. */
  984. static void debug_event(keyevent_t event)
  985. {
  986. debug_hex16((event.key.row<<8) | event.key.col);
  987. if (event.pressed) debug("d("); else debug("u(");
  988. debug_dec(event.time); debug(")");
  989. }
  990. static void debug_record(keyrecord_t record)
  991. {
  992. debug_event(record.event); debug(":"); debug_dec(record.tap.count);
  993. if (record.tap.interrupted) debug("-");
  994. }
  995. static void debug_action(action_t action)
  996. {
  997. switch (action.kind.id) {
  998. case ACT_LMODS: debug("ACT_LMODS"); break;
  999. case ACT_RMODS: debug("ACT_RMODS"); break;
  1000. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  1001. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  1002. case ACT_USAGE: debug("ACT_USAGE"); break;
  1003. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  1004. case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
  1005. case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
  1006. case ACT_MACRO: debug("ACT_MACRO"); break;
  1007. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  1008. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  1009. default: debug("UNKNOWN"); break;
  1010. }
  1011. debug("[");
  1012. debug_hex4(action.kind.param>>8);
  1013. debug(":");
  1014. debug_hex8(action.kind.param & 0xff);
  1015. debug("]");
  1016. }
  1017. #ifndef NO_ACTION_TAPPING
  1018. static void debug_tapping_key(void)
  1019. {
  1020. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  1021. }
  1022. static void debug_waiting_buffer(void)
  1023. {
  1024. debug("{ ");
  1025. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  1026. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  1027. }
  1028. debug("}\n");
  1029. }
  1030. #endif