report.h 9.7 KB

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