Initial commit

This commit is contained in:
Andre Henriques 2024-09-08 09:42:31 +01:00
commit 6fd6a4471c
5 changed files with 1175 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1000
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "monitor-controller"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = "4.5.17"
rouille = "3.6.2"

View File

@ -0,0 +1,11 @@
[Unit]
Description=Run the monitor-controller serivce
[Service]
Type=simple
StandardOutput=journal
# CHANGE THIS BEFORE COPYING TO THE SYSTEMD FOLDER
ExecStart=$HOME/.local/bin/monitor-controller -d -c $HOME/.config/hypr/mon.conf -p $HOME/.config/hypr
[Install]
WantedBy=default.target

155
src/main.rs Normal file
View File

@ -0,0 +1,155 @@
extern crate clap;
extern crate rouille;
use std::{
fs,
path::Path,
sync::{Arc, Mutex},
};
use clap::{Arg, ArgAction, Command};
use rouille::Response;
fn get_config(path: &Path) -> Vec<String> {
let c = fs::read_to_string(path)
.expect("Config file does not exist")
.split("\n")
.into_iter()
.map(|d| d.trim())
.filter(|d| d.len() > 0)
.map(|d| d.to_string())
.collect::<Vec<String>>();
if c.len() == 0 {
panic!("Config seams to be empty")
}
return c;
}
fn main() {
println!("Hello, world!");
let matches = Command::new("Monitor Controller")
.about("Monitor Contorller server/cli")
.arg(
Arg::new("daemon")
.short('d')
.help("Runs the app in daemon mode!")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("config")
.short('c')
.long("config")
.help("Gives the path for the config location of the list of configs"),
)
.arg(
Arg::new("base_path")
.short('p')
.help("Gives the base path for the configs"),
)
.arg(
Arg::new("change_on_boot")
.short('b')
.help("Change to the first option when the daemon starts")
.action(ArgAction::SetTrue),
)
.get_matches();
let daemon_mode: bool = matches
.get_one::<bool>("daemon")
.unwrap()
.clone()
.to_owned();
if daemon_mode {
println!("Paying attention on 11111");
let base_path = matches.get_one::<String>("base_path");
if base_path.is_none() {
println!("Base path is required when running daemon mode");
return;
}
let base_path = base_path.unwrap().clone().to_owned();
let config_path = matches.get_one::<String>("config");
if config_path.is_none() {
println!("Config path is required when running daemon mode");
return;
}
let config_path = Path::new(&config_path.unwrap())
.canonicalize()
.ok()
.unwrap();
let config = Arc::new(Mutex::new(get_config(&config_path)));
let change_on_boot: bool = matches
.get_one::<bool>("daemon")
.unwrap()
.clone()
.to_owned();
if change_on_boot {
let c = config.clone();
let c = c.lock().ok().unwrap();
let u = c[0].clone().to_owned();
std::process::Command::new("ln")
.arg("-sf")
.arg(base_path.clone().to_owned() + "/" + &u.to_string() + ".conf")
.arg(base_path.clone().to_owned() + "/monitors.conf")
.spawn()
.expect("Failed to change monitor on boot");
}
rouille::start_server("0.0.0.0:11111", move |request| {
if request.url().eq("/reload") {
println!("Reloading config!");
let c = config.clone();
let mut c = c.lock().ok().unwrap();
*c = get_config(&config_path);
return Response::text("reloaded!");
}
if !request.url().starts_with("/switch_to/") {
return Response::text("Not reallllly sure what you are trying to do :3");
}
let u = request.url().replace("/switch_to/", "");
let c = config.clone();
let c = c.lock().ok().unwrap();
let found_it = c.iter().any(|d| d.eq(&u));
if !found_it {
println!("Could not find the requested url");
return Response::text("Could not find the requested config!")
.with_status_code(404);
}
let res = std::process::Command::new("ln")
.arg("-sf")
.arg(base_path.clone().to_owned() + "/" + &u.to_string() + ".conf")
.arg(base_path.clone().to_owned() + "/monitors.conf")
.spawn();
if res.is_err() {
println!("Well somwthing went very wrong");
return Response::text("Something went very wrong").with_status_code(500);
}
return Response::text("Chaged it");
})
} else {
println!("Well well well I have not done this!")
}
}