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
11 changes: 11 additions & 0 deletions default/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Enable the ThinkPad Bluetooth hotkey
After=systemd-modules-load.service
ConditionPathExists=/sys/devices/platform/thinkpad_acpi/hotkey_mask

[Service]
Type=oneshot
ExecStart=/bin/bash /usr/local/lib/omarchy/thinkpad-bluetooth-hotkey.sh

[Install]
WantedBy=multi-user.target
17 changes: 17 additions & 0 deletions default/systemd/thinkpad-bluetooth-hotkey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Preserve the model's current mask, enabling only the Bluetooth hotkey.
mask_path=/sys/devices/platform/thinkpad_acpi/hotkey_mask
if [[ -e $mask_path ]]; then
if current=$(cat "$mask_path" 2>/dev/null); then
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if [[ $current =~ ^0[xX][0-9a-fA-F]+$ ]]; then
if ! printf '%#x\n' "$((current | 0x00100000))" | tee "$mask_path" >/dev/null; then
printf 'Warning: failed to write ThinkPad hotkey mask to %s\n' "$mask_path" >&2
fi
else
printf 'Warning: invalid hexadecimal ThinkPad hotkey mask in %s\n' "$mask_path" >&2
fi
else
printf 'Warning: failed to read ThinkPad hotkey mask from %s\n' "$mask_path" >&2
fi
fi
1 change: 1 addition & 0 deletions install/hardware/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ run_logged "$OMARCHY_INSTALL/hardware/apple/fix-t2.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-brcmfmac-supplicant.sh"

run_logged "$OMARCHY_INSTALL/hardware/lenovo/fix-yoga-pro7-bass-speakers.sh"
run_logged "$OMARCHY_INSTALL/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh"

run_logged "$OMARCHY_INSTALL/hardware/fix-bcm43xx.sh"
run_logged "$OMARCHY_INSTALL/hardware/fix-surface-keyboard.sh"
Expand Down
12 changes: 12 additions & 0 deletions install/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Unmask the ThinkPad Bluetooth F10 hotkey (thinkpad_acpi bit 20).

if ! omarchy-hw-match "ThinkPad" && [[ ! -d /sys/module/thinkpad_acpi ]]; then
return 0
fi

# Copy the helper to a root-owned path before using it at boot.
sudo install -Dm644 "$OMARCHY_PATH/default/systemd/thinkpad-bluetooth-hotkey.sh" /usr/local/lib/omarchy/thinkpad-bluetooth-hotkey.sh
sudo install -Dm644 "$OMARCHY_PATH/default/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service" /etc/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service
sudo systemctl daemon-reload
sudo systemctl enable omarchy-thinkpad-bluetooth-hotkey.service
sudo /bin/bash /usr/local/lib/omarchy/thinkpad-bluetooth-hotkey.sh
7 changes: 7 additions & 0 deletions migrations/1790544300.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo "Unmask the ThinkPad Bluetooth F10 hotkey (thinkpad_acpi bit 20)"

if ! omarchy-hw-match "ThinkPad" && [[ ! -d /sys/module/thinkpad_acpi ]]; then
exit 0
fi

source "$OMARCHY_PATH/install/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh"
135 changes: 135 additions & 0 deletions test/shell.d/thinkpad-bluetooth-hotkey-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

leaf=install/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh
migration=migrations/1790544300.sh
helper=default/systemd/thinkpad-bluetooth-hotkey.sh
unit=default/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service
[[ ! -e $ROOT/default/modprobe.d/omarchy-thinkpad-bluetooth-hotkey.conf ]] ||
fail "no model-specific boot mask is shipped"
rg -Fq 'lenovo/fix-thinkpad-bluetooth-hotkey.sh' "$ROOT/install/hardware/all.sh" ||
fail "hardware setup runs the ThinkPad leaf"
for line in 'After=systemd-modules-load.service' 'ConditionPathExists=/sys/devices/platform/thinkpad_acpi/hotkey_mask' 'Type=oneshot' 'ExecStart=/bin/bash /usr/local/lib/omarchy/thinkpad-bluetooth-hotkey.sh' 'WantedBy=multi-user.target'; do
rg -Fxq "$line" "$ROOT/$unit" || fail "boot unit contains $line"
done

