]> git.madduck.net Git - etc/awesome.git/blob - widgets/bat.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

wiki updated
[etc/awesome.git] / widgets / bat.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11 local naughty      = require("naughty")
12 local wibox        = require("wibox")
13
14 local math         = { floor  = math.floor }
15 local string       = { format = string.format }
16 local tonumber     = tonumber
17
18 local setmetatable = setmetatable
19
20 -- Battery infos
21 -- lain.widgets.bat
22
23 local function worker(args)
24     local bat = {}
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
30
31     bat.widget = wibox.widget.textbox('')
32
33     bat_notification_low_preset = {
34         title = "Battery low",
35         text = "Plug the cable!",
36         timeout = 15,
37         fg = "#202020",
38         bg = "#CDCDCD"
39     }
40
41     bat_notification_critical_preset = {
42         title = "Battery exhausted",
43         text = "Shutdown imminent",
44         timeout = 15,
45         fg = "#000000",
46         bg = "#FFFFFF"
47     }
48
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")
53
54     function update()
55         bat_now = {
56             status = "Not present",
57             perc   = "N/A",
58             time   = "N/A",
59             watt   = "N/A"
60         }
61
62         local bstr  = "/sys/class/power_supply/" .. battery
63
64         local present = helpers.first_line(bstr .. "/present")
65
66         if present == "1"
67         then
68             local rate  = helpers.first_line(bstr .. "/power_now") or
69                           helpers.first_line(bstr .. "/current_now")
70
71             local ratev = helpers.first_line(bstr .. "/voltage_now")
72
73             local rem   = helpers.first_line(bstr .. "/energy_now") or
74                           helpers.first_line(bstr .. "/charge_now")
75
76             local tot   = helpers.first_line(bstr .. "/energy_full") or
77                           helpers.first_line(bstr .. "/charge_full")
78
79             bat_now.status = helpers.first_line(bstr .. "/status") or "N/A"
80
81             rate  = tonumber(rate) or 1
82             ratev = tonumber(ratev)
83             rem   = tonumber(rem)
84             tot   = tonumber(tot)
85
86             local time_rat = 0
87             if bat_now.status == "Charging"
88             then
89                 time_rat = (tot - rem) / rate
90             elseif bat_now.status == "Discharging"
91             then
92                 time_rat = rem / rate
93             end
94
95             local hrs = math.floor(time_rat)
96             if hrs < 0 then hrs = 0 elseif hrs > 23 then hrs = 23 end
97
98             local min = math.floor((time_rat - hrs) * 60)
99             if min < 0 then min = 0 elseif min > 59 then min = 59 end
100
101             bat_now.time = string.format("%02d:%02d", hrs, min)
102
103             bat_now.perc = helpers.first_line(bstr .. "/capacity")
104
105             if not bat_now.perc then
106                 local perc = (rem / tot) * 100
107                 if perc <= 100 then
108                     bat_now.perc = string.format("%d", perc)
109                 elseif perc > 100 then
110                     bat_now.perc = "100"
111                 elseif perc < 0 then
112                     bat_now.perc = "0"
113                 end
114             end
115
116             if rate ~= nil and ratev ~= nil then
117                 bat_now.watt = string.format("%.2fW", (rate * ratev) / 1e12)
118             else
119                 bat_now.watt = "N/A"
120             end
121         end
122
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")
127         then
128             widget = bat.widget
129             settings()
130
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)
135         end
136
137         -- notifications for low and critical states
138         if bat_now.status == "Discharging" and notify == "on" and bat_now.perc ~= nil
139         then
140             local nperc = tonumber(bat_now.perc) or 100
141             if nperc <= 5
142             then
143                 bat.id = naughty.notify({
144                     preset = bat_notification_critical_preset,
145                     replaces_id = bat.id,
146                 }).id
147             elseif nperc <= 15
148             then
149                 bat.id = naughty.notify({
150                     preset = bat_notification_low_preset,
151                     replaces_id = bat.id,
152                 }).id
153             end
154         end
155     end
156
157     helpers.newtimer(battery, timeout, update)
158
159     return bat.widget
160 end
161
162 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })