logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

host.c (8159B)


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