host.c 4.0 KB

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