]>
git.gir.st - tmk_keyboard.git/blob - common/action_util.c
2 Copyright 2013 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
20 #include "action_util.h"
23 static inline void add_key_byte(uint8_t code
);
24 static inline void del_key_byte(uint8_t code
);
26 static inline void add_key_bit(uint8_t code
);
27 static inline void del_key_bit(uint8_t code
);
30 static uint8_t real_mods
= 0;
31 static uint8_t weak_mods
= 0;
34 // TODO: pointer variable is not needed
35 //report_keyboard_t keyboard_report = {};
36 report_keyboard_t
*keyboard_report
= &(report_keyboard_t
){};
38 #ifndef NO_ACTION_ONESHOT
39 static int8_t oneshot_mods
= 0;
40 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
41 static int16_t oneshot_time
= 0;
46 void send_keyboard_report(void) {
47 keyboard_report
->mods
= real_mods
;
48 keyboard_report
->mods
|= weak_mods
;
49 #ifndef NO_ACTION_ONESHOT
51 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
52 if (TIMER_DIFF_16(timer_read(), oneshot_time
) >= ONESHOT_TIMEOUT
) {
53 dprintf("Oneshot: timeout\n");
57 keyboard_report
->mods
|= oneshot_mods
;
63 host_keyboard_send(keyboard_report
);
67 void add_key(uint8_t key
)
78 void del_key(uint8_t key
)
92 for (int8_t i
= 1; i
< REPORT_SIZE
; i
++) {
93 keyboard_report
->raw
[i
] = 0;
99 uint8_t get_mods(void) { return real_mods
; }
100 void add_mods(uint8_t mods
) { real_mods
|= mods
; }
101 void del_mods(uint8_t mods
) { real_mods
&= ~mods
; }
102 void set_mods(uint8_t mods
) { real_mods
= mods
; }
103 void clear_mods(void) { real_mods
= 0; }
106 uint8_t get_weak_mods(void) { return weak_mods
; }
107 void add_weak_mods(uint8_t mods
) { weak_mods
|= mods
; }
108 void del_weak_mods(uint8_t mods
) { weak_mods
&= ~mods
; }
109 void set_weak_mods(uint8_t mods
) { weak_mods
= mods
; }
110 void clear_weak_mods(void) { weak_mods
= 0; }
112 /* Oneshot modifier */
113 #ifndef NO_ACTION_ONESHOT
114 void set_oneshot_mods(uint8_t mods
)
117 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
118 oneshot_time
= timer_read();
121 void clear_oneshot_mods(void)
124 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
134 * inspect keyboard state
136 uint8_t has_anykey(void)
139 for (uint8_t i
= 1; i
< REPORT_SIZE
; i
++) {
140 if (keyboard_report
->raw
[i
])
146 uint8_t has_anymod(void)
148 return bitpop(real_mods
);
151 uint8_t get_first_key(void)
156 for (; i
< REPORT_BITS
&& !keyboard_report
->nkro
.bits
[i
]; i
++)
158 return i
<<3 | biton(keyboard_report
->nkro
.bits
[i
]);
161 return keyboard_report
->keys
[0];
166 /* local functions */
167 static inline void add_key_byte(uint8_t code
)
171 for (; i
< REPORT_KEYS
; i
++) {
172 if (keyboard_report
->keys
[i
] == code
) {
175 if (empty
== -1 && keyboard_report
->keys
[i
] == 0) {
179 if (i
== REPORT_KEYS
) {
181 keyboard_report
->keys
[empty
] = code
;
186 static inline void del_key_byte(uint8_t code
)
188 for (uint8_t i
= 0; i
< REPORT_KEYS
; i
++) {
189 if (keyboard_report
->keys
[i
] == code
) {
190 keyboard_report
->keys
[i
] = 0;
196 static inline void add_key_bit(uint8_t code
)
198 if ((code
>>3) < REPORT_BITS
) {
199 keyboard_report
->nkro
.bits
[code
>>3] |= 1<<(code
&7);
201 dprintf("add_key_bit: can't add: %02X\n", code
);
205 static inline void del_key_bit(uint8_t code
)
207 if ((code
>>3) < REPORT_BITS
) {
208 keyboard_report
->nkro
.bits
[code
>>3] &= ~(1<<(code
&7));
210 dprintf("del_key_bit: can't del: %02X\n", code
);