aboutsummaryrefslogtreecommitdiff
path: root/bat.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2009-07-30 01:48:07 +0200
committerAdrian C. (anrxc) <anrxc@sysphere.org>2009-07-30 01:48:07 +0200
commite2d503ee36d0e3f0d20f7f888ab03caac68c32bb (patch)
tree558ffd011e2d1bc8ebd06fab4f21eb6da05c4ac3 /bat.lua
parent19ca7b5a038508179b2e195d223ecaa2299f14a1 (diff)
downloadvicious-legacy-e2d503ee36d0e3f0d20f7f888ab03caac68c32bb.tar.xz
Added a new, standalone, battery widget.
The widget reads /proc/acpi/battery/*/{info,state} and doesn't require an external utility like 'acpi' or 'acpitool'. It returns state, charge and remaining time information, just like the old bat (now batat) widget. Using /sys/class/power_supply we would need to open much more file descriptors so we use /proc for now.
Diffstat (limited to 'bat.lua')
-rw-r--r--bat.lua79
1 files changed, 79 insertions, 0 deletions
diff --git a/bat.lua b/bat.lua
new file mode 100644
index 0000000..143cd24
--- /dev/null
+++ b/bat.lua
@@ -0,0 +1,79 @@
+----------------------------------------------------------
+-- Licensed under the GNU General Public License version 2
+-- * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
+----------------------------------------------------------
+
+-- {{{ Grab environment
+local tonumber = tonumber
+local io = { open = io.open }
+local math = { floor = math.floor }
+local string = {
+ find = string.find,
+ match = string.match,
+ format = string.format
+}
+-- }}}
+
+
+-- Bat: provides state, charge, and remaining time for a requested battery
+module("vicious.bat")
+
+
+-- {{{ Battery widget type
+function worker(format, batid)
+ -- Initialise tables
+ local battery_state = {
+ ["full"] = "*",
+ ["unknown"] = " ",
+ ["charged"] = "*",
+ ["charging"] = "+",
+ ["discharging"] = "-"
+ }
+
+ -- Get /proc/acpi/battery info
+ local finfo = io.open("/proc/acpi/battery/"..batid.."/info")
+ local infofile = finfo:read("*all")
+ finfo:close()
+
+ -- Check if the file wasn't found or the battery isn't present
+ if infofile == nil or string.find(infofile, "present:[%s]+no") then
+ return { "/", "/", "/" }
+ else
+ -- Get capacity information
+ local capacity = string.match(infofile, "last full capacity:[%s]+([%d]+).*")
+
+
+ -- Get /proc/acpi/battery state
+ local fstate = io.open("/proc/acpi/battery/"..batid.."/state")
+ local statefile = fstate:read("*all")
+ fstate:close()
+
+ -- Get state information
+ local state = string.match(statefile, "charging state:[%s]+([%a]+).*")
+ local state = battery_state[state] or battery_state["unknown"]
+
+ -- Get charge information
+ local rate = string.match(statefile, "present rate:[%s]+([%d]+).*")
+ local remaining = string.match(statefile, "remaining capacity:[%s]+([%d]+).*")
+
+
+ -- Calculate percentage
+ local percent = math.floor(remaining / capacity * 100)
+ local percent = string.format("%02d", percent)
+
+ -- 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, "/" }
+ 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
+end
+-- }}}