]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

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