Clean up battery status plugin.
[wmiirc-lua.git] / plugins / battery.lua
blob3538e4eada874095bcf328efe1a8323e48c810fa
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")
171 -- Local Variables
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")
183 if fbatt == nil then
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")
213 .. " "
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")
224 .. " "
225 .. wmii.get_conf ("battery.low_bgcolor"),
227 else
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
236 batt_state = "="
238 if batt_state == "charging" then
239 batt_state = "^"
241 if batt_state == "discharging" then
242 batt_state = "v"
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] = {
259 name = battery,
260 widget = wmii.widget:new ("901_battery_" .. battery),
261 warned_low = false,
262 warned_crit = false,
265 update_single_battery( batteries[battery] )
268 return wmii.get_conf("battery.poll_rate")
272 local timer = wmii.timer:new (update_batt_data, 1)