action.c 20 KB

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