-
Notifications
You must be signed in to change notification settings - Fork 3
/
sound.go
79 lines (66 loc) · 1.49 KB
/
sound.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package status
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
)
var (
startMatcher = regexp.MustCompile(`index:\s`)
splitter = regexp.MustCompile(`^\s*(.+?)\s*[:=]\s*(.+?)\s*$`)
)
type sound struct{ value bool }
func (v *sound) load() error {
is, err := v.listSinkInputs()
if err != nil {
return err
}
for _, i := range is {
// these are always connected
if strings.HasPrefix(i["application.name"], "speech-dispatcher") {
continue
}
if i["state"] == "RUNNING" {
json.NewEncoder(os.Stderr).Encode(i)
v.value = true
return nil
}
}
v.value = false
return nil
}
func (v *sound) listSinkInputs() ([]map[string]string, error) {
c := exec.Command("pacmd", "list-sink-inputs")
c.Stderr = os.Stderr
out, err := c.Output()
if err != nil {
return nil, fmt.Errorf("pacmd list-sink-inputs: %w", err)
}
r := bytes.NewBuffer(out)
s := bufio.NewScanner(r)
var ret []map[string]string
var current map[string]string
for s.Scan() {
l := s.Text()
if startMatcher.MatchString(l) {
if current != nil {
ret = append(ret, current)
}
current = map[string]string{}
}
matches := splitter.FindStringSubmatch(l)
if len(matches) == 3 {
matches[2] = strings.TrimSuffix(matches[2], `"`)
matches[2] = strings.TrimPrefix(matches[2], `"`)
current[matches[1]] = matches[2]
}
}
ret = append(ret, current)
return ret, nil
}
func (v *sound) render(rw http.ResponseWriter) { fmt.Fprintf(rw, "%t\n", v.value) }