6 battery.lua - wmiirc-lua plugin for battery percentage
11 wmii.load_plugin("battery")
13 -- To configure (after loading plugin)
16 wmii.set_conf("battery.names", "BAT0,BAT1");
18 -- Polling rate (in seconds)
19 wmii.set_conf("battery.poll_rate", 30)
23 This plugin module provides a battery usage display.
25 =head1 CONFIGURATION AND ENVIRONMENT
27 There are several configurable options at the moment, most of which will not
28 need to be modified from the defaults for most users.
34 A comma-separated list of battery names to poll for status. This allows the
35 widget to display multiple battery names.
39 =item battery.poll_rate
41 Time in seconds to wait between checks for battery status.
47 Provide a "low battery" warning at this percentage of remaining capacity.
48 Colour of widget will change to the defined value, and the low_action, if any,
53 =item battery.low_fgcolor
55 Foreground colour of widget when in low battery state.
59 =item battery.low_bgcolor
61 Background colour of widget when in low battery state.
65 =item battery.low_action
67 Shell command to invoke on entering low battery state.
71 echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -
73 =item battery.critical
75 Provide a "critical battery" warning at this percentage of remaining capacity.
76 Colour of widget will change to the defined value, and the critical_action, if any,
81 =item battery.critical_fgcolor
83 Foreground colour of widget when in critical battery state.
87 =item battery.critical_bgcolor
89 Background colour of widget when in critical battery state.
93 =item battery.critical_action
95 Shell command to invoke on entering critical battery state.
99 echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -
103 =head1 BUGS AND LIMITATIONS
105 Please report problems to the author.
112 You can't have different low/critical warning thresholds or colours per
113 battery. If you actually want this, please send a patch.
119 L<wmii(1)>, L<lua(1)>
123 Dave O'Neill <dmo@dmo.ca>
125 Based on a port by Stefan Riegler <sr@bigfatflat.net> of the ruby-wmiirc
126 standard-plugin.rb battery handling originally written Mauricio Fernandez.
128 =head1 LICENCE AND COPYRIGHT
130 Copyright (c) 2007, Stefan Riegler <sr@bigfatflat.net>
131 Copyright (c) 2008, Dave O'Neill <dmo@dmo.ca>
133 This is free software. You may redistribute copies of it under the terms of
134 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
135 is NO WARRANTY, to the extent permitted by law.
141 local wmii
= require("wmii")
142 local io
= require("io")
143 local os
= require("os")
144 local string = require("string")
150 -- Configuration Settings
152 wmii
.set_conf ("battery.poll_rate", 30)
154 wmii
.set_conf ("battery.names", "BAT0")
156 wmii
.set_conf ("battery.low", 15)
157 wmii
.set_conf ("battery.low_fgcolor", "#000000")
158 wmii
.set_conf ("battery.low_bgcolor", "#FFFF66")
159 wmii
.set_conf ("battery.low_action", 'echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -')
161 wmii
.set_conf ("battery.critical", 5)
162 wmii
.set_conf ("battery.critical_fgcolor", "#000000")
163 wmii
.set_conf ("battery.critical_bgcolor", "#FF0000")
164 wmii
.set_conf ("battery.critical_action", 'echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -')
166 -- Should not need to be modified on Linux
167 wmii
.set_conf ("battery.statefile", "/proc/acpi/battery/%s/state")
168 wmii
.set_conf ("battery.infofile", "/proc/acpi/battery/%s/info")
173 local batteries
= { }
175 -- The actual work performed here.
176 -- parses info, state file and preps for display
177 local function update_single_battery ( battery
)
179 local printout
= "N/A"
180 local colors
= wmii
.get_ctl("normcolors")
182 local fbatt
= io
.open(string.format(wmii
.get_conf("battery.statefile"), battery
["name"] ),"r")
184 return battery
["widget"]:show(printout
, colors
)
187 local batt
= fbatt
:read("*a")
189 local battpresent
= batt
:match('present:%s+(%w+)')
190 if battpresent
~= "yes" then
191 return battery
["widget"]:show(printout
, colors
)
194 local low
= wmii
.get_conf ("battery.low")
195 local critical
= wmii
.get_conf ("battery.critical")
197 local fbattinfo
= io
.open(string.format(wmii
.get_conf("battery.infofile"), battery
["name"]),"r")
198 local battinfo
= fbattinfo
:read("*a")
200 local batt_percent
= batt
:match('remaining capacity:%s+(%d+)')
201 / battinfo
:match('last full capacity:%s+(%d+)') * 100
202 local batt_state
= batt
:match('charging state:%s+(%w+)')
204 -- Take action in case battery is low/critical
205 if batt_percent
<= critical
then
206 if batt_state
== "discharging" and not battery
["warned_crit"] then
207 wmii
.log("Warning about critical battery.")
208 os
.execute(wmii
.get_conf("battmon.critical_action"), "&")
209 battery
["warned_crit"] = true
211 colors
= string.gsub(colors
, "^%S+ %S+",
212 wmii
.get_conf ("battery.critical_fgcolor")
214 .. wmii
.get_conf ("battery.critical_bgcolor"),
216 elseif batt_percent
<= low
then
217 if batt_state
== "discharging" and not battery
["warned_low"] then
218 wmii
.log("Warning about low battery.")
219 os
.execute(wmii
.get_conf("battmon.low_action"), "&")
220 battery
["warned_low"] = true
222 colors
= string.gsub(colors
, "^%S+ %S+",
223 wmii
.get_conf ("battery.low_fgcolor")
225 .. wmii
.get_conf ("battery.low_bgcolor"),
228 battery
["warned_low"] = true
229 battery
["warned_crit"] = true
233 -- If percent is 100 and state is discharging then
234 -- the battery is full and not discharging.
235 if (batt_state
== "charged") or (batt_state
== "discharging" and batt_percent
>= 97) then
238 if batt_state
== "charging" then
241 if batt_state
== "discharging" then
245 printout
= batt_state
.. string.format("%.0f",batt_percent
) .. batt_state
247 battery
["widget"]:show(printout
, colors
)
250 -- ------------------------------------------------------------
251 -- The battery status update function (wmii.timer function)
252 local function update_batt_data (time_since_update
)
254 local batt_names
= wmii
.get_conf("battery.names");
256 for battery
in batt_names
:gmatch("%w+") do
257 if( not batteries
[battery
] ) then
258 batteries
[battery
] = {
260 widget
= wmii
.widget
:new ("901_battery_" .. battery
),
265 update_single_battery( batteries
[battery
] )
268 return wmii
.get_conf("battery.poll_rate")
272 local timer
= wmii
.timer
:new (update_batt_data
, 1)