]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

ed37cb5e96fbd580d70bef55a0b783f3582fdab7
[etc/awesome.git] / widget / 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
16 -- PulseAudio volume
17 -- lain.widget.pulseaudio
18
19 local function factory(args)
20     local pulseaudio  = { widget = wibox.widget.textbox() }
21     local args        = args or {}
22     local timeout     = args.timeout or 5
23     local settings    = args.settings or function() end
24     local scallback   = args.scallback
25  
26     pulseaudio.device = "N/A"
27     pulseaudio.devicetype = args.devicetype or "sink"
28     pulseaudio.cmd = args.cmd or "pacmd list-" .. pulseaudio.devicetype .. "s | sed -n -e '0,/*/d' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'"
29
30     function pulseaudio.update()
31         if scallback then pulseaudio.cmd = scallback() end
32
33         helpers.async({ shell, "-c", pulseaudio.cmd }, function(s)
34             volume_now = {
35                 index = string.match(s, "index: (%S+)") or "N/A",
36                 device = string.match(s, "device.string = \"(%S+)\"") or "N/A",
37                 sink   = device, -- legacy API
38                 muted  = string.match(s, "muted: (%S+)") or "N/A"
39             }
40
41             pulseaudio.device = volume_now.index
42
43             local ch = 1
44             volume_now.channel = {}
45             for v in string.gmatch(s, ":.-(%d+)%%") do
46                 volume_now.channel[ch] = v
47                 ch = ch + 1
48             end
49
50             volume_now.left  = volume_now.channel[1] or "N/A"
51             volume_now.right = volume_now.channel[2] or "N/A"
52
53             widget = pulseaudio.widget
54
55             settings()
56         end)
57     end
58
59     helpers.newtimer("pulseaudio", timeout, pulseaudio.update)
60
61     return pulseaudio
62 end
63
64 return factory