action_util.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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) {
  59. return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
  60. }
  61. #else
  62. bool has_oneshot_mods_timed_out(void) {
  63. return false;
  64. }
  65. #endif
  66. #endif
  67. /* oneshot layer */
  68. #ifndef NO_ACTION_ONESHOT
  69. /** \brief oneshot_layer_data bits
  70. * LLLL LSSS
  71. * where:
  72. * L => are layer bits
  73. * S => oneshot state bits
  74. */
  75. static int8_t oneshot_layer_data = 0;
  76. inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
  77. inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
  78. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  79. static uint16_t oneshot_layer_time = 0;
  80. inline bool has_oneshot_layer_timed_out() {
  81. return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
  82. !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
  83. }
  84. #endif
  85. /** \brief Set oneshot layer
  86. *
  87. * FIXME: needs doc
  88. */
  89. void set_oneshot_layer(uint8_t layer, uint8_t state)
  90. {
  91. oneshot_layer_data = layer << 3 | state;
  92. layer_on(layer);
  93. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  94. oneshot_layer_time = timer_read();
  95. #endif
  96. oneshot_layer_changed_kb(get_oneshot_layer());
  97. }
  98. /** \brief Reset oneshot layer
  99. *
  100. * FIXME: needs doc
  101. */
  102. void reset_oneshot_layer(void) {
  103. oneshot_layer_data = 0;
  104. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  105. oneshot_layer_time = 0;
  106. #endif
  107. oneshot_layer_changed_kb(get_oneshot_layer());
  108. }
  109. /** \brief Clear oneshot layer
  110. *
  111. * FIXME: needs doc
  112. */
  113. void clear_oneshot_layer_state(oneshot_fullfillment_t state)
  114. {
  115. uint8_t start_state = oneshot_layer_data;
  116. oneshot_layer_data &= ~state;
  117. if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
  118. layer_off(get_oneshot_layer());
  119. reset_oneshot_layer();
  120. }
  121. }
  122. /** \brief Is oneshot layer active
  123. *
  124. * FIXME: needs doc
  125. */
  126. bool is_oneshot_layer_active(void)
  127. {
  128. return get_oneshot_layer_state();
  129. }
  130. #endif
  131. /** \brief Send keyboard report
  132. *
  133. * FIXME: needs doc
  134. */
  135. void send_keyboard_report(void) {
  136. keyboard_report->mods = real_mods;
  137. keyboard_report->mods |= weak_mods;
  138. keyboard_report->mods |= macro_mods;
  139. #ifndef NO_ACTION_ONESHOT
  140. if (oneshot_mods) {
  141. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  142. if (has_oneshot_mods_timed_out()) {
  143. dprintf("Oneshot: timeout\n");
  144. clear_oneshot_mods();
  145. }
  146. #endif
  147. keyboard_report->mods |= oneshot_mods;
  148. if (has_anykey(keyboard_report)) {
  149. clear_oneshot_mods();
  150. }
  151. }
  152. #endif
  153. host_keyboard_send(keyboard_report);
  154. }
  155. /** \brief Get mods
  156. *
  157. * FIXME: needs doc
  158. */
  159. uint8_t get_mods(void) { return real_mods; }
  160. /** \brief add mods
  161. *
  162. * FIXME: needs doc
  163. */
  164. void add_mods(uint8_t mods) { real_mods |= mods; }
  165. /** \brief del mods
  166. *
  167. * FIXME: needs doc
  168. */
  169. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  170. /** \brief set mods
  171. *
  172. * FIXME: needs doc
  173. */
  174. void set_mods(uint8_t mods) { real_mods = mods; }
  175. /** \brief clear mods
  176. *
  177. * FIXME: needs doc
  178. */
  179. void clear_mods(void) { real_mods = 0; }
  180. /** \brief get weak mods
  181. *
  182. * FIXME: needs doc
  183. */
  184. uint8_t get_weak_mods(void) { return weak_mods; }
  185. /** \brief add weak mods
  186. *
  187. * FIXME: needs doc
  188. */
  189. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  190. /** \brief del weak mods
  191. *
  192. * FIXME: needs doc
  193. */
  194. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  195. /** \brief set weak mods
  196. *
  197. * FIXME: needs doc
  198. */
  199. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  200. /** \brief clear weak mods
  201. *
  202. * FIXME: needs doc
  203. */
  204. void clear_weak_mods(void) { weak_mods = 0; }
  205. /* macro modifier */
  206. /** \brief get macro mods
  207. *
  208. * FIXME: needs doc
  209. */
  210. uint8_t get_macro_mods(void) { return macro_mods; }
  211. /** \brief add macro mods
  212. *
  213. * FIXME: needs doc
  214. */
  215. void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
  216. /** \brief del macro mods
  217. *
  218. * FIXME: needs doc
  219. */
  220. void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
  221. /** \brief set macro mods
  222. *
  223. * FIXME: needs doc
  224. */
  225. void set_macro_mods(uint8_t mods) { macro_mods = mods; }
  226. /** \brief clear macro mods
  227. *
  228. * FIXME: needs doc
  229. */
  230. void clear_macro_mods(void) { macro_mods = 0; }
  231. #ifndef NO_ACTION_ONESHOT
  232. /** \brief set oneshot mods
  233. *
  234. * FIXME: needs doc
  235. */
  236. void set_oneshot_mods(uint8_t mods) {
  237. if (oneshot_mods != mods) {
  238. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  239. oneshot_time = timer_read();
  240. #endif
  241. oneshot_mods = mods;
  242. oneshot_mods_changed_kb(mods);
  243. }
  244. }
  245. /** \brief clear oneshot mods
  246. *
  247. * FIXME: needs doc
  248. */
  249. void clear_oneshot_mods(void) {
  250. if (oneshot_mods) {
  251. oneshot_mods = 0;
  252. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  253. oneshot_time = 0;
  254. #endif
  255. oneshot_mods_changed_kb(oneshot_mods);
  256. }
  257. }
  258. /** \brief get oneshot mods
  259. *
  260. * FIXME: needs doc
  261. */
  262. uint8_t get_oneshot_mods(void)
  263. {
  264. return oneshot_mods;
  265. }
  266. #endif
  267. /** \brief Called when the one shot modifiers have been changed.
  268. *
  269. * \param mods Contains the active modifiers active after the change.
  270. */
  271. __attribute__((weak))
  272. void oneshot_locked_mods_changed_user(uint8_t mods) { }
  273. /** \brief Called when the locked one shot modifiers have been changed.
  274. *
  275. * \param mods Contains the active modifiers active after the change.
  276. */
  277. __attribute__((weak))
  278. void oneshot_locked_mods_changed_kb(uint8_t mods) {
  279. oneshot_locked_mods_changed_user(mods);
  280. }
  281. /** \brief Called when the one shot modifiers have been changed.
  282. *
  283. * \param mods Contains the active modifiers active after the change.
  284. */
  285. __attribute__((weak))
  286. void oneshot_mods_changed_user(uint8_t mods) { }
  287. /** \brief Called when the one shot modifiers have been changed.
  288. *
  289. * \param mods Contains the active modifiers active after the change.
  290. */
  291. __attribute__((weak))
  292. void oneshot_mods_changed_kb(uint8_t mods) {
  293. oneshot_mods_changed_user(mods);
  294. }
  295. /** \brief Called when the one shot layers have been changed.
  296. *
  297. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  298. */
  299. __attribute__((weak))
  300. void oneshot_layer_changed_user(uint8_t layer) { }
  301. /** \brief Called when the one shot layers have been changed.
  302. *
  303. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  304. */
  305. __attribute__((weak))
  306. void oneshot_layer_changed_kb(uint8_t layer) {
  307. oneshot_layer_changed_user(layer);
  308. }
  309. /** \brief inspect keyboard state
  310. *
  311. * FIXME: needs doc
  312. */
  313. uint8_t has_anymod(void)
  314. {
  315. return bitpop(real_mods);
  316. }