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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

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