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

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