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"
34 extern keymap_config_t keymap_config;
40 #endif /* AUDIO_ENABLE */
42 static action_t keycode_to_action(uint16_t keycode);
44 /* converts key to action */
45 action_t action_for_key(uint8_t layer, keypos_t key)
47 // 16bit keycodes - important
48 uint16_t keycode = keymap_key_to_keycode(layer, key);
51 case KC_FN0 ... KC_FN31:
52 return keymap_fn_to_action(keycode);
55 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
56 return keycode_to_action(KC_LCTL);
58 return keycode_to_action(keycode);
60 if (keymap_config.swap_control_capslock) {
61 return keycode_to_action(KC_CAPSLOCK);
63 return keycode_to_action(KC_LCTL);
65 if (keymap_config.swap_lalt_lgui) {
66 if (keymap_config.no_gui) {
67 return keycode_to_action(ACTION_NO);
69 return keycode_to_action(KC_LGUI);
71 return keycode_to_action(KC_LALT);
73 if (keymap_config.swap_lalt_lgui) {
74 return keycode_to_action(KC_LALT);
76 if (keymap_config.no_gui) {
77 return keycode_to_action(ACTION_NO);
79 return keycode_to_action(KC_LGUI);
81 if (keymap_config.swap_ralt_rgui) {
82 if (keymap_config.no_gui) {
83 return keycode_to_action(ACTION_NO);
85 return keycode_to_action(KC_RGUI);
87 return keycode_to_action(KC_RALT);
89 if (keymap_config.swap_ralt_rgui) {
90 return keycode_to_action(KC_RALT);
92 if (keymap_config.no_gui) {
93 return keycode_to_action(ACTION_NO);
95 return keycode_to_action(KC_RGUI);
97 if (keymap_config.swap_grave_esc) {
98 return keycode_to_action(KC_ESC);
100 return keycode_to_action(KC_GRAVE);
102 if (keymap_config.swap_grave_esc) {
103 return keycode_to_action(KC_GRAVE);
105 return keycode_to_action(KC_ESC);
107 if (keymap_config.swap_backslash_backspace) {
108 return keycode_to_action(KC_BSPACE);
110 return keycode_to_action(KC_BSLASH);
112 if (keymap_config.swap_backslash_backspace) {
113 return keycode_to_action(KC_BSLASH);
115 return keycode_to_action(KC_BSPACE);
117 return keycode_to_action(keycode);
123 __attribute__ ((weak))
124 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
130 __attribute__ ((weak))
131 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
135 /* translates keycode to action */
136 static action_t keycode_to_action(uint16_t keycode)
140 case KC_A ... KC_EXSEL:
141 case KC_LCTRL ... KC_RGUI:
142 action.code = ACTION_KEY(keycode);
144 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
145 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
147 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
148 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
150 case KC_MS_UP ... KC_MS_ACCEL2:
151 action.code = ACTION_MOUSEKEY(keycode);
154 action.code = ACTION_TRANSPARENT;
156 case LCTL(0) ... 0x1FFF: ;
159 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
161 case FUNC(0) ... FUNC(0xFFF): ;
162 // Is a shortcut for function layer, pull last 12bits
163 // This means we have 4,096 FN macros at our disposal
164 return keymap_func_to_action(keycode & 0xFFF);
166 case M(0) ... M(0xFF):
167 action.code = ACTION_MACRO(keycode & 0xFF);
169 case LT(0, 0) ... LT(0xFF, 0xF):
170 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
172 #ifdef BACKLIGHT_ENABLE
174 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
177 action.code = ACTION_BACKLIGHT_DECREASE();
180 action.code = ACTION_BACKLIGHT_INCREASE();
183 action.code = ACTION_BACKLIGHT_TOGGLE();
186 action.code = ACTION_BACKLIGHT_STEP();
189 case RESET: ; // RESET is 0x5000, which is why this is here
197 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
201 case DEBUG: ; // DEBUG is 0x5001
202 print("\nDEBUG: enabled.\n");
205 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
206 // MAGIC actions (BOOTMAGIC without the boot)
207 if (!eeconfig_is_enabled()) {
211 keymap_config.raw = eeconfig_read_keymap();
212 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
213 keymap_config.swap_control_capslock = 1;
214 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
215 keymap_config.capslock_to_control = 1;
216 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
217 keymap_config.swap_lalt_lgui = 1;
218 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
219 keymap_config.swap_ralt_rgui = 1;
220 } else if (keycode == MAGIC_NO_GUI) {
221 keymap_config.no_gui = 1;
222 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
223 keymap_config.swap_grave_esc = 1;
224 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
225 keymap_config.swap_backslash_backspace = 1;
226 } else if (keycode == MAGIC_HOST_NKRO) {
227 keymap_config.nkro = 1;
228 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
229 keymap_config.swap_lalt_lgui = 1;
230 keymap_config.swap_ralt_rgui = 1;
233 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
234 keymap_config.swap_control_capslock = 0;
235 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
236 keymap_config.capslock_to_control = 0;
237 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
238 keymap_config.swap_lalt_lgui = 0;
239 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
240 keymap_config.swap_ralt_rgui = 0;
241 } else if (keycode == MAGIC_UNNO_GUI) {
242 keymap_config.no_gui = 0;
243 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
244 keymap_config.swap_grave_esc = 0;
245 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
246 keymap_config.swap_backslash_backspace = 0;
247 } else if (keycode == MAGIC_UNHOST_NKRO) {
248 keymap_config.nkro = 0;
249 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
250 keymap_config.swap_lalt_lgui = 0;
251 keymap_config.swap_ralt_rgui = 0;
253 eeconfig_update_keymap(keymap_config.raw);
255 case TO(0, 1) ... OSM(0xFF): ;
256 // Layer movement shortcuts
257 // See .h to see constraints/usage
258 int type = (keycode >> 0x8) & 0xF;
261 int when = (keycode >> 0x4) & 0x3;
262 int layer = keycode & 0xF;
263 action.code = ACTION_LAYER_SET(layer, when);
264 } else if (type == 0x2) {
266 int layer = keycode & 0xFF;
267 action.code = ACTION_LAYER_MOMENTARY(layer);
268 } else if (type == 0x3) {
270 int layer = keycode & 0xFF;
271 action.code = ACTION_DEFAULT_LAYER_SET(layer);
272 } else if (type == 0x4) {
274 int layer = keycode & 0xFF;
275 action.code = ACTION_LAYER_TOGGLE(layer);
276 } else if (type == 0x5) {
277 // OSL(layer) - One-shot layer
278 int layer = keycode & 0xFF;
279 action.code = ACTION_LAYER_ONESHOT(layer);
280 } else if (type == 0x6) {
281 // OSM(mod) - One-shot mod
282 int mod = keycode & 0xFF;
283 action.code = ACTION_MODS_ONESHOT(mod);
286 case MT(0, 0) ... MT(0xF, 0xFF):
287 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
290 action.code = ACTION_NO;
297 /* translates key to keycode */
298 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
300 // Read entire word (16bits)
301 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
304 /* translates Fn keycode to action */
305 action_t keymap_fn_to_action(uint16_t keycode)
307 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
310 action_t keymap_func_to_action(uint16_t keycode)
312 // For FUNC without 8bit limit
313 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
316 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
317 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {