You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.7 KiB

  1. /* Copyright 2021 SethBarberee <seth.barberee@gmail.com>
  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. #include "tap_dance.h"
  17. // Shamelessly stolen from QMK Docs
  18. int cur_dance (qk_tap_dance_state_t *state) {
  19. if (state->count == 1) {
  20. if (state->interrupted || !state->pressed) {
  21. return SINGLE_TAP;
  22. } else {
  23. return SINGLE_HOLD;
  24. }
  25. }
  26. else if (state->count == 2) {
  27. if (state->interrupted) return DOUBLE_SINGLE_TAP;
  28. else if (state->pressed) return DOUBLE_HOLD;
  29. else return DOUBLE_TAP;
  30. }
  31. if (state->count == 3) {
  32. if (state->interrupted || !state->pressed) return TRIPLE_TAP;
  33. else return TRIPLE_HOLD;
  34. }
  35. else return 8;
  36. }
  37. // Initialize it now
  38. tap caps_status = {
  39. .toggled = false,
  40. .state = 0
  41. };
  42. void dance_ecap_finished (qk_tap_dance_state_t *state, void *user_data){
  43. caps_status.state = cur_dance(state);
  44. switch(caps_status.state){
  45. case SINGLE_TAP:
  46. tap_code(KC_ESC);
  47. break;
  48. case SINGLE_HOLD:
  49. register_code(KC_LCTRL);
  50. break;
  51. case DOUBLE_TAP:
  52. tap_code(KC_CAPS);
  53. if(!caps_status.toggled){
  54. // Toggling caps so indicate
  55. caps_status.toggled = true;
  56. #ifdef RGBLIGHT_ENABLE
  57. // Save mode we can from
  58. caps_status.normal_mode = rgblight_get_mode();
  59. rgblight_mode_noeeprom(CAPS_LOCK_MODE);
  60. #endif
  61. } else {
  62. // Turning off so return to normal mode
  63. caps_status.toggled = false;
  64. #ifdef RGBLIGHT_ENABLE
  65. rgblight_mode_noeeprom(caps_status.normal_mode);
  66. #endif
  67. }
  68. break;
  69. }
  70. }
  71. void dance_ecap_reset (qk_tap_dance_state_t *state, void *user_data){
  72. if(caps_status.state == SINGLE_HOLD){
  73. unregister_code(KC_LCTRL);
  74. }
  75. caps_status.state = 0;
  76. }
  77. //Tap Dance Definitions
  78. qk_tap_dance_action_t tap_dance_actions[] = {
  79. //Tap once for Esc, twice for Caps Lock
  80. [TD_ECAP] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, dance_ecap_finished, dance_ecap_reset),
  81. // Other declarations would go here, separated by commas, if you have them
  82. };