Skip to content

Commit

Permalink
fix: Use dbus fallback instead of returning early #95 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
avalsch authored Feb 29, 2024
1 parent 65eb123 commit 2762413
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/brightness/backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dbus::{self, blocking::Connection, Message};
use inotify::{Inotify, WatchMask};
use std::error::Error;
use std::fs;
use std::fs::{File, OpenOptions};
use std::fs::File;
use std::io::ErrorKind;
use std::path::Path;

Expand All @@ -26,16 +26,21 @@ pub struct Backlight {
impl Backlight {
pub fn new(path: &str, min_brightness: u64) -> Result<Self, Box<dyn Error>> {
let brightness_path = Path::new(path).join("brightness");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open(&brightness_path)?;
let current_brightness = read(&mut file)?;
let has_write_permission = write(&mut file, current_brightness).is_ok();

let dbus = if has_write_permission {
None

let current_brightness = fs::read(&brightness_path)?;

let has_write_permission = fs::write(&brightness_path, current_brightness).is_ok();

let (file, dbus) = if has_write_permission {
let file = File::options()
.read(true)
.write(true)
.open(&brightness_path)?;

(file, None)
} else {
let file = File::open(&brightness_path)?;

let id = Path::new(path)
.file_name()
.and_then(|x| x.to_str())
Expand All @@ -50,12 +55,14 @@ impl Backlight {
.ok()
.map(|m| m.append2("backlight", id));

Connection::new_system().ok().and_then(|connection| {
let connection = Connection::new_system().ok().and_then(|connection| {
message.map(|message| Dbus {
connection,
message,
})
})
});

(file, connection)
};

let max_brightness = fs::read_to_string(Path::new(path).join("max_brightness"))?
Expand Down

0 comments on commit 2762413

Please sign in to comment.