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

mpd: added http streams check; #115
[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                           format = string.format }
17
18 local setmetatable    = setmetatable
19
20 -- ALSA volume
21 -- lain.widgets.alsa
22 local alsa = {}
23
24 local function worker(args)
25     local args     = args or {}
26     local timeout  = args.timeout or 5
27     local settings = args.settings or function() end
28
29     alsa.cmd     = args.cmd or "amixer"
30     alsa.channel = args.channel or "Master"
31
32     alsa.widget = wibox.widget.textbox('')
33
34     function alsa.update()
35         local f = assert(io.popen(string.format("%s get %s", alsa.cmd, alsa.channel)))
36         local mixer = f:read("*a")
37         f:close()
38
39         volume_now = {}
40
41         volume_now.level, volume_now.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
42
43         if volume_now.level == nil
44         then
45             volume_now.level  = "0"
46             volume_now.status = "off"
47         end
48
49         if volume_now.status == ""
50         then
51             if volume_now.level == "0"
52             then
53                 volume_now.status = "off"
54             else
55                 volume_now.status = "on"
56             end
57         end
58
59         widget = alsa.widget
60         settings()
61     end
62
63     timer_id = string.format("alsa-%s-%s", alsa.cmd, alsa.channel)
64
65     newtimer(timer_id, timeout, alsa.update)
66
67     return setmetatable(alsa, { __index = alsa.widget })
68 end
69
70 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })