host.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include "keyboard.h"
  16. #include "keycode.h"
  17. #include "action.h"
  18. #include "host.h"
  19. #include "util.h"
  20. #include "debug.h"
  21. #include "usb_device_state.h"
  22. #ifdef DIGITIZER_ENABLE
  23. # include "digitizer.h"
  24. #endif
  25. #ifdef JOYSTICK_ENABLE
  26. # include "joystick.h"
  27. #endif
  28. #ifdef CONNECTION_ENABLE
  29. # include "connection.h"
  30. #endif
  31. #ifdef BLUETOOTH_ENABLE
  32. # include "bluetooth.h"
  33. static void bluetooth_send_extra(report_extra_t *report) {
  34. switch (report->report_id) {
  35. case REPORT_ID_SYSTEM:
  36. bluetooth_send_system(report->usage);
  37. return;
  38. case REPORT_ID_CONSUMER:
  39. bluetooth_send_consumer(report->usage);
  40. return;
  41. }
  42. }
  43. host_driver_t bt_driver = {
  44. .keyboard_leds = bluetooth_keyboard_leds,
  45. .send_keyboard = bluetooth_send_keyboard,
  46. .send_nkro = bluetooth_send_nkro,
  47. .send_mouse = bluetooth_send_mouse,
  48. .send_extra = bluetooth_send_extra,
  49. # ifdef RAW_ENABLE
  50. .send_raw_hid = bluetooth_send_raw_hid,
  51. # endif
  52. };
  53. #endif
  54. #ifdef NKRO_ENABLE
  55. # include "keycode_config.h"
  56. extern keymap_config_t keymap_config;
  57. #endif
  58. static host_driver_t *driver;
  59. static uint16_t last_system_usage = 0;
  60. static uint16_t last_consumer_usage = 0;
  61. void host_set_driver(host_driver_t *d) {
  62. driver = d;
  63. }
  64. host_driver_t *host_get_driver(void) {
  65. return driver;
  66. }
  67. #ifdef CONNECTION_ENABLE
  68. static connection_host_t active_host = CONNECTION_HOST_NONE;
  69. __attribute__((weak)) void host_disconnect_active_driver_user(connection_host_t host) {}
  70. __attribute__((weak)) void host_disconnect_active_driver_kb(connection_host_t host) {}
  71. __attribute__((weak)) void host_connect_active_driver_user(connection_host_t host) {}
  72. __attribute__((weak)) void host_connect_active_driver_kb(connection_host_t host) {}
  73. // TODO: Additionally have host_driver_t handle swap
  74. static void host_update_active_driver(connection_host_t current, connection_host_t next) {
  75. host_disconnect_active_driver_user(current);
  76. host_disconnect_active_driver_kb(current);
  77. if (current != CONNECTION_HOST_NONE) {
  78. clear_keyboard();
  79. }
  80. host_connect_active_driver_user(next);
  81. host_connect_active_driver_kb(next);
  82. }
  83. #endif
  84. void host_init(void) {
  85. // currently do nothing
  86. }
  87. void host_task(void) {
  88. #ifdef CONNECTION_ENABLE
  89. connection_host_t next_host = connection_get_host();
  90. if (next_host != active_host) {
  91. host_update_active_driver(active_host, next_host);
  92. active_host = next_host;
  93. }
  94. #endif
  95. }
  96. static host_driver_t *host_get_active_driver(void) {
  97. #ifdef CONNECTION_ENABLE
  98. switch (active_host) {
  99. # ifdef BLUETOOTH_ENABLE
  100. case CONNECTION_HOST_BLUETOOTH:
  101. return &bt_driver;
  102. # endif
  103. case CONNECTION_HOST_NONE:
  104. return NULL;
  105. default:
  106. break;
  107. }
  108. #endif
  109. return driver;
  110. }
  111. bool host_can_send_nkro(void) {
  112. #ifdef CONNECTION_ENABLE
  113. switch (active_host) {
  114. # ifdef BLUETOOTH_ENABLE
  115. case CONNECTION_HOST_BLUETOOTH:
  116. return bluetooth_can_send_nkro();
  117. # endif
  118. case CONNECTION_HOST_NONE:
  119. return false;
  120. default:
  121. break;
  122. }
  123. #endif
  124. return usb_device_state_get_protocol() == USB_PROTOCOL_REPORT;
  125. }
  126. #ifdef SPLIT_KEYBOARD
  127. uint8_t split_led_state = 0;
  128. void set_split_host_keyboard_leds(uint8_t led_state) {
  129. split_led_state = led_state;
  130. }
  131. #endif
  132. uint8_t host_keyboard_leds(void) {
  133. #ifdef SPLIT_KEYBOARD
  134. if (!is_keyboard_master()) return split_led_state;
  135. #endif
  136. host_driver_t *driver = host_get_active_driver();
  137. if (!driver || !driver->keyboard_leds) return 0;
  138. return (*driver->keyboard_leds)();
  139. }
  140. led_t host_keyboard_led_state(void) {
  141. return (led_t)host_keyboard_leds();
  142. }
  143. /* send report */
  144. void host_keyboard_send(report_keyboard_t *report) {
  145. host_driver_t *driver = host_get_active_driver();
  146. if (!driver || !driver->send_keyboard) return;
  147. #ifdef KEYBOARD_SHARED_EP
  148. report->report_id = REPORT_ID_KEYBOARD;
  149. #endif
  150. (*driver->send_keyboard)(report);
  151. if (debug_keyboard) {
  152. dprintf("keyboard_report: %02X | ", report->mods);
  153. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  154. dprintf("%02X ", report->keys[i]);
  155. }
  156. dprint("\n");
  157. }
  158. }
  159. void host_nkro_send(report_nkro_t *report) {
  160. host_driver_t *driver = host_get_active_driver();
  161. if (!driver || !driver->send_nkro) return;
  162. report->report_id = REPORT_ID_NKRO;
  163. (*driver->send_nkro)(report);
  164. if (debug_keyboard) {
  165. dprintf("nkro_report: %02X | ", report->mods);
  166. for (uint8_t i = 0; i < NKRO_REPORT_BITS; i++) {
  167. dprintf("%02X ", report->bits[i]);
  168. }
  169. dprint("\n");
  170. }
  171. }
  172. void host_mouse_send(report_mouse_t *report) {
  173. host_driver_t *driver = host_get_active_driver();
  174. if (!driver || !driver->send_mouse) return;
  175. #ifdef MOUSE_SHARED_EP
  176. report->report_id = REPORT_ID_MOUSE;
  177. #endif
  178. #ifdef MOUSE_EXTENDED_REPORT
  179. // clip and copy to Boot protocol XY
  180. report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
  181. report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
  182. #endif
  183. (*driver->send_mouse)(report);
  184. }
  185. void host_system_send(uint16_t usage) {
  186. if (usage == last_system_usage) return;
  187. last_system_usage = usage;
  188. host_driver_t *driver = host_get_active_driver();
  189. if (!driver || !driver->send_extra) return;
  190. report_extra_t report = {
  191. .report_id = REPORT_ID_SYSTEM,
  192. .usage = usage,
  193. };
  194. (*driver->send_extra)(&report);
  195. }
  196. void host_consumer_send(uint16_t usage) {
  197. if (usage == last_consumer_usage) return;
  198. last_consumer_usage = usage;
  199. host_driver_t *driver = host_get_active_driver();
  200. if (!driver || !driver->send_extra) return;
  201. report_extra_t report = {
  202. .report_id = REPORT_ID_CONSUMER,
  203. .usage = usage,
  204. };
  205. (*driver->send_extra)(&report);
  206. }
  207. #ifdef JOYSTICK_ENABLE
  208. void host_joystick_send(joystick_t *joystick) {
  209. if (!driver) return;
  210. report_joystick_t report = {
  211. # ifdef JOYSTICK_SHARED_EP
  212. .report_id = REPORT_ID_JOYSTICK,
  213. # endif
  214. # if JOYSTICK_AXIS_COUNT > 0
  215. .axes =
  216. {
  217. joystick->axes[0],
  218. # if JOYSTICK_AXIS_COUNT >= 2
  219. joystick->axes[1],
  220. # endif
  221. # if JOYSTICK_AXIS_COUNT >= 3
  222. joystick->axes[2],
  223. # endif
  224. # if JOYSTICK_AXIS_COUNT >= 4
  225. joystick->axes[3],
  226. # endif
  227. # if JOYSTICK_AXIS_COUNT >= 5
  228. joystick->axes[4],
  229. # endif
  230. # if JOYSTICK_AXIS_COUNT >= 6
  231. joystick->axes[5],
  232. # endif
  233. },
  234. # endif
  235. # ifdef JOYSTICK_HAS_HAT
  236. .hat = joystick->hat,
  237. # endif
  238. # if JOYSTICK_BUTTON_COUNT > 0
  239. .buttons =
  240. {
  241. joystick->buttons[0],
  242. # if JOYSTICK_BUTTON_COUNT > 8
  243. joystick->buttons[1],
  244. # endif
  245. # if JOYSTICK_BUTTON_COUNT > 16
  246. joystick->buttons[2],
  247. # endif
  248. # if JOYSTICK_BUTTON_COUNT > 24
  249. joystick->buttons[3],
  250. # endif
  251. },
  252. # endif
  253. };
  254. send_joystick(&report);
  255. }
  256. #endif
  257. __attribute__((weak)) void send_joystick(report_joystick_t *report) {}
  258. #ifdef DIGITIZER_ENABLE
  259. void host_digitizer_send(digitizer_t *digitizer) {
  260. report_digitizer_t report = {
  261. # ifdef DIGITIZER_SHARED_EP
  262. .report_id = REPORT_ID_DIGITIZER,
  263. # endif
  264. .in_range = digitizer->in_range,
  265. .tip = digitizer->tip,
  266. .barrel = digitizer->barrel,
  267. .x = (uint16_t)(digitizer->x * 0x7FFF),
  268. .y = (uint16_t)(digitizer->y * 0x7FFF),
  269. };
  270. send_digitizer(&report);
  271. }
  272. #endif
  273. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  274. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  275. void host_programmable_button_send(uint32_t data) {
  276. report_programmable_button_t report = {
  277. .report_id = REPORT_ID_PROGRAMMABLE_BUTTON,
  278. .usage = data,
  279. };
  280. send_programmable_button(&report);
  281. }
  282. #endif
  283. __attribute__((weak)) void send_programmable_button(report_programmable_button_t *report) {}
  284. #ifdef PLOVER_HID_ENABLE
  285. void host_plover_hid_send(report_plover_hid_t *report) {
  286. send_plover_hid(report);
  287. }
  288. #endif
  289. __attribute__((weak)) void send_plover_hid(report_plover_hid_t *report) {}
  290. #ifdef RAW_ENABLE
  291. void host_raw_hid_send(uint8_t *data, uint8_t length) {
  292. host_driver_t *driver = host_get_active_driver();
  293. if (!driver || !driver->send_raw_hid) return;
  294. (*driver->send_raw_hid)(data, length);
  295. }
  296. #endif
  297. uint16_t host_last_system_usage(void) {
  298. return last_system_usage;
  299. }
  300. uint16_t host_last_consumer_usage(void) {
  301. return last_consumer_usage;
  302. }