forked from itchyny/volume-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume_linux.go
138 lines (114 loc) · 3.37 KB
/
volume_linux.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
//go:build !windows && !darwin
// +build !windows,!darwin
package volume
import (
"errors"
"regexp"
"strconv"
"strings"
)
var useAmixer bool
func init() {
if _, err := execCmd([]string{"pactl", "info"}); err != nil {
useAmixer = true
}
}
func cmdEnv() []string {
return []string{"LANG=C", "LC_ALL=C"}
}
func getVolumeCmd() []string {
if useAmixer {
return []string{"amixer", "get", "Master"}
}
return []string{"pactl", "list", "sinks"}
}
func getPulseAudioDefaultSink() (string, error) {
out, err := execCmd([]string{"pactl", "info"})
if err != nil {
return "", err
}
lines := strings.Split(string(out), "\n")
defaultSinkStr := "Default Sink: "
for _, line := range lines {
s := strings.TrimLeft(line, " \t")
if strings.HasPrefix(s, defaultSinkStr) {
return strings.TrimSpace(strings.Replace(s, defaultSinkStr, "", 1)), nil
}
}
return "", errors.New("could not find PulseAudio Default Sink")
}
var volumePattern = regexp.MustCompile(`\d+%`)
func parseVolume(out string) (int, error) {
sinkName, sinkNameErr := getPulseAudioDefaultSink()
isDefaultSink := sinkNameErr != nil
lines := strings.Split(out, "\n")
for _, line := range lines {
s := strings.TrimLeft(line, " \t")
if !useAmixer && !isDefaultSink && strings.Contains(s, "Name: "+sinkName) {
isDefaultSink = true
}
if useAmixer && strings.Contains(s, "Playback") && strings.Contains(s, "%") ||
!useAmixer && isDefaultSink && strings.HasPrefix(s, "Volume:") {
volumeStr := volumePattern.FindString(s)
return strconv.Atoi(volumeStr[:len(volumeStr)-1])
}
}
return 0, errors.New("no volume found")
}
func setVolumeCmd(volume int) []string {
if useAmixer {
return []string{"amixer", "set", "Master", strconv.Itoa(volume) + "%"}
}
return []string{"pactl", "set-sink-volume", "@DEFAULT_SINK@", strconv.Itoa(volume) + "%"}
}
func increaseVolumeCmd(diff int) []string {
var sign string
if diff >= 0 {
sign = "+"
} else if useAmixer {
diff = -diff
sign = "-"
}
if useAmixer {
return []string{"amixer", "set", "Master", strconv.Itoa(diff) + "%" + sign}
}
return []string{"pactl", "--", "set-sink-volume", "@DEFAULT_SINK@", sign + strconv.Itoa(diff) + "%"}
}
func getMutedCmd() []string {
if useAmixer {
return []string{"amixer", "get", "Master"}
}
return []string{"pactl", "list", "sinks"}
}
func parseMuted(out string) (bool, error) {
sinkName, sinkNameErr := getPulseAudioDefaultSink()
isDefaultSink := sinkNameErr != nil
lines := strings.Split(out, "\n")
for _, line := range lines {
s := strings.TrimLeft(line, " \t")
if !useAmixer && !isDefaultSink && strings.Contains(s, "Name: "+sinkName) {
isDefaultSink = true
}
if useAmixer && strings.Contains(s, "Playback") && strings.Contains(s, "%") ||
!useAmixer && isDefaultSink && strings.HasPrefix(s, "Mute: ") {
if strings.Contains(s, "[off]") || strings.Contains(s, "yes") {
return true, nil
} else if strings.Contains(s, "[on]") || strings.Contains(s, "no") {
return false, nil
}
}
}
return false, errors.New("no muted information found")
}
func muteCmd() []string {
if useAmixer {
return []string{"amixer", "-D", "pulse", "set", "Master", "mute"}
}
return []string{"pactl", "set-sink-mute", "@DEFAULT_SINK@", "1"}
}
func unmuteCmd() []string {
if useAmixer {
return []string{"amixer", "-D", "pulse", "set", "Master", "unmute"}
}
return []string{"pactl", "set-sink-mute", "@DEFAULT_SINK@", "0"}
}