]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/keycode_config.c
Fixup Bootmagic code (#6386)
[qmk_firmware.git] / quantum / keycode_config.c
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
19 extern keymap_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 uint16_t keycode_config(uint16_t keycode) {
27
28     switch (keycode) {
29         case KC_CAPSLOCK:
30         case KC_LOCKING_CAPS:
31             if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
32                 return KC_LCTL;
33             }
34             return keycode;
35         case KC_LCTL:
36             if (keymap_config.swap_control_capslock) {
37                 return KC_CAPSLOCK;
38             }
39             if (keymap_config.swap_lctl_lgui) {
40                 if (keymap_config.no_gui) {
41                     return KC_NO;
42                 }
43                 return KC_LGUI;
44             }
45             return KC_LCTL;
46         case KC_LALT:
47             if (keymap_config.swap_lalt_lgui) {
48                 if (keymap_config.no_gui) {
49                     return KC_NO;
50                 }
51                 return KC_LGUI;
52             }
53             return KC_LALT;
54         case KC_LGUI:
55             if (keymap_config.swap_lalt_lgui) {
56                 return KC_LALT;
57             }
58             if (keymap_config.swap_lctl_lgui) {
59               return KC_LCTRL;
60             }
61             if (keymap_config.no_gui) {
62                 return KC_NO;
63             }
64             return KC_LGUI;
65         case KC_RCTL:
66             if (keymap_config.swap_rctl_rgui) {
67                 if (keymap_config.no_gui) {
68                     return KC_NO;
69                 }
70                 return KC_RGUI;
71             }
72             return KC_RCTL;
73         case KC_RALT:
74             if (keymap_config.swap_ralt_rgui) {
75                 if (keymap_config.no_gui) {
76                     return KC_NO;
77                 }
78                 return KC_RGUI;
79             }
80             return KC_RALT;
81         case KC_RGUI:
82             if (keymap_config.swap_ralt_rgui) {
83                 return KC_RALT;
84             }
85             if (keymap_config.swap_rctl_rgui) {
86               return KC_RCTL;
87             }
88             if (keymap_config.no_gui) {
89                 return KC_NO;
90             }
91             return KC_RGUI;
92         case KC_GRAVE:
93             if (keymap_config.swap_grave_esc) {
94                 return KC_ESC;
95             }
96             return KC_GRAVE;
97         case KC_ESC:
98             if (keymap_config.swap_grave_esc) {
99                 return KC_GRAVE;
100             }
101             return KC_ESC;
102         case KC_BSLASH:
103             if (keymap_config.swap_backslash_backspace) {
104                 return KC_BSPACE;
105             }
106             return KC_BSLASH;
107         case KC_BSPACE:
108             if (keymap_config.swap_backslash_backspace) {
109                 return KC_BSLASH;
110             }
111             return KC_BSPACE;
112         default:
113             return keycode;
114     }
115 }
116
117 /** \brief mod_config
118  *
119  *  This function checks the mods passed to it against the bootmagic config,
120  *  and will remove or replace mods, based on that.
121  */
122
123 uint8_t mod_config(uint8_t mod) {
124     if (keymap_config.swap_lalt_lgui) {
125         if ((mod & MOD_RGUI) == MOD_LGUI) {
126             mod &= ~MOD_LGUI;
127             mod |= MOD_LALT;
128         } else if ((mod & MOD_RALT) == MOD_LALT) {
129             mod &= ~MOD_LALT;
130             mod |= MOD_LGUI;
131         }
132     }
133     if (keymap_config.swap_ralt_rgui) {
134         if ((mod & MOD_RGUI) == MOD_RGUI) {
135             mod &= ~MOD_RGUI;
136             mod |= MOD_RALT;
137         } else if ((mod & MOD_RALT) == MOD_RALT) {
138             mod &= ~MOD_RALT;
139             mod |= MOD_RGUI;
140         }
141     }
142     if (keymap_config.swap_lctl_lgui) {
143       if ((mod & MOD_RGUI) == MOD_LGUI) {
144         mod &= ~MOD_LGUI;
145         mod |= MOD_LCTL;
146       } else if ((mod & MOD_RCTL) == MOD_LCTL) {
147         mod &= ~MOD_LCTL;
148         mod |= MOD_LGUI;
149       }
150     }
151     if (keymap_config.swap_rctl_rgui) {
152       if ((mod & MOD_RGUI) == MOD_RGUI) {
153         mod &= ~MOD_RGUI;
154         mod |= MOD_RCTL;
155       } else if ((mod & MOD_RCTL) == MOD_RCTL) {
156         mod &= ~MOD_RCTL;
157         mod |= MOD_RGUI;
158       }
159     }
160     if (keymap_config.no_gui) {
161         mod &= ~MOD_LGUI;
162         mod &= ~MOD_RGUI;
163     }
164
165     return mod;
166 }