46 lines
844 B
Go
46 lines
844 B
Go
|
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
|
||
|
}
|