-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
action_pulsevolume.go
67 lines (52 loc) · 1.33 KB
/
action_pulsevolume.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
// +build linux
package main
import (
"github.com/pkg/errors"
)
func init() {
registerAction("pulsevolume", actionPulseVolume{})
}
type actionPulseVolume struct{}
func (actionPulseVolume) Execute(attributes attributeCollection) error {
if pulseClient == nil {
return errors.New("PulseAudio client not initialized")
}
if attributes.Device == "" {
return errors.New("Missing 'device' attribute")
}
if attributes.Match == "" {
return errors.New("Missing 'match' attribute")
}
// Read volume
var (
volAbs bool
volVal float64
)
if attributes.SetVolume != nil {
volVal = *attributes.SetVolume / 100
volAbs = true
} else if attributes.ChangeVolume != nil {
volVal = *attributes.ChangeVolume / 100
volAbs = false
}
// Execute change
switch attributes.Device {
case "input":
return errors.Wrap(
pulseClient.SetSinkInputVolume(attributes.Match, attributes.Mute, volVal, volAbs),
"Unable to set sink input volume",
)
case "sink":
return errors.Wrap(
pulseClient.SetSinkVolume(attributes.Match, attributes.Mute, volVal, volAbs),
"Unable to set sink volume",
)
case "source":
return errors.Wrap(
pulseClient.SetSourceVolume(attributes.Match, attributes.Mute, volVal, volAbs),
"Unable to set source volume",
)
default:
return errors.Errorf("Unsupported device type: %q", attributes.Device)
}
}