diff options
author | Ryan <[email protected]> | 2023-02-13 03:19:02 +1100 |
---|---|---|
committer | GitHub <[email protected]> | 2023-02-13 03:19:02 +1100 |
commit | bbf7a20b33de2d203518687cb5cd1aa85005ea27 (patch) | |
tree | ee1a5c412a02021d085c81a26321c3424eca7022 /quantum/leader.c | |
parent | d10350cd2ceb2b9d80522cdec3ea908118f7fd35 (diff) | |
download | qmk_firmware-bbf7a20b33de2d203518687cb5cd1aa85005ea27.tar.gz qmk_firmware-bbf7a20b33de2d203518687cb5cd1aa85005ea27.zip |
Refactor Leader key feature (#19632)
Co-authored-by: Drashna Jaelre <[email protected]>
Diffstat (limited to 'quantum/leader.c')
-rw-r--r-- | quantum/leader.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/quantum/leader.c b/quantum/leader.c new file mode 100644 index 0000000000..272609ad0c --- /dev/null +++ b/quantum/leader.c @@ -0,0 +1,101 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "leader.h" +#include "timer.h" +#include "util.h" + +#include <string.h> + +#ifndef LEADER_TIMEOUT +# define LEADER_TIMEOUT 300 +#endif + +// Leader key stuff +bool leading = false; +uint16_t leader_time = 0; +uint16_t leader_sequence[5] = {0, 0, 0, 0, 0}; +uint8_t leader_sequence_size = 0; + +__attribute__((weak)) void leader_start_user(void) {} + +__attribute__((weak)) void leader_end_user(void) {} + +void leader_start(void) { + if (leading) { + return; + } + leader_start_user(); + leading = true; + leader_time = timer_read(); + leader_sequence_size = 0; + memset(leader_sequence, 0, sizeof(leader_sequence)); +} + +void leader_end(void) { + leading = false; + leader_end_user(); +} + +void leader_task(void) { + if (leader_sequence_active() && leader_sequence_timed_out()) { + leader_end(); + } +} + +bool leader_sequence_active(void) { + return leading; +} + +bool leader_sequence_add(uint16_t keycode) { + if (leader_sequence_size >= ARRAY_SIZE(leader_sequence)) { + return false; + } + +#if defined(LEADER_NO_TIMEOUT) + if (leader_sequence_size == 0) { + leader_reset_timer(); + } +#endif + + leader_sequence[leader_sequence_size] = keycode; + leader_sequence_size++; + + return true; +} + +bool leader_sequence_timed_out(void) { +#if defined(LEADER_NO_TIMEOUT) + return leader_sequence_size > 0 && timer_elapsed(leader_time) > LEADER_TIMEOUT; +#else + return timer_elapsed(leader_time) > LEADER_TIMEOUT; +#endif +} + +void leader_reset_timer(void) { + leader_time = timer_read(); +} + +bool leader_sequence_is(uint16_t kc1, uint16_t kc2, uint16_t kc3, uint16_t kc4, uint16_t kc5) { + return leader_sequence[0] == kc1 && leader_sequence[1] == kc2 && leader_sequence[2] == kc3 && leader_sequence[3] == kc4 && leader_sequence[4] == kc5; +} + +bool leader_sequence_one_key(uint16_t kc) { + return leader_sequence_is(kc, 0, 0, 0, 0); +} + +bool leader_sequence_two_keys(uint16_t kc1, uint16_t kc2) { + return leader_sequence_is(kc1, kc2, 0, 0, 0); +} + +bool leader_sequence_three_keys(uint16_t kc1, uint16_t kc2, uint16_t kc3) { + return leader_sequence_is(kc1, kc2, kc3, 0, 0); +} + +bool leader_sequence_four_keys(uint16_t kc1, uint16_t kc2, uint16_t kc3, uint16_t kc4) { + return leader_sequence_is(kc1, kc2, kc3, kc4, 0); +} + +bool leader_sequence_five_keys(uint16_t kc1, uint16_t kc2, uint16_t kc3, uint16_t kc4, uint16_t kc5) { + return leader_sequence_is(kc1, kc2, kc3, kc4, kc5); +} |