#!/usr/bin/env python3 import argparse import time import selectors import enum import math import functions import subprocess parser = argparse.ArgumentParser(description='Pulseaudio volume manager for xmobar') parser.add_argument('output_pipe', type=argparse.FileType('w',bufsize=1)) parser.add_argument('command_pipe', type=argparse.FileType('r',bufsize=1)) args = parser.parse_args() class AudioState: _muted = False _volume = 0 @property def muted(self): return self._muted @muted.setter def muted(self, muted): if self._muted != muted: try: self._muted = muted self._pactl('set-sink-mute', str(int(muted))) except subprocess.CalledProcessError as e: pass @property def volume(self): return self._volume @volume.setter def volume(self, volume): if self.muted: self.muted = False if self._volume != volume: try: self._volume = volume self._pactl('set-sink-volume', str(volume)) except subprocess.CalledProcessError as e: pass def _pactl(self, command, arg): for i in range(6): subprocess.check_call(["pactl", command, str(i), arg]) audio_state = AudioState() def output_pipe(fd): bars = functions.action_bars(functions.create_bars(audio_state.volume) if not audio_state.muted else ' (mute) ', args.command_pipe.name) fd.writelines(bars+"\n") def command_pipe(fd): command = str.rstrip(fd.readline()) if len(command) == 0: return False if command[0] == '=': audio_state.volume = int(command[1:])*9000 elif command == 'm1': audio_state.muted = True elif command == 'm0': audio_state.muted = False elif command == 'mt': audio_state.muted = not audio_state.muted elif command == '+': audio_state.volume+=3000 elif command == '-': audio_state.volume-=3000 elif command == 'r': audio_state.volume=30000 return True #sel = selectors.DefaultSelector() #sel.register(args.output_pipe, selectors.EVENT_WRITE, data=output_pipe) #sel.register(args.command_pipe, selectors.EVENT_READ, data=command_pipe) while True: if command_pipe(args.command_pipe): output_pipe(args.output_pipe) else: time.sleep(1) #for key, events in sel.select(): # key.data(key.fileobj, events)