aboutsummaryrefslogtreecommitdiff
path: root/common/host.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2012-10-09 16:50:14 +0900
committertmk <nobody@nowhere>2012-10-17 15:55:37 +0900
commit71ac82337f803e8ec0c081b3221ac0ccf61035b0 (patch)
tree5692c88c0b16d7f4992321c19a82f5193ebe4dc2 /common/host.c
parent373ab0e7192811944786c095facb80938c33f1d5 (diff)
downloadqmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.tar.gz
qmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.zip
Clean host.h interface.
Diffstat (limited to 'common/host.c')
-rw-r--r--common/host.c85
1 files changed, 20 insertions, 65 deletions
diff --git a/common/host.c b/common/host.c
index 37f707d0b..a671c97d3 100644
--- a/common/host.c
+++ b/common/host.c
@@ -1,5 +1,5 @@
1/* 1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com> 2Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
3 3
4This program is free software: you can redistribute it and/or modify 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 5it under the terms of the GNU General Public License as published by
@@ -28,10 +28,7 @@ bool keyboard_nkro = false;
28#endif 28#endif
29 29
30static host_driver_t *driver; 30static host_driver_t *driver;
31static report_keyboard_t report0; 31report_keyboard_t *keyboard_report = &(report_keyboard_t){};
32static report_keyboard_t report1;
33report_keyboard_t *keyboard_report = &report0;
34report_keyboard_t *keyboard_report_prev = &report1;
35 32
36 33
37static inline void add_key_byte(uint8_t code); 34static inline void add_key_byte(uint8_t code);
@@ -56,27 +53,6 @@ uint8_t host_keyboard_leds(void)
56 return (*driver->keyboard_leds)(); 53 return (*driver->keyboard_leds)();
57} 54}
58 55
59/* new interface */
60void host_register_key(uint8_t key)
61{
62 host_add_key(key);
63 host_send_keyboard_report();
64}
65
66void host_unregister_key(uint8_t key)
67{
68 host_del_key(key);
69 host_send_keyboard_report();
70}
71
72void host_clear_all_keys_but_mods(void)
73{
74 for (int8_t i = 0; i < REPORT_KEYS; i++) {
75 keyboard_report->keys[i] = 0;
76 }
77 host_send_keyboard_report();
78}
79
80/* keyboard report operations */ 56/* keyboard report operations */
81void host_add_key(uint8_t key) 57void host_add_key(uint8_t key)
82{ 58{
@@ -100,6 +76,13 @@ void host_del_key(uint8_t key)
100 del_key_byte(key); 76 del_key_byte(key);
101} 77}
102 78
79void host_clear_keys(void)
80{
81 for (int8_t i = 0; i < REPORT_KEYS; i++) {
82 keyboard_report->keys[i] = 0;
83 }
84}
85
103void host_add_mod_bit(uint8_t mod) 86void host_add_mod_bit(uint8_t mod)
104{ 87{
105 keyboard_report->mods |= mod; 88 keyboard_report->mods |= mod;
@@ -115,40 +98,9 @@ void host_set_mods(uint8_t mods)
115 keyboard_report->mods = mods; 98 keyboard_report->mods = mods;
116} 99}
117 100
118void host_add_code(uint8_t code) 101void host_clear_mods(void)
119{
120 if (IS_MOD(code)) {
121 host_add_mod_bit(MOD_BIT(code));
122 } else {
123 host_add_key(code);
124 }
125}
126
127void host_del_code(uint8_t code)
128{
129 if (IS_MOD(code)) {
130 host_del_mod_bit(MOD_BIT(code));
131 } else {
132 host_del_key(code);
133 }
134}
135
136void host_swap_keyboard_report(void)
137{
138 uint8_t sreg = SREG;
139 cli();
140 report_keyboard_t *tmp = keyboard_report_prev;
141 keyboard_report_prev = keyboard_report;
142 keyboard_report = tmp;
143 SREG = sreg;
144}
145
146void host_clear_keyboard_report(void)
147{ 102{
148 keyboard_report->mods = 0; 103 keyboard_report->mods = 0;
149 for (int8_t i = 0; i < REPORT_KEYS; i++) {
150 keyboard_report->keys[i] = 0;
151 }
152} 104}
153 105
154uint8_t host_has_anykey(void) 106uint8_t host_has_anykey(void)
@@ -174,7 +126,6 @@ uint8_t host_get_first_key(void)
174 return keyboard_report->keys[0]; 126 return keyboard_report->keys[0];
175} 127}
176 128
177
178void host_send_keyboard_report(void) 129void host_send_keyboard_report(void)
179{ 130{
180 if (!driver) return; 131 if (!driver) return;
@@ -189,6 +140,14 @@ void host_send_keyboard_report(void)
189 } 140 }
190} 141}
191 142
143
144/* send report */
145void host_keyboard_send(report_keyboard_t *report)
146{
147 if (!driver) return;
148 (*driver->send_keyboard)(report);
149}
150
192void host_mouse_send(report_mouse_t *report) 151void host_mouse_send(report_mouse_t *report)
193{ 152{
194 if (!driver) return; 153 if (!driver) return;
@@ -218,17 +177,13 @@ void host_consumer_send(uint16_t data)
218 177
219static inline void add_key_byte(uint8_t code) 178static inline void add_key_byte(uint8_t code)
220{ 179{
221 // TODO: fix ugly code
222 int8_t i = 0; 180 int8_t i = 0;
223 int8_t empty = -1; 181 int8_t empty = -1;
224 for (; i < REPORT_KEYS; i++) { 182 for (; i < REPORT_KEYS; i++) {
225 if (keyboard_report_prev->keys[i] == code) { 183 if (keyboard_report->keys[i] == code) {
226 keyboard_report->keys[i] = code;
227 break; 184 break;
228 } 185 }
229 if (empty == -1 && 186 if (empty == -1 && keyboard_report->keys[i] == 0) {
230 keyboard_report_prev->keys[i] == 0 &&
231 keyboard_report->keys[i] == 0) {
232 empty = i; 187 empty = i;
233 } 188 }
234 } 189 }