diff options
| author | Gergely Nagy <algernon@madhouse-project.org> | 2016-07-20 11:34:45 +0200 |
|---|---|---|
| committer | Gergely Nagy <algernon@madhouse-project.org> | 2016-07-22 09:10:17 +0200 |
| commit | ce8cc9219fca5dde077f1142d03d011b38d27479 (patch) | |
| tree | 01991fe739a7fd2702e010f0132033672a5d9d45 /quantum/process_keycode/process_tap_dance.c | |
| parent | 70e42489dec375e558d8e81ed5ebfb69b4f3dbd9 (diff) | |
| download | qmk_firmware-ce8cc9219fca5dde077f1142d03d011b38d27479.tar.gz qmk_firmware-ce8cc9219fca5dde077f1142d03d011b38d27479.zip | |
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 <algernon@madhouse-project.org>
Diffstat (limited to 'quantum/process_keycode/process_tap_dance.c')
| -rw-r--r-- | quantum/process_keycode/process_tap_dance.c | 61 |
1 files changed, 20 insertions, 41 deletions
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 @@ | |||
| 2 | 2 | ||
| 3 | static qk_tap_dance_state_t qk_tap_dance_state; | 3 | static qk_tap_dance_state_t qk_tap_dance_state; |
| 4 | 4 | ||
| 5 | static void _process_tap_dance_action_pair (qk_tap_dance_state_t *state, | 5 | void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) { |
| 6 | uint16_t kc1, uint16_t kc2) { | 6 | qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data; |
| 7 | uint16_t kc; | ||
| 8 | 7 | ||
| 9 | if (state->count == 0) | 8 | if (state->count == 1) { |
| 10 | return; | 9 | register_code (pair->kc1); |
| 11 | 10 | } else if (state->count == 2) { | |
| 12 | kc = (state->count == 1) ? kc1 : kc2; | 11 | register_code (pair->kc2); |
| 12 | } | ||
| 13 | } | ||
| 13 | 14 | ||
| 14 | register_code (kc); | 15 | void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) { |
| 15 | unregister_code (kc); | 16 | qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data; |
| 16 | 17 | ||
| 17 | if (state->count >= 2) { | 18 | if (state->count == 1) { |
| 18 | reset_tap_dance (state); | 19 | unregister_code (pair->kc1); |
| 20 | } else if (state->count == 2) { | ||
| 21 | unregister_code (pair->kc2); | ||
| 19 | } | 22 | } |
| 20 | } | 23 | } |
| 21 | 24 | ||
| 22 | static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state, | 25 | static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state, |
| 26 | void *user_data, | ||
| 23 | qk_tap_dance_user_fn_t fn) | 27 | qk_tap_dance_user_fn_t fn) |
| 24 | { | 28 | { |
| 25 | if (fn) { | 29 | if (fn) { |
| 26 | fn(state); | 30 | fn(state, user_data); |
| 27 | } | 31 | } |
| 28 | } | 32 | } |
| 29 | 33 | ||
| @@ -34,14 +38,7 @@ void process_tap_dance_action_on_each_tap (uint16_t keycode) | |||
| 34 | 38 | ||
| 35 | action = tap_dance_actions[idx]; | 39 | action = tap_dance_actions[idx]; |
| 36 | 40 | ||
| 37 | switch (action.type) { | 41 | _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_each_tap); |
| 38 | case QK_TAP_DANCE_TYPE_FN: | ||
| 39 | _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_each_tap); | ||
| 40 | break; | ||
| 41 | |||
| 42 | default: | ||
| 43 | break; | ||
| 44 | } | ||
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | void process_tap_dance_action_on_dance_finished (uint16_t keycode) | 44 | 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) | |||
| 51 | 48 | ||
| 52 | action = tap_dance_actions[idx]; | 49 | action = tap_dance_actions[idx]; |
| 53 | 50 | ||
| 54 | switch (action.type) { | 51 | _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_dance_finished); |
| 55 | case QK_TAP_DANCE_TYPE_PAIR: | ||
| 56 | _process_tap_dance_action_pair (&qk_tap_dance_state, | ||
| 57 | action.pair.kc1, action.pair.kc2); | ||
| 58 | break; | ||
| 59 | case QK_TAP_DANCE_TYPE_FN: | ||
| 60 | _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn.on_dance_finished); | ||
| 61 | break; | ||
| 62 | |||
| 63 | default: | ||
| 64 | break; | ||
| 65 | } | ||
| 66 | } | 52 | } |
| 67 | 53 | ||
| 68 | bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { | 54 | bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { |
| @@ -118,15 +104,8 @@ void reset_tap_dance (qk_tap_dance_state_t *state) { | |||
| 118 | return; | 104 | return; |
| 119 | 105 | ||
| 120 | action = tap_dance_actions[idx]; | 106 | action = tap_dance_actions[idx]; |
| 121 | switch (action.type) { | 107 | if (action.fn.on_reset) { |
| 122 | case QK_TAP_DANCE_TYPE_FN: | 108 | action.fn.on_reset(state, action.user_data); |
| 123 | if (action.fn.on_reset) { | ||
| 124 | action.fn.on_reset(state); | ||
| 125 | } | ||
| 126 | break; | ||
| 127 | |||
| 128 | default: | ||
| 129 | break; | ||
| 130 | } | 109 | } |
| 131 | 110 | ||
| 132 | state->keycode = 0; | 111 | state->keycode = 0; |
