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

5a4335e63b7624a61f49389d0c4d21f252dd99a1
[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 card     = args.card or "0"
27     local timeout  = args.timeout or 5
28     local channel  = args.channel or "Master"
29     local settings = args.settings or function() end
30
31     alsa.widget = wibox.widget.textbox('')
32
33     function alsa.update()
34         local f = assert(io.popen(string.format("amixer -c %s -M get %s", card, channel)))
35         local mixer = f:read("*a")
36         f:close()
37
38         volume_now = {}
39
40         volume_now.level, volume_now.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
41
42         if volume_now.level == nil
43         then
44             volume_now.level  = "0"
45             volume_now.status = "off"
46         end
47
48         if volume_now.status == ""
49         then
50             if volume_now.level == "0"
51             then
52                 volume_now.status = "off"
53             else
54                 volume_now.status = "on"
55             end
56         end
57
58         widget = alsa.widget
59         settings()
60     end
61
62     newtimer("alsa", timeout, alsa.update)
63
64     return setmetatable(alsa, { __index = alsa.widget })
65 end
66
67 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })