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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# -*- python -*-
from plugins.alsa import ALSA
import alsaaudio
from common import *
class MyALSA(Entry):
def __init__(self, *args, timeout = None, sleep = None, cards = -1, mixers = None, colours = {}, prefix = '', **kwargs): ## TODO support multiple cards and all mixers
self.colours = colours if colours is not None else {}
if timeout is not None:
self.timeout = timeout
else:
self.timeout = 5
if sleep is not None:
self.sleep = sleep
else:
self.sleep = 5
if mixers is None:
mixers = ('Master', 'PCM')
elif isinstance(mixers, str):
mixers = [mixers]
else:
mixers = mixers
if cards is None:
cards = [-1]
elif isinstance(cards, str) or isinstance(cards, int):
cards = [cards]
else:
cards = cards
self.alsa = []
names = {}
for index in alsaaudio.card_indexes():
for name in alsaaudio.card_name(index):
names[name] = index
for card in cards:
card = names.get(card, card)
for mixer in mixers: ## TODO support by name (and 'default')
try:
if isinstance(mixer, tuple) or isinstance(mixer, list):
self.alsa.append([ALSA(card, m) for m in mixer])
else:
self.alsa.append(ALSA(card, mixer))
except:
pass
if True:
method = 3
try:
path = os.environ['PATH'].split(':')
for p in path:
p += '/bus'
if os.access(p, os.F_OK | os.X_OK, effective_ids = True):
method = 0
break
except:
pass
else:
try:
import posix_ipc
method = 1
except:
method = 3
try:
path = os.environ['PATH'].split(':')
for p in path:
p += '/cmdipc'
if os.access(p, os.F_OK | os.X_OK, effective_ids = True):
method = 2
break
except:
pass
self.prefix = prefix
self.sep_width = Bar.coloured_length(SEPARATOR)
self.get_volume()
self.broadcast_update = None
if method == 0:
self.create_broadcast_update_bus()
Entry.__init__(self, *args, **kwargs)
xasync((self.refresh_bus, self.refresh_posix_ipc, self.refresh_cmdipc, self.refresh_wait)[method], name = 'alsa')
def action(self, col, button, x, y):
if not self.alsa:
return
mixer = 0
mixer_text = self.text.split(SEPARATOR)
while mixer < len(mixer_text): ## the limit is just a precaution
mixer_width = Bar.coloured_length(mixer_text[mixer])
if col >= mixer_width:
col -= mixer_width
if col < self.sep_width:
return
col -= self.sep_width
else:
break
mixer += 1
mixer_text = mixer_text[mixer]
channel = -1
mixer_text = mixer_text.split(': ')
mixer_label = ': '.join(mixer_text[:-1]) + ': '
mixer_text = mixer_text[-1].split(' ')
mixer_label = Bar.coloured_length(mixer_label)
if col >= mixer_label:
col -= mixer_label
while channel < len(mixer_text): ## the limit is just a precaution
channel += 1
channel_width = Bar.coloured_length(mixer_text[channel])
if col < channel_width:
break
col -= channel_width
if col < 1:
channel = -1
break
col -= 1
mixer = self.alsa[mixer]
if isinstance(mixer, list):
if button == LEFT_BUTTON:
self.switch_exclusive(mixer)
elif button == LEFT_BUTTON:
muted = mixer.get_mute()
muted = not (any(muted) if channel == -1 else muted[channel])
mixer.set_mute(muted, channel)
elif button == RIGHT_BUTTON:
volume = mixer.get_volume()
volume = limited(sum(volume) / len(volume))
mixer.set_volume(volume, -1)
elif button in (SCROLL_UP, SCROLL_DOWN):
volume = mixer.get_volume()
adj = -5 if button == SCROLL_DOWN else 5
if channel < 0:
for ch in range(len(volume)):
mixer.set_volume(limited(volume[ch] + adj), ch)
else:
mixer.set_volume(limited(volume[channel] + adj), channel)
else:
return
self.get_volume()
self.invalidate()
if self.broadcast_update is not None:
self.broadcast_update()
def switch_exclusive(self, mixers):
def f(status):
if all(status):
return True
elif any(status):
return None
else:
return False
muted = [f(m.get_mute()) for m in mixers]
count = sum((0 if m else 1) for m in muted)
if None in muted or count != 1:
index = 0
else:
[index] = [i for i, m in enumerate(muted) if not m]
index = (index + 1) % len(mixers)
for m in mixers:
m.set_mute(True)
mixers[index].set_mute(False)
def get_exclusive(self, mixers):
def f(status):
if all(status):
return True
elif any(status):
return None
else:
return False
muted = [f(m.get_mute()) for m in mixers]
count = sum((0 if m else 1) for m in muted)
if None in muted or count != 1:
index = 0
for m in mixers:
m.set_mute(True)
mixers[index].set_mute(False)
else:
[index] = [i for i, m in enumerate(muted) if not m]
name = mixers[index].mixername
if name in self.colours:
name = '\033[%sm%s\033[0m' % (self.colours[name], name)
return name
def get_volume(self):
text_v = lambda v : '--%' if v is None else ('%2i%%' % v)[:3]
read_m = lambda m : '%s%s: %s' % (self.prefix, m.mixername, ' '.join(text_v(v) for v in m.get_volume()))
text = SEPARATOR.join((self.get_exclusive(m) if isinstance(m, list) else read_m(m)) for m in self.alsa)
if not text:
text = '%s%s' % (self.prefix, 'disconnected')
self.text = text
def refresh_bus(self):
if 'BUS_AUDIO' in os.environ:
bus = os.environ['BUS_AUDIO']
else:
bus = os.environ['XDG_RUNTIME_DIR'] + '/@bus/audio'
wait = pdeath('HUP', 'bus', 'wait', bus, 'true')
while True:
try:
spawn_read(*wait)
self.get_volume()
except:
time.sleep(self.sleep)
def create_broadcast_update_bus(self):
if 'BUS_AUDIO' in os.environ:
bus = os.environ['BUS_AUDIO']
else:
bus = os.environ['XDG_RUNTIME_DIR'] + '/@bus/audio'
self.bus_broadcast = pdeath('HUP', 'bus', 'broadcast', bus, '%i volume' % os.getpid())
self.broadcast_update = lambda : spawn_read(*(self.bus_broadcast))
def refresh_posix_ipc(self):
import posix_ipc
s = posix_ipc.Semaphore('/.xpybar.alsa.0', posix_ipc.O_CREAT, 0o600, 1)
c = posix_ipc.Semaphore('/.xpybar.alsa.1', posix_ipc.O_CREAT, 0o600, 0)
q = posix_ipc.Semaphore('/.xpybar.alsa.2', posix_ipc.O_CREAT, 0o600, 0)
try:
s.acquire(self.timeout)
while True:
failed = False
try:
c.release()
try:
s.release()
try:
q.acquire(timeout)
except posix_ipc.BusyError:
sys.exit(1)
pass
finally:
s.acquire(self.timeout)
finally:
c.acquire(self.timeout)
except:
sys.exit(1)
failed = True
try:
self.get_volume()
except:
sys.exit(1)
failed = True
if failed:
time.sleep(self.sleep)
finally:
s.release()
def refresh_cmdipc(self):
enter = pdeath('HUP', 'cmdipc', '-PCck', '/.xpybar.alsa.0/.xpybar.alsa.1/.xpybar.alsa.2', 'enter', '-b%i' % self.timeout)
wait = pdeath('HUP', 'cmdipc', '-PCk', '/.xpybar.alsa.0/.xpybar.alsa.1/.xpybar.alsa.2', 'wait', '-b%i' % self.timeout)
leave = pdeath('HUP', 'cmdipc', '-PCk', '/.xpybar.alsa.0/.xpybar.alsa.1/.xpybar.alsa.2', 'leave')
try:
spawn_read(*enter)
while True:
try:
spawn_read(*wait)
self.get_volume()
except:
time.sleep(self.sleep)
finally:
spawn_read(*leave)
def refresh_wait(self):
while True:
try:
time.sleep(self.sleep)
self.get_volume()
except:
pass
def function(self):
return self.text
|