]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/common/keymap.c
Merge remote-tracking branch 'tmk/master'
[max/tmk_keyboard.git] / tmk_core / common / keymap.c
1 /*
2 Copyright 2013,2016,2020 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "keymap.h"
18 #include "report.h"
19 #include "keycode.h"
20 #include "action_layer.h"
21 #include "action.h"
22 #include "action_macro.h"
23 #include "wait.h"
24 #include "debug.h"
25 #include "bootloader.h"
26 #if defined(__AVR__)
27 #include <avr/pgmspace.h>
28 #endif
29
30 #ifdef BOOTMAGIC_ENABLE
31 extern keymap_config_t keymap_config;
32 #endif
33
34 static action_t keycode_to_action(uint8_t keycode);
35
36
37 /* converts key to action */
38 __attribute__ ((weak))
39 action_t action_for_key(uint8_t layer, keypos_t key)
40 {
41     uint8_t keycode = keymap_key_to_keycode(layer, key);
42     switch (keycode) {
43         case KC_FN0 ... KC_FN31:
44             return keymap_fn_to_action(keycode);
45 #ifdef BOOTMAGIC_ENABLE
46         case KC_CAPSLOCK:
47         case KC_LOCKING_CAPS:
48             if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
49                 return keycode_to_action(KC_LCTL);
50             }
51             return keycode_to_action(keycode);
52         case KC_LCTL:
53             if (keymap_config.swap_control_capslock) {
54                 return keycode_to_action(KC_CAPSLOCK);
55             }
56             return keycode_to_action(KC_LCTL);
57         case KC_LALT:
58             if (keymap_config.swap_lalt_lgui) {
59                 if (keymap_config.no_gui) {
60                     return keycode_to_action(KC_NO);
61                 }
62                 return keycode_to_action(KC_LGUI);
63             }
64             return keycode_to_action(KC_LALT);
65         case KC_LGUI:
66             if (keymap_config.swap_lalt_lgui) {
67                 return keycode_to_action(KC_LALT);
68             }
69             if (keymap_config.no_gui) {
70                 return keycode_to_action(KC_NO);
71             }
72             return keycode_to_action(KC_LGUI);
73         case KC_RALT:
74             if (keymap_config.swap_ralt_rgui) {
75                 if (keymap_config.no_gui) {
76                     return keycode_to_action(KC_NO);
77                 }
78                 return keycode_to_action(KC_RGUI);
79             }
80             return keycode_to_action(KC_RALT);
81         case KC_RGUI:
82             if (keymap_config.swap_ralt_rgui) {
83                 return keycode_to_action(KC_RALT);
84             }
85             if (keymap_config.no_gui) {
86                 return keycode_to_action(KC_NO);
87             }
88             return keycode_to_action(KC_RGUI);
89         case KC_GRAVE:
90             if (keymap_config.swap_grave_esc) {
91                 return keycode_to_action(KC_ESC);
92             }
93             return keycode_to_action(KC_GRAVE);
94         case KC_ESC:
95             if (keymap_config.swap_grave_esc) {
96                 return keycode_to_action(KC_GRAVE);
97             }
98             return keycode_to_action(KC_ESC);
99         case KC_BSLASH:
100             if (keymap_config.swap_backslash_backspace) {
101                 return keycode_to_action(KC_BSPACE);
102             }
103             return keycode_to_action(KC_BSLASH);
104         case KC_BSPACE:
105             if (keymap_config.swap_backslash_backspace) {
106                 return keycode_to_action(KC_BSLASH);
107             }
108             return keycode_to_action(KC_BSPACE);
109 #endif
110         default:
111             return keycode_to_action(keycode);
112     }
113 }
114
115
116 /* Macro */
117 __attribute__ ((weak))
118 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
119 {
120     (void)record;
121     (void)id;
122     (void)opt;
123     return MACRO_NONE;
124 }
125
126 /* Function */
127 __attribute__ ((weak))
128 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
129 {
130     (void)record;
131     (void)id;
132     (void)opt;
133 }
134
135
136
137 /* translates keycode to action */
138 static action_t keycode_to_action(uint8_t keycode)
139 {
140     switch (keycode) {
141         case KC_A ... KC_EXSEL:
142         case KC_LCTRL ... KC_RGUI:
143             return (action_t)ACTION_KEY(keycode);
144             break;
145         case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
146             return (action_t)ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
147             break;
148         case KC_AUDIO_MUTE ... KC_BRIGHTNESS_DEC:
149             return (action_t)ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
150             break;
151         case KC_MS_UP ... KC_MS_ACCEL2:
152             return (action_t)ACTION_MOUSEKEY(keycode);
153             break;
154         case KC_TRNS:
155             return (action_t)ACTION_TRANSPARENT;
156             break;
157         case KC_BOOTLOADER:
158             return (action_t)ACTION_COMMAND(COMMAND_BOOTLOADER, 0);
159             break;
160         default:
161             return (action_t)ACTION_NO;
162             break;
163     }
164     return (action_t)ACTION_NO;
165 }
166
167
168
169 #ifdef USE_LEGACY_KEYMAP
170 /*
171  * Legacy keymap support
172  *      Consider using new keymap API instead.
173  */
174 extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
175 extern const uint8_t fn_layer[];
176 extern const uint8_t fn_keycode[];
177
178 __attribute__ ((weak))
179 uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col)
180 {
181     return pgm_read_byte(&keymaps[(layer)][(row)][(col)]);
182 }
183
184 __attribute__ ((weak))
185 uint8_t keymap_fn_layer(uint8_t index)
186 {
187     return pgm_read_byte(&fn_layer[index]);
188 }
189
190 __attribute__ ((weak))
191 uint8_t keymap_fn_keycode(uint8_t index)
192 {
193     return pgm_read_byte(&fn_keycode[index]);
194 }
195
196 __attribute__ ((weak))
197 uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
198 {
199     return keymap_get_keycode(layer, key.row, key.col);
200 }
201
202
203 /* Legacy keymap support */
204 __attribute__ ((weak))
205 action_t keymap_fn_to_action(uint8_t keycode)
206 {
207     switch (keycode) {
208         case KC_FN0 ... KC_FN31:
209             {
210                 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
211                 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
212                 if (key) {
213                     return (action_t)ACTION_LAYER_TAP_KEY(layer, key);
214                 } else {
215                     return (action_t)ACTION_LAYER_MOMENTARY(layer);
216                 }
217             }
218             return (action_t)ACTION_NO;
219         default:
220             return (action_t)ACTION_NO;
221     }
222 }
223
224 #else
225
226 /* user keymaps should be defined somewhere */
227 extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
228 extern const action_t fn_actions[];
229
230 __attribute__ ((weak))
231 uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
232 {
233 #if defined(__AVR__)
234     return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
235 #else
236     return keymaps[(layer)][(key.row)][(key.col)];
237 #endif
238 }
239
240 __attribute__ ((weak))
241 action_t keymap_fn_to_action(uint8_t keycode)
242 {
243 #if defined(__AVR__)
244     return (action_t)pgm_read_word(&fn_actions[FN_INDEX(keycode)]);
245 #else
246     return fn_actions[FN_INDEX(keycode)];
247 #endif
248 }
249
250 #endif