diff options
Diffstat (limited to 'third_party/rust/libudev/examples')
-rw-r--r-- | third_party/rust/libudev/examples/list_devices.rs | 45 | ||||
-rw-r--r-- | third_party/rust/libudev/examples/monitor.rs | 70 |
2 files changed, 115 insertions, 0 deletions
diff --git a/third_party/rust/libudev/examples/list_devices.rs b/third_party/rust/libudev/examples/list_devices.rs new file mode 100644 index 0000000000..5ef71765de --- /dev/null +++ b/third_party/rust/libudev/examples/list_devices.rs @@ -0,0 +1,45 @@ +extern crate libudev; + +use std::io; + +fn main() { + let context = libudev::Context::new().unwrap(); + list_devices(&context).unwrap(); +} + +fn list_devices(context: &libudev::Context) -> io::Result<()> { + let mut enumerator = try!(libudev::Enumerator::new(&context)); + + for device in try!(enumerator.scan_devices()) { + println!(""); + println!("initialized: {:?}", device.is_initialized()); + println!(" devnum: {:?}", device.devnum()); + println!(" syspath: {:?}", device.syspath()); + println!(" devpath: {:?}", device.devpath()); + println!(" subsystem: {:?}", device.subsystem()); + println!(" sysname: {:?}", device.sysname()); + println!(" sysnum: {:?}", device.sysnum()); + println!(" devtype: {:?}", device.devtype()); + println!(" driver: {:?}", device.driver()); + println!(" devnode: {:?}", device.devnode()); + + if let Some(parent) = device.parent() { + println!(" parent: {:?}", parent.syspath()); + } + else { + println!(" parent: None"); + } + + println!(" [properties]"); + for property in device.properties() { + println!(" - {:?} {:?}", property.name(), property.value()); + } + + println!(" [attributes]"); + for attribute in device.attributes() { + println!(" - {:?} {:?}", attribute.name(), attribute.value()); + } + } + + Ok(()) +} diff --git a/third_party/rust/libudev/examples/monitor.rs b/third_party/rust/libudev/examples/monitor.rs new file mode 100644 index 0000000000..ad9dedcbd0 --- /dev/null +++ b/third_party/rust/libudev/examples/monitor.rs @@ -0,0 +1,70 @@ +extern crate libudev; +extern crate libc; + +use std::io; +use std::ptr; +use std::thread; +use std::time::Duration; + +use std::os::unix::io::{AsRawFd}; + +use libc::{c_void,c_int,c_short,c_ulong,timespec}; + +#[repr(C)] +struct pollfd { + fd: c_int, + events: c_short, + revents: c_short, +} + +#[repr(C)] +struct sigset_t { + __private: c_void +} + +#[allow(non_camel_case_types)] +type nfds_t = c_ulong; + +const POLLIN: c_short = 0x0001; + +extern "C" { + fn ppoll(fds: *mut pollfd, nfds: nfds_t, timeout_ts: *mut libc::timespec, sigmask: *const sigset_t) -> c_int; +} + +fn main() { + let context = libudev::Context::new().unwrap(); + monitor(&context).unwrap(); +} + +fn monitor(context: &libudev::Context) -> io::Result<()> { + let mut monitor = try!(libudev::Monitor::new(&context)); + + try!(monitor.match_subsystem_devtype("usb", "usb_device")); + let mut socket = try!(monitor.listen()); + + let mut fds = vec!(pollfd { fd: socket.as_raw_fd(), events: POLLIN, revents: 0 }); + + loop { + let result = unsafe { ppoll((&mut fds[..]).as_mut_ptr(), fds.len() as nfds_t, ptr::null_mut(), ptr::null()) }; + + if result < 0 { + return Err(io::Error::last_os_error()); + } + + let event = match socket.receive_event() { + Some(evt) => evt, + None => { + thread::sleep(Duration::from_millis(10)); + continue; + } + }; + + println!("{}: {} {} (subsystem={}, sysname={}, devtype={})", + event.sequence_number(), + event.event_type(), + event.syspath().to_str().unwrap_or("---"), + event.subsystem().to_str().unwrap_or(""), + event.sysname().to_str().unwrap_or(""), + event.devtype().map_or("", |s| { s.to_str().unwrap_or("") })); + } +} |