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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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.key.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 0xc0 ... 0xdf:
  239. /* layer On/Off with modifiers */
  240. if (event.pressed) {
  241. layer_on(action.layer_tap.val);
  242. register_mods((action.layer_tap.code & 0x10) ?
  243. (action.layer_tap.code & 0x0f) << 4 :
  244. (action.layer_tap.code & 0x0f));
  245. } else {
  246. layer_off(action.layer_tap.val);
  247. unregister_mods((action.layer_tap.code & 0x10) ?
  248. (action.layer_tap.code & 0x0f) << 4 :
  249. (action.layer_tap.code & 0x0f));
  250. }
  251. break;
  252. case OP_TAP_TOGGLE:
  253. /* tap toggle */
  254. if (event.pressed) {
  255. if (tap_count < TAPPING_TOGGLE) {
  256. layer_invert(action.layer_tap.val);
  257. }
  258. } else {
  259. if (tap_count <= TAPPING_TOGGLE) {
  260. layer_invert(action.layer_tap.val);
  261. }
  262. }
  263. break;
  264. case OP_ON_OFF:
  265. event.pressed ? layer_on(action.layer_tap.val) :
  266. layer_off(action.layer_tap.val);
  267. break;
  268. case OP_OFF_ON:
  269. event.pressed ? layer_off(action.layer_tap.val) :
  270. layer_on(action.layer_tap.val);
  271. break;
  272. case OP_SET_CLEAR:
  273. event.pressed ? layer_move(action.layer_tap.val) :
  274. layer_clear();
  275. break;
  276. default:
  277. /* tap key */
  278. if (event.pressed) {
  279. if (tap_count > 0) {
  280. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  281. register_code(action.layer_tap.code);
  282. } else {
  283. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  284. layer_on(action.layer_tap.val);
  285. }
  286. } else {
  287. if (tap_count > 0) {
  288. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  289. unregister_code(action.layer_tap.code);
  290. } else {
  291. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  292. layer_off(action.layer_tap.val);
  293. }
  294. }
  295. break;
  296. }
  297. break;
  298. #endif
  299. #endif
  300. /* Extentions */
  301. #ifndef NO_ACTION_MACRO
  302. case ACT_MACRO:
  303. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  304. break;
  305. #endif
  306. #ifdef BACKLIGHT_ENABLE
  307. case ACT_BACKLIGHT:
  308. if (!event.pressed) {
  309. switch (action.backlight.opt) {
  310. case BACKLIGHT_INCREASE:
  311. backlight_increase();
  312. break;
  313. case BACKLIGHT_DECREASE:
  314. backlight_decrease();
  315. break;
  316. case BACKLIGHT_TOGGLE:
  317. backlight_toggle();
  318. break;
  319. case BACKLIGHT_STEP:
  320. backlight_step();
  321. break;
  322. case BACKLIGHT_LEVEL:
  323. backlight_level(action.backlight.level);
  324. break;
  325. }
  326. }
  327. break;
  328. #endif
  329. case ACT_COMMAND:
  330. break;
  331. #ifndef NO_ACTION_FUNCTION
  332. case ACT_FUNCTION:
  333. action_function(record, action.func.id, action.func.opt);
  334. break;
  335. #endif
  336. default:
  337. break;
  338. }
  339. }
  340. /*
  341. * Utilities for actions.
  342. */
  343. void register_code(uint8_t code)
  344. {
  345. if (code == KC_NO) {
  346. return;
  347. }
  348. #ifdef LOCKING_SUPPORT_ENABLE
  349. else if (KC_LOCKING_CAPS == code) {
  350. #ifdef LOCKING_RESYNC_ENABLE
  351. // Resync: ignore if caps lock already is on
  352. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  353. #endif
  354. add_key(KC_CAPSLOCK);
  355. send_keyboard_report();
  356. wait_ms(100);
  357. del_key(KC_CAPSLOCK);
  358. send_keyboard_report();
  359. }
  360. else if (KC_LOCKING_NUM == code) {
  361. #ifdef LOCKING_RESYNC_ENABLE
  362. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  363. #endif
  364. add_key(KC_NUMLOCK);
  365. send_keyboard_report();
  366. wait_ms(100);
  367. del_key(KC_NUMLOCK);
  368. send_keyboard_report();
  369. }
  370. else if (KC_LOCKING_SCROLL == code) {
  371. #ifdef LOCKING_RESYNC_ENABLE
  372. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  373. #endif
  374. add_key(KC_SCROLLLOCK);
  375. send_keyboard_report();
  376. wait_ms(100);
  377. del_key(KC_SCROLLLOCK);
  378. send_keyboard_report();
  379. }
  380. #endif
  381. else if IS_KEY(code) {
  382. // TODO: should push command_proc out of this block?
  383. if (command_proc(code)) return;
  384. #ifndef NO_ACTION_ONESHOT
  385. /* TODO: remove
  386. if (oneshot_state.mods && !oneshot_state.disabled) {
  387. uint8_t tmp_mods = get_mods();
  388. add_mods(oneshot_state.mods);
  389. add_key(code);
  390. send_keyboard_report();
  391. set_mods(tmp_mods);
  392. send_keyboard_report();
  393. oneshot_cancel();
  394. } else
  395. */
  396. #endif
  397. {
  398. add_key(code);
  399. send_keyboard_report();
  400. }
  401. }
  402. else if IS_MOD(code) {
  403. add_mods(MOD_BIT(code));
  404. send_keyboard_report();
  405. }
  406. else if IS_SYSTEM(code) {
  407. host_system_send(KEYCODE2SYSTEM(code));
  408. }
  409. else if IS_CONSUMER(code) {
  410. host_consumer_send(KEYCODE2CONSUMER(code));
  411. }
  412. }
  413. void unregister_code(uint8_t code)
  414. {
  415. if (code == KC_NO) {
  416. return;
  417. }
  418. #ifdef LOCKING_SUPPORT_ENABLE
  419. else if (KC_LOCKING_CAPS == code) {
  420. #ifdef LOCKING_RESYNC_ENABLE
  421. // Resync: ignore if caps lock already is off
  422. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  423. #endif
  424. add_key(KC_CAPSLOCK);
  425. send_keyboard_report();
  426. wait_ms(100);
  427. del_key(KC_CAPSLOCK);
  428. send_keyboard_report();
  429. }
  430. else if (KC_LOCKING_NUM == code) {
  431. #ifdef LOCKING_RESYNC_ENABLE
  432. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  433. #endif
  434. add_key(KC_NUMLOCK);
  435. send_keyboard_report();
  436. wait_ms(100);
  437. del_key(KC_NUMLOCK);
  438. send_keyboard_report();
  439. }
  440. else if (KC_LOCKING_SCROLL == code) {
  441. #ifdef LOCKING_RESYNC_ENABLE
  442. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  443. #endif
  444. add_key(KC_SCROLLLOCK);
  445. send_keyboard_report();
  446. wait_ms(100);
  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_keys();
  489. send_keyboard_report();
  490. #ifdef MOUSEKEY_ENABLE
  491. mousekey_clear();
  492. mousekey_send();
  493. #endif
  494. #ifdef EXTRAKEY_ENABLE
  495. host_system_send(0);
  496. host_consumer_send(0);
  497. #endif
  498. }
  499. bool is_tap_key(keypos_t key)
  500. {
  501. action_t action = layer_switch_get_action(key);
  502. switch (action.kind.id) {
  503. case ACT_LMODS_TAP:
  504. case ACT_RMODS_TAP:
  505. switch (action.key.code) {
  506. case MODS_ONESHOT:
  507. case MODS_TAP_TOGGLE:
  508. case KC_A ... KC_EXSEL: // tap key
  509. case KC_LCTRL ... KC_RGUI: // tap key
  510. return true;
  511. }
  512. case ACT_LAYER_TAP:
  513. case ACT_LAYER_TAP_EXT:
  514. switch (action.layer_tap.code) {
  515. case 0xc0 ... 0xdf: // with modifiers
  516. return false;
  517. case KC_A ... KC_EXSEL: // tap key
  518. case KC_LCTRL ... KC_RGUI: // tap key
  519. case OP_TAP_TOGGLE:
  520. return true;
  521. }
  522. return false;
  523. case ACT_MACRO:
  524. case ACT_FUNCTION:
  525. if (action.func.opt & FUNC_TAP) { return true; }
  526. return false;
  527. }
  528. return false;
  529. }
  530. /*
  531. * debug print
  532. */
  533. void debug_event(keyevent_t event)
  534. {
  535. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  536. }
  537. void debug_record(keyrecord_t record)
  538. {
  539. debug_event(record.event);
  540. #ifndef NO_ACTION_TAPPING
  541. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  542. #endif
  543. }
  544. void debug_action(action_t action)
  545. {
  546. switch (action.kind.id) {
  547. case ACT_LMODS: dprint("ACT_LMODS"); break;
  548. case ACT_RMODS: dprint("ACT_RMODS"); break;
  549. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  550. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  551. case ACT_USAGE: dprint("ACT_USAGE"); break;
  552. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  553. case ACT_LAYER: dprint("ACT_LAYER"); break;
  554. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  555. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  556. case ACT_MACRO: dprint("ACT_MACRO"); break;
  557. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  558. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  559. default: dprint("UNKNOWN"); break;
  560. }
  561. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  562. }