Skip to content

Instantly share code, notes, and snippets.

@zinozzino
Last active January 15, 2016 09:36
Show Gist options
  • Save zinozzino/6a40fa70d6e916661fdf to your computer and use it in GitHub Desktop.
Save zinozzino/6a40fa70d6e916661fdf to your computer and use it in GitHub Desktop.
Pulseaudio widget for awesome using lain
--[[
Inspired by
Lain (https://github.com/copycat-killer/lain),
APW (https://github.com/mokasin/apw)
--]]
local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local awful = require("awful")
local beautiful = require("beautiful")
local naughty = require("naughty")
local math = { modf = math.modf,
floor = math.floor }
local mouse = mouse
local string = { format = string.format,
match = string.match,
gmatch = string.gmatch,
rep = string.rep }
local tonumber = tonumber
local setmetatable = setmetatable
-- Pulseaudio volume bar
-- lain.widgets.pulsebar
local pulsebar = {
default_sink = "",
step = 0.03,
colors = {
background = beautiful.bg_normal,
mute = "#EB8F8F",
unmute = "#A4CE8A"
},
mixer = "pavucontrol",
notifications = {
font = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
font_size = "11",
color = beautiful.fg_normal,
bar_size = 18,
screen = 1
},
_current_level = 0,
_muted = false
}
function pulsebar.notify()
pulsebar.update()
local preset = {
title = "",
text = "",
timeout = 0.5,
screen = pulsebar.notifications.screen,
font = pulsebar.notifications.font .. " " ..
pulsebar.notifications.font_size,
fg = pulsebar.notifications.color
}
if pulsebar._muted
then
preset.title = "Muted"
else
preset.title = pulsebar._current_level .. "%"
end
int = math.modf((pulsebar._current_level / 100) * pulsebar.notifications.bar_size)
preset.text = "["
.. string.rep("|", int)
.. string.rep(" ", pulsebar.notifications.bar_size - int)
.. "]"
if pulsebar.followmouse then
preset.screen = mouse.screen
end
if pulsebar._notify ~= nil then
pulsebar._notify = naughty.notify ({
replaces_id = pulsebar._notify.id,
preset = preset,
})
else
pulsebar._notify = naughty.notify ({
preset = preset,
})
end
end
local function worker(args)
local args = args or {}
local timeout = args.timeout or 5
local settings = args.settings or function() end
local width = args.width or 63
local height = args.heigth or 1
local ticks = args.ticks or false
local ticks_size = args.ticks_size or 7
local vertical = args.vertical or false
pulsebar.cmd = args.cmd or "pacmd"
pulsebar.default_sink = args.default_sink or pulsebar.default_sink
pulsebar.step = args.step or pulsebar.step
pulsebar.colors = args.colors or pulsebar.colors
pulsebar.notifications = args.notifications or pulsebar.notifications
pulsebar.followmouse = args.followmouse or false
pulsebar.bar = awful.widget.progressbar()
pulsebar.bar:set_background_color(pulsebar.colors.background)
pulsebar.bar:set_color(pulsebar.colors.unmute)
pulsebar.tooltip = awful.tooltip({ objects = { pulsebar.bar } })
pulsebar.bar:set_width(width)
pulsebar.bar:set_height(height)
pulsebar.bar:set_ticks(ticks)
pulsebar.bar:set_ticks_size(ticks_size)
pulsebar.bar:set_vertical(vertical)
function pulsebar.setColor(mute)
end
function pulsebar.update()
-- Get mixer control contents
local f = io.popen(pulsebar.cmd .. " dump")
if f == nil then
return false
end
local out = f:read("*a")
f:close()
default_sink = string.match(out, "set%-default%-sink ([^\n]+)")
if default_sink == nil then
default_sink = ""
pulsebar.default_sink = ""
return false
end
pulsebar.default_sink = default_sink
local volu
for sink, value in string.gmatch(out, "set%-sink%-volume ([^%s]+) (0x%x+)") do
if sink == default_sink then
volu = tonumber(value) / 0x10000
end
end
local mute
for sink, value in string.gmatch(out, "set%-sink%-mute ([^%s]+) (%a+)") do
if sink == default_sink then
mute = value
end
end
pulsebar._current_level = volu
pulsebar.bar:set_value(volu)
if not mute and volu == 0 or mute == "yes"
then
pulsebar._muted = true
pulsebar.tooltip:set_text (" [Muted] ")
pulsebar.bar:set_color(pulsebar.colors.mute)
else
pulsebar._muted = false
pulsebar.tooltip:set_text(string.format(" %s%% ", math.floor(volu * 100)))
pulsebar.bar:set_color(pulsebar.colors.unmute)
end
volume_now = {}
volume_now.level = math.floor(volu * 100)
volume_now.status = mute
settings()
end
function pulsebar.SetVolume(vol)
if vol > 1 then
vol = 1
end
if vol < 0 then
vol = 0
end
vol = vol * 0x10000
awful.util.spawn_with_shell(pulsebar.cmd .. " set-sink-volume " .. pulsebar.default_sink .. " " .. string.format("0x%x", math.floor(vol)))
end
function pulsebar.Up()
pulsebar.SetVolume(pulsebar._current_level + pulsebar.step)
pulsebar.update()
end
function pulsebar.Down()
pulsebar.SetVolume(pulsebar._current_level - pulsebar.step)
pulsebar.update()
end
function pulsebar.ToggleMute()
if pulsebar._muted then
awful.util.spawn_with_shell(pulsebar.cmd .. " set-sink-mute " .. pulsebar.default_sink .. " 0")
else
awful.util.spawn_with_shell(pulsebar.cmd .. " set-sink-mute " .. pulsebar.default_sink .. " 1")
end
pulsebar.update()
end
function pulsebar.LaunchMixer()
awful.util.spawn_with_shell( pulsebar.mixer )
end
pulsebar.bar:buttons (awful.util.table.join (
awful.button ({}, 1, function()
pulsebar.ToggleMute()
end),
awful.button ({}, 3, function()
pulsebar.LaunchMixer()
end),
awful.button ({}, 4, function()
pulsebar.Up()
end),
awful.button ({}, 5, function()
pulsebar.Down()
end)
))
timer_id = string.format("pulsebar-%s-%s", pulsebar.cmd, pulsebar.default_sink)
newtimer(timer_id, timeout, pulsebar.update)
return pulsebar
end
return setmetatable(pulsebar, { __call = function(_, ...) return worker(...) end })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment