diff --git a/bin/omarchy-hw-facetimehd b/bin/omarchy-hw-facetimehd new file mode 100755 index 000000000000..d3051b5ab0a4 --- /dev/null +++ b/bin/omarchy-hw-facetimehd @@ -0,0 +1,6 @@ +#!/bin/bash + +# omarchy:summary=Detect a Broadcom FaceTime HD PCIe webcam (14e4:1570). + +# grep -q would SIGPIPE a chatty lspci under pipefail and look like "no camera". +lspci -nn | grep "14e4:1570" >/dev/null diff --git a/bin/omarchy-system-sleep-facetimehd b/bin/omarchy-system-sleep-facetimehd new file mode 100755 index 000000000000..8cf8678c6677 --- /dev/null +++ b/bin/omarchy-system-sleep-facetimehd @@ -0,0 +1,113 @@ +#!/bin/bash + +# omarchy:summary=Unload the FaceTime HD camera driver for hibernation, refusing to hibernate while the camera is in use +# omarchy:args= +# omarchy:group=system +# omarchy:hidden=true + +# facetimehd deadlocks the kernel's hibernation freeze, so the hibernating sleep units run this before they start and +# after they stop. The driver cannot be unloaded while an app has the camera open, and failing here is what cancels the +# sleep: systemd ignores a failing system-sleep hook. + +set -uo pipefail + +module=${OMARCHY_FACETIMEHD_MODULE:-/sys/module/facetimehd} +proc=${OMARCHY_FACETIMEHD_PROC:-/proc} +pci=${OMARCHY_FACETIMEHD_PCI:-/sys/bus/pci/devices} +# post runs within the sleep unit's stop timeout, so it waits briefly and leaves anything longer to the fetcher. +probe_wait=${OMARCHY_FACETIMEHD_PROBE_WAIT:-2} +lock_wait=${OMARCHY_FACETIMEHD_LOCK_WAIT:-10} + +# Shared with facetimehd-firmware-fetcher, which loads the driver once it has the firmware. Under the lock, it never +# loads the driver while a hibernating sleep is in progress, and leaves its activation pending for post to resume. +run=${OMARCHY_FACETIMEHD_RUN:-/run/lock} +lock=$run/facetimehd.lock +sleeping=$run/facetimehd.sleeping +unloaded=$run/facetimehd.unloaded +activate=$run/facetimehd-firmware-fetcher.activate + +camera_users() { + local fd pid + for fd in "$proc"/[0-9]*/fd/*; do + [[ $(readlink "$fd" 2>/dev/null) == /dev/video* ]] || continue + pid=${fd#"$proc"/} + cat "$proc/${pid%%/*}/comm" 2>/dev/null + done | sort -u | paste -sd, - | sed 's/,/, /g' +} + +# The module loading proves nothing about the camera, since the kernel keeps a module whose device failed to probe: it +# works once the 1570's PCI function is bound to facetimehd and has registered a video device. +camera_ready() { + local device found= + for device in "$pci"/*; do + [[ $(<"$device/vendor") == 0x14e4 && $(<"$device/device") == 0x1570 ]] || continue + [[ $(readlink "$device/driver") == */facetimehd ]] && compgen -G "$device/video4linux/video*" >/dev/null || return 1 + found=1 + done 2>/dev/null + [[ -n $found ]] +} + +restore_driver() { + local _ + modprobe facetimehd || return 1 + for _ in $(seq "$probe_wait"); do + camera_ready && return 0 + sleep 1 + done + camera_ready +} + +# The menu's systemctl hibernate shows nothing when the sleep fails, so each person at a seat is told why. +notify_seats() { + local uid user + while read -r uid user; do + runuser -u "$user" -- env XDG_RUNTIME_DIR="/run/user/$uid" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$uid/bus" \ + omarchy-notification-send -u critical "$1" "$2" >/dev/null 2>&1 || true + done < <(loginctl list-sessions --no-legend 2>/dev/null | awk '$4 ~ /^seat/ { print $2, $3 }' | sort -u) +} + +case ${1:-} in + pre) + exec 8>>"$lock" + # A fetch is loading the driver; hibernating now could catch it loaded. + flock -w 60 8 || { echo "Not hibernating: the camera driver is being loaded" >&2; exit 1; } + rm -f "$unloaded" + touch "$sleeping" + [[ -d $module ]] || exit 0 + if modprobe -r facetimehd; then + touch "$unloaded" + else + rm -f "$sleeping" + users=$(camera_users) + echo "Not hibernating: the FaceTime HD camera is in use${users:+ by $users}, and its driver hangs hibernation" >&2 + notify_seats "Hibernation cancelled" "Close ${users:-the app using the camera}, then try again. The camera's driver hangs hibernation while it is in use." + exit 1 + fi + ;; + post) + exec 8>>"$lock" + if flock -w "$lock_wait" 8; then + # Only a driver pre unloaded comes back, so one unloaded on purpose stays out. A camera that does not come back + # becomes the fetcher's pending activation, which it retries until the camera works. + if [[ -e $unloaded ]] && ! restore_driver; then + echo "The FaceTime HD camera did not come back after the sleep; facetimehd-firmware-fetcher retries it" >&2 + touch "$activate" + fi + rm -f "$unloaded" "$sleeping" + flock -u 8 + else + # The fetcher is loading the driver; the markers are left for the next sleep to reset and the fetcher to see stale. + echo "The camera driver is busy; facetimehd-firmware-fetcher brings the camera back" >&2 + [[ ! -e $unloaded ]] || touch "$activate" + fi + # An activation the fetcher held back during the sleep, or the load above, resumes now rather than at the next boot. + if [[ -e $activate ]]; then + systemctl start --no-block facetimehd-firmware-fetcher.service 2>/dev/null || + echo "facetimehd-firmware-fetcher is not installed; the camera driver loads at the next boot" >&2 + fi + ;; + *) + echo "Usage: omarchy-system-sleep-facetimehd " >&2 + exit 1 + ;; +esac diff --git a/default/systemd/system/omarchy-facetimehd-hibernate.conf b/default/systemd/system/omarchy-facetimehd-hibernate.conf new file mode 100644 index 000000000000..065278738016 --- /dev/null +++ b/default/systemd/system/omarchy-facetimehd-hibernate.conf @@ -0,0 +1,7 @@ +# Installed for systemd-hibernate, systemd-hybrid-sleep and systemd-suspend-then-hibernate on Macs with the FaceTime HD +# camera: its driver hangs the hibernation freeze, so it is unloaded first, and the sleep fails while it is in use. +[Service] +ExecStartPre=/usr/bin/omarchy-system-sleep-facetimehd pre +ExecStopPost=/usr/bin/omarchy-system-sleep-facetimehd post +# The sleep units allow post 5 s; it waits up to 10 s for the driver lock and a moment for the camera. +TimeoutStopSec=30s diff --git a/install/hardware/all.sh b/install/hardware/all.sh index 12add110c51f..71230e30ef67 100644 --- a/install/hardware/all.sh +++ b/install/hardware/all.sh @@ -35,6 +35,7 @@ run_logged "$OMARCHY_INSTALL/hardware/apple/fix-spi-keyboard.sh" run_logged "$OMARCHY_INSTALL/hardware/apple/fix-suspend-nvme.sh" run_logged "$OMARCHY_INSTALL/hardware/apple/fix-t2.sh" run_logged "$OMARCHY_INSTALL/hardware/apple/fix-brcmfmac-supplicant.sh" +run_logged "$OMARCHY_INSTALL/hardware/apple/fix-facetimehd.sh" run_logged "$OMARCHY_INSTALL/hardware/lenovo/fix-yoga-pro7-bass-speakers.sh" diff --git a/install/hardware/apple/fix-facetimehd.sh b/install/hardware/apple/fix-facetimehd.sh new file mode 100644 index 000000000000..12677fb237da --- /dev/null +++ b/install/hardware/apple/fix-facetimehd.sh @@ -0,0 +1,31 @@ +# Broadcom 720p FaceTime HD (PCI 14e4:1570) on Intel Macs. There is no in-tree driver, and Apple's license forbids +# redistributing the camera's firmware, so facetimehd-firmware-fetcher downloads it from Apple on the Mac itself. + +if omarchy-hw-facetimehd; then + echo "Detected FaceTime HD camera" + + # pacman -T resolves provides, so a Mac already running an AUR driver or firmware keeps it rather than conflicting. + packages=() + pacman -T facetimehd-dkms >/dev/null || pacman -T bcwc-pcie >/dev/null || + pacman -T bcwc-pcie-dkms >/dev/null || packages+=(facetimehd-dkms) + pacman -T facetimehd-firmware >/dev/null || pacman -T bcwc-pcie-firmware >/dev/null || + packages+=(facetimehd-firmware-fetcher) + (( ${#packages[@]} == 0 )) || omarchy-pkg-add "${packages[@]}" + + # An install with no network fetches on the first boot that has one. + if pacman -Q facetimehd-firmware-fetcher &>/dev/null; then + sudo systemctl enable facetimehd-firmware-fetcher.service + + # An image's first boot runs this after boot queued its units, so the fetch starts now rather than at the next boot. + if [[ ${OMARCHY_IMAGE_DEFERRED_HARDWARE:-} == "1" ]]; then + sudo systemctl start --no-block facetimehd-firmware-fetcher.service || true + fi + fi + + # Whichever package provides the driver, it hangs hibernation, so every hibernating sleep is guarded. + for unit in systemd-hibernate systemd-hybrid-sleep systemd-suspend-then-hibernate; do + sudo install -Dm644 "$OMARCHY_PATH/default/systemd/system/omarchy-facetimehd-hibernate.conf" \ + "/etc/systemd/system/$unit.service.d/omarchy-facetimehd.conf" + done + sudo systemctl daemon-reload +fi diff --git a/install/omarchy-other.packages b/install/omarchy-other.packages index 4a8b128ff95f..fea1e7e044a4 100644 --- a/install/omarchy-other.packages +++ b/install/omarchy-other.packages @@ -71,5 +71,9 @@ linux-t2 linux-t2-headers t2fanrd +# FaceTime HD camera on Intel Macs +facetimehd-dkms +facetimehd-firmware-fetcher + # Framework 16 qmk-hid diff --git a/manual/44-mac-support.md b/manual/44-mac-support.md index e86fdd064d8f..fc94179dfda8 100644 --- a/manual/44-mac-support.md +++ b/manual/44-mac-support.md @@ -35,12 +35,18 @@ It is necessary to disable Apple's Secure Boot in order to boot the bootable USB 3. Select the orange EFI Boot device 4. Proceed with the [install as normal](02-getting-started.md) -The installer detects Mac hardware and applies the needed fixes automatically: Broadcom Wi-Fi drivers and firmware, the SPI keyboard driver on the MacBook models that need it, and an NVMe suspend fix for those same models. +The installer detects Mac hardware and applies the needed fixes automatically: Broadcom Wi-Fi drivers and firmware, the FaceTime HD camera driver on machines with a Broadcom 1570 (its firmware is downloaded from Apple once the Mac is online, since the installer works offline), the SPI keyboard driver on the MacBook models that need it, and an NVMe suspend fix for those same models. ### Known Limitations Members of the community are constantly working on solutions to these challenges so if these are problematic for you, join #omarchy-on-other in our [Discord](https://discord.gg/tXFUdasqhY) and see if there's any up-to-date methods for resolving these. +#### FaceTime HD Camera + +The camera driver hangs the machine if it is loaded when the Mac hibernates, so it is unloaded first. It cannot be unloaded while an app has the camera open, so hibernation is cancelled with a notification naming the app: close it and hibernate again. That includes hibernating automatically on a critical battery, and suspend-then-hibernate, which is refused before it suspends. Ordinary suspend is refused by the driver itself while the camera is open, without a notification. + +The camera's firmware is downloaded from Apple, so the camera works only once the Mac has been online. The download starts at boot and is retried every 15 minutes while the network is the problem; if Apple's file has changed, it stops and waits for an Omarchy update. If the camera does not appear, `journalctl -u facetimehd-firmware-fetcher` says why, `sudo systemctl restart facetimehd-firmware-fetcher` tries again, and a reboot loads the driver if the download finished but the camera still is not there. + #### Devices with T1 Chip The Apple T1 chip was introduced in late 2016 and used exclusively in the first-generation MacBook Pro models with Touch Bar. diff --git a/migrations/1795284903.sh b/migrations/1795284903.sh new file mode 100644 index 000000000000..cf4f595b15ca --- /dev/null +++ b/migrations/1795284903.sh @@ -0,0 +1,15 @@ +echo "Install the FaceTime HD camera driver on Intel Macs that have one" + +# Fresh installs get this from install/hardware/apple/fix-facetimehd.sh. Existing +# Macs only pick it up on omarchy update via this migration. +if omarchy-hw-facetimehd; then + source "$OMARCHY_PATH/install/hardware/apple/fix-facetimehd.sh" + + # The module only loads at boot, so the camera stays missing until then. + (( ${#packages[@]} == 0 )) || omarchy-state set reboot-required + + # The leaf only enables the fetch for the next boot; on a running system it can start now. + if pacman -Q facetimehd-firmware-fetcher &>/dev/null; then + sudo systemctl start --no-block facetimehd-firmware-fetcher.service + fi +fi diff --git a/test/shell.d/facetimehd-test.sh b/test/shell.d/facetimehd-test.sh new file mode 100755 index 000000000000..2a1edba155ed --- /dev/null +++ b/test/shell.d/facetimehd-test.sh @@ -0,0 +1,292 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +leaf="$ROOT/install/hardware/apple/fix-facetimehd.sh" +hw="$ROOT/bin/omarchy-hw-facetimehd" +all="$ROOT/install/hardware/all.sh" +manual="$ROOT/manual/44-mac-support.md" +other_packages="$ROOT/install/omarchy-other.packages" +guard="$ROOT/bin/omarchy-system-sleep-facetimehd" +dropin="$ROOT/default/systemd/system/omarchy-facetimehd-hibernate.conf" + +grep -q 'apple/fix-facetimehd.sh' "$all" || + fail "the FaceTime HD fix runs during hardware setup" +grep -q 'omarchy-hw-facetimehd' "$leaf" || + fail "the install leaf keys off the FaceTime HD detector" +grep -q 'facetimehd-dkms' "$leaf" || + fail "the install leaf installs facetimehd-dkms" +grep -qx 'facetimehd-dkms' "$other_packages" || + fail "the ISO caches facetimehd-dkms" +grep -qx 'facetimehd-firmware-fetcher' "$other_packages" || + fail "the ISO caches facetimehd-firmware-fetcher" +! grep -qxE 'facetimehd-(firmware|data)' "$other_packages" || + fail "the ISO carries no Apple firmware, which may not be redistributed" +grep -q '14e4:1570' "$hw" || + fail "the detector matches the Broadcom 1570 PCI ID" +grep -q 'FaceTime HD' "$manual" || + fail "Mac support documents the FaceTime HD camera" +{ grep -Fxq 'ExecStartPre=/usr/bin/omarchy-system-sleep-facetimehd pre' "$dropin" && + grep -Fxq 'ExecStopPost=/usr/bin/omarchy-system-sleep-facetimehd post' "$dropin" && + grep -Fxq 'TimeoutStopSec=30s' "$dropin"; } || + fail "the hibernation drop-in runs the guard before the sleep and after it" +pass "the FaceTime HD fix is wired into hardware setup" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +stub_bin="$test_tmp/bin" +calls="$test_tmp/calls.log" +mkdir -p "$stub_bin" +: >"$calls" + +cat >"$stub_bin/lspci" <<'SH' +#!/bin/bash + +# Chatty like real lspci: keep writing well past the pipe buffer after the +# match, so a grep -q consumer would kill this stub with SIGPIPE and pipefail +# would read that as "no such hardware" (#6608). +if (( ${FACETIMEHD_HARDWARE:-0} == 1 )); then + echo '02:00.0 Multimedia controller [0480]: Broadcom Inc. 720p FaceTime HD Camera [14e4:1570]' +fi +for _ in {1..4096}; do + echo '00:00.0 Host bridge [0600]: Filler Device [ffff:0000]' +done +SH + +cat >"$stub_bin/omarchy-pkg-add" <<'SH' +#!/bin/bash + +printf 'omarchy-pkg-add' >>"$TEST_LOG" +printf '\t%s' "$@" >>"$TEST_LOG" +printf '\n' >>"$TEST_LOG" +printf '%s\n' "$@" >>"$TEST_INSTALLED" +SH + +# -T answers for what is installed or named in FACETIMEHD_PROVIDED, as an AUR package providing it would be. +cat >"$stub_bin/pacman" <<'SH' +#!/bin/bash + +satisfied() { + grep -qxF -- "$1" "$TEST_INSTALLED" || [[ " ${FACETIMEHD_PROVIDED:-} " == *" $1 "* ]] +} + +case "$1" in + -T) for dep in "${@:2}"; do satisfied "$dep" || { echo "$dep"; exit 127; }; done ;; + -Q) grep -qxF -- "$2" "$TEST_INSTALLED" ;; + *) exit 2 ;; +esac +SH + +cat >"$stub_bin/systemctl" <<'SH' +#!/bin/bash + +printf 'systemctl' >>"$TEST_LOG" +printf '\t%s' "$@" >>"$TEST_LOG" +printf '\n' >>"$TEST_LOG" +SH + +# Records the privileged call without making it, so nothing reaches this machine's /etc or systemd. +cat >"$stub_bin/sudo" <<'SH' +#!/bin/bash + +printf 'sudo' >>"$TEST_LOG" +printf '\t%s' "$@" >>"$TEST_LOG" +printf '\n' >>"$TEST_LOG" +SH + +install -m0755 "$hw" "$stub_bin/omarchy-hw-facetimehd" +chmod +x "$stub_bin"/* + +# Detector: present +FACETIMEHD_HARDWARE=1 PATH="$stub_bin:$PATH" "$stub_bin/omarchy-hw-facetimehd" +pass "detector matches PCI 14e4:1570" + +# Detector: absent +if FACETIMEHD_HARDWARE=0 PATH="$stub_bin:$PATH" "$stub_bin/omarchy-hw-facetimehd"; then + fail "detector ignores machines without a FaceTime HD camera" +fi +pass "detector skips machines without a FaceTime HD camera" + +installed="$test_tmp/installed" + +run_leaf() { + local hardware="$1" provided="${2:-}" + : >"$calls" + : >"$installed" + FACETIMEHD_HARDWARE="$hardware" FACETIMEHD_PROVIDED="$provided" PATH="$stub_bin:$PATH" TEST_LOG="$calls" \ + TEST_INSTALLED="$installed" OMARCHY_PATH="$ROOT" bash -euo pipefail -c 'source "$1"' _ "$leaf" +} + +fetcher_enabled=$'sudo\tsystemctl\tenable\tfacetimehd-firmware-fetcher.service' + +guarded() { + local unit + for unit in systemd-hibernate systemd-hybrid-sleep systemd-suspend-then-hibernate; do + grep -Fxq "sudo"$'\t'"install"$'\t'"-Dm644"$'\t'"$dropin"$'\t'"/etc/systemd/system/$unit.service.d/omarchy-facetimehd.conf" "$calls" || + return 1 + done + grep -Fxq $'sudo\tsystemctl\tdaemon-reload' "$calls" +} + +run_leaf 1 +grep -Fxq $'omarchy-pkg-add\tfacetimehd-dkms\tfacetimehd-firmware-fetcher' "$calls" || + fail "leaf installs the DKMS module and the firmware fetcher" "$(cat "$calls")" +grep -Fxq "$fetcher_enabled" "$calls" || + fail "leaf enables the fetch for a first boot with a network" "$(cat "$calls")" +guarded || fail "leaf guards every hibernating sleep" "$(cat "$calls")" +! grep -q $'sudo\tsystemctl\tstart' "$calls" || + fail "an install leaves the fetch to the boot that follows it" "$(cat "$calls")" +pass "leaf installs the DKMS module and the firmware fetcher, enables the fetch, and guards hibernation" + +OMARCHY_IMAGE_DEFERRED_HARDWARE=1 run_leaf 1 +grep -Fxq $'sudo\tsystemctl\tstart\t--no-block\tfacetimehd-firmware-fetcher.service' "$calls" || + fail "an image's first boot starts the fetch it enabled" "$(cat "$calls")" +pass "an image's first boot starts the fetch it enabled" + +run_leaf 1 facetimehd-dkms +grep -Fxq $'omarchy-pkg-add\tfacetimehd-firmware-fetcher' "$calls" || + fail "leaf keeps a driver the AUR already provides" "$(cat "$calls")" +pass "leaf keeps a driver the AUR already provides" + +run_leaf 1 bcwc-pcie +grep -Fxq $'omarchy-pkg-add\tfacetimehd-firmware-fetcher' "$calls" || + fail "leaf keeps the legacy bcwc-pcie driver" "$(cat "$calls")" +pass "leaf keeps the legacy bcwc-pcie driver" + +run_leaf 1 facetimehd-data +grep -Fxq $'omarchy-pkg-add\tfacetimehd-dkms\tfacetimehd-firmware-fetcher' "$calls" || + fail "calibration from the AUR does not stand in for the firmware" "$(cat "$calls")" +pass "calibration from the AUR does not stand in for the firmware" + +run_leaf 1 "facetimehd-dkms facetimehd-firmware" +! grep -q -e omarchy-pkg-add -e "$fetcher_enabled" "$calls" || + fail "leaf installs nothing where the driver and firmware are already provided" "$(cat "$calls")" +guarded || fail "a Mac keeping its AUR driver still has hibernation guarded" "$(cat "$calls")" +pass "leaf installs nothing where the driver and firmware are already provided, and still guards hibernation" + +run_leaf 0 +[[ ! -s $calls ]] || fail "non-matching hardware is a no-op" "$(cat "$calls")" +pass "leaf is a no-op on machines without a FaceTime HD camera" + +# The guard, run as the hibernating sleep units run it, against a stand-in module, /proc and logind. +guard_root="$test_tmp/guard" +mkdir -p "$guard_root/proc/4242/fd" "$guard_root/module" +echo chromium >"$guard_root/proc/4242/comm" +ln -s /dev/video0 "$guard_root/proc/4242/fd/7" + +cat >"$stub_bin/modprobe" <<'SH' +#!/bin/bash + +printf 'modprobe' >>"$TEST_LOG" +printf '\t%s' "$@" >>"$TEST_LOG" +printf '\n' >>"$TEST_LOG" +if [[ $1 == -r ]]; then [[ -z ${MODULE_BUSY:-} ]]; else [[ -z ${MODULE_LOAD_FAILS:-} ]]; fi +SH + +cat >"$stub_bin/loginctl" <<'SH' +#!/bin/bash + +echo '2 1000 mac seat0 1234 user tty1 no -' +echo '5 1000 mac - 9876 manager - no -' +SH + +cat >"$stub_bin/runuser" <<'SH' +#!/bin/bash + +printf 'runuser' >>"$TEST_LOG" +printf '\t%s' "$@" >>"$TEST_LOG" +printf '\n' >>"$TEST_LOG" +SH +chmod +x "$stub_bin"/* + +run_guard() { + : >"$calls" + PATH="$stub_bin:$PATH" TEST_LOG="$calls" OMARCHY_FACETIMEHD_MODULE="$guard_root/$1" \ + OMARCHY_FACETIMEHD_RUN="$guard_root/run" OMARCHY_FACETIMEHD_PROC="$guard_root/proc" \ + OMARCHY_FACETIMEHD_PCI="$guard_root/pci" OMARCHY_FACETIMEHD_PROBE_WAIT=0 \ + MODULE_BUSY="${3:-}" MODULE_LOAD_FAILS="${4:-}" "$guard" "$2" 2>"$test_tmp/guard.err" +} +mkdir -p "$guard_root/run" + +# A camera the driver has bound and registered, as the kernel shows one. +camera="$guard_root/pci/0000:02:00.0" +mkdir -p "$camera/video4linux/video0" "$guard_root/drivers/facetimehd" +echo 0x14e4 >"$camera/vendor" +echo 0x1570 >"$camera/device" +ln -s "$guard_root/drivers/facetimehd" "$camera/driver" +sleeping="$guard_root/run/facetimehd.sleeping" +unloaded="$guard_root/run/facetimehd.unloaded" +activate="$guard_root/run/facetimehd-firmware-fetcher.activate" +fetch_started=$'systemctl\tstart\t--no-block\tfacetimehd-firmware-fetcher.service' + +run_guard absent pre || fail "the guard lets a Mac without the driver loaded hibernate" +{ [[ -e $sleeping && ! -e $unloaded ]] && ! grep -q modprobe "$calls"; } || + fail "the guard marks the sleep in progress and leaves an unloaded driver alone" "$(cat "$calls")" +run_guard absent post +{ [[ ! -e $sleeping ]] && ! grep -q modprobe "$calls"; } || + fail "waking loads nothing that was not unloaded or asked for" "$(cat "$calls")" +pass "the guard lets a Mac without the driver loaded hibernate" + +# A fetch finishing mid-sleep leaves its activation pending rather than loading the driver; waking resumes it. +run_guard absent pre +touch "$activate" +run_guard absent post +{ grep -Fxq "$fetch_started" "$calls" && ! grep -q modprobe "$calls" && [[ ! -e $sleeping ]]; } || + fail "waking resumes an activation the fetch held back" "$(cat "$calls")" +rm -f "$activate" +pass "waking resumes an activation the fetch held back during the sleep" + +run_guard module pre || fail "the guard unloads an idle driver before hibernating" "$(cat "$test_tmp/guard.err")" +{ grep -Fxq $'modprobe\t-r\tfacetimehd' "$calls" && [[ -e $unloaded && -e $sleeping ]]; } || + fail "the guard unloads an idle driver before hibernating" "$(cat "$calls")" +run_guard module post +{ grep -Fxq $'modprobe\tfacetimehd' "$calls" && [[ ! -e $unloaded && ! -e $sleeping ]]; } || + fail "the guard loads the driver again after resuming" "$(cat "$calls")" +pass "the guard unloads an idle driver for hibernation and loads it again after" + +run_guard module post +! grep -q -e modprobe -e systemctl "$calls" || fail "the guard does not load a driver it did not unload" "$(cat "$calls")" +pass "the guard does not load a driver it did not unload" + +# A driver that will not load again after the sleep becomes the fetcher's pending activation, which it retries. +run_guard module pre +run_guard module post "" fails +{ [[ -e $activate && ! -e $unloaded && ! -e $sleeping ]] && grep -Fxq "$fetch_started" "$calls"; } || + fail "a failed reload is handed to the fetcher to retry" "$(cat "$calls")" +rm -f "$activate" +pass "a failed reload is handed to the fetcher to retry" + +# The module loading again is not the camera coming back: one that fails to probe is handed over too. +rmdir "$camera/video4linux/video0" +run_guard module pre +run_guard module post +{ grep -Fxq $'modprobe\tfacetimehd' "$calls" && [[ -e $activate ]] && grep -Fxq "$fetch_started" "$calls"; } || + fail "a camera that does not come back after the sleep is handed to the fetcher" "$(cat "$calls")" +rm -f "$activate" +mkdir "$camera/video4linux/video0" +pass "a camera that does not come back after the sleep is handed to the fetcher" + +# A post that cannot get the lock in time touches no shared state and hands the restore to the fetcher. +run_guard module pre +flock "$guard_root/run/facetimehd.lock" sleep 2 & +holder=$! +sleep 0.5 +OMARCHY_FACETIMEHD_LOCK_WAIT=0 run_guard module post +wait "$holder" +{ ! grep -q modprobe "$calls" && [[ -e $activate && -e $unloaded && -e $sleeping ]] && grep -Fxq "$fetch_started" "$calls"; } || + fail "a post that cannot get the lock leaves the markers and hands the restore to the fetcher" "$(cat "$calls")" +rm -f "$activate" "$unloaded" "$sleeping" +pass "a post that cannot get the lock leaves the markers and hands the restore to the fetcher" + +if run_guard module pre busy; then + fail "the guard cancels hibernation while the camera is in use" +fi +grep -q 'chromium' "$test_tmp/guard.err" || fail "the refusal names the app using the camera" "$(cat "$test_tmp/guard.err")" +grep -q $'^runuser\t-u\tmac\t.*omarchy-notification-send\t-u\tcritical\tHibernation cancelled\tClose chromium' "$calls" || + fail "the refusal notifies the person at the seat" "$(cat "$calls")" +[[ $(grep -c '^runuser' "$calls") == 1 && ! -e $unloaded && ! -e $sleeping ]] || + fail "only the seat session is notified, and no sleep is left marked" "$(cat "$calls")" +pass "the guard cancels hibernation while the camera is in use, and says which app to close"