host.c 3.6 KB

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