]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/keymap_common.c
61a51aedb43e46cafaddb030870b7359a0add532
[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
37     float goodbye[][2] = {
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
44 static action_t keycode_to_action(uint16_t keycode);
45
46 /* converts key to action */
47 action_t action_for_key(uint8_t layer, keypos_t key)
48 {
49         // 16bit keycodes - important
50     uint16_t keycode = keymap_key_to_keycode(layer, key);
51
52     switch (keycode) {
53         case KC_FN0 ... KC_FN31:
54             return keymap_fn_to_action(keycode);
55         case KC_CAPSLOCK:
56         case KC_LOCKING_CAPS:
57             if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
58                 return keycode_to_action(KC_LCTL);
59             }
60             return keycode_to_action(keycode);
61         case KC_LCTL:
62             if (keymap_config.swap_control_capslock) {
63                 return keycode_to_action(KC_CAPSLOCK);
64             }
65             return keycode_to_action(KC_LCTL);
66         case KC_LALT:
67             if (keymap_config.swap_lalt_lgui) {
68                 if (keymap_config.no_gui) {
69                     return keycode_to_action(ACTION_NO);
70                 }
71                 return keycode_to_action(KC_LGUI);
72             }
73             return keycode_to_action(KC_LALT);
74         case KC_LGUI:
75             if (keymap_config.swap_lalt_lgui) {
76                 return keycode_to_action(KC_LALT);
77             }
78             if (keymap_config.no_gui) {
79                 return keycode_to_action(ACTION_NO);
80             }
81             return keycode_to_action(KC_LGUI);
82         case KC_RALT:
83             if (keymap_config.swap_ralt_rgui) {
84                 if (keymap_config.no_gui) {
85                     return keycode_to_action(ACTION_NO);
86                 }
87                 return keycode_to_action(KC_RGUI);
88             }
89             return keycode_to_action(KC_RALT);
90         case KC_RGUI:
91             if (keymap_config.swap_ralt_rgui) {
92                 return keycode_to_action(KC_RALT);
93             }
94             if (keymap_config.no_gui) {
95                 return keycode_to_action(ACTION_NO);
96             }
97             return keycode_to_action(KC_RGUI);
98         case KC_GRAVE:
99             if (keymap_config.swap_grave_esc) {
100                 return keycode_to_action(KC_ESC);
101             }
102             return keycode_to_action(KC_GRAVE);
103         case KC_ESC:
104             if (keymap_config.swap_grave_esc) {
105                 return keycode_to_action(KC_GRAVE);
106             }
107             return keycode_to_action(KC_ESC);
108         case KC_BSLASH:
109             if (keymap_config.swap_backslash_backspace) {
110                 return keycode_to_action(KC_BSPACE);
111             }
112             return keycode_to_action(KC_BSLASH);
113         case KC_BSPACE:
114             if (keymap_config.swap_backslash_backspace) {
115                 return keycode_to_action(KC_BSLASH);
116             }
117             return keycode_to_action(KC_BSPACE);
118         default:
119             return keycode_to_action(keycode);
120     }
121 }
122
123
124 /* Macro */
125 __attribute__ ((weak))
126 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
127 {
128     return MACRO_NONE;
129 }
130
131 /* Function */
132 __attribute__ ((weak))
133 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
134 {
135 }
136
137 /* translates keycode to action */
138 static action_t keycode_to_action(uint16_t keycode)
139 {
140     action_t action;
141     switch (keycode) {
142         case KC_A ... KC_EXSEL:
143         case KC_LCTRL ... KC_RGUI:
144             action.code = ACTION_KEY(keycode);
145             break;
146         case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
147             action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
148             break;
149         case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
150             action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
151             break;
152         case KC_MS_UP ... KC_MS_ACCEL2:
153             action.code = ACTION_MOUSEKEY(keycode);
154             break;
155         case KC_TRNS:
156             action.code = ACTION_TRANSPARENT;
157             break;
158         case 0x0100 ... 0x1FFF: ;
159             // Has a modifier
160             // Split it up
161             action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
162             break;
163         case 0x2000 ... 0x2FFF:
164             // Is a shortcut for function layer, pull last 12bits
165             // This means we have 4,096 FN macros at our disposal
166             return keymap_func_to_action(keycode & 0xFFF);
167             break;
168         case 0x3000 ... 0x3FFF: ;
169             // When the code starts with 3, it's an action macro.
170             action.code = ACTION_MACRO(keycode & 0xFF);
171             break;
172     #ifdef BACKLIGHT_ENABLE
173         case BL_0 ... BL_15:
174             action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
175             break;
176         case BL_DEC:
177             action.code = ACTION_BACKLIGHT_DECREASE();
178             break;
179         case BL_INC:
180             action.code = ACTION_BACKLIGHT_INCREASE();
181             break;
182         case BL_TOGG:
183             action.code = ACTION_BACKLIGHT_TOGGLE();
184             break;
185         case BL_STEP:
186             action.code = ACTION_BACKLIGHT_STEP();
187             break;
188     #endif
189         case RESET: ; // RESET is 0x5000, which is why this is here
190             clear_keyboard();
191             #ifdef AUDIO_ENABLE
192                 PLAY_NOTE_ARRAY(goodbye, false, 0);
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_write_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             }
276             break;
277     #ifdef MIDI_ENABLE
278         case 0x6000 ... 0x6FFF:
279             action.code =  ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
280             break;
281     #endif
282         case 0x7000 ... 0x7FFF:
283             action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
284             break;
285         case 0x8000 ... 0x8FFF:
286             action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
287             break;
288     #ifdef UNICODE_ENABLE
289         case 0x8000000 ... 0x8FFFFFF:
290             uint16_t unicode = keycode & ~(0x8000);
291             action.code =  ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
292             break;
293     #endif
294         default:
295             action.code = ACTION_NO;
296             break;
297     }
298     return action;
299 }
300
301
302 /* translates key to keycode */
303 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
304 {
305         // Read entire word (16bits)
306     return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
307 }
308
309 /* translates Fn keycode to action */
310 action_t keymap_fn_to_action(uint16_t keycode)
311 {
312     return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
313 }
314
315 action_t keymap_func_to_action(uint16_t keycode)
316 {
317         // For FUNC without 8bit limit
318     return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
319 }
320
321 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
322   if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
323     layer_on(layer3);
324   } else {
325     layer_off(layer3);
326   }
327 }