aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/report.h
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-10-28 22:31:59 +0100
committerGitHub <noreply@github.com>2021-10-28 22:31:59 +0100
commitdcfffa7b67a072f7d9e37bd8c0029c53b61aeb0f (patch)
tree65491c3878ee76f50c4b46a318503cc27fdb4a6b /tmk_core/protocol/report.h
parent0c87e2e7025140ca6105b7fec2639c78c3cd8109 (diff)
downloadqmk_firmware-dcfffa7b67a072f7d9e37bd8c0029c53b61aeb0f.tar.gz
qmk_firmware-dcfffa7b67a072f7d9e37bd8c0029c53b61aeb0f.zip
Relocate protocol files within tmk_core/common/ (#14972)
* Relocate non platform files within tmk_core/common/ * clang
Diffstat (limited to 'tmk_core/protocol/report.h')
-rw-r--r--tmk_core/protocol/report.h325
1 files changed, 325 insertions, 0 deletions
diff --git a/tmk_core/protocol/report.h b/tmk_core/protocol/report.h
new file mode 100644
index 000000000..1adc892f3
--- /dev/null
+++ b/tmk_core/protocol/report.h
@@ -0,0 +1,325 @@
1/*
2Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20#include <stdint.h>
21#include <stdbool.h>
22#include "keycode.h"
23
24// clang-format off
25
26/* HID report IDs */
27enum hid_report_ids {
28 REPORT_ID_KEYBOARD = 1,
29 REPORT_ID_MOUSE,
30 REPORT_ID_SYSTEM,
31 REPORT_ID_CONSUMER,
32 REPORT_ID_PROGRAMMABLE_BUTTON,
33 REPORT_ID_NKRO,
34 REPORT_ID_JOYSTICK,
35 REPORT_ID_DIGITIZER
36};
37
38/* Mouse buttons */
39#define MOUSE_BTN_MASK(n) (1 << (n))
40enum mouse_buttons {
41 MOUSE_BTN1 = MOUSE_BTN_MASK(0),
42 MOUSE_BTN2 = MOUSE_BTN_MASK(1),
43 MOUSE_BTN3 = MOUSE_BTN_MASK(2),
44 MOUSE_BTN4 = MOUSE_BTN_MASK(3),
45 MOUSE_BTN5 = MOUSE_BTN_MASK(4),
46 MOUSE_BTN6 = MOUSE_BTN_MASK(5),
47 MOUSE_BTN7 = MOUSE_BTN_MASK(6),
48 MOUSE_BTN8 = MOUSE_BTN_MASK(7)
49};
50
51/* Consumer Page (0x0C)
52 *
53 * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=75
54 */
55enum consumer_usages {
56 // 15.5 Display Controls
57 SNAPSHOT = 0x065,
58 BRIGHTNESS_UP = 0x06F, // https://www.usb.org/sites/default/files/hutrr41_0.pdf
59 BRIGHTNESS_DOWN = 0x070,
60 // 15.7 Transport Controls
61 TRANSPORT_RECORD = 0x0B2,
62 TRANSPORT_FAST_FORWARD = 0x0B3,
63 TRANSPORT_REWIND = 0x0B4,
64 TRANSPORT_NEXT_TRACK = 0x0B5,
65 TRANSPORT_PREV_TRACK = 0x0B6,
66 TRANSPORT_STOP = 0x0B7,
67 TRANSPORT_EJECT = 0x0B8,
68 TRANSPORT_RANDOM_PLAY = 0x0B9,
69 TRANSPORT_STOP_EJECT = 0x0CC,
70 TRANSPORT_PLAY_PAUSE = 0x0CD,
71 // 15.9.1 Audio Controls - Volume
72 AUDIO_MUTE = 0x0E2,
73 AUDIO_VOL_UP = 0x0E9,
74 AUDIO_VOL_DOWN = 0x0EA,
75 // 15.15 Application Launch Buttons
76 AL_CC_CONFIG = 0x183,
77 AL_EMAIL = 0x18A,
78 AL_CALCULATOR = 0x192,
79 AL_LOCAL_BROWSER = 0x194,
80 AL_LOCK = 0x19E,
81 AL_CONTROL_PANEL = 0x19F,
82 AL_ASSISTANT = 0x1CB,
83 AL_KEYBOARD_LAYOUT = 0x1AE,
84 // 15.16 Generic GUI Application Controls
85 AC_NEW = 0x201,
86 AC_OPEN = 0x202,
87 AC_CLOSE = 0x203,
88 AC_EXIT = 0x204,
89 AC_MAXIMIZE = 0x205,
90 AC_MINIMIZE = 0x206,
91 AC_SAVE = 0x207,
92 AC_PRINT = 0x208,
93 AC_PROPERTIES = 0x209,
94 AC_UNDO = 0x21A,
95 AC_COPY = 0x21B,
96 AC_CUT = 0x21C,
97 AC_PASTE = 0x21D,
98 AC_SELECT_ALL = 0x21E,
99 AC_FIND = 0x21F,
100 AC_SEARCH = 0x221,
101 AC_HOME = 0x223,
102 AC_BACK = 0x224,
103 AC_FORWARD = 0x225,
104 AC_STOP = 0x226,
105 AC_REFRESH = 0x227,
106 AC_BOOKMARKS = 0x22A
107};
108
109/* Generic Desktop Page (0x01)
110 *
111 * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=26
112 */
113enum 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
123// clang-format on
124
125#define NKRO_SHARED_EP
126/* key report size(NKRO or boot mode) */
127#if defined(NKRO_ENABLE)
128# if defined(PROTOCOL_LUFA) || defined(PROTOCOL_CHIBIOS)
129# include "protocol/usb_descriptor.h"
130# define KEYBOARD_REPORT_BITS (SHARED_EPSIZE - 2)
131# elif defined(PROTOCOL_ARM_ATSAM)
132# include "protocol/arm_atsam/usb/udi_device_epsize.h"
133# define KEYBOARD_REPORT_BITS (NKRO_EPSIZE - 1)
134# undef NKRO_SHARED_EP
135# undef MOUSE_SHARED_EP
136# else
137# error "NKRO not supported with this protocol"
138# endif
139#endif
140
141#ifdef KEYBOARD_SHARED_EP
142# define KEYBOARD_REPORT_SIZE 9
143#else
144# define KEYBOARD_REPORT_SIZE 8
145#endif
146
147#define KEYBOARD_REPORT_KEYS 6
148
149#ifdef __cplusplus
150extern "C" {
151#endif
152
153/*
154 * keyboard report is 8-byte array retains state of 8 modifiers and 6 keys.
155 *
156 * byte |0 |1 |2 |3 |4 |5 |6 |7
157 * -----+--------+--------+--------+--------+--------+--------+--------+--------
158 * desc |mods |reserved|keys[0] |keys[1] |keys[2] |keys[3] |keys[4] |keys[5]
159 *
160 * It is exended to 16 bytes to retain 120keys+8mods when NKRO mode.
161 *
162 * byte |0 |1 |2 |3 |4 |5 |6 |7 ... |15
163 * -----+--------+--------+--------+--------+--------+--------+--------+-------- +--------
164 * desc |mods |bits[0] |bits[1] |bits[2] |bits[3] |bits[4] |bits[5] |bits[6] ... |bit[14]
165 *
166 * mods retains state of 8 modifiers.
167 *
168 * bit |0 |1 |2 |3 |4 |5 |6 |7
169 * -----+--------+--------+--------+--------+--------+--------+--------+--------
170 * desc |Lcontrol|Lshift |Lalt |Lgui |Rcontrol|Rshift |Ralt |Rgui
171 *
172 */
173typedef union {
174 uint8_t raw[KEYBOARD_REPORT_SIZE];
175 struct {
176#ifdef KEYBOARD_SHARED_EP
177 uint8_t report_id;
178#endif
179 uint8_t mods;
180 uint8_t reserved;
181 uint8_t keys[KEYBOARD_REPORT_KEYS];
182 };
183#ifdef NKRO_ENABLE
184 struct nkro_report {
185# ifdef NKRO_SHARED_EP
186 uint8_t report_id;
187# endif
188 uint8_t mods;
189 uint8_t bits[KEYBOARD_REPORT_BITS];
190 } nkro;
191#endif
192} __attribute__((packed)) report_keyboard_t;
193
194typedef struct {
195 uint8_t report_id;
196 uint16_t usage;
197} __attribute__((packed)) report_extra_t;
198
199typedef struct {
200 uint8_t report_id;
201 uint32_t usage;
202} __attribute__((packed)) report_programmable_button_t;
203
204typedef struct {
205#ifdef MOUSE_SHARED_EP
206 uint8_t report_id;
207#endif
208 uint8_t buttons;
209 int8_t x;
210 int8_t y;
211 int8_t v;
212 int8_t h;
213} __attribute__((packed)) report_mouse_t;
214
215typedef struct {
216#ifdef DIGITIZER_SHARED_EP
217 uint8_t report_id;
218#endif
219 uint8_t tip : 1;
220 uint8_t inrange : 1;
221 uint8_t pad2 : 6;
222 uint16_t x;
223 uint16_t y;
224} __attribute__((packed)) report_digitizer_t;
225
226typedef struct {
227#if JOYSTICK_AXES_COUNT > 0
228# if JOYSTICK_AXES_RESOLUTION > 8
229 int16_t axes[JOYSTICK_AXES_COUNT];
230# else
231 int8_t axes[JOYSTICK_AXES_COUNT];
232# endif
233#endif
234
235#if JOYSTICK_BUTTON_COUNT > 0
236 uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
237#endif
238} __attribute__((packed)) joystick_report_t;
239
240/* keycode to system usage */
241static inline uint16_t KEYCODE2SYSTEM(uint8_t key) {
242 switch (key) {
243 case KC_SYSTEM_POWER:
244 return SYSTEM_POWER_DOWN;
245 case KC_SYSTEM_SLEEP:
246 return SYSTEM_SLEEP;
247 case KC_SYSTEM_WAKE:
248 return SYSTEM_WAKE_UP;
249 default:
250 return 0;
251 }
252}
253
254/* keycode to consumer usage */
255static inline uint16_t KEYCODE2CONSUMER(uint8_t key) {
256 switch (key) {
257 case KC_AUDIO_MUTE:
258 return AUDIO_MUTE;
259 case KC_AUDIO_VOL_UP:
260 return AUDIO_VOL_UP;
261 case KC_AUDIO_VOL_DOWN:
262 return AUDIO_VOL_DOWN;
263 case KC_MEDIA_NEXT_TRACK:
264 return TRANSPORT_NEXT_TRACK;
265 case KC_MEDIA_PREV_TRACK:
266 return TRANSPORT_PREV_TRACK;
267 case KC_MEDIA_FAST_FORWARD:
268 return TRANSPORT_FAST_FORWARD;
269 case KC_MEDIA_REWIND:
270 return TRANSPORT_REWIND;
271 case KC_MEDIA_STOP:
272 return TRANSPORT_STOP;
273 case KC_MEDIA_EJECT:
274 return TRANSPORT_STOP_EJECT;
275 case KC_MEDIA_PLAY_PAUSE:
276 return TRANSPORT_PLAY_PAUSE;
277 case KC_MEDIA_SELECT:
278 return AL_CC_CONFIG;
279 case KC_MAIL:
280 return AL_EMAIL;
281 case KC_CALCULATOR:
282 return AL_CALCULATOR;
283 case KC_MY_COMPUTER:
284 return AL_LOCAL_BROWSER;
285 case KC_WWW_SEARCH:
286 return AC_SEARCH;
287 case KC_WWW_HOME:
288 return AC_HOME;
289 case KC_WWW_BACK:
290 return AC_BACK;
291 case KC_WWW_FORWARD:
292 return AC_FORWARD;
293 case KC_WWW_STOP:
294 return AC_STOP;
295 case KC_WWW_REFRESH:
296 return AC_REFRESH;
297 case KC_BRIGHTNESS_UP:
298 return BRIGHTNESS_UP;
299 case KC_BRIGHTNESS_DOWN:
300 return BRIGHTNESS_DOWN;
301 case KC_WWW_FAVORITES:
302 return AC_BOOKMARKS;
303 default:
304 return 0;
305 }
306}
307
308uint8_t has_anykey(report_keyboard_t* keyboard_report);
309uint8_t get_first_key(report_keyboard_t* keyboard_report);
310bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key);
311
312void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
313void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
314#ifdef NKRO_ENABLE
315void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
316void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code);
317#endif
318
319void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key);
320void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key);
321void clear_keys_from_report(report_keyboard_t* keyboard_report);
322
323#ifdef __cplusplus
324}
325#endif