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

calendar: separate args from attach; closes #294
[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 helpers      = require("lain.helpers")
10 local shell        = require("awful.util").shell
11 local wibox        = require("wibox")
12 local string       = { gmatch = string.gmatch,
13                        match  = string.match,
14                        format = string.format }
15 local setmetatable = setmetatable
16
17 -- PulseAudio volume
18 -- lain.widgets.pulseaudio
19 local pulseaudio = {}
20
21 local function worker(args)
22    local args        = args or {}
23    local timeout     = args.timeout or 5
24    local settings    = args.settings or function() end
25    local scallback   = args.scallback
26
27    pulseaudio.cmd    = args.cmd or "pacmd list-sinks | sed -n -e '0,/*/d' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'"
28
29    pulseaudio.widget = wibox.widget.textbox()
30
31    function pulseaudio.update()
32       if scallback then pulseaudio.cmd = scallback() end
33
34       helpers.async({ shell, "-c", pulseaudio.cmd }, function(s)
35           volume_now = {
36               index = string.match(s, "index: (%S+)") or "N/A",
37               sink  = string.match(s, "device.string = \"(%S+)\"") or "N/A",
38               muted = string.match(s, "muted: (%S+)") or "N/A"
39           }
40
41           local ch = 1
42           volume_now.channel = {}
43           for v in string.gmatch(s, ":.-(%d+)%%") do
44               volume_now.channel[ch] = v
45               ch = ch + 1
46           end
47
48           volume_now.left  = volume_now.channel[1] or "N/A"
49           volume_now.right = volume_now.channel[2] or "N/A"
50
51           widget = pulseaudio.widget
52           settings()
53       end)
54    end
55
56    helpers.newtimer("pulseaudio", timeout, pulseaudio.update)
57
58    return pulseaudio
59 end
60
61 return setmetatable(pulseaudio, { __call = function(_, ...) return worker(...) end })