]> git.gir.st - tmk_keyboard.git/blob - common/host.c
Clean host.h interface.
[tmk_keyboard.git] / common / host.c
1 /*
2 Copyright 2011,2012 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
18 #include <stdint.h>
19 #include <avr/interrupt.h>
20 #include "keycode.h"
21 #include "host.h"
22 #include "util.h"
23 #include "debug.h"
24
25
26 #ifdef NKRO_ENABLE
27 bool keyboard_nkro = false;
28 #endif
29
30 static host_driver_t *driver;
31 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
32
33
34 static inline void add_key_byte(uint8_t code);
35 static inline void del_key_byte(uint8_t code);
36 static inline void add_key_bit(uint8_t code);
37 static inline void del_key_bit(uint8_t code);
38
39
40 void host_set_driver(host_driver_t *d)
41 {
42 driver = d;
43 }
44
45 host_driver_t *host_get_driver(void)
46 {
47 return driver;
48 }
49
50 uint8_t host_keyboard_leds(void)
51 {
52 if (!driver) return 0;
53 return (*driver->keyboard_leds)();
54 }
55
56 /* keyboard report operations */
57 void host_add_key(uint8_t key)
58 {
59 #ifdef NKRO_ENABLE
60 if (keyboard_nkro) {
61 add_key_bit(key);
62 return;
63 }
64 #endif
65 add_key_byte(key);
66 }
67
68 void host_del_key(uint8_t key)
69 {
70 #ifdef NKRO_ENABLE
71 if (keyboard_nkro) {
72 del_key_bit(key);
73 return;
74 }
75 #endif
76 del_key_byte(key);
77 }
78
79 void host_clear_keys(void)
80 {
81 for (int8_t i = 0; i < REPORT_KEYS; i++) {
82 keyboard_report->keys[i] = 0;
83 }
84 }
85
86 void host_add_mod_bit(uint8_t mod)
87 {
88 keyboard_report->mods |= mod;
89 }
90
91 void host_del_mod_bit(uint8_t mod)
92 {
93 keyboard_report->mods &= ~mod;
94 }
95
96 void host_set_mods(uint8_t mods)
97 {
98 keyboard_report->mods = mods;
99 }
100
101 void host_clear_mods(void)
102 {
103 keyboard_report->mods = 0;
104 }
105
106 uint8_t host_has_anykey(void)
107 {
108 uint8_t cnt = 0;
109 for (int i = 0; i < REPORT_KEYS; i++) {
110 if (keyboard_report->keys[i])
111 cnt++;
112 }
113 return cnt;
114 }
115
116 uint8_t host_get_first_key(void)
117 {
118 #ifdef NKRO_ENABLE
119 if (keyboard_nkro) {
120 uint8_t i = 0;
121 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
122 ;
123 return i<<3 | biton(keyboard_report->keys[i]);
124 }
125 #endif
126 return keyboard_report->keys[0];
127 }
128
129 void host_send_keyboard_report(void)
130 {
131 if (!driver) return;
132 (*driver->send_keyboard)(keyboard_report);
133
134 if (debug_keyboard) {
135 print("keys: ");
136 for (int i = 0; i < REPORT_KEYS; i++) {
137 phex(keyboard_report->keys[i]); print(" ");
138 }
139 print(" mods: "); phex(keyboard_report->mods); print("\n");
140 }
141 }
142
143
144 /* send report */
145 void host_keyboard_send(report_keyboard_t *report)
146 {
147 if (!driver) return;
148 (*driver->send_keyboard)(report);
149 }
150
151 void host_mouse_send(report_mouse_t *report)
152 {
153 if (!driver) return;
154 (*driver->send_mouse)(report);
155 }
156
157 void host_system_send(uint16_t data)
158 {
159 static uint16_t last_data = 0;
160 if (data == last_data) return;
161 last_data = data;
162
163 if (!driver) return;
164 (*driver->send_system)(data);
165 }
166
167 void host_consumer_send(uint16_t data)
168 {
169 static uint16_t last_data = 0;
170 if (data == last_data) return;
171 last_data = data;
172
173 if (!driver) return;
174 (*driver->send_consumer)(data);
175 }
176
177
178 static inline void add_key_byte(uint8_t code)
179 {
180 int8_t i = 0;
181 int8_t empty = -1;
182 for (; i < REPORT_KEYS; i++) {
183 if (keyboard_report->keys[i] == code) {
184 break;
185 }
186 if (empty == -1 && keyboard_report->keys[i] == 0) {
187 empty = i;
188 }
189 }
190 if (i == REPORT_KEYS) {
191 if (empty != -1) {
192 keyboard_report->keys[empty] = code;
193 }
194 }
195 }
196
197 static inline void del_key_byte(uint8_t code)
198 {
199 int i = 0;
200 for (; i < REPORT_KEYS; i++) {
201 if (keyboard_report->keys[i] == code) {
202 keyboard_report->keys[i] = 0;
203 }
204 }
205 }
206
207 static inline void add_key_bit(uint8_t code)
208 {
209 if ((code>>3) < REPORT_KEYS) {
210 keyboard_report->keys[code>>3] |= 1<<(code&7);
211 } else {
212 debug("add_key_bit: can't add: "); phex(code); debug("\n");
213 }
214 }
215
216 static inline void del_key_bit(uint8_t code)
217 {
218 if ((code>>3) < REPORT_KEYS) {
219 keyboard_report->keys[code>>3] &= ~(1<<(code&7));
220 } else {
221 debug("del_key_bit: can't del: "); phex(code); debug("\n");
222 }
223 }
Imprint / Impressum