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 "extended_keymap_common.h"
21 #include "action_layer.h"
23 #include "action_macro.h"
25 #include "backlight.h"
28 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);
43 } else if (keycode >= 0x2000 && keycode < 0x3000) {
44 // Is a shortcut for function layer, pull last 12bits
45 return keymap_func_to_action(keycode & 0xFFF);
46 } else if (keycode >= 0x3000 && keycode < 0x4000) {
48 action.code = ACTION_MACRO(keycode & 0xFF);
50 } else if (keycode >= BL_0 & keycode <= BL_15) {
52 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
54 } else if (keycode == BL_DEC) {
56 action.code = ACTION_BACKLIGHT_DECREASE();
58 } else if (keycode == BL_INC) {
60 action.code = ACTION_BACKLIGHT_INCREASE();
62 } else if (keycode == BL_TOGG) {
64 action.code = ACTION_BACKLIGHT_TOGGLE();
66 } else if (keycode == BL_STEP) {
68 action.code = ACTION_BACKLIGHT_STEP();
70 } else if (keycode == RESET) {
76 case KC_FN0 ... KC_FN31:
77 return keymap_fn_to_action(keycode);
78 #ifdef BOOTMAGIC_ENABLE
81 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
82 return keycode_to_action(KC_LCTL);
84 return keycode_to_action(keycode);
86 if (keymap_config.swap_control_capslock) {
87 return keycode_to_action(KC_CAPSLOCK);
89 return keycode_to_action(KC_LCTL);
91 if (keymap_config.swap_lalt_lgui) {
92 if (keymap_config.no_gui) {
93 return keycode_to_action(ACTION_NO);
95 return keycode_to_action(KC_LGUI);
97 return keycode_to_action(KC_LALT);
99 if (keymap_config.swap_lalt_lgui) {
100 return keycode_to_action(KC_LALT);
102 if (keymap_config.no_gui) {
103 return keycode_to_action(ACTION_NO);
105 return keycode_to_action(KC_LGUI);
107 if (keymap_config.swap_ralt_rgui) {
108 if (keymap_config.no_gui) {
109 return keycode_to_action(ACTION_NO);
111 return keycode_to_action(KC_RGUI);
113 return keycode_to_action(KC_RALT);
115 if (keymap_config.swap_ralt_rgui) {
116 return keycode_to_action(KC_RALT);
118 if (keymap_config.no_gui) {
119 return keycode_to_action(ACTION_NO);
121 return keycode_to_action(KC_RGUI);
123 if (keymap_config.swap_grave_esc) {
124 return keycode_to_action(KC_ESC);
126 return keycode_to_action(KC_GRAVE);
128 if (keymap_config.swap_grave_esc) {
129 return keycode_to_action(KC_GRAVE);
131 return keycode_to_action(KC_ESC);
133 if (keymap_config.swap_backslash_backspace) {
134 return keycode_to_action(KC_BSPACE);
136 return keycode_to_action(KC_BSLASH);
138 if (keymap_config.swap_backslash_backspace) {
139 return keycode_to_action(KC_BSLASH);
141 return keycode_to_action(KC_BSPACE);
144 return keycode_to_action(keycode);
150 __attribute__ ((weak))
151 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
157 __attribute__ ((weak))
158 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
162 /* translates keycode to action */
163 static action_t keycode_to_action(uint16_t keycode)
167 case KC_A ... KC_EXSEL:
168 case KC_LCTRL ... KC_RGUI:
169 action.code = ACTION_KEY(keycode);
171 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
172 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
174 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
175 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
177 case KC_MS_UP ... KC_MS_ACCEL2:
178 action.code = ACTION_MOUSEKEY(keycode);
181 action.code = ACTION_TRANSPARENT;
184 action.code = ACTION_NO;
191 /* translates key to keycode */
192 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
194 // Read entire word (16bits)
195 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
198 /* translates Fn keycode to action */
199 action_t keymap_fn_to_action(uint16_t keycode)
201 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
204 action_t keymap_func_to_action(uint16_t keycode)
206 // For FUNC without 8bit limit
207 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };