| 1 |
use std::fmt; |
| 2 |
use std::ptr; |
| 3 |
|
| 4 |
use std::ffi::{CString,OsStr}; |
| 5 |
use std::ops::Deref; |
| 6 |
use std::os::unix::io::{RawFd,AsRawFd}; |
| 7 |
|
| 8 |
use ::context::{Context}; |
| 9 |
use ::device::{Device}; |
| 10 |
use ::handle::prelude::*; |
| 11 |
|
| 12 |
|
| 13 |
/// Monitors for device events. |
| 14 |
/// |
| 15 |
/// A monitor communicates with the kernel over a socket. Filtering events is performed efficiently |
| 16 |
/// in the kernel, and only events that match the filters are received by the socket. Filters must |
| 17 |
/// be setup before listening for events. |
| 18 |
pub struct Monitor<'a> { |
| 19 |
context: &'a Context, |
| 20 |
monitor: *mut ::ffi::udev_monitor |
| 21 |
} |
| 22 |
|
| 23 |
impl<'a> Drop for Monitor<'a> { |
| 24 |
fn drop(&mut self) { |
| 25 |
unsafe { |
| 26 |
::ffi::udev_monitor_unref(self.monitor); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl<'a> Monitor<'a> { |
| 32 |
/// Creates a new `Monitor`. |
| 33 |
pub fn new(context: &'a Context) -> ::Result<Self> { |
| 34 |
let name = CString::new("udev").unwrap(); |
| 35 |
|
| 36 |
let ptr = try_alloc!(unsafe { |
| 37 |
::ffi::udev_monitor_new_from_netlink(context.as_ptr(), name.as_ptr()) |
| 38 |
}); |
| 39 |
|
| 40 |
Ok(Monitor { |
| 41 |
context: context, |
| 42 |
monitor: ptr |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
/// Adds a filter that matches events for devices with the given subsystem. |
| 47 |
pub fn match_subsystem<T: AsRef<OsStr>>(&mut self, subsystem: T) -> ::Result<()> { |
| 48 |
let subsystem = try!(::util::os_str_to_cstring(subsystem)); |
| 49 |
|
| 50 |
::util::errno_to_result(unsafe { |
| 51 |
::ffi::udev_monitor_filter_add_match_subsystem_devtype(self.monitor, subsystem.as_ptr(), ptr::null()) |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
/// Adds a filter that matches events for devices with the given subsystem and device type. |
| 56 |
pub fn match_subsystem_devtype<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, subsystem: T, devtype: U) -> ::Result<()> { |
| 57 |
let subsystem = try!(::util::os_str_to_cstring(subsystem)); |
| 58 |
let devtype = try!(::util::os_str_to_cstring(devtype)); |
| 59 |
|
| 60 |
::util::errno_to_result(unsafe { |
| 61 |
::ffi::udev_monitor_filter_add_match_subsystem_devtype(self.monitor, subsystem.as_ptr(), devtype.as_ptr()) |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
/// Adds a filter that matches events for devices with the given tag. |
| 66 |
pub fn match_tag<T: AsRef<OsStr>>(&mut self, tag: T) -> ::Result<()> { |
| 67 |
let tag = try!(::util::os_str_to_cstring(tag)); |
| 68 |
|
| 69 |
::util::errno_to_result(unsafe { |
| 70 |
::ffi::udev_monitor_filter_add_match_tag(self.monitor, tag.as_ptr()) |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
/// Removes all filters currently set on the monitor. |
| 75 |
pub fn clear_filters(&mut self) -> ::Result<()> { |
| 76 |
::util::errno_to_result(unsafe { |
| 77 |
::ffi::udev_monitor_filter_remove(self.monitor) |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
/// Listens for events matching the current filters. |
| 82 |
/// |
| 83 |
/// This method consumes the `Monitor`. |
| 84 |
pub fn listen(self) -> ::Result<MonitorSocket<'a>> { |
| 85 |
try!(::util::errno_to_result(unsafe { |
| 86 |
::ffi::udev_monitor_enable_receiving(self.monitor) |
| 87 |
})); |
| 88 |
|
| 89 |
Ok(MonitorSocket { inner: self }) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/// An active monitor that can receive events. |
| 95 |
/// |
| 96 |
/// The events received by a `MonitorSocket` match the filters setup by the `Monitor` that created |
| 97 |
/// the socket. |
| 98 |
/// |
| 99 |
/// Monitors are initially setup to receive events from the kernel via a nonblocking socket. A |
| 100 |
/// variant of `poll()` should be used on the file descriptor returned by the `AsRawFd` trait to |
| 101 |
/// wait for new events. |
| 102 |
pub struct MonitorSocket<'a> { |
| 103 |
inner: Monitor<'a> |
| 104 |
} |
| 105 |
|
| 106 |
/// Provides raw access to the monitor's socket. |
| 107 |
impl<'a> AsRawFd for MonitorSocket<'a> { |
| 108 |
/// Returns the file descriptor of the monitor's socket. |
| 109 |
fn as_raw_fd(&self) -> RawFd { |
| 110 |
unsafe { |
| 111 |
::ffi::udev_monitor_get_fd(self.inner.monitor) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl<'a> MonitorSocket<'a> { |
| 117 |
/// Receives the next available event from the monitor. |
| 118 |
/// |
| 119 |
/// This method does not block. If no events are available, it returns `None` immediately. |
| 120 |
pub fn receive_event<'b>(&'b mut self) -> Option<Event<'a>> { |
| 121 |
let device = unsafe { |
| 122 |
::ffi::udev_monitor_receive_device(self.inner.monitor) |
| 123 |
}; |
| 124 |
|
| 125 |
if device.is_null() { |
| 126 |
None |
| 127 |
} |
| 128 |
else { |
| 129 |
let device = ::device::new(self.inner.context, device); |
| 130 |
|
| 131 |
Some(Event { device: device }) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/// Types of events that can be received from udev. |
| 137 |
#[derive(Debug,Clone,Copy,PartialEq,Eq)] |
| 138 |
pub enum EventType { |
| 139 |
/// A device was added. |
| 140 |
Add, |
| 141 |
|
| 142 |
/// A device changed. |
| 143 |
Change, |
| 144 |
|
| 145 |
/// A device was removed. |
| 146 |
Remove, |
| 147 |
|
| 148 |
/// An unknown event occurred. |
| 149 |
Unknown, |
| 150 |
} |
| 151 |
|
| 152 |
impl Default for EventType { |
| 153 |
fn default() -> EventType { |
| 154 |
EventType::Unknown |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
impl fmt::Display for EventType { |
| 159 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 160 |
f.write_str(match self { |
| 161 |
&EventType::Add => "add", |
| 162 |
&EventType::Change => "change", |
| 163 |
&EventType::Remove => "remove", |
| 164 |
&EventType::Unknown => "unknown", |
| 165 |
}) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/// An event that indicates a change in device state. |
| 171 |
pub struct Event<'a> { |
| 172 |
device: Device<'a> |
| 173 |
} |
| 174 |
|
| 175 |
/// Provides access to the device associated with the event. |
| 176 |
impl<'a> Deref for Event<'a> { |
| 177 |
type Target = Device<'a>; |
| 178 |
|
| 179 |
fn deref(&self) -> &Device<'a> { |
| 180 |
&self.device |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
impl<'a> Event<'a> { |
| 185 |
/// Returns the `EventType` corresponding to this event. |
| 186 |
pub fn event_type(&self) -> EventType { |
| 187 |
let value = match self.device.property_value("ACTION") { |
| 188 |
Some(s) => s.to_str(), |
| 189 |
None => None |
| 190 |
}; |
| 191 |
|
| 192 |
match value { |
| 193 |
Some("add") => EventType::Add, |
| 194 |
Some("change") => EventType::Change, |
| 195 |
Some("remove") => EventType::Remove, |
| 196 |
_ => EventType::Unknown |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/// Returns the event's sequence number. |
| 201 |
pub fn sequence_number(&self) -> u64 { |
| 202 |
unsafe { |
| 203 |
::ffi::udev_device_get_seqnum(self.device.as_ptr()) as u64 |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/// Returns the device associated with this event. |
| 208 |
pub fn device(&self) -> &Device { |
| 209 |
&self.device |
| 210 |
} |
| 211 |
} |