Compare commits

..

No commits in common. "master" and "add-license-1" have entirely different histories.

4 changed files with 148 additions and 477 deletions

173
Cargo.lock generated
View File

@ -1,33 +1,26 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.13"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.1.0"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -36,11 +29,11 @@ version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -49,100 +42,35 @@ name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "itoa"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.62"
version = "0.2.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.2.1"
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "1.0.3"
name = "redox_syscall"
version = "0.1.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rot8"
version = "0.1.3"
version = "0.1.1"
dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ryu"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde_derive 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_derive"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_json"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -151,13 +79,14 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
version = "1.0.5"
name = "termion"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
"numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -165,25 +94,12 @@ name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-width"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.2.0"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -193,7 +109,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.8"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -211,31 +127,20 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
"checksum proc-macro2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e98a83a9f9b331f54b924e68a66acb1bb35cb01fb0a23645139967abefb697e8"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd"
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
"checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
"checksum serde 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)" = "f4473e8506b213730ff2061073b48fa51dcc66349219e2e7c5608f0296a1d95a"
"checksum serde_derive 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)" = "11e410fde43e157d789fc290d26bc940778ad0fdd47836426fbac36573710dbb"
"checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704"
"checksum libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "42914d39aad277d9e176efbdad68acb1d5443ab65afe0e0e4f0d49352a950880"
"checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
"checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
"checksum termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dde0593aeb8d47accea5392b39350015b5eccb12c0d98044d856983d89548dea"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -1,18 +1,9 @@
[package]
name = "rot8"
version = "0.1.3"
authors = ["efernau <e.fernau@efero.de>", "deadly_platypus <mail@geth.systems>", "nicola-sorace"]
license = "MIT"
description = "automatic display rotation using built-in accelerometer"
homepage = "https://github.com/efernau/rot8"
documentation = "https://github.com/efernau/rot8"
repository = "https://github.com/efernau/rot8"
keywords = ["sway", "x11", "display", "rotation"]
version = "0.1.1"
authors = ["efernau <e.fernau@efero.de>"]
edition = "2018"
[dependencies]
clap = "2.33"
glob = "0.3"
regex = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = "2.33"

View File

