rot8/src/main.rs

157 lines
5.1 KiB
Rust
Raw Normal View History

2019-05-18 20:27:15 +00:00
extern crate glob;
2019-05-19 03:41:30 +00:00
extern crate clap;
use clap::{Arg, App};
2019-05-18 20:27:15 +00:00
use std::fs;
use std::thread;
use std::time::Duration;
use std::process::Command;
use glob::glob;
2019-05-19 03:41:30 +00:00
2019-05-18 20:27:15 +00:00
fn main() {
2019-05-19 03:41:30 +00:00
let mut mode = "";
2019-05-18 20:27:15 +00:00
let mut old_state = "normal";
let mut new_state: &str;
let mut path_x: String = "".to_string();
let mut path_y: String = "".to_string();
2019-05-19 03:41:30 +00:00
let mut matrix: [&str;9];
let mut x_state: &str;
let sway_pid = String::from_utf8(Command::new("pidof")
.arg("sway")
.output()
.unwrap()
.stdout).unwrap();
let x_pid = String::from_utf8(Command::new("pidof")
.arg("x")
.output()
.unwrap()
.stdout).unwrap();
if sway_pid.len() >= 1 {
mode = "sway";
}
if x_pid.len() >= 1 {
mode = "x";
}
let matches = App::new("rot8")
.version("0.1.1")
.arg(Arg::with_name("display")
.default_value("eDP-1")
.long("display")
.value_name("DISPLAY")
.help("Set Display Device")
.takes_value(true))
.arg(Arg::with_name("touchscreen")
.default_value("eDP-1")
.long("touchscreen")
.value_name("TOUCHSCREEN")
.help("Set Touchscreen Device")
.takes_value(true))
.get_matches();
let display = matches.value_of("display").unwrap_or("default.conf");
let touchscreen = matches.value_of("touchscreen").unwrap_or("default.conf");
2019-05-18 20:27:15 +00:00
for entry in glob("/sys/bus/iio/devices/iio:device*/in_accel_*_raw").unwrap(){
match entry {
Ok(path) => {
if path.to_str().unwrap().contains("x_raw"){
path_x = path.to_str().unwrap().to_owned();
} else if path.to_str().unwrap().contains("y_raw"){
path_y = path.to_str().unwrap().to_owned();
} else if path.to_str().unwrap().contains("z_raw"){
continue;
} else {
println!("{:?}", path);
panic!();
}
},
Err(e) => println!("{:?}",e)
}
}
loop {
let x_raw = fs::read_to_string(path_x.as_str()).unwrap();
let y_raw = fs::read_to_string(path_y.as_str()).unwrap();
let x = x_raw.trim_end_matches('\n').parse::<i32>().unwrap_or(0);
let y = y_raw.trim_end_matches('\n').parse::<i32>().unwrap_or(0);
2019-05-19 03:41:30 +00:00
2019-05-18 20:27:15 +00:00
if x < -500000 {
if y > 500000 {
new_state = "180";
2019-05-19 03:41:30 +00:00
x_state = "normal";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
2019-05-18 20:27:15 +00:00
}
else {
2019-05-19 03:41:30 +00:00
2019-05-18 20:27:15 +00:00
new_state = "90";
2019-05-19 03:41:30 +00:00
x_state = "left";
matrix = ["0", "-1", "1", "1", "0", "0", "0", "0", "1"];
2019-05-18 20:27:15 +00:00
}
} else if x > 500000 {
if y > 500000 {
new_state = "180";
2019-05-19 03:41:30 +00:00
x_state = "inverted";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
2019-05-18 20:27:15 +00:00
}
else {
new_state = "270";
2019-05-19 03:41:30 +00:00
x_state = "right";
matrix = ["0", "1", "0", "-1", "0", "1", "0", "0", "1"];
2019-05-18 20:27:15 +00:00
}
} else {
if y > 500000 {
new_state = "180";
2019-05-19 03:41:30 +00:00
x_state = "inverted";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
2019-05-18 20:27:15 +00:00
}
else {
new_state = "normal";
2019-05-19 03:41:30 +00:00
x_state = "normal";
matrix = ["1", "0", "0", "0", "1", "0", "0", "0", "1"];
2019-05-18 20:27:15 +00:00
}
}
if new_state != old_state {
2019-05-19 03:41:30 +00:00
if mode == "sway" {
Command::new("swaymsg")
.arg("output")
.arg(display)
.arg("transform")
.arg(new_state)
.spawn()
.expect("rotate command failed to start");
2019-05-18 20:27:15 +00:00
2019-05-19 03:41:30 +00:00
old_state = new_state;
}
if mode == "x" {
Command::new("xrandr")
.arg("-o")
.arg(x_state)
.spawn()
.expect("rotate command failed to start");
2019-05-18 20:27:15 +00:00
2019-05-19 03:41:30 +00:00
Command::new("xinput")
.arg("set-prop")
.arg(touchscreen)
.arg("Coordinate")
.arg("Transformation")
.arg("Matrix")
.args(&matrix)
.spawn()
.expect("rotate command failed to start");
old_state = new_state;
}
}
thread::sleep(Duration::from_millis(500));
}
2019-05-18 20:27:15 +00:00
}