]> git.gir.st - tmk_keyboard.git/blob - tmk_core/common/keymap.c
Merge branch 'master' into chibios
[tmk_keyboard.git] / tmk_core / common / keymap.c
1 /*
2 Copyright 2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #include "keymap.h"
18 #include "report.h"
19 #include "keycode.h"
20 #include "action_layer.h"
21 #include "action.h"
22 #include "action_macro.h"
23 #include "wait.h"
24 #include "debug.h"
25
26
27 static action_t keycode_to_action(uint8_t keycode);
28
29
30 /* converts key to action */
31 action_t action_for_key(uint8_t layer, keypos_t key)
32 {
33 uint8_t keycode = keymap_key_to_keycode(layer, key);
34 switch (keycode) {
35 case KC_FN0 ... KC_FN31:
36 return keymap_fn_to_action(keycode);
37 #ifdef BOOTMAGIC_ENABLE
38 case KC_CAPSLOCK:
39 case KC_LOCKING_CAPS:
40 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
41 return keycode_to_action(KC_LCTL);
42 }
43 return keycode_to_action(keycode);
44 case KC_LCTL:
45 if (keymap_config.swap_control_capslock) {
46 return keycode_to_action(KC_CAPSLOCK);
47 }
48 return keycode_to_action(KC_LCTL);
49 case KC_LALT:
50 if (keymap_config.swap_lalt_lgui) {
51 if (keymap_config.no_gui) {
52 return keycode_to_action(ACTION_NO);
53 }
54 return keycode_to_action(KC_LGUI);
55 }
56 return keycode_to_action(KC_LALT);
57 case KC_LGUI:
58 if (keymap_config.swap_lalt_lgui) {
59 return keycode_to_action(KC_LALT);
60 }
61 if (keymap_config.no_gui) {
62 return keycode_to_action(ACTION_NO);
63 }
64 return keycode_to_action(KC_LGUI);
65 case KC_RALT:
66 if (keymap_config.swap_ralt_rgui) {
67 if (keymap_config.no_gui) {
68 return keycode_to_action(ACTION_NO);
69 }
70 return keycode_to_action(KC_RGUI);
71 }
72 return keycode_to_action(KC_RALT);
73 case KC_RGUI:
74 if (keymap_config.swap_ralt_rgui) {
75 return keycode_to_action(KC_RALT);
76 }
77 if (keymap_config.no_gui) {
78 return keycode_to_action(ACTION_NO);
79 }
80 return keycode_to_action(KC_RGUI);
81 case KC_GRAVE:
82 if (keymap_config.swap_grave_esc) {
83 return keycode_to_action(KC_ESC);
84 }
85 return keycode_to_action(KC_GRAVE);
86 case KC_ESC:
87 if (keymap_config.swap_grave_esc) {
88 return keycode_to_action(KC_GRAVE);
89 }
90 return keycode_to_action(KC_ESC);
91 case KC_BSLASH:
92 if (keymap_config.swap_backslash_backspace) {
93 return keycode_to_action(KC_BSPACE);
94 }
95 return keycode_to_action(KC_BSLASH);
96 case KC_BSPACE:
97 if (keymap_config.swap_backslash_backspace) {
98 return keycode_to_action(KC_BSLASH);
99 }
100 return keycode_to_action(KC_BSPACE);
101 #endif
102 default:
103 return keycode_to_action(keycode);
104 }
105 }
106
107
108 /* Macro */
109 __attribute__ ((weak))
110 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
111 {
112 (void)record;
113 (void)id;
114 (void)opt;
115 return MACRO_NONE;
116 }
117
118 /* Function */
119 __attribute__ ((weak))
120 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
121 {
122 (void)record;
123 (void)id;
124 (void)opt;
125 }
126
127
128
129 /* translates keycode to action */
130 static action_t keycode_to_action(uint8_t keycode)
131 {
132 action_t action;
133 switch (keycode) {
134 case KC_A ... KC_EXSEL:
135 case KC_LCTRL ... KC_RGUI:
136 action.code = ACTION_KEY(keycode);
137 break;
138 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
139 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
140 break;
141 case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
142 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
143 break;
144 case KC_MS_UP ... KC_MS_ACCEL2:
145 action.code = ACTION_MOUSEKEY(keycode);
146 break;
147 case KC_TRNS:
148 action.code = ACTION_TRANSPARENT;
149 break;
150 case KC_BOOTLOADER:
151 clear_keyboard();
152 wait_ms(50);
153 bootloader_jump(); // not return
154 break;
155 default:
156 action.code = ACTION_NO;
157 break;
158 }
159 return action;
160 }
161
162
163
164 #ifdef USE_LEGACY_KEYMAP
165 /*
166 * Legacy keymap support
167 * Consider using new keymap API instead.
168 */
169 __attribute__ ((weak))
170 uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
171 {
172 return keymap_get_keycode(layer, key.row, key.col);
173 }
174
175
176 /* Legacy keymap support */
177 __attribute__ ((weak))
178 action_t keymap_fn_to_action(uint8_t keycode)
179 {
180 action_t action = { .code = ACTION_NO };
181 switch (keycode) {
182 case KC_FN0 ... KC_FN31:
183 {
184 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
185 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
186 if (key) {
187 action.code = ACTION_LAYER_TAP_KEY(layer, key);
188 } else {
189 action.code = ACTION_LAYER_MOMENTARY(layer);
190 }
191 }
192 return action;
193 default:
194 return action;
195 }
196 }
197 #endif
Imprint / Impressum