@ -1,59 +1,20 @@
# rot8
## automatic display rotation using built-in accelerometer
## automatic display rotation
Automatic rotate modern Linux desktop screen and input devices. Handy for
convertible touchscreen notebooks like the Dell Inspiron 7506 2n1.
Compatible with [sway](http://swaywm.org/) and [X11](https://www.x.org/wiki/Releases/7.7/).
convertible touchscreen notebooks like the Kaby Lake model of the HP Spectre x360.
Rust language and the cargo package manager are required to build the binary.
```
$ git clone https://github.com/EddieCueto/rot8
$ git clone https://github.com/efernau/rot8
$ cd rot8 && cargo build --release
$ cp target/release/rot8 /usr/bin/rot8
```
or
Call Rote8 from sway configuration file ~/.config/sway/config:
```
$ cargo install rot8
```
For Sway map your input to the output device:
```
$ swaymsg input <INPUTDEVICE> map_to_output <OUTPUTDEVICE>
```
Call rot8 from sway configuration file ~/.config/sway/config:
```
exec rot8
```
For X11 set Touchscreen Device
```
rot8 --touchscreen <TOUCHSCREEN>
```
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
--threshold // Set a rotation threshold between 0 and 1 (0.5)
```

View File

@ -1,342 +1,156 @@
extern crate clap;
extern crate glob;
extern crate regex;
extern crate clap;
use clap::{Arg, App};
use std::fs;
use std::process::Command;
use std::thread;
use std::time::Duration;
use clap::{App, Arg};
use std::process::Command;
use glob::glob;
use serde::Deserialize;
use serde_json::Value;
enum Backend {
Sway,
Xorg,
}
#[derive(Deserialize)]
struct SwayOutput {
name: String,
transform: String,
}
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,
)
.unwrap();
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![]);
}
}
}
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());
}
}
}
struct Orientation {
vector: (f32, f32),
new_state: &'static str,
x_state: &'static str,
matrix: [&'static str; 9],
}
fn main() -> Result<(), String> {
fn main() {
let mut mode = "";
let mut old_state = "normal";
let mut new_state: &str;
let mut path_x: String = "".to_string();
let mut path_y: String = "".to_string();
let mut matrix: [&str; 9];
let mut matrix: [&str;9];
let mut x_state: &str;
let backend = if String::from_utf8(Command::new("pidof").arg("sway").output().unwrap().stdout)
.unwrap()
.len()
>= 1
{
Backend::Sway
} else if String::from_utf8(Command::new("pidof").arg("Xorg").output().unwrap().stdout)
.unwrap()
.len()
>= 1
{
Backend::Xorg
} else {
return Err("Unable to find Sway or Xorg procceses".to_owned());
};
let sway_pid = String::from_utf8(Command::new("pidof")
.arg("sway")
.output()
.unwrap()
.stdout).unwrap();
let mut args = vec![
Arg::with_name("sleep")
.default_value("500")
.long("sleep")
.short("s")
.value_name("SLEEP")
.help("Set sleep millis")
.takes_value(true),
Arg::with_name("display")
.default_value("eDP-1")
.long("display")
.short("d")
.value_name("DISPLAY")
.help("Set Display Device")
.takes_value(true),
Arg::with_name("touchscreen")
.default_value("ELAN0732:00 04F3:22E1")
.long("touchscreen")
.short("i")
.value_name("TOUCHSCREEN")
.help("Set Touchscreen input Device (X11 only)")
.takes_value(true),
Arg::with_name("threshold")
.default_value("0.5")
.long("threshold")
.short("t")
.value_name("THRESHOLD")
.help("Set a rotation threshold between 0 and 1")
.takes_value(true),
Arg::with_name("normalization-factor")
.default_value("1e6")
.long("normalization-factor")
.short("n")
.value_name("NORMALIZATION_FACTOR")
.help("Set factor for sensor value normalization")
.takes_value(true),
];
let x_pid = String::from_utf8(Command::new("pidof")
.arg("x")
.output()
.unwrap()
.stdout).unwrap();
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 */ }
if sway_pid.len() >= 1 {
mode = "sway";
}
if x_pid.len() >= 1 {
mode = "x";
}
let cmd_lines = App::new("rot8").version("0.1.3").args(&args);
let matches = cmd_lines.get_matches();
let sleep = matches.value_of("sleep").unwrap_or("default.conf");
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");
let disable_keyboard = matches.is_present("keyboard");
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();
let touchscreen = matches.value_of("touchscreen").unwrap_or("default.conf");
let normalization_factor = matches.value_of("normalization-factor").unwrap_or("1e6");
let normalization_factor = normalization_factor.parse::<f32>().unwrap_or(1e6);
let keyboards = get_keyboards(&backend)?;
for entry in glob("/sys/bus/iio/devices/iio:device*/in_accel_*_raw").unwrap() {
match entry {
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") {
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") {
} 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") {
} else if path.to_str().unwrap().contains("z_raw"){
continue;
} else {
panic!("Unknown accelerometer device path {:?}", path);
println!("{:?}", path);
panic!();
}
}
Err(e) => println!("{:?}", e),
},
Err(e) => println!("{:?}",e)
}
}
let orientations = [
Orientation {
vector: (0.0, 0.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", "0", "-1", "0", "1", "0", "0", "1"],
},
Orientation {
vector: (1.0, 0.0),
new_state: "270",
x_state: "left",
matrix: ["0", "-1", "1", "1", "0", "0", "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_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);
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);
// Normalize vectors
let x: f32 = (x_clean as f32) / normalization_factor;
let y: f32 = (y_clean as f32) / normalization_factor;
if x < -500000 {
if y > 500000 {
new_state = "180";
x_state = "normal";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
}
else {
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 = "90";
x_state = "left";
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 = "right";
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"];
}
}
new_state = current_orient.new_state;
x_state = current_orient.x_state;
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")
if mode == "sway" {
Command::new("swaymsg")
.arg("output")
.arg(display)
.arg("transform")
.arg(new_state)
.spawn()
.expect("Swaymsg rotate command failed to start")
.wait()
.expect("Swaymsg rotate 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")
.expect("rotate command failed to start");
old_state = new_state;
}
if mode == "x" {
Command::new("xrandr")
.arg("-o")
.arg(x_state)
.spawn()
.expect("Xrandr rotate command failed to start")
.wait()
.expect("Xrandr rotate command wait failed");
.expect("rotate command failed to start");
Command::new("xinput")
Command::new("xinput")
.arg("set-prop")
.arg(touchscreen)
.arg("Coordinate Transformation Matrix")
.arg("Coordinate")
.arg("Transformation")
.arg("Matrix")
.args(&matrix)
.spawn()
.expect("Xinput rotate command failed to start")
.wait()
.expect("Xinput rotate command wait failed");
}
.expect("rotate command failed to start");
old_state = new_state;
}
old_state = new_state;
}
thread::sleep(Duration::from_millis(sleep.parse::<u64>().unwrap_or(0)));
thread::sleep(Duration::from_millis(500));
}
}