You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

  1. // Copyright 2021-2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include "caps_word.h"
  16. #include "timer.h"
  17. #include "action.h"
  18. #include "action_util.h"
  19. /** @brief True when Caps Word is active. */
  20. static bool caps_word_active = false;
  21. #if CAPS_WORD_IDLE_TIMEOUT > 0
  22. // Constrain timeout to a sensible range. With 16-bit timers, the longest
  23. // timeout possible is 32768 ms, rounded here to 30000 ms = half a minute.
  24. # if CAPS_WORD_IDLE_TIMEOUT < 100 || CAPS_WORD_IDLE_TIMEOUT > 30000
  25. # error "CAPS_WORD_IDLE_TIMEOUT must be between 100 and 30000 ms"
  26. # endif
  27. /** @brief Deadline for idle timeout. */
  28. static uint16_t idle_timer = 0;
  29. void caps_word_task(void) {
  30. if (caps_word_active && timer_expired(timer_read(), idle_timer)) {
  31. caps_word_off();
  32. }
  33. }
  34. void caps_word_reset_idle_timer(void) {
  35. idle_timer = timer_read() + CAPS_WORD_IDLE_TIMEOUT;
  36. }
  37. #else
  38. void caps_word_task(void) {}
  39. #endif // CAPS_WORD_IDLE_TIMEOUT > 0
  40. void caps_word_on(void) {
  41. if (caps_word_active) {
  42. return;
  43. }
  44. clear_mods();
  45. #ifndef NO_ACTION_ONESHOT
  46. clear_oneshot_mods();
  47. #endif // NO_ACTION_ONESHOT
  48. #if CAPS_WORD_IDLE_TIMEOUT > 0
  49. caps_word_reset_idle_timer();
  50. #endif // CAPS_WORD_IDLE_TIMEOUT > 0
  51. caps_word_active = true;
  52. caps_word_set_user(true);
  53. }
  54. void caps_word_off(void) {
  55. if (!caps_word_active) {
  56. return;
  57. }
  58. unregister_weak_mods(MOD_MASK_SHIFT); // Make sure weak shift is off.
  59. caps_word_active = false;
  60. caps_word_set_user(false);
  61. }
  62. void caps_word_toggle(void) {
  63. if (caps_word_active) {
  64. caps_word_off();
  65. } else {
  66. caps_word_on();
  67. }
  68. }
  69. bool is_caps_word_on(void) {
  70. return caps_word_active;
  71. }
  72. __attribute__((weak)) void caps_word_set_user(bool active) {}