]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_leader.c
[Keyboard] Add QMK configurator JSON for Alice PCB (#6397)
[qmk_firmware.git] / quantum / process_keycode / process_leader.c
1 /* Copyright 2016 Jack Humbert
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
17 #ifdef LEADER_ENABLE
18
19 #include "process_leader.h"
20 #include <string.h>
21
22 #ifndef LEADER_TIMEOUT
23   #define LEADER_TIMEOUT 300
24 #endif
25
26 __attribute__ ((weak))
27 void leader_start(void) {}
28
29 __attribute__ ((weak))
30 void leader_end(void) {}
31
32 // Leader key stuff
33 bool leading = false;
34 uint16_t leader_time = 0;
35
36 uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
37 uint8_t leader_sequence_size = 0;
38
39 void qk_leader_start(void) {
40   if (leading) { return; }
41   leader_start();
42   leading = true;
43   leader_time = timer_read();
44   leader_sequence_size = 0;
45   memset(leader_sequence, 0, sizeof(leader_sequence));
46 }
47
48 bool process_leader(uint16_t keycode, keyrecord_t *record) {
49   // Leader key set-up
50   if (record->event.pressed) {
51     if (leading) {
52       if (timer_elapsed(leader_time) < LEADER_TIMEOUT) {
53 #ifndef LEADER_KEY_STRICT_KEY_PROCESSING
54         if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
55           keycode = keycode & 0xFF;
56         }
57 #endif // LEADER_KEY_STRICT_KEY_PROCESSING
58         if ( leader_sequence_size < ( sizeof(leader_sequence) / sizeof(leader_sequence[0]) ) ) {
59           leader_sequence[leader_sequence_size] = keycode;
60           leader_sequence_size++;
61         } else {
62           leading = false;
63           leader_end();
64         }
65 #ifdef LEADER_PER_KEY_TIMING
66         leader_time = timer_read();
67 #endif
68         return false;
69       }
70     } else {
71       if (keycode == KC_LEAD) {
72         qk_leader_start();
73       }
74     }
75   }
76   return true;
77 }
78
79 #endif