This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvolume-control.lua
262 lines (223 loc) · 7.13 KB
/
volume-control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
-- Volume Control
local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local naughty = require("naughty")
-- compatibility fallbacks for 3.5:
local timer = gears.timer or timer
local spawn = awful.spawn or awful.util.spawn
local watch = awful.spawn and awful.spawn.with_line_callback
local function exec(command, callback)
awful.spawn.easy_async(command, callback or function() end)
end
------------------------------------------
-- Private utility functions
------------------------------------------
local function substitute(template, context)
if type(template) == "string" then
return (template:gsub("%${([%w_]+)}", function(key)
return tostring(context[key] or "default")
end))
else
-- function / functor:
return template(context)
end
end
local function new(self, ...)
local instance = setmetatable({}, {__index = self})
return instance:init(...) or instance
end
local function class(base)
return setmetatable({new = new}, {
__call = new,
__index = base,
})
end
------------------------------------------
-- Volume control interface
------------------------------------------
local vcontrol = class()
function vcontrol:init(args)
self.callbacks = {}
self.cmd = "amixer"
self.device = args.device or nil
self.cardid = args.cardid or nil
self.channel = args.channel or "Master"
self.step = args.step or '5%'
self.timer = timer({ timeout = args.timeout or 0.5 })
self.timer:connect_signal("timeout", function() self:get() end)
self.timer:start()
if args.listen and watch then
self.listener = watch({'stdbuf', '-oL', 'alsactl', 'monitor'}, {
stdout = function(line) self:get() end,
})
awesome.connect_signal("exit", function()
awesome.kill(self.listener, awesome.unix_signal.SIGTERM)
end)
end
end
function vcontrol:register(callback)
if callback then
table.insert(self.callbacks, callback)
end
end
function vcontrol:action(action)
if self[action] then self[action](self)
elseif type(action) == "function" then action(self)
elseif type(action) == "string" then spawn(action)
end
end
function vcontrol:update(status)
local volume = status:match("(%d?%d?%d)%%")
local state = status:match("%[(o[nf]*)%]")
if volume and state then
local volume = tonumber(volume)
local state = state:lower()
local muted = state == "off"
for _, callback in ipairs(self.callbacks) do
callback(self, {
volume = volume,
state = state,
muted = muted,
on = not muted,
})
end
end
end
function vcontrol:mixercommand(args, callback)
local args = awful.util.table.join(
{self.cmd},
(self.cmd == "amixer") and {"-M"} or {},
self.device and {"-D", self.device} or {},
self.cardid and {"-c", self.cardid} or {},
args)
exec(args, callback or function(output)
self:update(output)
end)
end
function vcontrol:get()
self:mixercommand({ "get", self.channel })
end
function vcontrol:up()
self:mixercommand({ "set", self.channel, self.step .. "+" })
end
function vcontrol:down()
self:mixercommand({ "set", self.channel, self.step .. "-" })
end
function vcontrol:toggle()
self:mixercommand({ "set", self.channel, "toggle" })
end
function vcontrol:mute()
self:mixercommand({ "set", "Master", "mute" })
end
function vcontrol:unmute()
self:mixercommand({ "set", "Master", "unmute" })
end
function vcontrol:list_sinks(callback)
exec("env LC_ALL=C pactl list sinks", function(output)
local sinks = {}
local sink
for line in output:gmatch("[^\r\n]+") do
if line:match("Sink #%d+") then
sink = {}
table.insert(sinks, sink)
else
local k, v = line:match("^%s*(%S+):%s*(.-)%s*$")
if k and v then sink[k:lower()] = v end
end
end
callback(sinks)
end)
end
function vcontrol:set_default_sink(name, callback)
exec({"pactl set-default-sink", name}, callback)
end
------------------------------------------
-- Volume control widget
------------------------------------------
-- derive so that users can still call up/down/mute etc
local vwidget = class(vcontrol)
function vwidget:init(args)
vcontrol.init(self, args)
self.lclick = args.lclick or "toggle"
self.mclick = args.mclick or "pavucontrol"
self.rclick = args.rclick or self.show_menu
self.font = args.font or nil
self.widget = args.widget or (self:create_widget(args) or self.widget)
self.tooltip = args.tooltip and (self:create_tooltip(args) or self.tooltip)
self:register(args.callback or self.update_widget)
self:register(args.tooltip and self.update_tooltip)
self.widget:buttons(awful.util.table.join(
awful.button({}, 1, function() self:action(self.lclick) end),
awful.button({}, 2, function() self:action(self.mclick) end),
awful.button({}, 3, function() self:action(self.rclick) end),
awful.button({}, 4, function() self:up() end),
awful.button({}, 5, function() self:down() end)
))
self:get()
end
-- text widget
function vwidget:create_widget(args)
self.widget_text = args.widget_text or {
on = '% 3d%% ',
off = '% 3dM ',
}
self.widget = wibox.widget.textbox()
if self.font then
self.widget.font = self.font
end
end
function vwidget:create_menu(callback)
self:list_sinks(function(sinks)
local sinks_submenu = {}
for i, sink in ipairs(sinks) do
table.insert(sinks_submenu, {sink.description, function()
self:set_default_sink(sink.name)
end})
end
callback(awful.menu { items = {
{ "mute", function() self:mute() end },
{ "unmute", function() self:unmute() end },
{ "Default Sink", sinks_submenu },
{ "pavucontrol", function() self:action("pavucontrol") end },
} })
end)
end
function vwidget:show_menu()
if self.menu then
self.menu:hide()
else
self:create_menu(function(menu)
self.menu = menu
self.menu:show()
self.menu.wibox:connect_signal("property::visible", function()
self.menu = nil
end)
end)
end
end
function vwidget:update_widget(setting)
self.widget:set_markup(
self.widget_text[setting.state]:format(setting.volume))
end
-- tooltip
function vwidget:create_tooltip(args)
self.tooltip_text = args.tooltip_text or [[
Volume: ${volume}% ${state}
Channel: ${channel}
Device: ${device}
Card: ${card}]]
self.tooltip = args.tooltip and awful.tooltip({objects={self.widget}})
end
function vwidget:update_tooltip(setting)
self.tooltip:set_text(substitute(self.tooltip_text, {
volume = setting.volume,
state = setting.state,
device = self.device,
card = self.card,
channel = self.channel,
}))
end
-- provide direct access to the control class
vwidget.control = vcontrol
return vwidget