Debian Codegraph @

flaw/alsa.lua

raw symbols SCIP
1 -- flaw, a Lua OO management framework for Awesome WM widgets.
2 -- Copyright (C) 2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
3
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of the GNU General Public License as published by
6 -- the Free Software Foundation, either version 3 of the License, or
7 -- (at your option) any later version.
8
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
13
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 -- Grab environment.
19 local io = io
20 local math = math
21 local os = os
22 local string = string
23 local tonumber = tonumber
24
25 local beautiful = require('beautiful')
26 local naughty = require('naughty')
27
28 local awful = {
29 button = require("awful.button"),
30 util = require("awful.util"),
31 }
32
33 local flaw = {
34 helper = require('flaw.helper'),
35 gadget = require('flaw.gadget'),
36 provider = require('flaw.provider')
37 }
38
39
40 --- ALSA control gadgets and provider.
41 --
42 -- <p>This module contains a provider for ALSA information and two
43 -- gadgets: a text gadget and an icon gadget.</p>
44 --
45 -- <h2>Gadgets</h2>
46 --
47 -- <p>The ID of the gadgets designates the identifier of the audio
48 -- card to control. ALSA gadgets provide no custom parameters. See the
49 -- <a href="flaw.gadget.html">gadget</a> module documentation to learn
50 -- about standard gadgets parameters.</p>
51 --
52 -- <p>All ALSA gadgets specialize their standard counterpart to allow
53 -- you to control the volume from the Master channel of the monitored
54 -- card. The buttons 4 and 5 of the mouse - which should normally be
55 -- bound to the wheel up and down movements - allow you to change the
56 -- sound level, whereas the left click mutes the channel.</p>
57 --
58 -- <h3>Icon Gadget</h3>
59 --
60 -- <p>The ALSA icon gadget can be instantiated by indexing the gadget
61 -- module with <code>icon.alsa</code>. Here is an example which
62 -- assumes the card identifier is 0, and the path of the icon to
63 -- display is stored in a <b>beautiful</b> property.</p>
64 --
65 -- <div class='example'>
66 -- g = flaw.gadget.icon.alsa(<br/>
67 -- &nbsp;&nbsp;&nbsp;'0', {}, { image = image(beautiful.sound_icon) })<br/>
68 -- </div>
69 --
70 -- <h3>Text Gadget</h3>
71 --
72 -- <p>The ALSA text gadget can be instantiated by indexing the gadget
73 -- module with <code>text.alsa</code>. By default, the gadget pattern
74 -- is <code>'$volume%'</code>. See the provider's documentation below
75 -- to learn about the available variables.</p>
76 --
77 -- <div class='example'>
78 -- g = flaw.gadget.text.alsa('0')
79 -- </div>
80 --
81 -- <h2>Provider</h2>
82 --
83 -- <p>The ALSA provider relies on the <code>amixer</code> program to
84 -- get the master channel status as well as to modify to sound
85 -- level</p>
86 --
87 -- <p>The ALSA provider data is composed of the following fields.</p>
88 --
89 -- <ul>
90 --
91 -- <li><code>volume</code>
92 --
93 -- <p>The sound level in percent on the Master channel on the monitored card,
94 -- as a number.</p></li>
95 --
96 -- <li><code>s_volume</code>
97 --
98 -- <p>A string which represents the current sound level in percent of
99 -- the Master channel on the monitored card, with a visual hint when
100 -- it is muted. The string is 3 characters long, padded by the
101 -- left.</p></li>
102 --
103 -- </ul>
104 --
105 --
106 -- @author David Soulayrol &lt;david.soulayrol AT gmail DOT com&gt;
107 -- @copyright 2010,2011 David Soulayrol
108 module('flaw.alsa')
109
110
111 CHANNEL = 'Master'
112
113 --- The ALSA provider prototype.
114 --
115 -- <p>The ALSA provider type is set to <code>alsa._NAME</code>.</p>
116 --
117 -- @class table
118 -- @name ALSAProvider
119 ALSAProvider = flaw.provider.CyclicProvider:new{ type = _NAME }
120
121 --- Callback for provider refresh.
122 function ALSAProvider:do_refresh()
123 local f = io.popen("amixer -c " .. self.id .. " -- sget " .. CHANNEL)
124
125 -- The following is shamelessly ripped of the Obvious lib.
126
127 -- TODO: In the future, it would be nice to analyse the different
128 -- playback channels (see the amix output).
129 if f ~= nil then
130 local status = f:read('*all')
131 if status ~=nil then
132 self.data.s_volume = flaw.helper.strings.pad_left(
133 string.match(status, '(%d?%d?%d)%%'), 3)
134 self.data.volume = tonumber(self.data.s_volume) or 0
135 status = string.match(status, '%[(o[^%]]*)%]')
136 if status == nil or string.find(status, 'off', 1, true) then
137 self.data.s_volume = '---'
138 end
139 end
140 f:close()
141 end
142 end
143
144 --- Raise the sound level on the Master channel from the monitored
145 --- card.
146 --
147 -- @param offset the value to add to the current sound level. This
148 -- value is one if the paramenter is <code>nil</code>.
149 function ALSAProvider:raise(offset)
150 awful.util.spawn('amixer -q -c ' .. self.id ..
151 ' sset ' .. CHANNEL .. ' ' .. (offset or 1) .. '+', false)
152 self:do_refresh()
153 self:refresh_gadgets(true)
154 end
155
156 --- Lower the sound level on the Master channel from the monitored
157 --- card.
158 --
159 -- @param offset the value to remove from the current sound
160 -- level. This value is one if the paramenter is
161 -- <code>nil</code>.
162 function ALSAProvider:lower(offset)
163 awful.util.spawn('amixer -q -c ' .. self.id ..
164 ' sset ' .. CHANNEL .. ' ' .. (offset or 1) .. '-', false)
165 self:do_refresh()
166 self:refresh_gadgets(true)
167 end
168
169 --- Mute the Master channel from the monitored card.
170 function ALSAProvider:mute()
171 awful.util.spawn('amixer -q -c ' .. self.id ..
172 ' sset ' .. CHANNEL .. ' toggle', false)
173 self:do_refresh()
174 self:refresh_gadgets(true)
175 end
176
177 --- A factory for ALSA providers.
178 --
179 -- <p>Only one provider is built for a card ID. Created providers are
180 -- stored in the global provider cache.</p>
181 --
182 -- @param id the identifier of the card for which the new
183 -- provider should gather information
184 -- @return a brand new ALSA provider, or an existing one if the
185 -- given ID was already used to create one.
186 function provider_factory(id)
187 local p = flaw.provider.get(_NAME, id)
188 -- Create the provider if necessary.
189 if p == nil then
190 p = ALSAProvider:new{ id = id, data = { volume = 0, s_volume = '---'} }
191 flaw.provider.add(p)
192 end
193 return p
194 end
195
196 -- A Text gadget prototype for ALSA status display.
197 ALSATextGadget = flaw.gadget.TextGadget:new{}
198
199 -- Create the wrapped gadget.
200 function ALSATextGadget:create(wopt)
201 flaw.gadget.TextGadget.create(self, wopt)
202 self.widget:buttons(
203 awful.util.table.join(
204 awful.button({ }, 4, function() self.provider:raise() end),
205 awful.button({ }, 5, function() self.provider:lower() end),
206 awful.button({ }, 1, function() self.provider:mute() end)))
207 end
208
209 -- A progress bar gadget prototype for ALSA status display.
210 ALSABarGadget = flaw.gadget.BarGadget:new{}
211
212 -- Create the wrapped gadget.
213 function ALSABarGadget:create(wopt)
214 flaw.gadget.BarGadget.create(self, wopt)
215 self.widget:buttons(
216 awful.util.table.join(
217 awful.button({ }, 4, function() self.provider:raise() end),
218 awful.button({ }, 5, function() self.provider:lower() end),
219 awful.button({ }, 1, function() self.provider:mute() end)))
220 end
221
222 -- An icon gadget prototype for ALSA status display.
223 ALSAIconGadget = flaw.gadget.IconGadget:new{}
224
225 -- Create the wrapped gadget.
226 function ALSAIconGadget:create(wopt)
227 flaw.gadget.IconGadget.create(self, wopt)
228 self.widget:buttons(
229 awful.util.table.join(
230 awful.button({ }, 4, function() self.provider:raise() end),
231 awful.button({ }, 5, function() self.provider:lower() end),
232 awful.button({ }, 1, function() self.provider:mute() end)))
233 end
234
235 --- Module initialization routine.
236 --
237 -- <p> This method tests the available information on the system. If
238 -- alsa data can be found, it registers gadgets and returns the
239 -- module; otherwise it simply returns nil.</p>
240 --
241 -- @return return this module if it can be used on the system,
242 -- false otherwise.
243 function init()
244 -- TODO: test alsa.
245
246 flaw.gadget.register.bar(
247 _M, { delay = 1, value = 'volume' }, {}, ALSABarGadget)
248 flaw.gadget.register.text(
249 _M, { delay = 1, pattern = '$s_volume%' }, {}, ALSATextGadget)
250 flaw.gadget.register.icon(_M, { delay = 1 }, {}, ALSAIconGadget)
251
252 return _M
253 end