summaryrefslogtreecommitdiff
path: root/keyboards/a_dux/keymaps/daliusd/flow.c
blob: 6e4db873fe318c18722d60aaea07aeb40d4c6f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* Copyright 2022 @daliusd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "flow.h"

extern const uint16_t flow_config[FLOW_COUNT][2];
extern const uint16_t flow_layers_config[FLOW_LAYERS_COUNT][2];

// Represents the states a flow key can be in
typedef enum {
    flow_up_unqueued,
    flow_up_queued,
    flow_up_queued_used,
    flow_down_unused,
    flow_down_used,
} flow_state_t;

#ifdef FLOW_ONESHOT_TERM
const int g_flow_oneshot_term = FLOW_ONESHOT_TERM;
#else
const int g_flow_oneshot_term = 500;
#endif

#ifdef FLOW_ONESHOT_WAIT_TERM
const int g_flow_oneshot_wait_term = FLOW_ONESHOT_WAIT_TERM;
#else
const int g_flow_oneshot_wait_term = 500;
#endif

flow_state_t flow_state[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = flow_up_unqueued };
bool flow_pressed[FLOW_COUNT][2] = { [0 ... FLOW_COUNT - 1] = {false, false} };
uint16_t flow_timers[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = 0 };
bool flow_timeout_timers_active[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = false };
uint16_t flow_timeout_timers_value[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = 0 };
uint16_t flow_timeout_wait_timers_value[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = 0 };

flow_state_t flow_layers_state[FLOW_LAYERS_COUNT] = {
    [0 ... FLOW_LAYERS_COUNT - 1] = flow_up_unqueued
};
bool flow_layer_timeout_timers_active[FLOW_LAYERS_COUNT] = { [0 ... FLOW_LAYERS_COUNT - 1] = false };
uint16_t flow_layer_timeout_timers_value[FLOW_LAYERS_COUNT] = { [0 ... FLOW_LAYERS_COUNT - 1] = 0 };
uint16_t flow_layer_timeout_wait_timers_value[FLOW_LAYERS_COUNT] = { [0 ... FLOW_LAYERS_COUNT - 1] = 0 };

bool is_flow_ignored_key(uint16_t keycode) {
    for (int i = 0; i < FLOW_COUNT; i++) {
        if (flow_config[i][0] == keycode) {
            return true;
        }
    }

    for (int i = 0; i < FLOW_LAYERS_COUNT; i++) {
        if (flow_layers_config[i][0] == keycode) {
            return true;
        }
    }

    if (keycode == KC_LSFT || keycode == KC_RSFT
            || keycode == KC_LCTL || keycode == KC_RCTL
            || keycode == KC_LALT || keycode == KC_RALT
            || keycode == KC_LGUI || keycode == KC_RGUI) {
        return true;
    }

    return false;
}

