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