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