bool update_flow_mods(
    uint16_t keycode,
    bool pressed
) {
    bool pass = true;
    bool flow_key_list_triggered[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = false };
    bool flow_key_list_pressed[FLOW_COUNT] = { [0 ... FLOW_COUNT - 1] = false };

    bool flow_triggered = false;

    for (uint8_t i = 0; i < FLOW_COUNT; i++) {
        // Layer key
        if (keycode == flow_config[i][0]) {
            if (pressed) {
                flow_pressed[i][0] = true;
            } else {
                flow_pressed[i][0] = false;
            }
        // KC mod key
        } else if (keycode == flow_config[i][1]) {
            if (pressed) {
                if (flow_pressed[i][0]) {
                    flow_pressed[i][1] = true;
                    flow_key_list_triggered[i] = true;
                    flow_triggered = true;
                    flow_key_list_pressed[i] = true;
                    pass = false;
                 }
            } else if (flow_pressed[i][1]) {
                flow_pressed[i][1] = false;
                if (flow_pressed[i][0]) {
                    flow_key_list_triggered[i] = true;
                    flow_triggered = true;
                    pass = false;
                } else if ((flow_state[i] == flow_down_unused)
                        || (flow_state[i] == flow_down_used)) {
                    flow_key_list_triggered[i] = true;
                    flow_triggered = true;
                    pass = false;
                }
            }
        }
    }

    for (uint8_t i = 0; i < FLOW_COUNT; i++) {
        if (flow_key_list_triggered[i]) {
            if (flow_key_list_pressed[i]) {
                if (flow_state[i] == flow_up_unqueued) {
                    register_code(flow_config[i][1]);
                }
                flow_timeout_wait_timers_value[i] = timer_read();
                flow_state[i] = flow_down_unused;
            } else {
                // Trigger keyup
                switch (flow_state[i]) {
                case flow_down_unused:
                    if (!flow_pressed[i][1]) {
                        if (timer_elapsed(flow_timeout_wait_timers_value[i]) > g_flow_oneshot_wait_term) {
                            flow_state[i] = flow_up_unqueued;
                            unregister_code(flow_config[i][1]);
                        } else {
                            // If we didn't use the mod while trigger was held, queue it.
                            flow_state[i] = flow_up_queued;
                            flow_timeout_timers_active[i] = true;
                            flow_timeout_timers_value[i] = timer_read();
                        }
                    }
                    break;
                case flow_down_used:
                    // If we did use the mod while trigger was held, unregister it.
                    if (!flow_pressed[i][1]) {
                        flow_state[i] = flow_up_unqueued;
                        unregister_code(flow_config[i][1]);
                    }
                    break;
                default:
                    break;
                }
            }
        } else if (!flow_triggered) {
            if (pressed) {
                if (!is_flow_ignored_key(keycode)) {
                    switch (flow_state[i]) {
                    case flow_up_queued:
                        flow_state[i] = flow_up_queued_used;
                        flow_timeout_timers_active[i] = false;
                        break;
                    case flow_up_queued_used:
                        flow_state[i] = flow_up_unqueued;
                        unregister_code(flow_config[i][1]);
                        break;
                    default:
                        break;
                    }
                }
            } else {
                if (!is_flow_ignored_key(keycode)) {
                    // On non-ignored keyup, consider the oneshot used.
                    switch (flow_state[i]) {
                    case flow_down_unused:
                        flow_state[i] = flow_down_used;
                        break;
                    case flow_up_queued:
                        flow_state[i] = flow_up_unqueued;
                        unregister_code(flow_config[i][1]);
                        break;
                    case flow_up_queued_used:
                        flow_state[i] = flow_up_unqueued;
                        unregister_code(flow_config[i][1]);
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }

    return pass;
}

void change_pressed_status(uint16_t keycode, bool pressed) {
    for (int i = 0; i < FLOW_COUNT; i++) {
        if (flow_config[i][0] == keycode) {
            flow_pressed[i][0] = pressed;
        }
    }
}

bool update_flow_layers(
    uint16_t keycode,
    bool pressed,
    keypos_t key_position
) {
    uint8_t key_layer = read_source_layers_cache(key_position);
    bool pass = true;

    for (int i = 0; i < FLOW_LAYERS_COUNT; i++) {
        uint16_t trigger = flow_layers_config[i][0];
        uint16_t layer = flow_layers_config[i][1];

        if (keycode == trigger) {
            if (pressed) {
                // Trigger keydown
                if (flow_layers_state[i] == flow_up_unqueued) {
                    layer_on(layer);
                    change_pressed_status(trigger, true);
                }
                flow_layer_timeout_wait_timers_value[i] = timer_read();
                flow_layers_state[i] = flow_down_unused;
                pass = false;
            } else {
                // Trigger keyup
                switch (flow_layers_state[i]) {
                case flow_down_unused:
                    if (timer_elapsed(flow_layer_timeout_wait_timers_value[i]) > g_flow_oneshot_wait_term) {
                        flow_layers_state[i] = flow_up_unqueued;
                        layer_off(layer);
                        change_pressed_status(trigger, false);
                        pass = false;
                    } else {
                        // If we didn't use the layer while trigger was held, queue it.
                        flow_layers_state[i] = flow_up_queued;
                        flow_layer_timeout_timers_active[i] = true;
                        flow_layer_timeout_timers_value[i] = timer_read();
                        pass = false;
                        change_pressed_status(trigger, true);
                    }
                    break;
                case flow_down_used:
                    // If we did use the layer while trigger was held, turn off it.
                    flow_layers_state[i] = flow_up_unqueued;
                    layer_off(layer);
                    change_pressed_status(trigger, false);
                    pass = false;
                    break;
                default:
                    break;
                }
            }
        } else {
            if (pressed) {
                if (key_layer == layer) {
                    // On non-ignored keyup, consider the oneshot used.
                    switch (flow_layers_state[i]) {
                    case flow_down_unused:
                        flow_layers_state[i] = flow_down_used;
                        break;
                    case flow_up_queued:
                        flow_layers_state[i] = flow_up_queued_used;
                        flow_layer_timeout_timers_active[i] = false;
                        break;
                   case flow_up_queued_used:
                        flow_layers_state[i] = flow_up_unqueued;
                        layer_off(layer);
                        change_pressed_status(trigger, false);
                        pass = false;
                        break;
                    default:
                        break;
                    }
                }
            } else {
                // Ignore key ups from other layers
                if (key_layer == layer) {
                    // On non-ignored keyup, consider the oneshot used.
                    switch (flow_layers_state[i]) {
                    case flow_up_queued:
                        flow_layers_state[i] = flow_up_unqueued;
                        layer_off(layer);
                        change_pressed_status(trigger, false);
                        break;
                    case flow_up_queued_used:
                        flow_layers_state[i] = flow_up_unqueued;
                        layer_off(layer);
                        change_pressed_status(trigger, false);
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }

    return pass;
}

bool update_flow(
    uint16_t keycode,
    bool pressed,
    keypos_t key_position
) {
    bool pass = update_flow_mods(keycode, pressed);
    pass = update_flow_layers(keycode, pressed, key_position) & pass;
    return pass;
}

void flow_matrix_scan(void) {
    for (int i = 0; i < FLOW_COUNT; i++) {
        if (flow_timeout_timers_active[i]
                && timer_elapsed(flow_timeout_timers_value[i]) > g_flow_oneshot_term) {
            flow_timeout_timers_active[i] = false;
            flow_state[i] = flow_up_unqueued;
            unregister_code(flow_config[i][1]);
        }
    }

    for (int i = 0; i < FLOW_LAYERS_COUNT; i++) {
        if (flow_layer_timeout_timers_active[i]
                && timer_elapsed(flow_layer_timeout_timers_value[i]) > g_flow_oneshot_term) {
            flow_layer_timeout_timers_active[i] = false;
            flow_layers_state[i] = flow_up_unqueued;
            layer_off(flow_layers_config[i][1]);
            change_pressed_status(flow_layers_config[i][0], false);
        }
    }
}