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. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # 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]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.11.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ 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]] [[package]]
name = "atty" name = "atty"
version = "0.2.13" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (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]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.1.0" version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -36,11 +29,11 @@ version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.1.0 (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)", "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)", "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)", "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -49,100 +42,35 @@ name = "glob"
version = "0.3.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "libc" name = "libc"
version = "0.2.62" version = "0.2.55"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "memchr" name = "numtoa"
version = "2.2.1" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "proc-macro2" name = "redox_syscall"
version = "1.0.3" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ 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]] [[package]]
name = "rot8" name = "rot8"
version = "0.1.3" version = "0.1.1"
dependencies = [ dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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]] [[package]]
@ -151,13 +79,14 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "syn" name = "termion"
version = "1.0.5" version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"proc-macro2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"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)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -165,25 +94,12 @@ name = "textwrap"
version = "0.11.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"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)",
]
[[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)",
] ]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.6" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -193,7 +109,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.8" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "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" source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [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 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 atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "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 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 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 libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "42914d39aad277d9e176efbdad68acb1d5443ab65afe0e0e4f0d49352a950880"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252"
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"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 strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "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 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.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"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 vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "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-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" "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] [package]
name = "rot8" name = "rot8"
version = "0.1.3" version = "0.1.1"
authors = ["efernau <e.fernau@efero.de>", "deadly_platypus <mail@geth.systems>", "nicola-sorace"] authors = ["efernau <e.fernau@efero.de>"]
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"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
clap = "2.33"
glob = "0.3" glob = "0.3"
regex = "1" clap = "2.33"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -1,59 +1,20 @@
# rot8 # rot8
## automatic display rotation using built-in accelerometer ## automatic display rotation
Automatic rotate modern Linux desktop screen and input devices. Handy for Automatic rotate modern Linux desktop screen and input devices. Handy for
convertible touchscreen notebooks like the Dell Inspiron 7506 2n1. convertible touchscreen notebooks like the Kaby Lake model of the HP Spectre x360.
Compatible with [sway](http://swaywm.org/) and [X11](https://www.x.org/wiki/Releases/7.7/).
Rust language and the cargo package manager are required to build the binary. 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 $ cd rot8 && cargo build --release
$ cp target/release/rot8 /usr/bin/rot8 $ 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 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 glob;
extern crate regex; extern crate clap;
use clap::{Arg, App};
use std::fs; use std::fs;
use std::process::Command;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use std::process::Command;
use clap::{App, Arg};
use glob::glob; 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> { fn main() {
match backend { let mut mode = "";
Backend::Sway => { let mut old_state = "normal";
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> {
let mut new_state: &str; let mut new_state: &str;
let mut path_x: String = "".to_string(); let mut path_x: String = "".to_string();
let mut path_y: 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 mut x_state: &str;
let backend = if String::from_utf8(Command::new("pidof").arg("sway").output().unwrap().stdout) let sway_pid = String::from_utf8(Command::new("pidof")
.unwrap() .arg("sway")
.len() .output()
>= 1 .unwrap()
{ .stdout).unwrap();
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 mut args = vec![ let x_pid = String::from_utf8(Command::new("pidof")
Arg::with_name("sleep") .arg("x")
.default_value("500") .output()
.long("sleep") .unwrap()
.short("s") .stdout).unwrap();
.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),
];
match backend { if sway_pid.len() >= 1 {
Backend::Sway => { mode = "sway";
args.push( }
Arg::with_name("keyboard") if x_pid.len() >= 1 {
.long("disable-keyboard") mode = "x";
.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.3").args(&args); let matches = App::new("rot8")
.version("0.1.1")
let matches = cmd_lines.get_matches(); .arg(Arg::with_name("display")
.default_value("eDP-1")
let sleep = matches.value_of("sleep").unwrap_or("default.conf"); .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 display = matches.value_of("display").unwrap_or("default.conf");
let touchscreen = matches.value_of("touchscreen").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 normalization_factor = matches.value_of("normalization-factor").unwrap_or("1e6"); for entry in glob("/sys/bus/iio/devices/iio:device*/in_accel_*_raw").unwrap(){
let normalization_factor = normalization_factor.parse::<f32>().unwrap_or(1e6); match entry {
let keyboards = get_keyboards(&backend)?;
for entry in glob("/sys/bus/iio/devices/iio:device*/in_accel_*_raw").unwrap() {
match entry {
Ok(path) => { 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(); 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(); 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; continue;
} else { } 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 { loop {
let x_raw = fs::read_to_string(path_x.as_str()).unwrap(); 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 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 x = 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 y = y_raw.trim_end_matches('\n').parse::<i32>().unwrap_or(0);
// Normalize vectors if x < -500000 {
let x: f32 = (x_clean as f32) / normalization_factor; if y > 500000 {
let y: f32 = (y_clean as f32) / normalization_factor; new_state = "180";
x_state = "normal";
matrix = ["-1", "0", "1", "0", "-1", "1", "0", "0", "1"];
}
else {
for (_i, orient) in orientations.iter().enumerate() { new_state = "90";
let d = (x - orient.vector.0).powf(2.0) + (y - orient.vector.1).powf(2.0); x_state = "left";
if d < threshold.parse::<f32>().unwrap_or(0.5) { matrix = ["0", "-1", "1", "1", "0", "0", "0", "0", "1"];
current_orient = &orient; }
break; } 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 { if new_state != old_state {
let keyboard_state = if new_state == "normal" { if mode == "sway" {
"enabled" Command::new("swaymsg")
} else {
"disabled"
};
match backend {
Backend::Sway => {
Command::new("swaymsg")
.arg("output") .arg("output")
.arg(display) .arg(display)
.arg("transform") .arg("transform")
.arg(new_state) .arg(new_state)
.spawn() .spawn()
.expect("Swaymsg rotate command failed to start") .expect("rotate command failed to start");
.wait()
.expect("Swaymsg rotate command wait failed"); old_state = new_state;
if disable_keyboard { }
for keyboard in &keyboards { if mode == "x" {
// println!("swaymsg input {} events {}", keyboard, keyboard_state);
Command::new("swaymsg") Command::new("xrandr")
.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")
.arg("-o") .arg("-o")
.arg(x_state) .arg(x_state)
.spawn() .spawn()
.expect("Xrandr rotate command failed to start") .expect("rotate command failed to start");
.wait()
.expect("Xrandr rotate command wait failed");
Command::new("xinput") Command::new("xinput")
.arg("set-prop") .arg("set-prop")
.arg(touchscreen) .arg(touchscreen)
.arg("Coordinate Transformation Matrix") .arg("Coordinate")
.arg("Transformation")
.arg("Matrix")
.args(&matrix) .args(&matrix)
.spawn() .spawn()
.expect("Xinput rotate command failed to start") .expect("rotate command failed to start");
.wait()
.expect("Xinput rotate command wait failed"); 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));
} }
} }