Refactor screensaver package into display and lockscreen
This commit is contained in:
parent
97884bfb64
commit
8bba5b30a4
45
display/display.go
Normal file
45
display/display.go
Normal file
@ -0,0 +1,45 @@
|
||||
package display
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
// Suspend the display using xset
|
||||
func Suspend() error {
|
||||
cmd := exec.Command("xset",
|
||||
"dpms",
|
||||
"force",
|
||||
"suspend",
|
||||
)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
// IsOn determines if the display is on
|
||||
func IsOn() (bool, error) {
|
||||
cmd := exec.Command("xset", "q")
|
||||
outputBytes, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return determineIfDisplayIsOn(string(outputBytes)), nil
|
||||
}
|
||||
|
||||
func determineIfDisplayIsOn(xsetOutput string) bool {
|
||||
if ! strings.Contains(xsetOutput, "Monitor") {
|
||||
// after booting xset q is missing this line
|
||||
// lacking better ideas assume display is on
|
||||
return true
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`Monitor is (\w+)`)
|
||||
matches := re.FindStringSubmatch(xsetOutput)
|
||||
if len(matches) >= 2 {
|
||||
if matches[1] == "On" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
26
lockscreen/lockscreen.go
Normal file
26
lockscreen/lockscreen.go
Normal file
@ -0,0 +1,26 @@
|
||||
package lockscreen
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
// IsActive checks whether the screen is locked (checks if the screensaver is active)
|
||||
func IsActive() (bool, error) {
|
||||
cmd := exec.Command("qdbus",
|
||||
"org.kde.screensaver",
|
||||
"/ScreenSaver",
|
||||
"org.freedesktop.ScreenSaver.GetActive",
|
||||
)
|
||||
outputBytes, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
output := string(outputBytes)
|
||||
|
||||
if strings.HasPrefix(output, "true") {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
13
main.go
13
main.go
@ -2,7 +2,8 @@ package main
|
||||
|
||||
import(
|
||||
"after-lock/evdev"
|
||||
"after-lock/screensaver"
|
||||
"after-lock/lockscreen"
|
||||
"after-lock/display"
|
||||
"after-lock/atomicflag"
|
||||
"time"
|
||||
"sync"
|
||||
@ -57,22 +58,22 @@ func (af *afterLock) detectKeypresses() {
|
||||
|
||||
func (af *afterLock) hybernateDisplayLoop() {
|
||||
for {
|
||||
active, err := screensaver.IsActive()
|
||||
screenLocked, err := lockscreen.IsActive()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !active {
|
||||
if !screenLocked {
|
||||
af.stopFlag.Set(true)
|
||||
af.wg.Wait()
|
||||
return
|
||||
}
|
||||
|
||||
shouldSleepDisplay, err := screensaver.ShouldBeActive()
|
||||
displayOn, err := display.IsOn()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if shouldSleepDisplay {
|
||||
err := screensaver.Activate()
|
||||
if displayOn {
|
||||
err := display.Suspend()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -1,61 +0,0 @@
|
||||
package screensaver
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
func IsActive() (bool, error) {
|
||||
cmd := exec.Command("qdbus",
|
||||
"org.kde.screensaver",
|
||||
"/ScreenSaver",
|
||||
"org.freedesktop.ScreenSaver.GetActive",
|
||||
)
|
||||
outputBytes, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
output := string(outputBytes)
|
||||
|
||||
if strings.HasPrefix(output, "true") {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func Activate() error {
|
||||
cmd := exec.Command("xset",
|
||||
"dpms",
|
||||
"force",
|
||||
"suspend",
|
||||
)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func ShouldBeActive() (bool, error) {
|
||||
cmd := exec.Command("xset", "q")
|
||||
outputBytes, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return arbitrateXsetOutput(string(outputBytes)), nil
|
||||
}
|
||||
|
||||
func arbitrateXsetOutput(output string) bool {
|
||||
if ! strings.Contains(output, "Monitor") {
|
||||
// after booting xset q is missing this line
|
||||
// lacking better ideas assume display is on
|
||||
return true
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`Monitor is (\w+)`)
|
||||
matches := re.FindStringSubmatch(output)
|
||||
if len(matches) >= 2 {
|
||||
if matches[1] == "On" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue
Block a user