2016-10-31 14:27:51 +01:00
|
|
|
import re
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import bumblebee.module
|
|
|
|
import bumblebee.util
|
|
|
|
|
2016-10-31 16:08:03 +01:00
|
|
|
def description():
|
|
|
|
module = __name__.split(".")[-1]
|
|
|
|
if module == "pasink":
|
|
|
|
return "Shows volume and mute status of the default PulseAudio Sink."
|
|
|
|
if module == "pasource":
|
|
|
|
return "Shows volume and mute status of the default PulseAudio Source."
|
|
|
|
return "See 'pasource'."
|
|
|
|
|
2016-11-05 16:18:53 +01:00
|
|
|
def parameters():
|
|
|
|
return [ "none" ]
|
|
|
|
|
|
|
|
|
2016-10-31 14:27:51 +01:00
|
|
|
class Module(bumblebee.module.Module):
|
2016-11-05 14:26:02 +01:00
|
|
|
def __init__(self, output, config, alias):
|
|
|
|
super(Module, self).__init__(output, config, alias)
|
2016-10-31 14:27:51 +01:00
|
|
|
|
|
|
|
self._module = self.__module__.split(".")[-1]
|
|
|
|
self._left = 0
|
|
|
|
self._right = 0
|
|
|
|
self._mono = 0
|
|
|
|
self._mute = False
|
2016-11-01 08:15:57 +01:00
|
|
|
channel = "sink" if self._module == "pasink" else "source"
|
|
|
|
|
2016-11-05 15:28:33 +01:00
|
|
|
output.add_callback(module=self.instance(), button=3,
|
|
|
|
cmd="pavucontrol")
|
|
|
|
output.add_callback(module=self.instance(), button=1,
|
|
|
|
cmd="pactl set-{}-mute @DEFAULT_{}@ toggle".format(channel, channel.upper()))
|
|
|
|
output.add_callback(module=self.instance(), button=4,
|
|
|
|
cmd="pactl set-{}-volume @DEFAULT_{}@ +2%".format(channel, channel.upper()))
|
|
|
|
output.add_callback(module=self.instance(), button=5,
|
|
|
|
cmd="pactl set-{}-volume @DEFAULT_{}@ -2%".format(channel, channel.upper()))
|
2016-10-31 14:27:51 +01:00
|
|
|
|
2016-11-05 13:50:51 +01:00
|
|
|
def widgets(self):
|
2016-10-31 14:27:51 +01:00
|
|
|
res = subprocess.check_output(shlex.split("pactl info"))
|
|
|
|
channel = "sinks" if self._module == "pasink" else "sources"
|
|
|
|
name = None
|
2016-11-05 16:33:35 +01:00
|
|
|
for line in res.decode().split("\n"):
|
2016-10-31 14:27:51 +01:00
|
|
|
if line.startswith("Default Sink: ") and channel == "sinks":
|
|
|
|
name = line[14:]
|
|
|
|
if line.startswith("Default Source: ") and channel == "sources":
|
|
|
|
name = line[16:]
|
|
|
|
|
|
|
|
res = subprocess.check_output(shlex.split("pactl list {}".format(channel)))
|
|
|
|
|
|
|
|
found = False
|
2016-11-05 16:33:35 +01:00
|
|
|
for line in res.decode().split("\n"):
|
2016-10-31 14:27:51 +01:00
|
|
|
if "Name:" in line and found == True:
|
|
|
|
break
|
|
|
|
if name in line:
|
|
|
|
found = True
|
|
|
|
if "Mute:" in line and found == True:
|
|
|
|
self._mute = False if " no" in line.lower() else True
|
|
|
|
|
|
|
|
if "Volume:" in line and found == True:
|
|
|
|
m = None
|
|
|
|
if "mono" in line:
|
|
|
|
m = re.search(r'mono:.*\s*\/\s*(\d+)%', line)
|
|
|
|
else:
|
|
|
|
m = re.search(r'left:.*\s*\/\s*(\d+)%.*right:.*\s*\/\s*(\d+)%', line)
|
|
|
|
if not m: continue
|
|
|
|
|
|
|
|
if "mono" in line:
|
|
|
|
self._mono = m.group(1)
|
|
|
|
else:
|
|
|
|
self._left = m.group(1)
|
|
|
|
self._right = m.group(2)
|
|
|
|
result = ""
|
2016-11-05 16:33:35 +01:00
|
|
|
if int(self._mono) > 0:
|
2016-10-31 14:27:51 +01:00
|
|
|
result = "{}%".format(self._mono)
|
|
|
|
elif self._left == self._right:
|
|
|
|
result = "{}%".format(self._left)
|
|
|
|
else:
|
|
|
|
result="{}%/{}%".format(self._left, self._right)
|
2016-11-05 13:50:51 +01:00
|
|
|
return bumblebee.output.Widget(self, result)
|
2016-10-31 14:27:51 +01:00
|
|
|
|
2016-11-05 13:50:51 +01:00
|
|
|
def state(self, widget):
|
2016-10-31 14:27:51 +01:00
|
|
|
return "muted" if self._mute is True else "unmuted"
|
|
|
|
|
2016-11-05 13:50:51 +01:00
|
|
|
def warning(self, widget):
|
2016-10-31 14:27:51 +01:00
|
|
|
return self._mute
|
|
|
|
|
2016-11-05 13:50:51 +01:00
|
|
|
def critical(self, widget):
|
2016-10-31 14:27:51 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|