Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

action.c 37KB

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