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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 "action_layer.h"
  21. #include "action_tapping.h"
  22. #include "action_oneshot.h"
  23. #include "action_macro.h"
  24. #include "action.h"
  25. #ifdef DEBUG_ACTION
  26. #include "debug.h"
  27. #else
  28. #include "nodebug.h"
  29. #endif
  30. void action_exec(keyevent_t event)
  31. {
  32. if (!IS_NOEVENT(event)) {
  33. dprint("\n---- action_exec: start -----\n");
  34. dprint("EVENT: "); debug_event(event); dprintln();
  35. }
  36. keyrecord_t record = { .event = event };
  37. #ifndef NO_ACTION_TAPPING
  38. action_tapping_process(record);
  39. #else
  40. process_action(&record);
  41. if (!IS_NOEVENT(record.event)) {
  42. dprint("processed: "); debug_record(record); dprintln();
  43. }
  44. #endif
  45. }
  46. void process_action(keyrecord_t *record)
  47. {
  48. keyevent_t event = record->event;
  49. #ifndef NO_ACTION_TAPPING
  50. uint8_t tap_count = record->tap.count;
  51. #endif
  52. if (IS_NOEVENT(event)) { return; }
  53. action_t action = layer_switch_get_action(event.key);
  54. dprint("ACTION: "); debug_action(action);
  55. #ifndef NO_ACTION_LAYER
  56. dprint(" layer_state: "); layer_debug();
  57. dprint(" default_layer_state: "); default_layer_debug();
  58. #endif
  59. dprintln();
  60. switch (action.kind.id) {
  61. /* Key and Mods */
  62. case ACT_LMODS:
  63. case ACT_RMODS:
  64. {
  65. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  66. action.key.mods<<4;
  67. if (event.pressed) {
  68. if (mods) {
  69. host_add_mods(mods);
  70. host_send_keyboard_report();
  71. }
  72. register_code(action.key.code);
  73. } else {
  74. unregister_code(action.key.code);
  75. if (mods) {
  76. host_del_mods(mods);
  77. host_send_keyboard_report();
  78. }
  79. }
  80. }
  81. break;
  82. #ifndef NO_ACTION_TAPPING
  83. case ACT_LMODS_TAP:
  84. case ACT_RMODS_TAP:
  85. {
  86. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  87. action.key.mods<<4;
  88. switch (action.layer_tap.code) {
  89. #ifndef NO_ACTION_ONESHOT
  90. case 0x00:
  91. // Oneshot modifier
  92. if (event.pressed) {
  93. if (tap_count == 0) {
  94. dprint("MODS_TAP: Oneshot: add_mods\n");
  95. add_mods(mods);
  96. }
  97. else if (tap_count == 1) {
  98. dprint("MODS_TAP: Oneshot: start\n");
  99. oneshot_start(mods);
  100. }
  101. else if (tap_count == TAPPING_TOGGLE) {
  102. dprint("MODS_TAP: Oneshot: toggle\n");
  103. oneshot_toggle();
  104. }
  105. else {
  106. dprint("MODS_TAP: Oneshot: cancel&add_mods\n");
  107. // double tap cancels oneshot and works as normal modifier.
  108. oneshot_cancel();
  109. add_mods(mods);
  110. }
  111. } else {
  112. if (tap_count == 0) {
  113. dprint("MODS_TAP: Oneshot: cancel/del_mods\n");
  114. // cancel oneshot on hold
  115. oneshot_cancel();
  116. del_mods(mods);
  117. }
  118. else if (tap_count == 1) {
  119. dprint("MODS_TAP: Oneshot: del_mods\n");
  120. // retain Oneshot
  121. del_mods(mods);
  122. }
  123. else {
  124. dprint("MODS_TAP: Oneshot: del_mods\n");
  125. // cancel Mods
  126. del_mods(mods);
  127. }
  128. }
  129. break;
  130. #endif
  131. default:
  132. if (event.pressed) {
  133. if (tap_count > 0) {
  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. add_mods(mods);
  139. } else {
  140. dprint("MODS_TAP: Tap: register_code\n");
  141. register_code(action.key.code);
  142. }
  143. } else {
  144. dprint("MODS_TAP: No tap: add_mods\n");
  145. add_mods(mods);
  146. }
  147. } else {
  148. if (tap_count > 0) {
  149. dprint("MODS_TAP: Tap: unregister_code\n");
  150. unregister_code(action.key.code);
  151. } else {
  152. dprint("MODS_TAP: No tap: add_mods\n");
  153. del_mods(mods);
  154. }
  155. }
  156. break;
  157. }
  158. }
  159. break;
  160. #endif
  161. #ifdef EXTRAKEY_ENABLE
  162. /* other HID usage */
  163. case ACT_USAGE:
  164. switch (action.usage.page) {
  165. case PAGE_SYSTEM:
  166. if (event.pressed) {
  167. host_system_send(action.usage.code);
  168. } else {
  169. host_system_send(0);
  170. }
  171. break;
  172. case PAGE_CONSUMER:
  173. if (event.pressed) {
  174. host_consumer_send(action.usage.code);
  175. } else {
  176. host_consumer_send(0);
  177. }
  178. break;
  179. }
  180. break;
  181. #endif
  182. #ifdef MOUSEKEY_ENABLE
  183. /* Mouse key */
  184. case ACT_MOUSEKEY:
  185. if (event.pressed) {
  186. mousekey_on(action.key.code);
  187. mousekey_send();
  188. } else {
  189. mousekey_off(action.key.code);
  190. mousekey_send();
  191. }
  192. break;
  193. #endif
  194. #ifndef NO_ACTION_LAYER
  195. case ACT_LAYER:
  196. if (action.layer_bitop.on == 0) {
  197. /* Default Layer Bitwise Operation */
  198. if (!event.pressed) {
  199. uint8_t shift = action.layer_bitop.part*4;
  200. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  201. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  202. switch (action.layer_bitop.op) {
  203. case OP_BIT_AND: default_layer_and(bits | mask); break;
  204. case OP_BIT_OR: default_layer_or(bits | mask); break;
  205. case OP_BIT_XOR: default_layer_xor(bits | mask); break;
  206. case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
  207. }
  208. }
  209. } else {
  210. /* Layer Bitwise Operation */
  211. if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
  212. (action.layer_bitop.on & ON_RELEASE)) {
  213. uint8_t shift = action.layer_bitop.part*4;
  214. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  215. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  216. switch (action.layer_bitop.op) {
  217. case OP_BIT_AND: layer_and(bits | mask); break;
  218. case OP_BIT_OR: layer_or(bits | mask); break;
  219. case OP_BIT_XOR: layer_xor(bits | mask); break;
  220. case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
  221. }
  222. }
  223. }
  224. break;
  225. #ifndef NO_ACTION_TAPPING
  226. case ACT_LAYER_TAP:
  227. case ACT_LAYER_TAP1:
  228. switch (action.layer_tap.code) {
  229. case OP_TAP_TOGGLE:
  230. /* tap toggle */
  231. if (event.pressed) {
  232. if (tap_count < TAPPING_TOGGLE) {
  233. layer_invert(action.layer_tap.val);
  234. }
  235. } else {
  236. if (tap_count <= TAPPING_TOGGLE) {
  237. layer_invert(action.layer_tap.val);
  238. }
  239. }
  240. break;
  241. case OP_ON_OFF:
  242. event.pressed ? layer_on(action.layer_tap.val) :
  243. layer_off(action.layer_tap.val);
  244. break;
  245. case OP_OFF_ON:
  246. event.pressed ? layer_off(action.layer_tap.val) :
  247. layer_on(action.layer_tap.val);
  248. break;
  249. case OP_SET_CLEAR:
  250. event.pressed ? layer_move(action.layer_tap.val) :
  251. layer_clear();
  252. break;
  253. default:
  254. /* tap key */
  255. if (event.pressed) {
  256. if (tap_count > 0) {
  257. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  258. register_code(action.layer_tap.code);
  259. } else {
  260. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  261. layer_on(action.layer_tap.val);
  262. }
  263. } else {
  264. if (tap_count > 0) {
  265. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  266. unregister_code(action.layer_tap.code);
  267. } else {
  268. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  269. layer_off(action.layer_tap.val);
  270. }
  271. }
  272. break;
  273. }
  274. break;
  275. #endif
  276. #endif
  277. /* Extentions */
  278. #ifndef NO_ACTION_MACRO
  279. case ACT_MACRO:
  280. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  281. break;
  282. #endif
  283. case ACT_COMMAND:
  284. break;
  285. #ifndef NO_ACTION_FUNCTION
  286. case ACT_FUNCTION:
  287. action_function(record, action.func.id, action.func.opt);
  288. break;
  289. #endif
  290. default:
  291. break;
  292. }
  293. }
  294. /*
  295. * Utilities for actions.
  296. */
  297. void register_code(uint8_t code)
  298. {
  299. if (code == KC_NO) {
  300. return;
  301. }
  302. #ifdef CAPSLOCK_LOCKING_ENABLE
  303. else if (KC_LOCKING_CAPS == code) {
  304. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  305. // Resync: ignore if caps lock already is on
  306. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  307. #endif
  308. host_add_key(KC_CAPSLOCK);
  309. host_send_keyboard_report();
  310. host_del_key(KC_CAPSLOCK);
  311. host_send_keyboard_report();
  312. }
  313. #endif
  314. else if IS_KEY(code) {
  315. // TODO: should push command_proc out of this block?
  316. if (command_proc(code)) return;
  317. #ifndef NO_ACTION_ONESHOT
  318. if (oneshot_state.mods && !oneshot_state.disabled) {
  319. uint8_t tmp_mods = host_get_mods();
  320. host_add_mods(oneshot_state.mods);
  321. host_add_key(code);
  322. host_send_keyboard_report();
  323. host_set_mods(tmp_mods);
  324. oneshot_cancel();
  325. } else
  326. #endif
  327. {
  328. host_add_key(code);
  329. host_send_keyboard_report();
  330. }
  331. }
  332. else if IS_MOD(code) {
  333. host_add_mods(MOD_BIT(code));
  334. host_send_keyboard_report();
  335. }
  336. }
  337. void unregister_code(uint8_t code)
  338. {
  339. if (code == KC_NO) {
  340. return;
  341. }
  342. #ifdef CAPSLOCK_LOCKING_ENABLE
  343. else if (KC_LOCKING_CAPS == code) {
  344. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  345. // Resync: ignore if caps lock already is off
  346. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  347. #endif
  348. host_add_key(KC_CAPSLOCK);
  349. host_send_keyboard_report();
  350. host_del_key(KC_CAPSLOCK);
  351. host_send_keyboard_report();
  352. }
  353. #endif
  354. else if IS_KEY(code) {
  355. host_del_key(code);
  356. host_send_keyboard_report();
  357. }
  358. else if IS_MOD(code) {
  359. host_del_mods(MOD_BIT(code));
  360. host_send_keyboard_report();
  361. }
  362. }
  363. void add_mods(uint8_t mods)
  364. {
  365. if (mods) {
  366. host_add_mods(mods);
  367. host_send_keyboard_report();
  368. }
  369. }
  370. void del_mods(uint8_t mods)
  371. {
  372. if (mods) {
  373. host_del_mods(mods);
  374. host_send_keyboard_report();
  375. }
  376. }
  377. void set_mods(uint8_t mods)
  378. {
  379. host_set_mods(mods);
  380. host_send_keyboard_report();
  381. }
  382. void clear_keyboard(void)
  383. {
  384. host_clear_mods();
  385. clear_keyboard_but_mods();
  386. }
  387. void clear_keyboard_but_mods(void)
  388. {
  389. host_clear_keys();
  390. host_send_keyboard_report();
  391. #ifdef MOUSEKEY_ENABLE
  392. mousekey_clear();
  393. mousekey_send();
  394. #endif
  395. #ifdef EXTRAKEY_ENABLE
  396. host_system_send(0);
  397. host_consumer_send(0);
  398. #endif
  399. }
  400. bool sending_anykey(void)
  401. {
  402. return (host_has_anykey() || host_mouse_in_use() ||
  403. host_last_sysytem_report() || host_last_consumer_report());
  404. }
  405. bool is_tap_key(key_t key)
  406. {
  407. action_t action = layer_switch_get_action(key);
  408. switch (action.kind.id) {
  409. case ACT_LMODS_TAP:
  410. case ACT_RMODS_TAP:
  411. case ACT_LAYER_TAP:
  412. case ACT_LAYER_TAP1:
  413. return true;
  414. case ACT_MACRO:
  415. case ACT_FUNCTION:
  416. if (action.func.opt & FUNC_TAP) { return true; }
  417. return false;
  418. }
  419. return false;
  420. }
  421. /*
  422. * debug print
  423. */
  424. void debug_event(keyevent_t event)
  425. {
  426. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  427. }
  428. void debug_record(keyrecord_t record)
  429. {
  430. debug_event(record.event);
  431. #ifndef NO_ACTION_TAPPING
  432. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  433. #endif
  434. }
  435. void debug_action(action_t action)
  436. {
  437. switch (action.kind.id) {
  438. case ACT_LMODS: dprint("ACT_LMODS"); break;
  439. case ACT_RMODS: dprint("ACT_RMODS"); break;
  440. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  441. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  442. case ACT_USAGE: dprint("ACT_USAGE"); break;
  443. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  444. case ACT_LAYER: dprint("ACT_LAYER"); break;
  445. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  446. case ACT_LAYER_TAP1: dprint("ACT_LAYER_TAP1"); break;
  447. case ACT_MACRO: dprint("ACT_MACRO"); break;
  448. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  449. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  450. default: dprint("UNKNOWN"); break;
  451. }
  452. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  453. }