action.c 19 KB

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