diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fd7bd4..c358056 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ -# 0.1.1 +# 0.1.2 +* Improved compatibility with docks and disabled monitors. + +## 0.1.1 * Fixed bug where zombie processes were not cleaned up. ## 0.1.0 diff --git a/Cargo.lock b/Cargo.lock index 49a22b6..3d04b15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,13 @@ # 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" @@ -24,10 +32,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "autoplank" -version = "0.1.1" +version = "0.1.2" dependencies = [ "bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.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.102 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -66,11 +75,21 @@ dependencies = [ "yaml-rust 0.3.5 (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.65" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memchr" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "1.0.6" @@ -87,6 +106,22 @@ dependencies = [ "proc-macro2 1.0.6 (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 = "serde" version = "1.0.102" @@ -128,6 +163,14 @@ 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)", +] + [[package]] name = "unicode-width" version = "0.1.6" @@ -168,6 +211,7 @@ version = "0.3.5" 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 autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" @@ -175,14 +219,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" +"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" "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 serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4b39bd9b0b087684013a792c59e3e07a46a01d2322518d8a1104641a0b1be0" "checksum serde_derive 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)" = "ca13fc1a832f793322228923fbb3aba9f3f44444898f835d31ad1b74fa0a2bf8" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "661641ea2aa15845cddeb97dad000d22070bb5c1fb456b96c1cba883ec691e92" "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 vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" diff --git a/Cargo.toml b/Cargo.toml index 998fe07..baa707b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "autoplank" -version = "0.1.1" +version = "0.1.2" authors = ["Edwin Svensson "] edition = "2018" @@ -10,3 +10,4 @@ edition = "2018" serde = { version = "1.0", features = ["derive"] } bincode = "1.2" clap = { version = "2", features = ["yaml"] } +regex = "1.3" diff --git a/src/monitor.rs b/src/monitor.rs index f08def8..60517f3 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -1,5 +1,6 @@ use std::process::Command; use super::mouse_location::MouseLocation; +use regex::Regex; #[derive(Debug)] pub struct Offset { @@ -7,11 +8,16 @@ pub struct Offset { pub y: u32 } +#[derive(Debug)] +pub struct Resolution { + pub width: u32, + pub height: u32 +} + #[derive(Debug)] pub struct Monitor { pub name: String, - pub width: u32, - pub height: u32, + pub resolution: Resolution, pub offset: Offset, pub primary: bool } @@ -21,6 +27,7 @@ impl Monitor { pub fn get_all() -> Vec { let mut monitors = Vec::::new(); + let re = Regex::new("[\\d]{1,9}[x][\\d]{1,9}[\\+][\\d]{1,9}[\\+][\\d]{1,9}").unwrap(); let output = Command::new("xrandr").output().unwrap(); let raw_str = String::from_utf8_lossy(&output.stdout); @@ -28,8 +35,11 @@ impl Monitor { let mut monitor_strings = Vec::::new(); for line in &all_lines { - if line.contains(" connected ") { + if line.contains(" connected ") && re.is_match(line) { monitor_strings.push(String::from(*line)); + } else if line.contains(" connected ") { + let parts: Vec<&str> = line.trim().split_whitespace().collect(); + println!("=> Ignoring {}", parts[0]); } } @@ -39,43 +49,59 @@ impl Monitor { let name = String::from(parts[0]); let primary = match parts[2] { "primary" => true, _ => false }; - let (width, height, offset) = Self::parse_res_offset(match primary { true => parts[3], false => parts[2] }); + let (resolution, offset) = Self::parse_res_offset(match primary { true => parts[3], false => parts[2] }); monitors.push(Monitor { name: name, - width: width, - height: height, + resolution: resolution, offset: offset, primary: primary }); } + for m in &monitors { + println!("=> Found {}", m); + } + monitors } - fn parse_res_offset(s: &str) -> (u32, u32, Offset) { + // Expects format like "1920x1080+1920+0" + fn parse_res_offset(s: &str) -> (Resolution, Offset) { let mut values = s.split(|c| c == 'x' || c == '+'); - let mut offset = Offset { x: 0, y: 0 }; - - let width = values.next().unwrap().parse().unwrap(); - let height = values.next().unwrap().parse().unwrap(); - offset.x = values.next().unwrap().parse().unwrap(); - offset.y = values.next().unwrap().parse().unwrap(); - - (width, height, offset) + ( + Resolution { + width: values.next().unwrap().parse().unwrap(), + height: values.next().unwrap().parse().unwrap() + }, + Offset { + x: values.next().unwrap().parse().unwrap(), + y: values.next().unwrap().parse().unwrap() + } + ) } pub fn mouse_here(&self, mouse_location: &MouseLocation) -> bool { - return mouse_location.x >= self.offset.x && mouse_location.x < self.offset.x + &self.width + return mouse_location.x >= self.offset.x && mouse_location.x < self.offset.x + &self.resolution.width } } +impl std::fmt::Display for Monitor { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + if self.primary { + write!(fmt, "{} primary {}x{}+{}+{}", self.name, self.resolution.width, self.resolution.height, self.offset.x, self.offset.y) + } else { + write!(fmt, "{} {}x{}+{}+{}", self.name, self.resolution.width, self.resolution.height, self.offset.x, self.offset.y) + } + } + +} diff --git a/src/threads/autoplank.rs b/src/threads/autoplank.rs index cad0f8e..621b243 100644 --- a/src/threads/autoplank.rs +++ b/src/threads/autoplank.rs @@ -11,6 +11,9 @@ pub fn autoplank(m: Arc>>) { loop { + // Sleep before locking the mutex to minimize the lock-time. + std::thread::sleep(std::time::Duration::from_millis(500)); + let ml = MouseLocation::get(); let monitors = &*m.lock().unwrap(); @@ -27,8 +30,6 @@ pub fn autoplank(m: Arc>>) { } - std::thread::sleep(std::time::Duration::from_millis(500)); - } }