2020-08-18 11:59:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"after-lock/afterlock"
|
2020-09-08 12:08:54 +00:00
|
|
|
"after-lock/xdg/display"
|
|
|
|
"after-lock/xdg/keypressdetector"
|
|
|
|
"after-lock/xdg/lockscreen"
|
2020-08-20 13:57:17 +00:00
|
|
|
"after-lock/lockfile"
|
|
|
|
"os"
|
2020-08-20 14:57:57 +00:00
|
|
|
"runtime/debug"
|
2020-09-08 13:27:52 +00:00
|
|
|
"log"
|
2020-08-18 11:59:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
initialDelay = 3
|
|
|
|
loopDelay = 6
|
|
|
|
keyboardDeviceNode = "/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd"
|
2020-08-20 13:57:17 +00:00
|
|
|
lockfilePath = "/tmp/after-lock.lock"
|
|
|
|
printStackTraces = false
|
2020-09-08 13:27:52 +00:00
|
|
|
logFilePath = "/home/superuser/Desktop/after-lock.log"
|
2020-08-18 11:59:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2020-08-20 13:57:17 +00:00
|
|
|
defer handleErrors()
|
2020-09-08 13:27:52 +00:00
|
|
|
f := initLogging()
|
|
|
|
if f != nil {
|
|
|
|
defer f.Close()
|
|
|
|
}
|
2020-08-20 13:57:17 +00:00
|
|
|
|
|
|
|
lock := grabExclusiveProcessLock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
2020-08-18 11:59:20 +00:00
|
|
|
al := afterlock.New(keyboardDeviceNode)
|
|
|
|
al.InitialDelay = initialDelay
|
|
|
|
al.LoopDelay = loopDelay
|
2020-08-20 14:57:57 +00:00
|
|
|
al.Display = display.Xset{}
|
|
|
|
al.LockScreen = lockscreen.XDG{}
|
|
|
|
al.KeypressDetector = keypressdetector.Evdev{}
|
2020-08-18 11:59:20 +00:00
|
|
|
|
|
|
|
al.Start()
|
2020-08-20 13:57:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 13:27:52 +00:00
|
|
|
func initLogging() *os.File {
|
|
|
|
if len(logFilePath) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(logFilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(f)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-08-20 13:57:17 +00:00
|
|
|
func handleErrors() {
|
|
|
|
if err := recover(); err != nil {
|
2020-09-08 13:27:52 +00:00
|
|
|
log.Printf("Crashed with unexpected error: %v!\n", err)
|
2020-08-20 13:57:17 +00:00
|
|
|
if printStackTraces {
|
2020-09-08 13:27:52 +00:00
|
|
|
log.Printf("Stack trace:\n\n")
|
2020-08-20 13:57:17 +00:00
|
|
|
debug.PrintStack()
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func grabExclusiveProcessLock() *lockfile.Lockfile {
|
|
|
|
lf := lockfile.New(lockfilePath)
|
|
|
|
err := lf.Lock()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return lf
|
2020-08-18 11:59:20 +00:00
|
|
|
}
|