]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
Move functionality from action_util to report
[qmk_firmware.git] / tmk_core / common / action_util.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 "host.h"
18 #include "report.h"
19 #include "debug.h"
20 #include "action_util.h"
21 #include "action_layer.h"
22 #include "timer.h"
23 #include "keycode_config.h"
24
25 extern keymap_config_t keymap_config;
26
27
28 static uint8_t real_mods = 0;
29 static uint8_t weak_mods = 0;
30 static uint8_t macro_mods = 0;
31
32 #ifdef USB_6KRO_ENABLE
33 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
34 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
35 #define RO_INC(a) RO_ADD(a, 1)
36 #define RO_DEC(a) RO_SUB(a, 1)
37 static int8_t cb_head = 0;
38 static int8_t cb_tail = 0;
39 static int8_t cb_count = 0;
40 #endif
41
42 // TODO: pointer variable is not needed
43 //report_keyboard_t keyboard_report = {};
44 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
45
46 #ifndef NO_ACTION_ONESHOT
47 static int8_t oneshot_mods = 0;
48 static int8_t oneshot_locked_mods = 0;
49 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
50 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
51 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
52 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
53 static int16_t oneshot_time = 0;
54 bool has_oneshot_mods_timed_out(void) {
55   return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
56 }
57 #else
58 bool has_oneshot_mods_timed_out(void) {
59     return false;
60 }
61 #endif
62 #endif
63
64 /* oneshot layer */
65 #ifndef NO_ACTION_ONESHOT
66 /* oneshot_layer_data bits
67 * LLLL LSSS
68 * where:
69 *   L => are layer bits
70 *   S => oneshot state bits
71 */
72 static int8_t oneshot_layer_data = 0;
73
74 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
75 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
76
77 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
78 static int16_t oneshot_layer_time = 0;
79 inline bool has_oneshot_layer_timed_out() {
80     return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
81         !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
82 }
83 #endif
84
85 /* Oneshot layer */
86 void set_oneshot_layer(uint8_t layer, uint8_t state)
87 {
88     oneshot_layer_data = layer << 3 | state;
89     layer_on(layer);
90 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
91     oneshot_layer_time = timer_read();
92 #endif
93 }
94 void reset_oneshot_layer(void) {
95     oneshot_layer_data = 0;
96 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
97     oneshot_layer_time = 0;
98 #endif
99 }
100 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
101 {
102     uint8_t start_state = oneshot_layer_data;
103     oneshot_layer_data &= ~state;
104     if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
105         layer_off(get_oneshot_layer());
106 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
107     oneshot_layer_time = 0;
108 #endif
109     }
110 }
111 bool is_oneshot_layer_active(void)
112 {
113     return get_oneshot_layer_state();
114 }
115 #endif
116
117 void send_keyboard_report(void) {
118     keyboard_report->mods  = real_mods;
119     keyboard_report->mods |= weak_mods;
120     keyboard_report->mods |= macro_mods;
121 #ifndef NO_ACTION_ONESHOT
122     if (oneshot_mods) {
123 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
124         if (has_oneshot_mods_timed_out()) {
125             dprintf("Oneshot: timeout\n");
126             clear_oneshot_mods();
127         }
128 #endif
129         keyboard_report->mods |= oneshot_mods;
130         if (has_anykey(keyboard_report)) {
131             clear_oneshot_mods();
132         }
133     }
134
135 #endif
136     host_keyboard_send(keyboard_report);
137 }
138
139 /* key */
140 void add_key(uint8_t key)
141 {
142 #ifdef NKRO_ENABLE
143     if (keyboard_protocol && keymap_config.nkro) {
144         add_key_bit(keyboard_report, key);
145         return;
146     }
147 #endif
148     add_key_byte(keyboard_report, key);
149 }
150
151 void del_key(uint8_t key)
152 {
153 #ifdef NKRO_ENABLE
154     if (keyboard_protocol && keymap_config.nkro) {
155         del_key_bit(keyboard_report, key);
156         return;
157     }
158 #endif
159     del_key_byte(keyboard_report, key);
160 }
161
162 void clear_keys(void)
163 {
164     // not clear mods
165     for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
166         keyboard_report->raw[i] = 0;
167     }
168 }
169
170
171 /* modifier */
172 uint8_t get_mods(void) { return real_mods; }
173 void add_mods(uint8_t mods) { real_mods |= mods; }
174 void del_mods(uint8_t mods) { real_mods &= ~mods; }
175 void set_mods(uint8_t mods) { real_mods = mods; }
176 void clear_mods(void) { real_mods = 0; }
177
178 /* weak modifier */
179 uint8_t get_weak_mods(void) { return weak_mods; }
180 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
181 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
182 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
183 void clear_weak_mods(void) { weak_mods = 0; }
184
185 /* macro modifier */
186 uint8_t get_macro_mods(void) { return macro_mods; }
187 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
188 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
189 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
190 void clear_macro_mods(void) { macro_mods = 0; }
191
192 /* Oneshot modifier */
193 #ifndef NO_ACTION_ONESHOT
194 void set_oneshot_mods(uint8_t mods)
195 {
196     oneshot_mods = mods;
197 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
198     oneshot_time = timer_read();
199 #endif
200 }
201 void clear_oneshot_mods(void)
202 {
203     oneshot_mods = 0;
204 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
205     oneshot_time = 0;
206 #endif
207 }
208 uint8_t get_oneshot_mods(void)
209 {
210     return oneshot_mods;
211 }
212 #endif
213
214 /*
215  * inspect keyboard state
216  */
217 uint8_t has_anymod(void)
218 {
219     return bitpop(real_mods);
220 }