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"
23 #include "action_macro.h"
25 #include "backlight.h"
26 #include "keymap_midi.h"
28 static action_t
keycode_to_action(uint16_t keycode
);
30 /* converts key to action */
31 action_t
action_for_key(uint8_t layer
, keypos_t key
)
33 // 16bit keycodes - important
34 uint16_t keycode
= keymap_key_to_keycode(layer
, key
);
36 if (keycode
>= 0x0100 && keycode
< 0x2000) {
40 action
.code
= ACTION_MODS_KEY(keycode
>> 8, keycode
& 0xFF); // adds modifier to key
42 } else if (keycode
>= 0x2000 && keycode
< 0x3000) {
43 // Is a shortcut for function layer, pull last 12bits
44 // This means we have 4,096 FN macros at our disposal
45 return keymap_func_to_action(keycode
& 0xFFF);
46 } else if (keycode
>= 0x3000 && keycode
< 0x4000) {
47 // When the code starts with 3, it's an action macro.
49 action
.code
= ACTION_MACRO(keycode
& 0xFF);
51 #ifdef BACKLIGHT_ENABLE
52 } else if (keycode
>= BL_0
& keycode
<= BL_15
) {
54 action
.code
= ACTION_BACKLIGHT_LEVEL(keycode
& 0x000F);
56 } else if (keycode
== BL_DEC
) {
58 action
.code
= ACTION_BACKLIGHT_DECREASE();
60 } else if (keycode
== BL_INC
) {
62 action
.code
= ACTION_BACKLIGHT_INCREASE();
64 } else if (keycode
== BL_TOGG
) {
66 action
.code
= ACTION_BACKLIGHT_TOGGLE();
68 } else if (keycode
== BL_STEP
) {
70 action
.code
= ACTION_BACKLIGHT_STEP();
73 } else if (keycode
== RESET
) { // RESET is 0x5000, which is why this is here
76 } else if (keycode
== DEBUG
) { // DEBUG is 0x5001
77 // TODO: Does this actually work?
78 print("\nDEBUG: enabled.\n");
81 } else if (keycode
>= 0x5000 && keycode
< 0x6000) {
82 // Layer movement shortcuts
83 // See .h to see constraints/usage
84 int type
= (keycode
>> 0x8) & 0xF;
87 int when
= (keycode
>> 0x4) & 0x3;
88 int layer
= keycode
& 0xF;
90 action
.code
= ACTION_LAYER_SET(layer
, when
);
92 } else if (type
== 0x2) {
94 int layer
= keycode
& 0xFF;
96 action
.code
= ACTION_LAYER_MOMENTARY(layer
);
98 } else if (type
== 0x3) {
100 int layer
= keycode
& 0xFF;
102 action
.code
= ACTION_DEFAULT_LAYER_SET(layer
);
106 } else if (keycode
>= 0x6000 && keycode
< 0x7000) {
108 action
.code
= ACTION_FUNCTION_OPT(keycode
& 0xFF, (keycode
& 0x0F00) >> 8);
111 #ifdef UNICODE_ENABLE
112 } else if (keycode
>= 0x8000) {
114 uint16_t unicode
= keycode
& ~(0x8000);
115 action
.code
= ACTION_FUNCTION_OPT(unicode
& 0xFF, (unicode
& 0xFF00) >> 8);
123 case KC_FN0
... KC_FN31
:
124 return keymap_fn_to_action(keycode
);
125 #ifdef BOOTMAGIC_ENABLE
127 case KC_LOCKING_CAPS
:
128 if (keymap_config
.swap_control_capslock
|| keymap_config
.capslock_to_control
) {
129 return keycode_to_action(KC_LCTL
);
131 return keycode_to_action(keycode
);
133 if (keymap_config
.swap_control_capslock
) {
134 return keycode_to_action(KC_CAPSLOCK
);
136 return keycode_to_action(KC_LCTL
);
138 if (keymap_config
.swap_lalt_lgui
) {
139 if (keymap_config
.no_gui
) {
140 return keycode_to_action(ACTION_NO
);
142 return keycode_to_action(KC_LGUI
);
144 return keycode_to_action(KC_LALT
);
146 if (keymap_config
.swap_lalt_lgui
) {
147 return keycode_to_action(KC_LALT
);
149 if (keymap_config
.no_gui
) {
150 return keycode_to_action(ACTION_NO
);
152 return keycode_to_action(KC_LGUI
);
154 if (keymap_config
.swap_ralt_rgui
) {
155 if (keymap_config
.no_gui
) {
156 return keycode_to_action(ACTION_NO
);
158 return keycode_to_action(KC_RGUI
);
160 return keycode_to_action(KC_RALT
);
162 if (keymap_config
.swap_ralt_rgui
) {
163 return keycode_to_action(KC_RALT
);
165 if (keymap_config
.no_gui
) {
166 return keycode_to_action(ACTION_NO
);
168 return keycode_to_action(KC_RGUI
);
170 if (keymap_config
.swap_grave_esc
) {
171 return keycode_to_action(KC_ESC
);
173 return keycode_to_action(KC_GRAVE
);
175 if (keymap_config
.swap_grave_esc
) {
176 return keycode_to_action(KC_GRAVE
);
178 return keycode_to_action(KC_ESC
);
180 if (keymap_config
.swap_backslash_backspace
) {
181 return keycode_to_action(KC_BSPACE
);
183 return keycode_to_action(KC_BSLASH
);
185 if (keymap_config
.swap_backslash_backspace
) {
186 return keycode_to_action(KC_BSLASH
);
188 return keycode_to_action(KC_BSPACE
);
191 return keycode_to_action(keycode
);
197 __attribute__ ((weak
))
198 const macro_t
*action_get_macro(keyrecord_t
*record
, uint8_t id
, uint8_t opt
)
204 __attribute__ ((weak
))
205 void action_function(keyrecord_t
*record
, uint8_t id
, uint8_t opt
)
209 /* translates keycode to action */
210 static action_t
keycode_to_action(uint16_t keycode
)
214 case KC_A
... KC_EXSEL
:
215 case KC_LCTRL
... KC_RGUI
:
216 action
.code
= ACTION_KEY(keycode
);
218 case KC_SYSTEM_POWER
... KC_SYSTEM_WAKE
:
219 action
.code
= ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode
));
221 case KC_AUDIO_MUTE
... KC_WWW_FAVORITES
:
222 action
.code
= ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode
));
224 case KC_MS_UP
... KC_MS_ACCEL2
:
225 action
.code
= ACTION_MOUSEKEY(keycode
);
228 action
.code
= ACTION_TRANSPARENT
;
231 action
.code
= ACTION_NO
;
238 /* translates key to keycode */
239 uint16_t keymap_key_to_keycode(uint8_t layer
, keypos_t key
)
241 // Read entire word (16bits)
242 return pgm_read_word(&keymaps
[(layer
)][(key
.row
)][(key
.col
)]);
245 /* translates Fn keycode to action */
246 action_t
keymap_fn_to_action(uint16_t keycode
)
248 return (action_t
){ .code
= pgm_read_word(&fn_actions
[FN_INDEX(keycode
)]) };
251 action_t
keymap_func_to_action(uint16_t keycode
)
253 // For FUNC without 8bit limit
254 return (action_t
){ .code
= pgm_read_word(&fn_actions
[(int)keycode
]) };