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), 400},
38 {440.0*pow(2.0,(60)/12.0), 400},
40 {440.0*pow(2.0,(55)/12.0), 600},
44 static action_t keycode_to_action(uint16_t keycode);
46 /* converts key to action */
47 action_t action_for_key(uint8_t layer, keypos_t key)
49 // 16bit keycodes - important
50 uint16_t keycode = keymap_key_to_keycode(layer, key);
52 if (keycode >= 0x0100 && keycode < 0x2000) {
56 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
58 } else if (keycode >= 0x2000 && keycode < 0x3000) {
59 // Is a shortcut for function layer, pull last 12bits
60 // This means we have 4,096 FN macros at our disposal
61 return keymap_func_to_action(keycode & 0xFFF);
62 } else if (keycode >= 0x3000 && keycode < 0x4000) {
63 // When the code starts with 3, it's an action macro.
65 action.code = ACTION_MACRO(keycode & 0xFF);
67 #ifdef BACKLIGHT_ENABLE
68 } else if (keycode >= BL_0 && keycode <= BL_15) {
70 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
72 } else if (keycode == BL_DEC) {
74 action.code = ACTION_BACKLIGHT_DECREASE();
76 } else if (keycode == BL_INC) {
78 action.code = ACTION_BACKLIGHT_INCREASE();
80 } else if (keycode == BL_TOGG) {
82 action.code = ACTION_BACKLIGHT_TOGGLE();
84 } else if (keycode == BL_STEP) {
86 action.code = ACTION_BACKLIGHT_STEP();
89 } else if (keycode == RESET) { // RESET is 0x5000, which is why this is here
93 play_notes(&goodbye, 5, false);
97 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
101 } else if (keycode == DEBUG) { // DEBUG is 0x5001
102 // TODO: Does this actually work?
104 print("\nDEBUG: enabled.\n");
107 } else if (keycode >= 0x5000 && keycode < 0x6000) {
108 // Layer movement shortcuts
109 // See .h to see constraints/usage
110 int type = (keycode >> 0x8) & 0xF;
113 int when = (keycode >> 0x4) & 0x3;
114 int layer = keycode & 0xF;
116 action.code = ACTION_LAYER_SET(layer, when);
118 } else if (type == 0x2) {
120 int layer = keycode & 0xFF;
122 action.code = ACTION_LAYER_MOMENTARY(layer);
124 } else if (type == 0x3) {
126 int layer = keycode & 0xFF;
128 action.code = ACTION_DEFAULT_LAYER_SET(layer);
130 } else if (type == 0x4) {
132 int layer = keycode & 0xFF;
134 action.code = ACTION_LAYER_TOGGLE(layer);
138 } else if (keycode >= 0x6000 && keycode < 0x7000) {
140 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
143 } else if (keycode >= 0x7000 && keycode < 0x8000) {
145 action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
147 } else if (keycode >= 0x8000 && keycode < 0x9000) {
149 action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
151 #ifdef UNICODE_ENABLE
152 } else if (keycode >= 0x8000000) {
154 uint16_t unicode = keycode & ~(0x8000);
155 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
163 case KC_FN0 ... KC_FN31:
164 return keymap_fn_to_action(keycode);
165 #ifdef BOOTMAGIC_ENABLE
167 case KC_LOCKING_CAPS:
168 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
169 return keycode_to_action(KC_LCTL);
171 return keycode_to_action(keycode);
173 if (keymap_config.swap_control_capslock) {
174 return keycode_to_action(KC_CAPSLOCK);
176 return keycode_to_action(KC_LCTL);
178 if (keymap_config.swap_lalt_lgui) {
179 if (keymap_config.no_gui) {
180 return keycode_to_action(ACTION_NO);
182 return keycode_to_action(KC_LGUI);
184 return keycode_to_action(KC_LALT);
186 if (keymap_config.swap_lalt_lgui) {
187 return keycode_to_action(KC_LALT);
189 if (keymap_config.no_gui) {
190 return keycode_to_action(ACTION_NO);
192 return keycode_to_action(KC_LGUI);
194 if (keymap_config.swap_ralt_rgui) {
195 if (keymap_config.no_gui) {
196 return keycode_to_action(ACTION_NO);
198 return keycode_to_action(KC_RGUI);
200 return keycode_to_action(KC_RALT);
202 if (keymap_config.swap_ralt_rgui) {
203 return keycode_to_action(KC_RALT);
205 if (keymap_config.no_gui) {
206 return keycode_to_action(ACTION_NO);
208 return keycode_to_action(KC_RGUI);
210 if (keymap_config.swap_grave_esc) {
211 return keycode_to_action(KC_ESC);
213 return keycode_to_action(KC_GRAVE);
215 if (keymap_config.swap_grave_esc) {
216 return keycode_to_action(KC_GRAVE);
218 return keycode_to_action(KC_ESC);
220 if (keymap_config.swap_backslash_backspace) {
221 return keycode_to_action(KC_BSPACE);
223 return keycode_to_action(KC_BSLASH);
225 if (keymap_config.swap_backslash_backspace) {
226 return keycode_to_action(KC_BSLASH);
228 return keycode_to_action(KC_BSPACE);
231 return keycode_to_action(keycode);
237 __attribute__ ((weak))
238 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
244 __attribute__ ((weak))
245 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
249 /* translates keycode to action */
250 static action_t keycode_to_action(uint16_t keycode)
254 case KC_A ... KC_EXSEL:
255 case KC_LCTRL ... KC_RGUI:
256 action.code = ACTION_KEY(keycode);
258 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
259 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
261 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
262 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
264 case KC_MS_UP ... KC_MS_ACCEL2:
265 action.code = ACTION_MOUSEKEY(keycode);
268 action.code = ACTION_TRANSPARENT;
271 action.code = ACTION_NO;
278 /* translates key to keycode */
279 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
281 // Read entire word (16bits)
282 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
285 /* translates Fn keycode to action */
286 action_t keymap_fn_to_action(uint16_t keycode)
288 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
291 action_t keymap_func_to_action(uint16_t keycode)
293 // For FUNC without 8bit limit
294 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };