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