]> git.gir.st - tmk_keyboard.git/blob - pjrc/host.c
added copyright and license notice.
[tmk_keyboard.git] / pjrc / 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 "usb_keyboard.h"
22 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
23 #include "usb_mouse.h"
24 #endif
25 #ifdef USB_EXTRA_ENABLE
26 #include "usb_extra.h"
27 #endif
28 #include "debug.h"
29 #include "host.h"
30 #include "util.h"
31
32
33 #ifdef USB_NKRO_ENABLE
34 bool keyboard_nkro = false;
35 #endif
36
37 static report_keyboard_t report0;
38 static report_keyboard_t report1;
39 report_keyboard_t *keyboard_report = &report0;
40 report_keyboard_t *keyboard_report_prev = &report1;
41
42 static inline void add_key_byte(uint8_t code);
43 static inline void add_key_bit(uint8_t code);
44
45
46 uint8_t host_keyboard_leds(void)
47 {
48 return usb_keyboard_leds;
49 }
50
51 /* keyboard report operations */
52 void host_add_key(uint8_t key)
53 {
54 #ifdef USB_NKRO_ENABLE
55 if (keyboard_nkro) {
56 add_key_bit(key);
57 return;
58 }
59 #endif
60 add_key_byte(key);
61 }
62
63 void host_add_mod_bit(uint8_t mod)
64 {
65 keyboard_report->mods |= mod;
66 }
67
68 void host_set_mods(uint8_t mods)
69 {
70 keyboard_report->mods = mods;
71 }
72
73 void host_add_code(uint8_t code)
74 {
75 if (IS_MOD(code)) {
76 host_add_mod_bit(MOD_BIT(code));
77 } else {
78 host_add_key(code);
79 }
80 }
81
82 void host_swap_keyboard_report(void)
83 {
84 uint8_t sreg = SREG;
85 cli();
86 report_keyboard_t *tmp = keyboard_report_prev;
87 keyboard_report_prev = keyboard_report;
88 keyboard_report = tmp;
89 SREG = sreg;
90 }
91
92 void host_clear_keyboard_report(void)
93 {
94 keyboard_report->mods = 0;
95 for (int8_t i = 0; i < REPORT_KEYS; i++) {
96 keyboard_report->keys[i] = 0;
97 }
98 }
99
100 uint8_t host_has_anykey(void)
101 {
102 uint8_t cnt = 0;
103 for (int i = 0; i < REPORT_KEYS; i++) {
104 if (keyboard_report->keys[i])
105 cnt++;
106 }
107 return cnt;
108 }
109
110 uint8_t host_get_first_key(void)
111 {
112 #ifdef USB_NKRO_ENABLE
113 if (keyboard_nkro) {
114 uint8_t i = 0;
115 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
116 ;
117 return i<<3 | biton(keyboard_report->keys[i]);
118 }
119 #endif
120 return keyboard_report->keys[0];
121 }
122
123
124 void host_send_keyboard_report(void)
125 {
126 usb_keyboard_send_report(keyboard_report);
127 }
128
129 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
130 void host_mouse_send(report_mouse_t *report)
131 {
132 usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
133 }
134 #endif
135
136 #ifdef USB_EXTRA_ENABLE
137 void host_system_send(uint16_t data)
138 {
139 usb_extra_system_send(data);
140 }
141
142 void host_consumer_send(uint16_t data)
143 {
144 static uint16_t last_data = 0;
145 if (data == last_data) return;
146 last_data = data;
147
148 usb_extra_consumer_send(data);
149 }
150 #endif
151
152
153 static inline void add_key_byte(uint8_t code)
154 {
155 // TODO: fix ugly code
156 int8_t i = 0;
157 int8_t empty = -1;
158 for (; i < REPORT_KEYS; i++) {
159 if (keyboard_report_prev->keys[i] == code) {
160 keyboard_report->keys[i] = code;
161 break;
162 }
163 if (empty == -1 &&
164 keyboard_report_prev->keys[i] == 0 &&
165 keyboard_report->keys[i] == 0) {
166 empty = i;
167 }
168 }
169 if (i == REPORT_KEYS) {
170 if (empty != -1) {
171 keyboard_report->keys[empty] = code;
172 }
173 }
174 }
175
176 static inline void add_key_bit(uint8_t code)
177 {
178 if ((code>>3) < REPORT_KEYS) {
179 keyboard_report->keys[code>>3] |= 1<<(code&7);
180 } else {
181 debug("add_key_bit: can't add: "); phex(code); debug("\n");
182 }
183 }
Imprint / Impressum