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
Open
joshuaswarren
wants to merge
14
commits into
omacom:main
Choose a base branch
from
joshuaswarren:w7H/brightness-auto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
709b67a
omarchy-mac: Add omarchy brightness auto, ambient light brightness th…
joshuaswarren fec1c54
omarchy-mac: Leave idle dimming to Omarchy in the wluma config
joshuaswarren 0ddbc0e
omarchy-mac: Fix brightness auto status, arguments and seeding checks
joshuaswarren 113ecbf
omarchy-mac: Refuse brightness auto without a panel, skip the Touch B…
joshuaswarren 667764e
omarchy-mac: Say why brightness auto stops when the panel disconnects…
joshuaswarren 1dabf33
omarchy-mac: Run the machine's own mv in the brightness-auto test stub
joshuaswarren cc8d463
omarchy-mac: Leave the keyboard backlight out of the generated wluma …
joshuaswarren 3e31f2e
Manual: say automatic brightness moves the screen only
joshuaswarren f8f9f69
Manual: the hardware table no longer says the keyboard backlight foll…
joshuaswarren 3767f5a
Brightness auto test: pin the panel-gone message, name the no-keyboar…
joshuaswarren ea666e7
omarchy-mac: Leave external displays out of automatic brightness
joshuaswarren 6b5fc2d
omarchy-mac: Refuse wluma older than 5.0
joshuaswarren e0f4201
test: stub pacman with the rest; wording per review
joshuaswarren 2d2ecbc
omarchy-mac: name the panel backlight path in wluma config
joshuaswarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| #!/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 | ||
| displays_conf=/usr/share/omarchy-platform/displays.conf | ||
| 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 | ||
| } | ||
|
|
||
| # The built-in panel's backlight, by the platform's displays.conf: skip the listed Touch Bar backlights, take the preferred | ||
| # one when present, else the first left. | ||
| panel_backlight() { | ||
| local dir name skip=" " prefer="" kind value first="" | ||
| if [[ -r $displays_conf ]]; then | ||
| while read -r kind value _; do | ||
| case "$kind" in | ||
| "backlight-skip") skip+="$value " ;; | ||
| "backlight-prefer") prefer=$value ;; | ||
| esac | ||
| done <"$displays_conf" | ||
| fi | ||
| for dir in "$sys_root"/class/backlight/*; do | ||
| name=${dir##*/} | ||
| if [[ ! -d $dir || $skip == *" $name "* ]]; then | ||
| continue | ||
| fi | ||
| if [[ $name == "$prefer" ]]; then | ||
| echo "$dir" | ||
| return 0 | ||
| fi | ||
| if [[ -z $first ]]; then | ||
| first=$dir | ||
| fi | ||
| done | ||
| if [[ -z $first ]]; then | ||
| return 1 | ||
| fi | ||
| echo "$first" | ||
| } | ||
|
|
||
| # 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=$(panel_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 for the panel: no screen capture, gamma left to hyprsunset, idle dimming left to Omarchy's idle | ||
| # chain, the keyboard backlight left to whatever else drives it. | ||
| write_config() { | ||
| local connector keyboard | ||
| if [[ -e $config_file ]]; then | ||
| return 0 | ||
| fi | ||
| # The panel can disconnect after turn_on checked it; without a name there is no config to write. | ||
| if ! connector=$(panel_connector); then | ||
| echo "The internal panel disconnected while brightness auto was starting, so no wluma config was written and wluma was not started. Wake the panel and run it again." >&2 | ||
| exit 1 | ||
| fi | ||
| mkdir -p "${config_file%/*}" | ||
| printf '[idle]\nenabled = false\n\n[[output.backlight]]\nname = "%s"\ncapturer = "none"\ngamma = false\n' "$connector" >"$config_file" | ||
| # wluma finds every kbd_backlight on its own and, with its idle off, learns the lock screen's keyboard-off as the | ||
| # user's choice, so the keyboard stays dark in a dark room. Naming the LED with enabled = false drops it. | ||
| if keyboard=$(first_dir "$sys_root"/class/leds/*kbd_backlight*); then | ||
| printf '\n[[keyboard]]\nname = "%s"\npath = "%s"\nenabled = false\n' "${keyboard##*/}" "$keyboard" >>"$config_file" | ||
| fi | ||
| } | ||
|
|
||
| enabled() { | ||
| systemctl --user is-enabled "$unit" >/dev/null 2>&1 | ||
| } | ||
|
|
||
| turn_on() { | ||
| if ! command -v wluma >/dev/null; then | ||
| echo "wluma is not installed. Install it with: omarchy-pkg-add wluma" >&2 | ||
| exit 1 | ||
| fi | ||
| if ! sensor_dir >/dev/null; then | ||
| echo "No ambient light sensor found." >&2 | ||
| exit 1 | ||
| fi | ||
| # Without a config wluma runs on its defaults: it captures the screen and dims on idle. | ||
| if [[ ! -e $config_file ]] && ! panel_connector >/dev/null; then | ||
| echo "No internal panel is connected and there is no wluma config, so wluma would run on its defaults. Wake the panel, or write $config_file first." >&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 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
With a connected eDP but no panel backlight this passes, so
seedwrites the keyboard curve beforewrite_configrefuses at:166. Could this checkpanel_backlighttoo?— Marcelo's agent