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"
35 float goodbye[][2] = {
36 {440.0*pow(2.0,(67)/12.0), 8},
37 {440.0*pow(2.0,(60)/12.0), 8},
38 {440.0*pow(2.0,(55)/12.0), 12},
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);
50 if (keycode >= 0x0100 && keycode < 0x2000) {
54 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
56 } else if (keycode >= 0x2000 && keycode < 0x3000) {
57 // Is a shortcut for function layer, pull last 12bits
58 // This means we have 4,096 FN macros at our disposal
59 return keymap_func_to_action(keycode & 0xFFF);
60 } else if (keycode >= 0x3000 && keycode < 0x4000) {
61 // When the code starts with 3, it's an action macro.
63 action.code = ACTION_MACRO(keycode & 0xFF);
65 #ifdef BACKLIGHT_ENABLE
66 } else if (keycode >= BL_0 && keycode <= BL_15) {
68 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
70 } else if (keycode == BL_DEC) {
72 action.code = ACTION_BACKLIGHT_DECREASE();
74 } else if (keycode == BL_INC) {
76 action.code = ACTION_BACKLIGHT_INCREASE();
78 } else if (keycode == BL_TOGG) {
80 action.code = ACTION_BACKLIGHT_TOGGLE();
82 } else if (keycode == BL_STEP) {
84 action.code = ACTION_BACKLIGHT_STEP();
87 } else if (keycode == RESET) { // RESET is 0x5000, which is why this is here
91 play_notes(&goodbye, 3, false);
95 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
99 } else if (keycode == DEBUG) { // DEBUG is 0x5001
100 // TODO: Does this actually work?
102 print("\nDEBUG: enabled.\n");
105 } else if (keycode >= 0x5000 && keycode < 0x6000) {
106 // Layer movement shortcuts
107 // See .h to see constraints/usage
108 int type = (keycode >> 0x8) & 0xF;
111 int when = (keycode >> 0x4) & 0x3;
112 int layer = keycode & 0xF;
114 action.code = ACTION_LAYER_SET(layer, when);
116 } else if (type == 0x2) {
118 int layer = keycode & 0xFF;
120 action.code = ACTION_LAYER_MOMENTARY(layer);
122 } else if (type == 0x3) {
124 int layer = keycode & 0xFF;
126 action.code = ACTION_DEFAULT_LAYER_SET(layer);
128 } else if (type == 0x4) {
130 int layer = keycode & 0xFF;
132 action.code = ACTION_LAYER_TOGGLE(layer);
136 } else if (keycode >= 0x6000 && keycode < 0x7000) {
138 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
141 } else if (keycode >= 0x7000 && keycode < 0x8000) {
143 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
145 } else if (keycode >= 0x8000 && keycode < 0x9000) {
147 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
149 #ifdef UNICODE_ENABLE
150 } else if (keycode >= 0x8000000) {
152 uint16_t unicode = keycode & ~(0x8000);
153 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
161 case KC_FN0 ... KC_FN31:
162 return keymap_fn_to_action(keycode);
163 #ifdef BOOTMAGIC_ENABLE
165 case KC_LOCKING_CAPS:
166 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
167 return keycode_to_action(KC_LCTL);
169 return keycode_to_action(keycode);
171 if (keymap_config.swap_control_capslock) {
172 return keycode_to_action(KC_CAPSLOCK);
174 return keycode_to_action(KC_LCTL);
176 if (keymap_config.swap_lalt_lgui) {
177 if (keymap_config.no_gui) {
178 return keycode_to_action(ACTION_NO);
180 return keycode_to_action(KC_LGUI);
182 return keycode_to_action(KC_LALT);
184 if (keymap_config.swap_lalt_lgui) {
185 return keycode_to_action(KC_LALT);
187 if (keymap_config.no_gui) {
188 return keycode_to_action(ACTION_NO);
190 return keycode_to_action(KC_LGUI);
192 if (keymap_config.swap_ralt_rgui) {
193 if (keymap_config.no_gui) {
194 return keycode_to_action(ACTION_NO);
196 return keycode_to_action(KC_RGUI);
198 return keycode_to_action(KC_RALT);
200 if (keymap_config.swap_ralt_rgui) {
201 return keycode_to_action(KC_RALT);
203 if (keymap_config.no_gui) {
204 return keycode_to_action(ACTION_NO);
206 return keycode_to_action(KC_RGUI);
208 if (keymap_config.swap_grave_esc) {
209 return keycode_to_action(KC_ESC);
211 return keycode_to_action(KC_GRAVE);
213 if (keymap_config.swap_grave_esc) {
214 return keycode_to_action(KC_GRAVE);
216 return keycode_to_action(KC_ESC);
218 if (keymap_config.swap_backslash_backspace) {
219 return keycode_to_action(KC_BSPACE);
221 return keycode_to_action(KC_BSLASH);
223 if (keymap_config.swap_backslash_backspace) {
224 return keycode_to_action(KC_BSLASH);
226 return keycode_to_action(KC_BSPACE);
229 return keycode_to_action(keycode);
235 __attribute__ ((weak))
236 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
242 __attribute__ ((weak))
243 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
247 /* translates keycode to action */
248 static action_t keycode_to_action(uint16_t keycode)
252 case KC_A ... KC_EXSEL:
253 case KC_LCTRL ... KC_RGUI:
254 action.code = ACTION_KEY(keycode);
256 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
257 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
259 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
260 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
262 case KC_MS_UP ... KC_MS_ACCEL2:
263 action.code = ACTION_MOUSEKEY(keycode);
266 action.code = ACTION_TRANSPARENT;
269 action.code = ACTION_NO;
276 /* translates key to keycode */
277 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
279 // Read entire word (16bits)
280 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
283 /* translates Fn keycode to action */
284 action_t keymap_fn_to_action(uint16_t keycode)
286 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
289 action_t keymap_func_to_action(uint16_t keycode)
291 // For FUNC without 8bit limit
292 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
295 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
296 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {