host.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 BLUETOOTH_ENABLE
  23. # include "outputselect.h"
  24. # ifdef BLUETOOTH_BLUEFRUIT_LE
  25. # include "bluefruit_le.h"
  26. # elif BLUETOOTH_RN42
  27. # include "rn42.h"
  28. # endif
  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_report = 0;
  36. static uint16_t last_consumer_report = 0;
  37. static uint32_t last_programmable_button_report = 0;
  38. void host_set_driver(host_driver_t *d) {
  39. driver = d;
  40. }
  41. host_driver_t *host_get_driver(void) {
  42. return driver;
  43. }
  44. #ifdef SPLIT_KEYBOARD
  45. uint8_t split_led_state = 0;
  46. void set_split_host_keyboard_leds(uint8_t led_state) {
  47. split_led_state = led_state;
  48. }
  49. #endif
  50. uint8_t host_keyboard_leds(void) {
  51. #ifdef SPLIT_KEYBOARD
  52. if (!is_keyboard_master()) return split_led_state;
  53. #endif
  54. if (!driver) return 0;
  55. return (*driver->keyboard_leds)();
  56. }
  57. led_t host_keyboard_led_state(void) {
  58. return (led_t)host_keyboard_leds();
  59. }
  60. /* send report */
  61. void host_keyboard_send(report_keyboard_t *report) {
  62. #ifdef BLUETOOTH_ENABLE
  63. if (where_to_send() == OUTPUT_BLUETOOTH) {
  64. # ifdef BLUETOOTH_BLUEFRUIT_LE
  65. bluefruit_le_send_keys(report->mods, report->keys, sizeof(report->keys));
  66. # elif BLUETOOTH_RN42
  67. rn42_send_keyboard(report);
  68. # endif
  69. return;
  70. }
  71. #endif
  72. if (!driver) return;
  73. #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP)
  74. if (keyboard_protocol && keymap_config.nkro) {
  75. /* The callers of this function assume that report->mods is where mods go in.
  76. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID.
  77. */
  78. report->nkro.mods = report->mods;
  79. report->nkro.report_id = REPORT_ID_NKRO;
  80. } else
  81. #endif
  82. {
  83. #ifdef KEYBOARD_SHARED_EP
  84. report->report_id = REPORT_ID_KEYBOARD;
  85. #endif
  86. }
  87. (*driver->send_keyboard)(report);
  88. if (debug_keyboard) {
  89. dprint("keyboard_report: ");
  90. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  91. dprintf("%02X ", report->raw[i]);
  92. }
  93. dprint("\n");
  94. }
  95. }
  96. void host_mouse_send(report_mouse_t *report) {
  97. #ifdef BLUETOOTH_ENABLE
  98. if (where_to_send() == OUTPUT_BLUETOOTH) {
  99. # ifdef BLUETOOTH_BLUEFRUIT_LE
  100. // FIXME: mouse buttons
  101. bluefruit_le_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
  102. # elif BLUETOOTH_RN42
  103. rn42_send_mouse(report);
  104. # endif
  105. return;
  106. }
  107. #endif
  108. if (!driver) return;
  109. #ifdef MOUSE_SHARED_EP
  110. report->report_id = REPORT_ID_MOUSE;
  111. #endif
  112. #ifdef MOUSE_EXTENDED_REPORT
  113. // clip and copy to Boot protocol XY
  114. report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
  115. report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
  116. #endif
  117. (*driver->send_mouse)(report);
  118. }
  119. void host_system_send(uint16_t report) {
  120. if (report == last_system_report) return;
  121. last_system_report = report;
  122. if (!driver) return;
  123. (*driver->send_extra)(REPORT_ID_SYSTEM, report);
  124. }
  125. void host_consumer_send(uint16_t report) {
  126. if (report == last_consumer_report) return;
  127. last_consumer_report = report;
  128. #ifdef BLUETOOTH_ENABLE
  129. if (where_to_send() == OUTPUT_BLUETOOTH) {
  130. # ifdef BLUETOOTH_BLUEFRUIT_LE
  131. bluefruit_le_send_consumer_key(report);
  132. # elif BLUETOOTH_RN42
  133. rn42_send_consumer(report);
  134. # endif
  135. return;
  136. }
  137. #endif
  138. if (!driver) return;
  139. (*driver->send_extra)(REPORT_ID_CONSUMER, report);
  140. }
  141. void host_digitizer_send(digitizer_t *digitizer) {
  142. if (!driver) return;
  143. report_digitizer_t report = {
  144. #ifdef DIGITIZER_SHARED_EP
  145. .report_id = REPORT_ID_DIGITIZER,
  146. #endif
  147. .tip = digitizer->tipswitch & 0x1,
  148. .inrange = digitizer->inrange & 0x1,
  149. .x = (uint16_t)(digitizer->x * 0x7FFF),
  150. .y = (uint16_t)(digitizer->y * 0x7FFF),
  151. };
  152. send_digitizer(&report);
  153. }
  154. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  155. void host_programmable_button_send(uint32_t report) {
  156. if (report == last_programmable_button_report) return;
  157. last_programmable_button_report = report;
  158. if (!driver) return;
  159. (*driver->send_programmable_button)(report);
  160. }
  161. uint16_t host_last_system_report(void) {
  162. return last_system_report;
  163. }
  164. uint16_t host_last_consumer_report(void) {
  165. return last_consumer_report;
  166. }
  167. uint32_t host_last_programmable_button_report(void) {
  168. return last_programmable_button_report;
  169. }