action.c 19 KB

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