您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

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