Skip to content

Instantly share code, notes, and snippets.

@michael-pisman
Last active May 23, 2022 04:47
Show Gist options
  • Save michael-pisman/2ef65116d5884800a9820baad6ffa22b to your computer and use it in GitHub Desktop.
Save michael-pisman/2ef65116d5884800a9820baad6ffa22b to your computer and use it in GitHub Desktop.
A Qtile widget to control volume of the default pulseaudio sink
#!/usr/bin/python3
from os import system
from os import path
import sys, subprocess
from libqtile.widget import base
from pulsectl import Pulse, PulseVolumeInfo
# Path to config file
DEFAULT_DEVICE = '/home/mpisman/.screenlayout/'
DEFAULT_FILE = 'eDP'
pulse = Pulse('Qtile_Volume')
# Brighness Widget
class PAVolume(base._TextBox):
defaults = [
("padding", None, "Padding left and right. Calculated if None."),
("theme_path", None, "Path of the icons"),
("background", None, "Background color"),
("volume_app", None, "App to control volume"),
("update_interval", 0.1, "Update time in seconds.")]
def __init__(self, **config):
super().__init__("", **config)
base._TextBox.__init__(self, "", **config)
self.add_defaults(self.defaults)
self.sinks = None
self.default_sink = None
self.volume = -1
self.mute = 0
self.add_callbacks({
'Button1': self.cmd_mute,
'Button3': self.cmd_run_app,
'Button4': self.cmd_increase_vol,
'Button5': self.cmd_decrease_vol
})
# Update list of sinks and populate the 'sinks' disctionary
def update_sinks(self):
sinks = pulse.sink_list()
result = {}
for i in sinks:
result[i.name] = i
self.sinks = result
# Return volume of the default sink or -1 if no default sink is found
def get_volume(self):
self.default_sink = self.sinks.get(pulse.server_info().default_sink_name, None)
return round(self.default_sink.volume.value_flat*100) if self.default_sink else -1
def set_volume(self, new_value):
if new_value is None:
new_value = self.volume
new_value = new_value/100
volume = self.default_sink.volume
volume.value_flat = new_value
pulse.volume_set(self.default_sink, volume)
def update(self):
self.update_sinks()
vol = self.get_volume()
if vol == -1:
self.text = "{:^7}".format("ﱝ")
if vol != self.volume or self.mute != self.default_sink.mute:
self.mute = self.default_sink.mute
if self.mute == 1:
icon = "婢"
self.text = " {} {:>3} ".format(icon, vol)
else:
self.volume = vol
if vol <= 0:
icon = '婢'
elif vol <= 30:
icon = '奄'
elif vol < 80:
icon = '奔'
elif vol >= 80:
icon = '墳'
self.text = " {} {:>3} ".format(icon, vol)
self.bar.draw()
self.timeout_add(self.update_interval, self.update)
def draw(self):
self.drawer.clear(self.background or self.bar.background)
base._TextBox.draw(self)
self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.length)
def timer_setup(self):
self.timeout_add(self.update_interval, self.update)
self.update_sinks()
def cmd_increase_vol(self):
if self.volume < 100 and self.mute == 0:
self.set_volume(self.volume + 1)
def cmd_decrease_vol(self):
if self.volume > 0 and self.mute == 0:
self.set_volume(self.volume - 1)
def cmd_mute(self):
pulse.mute(self.default_sink, not self.mute)
def cmd_run_app(self):
if self.volume_app:
subprocess.Popen(self.volume_app, shell=False)
def main():
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment