123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- #ifdef AUTO_SHIFT_ENABLE
- # include <stdbool.h>
- # include <stdio.h>
- # include "process_auto_shift.h"
- # ifndef AUTO_SHIFT_DISABLED_AT_STARTUP
- # define AUTO_SHIFT_STARTUP_STATE true
- # else
- # define AUTO_SHIFT_STARTUP_STATE false
- # endif
- static uint16_t autoshift_time = 0;
- # if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
- static uint16_t retroshift_time = 0;
- static uint16_t last_retroshift_time;
- # endif
- static uint16_t autoshift_timeout = AUTO_SHIFT_TIMEOUT;
- static uint16_t autoshift_lastkey = KC_NO;
- static keyrecord_t autoshift_lastrecord;
- static uint16_t autoshift_shift_states[((1 << 8) + 15) / 16];
- static struct {
-
- bool enabled : 1;
-
-
- bool lastshifted : 1;
-
- bool in_progress : 1;
-
- bool holding_shift : 1;
-
- bool cancelling_lshift : 1;
- bool cancelling_rshift : 1;
-
-
- } autoshift_flags = {AUTO_SHIFT_STARTUP_STATE, false, false, false, false, false};
- __attribute__((weak)) bool get_custom_auto_shifted_key(uint16_t keycode, keyrecord_t *record) {
- return false;
- }
- __attribute__((weak)) bool get_auto_shifted_key(uint16_t keycode, keyrecord_t *record) {
- switch (keycode) {
- # ifndef NO_AUTO_SHIFT_ALPHA
- case AUTO_SHIFT_ALPHA:
- # endif
- # ifndef NO_AUTO_SHIFT_NUMERIC
- case AUTO_SHIFT_NUMERIC:
- # endif
- # ifndef NO_AUTO_SHIFT_SPECIAL
- case AUTO_SHIFT_SPECIAL:
- # endif
- return true;
- }
- return get_custom_auto_shifted_key(keycode, record);
- }
- __attribute__((weak)) bool get_auto_shift_repeat(uint16_t keycode, keyrecord_t *record) {
- return true;
- }
- __attribute__((weak)) bool get_auto_shift_no_auto_repeat(uint16_t keycode, keyrecord_t *record) {
- return true;
- }
- __attribute__((weak)) void autoshift_press_user(uint16_t keycode, bool shifted, keyrecord_t *record) {
- if (shifted) {
- add_weak_mods(MOD_BIT(KC_LSFT));
- }
- register_code16((IS_RETRO(keycode)) ? keycode & 0xFF : keycode);
- }
- __attribute__((weak)) void autoshift_release_user(uint16_t keycode, bool shifted, keyrecord_t *record) {
- unregister_code16((IS_RETRO(keycode)) ? keycode & 0xFF : keycode);
- }
- void set_autoshift_shift_state(uint16_t keycode, bool shifted) {
- keycode = keycode & 0xFF;
- if (shifted) {
- autoshift_shift_states[keycode / 16] |= (uint16_t)1 << keycode % 16;
- } else {
- autoshift_shift_states[keycode / 16] &= ~((uint16_t)1 << keycode % 16);
- }
- }
- bool get_autoshift_shift_state(uint16_t keycode) {
- keycode = keycode & 0xFF;
- return (autoshift_shift_states[keycode / 16] & (uint16_t)1 << keycode % 16) != (uint16_t)0;
- }
- static void autoshift_flush_shift(void) {
- autoshift_flags.holding_shift = false;
- # ifdef CAPS_WORD_ENABLE
- if (!is_caps_word_on())
- # endif
- {
- del_weak_mods(MOD_BIT(KC_LSFT));
- }
- if (autoshift_flags.cancelling_lshift) {
- autoshift_flags.cancelling_lshift = false;
- add_mods(MOD_BIT(KC_LSFT));
- }
- if (autoshift_flags.cancelling_rshift) {
- autoshift_flags.cancelling_rshift = false;
- add_mods(MOD_BIT(KC_RSFT));
- }
- send_keyboard_report();
- }
- static bool autoshift_press(uint16_t keycode, uint16_t now, keyrecord_t *record) {
-
- if ((get_mods()
- # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
- | get_oneshot_mods()
- # endif
- ) & (~MOD_BIT(KC_LSFT))
- ) {
-
-
- autoshift_lastkey = KC_NO;
- # ifndef AUTO_SHIFT_MODIFIERS
-
-
-
- set_autoshift_shift_state(keycode, false);
- autoshift_press_user(keycode, false, record);
- # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
- set_oneshot_mods(get_oneshot_mods() & (~MOD_BIT(KC_LSFT)));
- clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
- # endif
- return false;
- # endif
- }
-
- autoshift_lastrecord = *record;
- autoshift_lastrecord.event.pressed = false;
- autoshift_lastrecord.event.time = 0;
-
- # if defined(AUTO_SHIFT_REPEAT) || defined(AUTO_SHIFT_REPEAT_PER_KEY)
- if (keycode == autoshift_lastkey &&
- # ifdef AUTO_SHIFT_REPEAT_PER_KEY
- get_auto_shift_repeat(autoshift_lastkey, record) &&
- # endif
- # if !defined(AUTO_SHIFT_NO_AUTO_REPEAT) || defined(AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY)
- (
- !autoshift_flags.lastshifted
- # ifdef AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY
- || get_auto_shift_no_auto_repeat(autoshift_lastkey, record)
- # endif
- ) &&
- # endif
- TIMER_DIFF_16(now, autoshift_time) < GET_TAPPING_TERM(autoshift_lastkey, record)
- ) {
-
-
- if (get_mods() & MOD_BIT(KC_LSFT)) {
- autoshift_flags.cancelling_lshift = true;
- del_mods(MOD_BIT(KC_LSFT));
- }
- if (get_mods() & MOD_BIT(KC_RSFT)) {
- autoshift_flags.cancelling_rshift = true;
- del_mods(MOD_BIT(KC_RSFT));
- }
-
- autoshift_press_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
- return false;
- }
- # endif
-
- # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
- autoshift_flags.lastshifted = (get_mods() | get_oneshot_mods()) & MOD_BIT(KC_LSFT);
- set_oneshot_mods(get_oneshot_mods() & (~MOD_BIT(KC_LSFT)));
- # else
- autoshift_flags.lastshifted = get_mods() & MOD_BIT(KC_LSFT);
- # endif
-
- autoshift_lastkey = keycode;
- autoshift_time = now;
- autoshift_flags.in_progress = true;
- # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
- clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
- # endif
- return false;
- }
- static void autoshift_end(uint16_t keycode, uint16_t now, bool matrix_trigger, keyrecord_t *record) {
- if (autoshift_flags.in_progress && (keycode == autoshift_lastkey || keycode == KC_NO)) {
-
- autoshift_flags.in_progress = false;
-
- autoshift_flags.lastshifted =
- autoshift_flags.lastshifted
- || TIMER_DIFF_16(now, autoshift_time) >=
- # ifdef AUTO_SHIFT_TIMEOUT_PER_KEY
- get_autoshift_timeout(autoshift_lastkey, record)
- # else
- autoshift_timeout
- # endif
- ;
-
- set_autoshift_shift_state(autoshift_lastkey, autoshift_flags.lastshifted);
- if (get_mods() & MOD_BIT(KC_LSFT)) {
- autoshift_flags.cancelling_lshift = true;
- del_mods(MOD_BIT(KC_LSFT));
- }
- if (get_mods() & MOD_BIT(KC_RSFT)) {
- autoshift_flags.cancelling_rshift = true;
- del_mods(MOD_BIT(KC_RSFT));
- }
- autoshift_press_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
-
- # if (defined(AUTO_SHIFT_REPEAT) || defined(AUTO_SHIFT_REPEAT_PER_KEY)) && (!defined(AUTO_SHIFT_NO_AUTO_REPEAT) || defined(AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY))
- if (matrix_trigger
- # ifdef AUTO_SHIFT_REPEAT_PER_KEY
- && get_auto_shift_repeat(autoshift_lastkey, record)
- # endif
- # ifdef AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY
- && !get_auto_shift_no_auto_repeat(autoshift_lastkey, record)
- # endif
- ) {
-
- return;
- }
- # endif
-
- # if TAP_CODE_DELAY > 0
- wait_ms(TAP_CODE_DELAY);
- # endif
- autoshift_release_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
- autoshift_flush_shift();
- } else {
-
- autoshift_release_user(keycode, get_autoshift_shift_state(keycode), record);
- if (keycode == autoshift_lastkey) {
-
-
-
- autoshift_flush_shift();
- }
- }
-
- autoshift_time = now;
- }
- void autoshift_matrix_scan(void) {
- if (autoshift_flags.in_progress) {
- const uint16_t now = timer_read();
- if (TIMER_DIFF_16(now, autoshift_time) >=
- # ifdef AUTO_SHIFT_TIMEOUT_PER_KEY
- get_autoshift_timeout(autoshift_lastkey, &autoshift_lastrecord)
- # else
- autoshift_timeout
- # endif
- ) {
- autoshift_end(autoshift_lastkey, now, true, &autoshift_lastrecord);
- }
- }
- }
- void autoshift_toggle(void) {
- autoshift_flags.enabled = !autoshift_flags.enabled;
- autoshift_flush_shift();
- }
- void autoshift_enable(void) {
- autoshift_flags.enabled = true;
- }
- void autoshift_disable(void) {
- autoshift_flags.enabled = false;
- autoshift_flush_shift();
- }
- # ifndef AUTO_SHIFT_NO_SETUP
- void autoshift_timer_report(void) {
- # ifdef SEND_STRING_ENABLE
- char display[8];
- snprintf(display, 8, "\n%d\n", autoshift_timeout);
- send_string((const char *)display);
- # endif
- }
- # endif
- bool get_autoshift_state(void) {
- return autoshift_flags.enabled;
- }
- uint16_t get_generic_autoshift_timeout() {
- return autoshift_timeout;
- }
- __attribute__((weak)) uint16_t get_autoshift_timeout(uint16_t keycode, keyrecord_t *record) {
- return autoshift_timeout;
- }
- void set_autoshift_timeout(uint16_t timeout) {
- autoshift_timeout = timeout;
- }
- bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
-
-
-
- const uint16_t now =
- # if !defined(RETRO_SHIFT) || defined(NO_ACTION_TAPPING)
- timer_read()
- # else
- (record->event.pressed) ? retroshift_time : timer_read()
- # endif
- ;
-
- if (record->event.pressed) {
- if (autoshift_flags.in_progress) {
-
- autoshift_end(KC_NO, now, false, &autoshift_lastrecord);
- }
- switch (keycode) {
- case KC_ASTG:
- autoshift_toggle();
- break;
- case KC_ASON:
- autoshift_enable();
- break;
- case KC_ASOFF:
- autoshift_disable();
- break;
- # ifndef AUTO_SHIFT_NO_SETUP
- case KC_ASUP:
- autoshift_timeout += 5;
- break;
- case KC_ASDN:
- autoshift_timeout -= 5;
- break;
- case KC_ASRP:
- autoshift_timer_report();
- break;
- # endif
- }
-
-
- if (IS_RETRO(keycode)
- # if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
-
- && (
- record->tap.count == 0
- # ifdef RETRO_TAPPING_PER_KEY
- || !get_retro_tapping(keycode, record)
- # endif
- || (record->tap.interrupted && (IS_LT(keycode)
- # if defined(HOLD_ON_OTHER_KEY_PRESS) || defined(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
- # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
- ? get_hold_on_other_key_press(keycode, record)
- # else
- ? true
- # endif
- # else
- ? false
- # endif
- # if defined(IGNORE_MOD_TAP_INTERRUPT) || defined(IGNORE_MOD_TAP_INTERRUPT_PER_KEY)
- # ifdef IGNORE_MOD_TAP_INTERRUPT_PER_KEY
- : !get_ignore_mod_tap_interrupt(keycode, record)
- # else
- : false
- # endif
- # else
- : true
- # endif
- ))
- )
- # endif
- ) {
-
- autoshift_lastkey = KC_NO;
- return true;
- }
- } else {
- if (keycode == KC_LSFT) {
- autoshift_flags.cancelling_lshift = false;
- } else if (keycode == KC_RSFT) {
- autoshift_flags.cancelling_rshift = false;
- }
-
-
-
- else if (IS_RETRO(keycode)
- # if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
- && (
- record->tap.count == 0
- # ifdef RETRO_TAPPING_PER_KEY
- || !get_retro_tapping(keycode, record)
- # endif
- )
- # endif
- ) {
-
- # if !defined(IGNORE_MOD_TAP_INTERRUPT) || defined(IGNORE_MOD_TAP_INTERRUPT_PER_KEY)
- if (autoshift_flags.in_progress
- # ifdef IGNORE_MOD_TAP_INTERRUPT_PER_KEY
- && !get_ignore_mod_tap_interrupt(keycode, record)
- # endif
- ) {
- autoshift_end(KC_NO, now, false, &autoshift_lastrecord);
- }
- # endif
-
- return true;
- }
- }
- if (!autoshift_flags.enabled) {
- return true;
- }
- if (get_auto_shifted_key(keycode, record)) {
- if (record->event.pressed) {
- return autoshift_press(keycode, now, record);
- } else {
- autoshift_end(keycode, now, false, record);
- return false;
- }
- }
-
-
- if (record->event.pressed) {
- autoshift_lastkey = KC_NO;
- }
- return true;
- }
- # if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
- void retroshift_poll_time(keyevent_t *event) {
- last_retroshift_time = retroshift_time;
- retroshift_time = timer_read();
- }
- void retroshift_swap_times() {
- if (last_retroshift_time != 0 && autoshift_flags.in_progress) {
- uint16_t temp = retroshift_time;
- retroshift_time = last_retroshift_time;
- last_retroshift_time = temp;
- }
- }
- # endif
- #endif
|