awesomewm/modules/volume/util.lua

165 lines
3.3 KiB
Lua
Executable File

local volume = {}
-- Create pamixer option string
volume.pamixer_options = ""
if (config.volume.pamixer_sink ~= "") then
volume.pamixer_options = volume.pamixer_options .. " --sink " .. config.volume.pamixer_sink
end
volume.commands = {
up = function(value, callback)
awful.spawn.easy_async(
"pamixer --increase " .. value .. " "
.. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
down = function(value, callback)
awful.spawn.easy_async(
"pamixer --decrease " .. value .. " "
.. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
set = function(value, callback)
awful.spawn.easy_async(
"pamixer --set_volume " .. tostring(value) .. " " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
mute = function(callback)
awful.spawn.easy_async(
"pamixer --mute " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
unmute = function(callback)
awful.spawn.easy_async(
"pamixer --unmute " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
watch = function(timeout, callback, widget)
awful.widget.watch(
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
timeout, callback, widget
)
end,
get = function(callback)
local muted
local value
awful.spawn.easy_async(
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
muted = string.match(stdout, "(%w%w%w%w%w?) ") -- "true" or "false"
muted = (muted == "true")
value = string.match(stdout, "(%d?%d?%d)") -- (\d?\d?\d)\%)
if (value == nil) then
value = false
else
value = tonumber(string.format("% 3d", value))
end
-- Value will be "false" if audio not connected,
-- needs to be handled gracefully
callback({
mute = muted,
value = value
})
end
)
end
}
awesome.connect_signal("module::volume:update_read",
function()
volume.commands.get(
function(status)
awesome.emit_signal(
"module::volume:update",
status
)
end
)
end
)
volume.volume_up = function(value)
if volume.muted then
volume.unmute()
end
volume.commands.up(value, function()
core.sound.play("volume_up")
end)
end
volume.volume_down = function(value)
if volume.muted then
volume.unmute()
end
volume.commands.down(value, function()
core.sound.play("volume_down")
end)
end
volume.mute = function()
volume.muted = true
volume.commands.mute()
end
volume.unmute = function()
volume.muted = false
volume.commands.unmute()
end
volume.toggle_mute = function()
volume.commands.get(function(status)
if status.mute then
volume.unmute()
else
volume.mute()
end
end)
end
return volume