Add hid_init() and hid_exit()

The really missing function was hid_exit() to free ressources allocated
by the HIDAPI and libusb libraries.
This commit is contained in:
Ludovic Rousseau 2011-07-19 15:18:41 +02:00 committed by Alan Ott
parent a8768da46f
commit 529fc51487
2 changed files with 46 additions and 10 deletions

View File

@ -79,6 +79,24 @@ extern "C" {
};
/** @brief Initialize the HIDAPI library.
@ingroup API
@returns
-1 on error.
*/
int HID_API_EXPORT HID_API_CALL hid_init(void);
/** @brief Finalize the HIDAPI library.
@ingroup API
@returns
-1 on error.
*/
int HID_API_EXPORT HID_API_CALL hid_exit(void);
/** @brief Enumerate the HID Devices.
This function returns a linked list of all the HID devices

View File

@ -378,6 +378,28 @@ static char *make_path(libusb_device *dev, int interface_number)
return strdup(str);
}
int HID_API_EXPORT hid_init(void)
{
if (!initialized) {
if (libusb_init(NULL))
return -1;
initialized = 1;
}
return 0;
}
int HID_API_EXPORT hid_exit(void)
{
if (initialized) {
libusb_exit(NULL);
initialized = 0;
}
return 0;
}
struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{
libusb_device **devs;
@ -391,11 +413,9 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
setlocale(LC_ALL,"");
if (!initialized) {
libusb_init(NULL);
initialized = 1;
}
if (!initialized)
hid_init();
num_devs = libusb_get_device_list(NULL, &devs);
if (num_devs < 0)
return NULL;
@ -729,11 +749,9 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
setlocale(LC_ALL,"");
if (!initialized) {
libusb_init(NULL);
initialized = 1;
}
if (!initialized)
hid_init();
num_devs = libusb_get_device_list(NULL, &devs);
while ((usb_dev = devs[d++]) != NULL) {
struct libusb_device_descriptor desc;