report.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "keycode.h"
  18. /* HID report IDs */
  19. enum hid_report_ids { REPORT_ID_KEYBOARD = 1, REPORT_ID_MOUSE, REPORT_ID_SYSTEM, REPORT_ID_CONSUMER, REPORT_ID_NKRO };
  20. /* Mouse buttons */
  21. enum mouse_buttons { MOUSE_BTN1 = (1 << 0), MOUSE_BTN2 = (1 << 1), MOUSE_BTN3 = (1 << 2), MOUSE_BTN4 = (1 << 3), MOUSE_BTN5 = (1 << 4) };
  22. // clang-format off
  23. /* Consumer Page (0x0C)
  24. *
  25. * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=75
  26. */
  27. enum consumer_usages {
  28. // 15.5 Display Controls (https://www.usb.org/sites/default/files/hutrr41_0.pdf)
  29. BRIGHTNESS_UP = 0x06F,
  30. BRIGHTNESS_DOWN = 0x070,
  31. // 15.7 Transport Controls
  32. TRANSPORT_RECORD = 0x0B2,
  33. TRANSPORT_FAST_FORWARD = 0x0B3,
  34. TRANSPORT_REWIND = 0x0B4,
  35. TRANSPORT_NEXT_TRACK = 0x0B5,
  36. TRANSPORT_PREV_TRACK = 0x0B6,
  37. TRANSPORT_STOP = 0x0B7,
  38. TRANSPORT_EJECT = 0x0B8,
  39. TRANSPORT_STOP_EJECT = 0x0CC,
  40. TRANSPORT_PLAY_PAUSE = 0x0CD,
  41. // 15.9.1 Audio Controls - Volume
  42. AUDIO_MUTE = 0x0E2,
  43. AUDIO_VOL_UP = 0x0E9,
  44. AUDIO_VOL_DOWN = 0x0EA,
  45. // 15.15 Application Launch Buttons
  46. AL_CC_CONFIG = 0x183,
  47. AL_EMAIL = 0x18A,
  48. AL_CALCULATOR = 0x192,
  49. AL_LOCAL_BROWSER = 0x194,
  50. AL_LOCK = 0x19E,
  51. // 15.16 Generic GUI Application Controls
  52. AC_MINIMIZE = 0x206,
  53. AC_SEARCH = 0x221,
  54. AC_HOME = 0x223,
  55. AC_BACK = 0x224,
  56. AC_FORWARD = 0x225,
  57. AC_STOP = 0x226,
  58. AC_REFRESH = 0x227,
  59. AC_BOOKMARKS = 0x22A
  60. };
  61. /* Generic Desktop Page (0x01)
  62. *
  63. * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=26
  64. */
  65. enum desktop_usages {
  66. // 4.5.1 System Controls - Power Controls
  67. SYSTEM_POWER_DOWN = 0x81,
  68. SYSTEM_SLEEP = 0x82,
  69. SYSTEM_WAKE_UP = 0x83
  70. };
  71. // clang-format on
  72. #define NKRO_SHARED_EP
  73. /* key report size(NKRO or boot mode) */
  74. #if defined(NKRO_ENABLE)
  75. # if defined(PROTOCOL_LUFA) || defined(PROTOCOL_CHIBIOS)
  76. # include "protocol/usb_descriptor.h"
  77. # define KEYBOARD_REPORT_BITS (SHARED_EPSIZE - 2)
  78. # elif defined(PROTOCOL_ARM_ATSAM)
  79. # include "protocol/arm_atsam/usb/udi_device_epsize.h"
  80. # define KEYBOARD_REPORT_BITS (NKRO_EPSIZE - 1)
  81. # undef NKRO_SHARED_EP
  82. # undef MOUSE_SHARED_EP
  83. # else
  84. # error "NKRO not supported with this protocol"
  85. # endif
  86. #endif
  87. #ifdef KEYBOARD_SHARED_EP
  88. # define KEYBOARD_REPORT_SIZE 9
  89. #else
  90. # define KEYBOARD_REPORT_SIZE 8
  91. #endif
  92. #define KEYBOARD_REPORT_KEYS 6
  93. /* VUSB hardcodes keyboard and mouse+extrakey only */
  94. #if defined(PROTOCOL_VUSB)
  95. # undef KEYBOARD_SHARED_EP
  96. # undef MOUSE_SHARED_EP
  97. #endif
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. /*
  102. * keyboard report is 8-byte array retains state of 8 modifiers and 6 keys.
  103. *
  104. * byte |0 |1 |2 |3 |4 |5 |6 |7
  105. * -----+--------+--------+--------+--------+--------+--------+--------+--------
  106. * desc |mods |reserved|keys[0] |keys[1] |keys[2] |keys[3] |keys[4] |keys[5]
  107. *
  108. * It is exended to 16 bytes to retain 120keys+8mods when NKRO mode.
  109. *
  110. * byte |0 |1 |2 |3 |4 |5 |6 |7 ... |15
  111. * -----+--------+--------+--------+--------+--------+--------+--------+-------- +--------
  112. * desc |mods |bits[0] |bits[1] |bits[2] |bits[3] |bits[4] |bits[5] |bits[6] ... |bit[14]
  113. *
  114. * mods retains state of 8 modifiers.
  115. *
  116. * bit |0 |1 |2 |3 |4 |5 |6 |7
  117. * -----+--------+--------+--------+--------+--------+--------+--------+--------
  118. * desc |Lcontrol|Lshift |Lalt |Lgui |Rcontrol|Rshift |Ralt |Rgui
  119. *
  120. */
  121. typedef union {
  122. uint8_t raw[KEYBOARD_REPORT_SIZE];
  123. struct {
  124. #ifdef KEYBOARD_SHARED_EP
  125. uint8_t report_id;
  126. #endif
  127. uint8_t mods;
  128. uint8_t reserved;
  129. uint8_t keys[KEYBOARD_REPORT_KEYS];
  130. };
  131. #ifdef NKRO_ENABLE
  132. struct nkro_report {
  133. # ifdef NKRO_SHARED_EP
  134. uint8_t report_id;
  135. # endif
  136. uint8_t mods;
  137. uint8_t bits[KEYBOARD_REPORT_BITS];
  138. } nkro;
  139. #endif
  140. } __attribute__((packed)) report_keyboard_t;
  141. typedef struct {
  142. uint8_t report_id;
  143. uint16_t usage;
  144. } __attribute__((packed)) report_extra_t;
  145. typedef struct {
  146. #ifdef MOUSE_SHARED_EP
  147. uint8_t report_id;
  148. #endif
  149. uint8_t buttons;
  150. int8_t x;
  151. int8_t y;
  152. int8_t v;
  153. int8_t h;
  154. } __attribute__((packed)) report_mouse_t;
  155. /* keycode to system usage */
  156. static inline uint16_t KEYCODE2SYSTEM(uint8_t key) {
  157. switch (key) {
  158. case KC_SYSTEM_POWER:
  159. return SYSTEM_POWER_DOWN;
  160. case KC_SYSTEM_SLEEP:
  161. return SYSTEM_SLEEP;
  162. case KC_SYSTEM_WAKE:
  163. return SYSTEM_WAKE_UP;
  164. default:
  165. return 0;
  166. }
  167. }
  168. /* keycode to consumer usage */
  169. static inline uint16_t KEYCODE2CONSUMER(uint8_t key) {
  170. switch (key) {
  171. case KC_AUDIO_MUTE:
  172. return AUDIO_MUTE;
  173. case KC_AUDIO_VOL_UP:
  174. return AUDIO_VOL_UP;
  175. case KC_AUDIO_VOL_DOWN:
  176. return AUDIO_VOL_DOWN;
  177. case KC_MEDIA_NEXT_TRACK:
  178. return TRANSPORT_NEXT_TRACK;
  179. case KC_MEDIA_PREV_TRACK:
  180. return TRANSPORT_PREV_TRACK;
  181. case KC_MEDIA_FAST_FORWARD:
  182. return TRANSPORT_FAST_FORWARD;
  183. case KC_MEDIA_REWIND:
  184. return TRANSPORT_REWIND;
  185. case KC_MEDIA_STOP:
  186. return TRANSPORT_STOP;
  187. case KC_MEDIA_EJECT:
  188. return TRANSPORT_STOP_EJECT;
  189. case KC_MEDIA_PLAY_PAUSE:
  190. return TRANSPORT_PLAY_PAUSE;
  191. case KC_MEDIA_SELECT:
  192. return AL_CC_CONFIG;
  193. case KC_MAIL:
  194. return AL_EMAIL;
  195. case KC_CALCULATOR:
  196. return AL_CALCULATOR;
  197. case KC_MY_COMPUTER:
  198. return AL_LOCAL_BROWSER;
  199. case KC_WWW_SEARCH:
  200. return AC_SEARCH;
  201. case KC_WWW_HOME:
  202. return AC_HOME;
  203. case KC_WWW_BACK:
  204. return AC_BACK;
  205. case KC_WWW_FORWARD:
  206. return AC_FORWARD;
  207. case KC_WWW_STOP:
  208. return AC_STOP;
  209. case KC_WWW_REFRESH:
  210. return AC_REFRESH;
  211. case KC_BRIGHTNESS_UP:
  212. return BRIGHTNESS_UP;
  213. case KC_BRIGHTNESS_DOWN:
  214. return BRIGHTNESS_DOWN;
  215. case KC_WWW_FAVORITES:
  216. return AC_BOOKMARKS;
  217. default:
  218. return 0;
  219. }
  220. }
  221. uint8_t has_anykey(report_keyboard_t* keyboard_report);
  222. uint8_t get_first_key(report_keyboard_t* keyboard_report);
  223. bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key);
  224. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  225. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  226. #ifdef NKRO_ENABLE
  227. void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
  228. void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
  229. #endif
  230. void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key);
  231. void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key);
  232. void clear_keys_from_report(report_keyboard_t* keyboard_report);
  233. #ifdef __cplusplus
  234. }
  235. #endif