1 module hidapi.bindings;
2 /*******************************************************
3  HIDAPI - Multi-Platform library for
4  communication with HID devices.
5 
6  Alan Ott
7  Signal 11 Software
8 
9  8/22/2009
10 
11  Copyright 2009, All Rights Reserved.
12 
13  At the discretion of the user of this library,
14  this software may be licensed under the terms of the
15  GNU General Public License v3, a BSD-Style license, or the
16  original HIDAPI license as outlined in the LICENSE.txt,
17  LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
18  files located at the root of the source distribution.
19  These files may also be found in the public source
20  code repository located at:
21         http://github.com/signal11/hidapi .
22 ********************************************************/
23 
24 /** @file
25  * @defgroup API hidapi API
26  */
27 
28 import core.stdc.stddef;
29 
30 extern (C): /**< API export macro */ /**< API call macro */ /**< API export and call macro*/
31 
32 struct hid_device_;
33 alias hid_device = hid_device_; /**< opaque hidapi structure */
34 
35 /** hidapi info structure */
36 struct hid_device_info
37 {
38     /** Platform-specific device path */
39     char* path;
40     /** Device Vendor ID */
41     ushort vendor_id;
42     /** Device Product ID */
43     ushort product_id;
44     /** Serial Number */
45     wchar_t* serial_number;
46     /** Device Release Number in binary-coded decimal,
47     			    also known as Device Version Number */
48     ushort release_number;
49     /** Manufacturer String */
50     wchar_t* manufacturer_string;
51     /** Product string */
52     wchar_t* product_string;
53     /** Usage Page for this Device/Interface
54     			    (Windows/Mac only). */
55     ushort usage_page;
56     /** Usage for this Device/Interface
57     			    (Windows/Mac only).*/
58     ushort usage;
59     /** The USB interface which this logical device
60     			    represents. Valid on both Linux implementations
61     			    in all cases, and valid on the Windows implementation
62     			    only if the device contains more than one interface. */
63     int interface_number;
64 
65     /** Pointer to the next device */
66     hid_device_info* next;
67 }
68 
69 /** @brief Initialize the HIDAPI library.
70 
71 			This function initializes the HIDAPI library. Calling it is not
72 			strictly necessary, as it will be called automatically by
73 			hid_enumerate() and any of the hid_open_*() functions if it is
74 			needed.  This function should be called at the beginning of
75 			execution however, if there is a chance of HIDAPI handles
76 			being opened by different threads simultaneously.
77 
78 			@ingroup API
79 
80 			@returns
81 				This function returns 0 on success and -1 on error.
82 		*/
83 int hid_init ();
84 
85 /** @brief Finalize the HIDAPI library.
86 
87 			This function frees all of the static data associated with
88 			HIDAPI. It should be called at the end of execution to avoid
89 			memory leaks.
90 
91 			@ingroup API
92 
93 		    @returns
94 				This function returns 0 on success and -1 on error.
95 		*/
96 int hid_exit ();
97 
98 /** @brief Enumerate the HID Devices.
99 
100 			This function returns a linked list of all the HID devices
101 			attached to the system which match vendor_id and product_id.
102 			If @p vendor_id is set to 0 then any vendor matches.
103 			If @p product_id is set to 0 then any product matches.
104 			If @p vendor_id and @p product_id are both set to 0, then
105 			all HID devices will be returned.
106 
107 			@ingroup API
108 			@param vendor_id The Vendor ID (VID) of the types of device
109 				to open.
110 			@param product_id The Product ID (PID) of the types of
111 				device to open.
112 
113 		    @returns
114 		    	This function returns a pointer to a linked list of type
115 		    	struct #hid_device, containing information about the HID devices
116 		    	attached to the system, or NULL in the case of failure. Free
117 		    	this linked list by calling hid_free_enumeration().
118 		*/
119 hid_device_info* hid_enumerate (ushort vendor_id, ushort product_id);
120 
121 /** @brief Free an enumeration Linked List
122 
123 		    This function frees a linked list created by hid_enumerate().
124 
125 			@ingroup API
126 		    @param devs Pointer to a list of struct_device returned from
127 		    	      hid_enumerate().
128 		*/
129 void hid_free_enumeration (hid_device_info* devs);
130 
131 /** @brief Open a HID device using a Vendor ID (VID), Product ID
132 			(PID) and optionally a serial number.
133 
134 			If @p serial_number is NULL, the first device with the
135 			specified VID and PID is opened.
136 
137 			@ingroup API
138 			@param vendor_id The Vendor ID (VID) of the device to open.
139 			@param product_id The Product ID (PID) of the device to open.
140 			@param serial_number The Serial Number of the device to open
141 				               (Optionally NULL).
142 
143 			@returns
144 				This function returns a pointer to a #hid_device object on
145 				success or NULL on failure.
146 		*/
147 hid_device* hid_open (ushort vendor_id, ushort product_id, const(wchar_t)* serial_number);
148 
149 /** @brief Open a HID device by its path name.
150 
151 			The path name be determined by calling hid_enumerate(), or a
152 			platform-specific path name can be used (eg: /dev/hidraw0 on
153 			Linux).
154 
155 			@ingroup API
156 		    @param path The path name of the device to open
157 
158 			@returns
159 				This function returns a pointer to a #hid_device object on
160 				success or NULL on failure.
161 		*/
162 hid_device* hid_open_path (const(char)* path);
163 
164 /** @brief Write an Output report to a HID device.
165 
166 			The first byte of @p data[] must contain the Report ID. For
167 			devices which only support a single report, this must be set
168 			to 0x0. The remaining bytes contain the report data. Since
169 			the Report ID is mandatory, calls to hid_write() will always
170 			contain one more byte than the report contains. For example,
171 			if a hid report is 16 bytes long, 17 bytes must be passed to
172 			hid_write(), the Report ID (or 0x0, for devices with a
173 			single report), followed by the report data (16 bytes). In
174 			this example, the length passed in would be 17.
175 
176 			hid_write() will send the data on the first OUT endpoint, if
177 			one exists. If it does not, it will send the data through
178 			the Control Endpoint (Endpoint 0).
179 
180 			@ingroup API
181 			@param device A device handle returned from hid_open().
182 			@param data The data to send, including the report number as
183 				the first byte.
184 			@param length The length in bytes of the data to send.
185 
186 			@returns
187 				This function returns the actual number of bytes written and
188 				-1 on error.
189 		*/
190 int hid_write (hid_device* device, const(ubyte)* data, size_t length);
191 
192 /** @brief Read an Input report from a HID device with timeout.
193 
194 			Input reports are returned
195 			to the host through the INTERRUPT IN endpoint. The first byte will
196 			contain the Report number if the device uses numbered reports.
197 
198 			@ingroup API
199 			@param device A device handle returned from hid_open().
200 			@param data A buffer to put the read data into.
201 			@param length The number of bytes to read. For devices with
202 				multiple reports, make sure to read an extra byte for
203 				the report number.
204 			@param milliseconds timeout in milliseconds or -1 for blocking wait.
205 
206 			@returns
207 				This function returns the actual number of bytes read and
208 				-1 on error.
209 		*/
210 int hid_read_timeout (hid_device* dev, ubyte* data, size_t length, int milliseconds);
211 
212 /** @brief Read an Input report from a HID device.
213 
214 			Input reports are returned
215 		    to the host through the INTERRUPT IN endpoint. The first byte will
216 			contain the Report number if the device uses numbered reports.
217 
218 			@ingroup API
219 			@param device A device handle returned from hid_open().
220 			@param data A buffer to put the read data into.
221 			@param length The number of bytes to read. For devices with
222 				multiple reports, make sure to read an extra byte for
223 				the report number.
224 
225 			@returns
226 				This function returns the actual number of bytes read and
227 				-1 on error.
228 		*/
229 int hid_read (hid_device* device, ubyte* data, size_t length);
230 
231 /** @brief Set the device handle to be non-blocking.
232 
233 			In non-blocking mode calls to hid_read() will return
234 			immediately with a value of 0 if there is no data to be
235 			read. In blocking mode, hid_read() will wait (block) until
236 			there is data to read before returning.
237 
238 			Nonblocking can be turned on and off at any time.
239 
240 			@ingroup API
241 			@param device A device handle returned from hid_open().
242 			@param nonblock enable or not the nonblocking reads
243 			 - 1 to enable nonblocking
244 			 - 0 to disable nonblocking.
245 
246 			@returns
247 				This function returns 0 on success and -1 on error.
248 		*/
249 int hid_set_nonblocking (hid_device* device, int nonblock);
250 
251 /** @brief Send a Feature report to the device.
252 
253 			Feature reports are sent over the Control endpoint as a
254 			Set_Report transfer.  The first byte of @p data[] must
255 			contain the Report ID. For devices which only support a
256 			single report, this must be set to 0x0. The remaining bytes
257 			contain the report data. Since the Report ID is mandatory,
258 			calls to hid_send_feature_report() will always contain one
259 			more byte than the report contains. For example, if a hid
260 			report is 16 bytes long, 17 bytes must be passed to
261 			hid_send_feature_report(): the Report ID (or 0x0, for
262 			devices which do not use numbered reports), followed by the
263 			report data (16 bytes). In this example, the length passed
264 			in would be 17.
265 
266 			@ingroup API
267 			@param device A device handle returned from hid_open().
268 			@param data The data to send, including the report number as
269 				the first byte.
270 			@param length The length in bytes of the data to send, including
271 				the report number.
272 
273 			@returns
274 				This function returns the actual number of bytes written and
275 				-1 on error.
276 		*/
277 int hid_send_feature_report (hid_device* device, const(ubyte)* data, size_t length);
278 
279 /** @brief Get a feature report from a HID device.
280 
281 			Make sure to set the first byte of @p data[] to the Report
282 			ID of the report to be read.  Make sure to allow space for
283 			this extra byte in @p data[].
284 
285 			@ingroup API
286 			@param device A device handle returned from hid_open().
287 			@param data A buffer to put the read data into, including
288 				the Report ID. Set the first byte of @p data[] to the
289 				Report ID of the report to be read.
290 			@param length The number of bytes to read, including an
291 				extra byte for the report ID. The buffer can be longer
292 				than the actual report.
293 
294 			@returns
295 				This function returns the number of bytes read and
296 				-1 on error.
297 		*/
298 int hid_get_feature_report (hid_device* device, ubyte* data, size_t length);
299 
300 /** @brief Close a HID device.
301 
302 			@ingroup API
303 			@param device A device handle returned from hid_open().
304 		*/
305 void hid_close (hid_device* device);
306 
307 /** @brief Get The Manufacturer String from a HID device.
308 
309 			@ingroup API
310 			@param device A device handle returned from hid_open().
311 			@param string A wide string buffer to put the data into.
312 			@param maxlen The length of the buffer in multiples of wchar_t.
313 
314 			@returns
315 				This function returns 0 on success and -1 on error.
316 		*/
317 int hid_get_manufacturer_string (hid_device* device, wchar_t* string, size_t maxlen);
318 
319 /** @brief Get The Product String from a HID device.
320 
321 			@ingroup API
322 			@param device A device handle returned from hid_open().
323 			@param string A wide string buffer to put the data into.
324 			@param maxlen The length of the buffer in multiples of wchar_t.
325 
326 			@returns
327 				This function returns 0 on success and -1 on error.
328 		*/
329 int hid_get_product_string (hid_device* device, wchar_t* string, size_t maxlen);
330 
331 /** @brief Get The Serial Number String from a HID device.
332 
333 			@ingroup API
334 			@param device A device handle returned from hid_open().
335 			@param string A wide string buffer to put the data into.
336 			@param maxlen The length of the buffer in multiples of wchar_t.
337 
338 			@returns
339 				This function returns 0 on success and -1 on error.
340 		*/
341 int hid_get_serial_number_string (hid_device* device, wchar_t* string, size_t maxlen);
342 
343 /** @brief Get a string from a HID device, based on its string index.
344 
345 			@ingroup API
346 			@param device A device handle returned from hid_open().
347 			@param string_index The index of the string to get.
348 			@param string A wide string buffer to put the data into.
349 			@param maxlen The length of the buffer in multiples of wchar_t.
350 
351 			@returns
352 				This function returns 0 on success and -1 on error.
353 		*/
354 int hid_get_indexed_string (hid_device* device, int string_index, wchar_t* string, size_t maxlen);
355 
356 /** @brief Get a string describing the last error which occurred.
357 
358 			@ingroup API
359 			@param device A device handle returned from hid_open().
360 
361 			@returns
362 				This function returns a string containing the last error
363 				which occurred or NULL if none has occurred.
364 		*/
365 const(wchar_t)* hid_error (hid_device* device);
366