QMK/users/drashna/drashna.c

314 lines
8.9 KiB
C

/*
Copyright 2017 Christopher Courtney <drashna@live.com> @drashna
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "drashna.h"
bool is_alt_tab_active = false;
uint16_t alt_tab_timer = 0;
bool is_ctrl_tab_active = false;
uint16_t ctrl_tab_timer = 0;
userspace_config_t userspace_config;
bool mod_key_press_timer(uint16_t code, uint16_t mod_code, bool pressed) {
static uint16_t this_timer;
if (pressed) {
this_timer = timer_read();
} else {
if (timer_elapsed(this_timer) < TAPPING_TERM) {
tap_code(code);
} else {
register_code(mod_code);
tap_code(code);
unregister_code(mod_code);
}
}
return false;
}
bool mod_key_press(uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
if (pressed) {
this_timer = timer_read();
} else {
if (timer_elapsed(this_timer) < TAPPING_TERM) {
tap_code(code);
} else {
register_code(mod_code);
tap_code(code);
unregister_code(mod_code);
}
}
return false;
}
__attribute__((weak)) void keyboard_pre_init_keymap(void) {}
void keyboard_pre_init_user(void) {
userspace_config.raw = eeconfig_read_user();
keyboard_pre_init_keymap();
}
// Add reconfigurable functions here, for keymap customization
// This allows for a global, userspace functions, and continued
// customization of the keymap. Use _keymap instead of _user
// functions in the keymaps
__attribute__((weak)) void matrix_init_keymap(void) {}
// Call user matrix init, set default RGB colors and then
// call the keymap's init function
void matrix_init_user(void) {
#if defined(BOOTLOADER_CATERINA) && defined(__AVR__)
DDRD &= ~(1 << 5);
PORTD &= ~(1 << 5);
DDRB &= ~(1 << 0);
PORTB &= ~(1 << 0);
#endif
matrix_init_keymap();
}
__attribute__((weak)) void keyboard_post_init_keymap(void) {}
void keyboard_post_init_user(void) {
#if defined(RGBLIGHT_ENABLE)
keyboard_post_init_rgb_light();
#endif
#if defined(RGB_MATRIX_ENABLE)
keyboard_post_init_rgb_matrix();
#endif
keyboard_post_init_keymap();
}
__attribute__((weak)) void shutdown_keymap(void) {}
void rgb_matrix_update_pwm_buffers(void);
void shutdown_user(void) {
#ifdef RGBLIGHT_ENABLE
rgblight_enable_noeeprom();
rgblight_mode_noeeprom(1);
rgblight_setrgb_red();
#endif // RGBLIGHT_ENABLE
#ifdef RGB_MATRIX_ENABLE
# ifdef __AVR__
rgb_matrix_set_color_all(0xFF, 0x00, 0x00);
rgb_matrix_update_pwm_buffers();
# else
rgb_matrix_sethsv_noeeprom(0, 255, 255);
rgb_matrix_mode_noeeprom(1);
# endif
#endif // RGB_MATRIX_ENABLE
shutdown_keymap();
}
__attribute__((weak)) void suspend_power_down_keymap(void) {}
void suspend_power_down_user(void) { suspend_power_down_keymap(); }
__attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
void suspend_wakeup_init_user(void) { suspend_wakeup_init_keymap(); }
__attribute__((weak)) void matrix_scan_keymap(void) {}
// No global matrix scan code, so just run keymap's matrix
// scan function
LEADER_EXTERNS();
void matrix_scan_user(void) {
LEADER_DICTIONARY() {
leading = false;
leader_end();
SEQ_ONE_KEY(KC_C) { // Inline Code
SEND_STRING("`` " SS_TAP(X_LEFT) SS_TAP(X_LEFT));
}
SEQ_ONE_KEY(KC_M) {
SEND_STRING("Mvh Frederik");
}
SEQ_ONE_KEY(KC_SCLN) {
}
SEQ_ONE_KEY(KC_QUOT) {
}
SEQ_ONE_KEY(KC_LBRC) {
}
SEQ_TWO_KEYS(KC_C, KC_B) { // Discord code block
SEND_STRING("```c" SS_LSFT("\n\n") "``` " SS_TAP(X_UP));
}
}
if (is_alt_tab_active) {
if (timer_elapsed(alt_tab_timer) > 750) {
unregister_code(KC_LALT);
is_alt_tab_active = false;
}
}
if (is_ctrl_tab_active) {
if (timer_elapsed(ctrl_tab_timer) > 750) {
unregister_code(KC_LCTRL);
is_ctrl_tab_active = false;
}
}
static bool has_ran_yet;
if (!has_ran_yet) {
has_ran_yet = true;
startup_user();
}
#ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
//Skip on Drop ALT
#if !(defined DROP_ALT)
run_diablo_macro_check();
#endif
#endif // TAP_DANCE_ENABLE
#if defined(RGBLIGHT_ENABLE)
matrix_scan_rgb_light();
#endif // RGBLIGHT_ENABLE
#if defined(RGB_MATRIX_ENABLE)
matrix_scan_rgb_matrix();
#endif
matrix_scan_keymap();
}
__attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
// on layer change, no matter where the change was initiated
// Then runs keymap's layer change check
layer_state_t layer_state_set_user(layer_state_t state) {
state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
#if defined(RGBLIGHT_ENABLE)
state = layer_state_set_rgb_light(state);
#endif // RGBLIGHT_ENABLE
return layer_state_set_keymap(state);
}
__attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
// Runs state check and changes underglow color and animation
layer_state_t default_layer_state_set_user(layer_state_t state) {
state = default_layer_state_set_keymap(state);
//#if 0
# if (defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE))
state = layer_state_set_rgb_light(state);
# endif // RGBLIGHT_ENABLE
//#endif
return state;
}
__attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
// Any custom LED code goes here.
// So far, I only have keyboard specific code,
// So nothing goes here.
void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
__attribute__((weak)) void eeconfig_init_keymap(void) {}
void eeconfig_init_user(void) {
userspace_config.raw = 0;
userspace_config.rgb_layer_change = true;
eeconfig_update_user(userspace_config.raw);
eeconfig_init_keymap();
keyboard_init();
}
bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
value &= 0xF;
mask &= 0xF;
return (value & mask) == mask;
}
#ifdef ENCODER_ENABLE
void encoder_update_user(uint8_t index, bool clockwise) {
if (index == 0) {
switch (biton32(layer_state)) {
case _WARCRAFT:
case _QWERTY:
// Move whole words. Hold shift to select while moving.
if (!clockwise) {
if (!is_alt_tab_active) {
is_alt_tab_active = true;
register_code(KC_LALT);
}
alt_tab_timer = timer_read();
tap_code16(KC_TAB);
} else {
if (!is_alt_tab_active) {
is_alt_tab_active = true;
register_code(KC_LALT);
}
alt_tab_timer = timer_read();
tap_code16(S(KC_TAB));
}
break;
case _RAISE:
if (!clockwise) {
if (!is_ctrl_tab_active) {
is_ctrl_tab_active = true;
register_code(KC_LCTRL);
}
ctrl_tab_timer = timer_read();
tap_code16(KC_TAB);
} else {
if (!is_ctrl_tab_active) {
is_ctrl_tab_active = true;
register_code(KC_LCTRL);
}
ctrl_tab_timer = timer_read();
tap_code16(S(KC_TAB));
}
break;
default:
// History scrubbing. For Adobe products, hold shift while moving
// backward to go forward instead.
if (clockwise) {
tap_code16(C(KC_Z));
} else {
tap_code16(C(KC_Y));
}
break;
}
} else if (index == 1) {
switch (biton32(layer_state)) {
case _QWERTY:
// Scrolling with Mouse wheel.
if (clockwise) {
tap_code(KC_WH_D);
} else {
tap_code(KC_WH_U);
}
break;
default:
// Volume control.
if (clockwise) {
tap_code(KC_VOLU);
} else {
tap_code(KC_VOLD);
}
break;
}
}
}
#endif