aboutsummaryrefslogtreecommitdiff
path: root/qmk/keyboards/sofle_choc/keymaps/custom/shift_dance.c
blob: 403c3dfa0ded48c53d8640e209e06acf9d4e2adb (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
#include QMK_KEYBOARD_H
#include "keymap_bepo.h"
#include "quad_tapdance.h"
#include "shift_dance.h"
//
// Definition for the key W.
//
// The main usage is to send the letter W when pressed, but the key is also
// used to activate the SHIFT mode when pressed.
//
static td_tap_t right_shift_tap_state = {
    .state = TD_NONE
};

// Do not switch the supended mode when the key is released.
// This allow to remove the mod earler without reactivating automatically.
static bool inhibit_mod = 0;
static bool lshift_interrupted = 0;

// If there is any other keypress before releasing the key, do not keep the
// layer once the key is release.
//
// This function is a callbackk called from process_record_user
void shift_dance_process_record(uint16_t keycode, keyrecord_t *record) {
    if (right_shift_tap_state.state == TD_SINGLE_HOLD && record->event.pressed) {
        // If there is any key pressed while holding the key, do not switch
        // in caps lock mod on release.
        inhibit_mod = 1;
    }
    switch (keycode) {
        // If the key is pressed without any other combination, remove the
        // caps lock if activated.
        // Otherwise, act just as a modifier key.
        case KC_LSFT:
            if (record->event.pressed) {
                lshift_interrupted = 0;
                break;
            }
            if (lshift_interrupted) {
                break;
            }
            // Continue on the next block
        case KC_ESC:
            if (host_keyboard_led_state().caps_lock) {
                tap_code16(KC_CAPS_LOCK);
            }
            break;
        default:
            lshift_interrupted = 1;
            break;
    }
    return;
}

void right_shift_finished(tap_dance_state_t *state, void *user_data) {
    right_shift_tap_state.state = cur_dance(state);
    switch (right_shift_tap_state.state) {
        case TD_SINGLE_HOLD:
            if (get_mods() & MOD_MASK_SHIFT) {
                del_mods(MOD_MASK_SHIFT);
                inhibit_mod = 1;
            }
            register_code(KC_RIGHT_SHIFT);
            break;
        default: break;
    }
}

void right_shift_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(BP_W);
    else if (!state->interrupted && !inhibit_mod) {
        // Final case, the key was keeped pressed without any other action
        // in this case, switch into CAPS LOCK
        del_mods(MOD_MASK_SHIFT);
        tap_code16(KC_CAPS_LOCK);
    }
    inhibit_mod = 0;
}

void right_shift_reset(tap_dance_state_t *state, void *user_data) {
    switch (right_shift_tap_state.state) {
        case TD_SINGLE_HOLD:
            unregister_code(KC_RIGHT_SHIFT);
            break;
        default: break;
    }
    right_shift_tap_state.state = TD_NONE;
}