action.c 20 KB

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