From 7f77db7c0157de566ea674bb16a7fa4667f6845e Mon Sep 17 00:00:00 2001 From: Timothy Davis Date: Fri, 5 Jan 2024 10:11:48 +0930 Subject: [PATCH] Add custom shift keys. Organise code --- combos.h | 94 ++++++++++++++++++++++++++ config.h | 1 + features/custom_shift_keys.c | 81 ++++++++++++++++++++++ features/custom_shift_keys.h | 99 +++++++++++++++++++++++++++ flmng0.c | 126 +++++++++-------------------------- hrm.h | 53 ++------------- layout.h | 62 +++++------------ rules.mk | 1 + 8 files changed, 329 insertions(+), 188 deletions(-) create mode 100644 combos.h create mode 100644 features/custom_shift_keys.c create mode 100644 features/custom_shift_keys.h diff --git a/combos.h b/combos.h new file mode 100644 index 0000000..b8b8491 --- /dev/null +++ b/combos.h @@ -0,0 +1,94 @@ +#include "quantum.h" +#include "hrm.h" + +/** + * Num row symbols. + */ +#define COMBO_DEF(var, ...) \ + const uint16_t PROGMEM var[] = {__VA_ARGS__, COMBO_END} + +COMBO_DEF(c_at, KC_W, HM_R); +COMBO_DEF(c_pound, KC_F, HM_S); +COMBO_DEF(c_dollar, KC_P, HM_T); +COMBO_DEF(c_percent, KC_B, KC_G); + +COMBO_DEF(c_carat, KC_J, KC_M); +COMBO_DEF(c_and, KC_L, HM_N); +COMBO_DEF(c_star, KC_U, HM_E); +COMBO_DEF(c_slash, KC_Y, HM_I); + +/** + * Parenthesis. + * + * Open on left hand, close on right hand. + */ +COMBO_DEF(c_lparen, HM_T, HM_S); +COMBO_DEF(c_rparen, HM_N, HM_E); + +COMBO_DEF(c_lbracket, HM_T, HM_R); +COMBO_DEF(c_rbracket, HM_N, HM_I); + +COMBO_DEF(c_lbrace, HM_S, HM_R); +COMBO_DEF(c_rbrace, HM_E, HM_I); + +COMBO_DEF(c_langle, HM_T, HM_S, HM_R); +COMBO_DEF(c_rangle, HM_N, HM_E, HM_I); + +/** + * Common symbols. + */ +COMBO_DEF(c_hyphen, KC_B, KC_J); +COMBO_DEF(c_equals, KC_G, KC_M); +COMBO_DEF(c_underscore, KC_V, KC_K); + +/** + * Functional keys. + */ +COMBO_DEF(c_backspace, KC_L, KC_U); +COMBO_DEF(c_delete, KC_U, KC_Y); + +enum COMBO_INDICES { + C_LPAREN, + C_RPAREN, + C_LBRACKET, + C_RBRACKET, + C_LBRACE, + C_RBRACE, + C_LANGLE, + C_RANGLE, +}; + + +combo_t key_combos[] = { + // Parenthesis + [C_LPAREN] = COMBO(c_lparen, LSFT(KC_9)), + [C_RPAREN] = COMBO(c_rparen, LSFT(KC_0)), + + [C_LBRACKET] = COMBO(c_lbracket, KC_LEFT_BRACKET), + [C_RBRACKET] = COMBO(c_rbracket, KC_RIGHT_BRACKET), + + [C_LBRACE] = COMBO(c_lbrace, LSFT(KC_LEFT_BRACKET)), + [C_RBRACE] = COMBO(c_rbrace, LSFT(KC_RIGHT_BRACKET)), + + [C_LANGLE] = COMBO(c_langle, LSFT(KC_COMMA)), + [C_RANGLE] = COMBO(c_rangle, LSFT(KC_DOT)), + + // Upper Symbols + COMBO(c_at, LSFT(KC_2)), + COMBO(c_pound, LSFT(KC_3)), + COMBO(c_dollar, LSFT(KC_4)), + COMBO(c_percent, LSFT(KC_5)), + COMBO(c_carat, LSFT(KC_6)), + COMBO(c_and, LSFT(KC_7)), + COMBO(c_star, LSFT(KC_8)), + COMBO(c_slash, LSFT(KC_9)), + + // Common symbols + COMBO(c_hyphen, KC_MINUS), + COMBO(c_equals, KC_EQUAL), + COMBO(c_underscore, LSFT(KC_MINUS)), + + COMBO(c_backspace, KC_BACKSPACE), + COMBO(c_delete, KC_DELETE), +}; + diff --git a/config.h b/config.h index e62e1da..4a8820a 100644 --- a/config.h +++ b/config.h @@ -6,6 +6,7 @@ #define TAP_TERM_PER_KEY #define TAPPING_TERM 250 +#define COMBO_TERM 50 #define COMBO_TERM_PER_COMBO #define DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD diff --git a/features/custom_shift_keys.c b/features/custom_shift_keys.c new file mode 100644 index 0000000..8b1934d --- /dev/null +++ b/features/custom_shift_keys.c @@ -0,0 +1,81 @@ +// Copyright 2021-2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file custom_shift_keys.c + * @brief Custom Shift Keys implementation + * + * For full documentation, see + * + */ + +#include "custom_shift_keys.h" + +#if !defined(IS_QK_MOD_TAP) +// Attempt to detect out-of-date QMK installation, which would fail with +// implicit-function-declaration errors in the code below. +#error "custom_shift_keys: QMK version is too old to build. Please update QMK." +#else + +bool process_custom_shift_keys(uint16_t keycode, keyrecord_t *record) { + static uint16_t registered_keycode = KC_NO; + + // If a custom shift key is registered, then this event is either releasing + // it or manipulating another key at the same time. Either way, we release + // the currently registered key. + if (registered_keycode != KC_NO) { + unregister_code16(registered_keycode); + registered_keycode = KC_NO; + } + + if (record->event.pressed) { // Press event. + const uint8_t mods = get_mods(); +#ifndef NO_ACTION_ONESHOT + if ((mods | get_weak_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT) { +#else + if ((mods | get_weak_mods()) & MOD_MASK_SHIFT) { // Shift is held. +#endif // NO_ACTION_ONESHOT + // Continue default handling if this is a tap-hold key being held. + if ((IS_QK_MOD_TAP(keycode) || IS_QK_LAYER_TAP(keycode)) && + record->tap.count == 0) { + return true; + } + + // Search for a custom shift key whose keycode is `keycode`. + for (int i = 0; i < NUM_CUSTOM_SHIFT_KEYS; ++i) { + if (keycode == custom_shift_keys[i].keycode) { + registered_keycode = custom_shift_keys[i].shifted_keycode; + if (IS_QK_MODS(registered_keycode) && // Should keycode be shifted? + (QK_MODS_GET_MODS(registered_keycode) & MOD_LSFT) != 0) { + register_code16(registered_keycode); // If so, press it directly. + } else { + // Otherwise cancel shift mods, press the key, and restore mods. + del_weak_mods(MOD_MASK_SHIFT); +#ifndef NO_ACTION_ONESHOT + del_oneshot_mods(MOD_MASK_SHIFT); +#endif // NO_ACTION_ONESHOT + unregister_mods(MOD_MASK_SHIFT); + register_code16(registered_keycode); + set_mods(mods); + } + return false; + } + } + } + } + + return true; // Continue with default handling. +} + +#endif // version check diff --git a/features/custom_shift_keys.h b/features/custom_shift_keys.h new file mode 100644 index 0000000..d6cb7a9 --- /dev/null +++ b/features/custom_shift_keys.h @@ -0,0 +1,99 @@ +// Copyright 2021-2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file custom_shift_keys.h + * @brief Custom shift keys: customize what keycode is produced when shifted. + * + * Overview + * -------- + * + * This library implements custom shift keys, keys where you can customize + * what keycode is produced when shifted. + * + * Step 1: In your keymap.c, define a table of custom shift keys like + * + * #include "features/custom_shift_keys.h" + * + * const custom_shift_key_t custom_shift_keys[] = { + * {KC_DOT , KC_QUES}, // Shift . is ? + * {KC_COMM, KC_EXLM}, // Shift , is ! + * {KC_MINS, KC_EQL }, // Shift - is = + * {KC_COLN, KC_SCLN}, // Shift : is ; + * }; + * + * Each row defines one key. The first field is the keycode as it appears in + * your layout and determines what is typed normally. The second entry is what + * you want the key to type when shifted. + * + * Step 2: Handle custom shift keys from your `process_record_user` function as + * + * bool process_record_user(uint16_t keycode, keyrecord_t* record) { + * if (!process_custom_shift_keys(keycode, record)) { return false; } + * // Your macros ... + * + * return true; + * } + * + * Step 3: add `features/custom_shift_keys.c` to your rules.mk as + * + * SRC += features/custom_shift_keys.c + * + * + * For full documentation, see + * + */ + +#pragma once + +#include "quantum.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Custom shift key entry. The `keycode` field is the keycode as it appears in + * your layout and determines what is typed normally. The `shifted_keycode` is + * what you want the key to type when shifted. + */ +typedef struct { + uint16_t keycode; + uint16_t shifted_keycode; +} custom_shift_key_t; + +/** Table of custom shift keys. */ +extern const custom_shift_key_t custom_shift_keys[]; +/** Number of entries in the `custom_shift_keys` table. */ +extern uint8_t NUM_CUSTOM_SHIFT_KEYS; + +/** + * Handler function for custom shift keys. + * + * In keymap.c, call this function from your `process_record_user` function as + * + * #include "features/custom_shift_keys.h" + * + * bool process_record_user(uint16_t keycode, keyrecord_t* record) { + * if (!process_custom_shift_keys(keycode, record)) { return false; } + * // Your macros ... + * + * return true; + * } + */ +bool process_custom_shift_keys(uint16_t keycode, keyrecord_t *record); + +#ifdef __cplusplus +} +#endif diff --git a/flmng0.c b/flmng0.c index 7912bc7..3554bfa 100644 --- a/flmng0.c +++ b/flmng0.c @@ -1,5 +1,6 @@ #pragma once +#include "action_util.h" #ifndef CAPS_WORD_ENABLE # define CAPS_WORD_ENABLE #endif @@ -8,96 +9,26 @@ #include "layout.h" #include "quantum.h" -#ifdef HOME_ROW_MODS -# ifdef HRM_LAYOUT_ONLY -# undef HRM_LAYOUT_ONLY -# endif -# include "hrm.h" -#endif +#include "features/custom_shift_keys.h" -/** - * Num row symbols. - */ -#define COMBO_DEF(var, ...) \ - const uint16_t PROGMEM var[] = {__VA_ARGS__, COMBO_END} - -COMBO_DEF(c_at, KC_W, CC_R); -COMBO_DEF(c_pound, KC_F, CC_S); -COMBO_DEF(c_dollar, KC_P, CC_T); -COMBO_DEF(c_percent, KC_B, KC_G); - -COMBO_DEF(c_carat, KC_J, KC_M); -COMBO_DEF(c_and, KC_L, CC_N); -COMBO_DEF(c_star, KC_U, CC_E); -COMBO_DEF(c_slash, KC_Y, CC_I); - -/** - * Parenthesis. - * - * Open on left hand, close on right hand. - */ -COMBO_DEF(c_lparen, CC_T, CC_S); -COMBO_DEF(c_rparen, CC_N, CC_E); - -COMBO_DEF(c_lbracket, CC_T, CC_R); -COMBO_DEF(c_rbracket, CC_N, CC_I); - -COMBO_DEF(c_lbrace, CC_S, CC_R); -COMBO_DEF(c_rbrace, CC_N, CC_E); - -COMBO_DEF(c_langle, CC_T, CC_S, CC_R); -COMBO_DEF(c_rangle, CC_N, CC_E, CC_I); - -/** - * Common symbols. - */ -COMBO_DEF(c_hyphen, KC_B, KC_J); -COMBO_DEF(c_equals, KC_G, KC_M); -COMBO_DEF(c_underscore, KC_V, KC_K); - -/** - * Functional keys. - */ -COMBO_DEF(c_backspace, KC_L, KC_U); -COMBO_DEF(c_delete, KC_U, KC_Y); - -combo_t key_combos[] = { - // Upper Symbols - COMBO(c_at, LSFT(KC_2)), - COMBO(c_pound, LSFT(KC_3)), - COMBO(c_dollar, LSFT(KC_4)), - COMBO(c_percent, LSFT(KC_5)), - COMBO(c_carat, LSFT(KC_6)), - COMBO(c_and, LSFT(KC_7)), - COMBO(c_star, LSFT(KC_8)), - COMBO(c_slash, LSFT(KC_9)), - - // Parenthesis - COMBO(c_lparen, LSFT(KC_9)), - COMBO(c_rparen, LSFT(KC_0)), - - COMBO(c_lbracket, KC_LEFT_BRACKET), - COMBO(c_rbracket, KC_RIGHT_BRACKET), - - COMBO(c_lbrace, LSFT(KC_LEFT_BRACKET)), - COMBO(c_rbrace, LSFT(KC_RIGHT_BRACKET)), - - COMBO(c_langle, LSFT(KC_COMMA)), - COMBO(c_rangle, LSFT(KC_DOT)), - - // Common symbols - COMBO(c_hyphen, KC_MINUS), - COMBO(c_equals, KC_EQUAL), - COMBO(c_underscore, LSFT(KC_MINUS)), - - COMBO(c_backspace, KC_BACKSPACE), - COMBO(c_delete, KC_DELETE), -}; +#include "hrm.h" +#include "combos.h" + +uint16_t get_combo_term(uint16_t index, combo_t *combo) { + (void)combo; + + switch (index) { + case C_LPAREN...C_RANGLE: + return 30; + } + + return COMBO_TERM; +} uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { switch (keycode) { - case L_NAV: - case L_SYM: + case TH_LH: + case TH_RA: return TAPPING_TERM + 200; default: @@ -107,9 +38,10 @@ uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { bool get_permissive_hold(uint16_t keycode, keyrecord_t *record) { switch (keycode) { - case L_NAV: - case L_SYM: - case L_NUM: + case TH_LA: + case TH_LH: + case TH_RA: + case TH_RH: return false; default: @@ -135,14 +67,19 @@ bool caps_word_press_user(uint16_t keycode) { } } +const custom_shift_key_t custom_shift_keys[] = { + {KC_QUESTION, KC_EXCLAIM}, + {KC_COMM, KC_SEMICOLON}, + {KC_DOT, KC_COLON}, +}; +uint8_t NUM_CUSTOM_SHIFT_KEYS = sizeof(custom_shift_keys) / sizeof(custom_shift_key_t); + bool process_record_user(uint16_t keycode, keyrecord_t *record) { -#ifdef HOME_ROW_MODS - if (!process_hrm(keycode, record)) { - return false; - } -#endif + if (!process_hrm(keycode, record)) { return false; } + if (!process_custom_shift_keys(keycode, record)) { return false; } const uint8_t mods = get_mods(); + switch (keycode) { /* If shift is already being pressed and smart shift is pressed, then activate caps word */ @@ -151,6 +88,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { caps_word_toggle(); return false; } + break; } return true; diff --git a/hrm.h b/hrm.h index 3df2354..d425597 100644 --- a/hrm.h +++ b/hrm.h @@ -1,50 +1,11 @@ -#include "caps_word.h" -#ifndef HRM_LAYOUT_ONLY -# include "keycode.h" -# include "quantum.h" -# include "timer.h" -#endif - -#ifndef HRM_KCS_DEFINED -#define HRM_KCS_DEFINED - -#define HM_A LGUI_T(KC_A) -#define HM_R LALT_T(KC_R) -#define HM_S LSFT_T(KC_S) -#define HM_T LCTL_T(KC_T) - -#define HM_N RCTL_T(KC_N) -#define HM_E RSFT_T(KC_E) -#define HM_I LALT_T(KC_I) -#define HM_O RGUI_T(KC_O) - -#define CC_T HM_T -#define CC_D KC_D - -#define CC_N HM_N -#define CC_H KC_H - -#define CC_S HM_S -#define CC_C KC_C +#pragma once -#define CC_E HM_E -#define CC_COMM KC_COMM - -#define CC_R HM_R -#define CC_X KC_X - -#define CC_I HM_I -#define CC_DOT KC_DOT - -#define BASE_HRM \ - KC_Q , KC_W , KC_F , KC_P , KC_B , KC_J , KC_L , KC_U , KC_Y , KC_SCLN, \ - HM_A , HM_R , HM_S , HM_T , KC_G , KC_M , HM_N , HM_E , HM_I , HM_O , \ - KC_Z , KC_X , KC_C , KC_D , KC_V , KC_K , KC_H , KC_COMM, KC_DOT , KC_QUOT, \ - TH_LA , TH_LH , TH_RH , TH_RA - -#endif +#include "caps_word.h" +#include "keycode.h" +#include "quantum.h" +#include "timer.h" -#ifndef HRM_LAYOUT_ONLY +#include "layout.h" #ifndef HRM_IDLE_DELAY #define HRM_IDLE_DELAY (150) @@ -92,5 +53,3 @@ bool process_hrm(uint16_t keycode, keyrecord_t *record) { } #define IS_IDLE (timer_elapsed32(idle_timer) >= HRM_IDLE_DELAY) - -#endif // HRM_LAYOUT_ONLY diff --git a/layout.h b/layout.h index 6ff3db0..7417fa4 100644 --- a/layout.h +++ b/layout.h @@ -1,5 +1,7 @@ #pragma once +#include "quantum_keycodes.h" + #define ID_BASE 0 #define ID_NAV 1 #define ID_SYM 2 @@ -13,62 +15,28 @@ #define LT_SYM(code) LT(ID_SYM, code) #define LT_NUM(code) LT(ID_NUM, code) -#define L_NAV LT_NAV(KC_SPACE) -#define L_SYM LT_SYM(KC_ENTER) -#define L_NUM LT_NUM(KC_TAB) - #define TH_LA LT_NUM(KC_TAB) #define TH_LH LT_NAV(KC_SPACE) -#define TH_RH LT_SYM(KC_ENTER) -#define TH_RA OSM(MOD_LSFT) - -#ifdef HOME_ROW_MODS - -# define HRM_LAYOUT_ONLY -# include "hrm.h" -# define _BASE BASE_HRM -# undef HRM_LAYOUT_ONLY - -#else - -#define BM_Z LGUI_T(KC_Z) -#define BM_X LALT_T(KC_X) -#define BM_C LSFT_T(KC_C) -#define BM_D LCTL_T(KC_D) - -#define BM_H RCTL_T(KC_H) -#define BM_COMM RSFT_T(KC_COMM) -#define BM_DOT LALT_T(KC_DOT) -#define BM_QUOT RGUI_T(KC_QUOT) +#define TH_RH OSM(MOD_LSFT) +#define TH_RA LT_SYM(KC_ENTER) -#define CC_T KC_T -#define CC_D BM_D - -#define CC_N KC_N -#define CC_H BM_H - -#define CC_S KC_S -#define CC_C BM_C - -#define CC_E KC_E -#define CC_COMM BM_COMM - -#define CC_R KC_R -#define CC_X BM_X - -#define CC_I KC_I -#define CC_DOT BM_DOT +#define HM_A LGUI_T(KC_A) +#define HM_R LALT_T(KC_R) +#define HM_S LSFT_T(KC_S) +#define HM_T LCTL_T(KC_T) +#define HM_N RCTL_T(KC_N) +#define HM_E RSFT_T(KC_E) +#define HM_I LALT_T(KC_I) +#define HM_O RGUI_T(KC_O) #define _BASE \ - KC_Q , KC_W , KC_F , KC_P , KC_B , KC_J , KC_L , KC_U , KC_Y , KC_SCLN, \ - KC_A , KC_R , KC_S , KC_T , KC_G , KC_M , KC_N , KC_E , KC_I , KC_O , \ - BM_Z , BM_X , BM_C , BM_D , KC_V , KC_K , BM_H , BM_COMM, BM_DOT , BM_QUOT, \ + KC_Q , KC_W , KC_F , KC_P , KC_B , KC_J , KC_L , KC_U , KC_Y , KC_QUES, \ + HM_A , HM_R , HM_S , HM_T , KC_G , KC_M , HM_N , HM_E , HM_I , HM_O , \ + KC_Z , KC_X , KC_C , KC_D , KC_V , KC_K , KC_H , KC_COMM, KC_DOT , KC_QUOT, \ TH_LA , TH_LH , TH_RH , TH_RA -#endif - #define _NAV \ KC_ESC , KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_HOME, KC_PGDN, KC_PGUP, KC_END , \ CW_TOGG, KC_NO , KC_NO , KC_NO , KC_NO , KC_NO , KC_LEFT, KC_DOWN, KC_UP , KC_RGHT, \ diff --git a/rules.mk b/rules.mk index 82ff6f9..8451d4f 100644 --- a/rules.mk +++ b/rules.mk @@ -2,6 +2,7 @@ COMBO_ENABLE = yes CAPS_WORD_ENABLE = yes INTROSPECTION_KEYMAP_C = flmng0.c +SRC += features/custom_shift_keys.c OPT_DEFS += -DHOME_ROW_MODS