action_util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. Copyright 2013 Jun Wako <wakojun@gmail.com>
  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 "report.h"
  16. #include "debug.h"
  17. #include "action_util.h"
  18. #include "action_layer.h"
  19. #include "timer.h"
  20. #include "keycode_config.h"
  21. #include <string.h>
  22. extern keymap_config_t keymap_config;
  23. static uint8_t real_mods = 0;
  24. static uint8_t weak_mods = 0;
  25. #ifdef KEY_OVERRIDE_ENABLE
  26. static uint8_t weak_override_mods = 0;
  27. static uint8_t suppressed_mods = 0;
  28. #endif
  29. // TODO: pointer variable is not needed
  30. // report_keyboard_t keyboard_report = {};
  31. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  32. extern inline void add_key(uint8_t key);
  33. extern inline void del_key(uint8_t key);
  34. extern inline void clear_keys(void);
  35. #ifndef NO_ACTION_ONESHOT
  36. static uint8_t oneshot_mods = 0;
  37. static uint8_t oneshot_locked_mods = 0;
  38. uint8_t get_oneshot_locked_mods(void) {
  39. return oneshot_locked_mods;
  40. }
  41. void set_oneshot_locked_mods(uint8_t mods) {
  42. if (mods != oneshot_locked_mods) {
  43. oneshot_locked_mods = mods;
  44. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  45. }
  46. }
  47. void clear_oneshot_locked_mods(void) {
  48. if (oneshot_locked_mods) {
  49. oneshot_locked_mods = 0;
  50. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  51. }
  52. }
  53. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  54. static uint16_t oneshot_time = 0;
  55. bool has_oneshot_mods_timed_out(void) {
  56. return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
  57. }
  58. # else
  59. bool has_oneshot_mods_timed_out(void) {
  60. return false;
  61. }
  62. # endif
  63. #endif
  64. /* oneshot layer */
  65. #ifndef NO_ACTION_ONESHOT
  66. /** \brief oneshot_layer_data bits
  67. * LLLL LSSS
  68. * where:
  69. * L => are layer bits
  70. * S => oneshot state bits
  71. */
  72. static int8_t oneshot_layer_data = 0;
  73. inline uint8_t get_oneshot_layer(void) {
  74. return oneshot_layer_data >> 3;
  75. }
  76. inline uint8_t get_oneshot_layer_state(void) {
  77. return oneshot_layer_data & 0b111;
  78. }
  79. # ifdef SWAP_HANDS_ENABLE
  80. enum {
  81. SHO_OFF,
  82. SHO_ACTIVE, // Swap hands button was pressed, and we didn't send any swapped keys yet
  83. SHO_PRESSED, // Swap hands button is currently pressed
  84. SHO_USED, // Swap hands button is still pressed, and we already sent swapped keys
  85. } swap_hands_oneshot = SHO_OFF;
  86. # endif
  87. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  88. static uint16_t oneshot_layer_time = 0;
  89. inline bool has_oneshot_layer_timed_out() {
  90. return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
  91. }
  92. # ifdef SWAP_HANDS_ENABLE
  93. static uint16_t oneshot_swaphands_time = 0;
  94. inline bool has_oneshot_swaphands_timed_out() {
  95. return TIMER_DIFF_16(timer_read(), oneshot_swaphands_time) >= ONESHOT_TIMEOUT && (swap_hands_oneshot == SHO_ACTIVE);
  96. }
  97. # endif
  98. # endif
  99. # ifdef SWAP_HANDS_ENABLE
  100. void set_oneshot_swaphands(void) {
  101. swap_hands_oneshot = SHO_PRESSED;
  102. swap_hands = true;
  103. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  104. oneshot_swaphands_time = timer_read();
  105. if (oneshot_layer_time != 0) {
  106. oneshot_layer_time = oneshot_swaphands_time;
  107. }
  108. # endif
  109. }
  110. void release_oneshot_swaphands(void) {
  111. if (swap_hands_oneshot == SHO_PRESSED) {
  112. swap_hands_oneshot = SHO_ACTIVE;
  113. }
  114. if (swap_hands_oneshot == SHO_USED) {
  115. clear_oneshot_swaphands();
  116. }
  117. }
  118. void use_oneshot_swaphands(void) {
  119. if (swap_hands_oneshot == SHO_PRESSED) {
  120. swap_hands_oneshot = SHO_USED;
  121. }
  122. if (swap_hands_oneshot == SHO_ACTIVE) {
  123. clear_oneshot_swaphands();
  124. }
  125. }
  126. void clear_oneshot_swaphands(void) {
  127. swap_hands_oneshot = SHO_OFF;
  128. swap_hands = false;
  129. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  130. oneshot_swaphands_time = 0;
  131. # endif
  132. }
  133. # endif
  134. /** \brief Set oneshot layer
  135. *
  136. * FIXME: needs doc
  137. */
  138. void set_oneshot_layer(uint8_t layer, uint8_t state) {
  139. if (keymap_config.oneshot_enable) {
  140. oneshot_layer_data = layer << 3 | state;
  141. layer_on(layer);
  142. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  143. oneshot_layer_time = timer_read();
  144. # endif
  145. oneshot_layer_changed_kb(get_oneshot_layer());
  146. } else {
  147. layer_on(layer);
  148. }
  149. }
  150. /** \brief Reset oneshot layer
  151. *
  152. * FIXME: needs doc
  153. */
  154. void reset_oneshot_layer(void) {
  155. oneshot_layer_data = 0;
  156. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  157. oneshot_layer_time = 0;
  158. # endif
  159. oneshot_layer_changed_kb(get_oneshot_layer());
  160. }
  161. /** \brief Clear oneshot layer
  162. *
  163. * FIXME: needs doc
  164. */
  165. void clear_oneshot_layer_state(oneshot_fullfillment_t state) {
  166. uint8_t start_state = oneshot_layer_data;
  167. oneshot_layer_data &= ~state;
  168. if ((!get_oneshot_layer_state() && start_state != oneshot_layer_data) && keymap_config.oneshot_enable) {
  169. layer_off(get_oneshot_layer());
  170. reset_oneshot_layer();
  171. }
  172. }
  173. /** \brief Is oneshot layer active
  174. *
  175. * FIXME: needs doc
  176. */
  177. bool is_oneshot_layer_active(void) {
  178. return get_oneshot_layer_state();
  179. }
  180. /** \brief set oneshot
  181. *
  182. * FIXME: needs doc
  183. */
  184. void oneshot_set(bool active) {
  185. if (keymap_config.oneshot_enable != active) {
  186. keymap_config.oneshot_enable = active;
  187. eeconfig_update_keymap(keymap_config.raw);
  188. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  189. dprintf("Oneshot: active: %d\n", active);
  190. }
  191. }
  192. /** \brief toggle oneshot
  193. *
  194. * FIXME: needs doc
  195. */
  196. void oneshot_toggle(void) {
  197. oneshot_set(!keymap_config.oneshot_enable);
  198. }
  199. /** \brief enable oneshot
  200. *
  201. * FIXME: needs doc
  202. */
  203. void oneshot_enable(void) {
  204. oneshot_set(true);
  205. }
  206. /** \brief disable oneshot
  207. *
  208. * FIXME: needs doc
  209. */
  210. void oneshot_disable(void) {
  211. oneshot_set(false);
  212. }
  213. bool is_oneshot_enabled(void) {
  214. return keymap_config.oneshot_enable;
  215. }
  216. #endif
  217. /** \brief Send keyboard report
  218. *
  219. * FIXME: needs doc
  220. */
  221. void send_keyboard_report(void) {
  222. keyboard_report->mods = real_mods;
  223. keyboard_report->mods |= weak_mods;
  224. #ifndef NO_ACTION_ONESHOT
  225. if (oneshot_mods) {
  226. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  227. if (has_oneshot_mods_timed_out()) {
  228. dprintf("Oneshot: timeout\n");
  229. clear_oneshot_mods();
  230. }
  231. # endif
  232. keyboard_report->mods |= oneshot_mods;
  233. if (has_anykey(keyboard_report)) {
  234. clear_oneshot_mods();
  235. }
  236. }
  237. #endif
  238. #ifdef KEY_OVERRIDE_ENABLE
  239. // These need to be last to be able to properly control key overrides
  240. keyboard_report->mods &= ~suppressed_mods;
  241. keyboard_report->mods |= weak_override_mods;
  242. #endif
  243. #ifdef PROTOCOL_VUSB
  244. host_keyboard_send(keyboard_report);
  245. #else
  246. static report_keyboard_t last_report;
  247. /* Only send the report if there are changes to propagate to the host. */
  248. if (memcmp(keyboard_report, &last_report, sizeof(report_keyboard_t)) != 0) {
  249. memcpy(&last_report, keyboard_report, sizeof(report_keyboard_t));
  250. host_keyboard_send(keyboard_report);
  251. }
  252. #endif
  253. }
  254. /** \brief Get mods
  255. *
  256. * FIXME: needs doc
  257. */
  258. uint8_t get_mods(void) {
  259. return real_mods;
  260. }
  261. /** \brief add mods
  262. *
  263. * FIXME: needs doc
  264. */
  265. void add_mods(uint8_t mods) {
  266. real_mods |= mods;
  267. }
  268. /** \brief del mods
  269. *
  270. * FIXME: needs doc
  271. */
  272. void del_mods(uint8_t mods) {
  273. real_mods &= ~mods;
  274. }
  275. /** \brief set mods
  276. *
  277. * FIXME: needs doc
  278. */
  279. void set_mods(uint8_t mods) {
  280. real_mods = mods;
  281. }
  282. /** \brief clear mods
  283. *
  284. * FIXME: needs doc
  285. */
  286. void clear_mods(void) {
  287. real_mods = 0;
  288. }
  289. /** \brief get weak mods
  290. *
  291. * FIXME: needs doc
  292. */
  293. uint8_t get_weak_mods(void) {
  294. return weak_mods;
  295. }
  296. /** \brief add weak mods
  297. *
  298. * FIXME: needs doc
  299. */
  300. void add_weak_mods(uint8_t mods) {
  301. weak_mods |= mods;
  302. }
  303. /** \brief del weak mods
  304. *
  305. * FIXME: needs doc
  306. */
  307. void del_weak_mods(uint8_t mods) {
  308. weak_mods &= ~mods;
  309. }
  310. /** \brief set weak mods
  311. *
  312. * FIXME: needs doc
  313. */
  314. void set_weak_mods(uint8_t mods) {
  315. weak_mods = mods;
  316. }
  317. /** \brief clear weak mods
  318. *
  319. * FIXME: needs doc
  320. */
  321. void clear_weak_mods(void) {
  322. weak_mods = 0;
  323. }
  324. #ifdef KEY_OVERRIDE_ENABLE
  325. /** \brief set weak mods used by key overrides. DO not call this manually
  326. */
  327. void set_weak_override_mods(uint8_t mods) {
  328. weak_override_mods = mods;
  329. }
  330. /** \brief clear weak mods used by key overrides. DO not call this manually
  331. */
  332. void clear_weak_override_mods(void) {
  333. weak_override_mods = 0;
  334. }
  335. /** \brief set suppressed mods used by key overrides. DO not call this manually
  336. */
  337. void set_suppressed_override_mods(uint8_t mods) {
  338. suppressed_mods = mods;
  339. }
  340. /** \brief clear suppressed mods used by key overrides. DO not call this manually
  341. */
  342. void clear_suppressed_override_mods(void) {
  343. suppressed_mods = 0;
  344. }
  345. #endif
  346. #ifndef NO_ACTION_ONESHOT
  347. /** \brief get oneshot mods
  348. *
  349. * FIXME: needs doc
  350. */
  351. uint8_t get_oneshot_mods(void) {
  352. return oneshot_mods;
  353. }
  354. void add_oneshot_mods(uint8_t mods) {
  355. if ((oneshot_mods & mods) != mods) {
  356. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  357. oneshot_time = timer_read();
  358. # endif
  359. oneshot_mods |= mods;
  360. oneshot_mods_changed_kb(mods);
  361. }
  362. }
  363. void del_oneshot_mods(uint8_t mods) {
  364. if (oneshot_mods & mods) {
  365. oneshot_mods &= ~mods;
  366. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  367. oneshot_time = oneshot_mods ? timer_read() : 0;
  368. # endif
  369. oneshot_mods_changed_kb(oneshot_mods);
  370. }
  371. }
  372. /** \brief set oneshot mods
  373. *
  374. * FIXME: needs doc
  375. */
  376. void set_oneshot_mods(uint8_t mods) {
  377. if (keymap_config.oneshot_enable) {
  378. if (oneshot_mods != mods) {
  379. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  380. oneshot_time = timer_read();
  381. # endif
  382. oneshot_mods = mods;
  383. oneshot_mods_changed_kb(mods);
  384. }
  385. }
  386. }
  387. /** \brief clear oneshot mods
  388. *
  389. * FIXME: needs doc
  390. */
  391. void clear_oneshot_mods(void) {
  392. if (oneshot_mods) {
  393. oneshot_mods = 0;
  394. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  395. oneshot_time = 0;
  396. # endif
  397. oneshot_mods_changed_kb(oneshot_mods);
  398. }
  399. }
  400. #endif
  401. /** \brief Called when the one shot modifiers have been changed.
  402. *
  403. * \param mods Contains the active modifiers active after the change.
  404. */
  405. __attribute__((weak)) void oneshot_locked_mods_changed_user(uint8_t mods) {}
  406. /** \brief Called when the locked one shot modifiers have been changed.
  407. *
  408. * \param mods Contains the active modifiers active after the change.
  409. */
  410. __attribute__((weak)) void oneshot_locked_mods_changed_kb(uint8_t mods) {
  411. oneshot_locked_mods_changed_user(mods);
  412. }
  413. /** \brief Called when the one shot modifiers have been changed.
  414. *
  415. * \param mods Contains the active modifiers active after the change.
  416. */
  417. __attribute__((weak)) void oneshot_mods_changed_user(uint8_t mods) {}
  418. /** \brief Called when the one shot modifiers have been changed.
  419. *
  420. * \param mods Contains the active modifiers active after the change.
  421. */
  422. __attribute__((weak)) void oneshot_mods_changed_kb(uint8_t mods) {
  423. oneshot_mods_changed_user(mods);
  424. }
  425. /** \brief Called when the one shot layers have been changed.
  426. *
  427. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  428. */
  429. __attribute__((weak)) void oneshot_layer_changed_user(uint8_t layer) {}
  430. /** \brief Called when the one shot layers have been changed.
  431. *
  432. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  433. */
  434. __attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) {
  435. oneshot_layer_changed_user(layer);
  436. }
  437. /** \brief inspect keyboard state
  438. *
  439. * FIXME: needs doc
  440. */
  441. uint8_t has_anymod(void) {
  442. return bitpop(real_mods);
  443. }