signal11 / hidapi

A Simple library for communicating with USB and Bluetooth HID devices on Linux, Mac, and Windows.
http://www.signal11.us/oss/hidapi/
Other
2.46k stars 901 forks source link

Windows: enumerate a specific list of vendor/product ids #448

Closed pedroos closed 4 years ago

pedroos commented 4 years ago

This additional function (enumerate_m()) works the same as enumerate(), but enables a set of specific vendor ids and product ids to be specified. This avoids manual filtering of results when more than one vendor id/product id combination needs to be enumerated.

Youw commented 4 years ago

Please see: #373.


As a maintainer of libusb/hidapi I vote against this PR for a number of reasons:


Why don't just use simple hid_enumerate ?

It will give you same result, for all supported platforms:

bool device_matches(struct hid_device_info *dev) {
    unsigned short vendor_ids[] = ...;
    unsigned short product_ids[] = ...;
    int products_size = 2;

    for (int i = 0; i < products_size; i++) {
        if (dev->vendor_id == vendor_ids[i] && dev->product_id == product_ids[i])
            return true;
    }
    return false;
}

// use your device here, or save it somewhere for later
bool use_dev(struct hid_device_info *dev);

struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs; 
while (cur_dev) {
    if (device_matches(cur_dev)) {
        use_dev(cur_dev);
    }
    cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
pedroos commented 4 years ago

Yeah, I hadn't seen the 0x0 option. Thanks for pointing that out.