Created
October 2, 2018 06:23
-
-
Save tetsu-koba/0f9e27cb9f95eb00430f986b07347a6e to your computer and use it in GitHub Desktop.
Dump input event in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dump input event in rust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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