2019-09-05 07:41:38 +02:00
|
|
|
-- battery widget type for GNU/Linux
|
|
|
|
-- Copyright (C) 2010 Adrian C. <anrxc@sysphere.org>
|
|
|
|
-- Copyright (C) 2013 NormalRa <normalrawr@gmail.com>
|
|
|
|
-- Copyright (C) 2017 David Udelson <dru5@cornell.edu>
|
|
|
|
-- Copyright (C) 2017 Roberto <empijei@users.noreply.github.com>
|
|
|
|
-- Copyright (C) 2017 mutlusun <mutlusun@github.com>
|
|
|
|
--
|
|
|
|
-- This file is part of Vicious.
|
|
|
|
--
|
|
|
|
-- Vicious is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as
|
|
|
|
-- published by the Free Software Foundation, either version 2 of the
|
|
|
|
-- License, or (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- Vicious is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
|
2009-07-30 01:48:07 +02:00
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
2009-11-11 03:50:25 +01:00
|
|
|
local string = { format = string.format }
|
2009-10-15 23:11:36 +02:00
|
|
|
local math = {
|
|
|
|
min = math.min,
|
|
|
|
floor = math.floor
|
|
|
|
}
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2019-09-05 07:41:38 +02:00
|
|
|
local helpers = require"vicious.helpers"
|
|
|
|
-- }}}
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2013-12-22 13:09:16 +01:00
|
|
|
-- Bat: provides state, charge, remaining time, and wear for a requested battery
|
2012-06-15 18:07:05 +02:00
|
|
|
-- vicious.widgets.bat
|
2017-01-25 17:59:31 +01:00
|
|
|
local bat_linux = {}
|
2009-07-30 01:48:07 +02:00
|
|
|
|
|
|
|
-- {{{ Battery widget type
|
2010-03-14 03:37:40 +01:00
|
|
|
local function worker(format, warg)
|
|
|
|
if not warg then return end
|
|
|
|
|
|
|
|
local battery = helpers.pathtotable("/sys/class/power_supply/"..warg)
|
2009-07-30 01:48:07 +02:00
|
|
|
local battery_state = {
|
2010-03-06 22:12:47 +01:00
|
|
|
["Full\n"] = "↯",
|
|
|
|
["Unknown\n"] = "⌁",
|
|
|
|
["Charged\n"] = "↯",
|
|
|
|
["Charging\n"] = "+",
|
2017-05-27 18:19:42 +02:00
|
|
|
["Discharging\n"] = "-"
|
2009-07-30 01:48:07 +02:00
|
|
|
}
|
|
|
|
|
2017-02-17 19:23:37 +01:00
|
|
|
-- Get current power usage in watt
|
|
|
|
local curpower = "N/A"
|
|
|
|
if battery.power_now then
|
|
|
|
curpower = string.format("%.2f", tonumber(battery.power_now) /1000000)
|
|
|
|
end
|
|
|
|
|
2009-10-04 16:26:34 +02:00
|
|
|
-- Check if the battery is present
|
2010-03-15 02:42:36 +01:00
|
|
|
if battery.present ~= "1\n" then
|
2017-02-17 19:23:37 +01:00
|
|
|
return {battery_state["Unknown\n"], 0, "N/A", 0, curpower}
|
2009-10-04 00:54:27 +02:00
|
|
|
end
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2009-10-04 00:54:27 +02:00
|
|
|
-- Get state information
|
2009-11-11 03:50:25 +01:00
|
|
|
local state = battery_state[battery.status] or battery_state["Unknown\n"]
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2009-11-11 03:50:25 +01:00
|
|
|
-- Get capacity information
|
2019-09-05 07:41:38 +02:00
|
|
|
local remaining, capacity, capacity_design
|
2009-11-11 03:50:25 +01:00
|
|
|
if battery.charge_now then
|
|
|
|
remaining, capacity = battery.charge_now, battery.charge_full
|
2013-12-22 13:09:16 +01:00
|
|
|
capacity_design = battery.charge_full_design or capacity
|
2009-11-11 03:50:25 +01:00
|
|
|
elseif battery.energy_now then
|
|
|
|
remaining, capacity = battery.energy_now, battery.energy_full
|
2013-12-22 13:09:16 +01:00
|
|
|
capacity_design = battery.energy_full_design or capacity
|
2009-11-11 03:50:25 +01:00
|
|
|
else
|
2017-02-17 19:23:37 +01:00
|
|
|
return {battery_state["Unknown\n"], 0, "N/A", 0, curpower}
|
2009-11-11 03:50:25 +01:00
|
|
|
end
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2013-12-22 13:09:16 +01:00
|
|
|
-- Calculate capacity and wear percentage (but work around broken BAT/ACPI implementations)
|
2009-10-15 23:11:36 +02:00
|
|
|
local percent = math.min(math.floor(remaining / capacity * 100), 100)
|
2013-12-22 13:09:16 +01:00
|
|
|
local wear = math.floor(100 - capacity / capacity_design * 100)
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2009-11-11 03:50:25 +01:00
|
|
|
-- Get charge information
|
2019-09-05 07:41:38 +02:00
|
|
|
local rate
|
2009-11-11 03:50:25 +01:00
|
|
|
if battery.current_now then
|
2012-02-09 12:43:22 +01:00
|
|
|
rate = tonumber(battery.current_now)
|
2011-02-15 03:49:43 +01:00
|
|
|
elseif battery.power_now then
|
2012-02-09 12:43:22 +01:00
|
|
|
rate = tonumber(battery.power_now)
|
2011-02-15 03:49:43 +01:00
|
|
|
else
|
2017-02-17 19:23:37 +01:00
|
|
|
return {state, percent, "N/A", wear, curpower}
|
2009-11-11 03:50:25 +01:00
|
|
|
end
|
|
|
|
|
2009-10-04 00:54:27 +02:00
|
|
|
-- Calculate remaining (charging or discharging) time
|
2011-02-21 06:46:49 +01:00
|
|
|
local time = "N/A"
|
2011-11-20 17:44:04 +01:00
|
|
|
|
2012-02-09 12:43:22 +01:00
|
|
|
if rate ~= nil and rate ~= 0 then
|
2019-09-05 07:41:38 +02:00
|
|
|
local timeleft
|
2011-02-21 06:46:49 +01:00
|
|
|
if state == "+" then
|
2019-09-05 07:41:38 +02:00
|
|
|
timeleft = (tonumber(capacity)-tonumber(remaining)) / tonumber(rate)
|
2017-05-27 18:19:42 +02:00
|
|
|
elseif state == "-" then
|
2011-02-21 06:46:49 +01:00
|
|
|
timeleft = tonumber(remaining) / tonumber(rate)
|
|
|
|
else
|
2017-02-17 19:23:37 +01:00
|
|
|
return {state, percent, time, wear, curpower}
|
2011-02-21 06:46:49 +01:00
|
|
|
end
|
2011-11-20 17:44:04 +01:00
|
|
|
|
2012-02-09 12:43:22 +01:00
|
|
|
-- Calculate time
|
|
|
|
local hoursleft = math.floor(timeleft)
|
|
|
|
local minutesleft = math.floor((timeleft - hoursleft) * 60 )
|
2011-11-20 17:44:04 +01:00
|
|
|
|
2011-02-21 06:46:49 +01:00
|
|
|
time = string.format("%02d:%02d", hoursleft, minutesleft)
|
2009-07-30 01:48:07 +02:00
|
|
|
end
|
2009-10-04 00:54:27 +02:00
|
|
|
|
2017-02-17 19:23:37 +01:00
|
|
|
return {state, percent, time, wear, curpower}
|
2009-07-30 01:48:07 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
2019-09-05 07:41:38 +02:00
|
|
|
return helpers.setcall(bat_linux, worker)
|