Skip to content

Commit

Permalink
fixed on/off and minor fixes in code style
Browse files Browse the repository at this point in the history
  • Loading branch information
PTFOPlayer committed Apr 21, 2023
1 parent 7f484b5 commit f0ea221
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 113 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
/screenpad_rs_binaries
/screenpad_rs_binaries
dev-build.sh
build
24 changes: 17 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
cargo build --release
sudo cp target/release/screenpad_rs /usr/bin
sudo cp ./src/brightness.sh /usr/bin/brightness.sh
sudo cp ./src/current.sh /usr/bin/current.sh
sudo cp ./screenpad_rs.service /etc/systemd/system/
sudo mkdir /var/screenpad_rs/
sudo echo 100 > /var/screenpad_rs/brightness
#cargo build --release
#sudo cp target/release/screenpad_rs /usr/bin
#sudo cp ./src/brightness.sh /usr/bin/brightness.sh
#sudo cp ./src/current.sh /usr/bin/current.sh
#sudo cp ./screenpad_rs.service /etc/systemd/system/
#sudo mkdir /var/screenpad_rs/
#sudo echo 100 > /var/screenpad_rs/brightness

mkdir build
cd build
tar -xvf ../screenpad_rs_binaries.tar.gz
cp screenpad_rs_binaries/* .

sudo cp screenpad_rs /usr/bin
sudo cp brightness.sh /usr/bin/brightness.sh
sudo cp current.sh /usr/bin/current.sh
sudo cp screenpad_rs.service /etc/systemd/system/
Binary file removed screenpad_rs_binaries.tar
Binary file not shown.
Binary file added screenpad_rs_binaries.tar.gz
Binary file not shown.
251 changes: 146 additions & 105 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use std::{
};

lazy_static! {
static ref ARGS:Vec<String> = {
static ref ARGS: Vec<String> = {
let mut args: Vec<String> = env::args().collect();
args.append(&mut vec!["".to_owned()]);
args
};
static ref ARG_1:String = ARGS[1].clone();
static ref ARG_2:String = ARGS[2].clone();
static ref ARG_1: String = ARGS[1].clone();
static ref ARG_2: String = ARGS[2].clone();
}

fn main() {
Expand All @@ -24,109 +24,15 @@ fn main() {

fn arg_parser() {
match ARG_1.as_str() {
"--brightness" | "-b" => {
let brightness = ARG_2.as_str();
let b_int: i32 = match brightness.parse() {
Ok(res) => res,
Err(_) => {
println!("wrong value provided, setting value to 100");
100
}
};
if b_int > 0 && b_int <= 255 {
execute(&format!("{}", b_int));
} else {
println!("wrong value, correct values are in range of 1..255")
}
}
"--off" | "-f" => {
execute("0");
}
"--on" | "-n" => {
execute("100");
}
"--sync" | "-s" => {
let mut set = Command::new("cat");
let out = set.arg("/sys/class/backlight/intel_backlight/brightness");
match out.output() {
Ok(out) => {
let val_str =
String::from_utf8(out.stdout[..(out.stdout.len() - 1)].to_vec()).unwrap();
let val = val_str
.parse::<i32>()
.expect("error ocured while getting main screen brightness");
let val = val / 98;
execute(&format!("{}", val));
}
Err(err) => {
println!("error: {}", err)
}
}
}
"--brightness" | "-b" => brightness(),
"--off" | "-f" => off(),
"--on" | "-n" => on(),
"--sync" | "-s" => sync(),
"--current" | "-c" => println!("current screenpad brightness: {}", current()),
"--up" | "-u" => match current().parse::<i32>() {
Ok(res) => {
if res + 10 < 255 {
execute(&format!("{}", res + 10))
} else {
println!("cannot increase more")
}
}
Err(_) => println!("error occured increasing brightness"),
},
"--down" | "-d" => match current().parse::<i32>() {
Ok(res) => {
if res - 10 > 0 {
execute(&format!("{}", res - 10))
} else {
println!("cannot decrease more")
}
}
Err(_) => println!("error occured decreasing brightness"),
},
"--watch" | "-w" => {
let mut prev = 0;
loop {
let mut set = Command::new("cat");
let out = set.arg("/sys/class/backlight/intel_backlight/brightness");
match out.output() {
Ok(out) => {
let val_str =
String::from_utf8(out.stdout[..(out.stdout.len() - 1)].to_vec())
.unwrap();
let mut val = val_str
.parse::<i32>()
.expect("error ocured while getting main screen brightness");
val = val / 98;
if val == 0 {
val += 1;
}
if val != prev {
prev = val;
execute(&format!("{}", val));
}
}
Err(err) => {
println!("error: {}", err)
}
}
sleep(Duration::from_secs_f32(0.2))
}
}
"--help" | "-h" => {
println!(
"
--brightness | -b : changes brightness in values from 1 to 255,
--sync | -s : synchronizes brightness of screenpad with main screen
--current | -c : gives current brightness of screenpad
--off | -f : turns screenpad off,
--on | -n : turns screenpad on,
--up | -u : increases brightness by 10
--down | -d : decreases brightness by 10
--watch | -w : automatic sync
"
);
}
"--up" | "-u" => up(),
"--down" | "-d" => down(),
"--watch" | "-w" => watch(),
"--help" | "-h" => help(),
&_ => {
println!("wrong argument, see --help");
}
Expand Down Expand Up @@ -155,3 +61,138 @@ fn current() -> String {
}
}
}

fn brightness() {
let brightness = ARG_2.as_str();
let b_int: i32 = match brightness.parse() {
Ok(res) => res,
Err(_) => {
println!("wrong value provided, setting value to 100");
100
}
};
if b_int > 0 && b_int <= 255 {
execute(&format!("{}", b_int));
} else {
println!("wrong value, correct values are in range of 1..255")
}
}

fn off() {
let mut ctl = Command::new("sudo");
let out = ctl
.args(["systemctl", "stop", "screenpad_rs.service"])
.output();
match out {
Ok(_) => {
println!("screenpad off (brightness set to 0, turning off actually ony works on KDE)");
execute("0");
}
Err(_) => {
println!("couldn't stop process `screenpad_rs.service`")
}
}
}

fn on() {
let mut ctl = Command::new("sudo");
let out = ctl
.args(["systemctl", "start", "screenpad_rs.service"])
.output();
match out {
Ok(_) => {
println!("screenpad on ");
}
Err(_) => {
println!("couldn't stop process `screenpad_rs.service`")
}
}
}

fn sync() {
let mut set = Command::new("cat");
let out = set.arg("/sys/class/backlight/intel_backlight/brightness");
match out.output() {
Ok(out) => {
let val_str = String::from_utf8(out.stdout[..(out.stdout.len() - 1)].to_vec()).unwrap();
let val = val_str
.parse::<i32>()
.expect("error ocured while getting main screen brightness");
let val = val / 98;
execute(&format!("{}", val));
}
Err(err) => {
println!("error: {}", err)
}
}
}

fn watch() {
let mut prev = 0;
loop {
let mut set = Command::new("cat");
let out = set.arg("/sys/class/backlight/intel_backlight/brightness");
match out.output() {
Ok(out) => {
let val_str =
String::from_utf8(out.stdout[..(out.stdout.len() - 1)].to_vec()).unwrap();
let mut val = val_str
.parse::<i32>()
.expect("error ocured while getting main screen brightness");
val = val / 98;
if val == 0 {
val += 1;
}
if val != prev {
prev = val;
execute(&format!("{}", val));
}
}
Err(err) => {
println!("error: {}", err)
}
}
sleep(Duration::from_secs_f32(0.2))
}
}

fn up() {
match current().parse::<i32>() {
Ok(res) => {
if res + 10 < 255 {
execute(&format!("{}", res + 10))
} else {
println!("cannot increase more")
}
}
Err(_) => println!("error occured increasing brightness"),
}
}

fn down() {
match current().parse::<i32>() {
Ok(res) => {
if res - 10 > 0 {
execute(&format!("{}", res - 10))
} else {
println!("cannot decrease more")
}
}
Err(_) => println!("error occured decreasing brightness"),
}
}

fn help() {
println!(
"
--brightness | -b : changes brightness in values from 1 to 255,
--sync | -s : synchronizes brightness of screenpad with main screen
--current | -c : gives current brightness of screenpad
--off | -f : turns screenpad off,
--on | -n : turns screenpad on,
--up | -u : increases brightness by 10
--down | -d : decreases brightness by 10
--watch | -w : automatic sync
"
);
}

0 comments on commit f0ea221

Please sign in to comment.