2 Copyright 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/>.
17 #include <avr/pgmspace.h>
21 #include "action_layer.h"
23 #include "action_macro.h"
27 static action_t keycode_to_action(uint8_t keycode);
30 /* converts key to action */
31 action_t action_for_key(uint8_t layer, key_t key)
33 uint8_t keycode = keymap_key_to_keycode(layer, key);
35 case KC_FN0 ... KC_FN31:
36 return keymap_fn_to_action(keycode);
37 #ifdef BOOTMAGIC_ENABLE
40 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
41 return keycode_to_action(KC_LCTL);
43 return keycode_to_action(keycode);
45 if (keymap_config.swap_control_capslock) {
46 return keycode_to_action(KC_CAPSLOCK);
48 return keycode_to_action(KC_LCTL);
50 if (keymap_config.swap_lalt_lgui) {
51 if (keymap_config.no_gui) {
52 return keycode_to_action(ACTION_NO);
54 return keycode_to_action(KC_LGUI);
56 return keycode_to_action(KC_LALT);
58 if (keymap_config.swap_lalt_lgui) {
59 return keycode_to_action(KC_LALT);
61 if (keymap_config.no_gui) {
62 return keycode_to_action(ACTION_NO);
64 return keycode_to_action(KC_LGUI);
66 if (keymap_config.swap_ralt_rgui) {
67 if (keymap_config.no_gui) {
68 return keycode_to_action(ACTION_NO);
70 return keycode_to_action(KC_RGUI);
72 return keycode_to_action(KC_RALT);
74 if (keymap_config.swap_ralt_rgui) {
75 return keycode_to_action(KC_RALT);
77 if (keymap_config.no_gui) {
78 return keycode_to_action(ACTION_NO);
80 return keycode_to_action(KC_RGUI);
82 if (keymap_config.swap_grave_esc) {
83 return keycode_to_action(KC_ESC);
85 return keycode_to_action(KC_GRAVE);
87 if (keymap_config.swap_grave_esc) {
88 return keycode_to_action(KC_GRAVE);
90 return keycode_to_action(KC_ESC);
92 if (keymap_config.swap_backslash_backspace) {
93 return keycode_to_action(KC_BSPACE);
95 return keycode_to_action(KC_BSLASH);
97 if (keymap_config.swap_backslash_backspace) {
98 return keycode_to_action(KC_BSLASH);
100 return keycode_to_action(KC_BSPACE);
103 return keycode_to_action(keycode);
109 __attribute__ ((weak))
110 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
116 __attribute__ ((weak))
117 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
123 /* translates keycode to action */
124 static action_t keycode_to_action(uint8_t keycode)
128 case KC_A ... KC_EXSEL:
129 case KC_LCTRL ... KC_RGUI:
130 action.code = ACTION_KEY(keycode);
132 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
133 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
135 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
136 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
138 case KC_MS_UP ... KC_MS_ACCEL2:
139 action.code = ACTION_MOUSEKEY(keycode);
142 action.code = ACTION_TRANSPARENT;
145 action.code = ACTION_NO;
153 #ifdef USE_LEGACY_KEYMAP
155 * Legacy keymap support
156 * Consider using new keymap API instead.
158 __attribute__ ((weak))
159 uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
161 return keymap_get_keycode(layer, key.row, key.col);
165 /* Legacy keymap support */
166 __attribute__ ((weak))
167 action_t keymap_fn_to_action(uint8_t keycode)
169 action_t action = { .code = ACTION_NO };
171 case KC_FN0 ... KC_FN31:
173 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
174 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
176 action.code = ACTION_LAYER_TAP_KEY(layer, key);
178 action.code = ACTION_LAYER_MOMENTARY(layer);