Skip to content

Commit

Permalink
Add clight brightness and gamma control
Browse files Browse the repository at this point in the history
  • Loading branch information
spikespaz committed Sep 1, 2022
1 parent b44f6af commit a72d561
Show file tree
Hide file tree
Showing 2 changed files with 390 additions and 3 deletions.
389 changes: 386 additions & 3 deletions system/powersave.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,396 @@
criticalPowerAction = "Hibernate";
};

# add a shell command for all users to show the current
# status of the battery
# location service for clight
location.provider = "geoclue2";
# services.geoclue2.enable = true;

# keyboard and monitor backlight
services.clight = {
enable = true;

## Gamma temperature during day and night
## this nix option overrides gamma.temp
temperature.day = 5500;
temperature.night = 3700;

settings = {
####################
# Clight conf file #
####################

## Verbose mode, useful to report bugs:
## run clight in verbose mode,
## then open issue on github attaching log
verbose = false;

## Delay in seconds before clight restarts working
## after system is resumed from suspend/hibernation.
## This may be needed because on some laptops on resume
## screen temp is not correctly applied, with warning in logs: "Failed to set gamma temperature."
## Clight is in fact too quick to act on resume, and it is resuming before X is fully resumed;
## thus failing to apply screen temperature.
## By default, it is disabled (0 seconds). Max value: 30seconds.
## Note: it requires systemd-logind (org.freedesktop.login1 dbus interface)
resumedelay = 0;

###################
# INHIBITION TOOL #
########################################################
# Requires DIMMER or DPMS enabled. Disabled otherwise. #
########################################################
inhibit = {
## Uncomment to disable
disabled = false;

## Uncomment to let Clight manage "Docked" laptop state
## as an inhibition (pausing DPMS and dimmer).
## Note that "Docked" state is only checked when
## laptop lid is closed or opened.
inhibit_docked = true;

## Uncomment to let Clight manage inhibition requests
## suppressing org.freedesktop.PowerManagement too
## (ie: preventing your laptop to suspend/hibernate)
inhibit_pm = true;

## Uncomment to let Clight pause backlight module
## while any inhibition is active;
## this is useful eg: to pause backlight calibration
## while watching a movie.
inhibit_bl = true;
};

##############################
# BACKLIGHT CALIBRATION TOOL #
##############################
backlight = {
## Uncomment to disable
disabled = true;

## Uncomment to restore screen backlight on exit
restore_on_exit = true;

## Uncomment to disable smooth transitions
no_smooth_transition = false;

## Transition step in percentage
trans_step = 0.05;

## Transition timeout in ms
trans_timeout = 30;

## When > 0, use a fixed transition duration (in ms),
## overriding trans_timeout and trans_step configs.
trans_fixed = 0;

## Timeouts between captures during day/night/event on AC
## Set any of these to <= 0 to disable captures
## in the corresponding day time.
ac_timeouts = [ 600 2700 300 ];

## Timeouts between captures during day/night/event on BATT
## Set any of these to <= 0 to disable captures
## in the corresponding day time.
batt_timeouts = [ 1200 5400 600 ];

## Set a threshold: if detected ambient brightness is below this threshold,
## capture will be discarded and no backlight change will be made.
## Very useful to discard captures with covered webcam.
shutter_threshold = 0.10;

## Disables automatic calibration for screen backlight.
## Then, it can only be manually triggered by bus api.
# no_auto_calibration = true;

## Uncomment to let BACKLIGHT module pause automatic calibration when laptop lid is closed.
## Mostly useful when laptop gets docked and thus internal webcam
## would not be able to correctly capture ambient brightness.
pause_on_lid_closed = true;

## Uncomment to let BACKLIGHT module fire an automatic calibration when laptop lid gets opened.
capture_on_lid_opened = true;
};

###################
# SENSOR settings #
###################
sensor = {
## Curves used to match ambient brightness to backlight level for each AC state.
## X axis: ambient brightness values (from 0 to 10)
## Y axis: desired backlight level for corresponding ambient brightness.
## Note: the array can be expanded up to 50 points for finer granularity.
ac_regression_points = [ 0.0 0.15 0.29 0.45 0.61 0.74 0.81 0.88 0.93 0.97 1.0 ];
batt_regression_points = [ 0.0 0.15 0.23 0.36 0.52 0.59 0.65 0.71 0.75 0.78 0.80 ];

## Sensor device to be used (Webcam or ALS device, eg: video0 or iio:device0).
## Leave this empty to let clight use first device it finds between supported ones,
## ie: webcams, ambient light sensors, or custom devices.
## Refer to Clightd wiki for more info: https://github.com/FedeDP/Clightd/wiki/Sensors
devname = "";

## Sensor settings to be used. Leave empty/commented to use default values.
## This can be really useful to further customize your sensor behaviour (together with backlight_regression_points).
## Have a look at Clightd wiki for more info: https://github.com/FedeDP/Clightd/wiki/Sensors#settings-string.
## Example for Camera sensor, to set a manual exposure -> "10094849=1,10094850=166";
settings = "";

## Number of frames or ALS device pollings to be captured on AC/on BATT.
## Must be between 1 and 20.
captures = [ 5 5 ];
};

## Curves used to match reference backlight level (computed through sensor.regression_points curves),
## to backlight level for each AC state for specific monitors.
## This allows per-monitor backlight adjustment; it means eg:
## that a 0.7 backlight level for your internal laptop monitor,
## can be mapped to 0.8 backlight level for your external monitor.
## It is mostly useful for people with internal laptop + external monitors or multiple external monitors,
## as often their backlight level will not properly match (ie: a 50% level on internal laptop can be much brighter than external monitor).
## All monitors not specified below will use default reference values (specified by sensor.regression_points curves).
## X axis: default backlight level (from 0 to 10)
## Y axis: desired backlight level adjusted for specific monitor.
## Note: arrays can be expanded up to 50 points for finer granularity.
## Customize "intel_backlight" or "acpi_video0" or whatever using your monitors IDs.
## To find monitor IDs, use Clightd:
## busctl call org.clightd.clightd /org/clightd/clightd/Backlight org.clightd.clightd.Backlight GetAll "s" ""
##
## You might ask why using a backlight-to-backlight mapping for specific monitors, instead of using
## multiple ambient_brightness-to-backlight mapping curves; fact is that this way Clight is also able to
## set correct backlight for each monitor even when just asked to set the backlight, ie:
## when the request comes from eg: dbus API asking for a certain backlight level (IncBl, DecBl dbus methods).
## In this case, we wouldn't have an "ambient brightess" to be used to compute correct backlight level on each monitor;
## instead, we just set the requested backlight as default, and adjust to it for each monitor specified here below.
monitor_override = [
# {
# monitor_id = "intel_backlight";
# ac_regression_points = [ 0.0 0.18 0.22 0.33 0.55 0.64 0.71 0.80 0.90 0.97 1.0 ];
# batt_regression_points = [ 0.0 0.15 0.29 0.45 0.61 0.74 0.81 0.88 0.93 0.97 1.0 ];
# }
# {
# monitor_id = "acpi_video0";
# ac_regression_points = [ 0.0 0.18 0.22 0.33 0.55 0.64 0.71 0.80 0.90 0.97 1.0 ];
# batt_regression_points = [ 0.0 0.15 0.29 0.45 0.61 0.74 0.81 0.88 0.93 0.97 1.0 ];
# }
];

##############################
# KEYBOARD BACKLIGHT TOOL #
##############################
keyboard = {
## Uncomment to disable keyboard automatic calibration.
## It is automatically disabled anyway where not available.
disabled = false;

## Timeouts on AC/on BATT for keyboard auto dimming.
## Set any of these to <= 0 to disable kbd backlight
## in the corresponding AC state.
timeouts = [ 15 7 ];

## Curves used to match screen backlight to keyboard backlight level for each AC state.
## X axis: default screen backlight level (from 0 to 10)
## Y axis: desired keyboard backlight level for corresponding screen backlight.
## Note: the array can be expanded up to 50 points for finer granularity.
## Note also that most keyboard offers only 3 backlight levels (off, mid, high).
## Default curves are same as default backlight curves but upside down
## (ie: the lower the screen backlight, the higher the keyboard backlight).
ac_regression_points = [ 1.0 0.97 0.93 0.88 0.81 0.74 0.61 0.45 0.29 0.15 0.0 ];
batt_regression_points = [ 0.80 0.78 0.75 0.71 0.65 0.59 0.52 0.36 0.23 0.15 0.0 ];
};

##############
# GAMMA TOOL #
##############
gamma = {
## Uncomment to disable gamma tool
disabled = true;

## Uncomment to restore screen temperature on exit
restore_on_exit = true;

## Uncomment to disable gamma smooth transitions
no_smooth_transition = false;

## Gamma transition step
trans_step = 50;

## Gamma transition timeout in ms
trans_timeout = 300;

## Enable to let GAMMA smooth transitions last (2 * event_duration),
## in a redshift-like way.
## When enabling this, transition steps and timeouts are automatically computed
## given DAY-NIGHT temperature difference and (2 * event_duration) duration.
##
## Note that if clight is started outside of an event, correct gamma temperature
## will be immediately setted using normal parameters:
## no_smooth_gamma_transition, gamma_trans_step, gamma_trans_timeout
long_transition = true;

## Let screen temperature match monitor backlight, using following algorithm:
## ```pseudocode
## diff = abs(temp[DAY] - temp[NIGHT])
## min_temp = min(temp[NIGHT], temp[DAY])
## new_temp = (diff * screen_bl) + min_temp;
## ```
## Ie: the higher the screen backlight, the colder the temp.
##
## When enabled, screen temperature won't be changed time-based.
## Note also that LOCATION is still needed to let BACKLIGHT module know current time of day.
## Finally, it requires BACKLIGHT module to be enabled, otherwise it gets disabled.
ambient_gamma = false;
};

################
# DAYTIME TOOL #
################
########################################################
# Retrieves Sunrise and Sunset times for user location #
########################################################
daytime = {
## Desired latitude for gamma support (sunrise/sunset in this location)
# latitude = 40.9;

## Desired longitude for gamma support (sunrise/sunset in this location)
# longitude = 7.16;

## Force set a sunrise time
sunrise = "6:30";

## Force set a sunset time
sunset = "20:30";

## Duration of an "event". Clight will enter "event" mode (more frequent screen recalibrations)
## from event_duration seconds before a sunrise/sunset, until event_duration seconds after.
event_duration = 1800;

## Set an offset in seconds from sunrise;
## eg: a value of 600 would force Clight to manage sunrise event
## 10 minutes after the real event.
## You can use negative values too.
sunrise_offset = 0;

## Set an offset in seconds from sunset;
## eg: a value of 600 would force Clight to manage sunset event
## 10 minutes after the real event.
## You can use negative values too.
sunset_offset = 0;
};

###############
# DIMMER TOOL #
###############
###########################################
# Requires BACKLIGHT. Disabled otherwise. #
###########################################
dimmer = rec {
## Uncomment to disable dimmer tool
disabled = false;

## Uncomment to disable smooth transitions
## when entering/leaving dimmed state
no_smooth_transition = [ false false ];

## Transition step in percentage.
## Entering dimmed state fade, and leaving fade
trans_steps = [ 0.01 0.08 ];

## Transition timeout in ms.
## Entering dimmed state fade, and leaving fade
trans_timeouts =
let
# calculates a duration for each step between
# full brightness and the dimmed percentage
formula = duration: target: step: builtins.floor (duration / ((1 - target) / step));
in
[
(formula 2000 dimmed_pct (builtins.elemAt trans_steps 0))
(formula 250 dimmed_pct (builtins.elemAt trans_steps 1))
];

## When > 0, use a fixed transition duration (in ms),
## for enter or leave, overriding trans_timeouts and trans_steps configs.
trans_fixed = [ 0 0 ];

## Timeouts on AC/on BATT.
## Set any of these to <= 0 to disable dimmer
## in the corresponding AC state.
timeouts = [ 30 15 ];

## Change dimmed backlight level, in percentage
dimmed_pct = 0.2;
};

#############
# DPMS TOOL #
#############
dpms = {
## Uncomment to disable dpms management
disabled = false;

## Timeouts on AC/on BATT.
## Set any of these to <= 0 to disable dpms
## in the corresponding AC state.
timeouts = [ 900 300 ];
};

############################
# SCREEN COMPENSATION TOOL #
############################
###########################################
# Requires BACKLIGHT. Disabled otherwise. #
###########################################
screen = {
##############################################################################################################
## Use this feature to enable screen-content based backlight adjustments to Clight. #
## This means that monitord backlight won't be changed to match ambient brightness alone, #
## but screen content brightness too, using following algorithm: #
## ```pseudocode #
## window_min = ambient_br - contrib; #
## window_max = ambient_br + contrib; #
## bl_new = window_max - (0.4 * screen_br) #
## ``` #
## Ie: ambient brightness is used as a sliding window 20% wide, #
## and screen content brightness is used to choose the correct point inside the window. #
## Rule is: higher the screen content brightness, nearest to window max; and viceversa. #
## Idea is fairly simple: given current ambient brightness, try to light up dark screen content, and dim #
## bright screen content. #
## #
## This can impact battery life, that is the reason why SCREEN module is disabled on battery by default; #
## in this case, you might want to keep SCREEN module disabled and manually calibrating screen backlight #
## through Clight desktop file "Capture" quick action or its DBus command. #
##############################################################################################################

## Uncomment to disable screen-emitted brightness compensation support
disabled = true;

## How much does your screen brightness affect
## ambient brightness, in percentage.
## As above algorithm outlines, it is used to build the ambient brightness window.
## Set it to 0.0 to disable screen-content based backlight compensation.
contrib = 0.2;

## Screen timeouts on AC/on BATT.
## Set any of these to <= 0 to disable screen-content based backlight compensation
## in the corresponding AC state.
## Disabled by default on BATT because it is quite an heavy operation,
## as it has to take a snapshot of your X desktop and compute its brightness.
timeouts = [ 5 0 ];
};
};
};

environment.shellAliases = {
showbattery = ''
# command for all users to get the current status of the battery
show-battery = ''
upower -i /org/freedesktop/UPower/devices/battery_BAT0 \
| grep -E 'state|charge-cycles|time\ to|percentage' \
| sed 's/^ *//g'
'';
# command for all users to get the path used by their clight daemon
clight-config = ''
printf $(systemctl --user cat clight.service | grep ExecStart | sed -E 's/.+--conf-file //')
'';
};

}
Loading

0 comments on commit a72d561

Please sign in to comment.