aboutsummaryrefslogtreecommitdiff
path: root/widgets/bat.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
commit237470c8f45190b213e3a173ce6ae1a74b3e11fe (patch)
tree7f53c8144761947d4bde20715bcad34f4be0d6c0 /widgets/bat.lua
parent9a82d4113a8271b7dfc7506f2b07379e3ede89a8 (diff)
downloadvicious-legacy-237470c8f45190b213e3a173ce6ae1a74b3e11fe.tar.xz
API: transform widgets namespace table to a directory
Diffstat (limited to 'widgets/bat.lua')
-rw-r--r--widgets/bat.lua79
1 files changed, 79 insertions, 0 deletions
diff --git a/widgets/bat.lua b/widgets/bat.lua
new file mode 100644
index 0000000..d6e3e6b
--- /dev/null
+++ b/widgets/bat.lua
@@ -0,0 +1,79 @@
+---------------------------------------------------
+-- Licensed under the GNU General Public License v2
+-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
+---------------------------------------------------
+
+-- {{{ Grab environment
+local tonumber = tonumber
+local setmetatable = setmetatable
+local string = { format = string.format }
+local helpers = require("vicious.helpers")
+local math = {
+ min = math.min,
+ floor = math.floor
+}
+-- }}}
+
+
+-- Batsys: provides state, charge, and remaining time for a requested battery
+module("vicious.widgets.bat")
+
+
+-- {{{ Battery widget type
+local function worker(format, batid)
+ -- Apple PMU and ACPI/procfs battery widgets are in the [contrib] branch
+ local battery = helpers.pathtotable("/sys/class/power_supply/" .. batid)
+ local battery_state = {
+ ["Full\n"] = "↯",
+ ["Unknown\n"] = "⌁",
+ ["Charged\n"] = "↯",
+ ["Charging\n"] = "+",
+ ["Discharging\n"] = "-"
+ }
+
+ -- Check if the battery is present
+ if not battery.present == "1\n" then
+ return {battery_state["Unknown\n"], 0, "N/A"}
+ end
+
+
+ -- Get state information
+ local state = battery_state[battery.status] or battery_state["Unknown\n"]
+
+ -- Get capacity information
+ if battery.charge_now then
+ remaining, capacity = battery.charge_now, battery.charge_full
+ elseif battery.energy_now then
+ remaining, capacity = battery.energy_now, battery.energy_full
+ else
+ return {battery_state["Unknown\n"], 0, "N/A"}
+ end
+
+ -- Calculate percentage (but work around broken BAT/ACPI implementations)
+ local percent = math.min(math.floor(remaining / capacity * 100), 100)
+
+
+ -- Get charge information
+ if battery.current_now then
+ rate = battery.current_now
+ else -- Todo: other rate sources, as with capacity?
+ return {state, percent, "N/A"}
+ end
+
+ -- Calculate remaining (charging or discharging) time
+ if state == "+" then
+ timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
+ elseif state == "-" then
+ timeleft = tonumber(remaining) / tonumber(rate)
+ else
+ return {state, percent, "N/A"}
+ end
+ local hoursleft = math.floor(timeleft)
+ local minutesleft = math.floor((timeleft - hoursleft) * 60 )
+ local time = string.format("%02d:%02d", hoursleft, minutesleft)
+
+ return {state, percent, time}
+end
+-- }}}
+
+setmetatable(_M, { __call = function(_, ...) return worker(...) end })