action.c 18 KB

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