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

8571ec600834d6799a1a1063648779a3095902ef
[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 newtimer        = require("lain.helpers").newtimer
11
12 local wibox           = require("wibox")
13
14 local io              = { popen  = io.popen }
15 local string          = { match  = string.match }
16
17 local setmetatable    = setmetatable
18
19 -- ALSA volume
20 -- lain.widgets.alsa
21 local alsa = {}
22
23 local function worker(args)
24     local args     = args or {}
25     local timeout  = args.timeout or 5
26     local channel  = args.channel or "Master"
27     local settings = args.settings or function() end
28
29     widget = wibox.widget.textbox('')
30
31     function update()
32         local f = io.popen('amixer get ' .. channel)
33         local mixer = f:read("*all")
34         f:close()
35
36         volume = {}
37
38         volume.level, volume.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
39
40         if volume.level == nil
41         then
42             volume.level  = 0
43             volume.status = "off"
44         end
45
46         if volume.status == ""
47         then
48             if volume.level == 0
49             then
50                 volume.status = "off"
51             else
52                 volume.status = "on"
53             end
54         end
55
56         settings()
57     end
58
59     newtimer("alsa", timeout, update)
60
61     output = { widget = widget, notify = update }
62
63     return setmetatable(output, { __index = output.widget })
64 end
65
66 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })