Repository navigation
omarchy-brightness-auto: ambient light brightness for the screen, off by default #144
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
709b67a
fec1c54
0ddbc0e
113ecbf
667764e
1dabf33
cc8d463
3e31f2e
f8f9f69
3767f5a
ea666e7
6b5fc2d
e0f4201
2d2ecbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| #!/bin/bash | ||
|
|
||
| # omarchy:summary=Turn automatic brightness from the ambient light sensor on or off | ||
| # omarchy:args=[status [--json]|on|off|toggle|seed [--force]] | ||
| # omarchy:examples=omarchy brightness auto | omarchy brightness auto toggle | omarchy brightness auto seed --force | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| sys_root=/sys | ||
| unit=wluma.service | ||
| state_dir=${XDG_STATE_HOME:-$HOME/.local/state}/wluma | ||
| config_file=${XDG_CONFIG_HOME:-$HOME/.config}/wluma/config.toml | ||
|
|
||
| # Starting curves, as lux:per-mille of the device's maximum. wluma keeps learning from manual changes on top of them. | ||
| panel_curve="0:50 5:120 20:220 50:320 150:450 400:600 1000:780 3000:920 10000:1000" | ||
| keyboard_curve="0:500 3:400 10:250 25:100 50:0" | ||
|
|
||
| usage() { | ||
| echo "Usage: omarchy brightness auto [status [--json]|on|off|toggle|seed [--force]]" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| sensor_dir() { | ||
| local dir | ||
| for dir in "$sys_root"/bus/iio/devices/*; do | ||
| if [[ -e $dir/in_illuminance_input || -e $dir/in_illuminance_raw ]]; then | ||
| echo "$dir" | ||
| return 0 | ||
| fi | ||
| done | ||
| return 1 | ||
| } | ||
|
|
||
| # The lux reading, or nothing while the sensor has none (reads fail with ENODATA until it reports). | ||
| read_lux() { | ||
| local dir file value | ||
| dir=$(sensor_dir) || return 0 | ||
| for file in in_illuminance_input in_illuminance_raw; do | ||
| if value=$(cat "$dir/$file" 2>/dev/null) && [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]]; then | ||
| echo "${value%.*}" | ||
| return 0 | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| first_dir() { | ||
| local dir | ||
| for dir in "$@"; do | ||
| if [[ -d $dir ]]; then | ||
| echo "$dir" | ||
| return 0 | ||
| fi | ||
| done | ||
| return 1 | ||
| } | ||
|
|
||
| # wluma names the panel after its DRM connector. | ||
| panel_connector() { | ||
| local dir name | ||
| for dir in "$sys_root"/class/drm/card*-eDP-*; do | ||
| if [[ -f $dir/status && $(<"$dir/status") == "connected" ]]; then | ||
| name=${dir##*/} | ||
| echo "${name#*-}" | ||
| return 0 | ||
| fi | ||
| done | ||
| return 1 | ||
| } | ||
|
|
||
| # Writes one wluma data file unless it exists: name, device maximum, curve. | ||
| write_curve() { | ||
| local name=$1 maximum=$2 curve=$3 force=$4 | ||
| local file=$state_dir/$name.yaml | ||
| if [[ ! $maximum =~ ^[1-9][0-9]*$ ]]; then | ||
| echo "Cannot read max_brightness for $name, so no curve is seeded for it." >&2 | ||
| exit 1 | ||
| fi | ||
| if [[ -e $file && $force != 1 ]]; then | ||
| return 0 | ||
| fi | ||
| mkdir -p "$state_dir" | ||
| { | ||
| echo "output_name: $name" | ||
| echo "entries:" | ||
| echo " brightness:" | ||
| awk -v maximum="$maximum" -v curve="$curve" 'BEGIN { | ||
| count = split(curve, points, " ") | ||
| for (index_ = 1; index_ <= count; index_++) { | ||
| split(points[index_], pair, ":") | ||
| value = int(maximum * pair[2] / 1000 + 0.5) | ||
| printf " - als: %d\n luma: 0\n value: %d\n", pair[1], value | ||
| } | ||
| }' | ||
| echo " dim: []" | ||
| echo " temperature: []" | ||
| } >"$file.tmp" | ||
| mv "$file.tmp" "$file" | ||
| } | ||
|
|
||
| seed() { | ||
| local force=0 panel keyboard connector seeded=0 | ||
| if [[ ${1:-} == "--force" ]]; then | ||
| force=1 | ||
| elif [[ -n ${1:-} ]]; then | ||
| usage | ||
| fi | ||
| if panel=$(first_dir "$sys_root"/class/backlight/*) && connector=$(panel_connector); then | ||
| write_curve "$connector" "$(<"$panel/max_brightness")" "$panel_curve" "$force" | ||
| seeded=1 | ||
| fi | ||
| if keyboard=$(first_dir "$sys_root"/class/leds/*kbd_backlight*); then | ||
| write_curve "${keyboard##*/}" "$(<"$keyboard/max_brightness")" "$keyboard_curve" "$force" | ||
| seeded=1 | ||
| fi | ||
| if (( seeded == 0 )); then | ||
| echo "No internal display backlight or keyboard backlight found to seed." >&2 | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # Ambient light only: no screen capture, gamma left to hyprsunset, idle dimming left to Omarchy's idle chain. | ||
| write_config() { | ||
| local connector | ||
| if [[ -e $config_file ]]; then | ||
| return 0 | ||
| fi | ||
| if ! connector=$(panel_connector); then | ||
| echo "No internal panel is connected, so the screen gets no wluma config." >&2 | ||
| return 0 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With no connected panel this returns success, and — Marcelo's agent
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 113ecbf. |
||
| fi | ||
| mkdir -p "${config_file%/*}" | ||
| printf '[idle]\nenabled = false\n\n[[output.backlight]]\nname = "%s"\ncapturer = "none"\ngamma = false\n' "$connector" >"$config_file" | ||
| } | ||
|
|
||
| enabled() { | ||
| systemctl --user is-enabled "$unit" >/dev/null 2>&1 | ||
| } | ||
|
|
||
| turn_on() { | ||
| if ! command -v wluma >/dev/null; then | ||
| echo "wluma is not installed." >&2 | ||
| exit 1 | ||
| fi | ||
| if ! sensor_dir >/dev/null; then | ||
| echo "No ambient light sensor found." >&2 | ||
| exit 1 | ||
| fi | ||
| seed | ||
| write_config | ||
| systemctl --user enable --now "$unit" | ||
| } | ||
|
|
||
| turn_off() { | ||
| systemctl --user disable --now "$unit" | ||
| } | ||
|
|
||
| status() { | ||
| local lux state=off | ||
| lux=$(read_lux) | ||
| if enabled; then | ||
| state=on | ||
| fi | ||
| if [[ ${1:-} == "--json" ]]; then | ||
| printf '{"enabled":%s,"lux":%s}\n' "$([[ $state == "on" ]] && echo true || echo false)" "${lux:-null}" | ||
| elif [[ -z ${1:-} ]]; then | ||
| echo "Automatic brightness: $state" | ||
| if [[ -n $lux ]]; then | ||
| echo "Ambient light: $lux lux" | ||
| else | ||
| echo "Ambient light: no reading" | ||
| fi | ||
| else | ||
| usage | ||
| fi | ||
| } | ||
|
|
||
| if ! omarchy-hw-apple-silicon; then | ||
| echo "Automatic brightness is only set up for Apple Silicon Macs." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # A verb takes at most this many words after it. | ||
| arguments_at_most() { | ||
| if (( $# - 2 > $1 )); then | ||
| usage | ||
| fi | ||
| } | ||
|
|
||
| case "${1:-status}" in | ||
| status) arguments_at_most 1 "$@"; status "${2:-}" ;; | ||
| on) arguments_at_most 0 "$@"; turn_on ;; | ||
| off) arguments_at_most 0 "$@"; turn_off ;; | ||
| toggle) arguments_at_most 0 "$@"; if enabled; then turn_off; else turn_on; fi ;; | ||
| seed) arguments_at_most 1 "$@"; seed "${2:-}" ;; | ||
| *) usage ;; | ||
| esac | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| #!/bin/bash | ||
| set -euo pipefail | ||
| source "$(dirname "$0")/base-test.sh" | ||
| work=$(mktemp -d) | ||
| trap 'rm -rf "$work"' EXIT | ||
| command_source="$ROOT/bin/omarchy-brightness-auto" | ||
| [[ -x $command_source ]] || fail 'ships the command' | ||
|
|
||
| # A fixture sysfs: the ambient light sensor, the panel and keyboard backlights, a connected eDP-1 connector. | ||
| sys="$work/sys" | ||
| mkdir -p "$sys/bus/iio/devices/iio:device0" "$sys/class/backlight/apple-panel-bl" \ | ||
| "$sys/class/leds/apple::kbd_backlight" "$sys/class/drm/card1-eDP-1" "$sys/class/drm/card1-DP-1" "$work/bin" "$work/state" "$work/config" | ||
| echo aop-sensors-als >"$sys/bus/iio/devices/iio:device0/name" | ||
| echo 123 >"$sys/bus/iio/devices/iio:device0/in_illuminance_input" | ||
| echo 1000 >"$sys/class/backlight/apple-panel-bl/max_brightness" | ||
| echo 255 >"$sys/class/leds/apple::kbd_backlight/max_brightness" | ||
| echo connected >"$sys/class/drm/card1-eDP-1/status" | ||
| echo disconnected >"$sys/class/drm/card1-DP-1/status" | ||
|
|
||
| # Stubs: wluma is present, systemctl records its arguments and answers is-enabled from a flag file. | ||
| printf '#!/bin/sh\nexit 0\n' >"$work/bin/wluma" | ||
| cat >"$work/bin/systemctl" <<'STUB' | ||
| #!/bin/bash | ||
| echo "$*" >>"$SYSTEMCTL_LOG" | ||
| if [[ $* == "--user is-enabled wluma.service" ]]; then | ||
| [[ -f $ENABLED_FLAG ]] && { echo enabled; exit 0; } | ||
| echo disabled | ||
| exit 1 | ||
| fi | ||
| exit 0 | ||
| STUB | ||
| # The runtime's Apple predicate: true unless the flag file says this is another machine. | ||
| cat >"$work/bin/omarchy-hw-apple-silicon" <<'STUB' | ||
| #!/bin/bash | ||
| [[ ! -f $NOT_APPLE_FLAG ]] | ||
| STUB | ||
| chmod +x "$work/bin/wluma" "$work/bin/systemctl" "$work/bin/omarchy-hw-apple-silicon" | ||
| export NOT_APPLE_FLAG="$work/not-apple" | ||
| export SYSTEMCTL_LOG="$work/systemctl.log" ENABLED_FLAG="$work/enabled" | ||
| export XDG_STATE_HOME="$work/state" XDG_CONFIG_HOME="$work/config" | ||
| : >"$SYSTEMCTL_LOG" | ||
|
|
||
| command="$work/bin/omarchy-brightness-auto" | ||
| sed "s|^sys_root=.*|sys_root=$sys|" "$command_source" >"$command" | ||
| chmod +x "$command" | ||
| run() { PATH="$work/bin:$PATH" "$command" "$@"; } | ||
|
|
||
| panel="$work/state/wluma/eDP-1.yaml" | ||
| keyboard="$work/state/wluma/apple::kbd_backlight.yaml" | ||
|
|
||
| # Seed curves. | ||
| run seed >/dev/null | ||
| [[ -f $panel && -f $keyboard ]] || fail 'seed writes one file per panel and keyboard, named as wluma names them' | ||
| grep -Fxq 'output_name: eDP-1' "$panel" || fail 'panel file names its output' | ||
| grep -Fxq 'output_name: apple::kbd_backlight' "$keyboard" || fail 'keyboard file names its output' | ||
| values() { awk '/^ +value: / { print $2 }' "$1" | paste -sd' '; } | ||
| lux() { awk '/^ +- als: / { print $3 }' "$1" | paste -sd' '; } | ||
| panel_values=$(values "$panel") | ||
| keyboard_values=$(values "$keyboard") | ||
| sorted_up=$(tr ' ' '\n' <<<"$panel_values" | sort -n | paste -sd' ') | ||
| sorted_down=$(tr ' ' '\n' <<<"$keyboard_values" | sort -rn | paste -sd' ') | ||
| [[ $panel_values == "$sorted_up" ]] || fail 'the panel gets brighter with the light' "$panel_values" | ||
| [[ $keyboard_values == "$sorted_down" ]] || fail 'the keyboard gets dimmer with the light' "$keyboard_values" | ||
| [[ ${panel_values##* } == 1000 ]] || fail 'the panel reaches its maximum in daylight' "$panel_values" | ||
| (( ${panel_values%% *} >= 1 && ${panel_values%% *} <= 100 )) || fail 'the panel never goes dark' "$panel_values" | ||
| [[ ${keyboard_values##* } == 0 ]] && (( ${keyboard_values%% *} >= 64 && ${keyboard_values%% *} <= 255 )) || fail 'the keyboard is lit in the dark and off in daylight' "$keyboard_values" | ||
| [[ $(lux "$panel" | cut -d' ' -f1) == 0 ]] || fail 'the curve starts at zero lux' | ||
| pass 'seed writes monotonic curves scaled to the device ranges' | ||
|
|
||
| # Seed keeps what wluma learned. | ||
| echo learned >"$panel" | ||
| run seed >/dev/null | ||
| [[ $(cat "$panel") == learned ]] || fail 'seed leaves an existing file alone' | ||
| run seed --force >/dev/null | ||
| grep -Fxq 'output_name: eDP-1' "$panel" || fail 'seed --force replaces it' | ||
| pass 'seed never overwrites learned data without --force' | ||
|
|
||
| # A smaller panel range scales the curve. | ||
| echo 255 >"$sys/class/backlight/apple-panel-bl/max_brightness" | ||
| run seed --force >/dev/null | ||
| [[ $(values "$panel" | tr ' ' '\n' | tail -1) == 255 ]] || fail 'the curve follows max_brightness' | ||
| echo 1000 >"$sys/class/backlight/apple-panel-bl/max_brightness" | ||
| run seed --force >/dev/null | ||
| pass 'seed scales to max_brightness' | ||
|
|
||
| # on: seeds, writes the config once, starts the service. | ||
| rm -f "$panel" "$keyboard" | ||
| run on >/dev/null | ||
| [[ -f $panel ]] || fail 'on seeds missing data' | ||
| config="$work/config/wluma/config.toml" | ||
| grep -Fq 'name = "eDP-1"' "$config" && grep -Fq 'capturer = "none"' "$config" && grep -Fq 'gamma = false' "$config" || fail 'on writes an ambient-light-only config for the panel' "$(cat "$config")" | ||
| awk '/^\[idle\]$/ { idle = 1; next } /^\[/ { idle = 0 } idle && /^enabled = false$/ { found = 1 } END { exit !found }' "$config" || fail 'on leaves idle dimming to Omarchy' "$(cat "$config")" | ||
| grep -Fxq -- '--user enable --now wluma.service' "$SYSTEMCTL_LOG" || fail 'on enables and starts the service' "$(cat "$SYSTEMCTL_LOG")" | ||
| echo '# mine' >"$config" | ||
| run on >/dev/null | ||
| [[ $(cat "$config") == '# mine' ]] || fail 'on keeps an existing config' | ||
| pass 'on seeds, writes the config once and starts wluma' | ||
|
|
||
| # on refuses without a sensor or without wluma. | ||
| : >"$SYSTEMCTL_LOG" | ||
| mv "$sys/bus/iio/devices/iio:device0" "$work/iio-away" | ||
| if run on >/dev/null 2>"$work/err"; then fail 'on refuses without a light sensor'; fi | ||
| grep -qi 'light sensor' "$work/err" || fail 'on says why' "$(cat "$work/err")" | ||
| mv "$work/iio-away" "$sys/bus/iio/devices/iio:device0" | ||
| # A PATH with the tools the command needs and no wluma. | ||
| mkdir -p "$work/nowluma" | ||
| for tool in bash cat awk mkdir mv paste; do ln -sf "$(command -v "$tool")" "$work/nowluma/$tool"; done | ||
| ln -sf "$work/bin/systemctl" "$work/nowluma/systemctl" | ||
| ln -sf "$work/bin/omarchy-hw-apple-silicon" "$work/nowluma/omarchy-hw-apple-silicon" | ||
| if PATH="$work/nowluma" "$command" on >/dev/null 2>"$work/err"; then fail 'on refuses without wluma'; fi | ||
| grep -q 'wluma is not installed' "$work/err" || fail 'on names the missing package' "$(cat "$work/err")" | ||
| [[ ! -s $SYSTEMCTL_LOG ]] || fail 'a refused on starts nothing' "$(cat "$SYSTEMCTL_LOG")" | ||
| pass 'on refuses without a sensor or without wluma' | ||
|
|
||
| # off, toggle, status. | ||
| run off >/dev/null | ||
| grep -Fxq -- '--user disable --now wluma.service' "$SYSTEMCTL_LOG" || fail 'off disables and stops the service' | ||
| : >"$SYSTEMCTL_LOG" | ||
| run toggle >/dev/null | ||
| grep -Fxq -- '--user enable --now wluma.service' "$SYSTEMCTL_LOG" || fail 'toggle turns it on when off' | ||
| touch "$ENABLED_FLAG" | ||
| : >"$SYSTEMCTL_LOG" | ||
| run toggle >/dev/null | ||
| grep -Fxq -- '--user disable --now wluma.service' "$SYSTEMCTL_LOG" || fail 'toggle turns it off when on' | ||
| pass 'off and toggle drive the service' | ||
|
|
||
| [[ $(run status --json) == '{"enabled":true,"lux":123}' ]] || fail 'status --json reports state and lux' "$(run status --json)" | ||
| rm -f "$ENABLED_FLAG" | ||
| [[ $(run status --json) == '{"enabled":false,"lux":123}' ]] || fail 'status --json reports off' | ||
| [[ $(run status | sed -n 2p) == 'Ambient light: 123 lux' ]] || fail 'status prints the lux once' "$(run status)" | ||
| rm "$sys/bus/iio/devices/iio:device0/in_illuminance_input" | ||
| [[ $(run status | sed -n 2p) == 'Ambient light: no reading' ]] || fail 'status says when there is no reading' "$(run status)" | ||
| [[ $(run status --json) == '{"enabled":false,"lux":null}' ]] || fail 'status --json reports a missing reading as null' "$(run status --json)" | ||
| pass 'status reports state and lux, plain and JSON' | ||
|
|
||
| if run bogus >/dev/null 2>&1; then fail 'unknown commands fail'; fi | ||
| pass 'rejects unknown commands' | ||
|
|
||
| # Extra arguments are an error, not ignored, and nothing runs. | ||
| : >"$SYSTEMCTL_LOG" | ||
| for args in 'off x' 'on x' 'toggle x' 'seed --force x' 'status --json x'; do | ||
| # shellcheck disable=SC2086 | ||
| if run $args >/dev/null 2>&1; then fail "'$args' is refused"; fi | ||
| done | ||
| [[ ! -s $SYSTEMCTL_LOG ]] || fail 'a refused call starts nothing' "$(cat "$SYSTEMCTL_LOG")" | ||
| pass 'rejects extra arguments' | ||
|
|
||
| # A device whose maximum cannot be read must not get an all-zero curve. | ||
| rm -rf "$work/state" | ||
| : >"$sys/class/backlight/apple-panel-bl/max_brightness" | ||
| if run seed >/dev/null 2>"$work/err"; then fail 'seed refuses an unreadable maximum'; fi | ||
| grep -q 'max_brightness' "$work/err" || fail 'seed names the bad maximum' "$(cat "$work/err")" | ||
| [[ ! -e $work/state/wluma/eDP-1.yaml ]] || fail 'seed writes no curve for it' | ||
| echo 1000 >"$sys/class/backlight/apple-panel-bl/max_brightness" | ||
| pass 'seed refuses an unreadable max_brightness' | ||
|
|
||
| # No connected internal panel: on still seeds the keyboard but says the screen gets no config. | ||
| rm -rf "$work/state" "$work/config" | ||
| echo 123 >"$sys/bus/iio/devices/iio:device0/in_illuminance_input" | ||
| echo disconnected >"$sys/class/drm/card1-eDP-1/status" | ||
| run on >/dev/null 2>"$work/err" || fail 'on still runs with only a keyboard backlight' "$(cat "$work/err")" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pins starting wluma without a config as correct. Please assert that nothing is started or written instead. — Marcelo's agent
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced in 113ecbf. The block now asserts that a refused |
||
| grep -qi 'panel' "$work/err" || fail 'on says the panel was not configured' "$(cat "$work/err")" | ||
| echo connected >"$sys/class/drm/card1-eDP-1/status" | ||
| pass 'on says when no panel is configured' | ||
|
|
||
| # Off Apple Silicon every verb refuses and touches nothing. | ||
| touch "$NOT_APPLE_FLAG" | ||
| rm -rf "$work/state" "$work/config" | ||
| : >"$SYSTEMCTL_LOG" | ||
| for verb in status on off toggle seed; do | ||
| if run "$verb" >/dev/null 2>"$work/err"; then fail "$verb refuses off Apple Silicon"; fi | ||
| grep -q 'Apple Silicon' "$work/err" || fail "$verb says why" "$(cat "$work/err")" | ||
| done | ||
| [[ ! -s $SYSTEMCTL_LOG && ! -e $work/state && ! -e $work/config ]] || fail 'a refused verb touches nothing' | ||
| rm "$NOT_APPLE_FLAG" | ||
| pass 'refuses off Apple Silicon' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first backlight can be the Touch Bar's
228600000.dsi.0, which sorts beforeapple-panel-bl. Please applydisplays.conf's skip and prefer rules.— Marcelo's agent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 113ecbf.
seednow picks the backlight withpanel_backlight, which readsdisplays.conf(backlight-skip,backlight-prefer): the Touch Bar's228600000.dsi.0anddisplay-pipeare skipped, the preferredapple-panel-blwins, and another backlight is used only when it is missing. The test fixture has228600000.dsi.0anddisplay-pipeatmax_brightness100, plus ana-otherbacklight that sorts before the panel; with the skip or the prefer rule removed the test fails.