Skip to content

Instantly share code, notes, and snippets.

@tetsu-koba
Created October 2, 2018 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tetsu-koba/0f9e27cb9f95eb00430f986b07347a6e to your computer and use it in GitHub Desktop.
Save tetsu-koba/0f9e27cb9f95eb00430f986b07347a6e to your computer and use it in GitHub Desktop.
Dump input event in rust
Dump input event in rust
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!("input.rs");
use std::io;
use std::io::Read;
use std::fs::File;
fn read_input_event(f: &mut File) -> io::Result<(input_event)> {
const sz: usize = std::mem::size_of::<input_event>();
let mut buf = [0; sz];
f.read_exact(&mut buf)?;
let ie: input_event = unsafe { std::mem::transmute(buf) };
Ok(ie)
}
fn dump_input_event(fname: &str) {
let mut f = File::open(fname).expect("open failed.");
loop {
match read_input_event(&mut f).expect("read error.") {
input_event { type_, code, value, .. } if type_ as u32 == EV_KEY => {
match code as u32 {
KEY_MUTE => {
if value == 1 {
println!("mute")
}
}
KEY_VOLUMEDOWN => {
if value != 0 {
println!("volume down")
}
}
KEY_VOLUMEUP => {
if value != 0 {
println!("volume up")
}
}
_ => {}
}
}
//ie => println!("input_event ={:?}", ie),
_ => {}
}
}
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} /dev/input/event0", &args[0]);
return;
}
dump_input_event(&args[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment