aboutsummaryrefslogtreecommitdiff
path: root/common/action_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/action_util.c')
-rw-r--r--common/action_util.c213
1 files changed, 213 insertions, 0 deletions
diff --git a/common/action_util.c b/common/action_util.c
new file mode 100644
index 000000000..99a3adaab
--- /dev/null
+++ b/common/action_util.c
@@ -0,0 +1,213 @@
1/*
2Copyright 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#include "host.h"
18#include "report.h"
19#include "debug.h"
20#include "action_util.h"
21#include "timer.h"
22
23static inline void add_key_byte(uint8_t code);
24static inline void del_key_byte(uint8_t code);
25#ifdef NKRO_ENABLE
26static inline void add_key_bit(uint8_t code);
27static inline void del_key_bit(uint8_t code);
28#endif
29
30static uint8_t real_mods = 0;
31static uint8_t weak_mods = 0;
32
33
34// TODO: pointer variable is not needed
35//report_keyboard_t keyboard_report = {};
36report_keyboard_t *keyboard_report = &(report_keyboard_t){};
37
38#ifndef NO_ACTION_ONESHOT
39static int8_t oneshot_mods = 0;
40#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
41static int16_t oneshot_time = 0;
42#endif
43#endif
44
45
46void send_keyboard_report(void) {
47 keyboard_report->mods = real_mods;
48 keyboard_report->mods |= weak_mods;
49#ifndef NO_ACTION_ONESHOT
50 if (oneshot_mods) {
51#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
52 if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
53 dprintf("Oneshot: timeout\n");
54 clear_oneshot_mods();
55 }
56#endif
57 keyboard_report->mods |= oneshot_mods;
58 if (has_anykey()) {
59 clear_oneshot_mods();
60 }
61 }
62#endif
63 host_keyboard_send(keyboard_report);
64}
65
66/* key */
67void add_key(uint8_t key)
68{
69#ifdef NKRO_ENABLE
70 if (keyboard_nkro) {
71 add_key_bit(key);
72 return;
73 }
74#endif
75 add_key_byte(key);
76}
77
78void del_key(uint8_t key)
79{
80#ifdef NKRO_ENABLE
81 if (keyboard_nkro) {
82 del_key_bit(key);
83 return;
84 }
85#endif
86 del_key_byte(key);
87}
88
89void clear_keys(void)
90{
91 // not clear mods
92 for (int8_t i = 1; i < REPORT_SIZE; i++) {
93 keyboard_report->raw[i] = 0;
94 }
95}
96
97
98/* modifier */
99uint8_t get_mods(void) { return real_mods; }
100void add_mods(uint8_t mods) { real_mods |= mods; }
101void del_mods(uint8_t mods) { real_mods &= ~mods; }
102void set_mods(uint8_t mods) { real_mods = mods; }
103void clear_mods(void) { real_mods = 0; }
104
105/* weak modifier */
106uint8_t get_weak_mods(void) { return weak_mods; }
107void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
108void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
109void set_weak_mods(uint8_t mods) { weak_mods = mods; }
110void clear_weak_mods(void) { weak_mods = 0; }
111
112/* Oneshot modifier */
113#ifndef NO_ACTION_ONESHOT
114void set_oneshot_mods(uint8_t mods)
115{
116 oneshot_mods = mods;
117#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
118 oneshot_time = timer_read();
119#endif
120}
121void clear_oneshot_mods(void)
122{
123 oneshot_mods = 0;
124#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
125 oneshot_time = 0;
126#endif
127}
128#endif
129
130
131
132
133/*
134 * inspect keyboard state
135 */
136uint8_t has_anykey(void)
137{
138 uint8_t cnt = 0;
139 for (uint8_t i = 1; i < REPORT_SIZE; i++) {
140 if (keyboard_report->raw[i])
141 cnt++;
142 }
143 return cnt;
144}
145
146uint8_t has_anymod(void)
147{
148 return bitpop(real_mods);
149}
150
151uint8_t get_first_key(void)
152{
153#ifdef NKRO_ENABLE
154 if (keyboard_nkro) {
155 uint8_t i = 0;
156 for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
157 ;
158 return i<<3 | biton(keyboard_report->nkro.bits[i]);
159 }
160#endif
161 return keyboard_report->keys[0];
162}
163
164
165
166/* local functions */
167static inline void add_key_byte(uint8_t code)
168{
169 int8_t i = 0;
170 int8_t empty = -1;
171 for (; i < REPORT_KEYS; i++) {
172 if (keyboard_report->keys[i] == code) {
173 break;
174 }
175 if (empty == -1 && keyboard_report->keys[i] == 0) {
176 empty = i;
177 }
178 }
179 if (i == REPORT_KEYS) {
180 if (empty != -1) {
181 keyboard_report->keys[empty] = code;
182 }
183 }
184}
185
186static inline void del_key_byte(uint8_t code)
187{
188 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
189 if (keyboard_report->keys[i] == code) {
190 keyboard_report->keys[i] = 0;
191 }
192 }
193}
194
195#ifdef NKRO_ENABLE
196static inline void add_key_bit(uint8_t code)
197{
198 if ((code>>3) < REPORT_BITS) {
199 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
200 } else {
201 dprintf("add_key_bit: can't add: %02X\n", code);
202 }
203}
204
205static inline void del_key_bit(uint8_t code)
206{
207 if ((code>>3) < REPORT_BITS) {
208 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
209 } else {
210 dprintf("del_key_bit: can't del: %02X\n", code);
211 }
212}
213#endif