Skip to content
Closed
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
12 changes: 12 additions & 0 deletions bin/omarchy-hw-laptop-closed
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
# omarchy:summary=Returns true when the laptop lid is closed
# omarchy:hidden=true

# logind reads the lid through evdev's SW_LID, so it answers on hardware that
# exposes no ACPI lid button at all. Apple Silicon has no /proc/acpi, where the
# glob below matches nothing and reports the lid open forever.
lid=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 \
org.freedesktop.login1.Manager LidClosed 2>/dev/null)

if [[ $lid == "b true" ]]; then
exit 0
elif [[ $lid == "b false" ]]; then
exit 1
fi

for state in /proc/acpi/button/lid/*/state; do
[[ -r $state ]] || continue
[[ $(< "$state") == *"closed"* ]] && exit 0
Expand Down
46 changes: 46 additions & 0 deletions test/shell.d/hw-laptop-closed-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

set -euo pipefail

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

hw_laptop_closed="$ROOT/bin/omarchy-hw-laptop-closed"
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT

# logind is the only lid source every laptop answers from, so each scenario pins
# its reply and checks the exit code the callers branch on.
stub_logind() {
mock_bin="$tmpdir/$1"
mkdir -p "$mock_bin"

cat >"$mock_bin/busctl" <<SH
#!/bin/bash
printf '%s\n' "$2"
SH
chmod +x "$mock_bin/busctl"
}

lid_status() {
local status=0

PATH="$mock_bin:$PATH" "$hw_laptop_closed" || status=$?
printf '%s\n' "$status"
}

stub_logind closed "b true"
status=$(lid_status)
(( status == 0 )) || fail "a lid logind calls closed is closed" "exit: $status"
pass "a lid logind calls closed is closed"

stub_logind open "b false"
status=$(lid_status)
(( status == 1 )) || fail "a lid logind calls open is open" "exit: $status"
pass "a lid logind calls open is open"

# Nothing else in the tree can stand in for logind here, so a machine whose
# logind cannot answer has to keep reaching the ACPI button this command has
# always read.
grep -F '/proc/acpi/button/lid/*/state' "$hw_laptop_closed" >/dev/null ||
fail "the ACPI lid button stays as the fallback"
pass "the ACPI lid button stays as the fallback"
Comment on lines +41 to +46