host.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 NKRO_ENABLE
  23. # include "keycode_config.h"
  24. extern keymap_config_t keymap_config;
  25. #endif
  26. static host_driver_t *driver;
  27. static uint16_t last_system_report = 0;
  28. static uint16_t last_consumer_report = 0;
  29. static uint32_t last_programmable_button_report = 0;
  30. void host_set_driver(host_driver_t *d) {
  31. driver = d;
  32. }
  33. host_driver_t *host_get_driver(void) {
  34. return driver;
  35. }
  36. #ifdef SPLIT_KEYBOARD
  37. uint8_t split_led_state = 0;
  38. void set_split_host_keyboard_leds(uint8_t led_state) {
  39. split_led_state = led_state;
  40. }
  41. #endif
  42. uint8_t host_keyboard_leds(void) {
  43. #ifdef SPLIT_KEYBOARD
  44. if (!is_keyboard_master()) return split_led_state;
  45. #endif
  46. if (!driver) return 0;
  47. return (*driver->keyboard_leds)();
  48. }
  49. led_t host_keyboard_led_state(void) {
  50. return (led_t)host_keyboard_leds();
  51. }
  52. /* send report */
  53. void host_keyboard_send(report_keyboard_t *report) {
  54. if (!driver) return;
  55. #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP)
  56. if (keyboard_protocol && keymap_config.nkro) {
  57. /* The callers of this function assume that report->mods is where mods go in.
  58. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID.
  59. */
  60. report->nkro.mods = report->mods;
  61. report->nkro.report_id = REPORT_ID_NKRO;
  62. } else
  63. #endif
  64. {
  65. #ifdef KEYBOARD_SHARED_EP
  66. report->report_id = REPORT_ID_KEYBOARD;
  67. #endif
  68. }
  69. (*driver->send_keyboard)(report);
  70. if (debug_keyboard) {
  71. dprint("keyboard_report: ");
  72. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  73. dprintf("%02X ", report->raw[i]);
  74. }
  75. dprint("\n");
  76. }
  77. }
  78. void host_mouse_send(report_mouse_t *report) {
  79. if (!driver) return;
  80. #ifdef MOUSE_SHARED_EP
  81. report->report_id = REPORT_ID_MOUSE;
  82. #endif
  83. #ifdef MOUSE_EXTENDED_REPORT
  84. // clip and copy to Boot protocol XY
  85. report->boot_x = (report->x > 127) ? 127 : ((report->x < -127) ? -127 : report->x);
  86. report->boot_y = (report->y > 127) ? 127 : ((report->y < -127) ? -127 : report->y);
  87. #endif
  88. (*driver->send_mouse)(report);
  89. }
  90. void host_system_send(uint16_t report) {
  91. if (report == last_system_report) return;
  92. last_system_report = report;
  93. if (!driver) return;
  94. (*driver->send_extra)(REPORT_ID_SYSTEM, report);
  95. }
  96. void host_consumer_send(uint16_t report) {
  97. if (report == last_consumer_report) return;
  98. last_consumer_report = report;
  99. if (!driver) return;
  100. (*driver->send_extra)(REPORT_ID_CONSUMER, report);
  101. }
  102. void host_digitizer_send(digitizer_t *digitizer) {
  103. if (!driver) return;
  104. report_digitizer_t report = {
  105. #ifdef DIGITIZER_SHARED_EP
  106. .report_id = REPORT_ID_DIGITIZER,
  107. #endif
  108. .tip = digitizer->tipswitch & 0x1,
  109. .inrange = digitizer->inrange & 0x1,
  110. .x = (uint16_t)(digitizer->x * 0x7FFF),
  111. .y = (uint16_t)(digitizer->y * 0x7FFF),
  112. };
  113. send_digitizer(&report);
  114. }
  115. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  116. void host_programmable_button_send(uint32_t report) {
  117. if (report == last_programmable_button_report) return;
  118. last_programmable_button_report = report;
  119. if (!driver) return;
  120. (*driver->send_programmable_button)(report);
  121. }
  122. uint16_t host_last_system_report(void) {
  123. return last_system_report;
  124. }
  125. uint16_t host_last_consumer_report(void) {
  126. return last_consumer_report;
  127. }
  128. uint32_t host_last_programmable_button_report(void) {
  129. return last_programmable_button_report;
  130. }