diff --git a/default/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service b/default/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service new file mode 100644 index 000000000000..1a64177f702f --- /dev/null +++ b/default/systemd/system/omarchy-thinkpad-bluetooth-hotkey.service @@ -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 diff --git a/default/systemd/thinkpad-bluetooth-hotkey.sh b/default/systemd/thinkpad-bluetooth-hotkey.sh new file mode 100644 index 000000000000..95415e6e2b9d --- /dev/null +++ b/default/systemd/thinkpad-bluetooth-hotkey.sh @@ -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 + 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 diff --git a/install/hardware/all.sh b/install/hardware/all.sh index 4f24cdcb5c7c..99d7af297262 100644 --- a/install/hardware/all.sh +++ b/install/hardware/all.sh @@ -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" diff --git a/install/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh b/install/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh new file mode 100644 index 000000000000..21501db4b6d0 --- /dev/null +++ b/install/hardware/lenovo/fix-thinkpad-bluetooth-hotkey.sh @@ -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 diff --git a/migrations/1790544300.sh b/migrations/1790544300.sh new file mode 100644 index 000000000000..07669d03c81e --- /dev/null +++ b/migrations/1790544300.sh @@ -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" diff --git a/test/shell.d/thinkpad-bluetooth-hotkey-test.sh b/test/shell.d/thinkpad-bluetooth-hotkey-test.sh new file mode 100755 index 000000000000..66d22af7c8c3 --- /dev/null +++ b/test/shell.d/thinkpad-bluetooth-hotkey-test.sh @@ -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