i3pystatus/i3pystatus/alsa.py
Matthieu Coudron d31cc380ef Follow up of https://github.com/enkore/i3pystatus/pull/149#issuecomment-67712619 .
This commit removes and replaces all the old methods 'on_*' by settings
with the same name. The old methods were renamed into more explicit names that can be used for the callbacks like "next_song","mute" etc...
For instance, you can test with:
status.register("clock",
	format=[
	    ("Format 0",'Europe/London'),
	    ("%a %-d Format 1",'Europe/Dublin'),
	    "%a %-d %b %X format 2",
	    ("%a %-d %b %X format 3", 'Europe/Paris'),
	],
	on_leftclick= ["urxvtc"] , # launch urxvtc on left click
	on_rightclick= ["scroll_format", 2] , # update format by steps of 2
	log_level=logging.DEBUG,
	)

This way much code could be removed from other modules, though I did it only for the clock module here.
2014-12-20 15:30:41 +01:00

102 lines
2.8 KiB
Python

from alsaaudio import Mixer, ALSAAudioError
from i3pystatus import IntervalModule
class ALSA(IntervalModule):
"""
Shows volume of ALSA mixer. You can also use this for inputs, btw.
Requires pyalsaaudio
.. rubric:: Available formatters
* `{volume}` — the current volume in percent
* `{muted}` — the value of one of the `muted` or `unmuted` settings
* `{card}` — the associated soundcard
* `{mixer}` — the associated ALSA mixer
"""
interval = 1
settings = (
"format",
("format_muted", "optional format string to use when muted"),
("mixer", "ALSA mixer"),
("mixer_id", "ALSA mixer id"),
("card", "ALSA sound card"),
("increment", "integer percentage of max volume to in/decrement volume on mousewheel"),
"muted", "unmuted",
"color_muted", "color",
"channel"
)
muted = "M"
unmuted = ""
color_muted = "#AAAAAA"
color = "#FFFFFF"
format = "♪: {volume}"
format_muted = None
mixer = "Master"
mixer_id = 0
card = 0
channel = 0
increment = 5
alsamixer = None
has_mute = True
on_upscroll = "increase_volume"
on_downscroll = "decrease_volume"
on_leftclick = "switch_mute"
on_rightclick = on_leftclick
def init(self):
self.create_mixer()
try:
self.alsamixer.getmute()
except ALSAAudioError:
self.has_mute = False
self.fdict = {
"card": self.alsamixer.cardname(),
"mixer": self.mixer,
}
def create_mixer(self):
self.alsamixer = Mixer(
control=self.mixer, id=self.mixer_id, cardindex=self.card)
def run(self):
self.create_mixer()
muted = False
if self.has_mute:
muted = self.alsamixer.getmute()[self.channel] == 1
self.fdict["volume"] = self.alsamixer.getvolume()[self.channel]
self.fdict["muted"] = self.muted if muted else self.unmuted
if muted and self.format_muted is not None:
output_format = self.format_muted
else:
output_format = self.format
self.output = {
"full_text": output_format.format(**self.fdict),
"color": self.color_muted if muted else self.color,
}
def switch_mute(self):
if self.has_mute:
muted = self.alsamixer.getmute()[self.channel]
self.alsamixer.setmute(not muted)
def increase_volume(self, delta=None):
vol = self.alsamixer.getvolume()[self.channel]
self.alsamixer.setvolume(min(100, vol + (delta if delta else self.increment)))
def decrease_volume(self, delta=None):
vol = self.alsamixer.getvolume()[self.channel]
self.alsamixer.setvolume(max(0, vol - (delta if delta else self.increment)))