Parent directory

keycode_config.c

5588 bytes
  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    switch (keycode) {
 28        case KC_CAPS_LOCK:
 29        case KC_LOCKING_CAPS_LOCK:
 30            if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
 31                return KC_LEFT_CTRL;
 32            } else if (keymap_config.swap_escape_capslock) {
 33                return KC_ESCAPE;
 34            }
 35            return keycode;
 36        case KC_LEFT_CTRL:
 37            if (keymap_config.swap_control_capslock) {
 38                return KC_CAPS_LOCK;
 39            }
 40            if (keymap_config.swap_lctl_lgui) {
 41                if (keymap_config.no_gui) {
 42                    return KC_NO;
 43                }
 44                return KC_LEFT_GUI;
 45            }
 46            return KC_LEFT_CTRL;
 47        case KC_LEFT_ALT:
 48            if (keymap_config.swap_lalt_lgui) {
 49                if (keymap_config.no_gui) {
 50                    return KC_NO;
 51                }
 52                return KC_LEFT_GUI;
 53            }
 54            return KC_LEFT_ALT;
 55        case KC_LEFT_GUI:
 56            if (keymap_config.swap_lalt_lgui) {
 57                return KC_LEFT_ALT;
 58            }
 59            if (keymap_config.swap_lctl_lgui) {
 60                return KC_LEFT_CTRL;
 61            }
 62            if (keymap_config.no_gui) {
 63                return KC_NO;
 64            }
 65            return KC_LEFT_GUI;
 66        case KC_RIGHT_CTRL:
 67            if (keymap_config.swap_rctl_rgui) {
 68                if (keymap_config.no_gui) {
 69                    return KC_NO;
 70                }
 71                return KC_RIGHT_GUI;
 72            }
 73            return KC_RIGHT_CTRL;
 74        case KC_RIGHT_ALT:
 75            if (keymap_config.swap_ralt_rgui) {
 76                if (keymap_config.no_gui) {
 77                    return KC_NO;
 78                }
 79                return KC_RIGHT_GUI;
 80            }
 81            return KC_RIGHT_ALT;
 82        case KC_RIGHT_GUI:
 83            if (keymap_config.swap_ralt_rgui) {
 84                return KC_RIGHT_ALT;
 85            }
 86            if (keymap_config.swap_rctl_rgui) {
 87                return KC_RIGHT_CTRL;
 88            }
 89            if (keymap_config.no_gui) {
 90                return KC_NO;
 91            }
 92            return KC_RIGHT_GUI;
 93        case KC_GRAVE:
 94            if (keymap_config.swap_grave_esc) {
 95                return KC_ESCAPE;
 96            }
 97            return KC_GRAVE;
 98        case KC_ESCAPE:
 99            if (keymap_config.swap_grave_esc) {
100                return KC_GRAVE;
101            } else if (keymap_config.swap_escape_capslock) {
102                return KC_CAPS_LOCK;
103            }
104            return KC_ESCAPE;
105        case KC_BACKSLASH:
106            if (keymap_config.swap_backslash_backspace) {
107                return KC_BACKSPACE;
108            }
109            return KC_BACKSLASH;
110        case KC_BACKSPACE:
111            if (keymap_config.swap_backslash_backspace) {
112                return KC_BACKSLASH;
113            }
114            return KC_BACKSPACE;
115        default:
116            return keycode;
117    }
118}
119
120/** \brief mod_config
121 *
122 *  This function checks the mods passed to it against the bootmagic config,
123 *  and will remove or replace mods, based on that.
124 */
125
126__attribute__((weak)) uint8_t mod_config(uint8_t mod) {
127    /**
128     * Note: This function is for the 5-bit packed mods, NOT the full 8-bit mods.
129     * More info about the mods can be seen in modifiers.h.
130     */
131    if (keymap_config.swap_lalt_lgui) {
132        /** If both modifiers pressed or neither pressed, do nothing
133         * Otherwise swap the values
134         * Note: The left mods are ANDed with the right-hand values to check
135         * if they were pressed with the right hand bit set
136         */
137        if (((mod & MOD_RALT) == MOD_LALT) ^ ((mod & MOD_RGUI) == MOD_LGUI)) {
138            mod ^= (MOD_LALT | MOD_LGUI);
139        }
140    }
141    if (keymap_config.swap_ralt_rgui) {
142        if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) {
143            /* lefthand values to preserve the right hand bit */
144            mod ^= (MOD_LALT | MOD_LGUI);
145        }
146    }
147    if (keymap_config.swap_lctl_lgui) {
148        /* left mods ANDed with right-hand values to check for right hand bit */
149        if (((mod & MOD_RCTL) == MOD_LCTL) ^ ((mod & MOD_RGUI) == MOD_LGUI)) {
150            mod ^= (MOD_LCTL | MOD_LGUI);
151        }
152    }
153    if (keymap_config.swap_rctl_rgui) {
154        if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) {
155            /* lefthand values to preserve the right hand bit */
156            mod ^= (MOD_LCTL | MOD_LGUI);
157        }
158    }
159    if (keymap_config.no_gui) {
160        mod &= ~MOD_LGUI;
161        mod &= ~MOD_RGUI;
162    }
163
164    return mod;
165}