host.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. (*driver->send_mouse)(report);
  84. }
  85. void host_system_send(uint16_t report) {
  86. if (report == last_system_report) return;
  87. last_system_report = report;
  88. if (!driver) return;
  89. (*driver->send_system)(report);
  90. }
  91. void host_consumer_send(uint16_t report) {
  92. if (report == last_consumer_report) return;
  93. last_consumer_report = report;
  94. if (!driver) return;
  95. (*driver->send_consumer)(report);
  96. }
  97. void host_digitizer_send(digitizer_t *digitizer) {
  98. if (!driver) return;
  99. report_digitizer_t report = {
  100. #ifdef DIGITIZER_SHARED_EP
  101. .report_id = REPORT_ID_DIGITIZER,
  102. #endif
  103. .tip = digitizer->tipswitch & 0x1,
  104. .inrange = digitizer->inrange & 0x1,
  105. .x = (uint16_t)(digitizer->x * 0x7FFF),
  106. .y = (uint16_t)(digitizer->y * 0x7FFF),
  107. };
  108. send_digitizer(&report);
  109. }
  110. __attribute__((weak)) void send_digitizer(report_digitizer_t *report) {}
  111. void host_programmable_button_send(uint32_t report) {
  112. if (report == last_programmable_button_report) return;
  113. last_programmable_button_report = report;
  114. if (!driver) return;
  115. (*driver->send_programmable_button)(report);
  116. }
  117. uint16_t host_last_system_report(void) {
  118. return last_system_report;
  119. }
  120. uint16_t host_last_consumer_report(void) {
  121. return last_consumer_report;
  122. }
  123. uint32_t host_last_programmable_button_report(void) {
  124. return last_programmable_button_report;
  125. }