report.h 9.3 KB

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