-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
outputs.go
153 lines (144 loc) · 2.94 KB
/
outputs.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
package pulseaudio
import "fmt"
// Output represents PulseAudio output.
type Output struct {
client *Client
CardID string
PortID string
CardName string
PortName string
Available bool
}
// Activate sets this output as the main one.
func (o Output) Activate() error {
c := o.client
cards, err := c.Cards()
if err != nil {
return err
}
if o.CardID == "all" && o.PortID == "none" {
for _, otherCard := range cards {
err = c.SetCardProfile(otherCard.Index, "off")
if err != nil {
return err
}
}
return nil
}
var found bool
var card Card
for _, card = range cards {
if card.Name == o.CardID {
found = true
break
}
}
if !found {
return fmt.Errorf("PulseAudio error: card %s is no longer available", o.CardID)
}
found = false
var port port
for _, port = range card.Ports {
if port.Name == o.PortID {
found = true
break
}
}
if !found {
return fmt.Errorf("PulseAudio error: port %s is no longer available", o.PortID)
}
for _, otherCard := range cards {
if otherCard.Index == card.Index {
continue
}
err = c.SetCardProfile(otherCard.Index, "off")
if err != nil {
return err
}
}
bestProfile := port.Profiles[0]
for _, profile := range port.Profiles {
if profile.Priority > bestProfile.Priority {
bestProfile = profile
}
}
err = c.SetCardProfile(card.Index, bestProfile.Name)
if err != nil {
return err
}
sinks, err := c.sinks()
if err != nil {
return err
}
s, err := c.ServerInfo()
if err != nil {
return err
}
for _, sink := range sinks {
if sink.CardIndex != card.Index {
continue
}
if s.DefaultSink == sink.Name {
continue
}
return c.setDefaultSink(sink.Name)
}
return nil
}
// Outputs returns a list of all audio outputs and an index of the active audio output.
//
// The last audio output is always called "None" and indicates that audio is disabled.
func (c *Client) Outputs() (outputs []Output, activeIndex int, err error) {
s, err := c.ServerInfo()
if err != nil {
return nil, 0, err
}
sinks, err := c.sinks()
if err != nil {
return nil, 0, err
}
cards, err := c.Cards()
if err != nil {
return nil, 0, err
}
activeIndex = -1
for _, card := range cards {
for _, port := range card.Ports {
if port.Direction != 1 {
continue
}
for _, sink := range sinks {
if sink.Name != s.DefaultSink {
continue
}
if sink.CardIndex != card.Index {
continue
}
if sink.ActivePortName != port.Name {
continue
}
activeIndex = len(outputs)
}
outputs = append(outputs, Output{
client: c,
CardID: card.Name,
CardName: card.PropList["device.description"],
PortID: port.Name,
PortName: port.Description,
Available: port.Available != 1,
})
}
}
if activeIndex == -1 {
activeIndex = len(outputs)
}
outputs = append(outputs, Output{
client: c,
CardID: "all",
CardName: "All",
PortID: "none",
PortName: "None",
Available: false,
})
return
}