host.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <avr/interrupt.h>
  16. #include "keyboard.h"
  17. #include "keycode.h"
  18. #include "host.h"
  19. #include "util.h"
  20. #include "debug.h"
  21. #include "digitizer.h"
  22. #ifdef JOYSTICK_ENABLE
  23. # include "joystick.h"
  24. #endif
  25. #ifdef BLUETOOTH_ENABLE
  26. # include "outputselect.h"
  27. # ifdef BLUETOOTH_BLUEFRUIT_LE
  28. # include "bluefruit_le.h"
  29. # elif BLUETOOTH_RN42
  30. # include "rn42.h"
  31. # endif
  32. #endif
  33. #ifdef NKRO_ENABLE
  34. # include "keycode_config.h"
  35. extern keymap_config_t keymap_config;
  36. #endif
  37. static host_driver_t *driver;
  38. static uint16_t last_system_report = 0;
  39. static uint16_t last_consumer_report = 0;
  40. static uint32_t last_programmable_button_report = 0;
  41. void host_set_driver(host_driver_t *d) {
  42. driver = d;
  43. }
  44. host_driver_t *host_get_driver(void) {
  45. return driver;
  46. }
  47. #ifdef SPLIT_KEYBOARD
  48. uint8_t split_led_state = 0;
  49. void set_split_host_keyboard_leds(uint8_t led_state) {
  50. split_led_state = led_state;
  51. }
  52. #endif
  53. uint8_t host_keyboard_leds(void) {
  54. #ifdef SPLIT_KEYBOARD
  55. if (!is_keyboard_master()) return split_led_state;
  56. #endif
  57. if (!driver) return 0;
  58. return (*driver->keyboard_leds)();
  59. }
  60. led_t host_keyboard_led_state(void) {
  61. return (led_t)host_keyboard_leds();
  62. }
  63. /* send report */
  64. void host_keyboard_send(report_keyboard_t *report) {
  65. #ifdef BLUETOOTH_ENABLE
  66. if (where_to_send() == OUTPUT_BLUETOOTH) {
  67. # ifdef BLUETOOTH_BLUEFRUIT_LE
  68. bluefruit_le_send_keys(report->mods, report->keys, sizeof(report->keys));
  69. # elif BLUETOOTH_RN42
  70. rn42_send_keyboard(report);
  71. # endif
  72. return;
  73. }
  74. #endif
  75. if (!driver) return;
  76. #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP)
  77. if (keyboard_protocol && keymap_config.nkro) {
  78. /* The callers of this function assume that report->mods is where mods go in.
  79. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID.
  80. */
  81. report->nkro.mods = report->mods;
  82. report->nkro.report_id = REPORT_ID_NKRO;
  83. } else
  84. #endif
  85. {
  86. #ifdef KEYBOARD_SHARED_EP
  87. report->report_id = REPORT_ID_KEYBOARD;
  88. #endif
  89. }
  90. (*driver->send_keyboard)(report);
  91. if (debug_keyboard) {
  92. dprint("keyboard_report: ");
  93. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  94. dprintf("%02X ", report->raw[i]);
  95. }
  96. dprint("\n");
  97. }
  98. }
  99. void host_mouse_send(report_mouse_t *report) {
  100. #ifdef BLUETOOTH_ENABLE
  101. if (where_to_send() == OUTPUT_BLUETOOTH) {
  102. # ifdef BLUETOOTH_BLUEFRUIT_LE
  103. // FIXME: mouse buttons
  104. bluefruit_le_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
  105. # elif BLUETOOTH_RN42
  106. rn42_send_mouse(report);
  107. # endif
  108. return;
  109. }
  110. #endif
  111. if (!driver) return;
  112. #ifdef MOUSE_SHARED_EP
  113. report->report_id = REPORT_ID_MOUSE;
  114. #endif
  115. #ifdef MOUSE_EXTENDED_REPORT
  116. // clip and copy to Boot protocol XY
  117. report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
  118. report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
  119. #endif
  120. (*driver->send_mouse)(report);
  121. }
  122. void host_system_send(uint16_t report) {
  123. if (report == last_system_report) return;
  124. last_system_report = report;
  125. if (!driver) return;
  126. (*driver->send_extra)(REPORT_ID_SYSTEM, report);
  127. }
  128. void host_consumer_send(uint16_t report) {
  129. if (report == last_consumer_report) return;
  130. last_consumer_report = report;
  131. #ifdef BLUETOOTH_ENABLE
  132. if (where_to_send() == OUTPUT_BLUETOOTH) {
  133. # ifdef BLUETOOTH_BLUEFRUIT_LE
  134. bluefruit_le_send_consumer_key(report);
  135. # elif BLUETOOTH_RN42
  136. rn42_send_consumer(report);
  137. # endif
  138. return;
  139. }
  140. #endif
  141. if (!driver) return;
  142. (*driver->send_extra)(REPORT_ID_CONSUMER, report);
  143. }
  144. #ifdef JOYSTICK_ENABLE
  145. void host_joystick_send(joystick_t *joystick) {
  146. if (!driver) return;
  147. report_joystick_t report = {
  148. # if JOYSTICK_AXES_COUNT > 0
  149. .axes =
  150. {
  151. joystick->axes[0],
  152. # if JOYSTICK_AXES_COUNT >= 2
  153. joystick->axes[1],
  154. # endif
  155. # if JOYSTICK_AXES_COUNT >= 3
  156. joystick->axes[2],
  157. # endif
  158. # if JOYSTICK_AXES_COUNT >= 4
  159. joystick->axes[3],
  160. # endif
  161. # if JOYSTICK_AXES_COUNT >= 5
  162. joystick->axes[4],
  163. # endif
  164. # if JOYSTICK_AXES_COUNT >= 6
  165. joystick->axes[5],
  166. # endif
  167. },
  168. # endif
  169. # if JOYSTICK_BUTTON_COUNT > 0
  170. .buttons =
  171. {
  172. joystick->buttons[0],
  173. # if JOYSTICK_BUTTON_COUNT > 8
  174. joystick->buttons[1],
  175. # endif
  176. # if JOYSTICK_BUTTON_COUNT > 16
  177. joystick->buttons[2],
  178. # endif
  179. # if JOYSTICK_BUTTON_COUNT > 24
  180. joystick->buttons[3],
  181. # endif
  182. },
  183. # endif
  184. };
  185. send_joystick(&report);
  186. }
  187. #endif
  188. __attribute__((weak)) void send_joystick(report_joystick_t *report) {}
  189. void host_digitizer_send(digitizer_t *digitizer) {
  190. if (!driver) return;
  191. report_digitizer_t report = {
  192. #ifdef DIGITIZER_SHARED_EP
  193. .report_id = REPORT_ID_DIGITIZER,
  194. #endif
  195. .tip = digitizer->tipswitch & 0x1,
  196. .inrange = digitizer->inrange & 0x1,
  197. .x = (uint16_t)(digitizer->x * 0x7FFF),
  198. .y = (uint16_t)(digitizer->y * 0x7FFF),
  199. };
  200. send_digitizer(&report);
  201. }
  202. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  203. void host_programmable_button_send(uint32_t report) {
  204. if (report == last_programmable_button_report) return;
  205. last_programmable_button_report = report;
  206. if (!driver) return;
  207. (*driver->send_programmable_button)(report);
  208. }
  209. uint16_t host_last_system_report(void) {
  210. return last_system_report;
  211. }
  212. uint16_t host_last_consumer_report(void) {
  213. return last_consumer_report;
  214. }
  215. uint32_t host_last_programmable_button_report(void) {
  216. return last_programmable_button_report;
  217. }