awesomewm/modules/volume/util.lua

165 lines
3.3 KiB
Lua
Raw Permalink Normal View History

2021-08-01 07:24:26 -07:00
local volume = {}
2022-11-05 09:47:15 -07:00
2021-08-01 07:24:26 -07:00
-- Create pamixer option string
volume.pamixer_options = ""
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
2022-11-05 09:47:15 -07:00
volume.commands = {
2022-11-05 09:54:25 -07:00
up = function(value, callback)
2022-11-05 09:47:15 -07:00
awful.spawn.easy_async(
2022-11-05 09:54:25 -07:00
"pamixer --increase " .. value .. " "
.. volume.pamixer_options,
2022-11-05 09:47:15 -07:00
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
2022-07-16 15:44:14 -07:00
end
2022-11-05 09:47:15 -07:00
)
end,
2022-11-05 09:54:25 -07:00
down = function(value, callback)
2022-11-05 09:47:15 -07:00
awful.spawn.easy_async(
2022-11-05 09:54:25 -07:00
"pamixer --decrease " .. value .. " "
.. volume.pamixer_options,
2022-11-05 09:47:15 -07:00
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
2023-08-21 16:09:33 -07:00
-- Value will be "false" if audio not connected,
-- needs to be handled gracefully
2022-11-05 09:47:15 -07:00
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
)
2021-08-01 07:24:26 -07:00
2022-11-05 09:54:25 -07:00
volume.volume_up = function(value)
2021-08-01 07:24:26 -07:00
if volume.muted then
volume.unmute()
end
2022-11-05 09:54:25 -07:00
volume.commands.up(value, function()
2022-11-05 09:47:15 -07:00
core.sound.play("volume_up")
end)
2021-08-01 07:24:26 -07:00
end
2022-11-05 09:54:25 -07:00
volume.volume_down = function(value)
2021-08-01 07:24:26 -07:00
if volume.muted then
volume.unmute()
end
2022-11-05 09:54:25 -07:00
volume.commands.down(value, function()
2022-11-05 09:47:15 -07:00
core.sound.play("volume_down")
end)
2021-08-01 07:24:26 -07:00
end
volume.mute = function()
volume.muted = true
2022-11-05 09:47:15 -07:00
volume.commands.mute()
2021-08-01 07:24:26 -07:00
end
volume.unmute = function()
volume.muted = false
2022-11-05 09:47:15 -07:00
volume.commands.unmute()
2021-08-01 07:24:26 -07:00
end
2022-07-16 15:44:14 -07:00
volume.toggle_mute = function()
2022-11-05 09:47:15 -07:00
volume.commands.get(function(status)
2021-08-01 07:24:26 -07:00
if status.mute then
volume.unmute()
else
volume.mute()
end
end)
end
return volume