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

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