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

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