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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. case ACT_KEYMAP:
  333. switch (action.layer.code) {
  334. /* Keymap clear */
  335. case OP_RESET:
  336. switch (action.layer.val & 0x03) {
  337. case 0:
  338. // NOTE: reserved
  339. overlay_clear();
  340. keymap_clear();
  341. break;
  342. case ON_PRESS:
  343. if (event.pressed) {
  344. overlay_clear();
  345. keymap_clear();
  346. }
  347. break;
  348. case ON_RELEASE:
  349. if (!event.pressed) {
  350. overlay_clear();
  351. keymap_clear();
  352. }
  353. break;
  354. case ON_BOTH:
  355. overlay_clear();
  356. keymap_clear();
  357. break;
  358. /* NOTE: 4-7 rserved */
  359. }
  360. break;
  361. /* Keymap Reset default layer */
  362. case (OP_RESET | ON_PRESS):
  363. if (event.pressed) {
  364. default_layer_set(action.layer.val);
  365. }
  366. break;
  367. case (OP_RESET | ON_RELEASE):
  368. if (!event.pressed) {
  369. default_layer_set(action.layer.val);
  370. }
  371. break;
  372. case (OP_RESET | ON_BOTH):
  373. default_layer_set(action.layer.val);
  374. break;
  375. /* Keymap Bit invert */
  376. case OP_INV:
  377. /* with tap toggle */
  378. if (event.pressed) {
  379. if (tap_count < TAPPING_TOGGLE) {
  380. debug("KEYMAP_INV: tap toggle(press).\n");
  381. keymap_invert(action.layer.val);
  382. }
  383. } else {
  384. if (tap_count <= TAPPING_TOGGLE) {
  385. debug("KEYMAP_INV: tap toggle(release).\n");
  386. keymap_invert(action.layer.val);
  387. }
  388. }
  389. break;
  390. case (OP_INV | ON_PRESS):
  391. if (event.pressed) {
  392. keymap_invert(action.layer.val);
  393. }
  394. break;
  395. case (OP_INV | ON_RELEASE):
  396. if (!event.pressed) {
  397. keymap_invert(action.layer.val);
  398. }
  399. break;
  400. case (OP_INV | ON_BOTH):
  401. keymap_invert(action.layer.val);
  402. break;
  403. /* Keymap Bit on */
  404. case OP_ON:
  405. if (event.pressed) {
  406. keymap_on(action.layer.val);
  407. } else {
  408. keymap_off(action.layer.val);
  409. }
  410. break;
  411. case (OP_ON | ON_PRESS):
  412. if (event.pressed) {
  413. keymap_on(action.layer.val);
  414. }
  415. break;
  416. case (OP_ON | ON_RELEASE):
  417. if (!event.pressed) {
  418. keymap_on(action.layer.val);
  419. }
  420. break;
  421. case (OP_ON | ON_BOTH):
  422. keymap_on(action.layer.val);
  423. break;
  424. /* Keymap Bit off */
  425. case OP_OFF:
  426. if (event.pressed) {
  427. keymap_off(action.layer.val);
  428. } else {
  429. keymap_on(action.layer.val);
  430. }
  431. break;
  432. case (OP_OFF | ON_PRESS):
  433. if (event.pressed) {
  434. keymap_off(action.layer.val);
  435. }
  436. break;
  437. case (OP_OFF | ON_RELEASE):
  438. if (!event.pressed) {
  439. keymap_off(action.layer.val);
  440. }
  441. break;
  442. case (OP_OFF | ON_BOTH):
  443. keymap_off(action.layer.val);
  444. break;
  445. /* Keymap Bit set */
  446. case OP_SET:
  447. if (event.pressed) {
  448. keymap_set(action.layer.val);
  449. } else {
  450. keymap_clear();
  451. }
  452. break;
  453. case (OP_SET | ON_PRESS):
  454. if (event.pressed) {
  455. keymap_set(action.layer.val);
  456. }
  457. break;
  458. case (OP_SET | ON_RELEASE):
  459. if (!event.pressed) {
  460. keymap_set(action.layer.val);
  461. }
  462. break;
  463. case (OP_SET | ON_BOTH):
  464. keymap_set(action.layer.val);
  465. break;
  466. /* Keymap Bit invert with tap key */
  467. default:
  468. if (event.pressed) {
  469. if (tap_count > 0) {
  470. debug("KEYMAP_TAP_KEY: Tap: register_code\n");
  471. register_code(action.layer.code);
  472. } else {
  473. debug("KEYMAP_TAP_KEY: No tap: On on press\n");
  474. keymap_on(action.layer.val);
  475. }
  476. } else {
  477. if (tap_count > 0) {
  478. debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  479. unregister_code(action.layer.code);
  480. } else {
  481. debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
  482. keymap_off(action.layer.val);
  483. }
  484. }
  485. break;
  486. }
  487. break;
  488. #ifndef NO_ACTION_OVERLAY
  489. case ACT_OVERLAY:
  490. switch (action.layer.code) {
  491. // Overlay Invert bit4
  492. case OP_INV4 | 0:
  493. if (action.layer.val == 0) {
  494. // NOTE: reserved for future use
  495. overlay_clear();
  496. } else {
  497. overlay_set(overlay_stat ^ action.layer.val);
  498. }
  499. break;
  500. case OP_INV4 | 1:
  501. if (action.layer.val == 0) {
  502. // on pressed
  503. if (event.pressed) overlay_clear();
  504. } else {
  505. overlay_set(overlay_stat ^ action.layer.val<<4);
  506. }
  507. break;
  508. case OP_INV4 | 2:
  509. if (action.layer.val == 0) {
  510. // on released
  511. if (!event.pressed) overlay_clear();
  512. } else {
  513. overlay_set(overlay_stat ^ action.layer.val<<8);
  514. }
  515. break;
  516. case OP_INV4 | 3:
  517. if (action.layer.val == 0) {
  518. // on both
  519. overlay_clear();
  520. } else {
  521. overlay_set(overlay_stat ^ action.layer.val<<12);
  522. }
  523. break;
  524. /* Overlay Bit invert */
  525. case OP_INV:
  526. /* with tap toggle */
  527. if (event.pressed) {
  528. if (tap_count < TAPPING_TOGGLE) {
  529. debug("OVERLAY_INV: tap toggle(press).\n");
  530. overlay_invert(action.layer.val);
  531. }
  532. } else {
  533. if (tap_count <= TAPPING_TOGGLE) {
  534. debug("OVERLAY_INV: tap toggle(release).\n");
  535. overlay_invert(action.layer.val);
  536. }
  537. }
  538. break;
  539. case (OP_INV | ON_PRESS):
  540. if (event.pressed) {
  541. overlay_invert(action.layer.val);
  542. }
  543. break;
  544. case (OP_INV | ON_RELEASE):
  545. if (!event.pressed) {
  546. overlay_invert(action.layer.val);
  547. }
  548. break;
  549. case (OP_INV | ON_BOTH):
  550. overlay_invert(action.layer.val);
  551. break;
  552. /* Overlay Bit on */
  553. case OP_ON:
  554. if (event.pressed) {
  555. overlay_on(action.layer.val);
  556. } else {
  557. overlay_off(action.layer.val);
  558. }
  559. break;
  560. case (OP_ON | ON_PRESS):
  561. if (event.pressed) {
  562. overlay_on(action.layer.val);
  563. }
  564. break;
  565. case (OP_ON | ON_RELEASE):
  566. if (!event.pressed) {
  567. overlay_on(action.layer.val);
  568. }
  569. break;
  570. case (OP_ON | ON_BOTH):
  571. overlay_on(action.layer.val);
  572. break;
  573. /* Overlay Bit off */
  574. case OP_OFF:
  575. if (event.pressed) {
  576. overlay_off(action.layer.val);
  577. } else {
  578. overlay_on(action.layer.val);
  579. }
  580. break;
  581. case (OP_OFF | ON_PRESS):
  582. if (event.pressed) {
  583. overlay_off(action.layer.val);
  584. }
  585. break;
  586. case (OP_OFF | ON_RELEASE):
  587. if (!event.pressed) {
  588. overlay_off(action.layer.val);
  589. }
  590. break;
  591. case (OP_OFF | ON_BOTH):
  592. overlay_off(action.layer.val);
  593. break;
  594. /* Overlay Bit set */
  595. case OP_SET:
  596. if (event.pressed) {
  597. overlay_move(action.layer.val);
  598. } else {
  599. overlay_clear();
  600. }
  601. break;
  602. case (OP_SET | ON_PRESS):
  603. if (event.pressed) {
  604. overlay_move(action.layer.val);
  605. }
  606. break;
  607. case (OP_SET | ON_RELEASE):
  608. if (!event.pressed) {
  609. overlay_move(action.layer.val);
  610. }
  611. break;
  612. case (OP_SET | ON_BOTH):
  613. overlay_move(action.layer.val);
  614. break;
  615. /* Overlay Bit invert with tap key */
  616. default:
  617. if (event.pressed) {
  618. if (tap_count > 0) {
  619. debug("OVERLAY_TAP_KEY: Tap: register_code\n");
  620. register_code(action.layer.code);
  621. } else {
  622. debug("OVERLAY_TAP_KEY: No tap: On on press\n");
  623. overlay_on(action.layer.val);
  624. }
  625. } else {
  626. if (tap_count > 0) {
  627. debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
  628. unregister_code(action.layer.code);
  629. } else {
  630. debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
  631. overlay_off(action.layer.val);
  632. }
  633. }
  634. break;
  635. }
  636. break;
  637. #endif
  638. /* Extentions */
  639. #ifndef NO_ACTION_MACRO
  640. case ACT_MACRO:
  641. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  642. break;
  643. #endif
  644. case ACT_COMMAND:
  645. break;
  646. #ifndef NO_ACTION_FUNCTION
  647. case ACT_FUNCTION:
  648. action_function(record, action.func.id, action.func.opt);
  649. break;
  650. #endif
  651. default:
  652. break;
  653. }
  654. }
  655. #ifndef NO_ACTION_TAPPING
  656. /* Tapping
  657. *
  658. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  659. * (without interfering by typing other key)
  660. */
  661. /* return true when key event is processed or consumed. */
  662. static bool process_tapping(keyrecord_t *keyp)
  663. {
  664. keyevent_t event = keyp->event;
  665. // if tapping
  666. if (IS_TAPPING_PRESSED()) {
  667. if (WITHIN_TAPPING_TERM(event)) {
  668. if (tapping_key.tap.count == 0) {
  669. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  670. // first tap!
  671. debug("Tapping: First tap(0->1).\n");
  672. tapping_key.tap.count = 1;
  673. tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
  674. debug_tapping_key();
  675. process_action(&tapping_key);
  676. // enqueue
  677. keyp->tap = tapping_key.tap;
  678. return false;
  679. }
  680. #if TAPPING_TERM >= 500
  681. /* This can prevent from typing some tap keys in a row at a time. */
  682. else if (!event.pressed && waiting_buffer_typed(event)) {
  683. // other key typed. not tap.
  684. debug("Tapping: End. No tap. Interfered by typing key\n");
  685. process_action(&tapping_key);
  686. tapping_key = (keyrecord_t){};
  687. debug_tapping_key();
  688. // enqueue
  689. return false;
  690. }
  691. #endif
  692. else {
  693. // other key events shall be enq'd till tapping state settles.
  694. return false;
  695. }
  696. }
  697. // tap_count > 0
  698. else {
  699. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  700. debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
  701. keyp->tap = tapping_key.tap;
  702. process_action(keyp);
  703. tapping_key = *keyp;
  704. debug_tapping_key();
  705. return true;
  706. }
  707. else if (is_tap_key(keyp->event.key) && event.pressed) {
  708. if (tapping_key.tap.count > 1) {
  709. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  710. // unregister key
  711. process_action(&(keyrecord_t){
  712. .tap = tapping_key.tap,
  713. .event.key = tapping_key.event.key,
  714. .event.time = event.time,
  715. .event.pressed = false
  716. });
  717. } else {
  718. debug("Tapping: Start while last tap(1).\n");
  719. }
  720. tapping_key = *keyp;
  721. waiting_buffer_scan_tap();
  722. debug_tapping_key();
  723. return true;
  724. }
  725. else {
  726. if (!IS_NOEVENT(keyp->event)) {
  727. debug("Tapping: key event while last tap(>0).\n");
  728. }
  729. process_action(keyp);
  730. return true;
  731. }
  732. }
  733. }
  734. // after TAPPING_TERM
  735. else {
  736. if (tapping_key.tap.count == 0) {
  737. debug("Tapping: End. Timeout. Not tap(0): ");
  738. debug_event(event); debug("\n");
  739. process_action(&tapping_key);
  740. tapping_key = (keyrecord_t){};
  741. debug_tapping_key();
  742. return false;
  743. } else {
  744. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  745. debug("Tapping: End. last timeout tap release(>0).");
  746. keyp->tap = tapping_key.tap;
  747. process_action(keyp);
  748. tapping_key = (keyrecord_t){};
  749. return true;
  750. }
  751. else if (is_tap_key(keyp->event.key) && event.pressed) {
  752. if (tapping_key.tap.count > 1) {
  753. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  754. // unregister key
  755. process_action(&(keyrecord_t){
  756. .tap = tapping_key.tap,
  757. .event.key = tapping_key.event.key,
  758. .event.time = event.time,
  759. .event.pressed = false
  760. });
  761. } else {
  762. debug("Tapping: Start while last timeout tap(1).\n");
  763. }
  764. tapping_key = *keyp;
  765. waiting_buffer_scan_tap();
  766. debug_tapping_key();
  767. return true;
  768. }
  769. else {
  770. if (!IS_NOEVENT(keyp->event)) {
  771. debug("Tapping: key event while last timeout tap(>0).\n");
  772. }
  773. process_action(keyp);
  774. return true;
  775. }
  776. }
  777. }
  778. } else if (IS_TAPPING_RELEASED()) {
  779. if (WITHIN_TAPPING_TERM(event)) {
  780. if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  781. // sequential tap.
  782. keyp->tap = tapping_key.tap;
  783. keyp->tap.count += 1;
  784. debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
  785. process_action(keyp);
  786. tapping_key = *keyp;
  787. debug_tapping_key();
  788. return true;
  789. } else if (event.pressed && is_tap_key(event.key)) {
  790. // Sequential tap can be interfered with other tap key.
  791. debug("Tapping: Start with interfering other tap.\n");
  792. tapping_key = *keyp;
  793. waiting_buffer_scan_tap();
  794. debug_tapping_key();
  795. return true;
  796. } else {
  797. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  798. process_action(keyp);
  799. return true;
  800. }
  801. } else {
  802. // timeout. no sequential tap.
  803. debug("Tapping: End(Timeout after releasing last tap): ");
  804. debug_event(event); debug("\n");
  805. tapping_key = (keyrecord_t){};
  806. debug_tapping_key();
  807. return false;
  808. }
  809. }
  810. // not tapping satate
  811. else {
  812. if (event.pressed && is_tap_key(event.key)) {
  813. debug("Tapping: Start(Press tap key).\n");
  814. tapping_key = *keyp;
  815. waiting_buffer_scan_tap();
  816. debug_tapping_key();
  817. return true;
  818. } else {
  819. process_action(keyp);
  820. return true;
  821. }
  822. }
  823. }
  824. /* scan buffer for tapping */
  825. static void waiting_buffer_scan_tap(void)
  826. {
  827. // tapping already is settled
  828. if (tapping_key.tap.count > 0) return;
  829. // invalid state: tapping_key released && tap.count == 0
  830. if (!tapping_key.event.pressed) return;
  831. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  832. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  833. !waiting_buffer[i].event.pressed &&
  834. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  835. tapping_key.tap.count = 1;
  836. waiting_buffer[i].tap.count = 1;
  837. process_action(&tapping_key);
  838. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  839. debug_waiting_buffer();
  840. return;
  841. }
  842. }
  843. }
  844. #endif
  845. /*
  846. * Utilities for actions.
  847. */
  848. void register_code(uint8_t code)
  849. {
  850. if (code == KC_NO) {
  851. return;
  852. }
  853. #ifdef CAPSLOCK_LOCKING_ENABLE
  854. else if (KC_LOCKING_CAPS == code) {
  855. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  856. // Resync: ignore if caps lock already is on
  857. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  858. #endif
  859. host_add_key(KC_CAPSLOCK);
  860. host_send_keyboard_report();
  861. host_del_key(KC_CAPSLOCK);
  862. host_send_keyboard_report();
  863. }
  864. #endif
  865. else if IS_KEY(code) {
  866. // TODO: should push command_proc out of this block?
  867. if (command_proc(code)) return;
  868. #ifndef NO_ACTION_TAPPING
  869. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  870. uint8_t tmp_mods = host_get_mods();
  871. host_add_mods(oneshot_state.mods);
  872. host_add_key(code);
  873. host_send_keyboard_report();
  874. host_set_mods(tmp_mods);
  875. oneshot_state.ready = false;
  876. } else
  877. #endif
  878. {
  879. host_add_key(code);
  880. host_send_keyboard_report();
  881. }
  882. }
  883. else if IS_MOD(code) {
  884. host_add_mods(MOD_BIT(code));
  885. host_send_keyboard_report();
  886. }
  887. }
  888. void unregister_code(uint8_t code)
  889. {
  890. if (code == KC_NO) {
  891. return;
  892. }
  893. #ifdef CAPSLOCK_LOCKING_ENABLE
  894. else if (KC_LOCKING_CAPS == code) {
  895. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  896. // Resync: ignore if caps lock already is off
  897. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  898. #endif
  899. host_add_key(KC_CAPSLOCK);
  900. host_send_keyboard_report();
  901. host_del_key(KC_CAPSLOCK);
  902. host_send_keyboard_report();
  903. }
  904. #endif
  905. else if IS_KEY(code) {
  906. host_del_key(code);
  907. host_send_keyboard_report();
  908. }
  909. else if IS_MOD(code) {
  910. host_del_mods(MOD_BIT(code));
  911. host_send_keyboard_report();
  912. }
  913. }
  914. void add_mods(uint8_t mods)
  915. {
  916. if (mods) {
  917. host_add_mods(mods);
  918. host_send_keyboard_report();
  919. }
  920. }
  921. void del_mods(uint8_t mods)
  922. {
  923. if (mods) {
  924. host_del_mods(mods);
  925. host_send_keyboard_report();
  926. }
  927. }
  928. void set_mods(uint8_t mods)
  929. {
  930. host_set_mods(mods);
  931. host_send_keyboard_report();
  932. }
  933. void clear_keyboard(void)
  934. {
  935. host_clear_mods();
  936. clear_keyboard_but_mods();
  937. }
  938. void clear_keyboard_but_mods(void)
  939. {
  940. host_clear_keys();
  941. host_send_keyboard_report();
  942. #ifdef MOUSEKEY_ENABLE
  943. mousekey_clear();
  944. mousekey_send();
  945. #endif
  946. #ifdef EXTRAKEY_ENABLE
  947. host_system_send(0);
  948. host_consumer_send(0);
  949. #endif
  950. }
  951. bool sending_anykey(void)
  952. {
  953. return (host_has_anykey() || host_mouse_in_use() ||
  954. host_last_sysytem_report() || host_last_consumer_report());
  955. }
  956. bool is_tap_key(key_t key)
  957. {
  958. action_t action = layer_switch_get_action(key);
  959. switch (action.kind.id) {
  960. case ACT_LMODS_TAP:
  961. case ACT_RMODS_TAP:
  962. return true;
  963. case ACT_KEYMAP:
  964. case ACT_OVERLAY:
  965. switch (action.layer.code) {
  966. case 0x04 ... 0xEF: /* tap key */
  967. case OP_INV:
  968. return true;
  969. default:
  970. return false;
  971. }
  972. case ACT_MACRO:
  973. case ACT_FUNCTION:
  974. if (action.func.opt & FUNC_TAP) { return true; }
  975. return false;
  976. }
  977. return false;
  978. }
  979. /*
  980. * debug print
  981. */
  982. static void debug_event(keyevent_t event)
  983. {
  984. debug_hex16((event.key.row<<8) | event.key.col);
  985. if (event.pressed) debug("d("); else debug("u(");
  986. debug_dec(event.time); debug(")");
  987. }
  988. static void debug_record(keyrecord_t record)
  989. {
  990. debug_event(record.event); debug(":"); debug_dec(record.tap.count);
  991. if (record.tap.interrupted) debug("-");
  992. }
  993. static void debug_action(action_t action)
  994. {
  995. switch (action.kind.id) {
  996. case ACT_LMODS: debug("ACT_LMODS"); break;
  997. case ACT_RMODS: debug("ACT_RMODS"); break;
  998. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  999. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  1000. case ACT_USAGE: debug("ACT_USAGE"); break;
  1001. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  1002. case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
  1003. case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
  1004. case ACT_MACRO: debug("ACT_MACRO"); break;
  1005. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  1006. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  1007. default: debug("UNKNOWN"); break;
  1008. }
  1009. debug("[");
  1010. debug_hex4(action.kind.param>>8);
  1011. debug(":");
  1012. debug_hex8(action.kind.param & 0xff);
  1013. debug("]");
  1014. }
  1015. #ifndef NO_ACTION_TAPPING
  1016. static void debug_tapping_key(void)
  1017. {
  1018. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  1019. }
  1020. static void debug_waiting_buffer(void)
  1021. {
  1022. debug("{ ");
  1023. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  1024. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  1025. }
  1026. debug("}\n");
  1027. }
  1028. #endif