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

49 lines
917 B
Go
Raw Normal View History

package display
import (
"os/exec"
"regexp"
"strings"
)
// Xset monitors/controls the display using xset
type Xset struct {}
// Suspend the display
func (Xset) Suspend() error {
cmd := exec.Command("xset",
"dpms",
"force",
"suspend",
)
return cmd.Run()
}
// IsOn determines if the display is on
func (Xset) 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
}