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.
4 Battery status widget for ThinkPad laptops that use SMAPI
5 lain.widget.contrib.tpbat
7 More on tp_smapi: http://www.thinkwiki.org/wiki/Tp_smapi
9 Licensed under GNU General Public License v2
10 * (c) 2013, Conor Heine
12 * (c) 2010-2012, Peter Hofmann
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")
28 -- ThinkPad SMAPI-enabled battery info widget
29 -- lain.widget.contrib.tpbat
33 if not tpbat.notification then return end
34 naughty.destroy(tpbat.notification)
35 tpbat.notification = nil
38 function tpbat.show(t_out)
43 if bat == nil or not bat:installed() then return end
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()
53 if status ~= "idle" and status ~= "nil" then
55 msg = "...Calculating time remaining..."
57 msg = time .. (status == "charging" and " until charged" or " remaining")
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)
66 tpbat.notification = naughty.notify({
69 screen = client.focus and client.focus.screen or 1
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
81 tpbat.bat = smapi:battery(battery) -- Create a new battery
84 tpbat.widget = wibox.widget.textbox()
86 bat_notification_low_preset = {
87 title = "Battery low",
88 text = "Plug the cable!",
94 bat_notification_critical_preset = {
95 title = "Battery exhausted",
96 text = "Shutdown imminent",
102 if bat:get('state') == nil
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
112 function tpbat.update()
114 status = "Not present",
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)
127 -- notifications for low and critical states (when discharging)
128 if bat_now.status == "discharging"
130 if bat_now.perc <= bat_critical_perc
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
137 elseif bat_now.perc <= bat_low_perc
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
147 bat_now.perc = tostring(bat_now.perc)
150 widget = tpbat.widget
155 newtimer("tpbat-" .. bat.name, timeout, tpbat.update)
157 widget:connect_signal('mouse::enter', function () tpbat.show() end)
158 widget:connect_signal('mouse::leave', function () tpbat.hide() end)
163 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })