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

added basic tp_smapi battery widget for thinkpads with hover-expandable status
[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 then
34         naughty.destroy(tpbat_notification)
35         tpbat_notification = nil
36     end
37 end
38
39 function tpbat:show(t_out)
40     tpbat:hide()
41     
42     local bat = self.bat
43     if bat == nil or not bat:installed() then return end
44
45     local mfgr   = bat:get('manufacturer') or "no_mfgr"
46     local model  = bat:get('model') or "no_model"
47     local chem   = bat:get('chemistry') or "no_chem"
48     local status = bat:get('state') or "nil"
49     local time   = bat:remaining_time()
50     local msg    = "\t"
51
52     if status ~= "idle" and status ~= "nil" then
53         if time == "N/A" then
54             msg = "...Calculating time remaining..."
55         else
56             msg = time .. (status == "charging" and " until charged" or " remaining")
57         end
58     else
59         msg = "On AC Power"
60     end
61
62     local str = string.format("%s : %s %s (%s)\n", bat.name, mfgr, model, chem)
63     str = str .. string.format("\n%s \t\t\t %s", status:upper(), msg)
64
65     tpbat_notification = naughty.notify({
66         preset = { fg = beautiful.fg_normal },
67         text = str,
68         timeout = t_out
69     })
70 end
71
72 function tpbat.register(args)
73     local args = args or {}
74     local timeout = args.timeout or 30
75     local battery = args.battery or "BAT0"
76     local settings = args.settings or function() end
77
78     tpbat.bat = smapi:battery(battery) -- Create a new battery
79     local bat = tpbat.bat
80     
81     tpbat.widget = wibox.widget.textbox('')
82
83         if bat:get('state') == nil then
84                 local n = naughty.notify({
85                         title = "SMAPI Battery Warning: Unable to read battery state!",
86                         text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
87                         position = "top_right",
88                         timeout = 15,
89                         fg="#202020",
90                         bg="#cdcdcd",
91                         ontop = true
92                 })
93         end
94
95     function update()
96         bat_now = {
97             status = "Not present",
98             perc   = "N/A",
99             time   = "N/A",
100             watt   = "N/A"
101         }
102
103         if bat:installed()
104         then
105             bat_now.status = bat:status()
106             bat_now.perc   = bat:percent()
107             bat_now.time   = bat:remaining_time()
108             -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
109
110             -- notifications for low and critical states
111             if bat_now.perc <= 5
112             then
113                 tpbat.id = naughty.notify({
114                     text = "shutdown imminent",
115                     title = "battery nearly exhausted",
116                     position = "top_right",
117                     timeout = 15,
118                     fg="#000000",
119                     bg="#ffffff",
120                     ontop = true,
121                     replaces_id = tpbat.id
122                 }).id
123             elseif bat_now.perc <= 15
124             then
125                 tpbat.id = naughty.notify({
126                     text = "plug the cable",
127                     title = "battery low",
128                     position = "top_right",
129                     timeout = 15,
130                     fg="#202020",
131                     bg="#cdcdcd",
132                     ontop = true,
133                     replaces_id = tpbat.id
134                 }).id
135             end
136
137             bat_now.perc = tostring(bat_now.perc)
138         end
139
140         widget = tpbat.widget -- 'widget' needed in rc.lua (following convention)
141         settings()
142     end
143
144     newtimer("tpbat", timeout, update)
145
146     widget:connect_signal('mouse::enter', function () tpbat:show(0) end)
147     widget:connect_signal('mouse::leave', function () tpbat:hide() end)
148
149     return tpbat.widget
150 end
151
152 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })