-
Notifications
You must be signed in to change notification settings - Fork 2
/
libinput.rs
32 lines (26 loc) · 929 Bytes
/
libinput.rs
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
//! Components for interacting with `libinput`.
use std::fs::{File, OpenOptions};
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::path::Path;
use input::LibinputInterface;
use libc::{O_RDONLY, O_RDWR, O_WRONLY};
/// Struct for libinput interface.
pub struct Interface;
impl LibinputInterface for Interface {
#[allow(clippy::bad_bit_mask)]
fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<RawFd, i32> {
OpenOptions::new()
.custom_flags(flags)
.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
.open(path)
.map(|file| file.into_raw_fd())
.map_err(|err| err.raw_os_error().unwrap())
}
fn close_restricted(&mut self, fd: RawFd) {
unsafe {
File::from_raw_fd(fd);
}
}
}