action_util.c 12 KB

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