diff options
Diffstat (limited to 'keyboards/a_dux/keymaps/daliusd/oneshot.c')
-rw-r--r-- | keyboards/a_dux/keymaps/daliusd/oneshot.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/keyboards/a_dux/keymaps/daliusd/oneshot.c b/keyboards/a_dux/keymaps/daliusd/oneshot.c new file mode 100644 index 000000000..1e7b4d965 --- /dev/null +++ b/keyboards/a_dux/keymaps/daliusd/oneshot.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* Copyright 2021 @daliusd | ||
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 "print.h" | ||
17 | #include "oneshot.h" | ||
18 | |||
19 | void update_oneshot( | ||
20 | oneshot_state *state, | ||
21 | uint16_t mod, | ||
22 | uint16_t trigger, | ||
23 | uint16_t keycode, | ||
24 | keyrecord_t *record | ||
25 | ) { | ||
26 | if (keycode == trigger) { | ||
27 | if (record->event.pressed) { | ||
28 | // Trigger keydown | ||
29 | if (*state == os_up_unqueued) { | ||
30 | register_code(mod); | ||
31 | } | ||
32 | *state = os_down_unused; | ||
33 | dprintf("trigger down (on?), mod: %d, ? -> os_down_unused\n", mod); | ||
34 | } else { | ||
35 | // Trigger keyup | ||
36 | switch (*state) { | ||
37 | case os_down_unused: | ||
38 | // If we didn't use the mod while trigger was held, queue it. | ||
39 | *state = os_up_queued; | ||
40 | dprintf("trigger up, mod: %d, os_down_unused -> os_up_queued\n", mod); | ||
41 | break; | ||
42 | case os_down_used: | ||
43 | // If we did use the mod while trigger was held, unregister it. | ||
44 | *state = os_up_unqueued; | ||
45 | unregister_code(mod); | ||
46 | dprintf("trigger up (off), mod: %d, os_down_used -> os_up_unqueued\n", mod); | ||
47 | break; | ||
48 | default: | ||
49 | break; | ||
50 | } | ||
51 | } | ||
52 | } else { | ||
53 | if (record->event.pressed) { | ||
54 | if (is_oneshot_cancel_key(keycode) && *state != os_up_unqueued) { | ||
55 | // Cancel oneshot on designated cancel keydown. | ||
56 | *state = os_up_unqueued; | ||
57 | unregister_code(mod); | ||
58 | dprintf("cancel (off), mod: %d, ? -> os_up_unqueued\n", mod); | ||
59 | } | ||
60 | if (!is_oneshot_ignored_key(keycode)) { | ||
61 | switch (*state) { | ||
62 | case os_up_queued: | ||
63 | *state = os_up_queued_used; | ||
64 | dprintf("key up (off), mod: %d, os_up_queued -> os_up_queued_used\n", mod); | ||
65 | break; | ||
66 | case os_up_queued_used: | ||
67 | *state = os_up_unqueued; | ||
68 | unregister_code(mod); | ||
69 | dprintf("key up (off), mod: %d, os_up_queued_used -> os_up_unqueued\n", mod); | ||
70 | break; | ||
71 | default: | ||
72 | break; | ||
73 | } | ||
74 | } | ||
75 | } else { | ||
76 | if (!is_oneshot_ignored_key(keycode)) { | ||
77 | // On non-ignored keyup, consider the oneshot used. | ||
78 | switch (*state) { | ||
79 | case os_down_unused: | ||
80 | *state = os_down_used; | ||
81 | dprintf("key up, mod: %d, os_down_unused -> os_down_used\n", mod); | ||
82 | break; | ||
83 | case os_up_queued: | ||
84 | *state = os_up_unqueued; | ||
85 | unregister_code(mod); | ||
86 | dprintf("key up (off), mod: %d, os_up_queued -> os_up_unqueued\n", mod); | ||
87 | break; | ||
88 | case os_up_queued_used: | ||
89 | *state = os_up_unqueued; | ||
90 | unregister_code(mod); | ||
91 | dprintf("key up (off), mod: %d, os_up_queued_used -> os_up_unqueued\n", mod); | ||
92 | break; | ||
93 | default: | ||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | bool update_oneshot_layer( | ||
102 | oneshot_state *state, | ||
103 | uint16_t layer, | ||
104 | uint16_t trigger, | ||
105 | uint16_t keycode, | ||
106 | keyrecord_t *record | ||
107 | ) { | ||
108 | if (keycode == trigger) { | ||
109 | if (record->event.pressed) { | ||
110 | // Trigger keydown | ||
111 | if (*state == os_up_unqueued) { | ||
112 | layer_on(layer); | ||
113 | } | ||
114 | *state = os_down_unused; | ||
115 | dprintf("trigger down (on?), layer: %d, ? -> os_down_unused\n", layer); | ||
116 | return false; | ||
117 | } else { | ||
118 | // Trigger keyup | ||
119 | switch (*state) { | ||
120 | case os_down_unused: | ||
121 | // If we didn't use the layer while trigger was held, queue it. | ||
122 | *state = os_up_queued; | ||
123 | dprintf("trigger up, layer: %d, os_down_unused -> os_up_queued\n", layer); | ||
124 | return false; | ||
125 | case os_down_used: | ||
126 | // If we did use the layer while trigger was held, turn off it. | ||
127 | *state = os_up_unqueued; | ||
128 | layer_off(layer); | ||
129 | dprintf("trigger up (off), layer: %d, os_down_used -> os_up_unqueued\n", layer); | ||
130 | return false; | ||
131 | default: | ||
132 | break; | ||
133 | } | ||
134 | } | ||
135 | } else { | ||
136 | if (record->event.pressed) { | ||
137 | if (is_oneshot_layer_cancel_key(keycode) && *state != os_up_unqueued) { | ||
138 | // Cancel oneshot layer on designated cancel keydown. | ||
139 | *state = os_up_unqueued; | ||
140 | layer_off(layer); | ||
141 | dprintf("cancel (off), layer: %d, ? -> os_up_unqueued\n", layer); | ||
142 | return false; | ||
143 | } | ||
144 | uint8_t key_layer = read_source_layers_cache(record->event.key); | ||
145 | if (key_layer == layer) { | ||
146 | // On non-ignored keyup, consider the oneshot used. | ||
147 | switch (*state) { | ||
148 | case os_down_unused: | ||
149 | *state = os_down_used; | ||
150 | dprintf("key down, layer: %d, os_down_unused -> os_down_used\n", layer); | ||
151 | return true; | ||
152 | case os_up_queued: | ||
153 | if (is_oneshot_mod_key(keycode)) { | ||
154 | *state = os_up_unqueued; | ||
155 | layer_off(layer); | ||
156 | dprintf("key down, layer: %d, os_up_queued -> os_up_unqueued\n", layer); | ||
157 | return false; | ||
158 | } else { | ||
159 | *state = os_up_queued_used; | ||
160 | dprintf("key down, layer: %d, os_up_queued -> os_up_queued_used\n", layer); | ||
161 | } | ||
162 | return true; | ||
163 | case os_up_queued_used: | ||
164 | *state = os_up_unqueued; | ||
165 | layer_off(layer); | ||
166 | dprintf("key down (off), layer: %d, os_up_queued_used -> os_up_unqueued\n", layer); | ||
167 | return false; | ||
168 | default: | ||
169 | break; | ||
170 | } | ||
171 | } | ||
172 | } else { | ||
173 | // Ignore key ups from other layers | ||
174 | uint8_t key_layer = read_source_layers_cache(record->event.key); | ||
175 | if (key_layer == layer) { | ||
176 | // On non-ignored keyup, consider the oneshot used. | ||
177 | switch (*state) { | ||
178 | case os_up_queued: | ||
179 | *state = os_up_unqueued; | ||
180 | layer_off(layer); | ||
181 | dprintf("key up (off), layer: %d, os_up_queued -> os_up_unqueued\n", layer); | ||
182 | return true; | ||
183 | case os_up_queued_used: | ||
184 | *state = os_up_unqueued; | ||
185 | layer_off(layer); | ||
186 | dprintf("key up (off), layer: %d, os_up_queued_used -> os_up_unqueued\n", layer); | ||
187 | return true; | ||
188 | default: | ||
189 | break; | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | } | ||
194 | return true; | ||
195 | } | ||