diff --git a/bin/omarchy-bluetooth-power b/bin/omarchy-bluetooth-power index 7f30533beba6..2168756f7aa3 100755 --- a/bin/omarchy-bluetooth-power +++ b/bin/omarchy-bluetooth-power @@ -2,7 +2,7 @@ # omarchy:summary=Turn Bluetooth on or off, remembered across reboots # omarchy:group=bluetooth -# omarchy:args= +# omarchy:args= # BlueZ never persists an adapter's Powered property, so turning Bluetooth off # through bluetoothctl lasts only until the next boot. The rfkill soft block does @@ -15,9 +15,24 @@ # at its stock default and bluetoothd powers the adapter up by itself. Every # Omarchy path that turns Bluetooth on or off goes through here, because a plain # `bluetoothctl power on` fails outright while the block is set. +# +# Desired on/off is also mirrored under XDG state and re-applied after login +# (#13342): some boots restore rfkill after bluetoothd has already AutoEnabled, +# so a late restore keeps the user's off preference. POWER_WAIT_SECONDS=${OMARCHY_BLUETOOTH_POWER_WAIT_SECONDS:-2} +desired_state_file() { + printf '%s\n' "${OMARCHY_BLUETOOTH_STATE_FILE:-${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/bluetooth-power}" +} + +remember() { + local state=$1 file + file=$(desired_state_file) + mkdir -p -- "$(dirname -- "$file")" + printf '%s\n' "$state" >"$file" +} + controllers() { timeout 2s bluetoothctl list 2>/dev/null | awk '{print $2}' } @@ -65,25 +80,42 @@ power_on() { case "${1:-}" in on) + remember on power_on ;; off) # No bluetoothctl power off to go with this: the block already drops the # adapter to Powered: no, and it is the half that survives the reboot. + remember off rfkill block bluetooth ;; toggle) if powered; then + remember off rfkill block bluetooth else + remember on power_on fi ;; is-on) powered ;; + restore) + # Best-effort: missing state file means "leave whatever rfkill restored". + state_file=$(desired_state_file) + [[ -f $state_file ]] || exit 0 + case $(<"$state_file") in + off) + rfkill block bluetooth + ;; + on) + power_on || true + ;; + esac + ;; *) - echo "Usage: omarchy-bluetooth-power " >&2 + echo "Usage: omarchy-bluetooth-power " >&2 exit 1 ;; esac diff --git a/default/systemd/user/omarchy-bluetooth-power-restore.service b/default/systemd/user/omarchy-bluetooth-power-restore.service new file mode 100644 index 000000000000..e39080f23fb0 --- /dev/null +++ b/default/systemd/user/omarchy-bluetooth-power-restore.service @@ -0,0 +1,22 @@ +[Unit] +Description=Restore Omarchy Bluetooth on/off preference +Documentation=man:rfkill(8) +# bluetoothd may AutoEnable before systemd-rfkill finishes restoring a soft +# block across reboot (#13342). Re-apply the user's last omarchy-bluetooth-power +# choice once the session and bluetooth stack are up. +After=bluetooth.service +# Wait for the compositor, not just graphical-session-pre.target: a queued +# SW_RFKILL_ALL event is flushed when Hyprland first syncs its input devices, +# and the kernel's rfkill-input then unblocks every radio. Restoring before that +# lets the unblock-all turn Bluetooth back on over a saved "off". +After=graphical-session.target +PartOf=graphical-session.target +ConditionPathIsDirectory=/sys/class/bluetooth + +[Service] +Type=oneshot +ExecStart=/usr/bin/omarchy-bluetooth-power restore +RemainAfterExit=yes + +[Install] +WantedBy=graphical-session.target diff --git a/install/user/first-run/enable-user-units.sh b/install/user/first-run/enable-user-units.sh index 0f73262282ec..6e307dbce0b3 100755 --- a/install/user/first-run/enable-user-units.sh +++ b/install/user/first-run/enable-user-units.sh @@ -13,6 +13,14 @@ set -euo pipefail omarchy-input-method configure --defaults || echo "Input method defaults deferred; run omarchy setup input after login." >&2 +# Install then enable the Bluetooth power restore unit. Packaging may also +# ship it under /usr/lib/systemd/user/; the user copy keeps existing installs +# working before that lands, and wins over the package unit when both exist. +unit_src="${OMARCHY_PATH:?}/default/systemd/user/omarchy-bluetooth-power-restore.service" +unit_dst="$HOME/.config/systemd/user/omarchy-bluetooth-power-restore.service" +mkdir -p -- "$(dirname -- "$unit_dst")" +install -Dm644 -- "$unit_src" "$unit_dst" + systemctl --user daemon-reload # One at a time: given a list, systemctl enables none of it when it cannot find @@ -21,6 +29,7 @@ failed=0 for unit in \ bt-agent.service \ owed.service \ + omarchy-bluetooth-power-restore.service \ omarchy-recover-internal-monitor.service \ omarchy-sleep-lock.service \ omarchy-migrate-notify.service \ diff --git a/migrations/1790599214.sh b/migrations/1790599214.sh new file mode 100644 index 000000000000..61e839480fa4 --- /dev/null +++ b/migrations/1790599214.sh @@ -0,0 +1,22 @@ +echo "Restore Bluetooth on/off preference after login" + +# BlueZ AutoEnable can race systemd-rfkill across reboot (#13342). Re-apply the +# last omarchy-bluetooth-power choice once the graphical session is up. + +unit_src="$OMARCHY_PATH/default/systemd/user/omarchy-bluetooth-power-restore.service" +unit_dst="$HOME/.config/systemd/user/omarchy-bluetooth-power-restore.service" + +mkdir -p -- "$(dirname -- "$unit_dst")" +install -Dm644 -- "$unit_src" "$unit_dst" + +systemctl --user daemon-reload >/dev/null 2>&1 || true + +if ! systemctl --user enable omarchy-bluetooth-power-restore.service >/dev/null 2>&1; then + wants_dir="$HOME/.config/systemd/user/graphical-session.target.wants" + mkdir -p "$wants_dir" + ln -sfn "$unit_dst" "$wants_dir/omarchy-bluetooth-power-restore.service" +fi + +if systemctl --user is-active --quiet graphical-session.target; then + systemctl --user start omarchy-bluetooth-power-restore.service >/dev/null 2>&1 || true +fi diff --git a/test/shell.d/bluetooth-test.sh b/test/shell.d/bluetooth-test.sh index b20727e6d9c4..60a31963c919 100644 --- a/test/shell.d/bluetooth-test.sh +++ b/test/shell.d/bluetooth-test.sh @@ -280,3 +280,44 @@ pass "bluetooth counts a secondary controller as on" grep -q 'AutoEnable=false' "$ROOT/install/hardware/bluetooth.sh" && fail "bluetooth install leaves AutoEnable at its default" pass "bluetooth install leaves AutoEnable at its default" + + +# Desired on/off is mirrored under XDG state and re-applied after login (#13342). +state_home="$device_tmp/state" +mkdir -p "$state_home" +export XDG_STATE_HOME="$state_home" + +off_log=$(bluetooth_power yes off) +state_file="$state_home/omarchy/bluetooth-power" +[[ $(<"$state_file") == "off" ]] || fail "bluetooth remembers off preference" "$(cat "$state_file" 2>/dev/null || true)" +pass "bluetooth remembers off preference" + +on_log=$(bluetooth_power no on) +[[ $(<"$state_file") == "on" ]] || fail "bluetooth remembers on preference" "$(cat "$state_file")" +pass "bluetooth remembers on preference" + +: >"$device_tmp/log" +PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" \ + OMARCHY_BLUETOOTH_POWER_WAIT_SECONDS=0 \ + "$ROOT/bin/omarchy-bluetooth-power" restore || + fail "bluetooth restore exits cleanly for on preference" +grep -qx "rfkill unblock bluetooth" "$device_tmp/log" || + fail "bluetooth restore re-applies on" "$(cat "$device_tmp/log")" +pass "bluetooth restore re-applies on" + +printf 'off\n' >"$state_file" +: >"$device_tmp/log" +PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" \ + OMARCHY_BLUETOOTH_POWER_WAIT_SECONDS=0 \ + "$ROOT/bin/omarchy-bluetooth-power" restore || + fail "bluetooth restore exits cleanly for off preference" +grep -qx "rfkill block bluetooth" "$device_tmp/log" || + fail "bluetooth restore re-applies off" "$(cat "$device_tmp/log")" +pass "bluetooth restore re-applies off" + +restore_unit="$ROOT/default/systemd/user/omarchy-bluetooth-power-restore.service" +grep -Fxq 'ExecStart=/usr/bin/omarchy-bluetooth-power restore' "$restore_unit" || + fail "bluetooth power restore unit invokes restore" +grep -Fq 'omarchy-bluetooth-power-restore.service' "$ROOT/install/user/first-run/enable-user-units.sh" || + fail "first-run enables bluetooth power restore" +pass "bluetooth power restore unit is installed and enabled at first-run" diff --git a/test/shell.d/systemd-test.sh b/test/shell.d/systemd-test.sh index 66074c8b3c19..d356ca70c905 100755 --- a/test/shell.d/systemd-test.sh +++ b/test/shell.d/systemd-test.sh @@ -104,3 +104,13 @@ pass "systemd-oomd acts on sustained memory stall" grep -Fx 'systemctl enable systemd-oomd.service' "$ROOT/install/config/enable-services.sh" >/dev/null || fail "new installs ship the oomd drop-ins with the daemon that reads them disabled" pass "new installs enable systemd-oomd" + +bt_restore="$ROOT/default/systemd/user/omarchy-bluetooth-power-restore.service" +[[ -f $bt_restore ]] || fail "bluetooth power restore unit is missing" +grep -Fx 'ExecStart=/usr/bin/omarchy-bluetooth-power restore' "$bt_restore" >/dev/null +grep -Fx 'After=graphical-session.target' "$bt_restore" >/dev/null || + fail "bluetooth power restore runs before the compositor's first input sync, whose rfkill-input unblock-all overrides a saved off" +grep -F 'omarchy-bluetooth-power-restore.service' "$first_run_units" >/dev/null || + fail "first-run does not enable bluetooth power restore" +pass "bluetooth power restore unit is enabled at first-run" +