From 3df0b7047faa33e625bba304ede2355fe0f4ed3a Mon Sep 17 00:00:00 2001 From: Arda Demir Date: Tue, 26 May 2020 18:23:19 +0300 Subject: [PATCH] [modules] New amixer module with input support --- bumblebee_status/modules/contrib/amixer2.py | 87 +++++++++++++++++++++ themes/icons/ascii.json | 8 ++ themes/icons/awesome-fonts.json | 4 + themes/icons/ionicons.json | 4 + 4 files changed, 103 insertions(+) create mode 100644 bumblebee_status/modules/contrib/amixer2.py diff --git a/bumblebee_status/modules/contrib/amixer2.py b/bumblebee_status/modules/contrib/amixer2.py new file mode 100644 index 0000000..b6d3e44 --- /dev/null +++ b/bumblebee_status/modules/contrib/amixer2.py @@ -0,0 +1,87 @@ +"""get volume level or control it + +Parameters: + * 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 `_ - many thanks! +""" +import re + +import core.module +import core.widget +import core.input + +import util.cli +import util.format + +class Module(core.module.Module): + def __init__(self, config, theme): + super().__init__(config, theme, core.widget.Widget(self.volume)) + + self._level = "n/a" + self._muted = True + 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)) + + def volume(self, widget): + if self._level == "n/a": + return self._level + m = re.search(r"([\d]+)\%", self._level) + self._muted = True + if m: + if m.group(1) != "0" and "[on]" in self._level: + self._muted = False + return "{}%".format(m.group(1)) + else: + return "0%" + + def update(self): + try: + self._level = util.cli.execute( + "amixer get {}".format(self._device) + ) + except Exception as e: + self._level = "n/a" + + def state(self, widget): + if self._muted: + return ["warning", "muted"] + return ["unmuted"] + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/icons/ascii.json b/themes/icons/ascii.json index 1913752..ab045bb 100644 --- a/themes/icons/ascii.json +++ b/themes/icons/ascii.json @@ -72,6 +72,14 @@ "prefix": "audio" } }, + "amixer2": { + "muted": { + "prefix": "audio(mute)" + }, + "unmuted": { + "prefix": "audio" + } + }, "pasource": { "muted": { "prefix": "mic(mute)" diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index e660c1d..b08ccf5 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -73,6 +73,10 @@ "muted": { "prefix": "" }, "unmuted": { "prefix": "" } }, + "amixer2": { + "muted": { "prefix": "" }, + "unmuted": { "prefix": "" } + }, "pasource": { "muted": { "prefix": "" }, "unmuted": { "prefix": "" } diff --git a/themes/icons/ionicons.json b/themes/icons/ionicons.json index 4b90133..f23ee77 100644 --- a/themes/icons/ionicons.json +++ b/themes/icons/ionicons.json @@ -55,6 +55,10 @@ "muted": { "prefix": "\uf3b9" }, "unmuted": { "prefix": "\uf3ba" } }, + "amixer2": { + "muted": { "prefix": "\uf3b9" }, + "unmuted": { "prefix": "\uf3ba" } + }, "pasource": { "muted": { "prefix": "\uf395" }, "unmuted": { "prefix": "\uf2ec" }