]> 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:

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