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"
29 static action_t keycode_to_action(uint16_t keycode);
31 /* converts key to action */
32 action_t action_for_key(uint8_t layer, keypos_t key)
34 // 16bit keycodes - important
35 uint16_t keycode = keymap_key_to_keycode(layer, key);
37 if (keycode >= 0x0100 && keycode < 0x2000) {
41 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
43 } else if (keycode >= 0x2000 && keycode < 0x3000) {
44 // Is a shortcut for function layer, pull last 12bits
45 // This means we have 4,096 FN macros at our disposal
46 return keymap_func_to_action(keycode & 0xFFF);
47 } else if (keycode >= 0x3000 && keycode < 0x4000) {
48 // When the code starts with 3, it's an action macro.
50 action.code = ACTION_MACRO(keycode & 0xFF);
52 #ifdef BACKLIGHT_ENABLE
53 } else if (keycode >= BL_0 & keycode <= BL_15) {
55 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
57 } else if (keycode == BL_DEC) {
59 action.code = ACTION_BACKLIGHT_DECREASE();
61 } else if (keycode == BL_INC) {
63 action.code = ACTION_BACKLIGHT_INCREASE();
65 } else if (keycode == BL_TOGG) {
67 action.code = ACTION_BACKLIGHT_TOGGLE();
69 } else if (keycode == BL_STEP) {
71 action.code = ACTION_BACKLIGHT_STEP();
74 } else if (keycode == RESET) { // RESET is 0x5000, which is why this is here
79 } else if (keycode == DEBUG) { // DEBUG is 0x5001
80 // TODO: Does this actually work?
81 print("\nDEBUG: enabled.\n");
84 } else if (keycode >= 0x5000 && keycode < 0x6000) {
85 // Layer movement shortcuts
86 // See .h to see constraints/usage
87 int type = (keycode >> 0x8) & 0xF;
90 int when = (keycode >> 0x4) & 0x3;
91 int layer = keycode & 0xF;
93 action.code = ACTION_LAYER_SET(layer, when);
95 } else if (type == 0x2) {
97 int layer = keycode & 0xFF;
99 action.code = ACTION_LAYER_MOMENTARY(layer);
101 } else if (type == 0x3) {
103 int layer = keycode & 0xFF;
105 action.code = ACTION_DEFAULT_LAYER_SET(layer);
107 } else if (type == 0x4) {
109 int layer = keycode & 0xFF;
111 action.code = ACTION_LAYER_TOGGLE(layer);
115 } else if (keycode >= 0x6000 && keycode < 0x7000) {
117 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
120 } else if (keycode >= 0x7000 && keycode < 0x8000) {
122 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
124 } else if (keycode >= 0x8000 && keycode < 0x9000) {
126 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
128 #ifdef UNICODE_ENABLE
129 } else if (keycode >= 0x8000000) {
131 uint16_t unicode = keycode & ~(0x8000);
132 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
140 case KC_FN0 ... KC_FN31:
141 return keymap_fn_to_action(keycode);
142 #ifdef BOOTMAGIC_ENABLE
144 case KC_LOCKING_CAPS:
145 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
146 return keycode_to_action(KC_LCTL);
148 return keycode_to_action(keycode);
150 if (keymap_config.swap_control_capslock) {
151 return keycode_to_action(KC_CAPSLOCK);
153 return keycode_to_action(KC_LCTL);
155 if (keymap_config.swap_lalt_lgui) {
156 if (keymap_config.no_gui) {
157 return keycode_to_action(ACTION_NO);
159 return keycode_to_action(KC_LGUI);
161 return keycode_to_action(KC_LALT);
163 if (keymap_config.swap_lalt_lgui) {
164 return keycode_to_action(KC_LALT);
166 if (keymap_config.no_gui) {
167 return keycode_to_action(ACTION_NO);
169 return keycode_to_action(KC_LGUI);
171 if (keymap_config.swap_ralt_rgui) {
172 if (keymap_config.no_gui) {
173 return keycode_to_action(ACTION_NO);
175 return keycode_to_action(KC_RGUI);
177 return keycode_to_action(KC_RALT);
179 if (keymap_config.swap_ralt_rgui) {
180 return keycode_to_action(KC_RALT);
182 if (keymap_config.no_gui) {
183 return keycode_to_action(ACTION_NO);
185 return keycode_to_action(KC_RGUI);
187 if (keymap_config.swap_grave_esc) {
188 return keycode_to_action(KC_ESC);
190 return keycode_to_action(KC_GRAVE);
192 if (keymap_config.swap_grave_esc) {
193 return keycode_to_action(KC_GRAVE);
195 return keycode_to_action(KC_ESC);
197 if (keymap_config.swap_backslash_backspace) {
198 return keycode_to_action(KC_BSPACE);
200 return keycode_to_action(KC_BSLASH);
202 if (keymap_config.swap_backslash_backspace) {
203 return keycode_to_action(KC_BSLASH);
205 return keycode_to_action(KC_BSPACE);
208 return keycode_to_action(keycode);
214 __attribute__ ((weak))
215 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
221 __attribute__ ((weak))
222 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
226 /* translates keycode to action */
227 static action_t keycode_to_action(uint16_t keycode)
231 case KC_A ... KC_EXSEL:
232 case KC_LCTRL ... KC_RGUI:
233 action.code = ACTION_KEY(keycode);
235 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
236 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
238 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
239 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
241 case KC_MS_UP ... KC_MS_ACCEL2:
242 action.code = ACTION_MOUSEKEY(keycode);
245 action.code = ACTION_TRANSPARENT;
248 action.code = ACTION_NO;
255 /* translates key to keycode */
256 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
258 // Read entire word (16bits)
259 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
262 /* translates Fn keycode to action */
263 action_t keymap_fn_to_action(uint16_t keycode)
265 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
268 action_t keymap_func_to_action(uint16_t keycode)
270 // For FUNC without 8bit limit
271 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };