76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"after-lock/afterlock"
|
|
"after-lock/xdg/display"
|
|
"after-lock/xdg/keypressdetector"
|
|
"after-lock/xdg/lockscreen"
|
|
"after-lock/lockfile"
|
|
"os"
|
|
"runtime/debug"
|
|
"log"
|
|
)
|
|
|
|
const (
|
|
initialDelay = 3
|
|
loopDelay = 6
|
|
keyboardDeviceNode = "/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd"
|
|
lockfilePath = "/tmp/after-lock.lock"
|
|
printStackTraces = false
|
|
logFilePath = "/home/superuser/Desktop/after-lock.log"
|
|
)
|
|
|
|
|
|
func main() {
|
|
defer handleErrors()
|
|
f := initLogging()
|
|
if f != nil {
|
|
defer f.Close()
|
|
}
|
|
|
|
lock := grabExclusiveProcessLock()
|
|
defer lock.Unlock()
|
|
|
|
al := afterlock.New(keyboardDeviceNode)
|
|
al.InitialDelay = initialDelay
|
|
al.LoopDelay = loopDelay
|
|
al.Display = display.Xset{}
|
|
al.LockScreen = lockscreen.XDG{}
|
|
al.KeypressDetector = keypressdetector.Evdev{}
|
|
|
|
al.Start()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func handleErrors() {
|
|
if err := recover(); err != nil {
|
|
log.Printf("Crashed with unexpected error: %v!\n", err)
|
|
if printStackTraces {
|
|
log.Printf("Stack trace:\n\n")
|
|
debug.PrintStack()
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func grabExclusiveProcessLock() *lockfile.Lockfile {
|
|
lf := lockfile.New(lockfilePath)
|
|
err := lf.Lock()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return lf
|
|
} |