kde-lockscreen-suspend-display/main.go

91 lines
1.4 KiB
Go
Raw Normal View History

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() {
2020-08-18 10:30:55 +00:00
al := newAfterLock()
al.start()
}
2020-08-18 10:30:55 +00:00
type afterLock struct {
wg sync.WaitGroup
stopFlag *atomicflag.AtomicFlag
keypressFlag *atomicflag.AtomicFlag
}
2020-08-18 10:30:55 +00:00
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)
2020-08-18 10:30:55 +00:00
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 {
2020-08-18 10:30:55 +00:00
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)
2020-08-18 10:30:55 +00:00
if af.keypressFlag.Get() {
af.keypressFlag.Set(false)
continue
} else {
break
}
}
}
}
}