ovly.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import "fmt"
  3. import "log"
  4. import "time"
  5. import "os/exec"
  6. import "github.com/hypebeast/go-osc/osc"
  7. // Overlay object
  8. type Overlay struct {
  9. title string
  10. t int
  11. show_t bool
  12. }
  13. //type Title struct {
  14. // title string
  15. //}
  16. //
  17. //type Timer struct {
  18. // t int
  19. // visible bool
  20. //}
  21. //type MediaPlayer interface {
  22. // set() string
  23. //}
  24. //type Titler interface {
  25. // set() string
  26. //}
  27. //
  28. //func (tit *Title)set(title string) {
  29. // fmt.Print("")
  30. // fmt.Printf(" %-160s %6d ", title, t)
  31. //}
  32. // Updates title of bar in Overlay object
  33. func update_title(title string, t int) {
  34. fmt.Print("")
  35. fmt.Printf(" %-160s %6d ", title, t)
  36. }
  37. // Run executable
  38. func run(output chan string, cmd string, args ...string) {
  39. // Find executable
  40. path, err := exec.LookPath(cmd)
  41. if err != nil {
  42. fmt.Printf("can't find '%s'\n", cmd)
  43. } else {
  44. fmt.Printf("'%s' executable is in '%s'\n", cmd, path)
  45. }
  46. out, err := exec.Command(path, args...).Output()
  47. if err != nil {
  48. log.Print(err)
  49. }
  50. log.Printf("%s\n", out)
  51. output <- string(out)
  52. }
  53. // Play media file via mpv, osc message sets the file path
  54. func play(msg *osc.Message) {
  55. var uri string;
  56. ch := make(chan string)
  57. for _, arg := range msg.Arguments {
  58. switch arg.(type) {
  59. case string:
  60. uri = arg.(string)
  61. }
  62. }
  63. run(ch, "mpv", "-fs", "--audio-device=alsa/hdmi", uri)
  64. }
  65. func main() {
  66. addr := "0.0.0.0:9137"
  67. d := osc.NewStandardDispatcher()
  68. ovly := Overlay{"DECRUNCH Screen...", 0, true}
  69. d.AddMsgHandler("/8bus/ovly/title", func(msg *osc.Message) {
  70. for _, arg := range msg.Arguments {
  71. switch arg.(type) {
  72. case string:
  73. ovly.title = arg.(string)
  74. }
  75. }
  76. update_title(ovly.title, 0)
  77. })
  78. ticker := time.NewTicker(time.Second)
  79. d.AddMsgHandler("/8bus/ovly/timer/reset", func (msg *osc.Message) {
  80. ovly.t = 0
  81. ticker.Reset(time.Second)
  82. update_title(ovly.title, 0)
  83. })
  84. d.AddMsgHandler("/8bus/ovly/video/play", play)
  85. // Main loop
  86. //done := make(chan bool)
  87. go func() {
  88. for {
  89. select {
  90. //case <-done:
  91. // return
  92. case <-ticker.C:
  93. ovly.t += 1
  94. update_title(ovly.title, ovly.t)
  95. }
  96. }
  97. }()
  98. // Disable cursor, move it home and clear the terminal
  99. fmt.Print("[?25l")
  100. // Set up and start OSC server
  101. server := &osc.Server{
  102. Addr: addr,
  103. Dispatcher: d,
  104. }
  105. server.ListenAndServe()
  106. // Enable cursor
  107. fmt.Print("[?25h")
  108. }