Refactor screensaver package into display and lockscreen
This commit is contained in:
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
|
||||
}
|
Reference in New Issue
Block a user