report.h 8.4 KB

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