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

Offer the possibility to show the used space in MB/GB
[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 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 = helpers.make_widget_textbox()
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     alsa.togglechannel = args.togglechannel
32     alsa.last_level    = "0"
33     alsa.last_status   = ""
34
35     function alsa.update()
36         mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel))
37         l, s  = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
38
39         -- HDMIs can have a channel different from Master for toggling mute
40         if alsa.togglechannel then
41             s = string.match(read_pipe(string.format("%s get %s", alsa.cmd, alsa.togglechannel)), "%[(%a+)%]")
42         end
43
44         if alsa.last_level ~= l or alsa.last_status ~= s then
45             volume_now = { level = l, status = s }
46             alsa.last_level  = l
47             alsa.last_status = s
48
49             widget = alsa.widget
50             settings()
51         end
52     end
53
54     timer_id = string.format("alsa-%s-%s", alsa.cmd, alsa.channel)
55     helpers.newtimer(timer_id, timeout, alsa.update)
56
57     return alsa
58 end
59
60 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })