diff options
Diffstat (limited to 'users/drashna/keyrecords')
-rw-r--r-- | users/drashna/keyrecords/caps_word.c | 164 | ||||
-rw-r--r-- | users/drashna/keyrecords/caps_word.h | 77 | ||||
-rw-r--r-- | users/drashna/keyrecords/process_records.c | 26 | ||||
-rw-r--r-- | users/drashna/keyrecords/process_records.h | 4 | ||||
-rw-r--r-- | users/drashna/keyrecords/unicode.c | 4 |
5 files changed, 205 insertions, 70 deletions
diff --git a/users/drashna/keyrecords/caps_word.c b/users/drashna/keyrecords/caps_word.c index 0c7cd6cfe5..a152b2387b 100644 --- a/users/drashna/keyrecords/caps_word.c +++ b/users/drashna/keyrecords/caps_word.c @@ -1,83 +1,139 @@ -// Copyright 2021 Google LLC. -// SPDX-License-Identifier: Apache-2.0 - #include "caps_word.h" -#ifndef IS_COMMAND -# define IS_COMMAND() (get_mods() == MOD_MASK_SHIFT) -#endif - -bool caps_word_enabled = false; -bool caps_word_shifted = false; - -/** - * @brief Handler for Caps Word feature. - * - * This checks the keycodes, and applies shift to the correct keys, if and when needid. - * - * @param keycode Keycode from matrix - * @param record keyrecord_t data structure - * @return true Continue processing keycode and sent to host - * @return false Stop processing keycode, and do not send to host - */ +static bool caps_word_active = false; + +#if CAPS_WORD_IDLE_TIMEOUT > 0 +# if CAPS_WORD_IDLE_TIMEOUT < 100 || CAPS_WORD_IDLE_TIMEOUT > 30000 +// Constrain timeout to a sensible range. With the 16-bit timer, the longest +// representable timeout is 32768 ms, rounded here to 30000 ms = half a minute. +# error "caps_word: CAPS_WORD_IDLE_TIMEOUT must be between 100 and 30000 ms" +# endif + +static uint16_t idle_timer = 0; + +void caps_word_task(void) { + if (caps_word_active && timer_expired(timer_read(), idle_timer)) { + caps_word_set(false); + } +} +#endif // CAPS_WORD_IDLE_TIMEOUT > 0 + bool process_caps_word(uint16_t keycode, keyrecord_t* record) { - if (!caps_word_enabled) { +#ifndef NO_ACTION_ONESHOT + const uint8_t mods = get_mods() | get_oneshot_mods(); +#else + const uint8_t mods = get_mods(); +#endif // NO_ACTION_ONESHOT + + if (!caps_word_active) { // Pressing both shift keys at the same time enables caps word. - if (IS_COMMAND()) { - clear_mods(); - clear_oneshot_mods(); - caps_word_shifted = false; - caps_word_enabled = true; + if ((mods & MOD_MASK_SHIFT) == MOD_MASK_SHIFT) { + caps_word_set(true); // Activate Caps Word. return false; } return true; + } else { +#if CAPS_WORD_IDLE_TIMEOUT > 0 + idle_timer = record->event.time + CAPS_WORD_IDLE_TIMEOUT; +#endif // CAPS_WORD_IDLE_TIMEOUT > 0 } if (!record->event.pressed) { return true; } - if (!((get_mods() | get_oneshot_mods()) & ~MOD_MASK_SHIFT)) { + if (!(mods & ~MOD_MASK_SHIFT)) { switch (keycode) { + // Ignore MO, TO, TG, TT, and OSL layer switch keys. + case QK_MOMENTARY ... QK_MOMENTARY_MAX: + case QK_TO ... QK_TO_MAX: + case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: + case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: + case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: + return true; + +#ifndef NO_ACTION_TAPPING case QK_MOD_TAP ... QK_MOD_TAP_MAX: - case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: - // Earlier return if this has not been considered tapped yet. if (record->tap.count == 0) { + // Deactivate if a mod becomes active through holding a mod-tap key. + caps_word_set(false); return true; } - // Get the base tapping keycode of a mod- or layer-tap key. keycode &= 0xff; - } + break; - switch (keycode) { - // Letter keys should be shifted. - case KC_A ... KC_Z: - if (!caps_word_shifted) { - register_code(KC_LSFT); +# ifndef NO_ACTION_LAYER + case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: +# endif // NO_ACTION_LAYER + if (record->tap.count == 0) { + return true; } - caps_word_shifted = true; - return true; + keycode &= 0xff; + break; +#endif // NO_ACTION_TAPPING - // Keycodes that continue caps word but shouldn't get shifted. - case KC_1 ... KC_0: - case KC_BSPC: - case KC_MINS: - case KC_UNDS: - if (caps_word_shifted) { - unregister_code(KC_LSFT); +#ifdef SWAP_HANDS_ENABLE + case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX: + if (keycode > 0x56F0 || record->tap.count == 0) { + return true; } - caps_word_shifted = false; - return true; + keycode &= 0xff; + break; +#endif // SWAP_HANDS_ENABLE + } - // Any other keycode disables caps word. + if (caps_word_press_user(keycode)) { + return true; } } - // Disable caps word. - caps_word_enabled = false; - if (caps_word_shifted) { - unregister_code(KC_LSFT); - } - caps_word_shifted = false; + caps_word_set(false); // Deactivate Caps Word. return true; } + +void caps_word_set(bool active) { + if (active != caps_word_active) { + if (active) { + clear_mods(); +#ifndef NO_ACTION_ONESHOT + clear_oneshot_mods(); +#endif // NO_ACTION_ONESHOT +#if CAPS_WORD_IDLE_TIMEOUT > 0 + idle_timer = timer_read() + CAPS_WORD_IDLE_TIMEOUT; +#endif // CAPS_WORD_IDLE_TIMEOUT > 0 + } else if ((get_weak_mods() & MOD_BIT(KC_LSFT)) != 0) { + // If the weak shift mod is still on, turn it off and send an update to + // the host computer. + del_weak_mods(MOD_BIT(KC_LSFT)); + send_keyboard_report(); + } + + caps_word_active = active; + caps_word_set_user(active); + } +} + +bool caps_word_get(void) { + return caps_word_active; +} + +__attribute__((weak)) void caps_word_set_user(bool active) {} + +__attribute__((weak)) bool caps_word_press_user(uint16_t keycode) { + switch (keycode) { + // Keycodes that continue Caps Word, with shift applied. + case KC_A ... KC_Z: + add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to the next key. + return true; + + // Keycodes that continue Caps Word, without shifting. + case KC_1 ... KC_0: + case KC_BSPC: + case KC_MINS: + case KC_UNDS: + return true; + + default: + return false; // Deactivate Caps Word. + } +} diff --git a/users/drashna/keyrecords/caps_word.h b/users/drashna/keyrecords/caps_word.h index 79e410ddda..4279d7e831 100644 --- a/users/drashna/keyrecords/caps_word.h +++ b/users/drashna/keyrecords/caps_word.h @@ -5,4 +5,81 @@ #include "drashna.h" +// Call this function from `process_record_user()` to implement Caps Word. bool process_caps_word(uint16_t keycode, keyrecord_t* record); + +// If CAPS_WORD_IDLE_TIMEOUT is set, call `caps_word_task()` from +// `matrix_scan_user()` as described above. +// +// If CAPS_WORD_IDLE_TIMEOUT isn't set, calling this function has no effect (but +// will still compile). +#if CAPS_WORD_IDLE_TIMEOUT > 0 +void caps_word_task(void); +#else +static inline void caps_word_task(void) {} +#endif + +// Activates or deactivates Caps Word. For instance activate Caps Word with a +// combo by defining a `COMBO_ACTION` that calls `caps_word_set(true)`: +// +// void process_combo_event(uint16_t combo_index, bool pressed) { +// switch(combo_index) { +// case CAPS_COMBO: +// if (pressed) { +// caps_word_set(true); // Activate Caps Word. +// } +// break; +// +// // Other combos... +// } +// } +void caps_word_set(bool active); + +// Returns whether Caps Word is currently active. +bool caps_word_get(void); + +// An optional callback that gets called when Caps Word turns on or off. This is +// useful to represent the current Caps Word state, e.g. by setting an LED or +// playing a sound. In your keymap, define +// +// void caps_word_set_user(bool active) { +// if (active) { +// // Do something when Caps Word activates. +// } else { +// // Do something when Caps Word deactivates. +// } +// } +void caps_word_set_user(bool active); + +// An optional callback which is called on every key press while Caps Word is +// active. When the key should be shifted (that is, a letter key), the callback +// should call `add_weak_mods(MOD_BIT(KC_LSFT))` to shift the key. The callback +// also determines whether the key should continue Caps Word. Returning true +// continues the current "word", while returning false is "word breaking" and +// deactivates Caps Word. The default callback is +// +// bool caps_word_press_user(uint16_t keycode) { +// switch (keycode) { +// // Keycodes that continue Caps Word, with shift applied. +// case KC_A ... KC_Z: +// add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to the next key. +// return true; +// +// // Keycodes that continue Caps Word, without shifting. +// case KC_1 ... KC_0: +// case KC_BSPC: +// case KC_MINS: +// case KC_UNDS: +// return true; +// +// default: +// return false; // Deactivate Caps Word. +// } +// } +// +// To customize, copy the above function into your keymap and add/remove +// keycodes to the above cases. +// +// NOTE: Outside of this callback, you can use `caps_word_set(false)` to +// deactivate Caps Word. +bool caps_word_press_user(uint16_t keycode); diff --git a/users/drashna/keyrecords/process_records.c b/users/drashna/keyrecords/process_records.c index 160a880215..99267d88a8 100644 --- a/users/drashna/keyrecords/process_records.c +++ b/users/drashna/keyrecords/process_records.c @@ -41,21 +41,21 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { #ifdef KEYLOGGER_ENABLE uprintf("KL: kc: 0x%04X, col: %2u, row: %2u, pressed: %b, time: %5u, int: %b, count: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed, record->event.time, record->tap.interrupted, record->tap.count); #endif // KEYLOGGER_ENABLE -#ifdef OLED_ENABLE +#if defined(OLED_ENABLE) && defined(CUSTOM_OLED_DRIVER) process_record_user_oled(keycode, record); #endif // OLED if (!(process_record_keymap(keycode, record) && process_record_secrets(keycode, record) -#ifdef RGB_MATRIX_ENABLE +#ifdef CUSTOM_RGB_MATRIX && process_record_user_rgb_matrix(keycode, record) #endif -#ifdef RGBLIGHT_ENABLE +#ifdef CUSTOM_RGBLIGHT && process_record_user_rgb_light(keycode, record) #endif #ifdef CUSTOM_UNICODE_ENABLE && process_record_unicode(keycode, record) #endif -#if defined(POINTING_DEVICE_ENABLE) +#if defined(CUSTOM_POINTING_DEVICE) && process_record_pointing(keycode, record) #endif #ifdef CAPS_WORD_ENABLE @@ -142,26 +142,26 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { } break; case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal -#if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE) +#if defined(CUSTOM_RGBLIGHT) || defined(CUSTOM_RGB_MATRIX) if (record->event.pressed) { userspace_config.rgb_layer_change ^= 1; dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change); eeconfig_update_user(userspace_config.raw); if (userspace_config.rgb_layer_change) { -# if defined(RGBLIGHT_ENABLE) && defined(RGB_MATRIX_ENABLE) +# if defined(CUSTOM_RGBLIGHT) && defined(CUSTOM_RGB_MATRIX) rgblight_enable_noeeprom(); # endif layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better) -# if defined(RGBLIGHT_ENABLE) && defined(RGB_MATRIX_ENABLE) +# if defined(CUSTOM_RGBLIGHT) && defined(CUSTOM_RGB_MATRIX) } else { rgblight_disable_noeeprom(); # endif } } -#endif // RGBLIGHT_ENABLE +#endif // CUSTOM_RGBLIGHT break; -#if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE) +#if defined(CUSTOM_RGBLIGHT) || defined(CUSTOM_RGB_MATRIX) case RGB_TOG: // Split keyboards need to trigger on key-up for edge-case issue # ifndef SPLIT_KEYBOARD @@ -169,10 +169,10 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { # else if (!record->event.pressed) { # endif -# if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_DISABLE_KEYCODES) +# if defined(CUSTOM_RGBLIGHT) && !defined(RGBLIGHT_DISABLE_KEYCODES) rgblight_toggle(); # endif -# if defined(RGB_MATRIX_ENABLE) && !defined(RGB_MATRIX_DISABLE_KEYCODES) +# if defined(CUSTOM_RGB_MATRIX) && !defined(RGB_MATRIX_DISABLE_KEYCODES) rgb_matrix_toggle(); # endif } @@ -181,7 +181,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions if (record->event.pressed) { bool is_eeprom_updated; -# if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_DISABLE_KEYCODES) +# if defined(CUSTOM_RGBLIGHT) && !defined(RGBLIGHT_DISABLE_KEYCODES) // This disables layer indication, as it's assumed that if you're changing this ... you want that disabled if (userspace_config.rgb_layer_change) { userspace_config.rgb_layer_change = false; @@ -189,7 +189,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { is_eeprom_updated = true; } # endif -# if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) +# if defined(CUSTOM_RGB_MATRIX) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) if (userspace_config.rgb_matrix_idle_anim) { userspace_config.rgb_matrix_idle_anim = false; dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim); diff --git a/users/drashna/keyrecords/process_records.h b/users/drashna/keyrecords/process_records.h index d7a81d4607..5ca2966131 100644 --- a/users/drashna/keyrecords/process_records.h +++ b/users/drashna/keyrecords/process_records.h @@ -4,8 +4,10 @@ #pragma once #include "drashna.h" -#if defined(KEYBOARD_handwired_tractyl_manuform) +#if defined(KEYBOARD_handwired_tractyl_manuform) && defined(POINTING_DEVICE_ENABLE) # define PLACEHOLDER_SAFE_RANGE KEYMAP_SAFE_RANGE +#elif defined(KEYBOARD_bastardkb_charybdis) +# define PLACEHOLDER_SAFE_RANGE CHARYBDIS_SAFE_RANGE #else # define PLACEHOLDER_SAFE_RANGE SAFE_RANGE #endif diff --git a/users/drashna/keyrecords/unicode.c b/users/drashna/keyrecords/unicode.c index 5acd51da9b..af87ee2a61 100644 --- a/users/drashna/keyrecords/unicode.c +++ b/users/drashna/keyrecords/unicode.c @@ -6,7 +6,7 @@ #include "drashna.h" #include "process_unicode_common.h" -uint16_t typing_mode; +uint16_t typing_mode = KC_NOMODE; /** * @brief Registers the unicode keystrokes based on desired unicode @@ -245,7 +245,7 @@ bool process_record_unicode(uint16_t keycode, keyrecord_t *record) { if (typing_mode != keycode) { typing_mode = keycode; } else { - typing_mode = 0; + typing_mode = KC_NOMODE; } } break; |