aboutsummaryrefslogtreecommitdiff
path: root/pjrc/host.c
diff options
context:
space:
mode:
Diffstat (limited to 'pjrc/host.c')
-rw-r--r--pjrc/host.c183
1 files changed, 0 insertions, 183 deletions
diff --git a/pjrc/host.c b/pjrc/host.c
deleted file mode 100644
index fcf71d579..000000000
--- a/pjrc/host.c
+++ /dev/null
@@ -1,183 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 EXTRAKEY_ENABLE
26#include "usb_extra.h"
27#endif
28#include "debug.h"
29#include "host.h"
30#include "util.h"
31
32
33#ifdef NKRO_ENABLE
34bool keyboard_nkro = false;
35#endif
36
37static report_keyboard_t report0;
38static report_keyboard_t report1;
39report_keyboard_t *keyboard_report = &report0;
40report_keyboard_t *keyboard_report_prev = &report1;
41
42static inline void add_key_byte(uint8_t code);
43static inline void add_key_bit(uint8_t code);
44
45
46uint8_t host_keyboard_leds(void)
47{
48 return usb_keyboard_leds;
49}
50
51/* keyboard report operations */
52void host_add_key(uint8_t key)
53{
54#ifdef NKRO_ENABLE
55 if (keyboard_nkro) {
56 add_key_bit(key);
57 return;
58 }
59#endif
60 add_key_byte(key);
61}
62
63void host_add_mod_bit(uint8_t mod)
64{
65 keyboard_report->mods |= mod;
66}
67
68void host_set_mods(uint8_t mods)
69{
70 keyboard_report->mods = mods;
71}
72
73void 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
82void 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
92void 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
100uint8_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
110uint8_t host_get_first_key(void)
111{
112#ifdef 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
124void host_send_keyboard_report(void)
125{
126 usb_keyboard_send_report(keyboard_report);
127}
128
129#if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
130void 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 EXTRAKEY_ENABLE
137void host_system_send(uint16_t data)
138{
139 usb_extra_system_send(data);
140}
141
142void 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
153static 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
176static 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}