Add package to control and monitor screensaver
This commit is contained in:
		
							
								
								
									
										61
									
								
								screensaver/screensaver.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								screensaver/screensaver.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user