action_util.c 12 KB

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