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