Debian Codegraph @

examples/evtest_nonblocking.rs

raw symbols SCIP
1 //! This example demonstrates how to use the evdev crate with a nonblocking file descriptor.
2 //!
3 //! Note that for this implementation the caller is responsible for ensuring the underlying
4 //! Device file descriptor is set to O_NONBLOCK. The caller must also create the epoll descriptor,
5 //! bind it, check for EAGAIN returns from fetch_events_*, call epoll_wait as appropriate, and
6 //! clean up the epoll file descriptor when finished.
7
8 #[cfg(not(target_os = "linux"))]
9 fn main() {}
10
11 // cli/"tui" shared between the evtest examples
12 #[cfg(target_os = "linux")]
13 mod _pick_device;
14
15 #[cfg(target_os = "linux")]
16 fn main() -> std::io::Result<()> {
17 use nix::sys::epoll;
18
19 let mut dev = _pick_device::pick_device();
20 println!("{dev}");
21
22 dev.set_nonblocking(true)?;
23
24 // Create epoll handle and attach raw_fd
25 let epoll = epoll::Epoll::new(epoll::EpollCreateFlags::EPOLL_CLOEXEC)?;
26 let event = epoll::EpollEvent::new(epoll::EpollFlags::EPOLLIN, 0);
27 epoll.add(&dev, event)?;
28
29 // We don't care about these, but the kernel wants to fill them.
30 let mut events = [epoll::EpollEvent::empty(); 2];
31
32 println!("Events:");
33 loop {
34 match dev.fetch_events() {
35 Ok(iterator) => {
36 for ev in iterator {
37 println!("{ev:?}");
38 }
39 }
40 Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
41 // Wait forever for bytes available on dev
42 epoll.wait(&mut events, epoll::EpollTimeout::NONE)?;
43 }
44 Err(e) => {
45 eprintln!("{e}");
46 break;
47 }
48 }
49 }
50 Ok(())
51 }