host.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "keycode.h"
  17. #include "host.h"
  18. #include "util.h"
  19. #include "debug.h"
  20. #ifdef NKRO_ENABLE
  21. # include "keycode_config.h"
  22. extern keymap_config_t keymap_config;
  23. #endif
  24. static host_driver_t *driver;
  25. static uint16_t last_system_report = 0;
  26. static uint16_t last_consumer_report = 0;
  27. void host_set_driver(host_driver_t *d) { driver = d; }
  28. host_driver_t *host_get_driver(void) { return driver; }
  29. uint8_t host_keyboard_leds(void) {
  30. if (!driver) return 0;
  31. return (*driver->keyboard_leds)();
  32. }
  33. led_t host_keyboard_led_state(void) {
  34. if (!driver) return (led_t){0};
  35. return (led_t)((*driver->keyboard_leds)());
  36. }
  37. /* send report */
  38. void host_keyboard_send(report_keyboard_t *report) {
  39. if (!driver) return;
  40. #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP)
  41. if (keyboard_protocol && keymap_config.nkro) {
  42. /* The callers of this function assume that report->mods is where mods go in.
  43. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID.
  44. */
  45. report->nkro.mods = report->mods;
  46. report->nkro.report_id = REPORT_ID_NKRO;
  47. } else
  48. #endif
  49. {
  50. #ifdef KEYBOARD_SHARED_EP
  51. report->report_id = REPORT_ID_KEYBOARD;
  52. #endif
  53. }
  54. (*driver->send_keyboard)(report);
  55. if (debug_keyboard) {
  56. dprint("keyboard_report: ");
  57. for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) {
  58. dprintf("%02X ", report->raw[i]);
  59. }
  60. dprint("\n");
  61. }
  62. }
  63. void host_mouse_send(report_mouse_t *report) {
  64. if (!driver) return;
  65. #ifdef MOUSE_SHARED_EP
  66. report->report_id = REPORT_ID_MOUSE;
  67. #endif
  68. (*driver->send_mouse)(report);
  69. }
  70. void host_system_send(uint16_t report) {
  71. if (report == last_system_report) return;
  72. last_system_report = report;
  73. if (!driver) return;
  74. (*driver->send_system)(report);
  75. }
  76. void host_consumer_send(uint16_t report) {
  77. if (report == last_consumer_report) return;
  78. last_consumer_report = report;
  79. if (!driver) return;
  80. (*driver->send_consumer)(report);
  81. }
  82. uint16_t host_last_system_report(void) { return last_system_report; }
  83. uint16_t host_last_consumer_report(void) { return last_consumer_report; }