2020-10-04 12:24:35 -07:00
|
|
|
/*
|
|
|
|
Copyright (c) 2020 Fred Silberberg
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 09:09:01 -08:00
|
|
|
#include "333fred.h"
|
|
|
|
#include "quantum.h"
|
|
|
|
#include "action.h"
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
SINGLE_TAP, SINGLE_HOLD, DOUBLE
|
|
|
|
} tap_dance_state_enum;
|
|
|
|
|
|
|
|
static tap_dance_state_enum tap_dance_state;
|
|
|
|
static bool tap_dance_active = false;
|
|
|
|
|
2022-12-14 12:40:25 -08:00
|
|
|
void tap_dance_sym_vim_finished(tap_dance_state_t *state, void *user_data) {
|
2017-11-06 09:09:01 -08:00
|
|
|
// Determine the current state
|
|
|
|
if (state->count == 1) {
|
|
|
|
if (state->interrupted || state->pressed == 0) tap_dance_state = SINGLE_TAP;
|
|
|
|
else tap_dance_state = SINGLE_HOLD;
|
|
|
|
} else {
|
|
|
|
// Handle any number of other taps as a VIM movement hold
|
|
|
|
tap_dance_state = DOUBLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tap_dance_state) {
|
|
|
|
case SINGLE_TAP:
|
|
|
|
if (tap_dance_active) {
|
|
|
|
reset_oneshot_layer();
|
|
|
|
tap_dance_active = false;
|
|
|
|
} else {
|
|
|
|
set_oneshot_layer(SYMB, ONESHOT_START);
|
|
|
|
tap_dance_active = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SINGLE_HOLD:
|
|
|
|
layer_on(SYMB);
|
|
|
|
break;
|
|
|
|
case DOUBLE:
|
|
|
|
layer_on(VIM);
|
2018-08-05 08:42:16 -07:00
|
|
|
break;
|
2017-11-06 09:09:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:40:25 -08:00
|
|
|
void tap_dance_sym_vim_reset(tap_dance_state_t *state, void *user_data) {
|
2017-11-06 09:09:01 -08:00
|
|
|
switch(tap_dance_state) {
|
|
|
|
case SINGLE_TAP:
|
|
|
|
clear_oneshot_layer_state(ONESHOT_PRESSED);
|
|
|
|
break;
|
|
|
|
case SINGLE_HOLD:
|
|
|
|
layer_off(SYMB);
|
|
|
|
break;
|
|
|
|
case DOUBLE:
|
|
|
|
layer_off(VIM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:40:25 -08:00
|
|
|
void tap_dance_copy_paste_finished(tap_dance_state_t *state, void *user_data) {
|
2019-02-24 14:57:16 -08:00
|
|
|
bool is_paste = state->count == 2;
|
|
|
|
// If either the one-shot shift is set, or if shift is being held, count as shift being held.
|
|
|
|
// We'll clear the one-shot shift if it was held
|
|
|
|
uint8_t one_shot_mods = get_oneshot_mods();
|
|
|
|
bool is_shift = false;
|
|
|
|
|
|
|
|
if (get_mods() & MOD_MASK_SHIFT) {
|
|
|
|
is_shift = true;
|
|
|
|
} else if (one_shot_mods & MOD_MASK_SHIFT) {
|
|
|
|
set_oneshot_mods(one_shot_mods & ~MOD_MASK_SHIFT);
|
|
|
|
is_shift = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_paste) {
|
|
|
|
if (is_shift) {
|
|
|
|
SEND_STRING(SS_LSFT(SS_TAP(X_INSERT)));
|
|
|
|
} else {
|
2022-10-17 05:45:23 -07:00
|
|
|
SEND_STRING(SS_LCTL("v"));
|
2019-02-24 14:57:16 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_shift) {
|
2022-10-17 05:45:23 -07:00
|
|
|
SEND_STRING(SS_LCTL(SS_TAP(X_INSERT)));
|
2019-02-24 14:57:16 -08:00
|
|
|
} else {
|
2022-10-17 05:45:23 -07:00
|
|
|
SEND_STRING(SS_LCTL("c"));
|
2019-02-24 14:57:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:40:25 -08:00
|
|
|
tap_dance_action_t tap_dance_actions[] = {
|
2019-02-24 14:57:16 -08:00
|
|
|
[TD_SYM_VIM] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, tap_dance_sym_vim_finished, tap_dance_sym_vim_reset),
|
|
|
|
[TD_COPY_PASTE] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, tap_dance_copy_paste_finished, NULL)
|
2017-11-06 09:09:01 -08:00
|
|
|
};
|
|
|
|
|
2019-05-29 07:44:43 -07:00
|
|
|
void tap_dance_process_keycode(uint16_t keycode) {
|
2017-11-06 09:09:01 -08:00
|
|
|
if (tap_dance_state == SINGLE_TAP && keycode != TD(TD_SYM_VIM)) {
|
|
|
|
tap_dance_active = false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-05 08:42:16 -07:00
|
|
|
|
|
|
|
__attribute__ ((weak))
|
2022-06-18 14:37:51 -07:00
|
|
|
void layer_state_set_rgb(layer_state_t state) {}
|
2018-08-05 08:42:16 -07:00
|
|
|
|
2021-07-19 09:50:55 -07:00
|
|
|
layer_state_t layer_state_set_user(layer_state_t state) {
|
2018-08-05 08:42:16 -07:00
|
|
|
layer_state_set_rgb(state);
|
|
|
|
return state;
|
|
|
|
}
|
2019-05-29 07:44:43 -07:00
|
|
|
|
|
|
|
bool try_handle_macro(uint16_t keycode, keyrecord_t *record) {
|
|
|
|
switch (keycode)
|
|
|
|
{
|
|
|
|
case DLEFT:
|
|
|
|
if (record->event.pressed)
|
|
|
|
SEND_STRING(SS_LGUI(SS_LALT(SS_TAP(X_LEFT))));
|
|
|
|
return true;
|
|
|
|
case DRIGHT:
|
|
|
|
if (record->event.pressed)
|
|
|
|
SEND_STRING(SS_LGUI(SS_LALT(SS_TAP(X_RIGHT))));
|
|
|
|
return true;
|
|
|
|
case PSCREEN_APP:
|
|
|
|
if (record->event.pressed)
|
2022-10-17 05:45:23 -07:00
|
|
|
SEND_STRING(SS_LALT(SS_TAP(X_PRINT_SCREEN)));
|
2019-05-29 07:44:43 -07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|