package main import( "after-lock/evdev" "after-lock/lockscreen" "after-lock/display" "after-lock/atomicflag" "time" "sync" ) const ( initialDelay = 3 loopDelay = 6 keyboardDeviceNode = "/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd" ) func main() { al := newAfterLock() al.start() } type afterLock struct { wg sync.WaitGroup stopFlag *atomicflag.AtomicFlag keypressFlag *atomicflag.AtomicFlag } func newAfterLock() *afterLock { return &afterLock{ stopFlag: atomicflag.New(false), keypressFlag: atomicflag.New(false), } } func (af *afterLock) start() { af.wg.Add(1) go af.detectKeypresses() time.Sleep(initialDelay * time.Second) af.hybernateDisplayLoop() } func (af *afterLock) detectKeypresses() { for { err := evdev.BlockUntilKeypress(keyboardDeviceNode) if err != nil { panic(err) } if af.stopFlag.Get() { af.wg.Done() return } af.keypressFlag.Set(true) } } func (af *afterLock) hybernateDisplayLoop() { for { screenLocked, err := lockscreen.IsActive() if err != nil { panic(err) } if !screenLocked { af.stopFlag.Set(true) af.wg.Wait() return } displayOn, err := display.IsOn() if err != nil { panic(err) } if displayOn { err := display.Suspend() if err != nil { panic(err) } for { time.Sleep(loopDelay * time.Second) if af.keypressFlag.Get() { af.keypressFlag.Set(false) continue } else { break } } } } }