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

merge and resolve conflicts from #134; #114
[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 local read_pipe       = require("lain.helpers").read_pipe
12
13 local wibox           = require("wibox")
14
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     level  = "0",
24     status = "off",
25 }
26
27 local function worker(args)
28     local args     = args or {}
29     local timeout  = args.timeout or 1
30     local settings = args.settings or function() end
31
32     alsa.cmd     = args.cmd or "amixer"
33     alsa.channel = args.channel or "Master"
34
35     alsa.widget = wibox.widget.textbox('')
36
37     function alsa.update()
38         local mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel))
39
40         volume_now = {}
41
42         volume_now.level, volume_now.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
43
44         if volume_now.level == nil
45         then
46             volume_now.level  = "0"
47             volume_now.status = "off"
48         end
49
50         if volume_now.status == ""
51         then
52             if volume_now.level == "0"
53             then
54                 volume_now.status = "off"
55             else
56                 volume_now.status = "on"
57             end
58         end
59
60         if alsa.level ~= volume_now.level or alsa.status ~= volume_now.status
61         then
62             widget = alsa.widget
63             settings()
64
65             alsa.level = volume_now.level
66             alsa.status = volume_now.status
67         end
68     end
69
70     timer_id = string.format("alsa-%s-%s", alsa.cmd, alsa.channel)
71
72     newtimer(timer_id, timeout, alsa.update)
73
74     return setmetatable(alsa, { __index = alsa.widget })
75 end
76
77 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })