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")
170 wmii
.set_conf ("battery.showtime", true)
171 wmii
.set_conf ("battery.showrate", true)
176 local batteries
= { }
178 -- The actual work performed here.
179 -- parses info, state file and preps for display
180 local function update_single_battery ( battery
)
182 local printout
= "N/A"
183 local colors
= wmii
.get_ctl("normcolors")
185 local fbatt
= io
.open(string.format(wmii
.get_conf("battery.statefile"), battery
["name"] ),"r")
187 return battery
["widget"]:show(printout
, colors
)
190 local batt
= fbatt
:read("*a")
193 local battpresent
= batt
:match('present:%s+(%w+)')
194 if battpresent
~= "yes" then
195 return battery
["widget"]:show(printout
, colors
)
198 local low
= wmii
.get_conf ("battery.low")
199 local critical
= wmii
.get_conf ("battery.critical")
201 local fbattinfo
= io
.open(string.format(wmii
.get_conf("battery.infofile"), battery
["name"]),"r")
202 local battinfo
= fbattinfo
:read("*a")
205 local batt_percent
= batt
:match('remaining capacity:%s+(%d+)')
206 / battinfo
:match('last full capacity:%s+(%d+)') * 100
207 local batt_state
= batt
:match('charging state:%s+(%w+)')
209 -- Take action in case battery is low/critical
210 if batt_percent
<= critical
then
211 if batt_state
== "discharging" and not battery
["warned_crit"] then
212 wmii
.log("Warning about critical battery.")
213 os
.execute(wmii
.get_conf("battmon.critical_action"), "&")
214 battery
["warned_crit"] = true
216 colors
= string.gsub(colors
, "^%S+ %S+",
217 wmii
.get_conf ("battery.critical_fgcolor")
219 .. wmii
.get_conf ("battery.critical_bgcolor"),
221 elseif batt_percent
<= low
then
222 if batt_state
== "discharging" and not battery
["warned_low"] then
223 wmii
.log("Warning about low battery.")
224 os
.execute(wmii
.get_conf("battmon.low_action"), "&")
225 battery
["warned_low"] = true
227 colors
= string.gsub(colors
, "^%S+ %S+",
228 wmii
.get_conf ("battery.low_fgcolor")
230 .. wmii
.get_conf ("battery.low_bgcolor"),
233 battery
["warned_low"] = true
234 battery
["warned_crit"] = true
238 -- If percent is 100 and state is discharging then
239 -- the battery is full and not discharging.
240 if (batt_state
== "charged") or (batt_state
== "discharging" and batt_percent
>= 97) then
243 if batt_state
== "charging" then
246 if batt_state
== "discharging" then
250 local batt_rate
= batt
:match('present rate:%s+(%d+)') * 1
253 if wmii
.get_conf(battery
.showtime
) then
255 if batt_rate
> 0 then
256 if batt_state
== "^" then
257 batt_time
= (battinfo
:match('last full capacity:%s+(%d+)') - batt
:match('remaining capacity:%s+(%d+)')) / batt_rate
259 batt_time
= batt
:match('remaining capacity:%s+(%d+)') / batt_rate
261 local hour
= string.format("%d",batt_time
)
262 local min = (batt_time
- hour
) * 60
271 batt_time
= hour
.. ':'
272 batt_time
= batt_time
.. string.format("%.2d",min)
276 local battrate_string
= ""
277 if wmii
.get_conf(battery
.showrate
) then
278 batt_rate
= batt_rate
/1000
279 battrate_string
= string.format("%.2f",batt_rate
) .. 'W '
281 printout
= battrate_string
.. batt_time
.. '(' .. batt_state
.. string.format("%.0f",batt_percent
) .. batt_state
.. ')'
283 battery
["widget"]:show(printout
, colors
)
286 -- ------------------------------------------------------------
287 -- The battery status update function (wmii.timer function)
288 local function update_batt_data (time_since_update
)
290 local batt_names
= wmii
.get_conf("battery.names");
292 for battery
in batt_names
:gmatch("%w+") do
293 if( not batteries
[battery
] ) then
294 batteries
[battery
] = {
296 widget
= wmii
.widget
:new ("901_battery_" .. battery
),
301 update_single_battery( batteries
[battery
] )
304 return wmii
.get_conf("battery.poll_rate")
308 local timer
= wmii
.timer
:new (update_batt_data
, 1)