From 4e773bd7c60dfed11c5a5a8df4115607ea9d523d Mon Sep 17 00:00:00 2001
From: Alexander Sulfrian <alexander@sulfrian.net>
Date: Wed, 28 Nov 2012 20:46:57 +0100
Subject: sound: replace inotify with dbus notify from pulseaudio

---
 bin/dzen2_pgrp.sh         |  7 ++++-
 bin/pulse-dbus-receive.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++
 bin/sound.sh              | 22 ----------------
 3 files changed, 73 insertions(+), 23 deletions(-)
 create mode 100755 bin/pulse-dbus-receive.py
 delete mode 100644 bin/sound.sh

diff --git a/bin/dzen2_pgrp.sh b/bin/dzen2_pgrp.sh
index 05c186e..83f801e 100644
--- a/bin/dzen2_pgrp.sh
+++ b/bin/dzen2_pgrp.sh
@@ -13,7 +13,12 @@ gcpubar -fg '#999' -bg '#444' -w 50 -h 7 -l "^i(${DIR}/icons/cpu.xbm) " | \
 	sed -u 's/#D56F6C/#ff0000/' | \
 	dzen2 -xs 1 -x 1240 -w 120 -fn "${FONT}" -bg black -e 'button1=none' &
 
-sh "${DIR}/bin/sound.sh" | dzen2 -xs 1 -x 1223 -tw 17 -w 100 -fn "${FONT}" -bg black -sa c -l 1 -e 'entertitle=uncollapse,unhide;leavetitle=collapse;button1=exec:amixer -q -c0 set Master toggle;button2=exec:pavucontrol;button3=exec:amixer -c0 -- set Master playback 80%;button4=exec:amixer -c0 -- set Master playback 2%+;button5=exec:amixer -c0 -- set Master playback 2%-' &
+${DIR}/bin/pulse-dbus-receive.py | \
+	/home/alex/dev/dzen/dzen2 -xs 1 -x 1223 -tw 17 -w 100 -fn "${FONT}" -bg black -sa c -l 1 \
+		-e 'entertitle=uncollapse,unhide;leavetitle=collapse;button2=exec:pavucontrol\
+		    ;button1=print:sset Master toggle;button3=print:sset Master 80%\
+		    ;button4=print:sset Master 2%+;button5=print:sset Master 2%-' | \
+	amixer -sq &
 
 # kill bars on new RandR config
 ( ${DIR}/bin/xrandr-notify && kill ${MAIN} ) &
diff --git a/bin/pulse-dbus-receive.py b/bin/pulse-dbus-receive.py
new file mode 100755
index 0000000..6126c76
--- /dev/null
+++ b/bin/pulse-dbus-receive.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+
+import os, sys
+import dbus
+import gobject
+import subprocess
+from dbus.mainloop.glib import DBusGMainLoop
+
+home = os.path.expanduser('~')
+safe = {
+    'muted': False,
+    'volume': []
+}
+BAR_WIDTH = 50
+
+def output_volume(vol):
+    bar = min(vol * BAR_WIDTH / 100, BAR_WIDTH)
+    print('^cs()')
+    print('^fg(#999)^r(%dx6)^fg(#444)^r(%dx6)^fg()' % (bar, BAR_WIDTH - bar))
+    sys.stdout.flush()
+
+def output_mute(mute):
+    if mute:
+        print('^tw()^fg(#FF0000)^i(%s/.dzen2/icons/vol-mute.xbm)' % home)
+    else:
+        print('^tw()^fg()^i(%s/.dzen2/icons/vol-hi.xbm)' % home)
+    sys.stdout.flush()
+
+def init():
+    proc = subprocess.Popen('amixer sget Master', shell=True, stdout=subprocess.PIPE)
+    amixer_stdout = proc.communicate()[0]
+    proc.wait()
+
+    try:
+        volume_start = amixer_stdout.find('[') + 1
+        volume_end = amixer_stdout.find('%]', volume_start)
+        mute_start = amixer_stdout.find('[', volume_end) + 1
+        mute_end = amixer_stdout.find(']', mute_start)
+
+        safe['volume'] = int(amixer_stdout[volume_start:volume_end])
+        safe['muted'] = (amixer_stdout[mute_start:mute_end] == 'off')
+
+        output_mute(safe['muted'])
+        output_volume(safe['volume'])
+    except:
+        pass
+
+def change_volume(iface, muted, volume):
+    if 'alsa_output.pci-' in iface:
+        if safe['muted'] != muted:
+            safe['muted'] = muted
+            output_mute(muted);
+
+        vol = max(volume)
+        if safe['volume'] != vol:
+            safe['volume'] = vol
+            output_volume(vol)
+
+if __name__ == '__main__':
+    init()
+    DBusGMainLoop(set_as_default=True)
+    bus = dbus.SessionBus()
+    bus.add_signal_receiver(change_volume, "VolumeChange")
+
+    loop = gobject.MainLoop()
+    loop.run()
diff --git a/bin/sound.sh b/bin/sound.sh
deleted file mode 100644
index 382c461..0000000
--- a/bin/sound.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-function print_mute {
-	local stat VOL MUTE
-
-	stat=$(amixer -c0 get Master | grep "Front Left:")		
-	VOL=$(sed  's/^.*\[\(.*\)%\].*$/\1/g' <<< "${stat}")
-	MUTE=$(sed  's/^.*\[\([^\[]*\)\]$/\1/g' <<< "${stat}")
-	
-	if [ "${MUTE}" == "off" -o "${MUTE}" == "0" ]
-	then
-		echo "^tw()^fg(#FF0000)^i(${HOME}/.dzen2/icons/vol-mute.xbm)"
-	else
-		echo "^tw()^fg()^i(${HOME}/.dzen2/icons/vol-hi.xbm)"
-	fi
-	
- 	echo "$(echo "$VOL" | tail -n 1)" | gdbar -h 6 -w 50 -fg '#999' -bg '#444'
-}
-
-print_mute
-inotifywait -q -m /dev/snd/controlC0 -e ACCESS | while read i
-do
-	print_mute
-done
-- 
cgit v1.2.3