| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use crate::transport::device_selector::DeviceSelectorEvent; |
| 6 |
use devd_rs; |
| 7 |
use runloop::RunLoop; |
| 8 |
use std::collections::HashMap; |
| 9 |
use std::error::Error; |
| 10 |
use std::ffi::OsString; |
| 11 |
use std::sync::{mpsc::Sender, Arc}; |
| 12 |
use std::{fs, io}; |
| 13 |
|
| 14 |
const POLL_TIMEOUT: usize = 100; |
| 15 |
|
| 16 |
pub enum Event { |
| 17 |
Add(OsString), |
| 18 |
Remove(OsString), |
| 19 |
} |
| 20 |
|
| 21 |
impl Event { |
| 22 |
fn from_devd(event: devd_rs::Event) -> Option<Self> { |
| 23 |
match event { |
| 24 |
devd_rs::Event::Attach { |
| 25 |
ref dev, |
| 26 |
parent: _, |
| 27 |
location: _, |
| 28 |
} if dev.starts_with("uhid") => Some(Event::Add(("/dev/".to_owned() + dev).into())), |
| 29 |
devd_rs::Event::Detach { |
| 30 |
ref dev, |
| 31 |
parent: _, |
| 32 |
location: _, |
| 33 |
} if dev.starts_with("uhid") => Some(Event::Remove(("/dev/".to_owned() + dev).into())), |
| 34 |
_ => None, |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
fn convert_error(e: devd_rs::Error) -> io::Error { |
| 40 |
e.into() |
| 41 |
} |
| 42 |
|
| 43 |
pub struct Monitor<F> |
| 44 |
where |
| 45 |
F: Fn(OsString, Sender<DeviceSelectorEvent>, Sender<crate::StatusUpdate>, &dyn Fn() -> bool) |
| 46 |
+ Sync, |
| 47 |
{ |
| 48 |
runloops: HashMap<OsString, RunLoop>, |
| 49 |
new_device_cb: Arc<F>, |
| 50 |
selector_sender: Sender<DeviceSelectorEvent>, |
| 51 |
status_sender: Sender<crate::StatusUpdate>, |
| 52 |
} |
| 53 |
|
| 54 |
impl<F> Monitor<F> |
| 55 |
where |
| 56 |
F: Fn(OsString, Sender<DeviceSelectorEvent>, Sender<crate::StatusUpdate>, &dyn Fn() -> bool) |
| 57 |
+ Send |
| 58 |
+ Sync |
| 59 |
+ 'static, |
| 60 |
{ |
| 61 |
pub fn new( |
| 62 |
new_device_cb: F, |
| 63 |
selector_sender: Sender<DeviceSelectorEvent>, |
| 64 |
status_sender: Sender<crate::StatusUpdate>, |
| 65 |
) -> Self { |
| 66 |
Self { |
| 67 |
runloops: HashMap::new(), |
| 68 |
new_device_cb: Arc::new(new_device_cb), |
| 69 |
selector_sender, |
| 70 |
status_sender, |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
pub fn run(&mut self, alive: &dyn Fn() -> bool) -> Result<(), Box<dyn Error>> { |
| 75 |
let mut ctx = devd_rs::Context::new().map_err(convert_error)?; |
| 76 |
|
| 77 |
let mut initial_devs = Vec::new(); |
| 78 |
// Iterate all existing devices. |
| 79 |
for dev in (fs::read_dir("/dev")?).flatten() { |
| 80 |
let filename_ = dev.file_name(); |
| 81 |
let filename = filename_.to_str().unwrap_or(""); |
| 82 |
if filename.starts_with("uhid") { |
| 83 |
let path = OsString::from("/dev/".to_owned() + filename); |
| 84 |
initial_devs.push(path.clone()); |
| 85 |
self.add_device(path); |
| 86 |
} |
| 87 |
} |
| 88 |
let _ = self |
| 89 |
.selector_sender |
| 90 |
.send(DeviceSelectorEvent::DevicesAdded(initial_devs)); |
| 91 |
|
| 92 |
// Loop until we're stopped by the controlling thread, or fail. |
| 93 |
while alive() { |
| 94 |
// Wait for new events, break on failure. |
| 95 |
match ctx.wait_for_event(POLL_TIMEOUT) { |
| 96 |
Err(devd_rs::Error::Timeout) => (), |
| 97 |
Err(e) => return Err(convert_error(e).into()), |
| 98 |
Ok(event) => { |
| 99 |
if let Some(event) = Event::from_devd(event) { |
| 100 |
self.process_event(event); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Remove all tracked devices. |
| 107 |
self.remove_all_devices(); |
| 108 |
|
| 109 |
Ok(()) |
| 110 |
} |
| 111 |
|
| 112 |
fn process_event(&mut self, event: Event) { |
| 113 |
match event { |
| 114 |
Event::Add(path) => { |
| 115 |
let _ = self |
| 116 |
.selector_sender |
| 117 |
.send(DeviceSelectorEvent::DevicesAdded(vec![path.clone()])); |
| 118 |
self.add_device(path); |
| 119 |
} |
| 120 |
Event::Remove(path) => { |
| 121 |
self.remove_device(path); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
fn add_device(&mut self, path: OsString) { |
| 127 |
let f = self.new_device_cb.clone(); |
| 128 |
let selector_sender = self.selector_sender.clone(); |
| 129 |
let status_sender = self.status_sender.clone(); |
| 130 |
let key = path.clone(); |
| 131 |
debug!("Adding device {}", key.to_string_lossy()); |
| 132 |
|
| 133 |
let runloop = RunLoop::new(move |alive| { |
| 134 |
if alive() { |
| 135 |
f(path, selector_sender, status_sender, alive); |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
if let Ok(runloop) = runloop { |
| 140 |
self.runloops.insert(key, runloop); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
fn remove_device(&mut self, path: OsString) { |
| 145 |
let _ = self |
| 146 |
.selector_sender |
| 147 |
.send(DeviceSelectorEvent::DeviceRemoved(path.clone())); |
| 148 |
|
| 149 |
debug!("Removing device {}", path.to_string_lossy()); |
| 150 |
if let Some(runloop) = self.runloops.remove(&path) { |
| 151 |
runloop.cancel(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
fn remove_all_devices(&mut self) { |
| 156 |
while !self.runloops.is_empty() { |
| 157 |
let path = self.runloops.keys().next().unwrap().clone(); |
| 158 |
self.remove_device(path); |
| 159 |
} |
| 160 |
} |
| 161 |
} |