bumblebee-status/bumblebee_status/modules/contrib/amixer.py

90 lines
2.5 KiB
Python
Raw Normal View History

2020-05-28 08:23:47 +02:00
"""get volume level or control it
2020-04-13 09:14:22 +02:00
2020-04-13 09:19:18 +02:00
Parameters:
2020-05-28 08:23:47 +02:00
* amixer.device: Device to use (default is Master,0)
* amixer.percent_change: How much to change volume by when scrolling on the module (default is 4%)
contributed by `zetxx <https://github.com/zetxx>`_ - many thanks!
2020-05-28 08:23:47 +02:00
input handling contributed by `ardadem <https://github.com/ardadem>`_ - many thanks!
2020-04-13 09:14:22 +02:00
"""
import re
2020-04-13 09:19:18 +02:00
import core.module
import core.widget
2020-05-28 08:23:47 +02:00
import core.input
2020-04-13 09:14:22 +02:00
2020-04-13 09:19:18 +02:00
import util.cli
2020-05-28 08:23:47 +02:00
import util.format
2020-04-13 09:19:18 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.volume))
2020-04-13 09:19:18 +02:00
self.__level = "n/a"
2020-04-13 09:19:18 +02:00
self.__muted = True
2020-05-28 08:23:47 +02:00
self.__device = self.parameter("device", "Master,0")
self.__change = util.format.asint(
self.parameter("percent_change", "4%").strip("%"), 0, 100
)
events = [
{
"type": "mute",
"action": self.toggle,
"button": core.input.LEFT_MOUSE,
},
{
"type": "volume",
"action": self.increase_volume,
"button": core.input.WHEEL_UP,
},
{
"type": "volume",
"action": self.decrease_volume,
"button": core.input.WHEEL_DOWN,
},
]
for event in events:
core.input.register(self, button=event["button"], cmd=event["action"])
def toggle(self, event):
self.set_parameter("toggle")
def increase_volume(self, event):
self.set_parameter("{}%+".format(self.__change))
def decrease_volume(self, event):
self.set_parameter("{}%-".format(self.__change))
def set_parameter(self, parameter):
util.cli.execute("amixer -q set {} {}".format(self.__device, parameter))
2020-04-13 09:14:22 +02:00
def volume(self, widget):
if self.__level == "n/a":
2020-04-13 09:19:18 +02:00
return self.__level
m = re.search(r"([\d]+)\%", self.__level)
2020-04-13 09:19:18 +02:00
self.__muted = True
2020-04-13 09:14:22 +02:00
if m:
if m.group(1) != "0" and "[on]" in self.__level:
2020-04-13 09:19:18 +02:00
self.__muted = False
return "{}%".format(m.group(1))
2020-04-13 09:14:22 +02:00
else:
return "0%"
2020-04-13 09:14:22 +02:00
2020-04-13 09:19:18 +02:00
def update(self):
2020-04-13 09:14:22 +02:00
try:
self.__level = util.cli.execute(
2020-05-28 08:23:47 +02:00
"amixer get {}".format(self.__device)
)
2020-04-13 09:14:22 +02:00
except Exception as e:
self.__level = "n/a"
2020-04-13 09:14:22 +02:00
def state(self, widget):
2020-04-13 09:19:18 +02:00
if self.__muted:
return ["warning", "muted"]
return ["unmuted"]
2020-04-13 09:19:18 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4