]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/planck/extended_keymap_common.c
macros
[qmk_firmware.git] / keyboard / planck / extended_keymap_common.c
1 /*
2 Copyright 2012,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
18 #include "extended_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
26
27 static action_t keycode_to_action(uint16_t keycode);
28
29
30 /* converts key to action */
31 action_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         }
50
51     switch (keycode) {
52         case KC_FN0 ... KC_FN31:
53             return keymap_fn_to_action(keycode);
54 #ifdef BOOTMAGIC_ENABLE
55         case KC_CAPSLOCK:
56         case KC_LOCKING_CAPS:
57             if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
58                 return keycode_to_action(KC_LCTL);
59             }
60             return keycode_to_action(keycode);
61         case KC_LCTL:
62             if (keymap_config.swap_control_capslock) {
63                 return keycode_to_action(KC_CAPSLOCK);
64             }
65             return keycode_to_action(KC_LCTL);
66         case KC_LALT:
67             if (keymap_config.swap_lalt_lgui) {
68                 if (keymap_config.no_gui) {
69                     return keycode_to_action(ACTION_NO);
70                 }
71                 return keycode_to_action(KC_LGUI);
72             }
73             return keycode_to_action(KC_LALT);
74         case KC_LGUI:
75             if (keymap_config.swap_lalt_lgui) {
76                 return keycode_to_action(KC_LALT);
77             }
78             if (keymap_config.no_gui) {
79                 return keycode_to_action(ACTION_NO);
80             }
81             return keycode_to_action(KC_LGUI);
82         case KC_RALT:
83             if (keymap_config.swap_ralt_rgui) {
84                 if (keymap_config.no_gui) {
85                     return keycode_to_action(ACTION_NO);
86                 }
87                 return keycode_to_action(KC_RGUI);
88             }
89             return keycode_to_action(KC_RALT);
90         case KC_RGUI:
91             if (keymap_config.swap_ralt_rgui) {
92                 return keycode_to_action(KC_RALT);
93             }
94             if (keymap_config.no_gui) {
95                 return keycode_to_action(ACTION_NO);
96             }
97             return keycode_to_action(KC_RGUI);
98         case KC_GRAVE:
99             if (keymap_config.swap_grave_esc) {
100                 return keycode_to_action(KC_ESC);
101             }
102             return keycode_to_action(KC_GRAVE);
103         case KC_ESC:
104             if (keymap_config.swap_grave_esc) {
105                 return keycode_to_action(KC_GRAVE);
106             }
107             return keycode_to_action(KC_ESC);
108         case KC_BSLASH:
109             if (keymap_config.swap_backslash_backspace) {
110                 return keycode_to_action(KC_BSPACE);
111             }
112             return keycode_to_action(KC_BSLASH);
113         case KC_BSPACE:
114             if (keymap_config.swap_backslash_backspace) {
115                 return keycode_to_action(KC_BSLASH);
116             }
117             return keycode_to_action(KC_BSPACE);
118 #endif
119         default:
120             return keycode_to_action(keycode);
121     }
122 }
123
124
125 /* Macro */
126 __attribute__ ((weak))
127 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
128 {
129     return MACRO_NONE;
130 }
131
132 /* Function */
133 __attribute__ ((weak))
134 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
135 {
136 }
137
138 /* translates keycode to action */
139 static action_t keycode_to_action(uint16_t keycode)
140 {
141     action_t action;
142     switch (keycode) {
143         case KC_A ... KC_EXSEL:
144         case KC_LCTRL ... KC_RGUI:
145             action.code = ACTION_KEY(keycode);
146             break;
147         case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
148             action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
149             break;
150         case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
151             action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
152             break;
153         case KC_MS_UP ... KC_MS_ACCEL2:
154             action.code = ACTION_MOUSEKEY(keycode);
155             break;
156         case KC_TRNS:
157             action.code = ACTION_TRANSPARENT;
158             break;
159         default:
160             action.code = ACTION_NO;
161             break;
162     }
163     return action;
164 }
165
166
167 /* translates key to keycode */
168 uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
169 {
170         // Read entire word (16bits)
171     return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
172 }
173
174 /* translates Fn keycode to action */
175 action_t keymap_fn_to_action(uint16_t keycode)
176 {
177     return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
178 }
179
180 action_t keymap_func_to_action(uint16_t keycode)
181 {
182         // For FUNC without 8bit limit
183     return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
184 }