awesomewm/modules/volume/util.lua

146 lines
2.7 KiB
Lua
Executable File

local volume = {}
volume.commands = {}
-- Create pamixer option string
volume.pamixer_options = ""
if (conf.pamixer_sink ~= "") then
volume.pamixer_options = volume.pamixer_options .. " --sink " .. conf.pamixer_sink
end
function volume.commands:up()
awful.spawn("pamixer --increase 5" .. volume.pamixer_options, false)
end
function volume.commands:down()
awful.spawn("pamixer --decrease 5" .. volume.pamixer_options, false)
end
function volume.commands:set(value)
awful.spawn("pamixer --set_volume" .. tostring(value) .. " " .. volume.pamixer_options, false)
end
function volume.commands:mute()
awful.spawn("pamixer --mute" .. volume.pamixer_options, false)
end
function volume.commands:unmute()
awful.spawn("pamixer --unmute" .. volume.pamixer_options, false)
end
function volume.commands:get(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
callback({
mute = muted,
value = value
})
end
)
end
function volume.commands:watch(timeout, callback, widget)
awful.widget.watch(
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
timeout, callback, widget
)
end
volume.hooks = {}
volume.add_hook = function(callback)
volume.hooks[#volume.hooks + 1] = callback
end
volume.exec_hooks = function()
volume.commands:get(function(status)
for i=1, #volume.hooks do
volume.hooks[i](status)
end
end)
end
volume.volume_up = function()
if volume.muted then
volume.unmute()
end
volume.commands:up()
core.sound.play("volume_up")
volume.exec_hooks()
end
volume.volume_down = function()
if volume.muted then
volume.unmute()
end
volume.commands:down()
core.sound.play("volume_down")
volume.exec_hooks()
end
volume.mute = function()
volume.commands:mute()
volume.muted = true
-- Spawn notification
local n = naughty.notify({
title = "Volume muted",
text = "Silence!",
timeout = 1,
replaces_id = volume.notificationid
})
volume.notificationid = n.id
volume.exec_hooks()
end
volume.unmute = function()
volume.commands:unmute()
volume.muted = false
-- Spawn notification
local n = naughty.notify({
title = "Volume unmuted",
text = "Loud.",
timeout = 1,
replaces_id = volume.notificationid
})
volume.notificationid = n.id
volume.exec_hooks()
end
volume.toggle_mute = function()
volume.commands:get(function(status)
if status.mute then
volume.unmute()
else
volume.mute()
end
end)
end
return volume