]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/report.h
Use a single endpoint for HID reports (#3951)
[qmk_firmware.git] / tmk_core / common / report.h
1 /*
2 Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef REPORT_H
19 #define REPORT_H
20
21 #include <stdint.h>
22 #include "keycode.h"
23
24
25 /* report id */
26 #define REPORT_ID_KEYBOARD  1
27 #define REPORT_ID_MOUSE     2
28 #define REPORT_ID_SYSTEM    3
29 #define REPORT_ID_CONSUMER  4
30 #define REPORT_ID_NKRO      5
31
32 /* mouse buttons */
33 #define MOUSE_BTN1 (1<<0)
34 #define MOUSE_BTN2 (1<<1)
35 #define MOUSE_BTN3 (1<<2)
36 #define MOUSE_BTN4 (1<<3)
37 #define MOUSE_BTN5 (1<<4)
38
39 /* Consumer Page(0x0C)
40  * following are supported by Windows: http://msdn.microsoft.com/en-us/windows/hardware/gg463372.aspx
41  */
42 #define AUDIO_MUTE              0x00E2
43 #define AUDIO_VOL_UP            0x00E9
44 #define AUDIO_VOL_DOWN          0x00EA
45 #define TRANSPORT_NEXT_TRACK    0x00B5
46 #define TRANSPORT_PREV_TRACK    0x00B6
47 #define TRANSPORT_STOP          0x00B7
48 #define TRANSPORT_STOP_EJECT    0x00CC
49 #define TRANSPORT_PLAY_PAUSE    0x00CD
50 /* application launch */
51 #define AL_CC_CONFIG            0x0183
52 #define AL_EMAIL                0x018A
53 #define AL_CALCULATOR           0x0192
54 #define AL_LOCAL_BROWSER        0x0194
55 /* application control */
56 #define AC_SEARCH               0x0221
57 #define AC_HOME                 0x0223
58 #define AC_BACK                 0x0224
59 #define AC_FORWARD              0x0225
60 #define AC_STOP                 0x0226
61 #define AC_REFRESH              0x0227
62 #define AC_BOOKMARKS            0x022A
63 /* supplement for Bluegiga iWRAP HID(not supported by Windows?) */
64 #define AL_LOCK                 0x019E
65 #define TRANSPORT_RECORD        0x00B2
66 #define TRANSPORT_FAST_FORWARD  0x00B3
67 #define TRANSPORT_REWIND        0x00B4
68 #define TRANSPORT_EJECT         0x00B8
69 #define AC_MINIMIZE             0x0206
70
71 /* Generic Desktop Page(0x01) - system power control */
72 #define SYSTEM_POWER_DOWN       0x0081
73 #define SYSTEM_SLEEP            0x0082
74 #define SYSTEM_WAKE_UP          0x0083
75
76
77 #define NKRO_SHARED_EP
78 /* key report size(NKRO or boot mode) */
79 #if defined(NKRO_ENABLE)
80   #if defined(PROTOCOL_LUFA) || defined(PROTOCOL_CHIBIOS)
81     #include "protocol/usb_descriptor.h"
82     #define KEYBOARD_REPORT_BITS (SHARED_EPSIZE - 2)
83   #elif defined(PROTOCOL_ARM_ATSAM)
84     #include "protocol/arm_atsam/usb/udi_device_epsize.h"
85     #define KEYBOARD_REPORT_BITS (NKRO_EPSIZE - 1)
86     #undef NKRO_SHARED_EP
87     #undef MOUSE_SHARED_EP
88   #else
89     #error "NKRO not supported with this protocol"
90   #endif
91 #endif
92
93 #ifdef KEYBOARD_SHARED_EP
94 #   define KEYBOARD_REPORT_SIZE 9
95 #else
96 #   define KEYBOARD_REPORT_SIZE 8
97 #endif
98
99 #define KEYBOARD_REPORT_KEYS 6
100
101 /* VUSB hardcodes keyboard and mouse+extrakey only */
102 #if defined(PROTOCOL_VUSB)
103   #undef KEYBOARD_SHARED_EP
104   #undef MOUSE_SHARED_EP
105 #endif
106
107 #ifdef __cplusplus
108 extern "C" {
109 #endif
110
111 /*
112  * keyboard report is 8-byte array retains state of 8 modifiers and 6 keys.
113  *
114  * byte |0       |1       |2       |3       |4       |5       |6       |7
115  * -----+--------+--------+--------+--------+--------+--------+--------+--------
116  * desc |mods    |reserved|keys[0] |keys[1] |keys[2] |keys[3] |keys[4] |keys[5]
117  *
118  * It is exended to 16 bytes to retain 120keys+8mods when NKRO mode.
119  *
120  * byte |0       |1       |2       |3       |4       |5       |6       |7        ... |15
121  * -----+--------+--------+--------+--------+--------+--------+--------+--------     +--------
122  * desc |mods    |bits[0] |bits[1] |bits[2] |bits[3] |bits[4] |bits[5] |bits[6]  ... |bit[14]
123  *
124  * mods retains state of 8 modifiers.
125  *
126  *  bit |0       |1       |2       |3       |4       |5       |6       |7
127  * -----+--------+--------+--------+--------+--------+--------+--------+--------
128  * desc |Lcontrol|Lshift  |Lalt    |Lgui    |Rcontrol|Rshift  |Ralt    |Rgui
129  *
130  */
131 typedef union {
132     uint8_t raw[KEYBOARD_REPORT_SIZE];
133     struct {
134 #ifdef KEYBOARD_SHARED_EP
135         uint8_t report_id;
136 #endif
137         uint8_t mods;
138         uint8_t reserved;
139         uint8_t keys[KEYBOARD_REPORT_KEYS];
140     };
141 #ifdef NKRO_ENABLE
142     struct nkro_report {
143 #ifdef NKRO_SHARED_EP
144         uint8_t report_id;
145 #endif
146         uint8_t mods;
147         uint8_t bits[KEYBOARD_REPORT_BITS];
148     } nkro;
149 #endif
150 } __attribute__ ((packed)) report_keyboard_t;
151
152 typedef struct {
153 #ifdef MOUSE_SHARED_EP
154     uint8_t report_id;
155 #endif
156     uint8_t buttons;
157     int8_t x;
158     int8_t y;
159     int8_t v;
160     int8_t h;
161 } __attribute__ ((packed)) report_mouse_t;
162
163
164 /* keycode to system usage */
165 #define KEYCODE2SYSTEM(key) \
166     (key == KC_SYSTEM_POWER ? SYSTEM_POWER_DOWN : \
167     (key == KC_SYSTEM_SLEEP ? SYSTEM_SLEEP : \
168     (key == KC_SYSTEM_WAKE  ? SYSTEM_WAKE_UP : 0)))
169
170 /* keycode to consumer usage */
171 #define KEYCODE2CONSUMER(key) \
172     (key == KC_AUDIO_MUTE       ?  AUDIO_MUTE : \
173     (key == KC_AUDIO_VOL_UP     ?  AUDIO_VOL_UP : \
174     (key == KC_AUDIO_VOL_DOWN   ?  AUDIO_VOL_DOWN : \
175     (key == KC_MEDIA_NEXT_TRACK ?  TRANSPORT_NEXT_TRACK : \
176     (key == KC_MEDIA_PREV_TRACK ?  TRANSPORT_PREV_TRACK : \
177     (key == KC_MEDIA_FAST_FORWARD ?  TRANSPORT_FAST_FORWARD : \
178     (key == KC_MEDIA_REWIND     ?  TRANSPORT_REWIND : \
179     (key == KC_MEDIA_STOP       ?  TRANSPORT_STOP : \
180     (key == KC_MEDIA_EJECT      ?  TRANSPORT_STOP_EJECT : \
181     (key == KC_MEDIA_PLAY_PAUSE ?  TRANSPORT_PLAY_PAUSE : \
182     (key == KC_MEDIA_SELECT     ?  AL_CC_CONFIG : \
183     (key == KC_MAIL             ?  AL_EMAIL : \
184     (key == KC_CALCULATOR       ?  AL_CALCULATOR : \
185     (key == KC_MY_COMPUTER      ?  AL_LOCAL_BROWSER : \
186     (key == KC_WWW_SEARCH       ?  AC_SEARCH : \
187     (key == KC_WWW_HOME         ?  AC_HOME : \
188     (key == KC_WWW_BACK         ?  AC_BACK : \
189     (key == KC_WWW_FORWARD      ?  AC_FORWARD : \
190     (key == KC_WWW_STOP         ?  AC_STOP : \
191     (key == KC_WWW_REFRESH      ?  AC_REFRESH : \
192     (key == KC_WWW_FAVORITES    ?  AC_BOOKMARKS : 0)))))))))))))))))))))
193
194 uint8_t has_anykey(report_keyboard_t* keyboard_report);
195 uint8_t get_first_key(report_keyboard_t* keyboard_report);
196
197 void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
198 void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
199 #ifdef NKRO_ENABLE
200 void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
201 void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
202 #endif
203
204 void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key);
205 void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key);
206 void clear_keys_from_report(report_keyboard_t* keyboard_report);
207
208 #ifdef __cplusplus
209 }
210 #endif
211
212 #endif