at master 172 lines 5.7 kB view raw
1/* Copyright 2016 Jack Humbert 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17#include "keycode_config.h" 18 19keymap_config_t keymap_config; 20 21/** \brief keycode_config 22 * 23 * This function is used to check a specific keycode against the bootmagic config, 24 * and will return the corrected keycode, when appropriate. 25 */ 26__attribute__((weak)) uint16_t keycode_config(uint16_t keycode) { 27#ifdef MAGIC_ENABLE 28 switch (keycode) { 29 case KC_CAPS_LOCK: 30 case KC_LOCKING_CAPS_LOCK: 31 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) { 32 return KC_LEFT_CTRL; 33 } else if (keymap_config.swap_escape_capslock) { 34 return KC_ESCAPE; 35 } 36 return keycode; 37 case KC_LEFT_CTRL: 38 if (keymap_config.swap_control_capslock) { 39 return KC_CAPS_LOCK; 40 } 41 if (keymap_config.swap_lctl_lgui) { 42 if (keymap_config.no_gui) { 43 return KC_NO; 44 } 45 return KC_LEFT_GUI; 46 } 47 return KC_LEFT_CTRL; 48 case KC_LEFT_ALT: 49 if (keymap_config.swap_lalt_lgui) { 50 if (keymap_config.no_gui) { 51 return KC_NO; 52 } 53 return KC_LEFT_GUI; 54 } 55 return KC_LEFT_ALT; 56 case KC_LEFT_GUI: 57 if (keymap_config.swap_lalt_lgui) { 58 return KC_LEFT_ALT; 59 } 60 if (keymap_config.swap_lctl_lgui) { 61 return KC_LEFT_CTRL; 62 } 63 if (keymap_config.no_gui) { 64 return KC_NO; 65 } 66 return KC_LEFT_GUI; 67 case KC_RIGHT_CTRL: 68 if (keymap_config.swap_rctl_rgui) { 69 if (keymap_config.no_gui) { 70 return KC_NO; 71 } 72 return KC_RIGHT_GUI; 73 } 74 return KC_RIGHT_CTRL; 75 case KC_RIGHT_ALT: 76 if (keymap_config.swap_ralt_rgui) { 77 if (keymap_config.no_gui) { 78 return KC_NO; 79 } 80 return KC_RIGHT_GUI; 81 } 82 return KC_RIGHT_ALT; 83 case KC_RIGHT_GUI: 84 if (keymap_config.swap_ralt_rgui) { 85 return KC_RIGHT_ALT; 86 } 87 if (keymap_config.swap_rctl_rgui) { 88 return KC_RIGHT_CTRL; 89 } 90 if (keymap_config.no_gui) { 91 return KC_NO; 92 } 93 return KC_RIGHT_GUI; 94 case KC_GRAVE: 95 if (keymap_config.swap_grave_esc) { 96 return KC_ESCAPE; 97 } 98 return KC_GRAVE; 99 case KC_ESCAPE: 100 if (keymap_config.swap_grave_esc) { 101 return KC_GRAVE; 102 } else if (keymap_config.swap_escape_capslock) { 103 return KC_CAPS_LOCK; 104 } 105 return KC_ESCAPE; 106 case KC_BACKSLASH: 107 if (keymap_config.swap_backslash_backspace) { 108 return KC_BACKSPACE; 109 } 110 return KC_BACKSLASH; 111 case KC_BACKSPACE: 112 if (keymap_config.swap_backslash_backspace) { 113 return KC_BACKSLASH; 114 } 115 return KC_BACKSPACE; 116 default: 117 return keycode; 118 } 119#else 120 return keycode; 121#endif // MAGIC_ENABLE 122} 123 124/** \brief mod_config 125 * 126 * This function checks the mods passed to it against the bootmagic config, 127 * and will remove or replace mods, based on that. 128 */ 129 130__attribute__((weak)) uint8_t mod_config(uint8_t mod) { 131#ifdef MAGIC_ENABLE 132 /** 133 * Note: This function is for the 5-bit packed mods, NOT the full 8-bit mods. 134 * More info about the mods can be seen in modifiers.h. 135 */ 136 if (keymap_config.swap_lalt_lgui) { 137 /** If both modifiers pressed or neither pressed, do nothing 138 * Otherwise swap the values 139 * Note: The left mods are ANDed with the right-hand values to check 140 * if they were pressed with the right hand bit set 141 */ 142 if (((mod & MOD_RALT) == MOD_LALT) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { 143 mod ^= (MOD_LALT | MOD_LGUI); 144 } 145 } 146 if (keymap_config.swap_ralt_rgui) { 147 if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { 148 /* lefthand values to preserve the right hand bit */ 149 mod ^= (MOD_LALT | MOD_LGUI); 150 } 151 } 152 if (keymap_config.swap_lctl_lgui) { 153 /* left mods ANDed with right-hand values to check for right hand bit */ 154 if (((mod & MOD_RCTL) == MOD_LCTL) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { 155 mod ^= (MOD_LCTL | MOD_LGUI); 156 } 157 } 158 if (keymap_config.swap_rctl_rgui) { 159 if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { 160 /* lefthand values to preserve the right hand bit */ 161 mod ^= (MOD_LCTL | MOD_LGUI); 162 } 163 } 164 if (keymap_config.no_gui) { 165 if (mod & MOD_LGUI) { 166 mod &= ~MOD_RGUI; 167 } 168 } 169 170#endif // MAGIC_ENABLE 171 return mod; 172}