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