added options to control if the current rate and remaining time should be displayed
[wmiirc-lua.git] / plugins / battery.lua
blob4fc587a74191b05c94047b5fd1f5880f1e7d489e
1 --[[
2 =pod
4 =head1 NAME
6 battery.lua - wmiirc-lua plugin for battery percentage
8 =head1 SYNOPSIS
10 -- in your wmiirc
11 wmii.load_plugin("battery")
13 -- To configure (after loading plugin)
15 -- Multiple batteries
16 wmii.set_conf("battery.names", "BAT0,BAT1");
18 -- Polling rate (in seconds)
19 wmii.set_conf("battery.poll_rate", 30)
21 =head1 DESCRIPTION
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.
30 =over 4
32 =item battery.names
34 A comma-separated list of battery names to poll for status. This allows the
35 widget to display multiple battery names.
37 Defaults to "BAT0"
39 =item battery.poll_rate
41 Time in seconds to wait between checks for battery status.
43 Defaults to 30
45 =item battery.low
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,
49 will be invoked.
51 Defaults to 15
53 =item battery.low_fgcolor
55 Foreground colour of widget when in low battery state.
57 Defaults to #000000
59 =item battery.low_bgcolor
61 Background colour of widget when in low battery state.
63 Defaults to #FFFF66
65 =item battery.low_action
67 Shell command to invoke on entering low battery state.
69 Defaults to
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,
77 will be invoked.
79 Defaults to 5
81 =item battery.critical_fgcolor
83 Foreground colour of widget when in critical battery state.
85 Defaults to #000000
87 =item battery.critical_bgcolor
89 Background colour of widget when in critical battery state.
91 Defaults to #FF0000
93 =item battery.critical_action
95 Shell command to invoke on entering critical battery state.
97 Defaults to
99 echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -
101 =back
103 =head1 BUGS AND LIMITATIONS
105 Please report problems to the author.
106 Patches are welcome.
108 =over 4
110 =item *
112 You can't have different low/critical warning thresholds or colours per
113 battery. If you actually want this, please send a patch.
115 =back
117 =head1 SEE ALSO
119 L<wmii(1)>, L<lua(1)>
121 =head1 AUTHOR
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.
137 =cut
139 --]]
141 local wmii = require("wmii")
142 local io = require("io")
143 local os = require("os")
144 local string = require("string")
146 module("battery")
147 api_version=0.1
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)
174 -- Local Variables
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")
186 if fbatt == nil then
187 return battery["widget"]:show(printout, colors)
190 local batt = fbatt:read("*a")
191 fbatt:close()
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")
203 fbattinfo:close()
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")
218 .. " "
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")
229 .. " "
230 .. wmii.get_conf ("battery.low_bgcolor"),
232 else
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
241 batt_state = "="
243 if batt_state == "charging" then
244 batt_state = "^"
246 if batt_state == "discharging" then
247 batt_state = "v"
250 local batt_rate = batt:match('present rate:%s+(%d+)') * 1
252 local batt_time = ""
253 if wmii.get_conf(battery.showtime) then
254 batt_time = "inf"
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
258 else
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
264 if min > 59 then
265 min = min - 60
266 hour = hour + 1
268 if min < 0 then
269 min = 0
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] = {
295 name = battery,
296 widget = wmii.widget:new ("901_battery_" .. battery),
297 warned_low = false,
298 warned_crit = false,
301 update_single_battery( batteries[battery] )
304 return wmii.get_conf("battery.poll_rate")
308 local timer = wmii.timer:new (update_batt_data, 1)