You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

action.c 19KB

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