television

a simple LAN/WLAN desktop public broadcasting service
git clone http://git.permacomputing.net/repos/television.git # read-only access
Log | Files | Refs | README

commit 2610bbc57b7246c95031b210ac488345347f3afb
parent bbafdc28f9e6e469f71c384a8adfd086c6c6c5cd
Author: ugrnm <ultrageranium@bleu255.com>
Date:   Mon,  7 Oct 2024 20:47:52 +0200

idling detect

Diffstat:
Mgo.mod | 1+
Mtelevision.go | 85+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
2 files changed, 52 insertions(+), 34 deletions(-)

diff --git a/go.mod b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/JelmerDeHen/scrnsaver v0.0.0-20220827073217-d8af05db8655 // indirect github.com/gen2brain/shm v0.1.0 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/jezek/xgb v1.1.1 // indirect diff --git a/television.go b/television.go @@ -9,74 +9,91 @@ import ( "net/http" "github.com/kbinani/screenshot" "github.com/pixiv/go-libjpeg/jpeg" + "github.com/JelmerDeHen/scrnsaver" ) var( - portNum int - wwwDir string - snapCmd string + portNum int + wwwDir string + snapCmd string + idleDetect bool + snap func() + shot func() ) -func snap() { - - for { - - img, err := screenshot.CaptureDisplay(0) - if err != nil { - panic(err) - } - - fileName := wwwDir + "/tv.jpg.tmp" - file, _ := os.Create(fileName) - defer file.Close() - - jpeg.Encode(file, img, &jpeg.EncoderOptions{ - Quality: 50, - OptimizeCoding: false, - ProgressiveMode: false, - DCTMethod: jpeg.DCTIFast}) +func compatSnap() { + img, err := screenshot.CaptureDisplay(0) + if err != nil { + panic(err) + } - os.Rename(fileName, wwwDir + "/tv.jpg") + fileName := wwwDir + "/tv.jpg.tmp" + file, _ := os.Create(fileName) + defer file.Close() - //fmt.Printf("screenshot taken\n") // debug + jpeg.Encode(file, img, &jpeg.EncoderOptions{ + Quality: 50, + OptimizeCoding: false, + ProgressiveMode: false, + DCTMethod: jpeg.DCTIFast}) - time.Sleep(1000 * time.Millisecond) + os.Rename(fileName, wwwDir + "/tv.jpg") - } } -func mysnap() { +func mySnap() { + cmd := exec.Command("sh", "-c", snapCmd) + if err := cmd.Run(); err != nil { + panic(err) + } +} +func maybeShot() { for { - - cmd := exec.Command("sh", "-c", snapCmd) - if err := cmd.Run(); err != nil { - panic(err) + info, err := scrnsaver.GetXScreenSaverInfo() + if err != nil { + panic(err) } + if info.Idle.Milliseconds() < 2000 { + snap() + } time.Sleep(1000 * time.Millisecond) + } +} +func alwaysShot() { + for { + snap() + time.Sleep(1000 * time.Millisecond) } } func main() { - flag.IntVar(&portNum, "port", 8888, "http port number") flag.StringVar(&wwwDir, "www", "./www", "www directory") flag.StringVar(&snapCmd, "snap", "", "custom snap command") + flag.BoolVar(&idleDetect, "idle", false, "user activity detection") flag.Parse() if len(snapCmd) > 0 { - go mysnap() + snap = mySnap } else { - go snap() + snap = compatSnap } + if idleDetect { + shot = maybeShot + } else { + shot = alwaysShot + } + + go shot() + fmt.Printf("we're live on port %d!\n", portNum) fmt.Printf("ctrl-c for emergency shutdown\n") http.Handle("/", http.FileServer(http.Dir(wwwDir))) http.ListenAndServe(":" + fmt.Sprintf("%d", portNum), nil) - }