action_util.c 11 KB

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