aboutsummaryrefslogtreecommitdiff
path: root/hid/src/manager.rs
blob: 4c2eb236185270616ed9c4968f2deea3a0b532ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
	}
}