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"
31 extern keymap_config_t keymap_config;
37 #endif /* AUDIO_ENABLE */
39 static action_t keycode_to_action(uint16_t keycode);
41 /* converts key to action */
42 action_t action_for_key(uint8_t layer, keypos_t key)
44 // 16bit keycodes - important
45 uint16_t keycode = keymap_key_to_keycode(layer, key);
48 case KC_FN0 ... KC_FN31:
49 return keymap_fn_to_action(keycode);
52 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
53 return keycode_to_action(KC_LCTL);
55 return keycode_to_action(keycode);
57 if (keymap_config.swap_control_capslock) {
58 return keycode_to_action(KC_CAPSLOCK);
60 return keycode_to_action(KC_LCTL);
62 if (keymap_config.swap_lalt_lgui) {
63 if (keymap_config.no_gui) {
64 return keycode_to_action(ACTION_NO);
66 return keycode_to_action(KC_LGUI);
68 return keycode_to_action(KC_LALT);
70 if (keymap_config.swap_lalt_lgui) {
71 return keycode_to_action(KC_LALT);
73 if (keymap_config.no_gui) {
74 return keycode_to_action(ACTION_NO);
76 return keycode_to_action(KC_LGUI);
78 if (keymap_config.swap_ralt_rgui) {
79 if (keymap_config.no_gui) {
80 return keycode_to_action(ACTION_NO);
82 return keycode_to_action(KC_RGUI);
84 return keycode_to_action(KC_RALT);
86 if (keymap_config.swap_ralt_rgui) {
87 return keycode_to_action(KC_RALT);
89 if (keymap_config.no_gui) {
90 return keycode_to_action(ACTION_NO);
92 return keycode_to_action(KC_RGUI);
94 if (keymap_config.swap_grave_esc) {
95 return keycode_to_action(KC_ESC);
97 return keycode_to_action(KC_GRAVE);
99 if (keymap_config.swap_grave_esc) {
100 return keycode_to_action(KC_GRAVE);
102 return keycode_to_action(KC_ESC);
104 if (keymap_config.swap_backslash_backspace) {
105 return keycode_to_action(KC_BSPACE);
107 return keycode_to_action(KC_BSLASH);
109 if (keymap_config.swap_backslash_backspace) {
110 return keycode_to_action(KC_BSLASH);
112 return keycode_to_action(KC_BSPACE);
114 return keycode_to_action(keycode);
120 __attribute__ ((weak))
121 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
127 __attribute__ ((weak))
128 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
132 /* translates keycode to action */
133 static action_t keycode_to_action(uint16_t keycode)
137 case KC_A ... KC_EXSEL:
138 case KC_LCTRL ... KC_RGUI:
139 action.code = ACTION_KEY(keycode);
141 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
142 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
144 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
145 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
147 case KC_MS_UP ... KC_MS_ACCEL2:
148 action.code = ACTION_MOUSEKEY(keycode);
151 action.code = ACTION_TRANSPARENT;
153 case 0x0100 ... 0x1FFF: ;
156 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
158 case 0x2000 ... 0x2FFF:
159 // Is a shortcut for function layer, pull last 12bits
160 // This means we have 4,096 FN macros at our disposal
161 return keymap_func_to_action(keycode & 0xFFF);
163 case 0x3000 ... 0x3FFF: ;
164 // When the code starts with 3, it's an action macro.
165 action.code = ACTION_MACRO(keycode & 0xFF);
167 #ifdef BACKLIGHT_ENABLE
169 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
172 action.code = ACTION_BACKLIGHT_DECREASE();
175 action.code = ACTION_BACKLIGHT_INCREASE();
178 action.code = ACTION_BACKLIGHT_TOGGLE();
181 action.code = ACTION_BACKLIGHT_STEP();
184 case RESET: ; // RESET is 0x5000, which is why this is here
192 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
196 case DEBUG: ; // DEBUG is 0x5001
197 print("\nDEBUG: enabled.\n");
200 case 0x5002 ... 0x50FF:
201 // MAGIC actions (BOOTMAGIC without the boot)
202 if (!eeconfig_is_enabled()) {
206 keymap_config.raw = eeconfig_read_keymap();
207 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
208 keymap_config.swap_control_capslock = 1;
209 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
210 keymap_config.capslock_to_control = 1;
211 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
212 keymap_config.swap_lalt_lgui = 1;
213 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
214 keymap_config.swap_ralt_rgui = 1;
215 } else if (keycode == MAGIC_NO_GUI) {
216 keymap_config.no_gui = 1;
217 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
218 keymap_config.swap_grave_esc = 1;
219 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
220 keymap_config.swap_backslash_backspace = 1;
221 } else if (keycode == MAGIC_HOST_NKRO) {
222 keymap_config.nkro = 1;
223 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
224 keymap_config.swap_lalt_lgui = 1;
225 keymap_config.swap_ralt_rgui = 1;
228 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
229 keymap_config.swap_control_capslock = 0;
230 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
231 keymap_config.capslock_to_control = 0;
232 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
233 keymap_config.swap_lalt_lgui = 0;
234 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
235 keymap_config.swap_ralt_rgui = 0;
236 } else if (keycode == MAGIC_UNNO_GUI) {
237 keymap_config.no_gui = 0;
238 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
239 keymap_config.swap_grave_esc = 0;
240 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
241 keymap_config.swap_backslash_backspace = 0;
242 } else if (keycode == MAGIC_UNHOST_NKRO) {
243 keymap_config.nkro = 0;
244 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
245 keymap_config.swap_lalt_lgui = 0;
246 keymap_config.swap_ralt_rgui = 0;
248 eeconfig_update_keymap(keymap_config.raw);
250 case 0x5100 ... 0x5FFF: ;
251 // Layer movement shortcuts
252 // See .h to see constraints/usage
253 int type = (keycode >> 0x8) & 0xF;
256 int when = (keycode >> 0x4) & 0x3;
257 int layer = keycode & 0xF;
258 action.code = ACTION_LAYER_SET(layer, when);
259 } else if (type == 0x2) {
261 int layer = keycode & 0xFF;
262 action.code = ACTION_LAYER_MOMENTARY(layer);
263 } else if (type == 0x3) {
265 int layer = keycode & 0xFF;
266 action.code = ACTION_DEFAULT_LAYER_SET(layer);
267 } else if (type == 0x4) {
269 int layer = keycode & 0xFF;
270 action.code = ACTION_LAYER_TOGGLE(layer);
274 case 0x6000 ... 0x6FFF:
275 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
278 case 0x7000 ... 0x7FFF:
279 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
281 case 0x8000 ... 0x8FFF:
282 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
284 #ifdef UNICODE_ENABLE
285 case 0x8000000 ... 0x8FFFFFF:
286 uint16_t unicode = keycode & ~(0x8000);
287 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
291 action.code = ACTION_NO;
298 /* translates key to keycode */
299 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
301 // Read entire word (16bits)
302 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
305 /* translates Fn keycode to action */
306 action_t keymap_fn_to_action(uint16_t keycode)
308 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
311 action_t keymap_func_to_action(uint16_t keycode)
313 // For FUNC without 8bit limit
314 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
317 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
318 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {