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.
5 Battery status widget for ThinkPad laptops that use SMAPI
6 lain.widgets.contrib.tpbat
8 More on tp_smapi: http://www.thinkwiki.org/wiki/Tp_smapi
10 Licensed under GNU General Public License v2
11 * (c) 2013, Conor Heine
12 * (c) 2013, Luke Bonham
13 * (c) 2010-2012, Peter Hofmann
17 local debug = { getinfo = debug.getinfo }
18 local newtimer = require("lain.helpers").newtimer
19 local first_line = require("lain.helpers").first_line
20 local naughty = require("naughty")
21 local wibox = require("wibox")
22 local string = { format = string.format }
23 local math = { floor = math.floor }
24 local tostring = tostring
25 local setmetatable = setmetatable
26 package.path = debug.getinfo(1,"S").source:match[[^@?(.*[\/])[^\/]-$]] .. "?.lua;" .. package.path
27 local smapi = require("smapi")
29 -- ThinkPad SMAPI-enabled battery info widget
30 -- lain.widgets.contrib.tpbat
34 if not tpbat.notification then return end
35 naughty.destroy(tpbat.notification)
36 tpbat.notification = nil
39 function tpbat.show(t_out)
44 if bat == nil or not bat:installed() then return end
46 local t_out = t_out or 0
47 local mfgr = bat:get('manufacturer') or "no_mfgr"
48 local model = bat:get('model') or "no_model"
49 local chem = bat:get('chemistry') or "no_chem"
50 local status = bat:get('state') or "nil"
51 local time = bat:remaining_time()
54 if status ~= "idle" and status ~= "nil" then
56 msg = "...Calculating time remaining..."
58 msg = time .. (status == "charging" and " until charged" or " remaining")
64 local str = string.format("%s : %s %s (%s)\n", bat.name, mfgr, model, chem)
65 .. string.format("\n%s \t\t\t %s", status:upper(), msg)
67 tpbat.notification = naughty.notify({
70 screen = client.focus and client.focus.screen or 1
74 function tpbat.register(args)
75 local args = args or {}
76 local timeout = args.timeout or 30
77 local battery = args.battery or "BAT0"
78 local settings = args.settings or function() end
80 tpbat.bat = smapi:battery(battery) -- Create a new battery
83 tpbat.widget = wibox.widget.textbox()
85 bat_notification_low_preset = {
86 title = "Battery low",
87 text = "Plug the cable!",
93 bat_notification_critical_preset = {
94 title = "Battery exhausted",
95 text = "Shutdown imminent",
101 if bat:get('state') == nil
103 local n = naughty.notify({
104 preset = bat_notification_low_preset,
105 title = "SMAPI Battery Warning: Unable to read battery state!",
106 text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
107 screen = client.focus and client.focus.screen or 1
113 status = "Not present",
121 bat_now.status = bat:status() or "N/A"
122 bat_now.perc = bat:percent()
123 bat_now.time = bat:remaining_time()
124 -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
126 -- notifications for low and critical states (when discharging)
127 if bat_now.status == "discharging"
131 tpbat.id = naughty.notify({
132 preset = bat_notification_critical_preset,
133 replaces_id = tpbat.id,
134 screen = client.focus and client.focus.screen or 1
136 elseif bat_now.perc <= 15
138 tpbat.id = naughty.notify({
139 preset = bat_notification_low_preset,
140 replaces_id = tpbat.id,
141 screen = client.focus and client.focus.screen or 1
146 bat_now.perc = tostring(bat_now.perc)
149 widget = tpbat.widget
154 newtimer("tpbat-" .. bat.name, timeout, update)
156 widget:connect_signal('mouse::enter', function () tpbat.show() end)
157 widget:connect_signal('mouse::leave', function () tpbat.hide() end)
162 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })