summaryrefslogtreecommitdiff
path: root/users/mnil/mnil.c
blob: 11d5ee28dfa2374715a7a79c0a8e522ce452a03d (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Copyright 2021 Mats Nilsson
 *
 * 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 "mnil.h"

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    switch (keycode) {
        case M_TILD:  // ~
            if (record->event.pressed) {
                tap_code16(RALT(KC_RBRC));
                tap_code(KC_SPC);
            } else {
            }
            break;
        case M_CIRC:  // ^
            if (record->event.pressed) {
                tap_code16(S(KC_RBRC));
                tap_code(KC_SPC);
            } else {
            }
            break;
        case M_BTCK:  // `
            if (record->event.pressed) {
                tap_code16(S(KC_EQL));
                tap_code(KC_SPC);
            } else {
            }
            break;
        case QWE_COL:  // Swap default keymap layer
            if (record->event.pressed) {
                if (get_highest_layer(default_layer_state) == _COLEMAK) {
                    default_layer_set(1UL << _QWERTY);
                } else {
                    default_layer_set(1UL << _COLEMAK);
                }
            }
            break;
    }
    return true;
};

// Tap Dance
// Determine the current tap dance state
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_SINGLE_TAP;
    }
    if (state->count == 3) {
        if (state->interrupted || !state->pressed)
            return TRIPLE_TAP;
        else
            return TRIPLE_HOLD;
    } else
        return 8;
}

static tap ae_tap_state = {.is_press_action = true, .state = 0};

void ae_finished(qk_tap_dance_state_t *state, void *user_data) {
    ae_tap_state.state = cur_dance(state);
    switch (ae_tap_state.state) {
        case SINGLE_TAP:
            register_code(KC_A);
            break;
        case SINGLE_HOLD:
            tap_code(SE_AE);
            break;
        case DOUBLE_SINGLE_TAP:
            tap_code(KC_A);
            register_code(KC_A);
            break;
    }
}

void ae_reset(qk_tap_dance_state_t *state, void *user_data) {
    switch (ae_tap_state.state) {
        case SINGLE_TAP:
            unregister_code(KC_A);
            break;
        case DOUBLE_SINGLE_TAP:
            unregister_code(KC_A);
            break;
    }
    ae_tap_state.state = 0;
}

static tap aa_tap_state = {.is_press_action = true, .state = 0};

void aa_finished(qk_tap_dance_state_t *state, void *user_data) {
    aa_tap_state.state = cur_dance(state);
    switch (aa_tap_state.state) {
        case SINGLE_TAP:
            register_code(SE_OSLH);
            break;
        case SINGLE_HOLD:
            register_code(SE_AA);
            unregister_code(SE_AA);
            break;
        case DOUBLE_SINGLE_TAP:
            tap_code(SE_OSLH);
            register_code(SE_OSLH);
            break;
    }
}

void aa_reset(qk_tap_dance_state_t *state, void *user_data) {
    switch (aa_tap_state.state) {
        case SINGLE_TAP:
            unregister_code(SE_OSLH);
            break;
        case DOUBLE_SINGLE_TAP:
            unregister_code(SE_OSLH);
            break;
    }
    aa_tap_state.state = 0;
}

// clang-format off
qk_tap_dance_action_t tap_dance_actions[] = {
  [AAE] =  ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, ae_finished, ae_reset, 250),
  [OAA] =  ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, aa_finished, aa_reset, 250)
};
// clang-format on