From f3b56701ed7e6c622dc48e429780124ba5fde172 Mon Sep 17 00:00:00 2001 From: Pavlos Vinieratos Date: Wed, 13 Jul 2016 16:47:45 +0200 Subject: add an `anyway` and a `reset` callback when using tap dance, we have the `regular` callback that is called on the last tap. this commit adds an `anyway` callback that is called on every tap, and a `reset` callback that is called on reset of the tap dance taps. --- quantum/process_keycode/process_tap_dance.h | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'quantum/process_keycode/process_tap_dance.h') diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index b9d7c7fcf..bf925df0f 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -22,6 +22,7 @@ typedef enum } qk_tap_dance_type_t; typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state); +typedef void (*qk_tap_dance_user_fn_reset_t) (void); typedef struct { @@ -31,18 +32,37 @@ typedef struct uint16_t kc1; uint16_t kc2; } pair; - qk_tap_dance_user_fn_t fn; + struct { + qk_tap_dance_user_fn_t regular; + qk_tap_dance_user_fn_t anyway; + qk_tap_dance_user_fn_reset_t reset; + } fn; }; } qk_tap_dance_action_t; #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \ - .type = QK_TAP_DANCE_TYPE_PAIR, \ - .pair = { kc1, kc2 } \ + .type = QK_TAP_DANCE_TYPE_PAIR, \ + .pair = { kc1, kc2 } \ } #define ACTION_TAP_DANCE_FN(user_fn) { \ .type = QK_TAP_DANCE_TYPE_FN, \ - .fn = user_fn \ + .fn = { user_fn, NULL, NULL } \ + } + +#define ACTION_TAP_DANCE_FN_ANYWAY(user_fn, user_fn_anyway) { \ + .type = QK_TAP_DANCE_TYPE_FN, \ + .fn = { user_fn, user_fn_anyway, NULL } \ + } + +#define ACTION_TAP_DANCE_FN_RESET(user_fn, user_fn_reset) { \ + .type = QK_TAP_DANCE_TYPE_FN, \ + .fn = { user_fn, NULL, user_fn_reset } \ + } + +#define ACTION_TAP_DANCE_FN_ANYWAY_RESET(user_fn, user_fn_anyway, user_fn_reset) { \ + .type = QK_TAP_DANCE_TYPE_FN, \ + .fn = { user_fn, user_fn_anyway, user_fn_reset } \ } extern const qk_tap_dance_action_t tap_dance_actions[]; -- cgit v1.2.3 From d3091faf363afc8fef73ddf4948f872439b0e827 Mon Sep 17 00:00:00 2001 From: Pavlos Vinieratos Date: Fri, 15 Jul 2016 23:54:08 +0200 Subject: change naming, and remove extraneous definition --- quantum/process_keycode/process_tap_dance.c | 32 ++++++++++++++--------------- quantum/process_keycode/process_tap_dance.h | 27 ++++++++---------------- 2 files changed, 23 insertions(+), 36 deletions(-) (limited to 'quantum/process_keycode/process_tap_dance.h') diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index 40fba2a6a..93b326b5f 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -27,7 +27,7 @@ static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state, } } -void process_tap_dance_action (uint16_t keycode) +void process_tap_dance_action_on_each_tap (uint16_t keycode) { uint16_t idx = keycode - QK_TAP_DANCE; qk_tap_dance_action_t action; @@ -35,12 +35,8 @@ void process_tap_dance_action (uint16_t keycode) action = tap_dance_actions[idx]; switch (action.type) { - case QK_TAP_DANCE_TYPE_PAIR: - _process_tap_dance_action_pair (&qk_tap_dance_state, - action.pair.kc1, action.pair.kc2); - break; case QK_TAP_DANCE_TYPE_FN: - _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.regular); + _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_each_tap); break; default: @@ -48,7 +44,7 @@ void process_tap_dance_action (uint16_t keycode) } } -void process_tap_dance_action_anyway (uint16_t keycode) +void process_tap_dance_action_on_dance_finished (uint16_t keycode) { uint16_t idx = keycode - QK_TAP_DANCE; qk_tap_dance_action_t action; @@ -56,8 +52,12 @@ void process_tap_dance_action_anyway (uint16_t keycode) action = tap_dance_actions[idx]; switch (action.type) { + case QK_TAP_DANCE_TYPE_PAIR: + _process_tap_dance_action_pair (&qk_tap_dance_state, + action.pair.kc1, action.pair.kc2); + break; case QK_TAP_DANCE_TYPE_FN: - _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.anyway); + _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_dance_finished); break; default: @@ -70,9 +70,9 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { switch(keycode) { case QK_TAP_DANCE ... QK_TAP_DANCE_MAX: - process_tap_dance_action_anyway (qk_tap_dance_state.keycode); + process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode); if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) { - process_tap_dance_action (qk_tap_dance_state.keycode); + process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode); } else { r = false; } @@ -85,10 +85,9 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { break; default: + process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode); if (qk_tap_dance_state.keycode) { - //process_tap_dance_action_anyway (qk_tap_dance_state.keycode); - process_tap_dance_action (qk_tap_dance_state.keycode); - + process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode); reset_tap_dance (&qk_tap_dance_state); } break; @@ -99,8 +98,7 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { void matrix_scan_tap_dance () { if (qk_tap_dance_state.keycode && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) { - process_tap_dance_action (qk_tap_dance_state.keycode); - + process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode); reset_tap_dance (&qk_tap_dance_state); } } @@ -112,8 +110,8 @@ void reset_tap_dance (qk_tap_dance_state_t *state) { action = tap_dance_actions[idx]; switch (action.type) { case QK_TAP_DANCE_TYPE_FN: - if (action.fn.reset) { - action.fn.reset(); + if (action.fn.on_reset) { + action.fn.on_reset(state); } break; diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index bf925df0f..7b820584a 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -22,7 +22,6 @@ typedef enum } qk_tap_dance_type_t; typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state); -typedef void (*qk_tap_dance_user_fn_reset_t) (void); typedef struct { @@ -33,9 +32,9 @@ typedef struct uint16_t kc2; } pair; struct { - qk_tap_dance_user_fn_t regular; - qk_tap_dance_user_fn_t anyway; - qk_tap_dance_user_fn_reset_t reset; + qk_tap_dance_user_fn_t on_each_tap; + qk_tap_dance_user_fn_t on_dance_finished; + qk_tap_dance_user_fn_t on_reset; } fn; }; } qk_tap_dance_action_t; @@ -45,24 +44,14 @@ typedef struct .pair = { kc1, kc2 } \ } -#define ACTION_TAP_DANCE_FN(user_fn) { \ +#define ACTION_TAP_DANCE_FN(user_fn) { \ .type = QK_TAP_DANCE_TYPE_FN, \ - .fn = { user_fn, NULL, NULL } \ + .fn = { NULL, user_fn, NULL } \ } -#define ACTION_TAP_DANCE_FN_ANYWAY(user_fn, user_fn_anyway) { \ - .type = QK_TAP_DANCE_TYPE_FN, \ - .fn = { user_fn, user_fn_anyway, NULL } \ - } - -#define ACTION_TAP_DANCE_FN_RESET(user_fn, user_fn_reset) { \ - .type = QK_TAP_DANCE_TYPE_FN, \ - .fn = { user_fn, NULL, user_fn_reset } \ - } - -#define ACTION_TAP_DANCE_FN_ANYWAY_RESET(user_fn, user_fn_anyway, user_fn_reset) { \ - .type = QK_TAP_DANCE_TYPE_FN, \ - .fn = { user_fn, user_fn_anyway, user_fn_reset } \ +#define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset) { \ + .type = QK_TAP_DANCE_TYPE_FN, \ + .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset } \ } extern const qk_tap_dance_action_t tap_dance_actions[]; -- cgit v1.2.3 From 70e42489dec375e558d8e81ed5ebfb69b4f3dbd9 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 20 Jul 2016 10:22:52 +0200 Subject: tap-dance: Support for holding keys With this change, tap dance will now store the pressed state of the tap-dance key, and allow one to make an action sooner, while the key is still held, and only unregister when the key is released. The registration must happen in the `on_dance_finished` callback, while unregistering goes to `on_reset`. The surrounding code makes sure not to call either multiple times. Signed-off-by: Gergely Nagy --- quantum/process_keycode/process_tap_dance.c | 11 ++++++++++- quantum/process_keycode/process_tap_dance.h | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'quantum/process_keycode/process_tap_dance.h') diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index b9b836df2..097440405 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -73,10 +73,14 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode); if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) { process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode); + } else if (qk_tap_dance_state.active && qk_tap_dance_state.pressed) { + reset_tap_dance (&qk_tap_dance_state); } else { r = false; } + qk_tap_dance_state.active = true; + qk_tap_dance_state.pressed = record->event.pressed; if (record->event.pressed) { qk_tap_dance_state.keycode = keycode; qk_tap_dance_state.timer = timer_read (); @@ -90,6 +94,7 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { process_tap_dance_action_on_each_tap (qk_tap_dance_state.keycode); process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode); reset_tap_dance (&qk_tap_dance_state); + qk_tap_dance_state.active = false; } break; } @@ -98,7 +103,7 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { } void matrix_scan_tap_dance () { - if (qk_tap_dance_state.keycode && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) { + if (qk_tap_dance_state.active && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) { // if we are here, the tap dance was timed out process_tap_dance_action_on_dance_finished (qk_tap_dance_state.keycode); reset_tap_dance (&qk_tap_dance_state); @@ -109,6 +114,9 @@ void reset_tap_dance (qk_tap_dance_state_t *state) { uint16_t idx = state->keycode - QK_TAP_DANCE; qk_tap_dance_action_t action; + if (state->pressed) + return; + action = tap_dance_actions[idx]; switch (action.type) { case QK_TAP_DANCE_TYPE_FN: @@ -123,4 +131,5 @@ void reset_tap_dance (qk_tap_dance_state_t *state) { state->keycode = 0; state->count = 0; + state->active = false; } diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index 7b820584a..d457db9b3 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -11,6 +11,8 @@ typedef struct uint8_t count; uint16_t keycode; uint16_t timer; + bool active:1; + bool pressed:1; } qk_tap_dance_state_t; #define TD(n) (QK_TAP_DANCE + n) -- cgit v1.2.3 From ce8cc9219fca5dde077f1142d03d011b38d27479 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 20 Jul 2016 11:34:45 +0200 Subject: tap-dance: Support user_data for the callbacks Refactored the code a little, so all callbacks now receive a `user_data` pointer, which can be anything. As an example, the key pairs from `ACTION_TAP_DANCE_DOUBLE` now use this, and custom, built-in functions. This makes it easier to extend the tap dance functionality, and also simplifies the code a little. Signed-off-by: Gergely Nagy --- quantum/process_keycode/process_tap_dance.c | 61 ++++++++++------------------- quantum/process_keycode/process_tap_dance.h | 41 +++++++++---------- 2 files changed, 38 insertions(+), 64 deletions(-) (limited to 'quantum/process_keycode/process_tap_dance.h') diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index 097440405..94b6af130 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -2,28 +2,32 @@ static qk_tap_dance_state_t qk_tap_dance_state; -static void _process_tap_dance_action_pair (qk_tap_dance_state_t *state, - uint16_t kc1, uint16_t kc2) { - uint16_t kc; +void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) { + qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data; - if (state->count == 0) - return; - - kc = (state->count == 1) ? kc1 : kc2; + if (state->count == 1) { + register_code (pair->kc1); + } else if (state->count == 2) { + register_code (pair->kc2); + } +} - register_code (kc); - unregister_code (kc); +void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) { + qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data; - if (state->count >= 2) { - reset_tap_dance (state); + if (state->count == 1) { + unregister_code (pair->kc1); + } else if (state->count == 2) { + unregister_code (pair->kc2); } } static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state, + void *user_data, qk_tap_dance_user_fn_t fn) { if (fn) { - fn(state); + fn(state, user_data); } } @@ -34,14 +38,7 @@ void process_tap_dance_action_on_each_tap (uint16_t keycode) action = tap_dance_actions[idx]; - switch (action.type) { - case QK_TAP_DANCE_TYPE_FN: - _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_each_tap); - break; - - default: - break; - } + _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_each_tap); } void process_tap_dance_action_on_dance_finished (uint16_t keycode) @@ -51,18 +48,7 @@ void process_tap_dance_action_on_dance_finished (uint16_t keycode) action = tap_dance_actions[idx]; - switch (action.type) { - case QK_TAP_DANCE_TYPE_PAIR: - _process_tap_dance_action_pair (&qk_tap_dance_state, - action.pair.kc1, action.pair.kc2); - break; - case QK_TAP_DANCE_TYPE_FN: - _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_dance_finished); - break; - - default: - break; - } + _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_dance_finished); } bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { @@ -118,15 +104,8 @@ void reset_tap_dance (qk_tap_dance_state_t *state) { return; action = tap_dance_actions[idx]; - switch (action.type) { - case QK_TAP_DANCE_TYPE_FN: - if (action.fn.on_reset) { - action.fn.on_reset(state); - } - break; - - default: - break; + if (action.fn.on_reset) { + action.fn.on_reset(state, action.user_data); } state->keycode = 0; diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index d457db9b3..e2c74efe9 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -17,42 +17,34 @@ typedef struct #define TD(n) (QK_TAP_DANCE + n) -typedef enum -{ - QK_TAP_DANCE_TYPE_PAIR, - QK_TAP_DANCE_TYPE_FN, -} qk_tap_dance_type_t; - -typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state); +typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data); typedef struct { - qk_tap_dance_type_t type; - union { - struct { - uint16_t kc1; - uint16_t kc2; - } pair; - struct { - qk_tap_dance_user_fn_t on_each_tap; - qk_tap_dance_user_fn_t on_dance_finished; - qk_tap_dance_user_fn_t on_reset; - } fn; - }; + struct { + qk_tap_dance_user_fn_t on_each_tap; + qk_tap_dance_user_fn_t on_dance_finished; + qk_tap_dance_user_fn_t on_reset; + } fn; + void *user_data; } qk_tap_dance_action_t; +typedef struct +{ + uint16_t kc1; + uint16_t kc2; +} qk_tap_dance_pair_t; + #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \ - .type = QK_TAP_DANCE_TYPE_PAIR, \ - .pair = { kc1, kc2 } \ + .fn = { NULL, qk_tap_dance_pair_finished, qk_tap_dance_pair_reset }, \ + .user_data = (void *)&((qk_tap_dance_pair_t) { kc1, kc2 }) \ } #define ACTION_TAP_DANCE_FN(user_fn) { \ - .type = QK_TAP_DANCE_TYPE_FN, \ .fn = { NULL, user_fn, NULL } \ } #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset) { \ - .type = QK_TAP_DANCE_TYPE_FN, \ .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset } \ } @@ -64,6 +56,9 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record); void matrix_scan_tap_dance (void); void reset_tap_dance (qk_tap_dance_state_t *state); +void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data); +void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data); + #else #define TD(n) KC_NO -- cgit v1.2.3 From b21e8b97acb722bfa7b85831cfd010716ed77962 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 27 Jul 2016 08:42:09 +0200 Subject: tap-dance: Add some debugging support Signed-off-by: Gergely Nagy --- quantum/process_keycode/process_tap_dance.c | 15 +++++++++++++++ quantum/process_keycode/process_tap_dance.h | 1 + 2 files changed, 16 insertions(+) (limited to 'quantum/process_keycode/process_tap_dance.h') diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index d240dc2e6..5429e3438 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -1,6 +1,18 @@ #include "quantum.h" static qk_tap_dance_state_t qk_tap_dance_state; +bool td_debug_enable = false; + +#if CONSOLE_ENABLE +#define td_debug(s) if (td_debug_enable) \ + { \ + xprintf ("D:tap_dance:%s:%s = { keycode = %d, count = %d, active = %d, pressed = %d }\n", __FUNCTION__, s, \ + qk_tap_dance_state.keycode, qk_tap_dance_state.count, \ + qk_tap_dance_state.active, qk_tap_dance_state.pressed); \ + } +#else +#define td_debug(s) +#endif void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) { qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data; @@ -33,16 +45,19 @@ static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state, static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t action) { + td_debug("trigger"); _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_each_tap); } static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t action) { + td_debug("trigger"); _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_dance_finished); } static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t action) { + td_debug("trigger") _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_reset); } diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index e2c74efe9..6a1258067 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -49,6 +49,7 @@ typedef struct } extern const qk_tap_dance_action_t tap_dance_actions[]; +extern bool td_debug_enable; /* To be used internally */ -- cgit v1.2.3