#!/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()