Debian Codegraph @

vicious/widgets/bat_openbsd.lua

raw symbols SCIP
1 -- battery widget type for OpenBSD
2 -- Copyright (C) 2019 Enric Morales <[email protected]>
3 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
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 pairs = pairs
22 local tonumber = tonumber
23 local table = {
24 insert = table.insert
25 }
26
27 local math = {
28 floor = math.floor,
29 modf = math.modf
30 }
31
32 local helpers = require("vicious.helpers")
33 -- }}}
34
35 local bat_openbsd = {}
36 function bat_openbsd.async(format, warg, callback)
37 local battery_id = warg or "bat0"
38
39 local fields = {
40 charging_rate = ("hw.sensors.acpi%s.power0"):format(battery_id),
41 last_full_capacity = ("hw.sensors.acpi%s.watthour0"):format(battery_id),
42 remaining_capacity = ("hw.sensors.acpi%s.watthour3"):format(battery_id),
43 design_capacity = ("hw.sensors.acpi%s.watthour4"):format(battery_id),
44 state = ("hw.sensors.acpi%s.raw0"):format(battery_id)
45 }
46
47 local sysctl_args = {}
48 for _, v in pairs(fields) do table.insert(sysctl_args, v) end
49
50 local battery = {}
51 helpers.sysctl_async(sysctl_args, function (ret)
52 for k, v in pairs(fields) do
53 -- discard the description that comes after the values
54 battery[k] = tonumber(ret[v]:match("(.-) "))
55 end
56
57 local states = {
58 [0] = "↯", -- not charging
59 [1] = "-", -- discharging
60 [2] = "!", -- critical
61 [3] = "+", -- charging
62 [4] = "N/A", -- unknown status
63 [255] = "N/A" -- unimplemented by the driver
64 }
65 local state = states[battery.state]
66
67 local charge = tonumber(battery.remaining_capacity
68 / battery.last_full_capacity * 100)
69
70 local remaining_time
71 if battery.charging_rate < 1 then
72 remaining_time = "∞"
73 else
74 local raw_time = battery.remaining_capacity / battery.rate
75 local hours, hour_fraction = math.modf(raw_time)
76 local minutes = math.floor(60 * hour_fraction)
77 remaining_time = ("%d:%0.2d"):format(hours, minutes)
78 end
79
80 local wear = math.floor(battery.last_full_capacity,
81 battery.design_capacity)
82
83 -- Pass the following arguments to callback function:
84 -- * battery state symbol (↯, -, !, + or N/A)
85 -- * remaining capacity (in percent)
86 -- * remaining time, as reported by the battery
87 -- * wear level (in percent)
88 -- * present_rate (in Watts/hour)
89 return callback({ state, charge, remaining_time,
90 wear, battery.charging_rate })
91 end)
92 end
93
94 return helpers.setasyncall(bat_openbsd)