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

General brightness improvements: new brightnessctl backend, set by percentage, and pick device #60

Merged
merged 10 commits into from
Dec 9, 2023

Conversation

Syndelis
Copy link
Contributor

Resolves #56
Resolves #37
Partially Resolves #27

About this PR

This PR separates the code that controls brightness into a new module. This module exports a trait that different backends can implement in order to control brightness like SwayOSD expects.

Besides the existing "blight" backend, a new backend has been introduced, one that is backed by the command line util brightnessctl (#56). This backend is attempted first and "blight" serves as a fallback if the command is not available. I've made it this way because "blight" requires some setting up, while brightnessctl requires only the command to be installed.

The brightness command also accepts brightness={val} to set the brightness directly in percentages (#37).

Finally, the PR also allows for the user to pick a different device to have its brightness set, via the already present --device parameter. I believe this partially resolves #27 because the user could now make a script to call SwayOSD for each device, which was previously impossible.

@Syndelis
Copy link
Contributor Author

Hi there, @ErikReider ! Can I get some feedback on this one? I'll be happy to make any adjustments!

Copy link
Owner

@ErikReider ErikReider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please excuse me for the late review 😅

Copy link
Owner

@ErikReider ErikReider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than that, it LGTM :)

Comment on lines 1 to 37
use self::{brightnessctl::BrightnessCtl, blight::Blight};

mod blight;

mod brightnessctl;

pub type BrightnessBackendResult = anyhow::Result<Box<dyn BrightnessBackend>>;

pub trait BrightnessBackendConstructor: BrightnessBackend + Sized + 'static {
fn try_new(device_name: Option<String>) -> anyhow::Result<Self>;

fn try_new_boxed(device_name: Option<String>) -> BrightnessBackendResult {
let backend = Self::try_new(device_name);
match backend {
Ok(backend) => Ok(Box::new(backend)),
Err(e) => Err(e),
}
}
}

pub trait BrightnessBackend {
fn get_current(&mut self) -> u32;
fn get_max(&mut self) -> u32;

fn lower(&mut self, by: u32) -> anyhow::Result<()>;
fn raise(&mut self, by: u32) -> anyhow::Result<()>;
fn set(&mut self, val: u32) -> anyhow::Result<()>;
}

pub fn get_preferred_backend(device_name: Option<String>) -> BrightnessBackendResult {
println!("Trying BrightnessCtl Backend...");
BrightnessCtl::try_new_boxed(device_name.clone()).or_else(|_| {
println!("...Command failed! Falling back to Blight");
Blight::try_new_boxed(device_name)
})
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to run cargo fmt --all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in ba06c46

@ErikReider ErikReider linked an issue Dec 8, 2023 that may be closed by this pull request
@Syndelis
Copy link
Contributor Author

Syndelis commented Dec 9, 2023

@ErikReider I've addressed the review comments!

@Syndelis Syndelis force-pushed the feat/brightnessctl-backend branch from ba06c46 to f7751ec Compare December 9, 2023 22:05
Copy link
Owner

@ErikReider ErikReider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the amazing work! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants