summaryrefslogtreecommitdiffstats
path: root/bin/pulse-dbus-receive.py
blob: 6126c764c38a2584a4dcd1ff51ec17085399c86e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()