report.h 10 KB

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