Pulseaudio: Display/control active sink
Make sink a property which checks which sink is currently active. Use pactl to control volumes which gets standard insalled with libpulse which is already a requirement.
This commit is contained in:
parent
0b49c4058a
commit
b711ba96ed
@ -6,6 +6,7 @@ from .pulse import *
|
|||||||
|
|
||||||
from i3pystatus.core.command import execute
|
from i3pystatus.core.command import execute
|
||||||
from i3pystatus import Module
|
from i3pystatus import Module
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
class PulseAudio(Module, ColorRangeModule):
|
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)
|
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):
|
def request_update(self, context):
|
||||||
"""Requests a sink info update (sink_info_cb is called)"""
|
"""Requests a sink info update (sink_info_cb is called)"""
|
||||||
pa_operation_unref(pa_context_get_sink_info_by_name(
|
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):
|
def success_cb(self, context, success, userdata):
|
||||||
pass
|
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):
|
def server_info_cb(self, context, server_info_p, userdata):
|
||||||
"""Retrieves the default sink and calls request_update"""
|
"""Retrieves the default sink and calls request_update"""
|
||||||
server_info = server_info_p.contents
|
server_info = server_info_p.contents
|
||||||
|
|
||||||
self.sink = server_info.default_sink_name
|
|
||||||
|
|
||||||
self.request_update(context)
|
self.request_update(context)
|
||||||
|
|
||||||
def context_notify_cb(self, context, _):
|
def context_notify_cb(self, context, _):
|
||||||
@ -166,20 +176,10 @@ class PulseAudio(Module, ColorRangeModule):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def switch_mute(self):
|
def switch_mute(self):
|
||||||
if self.has_amixer:
|
subprocess.Popen(['pactl', 'set-sink-mute', self.sink, "toggle"])
|
||||||
command = "amixer -q -D pulse sset Master "
|
|
||||||
if self.currently_muted:
|
|
||||||
command += 'unmute'
|
|
||||||
else:
|
|
||||||
command += 'mute'
|
|
||||||
execute(command)
|
|
||||||
|
|
||||||
def increase_volume(self):
|
def increase_volume(self):
|
||||||
if self.has_amixer:
|
subprocess.Popen(['pactl', 'set-sink-volume', self.sink, "+%s%%" % self.step])
|
||||||
command = "amixer -q -D pulse sset Master %s%%+" % self.step
|
|
||||||
execute(command)
|
|
||||||
|
|
||||||
def decrease_volume(self):
|
def decrease_volume(self):
|
||||||
if self.has_amixer:
|
subprocess.Popen(['pactl', 'set-sink-volume', self.sink, "-%s%%" % self.step])
|
||||||
command = "amixer -q -D pulse sset Master %s%%-" % self.step
|
|
||||||
execute(command)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user