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

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