linux_raw_input_rs/
lib.rs

1#[macro_use]
2extern crate serde_derive;
3extern crate serde;
4
5pub mod input;
6pub mod keys;
7    
8use std::process::Command;
9use std::str::from_utf8;
10use std::fs::{File, OpenOptions};
11use std::io::Read;
12
13use input::Input;
14
15#[derive(Debug)]
16pub struct InputReader {
17    file: File
18}
19
20impl InputReader {
21    pub fn new(path: String) -> InputReader {
22        InputReader{file: OpenOptions::new().read(true).write(false).append(false).open(path).expect("could not open device file")}
23    }
24    pub fn current_state(&mut self) -> Input{
25        let mut buf: [u8; 24] = [0 as u8; 24];
26        self.file.read(&mut buf).expect("error reading file");
27        Input::from_read(&buf)
28    }
29}
30
31pub fn get_input_devices() -> Vec<String>{
32    let command = "grep -E 'Handlers|EV' /proc/bus/input/devices | grep -B1 120013 | grep -Eo event[0-9]+".to_string();
33    let result = Command::new("sh").arg("-c").arg(command).output().expect("could not execute command to search for input devices");
34    let result_str = from_utf8(&result.stdout).expect("could not get results");
35        
36    result_str.trim().split('\n').map(|s| "/dev/input/".to_string() + s).collect()
37}