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

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