2020-08-17 13:50:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import(
|
|
|
|
"after-lock/evdev"
|
2020-08-18 11:15:45 +00:00
|
|
|
"after-lock/lockscreen"
|
|
|
|
"after-lock/display"
|
2020-08-18 09:06:59 +00:00
|
|
|
"after-lock/atomicflag"
|
|
|
|
"time"
|
|
|
|
"sync"
|
2020-08-17 13:50:39 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 09:06:59 +00:00
|
|
|
const (
|
|
|
|
initialDelay = 3
|
|
|
|
loopDelay = 6
|
|
|
|
keyboardDeviceNode = "/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-08-17 13:50:39 +00:00
|
|
|
func main() {
|
2020-08-18 10:30:55 +00:00
|
|
|
al := newAfterLock()
|
|
|
|
al.start()
|
|
|
|
}
|
2020-08-18 09:06:59 +00:00
|
|
|
|
2020-08-18 10:30:55 +00:00
|
|
|
type afterLock struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
stopFlag *atomicflag.AtomicFlag
|
|
|
|
keypressFlag *atomicflag.AtomicFlag
|
|
|
|
}
|
2020-08-18 09:06:59 +00:00
|
|
|
|
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()
|
2020-08-18 09:06:59 +00:00
|
|
|
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() {
|
2020-08-18 09:06:59 +00:00
|
|
|
for {
|
2020-08-18 11:15:45 +00:00
|
|
|
screenLocked, err := lockscreen.IsActive()
|
2020-08-18 09:06:59 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-18 11:15:45 +00:00
|
|
|
if !screenLocked {
|
2020-08-18 10:30:55 +00:00
|
|
|
af.stopFlag.Set(true)
|
|
|
|
af.wg.Wait()
|
2020-08-18 09:06:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:15:45 +00:00
|
|
|
displayOn, err := display.IsOn()
|
2020-08-18 09:06:59 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-18 11:15:45 +00:00
|
|
|
if displayOn {
|
|
|
|
err := display.Suspend()
|
2020-08-18 09:06:59 +00:00
|
|
|
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)
|
2020-08-18 09:06:59 +00:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 13:50:39 +00:00
|
|
|
}
|
|
|
|
}
|