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.

105 lines
2.7 KiB

  1. /* Copyright 2021 Joshua T.
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "caps_word.h"
  17. static bool is_caps_word_on = false;
  18. bool is_caps_word_enabled(void) {
  19. return is_caps_word_on;
  20. }
  21. void enable_caps_word(void) {
  22. if (is_caps_word_on) return;
  23. is_caps_word_on = true;
  24. tap_code(KC_CAPS);
  25. }
  26. void disable_caps_word(void) {
  27. if (!is_caps_word_on) return;
  28. is_caps_word_on = false;
  29. tap_code(KC_CAPS);
  30. }
  31. void toggle_caps_word(void) {
  32. if (is_caps_word_on) {
  33. disable_caps_word();
  34. }
  35. else {
  36. enable_caps_word();
  37. }
  38. }
  39. bool should_terminate_caps_word(uint16_t keycode, const keyrecord_t *record) {
  40. switch (keycode) {
  41. // Keycodes which should not disable caps word mode
  42. case KC_A ... KC_Z:
  43. case KC_1 ... KC_0:
  44. case KC_MINS:
  45. case KC_UNDS:
  46. case KC_BSPC:
  47. return false;
  48. default:
  49. if (record->event.pressed) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. // Should be unreachable
  55. return false;
  56. }
  57. bool process_record_caps_word(uint16_t keycode, const keyrecord_t *record) {
  58. // Nothing in this function acts on key release
  59. if (!record->event.pressed) {
  60. return true;
  61. }
  62. // Handle the custom keycodes that go with this feature
  63. if (keycode == CAPWORD) {
  64. enable_caps_word();
  65. return false;
  66. }
  67. // If the behavior isn't enabled and the keypress isn't a keycode to
  68. // toggle the behavior, allow QMK to handle the keypress as usual
  69. if (!is_caps_word_on) {
  70. return true;
  71. }
  72. // Get the base keycode of a mod or layer tap key
  73. switch (keycode) {
  74. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  75. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  76. case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
  77. // Earlier return if this has not been considered tapped yet
  78. if (record->tap.count == 0)
  79. return true;
  80. keycode = keycode & 0xFF;
  81. break;
  82. default:
  83. break;
  84. }
  85. if (should_terminate_caps_word(keycode, record)) {
  86. disable_caps_word();
  87. }
  88. return true;
  89. }