importing rotation algo update

This commit is contained in:
Derrick McKee 2019-12-31 16:23:29 -05:00
parent 41fb696fb0
commit c654e80acc
1 changed files with 66 additions and 37 deletions

View File

@ -83,6 +83,13 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result<
}
}
struct Orientation {
vector: (f32, f32),
new_state: &'static str,
x_state: &'static str,
matrix: [&'static str; 9]
}
fn main() -> Result<(), String> {
let mut new_state: &str;
let mut path_x: String = "".to_string();
@ -132,10 +139,19 @@ fn main() -> Result<(), String> {
.help("Set Touchscreen Device (X11)")
.takes_value(true),
)
.arg(
Arg::with_name("threshold")
.default_value("0.5")
.long("threshold")
.value_name("THRESHOLD")
.help("Set a rotation threshold between 0 and 1")
.takes_value(true)
)
.get_matches();
let sleep = matches.value_of("sleep").unwrap_or("default.conf");
let display = matches.value_of("display").unwrap_or("default.conf");
let touchscreen = matches.value_of("touchscreen").unwrap_or("default.conf");
let threshold = matches.value_of("threshold").unwrap_or("default.conf");
let old_state_owned = get_window_server_rotation_state(display, &backend)?;
let mut old_state = old_state_owned.as_str();
@ -156,44 +172,57 @@ fn main() -> Result<(), String> {
}
}
let orientations = [
Orientation {
vector: (0.0, -1.0),
new_state: "normal",
x_state: "normal",
matrix: ["1", "0", "0", "0", "1", "0", "0", "0", "1"]
},
Orientation {
vector: (0.0, 1.0),
new_state: "180",
x_state: "inverted",
matrix: ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"]
},
Orientation {
vector: (-1.0, 0.0),
new_state: "90",
x_state: "right",
matrix: ["0", "-1", "1", "1", "0", "0", "0", "0", "1"]
},
Orientation {
vector: (1.0, 0.0),
new_state: "270",
x_state: "left",
matrix: ["0", "1", "0", "-1", "0", "1", "0", "0", "1"]
}
];
let mut current_orient: &Orientation = &orientations[0];
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);
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);
if x < -500000 {
if y > 500000 {
new_state = "180";
x_state = "normal";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
} else {
new_state = "90";
x_state = "right";
matrix = ["0", "-1", "1", "1", "0", "0", "0", "0", "1"];
}
} else if x > 500000 {
if y > 500000 {
new_state = "180";
x_state = "inverted";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
} else {
new_state = "270";
x_state = "left";
matrix = ["0", "1", "0", "-1", "0", "1", "0", "0", "1"];
}
} else {
if y > 500000 {
new_state = "180";
x_state = "inverted";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
} else {
new_state = "normal";
x_state = "normal";
matrix = ["1", "0", "0", "0", "1", "0", "0", "0", "1"];
// Normalize vectors
let x: f32 = (x_clean as f32)/1e6;
let y: f32 = (y_clean as f32)/1e6;
for (_i, orient) in orientations.iter().enumerate() {
let d = (x-orient.vector.0).powf(2.0) + (y-orient.vector.1).powf(2.0);
if d < threshold.parse::<f32>().unwrap_or(0.5) {
current_orient = &orient;
break;
}
}
new_state = current_orient.new_state;
x_state = current_orient.x_state;
matrix = current_orient.matrix;
if new_state != old_state {
match backend {
Backend::Sway => {