]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/keymap_common.c
43debf4ef62869d608627d09a42d003b0a386543
[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                 stop_all_notes();
188                 play_goodbye_tone();
189             #endif
190             _delay_ms(250);
191             #ifdef ATREUS_ASTAR
192                 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
193             #endif
194             bootloader_jump();
195             break;
196         case DEBUG: ; // DEBUG is 0x5001
197             print("\nDEBUG: enabled.\n");
198             debug_enable = true;
199             break;
200         case 0x5002 ... 0x50FF:
201             // MAGIC actions (BOOTMAGIC without the boot)
202             if (!eeconfig_is_enabled()) {
203                 eeconfig_init();
204             }
205             /* keymap config */
206             keymap_config.raw = eeconfig_read_keymap();
207             if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
208                 keymap_config.swap_control_capslock = 1;
209             } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
210                 keymap_config.capslock_to_control = 1;
211             } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
212                 keymap_config.swap_lalt_lgui = 1;
213             } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
214                 keymap_config.swap_ralt_rgui = 1;
215             } else if (keycode == MAGIC_NO_GUI) {
216                 keymap_config.no_gui = 1;
217             } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
218                 keymap_config.swap_grave_esc = 1;
219             } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
220                 keymap_config.swap_backslash_backspace = 1;
221             } else if (keycode == MAGIC_HOST_NKRO) {
222                 keymap_config.nkro = 1;
223             } else if (keycode == MAGIC_SWAP_ALT_GUI) {
224                 keymap_config.swap_lalt_lgui = 1;
225                 keymap_config.swap_ralt_rgui = 1;
226             }
227             /* UNs */
228             else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
229                 keymap_config.swap_control_capslock = 0;
230             } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
231                 keymap_config.capslock_to_control = 0;
232             } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
233                 keymap_config.swap_lalt_lgui = 0;
234             } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
235                 keymap_config.swap_ralt_rgui = 0;
236             } else if (keycode == MAGIC_UNNO_GUI) {
237                 keymap_config.no_gui = 0;
238             } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
239                 keymap_config.swap_grave_esc = 0;
240             } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
241                 keymap_config.swap_backslash_backspace = 0;
242             } else if (keycode == MAGIC_UNHOST_NKRO) {
243                 keymap_config.nkro = 0;
244             } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
245                 keymap_config.swap_lalt_lgui = 0;
246                 keymap_config.swap_ralt_rgui = 0;
247             }
248             eeconfig_write_keymap(keymap_config.raw);
249             break;
250         case 0x5100 ... 0x5FFF: ;
251             // Layer movement shortcuts
252             // See .h to see constraints/usage
253             int type = (keycode >> 0x8) & 0xF;
254             if (type == 0x1) {
255                 // Layer set "GOTO"
256                 int when = (keycode >> 0x4) & 0x3;
257                 int layer = keycode & 0xF;
258                 action.code = ACTION_LAYER_SET(layer, when);
259             } else if (type == 0x2) {
260                 // Momentary layer
261                 int layer = keycode & 0xFF;
262                 action.code = ACTION_LAYER_MOMENTARY(layer);
263             } else if (type == 0x3) {
264                 // Set default layer
265                 int layer = keycode & 0xFF;
266                 action.code = ACTION_DEFAULT_LAYER_SET(layer);
267             } else if (type == 0x4) {
268                 // Set default layer
269                 int layer = keycode & 0xFF;
270                 action.code = ACTION_LAYER_TOGGLE(layer);
271             }
272             break;
273     #ifdef MIDI_ENABLE
274         case 0x6000 ... 0x6FFF:
275             action.code =  ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
276             break;
277     #endif
278         case 0x7000 ... 0x7FFF:
279             action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
280             break;
281         case 0x8000 ... 0x8FFF:
282             action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
283             break;
284     #ifdef UNICODE_ENABLE
285         case 0x8000000 ... 0x8FFFFFF:
286             uint16_t unicode = keycode & ~(0x8000);
287             action.code =  ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
288             break;
289     #endif
290         default:
291             action.code = ACTION_NO;
292             break;
293     }
294     return action;
295 }
296
297
298 /* translates key to keycode */
299 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
300 {
301     // Read entire word (16bits)
302     return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
303 }
304
305 /* translates Fn keycode to action */
306 action_t keymap_fn_to_action(uint16_t keycode)
307 {
308     return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
309 }
310
311 action_t keymap_func_to_action(uint16_t keycode)
312 {
313     // For FUNC without 8bit limit
314     return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
315 }
316
317 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
318   if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
319     layer_on(layer3);
320   } else {
321     layer_off(layer3);
322   }
323 }