From 6876729d8e5c205d63eeb2b05c997ab1f4f92002 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 8 Jul 2015 06:38:42 +0200 Subject: pulse-dbus-receive: Use native dbus-protocol Rewrite to use the native module-dbus-protocol for pulse. This removes the dependency for amixer, too. --- bin/pulse-dbus-receive.py | 80 ++++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/bin/pulse-dbus-receive.py b/bin/pulse-dbus-receive.py index 6126c76..347fa66 100755 --- a/bin/pulse-dbus-receive.py +++ b/bin/pulse-dbus-receive.py @@ -6,17 +6,20 @@ import dbus import gobject import subprocess from dbus.mainloop.glib import DBusGMainLoop +import sys home = os.path.expanduser('~') safe = { + 'sink': '/org/pulseaudio/core1/sink0', 'muted': False, - 'volume': [] + 'volume': 0, + 'baseVolume': 0, } BAR_WIDTH = 50 def output_volume(vol): - bar = min(vol * BAR_WIDTH / 100, BAR_WIDTH) print('^cs()') + bar = min(vol * BAR_WIDTH / 100, BAR_WIDTH) print('^fg(#999)^r(%dx6)^fg(#444)^r(%dx6)^fg()' % (bar, BAR_WIDTH - bar)) sys.stdout.flush() @@ -27,41 +30,60 @@ def output_mute(mute): 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() +def dbus_connect(): + if 'PULSE_DBUS_SERVER' in os.environ: + address = os.environ['PULSE_DBUS_SERVER'] + else: + bus = dbus.SessionBus() + server_lookup = bus.get_object("org.PulseAudio1", "/org/pulseaudio/server_lookup1") + address = server_lookup.Get("org.PulseAudio.ServerLookup1", "Address", dbus_interface="org.freedesktop.DBus.Properties") + return dbus.connection.Connection(address) - 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) +def update_volume(volumes, force=False): + volume = (max(volumes) * 100)/safe['baseVolume'] + if safe['volume'] != volume or force: + safe['volume'] = volume + output_volume(safe['volume']) - safe['volume'] = int(amixer_stdout[volume_start:volume_end]) - safe['muted'] = (amixer_stdout[mute_start:mute_end] == 'off') +def update_muted(muted, force=False): + if safe['muted'] != muted or force: + safe['muted'] = muted + output_mute(muted) - output_mute(safe['muted']) - output_volume(safe['volume']) - except: - pass +def signal_cb(*args, **kwargs): + msg = kwargs['msg'] -def change_volume(iface, muted, volume): - if 'alsa_output.pci-' in iface: - if safe['muted'] != muted: - safe['muted'] = muted - output_mute(muted); + if msg.get_path() != safe['sink']: + return - vol = max(volume) - if safe['volume'] != vol: - safe['volume'] = vol - output_volume(vol) + if msg.get_interface() == 'org.PulseAudio.Core1.Device' and \ + msg.get_member() == 'VolumeUpdated': + update_volume(args[0]) + elif msg.get_interface() == 'org.PulseAudio.Core1.Device' and \ + msg.get_member() == 'MuteUpdated': + update_muted(args[0]) + else: + print msg + +def init(bus): + if len(sys.argv) > 1: + safe['sink'] = sys.argv[1] + + sink = bus.get_object('org.PulseAudio.Core1.Device', safe['sink']) + safe['baseVolume'] = sink.Get('org.PulseAudio.Core1.Device', 'BaseVolume') + + update_muted(sink.Get('org.PulseAudio.Core1.Device', 'Mute'), True) + update_volume(sink.Get('org.PulseAudio.Core1.Device', 'Volume'), True) if __name__ == '__main__': - init() DBusGMainLoop(set_as_default=True) - bus = dbus.SessionBus() - bus.add_signal_receiver(change_volume, "VolumeChange") + bus = dbus_connect() + init(bus) + + bus.add_signal_receiver(signal_cb, message_keyword='msg') + core = bus.get_object(object_path="/org/pulseaudio/core1") + core.ListenForSignal('org.PulseAudio.Core1.Device.VolumeUpdated', dbus.Array(signature='o')) + core.ListenForSignal('org.PulseAudio.Core1.Device.MuteUpdated', dbus.Array(signature='o')) loop = gobject.MainLoop() loop.run() -- cgit v1.2.3