]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

Workaround premature end of file with awful.spawn.easy_async
[etc/awesome.git] / widget / 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 shell   = require("awful.util").shell
12 local wibox   = require("wibox")
13 local string  = { match  = string.match,
14                   format = string.format }
15
16 -- ALSA volume
17 -- lain.widget.alsa
18
19 local function factory(args)
20     local alsa     = { widget = wibox.widget.textbox() }
21     local args     = args or {}
22     local timeout  = args.timeout or 5
23     local settings = args.settings or function() end
24
25     alsa.cmd           = args.cmd or "amixer"
26     alsa.channel       = args.channel or "Master"
27     alsa.togglechannel = args.togglechannel
28
29     local format_cmd = string.format("%s get %s", alsa.cmd, alsa.channel)
30
31     if alsa.togglechannel then
32         format_cmd = { shell, "-c", string.format("%s get %s; %s get %s",
33         alsa.cmd, alsa.channel, alsa.cmd, alsa.togglechannel) }
34     end
35
36     alsa.last = {}
37
38     function alsa.update()
39         helpers.async(format_cmd, function(mixer)
40             local l,s = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
41             if alsa.last.level ~= l or alsa.last.status ~= s then
42                 volume_now = { level = l, status = s }
43                 widget = alsa.widget
44                 settings()
45                 alsa.last = volume_now
46             end
47         end)
48     end
49
50     helpers.newtimer(string.format("alsa-%s-%s", alsa.cmd, alsa.channel), timeout, alsa.update)
51
52     return alsa
53 end
54
55 return factory