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

96f30b333e8221c58b40575cf0c069940830c853
[etc/awesome.git] / widgets / alsa.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6       * (c) 2010, Adrian C. <anrxc@sysphere.org>  
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11 local shell        = require("awful.util").shell
12 local wibox        = require("wibox")
13 local string       = { match  = string.match,
14                        format = string.format }
15 local setmetatable = setmetatable
16
17 -- ALSA volume
18 -- lain.widgets.alsa
19 local alsa = helpers.make_widget_textbox()
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
26     alsa.cmd           = args.cmd or "amixer"
27     alsa.channel       = args.channel or "Master"
28     alsa.togglechannel = args.togglechannel
29
30     if alsa.togglechannel then
31         alsa.cmd = { shell, "-c", string.format("%s get %s; %s get %s",
32         alsa.cmd, alsa.channel, alsa.cmd, alsa.togglechannel) }
33     end
34
35     alsa.last = {}
36
37     function alsa.update()
38         helpers.async(alsa.cmd, function(mixer)
39             local l,s = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
40             if alsa.last.level ~= l or alsa.last.status ~= s then
41                 volume_now = { level = l, status = s }
42                 widget = alsa.widget
43                 settings()
44                 alsa.last = volume_now
45             end
46         end)
47     end
48
49     helpers.newtimer(string.format("alsa-%s-%s", alsa.cmd, alsa.channel), timeout, alsa.update)
50
51     return setmetatable(alsa, { __index = alsa.widget })
52 end
53
54 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })