2019-05-19 03:41:30 +00:00
|
|
|
extern crate clap;
|
2019-09-06 00:28:50 +00:00
|
|
|
extern crate glob;
|
2019-09-05 14:08:19 +00:00
|
|
|
extern crate regex;
|
|
|
|
|
2019-09-06 00:28:50 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
use glob::glob;
|
2019-09-05 14:08:19 +00:00
|
|
|
use serde::Deserialize;
|
2019-05-18 20:27:15 +00:00
|
|
|
use std::fs;
|
2019-09-06 00:28:50 +00:00
|
|
|
use std::process::Command;
|
2019-05-18 20:27:15 +00:00
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2019-09-05 14:08:19 +00:00
|
|
|
enum Backend {
|
2019-09-06 00:28:26 +00:00
|
|
|
Sway,
|
|
|
|
Xorg,
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:08:19 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SwayOutput {
|
|
|
|
name: String,
|
|
|
|
transform: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result<String, String> {
|
|
|
|
match backend {
|
|
|
|
Backend::Sway => {
|
|
|
|
let raw_rotation_state = String::from_utf8(
|
|
|
|
Command::new("swaymsg")
|
|
|
|
.arg("-t")
|
|
|
|
.arg("get_outputs")
|
|
|
|
.arg("--raw")
|
|
|
|
.output()
|
|
|
|
.expect("Swaymsg get outputs command failed to start")
|
|
|
|
.stdout,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let deserialized: Vec<SwayOutput> = serde_json::from_str(&raw_rotation_state)
|
|
|
|
.expect("Unable to deserialize swaymsg JSON output");
|
|
|
|
for output in deserialized {
|
|
|
|
if output.name == display {
|
|
|
|
return Ok(output.transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(format!(
|
|
|
|
"Unable to determine rotation state: display {} not found in 'swaymsg -t get_outputs'",
|
|
|
|
display
|
|
|
|
)
|
|
|
|
.to_owned());
|
|
|
|
}
|
|
|
|
Backend::Xorg => {
|
|
|
|
let raw_rotation_state = String::from_utf8(
|
|
|
|
Command::new("xrandr")
|
|
|
|
.output()
|
|
|
|
.expect("Xrandr get outputs command failed to start")
|
|
|
|
.stdout,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let xrandr_output_pattern = regex::Regex::new(format!(
|
|
|
|
r"^{} connected .+? .+? (normal |inverted |left |right )?\(normal left inverted right x axis y axis\) .+$",
|
|
|
|
regex::escape(display),
|
|
|
|
).as_str()).unwrap();
|
|
|
|
for xrandr_output_line in raw_rotation_state.split("\n") {
|
|
|
|
if !xrandr_output_pattern.is_match(xrandr_output_line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let xrandr_output_captures =
|
|
|
|
xrandr_output_pattern.captures(xrandr_output_line).unwrap();
|
|
|
|
if let Some(transform) = xrandr_output_captures.get(1) {
|
|
|
|
return Ok(transform.as_str().to_owned());
|
|
|
|
} else {
|
|
|
|
return Ok("normal".to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(format!(
|
|
|
|
"Unable to determine rotation state: display {} not found in xrandr output",
|
|
|
|
display
|
|
|
|
)
|
|
|
|
.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:28:26 +00:00
|
|
|
fn main() -> Result<(), String> {
|
2019-05-18 20:27:15 +00:00
|
|
|
let mut new_state: &str;
|
|
|
|
let mut path_x: String = "".to_string();
|
|
|
|
let mut path_y: String = "".to_string();
|
2019-09-06 00:28:50 +00:00
|
|
|
let mut matrix: [&str; 9];
|
2019-05-19 03:41:30 +00:00
|
|
|
let mut x_state: &str;
|
|
|
|
|
2019-09-05 14:08:19 +00:00
|
|
|
let backend = if String::from_utf8(Command::new("pidof").arg("sway").output().unwrap().stdout)
|
2019-09-06 00:28:26 +00:00
|
|
|
.unwrap()
|
|
|
|
.len()
|
|
|
|
>= 1
|
|
|
|
{
|
2019-09-05 14:08:19 +00:00
|
|
|
Backend::Sway
|
2019-09-06 00:28:26 +00:00
|
|
|
} else if String::from_utf8(Command::new("pidof").arg("Xorg").output().unwrap().stdout)
|
|
|
|
.unwrap()
|
|
|
|
.len()
|
|
|
|
>= 1
|
|
|
|
{
|
2019-09-05 14:08:19 +00:00
|
|
|
Backend::Xorg
|
2019-09-06 00:28:26 +00:00
|
|
|
} else {
|
|
|
|
return Err("Unable to find Sway or Xorg procceses".to_owned());
|
|
|
|
};
|
2019-05-19 03:41:30 +00:00
|
|
|
|
|
|
|
let matches = App::new("rot8")
|
2019-09-06 00:28:50 +00:00
|
|
|
.version("0.1.1")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("sleep")
|
|
|
|
.default_value("500")
|
|
|
|
.long("sleep")
|
|
|
|
.value_name("SLEEP")
|
|
|
|
.help("Set sleep millis")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.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("ELAN0732:00 04F3:22E1")
|
|
|
|
.long("touchscreen")
|
|
|
|
.value_name("TOUCHSCREEN")
|
|
|
|
.help("Set Touchscreen Device (X11)")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.get_matches();
|
2019-08-16 23:47:36 +00:00
|
|
|
let sleep = matches.value_of("sleep").unwrap_or("default.conf");
|
2019-05-19 03:41:30 +00:00
|
|
|
let display = matches.value_of("display").unwrap_or("default.conf");
|
2019-09-06 00:28:50 +00:00
|
|
|
let touchscreen = matches.value_of("touchscreen").unwrap_or("default.conf");
|
2019-09-05 14:08:19 +00:00
|
|
|
let old_state_owned = get_window_server_rotation_state(display, &backend)?;
|
|
|
|
let mut old_state = old_state_owned.as_str();
|
2019-05-19 03:41:30 +00:00
|
|
|
|
2019-09-06 00:28:50 +00:00
|
|
|
for entry in glob("/sys/bus/iio/devices/iio:device*/in_accel_*_raw").unwrap() {
|
|
|
|
match entry {
|
2019-05-18 20:27:15 +00:00
|
|
|
Ok(path) => {
|
2019-09-06 00:28:50 +00:00
|
|
|
if path.to_str().unwrap().contains("x_raw") {
|
2019-05-18 20:27:15 +00:00
|
|
|
path_x = path.to_str().unwrap().to_owned();
|
2019-09-06 00:28:50 +00:00
|
|
|
} else if path.to_str().unwrap().contains("y_raw") {
|
2019-05-18 20:27:15 +00:00
|
|
|
path_y = path.to_str().unwrap().to_owned();
|
2019-09-06 00:28:50 +00:00
|
|
|
} else if path.to_str().unwrap().contains("z_raw") {
|
2019-05-18 20:27:15 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2019-09-05 14:08:19 +00:00
|
|
|
panic!("Unknown accelerometer device path {:?}", path);
|
2019-05-18 20:27:15 +00:00
|
|
|
}
|
2019-09-06 00:28:50 +00:00
|
|
|
}
|
|
|
|
Err(e) => println!("{:?}", e),
|
2019-05-18 20:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-06 00:28:50 +00:00
|
|
|
} else {
|
|
|
|
new_state = "90";
|
2019-09-06 07:00:27 +00:00
|
|
|
x_state = "right";
|
2019-09-06 00:28:50 +00:00
|
|
|
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-09-06 00:28:50 +00:00
|
|
|
} else {
|
2019-05-18 20:27:15 +00:00
|
|
|
new_state = "270";
|
2019-09-06 07:00:27 +00:00
|
|
|
x_state = "left";
|
2019-05-19 03:41:30 +00:00
|
|
|
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-09-06 00:28:50 +00:00
|
|
|
} else {
|
2019-05-18 20:27:15 +00:00
|
|
|
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-09-05 14:08:19 +00:00
|
|
|
match backend {
|
|
|
|
Backend::Sway => {
|
2019-09-06 00:28:26 +00:00
|
|
|
Command::new("swaymsg")
|
|
|
|
.arg("output")
|
|
|
|
.arg(display)
|
|
|
|
.arg("transform")
|
|
|
|
.arg(new_state)
|
|
|
|
.spawn()
|
2019-09-05 14:08:19 +00:00
|
|
|
.expect("Swaymsg rotate command failed to start");
|
2019-09-06 00:28:26 +00:00
|
|
|
}
|
2019-09-05 14:08:19 +00:00
|
|
|
Backend::Xorg => {
|
2019-09-06 00:28:26 +00:00
|
|
|
Command::new("xrandr")
|
|
|
|
.arg("-o")
|
|
|
|
.arg(x_state)
|
|
|
|
.spawn()
|
2019-09-05 14:08:19 +00:00
|
|
|
.expect("Xrandr rotate command failed to start");
|
2019-05-19 03:41:30 +00:00
|
|
|
|
2019-09-06 00:28:26 +00:00
|
|
|
Command::new("xinput")
|
|
|
|
.arg("set-prop")
|
|
|
|
.arg(touchscreen)
|
|
|
|
.arg("Coordinate")
|
|
|
|
.arg("Transformation")
|
|
|
|
.arg("Matrix")
|
|
|
|
.args(&matrix)
|
|
|
|
.spawn()
|
2019-09-05 14:08:19 +00:00
|
|
|
.expect("Xinput rotate command failed to start");
|
2019-09-06 00:28:26 +00:00
|
|
|
}
|
2019-05-19 03:41:30 +00:00
|
|
|
}
|
2019-09-06 00:28:26 +00:00
|
|
|
old_state = new_state;
|
2019-05-19 03:41:30 +00:00
|
|
|
}
|
2019-08-17 00:07:30 +00:00
|
|
|
thread::sleep(Duration::from_millis(sleep.parse::<u64>().unwrap_or(0)));
|
2019-05-19 03:41:30 +00:00
|
|
|
}
|
2019-05-18 20:27:15 +00:00
|
|
|
}
|