]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/keymap_common.c
Merge branch 'master' of https://github.com/sboesebeck/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 "keymap_midi.h"
28
29 static action_t keycode_to_action(uint16_t keycode);
30
31 /* converts key to action */
32 action_t action_for_key(uint8_t layer, keypos_t key)
33 {
34         // 16bit keycodes - important
35     uint16_t keycode = keymap_key_to_keycode(layer, key);
36
37     if (keycode >= 0x0100 && keycode < 0x2000) {
38         // Has a modifier
39         action_t action;
40         // Split it up
41         action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
42         return action;
43         } else if (keycode >= 0x2000 && keycode < 0x3000) {
44         // Is a shortcut for function layer, pull last 12bits
45         // This means we have 4,096 FN macros at our disposal
46         return keymap_func_to_action(keycode & 0xFFF);
47         } else if (keycode >= 0x3000 && keycode < 0x4000) {
48       // When the code starts with 3, it's an action macro.
49         action_t action;
50         action.code = ACTION_MACRO(keycode & 0xFF);
51         return action;
52 #ifdef BACKLIGHT_ENABLE
53         } else if (keycode >= BL_0 & keycode <= BL_15) {
54         action_t action;
55         action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
56         return action;
57     } else if (keycode == BL_DEC) {
58         action_t action;
59         action.code = ACTION_BACKLIGHT_DECREASE();
60         return action;
61     } else if (keycode == BL_INC) {
62         action_t action;
63         action.code = ACTION_BACKLIGHT_INCREASE();
64         return action;
65     } else if (keycode == BL_TOGG) {
66         action_t action;
67         action.code = ACTION_BACKLIGHT_TOGGLE();
68         return action;
69     } else if (keycode == BL_STEP) {
70         action_t action;
71         action.code = ACTION_BACKLIGHT_STEP();
72         return action;
73 #endif
74     } else if (keycode == RESET) { // RESET is 0x5000, which is why this is here
75         clear_keyboard();
76         _delay_ms(250);
77         bootloader_jump();
78         return;
79     } else if (keycode == DEBUG) { // DEBUG is 0x5001
80       // TODO: Does this actually work?
81         print("\nDEBUG: enabled.\n");
82         debug_enable = true;
83         return;
84     } else if (keycode >= 0x5000 && keycode < 0x6000) {
85         // Layer movement shortcuts
86         // See .h to see constraints/usage
87         int type = (keycode >> 0x8) & 0xF;
88         if (type == 0x1) {
89             // Layer set "GOTO"
90             int when = (keycode >> 0x4) & 0x3;
91             int layer = keycode & 0xF;
92             action_t action;
93             action.code = ACTION_LAYER_SET(layer, when);
94             return action;
95         } else if (type == 0x2) {
96             // Momentary layer
97             int layer = keycode & 0xFF;
98             action_t action;
99             action.code = ACTION_LAYER_MOMENTARY(layer);
100             return action;
101         } else if (type == 0x3) {
102             // Set default layer
103             int layer = keycode & 0xFF;
104             action_t action;
105             action.code = ACTION_DEFAULT_LAYER_SET(layer);
106             return action;
107         } else if (type == 0x4) {
108             // Set default layer
109             int layer = keycode & 0xFF;
110             action_t action;
111             action.code = ACTION_LAYER_TOGGLE(layer);
112             return action;
113         }
114 #ifdef MIDI_ENABLE
115     } else if (keycode >= 0x6000 && keycode < 0x7000) {
116         action_t action;
117         action.code =  ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
118         return action;
119 #endif
120     } else if (keycode >= 0x7000 && keycode < 0x8000) {
121         action_t action;
122         action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
123         return action;
124     } else if (keycode >= 0x8000 && keycode < 0x9000) {
125         action_t action;
126         action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
127         return action;
128 #ifdef UNICODE_ENABLE
129     } else if (keycode >= 0x8000000) {
130         action_t action;
131         uint16_t unicode = keycode & ~(0x8000);
132         action.code =  ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
133         return action;
134 #endif
135     } else {
136
137     }
138
139     switch (keycode) {
140         case KC_FN0 ... KC_FN31:
141             return keymap_fn_to_action(keycode);
142 #ifdef BOOTMAGIC_ENABLE
143         case KC_CAPSLOCK:
144         case KC_LOCKING_CAPS:
145             if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
146                 return keycode_to_action(KC_LCTL);
147             }
148             return keycode_to_action(keycode);
149         case KC_LCTL:
150             if (keymap_config.swap_control_capslock) {
151                 return keycode_to_action(KC_CAPSLOCK);
152             }
153             return keycode_to_action(KC_LCTL);
154         case KC_LALT:
155             if (keymap_config.swap_lalt_lgui) {
156                 if (keymap_config.no_gui) {
157                     return keycode_to_action(ACTION_NO);
158                 }
159                 return keycode_to_action(KC_LGUI);
160             }
161             return keycode_to_action(KC_LALT);
162         case KC_LGUI:
163             if (keymap_config.swap_lalt_lgui) {
164                 return keycode_to_action(KC_LALT);
165             }
166             if (keymap_config.no_gui) {
167                 return keycode_to_action(ACTION_NO);
168             }
169             return keycode_to_action(KC_LGUI);
170         case KC_RALT:
171             if (keymap_config.swap_ralt_rgui) {
172                 if (keymap_config.no_gui) {
173                     return keycode_to_action(ACTION_NO);
174                 }
175                 return keycode_to_action(KC_RGUI);
176             }
177             return keycode_to_action(KC_RALT);
178         case KC_RGUI:
179             if (keymap_config.swap_ralt_rgui) {
180                 return keycode_to_action(KC_RALT);
181             }
182             if (keymap_config.no_gui) {
183                 return keycode_to_action(ACTION_NO);
184             }
185             return keycode_to_action(KC_RGUI);
186         case KC_GRAVE:
187             if (keymap_config.swap_grave_esc) {
188                 return keycode_to_action(KC_ESC);
189             }
190             return keycode_to_action(KC_GRAVE);
191         case KC_ESC:
192             if (keymap_config.swap_grave_esc) {
193                 return keycode_to_action(KC_GRAVE);
194             }
195             return keycode_to_action(KC_ESC);
196         case KC_BSLASH:
197             if (keymap_config.swap_backslash_backspace) {
198                 return keycode_to_action(KC_BSPACE);
199             }
200             return keycode_to_action(KC_BSLASH);
201         case KC_BSPACE:
202             if (keymap_config.swap_backslash_backspace) {
203                 return keycode_to_action(KC_BSLASH);
204             }
205             return keycode_to_action(KC_BSPACE);
206 #endif
207         default:
208             return keycode_to_action(keycode);
209     }
210 }
211
212
213 /* Macro */
214 __attribute__ ((weak))
215 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
216 {
217     return MACRO_NONE;
218 }
219
220 /* Function */
221 __attribute__ ((weak))
222 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
223 {
224 }
225
226 /* translates keycode to action */
227 static action_t keycode_to_action(uint16_t keycode)
228 {
229     action_t action;
230     switch (keycode) {
231         case KC_A ... KC_EXSEL:
232         case KC_LCTRL ... KC_RGUI:
233             action.code = ACTION_KEY(keycode);
234             break;
235         case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
236             action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
237             break;
238         case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
239             action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
240             break;
241         case KC_MS_UP ... KC_MS_ACCEL2:
242             action.code = ACTION_MOUSEKEY(keycode);
243             break;
244         case KC_TRNS:
245             action.code = ACTION_TRANSPARENT;
246             break;
247         default:
248             action.code = ACTION_NO;
249             break;
250     }
251     return action;
252 }
253
254
255 /* translates key to keycode */
256 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
257 {
258         // Read entire word (16bits)
259     return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
260 }
261
262 /* translates Fn keycode to action */
263 action_t keymap_fn_to_action(uint16_t keycode)
264 {
265     return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
266 }
267
268 action_t keymap_func_to_action(uint16_t keycode)
269 {
270         // For FUNC without 8bit limit
271     return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
272 }