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