usr bin env python import sys from optparse import OptionParser import

  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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
import sys
from optparse import OptionParser
import gtk
import gobject
import alsaaudio
class Mixer(object):
def __init__(self, *args, **kwargs):
# alsaaudio.Mixer don't allows for inheritance
self._mixer = alsaaudio.Mixer(*args, **kwargs)
def __getattr__(self, attr):
return getattr(self._mixer, attr)
def getvolume(self):
return self._mixer.getvolume()[0]
def getmute(self):
return self._mixer.getmute()[0]
@property
def percent(self):
if self.getmute():
return 0
return float(self.getvolume()) / 100
@property
def label(self):
if self.getmute():
return 'Mute'
return '%s %%' % self.getvolume()
def togglemute(self):
self.setmute(not self.getmute())
def increasevol(self, amount):
vol = self.getvolume() + amount
if vol < 0:
vol = 0
elif vol > 100:
vol = 100
self.setvolume(vol)
def decreasevol(self, amount):
self.increasevol(-amount)
class ProgressBar(object):
"""
Class to display volume bar
"""
def timeout_callback(self):
gtk.main_quit()
return False
def __init__(self, fraction, label, time=1000):
self.window = gtk.Window(gtk.WINDOW_POPUP)
self.window.set_border_width(0)
self.window.set_default_size(180, -1)
self.window.set_position(gtk.WIN_POS_CENTER)
timer = gobject.timeout_add(time, self.timeout_callback)
self.bar = gtk.ProgressBar()
self.bar.set_fraction(fraction)
self.bar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
self.bar.set_text(label)
self.bar.show()
self.window.add(self.bar)
self.window.show()
if __name__ == '__main__':
usage = 'usage: %prog [options] [argument]'
parser = OptionParser(usage)
parser.add_option('-q', '--quiet', action="store_true", default=False,
help="don't display the gtk progressbar, just change the volume")
parser.add_option('-t', '--time', default=1000, type=int,
help="time to display progressbar")
parser.add_option('-i', '--increase', action="store_true", default=False,
help="increase volume by argument")
parser.add_option('-d', '--decrease', action="store_true", default=False,
help="decrease volume by argument")
parser.add_option('-c', '--control', default="Master",
help="specify the mixer control (default Master)")
parser.add_option('-m', '--mute', action="store_true", default=False,
help="toggle mute")
parser.add_option('-s', '--status', action="store_true", default=False,
help="display current volume status without modifying it")
opts, args = parser.parse_args()
if ((opts.increase and opts.decrease) or
(opts.mute and (opts.increase or opts.decrease))):
parser.error("opts --mute, --increase and --descrease are mutually exclusive")
if not (opts.increase or opts.decrease or opts.mute or opts.status):
parser.print_help()
sys.exit()
try:
amount = int(args[0])
except ValueError:
parser.error("argument should be an integer value")
except IndexError:
amount = 2
mixer = Mixer(opts.control)
if opts.mute:
mixer.togglemute()
elif opts.increase:
mixer.increasevol(amount)
elif opts.decrease:
mixer.decreasevol(amount)
if not opts.quiet:
ProgressBar(mixer.percent, mixer.label, opts.time)
gtk.main()