Use xprintidle to stop

This commit is contained in:
nemunaire 2018-12-16 04:59:12 +01:00
parent 9bbe36d2d8
commit 1be401d519

24
main.go
View File

@ -1,11 +1,15 @@
package main
import (
"bytes"
"flag"
"io"
"log"
"math/rand"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/faiface/beep"
@ -19,6 +23,22 @@ const (
SampleRate = beep.SampleRate(48000)
)
func xPrintIdle() (idle uint64) {
cmd := exec.Command("xprintidle")
cmd.Env = append(os.Environ(), "DISPLAY=:0")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
log.Println(err)
} else {
s := string(out.Bytes())
if idle, err = strconv.ParseUint(strings.TrimSpace(s), 10, 64); err != nil {
log.Println(err)
}
}
return
}
func loadFile(path string) (s beep.Streamer, err error) {
var format beep.Format
@ -95,6 +115,7 @@ func main() {
defer ticker.Stop()
launched := time.Now()
idle := xPrintIdle()
loop:
for {
select{
@ -105,6 +126,9 @@ loop:
if t.Sub(launched) > 1 * time.Hour {
break loop
}
if xPrintIdle() < idle {
break loop
}
}
}
}