Skip to content

Commit

Permalink
Merge pull request #3 from olback/issue-2
Browse files Browse the repository at this point in the history
fix #2
  • Loading branch information
olback authored Dec 13, 2019
2 parents ee2ebd6 + 0199a92 commit 7c359d7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
51 changes: 50 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autoplank"
version = "0.1.1"
version = "0.1.2"
authors = ["Edwin Svensson <git@olback.net>"]
edition = "2018"

Expand All @@ -10,3 +10,4 @@ edition = "2018"
serde = { version = "1.0", features = ["derive"] }
bincode = "1.2"
clap = { version = "2", features = ["yaml"] }
regex = "1.3"
58 changes: 42 additions & 16 deletions src/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use std::process::Command;
use super::mouse_location::MouseLocation;
use regex::Regex;

#[derive(Debug)]
pub struct Offset {
pub x: u32,
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
}
Expand All @@ -21,15 +27,19 @@ impl Monitor {
pub fn get_all() -> Vec<Monitor> {

let mut monitors = Vec::<Monitor>::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);
let all_lines: Vec<&str> = raw_str.trim().split("\n").collect();
let mut monitor_strings = Vec::<String>::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]);
}
}

Expand All @@ -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)
}
}

}
5 changes: 3 additions & 2 deletions src/threads/autoplank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub fn autoplank(m: Arc<Mutex<Vec<Monitor>>>) {

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();

Expand All @@ -27,8 +30,6 @@ pub fn autoplank(m: Arc<Mutex<Vec<Monitor>>>) {

}

std::thread::sleep(std::time::Duration::from_millis(500));

}

}

0 comments on commit 7c359d7

Please sign in to comment.