test_tmp=$(mktemp -d)
trap 'chmod -R u+rwX "$test_tmp"; rm -rf "$test_tmp"' EXIT
export OMARCHY_PATH="$test_tmp/repo" TEST_LOG="$test_tmp/calls" TEST_MASK="$test_tmp/mask"
export TEST_SYS="$test_tmp/sys" TEST_READ_FAIL=0 TEST_WRITE_FAIL=0 TEST_THINKPAD=1
mkdir -p "$test_tmp/bin" "$TEST_SYS/module/thinkpad_acpi" "$TEST_SYS/devices/platform/thinkpad_acpi"
# Redirect filesystem boundaries; execute the actual scripts and copied helper.
for script in "$leaf" "$migration" "$helper" "$unit"; do
mkdir -p "$OMARCHY_PATH/$(dirname "$script")"
sed -e "s|/sys/|$TEST_SYS/|g" -e "s|/etc/|$test_tmp/etc/|g" \
-e "s|/usr/local/|$test_tmp/usr/local/|g" "$ROOT/$script" >"$OMARCHY_PATH/$script"
done
mkdir -p "$test_tmp/etc/modprobe.d"
printf 'administrator configuration\n' >"$test_tmp/etc/modprobe.d/omarchy-thinkpad-hotkey.conf"

cat >"$test_tmp/bin/sudo" <<'STUB'
#!/bin/bash
printf '%s\n' "$*" >>"$TEST_LOG"
case $1 in
install|/bin/bash) "$@" ;;
systemctl) ;;
*) exit 99 ;;
esac
STUB
cat >"$test_tmp/bin/cat" <<'STUB'
#!/bin/bash
# Simulate root access to sysfs, including a read failure, even in root-run CI.
(( TEST_READ_FAIL == 0 )) || exit 1
/bin/cat "$TEST_MASK"
STUB
cat >"$test_tmp/bin/tee" <<'STUB'
#!/bin/bash
(( TEST_WRITE_FAIL == 0 )) || exit 1
/usr/bin/tee "$TEST_MASK"
STUB
cat >"$test_tmp/bin/omarchy-hw-match" <<'STUB'
#!/bin/bash
[[ $1 == "ThinkPad" ]] && (( TEST_THINKPAD == 1 ))
STUB
chmod +x "$test_tmp/bin/"*
export PATH="$test_tmp/bin:$PATH"
mask_path="$TEST_SYS/devices/platform/thinkpad_acpi/hotkey_mask"
installed_helper="$test_tmp/usr/local/lib/omarchy/thinkpad-bluetooth-hotkey.sh"
installed_unit="$test_tmp/etc/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service"

run_script() {
bash -euo pipefail -c 'source "$1"; echo survived' bash "$OMARCHY_PATH/$1" >"$test_tmp/output"
rg -q '^survived$' "$test_tmp/output" || fail "script completes under strict mode"
cmp "$OMARCHY_PATH/$helper" "$installed_helper" || fail "shared helper is installed"
cmp "$OMARCHY_PATH/$unit" "$installed_unit" || fail "boot unit is installed"
rg -Fxq 'systemctl daemon-reload' "$TEST_LOG" || fail "system units are reloaded"
rg -Fxq 'systemctl enable omarchy-thinkpad-bluetooth-hotkey.service' "$TEST_LOG" || fail "boot unit is enabled"
rg -Fxq "/bin/bash $installed_helper" "$TEST_LOG" || fail "live mask is applied with root privileges"
if rg -q 'modprobe|mkinitcpio' "$TEST_LOG"; then fail "no modprobe mask or initramfs rebuild is requested"; fi
}

