2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
18 #include "keymap_common.h"
21 #include "action_layer.h"
22 #include <util/delay.h>
24 #include "action_macro.h"
26 #include "backlight.h"
27 #include "keymap_midi.h"
28 #include "bootloader.h"
30 extern keymap_config_t keymap_config;
37 #define TONE_GOODBYE { \
38 {440.0*pow(2.0,(31)/12.0), 8}, \
39 {440.0*pow(2.0,(24)/12.0), 8}, \
40 {440.0*pow(2.0,(19)/12.0), 12}, \
43 float tone_goodbye[][2] = TONE_GOODBYE;
46 static action_t keycode_to_action(uint16_t keycode);
48 /* converts key to action */
49 action_t action_for_key(uint8_t layer, keypos_t key)
51 // 16bit keycodes - important
52 uint16_t keycode = keymap_key_to_keycode(layer, key);
55 case KC_FN0 ... KC_FN31:
56 return keymap_fn_to_action(keycode);
59 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
60 return keycode_to_action(KC_LCTL);
62 return keycode_to_action(keycode);
64 if (keymap_config.swap_control_capslock) {
65 return keycode_to_action(KC_CAPSLOCK);
67 return keycode_to_action(KC_LCTL);
69 if (keymap_config.swap_lalt_lgui) {
70 if (keymap_config.no_gui) {
71 return keycode_to_action(ACTION_NO);
73 return keycode_to_action(KC_LGUI);
75 return keycode_to_action(KC_LALT);
77 if (keymap_config.swap_lalt_lgui) {
78 return keycode_to_action(KC_LALT);
80 if (keymap_config.no_gui) {
81 return keycode_to_action(ACTION_NO);
83 return keycode_to_action(KC_LGUI);
85 if (keymap_config.swap_ralt_rgui) {
86 if (keymap_config.no_gui) {
87 return keycode_to_action(ACTION_NO);
89 return keycode_to_action(KC_RGUI);
91 return keycode_to_action(KC_RALT);
93 if (keymap_config.swap_ralt_rgui) {
94 return keycode_to_action(KC_RALT);
96 if (keymap_config.no_gui) {
97 return keycode_to_action(ACTION_NO);
99 return keycode_to_action(KC_RGUI);
101 if (keymap_config.swap_grave_esc) {
102 return keycode_to_action(KC_ESC);
104 return keycode_to_action(KC_GRAVE);
106 if (keymap_config.swap_grave_esc) {
107 return keycode_to_action(KC_GRAVE);
109 return keycode_to_action(KC_ESC);
111 if (keymap_config.swap_backslash_backspace) {
112 return keycode_to_action(KC_BSPACE);
114 return keycode_to_action(KC_BSLASH);
116 if (keymap_config.swap_backslash_backspace) {
117 return keycode_to_action(KC_BSLASH);
119 return keycode_to_action(KC_BSPACE);
121 return keycode_to_action(keycode);
127 __attribute__ ((weak))
128 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
134 __attribute__ ((weak))
135 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
139 /* translates keycode to action */
140 static action_t keycode_to_action(uint16_t keycode)
144 case KC_A ... KC_EXSEL:
145 case KC_LCTRL ... KC_RGUI:
146 action.code = ACTION_KEY(keycode);
148 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
149 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
151 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
152 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
154 case KC_MS_UP ... KC_MS_ACCEL2:
155 action.code = ACTION_MOUSEKEY(keycode);
158 action.code = ACTION_TRANSPARENT;
160 case 0x0100 ... 0x1FFF: ;
163 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
165 case 0x2000 ... 0x2FFF:
166 // Is a shortcut for function layer, pull last 12bits
167 // This means we have 4,096 FN macros at our disposal
168 return keymap_func_to_action(keycode & 0xFFF);
170 case 0x3000 ... 0x3FFF: ;
171 // When the code starts with 3, it's an action macro.
172 action.code = ACTION_MACRO(keycode & 0xFF);
174 #ifdef BACKLIGHT_ENABLE
176 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
179 action.code = ACTION_BACKLIGHT_DECREASE();
182 action.code = ACTION_BACKLIGHT_INCREASE();
185 action.code = ACTION_BACKLIGHT_TOGGLE();
188 action.code = ACTION_BACKLIGHT_STEP();
191 case RESET: ; // RESET is 0x5000, which is why this is here
194 PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
198 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
202 case DEBUG: ; // DEBUG is 0x5001
203 print("\nDEBUG: enabled.\n");
206 case 0x5002 ... 0x50FF:
207 // MAGIC actions (BOOTMAGIC without the boot)
208 if (!eeconfig_is_enabled()) {
212 keymap_config.raw = eeconfig_read_keymap();
213 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
214 keymap_config.swap_control_capslock = 1;
215 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
216 keymap_config.capslock_to_control = 1;
217 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
218 keymap_config.swap_lalt_lgui = 1;
219 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
220 keymap_config.swap_ralt_rgui = 1;
221 } else if (keycode == MAGIC_NO_GUI) {
222 keymap_config.no_gui = 1;
223 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
224 keymap_config.swap_grave_esc = 1;
225 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
226 keymap_config.swap_backslash_backspace = 1;
227 } else if (keycode == MAGIC_HOST_NKRO) {
228 keymap_config.nkro = 1;
229 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
230 keymap_config.swap_lalt_lgui = 1;
231 keymap_config.swap_ralt_rgui = 1;
234 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
235 keymap_config.swap_control_capslock = 0;
236 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
237 keymap_config.capslock_to_control = 0;
238 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
239 keymap_config.swap_lalt_lgui = 0;
240 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
241 keymap_config.swap_ralt_rgui = 0;
242 } else if (keycode == MAGIC_UNNO_GUI) {
243 keymap_config.no_gui = 0;
244 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
245 keymap_config.swap_grave_esc = 0;
246 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
247 keymap_config.swap_backslash_backspace = 0;
248 } else if (keycode == MAGIC_UNHOST_NKRO) {
249 keymap_config.nkro = 0;
250 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
251 keymap_config.swap_lalt_lgui = 0;
252 keymap_config.swap_ralt_rgui = 0;
254 eeconfig_write_keymap(keymap_config.raw);
256 case 0x5100 ... 0x5FFF: ;
257 // Layer movement shortcuts
258 // See .h to see constraints/usage
259 int type = (keycode >> 0x8) & 0xF;
262 int when = (keycode >> 0x4) & 0x3;
263 int layer = keycode & 0xF;
264 action.code = ACTION_LAYER_SET(layer, when);
265 } else if (type == 0x2) {
267 int layer = keycode & 0xFF;
268 action.code = ACTION_LAYER_MOMENTARY(layer);
269 } else if (type == 0x3) {
271 int layer = keycode & 0xFF;
272 action.code = ACTION_DEFAULT_LAYER_SET(layer);
273 } else if (type == 0x4) {
275 int layer = keycode & 0xFF;
276 action.code = ACTION_LAYER_TOGGLE(layer);
280 case 0x6000 ... 0x6FFF:
281 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
284 case 0x7000 ... 0x7FFF:
285 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
287 case 0x8000 ... 0x8FFF:
288 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
290 #ifdef UNICODE_ENABLE
291 case 0x8000000 ... 0x8FFFFFF:
292 uint16_t unicode = keycode & ~(0x8000);
293 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
297 action.code = ACTION_NO;
304 /* translates key to keycode */
305 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
307 // Read entire word (16bits)
308 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
311 /* translates Fn keycode to action */
312 action_t keymap_fn_to_action(uint16_t keycode)
314 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
317 action_t keymap_func_to_action(uint16_t keycode)
319 // For FUNC without 8bit limit
320 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
323 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
324 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {