From 480aea8b372785d84f5b6e625a411ed59ead3983 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Mon, 9 Nov 2009 17:35:45 +0100 Subject: [PATCH] batsys: remove LuaFileSystem dependancy This module will most likely be moved to master in the future. While batat will be retired, and moved to contrib. --- extra/batsys.lua | 51 ++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/extra/batsys.lua b/extra/batsys.lua index 7238e23..82ebbe0 100644 --- a/extra/batsys.lua +++ b/extra/batsys.lua @@ -5,15 +5,13 @@ --------------------------------------------------- -- {{{ Grab environment -local lfs_available, lfs = pcall(require, "lfs") - local unpack = unpack local setmetatable = setmetatable -local getmetatable = getmetatable local io = { open = io.open } local string = { format = string.format } local math = { min = math.min, + ceil = math.ceil, floor = math.floor } local os = { @@ -22,29 +20,6 @@ local os = { } -- }}} --- {{{ Sysfs helper -local sys_metatable = { - __index = -- sysfs items available as lua tables - function (table, index) - path = table._path .. '/' .. index - attr = lfs.attributes(path) - if attr then - if attr.mode == "file" then - return io.open(path):read("*all") - elseif attr.mode == "directory" then - local obj = { _path = path } - setmetatable(obj, getmetatable(table)) - return obj - end - end - return nil - end, -} - -local sys = { _path = "/sys" } -setmetatable(sys, sys_metatable) --- }}} - -- Batsys: provides state, charge, and remaining time for a requested battery using sysfs module("vicious.batsys") @@ -55,7 +30,15 @@ local time_energy = {} -- {{{ Battery widget type local function worker(format, batid) - local battery = sys.class.power_supply[batid] + local battery = setmetatable({}, {__index = function(table, name) + local file = io.open("/sys/class/power_supply/"..batid.."/"..name) + if file then + local f = file:read("*all") + file:close() + return f + end + end}) + local battery_state = { ["Full\n"] = "↯", ["Unknown\n"] = "⌁", @@ -65,7 +48,7 @@ local function worker(format, batid) } -- Check if the battery is present - if not battery or battery.present == "0\n" then + if not battery.present == "1\n" then return {battery_state["Unknown\n"], 0, "N/A"} end @@ -82,23 +65,23 @@ local function worker(format, batid) return {battery_state["Unknown\n"], 0, "N/A"} end + -- Calculate percentage (but work around broken BAT/ACPI implementations) local charge = energy_now / energy_full - local percent = math.min(math.floor(charge * 100), 100) + local percent = math.min(math.ceil(charge * 100), 100) -- Calculate remaining (charging or discharging) time - -- - -- Default values on our first run if not time_energy[batid] then + -- Default values on our first run time_energy[batid] = { os.time(), energy_now } return {state, percent, "N/A"} end local time, energy = unpack(time_energy[batid]) - local difft = os.difftime(os.time(), time) - local diffe = energy_now - energy - local rate = diffe / difft + local timediff = os.difftime(os.time(), time) + local enerdiff = energy_now - energy + local rate = enerdiff / timediff if rate > 0 then timeleft = (energy_full - energy_now) / rate