From 4dd544fb412c3dca8c5bb7fac61b3f532d2994c7 Mon Sep 17 00:00:00 2001 From: Derrick McKee Date: Fri, 27 Dec 2019 10:03:55 -0500 Subject: [PATCH 1/5] adding keyboard deactivation upon rotation --- README.md | 1 + src/main.rs | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8c779db..b60250d 100644 --- a/README.md +++ b/README.md @@ -55,5 +55,6 @@ there are the following args. --sleep // Set sleep millis (500) --display // Set Display Device (eDP-1) --touchscreen // Set Touchscreen Device X11 (ELAN0732:00 04F3:22E1) +--keyboard // Set keyboard to deactivate upon rotation ``` diff --git a/src/main.rs b/src/main.rs index 5c69dba..b4ecd4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< .expect("Swaymsg get outputs command failed to start") .stdout, ) - .unwrap(); + .unwrap(); let deserialized: Vec = serde_json::from_str(&raw_rotation_state) .expect("Unable to deserialize swaymsg JSON output"); for output in deserialized { @@ -46,7 +46,7 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< "Unable to determine rotation state: display {} not found in 'swaymsg -t get_outputs'", display ) - .to_owned()); + .to_owned()); } Backend::Xorg => { let raw_rotation_state = String::from_utf8( @@ -55,10 +55,10 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< .expect("Xrandr get outputs command failed to start") .stdout, ) - .unwrap(); + .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), + 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) { @@ -78,7 +78,7 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< "Unable to determine rotation state: display {} not found in xrandr output", display ) - .to_owned()); + .to_owned()); } } } @@ -89,6 +89,7 @@ fn main() -> Result<(), String> { let mut path_y: String = "".to_string(); let mut matrix: [&str; 9]; let mut x_state: &str; + let mut keyboard_state: &str; let backend = if String::from_utf8(Command::new("pidof").arg("sway").output().unwrap().stdout) .unwrap() @@ -132,10 +133,18 @@ fn main() -> Result<(), String> { .help("Set Touchscreen Device (X11)") .takes_value(true), ) + .arg( + Arg::with_name("keyboard") + .long("keyboard") + .value_name("KEYBOARD") + .help("Disable keyboard for tablet modes") + .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 keyboard = matches.value_of("keyboard").unwrap_or("default.conf"); let old_state_owned = get_window_server_rotation_state(display, &backend)?; let mut old_state = old_state_owned.as_str(); @@ -165,10 +174,12 @@ fn main() -> Result<(), String> { if x < -500000 { if y > 500000 { new_state = "180"; + keyboard_state = "disabled"; x_state = "normal"; matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"]; } else { new_state = "90"; + keyboard_state = "disabled"; x_state = "right"; matrix = ["0", "-1", "1", "1", "0", "0", "0", "0", "1"]; } @@ -176,19 +187,23 @@ fn main() -> Result<(), String> { if y > 500000 { new_state = "180"; x_state = "inverted"; + keyboard_state = "disabled"; matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"]; } else { new_state = "270"; + keyboard_state = "disabled"; x_state = "left"; matrix = ["0", "1", "0", "-1", "0", "1", "0", "0", "1"]; } } else { if y > 500000 { new_state = "180"; + keyboard_state = "disabled"; x_state = "inverted"; matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"]; } else { new_state = "normal"; + keyboard_state = "enabled"; x_state = "normal"; matrix = ["1", "0", "0", "0", "1", "0", "0", "0", "1"]; } @@ -206,6 +221,15 @@ fn main() -> Result<(), String> { .expect("Swaymsg rotate command failed to start") .wait() .expect("Swaymsg rotate command wait failed"); + 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"); } Backend::Xorg => { Command::new("xrandr") From 8036af4a4bc1a0c9c10cbbbe78cd906b1c15f7a7 Mon Sep 17 00:00:00 2001 From: Derrick McKee Date: Fri, 27 Dec 2019 10:12:42 -0500 Subject: [PATCH 2/5] adding name to Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3f3bf0a..1e32b43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rot8" version = "0.1.2" -authors = ["efernau "] +authors = ["efernau ", "deadly_platypus "] license = "MIT" description = "automatic display rotation using built-in accelerometer" homepage = "https://github.com/efernau/rot8" From 571bd5897ffa025b0fc69b456cbe4785718c26bb Mon Sep 17 00:00:00 2001 From: Derrick McKee Date: Sun, 29 Dec 2019 18:13:41 -0500 Subject: [PATCH 3/5] automatically get all keyboards and disable them if desired --- src/main.rs | 139 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index b4ecd4d..b06f042 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,16 @@ extern crate clap; extern crate glob; extern crate regex; -use clap::{App, Arg}; -use glob::glob; -use serde::Deserialize; use std::fs; use std::process::Command; use std::thread; use std::time::Duration; +use clap::{App, Arg}; +use glob::glob; +use serde::Deserialize; +use serde_json::Value; + enum Backend { Sway, Xorg, @@ -21,6 +23,38 @@ struct SwayOutput { transform: String, } +fn get_keyboards(backend: &Backend) -> Result, 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, + ) + .unwrap(); + + let mut keyboards = vec![]; + let deserialized: Vec = 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![]); + } + } +} + fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result { match backend { Backend::Sway => { @@ -107,47 +141,55 @@ fn main() -> Result<(), String> { return Err("Unable to find Sway or Xorg procceses".to_owned()); }; - let matches = App::new("rot8") + let mut args = vec![ + Arg::with_name("sleep") + .default_value("500") + .long("sleep") + .value_name("SLEEP") + .help("Set sleep millis") + .takes_value(true), + Arg::with_name("display") + .default_value("eDP-1") + .long("display") + .value_name("DISPLAY") + .help("Set Display Device") + .takes_value(true), + Arg::with_name("touchscreen") + .default_value("ELAN0732:00 04F3:22E1") + .long("touchscreen") + .value_name("TOUCHSCREEN") + .help("Set Touchscreen Device (X11)") + .takes_value(true) + ]; + + match backend { + Backend::Sway => { + args.push( + Arg::with_name("keyboard") + .long("disable-keyboard") + .short("k") + .help("Disable keyboard for tablet modes (Sway only)") + .takes_value(false) + ); + } + Backend::Xorg => { /* Keyboard disabling in Xorg is not supported yet */ } + } + + let cmd_lines = App::new("rot8") .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), - ) - .arg( - Arg::with_name("keyboard") - .long("keyboard") - .value_name("KEYBOARD") - .help("Disable keyboard for tablet modes") - .takes_value(true), - ) - .get_matches(); + .args(&args); + + let matches = cmd_lines.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 keyboard = matches.value_of("keyboard").unwrap_or("default.conf"); + let disable_keyboard = matches.is_present("keyboard"); let old_state_owned = get_window_server_rotation_state(display, &backend)?; let mut old_state = old_state_owned.as_str(); + let keyboards = get_keyboards(&backend)?; + for entry in glob("/sys/bus/iio/devices/iio:device*/in_accel_*_raw").unwrap() { match entry { Ok(path) => { @@ -221,15 +263,20 @@ fn main() -> Result<(), String> { .expect("Swaymsg rotate command failed to start") .wait() .expect("Swaymsg rotate command wait failed"); - 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"); + if disable_keyboard { + for keyboard in &keyboards { +// println!("swaymsg input {} events {}", keyboard, keyboard_state); + 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"); + } + } } Backend::Xorg => { Command::new("xrandr") From c654e80accbacb185595528893286aa7661a0ec9 Mon Sep 17 00:00:00 2001 From: Derrick McKee Date: Tue, 31 Dec 2019 16:23:29 -0500 Subject: [PATCH 4/5] importing rotation algo update --- src/main.rs | 103 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c69dba..40f06bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< .expect("Swaymsg get outputs command failed to start") .stdout, ) - .unwrap(); + .unwrap(); let deserialized: Vec = serde_json::from_str(&raw_rotation_state) .expect("Unable to deserialize swaymsg JSON output"); for output in deserialized { @@ -46,7 +46,7 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< "Unable to determine rotation state: display {} not found in 'swaymsg -t get_outputs'", display ) - .to_owned()); + .to_owned()); } Backend::Xorg => { let raw_rotation_state = String::from_utf8( @@ -55,10 +55,10 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< .expect("Xrandr get outputs command failed to start") .stdout, ) - .unwrap(); + .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), + 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) { @@ -78,11 +78,18 @@ fn get_window_server_rotation_state(display: &str, backend: &Backend) -> Result< "Unable to determine rotation state: display {} not found in xrandr output", display ) - .to_owned()); + .to_owned()); } } } +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::().unwrap_or(0); - let y = y_raw.trim_end_matches('\n').parse::().unwrap_or(0); + let x_clean = x_raw.trim_end_matches('\n').parse::().unwrap_or(0); + let y_clean = y_raw.trim_end_matches('\n').parse::().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::().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 => { From 0b3b48967f15c2266bd23a7233cbdeb21eb5ba94 Mon Sep 17 00:00:00 2001 From: Derrick McKee Date: Tue, 31 Dec 2019 16:39:09 -0500 Subject: [PATCH 5/5] incorporating rotation algorithm update --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index a8fda2b..52667a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -272,6 +272,7 @@ fn main() -> Result<(), String> { matrix = current_orient.matrix; if new_state != old_state { + let keyboard_state = if new_state == "normal" { "enabled" } else { "disabled" }; match backend { Backend::Sway => { Command::new("swaymsg")