Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use dbus fallback instead of returning early #95 #96

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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