Added volume widget

This commit is contained in:
2022-07-16 15:44:14 -07:00
parent 840f084db8
commit 8b3a1ac5a0
9 changed files with 49 additions and 42 deletions

16
modules/volume/init.lua Normal file
View File

@ -0,0 +1,16 @@
local volume = require("modules.volume.util")
local widget = require("modules.volume.widget")
return {
widgets = {
volume = require("modules.volume.widget").widget
},
keybinds = require("modules.volume.keybinds"),
init = function ()
-- Make sure volume is unmuted
volume.commands:unmute()
-- Update volume widget at start
volume.commands:get(widget.update)
end
}

33
modules/volume/keybinds.lua Executable file
View File

@ -0,0 +1,33 @@
local volume = req_rel(..., "util")
return gears.table.join(
awful.key( {}, "XF86AudioRaiseVolume",
function ()
volume.volume_up()
end,
{
description = "Volume up",
group = "System"
}
),
awful.key( {}, "XF86AudioLowerVolume",
function ()
volume.volume_down()
end,
{
description = "Volume down",
group = "System"
}
),
awful.key( {}, "XF86AudioMute",
function ()
volume.toggle_mute()
end,
{
description = "Mute",
group = "System"
}
)
)

145
modules/volume/util.lua Executable file
View File

@ -0,0 +1,145 @@
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()
wrapper.sound.play("volume_up")
volume.exec_hooks()
end
volume.volume_down = function()
if volume.muted then
volume.unmute()
end
volume.commands:down()
wrapper.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

136
modules/volume/widget.lua Executable file
View File

@ -0,0 +1,136 @@
local volume = req_rel(..., "util")
local widget = {}
widget.icon = wibox.widget {
resize = true,
image = beautiful.icons.volume.mute,
widget = wibox.widget.imagebox
}
widget.arc = wibox.widget {
{
widget.icon,
top = beautiful.dpi(1),
bottom = beautiful.dpi(1),
layout = wibox.container.margin,
},
max_value = 1,
thickness = beautiful.dpi(4),
start_angle = 4.71238898, -- 2pi*3/4
--forced_height = beautiful.dpi(16),
--forced_width = beautiful.dpi(16),
colors = {"#27D4CC", "#00446B"},
bg = "#FFFFFF30",
paddings = beautiful.dpi(2),
widget = wibox.container.arcchart
}
widget.bar = wibox.widget {
max_value = 100,
value = 0,
--forced_height = 20,
forced_width = beautiful.dpi(50),
paddings = 0,
border_width = 0,
color = {
type = "linear",
from = {0, 0}, to = {beautiful.dpi(50), 0},
stops = { { 0, "#27D4CC" }, { 1, "#00446B" }}
},
background_color = "#FFFFFF30",
widget = wibox.widget.progressbar,
shape = gears.shape.rounded_bar,
}
widget.widget = wibox.widget {
{
{ -- Right space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
{
widget.arc,
top = beautiful.dpi(2),
bottom = beautiful.dpi(2),
layout = wibox.container.margin,
},
{ -- Left space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
widget.widget:connect_signal("mouse::enter", function(result)
widget.widget.bg = beautiful.color.bar.hover_bg
end)
widget.widget:connect_signal("mouse::leave", function(result)
widget.widget.bg = beautiful.color.transparent
end)
widget.widget:connect_signal("button::press",
function(_, _, _, button, mods)
-- Right-click
if (button == 3) then
volume.toggle_mute()
-- Scroll up
elseif (button == 4) then
volume.volume_up()
-- Scroll down
elseif (button == 5) then
volume.volume_down()
end
end
)
widget.update = function(status)
if (status.value == false) then
widget.icon.image = beautiful.icons.volume.error
widget.arc.value = 100;
widget.bar.value = 100;
return
end
widget.arc.value = status.value / 100;
widget.bar.value = status.value;
if (status.mute) then
-- Set muted icon
widget.icon.image = beautiful.icons.volume.mute
else
-- Set icon by value
if status.value > 70 then
widget.icon.image = beautiful.icons.volume.high
elseif status.value > 40 then
widget.icon.image = beautiful.icons.volume.medium
elseif status.value > 0 then
widget.icon.image = beautiful.icons.volume.low
elseif status.value == 0 then
widget.icon.image = beautiful.icons.volume.off
end
end
end
volume.add_hook(widget.update)
volume.commands:watch(
5,
function()
volume.commands:get(widget.update)
end,
widget.widget
)
return widget