Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process leader mappings on key press #7270

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions quantum/process_keycode/process_leader.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@
# define LEADER_TIMEOUT 300
# endif

# ifndef LEADER_ABORT
# define LEADER_ABORT KC_LEAD
# endif

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

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

__attribute__((weak)) bool leader_process_user(uint16_t *leader_sequence, bool is_timeout) { return true; }

// Leader key stuff
bool leading = false;
uint16_t leader_time = 0;
Expand All @@ -45,6 +51,14 @@ void qk_leader_start(void) {
memset(leader_sequence, 0, sizeof(leader_sequence));
}

void matrix_scan_leader(void) {
if (leading && timer_elapsed(leader_time) > LEADER_TIMEOUT) {
leader_process_user(leader_sequence, true);
leading = false;
leader_end();
}
}

bool process_leader(uint16_t keycode, keyrecord_t *record) {
// Leader key set-up
if (record->event.pressed) {
Expand All @@ -55,9 +69,21 @@ bool process_leader(uint16_t keycode, keyrecord_t *record) {
keycode = keycode & 0xFF;
}
# endif // LEADER_KEY_STRICT_KEY_PROCESSING
if (keycode == LEADER_ABORT) {
leading = false;
leader_end();
return false;
}
if (leader_sequence_size < (sizeof(leader_sequence) / sizeof(leader_sequence[0]))) {
leader_sequence[leader_sequence_size] = keycode;
leader_sequence_size++;
# ifdef LEADER_ON_KEY_PROCESSING
if (!leader_process_user(leader_sequence, false)) {
leading = false;
leader_end();
return false;
}
# endif // LEADER_ON_KEY_PROCESSING
} else {
leading = false;
leader_end();
Expand Down
3 changes: 3 additions & 0 deletions quantum/process_keycode/process_leader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
#include "quantum.h"

bool process_leader(uint16_t keycode, keyrecord_t *record);
void matrix_scan_leader(void);

void leader_start(void);
void leader_end(void);
bool leader_process_user(uint16_t *leader_sequence, bool is_timeout);

void qk_leader_start(void);

#define SEQ_ONE_KEY(key) if (leader_sequence[0] == (key) && leader_sequence[1] == 0 && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
Expand Down
4 changes: 4 additions & 0 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,10 @@ void matrix_scan_quantum() {
matrix_scan_combo();
#endif

#if defined(LEADER_ENABLE) && defined(LEADER_ON_KEY_PROCESSING)
Copy link
Member

@drashna drashna Nov 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could just use:

Suggested change
#if defined(LEADER_ENABLE) && defined(LEADER_ON_KEY_PROCESSING)
#if defined(LEADER_ENABLE)

If not, it would be nice ideal if things could be changed so this is called in such a way that it could be use regardless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would break existing code because calling matrix_scan_leader() when on key processing is not enabled would finish the leader mode before the user gets the chance to catch it in his matrix_scan_user() with the usual LEADER_DICTIONARY() method.

However, we could move the check to into matrix_scan_leader() to keep the matrix function cleaner.

matrix_scan_leader();
#endif

#if defined(BACKLIGHT_ENABLE)
# if defined(LED_MATRIX_ENABLE)
led_matrix_task();
Expand Down