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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::fmt;
use std::ptr;
use std::ffi::OsStr;
use std::ops::Deref;
use std::os::unix::io::{RawFd, AsRawFd};
use ::context::Context;
use ::device::Device;
use ::handle::Handle;
pub struct Monitor {
monitor: *mut ::ffi::udev_monitor,
}
impl Drop for Monitor {
fn drop(&mut self) {
unsafe {
let udev = ::ffi::udev_monitor_get_udev(self.monitor);
::ffi::udev_monitor_unref(self.monitor);
::ffi::udev_unref(udev);
}
}
}
impl Monitor {
pub fn new(context: &Context) -> ::Result<Self> {
unsafe {
let ptr = try_alloc!(
::ffi::udev_monitor_new_from_netlink(context.as_ptr(), b"udev\0".as_ptr() as *mut _)
);
::ffi::udev_ref(context.as_ptr());
Ok(Monitor { monitor: ptr })
}
}
pub fn match_subsystem<T: AsRef<OsStr>>(&mut self, subsystem: T) -> ::Result<()> {
let subsystem = try!(::util::os_str_to_cstring(subsystem));
::util::errno_to_result(unsafe {
::ffi::udev_monitor_filter_add_match_subsystem_devtype(self.monitor, subsystem.as_ptr(), ptr::null())
})
}
pub fn match_subsystem_devtype<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, subsystem: T, devtype: U) -> ::Result<()> {
let subsystem = try!(::util::os_str_to_cstring(subsystem));
let devtype = try!(::util::os_str_to_cstring(devtype));
::util::errno_to_result(unsafe {
::ffi::udev_monitor_filter_add_match_subsystem_devtype(self.monitor, subsystem.as_ptr(), devtype.as_ptr())
})
}
pub fn match_tag<T: AsRef<OsStr>>(&mut self, tag: T) -> ::Result<()> {
let tag = try!(::util::os_str_to_cstring(tag));
::util::errno_to_result(unsafe {
::ffi::udev_monitor_filter_add_match_tag(self.monitor, tag.as_ptr())
})
}
pub fn clear_filters(&mut self) -> ::Result<()> {
::util::errno_to_result(unsafe {
::ffi::udev_monitor_filter_remove(self.monitor)
})
}
pub fn listen(self) -> ::Result<MonitorSocket> {
try!(::util::errno_to_result(unsafe {
::ffi::udev_monitor_enable_receiving(self.monitor)
}));
Ok(MonitorSocket { inner: self })
}
}
pub struct MonitorSocket {
inner: Monitor,
}
impl AsRawFd for MonitorSocket {
fn as_raw_fd(&self) -> RawFd {
unsafe {
::ffi::udev_monitor_get_fd(self.inner.monitor)
}
}
}
impl MonitorSocket {
pub fn receive_event(&mut self) -> Option<Event> {
let device = unsafe {
::ffi::udev_monitor_receive_device(self.inner.monitor)
};
if !device.is_null() {
Some(Event {
device: unsafe { ::device::from_raw(device) },
})
}
else {
None
}
}
}
#[derive(Debug,Clone,Copy,PartialEq,Eq)]
pub enum EventType {
Add,
Change,
Remove,
Unknown,
}
impl Default for EventType {
fn default() -> EventType {
EventType::Unknown
}
}
impl fmt::Display for EventType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
&EventType::Add => "add",
&EventType::Change => "change",
&EventType::Remove => "remove",
&EventType::Unknown => "unknown",
})
}
}
pub struct Event {
device: Device,
}
impl Deref for Event {
type Target = Device;
fn deref(&self) -> &Device {
&self.device
}
}
impl Event {
pub fn event_type(&self) -> EventType {
let value = match self.device.property_value("ACTION") {
Some(s) => s.to_str(),
None => None,
};
match value {
Some("add") => EventType::Add,
Some("change") => EventType::Change,
Some("remove") => EventType::Remove,
_ => EventType::Unknown
}
}
pub fn sequence_number(&self) -> u64 {
unsafe {
::ffi::udev_device_get_seqnum(self.device.as_ptr()) as u64
}
}
pub fn device(&self) -> &Device {
&self.device
}
}