Keyboard firmwares for Atmel AVR and Cortex-M
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.

action.c 36KB

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