process_tap_dance.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #pragma once
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include "action.h"
  20. #include "quantum_keycodes.h"
  21. typedef struct {
  22. uint16_t interrupting_keycode;
  23. uint8_t count;
  24. uint8_t weak_mods;
  25. #ifndef NO_ACTION_ONESHOT
  26. uint8_t oneshot_mods;
  27. #endif
  28. bool pressed : 1;
  29. bool finished : 1;
  30. bool interrupted : 1;
  31. } tap_dance_state_t;
  32. typedef void (*tap_dance_user_fn_t)(tap_dance_state_t *state, void *user_data);
  33. typedef struct tap_dance_action_t {
  34. tap_dance_state_t state;
  35. struct {
  36. tap_dance_user_fn_t on_each_tap;
  37. tap_dance_user_fn_t on_dance_finished;
  38. tap_dance_user_fn_t on_reset;
  39. tap_dance_user_fn_t on_each_release;
  40. } fn;
  41. void *user_data;
  42. } tap_dance_action_t;
  43. typedef struct {
  44. uint16_t kc1;
  45. uint16_t kc2;
  46. } tap_dance_pair_t;
  47. typedef struct {
  48. uint16_t kc;
  49. uint8_t layer;
  50. void (*layer_function)(uint8_t);
  51. } tap_dance_dual_role_t;
  52. #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) \
  53. { \
  54. .fn = {tap_dance_pair_on_each_tap, tap_dance_pair_finished, tap_dance_pair_reset, NULL}, \
  55. .user_data = (void *)&((tap_dance_pair_t){kc1, kc2}), \
  56. }
  57. #define ACTION_TAP_DANCE_LAYER_MOVE(kc, layer) \
  58. { \
  59. .fn = {tap_dance_dual_role_on_each_tap, tap_dance_dual_role_finished, tap_dance_dual_role_reset, NULL}, \
  60. .user_data = (void *)&((tap_dance_dual_role_t){kc, layer, layer_move}), \
  61. }
  62. #define ACTION_TAP_DANCE_LAYER_TOGGLE(kc, layer) \
  63. { \
  64. .fn = {NULL, tap_dance_dual_role_finished, tap_dance_dual_role_reset, NULL}, \
  65. .user_data = (void *)&((tap_dance_dual_role_t){kc, layer, layer_invert}), \
  66. }
  67. #define ACTION_TAP_DANCE_FN(user_fn) \
  68. { \
  69. .fn = {NULL, user_fn, NULL, NULL}, \
  70. .user_data = NULL, \
  71. }
  72. #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset) \
  73. { \
  74. .fn = {user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset, NULL}, \
  75. .user_data = NULL, \
  76. }
  77. #define ACTION_TAP_DANCE_FN_ADVANCED_WITH_RELEASE(user_fn_on_each_tap, user_fn_on_each_release, user_fn_on_dance_finished, user_fn_on_dance_reset) \
  78. { \
  79. .fn = {user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset, user_fn_on_each_release}, \
  80. .user_data = NULL, \
  81. }
  82. #define TD_INDEX(code) QK_TAP_DANCE_GET_INDEX(code)
  83. #define TAP_DANCE_KEYCODE(state) TD(((tap_dance_action_t *)state) - tap_dance_actions)
  84. void reset_tap_dance(tap_dance_state_t *state);
  85. /* To be used internally */
  86. bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record);
  87. bool process_tap_dance(uint16_t keycode, keyrecord_t *record);
  88. void tap_dance_task(void);
  89. void tap_dance_pair_on_each_tap(tap_dance_state_t *state, void *user_data);
  90. void tap_dance_pair_finished(tap_dance_state_t *state, void *user_data);
  91. void tap_dance_pair_reset(tap_dance_state_t *state, void *user_data);
  92. void tap_dance_dual_role_on_each_tap(tap_dance_state_t *state, void *user_data);
  93. void tap_dance_dual_role_finished(tap_dance_state_t *state, void *user_data);
  94. void tap_dance_dual_role_reset(tap_dance_state_t *state, void *user_data);