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