Skip to content

Commit

Permalink
Add pulseaudio widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ojv committed May 17, 2017
1 parent ff21515 commit 45ccfa9
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
11 changes: 11 additions & 0 deletions awk/pulseaudio-sink.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# only read from specified sink
$1 == "Sink" { if (int($2) == sink) { read = 1 }
else { read = 0 } }
# skip to next line if we're not reading
!read { next }
# average left and right speaker
$1 == "Volume:" { vol = (int($5) + int($12)) / 2 }
$1 == "Mute:" { m = $2 }
# return volume negated if sink is muted
END { if (m == "yes") { vol = -vol }
print vol }
159 changes: 159 additions & 0 deletions widgets/pulseaudio.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
-- usage {{{1
--[[
Requirements:
- Battery icons in theme:
- beautiful["icon-pulseaudio-V"] = <filename>
where V is max, med, min, 0 and muted
Configuration in rc.lua, here shown with the default settings:
local conky = require("conky")
conky.config.pulseaudio = {
sink = 0, the sink to target
step = 10, volume percentage to increment/decrement
slider = { configuration of the slider appearance, see:
forced_width = 50, awesomewm.org/apidoc/wibox.widget.slider.html
handle_width = 3,
bar_height = 3,
},
}
--]]


-- init {{{1
local beautiful = require("beautiful")
local awful = require("awful")
local wibox = require("wibox")
local util = require("conky/util")

local settings = {
sink = 0,
step = 10,
slider = {},
}

local slider_defaults = {
value = 50,
forced_width = 50,
handle_width = 3,
bar_height = 3,
}

local slider_immutable = {
minimum = 0,
maximum = 100,
visible = false,
widget = wibox.widget.slider,
}

local function setter_for(sink) -- {{{1
local cmd = "pactl set-sink-mute " .. sink .. " false;" ..
"pactl set-sink-volume " .. sink .. " "
return function(perc)
awful.spawn.with_shell(cmd .. perc .. "%")
end
end

local function mute_toggle_for(sink) -- {{{1
local cmd = "pactl set-sink-mute " .. sink .. " toggle"
return function()
awful.spawn.with_shell(cmd)
end
end

-- widget constructor {{{1
return function(user_settings)
for k,v in pairs(user_settings) do
settings[k] = v
end

local slider = wibox.widget(awful.util.table.join(
slider_defaults,
settings.slider,
slider_immutable))

local current_icon = nil
local current_volume = 0
local muted = false
local set_volume = setter_for(settings.sink)
local mute_toggle = mute_toggle_for(settings.sink)

local updater = function(update, _, iconbox, _)
local icon = "icon-pulseaudio-"
local volume = tonumber(update) or 0
if volume < 0 then
muted = true
volume = volume * -1
else
muted = false
end

if muted then
icon = icon .. "muted"
elseif volume == 0 then
icon = icon .. "0"
elseif volume < 33 then
icon = icon .. "min"
elseif volume < 66 then
icon = icon .. "med"
else
icon = icon .. "max"
end

if volume ~= current_volume then
slider.value = volume
current_volume = volume
end

if icon ~= current_icon then
iconbox:set_image(beautiful[icon] or beautiful["icon-pulseaudio-muted"])
current_icon = icon
end

end

return {
slider,
{
icon = beautiful["icon-pulseaudio-med"],
conky = "${exec pactl list sinks | " ..
util.awk("pulseaudio-sink", { sink = settings.sink }) .. "}",
conkybox = { visible = false },

signals = {
['mouse::enter'] = function() slider.visible = true end,
['mouse::leave'] = function() slider.visible = false end
},

buttons = {
{ {}, 1, function(_, iconbox, _)
mute_toggle()
muted = not muted
updater("" .. slider.value, nil, iconbox)
end },
{ {}, 4, function(_, iconbox, _)
local new = slider.value + settings.step
if new > slider.maximum then
slider.value = slider.maximum
else
slider.value = new
end
set_volume(slider.value)
updater("" .. slider.value, nil, iconbox)
end },
{ {}, 5, function(_, iconbox, _)
local new = slider.value - settings.step
if new < slider.minimum then
slider.value = slider.minimum
else
slider.value = new
end
set_volume(slider.value)
updater("" .. slider.value, nil, iconbox)
end },
},
updater = updater,
}
}
end

0 comments on commit 45ccfa9

Please sign in to comment.