Add package to control and monitor screensaver

This commit is contained in:
Kristóf Tóth 2020-08-17 16:54:03 +02:00
parent 5c6db0577f
commit 202b72879a
1 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,61 @@
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
}