Skip to main content

Crate blight

Crate blight 

Source
Expand description

§About

blight is primarily a CLI backlight utility for Linux which is focused on providing hassle-free backlight control. However, the parts which blight relies on to make backlight changes, are also exposed through the library aspect of this crate, which can be used like any other Rust library by using the command cargo add blight --no-default-features in your Rust project. The CLI utility, on the other hand, can be installed by running cargo install blight. This documentation only covers the library aspect, for CLI related docs, visit the project’s Github repo.

The latest version of the libary now supports controlling LEDs using the /sys/class/leds interface.

Three features of blight that standout:

  1. Prioritizing backlight device detection in this order: iGPU>dGPU>ACPI>Fallback device.
  2. Smooth dimming by writing in increments/decrements of 1 with a few milliseconds of delay (sweep write).
  3. The library has zero external dependencies.

IMPORTANT: You need write permission for the file /sys/class/backlight/{your_device}/brightness to change brightness. The CLI utility comes with a helper script that let’s you gain access to the brightness file (which may not always work), which you can run by using the command sudo blight setup. If you’re only using blight as a dependency, you can read about gaining file permissions here.

For LED specific documentation and usage, see led module.

§Usage

use blight::{Change, Device, Direction, Delay, Light};

fn main() -> blight::Result<()> {
    // Using the helper functions
    blight::change_bl(5, Change::Regular, Direction::Inc, None)?; // Increases brightness by 5%
    blight::set_bl(50, Some("nvidia_0".into()))?; // Sets brightness value (not percentage) to 50

    // Doing it manually
    let mut dev = Device::new(None)?;
    let new = dev.calculate_change(5, Direction::Dec); // safely calculate value to write
    dev.write_value(new)?; // decreases brightness by 5%
    dev.reload(); // reloads current brightness value from the brightness file (optional)
    let new = dev.calculate_change(5, Direction::Inc);
    dev.sweep_write(new, Delay::default()); // smoothly increases brightness by 5%
    Ok(())
}

Re-exports§

pub use err::Error;
pub use err::ErrorKind;
pub use err::Result;

Modules§

err
All blight library related errors in one place. See Error and ErrorKind
led
Abstractions and functions for controlling LEDs using the /sys/class/leds Linux interface

Structs§

Delay
A wrapper type for std::time::Duration used for specifying delay between each iteration of the loop in Device::sweep_write.
Device
An abstraction of a backlight device containing a name, current and max backlight values, and some related functionality.

Enums§

Change
This enum is used to specify the kind of backlight change to carry out while calling the change_bl function. \
Direction
This enum is used to specify the direction in which the backlight should be changed in the change_bl and Device::calculate_change functions. Inc -> Increase, Dec -> Decrease.

Constants§

BLDIR
Linux backlight directory location. All backlight hardware devices appear here.

Traits§

Dimmable
Marker trait to signify that a backlight device or an LED is dimmable
Light
The interface for controlling both backlight and LED devices
Toggleable
Marker trait to signify that an LED can only be toggled on/off

Functions§

change_bl
A helper function to change backlight based on step-size (percentage), Change type and Direction.
set_bl
A helper function which takes a brightness value and writes the value to the brightness file as long as the given value falls under the min and max bounds of the detected backlight device and is different from the current value.