QMK/keyboards/betalupi_ergodox/features/leader/leader.c

103 lines
2.5 KiB
C
Raw Normal View History

2023-12-19 08:34:06 -08:00
#ifdef ENABLE_BETA_LEADER
#include "beta_leader.h"
#include <string.h>
#ifndef LEADER_TIMEOUT
#define LEADER_TIMEOUT 300
#endif
__attribute__((weak)) void beta_leader_start(void) {}
__attribute__((weak)) void beta_leader_end(void) {}
__attribute__((weak)) bool beta_leader_check(void) { return true; }
// Leader key stuff
bool beta_leading = false;
uint16_t beta_leader_time = 0;
bool beta_leader_change = false;
uint16_t beta_leader_sequence[5] = {0, 0, 0, 0, 0};
uint8_t beta_leader_sequence_size = 0;
void beta_qk_leader_start(void) {
if (beta_leading) {
return;
}
beta_leader_start();
beta_leading = true;
beta_leader_time = timer_read();
beta_leader_sequence_size = 0;
beta_leader_change = false; // If true, run
memset(beta_leader_sequence, 0, sizeof(beta_leader_sequence));
}
void beta_qk_leader_check() {
#ifdef LEADER_NO_TIMEOUT
bool timeout = (beta_leading && beta_leader_sequence_size > 0 && timer_elapsed(beta_leader_time) > LEADER_TIMEOUT);
#else
bool timeout = (beta_leading && timer_elapsed(beta_leader_time) > LEADER_TIMEOUT);
#endif
if (beta_leader_change || timeout) {
beta_leader_change = false;
if (
beta_leader_sequence[beta_leader_sequence_size - 1] == (KC_LEAD)
) {
beta_leading = false;
beta_leader_end();
return;
}
if (timeout) {
beta_leading = false;
beta_leader_end();
}
if (!beta_leader_check()) {
beta_leading = false;
beta_leader_end();
};
}
}
bool beta_process_leader(uint16_t keycode, keyrecord_t *record) {
// Leader key set-up
if (record->event.pressed) {
if (beta_leading) {
# ifndef LEADER_NO_TIMEOUT
if (timer_elapsed(beta_leader_time) < LEADER_TIMEOUT)
# endif // LEADER_NO_TIMEOUT
{
# ifndef LEADER_KEY_STRICT_KEY_PROCESSING
if (IS_QK_MOD_TAP(keycode)) {
keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
} else if (IS_QK_LAYER_TAP(keycode)) {
keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
}
# endif // LEADER_KEY_STRICT_KEY_PROCESSING
if (beta_leader_sequence_size < ARRAY_SIZE(beta_leader_sequence)) {
beta_leader_sequence[beta_leader_sequence_size] = keycode;
beta_leader_sequence_size++;
beta_leader_change = true;
} else {
beta_leading = false;
beta_leader_end();
return true;
}
# ifdef LEADER_PER_KEY_TIMING
beta_leader_time = timer_read();
# endif
return false;
}
} else {
if (keycode == QK_LEADER) {
beta_qk_leader_start();
}
}
}
return true;
}
#endif