kde-lockscreen-suspend-display/screensaver/screensaver.go

62 lines
1.1 KiB
Go

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
}