process_tap_dance.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright 2016 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef PROCESS_TAP_DANCE_H
  17. #define PROCESS_TAP_DANCE_H
  18. #ifdef TAP_DANCE_ENABLE
  19. #include <stdbool.h>
  20. #include <inttypes.h>
  21. typedef struct
  22. {
  23. uint8_t count;
  24. uint8_t oneshot_mods;
  25. uint16_t keycode;
  26. uint16_t timer;
  27. bool interrupted;
  28. bool pressed;
  29. bool finished;
  30. } qk_tap_dance_state_t;
  31. #define TD(n) (QK_TAP_DANCE + n)
  32. typedef void (*qk_tap_dance_user_fn_t) (qk_tap_dance_state_t *state, void *user_data);
  33. typedef struct
  34. {
  35. struct {
  36. qk_tap_dance_user_fn_t on_each_tap;
  37. qk_tap_dance_user_fn_t on_dance_finished;
  38. qk_tap_dance_user_fn_t on_reset;
  39. } fn;
  40. qk_tap_dance_state_t state;
  41. uint16_t custom_tapping_term;
  42. void *user_data;
  43. } qk_tap_dance_action_t;
  44. typedef struct
  45. {
  46. uint16_t kc1;
  47. uint16_t kc2;
  48. } qk_tap_dance_pair_t;
  49. typedef struct
  50. {
  51. uint16_t kc;
  52. uint8_t layer;
  53. } qk_tap_dance_dual_role_t;
  54. #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) { \
  55. .fn = { NULL, qk_tap_dance_pair_finished, qk_tap_dance_pair_reset }, \
  56. .user_data = (void *)&((qk_tap_dance_pair_t) { kc1, kc2 }), \
  57. }
  58. #define ACTION_TAP_DANCE_DUAL_ROLE(kc, layer) { \
  59. .fn = { NULL, qk_tap_dance_dual_role_finished, qk_tap_dance_dual_role_reset }, \
  60. .user_data = (void *)&((qk_tap_dance_dual_role_t) { kc, layer }), \
  61. }
  62. #define ACTION_TAP_DANCE_FN(user_fn) { \
  63. .fn = { NULL, user_fn, NULL }, \
  64. .user_data = NULL, \
  65. }
  66. #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset) { \
  67. .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset }, \
  68. .user_data = NULL, \
  69. }
  70. #define ACTION_TAP_DANCE_FN_ADVANCED_TIME(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset, tap_specific_tapping_term) { \
  71. .fn = { user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset }, \
  72. .user_data = NULL, \
  73. .custom_tapping_term = tap_specific_tapping_term, \
  74. }
  75. extern qk_tap_dance_action_t tap_dance_actions[];
  76. /* To be used internally */
  77. bool process_tap_dance(uint16_t keycode, keyrecord_t *record);
  78. void matrix_scan_tap_dance (void);
  79. void reset_tap_dance (qk_tap_dance_state_t *state);
  80. void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data);
  81. void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data);
  82. void qk_tap_dance_dual_role_finished (qk_tap_dance_state_t *state, void *user_data);
  83. void qk_tap_dance_dual_role_reset (qk_tap_dance_state_t *state, void *user_data);
  84. #else
  85. #define TD(n) KC_NO
  86. #endif
  87. #endif