]> git.gir.st - tmk_keyboard.git/blob - host.c
fixed a bug on host_system_send().
[tmk_keyboard.git] / host.c
1 /*
2 Copyright 2011 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 "usb_keycodes.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 static report_keyboard_t report0;
32 static report_keyboard_t report1;
33 report_keyboard_t *keyboard_report = &report0;
34 report_keyboard_t *keyboard_report_prev = &report1;
35
36
37 static inline void add_key_byte(uint8_t code);
38 static inline void add_key_bit(uint8_t code);
39
40
41 void host_set_driver(host_driver_t *d)
42 {
43 driver = d;
44 }
45
46 host_driver_t *host_get_driver(void)
47 {
48 return driver;
49 }
50
51 uint8_t host_keyboard_leds(void)
52 {
53 if (!driver) return 0;
54 return (*driver->keyboard_leds)();
55 }
56
57 /* keyboard report operations */
58 void host_add_key(uint8_t key)
59 {
60 #ifdef NKRO_ENABLE
61 if (keyboard_nkro) {
62 add_key_bit(key);
63 return;
64 }
65 #endif
66 add_key_byte(key);
67 }
68
69 void host_add_mod_bit(uint8_t mod)
70 {
71 keyboard_report->mods |= mod;
72 }
73
74 void host_set_mods(uint8_t mods)
75 {
76 keyboard_report->mods = mods;
77 }
78
79 void host_add_code(uint8_t code)
80 {
81 if (IS_MOD(code)) {
82 host_add_mod_bit(MOD_BIT(code));
83 } else {
84 host_add_key(code);
85 }
86 }
87
88 void host_swap_keyboard_report(void)
89 {
90 uint8_t sreg = SREG;
91 cli();
92 report_keyboard_t *tmp = keyboard_report_prev;
93 keyboard_report_prev = keyboard_report;
94 keyboard_report = tmp;
95 SREG = sreg;
96 }
97
98 void host_clear_keyboard_report(void)
99 {
100 keyboard_report->mods = 0;
101 for (int8_t i = 0; i < REPORT_KEYS; i++) {
102 keyboard_report->keys[i] = 0;
103 }
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
130 void host_send_keyboard_report(void)
131 {
132 if (!driver) return;
133 (*driver->send_keyboard)(keyboard_report);
134 }
135
136 void host_mouse_send(report_mouse_t *report)
137 {
138 if (!driver) return;
139 (*driver->send_mouse)(report);
140 }
141
142 void host_system_send(uint16_t data)
143 {
144 if (!driver) return;
145 (*driver->send_system)(data);
146 }
147
148 void host_consumer_send(uint16_t data)
149 {
150 // TODO: this is needed?
151 static uint16_t last_data = 0;
152 if (data == last_data) return;
153 last_data = data;
154
155 if (!driver) return;
156 (*driver->send_consumer)(data);
157 }
158
159
160 static inline void add_key_byte(uint8_t code)
161 {
162 // TODO: fix ugly code
163 int8_t i = 0;
164 int8_t empty = -1;
165 for (; i < REPORT_KEYS; i++) {
166 if (keyboard_report_prev->keys[i] == code) {
167 keyboard_report->keys[i] = code;
168 break;
169 }
170 if (empty == -1 &&
171 keyboard_report_prev->keys[i] == 0 &&
172 keyboard_report->keys[i] == 0) {
173 empty = i;
174 }
175 }
176 if (i == REPORT_KEYS) {
177 if (empty != -1) {
178 keyboard_report->keys[empty] = code;
179 }
180 }
181 }
182
183 static inline void add_key_bit(uint8_t code)
184 {
185 if ((code>>3) < REPORT_KEYS) {
186 keyboard_report->keys[code>>3] |= 1<<(code&7);
187 } else {
188 debug("add_key_bit: can't add: "); phex(code); debug("\n");
189 }
190 }
Imprint / Impressum