Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mac-manual/content/06-keyboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Apple keyboards have no Print Screen key, so Omarchy's `Print` bindings have no

On Macs with an ambient light sensor, the keyboard backlight follows the room: lit in the dark, off in bright light. Apple Silicon keyboards have no backlight keys, so Omarchy's keyboard backlight bindings have no key on the built-in keyboard yet; [omacom/omarchy-mac#683](https://github.com/omacom/omarchy-mac/issues/683) tracks giving it some.

`omarchy brightness auto on` also makes the screen follow the room, and the keyboard backlight with it, from the same sensor through wluma; `omarchy brightness auto off` stops it. It starts from a built-in curve and keeps learning from the brightness you set by hand.

## Trackpad

The trackpad follows Omarchy's defaults, as on any other machine. Change scrolling, tap-to-click and gestures in `~/.config/hypr/input.lua`, as [Keyboard, mouse, trackpad](https://omarchy.org/manual/keyboard-mouse-trackpad/) describes. For example, to scroll naturally and click only by pressing the trackpad:
Expand Down
8 changes: 8 additions & 0 deletions omarchy-mac/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ A set limit is saved as `CHARGE_CONTROL_END_THRESHOLD=` in `/etc/udev/macsmc-bat

asahi-scripts' path unit starts `macsmc-battery-charge-control-end-threshold.service`, which saves the limit, on every SMC battery event, so plugging in the charger or resuming can start it more than five times in ten seconds and leave it `failed` with `start-limit-hit`. Since it only rewrites the same line, the package's drop-in removes its start limit; the drop-in goes once AsahiLinux/asahi-scripts#78 fixes the unit upstream.

## Automatic brightness

`omarchy brightness auto [status [--json]|on|off|toggle|seed [--force]]` turns on automatic brightness for the panel and the keyboard backlight from the ambient light sensor, as macOS does. It runs [wluma](https://github.com/max-baz/wluma) as the user unit `wluma.service` with the sensor alone: no screen capture, and no gamma control, so hyprsunset keeps the colour temperature. `on` refuses without wluma or without a light sensor under `/sys/bus/iio/devices` (`in_illuminance_input` or `in_illuminance_raw`).

wluma learns from manual brightness changes and does nothing until it has data, so `on` first seeds one starting curve per device in `~/.local/state/wluma/`, named as wluma names them: the panel's DRM connector and the keyboard LED. The panel curve climbs from 5% of `max_brightness` in the dark to 100% at 10000 lux; the keyboard is at half of its maximum in the dark and off from 50 lux. A seed never overwrites a file wluma has written; `seed --force` does. `on` also writes `~/.config/wluma/config.toml` when none exists, and never replaces one: the panel with ambient light only, and wluma's idle dimming off, since Omarchy's own idle chain handles an idle screen and would otherwise fight wluma over the keyboard backlight.

`status` reports whether the unit is enabled and the current lux reading, which stays empty until the sensor reports.

## Keyboard function-key mode

The built-in keyboard binds to `hid_apple`, whose `fnmode` decides what the top row sends. Apple Silicon uses the kernel's default, `fnmode=3` (auto): Apple keyboards send mute, volume, brightness and media first with F1-F12 behind Fn, as in macOS, and the non-Apple boards `hid_apple` recognises (Keychron and similar) keep F-keys first. The package ships no `hid_apple` option and the desktop install leaf writes none on Apple Silicon, so any option the owner sets wins, whatever its file name. For F-keys first on the Mac, write `options hid_apple fnmode=2` to `/etc/modprobe.d/hid_apple.conf` and rebuild the boot image (`sudo mkinitcpio -P`, or `sudo omarchy-mac-boot-update` on a Limine Mac): `hid_apple` loads from the initramfs with the options it was built with.
Expand Down
196 changes: 196 additions & 0 deletions omarchy-mac/bin/omarchy-brightness-auto
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

Copy link
Copy Markdown
Member

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 before apple-panel-bl. Please apply displays.conf's skip and prefer rules.

— Marcelo's agent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 113ecbf. seed now picks the backlight with panel_backlight, which reads displays.conf (backlight-skip, backlight-prefer): the Touch Bar's 228600000.dsi.0 and display-pipe are skipped, the preferred apple-panel-bl wins, and another backlight is used only when it is missing. The test fixture has 228600000.dsi.0 and display-pipe at max_brightness 100, plus an a-other backlight that sorts before the panel; with the skip or the prefer rule removed the test fails.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With no connected panel this returns success, and turn_on then enables wluma with no config, so it runs on its defaults. Could on refuse here before systemctl instead?

— Marcelo's agent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 113ecbf. turn_on now checks, before it seeds, writes or starts anything, that there is a config or a connected panel; with neither it exits 1 and says wluma would run on its defaults. write_config no longer has the warn-and-continue path. A config of the user's own is enough, and on then leaves it alone.

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
176 changes: 176 additions & 0 deletions omarchy-mac/test/brightness-auto-test.sh
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")"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced in 113ecbf. The block now asserts that a refused on leaves the systemctl log empty and writes neither a config nor seed files, then that a user config alone is enough and is not touched.

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'
Loading
Loading