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 | |
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>
-rw-r--r-- | quantum/process_keycode/process_tap_dance.c | 61 | ||||
-rw-r--r-- | quantum/process_keycode/process_tap_dance.h | 41 |
2 files changed, 38 insertions, 64 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; |
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 | |||
17 | 17 | ||
18 | #define TD(n) (QK_TAP_DANCE + n) | 18 | #define TD(n) (QK_TAP_DANCE + n) |
19 | 19 | ||
20 | typedef enum | 20 | typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data); |
21 | { | ||
22 | QK_TAP_DANCE_TYPE_PAIR, | ||
23 | QK_TAP_DANCE_TYPE_FN, | ||
24 | } qk_tap_dance_type_t; | ||
25 | |||
26 | typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state); | ||
27 | 21 | ||
28 | typedef struct | 22 | typedef struct |
29 | { | 23 | { |
30 | qk_tap_dance_type_t type; | 24 | struct { |
31 | union { | 25 | qk_tap_dance_user_fn_t on_each_tap; |
32 | struct { | 26 | qk_tap_dance_user_fn_t on_dance_finished; |
33 | uint16_t kc1; | 27 | qk_tap_dance_user_fn_t on_reset; |
34 | uint16_t kc2; | 28 | } fn; |
35 | } pair; | 29 | void *user_data; |
36 | struct { | ||
37 | qk_tap_dance_user_fn_t on_each_tap; | ||
38 | qk_tap_dance_user_fn_t on_dance_finished; | ||
39 | qk_tap_dance_user_fn_t on_reset; | ||
40 | } fn; | ||
41 | }; | ||
42 | } qk_tap_dance_action_t; | 30 | } qk_tap_dance_action_t; |
43 | 31 | ||
32 | typedef struct | ||
33 | { | ||
34 | uint16_t kc1; | ||
35 | uint16_t kc2; | ||
36 | } qk_tap_dance_pair_t; | ||
37 | |||
44 | #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \ | 38 | #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \ |
45 | .type = QK_TAP_DANCE_TYPE_PAIR, \ | 39 | .fn = { NULL, qk_tap_dance_pair_finished, qk_tap_dance_pair_reset }, \ |
46 | .pair = { kc1, kc2 } \ | 40 | .user_data = (void *)&((qk_tap_dance_pair_t) { kc1, kc2 }) \ |
47 | } | 41 | } |
48 | 42 | ||
49 | #define ACTION_TAP_DANCE_FN(user_fn) { \ | 43 | #define ACTION_TAP_DANCE_FN(user_fn) { \ |
50 | .type = QK_TAP_DANCE_TYPE_FN, \ | ||
51 | .fn = { NULL, user_fn, NULL } \ | 44 | .fn = { NULL, user_fn, NULL } \ |
52 | } | 45 | } |
53 | 46 | ||
54 | #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset) { \ | 47 | #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset) { \ |
55 | .type = QK_TAP_DANCE_TYPE_FN, \ | ||
56 | .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset } \ | 48 | .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_reset } \ |
57 | } | 49 | } |
58 | 50 | ||
@@ -64,6 +56,9 @@ bool process_tap_dance(uint16_t keycode, keyrecord_t *record); | |||
64 | void matrix_scan_tap_dance (void); | 56 | void matrix_scan_tap_dance (void); |
65 | void reset_tap_dance (qk_tap_dance_state_t *state); | 57 | void reset_tap_dance (qk_tap_dance_state_t *state); |
66 | 58 | ||
59 | void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data); | ||
60 | void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data); | ||
61 | |||
67 | #else | 62 | #else |
68 | 63 | ||
69 | #define TD(n) KC_NO | 64 | #define TD(n) KC_NO |