Group lockscreen and display suspension related code
This commit is contained in:
		
							
								
								
									
										48
									
								
								xdg/display/display.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								xdg/display/display.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user