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.
3 Battery status widget for ThinkPad laptops that use SMAPI
4 lain.widgets.contrib.tpbat
6 More on tp_smapi: http://www.thinkwiki.org/wiki/Tp_smapi
8 Licensed under GNU General Public License v2
9 * (c) 2013, Conor Heine
10 * (c) 2013, Luke Bonham
11 * (c) 2010-2012, Peter Hofmann
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 :(
22 local string = { format = string.format }
23 local math = { floor = math.floor }
24 local tostring = tostring
25 local setmetatable = setmetatable
27 -- ThinkPad SMAPI-enabled battery info widget
30 local tpbat_notification = nil
33 if tpbat_notification ~= nil then
34 naughty.destroy(tpbat_notification)
35 tpbat_notification = nil
39 function tpbat:show(t_out)
43 if bat == nil or not bat:installed() then return end
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()
52 if status ~= "idle" and status ~= "nil" then
54 msg = "...Calculating time remaining..."
56 msg = time .. (status == "charging" and " until charged" or " remaining")
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)
65 tpbat_notification = naughty.notify({
66 preset = { fg = beautiful.fg_normal },
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
78 tpbat.bat = smapi:battery(battery) -- Create a new battery
81 tpbat.widget = wibox.widget.textbox('')
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",
97 status = "Not present",
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)
110 -- notifications for low and critical states
113 tpbat.id = naughty.notify({
114 text = "shutdown imminent",
115 title = "battery nearly exhausted",
116 position = "top_right",
121 replaces_id = tpbat.id
123 elseif bat_now.perc <= 15
125 tpbat.id = naughty.notify({
126 text = "plug the cable",
127 title = "battery low",
128 position = "top_right",
133 replaces_id = tpbat.id
137 bat_now.perc = tostring(bat_now.perc)
140 widget = tpbat.widget -- 'widget' needed in rc.lua (following convention)
144 newtimer("tpbat", timeout, update)
146 widget:connect_signal('mouse::enter', function () tpbat:show(0) end)
147 widget:connect_signal('mouse::leave', function () tpbat:hide() end)
152 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })