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 37KB

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