Implement proof of concept core logic (requires refactor)
This commit is contained in:
		
							
								
								
									
										66
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										66
									
								
								main.go
									
									
									
									
									
								
							@@ -2,12 +2,70 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import(
 | 
					import(
 | 
				
			||||||
	"after-lock/evdev"
 | 
						"after-lock/evdev"
 | 
				
			||||||
	"log"
 | 
						"after-lock/screensaver"
 | 
				
			||||||
 | 
						"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() {
 | 
					func main() {
 | 
				
			||||||
	err := evdev.BlockUntilKeypress("/dev/input/by-id/usb-Dell_Dell_USB_Entry_Keyboard-event-kbd")
 | 
						var wg sync.WaitGroup
 | 
				
			||||||
	if err != nil {
 | 
						wg.Add(1)
 | 
				
			||||||
		log.Fatal(err)
 | 
						stopFlag := atomicflag.New(false)
 | 
				
			||||||
 | 
						keypressFlag := atomicflag.New(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							for {
 | 
				
			||||||
 | 
								err := evdev.BlockUntilKeypress(keyboardDeviceNode)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if stopFlag.Get() {
 | 
				
			||||||
 | 
									wg.Done()
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									keypressFlag.Set(true)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						time.Sleep(initialDelay * time.Second)
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							active, err := screensaver.IsActive()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								panic(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if !active {
 | 
				
			||||||
 | 
								stopFlag.Set(true)
 | 
				
			||||||
 | 
								wg.Wait()
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							shouldSleepDisplay, err := screensaver.ShouldBeActive()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								panic(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if shouldSleepDisplay {
 | 
				
			||||||
 | 
								err := screensaver.Activate()
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								for {
 | 
				
			||||||
 | 
									time.Sleep(loopDelay * time.Second)
 | 
				
			||||||
 | 
									if keypressFlag.Get() {
 | 
				
			||||||
 | 
										keypressFlag.Set(false)
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										break
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user