]> git.madduck.net Git - etc/awesome.git/blob - widgets/pulseaudio.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

4bda3c5a8555b04b3b96552eeef19a10c256814f
[etc/awesome.git] / widgets / pulseaudio.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2016, Luke Bonham                     
6                                                   
7 --]]
8
9 local read_pipe    = require("lain.helpers").read_pipe
10 local newtimer     = require("lain.helpers").newtimer
11 local wibox        = require("wibox")
12
13 local string       = { gmatch = string.gmatch,
14                        match  = string.match,
15                        format = string.format }
16
17 local setmetatable = setmetatable
18
19 -- PulseAudio volume
20 -- lain.widgets.pulseaudio
21 local pulseaudio = {}
22
23 local function worker(args)
24    local args        = args or {}
25    local timeout     = args.timeout or 5
26    local settings    = args.settings or function() end
27    local scallback   = args.scallback
28
29    pulseaudio.cmd    = args.cmd or string.format("pacmd list-sinks | sed -n -e '0,/*/d' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p'")
30    pulseaudio.widget = wibox.widget.textbox('')
31
32    function pulseaudio.update()
33       if scallback then pulseaudio.cmd = scallback() end
34       local s = read_pipe(pulseaudio.cmd)
35
36       volume_now = {}
37       volume.now.index = string.match(s, "index: (%S+)") or "N/A"
38       volume_now.muted = string.match(s, "muted: (%S+)") or "N/A"
39
40       local ch = 1
41       volume_now.channel = {}
42       for v in string.gmatch(s, ":.-(%d+)%%") do
43           volume_now.channel[ch] = v
44           ch = ch + 1
45       end
46
47       volume_now.left  = volume_now.channel[1] or "N/A"
48       volume_now.right = volume_now.channel[2] or "N/A"
49
50       widget = pulseaudio.widget
51       settings()
52    end
53
54    newtimer(string.format("pulseaudio-%s", timeout), timeout, pulseaudio.update)
55
56    return setmetatable(pulseaudio, { __index = pulseaudio.widget })
57 end
58
59 return setmetatable(pulseaudio, { __call = function(_, ...) return worker(...) end })