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 "bootloader.h"
31 #include "keymap_midi.h"
35 extern keymap_config_t keymap_config;
41 #endif /* AUDIO_ENABLE */
43 static action_t keycode_to_action(uint16_t keycode);
45 /* converts key to action */
46 action_t action_for_key(uint8_t layer, keypos_t key)
48 // 16bit keycodes - important
49 uint16_t keycode = keymap_key_to_keycode(layer, key);
52 case KC_FN0 ... KC_FN31:
53 return keymap_fn_to_action(keycode);
56 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
57 return keycode_to_action(KC_LCTL);
59 return keycode_to_action(keycode);
61 if (keymap_config.swap_control_capslock) {
62 return keycode_to_action(KC_CAPSLOCK);
64 return keycode_to_action(KC_LCTL);
66 if (keymap_config.swap_lalt_lgui) {
67 if (keymap_config.no_gui) {
68 return keycode_to_action(ACTION_NO);
70 return keycode_to_action(KC_LGUI);
72 return keycode_to_action(KC_LALT);
74 if (keymap_config.swap_lalt_lgui) {
75 return keycode_to_action(KC_LALT);
77 if (keymap_config.no_gui) {
78 return keycode_to_action(ACTION_NO);
80 return keycode_to_action(KC_LGUI);
82 if (keymap_config.swap_ralt_rgui) {
83 if (keymap_config.no_gui) {
84 return keycode_to_action(ACTION_NO);
86 return keycode_to_action(KC_RGUI);
88 return keycode_to_action(KC_RALT);
90 if (keymap_config.swap_ralt_rgui) {
91 return keycode_to_action(KC_RALT);
93 if (keymap_config.no_gui) {
94 return keycode_to_action(ACTION_NO);
96 return keycode_to_action(KC_RGUI);
98 if (keymap_config.swap_grave_esc) {
99 return keycode_to_action(KC_ESC);
101 return keycode_to_action(KC_GRAVE);
103 if (keymap_config.swap_grave_esc) {
104 return keycode_to_action(KC_GRAVE);
106 return keycode_to_action(KC_ESC);
108 if (keymap_config.swap_backslash_backspace) {
109 return keycode_to_action(KC_BSPACE);
111 return keycode_to_action(KC_BSLASH);
113 if (keymap_config.swap_backslash_backspace) {
114 return keycode_to_action(KC_BSLASH);
116 return keycode_to_action(KC_BSPACE);
118 return keycode_to_action(keycode);
124 __attribute__ ((weak))
125 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
131 __attribute__ ((weak))
132 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
136 /* translates keycode to action */
137 static action_t keycode_to_action(uint16_t keycode)
141 case KC_A ... KC_EXSEL:
142 case KC_LCTRL ... KC_RGUI:
143 action.code = ACTION_KEY(keycode);
145 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
146 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
148 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
149 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
151 case KC_MS_UP ... KC_MS_ACCEL2:
152 action.code = ACTION_MOUSEKEY(keycode);
155 action.code = ACTION_TRANSPARENT;
157 case 0x0100 ... 0x1FFF: ;
160 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
162 case 0x2000 ... 0x2FFF:
163 // Is a shortcut for function layer, pull last 12bits
164 // This means we have 4,096 FN macros at our disposal
165 return keymap_func_to_action(keycode & 0xFFF);
167 case 0x3000 ... 0x3FFF: ;
168 // When the code starts with 3, it's an action macro.
169 action.code = ACTION_MACRO(keycode & 0xFF);
171 #ifdef BACKLIGHT_ENABLE
173 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
176 action.code = ACTION_BACKLIGHT_DECREASE();
179 action.code = ACTION_BACKLIGHT_INCREASE();
182 action.code = ACTION_BACKLIGHT_TOGGLE();
185 action.code = ACTION_BACKLIGHT_STEP();
188 case RESET: ; // RESET is 0x5000, which is why this is here
196 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
200 case DEBUG: ; // DEBUG is 0x5001
201 print("\nDEBUG: enabled.\n");
204 case 0x5002 ... 0x50FF:
205 // MAGIC actions (BOOTMAGIC without the boot)
206 if (!eeconfig_is_enabled()) {
210 keymap_config.raw = eeconfig_read_keymap();
211 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
212 keymap_config.swap_control_capslock = 1;
213 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
214 keymap_config.capslock_to_control = 1;
215 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
216 keymap_config.swap_lalt_lgui = 1;
217 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
218 keymap_config.swap_ralt_rgui = 1;
219 } else if (keycode == MAGIC_NO_GUI) {
220 keymap_config.no_gui = 1;
221 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
222 keymap_config.swap_grave_esc = 1;
223 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
224 keymap_config.swap_backslash_backspace = 1;
225 } else if (keycode == MAGIC_HOST_NKRO) {
226 keymap_config.nkro = 1;
227 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
228 keymap_config.swap_lalt_lgui = 1;
229 keymap_config.swap_ralt_rgui = 1;
232 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
233 keymap_config.swap_control_capslock = 0;
234 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
235 keymap_config.capslock_to_control = 0;
236 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
237 keymap_config.swap_lalt_lgui = 0;
238 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
239 keymap_config.swap_ralt_rgui = 0;
240 } else if (keycode == MAGIC_UNNO_GUI) {
241 keymap_config.no_gui = 0;
242 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
243 keymap_config.swap_grave_esc = 0;
244 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
245 keymap_config.swap_backslash_backspace = 0;
246 } else if (keycode == MAGIC_UNHOST_NKRO) {
247 keymap_config.nkro = 0;
248 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
249 keymap_config.swap_lalt_lgui = 0;
250 keymap_config.swap_ralt_rgui = 0;
252 eeconfig_update_keymap(keymap_config.raw);
254 case 0x5100 ... 0x56FF: ;
255 // Layer movement shortcuts
256 // See .h to see constraints/usage
257 int type = (keycode >> 0x8) & 0xF;
260 int when = (keycode >> 0x4) & 0x3;
261 int layer = keycode & 0xF;
262 action.code = ACTION_LAYER_SET(layer, when);
263 } else if (type == 0x2) {
265 int layer = keycode & 0xFF;
266 action.code = ACTION_LAYER_MOMENTARY(layer);
267 } else if (type == 0x3) {
269 int layer = keycode & 0xFF;
270 action.code = ACTION_DEFAULT_LAYER_SET(layer);
271 } else if (type == 0x4) {
273 int layer = keycode & 0xFF;
274 action.code = ACTION_LAYER_TOGGLE(layer);
275 } else if (type == 0x5) {
276 // OSL(layer) - One-shot layer
277 int layer = keycode & 0xFF;
278 action.code = ACTION_LAYER_ONESHOT(layer);
279 } else if (type == 0x6) {
280 // OSM(mod) - One-shot mod
281 int mod = keycode & 0xFF;
282 action.code = ACTION_MODS_ONESHOT(mod);
285 case 0x7000 ... 0x7FFF:
286 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
288 case 0x8000 ... 0x8FFF:
289 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
291 #ifdef UNICODE_ENABLE
292 case 0x8000000 ... 0x8FFFFFF:
293 uint16_t unicode = keycode & ~(0x8000);
294 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
298 action.code = ACTION_NO;
305 /* translates key to keycode */
306 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
308 // Read entire word (16bits)
309 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
312 /* translates Fn keycode to action */
313 action_t keymap_fn_to_action(uint16_t keycode)
315 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
318 action_t keymap_func_to_action(uint16_t keycode)
320 // For FUNC without 8bit limit
321 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
324 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
325 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {