diff options
author | Daniel Mueller <deso@posteo.net> | 2017-03-26 17:07:56 -0700 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2017-03-26 17:07:56 -0700 |
commit | cd6e41862cea59ec306e7c9c578270575eb2a73b (patch) | |
tree | 1ee556e55face6290f6646f544282d3319fc6b35 /hid/src/devices.rs | |
parent | 654b2e5fa1566f0479b09adf2d73f783196ad032 (diff) | |
download | nitrocli-cd6e41862cea59ec306e7c9c578270575eb2a73b.tar.gz nitrocli-cd6e41862cea59ec306e7c9c578270575eb2a73b.tar.bz2 |
Import subrepo hid/:hid at 3ac1005fe3e874bef850ab733fe1a09bc36b91c5
Diffstat (limited to 'hid/src/devices.rs')
-rw-r--r-- | hid/src/devices.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/hid/src/devices.rs b/hid/src/devices.rs new file mode 100644 index 0000000..002ffa9 --- /dev/null +++ b/hid/src/devices.rs @@ -0,0 +1,51 @@ +use std::marker::PhantomData; + +use sys::*; +use Device; + +/// An iterator over the available devices. +pub struct Devices<'a> { + ptr: *mut hid_device_info, + cur: *mut hid_device_info, + + _marker: PhantomData<&'a ()>, +} + +impl<'a> Devices<'a> { + #[doc(hidden)] + pub unsafe fn new(vendor: Option<u16>, product: Option<u16>) -> Self { + let list = hid_enumerate(vendor.unwrap_or(0), product.unwrap_or(0)); + + Devices { + ptr: list, + cur: list, + + _marker: PhantomData, + } + } +} + +impl<'a> Iterator for Devices<'a> { + type Item = Device<'a>; + + fn next(&mut self) -> Option<Self::Item> { + if self.cur.is_null() { + return None; + } + + unsafe { + let info = Device::new(self.cur); + self.cur = (*self.cur).next; + + Some(info) + } + } +} + +impl<'a> Drop for Devices<'a> { + fn drop(&mut self) { + unsafe { + hid_free_enumeration(self.ptr); + } + } +} |