All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
6 * (c) 2010-2012, Peter Hofmann
10 local helpers = require("lain.helpers")
11 local naughty = require("naughty")
12 local wibox = require("wibox")
14 local math = { floor = math.floor }
15 local string = { format = string.format }
16 local tonumber = tonumber
18 local setmetatable = setmetatable
23 local function worker(args)
25 local args = args or {}
26 local timeout = args.timeout or 30
27 local battery = args.battery or "BAT0"
28 local notify = args.notify or "on"
29 local settings = args.settings or function() end
31 bat.widget = wibox.widget.textbox('')
33 bat_notification_low_preset = {
34 title = "Battery low",
35 text = "Plug the cable!",
41 bat_notification_critical_preset = {
42 title = "Battery exhausted",
43 text = "Shutdown imminent",
49 helpers.set_map(battery .. "status", "N/A")
50 helpers.set_map(battery .. "perc", "N/A")
51 helpers.set_map(battery .. "time", "N/A")
52 helpers.set_map(battery .. "watt", "N/A")
56 status = "Not present",
62 local bstr = "/sys/class/power_supply/" .. battery
64 local present = helpers.first_line(bstr .. "/present")
68 local rate = helpers.first_line(bstr .. "/power_now") or
69 helpers.first_line(bstr .. "/current_now")
71 local ratev = helpers.first_line(bstr .. "/voltage_now")
73 local rem = helpers.first_line(bstr .. "/energy_now") or
74 helpers.first_line(bstr .. "/charge_now")
76 local tot = helpers.first_line(bstr .. "/energy_full") or
77 helpers.first_line(bstr .. "/charge_full")
79 bat_now.status = helpers.first_line(bstr .. "/status") or "N/A"
81 rate = tonumber(rate) or 1
82 ratev = tonumber(ratev)
87 if bat_now.status == "Charging"
89 time_rat = (tot - rem) / rate
90 elseif bat_now.status == "Discharging"
95 local hrs = math.floor(time_rat)
96 if hrs < 0 then hrs = 0 elseif hrs > 23 then hrs = 23 end
98 local min = math.floor((time_rat - hrs) * 60)
99 if min < 0 then min = 0 elseif min > 59 then min = 59 end
101 bat_now.time = string.format("%02d:%02d", hrs, min)
103 bat_now.perc = helpers.first_line(bstr .. "/capacity")
105 if not bat_now.perc then
106 local perc = (rem / tot) * 100
108 bat_now.perc = string.format("%d", perc)
109 elseif perc > 100 then
116 if rate ~= nil and ratev ~= nil then
117 bat_now.watt = string.format("%.2fW", (rate * ratev) / 1e12)
123 if bat_now.status ~= helpers.get_map(battery .. "status")
124 or bat_now.perc ~= helpers.get_map(battery .. "perc")
125 or bat_now.time ~= helpers.get_map(battery .. "time")
126 or bat_now.watt ~= helpers.get_map(battery .. "watt")
131 helpers.set_map(battery .. "status", bat_now.status)
132 helpers.set_map(battery .. "perc", bat_now.perc)
133 helpers.set_map(battery .. "time", bat_now.time)
134 helpers.set_map(battery .. "watt", bat_now.watt)
137 -- notifications for low and critical states
138 if bat_now.status == "Discharging" and notify == "on" and bat_now.perc ~= nil
140 local nperc = tonumber(bat_now.perc) or 100
143 bat.id = naughty.notify({
144 preset = bat_notification_critical_preset,
145 replaces_id = bat.id,
149 bat.id = naughty.notify({
150 preset = bat_notification_low_preset,
151 replaces_id = bat.id,
157 helpers.newtimer(battery, timeout, update)
162 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })