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. If no packet was available to be read within
209 				the timeout period, this function returns 0.
210 		*/
211 int hid_read_timeout (hid_device* dev, ubyte* data, size_t length, int milliseconds);
212 
213 /** @brief Read an Input report from a HID device.
214 
215 			Input reports are returned
216 		    to the host through the INTERRUPT IN endpoint. The first byte will
217 			contain the Report number if the device uses numbered reports.
218 
219 			@ingroup API
220 			@param device A device handle returned from hid_open().
221 			@param data A buffer to put the read data into.
222 			@param length The number of bytes to read. For devices with
223 				multiple reports, make sure to read an extra byte for
224 				the report number.
225 
226 			@returns
227 				This function returns the actual number of bytes read and
228 				-1 on error. If no packet was available to be read and
229 				the handle is in non-blocking mode, this function returns 0.
230 		*/
231 int hid_read (hid_device* device, ubyte* data, size_t length);
232 
233 /** @brief Set the device handle to be non-blocking.
234 
235 			In non-blocking mode calls to hid_read() will return
236 			immediately with a value of 0 if there is no data to be
237 			read. In blocking mode, hid_read() will wait (block) until
238 			there is data to read before returning.
239 
240 			Nonblocking can be turned on and off at any time.
241 
242 			@ingroup API
243 			@param device A device handle returned from hid_open().
244 			@param nonblock enable or not the nonblocking reads
245 			 - 1 to enable nonblocking
246 			 - 0 to disable nonblocking.
247 
248 			@returns
249 				This function returns 0 on success and -1 on error.
250 		*/
251 int hid_set_nonblocking (hid_device* device, int nonblock);
252 
253 /** @brief Send a Feature report to the device.
254 
255 			Feature reports are sent over the Control endpoint as a
256 			Set_Report transfer.  The first byte of @p data[] must
257 			contain the Report ID. For devices which only support a
258 			single report, this must be set to 0x0. The remaining bytes
259 			contain the report data. Since the Report ID is mandatory,
260 			calls to hid_send_feature_report() will always contain one
261 			more byte than the report contains. For example, if a hid
262 			report is 16 bytes long, 17 bytes must be passed to
263 			hid_send_feature_report(): the Report ID (or 0x0, for
264 			devices which do not use numbered reports), followed by the
265 			report data (16 bytes). In this example, the length passed
266 			in would be 17.
267 
268 			@ingroup API
269 			@param device A device handle returned from hid_open().
270 			@param data The data to send, including the report number as
271 				the first byte.
272 			@param length The length in bytes of the data to send, including
273 				the report number.
274 
275 			@returns
276 				This function returns the actual number of bytes written and
277 				-1 on error.
278 		*/
279 int hid_send_feature_report (hid_device* device, const(ubyte)* data, size_t length);
280 
281 /** @brief Get a feature report from a HID device.
282 
283 			Set the first byte of @p data[] to the Report ID of the
284 			report to be read.  Make sure to allow space for this
285 			extra byte in @p data[]. Upon return, the first byte will
286 			still contain the Report ID, and the report data will
287 			start in data[1].
288 
289 			@ingroup API
290 			@param device A device handle returned from hid_open().
291 			@param data A buffer to put the read data into, including
292 				the Report ID. Set the first byte of @p data[] to the
293 				Report ID of the report to be read, or set it to zero
294 				if your device does not use numbered reports.
295 			@param length The number of bytes to read, including an
296 				extra byte for the report ID. The buffer can be longer
297 				than the actual report.
298 
299 			@returns
300 				This function returns the number of bytes read plus
301 				one for the report ID (which is still in the first
302 				byte), or -1 on error.
303 		*/
304 int hid_get_feature_report (hid_device* device, ubyte* data, size_t length);
305 
306 /** @brief Close a HID device.
307 
308 			@ingroup API
309 			@param device A device handle returned from hid_open().
310 		*/
311 void hid_close (hid_device* device);
312 
313 /** @brief Get The Manufacturer String from a HID device.
314 
315 			@ingroup API
316 			@param device A device handle returned from hid_open().
317 			@param string A wide string buffer to put the data into.
318 			@param maxlen The length of the buffer in multiples of wchar_t.
319 
320 			@returns
321 				This function returns 0 on success and -1 on error.
322 		*/
323 int hid_get_manufacturer_string (hid_device* device, wchar_t* string, size_t maxlen);
324 
325 /** @brief Get The Product String from a HID device.
326 
327 			@ingroup API
328 			@param device A device handle returned from hid_open().
329 			@param string A wide string buffer to put the data into.
330 			@param maxlen The length of the buffer in multiples of wchar_t.
331 
332 			@returns
333 				This function returns 0 on success and -1 on error.
334 		*/
335 int hid_get_product_string (hid_device* device, wchar_t* string, size_t maxlen);
336 
337 /** @brief Get The Serial Number String from a HID device.
338 
339 			@ingroup API
340 			@param device A device handle returned from hid_open().
341 			@param string A wide string buffer to put the data into.
342 			@param maxlen The length of the buffer in multiples of wchar_t.
343 
344 			@returns
345 				This function returns 0 on success and -1 on error.
346 		*/
347 int hid_get_serial_number_string (hid_device* device, wchar_t* string, size_t maxlen);
348 
349 /** @brief Get a string from a HID device, based on its string index.
350 
351 			@ingroup API
352 			@param device A device handle returned from hid_open().
353 			@param string_index The index of the string to get.
354 			@param string A wide string buffer to put the data into.
355 			@param maxlen The length of the buffer in multiples of wchar_t.
356 
357 			@returns
358 				This function returns 0 on success and -1 on error.
359 		*/
360 int hid_get_indexed_string (hid_device* device, int string_index, wchar_t* string, size_t maxlen);
361 
362 /** @brief Get a string describing the last error which occurred.
363 
364 			@ingroup API
365 			@param device A device handle returned from hid_open().
366 
367 			@returns
368 				This function returns a string containing the last error
369 				which occurred or NULL if none has occurred.
370 		*/
371 const(wchar_t)* hid_error (hid_device* device);
372