report.h 7.9 KB

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