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

television.go (2374B)


      1 /*
      2   television
      3 
      4   "Unless we design and implement alternate information structures which
      5   transcend and reconfigure the existing ones, other alternate systems and life
      6   styles will be no more than products of the existing process."
      7 
      8                                           Radical Software
      9                                           Volume I, Number 1
     10                                           The Alternate Television Movement
     11                                           Spring 1970
     12 */
     13 
     14 // context
     15 
     16 package main
     17 
     18 import (
     19     "flag"
     20     "os"
     21     "os/exec"
     22     "fmt"
     23     "time"
     24     "net/http"
     25     "github.com/kbinani/screenshot"
     26     "github.com/pixiv/go-libjpeg/jpeg"
     27     "github.com/JelmerDeHen/scrnsaver"
     28 )
     29 
     30 var(
     31   portNum       int
     32   wwwDir        string
     33   snapCmd       string
     34   idleDetect    bool
     35   snap          func()
     36   shot          func()
     37 )
     38 
     39 // components
     40 
     41 func compatSnap() {
     42   img, err := screenshot.CaptureDisplay(0)
     43   if err != nil {
     44     panic(err)
     45   }
     46 
     47   fileName := wwwDir + "/tv.jpg.tmp"
     48   file, _ := os.Create(fileName)
     49   defer file.Close()
     50 
     51   jpeg.Encode(file, img, &jpeg.EncoderOptions{
     52     Quality: 50,
     53     OptimizeCoding: false,
     54     ProgressiveMode: false,
     55     DCTMethod: jpeg.DCTIFast})
     56 
     57   os.Rename(fileName, wwwDir + "/tv.jpg")
     58 
     59 }
     60 
     61 func mySnap() {
     62   cmd := exec.Command("sh", "-c", snapCmd)
     63   if err := cmd.Run(); err != nil {
     64     panic(err)
     65   }
     66 }
     67 
     68 func maybeShot() {
     69   for {
     70     info, err := scrnsaver.GetXScreenSaverInfo()
     71       if err != nil {
     72         panic(err)
     73     }
     74 
     75     if info.Idle.Milliseconds() < 2000 {
     76       snap()
     77     }
     78     time.Sleep(1000 * time.Millisecond)
     79   }
     80 }
     81 
     82 func alwaysShot() {
     83   for {
     84     snap()
     85     time.Sleep(1000 * time.Millisecond)
     86   }
     87 }
     88 
     89 // combination
     90 
     91 func main() {
     92   flag.IntVar(&portNum, "port", 8888, "http port number")
     93   flag.StringVar(&wwwDir, "www", "./www", "www directory")
     94   flag.StringVar(&snapCmd, "snap", "", "custom snap command")
     95   flag.BoolVar(&idleDetect, "idle", false, "user activity detection")
     96   flag.Parse()
     97 
     98   if len(snapCmd) > 0 {
     99     snap = mySnap
    100   } else {
    101     snap = compatSnap
    102   }
    103 
    104   if idleDetect {
    105     shot = maybeShot
    106   } else {
    107     shot = alwaysShot
    108   }
    109 
    110   go shot()
    111 
    112   fmt.Printf("we're live on port %d!\n", portNum)
    113   fmt.Printf("ctrl-c for emergency shutdown\n")
    114 
    115   http.Handle("/", http.FileServer(http.Dir(wwwDir)))
    116   http.ListenAndServe(":" + fmt.Sprintf("%d", portNum), nil)
    117 }
    118