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