Use private libusb_context for linux/libusb implementation.

Use a private libusb_context to avoid conflicts between
multiple libusb instances inside a single application
process. Many 3rd party libraries use libusb as backend,
so there is a good chance of collisions if the default NULL
context is used.

Modified by Alan Ott to also remove the initialized
variable since it's no longer needed (since we can use
the context instead), to make the style consistent, and
to remove unnecessary checks for initialized before calling
hid_init().

Signed-off-by: Mario Kleiner <mario.kleiner@tuebingen.mpg.de>
Signed-off-by: Alan Ott <alan@signal11.us>
This commit is contained in:
Mario Kleiner 2011-11-28 04:45:23 +01:00 committed by Alan Ott
parent 011208de6a
commit e8c3e8db61

View File

@ -106,7 +106,7 @@ struct hid_device_ {
struct input_report *input_reports;
};
static int initialized = 0;
static libusb_context *usb_context = NULL;
uint16_t get_usb_code_for_current_locale(void);
static int return_data(hid_device *dev, unsigned char *data, size_t length);
@ -384,10 +384,9 @@ static char *make_path(libusb_device *dev, int interface_number)
int HID_API_EXPORT hid_init(void)
{
if (!initialized) {
if (libusb_init(NULL))
if (!usb_context) {
if (libusb_init(&usb_context))
return -1;
initialized = 1;
}
return 0;
@ -395,9 +394,9 @@ int HID_API_EXPORT hid_init(void)
int HID_API_EXPORT hid_exit(void)
{
if (initialized) {
libusb_exit(NULL);
initialized = 0;
if (usb_context) {
libusb_exit(usb_context);
usb_context = NULL;
}
return 0;
@ -416,10 +415,9 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
setlocale(LC_ALL,"");
if (!initialized)
hid_init();
hid_init();
num_devs = libusb_get_device_list(NULL, &devs);
num_devs = libusb_get_device_list(usb_context, &devs);
if (num_devs < 0)
return NULL;
while ((dev = devs[i++]) != NULL) {
@ -702,7 +700,7 @@ static void *read_thread(void *param)
/* Handle all the events. */
while (!dev->shutdown_thread) {
int res;
res = libusb_handle_events(NULL);
res = libusb_handle_events(usb_context);
if (res < 0) {
/* There was an error. Break out of this loop. */
break;
@ -713,7 +711,7 @@ static void *read_thread(void *param)
if no transfers are pending, but that's OK. */
if (libusb_cancel_transfer(dev->transfer) == 0) {
/* The transfer was cancelled, so wait for its completion. */
libusb_handle_events(NULL);
libusb_handle_events(usb_context);
}
/* Now that the read thread is stopping, Wake any threads which are
@ -752,10 +750,9 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
setlocale(LC_ALL,"");
if (!initialized)
hid_init();
hid_init();
num_devs = libusb_get_device_list(NULL, &devs);
num_devs = libusb_get_device_list(usb_context, &devs);
while ((usb_dev = devs[d++]) != NULL) {
struct libusb_device_descriptor desc;
struct libusb_config_descriptor *conf_desc = NULL;