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