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

Merge pull request #398 from BarbUk/feature/tpbat/configurable-battery-thresholds
[etc/awesome.git] / widget / pulse.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2016, Luca CPZ
5
6 --]]
7
8 local helpers = require("lain.helpers")
9 local shell   = require("awful.util").shell
10 local wibox   = require("wibox")
11 local string  = { gmatch = string.gmatch,
12                   match  = string.match,
13                   format = string.format }
14 local type = type
15
16 -- PulseAudio volume
17 -- lain.widget.pulse
18
19 local function factory(args)
20     local pulse    = { widget = wibox.widget.textbox(), device = "N/A" }
21     local args     = args or {}
22     local timeout  = args.timeout or 5
23     local settings = args.settings or function() end
24
25     pulse.devicetype = args.devicetype or "sink"
26     pulse.cmd = args.cmd or "pacmd list-" .. pulse.devicetype .. "s | sed -n -e '/*/,$!d' -e '/index/p' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'"
27
28     function pulse.update()
29         helpers.async({ shell, "-c", type(pulse.cmd) == "string" and pulse.cmd or pulse.cmd() },
30         function(s)
31             volume_now = {
32                 index  = string.match(s, "index: (%S+)") or "N/A",
33                 device = string.match(s, "device.string = \"(%S+)\"") or "N/A",
34                 muted  = string.match(s, "muted: (%S+)") or "N/A"
35             }
36
37             pulse.device = volume_now.index
38
39             local ch = 1
40             volume_now.channel = {}
41             for v in string.gmatch(s, ":.-(%d+)%%") do
42                 volume_now.channel[ch] = v
43                 ch = ch + 1
44             end
45
46             volume_now.left  = volume_now.channel[1] or "N/A"
47             volume_now.right = volume_now.channel[2] or "N/A"
48
49             widget = pulse.widget
50             settings()
51         end)
52     end
53
54     helpers.newtimer("pulse", timeout, pulse.update)
55
56     return pulse
57 end
58
59 return factory