diff --git a/i3pystatus/pulseaudio/__init__.py b/i3pystatus/pulseaudio/__init__.py index 09f9977..9f11721 100644 --- a/i3pystatus/pulseaudio/__init__.py +++ b/i3pystatus/pulseaudio/__init__.py @@ -6,6 +6,7 @@ from .pulse import * from i3pystatus.core.command import execute from i3pystatus import Module +import subprocess class PulseAudio(Module, ColorRangeModule): @@ -79,9 +80,6 @@ class PulseAudio(Module, ColorRangeModule): self.colors = self.get_hex_color_range(self.color_muted, self.color_unmuted, 100) - # Check that we have amixer for toggling mute/unmute and incrementing/decrementing volume - self.has_amixer = shutil.which('amixer') is not None - def request_update(self, context): """Requests a sink info update (sink_info_cb is called)""" pa_operation_unref(pa_context_get_sink_info_by_name( @@ -90,12 +88,24 @@ class PulseAudio(Module, ColorRangeModule): def success_cb(self, context, success, userdata): pass + @property + def sink(self): + sinks = subprocess.Popen(['pactl', 'list', 'short', 'sinks'], stdout=subprocess.PIPE).stdout.read() + bestsink = None + state = b'DEFAULT' + for sink in sinks.splitlines(): + attribs = sink.split() + if attribs[-1] == b'RUNNING': + bestsink = attribs[1] + state = 'RUNNING' + elif attribs[-1] == b'IDLE' and state == b'DEFAULT': + bestsink = attribs[1] + state = b'IDLE' + return bestsink + def server_info_cb(self, context, server_info_p, userdata): """Retrieves the default sink and calls request_update""" server_info = server_info_p.contents - - self.sink = server_info.default_sink_name - self.request_update(context) def context_notify_cb(self, context, _): @@ -166,20 +176,10 @@ class PulseAudio(Module, ColorRangeModule): } def switch_mute(self): - if self.has_amixer: - command = "amixer -q -D pulse sset Master " - if self.currently_muted: - command += 'unmute' - else: - command += 'mute' - execute(command) + subprocess.Popen(['pactl', 'set-sink-mute', self.sink, "toggle"]) def increase_volume(self): - if self.has_amixer: - command = "amixer -q -D pulse sset Master %s%%+" % self.step - execute(command) + subprocess.Popen(['pactl', 'set-sink-volume', self.sink, "+%s%%" % self.step]) def decrease_volume(self): - if self.has_amixer: - command = "amixer -q -D pulse sset Master %s%%-" % self.step - execute(command) + subprocess.Popen(['pactl', 'set-sink-volume', self.sink, "-%s%%" % self.step])