Debian Codegraph @

vicious/contrib/batproc_linux.lua

raw symbols SCIP
1 -- contrib/batproc_linux.lua
2 -- Copyright (C) 2010 Adrian C. <[email protected]>
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
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
21 local tonumber = tonumber
22 local io = { open = io.open }
23 local setmetatable = setmetatable
24 local math = {
25 min = math.min,
26 floor = math.floor
27 }
28 local string = {
29 find = string.find,
30 match = string.match,
31 format = string.format
32 }
33 -- }}}
34
35
36 -- Batproc: provides state, charge, and remaining time for a requested battery using procfs
37 -- vicious.contrib.batproc
38 local batproc_linux = {}
39
40
41 -- {{{ Battery widget type
42 local function worker(format, batid)
43 local battery_state = {
44 ["full"] = "↯",
45 ["unknown"] = "⌁",
46 ["charged"] = "↯",
47 ["charging"] = "+",
48 ["discharging"] = "-"
49 }
50
51 -- Get /proc/acpi/battery info
52 local f = io.open("/proc/acpi/battery/"..batid.."/info")
53 -- Handler for incompetent users
54 if not f then return {battery_state["unknown"], 0, "N/A"} end
55 local infofile = f:read("*all")
56 f:close()
57
58 -- Check if the battery is present
59 if infofile == nil or string.find(infofile, "present:[%s]+no") then
60 return {battery_state["unknown"], 0, "N/A"}
61 end
62
63 -- Get capacity information
64 local capacity = string.match(infofile, "last full capacity:[%s]+([%d]+).*")
65
66
67 -- Get /proc/acpi/battery state
68 local f = io.open("/proc/acpi/battery/"..batid.."/state")
69 local statefile = f:read("*all")
70 f:close()
71
72 -- Get state information
73 local state = string.match(statefile, "charging state:[%s]+([%a]+).*")
74 local state = battery_state[state] or battery_state["unknown"]
75
76 -- Get charge information
77 local rate = string.match(statefile, "present rate:[%s]+([%d]+).*")
78 local remaining = string.match(statefile, "remaining capacity:[%s]+([%d]+).*")
79
80
81 -- Calculate percentage (but work around broken BAT/ACPI implementations)
82 local percent = math.min(math.floor(remaining / capacity * 100), 100)
83
84 -- Calculate remaining (charging or discharging) time
85 if state == "+" then
86 timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
87 elseif state == "-" then
88 timeleft = tonumber(remaining) / tonumber(rate)
89 else
90 return {state, percent, "N/A"}
91 end
92 local hoursleft = math.floor(timeleft)
93 local minutesleft = math.floor((timeleft - hoursleft) * 60 )
94 local time = string.format("%02d:%02d", hoursleft, minutesleft)
95
96 return {state, percent, time}
97 end
98 -- }}}
99
100 return setmetatable(batproc_linux, { __call = function(_, ...) return worker(...) end })