action.c 19 KB

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