bumblebee-status/bumblebee_status/modules/contrib/pipewire.py
Shahan Arshad 04a222f3c8
Fix: pipewire.py module
mousewheel up and down events were initially not working on my bumblebee status bar. After verifying on the command line, it seems that wpctl set-volume command requires sink ID first and percentage change as the second arg.

steps to verify, assuming wireplumber is installed and sink ID is 32

```sh
wpctl set-volume 32 50%
```
will set the volume to 50%

```sh
wpctl set-volume 50% 32
```
will result in `Object '52' not found
2023-07-20 02:16:24 +05:00

90 lines
2.6 KiB
Python

"""get volume level or control it
Requires the following executable:
* wpctl
Parameters:
* wpctl.percent_change: How much to change volume by when scrolling on the module (default is 4%)
heavily based on amixer module
"""
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.__change = (
util.format.asint(self.parameter("percent_change", "4%").strip("%"), 0, 200)
/ 100.0
) # divide by 100 because wpctl represents 100% volume as 1.00, 50% as 0.50, etc
self.__id = self.parameter("sink_id") or "@DEFAULT_AUDIO_SINK@"
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):
util.cli.execute("wpctl set-mute {} toggle".format(self.__id))
def increase_volume(self, event):
util.cli.execute(
"wpctl set-volume --limit 1.0 {} {}+".format(self.__id, self.__change)
)
def decrease_volume(self, event):
util.cli.execute(
"wpctl set-volume --limit 1.0 {} {}-".format(self.__id, self.__change)
)
def volume(self, widget):
if self.__level == "N/A":
return self.__level
return "{}%".format(int(float(self.__level) * 100))
def update(self):
try:
# `wpctl get-volume` will return a string like "Volume: n.nn" or "Volume: n.nn [MUTED]"
volume = util.cli.execute("wpctl get-volume {}".format(self.__id))
v = re.search("\d\.\d+", volume)
m = re.search("MUTED", volume)
self.__level = v.group()
self.__muted = True if m else False
except Exception:
self.__level = "N/A"
def state(self, widget):
if self.__muted:
return ["warning", "muted"]
return ["unmuted"]
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4