aboutsummaryrefslogtreecommitdiff
path: root/qmk/keyboards/sofle_choc/keymaps/custom/enter_dance.c
blob: d5e08389c153f84e3c5badb1c327cfea4b2040ad (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
#include QMK_KEYBOARD_H
#include "quad_tapdance.h"
#include "enter_dance.h"

static td_tap_t enter_tap_state = {
    .state = TD_NONE
};

static bool inhibit_mod = 0;

void enter_dance_process_record(uint16_t keycode, keyrecord_t *record) {
    if (enter_tap_state.state == TD_SINGLE_HOLD && record->event.pressed) {
        inhibit_mod = 1;
    }
    return;
}

void enter_finished(tap_dance_state_t *state, void *user_data) {
    enter_tap_state.state = cur_dance(state);
    switch (enter_tap_state.state) {
        case TD_SINGLE_HOLD:
            if (get_oneshot_mods() & MOD_MASK_ALT) {
                del_oneshot_mods(MOD_MASK_ALT);
                //clear_oneshot_mods();
                inhibit_mod = 1;
            }
            register_code(KC_RIGHT_ALT);
            break;
        default: break;
    }
}

void enter_released(tap_dance_state_t *state, void *user_data) {
    if (state->finished && state->count > 1)
        // Exit prevently, we are sure in the next conditions we don’t fall in
        // this case.
        return;
    else if (!state->finished) {
        tap_code16(KC_ENTER);
        reset_tap_dance(state);
    } else if (!state->interrupted) {
        // Final case, the key was keeped pressed without any other action
        // in this case, active the one shot key for right alt
        if (get_oneshot_mods() & MOD_MASK_ALT) {
          del_oneshot_mods(MOD_MASK_ALT);
          //clear_oneshot_mods();
        } else if (!inhibit_mod) {
           set_oneshot_mods(MOD_BIT(KC_RIGHT_ALT));
        }
    }
    inhibit_mod = 0;
}

void enter_reset(tap_dance_state_t *state, void *user_data) {
    switch (enter_tap_state.state) {
        case TD_SINGLE_HOLD: unregister_code(KC_RIGHT_ALT); break;
        default: break;
    }
    enter_tap_state.state = TD_NONE;
}