From 529fc514870fbc68744373d1c7f44d0ed872c09a Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Tue, 19 Jul 2011 15:18:41 +0200 Subject: [PATCH] Add hid_init() and hid_exit() The really missing function was hid_exit() to free ressources allocated by the HIDAPI and libusb libraries. --- hidapi/hidapi.h | 18 ++++++++++++++++++ linux/hid-libusb.c | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/hidapi/hidapi.h b/hidapi/hidapi.h index 42dabf4..b83e5b9 100644 --- a/hidapi/hidapi.h +++ b/hidapi/hidapi.h @@ -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 diff --git a/linux/hid-libusb.c b/linux/hid-libusb.c index b3ef426..d138ea4 100644 --- a/linux/hid-libusb.c +++ b/linux/hid-libusb.c @@ -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;