From 68e0b51178487f631b1e38183dca454f0cfc396e Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Thu, 15 Jun 2017 11:09:00 +0200 Subject: [PATCH] [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 --- bumblebee/modules/pulseaudio.py | 45 ++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index c4eb000..97f1c4a 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -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: