]> git.gir.st - tmk_keyboard.git/blob - common/host.c
Merge branch 'keymap2'
[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 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
31 report_mouse_t mouse_report = {};
32
33
34 static host_driver_t *driver;
35 static uint16_t last_system_report = 0;
36 static uint16_t last_consumer_report = 0;
37
38 static inline void add_key_byte(uint8_t code);
39 static inline void del_key_byte(uint8_t code);
40 static inline void add_key_bit(uint8_t code);
41 static inline void del_key_bit(uint8_t code);
42
43
44 void host_set_driver(host_driver_t *d)
45 {
46 driver = d;
47 }
48
49 host_driver_t *host_get_driver(void)
50 {
51 return driver;
52 }
53
54 uint8_t host_keyboard_leds(void)
55 {
56 if (!driver) return 0;
57 return (*driver->keyboard_leds)();
58 }
59 /* send report */
60 void host_keyboard_send(report_keyboard_t *report)
61 {
62 if (!driver) return;
63 (*driver->send_keyboard)(report);
64
65 if (debug_keyboard) {
66 print("keys: ");
67 for (int i = 0; i < REPORT_KEYS; i++) {
68 phex(keyboard_report->keys[i]); print(" ");
69 }
70 print(" mods: "); phex(keyboard_report->mods); print("\n");
71 }
72 }
73
74 void host_mouse_send(report_mouse_t *report)
75 {
76 if (!driver) return;
77 (*driver->send_mouse)(report);
78 }
79
80 void host_system_send(uint16_t report)
81 {
82 if (report == last_system_report) return;
83 last_system_report = report;
84
85 if (!driver) return;
86 (*driver->send_system)(report);
87 }
88
89 void host_consumer_send(uint16_t report)
90 {
91 if (report == last_consumer_report) return;
92 last_consumer_report = report;
93
94 if (!driver) return;
95 (*driver->send_consumer)(report);
96 }
97
98
99
100 /* keyboard report utils */
101 void host_add_key(uint8_t key)
102 {
103 #ifdef NKRO_ENABLE
104 if (keyboard_nkro) {
105 add_key_bit(key);
106 return;
107 }
108 #endif
109 add_key_byte(key);
110 }
111
112 void host_del_key(uint8_t key)
113 {
114 #ifdef NKRO_ENABLE
115 if (keyboard_nkro) {
116 del_key_bit(key);
117 return;
118 }
119 #endif
120 del_key_byte(key);
121 }
122
123 void host_clear_keys(void)
124 {
125 for (int8_t i = 0; i < REPORT_KEYS; i++) {
126 keyboard_report->keys[i] = 0;
127 }
128 }
129
130 uint8_t host_get_mods(void)
131 {
132 return keyboard_report->mods;
133 }
134
135 void host_add_mods(uint8_t mods)
136 {
137 keyboard_report->mods |= mods;
138 }
139
140 void host_del_mods(uint8_t mods)
141 {
142 keyboard_report->mods &= ~mods;
143 }
144
145 void host_set_mods(uint8_t mods)
146 {
147 keyboard_report->mods = mods;
148 }
149
150 void host_clear_mods(void)
151 {
152 keyboard_report->mods = 0;
153 }
154
155 uint8_t host_has_anykey(void)
156 {
157 uint8_t cnt = 0;
158 for (int i = 0; i < REPORT_KEYS; i++) {
159 if (keyboard_report->keys[i])
160 cnt++;
161 }
162 return cnt;
163 }
164
165 uint8_t host_has_anymod(void)
166 {
167 return bitpop(keyboard_report->mods);
168 }
169
170 uint8_t host_get_first_key(void)
171 {
172 #ifdef NKRO_ENABLE
173 if (keyboard_nkro) {
174 uint8_t i = 0;
175 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
176 ;
177 return i<<3 | biton(keyboard_report->keys[i]);
178 }
179 #endif
180 return keyboard_report->keys[0];
181 }
182
183 void host_send_keyboard_report(void)
184 {
185 if (!driver) return;
186 host_keyboard_send(keyboard_report);
187 }
188
189 uint8_t host_mouse_in_use(void)
190 {
191 return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
192 }
193
194 uint16_t host_last_sysytem_report(void)
195 {
196 return last_system_report;
197 }
198
199 uint16_t host_last_consumer_report(void)
200 {
201 return last_consumer_report;
202 }
203
204 static inline void add_key_byte(uint8_t code)
205 {
206 int8_t i = 0;
207 int8_t empty = -1;
208 for (; i < REPORT_KEYS; i++) {
209 if (keyboard_report->keys[i] == code) {
210 break;
211 }
212 if (empty == -1 && keyboard_report->keys[i] == 0) {
213 empty = i;
214 }
215 }
216 if (i == REPORT_KEYS) {
217 if (empty != -1) {
218 keyboard_report->keys[empty] = code;
219 }
220 }
221 }
222
223 static inline void del_key_byte(uint8_t code)
224 {
225 int i = 0;
226 for (; i < REPORT_KEYS; i++) {
227 if (keyboard_report->keys[i] == code) {
228 keyboard_report->keys[i] = 0;
229 }
230 }
231 }
232
233 static inline void add_key_bit(uint8_t code)
234 {
235 if ((code>>3) < REPORT_KEYS) {
236 keyboard_report->keys[code>>3] |= 1<<(code&7);
237 } else {
238 debug("add_key_bit: can't add: "); phex(code); debug("\n");
239 }
240 }
241
242 static inline void del_key_bit(uint8_t code)
243 {
244 if ((code>>3) < REPORT_KEYS) {
245 keyboard_report->keys[code>>3] &= ~(1<<(code&7));
246 } else {
247 debug("del_key_bit: can't del: "); phex(code); debug("\n");
248 }
249 }
Imprint / Impressum