host.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP)
  69. if (keyboard_protocol && keymap_config.nkro) {
  70. /* The callers of this function assume that report->mods is where mods go in.
  71. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID.
  72. */
  73. report->nkro.mods = report->mods;
  74. report->nkro.report_id = REPORT_ID_NKRO;
  75. } else
  76. #endif
  77. {
  78. #ifdef KEYBOARD_SHARED_EP
  79. report->report_id = REPORT_ID_KEYBOARD;
  80. #endif
  81. }
  82. (*driver->send_keyboard)(report);
  83. if (debug_keyboard) {
  84. dprint("keyboard_report: ");
  85. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  86. dprintf("%02X ", report->raw[i]);
  87. }
  88. dprint("\n");
  89. }
  90. }
  91. void host_mouse_send(report_mouse_t *report) {
  92. #ifdef BLUETOOTH_ENABLE
  93. if (where_to_send() == OUTPUT_BLUETOOTH) {
  94. bluetooth_send_mouse(report);
  95. return;
  96. }
  97. #endif
  98. if (!driver) return;
  99. #ifdef MOUSE_SHARED_EP
  100. report->report_id = REPORT_ID_MOUSE;
  101. #endif
  102. #ifdef MOUSE_EXTENDED_REPORT
  103. // clip and copy to Boot protocol XY
  104. report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
  105. report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
  106. #endif
  107. (*driver->send_mouse)(report);
  108. }
  109. void host_system_send(uint16_t usage) {
  110. if (usage == last_system_usage) return;
  111. last_system_usage = usage;
  112. if (!driver) return;
  113. report_extra_t report = {
  114. .report_id = REPORT_ID_SYSTEM,
  115. .usage = usage,
  116. };
  117. (*driver->send_extra)(&report);
  118. }
  119. void host_consumer_send(uint16_t usage) {
  120. if (usage == last_consumer_usage) return;
  121. last_consumer_usage = usage;
  122. #ifdef BLUETOOTH_ENABLE
  123. if (where_to_send() == OUTPUT_BLUETOOTH) {
  124. bluetooth_send_consumer(usage);
  125. return;
  126. }
  127. #endif
  128. if (!driver) return;
  129. report_extra_t report = {
  130. .report_id = REPORT_ID_CONSUMER,
  131. .usage = usage,
  132. };
  133. (*driver->send_extra)(&report);
  134. }
  135. #ifdef JOYSTICK_ENABLE
  136. void host_joystick_send(joystick_t *joystick) {
  137. if (!driver) return;
  138. report_joystick_t report = {
  139. # ifdef JOYSTICK_SHARED_EP
  140. .report_id = REPORT_ID_JOYSTICK,
  141. # endif
  142. # if JOYSTICK_AXIS_COUNT > 0
  143. .axes =
  144. {
  145. joystick->axes[0],
  146. # if JOYSTICK_AXIS_COUNT >= 2
  147. joystick->axes[1],
  148. # endif
  149. # if JOYSTICK_AXIS_COUNT >= 3
  150. joystick->axes[2],
  151. # endif
  152. # if JOYSTICK_AXIS_COUNT >= 4
  153. joystick->axes[3],
  154. # endif
  155. # if JOYSTICK_AXIS_COUNT >= 5
  156. joystick->axes[4],
  157. # endif
  158. # if JOYSTICK_AXIS_COUNT >= 6
  159. joystick->axes[5],
  160. # endif
  161. },
  162. # endif
  163. # if JOYSTICK_BUTTON_COUNT > 0
  164. .buttons =
  165. {
  166. joystick->buttons[0],
  167. # if JOYSTICK_BUTTON_COUNT > 8
  168. joystick->buttons[1],
  169. # endif
  170. # if JOYSTICK_BUTTON_COUNT > 16
  171. joystick->buttons[2],
  172. # endif
  173. # if JOYSTICK_BUTTON_COUNT > 24
  174. joystick->buttons[3],
  175. # endif
  176. },
  177. # endif
  178. };
  179. send_joystick(&report);
  180. }
  181. #endif
  182. __attribute__((weak)) void send_joystick(report_joystick_t *report) {}
  183. #ifdef DIGITIZER_ENABLE
  184. void host_digitizer_send(digitizer_t *digitizer) {
  185. report_digitizer_t report = {
  186. # ifdef DIGITIZER_SHARED_EP
  187. .report_id = REPORT_ID_DIGITIZER,
  188. # endif
  189. .in_range = digitizer->in_range,
  190. .tip = digitizer->tip,
  191. .barrel = digitizer->barrel,
  192. .x = (uint16_t)(digitizer->x * 0x7FFF),
  193. .y = (uint16_t)(digitizer->y * 0x7FFF),
  194. };
  195. send_digitizer(&report);
  196. }
  197. #endif
  198. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  199. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  200. void host_programmable_button_send(uint32_t data) {
  201. report_programmable_button_t report = {
  202. .report_id = REPORT_ID_PROGRAMMABLE_BUTTON,
  203. .usage = data,
  204. };
  205. send_programmable_button(&report);
  206. }
  207. #endif
  208. __attribute__((weak)) void send_programmable_button(report_programmable_button_t *report) {}
  209. uint16_t host_last_system_usage(void) {
  210. return last_system_usage;
  211. }
  212. uint16_t host_last_consumer_usage(void) {
  213. return last_consumer_usage;
  214. }