Debian Codegraph @

3rdparty/hidapi/windows/hidapi_descriptor_reconstruct.h

raw symbols SCIP
1 /*******************************************************
2 HIDAPI - Multi-Platform library for
3 communication with HID devices.
4
5 libusb/hidapi Team
6
7 Copyright 2022, All Rights Reserved.
8
9 At the discretion of the user of this library,
10 this software may be licensed under the terms of the
11 GNU General Public License v3, a BSD-Style license, or the
12 original HIDAPI license as outlined in the LICENSE.txt,
13 LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
14 files located at the root of the source distribution.
15 These files may also be found in the public source
16 code repository located at:
17 https://github.com/libusb/hidapi .
18 ********************************************************/
19 #ifndef HIDAPI_DESCRIPTOR_RECONSTRUCT_H__
20 #define HIDAPI_DESCRIPTOR_RECONSTRUCT_H__
21
22 #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
23 /* Do not warn about wcsncpy usage.
24 https://docs.microsoft.com/cpp/c-runtime-library/security-features-in-the-crt */
25 #define _CRT_SECURE_NO_WARNINGS
26 #endif
27
28 #include "hidapi_winapi.h"
29
30 #ifdef _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable: 4200)
33 #pragma warning(disable: 4201)
34 #pragma warning(disable: 4214)
35 #endif
36
37 #include <windows.h>
38
39 #include "hidapi_hidsdi.h"
40 #include <assert.h>
41
42 #define NUM_OF_HIDP_REPORT_TYPES 3
43
44 typedef enum rd_items_ {
45 rd_main_input = 0x80, /* 1000 00 nn */
46 rd_main_output = 0x90, /* 1001 00 nn */
47 rd_main_feature = 0xB0, /* 1011 00 nn */
48 rd_main_collection = 0xA0, /* 1010 00 nn */
49 rd_main_collection_end = 0xC0, /* 1100 00 nn */
50 rd_global_usage_page = 0x04, /* 0000 01 nn */
51 rd_global_logical_minimum = 0x14, /* 0001 01 nn */
52 rd_global_logical_maximum = 0x24, /* 0010 01 nn */
53 rd_global_physical_minimum = 0x34, /* 0011 01 nn */
54 rd_global_physical_maximum = 0x44, /* 0100 01 nn */
55 rd_global_unit_exponent = 0x54, /* 0101 01 nn */
56 rd_global_unit = 0x64, /* 0110 01 nn */
57 rd_global_report_size = 0x74, /* 0111 01 nn */
58 rd_global_report_id = 0x84, /* 1000 01 nn */
59 rd_global_report_count = 0x94, /* 1001 01 nn */
60 rd_global_push = 0xA4, /* 1010 01 nn */
61 rd_global_pop = 0xB4, /* 1011 01 nn */
62 rd_local_usage = 0x08, /* 0000 10 nn */
63 rd_local_usage_minimum = 0x18, /* 0001 10 nn */
64 rd_local_usage_maximum = 0x28, /* 0010 10 nn */
65 rd_local_designator_index = 0x38, /* 0011 10 nn */
66 rd_local_designator_minimum = 0x48, /* 0100 10 nn */
67 rd_local_designator_maximum = 0x58, /* 0101 10 nn */
68 rd_local_string = 0x78, /* 0111 10 nn */
69 rd_local_string_minimum = 0x88, /* 1000 10 nn */
70 rd_local_string_maximum = 0x98, /* 1001 10 nn */
71 rd_local_delimiter = 0xA8 /* 1010 10 nn */
72 } rd_items;
73
74 typedef enum rd_main_items_ {
75 rd_input = HidP_Input,
76 rd_output = HidP_Output,
77 rd_feature = HidP_Feature,
78 rd_collection,
79 rd_collection_end,
80 rd_delimiter_open,
81 rd_delimiter_usage,
82 rd_delimiter_close,
83 } rd_main_items;
84
85 typedef struct rd_bit_range_ {
86 int FirstBit;
87 int LastBit;
88 } rd_bit_range;
89
90 typedef enum rd_item_node_type_ {
91 rd_item_node_cap,
92 rd_item_node_padding,
93 rd_item_node_collection,
94 } rd_node_type;
95
96 struct rd_main_item_node {
97 int FirstBit; /* Position of first bit in report (counting from 0) */
98 int LastBit; /* Position of last bit in report (counting from 0) */
99 rd_node_type TypeOfNode; /* Information if caps index refers to the array of button caps, value caps,
100 or if the node is just a padding element to fill unused bit positions.
101 The node can also be a collection node without any bits in the report. */
102 int CapsIndex; /* Index in the array of caps */
103 int CollectionIndex; /* Index in the array of link collections */
104 rd_main_items MainItemType; /* Input, Output, Feature, Collection or Collection End */
105 unsigned char ReportID;
106 struct rd_main_item_node* next;
107 };
108
109 typedef struct hid_pp_caps_info_ {
110 USHORT FirstCap;
111 USHORT NumberOfCaps; // Includes empty caps after LastCap
112 USHORT LastCap;
113 USHORT ReportByteLength;
114 } hid_pp_caps_info, *phid_pp_caps_info;
115
116 typedef struct hid_pp_link_collection_node_ {
117 USAGE LinkUsage;
118 USAGE LinkUsagePage;
119 USHORT Parent;
120 USHORT NumberOfChildren;
121 USHORT NextSibling;
122 USHORT FirstChild;
123 ULONG CollectionType : 8;
124 ULONG IsAlias : 1;
125 ULONG Reserved : 23;
126 // Same as the public API structure HIDP_LINK_COLLECTION_NODE, but without PVOID UserContext at the end
127 } hid_pp_link_collection_node, *phid_pp_link_collection_node;
128
129 // Note: This is risk-reduction-measure for this specific struct, as it has ULONG bit-field.
130 // Although very unlikely, it might still be possible that the compiler creates a memory layout that is
131 // not binary compatile.
132 // Other structs are not checked at the time of writing.
133 static_assert(sizeof(struct hid_pp_link_collection_node_) == 16,
134 "Size of struct hid_pp_link_collection_node_ not as expected. This might break binary compatibility");
135
136 typedef struct hidp_unknown_token_ {
137 UCHAR Token; /* Specifies the one-byte prefix of a global item. */
138 UCHAR Reserved[3];
139 ULONG BitField; /* Specifies the data part of the global item. */
140 } hidp_unknown_token, * phidp_unknown_token;
141
142 typedef struct hid_pp_cap_ {
143 USAGE UsagePage;
144 UCHAR ReportID;
145 UCHAR BitPosition;
146 USHORT ReportSize; // WIN32 term for this is BitSize
147 USHORT ReportCount;
148 USHORT BytePosition;
149 USHORT BitCount;
150 ULONG BitField;
151 USHORT NextBytePosition;
152 USHORT LinkCollection;
153 USAGE LinkUsagePage;
154 USAGE LinkUsage;
155
156 // Start of 8 Flags in one byte
157 BOOLEAN IsMultipleItemsForArray:1;
158
159 BOOLEAN IsPadding:1;
160 BOOLEAN IsButtonCap:1;
161 BOOLEAN IsAbsolute:1;
162 BOOLEAN IsRange:1;
163 BOOLEAN IsAlias:1; // IsAlias is set to TRUE in the first n-1 capability structures added to the capability array. IsAlias set to FALSE in the nth capability structure.
164 BOOLEAN IsStringRange:1;
165 BOOLEAN IsDesignatorRange:1;
166 // End of 8 Flags in one byte
167 BOOLEAN Reserved1[3];
168
169 hidp_unknown_token UnknownTokens[4]; // 4 x 8 Byte
170
171 union {
172 struct {
173 USAGE UsageMin;
174 USAGE UsageMax;
175 USHORT StringMin;
176 USHORT StringMax;
177 USHORT DesignatorMin;
178 USHORT DesignatorMax;
179 USHORT DataIndexMin;
180 USHORT DataIndexMax;
181 } Range;
182 struct {
183 USAGE Usage;
184 USAGE Reserved1;
185 USHORT StringIndex;
186 USHORT Reserved2;
187 USHORT DesignatorIndex;
188 USHORT Reserved3;
189 USHORT DataIndex;
190 USHORT Reserved4;
191 } NotRange;
192 };
193 union {
194 struct {
195 LONG LogicalMin;
196 LONG LogicalMax;
197 } Button;
198 struct {
199 BOOLEAN HasNull;
200 UCHAR Reserved4[3];
201 LONG LogicalMin;
202 LONG LogicalMax;
203 LONG PhysicalMin;
204 LONG PhysicalMax;
205 } NotButton;
206 };
207 ULONG Units;
208 ULONG UnitsExp;
209
210 } hid_pp_cap, *phid_pp_cap;
211
212 typedef struct hidp_preparsed_data_ {
213 UCHAR MagicKey[8];
214 USAGE Usage;
215 USAGE UsagePage;
216 USHORT Reserved[2];
217
218 // CAPS structure for Input, Output and Feature
219 hid_pp_caps_info caps_info[3];
220
221 USHORT FirstByteOfLinkCollectionArray;
222 USHORT NumberLinkCollectionNodes;
223
224 #ifndef _MSC_VER
225 // MINGW fails with: Flexible array member in union not supported
226 // Solution: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
227 union {
228 #pragma GCC diagnostic push
229 #pragma GCC diagnostic ignored "-Wpedantic"
230 hid_pp_cap caps[0];
231 hid_pp_link_collection_node LinkCollectionArray[0];
232 #pragma GCC diagnostic pop
233 };
234 #else
235 union {
236 hid_pp_cap caps[];
237 hid_pp_link_collection_node LinkCollectionArray[];
238 };
239 #endif
240
241 } hidp_preparsed_data;
242
243 #ifdef _MSC_VER
244 #pragma warning(pop)
245 #endif
246
247 #endif