After doing some more digging I found that the reason ioctl(m_file, EVIOCGID, &inpid)
fails is that the device I'm opening is a joystick (/dev/input/js
) and the EVIOCGID
ioctl is for event devices (/dev/input/event
) hence the failure. Unfortunately there is not JSIOCGID
ioctl so I had to change tactics. Instead I'm using udev to access the joystick's vendor and product IDs. Here's the code I'm using:
// Use udev to look up the product and manufacturer IDs
struct udev *udev = udev_new();
if (udev)
{
char sysname[32];
std::snprintf(sysname, sizeof(sysname), "js%u", index);
struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);
dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
if (!dev)
{
err() << "Unable to find parent USB device" << std::endl;
}
std::stringstream ss;
ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
ss >> m_manufacturerID;
ss.clear();
ss.str("");
ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
ss >> m_productID;
udev_device_unref(dev);
udev_unref(udev);
}
else
{
err() << "Cannot create udev" << std::endl;
}
With udev I've been able to consistently retrieve vendor and product IDs for USB joysticks on Ubuntu 13.10 x64.
UPDATE
I've tested this further with:
- Linux Mint 16 x64
- Manjaro x64
- Fedora 20 x64
In all instances this code worked great.