Merge branch 'personal_atomic_planck' of github.com:IBNobody/qmk_firmware
[jackhill/qmk/firmware.git] / quantum / keymap_common.c
1 /*
2 Copyright 2012,2013 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
18 #include "keymap_common.h"
19 #include "report.h"
20 #include "keycode.h"
21 #include "action_layer.h"
22 #include <util/delay.h>
23 #include "action.h"
24 #include "action_macro.h"
25 #include "debug.h"
26 #include "backlight.h"
27 #include "keymap_midi.h"
28 #include "bootloader.h"
29 #include "eeconfig.h"
30
31 extern keymap_config_t keymap_config;
32
33 #include <stdio.h>
34 #include <inttypes.h>
35 #ifdef AUDIO_ENABLE
36 #include "audio.h"
37 #endif /* AUDIO_ENABLE */
38
39 static action_t keycode_to_action(uint16_t keycode);
40
41 /* converts key to action */
42 action_t action_for_key(uint8_t layer, keypos_t key)
43 {
44 // 16bit keycodes - important
45 uint16_t keycode = keymap_key_to_keycode(layer, key);
46
47 switch (keycode) {
48 case KC_FN0 ... KC_FN31:
49 return keymap_fn_to_action(keycode);
50 case KC_CAPSLOCK:
51 case KC_LOCKING_CAPS:
52 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
53 return keycode_to_action(KC_LCTL);
54 }
55 return keycode_to_action(keycode);
56 case KC_LCTL:
57 if (keymap_config.swap_control_capslock) {
58 return keycode_to_action(KC_CAPSLOCK);
59 }
60 return keycode_to_action(KC_LCTL);
61 case KC_LALT:
62 if (keymap_config.swap_lalt_lgui) {
63 if (keymap_config.no_gui) {
64 return keycode_to_action(ACTION_NO);
65 }
66 return keycode_to_action(KC_LGUI);
67 }
68 return keycode_to_action(KC_LALT);
69 case KC_LGUI:
70 if (keymap_config.swap_lalt_lgui) {
71 return keycode_to_action(KC_LALT);
72 }
73 if (keymap_config.no_gui) {
74 return keycode_to_action(ACTION_NO);
75 }
76 return keycode_to_action(KC_LGUI);
77 case KC_RALT:
78 if (keymap_config.swap_ralt_rgui) {
79 if (keymap_config.no_gui) {
80 return keycode_to_action(ACTION_NO);
81 }
82 return keycode_to_action(KC_RGUI);
83 }
84 return keycode_to_action(KC_RALT);
85 case KC_RGUI:
86 if (keymap_config.swap_ralt_rgui) {
87 return keycode_to_action(KC_RALT);
88 }
89 if (keymap_config.no_gui) {
90 return keycode_to_action(ACTION_NO);
91 }
92 return keycode_to_action(KC_RGUI);
93 case KC_GRAVE:
94 if (keymap_config.swap_grave_esc) {
95 return keycode_to_action(KC_ESC);
96 }
97 return keycode_to_action(KC_GRAVE);
98 case KC_ESC:
99 if (keymap_config.swap_grave_esc) {
100 return keycode_to_action(KC_GRAVE);
101 }
102 return keycode_to_action(KC_ESC);
103 case KC_BSLASH:
104 if (keymap_config.swap_backslash_backspace) {
105 return keycode_to_action(KC_BSPACE);
106 }
107 return keycode_to_action(KC_BSLASH);
108 case KC_BSPACE:
109 if (keymap_config.swap_backslash_backspace) {
110 return keycode_to_action(KC_BSLASH);
111 }
112 return keycode_to_action(KC_BSPACE);
113 default:
114 return keycode_to_action(keycode);
115 }
116 }
117
118
119 /* Macro */
120 __attribute__ ((weak))
121 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
122 {
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 }
131
132 /* translates keycode to action */
133 static action_t keycode_to_action(uint16_t keycode)
134 {
135 action_t action;
136 switch (keycode) {
137 case KC_A ... KC_EXSEL:
138 case KC_LCTRL ... KC_RGUI:
139 action.code = ACTION_KEY(keycode);
140 break;
141 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
142 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
143 break;
144 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
145 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
146 break;
147 case KC_MS_UP ... KC_MS_ACCEL2:
148 action.code = ACTION_MOUSEKEY(keycode);
149 break;
150 case KC_TRNS:
151 action.code = ACTION_TRANSPARENT;
152 break;
153 case 0x0100 ... 0x1FFF: ;
154 // Has a modifier
155 // Split it up
156 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
157 break;
158 case 0x2000 ... 0x2FFF:
159 // Is a shortcut for function layer, pull last 12bits
160 // This means we have 4,096 FN macros at our disposal
161 return keymap_func_to_action(keycode & 0xFFF);
162 break;
163 case 0x3000 ... 0x3FFF: ;
164 // When the code starts with 3, it's an action macro.
165 action.code = ACTION_MACRO(keycode & 0xFF);
166 break;
167 #ifdef BACKLIGHT_ENABLE
168 case BL_0 ... BL_15:
169 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
170 break;
171 case BL_DEC:
172 action.code = ACTION_BACKLIGHT_DECREASE();
173 break;
174 case BL_INC:
175 action.code = ACTION_BACKLIGHT_INCREASE();
176 break;
177 case BL_TOGG:
178 action.code = ACTION_BACKLIGHT_TOGGLE();
179 break;
180 case BL_STEP:
181 action.code = ACTION_BACKLIGHT_STEP();
182 break;
183 #endif
184 case RESET: ; // RESET is 0x5000, which is why this is here
185 clear_keyboard();
186 #ifdef AUDIO_ENABLE
187 play_goodbye_tone();
188 #endif
189 _delay_ms(250);
190 #ifdef ATREUS_ASTAR
191 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
192 #endif
193 bootloader_jump();
194 break;
195 case DEBUG: ; // DEBUG is 0x5001
196 print("\nDEBUG: enabled.\n");
197 debug_enable = true;
198 break;
199 case 0x5002 ... 0x50FF:
200 // MAGIC actions (BOOTMAGIC without the boot)
201 if (!eeconfig_is_enabled()) {
202 eeconfig_init();
203 }
204 /* keymap config */
205 keymap_config.raw = eeconfig_read_keymap();
206 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
207 keymap_config.swap_control_capslock = 1;
208 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
209 keymap_config.capslock_to_control = 1;
210 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
211 keymap_config.swap_lalt_lgui = 1;
212 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
213 keymap_config.swap_ralt_rgui = 1;
214 } else if (keycode == MAGIC_NO_GUI) {
215 keymap_config.no_gui = 1;
216 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
217 keymap_config.swap_grave_esc = 1;
218 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
219 keymap_config.swap_backslash_backspace = 1;
220 } else if (keycode == MAGIC_HOST_NKRO) {
221 keymap_config.nkro = 1;
222 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
223 keymap_config.swap_lalt_lgui = 1;
224 keymap_config.swap_ralt_rgui = 1;
225 }
226 /* UNs */
227 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
228 keymap_config.swap_control_capslock = 0;
229 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
230 keymap_config.capslock_to_control = 0;
231 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
232 keymap_config.swap_lalt_lgui = 0;
233 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
234 keymap_config.swap_ralt_rgui = 0;
235 } else if (keycode == MAGIC_UNNO_GUI) {
236 keymap_config.no_gui = 0;
237 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
238 keymap_config.swap_grave_esc = 0;
239 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
240 keymap_config.swap_backslash_backspace = 0;
241 } else if (keycode == MAGIC_UNHOST_NKRO) {
242 keymap_config.nkro = 0;
243 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
244 keymap_config.swap_lalt_lgui = 0;
245 keymap_config.swap_ralt_rgui = 0;
246 }
247 eeconfig_write_keymap(keymap_config.raw);
248 break;
249 case 0x5100 ... 0x5FFF: ;
250 // Layer movement shortcuts
251 // See .h to see constraints/usage
252 int type = (keycode >> 0x8) & 0xF;
253 if (type == 0x1) {
254 // Layer set "GOTO"
255 int when = (keycode >> 0x4) & 0x3;
256 int layer = keycode & 0xF;
257 action.code = ACTION_LAYER_SET(layer, when);
258 } else if (type == 0x2) {
259 // Momentary layer
260 int layer = keycode & 0xFF;
261 action.code = ACTION_LAYER_MOMENTARY(layer);
262 } else if (type == 0x3) {
263 // Set default layer
264 int layer = keycode & 0xFF;
265 action.code = ACTION_DEFAULT_LAYER_SET(layer);
266 } else if (type == 0x4) {
267 // Set default layer
268 int layer = keycode & 0xFF;
269 action.code = ACTION_LAYER_TOGGLE(layer);
270 }
271 break;
272 #ifdef MIDI_ENABLE
273 case 0x6000 ... 0x6FFF:
274 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
275 break;
276 #endif
277 case 0x7000 ... 0x7FFF:
278 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
279 break;
280 case 0x8000 ... 0x8FFF:
281 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
282 break;
283 #ifdef UNICODE_ENABLE
284 case 0x8000000 ... 0x8FFFFFF:
285 uint16_t unicode = keycode & ~(0x8000);
286 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
287 break;
288 #endif
289 default:
290 action.code = ACTION_NO;
291 break;
292 }
293 return action;
294 }
295
296
297 /* translates key to keycode */
298 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
299 {
300 // Read entire word (16bits)
301 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
302 }
303
304 /* translates Fn keycode to action */
305 action_t keymap_fn_to_action(uint16_t keycode)
306 {
307 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
308 }
309
310 action_t keymap_func_to_action(uint16_t keycode)
311 {
312 // For FUNC without 8bit limit
313 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
314 }
315
316 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
317 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
318 layer_on(layer3);
319 } else {
320 layer_off(layer3);
321 }
322 }