From a1d94d4355a7553dfd9de793f4d50d7be5f072e7 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Fri, 9 Sep 2022 21:34:32 +0200 Subject: [PATCH] [modules/pulsectl] add mouse actions add toggle mute on click and volume up/down on scroll --- bumblebee_status/modules/core/pulsectl.py | 48 ++++++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/bumblebee_status/modules/core/pulsectl.py b/bumblebee_status/modules/core/pulsectl.py index 70fc356..46f3775 100644 --- a/bumblebee_status/modules/core/pulsectl.py +++ b/bumblebee_status/modules/core/pulsectl.py @@ -4,10 +4,11 @@ import pulsectl import core.module import core.widget -import core.decorators import core.input import core.event +import util.format + class Module(core.module.Module): def __init__(self, config, theme, type): super().__init__(config, theme, core.widget.Widget(self.display)) @@ -17,14 +18,57 @@ class Module(core.module.Module): self.__volume = "n/a" self.__muted = False + self.__change = util.format.asint( + self.parameter("percent_change", "2%").strip("%"), 0, 100 + ) + + events = [ + { + "type": "mute", + "action": self.toggle_mute, + "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"]) + self.process(None) def display(self, _): return f"{int(self.__volume*100)}%" + def toggle_mute(self, _): + with pulsectl.Pulse(self.id + "vol") as pulse: + dev = pulse.sink_list()[0] if self.__type == "sink" else pulse.source_list()[1] + pulse.mute(dev, not self.__muted) + + def change_volume(self, amount): + with pulsectl.Pulse(self.id + "vol") as pulse: + dev = pulse.sink_list()[0] if self.__type == "sink" else pulse.source_list()[1] + vol = dev.volume + vol.value_flat += amount + pulse.volume_set(dev, vol) + + def increase_volume(self, _): + self.change_volume(self.__change/100.0) + + def decrease_volume(self, _): + self.change_volume(-self.__change/100.0) + def process(self, _): with pulsectl.Pulse(self.id + "proc") as pulse: - dev = pulse.sink_list()[0] if self.__type == "sink" else pulse.source_list()[0] + dev = pulse.sink_list()[0] if self.__type == "sink" else pulse.source_list()[1] self.__volume = dev.volume.value_flat self.__muted = dev.mute core.event.trigger("update", [self.id], redraw_only=True)