report.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef REPORT_H
  15. #define REPORT_H
  16. #include <stdint.h>
  17. #include "keycode.h"
  18. /* report id */
  19. #define REPORT_ID_KEYBOARD 1
  20. #define REPORT_ID_MOUSE 2
  21. #define REPORT_ID_SYSTEM 3
  22. #define REPORT_ID_CONSUMER 4
  23. #define REPORT_ID_NKRO 5
  24. /* mouse buttons */
  25. #define MOUSE_BTN1 (1 << 0)
  26. #define MOUSE_BTN2 (1 << 1)
  27. #define MOUSE_BTN3 (1 << 2)
  28. #define MOUSE_BTN4 (1 << 3)
  29. #define MOUSE_BTN5 (1 << 4)
  30. /* Consumer Page(0x0C)
  31. * following are supported by Windows: http://msdn.microsoft.com/en-us/windows/hardware/gg463372.aspx
  32. * see also https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/display-brightness-control
  33. */
  34. #define AUDIO_MUTE 0x00E2
  35. #define AUDIO_VOL_UP 0x00E9
  36. #define AUDIO_VOL_DOWN 0x00EA
  37. #define TRANSPORT_NEXT_TRACK 0x00B5
  38. #define TRANSPORT_PREV_TRACK 0x00B6
  39. #define TRANSPORT_STOP 0x00B7
  40. #define TRANSPORT_STOP_EJECT 0x00CC
  41. #define TRANSPORT_PLAY_PAUSE 0x00CD
  42. #define BRIGHTNESS_UP 0x006F
  43. #define BRIGHTNESS_DOWN 0x0070
  44. /* application launch */
  45. #define AL_CC_CONFIG 0x0183
  46. #define AL_EMAIL 0x018A
  47. #define AL_CALCULATOR 0x0192
  48. #define AL_LOCAL_BROWSER 0x0194
  49. /* application control */
  50. #define AC_SEARCH 0x0221
  51. #define AC_HOME 0x0223
  52. #define AC_BACK 0x0224
  53. #define AC_FORWARD 0x0225
  54. #define AC_STOP 0x0226
  55. #define AC_REFRESH 0x0227
  56. #define AC_BOOKMARKS 0x022A
  57. /* supplement for Bluegiga iWRAP HID(not supported by Windows?) */
  58. #define AL_LOCK 0x019E
  59. #define TRANSPORT_RECORD 0x00B2
  60. #define TRANSPORT_FAST_FORWARD 0x00B3
  61. #define TRANSPORT_REWIND 0x00B4
  62. #define TRANSPORT_EJECT 0x00B8
  63. #define AC_MINIMIZE 0x0206
  64. /* Generic Desktop Page(0x01) - system power control */
  65. #define SYSTEM_POWER_DOWN 0x0081
  66. #define SYSTEM_SLEEP 0x0082
  67. #define SYSTEM_WAKE_UP 0x0083
  68. #define NKRO_SHARED_EP
  69. /* key report size(NKRO or boot mode) */
  70. #if defined(NKRO_ENABLE)
  71. # if defined(PROTOCOL_LUFA) || defined(PROTOCOL_CHIBIOS)
  72. # include "protocol/usb_descriptor.h"
  73. # define KEYBOARD_REPORT_BITS (SHARED_EPSIZE - 2)
  74. # elif defined(PROTOCOL_ARM_ATSAM)
  75. # include "protocol/arm_atsam/usb/udi_device_epsize.h"
  76. # define KEYBOARD_REPORT_BITS (NKRO_EPSIZE - 1)
  77. # undef NKRO_SHARED_EP
  78. # undef MOUSE_SHARED_EP
  79. # else
  80. # error "NKRO not supported with this protocol"
  81. # endif
  82. #endif
  83. #ifdef KEYBOARD_SHARED_EP
  84. # define KEYBOARD_REPORT_SIZE 9
  85. #else
  86. # define KEYBOARD_REPORT_SIZE 8
  87. #endif
  88. #define KEYBOARD_REPORT_KEYS 6
  89. /* VUSB hardcodes keyboard and mouse+extrakey only */
  90. #if defined(PROTOCOL_VUSB)
  91. # undef KEYBOARD_SHARED_EP
  92. # undef MOUSE_SHARED_EP
  93. #endif
  94. #ifdef __cplusplus
  95. extern "C" {
  96. #endif
  97. /*
  98. * keyboard report is 8-byte array retains state of 8 modifiers and 6 keys.
  99. *
  100. * byte |0 |1 |2 |3 |4 |5 |6 |7
  101. * -----+--------+--------+--------+--------+--------+--------+--------+--------
  102. * desc |mods |reserved|keys[0] |keys[1] |keys[2] |keys[3] |keys[4] |keys[5]
  103. *
  104. * It is exended to 16 bytes to retain 120keys+8mods when NKRO mode.
  105. *
  106. * byte |0 |1 |2 |3 |4 |5 |6 |7 ... |15
  107. * -----+--------+--------+--------+--------+--------+--------+--------+-------- +--------
  108. * desc |mods |bits[0] |bits[1] |bits[2] |bits[3] |bits[4] |bits[5] |bits[6] ... |bit[14]
  109. *
  110. * mods retains state of 8 modifiers.
  111. *
  112. * bit |0 |1 |2 |3 |4 |5 |6 |7
  113. * -----+--------+--------+--------+--------+--------+--------+--------+--------
  114. * desc |Lcontrol|Lshift |Lalt |Lgui |Rcontrol|Rshift |Ralt |Rgui
  115. *
  116. */
  117. typedef union {
  118. uint8_t raw[KEYBOARD_REPORT_SIZE];
  119. struct {
  120. #ifdef KEYBOARD_SHARED_EP
  121. uint8_t report_id;
  122. #endif
  123. uint8_t mods;
  124. uint8_t reserved;
  125. uint8_t keys[KEYBOARD_REPORT_KEYS];
  126. };
  127. #ifdef NKRO_ENABLE
  128. struct nkro_report {
  129. # ifdef NKRO_SHARED_EP
  130. uint8_t report_id;
  131. # endif
  132. uint8_t mods;
  133. uint8_t bits[KEYBOARD_REPORT_BITS];
  134. } nkro;
  135. #endif
  136. } __attribute__((packed)) report_keyboard_t;
  137. typedef struct {
  138. uint8_t report_id;
  139. uint16_t usage;
  140. } __attribute__((packed)) report_extra_t;
  141. typedef struct {
  142. #ifdef MOUSE_SHARED_EP
  143. uint8_t report_id;
  144. #endif
  145. uint8_t buttons;
  146. int8_t x;
  147. int8_t y;
  148. int8_t v;
  149. int8_t h;
  150. } __attribute__((packed)) report_mouse_t;
  151. /* keycode to system usage */
  152. #define KEYCODE2SYSTEM(key) (key == KC_SYSTEM_POWER ? SYSTEM_POWER_DOWN : (key == KC_SYSTEM_SLEEP ? SYSTEM_SLEEP : (key == KC_SYSTEM_WAKE ? SYSTEM_WAKE_UP : 0)))
  153. /* keycode to consumer usage */
  154. #define KEYCODE2CONSUMER(key) \
  155. (key == KC_AUDIO_MUTE ? AUDIO_MUTE : (key == KC_AUDIO_VOL_UP ? AUDIO_VOL_UP : (key == KC_AUDIO_VOL_DOWN ? AUDIO_VOL_DOWN : (key == KC_MEDIA_NEXT_TRACK ? TRANSPORT_NEXT_TRACK : (key == KC_MEDIA_PREV_TRACK ? TRANSPORT_PREV_TRACK : (key == KC_MEDIA_FAST_FORWARD ? TRANSPORT_FAST_FORWARD : (key == KC_MEDIA_REWIND ? TRANSPORT_REWIND : (key == KC_MEDIA_STOP ? TRANSPORT_STOP : (key == KC_MEDIA_EJECT ? TRANSPORT_STOP_EJECT : (key == KC_MEDIA_PLAY_PAUSE ? TRANSPORT_PLAY_PAUSE : (key == KC_MEDIA_SELECT ? AL_CC_CONFIG : (key == KC_MAIL ? AL_EMAIL : (key == KC_CALCULATOR ? AL_CALCULATOR : (key == KC_MY_COMPUTER ? AL_LOCAL_BROWSER : (key == KC_WWW_SEARCH ? AC_SEARCH : (key == KC_WWW_HOME ? AC_HOME : (key == KC_WWW_BACK ? AC_BACK : (key == KC_WWW_FORWARD ? AC_FORWARD : (key == KC_WWW_STOP ? AC_STOP : (key == KC_WWW_REFRESH ? AC_REFRESH : (key == KC_BRIGHTNESS_UP ? BRIGHTNESS_UP : (key == KC_BRIGHTNESS_DOWN ? BRIGHTNESS_DOWN : (key == KC_WWW_FAVORITES ? AC_BOOKMARKS : 0)))))))))))))))))))))))
  156. uint8_t has_anykey(report_keyboard_t* keyboard_report);
  157. uint8_t get_first_key(report_keyboard_t* keyboard_report);
  158. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  159. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  160. #ifdef NKRO_ENABLE
  161. void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
  162. void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
  163. #endif
  164. void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key);
  165. void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key);
  166. void clear_keys_from_report(report_keyboard_t* keyboard_report);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif