#!/usr/bin/env python __version__='0.1' __author__ ='Antonio Cuni ' __url__='http://codespeak.net/svn/user/antocuni/desklets/volume.py' import sys import alsaaudio class Mixer(object): def __init__(self): if 'Master' in alsaaudio.mixers(): self.mixer = alsaaudio.Mixer('Master') else: self.mixer = alsaaudio.Mixer('Front') self.vol = self.mixer.getvolume()[0] self.mute = self.mixer.getmute()[0] def up(self): self.vol = min(self.vol+5, 100) self.mixer.setvolume(self.vol) def down(self): self.vol = max(self.vol-5, 0) self.mixer.setvolume(self.vol) def toggle_mute(self): self.mute = not self.mute self.mixer.setmute(self.mute) def get_status(self): if self.mute: return 'muted' if self.vol < 33: return 'low' if self.vol < 66: return 'medium' return 'high' def test(m=None): if m is None: m = Mixer() print "" print "" #print "" print "" print "" print "" name = 'volume-%s.png' % m.get_status() print "" % name, print m.vol, '%' #print "" % m.vol def click(button): m = Mixer() if button == '1': # left click m.toggle_mute() elif button == '4': # mouse wheel up m.up() elif button == '5': # mouse wheel down m.down() test(m) def main(): if len(sys.argv) < 2: test() # for testing return action = sys.argv[1] args = sys.argv[2:] if action == 'test': test() elif action == 'click': click(*args) else: print 'unexpected action: %s' % action test() if __name__ == '__main__': try: main() except Exception, e: print repr(e) sys.exit(1)