]> 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 #420 from sim590/unpack-lua51-compat
[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  = string
12 local type    = type
13
14 -- PulseAudio volume
15 -- lain.widget.pulse
16
17 local function factory(args)
18     local pulse    = { widget = wibox.widget.textbox(), device = "N/A" }
19     local args     = args or {}
20     local timeout  = args.timeout or 5
21     local settings = args.settings or function() end
22
23     pulse.devicetype = args.devicetype or "sink"
24     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'"
25
26     function pulse.update()
27         helpers.async({ shell, "-c", type(pulse.cmd) == "string" and pulse.cmd or pulse.cmd() },
28         function(s)
29             volume_now = {
30                 index  = string.match(s, "index: (%S+)") or "N/A",
31                 device = string.match(s, "device.string = \"(%S+)\"") or "N/A",
32                 muted  = string.match(s, "muted: (%S+)") or "N/A"
33             }
34
35             pulse.device = volume_now.index
36
37             local ch = 1
38             volume_now.channel = {}
39             for v in string.gmatch(s, ":.-(%d+)%%") do
40                 volume_now.channel[ch] = v
41                 ch = ch + 1
42             end
43
44             volume_now.left  = volume_now.channel[1] or "N/A"
45             volume_now.right = volume_now.channel[2] or "N/A"
46
47             widget = pulse.widget
48             settings()
49         end)
50     end
51
52     helpers.newtimer("pulse", timeout, pulse.update)
53
54     return pulse
55 end
56
57 return factory