diff options
Diffstat (limited to 'users/replicaJunction/features/caps_word.c')
-rw-r--r-- | users/replicaJunction/features/caps_word.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/users/replicaJunction/features/caps_word.c b/users/replicaJunction/features/caps_word.c new file mode 100644 index 000000000..536da81ec --- /dev/null +++ b/users/replicaJunction/features/caps_word.c | |||
@@ -0,0 +1,105 @@ | |||
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 | |||
17 | #include "caps_word.h" | ||
18 | |||
19 | static bool is_caps_word_on = false; | ||
20 | |||
21 | bool is_caps_word_enabled(void) { | ||
22 | return is_caps_word_on; | ||
23 | } | ||
24 | |||
25 | void enable_caps_word(void) { | ||
26 | if (is_caps_word_on) return; | ||
27 | is_caps_word_on = true; | ||
28 | tap_code(KC_CAPS); | ||
29 | } | ||
30 | |||
31 | void disable_caps_word(void) { | ||
32 | if (!is_caps_word_on) return; | ||
33 | is_caps_word_on = false; | ||
34 | tap_code(KC_CAPS); | ||
35 | } | ||
36 | |||
37 | void toggle_caps_word(void) { | ||
38 | if (is_caps_word_on) { | ||
39 | disable_caps_word(); | ||
40 | } | ||
41 | else { | ||
42 | enable_caps_word(); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | bool should_terminate_caps_word(uint16_t keycode, const keyrecord_t *record) { | ||
47 | switch (keycode) { | ||
48 | // Keycodes which should not disable caps word mode | ||
49 | case KC_A ... KC_Z: | ||
50 | case KC_1 ... KC_0: | ||
51 | case KC_MINS: | ||
52 | case KC_UNDS: | ||
53 | case KC_BSPC: | ||
54 | return false; | ||
55 | |||
56 | default: | ||
57 | if (record->event.pressed) { | ||
58 | return true; | ||
59 | } | ||
60 | return false; | ||
61 | } | ||
62 | |||
63 | // Should be unreachable | ||
64 | return false; | ||
65 | } | ||
66 | |||
67 | |||
68 | bool process_record_caps_word(uint16_t keycode, const keyrecord_t *record) { | ||
69 | // Nothing in this function acts on key release | ||
70 | if (!record->event.pressed) { | ||
71 | return true; | ||
72 | } | ||
73 | |||
74 | // Handle the custom keycodes that go with this feature | ||
75 | if (keycode == CAPWORD) { | ||
76 | enable_caps_word(); | ||
77 | return false; | ||
78 | } | ||
79 | |||
80 | // If the behavior isn't enabled and the keypress isn't a keycode to | ||
81 | // toggle the behavior, allow QMK to handle the keypress as usual | ||
82 | if (!is_caps_word_on) { | ||
83 | return true; | ||
84 | } | ||
85 | |||
86 | // Get the base keycode of a mod or layer tap key | ||
87 | switch (keycode) { | ||
88 | case QK_MOD_TAP ... QK_MOD_TAP_MAX: | ||
89 | case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: | ||
90 | case QK_TAP_DANCE ... QK_TAP_DANCE_MAX: | ||
91 | // Earlier return if this has not been considered tapped yet | ||
92 | if (record->tap.count == 0) | ||
93 | return true; | ||
94 | keycode = keycode & 0xFF; | ||
95 | break; | ||
96 | default: | ||
97 | break; | ||
98 | } | ||
99 | |||
100 | if (should_terminate_caps_word(keycode, record)) { | ||
101 | disable_caps_word(); | ||
102 | } | ||
103 | |||
104 | return true; | ||
105 | } | ||