summaryrefslogtreecommitdiffstats
path: root/quantum/leader.c
blob: 272609ad0cfa1cb03d980cc7a2844db702024214 (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
// 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);
}