Skip to content
Open
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
36 changes: 34 additions & 2 deletions bin/omarchy-bluetooth-power
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# omarchy:summary=Turn Bluetooth on or off, remembered across reboots
# omarchy:group=bluetooth
# omarchy:args=<on|off|toggle|is-on>
# omarchy:args=<on|off|toggle|is-on|restore>

# 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
Expand All @@ -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}'
}
Expand Down Expand Up @@ -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 <on|off|toggle|is-on>" >&2
echo "Usage: omarchy-bluetooth-power <on|off|toggle|is-on|restore>" >&2
exit 1
;;
esac
22 changes: 22 additions & 0 deletions default/systemd/user/omarchy-bluetooth-power-restore.service
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions install/user/first-run/enable-user-units.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down
22 changes: 22 additions & 0 deletions migrations/1790599214.sh
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions test/shell.d/bluetooth-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment on lines +286 to +288

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Tests overwrite the saved choice

XDG_STATE_HOME is set only after earlier calls to omarchy-bluetooth-power. Those calls now save preferences, so running this test with the normal environment overwrites ~/.local/state/omarchy/bluetooth-power. The last earlier toggle saves off, which the new service applies at the next login.

Move the temporary state setup before the first helper call, and isolate OMARCHY_BLUETOOTH_STATE_FILE too.


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"
10 changes: 10 additions & 0 deletions test/shell.d/systemd-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"