Invert display and lockscreen control dependencies
This commit is contained in:
		@@ -1,9 +1,6 @@
 | 
			
		||||
package afterlock
 | 
			
		||||
 | 
			
		||||
import(
 | 
			
		||||
	"after-lock/evdev"
 | 
			
		||||
	"after-lock/lockscreen"
 | 
			
		||||
	"after-lock/display"
 | 
			
		||||
	"after-lock/atomicflag"
 | 
			
		||||
	"time"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -11,6 +8,22 @@ import(
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Display controls and monitors the physical display
 | 
			
		||||
type Display interface {
 | 
			
		||||
	Suspend() error
 | 
			
		||||
	IsOn() (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lockscreen can determine whether the screensaver is active or not
 | 
			
		||||
type Lockscreen interface {
 | 
			
		||||
	IsActive() (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// KeypressDetector can wait for keypresses
 | 
			
		||||
type KeypressDetector interface {
 | 
			
		||||
	BlockUntilKeypress(string) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AfterLock suspends the display when the lockscreen is active
 | 
			
		||||
type AfterLock struct {
 | 
			
		||||
	InitialDelay uint
 | 
			
		||||
@@ -19,6 +32,9 @@ type AfterLock struct {
 | 
			
		||||
	wg sync.WaitGroup
 | 
			
		||||
	stopFlag *atomicflag.AtomicFlag
 | 
			
		||||
	keypressFlag *atomicflag.AtomicFlag
 | 
			
		||||
	Display Display
 | 
			
		||||
	LockScreen Lockscreen
 | 
			
		||||
	KeypressDetector KeypressDetector
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// New constructs a new AfterLock instance
 | 
			
		||||
@@ -30,11 +46,12 @@ func New(keyboardDeviceNode string) *AfterLock {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Start starts monitoring the lockscreen/keyboard status to determine
 | 
			
		||||
// Start starts monitoring the lockscreen/keyboard to determine
 | 
			
		||||
// when to suspend the display.
 | 
			
		||||
// Exits after the computer is unlocked
 | 
			
		||||
// Requires InitialDelay, LoopDelay, Display and LockScreen to be set
 | 
			
		||||
func (af *AfterLock) Start() {
 | 
			
		||||
	af.checkDelaysConfigured()
 | 
			
		||||
	af.checkStructConfigured()
 | 
			
		||||
 | 
			
		||||
	af.wg.Add(1)
 | 
			
		||||
	go af.detectKeypresses()
 | 
			
		||||
@@ -43,18 +60,27 @@ func (af *AfterLock) Start() {
 | 
			
		||||
	af.hybernateDisplayLoop()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (af *AfterLock) checkDelaysConfigured() {
 | 
			
		||||
func (af *AfterLock) checkStructConfigured() {
 | 
			
		||||
	if af.InitialDelay == 0 {
 | 
			
		||||
		panic(errors.New("InitialDelay not configured"))
 | 
			
		||||
	}
 | 
			
		||||
	if af.LoopDelay == 0 {
 | 
			
		||||
		panic(errors.New("LoopDelay not configured"))
 | 
			
		||||
	}
 | 
			
		||||
	if af.Display == nil {
 | 
			
		||||
		panic(errors.New("Display not configured"))
 | 
			
		||||
	}
 | 
			
		||||
	if af.LockScreen == nil {
 | 
			
		||||
		panic(errors.New("LockScreen not configured"))
 | 
			
		||||
	}
 | 
			
		||||
	if af.KeypressDetector == nil {
 | 
			
		||||
		panic(errors.New("KeypressDetector not configured"))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (af *AfterLock) detectKeypresses() {
 | 
			
		||||
	for {
 | 
			
		||||
		err := evdev.BlockUntilKeypress(af.keyboardDeviceNode)
 | 
			
		||||
		err := af.KeypressDetector.BlockUntilKeypress(af.keyboardDeviceNode)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -68,7 +94,7 @@ func (af *AfterLock) detectKeypresses() {
 | 
			
		||||
 | 
			
		||||
func (af *AfterLock) hybernateDisplayLoop() {
 | 
			
		||||
	for {
 | 
			
		||||
		screenLocked, err := lockscreen.IsActive()
 | 
			
		||||
		screenLocked, err := af.LockScreen.IsActive()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -78,12 +104,12 @@ func (af *AfterLock) hybernateDisplayLoop() {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		displayOn, err := display.IsOn()
 | 
			
		||||
		displayOn, err := af.Display.IsOn()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		if displayOn {
 | 
			
		||||
			err := display.Suspend()
 | 
			
		||||
			err := af.Display.Suspend()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				panic(err)
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user