action_util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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_disable) {
  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_disable) {
  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. const bool disable = !active;
  186. if (keymap_config.oneshot_disable != disable) {
  187. keymap_config.oneshot_disable = disable;
  188. eeconfig_update_keymap(keymap_config.raw);
  189. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  190. dprintf("Oneshot: active: %d\n", active);
  191. }
  192. }
  193. /** \brief toggle oneshot
  194. *
  195. * FIXME: needs doc
  196. */
  197. void oneshot_toggle(void) {
  198. oneshot_set(!keymap_config.oneshot_disable);
  199. }
  200. /** \brief enable oneshot
  201. *
  202. * FIXME: needs doc
  203. */
  204. void oneshot_enable(void) {
  205. oneshot_set(true);
  206. }
  207. /** \brief disable oneshot
  208. *
  209. * FIXME: needs doc
  210. */
  211. void oneshot_disable(void) {
  212. oneshot_set(false);
  213. }
  214. bool is_oneshot_enabled(void) {
  215. return !keymap_config.oneshot_disable;
  216. }
  217. #endif
  218. /** \brief Send keyboard report
  219. *
  220. * FIXME: needs doc
  221. */
  222. void send_keyboard_report(void) {
  223. keyboard_report->mods = real_mods;
  224. keyboard_report->mods |= weak_mods;
  225. #ifndef NO_ACTION_ONESHOT
  226. if (oneshot_mods) {
  227. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  228. if (has_oneshot_mods_timed_out()) {
  229. dprintf("Oneshot: timeout\n");
  230. clear_oneshot_mods();
  231. }
  232. # endif
  233. keyboard_report->mods |= oneshot_mods;
  234. if (has_anykey(keyboard_report)) {
  235. clear_oneshot_mods();
  236. }
  237. }
  238. #endif
  239. #ifdef KEY_OVERRIDE_ENABLE
  240. // These need to be last to be able to properly control key overrides
  241. keyboard_report->mods &= ~suppressed_mods;
  242. keyboard_report->mods |= weak_override_mods;
  243. #endif
  244. #ifdef PROTOCOL_VUSB
  245. host_keyboard_send(keyboard_report);
  246. #else
  247. static report_keyboard_t last_report;
  248. /* Only send the report if there are changes to propagate to the host. */
  249. if (memcmp(keyboard_report, &last_report, sizeof(report_keyboard_t)) != 0) {
  250. memcpy(&last_report, keyboard_report, sizeof(report_keyboard_t));
  251. host_keyboard_send(keyboard_report);
  252. }
  253. #endif
  254. }
  255. /** \brief Get mods
  256. *
  257. * FIXME: needs doc
  258. */
  259. uint8_t get_mods(void) {
  260. return real_mods;
  261. }
  262. /** \brief add mods
  263. *
  264. * FIXME: needs doc
  265. */
  266. void add_mods(uint8_t mods) {
  267. real_mods |= mods;
  268. }
  269. /** \brief del mods
  270. *
  271. * FIXME: needs doc
  272. */
  273. void del_mods(uint8_t mods) {
  274. real_mods &= ~mods;
  275. }
  276. /** \brief set mods
  277. *
  278. * FIXME: needs doc
  279. */
  280. void set_mods(uint8_t mods) {
  281. real_mods = mods;
  282. }
  283. /** \brief clear mods
  284. *
  285. * FIXME: needs doc
  286. */
  287. void clear_mods(void) {
  288. real_mods = 0;
  289. }
  290. /** \brief get weak mods
  291. *
  292. * FIXME: needs doc
  293. */
  294. uint8_t get_weak_mods(void) {
  295. return weak_mods;
  296. }
  297. /** \brief add weak mods
  298. *
  299. * FIXME: needs doc
  300. */
  301. void add_weak_mods(uint8_t mods) {
  302. weak_mods |= mods;
  303. }
  304. /** \brief del weak mods
  305. *
  306. * FIXME: needs doc
  307. */
  308. void del_weak_mods(uint8_t mods) {
  309. weak_mods &= ~mods;
  310. }
  311. /** \brief set weak mods
  312. *
  313. * FIXME: needs doc
  314. */
  315. void set_weak_mods(uint8_t mods) {
  316. weak_mods = mods;
  317. }
  318. /** \brief clear weak mods
  319. *
  320. * FIXME: needs doc
  321. */
  322. void clear_weak_mods(void) {
  323. weak_mods = 0;
  324. }
  325. #ifdef KEY_OVERRIDE_ENABLE
  326. /** \brief set weak mods used by key overrides. DO not call this manually
  327. */
  328. void set_weak_override_mods(uint8_t mods) {
  329. weak_override_mods = mods;
  330. }
  331. /** \brief clear weak mods used by key overrides. DO not call this manually
  332. */
  333. void clear_weak_override_mods(void) {
  334. weak_override_mods = 0;
  335. }
  336. /** \brief set suppressed mods used by key overrides. DO not call this manually
  337. */
  338. void set_suppressed_override_mods(uint8_t mods) {
  339. suppressed_mods = mods;
  340. }
  341. /** \brief clear suppressed mods used by key overrides. DO not call this manually
  342. */
  343. void clear_suppressed_override_mods(void) {
  344. suppressed_mods = 0;
  345. }
  346. #endif
  347. #ifndef NO_ACTION_ONESHOT
  348. /** \brief get oneshot mods
  349. *
  350. * FIXME: needs doc
  351. */
  352. uint8_t get_oneshot_mods(void) {
  353. return oneshot_mods;
  354. }
  355. void add_oneshot_mods(uint8_t mods) {
  356. if ((oneshot_mods & mods) != mods) {
  357. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  358. oneshot_time = timer_read();
  359. # endif
  360. oneshot_mods |= mods;
  361. oneshot_mods_changed_kb(mods);
  362. }
  363. }
  364. void del_oneshot_mods(uint8_t mods) {
  365. if (oneshot_mods & mods) {
  366. oneshot_mods &= ~mods;
  367. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  368. oneshot_time = oneshot_mods ? timer_read() : 0;
  369. # endif
  370. oneshot_mods_changed_kb(oneshot_mods);
  371. }
  372. }
  373. /** \brief set oneshot mods
  374. *
  375. * FIXME: needs doc
  376. */
  377. void set_oneshot_mods(uint8_t mods) {
  378. if (!keymap_config.oneshot_disable) {
  379. if (oneshot_mods != mods) {
  380. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  381. oneshot_time = timer_read();
  382. # endif
  383. oneshot_mods = mods;
  384. oneshot_mods_changed_kb(mods);
  385. }
  386. }
  387. }
  388. /** \brief clear oneshot mods
  389. *
  390. * FIXME: needs doc
  391. */
  392. void clear_oneshot_mods(void) {
  393. if (oneshot_mods) {
  394. oneshot_mods = 0;
  395. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  396. oneshot_time = 0;
  397. # endif
  398. oneshot_mods_changed_kb(oneshot_mods);
  399. }
  400. }
  401. #endif
  402. /** \brief Called when the one shot modifiers have been changed.
  403. *
  404. * \param mods Contains the active modifiers active after the change.
  405. */
  406. __attribute__((weak)) void oneshot_locked_mods_changed_user(uint8_t mods) {}
  407. /** \brief Called when the locked one shot modifiers have been changed.
  408. *
  409. * \param mods Contains the active modifiers active after the change.
  410. */
  411. __attribute__((weak)) void oneshot_locked_mods_changed_kb(uint8_t mods) {
  412. oneshot_locked_mods_changed_user(mods);
  413. }
  414. /** \brief Called when the one shot modifiers have been changed.
  415. *
  416. * \param mods Contains the active modifiers active after the change.
  417. */
  418. __attribute__((weak)) void oneshot_mods_changed_user(uint8_t mods) {}
  419. /** \brief Called when the one shot modifiers have been changed.
  420. *
  421. * \param mods Contains the active modifiers active after the change.
  422. */
  423. __attribute__((weak)) void oneshot_mods_changed_kb(uint8_t mods) {
  424. oneshot_mods_changed_user(mods);
  425. }
  426. /** \brief Called when the one shot layers have been changed.
  427. *
  428. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  429. */
  430. __attribute__((weak)) void oneshot_layer_changed_user(uint8_t layer) {}
  431. /** \brief Called when the one shot layers have been changed.
  432. *
  433. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  434. */
  435. __attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) {
  436. oneshot_layer_changed_user(layer);
  437. }
  438. /** \brief inspect keyboard state
  439. *
  440. * FIXME: needs doc
  441. */
  442. uint8_t has_anymod(void) {
  443. return bitpop(real_mods);
  444. }