Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/omarchy-mac/ORIGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ The network backend default follows Marcelo Alcantara's Apple Silicon integratio

- Microphone mapper, user service, headset priority and behavioral tests: Scott Jones, `1d2f4af12cb4b65731bafa1305d1d5c9ac5507a8` (Map the Asahi mic array and install the protected audio stack). The mapper is extracted unchanged, including gain/mute persistence, device choice protection, graph rollback, and event-based supervision.
- Notch module default: Scott Jones, `cfe1d37e8f0fa27811938872325119e3eeca9354` (Floor the top bar at the Apple Silicon camera cutout), extracted from `install/hardware/apple/enable-notch.sh` at the baseline above; only the existing `appledrm show_notch=1` default moves to the vendor directory.
- Battery charge limit command and behavioral tests: Naeem Malik, `00a2d31019e4d5ea5a3e5e9cb45a75aa90d07ee2` (Add an Apple Silicon battery charge limit command, #497). The driver semantics and readback check are retained; the platform gate, saved limit and boot-time restore are new.
- Keyboard function-key mode: Marcelo Alcantara, maralcbr/omarchy-mx-mac `6f2248d4cc065f2cd88550ed414012151bc57622` (#266, `fnmode=3` on Apple Silicon), replacing Scott Jones's `fnmode=1` from `0022c4374d9f13dffd8b6415c095d1a5ae491d33` (Put media keys on the Apple Silicon top row without stealing x86 F-keys). The generated-line retirement, owed-rebuild handling and live switch follow both forks' migrations; leaving the kernel default in place and the setup command are new work.
6 changes: 6 additions & 0 deletions packages/omarchy-mac/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ Run `omarchy-mac-setup-user` as each target user with their HOME/XDG directories

Vendor defaults use NetworkManager's `/usr/lib/NetworkManager/conf.d`, systemd's `/usr/lib/systemd`, modprobe's `/usr/lib/modprobe.d`, and WirePlumber's `/usr/share/wireplumber/wireplumber.conf.d`. Same-name `/etc` or user fragments retain precedence. Setup reports effective live NetworkManager/module configuration and systemd fragments. Review those reports and any drop-ins when diagnosing overrides; custom policy is never normalized to the package default.

## Battery charge limit

`omarchy battery charge limit [80|100]` shows or sets the `macsmc-battery` charge thresholds and refuses unless `omarchy-hw-platform` reports `apple-silicon`. It writes only the end threshold through `sudo`, since the driver derives the start threshold (80 restarts charging at 75; 100 restores full charging), and checks the SMC's readback. Full charging stays the default.

A set limit is saved as `CHARGE_CONTROL_END_THRESHOLD=` in `/etc/udev/macsmc-battery.conf`, the file asahi-scripts' own rule restores from, so both agree on one value. The package's udev rule runs `/usr/lib/omarchy-mac/battery-charge-limit-restore` when the SMC battery appears at boot; it reapplies a saved 80 or 100 on Apple Silicon and otherwise leaves the SMC alone. Nothing needs enabling.

## 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
72 changes: 72 additions & 0 deletions packages/omarchy-mac/bin/omarchy-battery-charge-limit
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

# omarchy:summary=Set or show the Apple Silicon battery charge limit
# omarchy:args=[80|100]
# omarchy:examples=omarchy battery charge limit | omarchy battery charge limit 80 | omarchy battery charge limit 100

set -euo pipefail

battery_path="/sys/class/power_supply/macsmc-battery"
start_file="$battery_path/charge_control_start_threshold"
end_file="$battery_path/charge_control_end_threshold"
# asahi-scripts saves and restores the limit through this same file, so both
# mechanisms agree on one saved value.
saved_file="/etc/udev/macsmc-battery.conf"

usage() {
echo "Usage: omarchy battery charge limit [80|100]" >&2
exit 2
}

write_as_root() {
if (( EUID == 0 )); then
/usr/bin/tee "$1" >/dev/null
else
sudo /usr/bin/tee "$1" >/dev/null
fi
}

(( $# <= 1 )) || usage
limit="${1:-status}"
case "$limit" in
status | 80 | 100) ;;
*) usage ;;
esac

if ! platform=$(omarchy-hw-platform) || [[ $platform != "apple-silicon" ]]; then
echo "The battery charge limit is only available on Apple Silicon Macs." >&2
exit 1
fi

if [[ ! -r $start_file || ! -r $end_file ]]; then
echo "Apple Silicon charge thresholds are not available on this system." >&2
exit 1
fi

if [[ $limit == "status" ]]; then
printf 'Charge limit: %s%% (restart charging at %s%%)\n' "$(<"$end_file")" "$(<"$start_file")"
else
if [[ $limit == "80" ]]; then
expected_start=75
else
expected_start=100
fi

# The driver derives the start threshold from the end threshold and ignores
# writes to the start threshold.
printf '%s\n' "$limit" | write_as_root "$end_file"

actual_start=$(<"$start_file")
actual_end=$(<"$end_file")
if [[ $actual_start != $expected_start || $actual_end != $limit ]]; then
echo "Charge threshold update did not stick; the SMC reports ${actual_start}%/${actual_end}%." >&2
exit 1
fi

if ! printf 'CHARGE_CONTROL_END_THRESHOLD=%s\n' "$limit" | write_as_root "$saved_file"; then
echo "Charge limit set to ${limit}%, but it could not be saved for the next boot." >&2
exit 1
fi

printf 'Charge limit set to %s%% (restart charging at %s%%).\n' "$actual_end" "$actual_start"
fi
21 changes: 21 additions & 0 deletions packages/omarchy-mac/lib/battery-charge-limit-restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
# udev runs this as the SMC battery appears, so a saved charge limit survives
# reboots. It never fails the event: anything unexpected leaves the SMC alone.
set -euo pipefail

saved_file="/etc/udev/macsmc-battery.conf"
end_file="/sys/class/power_supply/macsmc-battery/charge_control_end_threshold"

[[ $(omarchy-hw-platform 2>/dev/null) == "apple-silicon" ]] || exit 0
[[ -r $saved_file && -w $end_file ]] || exit 0

limit=""
while IFS= read -r line || [[ -n $line ]]; do
if [[ $line == CHARGE_CONTROL_END_THRESHOLD=* ]]; then
limit=${line#*=}
fi
done <"$saved_file"

if [[ $limit == "80" || $limit == "100" ]]; then
printf '%s\n' "$limit" >"$end_file" || true
fi
162 changes: 162 additions & 0 deletions packages/omarchy-mac/test/battery-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/bin/bash
set -euo pipefail
source "$(dirname "$0")/base-test.sh"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
stage="$work/stage"
"$ROOT/install" "$stage"
rule="$stage/usr/lib/udev/rules.d/94-omarchy-mac-battery-charge-limit.rules"
[[ -x $stage/usr/bin/omarchy-battery-charge-limit && -x $stage/usr/lib/omarchy-mac/battery-charge-limit-restore && -f $rule ]] || fail 'stages the command, restore helper and udev rule'
grep -Fxq 'ACTION=="add", SUBSYSTEM=="power_supply", KERNEL=="macsmc-battery", RUN+="/usr/lib/omarchy-mac/battery-charge-limit-restore"' "$rule" || fail 'rule runs the staged helper only for the SMC battery'
if command -v udevadm >/dev/null && udevadm verify --help >/dev/null 2>&1; then
udevadm verify --resolve-names=never "$rule" >/dev/null || fail 'udev rule verifies'
fi
pass 'stages a udev rule that restores the limit for the SMC battery only'

battery="$work/sys/macsmc-battery"
saved="$work/etc/udev/macsmc-battery.conf"
mkdir -p "$battery" "${saved%/*}" "$work/bin"
export BATTERY="$battery" SAVED="$saved" SUDO_LOG="$work/sudo.log"

# Exercise the terminal sudo path even when the suite runs as root, against a
# fixture battery and saved-limit file.
fixture() {
sed -e "s|/sys/class/power_supply/macsmc-battery|$battery|" \
-e "s|/etc/udev/macsmc-battery.conf|$saved|" \
-e 's/(( EUID == 0 ))/(( 1 == 0 ))/' "$1" >"$2"
chmod +x "$2"
}
command="$work/bin/omarchy-battery-charge-limit"
restore="$work/battery-charge-limit-restore"
fixture "$stage/usr/bin/omarchy-battery-charge-limit" "$command"
fixture "$stage/usr/lib/omarchy-mac/battery-charge-limit-restore" "$restore"

cat >"$work/bin/omarchy-hw-platform" <<'STUB'
#!/bin/bash
if [[ ${PLATFORM:-apple-silicon} == "contradiction" ]]; then
echo "Error: contradictory platform identity" >&2
exit 1
fi
echo "${PLATFORM:-apple-silicon}"
STUB
# The SMC derives the start threshold from the end threshold.
cat >"$work/bin/sudo" <<'STUB'
#!/bin/bash
(( $# == 2 )) && [[ $1 == "/usr/bin/tee" ]] || exit 1
printf '%s\n' "$2" >>"$SUDO_LOG"
value=$(cat)
if [[ $2 == $BATTERY/charge_control_end_threshold ]]; then
[[ ${SUDO_FAIL:-} != 1 ]] || exit 1
[[ ${SUDO_IGNORE:-} != 1 ]] || exit 0
printf '%s\n' "$value" >"$2"
if [[ $value == "80" ]]; then
printf '75\n' >"$BATTERY/charge_control_start_threshold"
else
printf '100\n' >"$BATTERY/charge_control_start_threshold"
fi
elif [[ $2 == $SAVED ]]; then
[[ ${SAVE_FAIL:-} != 1 ]] || exit 1
printf '%s\n' "$value" >"$2"
else
exit 1
fi
STUB
chmod +x "$work/bin/"*
export PATH="$work/bin:$PATH"

smc() {
printf '%s\n' "$1" >"$battery/charge_control_start_threshold"
printf '%s\n' "$2" >"$battery/charge_control_end_threshold"
}
reads() {
[[ $(<"$battery/charge_control_start_threshold") == $1 && $(<"$battery/charge_control_end_threshold") == $2 ]]
}
smc 100 100

[[ $("$command") == "Charge limit: 100% (restart charging at 100%)" ]] || fail 'reports the active limit'
[[ ! -e $SUDO_LOG && ! -e $saved ]] || fail 'status needs no privilege and saves nothing'
pass 'reports the active Apple Silicon charge thresholds'

[[ $("$command" 80) == "Charge limit set to 80% (restart charging at 75%)." ]] || fail 'reports the 80% cap'
reads 75 80 || fail 'sets the 80% cap and five point hysteresis'
[[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=80" ]] || fail 'saves the 80% cap for the next boot'
"$command" 100 >/dev/null
reads 100 100 || fail 'restores full charging'
[[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'saves full charging for the next boot'
(( $(grep -c charge_control_end_threshold "$SUDO_LOG") == 2 && $(grep -c -v charge_control_end_threshold "$SUDO_LOG") == 2 )) || fail 'writes only the end threshold and the saved limit once per change'
pass 'sets and clears the limit and saves it for the next boot'

if SUDO_FAIL=1 "$command" 80 >/dev/null 2>&1; then fail 'reports a failed threshold update'; fi
reads 100 100 && [[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'a failed write changes nothing'
if SUDO_IGNORE=1 "$command" 80 >/dev/null 2>&1; then fail 'reports an update that did not stick'; fi
[[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'an update that did not stick is not saved'
if SAVE_FAIL=1 "$command" 80 >/dev/null 2>"$work/err"; then fail 'reports a limit that could not be saved'; fi
grep -Fq 'could not be saved' "$work/err" || fail 'says the limit was not saved'
"$command" 100 >/dev/null
pass 'checks the driver readback and reports failed writes and saves'

# As root the command writes both files itself, without sudo.
sed 's/(( 1 == 0 ))/(( 0 == 0 ))/' "$command" >"$work/root-command"
chmod +x "$work/root-command"
: >"$SUDO_LOG"
smc 75 100
"$work/root-command" 80 >/dev/null || fail 'root sets the limit'
reads 75 80 && [[ $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=80" && ! -s $SUDO_LOG ]] || fail 'root writes the threshold and saved limit without sudo'
"$command" 100 >/dev/null
pass 'root sets and saves the limit without sudo'

: >"$SUDO_LOG"
for args in 85 0 '80 100'; do
# shellcheck disable=SC2086
if "$command" $args >/dev/null 2>&1; then fail "rejects $args"; fi
done
reads 100 100 && [[ ! -s $SUDO_LOG ]] || fail 'invalid input leaves the limit unchanged'
pass 'rejects unsupported limits without changing the device'

for platform in qualcomm generic-aarch64 generic contradiction; do
for args in '' 80 100; do
# shellcheck disable=SC2086
if PLATFORM=$platform "$command" $args >/dev/null 2>&1; then fail "$platform refuses ${args:-status}"; fi
done
done
reads 100 100 && [[ ! -s $SUDO_LOG && $(<"$saved") == "CHARGE_CONTROL_END_THRESHOLD=100" ]] || fail 'non-Apple platforms leave the battery and saved limit alone'
rm "$battery/charge_control_start_threshold"
if "$command" >/dev/null 2>"$work/err"; then fail 'reports missing thresholds'; fi
grep -Fq 'not available' "$work/err" || fail 'explains missing thresholds'
smc 100 100
pass 'refuses on non-Apple platforms and on kernels without thresholds'

# A reboot resets the fixture SMC to full charging; the udev helper reapplies
# the saved limit.
"$command" 80 >/dev/null
smc 100 100
"$restore" || fail 'restore succeeds'
[[ $(<"$battery/charge_control_end_threshold") == 80 ]] || fail 'restores the saved 80% cap after a reboot'
"$command" 100 >/dev/null
printf 'sentinel\n' >"$battery/charge_control_end_threshold"
"$restore"
[[ $(<"$battery/charge_control_end_threshold") == 100 ]] || fail 'restores saved full charging'
printf 'CHARGE_CONTROL_END_THRESHOLD=100\nCHARGE_CONTROL_END_THRESHOLD=80' >"$saved"
"$restore"
[[ $(<"$battery/charge_control_end_threshold") == 80 ]] || fail 'the last saved value wins, with or without a final newline'
pass 'reapplies the saved limit, including one saved by asahi-scripts'

untouched() {
printf 'sentinel\n' >"$battery/charge_control_end_threshold"
"$@" || fail 'restore never fails the udev event'
[[ $(<"$battery/charge_control_end_threshold") == "sentinel" ]]
}
rm "$saved"
untouched "$restore" || fail 'no saved limit leaves the SMC alone'
for content in 'CHARGE_CONTROL_END_THRESHOLD=85' 'CHARGE_CONTROL_END_THRESHOLD=60' 'CHARGE_CONTROL_END_THRESHOLD="80"' 'CHARGE_CONTROL_END_THRESHOLD=80 ' 'OTHER=80' ''; do
printf '%s\n' "$content" >"$saved"
untouched "$restore" || fail "ignores saved content: $content"
done
printf 'CHARGE_CONTROL_END_THRESHOLD=80\n' >"$saved"
for platform in qualcomm generic-aarch64 generic contradiction; do
PLATFORM=$platform untouched "$restore" || fail "$platform leaves the battery alone"
done
rm "$battery/charge_control_end_threshold"
"$restore" || fail 'restore without a battery succeeds'
[[ ! -e $battery/charge_control_end_threshold ]] || fail 'restore without a battery writes nothing'
pass 'restore ignores unsupported saved values, missing batteries and non-Apple platforms'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Reapply the charge limit saved by omarchy-battery-charge-limit when the Apple SMC battery appears.
ACTION=="add", SUBSYSTEM=="power_supply", KERNEL=="macsmc-battery", RUN+="/usr/lib/omarchy-mac/battery-charge-limit-restore"
Loading