ruabmbua / hidapi-rs

Rust bindings for the hidapi C library
MIT License
167 stars 80 forks source link

Duplicate device for Xbox and Switch Pro controllers using device_list() #97

Closed ludokx closed 1 year ago

ludokx commented 1 year ago

Very cool crate, thanks for writing this!

As I was pulling information from plugged in controller (BT or USB) on Linux, for some devices the list returns duplicate entries. Here's the code:

let hidapi = HidApi::new()?;
let xbox_controllers = hidapi.device_list();

For the Xbox controller over BT it reports 5 duplicates and I have to filter out the entries:

xbox_controllers.dedup_by(|a, b| a.serial_number() == b.serial_number());

Check out the output from udevadm monitor:

(deck@steamdeck ~)$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[71184.320496] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:1 (bluetooth)
UDEV  [71184.324237] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:1 (bluetooth)
KERNEL[71206.514944] remove   /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:1 (bluetooth)
UDEV  [71206.518281] remove   /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:1 (bluetooth)
KERNEL[71211.910437] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2 (bluetooth)
UDEV  [71211.913694] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2 (bluetooth)
KERNEL[71216.538697] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039 (hid)
KERNEL[71216.539040] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery/wakeup48 (wakeup)
KERNEL[71216.539080] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery/hwmon6 (hwmon)
KERNEL[71216.539142] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/input/input237 (input)
KERNEL[71216.539274] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/input/input237/event18 (input)
KERNEL[71216.539477] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/input/input237/js3 (input)
KERNEL[71216.539574] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/hidraw/hidraw4 (hidraw)
KERNEL[71216.539621] bind     /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039 (hid)
UDEV  [71216.543977] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039 (hid)
UDEV  [71216.547960] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/input/input237 (input)
UDEV  [71216.552107] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/input/input237/js3 (input)
UDEV  [71216.556342] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/hidraw/hidraw4 (hidraw)
UDEV  [71216.597122] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/input/input237/event18 (input)
KERNEL[71216.818149] add      /devices/virtual/input/input238 (input)
KERNEL[71216.818204] add      /devices/virtual/input/input238/event19 (input)
KERNEL[71216.818231] add      /devices/virtual/input/input238/js4 (input)
UDEV  [71216.820131] add      /devices/virtual/input/input238 (input)
UDEV  [71216.822003] add      /devices/virtual/input/input238/js4 (input)
UDEV  [71216.892657] add      /devices/virtual/input/input238/event19 (input)
UDEV  [71221.686737] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery/wakeup48 (wakeup)
UDEV  [71226.806650] add      /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery/hwmon6 (hwmon)
UDEV  [71226.809124] bind     /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039 (hid)
KERNEL[71231.928211] change   /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery (power_supply)
KERNEL[71231.928296] change   /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery (power_supply)
UDEV  [71231.931738] change   /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery (power_supply)
UDEV  [71231.933992] change   /devices/pci0000:00/0000:00:08.1/0000:04:00.4/usb3/3-5/3-5:1.0/bluetooth/hci0/hci0:2/0005:045E:02FD.0039/power_supply/hid-c8:3f:26:5a:94:44-battery (power_supply)

For the Switch Pro controller, it reports 2 duplicates over USB, see this screenshot:

image
ruabmbua commented 1 year ago

Please see https://github.com/ruabmbua/hidapi-rs/issues/89 for the reason why devices can show up multiple times in the list.