package utils

import (
	"os"
	"os/exec"
	"sync"
)

var (
	xsetBin string
	once    sync.Once
)

func init() {
	once.Do(func() {
		if found, err := exec.LookPath("xset"); err == nil {
			xsetBin = found
		}
	})
}

// BlankOpen 打开屏幕
func BlankOpen() {
	if xsetBin != "" {
		exec.Command(xsetBin, "dpms", "force", "on").Run()
		return
	}
	os.WriteFile("/sys/class/graphics/fb0/blank", []byte("0"), 0644)
}

// BlankClose 关闭屏幕
func BlankClose() {
	if xsetBin != "" {
		exec.Command(xsetBin, "dpms", "force", "off").Run()
		return
	}
	os.WriteFile("/sys/class/graphics/fb0/blank", []byte("1"), 0644)
}
