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.

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