summaryrefslogtreecommitdiffstats
path: root/.local/bin/pulse-volume-watcher.py
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/pulse-volume-watcher.py')
-rwxr-xr-x.local/bin/pulse-volume-watcher.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/.local/bin/pulse-volume-watcher.py b/.local/bin/pulse-volume-watcher.py
new file mode 100755
index 0000000..1b0fcd5
--- /dev/null
+++ b/.local/bin/pulse-volume-watcher.py
@@ -0,0 +1,48 @@
1#!/usr/bin/env python3
2import sys
3
4from pulsectl import Pulse, PulseLoopStop
5
6with Pulse() as pulse:
7
8 def callback(ev):
9 if ev.index == sink_index:
10 raise PulseLoopStop
11
12 def current_status(sink):
13 return round(sink.volume.value_flat * 100), sink.mute == 1
14
15 try:
16 sinks = {s.index: s for s in pulse.sink_list()}
17 if len(sys.argv) > 1:
18 # Sink index from command line argument if provided
19 sink_index = int(sys.argv[1])
20 if sink_index not in sinks:
21 raise KeyError(f"Sink index {sink_index} not found in list of sinks.")
22 else:
23 # Automatic determination of default sink otherwise
24 default_sink_name = pulse.server_info().default_sink_name
25 try:
26 sink_index = next(
27 index
28 for index, sink in sinks.items()
29 if sink.name == default_sink_name
30 )
31 except StopIteration:
32 raise StopIteration("No default sink was found.")
33
34 pulse.event_mask_set("sink")
35 pulse.event_callback_set(callback)
36 last_value, last_mute = current_status(sinks[sink_index])
37
38 while True:
39 pulse.event_listen()
40 sinks = {s.index: s for s in pulse.sink_list()}
41 value, mute = current_status(sinks[sink_index])
42 if value != last_value or mute != last_mute:
43 print(str(value) + ("!" if mute else ""))
44 last_value, last_mute = value, mute
45 sys.stdout.flush()
46
47 except Exception as e:
48 print(f"ERROR: {e}", file=sys.stderr)