for script in "$leaf" "$migration"; do
for mask in 0xff8c7ffb 0x00000005 0x00100005; do
printf '%s\n' "$mask" >"$TEST_MASK"
if [[ -e $mask_path ]]; then chmod 644 "$mask_path"; fi
cp "$TEST_MASK" "$mask_path"
chmod 444 "$mask_path"
: >"$TEST_LOG"
run_script "$script"
expected=$(printf '%#x' "$((mask | 0x00100000))")
[[ $(<"$TEST_MASK") == "$expected" ]] || fail "only bit 20 changes for $mask"
run_script "$script"
[[ $(<"$TEST_MASK") == "$expected" ]] || fail "repeated execution preserves mask"

# Simulate a reboot resetting the mask, then run the unit's actual ExecStart.
printf '%s\n' "$mask" >"$TEST_MASK"
boot_command=$(sed -n 's/^ExecStart=//p' "$installed_unit")
bash -euo pipefail -c "$boot_command"
[[ $(<"$TEST_MASK") == "$expected" ]] || fail "boot restores only bit 20 for $mask"
done

for failure in read write; do
export TEST_READ_FAIL=0 TEST_WRITE_FAIL=0
if [[ $failure == "read" ]]; then export TEST_READ_FAIL=1; else export TEST_WRITE_FAIL=1; fi
printf '0x5\n' >"$TEST_MASK"
: >"$TEST_LOG"
run_script "$script" 2>"$test_tmp/stderr"
rg -Fq "Warning: failed to $failure ThinkPad hotkey mask" "$test_tmp/stderr" || fail "failed $failure warns during setup"
rg -Fq "$mask_path" "$test_tmp/stderr" || fail "failed $failure warning names mask path during setup"
bash -euo pipefail "$installed_helper" 2>"$test_tmp/stderr" || fail "failed $failure exits successfully"
rg -Fq "Warning: failed to $failure ThinkPad hotkey mask" "$test_tmp/stderr" || fail "failed $failure warns"
rg -Fq "$mask_path" "$test_tmp/stderr" || fail "failed $failure warning names mask path"
[[ $(<"$TEST_MASK") == "0x5" ]] || fail "failed $failure preserves mask"
done
export TEST_READ_FAIL=0 TEST_WRITE_FAIL=0

printf '0xinvalid\n' >"$TEST_MASK"
bash -euo pipefail "$installed_helper" 2>"$test_tmp/stderr" || fail "invalid hexadecimal mask exits successfully"
rg -Fq 'Warning: invalid hexadecimal ThinkPad hotkey mask' "$test_tmp/stderr" || fail "invalid hexadecimal mask warns"
rg -Fq "$mask_path" "$test_tmp/stderr" || fail "invalid hexadecimal warning names mask path"
[[ $(<"$TEST_MASK") == "0xinvalid" ]] || fail "invalid hexadecimal mask is preserved"
printf '0x5\n' >"$TEST_MASK"

rm -f "$mask_path"
: >"$TEST_LOG"
run_script "$script"
bash -euo pipefail "$installed_helper"
[[ $(<"$TEST_MASK") == "0x5" ]] || fail "missing sysfs preserves mask"

rmdir "$TEST_SYS/module/thinkpad_acpi"
export TEST_THINKPAD=0
: >"$TEST_LOG"
bash -euo pipefail -c 'source "$1"' bash "$OMARCHY_PATH/$script" >"$test_tmp/output"
[[ ! -s $TEST_LOG ]] || fail "unrelated hardware is untouched"
export TEST_THINKPAD=1
mkdir -p "$TEST_SYS/module/thinkpad_acpi"

[[ $(<"$test_tmp/etc/modprobe.d/omarchy-thinkpad-hotkey.conf") == "administrator configuration" ]] ||
fail "unrelated configuration is preserved"
[[ ! -e $test_tmp/etc/modprobe.d/omarchy-thinkpad-bluetooth-hotkey.conf ]] ||
fail "no modprobe configuration is installed"
pass "$script installs persistence and safely applies the ThinkPad mask under strict mode"
done