dancelayers.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright 2020 Stephen J. Bush
  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. #ifdef TAP_DANCE_ENABLE
  17. # include QMK_KEYBOARD_H
  18. # include "muppetjones.h"
  19. # include "dancelayers.h"
  20. // Initialize tap structure associated with example tap dance key
  21. static td_tap_t lyr_tap_state = {.is_press_action = true, .state = TD_NONE};
  22. /* @brief Determine the current tap dance state
  23. * @param A tap dance state struct.
  24. * @return A struct.
  25. */
  26. td_state_t cur_dance(qk_tap_dance_state_t *state) {
  27. switch (state->count) {
  28. case 1:
  29. if (!state->pressed)
  30. return TD_1X_TAP;
  31. else
  32. return TD_1X_HOLD;
  33. case 2:
  34. return TD_2X_TAP;
  35. break;
  36. case 3:
  37. return TD_3X_TAP;
  38. break;
  39. case 4:
  40. return TD_4X_TAP;
  41. break;
  42. default:
  43. return TD_UNKNOWN;
  44. }
  45. }
  46. // Functions that control what our tap dance key does
  47. __attribute__((weak)) void td_layer_finished(qk_tap_dance_state_t *state, void *user_data) {
  48. lyr_tap_state.state = cur_dance(state);
  49. switch (lyr_tap_state.state) {
  50. case TD_1X_TAP:
  51. if (layer_state_is(_MOUSE))
  52. layer_off(_MOUSE);
  53. else
  54. layer_on(_MOUSE);
  55. break;
  56. case TD_1X_HOLD:
  57. layer_on(_ADJUST);
  58. break;
  59. case TD_2X_TAP:
  60. // Toggle lower layer
  61. if (layer_state_is(_LOWER))
  62. layer_off(_LOWER);
  63. else
  64. layer_on(_LOWER);
  65. break;
  66. case TD_3X_TAP:
  67. // Toggle lower layer
  68. if (layer_state_is(_RAISE))
  69. layer_off(_RAISE);
  70. else
  71. layer_on(_RAISE);
  72. break;
  73. case TD_4X_TAP:
  74. // Toggle lower layer
  75. if (layer_state_is(_ADJUST))
  76. layer_off(_ADJUST);
  77. else
  78. layer_on(_ADJUST);
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. __attribute__((weak)) void td_layer_reset(qk_tap_dance_state_t *state, void *user_data) {
  85. // If the key was held down and now is released then switch off the layer
  86. if (lyr_tap_state.state == TD_1X_HOLD) {
  87. layer_off(_ADJUST);
  88. }
  89. lyr_tap_state.state = TD_NONE;
  90. }
  91. #endif