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"
22 static inline void add_key_byte(uint8_t code);
23 static inline void del_key_byte(uint8_t code);
25 static inline void add_key_bit(uint8_t code);
26 static inline void del_key_bit(uint8_t code);
29 static uint8_t real_mods = 0;
30 static uint8_t weak_mods = 0;
33 // TODO: pointer variable is not needed
34 //report_keyboard_t keyboard_report = {};
35 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
37 #ifndef NO_ACTION_ONESHOT
38 static bool oneshot_enabled = true;
39 static int8_t oneshot_mods = 0;
42 void send_keyboard_report(void) {
43 keyboard_report->mods = real_mods;
44 keyboard_report->mods |= weak_mods;
45 #ifndef NO_ACTION_ONESHOT
46 keyboard_report->mods |= oneshot_mods;
51 host_keyboard_send(keyboard_report);
55 void add_key(uint8_t key)
66 void del_key(uint8_t key)
80 for (int8_t i = 1; i < REPORT_SIZE; i++) {
81 keyboard_report->raw[i] = 0;
87 uint8_t get_mods(void) { return real_mods; }
88 void add_mods(uint8_t mods) { real_mods |= mods; }
89 void del_mods(uint8_t mods) { real_mods &= ~mods; }
90 void set_mods(uint8_t mods) { real_mods = mods; }
91 void clear_mods(void) { real_mods = 0; }
94 uint8_t get_weak_mods(void) { return weak_mods; }
95 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
96 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
97 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
98 void clear_weak_mods(void) { weak_mods = 0; }
100 /* Oneshot modifier */
101 #ifndef NO_ACTION_ONESHOT
102 void set_oneshot_mods(uint8_t mods) { oneshot_mods = mods; }
103 void clear_oneshot_mods(void) { oneshot_mods = 0; }
104 void oneshot_toggle(void) { oneshot_enabled = !oneshot_enabled; }
105 void oneshot_enable(void) { oneshot_enabled = true; }
106 void oneshot_disable(void) { oneshot_enabled = false; }
113 * inspect keyboard state
115 uint8_t has_anykey(void)
118 for (uint8_t i = 1; i < REPORT_SIZE; i++) {
119 if (keyboard_report->raw[i])
125 uint8_t has_anymod(void)
127 return bitpop(real_mods);
130 uint8_t get_first_key(void)
135 for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
137 return i<<3 | biton(keyboard_report->nkro.bits[i]);
140 return keyboard_report->keys[0];
145 /* local functions */
146 static inline void add_key_byte(uint8_t code)
150 for (; i < REPORT_KEYS; i++) {
151 if (keyboard_report->keys[i] == code) {
154 if (empty == -1 && keyboard_report->keys[i] == 0) {
158 if (i == REPORT_KEYS) {
160 keyboard_report->keys[empty] = code;
165 static inline void del_key_byte(uint8_t code)
167 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
168 if (keyboard_report->keys[i] == code) {
169 keyboard_report->keys[i] = 0;
175 static inline void add_key_bit(uint8_t code)
177 if ((code>>3) < REPORT_BITS) {
178 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
180 dprintf("add_key_bit: can't add: %02X\n", code);
184 static inline void del_key_bit(uint8_t code)
186 if ((code>>3) < REPORT_BITS) {
187 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
189 dprintf("del_key_bit: can't del: %02X\n", code);