Keyboard firmwares for Atmel AVR and Cortex-M
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

action.c 35KB

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