action.c 19 KB

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