]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/keymap_common.c
Merge branch 'master' of https://github.com/jackhumbert/qmk_firmware
[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 #include "quantum.h"
30
31 #ifdef MIDI_ENABLE
32         #include "keymap_midi.h"
33 #endif
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 LCTL(0) ... 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 FUNC(0) ... FUNC(0xFFF): ;
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 M(0) ... M(0xFF):
168             action.code = ACTION_MACRO(keycode & 0xFF);
169             break;
170         case LT(0, 0) ... LT(0xFF, 0xF):
171             action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, 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                 stop_all_notes();
194                 shutdown_user();
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 MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
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_update_keymap(keymap_config.raw);
255             break;
256         case TO(0, 1) ... OSM(0xFF): ;
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             } else if (type == 0x5) {
278                 // OSL(layer) - One-shot layer
279                 int layer = keycode & 0xFF;
280                 action.code = ACTION_LAYER_ONESHOT(layer);
281             } else if (type == 0x6) {
282                 // OSM(mod) - One-shot mod
283                 int mod = keycode & 0xFF;
284                 action.code = ACTION_MODS_ONESHOT(mod);
285             }
286             break;
287         case MT(0, 0) ... MT(0xF, 0xFF):
288             action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
289             break;
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 }