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
;
39 #define TONE_GOODBYE OLKB_GOODBYE
40 #endif /*! TONE_GOODBYE */
42 float tone_goodbye
[][2] = SONG(TONE_GOODBYE
);
43 #endif /* AUDIO_ENABLE */
45 static action_t
keycode_to_action(uint16_t keycode
);
47 /* converts key to action */
48 action_t
action_for_key(uint8_t layer
, keypos_t key
)
50 // 16bit keycodes - important
51 uint16_t keycode
= keymap_key_to_keycode(layer
, key
);
54 case KC_FN0
... KC_FN31
:
55 return keymap_fn_to_action(keycode
);
58 if (keymap_config
.swap_control_capslock
|| keymap_config
.capslock_to_control
) {
59 return keycode_to_action(KC_LCTL
);
61 return keycode_to_action(keycode
);
63 if (keymap_config
.swap_control_capslock
) {
64 return keycode_to_action(KC_CAPSLOCK
);
66 return keycode_to_action(KC_LCTL
);
68 if (keymap_config
.swap_lalt_lgui
) {
69 if (keymap_config
.no_gui
) {
70 return keycode_to_action(ACTION_NO
);
72 return keycode_to_action(KC_LGUI
);
74 return keycode_to_action(KC_LALT
);
76 if (keymap_config
.swap_lalt_lgui
) {
77 return keycode_to_action(KC_LALT
);
79 if (keymap_config
.no_gui
) {
80 return keycode_to_action(ACTION_NO
);
82 return keycode_to_action(KC_LGUI
);
84 if (keymap_config
.swap_ralt_rgui
) {
85 if (keymap_config
.no_gui
) {
86 return keycode_to_action(ACTION_NO
);
88 return keycode_to_action(KC_RGUI
);
90 return keycode_to_action(KC_RALT
);
92 if (keymap_config
.swap_ralt_rgui
) {
93 return keycode_to_action(KC_RALT
);
95 if (keymap_config
.no_gui
) {
96 return keycode_to_action(ACTION_NO
);
98 return keycode_to_action(KC_RGUI
);
100 if (keymap_config
.swap_grave_esc
) {
101 return keycode_to_action(KC_ESC
);
103 return keycode_to_action(KC_GRAVE
);
105 if (keymap_config
.swap_grave_esc
) {
106 return keycode_to_action(KC_GRAVE
);
108 return keycode_to_action(KC_ESC
);
110 if (keymap_config
.swap_backslash_backspace
) {
111 return keycode_to_action(KC_BSPACE
);
113 return keycode_to_action(KC_BSLASH
);
115 if (keymap_config
.swap_backslash_backspace
) {
116 return keycode_to_action(KC_BSLASH
);
118 return keycode_to_action(KC_BSPACE
);
120 return keycode_to_action(keycode
);
126 __attribute__ ((weak
))
127 const macro_t
*action_get_macro(keyrecord_t
*record
, uint8_t id
, uint8_t opt
)
133 __attribute__ ((weak
))
134 void action_function(keyrecord_t
*record
, uint8_t id
, uint8_t opt
)
138 /* translates keycode to action */
139 static action_t
keycode_to_action(uint16_t keycode
)
143 case KC_A
... KC_EXSEL
:
144 case KC_LCTRL
... KC_RGUI
:
145 action
.code
= ACTION_KEY(keycode
);
147 case KC_SYSTEM_POWER
... KC_SYSTEM_WAKE
:
148 action
.code
= ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode
));
150 case KC_AUDIO_MUTE
... KC_WWW_FAVORITES
:
151 action
.code
= ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode
));
153 case KC_MS_UP
... KC_MS_ACCEL2
:
154 action
.code
= ACTION_MOUSEKEY(keycode
);
157 action
.code
= ACTION_TRANSPARENT
;
159 case 0x0100 ... 0x1FFF: ;
162 action
.code
= ACTION_MODS_KEY(keycode
>> 8, keycode
& 0xFF); // adds modifier to key
164 case 0x2000 ... 0x2FFF:
165 // Is a shortcut for function layer, pull last 12bits
166 // This means we have 4,096 FN macros at our disposal
167 return keymap_func_to_action(keycode
& 0xFFF);
169 case 0x3000 ... 0x3FFF: ;
170 // When the code starts with 3, it's an action macro.
171 action
.code
= ACTION_MACRO(keycode
& 0xFF);
173 #ifdef BACKLIGHT_ENABLE
175 action
.code
= ACTION_BACKLIGHT_LEVEL(keycode
& 0x000F);
178 action
.code
= ACTION_BACKLIGHT_DECREASE();
181 action
.code
= ACTION_BACKLIGHT_INCREASE();
184 action
.code
= ACTION_BACKLIGHT_TOGGLE();
187 action
.code
= ACTION_BACKLIGHT_STEP();
190 case RESET
: ; // RESET is 0x5000, which is why this is here
193 PLAY_NOTE_ARRAY(tone_goodbye
, false, 0);
197 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
201 case DEBUG
: ; // DEBUG is 0x5001
202 print("\nDEBUG: enabled.\n");
205 case 0x5002 ... 0x50FF:
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_write_keymap(keymap_config
.raw
);
255 case 0x5100 ... 0x5FFF: ;
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
);
279 case 0x6000 ... 0x6FFF:
280 action
.code
= ACTION_FUNCTION_OPT(keycode
& 0xFF, (keycode
& 0x0F00) >> 8);
283 case 0x7000 ... 0x7FFF:
284 action
.code
= ACTION_MODS_TAP_KEY((keycode
>> 0x8) & 0xF, keycode
& 0xFF);
286 case 0x8000 ... 0x8FFF:
287 action
.code
= ACTION_LAYER_TAP_KEY((keycode
>> 0x8) & 0xF, keycode
& 0xFF);
289 #ifdef UNICODE_ENABLE
290 case 0x8000000 ... 0x8FFFFFF:
291 uint16_t unicode
= keycode
& ~(0x8000);
292 action
.code
= ACTION_FUNCTION_OPT(unicode
& 0xFF, (unicode
& 0xFF00) >> 8);
296 action
.code
= ACTION_NO
;
303 /* translates key to keycode */
304 uint16_t keymap_key_to_keycode(uint8_t layer
, keypos_t key
)
306 // Read entire word (16bits)
307 return pgm_read_word(&keymaps
[(layer
)][(key
.row
)][(key
.col
)]);
310 /* translates Fn keycode to action */
311 action_t
keymap_fn_to_action(uint16_t keycode
)
313 return (action_t
){ .code
= pgm_read_word(&fn_actions
[FN_INDEX(keycode
)]) };
316 action_t
keymap_func_to_action(uint16_t keycode
)
318 // For FUNC without 8bit limit
319 return (action_t
){ .code
= pgm_read_word(&fn_actions
[(int)keycode
]) };
322 void update_tri_layer(uint8_t layer1
, uint8_t layer2
, uint8_t layer3
) {
323 if (IS_LAYER_ON(layer1
) && IS_LAYER_ON(layer2
)) {