host.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "host.h"
  18. #include "util.h"
  19. #include "debug.h"
  20. #ifdef DIGITIZER_ENABLE
  21. # include "digitizer.h"
  22. #endif
  23. #ifdef JOYSTICK_ENABLE
  24. # include "joystick.h"
  25. #endif
  26. #ifdef BLUETOOTH_ENABLE
  27. # include "bluetooth.h"
  28. # include "outputselect.h"
  29. #endif
  30. #ifdef NKRO_ENABLE
  31. # include "keycode_config.h"
  32. extern keymap_config_t keymap_config;
  33. #endif
  34. static host_driver_t *driver;
  35. static uint16_t last_system_usage = 0;
  36. static uint16_t last_consumer_usage = 0;
  37. void host_set_driver(host_driver_t *d) {
  38. driver = d;
  39. }
  40. host_driver_t *host_get_driver(void) {
  41. return driver;
  42. }
  43. #ifdef SPLIT_KEYBOARD
  44. uint8_t split_led_state = 0;
  45. void set_split_host_keyboard_leds(uint8_t led_state) {
  46. split_led_state = led_state;
  47. }
  48. #endif
  49. uint8_t host_keyboard_leds(void) {
  50. #ifdef SPLIT_KEYBOARD
  51. if (!is_keyboard_master()) return split_led_state;
  52. #endif
  53. if (!driver) return 0;
  54. return (*driver->keyboard_leds)();
  55. }
  56. led_t host_keyboard_led_state(void) {
  57. return (led_t)host_keyboard_leds();
  58. }
  59. /* send report */
  60. void host_keyboard_send(report_keyboard_t *report) {
  61. #ifdef BLUETOOTH_ENABLE
  62. if (where_to_send() == OUTPUT_BLUETOOTH) {
  63. bluetooth_send_keyboard(report);
  64. return;
  65. }
  66. #endif
  67. if (!driver) return;
  68. #ifdef KEYBOARD_SHARED_EP
  69. report->report_id = REPORT_ID_KEYBOARD;
  70. #endif
  71. (*driver->send_keyboard)(report);
  72. if (debug_keyboard) {
  73. dprintf("keyboard_report: %02X | ", report->mods);
  74. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  75. dprintf("%02X ", report->keys[i]);
  76. }
  77. dprint("\n");
  78. }
  79. }
  80. void host_nkro_send(report_nkro_t *report) {
  81. if (!driver) return;
  82. report->report_id = REPORT_ID_NKRO;
  83. (*driver->send_nkro)(report);
  84. if (debug_keyboard) {
  85. dprintf("nkro_report: %02X | ", report->mods);
  86. for (uint8_t i = 0; i < NKRO_REPORT_BITS; i++) {
  87. dprintf("%02X ", report->bits[i]);
  88. }
  89. dprint("\n");
  90. }
  91. }
  92. void host_mouse_send(report_mouse_t *report) {
  93. #ifdef BLUETOOTH_ENABLE
  94. if (where_to_send() == OUTPUT_BLUETOOTH) {
  95. bluetooth_send_mouse(report);
  96. return;
  97. }
  98. #endif
  99. if (!driver) return;
  100. #ifdef MOUSE_SHARED_EP
  101. report->report_id = REPORT_ID_MOUSE;
  102. #endif
  103. #ifdef MOUSE_EXTENDED_REPORT
  104. // clip and copy to Boot protocol XY
  105. report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
  106. report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
  107. #endif
  108. (*driver->send_mouse)(report);
  109. }
  110. void host_system_send(uint16_t usage) {
  111. if (usage == last_system_usage) return;
  112. last_system_usage = usage;
  113. if (!driver) return;
  114. report_extra_t report = {
  115. .report_id = REPORT_ID_SYSTEM,
  116. .usage = usage,
  117. };
  118. (*driver->send_extra)(&report);
  119. }
  120. void host_consumer_send(uint16_t usage) {
  121. if (usage == last_consumer_usage) return;
  122. last_consumer_usage = usage;
  123. #ifdef BLUETOOTH_ENABLE
  124. if (where_to_send() == OUTPUT_BLUETOOTH) {
  125. bluetooth_send_consumer(usage);
  126. return;
  127. }
  128. #endif
  129. if (!driver) return;
  130. report_extra_t report = {
  131. .report_id = REPORT_ID_CONSUMER,
  132. .usage = usage,
  133. };
  134. (*driver->send_extra)(&report);
  135. }
  136. #ifdef JOYSTICK_ENABLE
  137. void host_joystick_send(joystick_t *joystick) {
  138. if (!driver) return;
  139. report_joystick_t report = {
  140. # ifdef JOYSTICK_SHARED_EP
  141. .report_id = REPORT_ID_JOYSTICK,
  142. # endif
  143. # if JOYSTICK_AXIS_COUNT > 0
  144. .axes =
  145. {
  146. joystick->axes[0],
  147. # if JOYSTICK_AXIS_COUNT >= 2
  148. joystick->axes[1],
  149. # endif
  150. # if JOYSTICK_AXIS_COUNT >= 3
  151. joystick->axes[2],
  152. # endif
  153. # if JOYSTICK_AXIS_COUNT >= 4
  154. joystick->axes[3],
  155. # endif
  156. # if JOYSTICK_AXIS_COUNT >= 5
  157. joystick->axes[4],
  158. # endif
  159. # if JOYSTICK_AXIS_COUNT >= 6
  160. joystick->axes[5],
  161. # endif
  162. },
  163. # endif
  164. # ifdef JOYSTICK_HAS_HAT
  165. .hat = joystick->hat,
  166. # endif
  167. # if JOYSTICK_BUTTON_COUNT > 0
  168. .buttons =
  169. {
  170. joystick->buttons[0],
  171. # if JOYSTICK_BUTTON_COUNT > 8
  172. joystick->buttons[1],
  173. # endif
  174. # if JOYSTICK_BUTTON_COUNT > 16
  175. joystick->buttons[2],
  176. # endif
  177. # if JOYSTICK_BUTTON_COUNT > 24
  178. joystick->buttons[3],
  179. # endif
  180. },
  181. # endif
  182. };
  183. send_joystick(&report);
  184. }
  185. #endif
  186. __attribute__((weak)) void send_joystick(report_joystick_t *report) {}
  187. #ifdef DIGITIZER_ENABLE
  188. void host_digitizer_send(digitizer_t *digitizer) {
  189. report_digitizer_t report = {
  190. # ifdef DIGITIZER_SHARED_EP
  191. .report_id = REPORT_ID_DIGITIZER,
  192. # endif
  193. .in_range = digitizer->in_range,
  194. .tip = digitizer->tip,
  195. .barrel = digitizer->barrel,
  196. .x = (uint16_t)(digitizer->x * 0x7FFF),
  197. .y = (uint16_t)(digitizer->y * 0x7FFF),
  198. };
  199. send_digitizer(&report);
  200. }
  201. #endif
  202. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  203. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  204. void host_programmable_button_send(uint32_t data) {
  205. report_programmable_button_t report = {
  206. .report_id = REPORT_ID_PROGRAMMABLE_BUTTON,
  207. .usage = data,
  208. };
  209. send_programmable_button(&report);
  210. }
  211. #endif
  212. __attribute__((weak)) void send_programmable_button(report_programmable_button_t *report) {}
  213. uint16_t host_last_system_usage(void) {
  214. return last_system_usage;
  215. }
  216. uint16_t host_last_consumer_usage(void) {
  217. return last_consumer_usage;
  218. }