awesomewm/classes/volume/volume.lua

218 lines
4.2 KiB
Lua
Raw Normal View History

2022-04-24 08:27:47 -07:00
local widget_types = {
2022-04-24 09:16:52 -07:00
bar = require("classes/volume/bar_widget"),
arc = require("classes/volume/arc_widget")
2022-04-24 08:27:47 -07:00
}
2022-04-24 10:12:00 -07:00
local P = {
name = "volume"
}
2022-04-24 12:27:54 -07:00
P.__index = P
2022-04-23 21:13:35 -07:00
---
-- Internal methods
---
function P:_full_args()
-- Build complete pamixer arguments, combining sink and options.
-- Must have a leading space.
if (self.pa_sink == "") then
return " " .. self.pa_options
else
return " --sink" .. self.pa_sink .. " " .. self.pa_options
end
end
function P:_get_status()
-- Update mute status and volume.
-- Values in this table are updated.
awful.spawn.easy_async(
"pamixer --get-mute --get-volume" .. self:_full_args(),
function(stdout, stderr, exitreason, exitcode)
-- Get mute state
local muted = string.match(stdout, "(%w%w%w%w%w?) ")
self.muted = (muted == "true")
local value = string.match(stdout, "(%d?%d?%d)") -- (\d?\d?\d)\%)
if (value == nil) then
self.volume = nil
2022-04-24 08:27:47 -07:00
self._ready = false
2022-04-23 21:13:35 -07:00
else
self.volume = tonumber(string.format("% 3d", value))
2022-04-24 08:27:47 -07:00
self._ready = true
2022-04-23 21:13:35 -07:00
end
self:_update_widget()
end
)
-- This is used inside a timer, so return true to keep it running.
return true
end
function P:_update_widget()
2022-04-24 08:27:47 -07:00
if (not self._ready) then
2022-04-24 13:31:01 -07:00
self._widget:set_state("error")
self._widget:set_value(self.max_value);
self._widget:set_tooltip("Volume error");
2022-04-23 21:13:35 -07:00
return
end
2022-04-24 13:31:01 -07:00
self._widget:set_value(self.volume);
self._widget:set_tooltip("Volume " .. self.volume .. "%");
2022-04-23 21:13:35 -07:00
if (self.muted) then
2022-04-24 13:31:01 -07:00
self._widget:set_state("muted")
2022-04-23 21:13:35 -07:00
else
2022-04-24 13:31:01 -07:00
self._widget:set_state("unmuted")
2022-04-23 21:13:35 -07:00
end
end
---
-- Simple actions
---
function P:volume_up()
2022-04-24 08:27:47 -07:00
if (not self._ready) or (self.volume >= self.max_value) then
2022-04-23 21:13:35 -07:00
return
end
if self.muted then
self:unmute()
end
awful.spawn("pamixer --increase 5" .. self:_full_args(), false)
wrapper.sound.play("volume_up")
self.volume = self.volume + 5
2022-04-24 08:27:47 -07:00
if self.volume > self.max_value then
self.volume = self.max_value
2022-04-23 21:18:23 -07:00
end
2022-04-23 21:13:35 -07:00
self:_update_widget()
end
function P:volume_down()
2022-04-24 08:27:47 -07:00
if (not self._ready) or (self.volume <= 0) then
2022-04-23 21:13:35 -07:00
return
end
if self.muted then
self:unmute()
end
awful.spawn("pamixer --decrease 5" .. self:_full_args(), false)
wrapper.sound.play("volume_down")
self.volume = self.volume - 5
2022-04-23 21:18:23 -07:00
if self.volume < 0 then
self.volume = 0
end
2022-04-23 21:13:35 -07:00
self:_update_widget()
end
function P:volume_set(value)
2022-04-24 08:27:47 -07:00
if (not self._ready) then
2022-04-23 21:13:35 -07:00
return
end
if self.muted then
self:unmute()
end
2022-04-24 08:27:47 -07:00
if (value < 0) then
value = 0
end
if (value > self.max_value) then
value = self.max_value
end
2022-04-23 21:13:35 -07:00
awful.spawn("pamixer --set_volume " .. tostring(value) .. self:_full_args(), false)
self.volume = value
self:_update_widget()
end
function P:mute()
2022-04-24 08:27:47 -07:00
if (not self._ready) then
2022-04-23 21:13:35 -07:00
return
end
self.muted = true
awful.spawn("pamixer --mute" .. self:_full_args(), false)
2022-04-24 12:25:13 -07:00
self:_update_widget()
2022-04-23 21:13:35 -07:00
end
function P:unmute()
2022-04-24 08:27:47 -07:00
if (not self._ready) then
2022-04-23 21:13:35 -07:00
return
end
self.muted = false
awful.spawn("pamixer --unmute" .. self:_full_args(), false)
self:_update_widget()
end
function P:toggle_mute()
2022-04-24 08:27:47 -07:00
if (not self._ready) then
2022-04-23 21:13:35 -07:00
return
end
if self.muted then
self:unmute()
else
self:mute()
end
end
---
-- Create new volume_interface
---
function P:new(args)
-- Arguments
2022-04-24 10:12:00 -07:00
local v = {
2022-04-23 21:13:35 -07:00
pa_options = args.cli_options or "",
pa_sink = args.pa_sink or "",
2022-04-24 08:27:47 -07:00
update_interval = args.update_interal or 5,
max_value = args.max_value or 100,
widget_type = args.widget_type or "arc",
_ready = false, -- is all the information in this class up-to-date?
2022-04-23 21:13:35 -07:00
-- if this is false, ui will show an error and most methods will
-- do nothing. Updated in _get_status()
}
-- Create widget for this volume interface
2022-04-24 13:31:01 -07:00
v._widget = widget_types[v.widget_type]:new()
v.widget = v._widget.widget
2022-04-23 21:13:35 -07:00
-- Attach button press signals
2022-04-24 13:31:01 -07:00
v.widget:connect_signal("button::press",
2022-04-23 21:13:35 -07:00
function(_, _, _, button, mods)
if (button == 3) then -- Right-click
v:toggle_mute()
elseif (button == 4) then -- Scroll up
v:volume_up()
elseif (button == 5) then -- Scroll down
v:volume_down()
end
end
)
setmetatable(v, self)
-- This timer keeps mute and volume status up-to-date.
v.timer = gears.timer {
timeout = v.update_interval,
call_now = true,
autostart = true,
callback = function()
v:_get_status()
end
}
return v
end
return P