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

first commit
[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-2012, Peter Hofmann                  
7       * (c) 2010,      Adrian C. <anrxc@sysphere.org> 
8                                                       
9 --]]
10
11 local markup          = require("lain.util.markup")
12 local run_in_terminal = require("lain.helpers").run_in_terminal
13
14 local awful           = require("awful")
15 local beautiful       = require("beautiful")
16 local wibox           = require("wibox")
17
18 local io              = io
19 local string          = { format = string.format,
20                           match  = string.match }
21
22 local setmetatable    = setmetatable
23
24 -- ALSA volume infos
25 -- nain.widgets.alsa
26 local alsa = {
27     volume = 0,
28     mute = false,
29 }
30
31 function worker(args)
32     local args = args or {}
33     local channel = args.channel or "Master"
34     local step = args.step or "1%"
35     local header = args.header or " Vol "
36     local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
37     local color = args.color or beautiful.fg_focus or "#FFFFFF"
38
39     local myvolume = wibox.widget.textbox()
40     local myvolumeupdate = function()
41         local f = io.popen('amixer get ' .. channel)
42         local mixer = f:read("*all")
43         f:close()
44
45         local volume, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
46
47         if volume == nil
48         then
49             alsa.volume = 0
50         else
51             alsa.volume = volume
52         end
53
54         if mute == nil or mute == 'on'
55         then
56             alsa.mute = true
57             mute = ''
58         else
59             alsa.mute = false
60             mute = 'M'
61         end
62
63         local ret = markup(color, string.format("%d%s", volume, mute))
64         myvolume:set_markup(markup(header_color, header) .. ret .. " ")
65     end
66
67     local myvolumetimer = timer({ timeout = 5 })
68     myvolumetimer:connect_signal("timeout", myvolumeupdate)
69     myvolumetimer:start()
70     myvolumetimer:emit_signal("timeout")
71
72     myvolume:buttons(awful.util.table.join(
73         awful.button({}, 1,
74             function()
75                 run_in_terminal('alsamixer')
76              end),
77         awful.button({}, 3,
78             function()
79                 awful.util.spawn('amixer sset ' .. channel ' toggle')
80             end),
81
82         awful.button({}, 4,
83             function()
84                 awful.util.spawn('amixer sset ' .. channel .. ' ' .. step '+')
85                 myvolumeupdate()
86             end),
87
88         awful.button({}, 5,
89             function()
90                 awful.util.spawn('amixer sset ' .. channel .. ' ' .. step '-')
91                 myvolumeupdate()
92             end)
93     ))
94
95     alsa.widget = myvolume
96     alsa.channel = channel
97     alsa.step = step
98     alsa.notify = myvolumeupdate
99
100     return setmetatable(alsa, { __index = alsa.widget })
101 end
102
103 return setmetatable(alsa, { __call = function(_, ...) return worker(...) end })