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

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