diff --git a/daemon.py b/daemon.py index 439f2c3..7a00520 100755 --- a/daemon.py +++ b/daemon.py @@ -12,7 +12,7 @@ modules.Application( separator='' ), modules.ActionWrapperControl( - modules.PaCtlVolumeControl(), + modules.PulseCtlVolumeControl(), action='pavucontrol', buttons=modules.core.Button.RIGHT ), diff --git a/modules/__init__.py b/modules/__init__.py index d4ef7a4..14180f7 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -2,4 +2,4 @@ from .application import Application from .core import GroupedControl, ActionWrapperControl from .caffeine import CaffeineControl from .redshift import RedshiftControl -from .volume import PaCtlVolumeControl \ No newline at end of file +from .volume import PaCtlVolumeControl, VolumeControl, PulseCtlVolumeControl diff --git a/modules/core.py b/modules/core.py index e0df041..9b383dd 100644 --- a/modules/core.py +++ b/modules/core.py @@ -283,7 +283,7 @@ class WrappingControl(AbstractControl): return self.child.visible def periodic(self): - self.child.periodic() + return self.child.periodic() def load_state_ex(self, state): self.child.load_state_ex(state) diff --git a/modules/volume.py b/modules/volume.py index eaf593f..40647dd 100644 --- a/modules/volume.py +++ b/modules/volume.py @@ -139,3 +139,37 @@ class PaCtlVolumeControl(AbstractVolumeControl): logger.debug("Calling pactl: %s %s %s", command, i, arg) subprocess.check_call(["pactl", command, i, arg], stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + +try: + import pulsectl + + class PulseCtlVolumeControl(AbstractVolumeControl): + def __init__(self): + super().__init__() + self.__pulse = pulsectl.Pulse(self.__class__.__name__) + self.__default_sink = None + self.periodic() + + def periodic(self): + server_info = self.__pulse.server_info() + self.__default_sink = next(filter(lambda sink: sink.name == server_info.default_sink_name, self.__pulse.sink_list())) + prev_muted = self.muted + self.muted = bool(self.__default_sink.mute) + prev_volume = self.volume + if not self.muted: + self.volume = self.__default_sink.volume.value_flat + return prev_muted != self.muted or prev_volume != self.volume + + def _set_muted(self, muted: bool) -> bool: + self.__pulse.sink_mute(self.__default_sink.index, muted) + return True + + def _set_volume(self, volume: float) -> bool: + self.__default_sink.volume.value_flat = volume + self.__pulse.sink_volume_set(self.__default_sink.index, self.__default_sink.volume) + return True + + + VolumeControl = PulseCtlVolumeControl +except ImportError: + VolumeControl = PaCtlVolumeControl diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a15bd5a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pulsectl==16.12.4