summaryrefslogtreecommitdiff
path: root/hid/src/manager.rs
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2017-03-26 17:07:56 -0700
committerDaniel Mueller <deso@posteo.net>2017-03-26 17:07:56 -0700
commitcd6e41862cea59ec306e7c9c578270575eb2a73b (patch)
tree1ee556e55face6290f6646f544282d3319fc6b35 /hid/src/manager.rs
parent654b2e5fa1566f0479b09adf2d73f783196ad032 (diff)
downloadnitrocli-cd6e41862cea59ec306e7c9c578270575eb2a73b.tar.gz
nitrocli-cd6e41862cea59ec306e7c9c578270575eb2a73b.tar.bz2
Import subrepo hid/:hid at 3ac1005fe3e874bef850ab733fe1a09bc36b91c5
Diffstat (limited to 'hid/src/manager.rs')
-rw-r--r--hid/src/manager.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/hid/src/manager.rs b/hid/src/manager.rs
new file mode 100644
index 0000000..4c2eb23
--- /dev/null
+++ b/hid/src/manager.rs
@@ -0,0 +1,56 @@
+use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
+
+use sys::*;
+use error::{self, Error};
+use devices::Devices;
+
+static INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
+
+/// The device manager.
+pub struct Manager;
+
+unsafe impl Send for Manager { }
+
+/// Create the manager.
+pub fn init() -> error::Result<Manager> {
+ if INITIALIZED.load(Ordering::Relaxed) {
+ return Err(Error::Initialized);
+ }
+
+ let status = unsafe { hid_init() };
+
+ if status != 0 {
+ return Err(Error::from(status));
+ }
+
+ INITIALIZED.store(true, Ordering::Relaxed);
+
+ Ok(Manager)
+}
+
+impl Drop for Manager {
+ fn drop(&mut self) {
+ let status = unsafe { hid_exit() };
+
+ if status != 0 {
+ panic!("hid_exit() failed");
+ }
+
+ INITIALIZED.store(false, Ordering::Relaxed);
+ }
+}
+
+impl Manager {
+ /// Find the wanted device, `vendor` or `product` are given it will
+ /// returns only the matches devices.
+ pub fn find(&self, vendor: Option<u16>, product: Option<u16>) -> Devices {
+ unsafe {
+ Devices::new(vendor, product)
+ }
+ }
+
+ /// Return all devices.
+ pub fn devices(&self) -> Devices {
+ self.find(None, None)
+ }
+}