qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

action_tapping.h (8357B)


      1 /*
      2 Copyright 2013 Jun Wako <wakojun@gmail.com>
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 
     18 #pragma once
     19 
     20 /* period of tapping(ms) */
     21 #ifndef TAPPING_TERM
     22 #    define TAPPING_TERM 200
     23 #endif
     24 
     25 /* period of quick tap(ms) */
     26 #if !defined(QUICK_TAP_TERM) || QUICK_TAP_TERM > TAPPING_TERM
     27 #    define QUICK_TAP_TERM TAPPING_TERM
     28 #endif
     29 
     30 /* tap count needed for toggling a feature */
     31 #ifndef TAPPING_TOGGLE
     32 #    define TAPPING_TOGGLE 5
     33 #endif
     34 
     35 #define WAITING_BUFFER_SIZE 8
     36 
     37 #ifndef NO_ACTION_TAPPING
     38 uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache);
     39 uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache);
     40 void     action_tapping_process(keyrecord_t record);
     41 #endif
     42 
     43 uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record);
     44 uint16_t get_quick_tap_term(uint16_t keycode, keyrecord_t *record);
     45 bool     get_permissive_hold(uint16_t keycode, keyrecord_t *record);
     46 bool     get_retro_tapping(uint16_t keycode, keyrecord_t *record);
     47 bool     get_hold_on_other_key_press(uint16_t keycode, keyrecord_t *record);
     48 
     49 #ifdef SPECULATIVE_HOLD
     50 /** Gets the currently active speculative mods. */
     51 uint8_t get_speculative_mods(void);
     52 
     53 /**
     54  * Callback to say if a mod-tap key may be speculatively held.
     55  *
     56  * By default, speculative hold is enabled for mod-tap keys where the mod is
     57  * Ctrl, Shift, and Ctrl+Shift for either hand.
     58  *
     59  * @param keycode  Keycode of the mod-tap key.
     60  * @param record   Record associated with the mod-tap press event.
     61  * @return True if the mod-tap key may be speculatively held.
     62  */
     63 bool get_speculative_hold(uint16_t keycode, keyrecord_t *record);
     64 
     65 /**
     66  * Handler to be called on press events after tap-holds are settled.
     67  *
     68  * This function is to be called in process_record() in action.c, that is, just
     69  * after tap-hold events are settled as either tapped or held. When `record`
     70  * corresponds to a speculatively-held key, the speculative mod is cleared.
     71  *
     72  * @param record   Record associated with the mod-tap press event.
     73  */
     74 void speculative_key_settled(keyrecord_t *record);
     75 #else
     76 #    define get_speculative_mods() 0
     77 #endif // SPECULATIVE_HOLD
     78 
     79 #ifdef CHORDAL_HOLD
     80 /**
     81  * Callback to say when a key chord before the tapping term may be held.
     82  *
     83  * In keymap.c, define the callback
     84  *
     85  *     bool get_chordal_hold(uint16_t tap_hold_keycode,
     86  *                           keyrecord_t* tap_hold_record,
     87  *                           uint16_t other_keycode,
     88  *                           keyrecord_t* other_record) {
     89  *        // Conditions...
     90  *     }
     91  *
     92  * This callback is called when:
     93  *
     94  * 1. `tap_hold_keycode` is pressed.
     95  * 2. `other_keycode` is pressed while `tap_hold_keycode` is still held,
     96  *     provided `other_keycode` is *not* also a tap-hold key and it is pressed
     97  *     before the tapping term.
     98  *
     99  * If false is returned, this has the effect of immediately settling the
    100  * tap-hold key as tapped. If true is returned, the tap-hold key is still
    101  * unsettled, and may be settled as held depending on configuration and
    102  * subsequent events.
    103  *
    104  * @param tap_hold_keycode   Keycode of the tap-hold key.
    105  * @param tap_hold_record    Record from the tap-hold press event.
    106  * @param other_keycode      Keycode of the other key.
    107  * @param other_record       Record from the other key's press event.
    108  * @return True if the tap-hold key may be considered held; false if tapped.
    109  */
    110 bool get_chordal_hold(uint16_t tap_hold_keycode, keyrecord_t *tap_hold_record, uint16_t other_keycode, keyrecord_t *other_record);
    111 
    112 /**
    113  * Default "opposite hands rule" for whether a key chord may settle as held.
    114  *
    115  * This function returns true when the tap-hold key and other key are on
    116  * "opposite hands." In detail, handedness of the two keys are compared. If
    117  * handedness values differ, or if either handedness is '*', the function
    118  * returns true, indicating that it may be held. Otherwise, it returns false,
    119  * in which case the tap-hold key is immediately settled at tapped.
    120  *
    121  * @param tap_hold_record  Record of the active tap-hold key press.
    122  * @param other_record     Record of the other, interrupting key press.
    123  * @return True if the tap-hold key may be considered held; false if tapped.
    124  */
    125 bool get_chordal_hold_default(keyrecord_t *tap_hold_record, keyrecord_t *other_record);
    126 
    127 /**
    128  * Gets the handedness of a key.
    129  *
    130  * This function returns:
    131  *   'L' for keys pressed by the left hand,
    132  *   'R' for keys on the right hand,
    133  *   '*' for keys exempt from the "opposite hands rule." This could be used
    134  *       perhaps on thumb keys or keys that might be pressed by either hand.
    135  *
    136  * @param key   A key matrix position.
    137  * @return Handedness value.
    138  */
    139 char chordal_hold_handedness(keypos_t key);
    140 
    141 extern const char chordal_hold_layout[MATRIX_ROWS][MATRIX_COLS] PROGMEM;
    142 #endif
    143 
    144 #ifdef FLOW_TAP_TERM
    145 /**
    146  * Callback to specify the keys where Flow Tap is enabled.
    147  *
    148  * Flow Tap is constrained to certain keys by the following rule: this callback
    149  * is called for both the tap-hold key *and* the key press immediately preceding
    150  * it. If the callback returns true for both keycodes, Flow Tap is enabled.
    151  *
    152  * The default implementation of this callback corresponds to
    153  *
    154  *     bool is_flow_tap_key(uint16_t keycode) {
    155  *       switch (get_tap_keycode(keycode)) {
    156  *         case KC_SPC:
    157  *         case KC_A ... KC_Z:
    158  *         case KC_DOT:
    159  *         case KC_COMM:
    160  *         case KC_SCLN:
    161  *         case KC_SLSH:
    162  *           return true;
    163  *       }
    164  *       return false;
    165  *     }
    166  *
    167  * @param keycode Keycode of the key.
    168  * @return Whether to enable Flow Tap for this key.
    169  */
    170 bool is_flow_tap_key(uint16_t keycode);
    171 
    172 /**
    173  * Callback to customize Flow Tap filtering.
    174  *
    175  * Flow Tap acts only when key events are closer together than this time.
    176  *
    177  * Return a time of 0 to disable filtering. In this way, Flow Tap may be
    178  * disabled for certain tap-hold keys, or when following certain previous keys.
    179  *
    180  * The default implementation of this callback is
    181  *
    182  *     uint16_t get_flow_tap_term(uint16_t keycode, keyrecord_t* record,
    183  *                                uint16_t prev_keycode) {
    184  *       if (is_flow_tap_key(keycode) && is_flow_tap_key(prev_keycode)) {
    185  *         return g_flow_tap_term;
    186  *       }
    187  *       return 0;
    188  *     }
    189  *
    190  * NOTE: If both `is_flow_tap_key()` and `get_flow_tap_term()` are defined, then
    191  * `get_flow_tap_term()` takes precedence.
    192  *
    193  * @param keycode Keycode of the tap-hold key.
    194  * @param record keyrecord_t of the tap-hold event.
    195  * @param prev_keycode Keycode of the previously pressed key.
    196  * @return Time in milliseconds.
    197  */
    198 uint16_t get_flow_tap_term(uint16_t keycode, keyrecord_t *record, uint16_t prev_keycode);
    199 
    200 /** Updates the Flow Tap last key and timer. */
    201 void flow_tap_update_last_event(keyrecord_t *record);
    202 
    203 /**
    204  * Checks if the pressed key is within the flow tap term.
    205  * Can be used to avoid triggering combos or other actions within the flow tap term.
    206  *
    207  * @param keycode The keycode of the pressed key.
    208  * @param record The keyrecord of the pressed key.
    209  * @return True if the pressed key is within the flow tap term; false otherwise.
    210  */
    211 bool within_flow_tap_term(uint16_t keycode, keyrecord_t *record);
    212 #endif // FLOW_TAP_TERM
    213 
    214 #ifdef DYNAMIC_TAPPING_TERM_ENABLE
    215 extern uint16_t g_tapping_term;
    216 #endif
    217 
    218 #if defined(TAPPING_TERM_PER_KEY) && !defined(NO_ACTION_TAPPING)
    219 #    define GET_TAPPING_TERM(keycode, record) get_tapping_term(keycode, record)
    220 #elif defined(DYNAMIC_TAPPING_TERM_ENABLE) && !defined(NO_ACTION_TAPPING)
    221 #    define GET_TAPPING_TERM(keycode, record) g_tapping_term
    222 #else
    223 #    define GET_TAPPING_TERM(keycode, record) (TAPPING_TERM)
    224 #endif
    225 
    226 #ifdef QUICK_TAP_TERM_PER_KEY
    227 #    define GET_QUICK_TAP_TERM(keycode, record) get_quick_tap_term(keycode, record)
    228 #else
    229 #    define GET_QUICK_TAP_TERM(keycode, record) (QUICK_TAP_TERM)
    230 #endif