Files
magpie/internal/awake/hold_linux.go
T
yetone 56b770ec99 settings: keep awake can keep the display on too (#975, Hu9956: magpie's keep-awake held the system up, but the screen still went black and locked, and their agents record the screen to check their work, which a locked screen stops) — Settings › General's "Keep awake while agents work" row now has three segments, Off / On / Screen on too, saved as keepAwake and the new keepAwakeDisplay (default off, so On and every existing settings.json behave as before). awake.Keep takes a Level (Off, System, Display) in place of a bool; a level changed while held releases and takes the hold anew. On a Mac the display level runs caffeinate -i -d -w <pid> (pmset -g assertions on a caffeinate of the same flags shows PreventUserIdleDisplaySleep beside PreventUserIdleSystemSleep); on Windows SetThreadExecutionState gets ES_DISPLAY_REQUIRED too; on Linux logind's idle inhibitor (systemd-inhibit --what=idle:sleep, unchanged) is all there is, and whether the screen stays on is up to the desktop. keepAwakeDisplay is this computer's own (KeepOwn) and goes in prefsKeep, so another setting's save keeps it. The row's line says the display stays on when it's chosen; it keeps its English in data-en, so a language change re-translates it. zh/ja/de strings added. No TUI or CLI has keep-awake, so none changed. TestKeepDisplay (taken once, taken anew with the display, not again at the same level, back to System, released at Off), TestCaffeinateArgs, TestExecutionState (GOOS=windows test build) and TestKeepAwakeKeptAndOwn cover it; go test ./internal/awake ./internal/settings ./internal/gui ./internal/gateway pass, -race -count=5 on awake. keep-awake.test.cjs now runs en/zh/ja/de at 900px and 440px in Chromium and WebKit (display posts keepAwake+keepAwakeDisplay true, the line changes, another setting's save keeps it, segments fit the row, On and Off clear it, no click moves the page): 32/32 twice; gui-ja, gui-de, i18n-keys and click-scroll pass. go vet (darwin, windows, linux), darwin, linux (nogui) and windows builds pass. Not tried with an agent recording the screen; the reporter is asked to confirm.
2026-10-06 15:33:28 +08:00

30 lines
942 B
Go

package awake
import (
"os/exec"
"syscall"
"github.com/yetone/magpie/internal/proc"
)
// takeHold runs systemd-inhibit holding idle and sleep off around a sleep
// that never ends; the inhibitor goes with the process, which goes with
// magpie (Pdeathsig) should magpie end without letting go. The idle
// inhibitor is all logind has for the display as well, so display asks for
// nothing more: whether the screen stays on is the desktop's to say.
func takeHold(display bool) (func(), error) {
path, err := exec.LookPath("systemd-inhibit")
if err != nil {
return nil, err
}
cmd := proc.Command(path, "--what=idle:sleep", "--who=magpie", "--why=Agents are working through magpie", "--mode=block", "sleep", "infinity")
cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGTERM}
if err := cmd.Start(); err != nil {
return nil, err
}
return func() {
_ = cmd.Process.Signal(syscall.SIGTERM)
_ = cmd.Wait()
}, nil
}