aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/keymap.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common/keymap.c')
-rw-r--r--tmk_core/common/keymap.c185
1 files changed, 185 insertions, 0 deletions
diff --git a/tmk_core/common/keymap.c b/tmk_core/common/keymap.c
new file mode 100644
index 000000000..4c0b61b8c
--- /dev/null
+++ b/tmk_core/common/keymap.c
@@ -0,0 +1,185 @@
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 "debug.h"
24
25
26static action_t keycode_to_action(uint8_t keycode);
27
28
29/* converts key to action */
30action_t action_for_key(uint8_t layer, keypos_t key)
31{
32 uint8_t keycode = keymap_key_to_keycode(layer, key);
33 switch (keycode) {
34 case KC_FN0 ... KC_FN31:
35 return keymap_fn_to_action(keycode);
36#ifdef BOOTMAGIC_ENABLE
37 case KC_CAPSLOCK:
38 case KC_LOCKING_CAPS:
39 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
40 return keycode_to_action(KC_LCTL);
41 }
42 return keycode_to_action(keycode);
43 case KC_LCTL:
44 if (keymap_config.swap_control_capslock) {
45 return keycode_to_action(KC_CAPSLOCK);
46 }
47 return keycode_to_action(KC_LCTL);
48 case KC_LALT:
49 if (keymap_config.swap_lalt_lgui) {
50 if (keymap_config.no_gui) {
51 return keycode_to_action(ACTION_NO);
52 }
53 return keycode_to_action(KC_LGUI);
54 }
55 return keycode_to_action(KC_LALT);
56 case KC_LGUI:
57 if (keymap_config.swap_lalt_lgui) {
58 return keycode_to_action(KC_LALT);
59 }
60 if (keymap_config.no_gui) {
61 return keycode_to_action(ACTION_NO);
62 }
63 return keycode_to_action(KC_LGUI);
64 case KC_RALT:
65 if (keymap_config.swap_ralt_rgui) {
66 if (keymap_config.no_gui) {
67 return keycode_to_action(ACTION_NO);
68 }
69 return keycode_to_action(KC_RGUI);
70 }
71 return keycode_to_action(KC_RALT);
72 case KC_RGUI:
73 if (keymap_config.swap_ralt_rgui) {
74 return keycode_to_action(KC_RALT);
75 }
76 if (keymap_config.no_gui) {
77 return keycode_to_action(ACTION_NO);
78 }
79 return keycode_to_action(KC_RGUI);
80 case KC_GRAVE:
81 if (keymap_config.swap_grave_esc) {
82 return keycode_to_action(KC_ESC);
83 }
84 return keycode_to_action(KC_GRAVE);
85 case KC_ESC:
86 if (keymap_config.swap_grave_esc) {
87 return keycode_to_action(KC_GRAVE);
88 }
89 return keycode_to_action(KC_ESC);
90 case KC_BSLASH:
91 if (keymap_config.swap_backslash_backspace) {
92 return keycode_to_action(KC_BSPACE);
93 }
94 return keycode_to_action(KC_BSLASH);
95 case KC_BSPACE:
96 if (keymap_config.swap_backslash_backspace) {
97 return keycode_to_action(KC_BSLASH);
98 }
99 return keycode_to_action(KC_BSPACE);
100#endif
101 default:
102 return keycode_to_action(keycode);
103 }
104}
105
106
107/* Macro */
108__attribute__ ((weak))
109const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
110{
111 return MACRO_NONE;
112}
113
114/* Function */
115__attribute__ ((weak))
116void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
117{
118}
119
120
121
122/* translates keycode to action */
123static action_t keycode_to_action(uint8_t keycode)
124{
125 action_t action;
126 switch (keycode) {
127 case KC_A ... KC_EXSEL:
128 case KC_LCTRL ... KC_RGUI:
129 action.code = ACTION_KEY(keycode);
130 break;
131 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
132 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
133 break;
134 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
135 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
136 break;
137 case KC_MS_UP ... KC_MS_ACCEL2:
138 action.code = ACTION_MOUSEKEY(keycode);
139 break;
140 case KC_TRNS:
141 action.code = ACTION_TRANSPARENT;
142 break;
143 default:
144 action.code = ACTION_NO;
145 break;
146 }
147 return action;
148}
149
150
151
152#ifdef USE_LEGACY_KEYMAP
153/*
154 * Legacy keymap support
155 * Consider using new keymap API instead.
156 */
157__attribute__ ((weak))
158uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
159{
160 return keymap_get_keycode(layer, key.row, key.col);
161}
162
163
164/* Legacy keymap support */
165__attribute__ ((weak))
166action_t keymap_fn_to_action(uint8_t keycode)
167{
168 action_t action = { .code = ACTION_NO };
169 switch (keycode) {
170 case KC_FN0 ... KC_FN31:
171 {
172 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
173 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
174 if (key) {
175 action.code = ACTION_LAYER_TAP_KEY(layer, key);
176 } else {
177 action.code = ACTION_LAYER_MOMENTARY(layer);
178 }
179 }
180 return action;
181 default:
182 return action;
183 }
184}
185#endif