[modules/pulseaudio] Automatically restart pulseaudio daemon

If the current volume and mute status cannot be retrieved, the most
likely explanation is that the pulseaudio daemon is not running.
Automatically start it in such a case.

Also, add a parameter "autostart" to the pulseaudio module to disable
this behaviour in case it causes issues.

see #108
This commit is contained in:
Tobias Witek 2017-06-15 11:09:00 +02:00
parent 7e45e797f1
commit 68e0b51178

View file

@ -4,6 +4,9 @@
Aliases: pasink, pasource
Parameters:
* pulseaudio.autostart: If set to "true" (default), automatically starts the pulseaudio daemon if it is not running
Requires the following executable:
* pactl
"""
@ -31,6 +34,7 @@ class Module(bumblebee.engine.Module):
self._right = 0
self._mono = 0
self._mute = False
self._failed = False
channel = "sink" if self.name == "pasink" else "source"
self._patterns = [
@ -76,32 +80,43 @@ class Module(bumblebee.engine.Module):
return "n/a"
def volume(self, widget):
if self._failed == True:
return "n/a"
if int(self._mono) > 0:
return "{}%".format(self._mono)
elif self._left == self._right:
return "{}%".format(self._left)
else:
return "{}%/{}%".format(self._left, self._right)
return "n/a"
def update(self, widgets):
channel = "sinks" if self.name == "pasink" else "sources"
device = self._default_device()
try:
self._failed = False
channel = "sinks" if self.name == "pasink" else "sources"
device = self._default_device()
result = bumblebee.util.execute("pacmd list-{}".format(channel))
found = False
result = bumblebee.util.execute("pacmd list-{}".format(channel))
found = False
for line in result.split("\n"):
if device in line:
found = True
continue
if found == False:
continue
for pattern in self._patterns:
if not pattern["expr"] in line:
for line in result.split("\n"):
if device in line:
found = True
continue
if pattern["callback"](line) == False and found == True:
return
if found == False:
continue
for pattern in self._patterns:
if not pattern["expr"] in line:
continue
if pattern["callback"](line) == False and found == True:
return
except Exception:
self._failed = True
if self.parameter("autostart", "true") == "true":
try:
bumblebee.util.execute("pulseaudio --start")
self.update(widgets)
except Exception:
pass
def state(self, widget):
if self._mute: