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
11 * (c) 2013, Luke Bonham
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 settings = args.settings or function() end
79 tpbat.bat = smapi:battery(battery) -- Create a new battery
82 tpbat.widget = wibox.widget.textbox()
84 bat_notification_low_preset = {
85 title = "Battery low",
86 text = "Plug the cable!",
92 bat_notification_critical_preset = {
93 title = "Battery exhausted",
94 text = "Shutdown imminent",
100 if bat:get('state') == nil
102 local n = naughty.notify({
103 preset = bat_notification_low_preset,
104 title = "SMAPI Battery Warning: Unable to read battery state!",
105 text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
106 screen = client.focus and client.focus.screen or 1
110 function tpbat.update()
112 status = "Not present",
120 bat_now.status = bat:status() or "N/A"
121 bat_now.perc = bat:percent()
122 bat_now.time = bat:remaining_time()
123 -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
125 -- notifications for low and critical states (when discharging)
126 if bat_now.status == "discharging"
130 tpbat.id = naughty.notify({
131 preset = bat_notification_critical_preset,
132 replaces_id = tpbat.id,
133 screen = client.focus and client.focus.screen or 1
135 elseif bat_now.perc <= 15
137 tpbat.id = naughty.notify({
138 preset = bat_notification_low_preset,
139 replaces_id = tpbat.id,
140 screen = client.focus and client.focus.screen or 1
145 bat_now.perc = tostring(bat_now.perc)
148 widget = tpbat.widget
153 newtimer("tpbat-" .. bat.name, timeout, tpbat.update)
155 widget:connect_signal('mouse::enter', function () tpbat.show() end)
156 widget:connect_signal('mouse::leave', function () tpbat.hide() end)
161 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })