forked from muesli/deckmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_pulseaudio_control.go
169 lines (140 loc) · 3.86 KB
/
widget_pulseaudio_control.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
)
const (
regexExpression = `index: ([0-9]+)[\s\S]*?muted: (no|yes)[\s\S]*?media.name = \"(.*?)\"[\s\S]*?application.name = \"(.*?)\"`
regexGroupClientId = 1
regexGroupMuted = 2
regexGroupMediaName = 3
regexGroupAppName = 4
listInputSinksCommand = "pacmd list-sink-inputs"
)
// PulseAudioControlWidget is a widget displaying a recently activated window.
type PulseAudioControlWidget struct {
*ButtonWidget
appName string
mode string
showTitle bool
}
type sinkInputData struct {
muted bool
title string
index string
}
// NewPulseAudioControlWidget returns a new PulseAudioControlWidget.
func NewPulseAudioControlWidget(bw *BaseWidget, opts WidgetConfig) (*PulseAudioControlWidget, error) {
var appName string
if err := ConfigValue(opts.Config["appName"], &appName); err != nil {
return nil, err
}
var mode string
if err := ConfigValue(opts.Config["mode"], &mode); err != nil {
return nil, err
}
var showTitle bool
_ = ConfigValue(opts.Config["showTitle"], &showTitle)
widget, err := NewButtonWidget(bw, opts)
if err != nil {
return nil, err
}
return &PulseAudioControlWidget{
ButtonWidget: widget,
appName: appName,
mode: mode,
showTitle: showTitle,
}, nil
}
// RequiresUpdate returns true when the widget wants to be repainted.
func (w *PulseAudioControlWidget) RequiresUpdate() bool {
//TODO
return w.BaseWidget.RequiresUpdate()
}
// Update renders the widget.
func (w *PulseAudioControlWidget) Update() error {
sinkInputData, err := getSinkInputDataForApp(w.appName)
if err != nil {
return err
}
var icon string
var label string
if sinkInputData != nil {
if sinkInputData.muted {
icon = "assets/muted.png"
} else {
icon = "assets/not_muted.png"
}
if w.showTitle && sinkInputData.title != "" {
label = sinkInputData.title
} else {
label = w.appName
}
} else {
icon = "assets/not_playing.png"
label = w.appName
}
if err := w.LoadImage(icon); err != nil {
return err
}
w.label = stripTextTo(10, label)
return w.ButtonWidget.Update()
}
// TriggerAction gets called when a button is pressed.
func (w *PulseAudioControlWidget) TriggerAction(hold bool) {
if w.mode != "mute" {
fmt.Fprintln(os.Stderr, "unknown mode:", w.mode)
return
}
sinkInputData, err := getSinkInputDataForApp(w.appName)
if err != nil {
fmt.Fprintln(os.Stderr, "can't toggle mute for pulseaudio app "+w.appName, err)
}
if sinkInputData == nil {
fmt.Fprintln(os.Stderr, "No running sink found for pulseaudio app "+w.appName, err)
}
toggleMute(sinkInputData.index)
}
func toggleMute(sinkIndex string) {
err := exec.Command("sh", "-c", "pactl set-sink-input-mute "+sinkIndex+" toggle").Run()
if err != nil {
fmt.Fprintln(os.Stderr, "can't toggle mute for pulseaudio sink index: "+sinkIndex, err)
}
}
func stripTextTo(maxLength int, text string) string {
runes := []rune(text)
if len(runes) > maxLength {
return string(runes[:maxLength])
}
return text
}
func getSinkInputDataForApp(appName string) (*sinkInputData, error) {
output, err := exec.Command("sh", "-c", listInputSinksCommand).Output()
if err != nil {
return nil, fmt.Errorf("can't get pulseaudio sinks. 'pacmd' missing? %s", err)
}
var regex = regexp.MustCompile(regexExpression)
matches := regex.FindAllStringSubmatch(string(output), -1)
var sinkData *sinkInputData
for match := range matches {
if appName == matches[match][regexGroupAppName] {
sinkData = &sinkInputData{}
sinkData.index = matches[match][regexGroupClientId]
sinkData.muted = yesOrNoToBool(matches[match][regexGroupMuted])
sinkData.title = matches[match][regexGroupMediaName]
}
}
return sinkData, nil
}
func yesOrNoToBool(yesOrNo string) bool {
switch yesOrNo {
case "yes":
return true
case "no":
return false
}
fmt.Fprintln(os.Stderr, "can't convert yes|no to bool: "+yesOrNo)
return false
}