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