action.c 19 KB

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