report.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include "util.h"
  19. #ifdef JOYSTICK_ENABLE
  20. # include "joystick.h"
  21. #endif
  22. // clang-format off
  23. /* HID report IDs */
  24. enum hid_report_ids {
  25. REPORT_ID_ALL = 0,
  26. REPORT_ID_KEYBOARD = 1,
  27. REPORT_ID_MOUSE,
  28. REPORT_ID_SYSTEM,
  29. REPORT_ID_CONSUMER,
  30. REPORT_ID_PROGRAMMABLE_BUTTON,
  31. REPORT_ID_NKRO,
  32. REPORT_ID_JOYSTICK,
  33. REPORT_ID_DIGITIZER,
  34. REPORT_ID_COUNT = REPORT_ID_DIGITIZER
  35. };
  36. #define IS_VALID_REPORT_ID(id) ((id) >= REPORT_ID_ALL && (id) <= REPORT_ID_COUNT)
  37. // Plover HID has its own dedicated interface rather than the shared endpoint, so its report ID
  38. // is fixed by the protocol at 0x50 ('P') and is intentionally NOT a member of enum
  39. // hid_report_ids above (which only enumerates shared-endpoint reports up to REPORT_ID_COUNT).
  40. #define REPORT_ID_PLOVER_HID 0x50
  41. /* Mouse buttons */
  42. #define MOUSE_BTN_MASK(n) (1 << (n))
  43. enum mouse_buttons {
  44. MOUSE_BTN1 = MOUSE_BTN_MASK(0),
  45. MOUSE_BTN2 = MOUSE_BTN_MASK(1),
  46. MOUSE_BTN3 = MOUSE_BTN_MASK(2),
  47. MOUSE_BTN4 = MOUSE_BTN_MASK(3),
  48. MOUSE_BTN5 = MOUSE_BTN_MASK(4),
  49. MOUSE_BTN6 = MOUSE_BTN_MASK(5),
  50. MOUSE_BTN7 = MOUSE_BTN_MASK(6),
  51. MOUSE_BTN8 = MOUSE_BTN_MASK(7)
  52. };
  53. /* Consumer Page (0x0C)
  54. *
  55. * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=75
  56. */
  57. enum consumer_usages {
  58. // 15.5 Display Controls
  59. SNAPSHOT = 0x065,
  60. BRIGHTNESS_UP = 0x06F, // https://www.usb.org/sites/default/files/hutrr41_0.pdf
  61. BRIGHTNESS_DOWN = 0x070,
  62. // 15.7 Transport Controls
  63. TRANSPORT_RECORD = 0x0B2,
  64. TRANSPORT_FAST_FORWARD = 0x0B3,
  65. TRANSPORT_REWIND = 0x0B4,
  66. TRANSPORT_NEXT_TRACK = 0x0B5,
  67. TRANSPORT_PREV_TRACK = 0x0B6,
  68. TRANSPORT_STOP = 0x0B7,
  69. TRANSPORT_EJECT = 0x0B8,
  70. TRANSPORT_RANDOM_PLAY = 0x0B9,
  71. TRANSPORT_STOP_EJECT = 0x0CC,
  72. TRANSPORT_PLAY_PAUSE = 0x0CD,
  73. // 15.9.1 Audio Controls - Volume
  74. AUDIO_MUTE = 0x0E2,
  75. AUDIO_VOL_UP = 0x0E9,
  76. AUDIO_VOL_DOWN = 0x0EA,
  77. // 15.15 Application Launch Buttons
  78. AL_CC_CONFIG = 0x183,
  79. AL_EMAIL = 0x18A,
  80. AL_CALCULATOR = 0x192,
  81. AL_LOCAL_BROWSER = 0x194,
  82. AL_LOCK = 0x19E,
  83. AL_CONTROL_PANEL = 0x19F,
  84. AL_ASSISTANT = 0x1CB,
  85. AL_KEYBOARD_LAYOUT = 0x1AE,
  86. // 15.16 Generic GUI Application Controls
  87. AC_NEW = 0x201,
  88. AC_OPEN = 0x202,
  89. AC_CLOSE = 0x203,
  90. AC_EXIT = 0x204,
  91. AC_MAXIMIZE = 0x205,
  92. AC_MINIMIZE = 0x206,
  93. AC_SAVE = 0x207,
  94. AC_PRINT = 0x208,
  95. AC_PROPERTIES = 0x209,
  96. AC_UNDO = 0x21A,
  97. AC_COPY = 0x21B,
  98. AC_CUT = 0x21C,
  99. AC_PASTE = 0x21D,
  100. AC_SELECT_ALL = 0x21E,
  101. AC_FIND = 0x21F,
  102. AC_SEARCH = 0x221,
  103. AC_HOME = 0x223,
  104. AC_BACK = 0x224,
  105. AC_FORWARD = 0x225,
  106. AC_STOP = 0x226,
  107. AC_REFRESH = 0x227,
  108. AC_BOOKMARKS = 0x22A,
  109. AC_NEXT_KEYBOARD_LAYOUT_SELECT = 0x29D,
  110. AC_DESKTOP_SHOW_ALL_WINDOWS = 0x29F,
  111. AC_SOFT_KEY_LEFT = 0x2A0
  112. };
  113. /* Generic Desktop Page (0x01)
  114. *
  115. * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=26
  116. */
  117. enum desktop_usages {
  118. // 4.5.1 System Controls - Power Controls
  119. SYSTEM_POWER_DOWN = 0x81,
  120. SYSTEM_SLEEP = 0x82,
  121. SYSTEM_WAKE_UP = 0x83,
  122. SYSTEM_RESTART = 0x8F,
  123. // 4.10 System Display Controls
  124. SYSTEM_DISPLAY_TOGGLE_INT_EXT = 0xB5
  125. };
  126. // clang-format on
  127. #define NKRO_REPORT_BITS 30
  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 struct {
  158. #ifdef KEYBOARD_SHARED_EP
  159. uint8_t report_id;
  160. #endif
  161. uint8_t mods;
  162. uint8_t reserved;
  163. uint8_t keys[KEYBOARD_REPORT_KEYS];
  164. } PACKED report_keyboard_t;
  165. typedef struct {
  166. uint8_t report_id;
  167. uint8_t mods;
  168. uint8_t bits[NKRO_REPORT_BITS];
  169. } PACKED report_nkro_t;
  170. typedef struct {
  171. uint8_t report_id;
  172. uint16_t usage;
  173. } PACKED report_extra_t;
  174. typedef struct {
  175. uint8_t report_id;
  176. uint32_t usage;
  177. } PACKED report_programmable_button_t;
  178. typedef struct {
  179. uint8_t report_id; // REPORT_ID_PLOVER_HID
  180. uint8_t data[8];
  181. } PACKED report_plover_hid_t;
  182. #ifdef MOUSE_EXTENDED_REPORT
  183. # define MOUSE_REPORT_XY_MIN (INT16_MIN + 1)
  184. # define MOUSE_REPORT_XY_MAX INT16_MAX
  185. typedef int16_t mouse_xy_report_t;
  186. #else
  187. # define MOUSE_REPORT_XY_MIN (INT8_MIN + 1)
  188. # define MOUSE_REPORT_XY_MAX INT8_MAX
  189. typedef int8_t mouse_xy_report_t;
  190. #endif
  191. #ifdef WHEEL_EXTENDED_REPORT
  192. # define MOUSE_REPORT_HV_MIN (INT16_MIN + 1)
  193. # define MOUSE_REPORT_HV_MAX INT16_MAX
  194. typedef int16_t mouse_hv_report_t;
  195. #else
  196. # define MOUSE_REPORT_HV_MIN (INT8_MIN + 1)
  197. # define MOUSE_REPORT_HV_MAX INT8_MAX
  198. typedef int8_t mouse_hv_report_t;
  199. #endif
  200. typedef struct {
  201. #ifdef MOUSE_SHARED_EP
  202. uint8_t report_id;
  203. #endif
  204. uint8_t buttons;
  205. #ifdef MOUSE_EXTENDED_REPORT
  206. int8_t boot_x;
  207. int8_t boot_y;
  208. #endif
  209. mouse_xy_report_t x;
  210. mouse_xy_report_t y;
  211. mouse_hv_report_t v;
  212. mouse_hv_report_t h;
  213. } PACKED report_mouse_t;
  214. typedef struct {
  215. #ifdef DIGITIZER_SHARED_EP
  216. uint8_t report_id;
  217. #endif
  218. bool in_range : 1;
  219. bool tip : 1;
  220. bool barrel : 1;
  221. uint8_t reserved : 5;
  222. uint16_t x;
  223. uint16_t y;
  224. } PACKED report_digitizer_t;
  225. #if JOYSTICK_AXIS_RESOLUTION > 8
  226. typedef int16_t joystick_axis_t;
  227. #else
  228. typedef int8_t joystick_axis_t;
  229. #endif
  230. typedef struct {
  231. #ifdef JOYSTICK_SHARED_EP
  232. uint8_t report_id;
  233. #endif
  234. #if JOYSTICK_AXIS_COUNT > 0
  235. joystick_axis_t axes[JOYSTICK_AXIS_COUNT];
  236. #endif
  237. #ifdef JOYSTICK_HAS_HAT
  238. int8_t hat : 4;
  239. uint8_t reserved : 4;
  240. #endif
  241. #if JOYSTICK_BUTTON_COUNT > 0
  242. uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
  243. #endif
  244. } PACKED report_joystick_t;
  245. /* keycode to system usage */
  246. static inline uint16_t KEYCODE2SYSTEM(uint8_t key) {
  247. switch (key) {
  248. case KC_SYSTEM_POWER:
  249. return SYSTEM_POWER_DOWN;
  250. case KC_SYSTEM_SLEEP:
  251. return SYSTEM_SLEEP;
  252. case KC_SYSTEM_WAKE:
  253. return SYSTEM_WAKE_UP;
  254. default:
  255. return 0;
  256. }
  257. }
  258. /* keycode to consumer usage */
  259. static inline uint16_t KEYCODE2CONSUMER(uint8_t key) {
  260. switch (key) {
  261. case KC_AUDIO_MUTE:
  262. return AUDIO_MUTE;
  263. case KC_AUDIO_VOL_UP:
  264. return AUDIO_VOL_UP;
  265. case KC_AUDIO_VOL_DOWN:
  266. return AUDIO_VOL_DOWN;
  267. case KC_MEDIA_NEXT_TRACK:
  268. return TRANSPORT_NEXT_TRACK;
  269. case KC_MEDIA_PREV_TRACK:
  270. return TRANSPORT_PREV_TRACK;
  271. case KC_MEDIA_FAST_FORWARD:
  272. return TRANSPORT_FAST_FORWARD;
  273. case KC_MEDIA_REWIND:
  274. return TRANSPORT_REWIND;
  275. case KC_MEDIA_STOP:
  276. return TRANSPORT_STOP;
  277. case KC_MEDIA_EJECT:
  278. return TRANSPORT_STOP_EJECT;
  279. case KC_MEDIA_PLAY_PAUSE:
  280. return TRANSPORT_PLAY_PAUSE;
  281. case KC_MEDIA_SELECT:
  282. return AL_CC_CONFIG;
  283. case KC_MAIL:
  284. return AL_EMAIL;
  285. case KC_CALCULATOR:
  286. return AL_CALCULATOR;
  287. case KC_MY_COMPUTER:
  288. return AL_LOCAL_BROWSER;
  289. case KC_CONTROL_PANEL:
  290. return AL_CONTROL_PANEL;
  291. case KC_ASSISTANT:
  292. return AL_ASSISTANT;
  293. case KC_WWW_SEARCH:
  294. return AC_SEARCH;
  295. case KC_WWW_HOME:
  296. return AC_HOME;
  297. case KC_WWW_BACK:
  298. return AC_BACK;
  299. case KC_WWW_FORWARD:
  300. return AC_FORWARD;
  301. case KC_WWW_STOP:
  302. return AC_STOP;
  303. case KC_WWW_REFRESH:
  304. return AC_REFRESH;
  305. case KC_BRIGHTNESS_UP:
  306. return BRIGHTNESS_UP;
  307. case KC_BRIGHTNESS_DOWN:
  308. return BRIGHTNESS_DOWN;
  309. case KC_WWW_FAVORITES:
  310. return AC_BOOKMARKS;
  311. case KC_MISSION_CONTROL:
  312. return AC_DESKTOP_SHOW_ALL_WINDOWS;
  313. case KC_LAUNCHPAD:
  314. return AC_SOFT_KEY_LEFT;
  315. default:
  316. return 0;
  317. }
  318. }
  319. uint8_t has_anykey(void);
  320. uint8_t get_first_key(void);
  321. bool is_key_pressed(uint8_t key);
  322. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  323. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  324. #ifdef NKRO_ENABLE
  325. void add_key_bit(report_nkro_t* nkro_report, uint8_t code);
  326. void del_key_bit(report_nkro_t* nkro_report, uint8_t code);
  327. #endif
  328. void add_key_to_report(uint8_t key);
  329. void del_key_from_report(uint8_t key);
  330. void clear_keys_from_report(void);
  331. #ifdef MOUSE_ENABLE
  332. bool has_mouse_report_changed(report_mouse_t* new_report, report_mouse_t* old_report);
  333. #endif
  334. #ifdef __cplusplus
  335. }
  336. #endif