1-- contrib/batpmu_linux.lua
2-- Copyright (C) 2010  Adrian C. <anrxc@sysphere.org>
3-- Copyright (C) 2017  Jörg Thalheim <joerg@higgsboson.tk>
4--
5-- This file is part of Vicious.
6--
7-- Vicious is free software: you can redistribute it and/or modify
8-- it under the terms of the GNU General Public License as
9-- published by the Free Software Foundation, either version 2 of the
10-- License, or (at your option) any later version.
11--
12-- Vicious is distributed in the hope that it will be useful,
13-- but WITHOUT ANY WARRANTY; without even the implied warranty of
14-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15-- GNU General Public License for more details.
16--
17-- You should have received a copy of the GNU General Public License
18-- along with Vicious.  If not, see <https://www.gnu.org/licenses/>.
19
20-- {{{ Grab environment
21local tonumber = tonumber
22local io = { open = io.open }
23local setmetatable = setmetatable
24local math = {
25    min = math.min,
26    floor = math.floor
27}
28local string = {
29    find = string.find,
30    match = string.match,
31    format = string.format
32}
33-- }}}
34
35
36-- Batpmu: provides state, charge and remaining time for a requested battery using PMU
37-- vicious.contrib.batpmu
38local batpmu_linux = {}
39
40
41-- {{{ Battery widget type
42local function worker(format, batid)
43    local battery_state = {
44        ["full"] = "↯",
45        ["unknown"] = "⌁",
46        ["00000013"] = "+",
47        ["00000011"] = "-"
48    }
49
50    -- Get /proc/pmu/battery* state
51    local f = io.open("/proc/pmu/" .. batid)
52    -- Handler for incompetent users
53    if not f then return {battery_state["unknown"], 0, "N/A"} end
54    local statefile = f:read("*all")
55    f:close()
56
57    -- Get /proc/pmu/info data
58    local f = io.open("/proc/pmu/info")
59    local infofile = f:read("*all")
60    f:close()
61
62    -- Check if the battery is present
63    if infofile == nil or string.find(infofile, "Battery count[%s]+:[%s]0") then
64        return {battery_state["unknown"], 0, "N/A"}
65    end
66
67
68    -- Get capacity and charge information
69    local capacity = string.match(statefile, "max_charge[%s]+:[%s]([%d]+).*")
70    local remaining = string.match(statefile, "charge[%s]+:[%s]([%d]+).*")
71
72    -- Calculate percentage
73    local percent = math.min(math.floor(remaining / capacity * 100), 100)
74
75
76    -- Get timer information
77    local timer = string.match(statefile, "time rem%.[%s]+:[%s]([%d]+).*")
78    if timer == "0" then return {battery_state["full"], percent, "N/A"} end
79
80    -- Get state information
81    local state = string.match(statefile, "flags[%s]+:[%s]([%d]+).*")
82    local state = battery_state[state] or battery_state["unknown"]
83
84    -- Calculate remaining (charging or discharging) time
85    local hoursleft = math.floor(tonumber(timer) / 3600)
86    local minutesleft = math.floor((tonumber(timer) / 60) % 60)
87    local time = string.format("%02d:%02d", hoursleft, minutesleft)
88
89    return {state, percent, time}
90end
91-- }}}
92
93return setmetatable(batpmu_linux, { __call = function(_, ...) return worker(...) end })
94