summaryrefslogtreecommitdiff
path: root/users/sethBarberee/tap_dance.c
blob: 588ac9be6655f8afe1bced8dd22891fb4e8558ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 /* Copyright 2021 SethBarberee <seth.barberee@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 2 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
#include "tap_dance.h"

// Shamelessly stolen from QMK Docs
int cur_dance (qk_tap_dance_state_t *state) {
  if (state->count == 1) {
    if (state->interrupted || !state->pressed) {
      return SINGLE_TAP;
    } else {
      return SINGLE_HOLD;
    }
  }
  else if (state->count == 2) {
    if (state->interrupted) return DOUBLE_SINGLE_TAP;
    else if (state->pressed) return DOUBLE_HOLD;
    else return DOUBLE_TAP;
  }
  if (state->count == 3) {
    if (state->interrupted || !state->pressed)  return TRIPLE_TAP;
    else return TRIPLE_HOLD;
  }
  else return 8;
}


// Initialize it now
tap caps_status = {
    .toggled = false,
    .state = 0
};


void dance_ecap_finished (qk_tap_dance_state_t *state, void *user_data){
    caps_status.state = cur_dance(state);
    switch(caps_status.state){
        case SINGLE_TAP:
            tap_code(KC_ESC);
            break;
        case SINGLE_HOLD:
            register_code(KC_LCTRL);
            break;
        case DOUBLE_TAP:
            tap_code(KC_CAPS);
            if(!caps_status.toggled){
                // Toggling caps so indicate
                caps_status.toggled =  true;
#ifdef RGBLIGHT_ENABLE
                // Save mode we can from
                caps_status.normal_mode = rgblight_get_mode();
                rgblight_mode_noeeprom(CAPS_LOCK_MODE);
#endif
            } else {
                // Turning off so return to normal mode
                caps_status.toggled = false;
#ifdef RGBLIGHT_ENABLE
                rgblight_mode_noeeprom(caps_status.normal_mode);
#endif
            }
            break;
    }
}

void dance_ecap_reset (qk_tap_dance_state_t *state, void *user_data){
    if(caps_status.state == SINGLE_HOLD){
        unregister_code(KC_LCTRL);
    }
    caps_status.state = 0;
}

//Tap Dance Definitions
qk_tap_dance_action_t tap_dance_actions[] = {
  //Tap once for Esc, twice for Caps Lock
  [TD_ECAP]  = ACTION_TAP_DANCE_FN_ADVANCED(NULL, dance_ecap_finished, dance_ecap_reset),
// Other declarations would go here, separated by commas, if you have them
};