summaryrefslogtreecommitdiffstats
path: root/.local/bin/pulse-volume-watcher.py
diff options
context:
space:
mode:
authorYigit Sever2021-10-08 00:05:34 +0300
committerYigit Sever2021-10-08 00:05:34 +0300
commit75d6e605a64d2f862942afb57f1ff5668d60ae52 (patch)
tree8d9da281e33f1f58b5e5d063ec0de4e9a3777cb4 /.local/bin/pulse-volume-watcher.py
parent0287e6b35edab6c2694a8c59f5a5195e0de13014 (diff)
downloaddotfiles-75d6e605a64d2f862942afb57f1ff5668d60ae52.tar.gz
dotfiles-75d6e605a64d2f862942afb57f1ff5668d60ae52.tar.bz2
dotfiles-75d6e605a64d2f862942afb57f1ff5668d60ae52.zip
bin: track bunch of scripts
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)