2021-08-01 07:24:26 -07:00
|
|
|
local volume = {}
|
|
|
|
volume.commands = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- Create pamixer option string
|
|
|
|
volume.pamixer_options = ""
|
2022-07-16 19:06:55 -07:00
|
|
|
if (config.volume.pamixer_sink ~= "") then
|
|
|
|
volume.pamixer_options = volume.pamixer_options .. " --sink " .. config.volume.pamixer_sink
|
2021-08-01 07:24:26 -07:00
|
|
|
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)
|
2022-07-16 15:44:14 -07:00
|
|
|
muted = string.match(stdout, "(%w%w%w%w%w?) ") -- "true" or "false"
|
|
|
|
muted = (muted == "true")
|
2021-08-01 07:24:26 -07:00
|
|
|
|
2022-07-16 15:44:14 -07:00
|
|
|
value = string.match(stdout, "(%d?%d?%d)") -- (\d?\d?\d)\%)
|
2021-08-01 07:24:26 -07:00
|
|
|
|
2022-07-16 15:44:14 -07:00
|
|
|
if (value == nil) then
|
|
|
|
value = false
|
|
|
|
else
|
|
|
|
value = tonumber(string.format("% 3d", value))
|
|
|
|
end
|
2021-08-01 07:24:26 -07:00
|
|
|
|
2022-07-16 15:44:14 -07:00
|
|
|
callback({
|
|
|
|
mute = muted,
|
|
|
|
value = value
|
|
|
|
})
|
|
|
|
end
|
|
|
|
)
|
2021-08-01 07:24:26 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function volume.commands:watch(timeout, callback, widget)
|
|
|
|
awful.widget.watch(
|
|
|
|
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
|
2022-07-16 12:02:19 -07:00
|
|
|
timeout, callback, widget
|
2021-08-01 07:24:26 -07:00
|
|
|
)
|
|
|
|
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
|
|
|
|
|
2022-07-16 15:44:14 -07:00
|
|
|
volume.volume_up = function()
|
2021-08-01 07:24:26 -07:00
|
|
|
if volume.muted then
|
|
|
|
volume.unmute()
|
|
|
|
end
|
|
|
|
|
|
|
|
volume.commands:up()
|
2022-07-16 17:18:27 -07:00
|
|
|
core.sound.play("volume_up")
|
2021-08-01 07:24:26 -07:00
|
|
|
|
|
|
|
volume.exec_hooks()
|
|
|
|
end
|
|
|
|
|
2022-07-16 15:44:14 -07:00
|
|
|
volume.volume_down = function()
|
2021-08-01 07:24:26 -07:00
|
|
|
if volume.muted then
|
|
|
|
volume.unmute()
|
|
|
|
end
|
|
|
|
|
|
|
|
volume.commands:down()
|
2022-07-16 17:18:27 -07:00
|
|
|
core.sound.play("volume_down")
|
2021-08-01 07:24:26 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-16 15:44:14 -07:00
|
|
|
volume.toggle_mute = function()
|
2021-08-01 07:24:26 -07:00
|
|
|
volume.commands:get(function(status)
|
|
|
|
if status.mute then
|
|
|
|
volume.unmute()
|
|
|
|
else
|
|
|
|
volume.mute()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return volume
|