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"
27 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);
42 } else if (keycode >= 0x2000 && keycode < 0x3000) {
43 // Is a shortcut for function layer, pull last 12bits
44 return keymap_func_to_action(keycode & 0xFFF);
45 } else if (keycode >= 0x3000 && keycode < 0x4000) {
47 action.code = ACTION_MACRO(keycode & 0xFF);
52 case KC_FN0 ... KC_FN31:
53 return keymap_fn_to_action(keycode);
54 #ifdef BOOTMAGIC_ENABLE
57 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
58 return keycode_to_action(KC_LCTL);
60 return keycode_to_action(keycode);
62 if (keymap_config.swap_control_capslock) {
63 return keycode_to_action(KC_CAPSLOCK);
65 return keycode_to_action(KC_LCTL);
67 if (keymap_config.swap_lalt_lgui) {
68 if (keymap_config.no_gui) {
69 return keycode_to_action(ACTION_NO);
71 return keycode_to_action(KC_LGUI);
73 return keycode_to_action(KC_LALT);
75 if (keymap_config.swap_lalt_lgui) {
76 return keycode_to_action(KC_LALT);
78 if (keymap_config.no_gui) {
79 return keycode_to_action(ACTION_NO);
81 return keycode_to_action(KC_LGUI);
83 if (keymap_config.swap_ralt_rgui) {
84 if (keymap_config.no_gui) {
85 return keycode_to_action(ACTION_NO);
87 return keycode_to_action(KC_RGUI);
89 return keycode_to_action(KC_RALT);
91 if (keymap_config.swap_ralt_rgui) {
92 return keycode_to_action(KC_RALT);
94 if (keymap_config.no_gui) {
95 return keycode_to_action(ACTION_NO);
97 return keycode_to_action(KC_RGUI);
99 if (keymap_config.swap_grave_esc) {
100 return keycode_to_action(KC_ESC);
102 return keycode_to_action(KC_GRAVE);
104 if (keymap_config.swap_grave_esc) {
105 return keycode_to_action(KC_GRAVE);
107 return keycode_to_action(KC_ESC);
109 if (keymap_config.swap_backslash_backspace) {
110 return keycode_to_action(KC_BSPACE);
112 return keycode_to_action(KC_BSLASH);
114 if (keymap_config.swap_backslash_backspace) {
115 return keycode_to_action(KC_BSLASH);
117 return keycode_to_action(KC_BSPACE);
120 return keycode_to_action(keycode);
126 __attribute__ ((weak))
127 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
133 __attribute__ ((weak))
134 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
138 /* translates keycode to action */
139 static action_t keycode_to_action(uint16_t keycode)
143 case KC_A ... KC_EXSEL:
144 case KC_LCTRL ... KC_RGUI:
145 action.code = ACTION_KEY(keycode);
147 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
148 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
150 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
151 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
153 case KC_MS_UP ... KC_MS_ACCEL2:
154 action.code = ACTION_MOUSEKEY(keycode);
157 action.code = ACTION_TRANSPARENT;
160 action.code = ACTION_NO;
167 /* translates key to keycode */
168 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
170 // Read entire word (16bits)
171 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
174 /* translates Fn keycode to action */
175 action_t keymap_fn_to_action(uint16_t keycode)
177 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
180 action_t keymap_func_to_action(uint16_t keycode)
182 // For FUNC without 8bit limit
183 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };