Skip to content

Commit

Permalink
added -r to rescan
Browse files Browse the repository at this point in the history
  • Loading branch information
olback committed Nov 21, 2019
1 parent 120c4d1 commit 49773d6
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 25 deletions.
186 changes: 186 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0", features = ["derive"] }
bincode = "1.2"
clap = { version = "2", features = ["yaml"] }
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ Description=Move plank to the monitor your mouse is at.
Exec=.local/bin/autoplank
```

When adding/removing a monitor make sure to rescan:
```terminal
autoplank -r
```

Inspired by [abiosoft/autoplank](https://github.com/abiosoft/autoplank).
11 changes: 11 additions & 0 deletions cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: "Autoplank"
# version: "0.1.0"
author: "olback <autoplank@olback.net>"
about: "Autoplank. Automatically move plank across monitors!"
args:
- rescan:
help: "Rescan monitors. This is needed when you add/remove a monitor."
short: r
long: rescan
takes_value: false
multiple: false
7 changes: 6 additions & 1 deletion src/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::process::Command;

const DEPENDENCIES: &'static [&'static str] = &["plank", "xrandr", "xdotool", "dconf"];
const DEPENDENCIES: &'static [&'static str] = &[
"plank",
"xrandr",
"xdotool",
"dconf"
];

pub fn check() -> (bool, Vec<&'static str>) {

Expand Down
81 changes: 59 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,57 @@ mod mouse_location;
mod monitor;
mod plank;
mod dependencies;
use mouse_location::MouseLocation;
mod threads;

use monitor::Monitor;
use plank::Plank;
use threads::{
SOCKET_ADDR,
SocketAction,
SocketMessage
};
use std::{
net::Shutdown,
os::unix::net::UnixStream,
io::Write,
sync::{
Arc,
Mutex
}
};
use clap::{self, load_yaml, crate_version};
use bincode;

fn main() {

let yml = load_yaml!("../cli.yml");
let matches = clap::App::from(yml).version(crate_version!()).get_matches();

if matches.is_present("rescan") {

let mut socket = match UnixStream::connect(SOCKET_ADDR) {
Ok(s) => s,
Err(e) => {
eprintln!("{}", e);
return;
}
};
let msg = SocketMessage {
action: SocketAction::RefreshMonitors
};
let data = bincode::serialize::<SocketMessage>(&msg).unwrap();
socket.write(&data).unwrap();
socket.shutdown(Shutdown::Both).unwrap();

println!("Rescanning started");

std::process::exit(0);

}

println!("Autoplank");

// Make sure all dependencies are installed
let deps = dependencies::check();

if !deps.0 {
eprintln!("Missing dependencies:");
for dep in deps.1 {
Expand All @@ -20,28 +61,24 @@ fn main() {
std::process::exit(1);
}

let monitors = Monitor::get_all();
let mut p = Plank::new();

loop {

let ml = MouseLocation::get();

for monitor in &monitors {

if monitor.mouse_here(&ml) {
// Keep track of monitors
let monitors = Arc::new(Mutex::new(Monitor::get_all()));
let mut thread_handlers = Vec::<std::thread::JoinHandle<()>>::new();

if p.set_monitor(&monitor.name) {
// Plank needs to be restarted if the change needs to happen immediately.
p.restart();
}

}

}
// Autoplank thread, fetches mouse location every 500ms
let autoplank_monitors = Arc::clone(&monitors);
thread_handlers.push(std::thread::spawn(|| {
threads::autoplank(autoplank_monitors);
}));

std::thread::sleep(std::time::Duration::from_millis(500));
// Socket thread
let socket_monitors = Arc::clone(&monitors);
thread_handlers.push(std::thread::spawn(|| {
threads::socket(socket_monitors);
}));

for th in thread_handlers {
th.join().unwrap();
}

}
2 changes: 1 addition & 1 deletion src/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::process::Command;
use super::MouseLocation;
use super::mouse_location::MouseLocation;

#[derive(Debug)]
pub struct Offset {
Expand Down
2 changes: 1 addition & 1 deletion src/plank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Plank {

if current.to_string().trim() != new {

println!("Switching to {}", new);
println!("=> Switching to {}", new);

let output = Command::new("dconf").args(&["write", DCONF_KEY, &new]).output().unwrap();
let status = output.status.success();
Expand Down
34 changes: 34 additions & 0 deletions src/threads/autoplank.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::sync::{Arc, Mutex};
use super::super::{
plank::Plank,
mouse_location::MouseLocation,
monitor::Monitor
};

pub fn autoplank(m: Arc<Mutex<Vec<Monitor>>>) {

let mut p = Plank::new();

loop {

let ml = MouseLocation::get();
let monitors = &*m.lock().unwrap();

for monitor in monitors {

if monitor.mouse_here(&ml) {

if p.set_monitor(&monitor.name) {
// Plank needs to be restarted if the change needs to happen immediately.
p.restart();
}

}

}

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

}

}
Loading

0 comments on commit 49773d6

Please sign in to comment.