aboutsummaryrefslogtreecommitdiff
path: root/quantum/keymap_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/keymap_common.c')
-rw-r--r--quantum/keymap_common.c226
1 files changed, 226 insertions, 0 deletions
diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c
new file mode 100644
index 000000000..66cf2883c
--- /dev/null
+++ b/quantum/keymap_common.c
@@ -0,0 +1,226 @@
1/*
2Copyright 2012,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
18#include "keymap_common.h"
19#include "report.h"
20#include "keycode.h"
21#include "action_layer.h"
22#include "action.h"
23#include "action_macro.h"
24#include "debug.h"
25#include "backlight.h"
26#include "keymap_midi.h"
27
28static action_t keycode_to_action(uint16_t keycode);
29
30/* converts key to action */
31action_t action_for_key(uint8_t layer, keypos_t key)
32{
33 // 16bit keycodes - important
34 uint16_t keycode = keymap_key_to_keycode(layer, key);
35
36 if (keycode >= 0x0100 && keycode < 0x2000) {
37 // Has a modifier
38 action_t action;
39 // Split it up
40 action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF);
41 return action;
42 } else if (keycode >= 0x2000 && keycode < 0x3000) {
43 // Is a shortcut for function layer, pull last 12bits
44 return keymap_func_to_action(keycode & 0xFFF);
45 } else if (keycode >= 0x3000 && keycode < 0x4000) {
46 action_t action;
47 action.code = ACTION_MACRO(keycode & 0xFF);
48 return action;
49 } else if (keycode >= BL_0 & keycode <= BL_15) {
50 action_t action;
51 action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
52 return action;
53 } else if (keycode == BL_DEC) {
54 action_t action;
55 action.code = ACTION_BACKLIGHT_DECREASE();
56 return action;
57 } else if (keycode == BL_INC) {
58 action_t action;
59 action.code = ACTION_BACKLIGHT_INCREASE();
60 return action;
61 } else if (keycode == BL_TOGG) {
62 action_t action;
63 action.code = ACTION_BACKLIGHT_TOGGLE();
64 return action;
65 } else if (keycode == BL_STEP) {
66 action_t action;
67 action.code = ACTION_BACKLIGHT_STEP();
68 return action;
69 } else if (keycode == RESET) {
70 bootloader_jump();
71 return;
72 } else if (keycode == DEBUG) {
73 print("\nDEBUG: enabled.\n");
74 debug_enable = true;
75 return;
76 } else if (keycode >= 0x5000 && keycode < 0x6000) {
77 int when = (keycode >> 0x9) & 0x3;
78 int layer = keycode & 0xFF;
79 action_t action;
80 action.code = ACTION_LAYER_SET(layer, when);
81 return action;
82 } else if (keycode >= 0x6000 && keycode < 0x7000) {
83 action_t action;
84 action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
85 return action;
86 } else if (keycode >= 0x8000) {
87 action_t action;
88 uint16_t unicode = keycode & ~(0x8000);
89 action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
90 return action;
91 }
92
93 switch (keycode) {
94 case KC_FN0 ... KC_FN31:
95 return keymap_fn_to_action(keycode);
96#ifdef BOOTMAGIC_ENABLE
97 case KC_CAPSLOCK:
98 case KC_LOCKING_CAPS:
99 if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
100 return keycode_to_action(KC_LCTL);
101 }
102 return keycode_to_action(keycode);
103 case KC_LCTL:
104 if (keymap_config.swap_control_capslock) {
105 return keycode_to_action(KC_CAPSLOCK);
106 }
107 return keycode_to_action(KC_LCTL);
108 case KC_LALT:
109 if (keymap_config.swap_lalt_lgui) {
110 if (keymap_config.no_gui) {
111 return keycode_to_action(ACTION_NO);
112 }
113 return keycode_to_action(KC_LGUI);
114 }
115 return keycode_to_action(KC_LALT);
116 case KC_LGUI:
117 if (keymap_config.swap_lalt_lgui) {
118 return keycode_to_action(KC_LALT);
119 }
120 if (keymap_config.no_gui) {
121 return keycode_to_action(ACTION_NO);
122 }
123 return keycode_to_action(KC_LGUI);
124 case KC_RALT:
125 if (keymap_config.swap_ralt_rgui) {
126 if (keymap_config.no_gui) {
127 return keycode_to_action(ACTION_NO);
128 }
129 return keycode_to_action(KC_RGUI);
130 }
131 return keycode_to_action(KC_RALT);
132 case KC_RGUI:
133 if (keymap_config.swap_ralt_rgui) {
134 return keycode_to_action(KC_RALT);
135 }
136 if (keymap_config.no_gui) {
137 return keycode_to_action(ACTION_NO);
138 }
139 return keycode_to_action(KC_RGUI);
140 case KC_GRAVE:
141 if (keymap_config.swap_grave_esc) {
142 return keycode_to_action(KC_ESC);
143 }
144 return keycode_to_action(KC_GRAVE);
145 case KC_ESC:
146 if (keymap_config.swap_grave_esc) {
147 return keycode_to_action(KC_GRAVE);
148 }
149 return keycode_to_action(KC_ESC);
150 case KC_BSLASH:
151 if (keymap_config.swap_backslash_backspace) {
152 return keycode_to_action(KC_BSPACE);
153 }
154 return keycode_to_action(KC_BSLASH);
155 case KC_BSPACE:
156 if (keymap_config.swap_backslash_backspace) {
157 return keycode_to_action(KC_BSLASH);
158 }
159 return keycode_to_action(KC_BSPACE);
160#endif
161 default:
162 return keycode_to_action(keycode);
163 }
164}
165
166
167/* Macro */
168__attribute__ ((weak))
169const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
170{
171 return MACRO_NONE;
172}
173
174/* Function */
175__attribute__ ((weak))
176void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
177{
178}
179
180/* translates keycode to action */
181static action_t keycode_to_action(uint16_t keycode)
182{
183 action_t action;
184 switch (keycode) {
185 case KC_A ... KC_EXSEL:
186 case KC_LCTRL ... KC_RGUI:
187 action.code = ACTION_KEY(keycode);
188 break;
189 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
190 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
191 break;
192 case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
193 action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
194 break;
195 case KC_MS_UP ... KC_MS_ACCEL2:
196 action.code = ACTION_MOUSEKEY(keycode);
197 break;
198 case KC_TRNS:
199 action.code = ACTION_TRANSPARENT;
200 break;
201 default:
202 action.code = ACTION_NO;
203 break;
204 }
205 return action;
206}
207
208
209/* translates key to keycode */
210uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
211{
212 // Read entire word (16bits)
213 return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
214}
215
216/* translates Fn keycode to action */
217action_t keymap_fn_to_action(uint16_t keycode)
218{
219 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
220}
221
222action_t keymap_func_to_action(uint16_t keycode)
223{
224 // For FUNC without 8bit limit
225 return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
226}