action.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
  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 "keycode.h"
  16. #include "keyboard.h"
  17. #include "mousekey.h"
  18. #include "command.h"
  19. #include "led.h"
  20. #include "backlight.h"
  21. #include "action_layer.h"
  22. #include "action_tapping.h"
  23. #include "action_macro.h"
  24. #include "action_util.h"
  25. #include "action.h"
  26. #ifdef DEBUG_ACTION
  27. #include "debug.h"
  28. #else
  29. #include "nodebug.h"
  30. #endif
  31. void action_exec(keyevent_t event)
  32. {
  33. if (!IS_NOEVENT(event)) {
  34. dprint("\n---- action_exec: start -----\n");
  35. dprint("EVENT: "); debug_event(event); dprintln();
  36. }
  37. keyrecord_t record = { .event = event };
  38. #ifndef NO_ACTION_TAPPING
  39. action_tapping_process(record);
  40. #else
  41. process_action(&record);
  42. if (!IS_NOEVENT(record.event)) {
  43. dprint("processed: "); debug_record(record); dprintln();
  44. }
  45. #endif
  46. }
  47. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  48. bool disable_action_cache = false;
  49. uint8_t source_layers_cache[5][(MATRIX_ROWS * MATRIX_COLS + 7) / 8] = {0};
  50. void process_action_nocache(keyrecord_t *record)
  51. {
  52. disable_action_cache = true;
  53. process_action(record);
  54. disable_action_cache = false;
  55. }
  56. #else
  57. void process_action_nocache(keyrecord_t *record)
  58. {
  59. process_action(record);
  60. }
  61. #endif
  62. /*
  63. * Make sure the action triggered when the key is released is the same
  64. * one as the one triggered on press. It's important for the mod keys
  65. * when the layer is switched after the down event but before the up
  66. * event as they may get stuck otherwise.
  67. */
  68. action_t store_or_get_action(bool pressed, keypos_t key)
  69. {
  70. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  71. if (disable_action_cache) {
  72. return layer_switch_get_action(key);
  73. }
  74. const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
  75. const uint8_t storage_row = key_number / 8;
  76. const uint8_t storage_bit = key_number % 8;
  77. uint8_t layer;
  78. if (pressed) {
  79. layer = layer_switch_get_layer(key);
  80. for (uint8_t bit_number = 0; bit_number < 5; bit_number++) {
  81. source_layers_cache[bit_number][storage_row] ^=
  82. (-((layer & (1U << bit_number)) != 0)
  83. ^ source_layers_cache[bit_number][storage_row])
  84. & (1U << storage_bit);
  85. }
  86. }
  87. else {
  88. layer = 0;
  89. for (uint8_t bit_number = 0; bit_number < 5; bit_number++) {
  90. layer |=
  91. ((source_layers_cache[bit_number][storage_row]
  92. & (1U << storage_bit)) != 0)
  93. << bit_number;
  94. }
  95. }
  96. return action_for_key(layer, key);
  97. #else
  98. return layer_switch_get_action(key);
  99. #endif
  100. }
  101. void process_action(keyrecord_t *record)
  102. {
  103. keyevent_t event = record->event;
  104. #ifndef NO_ACTION_TAPPING
  105. uint8_t tap_count = record->tap.count;
  106. #endif
  107. if (IS_NOEVENT(event)) { return; }
  108. action_t action = store_or_get_action(event.pressed, event.key);
  109. dprint("ACTION: "); debug_action(action);
  110. #ifndef NO_ACTION_LAYER
  111. dprint(" layer_state: "); layer_debug();
  112. dprint(" default_layer_state: "); default_layer_debug();
  113. #endif
  114. dprintln();
  115. switch (action.kind.id) {
  116. /* Key and Mods */
  117. case ACT_LMODS:
  118. case ACT_RMODS:
  119. {
  120. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  121. action.key.mods<<4;
  122. if (event.pressed) {
  123. if (mods) {
  124. add_weak_mods(mods);
  125. send_keyboard_report();
  126. }
  127. register_code(action.key.code);
  128. } else {
  129. unregister_code(action.key.code);
  130. if (mods) {
  131. del_weak_mods(mods);
  132. send_keyboard_report();
  133. }
  134. }
  135. }
  136. break;
  137. #ifndef NO_ACTION_TAPPING
  138. case ACT_LMODS_TAP:
  139. case ACT_RMODS_TAP:
  140. {
  141. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  142. action.key.mods<<4;
  143. switch (action.layer_tap.code) {
  144. #ifndef NO_ACTION_ONESHOT
  145. case MODS_ONESHOT:
  146. // Oneshot modifier
  147. if (event.pressed) {
  148. if (tap_count == 0) {
  149. register_mods(mods);
  150. }
  151. else if (tap_count == 1) {
  152. dprint("MODS_TAP: Oneshot: start\n");
  153. set_oneshot_mods(mods);
  154. }
  155. else {
  156. register_mods(mods);
  157. }
  158. } else {
  159. if (tap_count == 0) {
  160. clear_oneshot_mods();
  161. unregister_mods(mods);
  162. }
  163. else if (tap_count == 1) {
  164. // Retain Oneshot mods
  165. }
  166. else {
  167. clear_oneshot_mods();
  168. unregister_mods(mods);
  169. }
  170. }
  171. break;
  172. #endif
  173. case MODS_TAP_TOGGLE:
  174. if (event.pressed) {
  175. if (tap_count <= TAPPING_TOGGLE) {
  176. register_mods(mods);
  177. }
  178. } else {
  179. if (tap_count < TAPPING_TOGGLE) {
  180. unregister_mods(mods);
  181. }
  182. }
  183. break;
  184. default:
  185. if (event.pressed) {
  186. if (tap_count > 0) {
  187. #ifndef IGNORE_MOD_TAP_INTERRUPT
  188. if (record->tap.interrupted) {
  189. dprint("mods_tap: tap: cancel: add_mods\n");
  190. // ad hoc: set 0 to cancel tap
  191. record->tap.count = 0;
  192. register_mods(mods);
  193. } else
  194. #endif
  195. {
  196. dprint("MODS_TAP: Tap: register_code\n");
  197. register_code(action.key.code);
  198. }
  199. } else {
  200. dprint("MODS_TAP: No tap: add_mods\n");
  201. register_mods(mods);
  202. }
  203. } else {
  204. if (tap_count > 0) {
  205. dprint("MODS_TAP: Tap: unregister_code\n");
  206. unregister_code(action.key.code);
  207. } else {
  208. dprint("MODS_TAP: No tap: add_mods\n");
  209. unregister_mods(mods);
  210. }
  211. }
  212. break;
  213. }
  214. }
  215. break;
  216. #endif
  217. #ifdef EXTRAKEY_ENABLE
  218. /* other HID usage */
  219. case ACT_USAGE:
  220. switch (action.usage.page) {
  221. case PAGE_SYSTEM:
  222. if (event.pressed) {
  223. host_system_send(action.usage.code);
  224. } else {
  225. host_system_send(0);
  226. }
  227. break;
  228. case PAGE_CONSUMER:
  229. if (event.pressed) {
  230. host_consumer_send(action.usage.code);
  231. } else {
  232. host_consumer_send(0);
  233. }
  234. break;
  235. }
  236. break;
  237. #endif
  238. #ifdef MOUSEKEY_ENABLE
  239. /* Mouse key */
  240. case ACT_MOUSEKEY:
  241. if (event.pressed) {
  242. mousekey_on(action.key.code);
  243. mousekey_send();
  244. } else {
  245. mousekey_off(action.key.code);
  246. mousekey_send();
  247. }
  248. break;
  249. #endif
  250. #ifndef NO_ACTION_LAYER
  251. case ACT_LAYER:
  252. if (action.layer_bitop.on == 0) {
  253. /* Default Layer Bitwise Operation */
  254. if (!event.pressed) {
  255. uint8_t shift = action.layer_bitop.part*4;
  256. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  257. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  258. switch (action.layer_bitop.op) {
  259. case OP_BIT_AND: default_layer_and(bits | mask); break;
  260. case OP_BIT_OR: default_layer_or(bits | mask); break;
  261. case OP_BIT_XOR: default_layer_xor(bits | mask); break;
  262. case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
  263. }
  264. }
  265. } else {
  266. /* Layer Bitwise Operation */
  267. if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
  268. (action.layer_bitop.on & ON_RELEASE)) {
  269. uint8_t shift = action.layer_bitop.part*4;
  270. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  271. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  272. switch (action.layer_bitop.op) {
  273. case OP_BIT_AND: layer_and(bits | mask); break;
  274. case OP_BIT_OR: layer_or(bits | mask); break;
  275. case OP_BIT_XOR: layer_xor(bits | mask); break;
  276. case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
  277. }
  278. }
  279. }
  280. break;
  281. #ifndef NO_ACTION_TAPPING
  282. case ACT_LAYER_TAP:
  283. case ACT_LAYER_TAP_EXT:
  284. switch (action.layer_tap.code) {
  285. case 0xe0 ... 0xef:
  286. /* layer On/Off with modifiers(left only) */
  287. if (event.pressed) {
  288. layer_on(action.layer_tap.val);
  289. register_mods(action.layer_tap.code & 0x0f);
  290. } else {
  291. layer_off(action.layer_tap.val);
  292. unregister_mods(action.layer_tap.code & 0x0f);
  293. }
  294. break;
  295. case OP_TAP_TOGGLE:
  296. /* tap toggle */
  297. if (event.pressed) {
  298. if (tap_count < TAPPING_TOGGLE) {
  299. layer_invert(action.layer_tap.val);
  300. }
  301. } else {
  302. if (tap_count <= TAPPING_TOGGLE) {
  303. layer_invert(action.layer_tap.val);
  304. }
  305. }
  306. break;
  307. case OP_ON_OFF:
  308. event.pressed ? layer_on(action.layer_tap.val) :
  309. layer_off(action.layer_tap.val);
  310. break;
  311. case OP_OFF_ON:
  312. event.pressed ? layer_off(action.layer_tap.val) :
  313. layer_on(action.layer_tap.val);
  314. break;
  315. case OP_SET_CLEAR:
  316. event.pressed ? layer_move(action.layer_tap.val) :
  317. layer_clear();
  318. break;
  319. default:
  320. /* tap key */
  321. if (event.pressed) {
  322. if (tap_count > 0) {
  323. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  324. register_code(action.layer_tap.code);
  325. } else {
  326. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  327. layer_on(action.layer_tap.val);
  328. }
  329. } else {
  330. if (tap_count > 0) {
  331. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  332. unregister_code(action.layer_tap.code);
  333. } else {
  334. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  335. layer_off(action.layer_tap.val);
  336. }
  337. }
  338. break;
  339. }
  340. break;
  341. #endif
  342. #endif
  343. /* Extentions */
  344. #ifndef NO_ACTION_MACRO
  345. case ACT_MACRO:
  346. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  347. break;
  348. #endif
  349. #ifdef BACKLIGHT_ENABLE
  350. case ACT_BACKLIGHT:
  351. if (!event.pressed) {
  352. switch (action.backlight.opt) {
  353. case BACKLIGHT_INCREASE:
  354. backlight_increase();
  355. break;
  356. case BACKLIGHT_DECREASE:
  357. backlight_decrease();
  358. break;
  359. case BACKLIGHT_TOGGLE:
  360. backlight_toggle();
  361. break;
  362. case BACKLIGHT_STEP:
  363. backlight_step();
  364. break;
  365. case BACKLIGHT_LEVEL:
  366. backlight_level(action.backlight.level);
  367. break;
  368. }
  369. }
  370. break;
  371. #endif
  372. case ACT_COMMAND:
  373. break;
  374. #ifndef NO_ACTION_FUNCTION
  375. case ACT_FUNCTION:
  376. action_function(record, action.func.id, action.func.opt);
  377. break;
  378. #endif
  379. default:
  380. break;
  381. }
  382. }
  383. /*
  384. * Utilities for actions.
  385. */
  386. void register_code(uint8_t code)
  387. {
  388. if (code == KC_NO) {
  389. return;
  390. }
  391. #ifdef LOCKING_SUPPORT_ENABLE
  392. else if (KC_LOCKING_CAPS == code) {
  393. #ifdef LOCKING_RESYNC_ENABLE
  394. // Resync: ignore if caps lock already is on
  395. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  396. #endif
  397. add_key(KC_CAPSLOCK);
  398. send_keyboard_report();
  399. del_key(KC_CAPSLOCK);
  400. send_keyboard_report();
  401. }
  402. else if (KC_LOCKING_NUM == code) {
  403. #ifdef LOCKING_RESYNC_ENABLE
  404. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  405. #endif
  406. add_key(KC_NUMLOCK);
  407. send_keyboard_report();
  408. del_key(KC_NUMLOCK);
  409. send_keyboard_report();
  410. }
  411. else if (KC_LOCKING_SCROLL == code) {
  412. #ifdef LOCKING_RESYNC_ENABLE
  413. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  414. #endif
  415. add_key(KC_SCROLLLOCK);
  416. send_keyboard_report();
  417. del_key(KC_SCROLLLOCK);
  418. send_keyboard_report();
  419. }
  420. #endif
  421. else if IS_KEY(code) {
  422. // TODO: should push command_proc out of this block?
  423. if (command_proc(code)) return;
  424. #ifndef NO_ACTION_ONESHOT
  425. /* TODO: remove
  426. if (oneshot_state.mods && !oneshot_state.disabled) {
  427. uint8_t tmp_mods = get_mods();
  428. add_mods(oneshot_state.mods);
  429. add_key(code);
  430. send_keyboard_report();
  431. set_mods(tmp_mods);
  432. send_keyboard_report();
  433. oneshot_cancel();
  434. } else
  435. */
  436. #endif
  437. {
  438. add_key(code);
  439. send_keyboard_report();
  440. }
  441. }
  442. else if IS_MOD(code) {
  443. add_mods(MOD_BIT(code));
  444. send_keyboard_report();
  445. }
  446. else if IS_SYSTEM(code) {
  447. host_system_send(KEYCODE2SYSTEM(code));
  448. }
  449. else if IS_CONSUMER(code) {
  450. host_consumer_send(KEYCODE2CONSUMER(code));
  451. }
  452. }
  453. void unregister_code(uint8_t code)
  454. {
  455. if (code == KC_NO) {
  456. return;
  457. }
  458. #ifdef LOCKING_SUPPORT_ENABLE
  459. else if (KC_LOCKING_CAPS == code) {
  460. #ifdef LOCKING_RESYNC_ENABLE
  461. // Resync: ignore if caps lock already is off
  462. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  463. #endif
  464. add_key(KC_CAPSLOCK);
  465. send_keyboard_report();
  466. del_key(KC_CAPSLOCK);
  467. send_keyboard_report();
  468. }
  469. else if (KC_LOCKING_NUM == code) {
  470. #ifdef LOCKING_RESYNC_ENABLE
  471. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  472. #endif
  473. add_key(KC_NUMLOCK);
  474. send_keyboard_report();
  475. del_key(KC_NUMLOCK);
  476. send_keyboard_report();
  477. }
  478. else if (KC_LOCKING_SCROLL == code) {
  479. #ifdef LOCKING_RESYNC_ENABLE
  480. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  481. #endif
  482. add_key(KC_SCROLLLOCK);
  483. send_keyboard_report();
  484. del_key(KC_SCROLLLOCK);
  485. send_keyboard_report();
  486. }
  487. #endif
  488. else if IS_KEY(code) {
  489. del_key(code);
  490. send_keyboard_report();
  491. }
  492. else if IS_MOD(code) {
  493. del_mods(MOD_BIT(code));
  494. send_keyboard_report();
  495. }
  496. else if IS_SYSTEM(code) {
  497. host_system_send(0);
  498. }
  499. else if IS_CONSUMER(code) {
  500. host_consumer_send(0);
  501. }
  502. }
  503. void register_mods(uint8_t mods)
  504. {
  505. if (mods) {
  506. add_mods(mods);
  507. send_keyboard_report();
  508. }
  509. }
  510. void unregister_mods(uint8_t mods)
  511. {
  512. if (mods) {
  513. del_mods(mods);
  514. send_keyboard_report();
  515. }
  516. }
  517. void clear_keyboard(void)
  518. {
  519. clear_mods();
  520. clear_keyboard_but_mods();
  521. }
  522. void clear_keyboard_but_mods(void)
  523. {
  524. clear_weak_mods();
  525. clear_keys();
  526. send_keyboard_report();
  527. #ifdef MOUSEKEY_ENABLE
  528. mousekey_clear();
  529. mousekey_send();
  530. #endif
  531. #ifdef EXTRAKEY_ENABLE
  532. host_system_send(0);
  533. host_consumer_send(0);
  534. #endif
  535. }
  536. bool is_tap_key(keypos_t key)
  537. {
  538. action_t action = layer_switch_get_action(key);
  539. switch (action.kind.id) {
  540. case ACT_LMODS_TAP:
  541. case ACT_RMODS_TAP:
  542. case ACT_LAYER_TAP:
  543. case ACT_LAYER_TAP_EXT:
  544. switch (action.layer_tap.code) {
  545. case 0x00 ... 0xdf:
  546. case OP_TAP_TOGGLE:
  547. return true;
  548. }
  549. return false;
  550. case ACT_MACRO:
  551. case ACT_FUNCTION:
  552. if (action.func.opt & FUNC_TAP) { return true; }
  553. return false;
  554. }
  555. return false;
  556. }
  557. /*
  558. * debug print
  559. */
  560. void debug_event(keyevent_t event)
  561. {
  562. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  563. }
  564. void debug_record(keyrecord_t record)
  565. {
  566. debug_event(record.event);
  567. #ifndef NO_ACTION_TAPPING
  568. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  569. #endif
  570. }
  571. void debug_action(action_t action)
  572. {
  573. switch (action.kind.id) {
  574. case ACT_LMODS: dprint("ACT_LMODS"); break;
  575. case ACT_RMODS: dprint("ACT_RMODS"); break;
  576. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  577. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  578. case ACT_USAGE: dprint("ACT_USAGE"); break;
  579. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  580. case ACT_LAYER: dprint("ACT_LAYER"); break;
  581. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  582. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  583. case ACT_MACRO: dprint("ACT_MACRO"); break;
  584. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  585. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  586. default: dprint("UNKNOWN"); break;
  587. }
  588. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  589. }