summaryrefslogtreecommitdiffstats
path: root/quantum/process_keycode/process_leader.c
blob: 897e9eabf673cac0abbdc8b8ddb3fc0903890bf3 (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
/* Copyright 2016 Jack Humbert
 *
 * 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/>.
 */

#ifdef LEADER_ENABLE

#include "process_leader.h"

#ifndef LEADER_TIMEOUT
  #define LEADER_TIMEOUT 300
#endif

__attribute__ ((weak))
void leader_start(void) {}

__attribute__ ((weak))
void leader_end(void) {}

// 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;

void qk_leader_start(void) {
  if (leading) { return; }
  leader_start();
  leading = true;
  leader_time = timer_read();
  leader_sequence_size = 0;
  leader_sequence[0] = 0;
  leader_sequence[1] = 0;
  leader_sequence[2] = 0;
  leader_sequence[3] = 0;
  leader_sequence[4] = 0;
}

bool process_leader(uint16_t keycode, keyrecord_t *record) {
  // Leader key set-up
  if (record->event.pressed) {
    if (leading) {
      if (timer_elapsed(leader_time) < LEADER_TIMEOUT) {
#ifndef LEADER_KEY_STRICT_KEY_PROCESSING
        if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
          keycode = keycode & 0xFF;
        }
#endif // LEADER_KEY_STRICT_KEY_PROCESSING
        leader_sequence[leader_sequence_size] = keycode;
        leader_sequence_size++;
#ifdef LEADER_PER_KEY_TIMING
        leader_time = timer_read();
#endif
        return false;
      }
    } else {
      if (keycode == KC_LEAD) {
        qk_leader_start();
      }
    }
  }
  return true;
}

#endif