6 volume.lua - wmiirc-lua plugin for volume control
10 -- in your wmiirc.lua:
11 wmii.load_plugin("volume")
13 -- If you also want keybindings for volume control, you might want to add:
14 wmii.add_key_handler('Mod1-minus', function (key)
15 volume.update_volume(-1)
17 wmii.add_key_handler('Mod1-plus', function (key)
18 volume.update_volume(1)
24 This plugin module provides a volume control widget that uses I<amixer> to view and control volume.
26 The following controls are available:
30 =item left mouse click
32 Spawns a mixer (TODO: not implemented yet)
34 =item right mouse click
38 =item scroll wheel up/down
40 Adjusts volume up/down
44 =head1 CONFIGURATION AND ENVIRONMENT
46 There are two configurable options at the moment, modifiable via wmii.set_conf():
52 Interval in seconds for polling the mixer, to determine current volume level.
53 Without this, your volume display can get out of sync with reality if other
54 apps adjust the mixer volume.
60 The name of the mixer setting to adjust. Default is 'Master', which should
61 work fine for most, but if you have a more complex audio setup, you may wish to
68 One public method is provided. It can be used in your own plugins, in
73 =item update_volume ( val )
75 Updates the volume, and changes the plugin display.
77 If 'val' is numeric and positive, volume is increased by that many steps
78 (volume controls currently work against "exact hardware value" as described in
79 the L<amixer(1)> manual. The scale varies from hardware to hardware -- pinal
80 Tap's goes to 11. Mine goes to 2^5 - 1.).
82 If 'val' is negative, volume is decremented that many steps.
84 If 'val' is set to the string 'mute', volume is muted. If set to 'unmute',
89 =head1 BUGS AND LIMITATIONS
91 Please report problems to the author.
94 Current known issues and TODO items:
98 =item left-click does not spawn a mixer app
100 =item validity and bounds-checking on update_volume()
102 =item rather than using hardware values, increment/decrement with percentages?
108 L<wmii(1)>, L<lua(1)>
112 Dave O'Neill <dmo@dmo.ca>
114 =head1 LICENCE AND COPYRIGHT
116 Copyright (c) 2007, Dave O'Neill <dmo@dmo.ca>
118 This is free software. You may redistribute copies of it under the terms of
119 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
120 is NO WARRANTY, to the extent permitted by law.
125 local wmii
= require("wmii")
126 local os
= require("os")
127 local io
= require("io")
128 local math
= require("math")
135 -- ------------------------------------------------------------
136 -- VOLUME CONFIGURATION VARIABLES
138 -- these can be overridden by wmiirc
140 wmii
.set_conf ("volume.update", 10);
141 wmii
.set_conf ("volume.mixer", "Master");
143 -- ------------------------------------------------------------
148 widget
= wmii
.widget
:new ("999_volume")
150 local function _amixer_command ( cmd
)
152 wmii
.log( "about to run " .. cmd
)
153 local file
= io
.popen( cmd
)
154 local status
= file
:read("*a")
158 -- omfg. lua regexes are uuuugly
159 volume
= status
:match("%[(%d+%%)%]");
160 if status
:match("%[off%]") then
167 local function mixer_set_volume (value
)
168 wmii
.log( "mixer_set_volume(" .. value
.. ")")
169 local mixer
= wmii
.get_conf("volume.mixer")
170 return _amixer_command("amixer set \"" .. mixer
.. ",0\" " .. value
)
173 local function mixer_get_volume ( )
174 wmii
.log( "mixer_get_volume")
175 local mixer
= wmii
.get_conf("volume.mixer")
176 return _amixer_command("amixer get \"" .. mixer
.. ",0\"")
179 function update_volume ( new_vol
)
181 wmii
.log("update_volume(" .. new_vol
.. ")")
184 if type( new_vol
) == "number" then
192 value
= math
.abs( new_vol
) .. sign
198 local volume
= mixer_set_volume( value
)
200 widget
:show("VOL [" .. volume
.. "]")
203 local function button_handler (ev
, button
)
205 wmii
.log("button_handler(" .. ev
.. "," .. button
.. ")")
208 elseif button
== 2 then
210 elseif button
== 3 then
213 local cur_volume
= mixer_get_volume()
214 if cur_volume
== "OFF" then
219 update_volume( action
)
220 elseif button
== 4 then
223 elseif button
== 5 then
229 widget
:add_event_handler("RightBarClick", button_handler
)
232 local function volume_timer ( timer
)
234 wmii
.log("volume_timer()")
237 -- returning a positive number of seconds before next wakeup, or
238 -- nil (or no return at all) repeats the last schedule, or
239 -- -1 to stop the timer
243 timer
= wmii
.timer
:new (volume_timer
, 1)