My collection of plugins for the Qtile window manager.
git clone https://mcol.xyz/code/qtools
Log | Files | Refs | README

commit 24f2167c05001ccb990532f6ba47b04c66b14538
parent d5f3ad7666c6bcb1db84d651bbcf7429ec798677
Author: mcol <mcol@posteo.net>
Date:   Fri, 10 Jan 2020 22:07:27 +0000

add alsa volume level Notify plugin

Diffstat:
Aamixer/__init__.py | 1+
Aamixer/amixer.py | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/amixer/__init__.py b/amixer/__init__.py @@ -0,0 +1 @@ +from .amixer import Volume diff --git a/amixer/amixer.py b/amixer/amixer.py @@ -0,0 +1,67 @@ +""" +Qtile plugin to control an ALSA device volume level. + +Example usage: + + import qtools.amixer + vol = qtools.amixer.Volume() + keys.extend([EzKey(k, v) for k, v in { + '<XF86AudioMute>': vol.lazy_mute, + '<XF86AudioRaiseVolume>': vol.lazy_increase, + '<XF86AudioLowerVolume>': vol.lazy_decrease, + }.items()]) + +""" + + +import subprocess + +from libqtile.log_utils import logger +from qtools import Notifier + + +class Volume(Notifier): + defaults = [ + ('mixer', 'Master', 'ALSA mixer to control.'), + ('interval', 5, 'Percentage interval to change volume by.'), + ] + def __init__(self, **config): + Notifier.__init__(self, **config) + self.add_defaults(Volume.defaults) + + def increase(self, qtile=None): + volume = self._run(f'{self.interval}%+') + self.show(self.interval * round(volume/self.interval)) + + def decrease(self, qtile=None): + volume = self._run(f'{self.interval}%-') + self.show(self.interval * round(volume/self.interval)) + + def toggle(self, qtile=None): + volume = self._run('toggle') + self.show(self.interval * round(volume/self.interval)) + + def mute(self, qtile=None): + self._run('mute') + self.show('Muted') + + def unmute(self, qtile=None): + volume = self._run('unmute') + self.show(volume) + + def _run(self, setting): + try: + output = subprocess.run( + ['amixer', 'set', self.mixer, setting], + stdout=subprocess.PIPE, + ) + stdout = output.stdout.splitlines() + except subprocess.CalledProcessError as err: + logger.error(err.output.decode()) + return + + if len(stdout) == 5: + volume = int(stdout[4].decode().split()[3][1:-2]) + elif len(stdout) == 6: + volume = int(stdout[5].decode().split()[4][1:-2]) + return volume