]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
148162a5102ecae8166a9c8b4625cc392f49750e
[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 extern inline void add_key(uint8_t key);
47 extern inline void del_key(uint8_t key);
48 extern inline void clear_keys(void);
49
50 #ifndef NO_ACTION_ONESHOT
51 static int8_t oneshot_mods = 0;
52 static int8_t oneshot_locked_mods = 0;
53 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
54 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
55 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
56 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
57 static int16_t oneshot_time = 0;
58 bool has_oneshot_mods_timed_out(void) {
59   return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
60 }
61 #else
62 bool has_oneshot_mods_timed_out(void) {
63     return false;
64 }
65 #endif
66 #endif
67
68 /* oneshot layer */
69 #ifndef NO_ACTION_ONESHOT
70 /* oneshot_layer_data bits
71 * LLLL LSSS
72 * where:
73 *   L => are layer bits
74 *   S => oneshot state bits
75 */
76 static int8_t oneshot_layer_data = 0;
77
78 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
79 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
80
81 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
82 static int16_t oneshot_layer_time = 0;
83 inline bool has_oneshot_layer_timed_out() {
84     return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
85         !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
86 }
87 #endif
88
89 /* Oneshot layer */
90 void set_oneshot_layer(uint8_t layer, uint8_t state)
91 {
92     oneshot_layer_data = layer << 3 | state;
93     layer_on(layer);
94 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
95     oneshot_layer_time = timer_read();
96 #endif
97 }
98 void reset_oneshot_layer(void) {
99     oneshot_layer_data = 0;
100 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
101     oneshot_layer_time = 0;
102 #endif
103 }
104 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
105 {
106     uint8_t start_state = oneshot_layer_data;
107     oneshot_layer_data &= ~state;
108     if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
109         layer_off(get_oneshot_layer());
110 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
111     oneshot_layer_time = 0;
112 #endif
113     }
114 }
115 bool is_oneshot_layer_active(void)
116 {
117     return get_oneshot_layer_state();
118 }
119 #endif
120
121 void send_keyboard_report(void) {
122     keyboard_report->mods  = real_mods;
123     keyboard_report->mods |= weak_mods;
124     keyboard_report->mods |= macro_mods;
125 #ifndef NO_ACTION_ONESHOT
126     if (oneshot_mods) {
127 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
128         if (has_oneshot_mods_timed_out()) {
129             dprintf("Oneshot: timeout\n");
130             clear_oneshot_mods();
131         }
132 #endif
133         keyboard_report->mods |= oneshot_mods;
134         if (has_anykey(keyboard_report)) {
135             clear_oneshot_mods();
136         }
137     }
138
139 #endif
140     host_keyboard_send(keyboard_report);
141 }
142
143 /* modifier */
144 uint8_t get_mods(void) { return real_mods; }
145 void add_mods(uint8_t mods) { real_mods |= mods; }
146 void del_mods(uint8_t mods) { real_mods &= ~mods; }
147 void set_mods(uint8_t mods) { real_mods = mods; }
148 void clear_mods(void) { real_mods = 0; }
149
150 /* weak modifier */
151 uint8_t get_weak_mods(void) { return weak_mods; }
152 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
153 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
154 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
155 void clear_weak_mods(void) { weak_mods = 0; }
156
157 /* macro modifier */
158 uint8_t get_macro_mods(void) { return macro_mods; }
159 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
160 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
161 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
162 void clear_macro_mods(void) { macro_mods = 0; }
163
164 /* Oneshot modifier */
165 #ifndef NO_ACTION_ONESHOT
166 void set_oneshot_mods(uint8_t mods)
167 {
168     oneshot_mods = mods;
169 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
170     oneshot_time = timer_read();
171 #endif
172 }
173 void clear_oneshot_mods(void)
174 {
175     oneshot_mods = 0;
176 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
177     oneshot_time = 0;
178 #endif
179 }
180 uint8_t get_oneshot_mods(void)
181 {
182     return oneshot_mods;
183 }
184 #endif
185
186 /*
187  * inspect keyboard state
188  */
189 uint8_t has_anymod(void)
190 {
191     return bitpop(real_mods);
192 }