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-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-12-29 23:13:41 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
use glob::glob;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-12-29 23:13:41 +00:00
|
|
|
fn get_keyboards(backend: &Backend) -> Result<Vec<String>, String> {
|
|
|
|
match backend {
|
|
|
|
Backend::Sway => {
|
|
|
|
let raw_inputs = String::from_utf8(
|
|
|
|
Command::new("swaymsg")
|
|
|
|
.arg("-t")
|
|
|
|
.arg("get_inputs")
|
|
|
|
.arg("--raw")
|
|
|
|
.output()
|
|
|
|
.expect("Swaymsg get inputs command failed")
|
|
|
|
.stdout,
|
|
|
|
)
|
2020-01-04 22:40:30 +00:00
|
|
|
.unwrap();
|
2019-12-29 23:13:41 +00:00
|
|
|
|
|
|
|
let mut keyboards = vec![];
|
|
|
|
let deserialized: Vec<Value> = serde_json::from_str(&raw_inputs)
|
|
|
|
.expect("Unable to deserialize swaymsg JSON output");
|
|
|
|
for output in deserialized {
|
|
|
|
let input_type = output["type"].as_str().unwrap();
|
|
|
|
if input_type == "keyboard" {
|
|
|
|
keyboards.push(output["identifier"].to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(keyboards);
|
|
|
|
}
|
|
|
|
Backend::Xorg => {
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:08:19 +00:00
|
|
|
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,
|
|
|
|
)
|
2020-01-04 22:40:30 +00:00
|
|
|
.unwrap();
|
2019-09-05 14:08:19 +00:00
|
|
|
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
|
|
|
|
)
|
2019-12-31 21:23:29 +00:00
|
|
|
.to_owned());
|
2019-09-05 14:08:19 +00:00
|
|
|
}
|
|
|
|
Backend::Xorg => {
|
|
|
|
let raw_rotation_state = String::from_utf8(
|
|
|
|
Command::new("xrandr")
|
|
|
|
.output()
|
|
|
|
.expect("Xrandr get outputs command failed to start")
|
|
|
|
.stdout,
|
|
|
|
)
|
2020-01-04 22:40:30 +00:00
|
|
|
.unwrap();
|
2019-09-05 14:08:19 +00:00
|
|
|
let xrandr_output_pattern = regex::Regex::new(format!(
|
2019-12-31 21:23:29 +00:00
|
|
|
r"^{} connected .+? .+? (normal |inverted |left |right )?\(normal left inverted right x axis y axis\) .+$",
|
|
|
|
regex::escape(display),
|
2019-09-05 14:08:19 +00:00
|
|
|
).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
|
|
|
|
)
|
2020-01-04 22:40:30 +00:00
|
|
|
.to_owned());
|
2019-09-05 14:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:23:29 +00:00
|
|
|
struct Orientation {
|
|
|
|
vector: (f32, f32),
|
|
|
|
new_state: &'static str,
|
|
|
|
x_state: &'static str,
|
2020-01-04 22:40:30 +00:00
|
|
|
matrix: [&'static str; 9],
|
2019-12-31 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-12-29 23:13:41 +00:00
|
|
|
let mut args = vec![
|
|
|
|
Arg::with_name("sleep")
|
|
|
|
.default_value("500")
|
|
|
|
.long("sleep")
|
2020-01-04 22:40:30 +00:00
|
|
|
.short("s")
|
2019-12-29 23:13:41 +00:00
|
|
|
.value_name("SLEEP")
|
|
|
|
.help("Set sleep millis")
|
|
|
|
.takes_value(true),
|
|
|
|
Arg::with_name("display")
|
|
|
|
.default_value("eDP-1")
|
|
|
|
.long("display")
|
2020-01-04 22:40:30 +00:00
|
|
|
.short("d")
|
2019-12-29 23:13:41 +00:00
|
|
|
.value_name("DISPLAY")
|
|
|
|
.help("Set Display Device")
|
|
|
|
.takes_value(true),
|
|
|
|
Arg::with_name("touchscreen")
|
|
|
|
.default_value("ELAN0732:00 04F3:22E1")
|
|
|
|
.long("touchscreen")
|
2020-01-04 22:40:30 +00:00
|
|
|
.short("i")
|
2019-12-29 23:13:41 +00:00
|
|
|
.value_name("TOUCHSCREEN")
|
2020-01-04 22:40:30 +00:00
|
|
|
.help("Set Touchscreen input Device (X11 only)")
|
2019-12-31 21:29:56 +00:00
|
|
|
.takes_value(true),
|
|
|
|
Arg::with_name("threshold")
|
|
|
|
.default_value("0.5")
|
|
|
|
.long("threshold")
|
2020-01-04 22:40:30 +00:00
|
|
|
.short("t")
|
2019-12-31 21:29:56 +00:00
|
|
|
.value_name("THRESHOLD")
|
|
|
|
.help("Set a rotation threshold between 0 and 1")
|
2020-01-04 22:40:30 +00:00
|
|
|
.takes_value(true),
|
2019-12-29 23:13:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
match backend {
|
|
|
|
Backend::Sway => {
|
|
|
|
args.push(
|
|
|
|
Arg::with_name("keyboard")
|
|
|
|
.long("disable-keyboard")
|
|
|
|
.short("k")
|
|
|
|
.help("Disable keyboard for tablet modes (Sway only)")
|
2020-01-04 22:40:30 +00:00
|
|
|
.takes_value(false),
|
2019-12-29 23:13:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Backend::Xorg => { /* Keyboard disabling in Xorg is not supported yet */ }
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:50:44 +00:00
|
|
|
let cmd_lines = App::new("rot8").version("0.1.3").args(&args);
|
2019-12-29 23:13:41 +00:00
|
|
|
|
|
|
|
let matches = cmd_lines.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-12-29 23:13:41 +00:00
|
|
|
let disable_keyboard = matches.is_present("keyboard");
|
2019-12-31 21:23:29 +00:00
|
|
|
let threshold = matches.value_of("threshold").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-12-29 23:13:41 +00:00
|
|
|
let keyboards = get_keyboards(&backend)?;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:23:29 +00:00
|
|
|
let orientations = [
|
|
|
|
Orientation {
|
|
|
|
vector: (0.0, -1.0),
|
|
|
|
new_state: "normal",
|
|
|
|
x_state: "normal",
|
2020-01-04 22:40:30 +00:00
|
|
|
matrix: ["1", "0", "0", "0", "1", "0", "0", "0", "1"],
|
2019-12-31 21:23:29 +00:00
|
|
|
},
|
|
|
|
Orientation {
|
|
|
|
vector: (0.0, 1.0),
|
|
|
|
new_state: "180",
|
|
|
|
x_state: "inverted",
|
2020-01-04 22:40:30 +00:00
|
|
|
matrix: ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"],
|
2019-12-31 21:23:29 +00:00
|
|
|
},
|
|
|
|
Orientation {
|
|
|
|
vector: (-1.0, 0.0),
|
|
|
|
new_state: "90",
|
|
|
|
x_state: "right",
|
2020-03-05 21:06:16 +00:00
|
|
|
matrix: ["0", "1", "0", "-1", "0", "1", "0", "0", "1"],
|
2019-12-31 21:23:29 +00:00
|
|
|
},
|
|
|
|
Orientation {
|
|
|
|
vector: (1.0, 0.0),
|
|
|
|
new_state: "270",
|
|
|
|
x_state: "left",
|
2020-03-05 21:06:16 +00:00
|
|
|
matrix: ["0", "-1", "1", "1", "0", "0", "0", "0", "1"],
|
2020-01-04 22:40:30 +00:00
|
|
|
},
|
2019-12-31 21:23:29 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let mut current_orient: &Orientation = &orientations[0];
|
|
|
|
|
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();
|
2019-12-31 21:23:29 +00:00
|
|
|
let x_clean = x_raw.trim_end_matches('\n').parse::<i32>().unwrap_or(0);
|
|
|
|
let y_clean = y_raw.trim_end_matches('\n').parse::<i32>().unwrap_or(0);
|
|
|
|
|
|
|
|
// Normalize vectors
|
2020-01-04 22:40:30 +00:00
|
|
|
let x: f32 = (x_clean as f32) / 1e6;
|
|
|
|
let y: f32 = (y_clean as f32) / 1e6;
|
2019-12-31 21:23:29 +00:00
|
|
|
|
|
|
|
for (_i, orient) in orientations.iter().enumerate() {
|
2020-01-04 22:40:30 +00:00
|
|
|
let d = (x - orient.vector.0).powf(2.0) + (y - orient.vector.1).powf(2.0);
|
2019-12-31 21:23:29 +00:00
|
|
|
if d < threshold.parse::<f32>().unwrap_or(0.5) {
|
|
|
|
current_orient = &orient;
|
|
|
|
break;
|
2019-05-18 20:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:23:29 +00:00
|
|
|
new_state = current_orient.new_state;
|
|
|
|
x_state = current_orient.x_state;
|
|
|
|
matrix = current_orient.matrix;
|
|
|
|
|
2019-05-18 20:27:15 +00:00
|
|
|
if new_state != old_state {
|
2020-01-04 22:40:30 +00:00
|
|
|
let keyboard_state = if new_state == "normal" {
|
|
|
|
"enabled"
|
|
|
|
} else {
|
|
|
|
"disabled"
|
|
|
|
};
|
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-06 07:22:08 +00:00
|
|
|
.expect("Swaymsg rotate command failed to start")
|
|
|
|
.wait()
|
|
|
|
.expect("Swaymsg rotate command wait failed");
|
2019-12-29 23:13:41 +00:00
|
|
|
if disable_keyboard {
|
|
|
|
for keyboard in &keyboards {
|
2020-01-04 22:40:30 +00:00
|
|
|
// println!("swaymsg input {} events {}", keyboard, keyboard_state);
|
2019-12-29 23:13:41 +00:00
|
|
|
Command::new("swaymsg")
|
|
|
|
.arg("input")
|
|
|
|
.arg(keyboard)
|
|
|
|
.arg("events")
|
|
|
|
.arg(keyboard_state)
|
|
|
|
.spawn()
|
|
|
|
.expect("Swaymsg keyboard command failed to start")
|
|
|
|
.wait()
|
|
|
|
.expect("Swaymsg keyboard command wait failed");
|
|
|
|
}
|
|
|
|
}
|
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-06 07:22:08 +00:00
|
|
|
.expect("Xrandr rotate command failed to start")
|
|
|
|
.wait()
|
|
|
|
.expect("Xrandr rotate command wait failed");
|
2019-05-19 03:41:30 +00:00
|
|
|
|
2019-09-06 00:28:26 +00:00
|
|
|
Command::new("xinput")
|
|
|
|
.arg("set-prop")
|
|
|
|
.arg(touchscreen)
|
2020-03-05 21:06:16 +00:00
|
|
|
.arg("Coordinate Transformation Matrix")
|
2019-09-06 00:28:26 +00:00
|
|
|
.args(&matrix)
|
|
|
|
.spawn()
|
2019-09-06 07:22:08 +00:00
|
|
|
.expect("Xinput rotate command failed to start")
|
|
|
|
.wait()
|
|
|
|
.expect("Xinput rotate command wait failed");
|
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
|
|
|
}
|