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

widget.contrib.kbdlayout removed, please use awful.widget.keyboardlayout; closes...
[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 bat_low_perc = args.bat_low_perc or 15
78     local bat_critical_perc = args.bat_critical_perc or 5
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     bat_notification_low_preset = {
87         title = "Battery low",
88         text = "Plug the cable!",
89         timeout = 15,
90         fg = "#202020",
91         bg = "#CDCDCD"
92     }
93
94     bat_notification_critical_preset = {
95         title = "Battery exhausted",
96         text = "Shutdown imminent",
97         timeout = 15,
98         fg = "#000000",
99         bg = "#FFFFFF"
100     }
101
102     if bat:get('state') == nil
103     then
104         local n = naughty.notify({
105             preset = bat_notification_low_preset,
106             title = "SMAPI Battery Warning: Unable to read battery state!",
107             text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
108             screen = client.focus and client.focus.screen or 1
109         })
110     end
111
112     function tpbat.update()
113         bat_now = {
114             status = "Not present",
115             perc   = "N/A",
116             time   = "N/A",
117             watt   = "N/A"
118         }
119
120         if bat:installed()
121         then
122             bat_now.status = bat:status() or "N/A"
123             bat_now.perc   = bat:percent()
124             bat_now.time   = bat:remaining_time()
125             -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
126
127             -- notifications for low and critical states (when discharging)
128             if bat_now.status == "discharging"
129             then
130                 if bat_now.perc <= bat_critical_perc
131                 then
132                     tpbat.id = naughty.notify({
133                         preset = bat_notification_critical_preset,
134                         replaces_id = tpbat.id,
135                         screen = client.focus and client.focus.screen or 1
136                     }).id
137                 elseif bat_now.perc <= bat_low_perc
138                 then
139                     tpbat.id = naughty.notify({
140                         preset = bat_notification_low_preset,
141                         replaces_id = tpbat.id,
142                         screen = client.focus and client.focus.screen or 1
143                     }).id
144                 end
145             end
146
147             bat_now.perc = tostring(bat_now.perc)
148         end
149
150         widget = tpbat.widget
151
152         settings()
153     end
154
155     newtimer("tpbat-" .. bat.name, timeout, tpbat.update)
156
157     widget:connect_signal('mouse::enter', function () tpbat.show() end)
158     widget:connect_signal('mouse::leave', function () tpbat.hide() end)
159
160     return tpbat
161 end
162
163 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })