]> git.madduck.net Git - etc/awesome.git/blob - widget/contrib/tpbat/init.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:

e9675ef42d530b236f23148884b54bc75ac18bb4
[etc/awesome.git] / widget / contrib / tpbat / init.lua
1 --[[
2
3      tpbat.lua
4      Battery status widget for ThinkPad laptops that use SMAPI
5      lain.widget.contrib.tpbat
6
7      More on tp_smapi: http://www.thinkwiki.org/wiki/Tp_smapi
8
9      Licensed under GNU General Public License v2
10       * (c) 2013,      Conor Heine
11       * (c) 2013,      Luca CPZ
12       * (c) 2010-2012, Peter Hofmann
13
14 --]]
15
16 local debug        = { getinfo = debug.getinfo }
17 local newtimer     = require("lain.helpers").newtimer
18 local first_line   = require("lain.helpers").first_line
19 local naughty      = require("naughty")
20 local wibox        = require("wibox")
21 local string       = { format = string.format }
22 local math         = { floor = math.floor }
23 local tostring     = tostring
24 local setmetatable = setmetatable
25 package.path       = debug.getinfo(1,"S").source:match[[^@?(.*[\/])[^\/]-$]] .. "?.lua;" .. package.path
26 local smapi        = require("smapi")
27
28 -- ThinkPad SMAPI-enabled battery info widget
29 -- lain.widget.contrib.tpbat
30 local tpbat = {}
31
32 function tpbat.hide()
33     if not tpbat.notification then return end
34     naughty.destroy(tpbat.notification)
35     tpbat.notification = nil
36 end
37
38 function tpbat.show(t_out)
39     tpbat.hide()
40
41     local bat = tpbat.bat
42
43     if bat == nil or not bat:installed() then return end
44
45     local t_out = t_out or 0
46     local mfgr   = bat:get('manufacturer') or "no_mfgr"
47     local model  = bat:get('model') or "no_model"
48     local chem   = bat:get('chemistry') or "no_chem"
49     local status = bat:get('state') or "nil"
50     local time   = bat:remaining_time()
51     local msg    = "\t"
52
53     if status ~= "idle" and status ~= "nil" then
54         if time == "N/A" then
55             msg = "...Calculating time remaining..."
56         else
57             msg = time .. (status == "charging" and " until charged" or " remaining")
58         end
59     else
60         msg = "On AC Power"
61     end
62
63     local str = string.format("%s : %s %s (%s)\n", bat.name, mfgr, model, chem)
64                 .. string.format("\n%s \t\t\t %s", status:upper(), msg)
65
66     tpbat.notification = naughty.notify({
67         text    = str,
68         timeout = t_out,
69         screen  = client.focus and client.focus.screen or 1
70     })
71 end
72
73 function tpbat.register(args)
74     local args = args or {}
75     local timeout = args.timeout or 30
76     local battery = args.battery or "BAT0"
77     local settings = args.settings or function() end
78
79     tpbat.bat = smapi:battery(battery) -- Create a new battery
80     local bat = tpbat.bat
81
82     tpbat.widget = wibox.widget.textbox()
83
84     bat_notification_low_preset = {
85         title = "Battery low",
86         text = "Plug the cable!",
87         timeout = 15,
88         fg = "#202020",
89         bg = "#CDCDCD"
90     }
91
92     bat_notification_critical_preset = {
93         title = "Battery exhausted",
94         text = "Shutdown imminent",
95         timeout = 15,
96         fg = "#000000",
97         bg = "#FFFFFF"
98     }
99
100     if bat:get('state') == nil
101     then
102         local n = naughty.notify({
103             preset = bat_notification_low_preset,
104             title = "SMAPI Battery Warning: Unable to read battery state!",
105             text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
106             screen = client.focus and client.focus.screen or 1
107         })
108     end
109
110     function tpbat.update()
111         bat_now = {
112             status = "Not present",
113             perc   = "N/A",
114             time   = "N/A",
115             watt   = "N/A"
116         }
117
118         if bat:installed()
119         then
120             bat_now.status = bat:status() or "N/A"
121             bat_now.perc   = bat:percent()
122             bat_now.time   = bat:remaining_time()
123             -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
124
125             -- notifications for low and critical states (when discharging)
126             if bat_now.status == "discharging"
127             then
128                 if bat_now.perc <= 5
129                 then
130                     tpbat.id = naughty.notify({
131                         preset = bat_notification_critical_preset,
132                         replaces_id = tpbat.id,
133                         screen = client.focus and client.focus.screen or 1
134                     }).id
135                 elseif bat_now.perc <= 15
136                 then
137                     tpbat.id = naughty.notify({
138                         preset = bat_notification_low_preset,
139                         replaces_id = tpbat.id,
140                         screen = client.focus and client.focus.screen or 1
141                     }).id
142                 end
143             end
144
145             bat_now.perc = tostring(bat_now.perc)
146         end
147
148         widget = tpbat.widget
149
150         settings()
151     end
152
153     newtimer("tpbat-" .. bat.name, timeout, tpbat.update)
154
155     widget:connect_signal('mouse::enter', function () tpbat.show() end)
156     widget:connect_signal('mouse::leave', function () tpbat.hide() end)
157
158     return tpbat
159 end
160
161 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })