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