]> 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:

wiki updated
[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          = { match  = string.match,
14                           format = string.format }
15
16 local setmetatable    = setmetatable
17
18 -- PulseAudio volume
19 -- lain.widgets.pulseaudio
20 local pulseaudio = {}
21
22 local function worker(args)
23    local args        = args or {}
24    local timeout     = args.timeout or 5
25    local settings    = args.settings or function() end
26
27    pulseaudio.sink   = args.sink or 0 -- user defined or first one
28    pulseaudio.cmd    = args.cmd or string.format("pacmd list-sinks | grep -e 'index: %d' -e 'volume: front' -e 'muted'", pulseaudio.sink)
29    pulseaudio.widget = wibox.widget.textbox('')
30
31    function pulseaudio.update()
32       local s = read_pipe(pulseaudio.cmd)
33
34       volume_now = {}
35       volume_now.left  = tonumber(string.match(s, "left.-(%d+)%%"))
36       volume_now.right = tonumber(string.match(s, "right.-(%d+)%%"))
37       volume_now.muted = string.match(s, "muted: (%S+)")
38
39       widget = pulseaudio.widget
40       settings()
41    end
42
43    newtimer(string.format("pulseaudio-%s", pulseaudio.sink), timeout, pulseaudio.update)
44
45    return setmetatable(pulseaudio, { __index = pulseaudio.widget })
46 end
47
48 return setmetatable(pulseaudio, { __call = function(_, ...) return worker(...) end })