Keyboard firmwares for Atmel AVR and Cortex-M
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.

action.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. #include "wait.h"
  28. #ifdef DEBUG_ACTION
  29. #include "debug.h"
  30. #else
  31. #include "nodebug.h"
  32. #endif
  33. void action_exec(keyevent_t event)
  34. {
  35. if (!IS_NOEVENT(event)) {
  36. dprint("\n---- action_exec: start -----\n");
  37. dprint("EVENT: "); debug_event(event); dprintln();
  38. hook_matrix_change(event);
  39. }
  40. keyrecord_t record = { .event = event };
  41. #ifndef NO_ACTION_TAPPING
  42. action_tapping_process(record);
  43. #else
  44. process_action(&record);
  45. if (!IS_NOEVENT(record.event)) {
  46. dprint("processed: "); debug_record(record); dprintln();
  47. }
  48. #endif
  49. }
  50. void process_action(keyrecord_t *record)
  51. {
  52. keyevent_t event = record->event;
  53. #ifndef NO_ACTION_TAPPING
  54. uint8_t tap_count = record->tap.count;
  55. #endif
  56. if (IS_NOEVENT(event)) { return; }
  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. switch (action.kind.id) {
  65. /* Key and Mods */
  66. case ACT_LMODS:
  67. case ACT_RMODS:
  68. {
  69. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  70. action.key.mods<<4;
  71. if (event.pressed) {
  72. if (mods) {
  73. add_weak_mods(mods);
  74. send_keyboard_report();
  75. }
  76. register_code(action.key.code);
  77. } else {
  78. unregister_code(action.key.code);
  79. if (mods) {
  80. del_weak_mods(mods);
  81. send_keyboard_report();
  82. }
  83. }
  84. }
  85. break;
  86. #ifndef NO_ACTION_TAPPING
  87. case ACT_LMODS_TAP:
  88. case ACT_RMODS_TAP:
  89. {
  90. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  91. action.key.mods<<4;
  92. switch (action.layer_tap.code) {
  93. #ifndef NO_ACTION_ONESHOT
  94. case MODS_ONESHOT:
  95. // Oneshot modifier
  96. if (event.pressed) {
  97. if (tap_count == 0) {
  98. register_mods(mods);
  99. }
  100. else if (tap_count == 1) {
  101. dprint("MODS_TAP: Oneshot: start\n");
  102. set_oneshot_mods(mods);
  103. }
  104. else {
  105. register_mods(mods);
  106. }
  107. } else {
  108. if (tap_count == 0) {
  109. clear_oneshot_mods();
  110. unregister_mods(mods);
  111. }
  112. else if (tap_count == 1) {
  113. // Retain Oneshot mods
  114. }
  115. else {
  116. clear_oneshot_mods();
  117. unregister_mods(mods);
  118. }
  119. }
  120. break;
  121. #endif
  122. case MODS_TAP_TOGGLE:
  123. if (event.pressed) {
  124. if (tap_count <= TAPPING_TOGGLE) {
  125. if (mods & get_mods()) {
  126. dprint("MODS_TAP_TOGGLE: toggle mods off\n");
  127. unregister_mods(mods);
  128. } else {
  129. dprint("MODS_TAP_TOGGLE: toggle mods on\n");
  130. register_mods(mods);
  131. }
  132. }
  133. } else {
  134. if (tap_count < TAPPING_TOGGLE) {
  135. dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
  136. unregister_mods(mods);
  137. }
  138. }
  139. break;
  140. default:
  141. if (event.pressed) {
  142. if (tap_count > 0) {
  143. if (record->tap.interrupted) {
  144. dprint("MODS_TAP: Tap: Cancel: add_mods\n");
  145. // ad hoc: set 0 to cancel tap
  146. record->tap.count = 0;
  147. register_mods(mods);
  148. } else {
  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. wait_ms(100);
  353. del_key(KC_CAPSLOCK);
  354. send_keyboard_report();
  355. }
  356. else if (KC_LOCKING_NUM == code) {
  357. #ifdef LOCKING_RESYNC_ENABLE
  358. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  359. #endif
  360. add_key(KC_NUMLOCK);
  361. send_keyboard_report();
  362. wait_ms(100);
  363. del_key(KC_NUMLOCK);
  364. send_keyboard_report();
  365. }
  366. else if (KC_LOCKING_SCROLL == code) {
  367. #ifdef LOCKING_RESYNC_ENABLE
  368. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  369. #endif
  370. add_key(KC_SCROLLLOCK);
  371. send_keyboard_report();
  372. wait_ms(100);
  373. del_key(KC_SCROLLLOCK);
  374. send_keyboard_report();
  375. }
  376. #endif
  377. else if IS_KEY(code) {
  378. // TODO: should push command_proc out of this block?
  379. if (command_proc(code)) return;
  380. #ifndef NO_ACTION_ONESHOT
  381. /* TODO: remove
  382. if (oneshot_state.mods && !oneshot_state.disabled) {
  383. uint8_t tmp_mods = get_mods();
  384. add_mods(oneshot_state.mods);
  385. add_key(code);
  386. send_keyboard_report();
  387. set_mods(tmp_mods);
  388. send_keyboard_report();
  389. oneshot_cancel();
  390. } else
  391. */
  392. #endif
  393. {
  394. add_key(code);
  395. send_keyboard_report();
  396. }
  397. }
  398. else if IS_MOD(code) {
  399. add_mods(MOD_BIT(code));
  400. send_keyboard_report();
  401. }
  402. else if IS_SYSTEM(code) {
  403. host_system_send(KEYCODE2SYSTEM(code));
  404. }
  405. else if IS_CONSUMER(code) {
  406. host_consumer_send(KEYCODE2CONSUMER(code));
  407. }
  408. }
  409. void unregister_code(uint8_t code)
  410. {
  411. if (code == KC_NO) {
  412. return;
  413. }
  414. #ifdef LOCKING_SUPPORT_ENABLE
  415. else if (KC_LOCKING_CAPS == code) {
  416. #ifdef LOCKING_RESYNC_ENABLE
  417. // Resync: ignore if caps lock already is off
  418. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  419. #endif
  420. add_key(KC_CAPSLOCK);
  421. send_keyboard_report();
  422. wait_ms(100);
  423. del_key(KC_CAPSLOCK);
  424. send_keyboard_report();
  425. }
  426. else if (KC_LOCKING_NUM == code) {
  427. #ifdef LOCKING_RESYNC_ENABLE
  428. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  429. #endif
  430. add_key(KC_NUMLOCK);
  431. send_keyboard_report();
  432. wait_ms(100);
  433. del_key(KC_NUMLOCK);
  434. send_keyboard_report();
  435. }
  436. else if (KC_LOCKING_SCROLL == code) {
  437. #ifdef LOCKING_RESYNC_ENABLE
  438. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  439. #endif
  440. add_key(KC_SCROLLLOCK);
  441. send_keyboard_report();
  442. wait_ms(100);
  443. del_key(KC_SCROLLLOCK);
  444. send_keyboard_report();
  445. }
  446. #endif
  447. else if IS_KEY(code) {
  448. del_key(code);
  449. send_keyboard_report();
  450. }
  451. else if IS_MOD(code) {
  452. del_mods(MOD_BIT(code));
  453. send_keyboard_report();
  454. }
  455. else if IS_SYSTEM(code) {
  456. host_system_send(0);
  457. }
  458. else if IS_CONSUMER(code) {
  459. host_consumer_send(0);
  460. }
  461. }
  462. void register_mods(uint8_t mods)
  463. {
  464. if (mods) {
  465. add_mods(mods);
  466. send_keyboard_report();
  467. }
  468. }
  469. void unregister_mods(uint8_t mods)
  470. {
  471. if (mods) {
  472. del_mods(mods);
  473. send_keyboard_report();
  474. }
  475. }
  476. void clear_keyboard(void)
  477. {
  478. clear_mods();
  479. clear_keyboard_but_mods();
  480. }
  481. void clear_keyboard_but_mods(void)
  482. {
  483. clear_weak_mods();
  484. clear_keys();
  485. send_keyboard_report();
  486. #ifdef MOUSEKEY_ENABLE
  487. mousekey_clear();
  488. mousekey_send();
  489. #endif
  490. #ifdef EXTRAKEY_ENABLE
  491. host_system_send(0);
  492. host_consumer_send(0);
  493. #endif
  494. }
  495. bool is_tap_key(keypos_t key)
  496. {
  497. action_t action = layer_switch_get_action(key);
  498. switch (action.kind.id) {
  499. case ACT_LMODS_TAP:
  500. case ACT_RMODS_TAP:
  501. case ACT_LAYER_TAP:
  502. case ACT_LAYER_TAP_EXT:
  503. switch (action.layer_tap.code) {
  504. case KC_A ... KC_EXSEL:
  505. case KC_KP_00 ... KC_KP_HEXADECIMAL:
  506. case KC_LCTRL ... KC_RGUI:
  507. case OP_TAP_TOGGLE:
  508. return true;
  509. }
  510. return false;
  511. case ACT_MACRO:
  512. case ACT_FUNCTION:
  513. if (action.func.opt & FUNC_TAP) { return true; }
  514. return false;
  515. }
  516. return false;
  517. }
  518. /*
  519. * debug print
  520. */
  521. void debug_event(keyevent_t event)
  522. {
  523. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  524. }
  525. void debug_record(keyrecord_t record)
  526. {
  527. debug_event(record.event);
  528. #ifndef NO_ACTION_TAPPING
  529. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  530. #endif
  531. }
  532. void debug_action(action_t action)
  533. {
  534. switch (action.kind.id) {
  535. case ACT_LMODS: dprint("ACT_LMODS"); break;
  536. case ACT_RMODS: dprint("ACT_RMODS"); break;
  537. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  538. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  539. case ACT_USAGE: dprint("ACT_USAGE"); break;
  540. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  541. case ACT_LAYER: dprint("ACT_LAYER"); break;
  542. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  543. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  544. case ACT_MACRO: dprint("ACT_MACRO"); break;
  545. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  546. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  547. default: dprint("UNKNOWN"); break;
  548. }
  549. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  550. }