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
35 naughty.destroy(tpbat_notification)
36 tpbat_notification = nil
40 function tpbat:show(t_out)
44 if bat == nil or not bat:installed() then return end
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"
57 msg = "...Calculating time remaining..."
59 msg = time .. (status == "charging" and " until charged" or " remaining")
65 local str = string.format("%s : %s %s (%s)\n", bat.name, mfgr, model, chem)
66 str = str .. string.format("\n%s \t\t\t %s", status:upper(), msg)
68 tpbat_notification = naughty.notify({
69 preset = { fg = beautiful.fg_normal },
75 function tpbat.register(args)
76 local args = args or {}
77 local timeout = args.timeout or 30
78 local battery = args.battery or "BAT0"
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 if bat:get('state') == nil
88 local n = naughty.notify({
89 title = "SMAPI Battery Warning: Unable to read battery state!",
90 text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
91 position = "top_right",
101 status = "Not present",
109 bat_now.status = bat:status()
110 bat_now.perc = bat:percent()
111 bat_now.time = bat:remaining_time()
112 -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
114 -- notifications for low and critical states
117 tpbat.id = naughty.notify({
118 text = "shutdown imminent",
119 title = "battery nearly exhausted",
120 position = "top_right",
125 replaces_id = tpbat.id
127 elseif bat_now.perc <= 15
129 tpbat.id = naughty.notify({
130 text = "plug the cable",
131 title = "battery low",
132 position = "top_right",
137 replaces_id = tpbat.id
141 bat_now.perc = tostring(bat_now.perc)
144 widget = tpbat.widget -- 'widget' needed in rc.lua (following convention)
148 newtimer("tpbat", timeout, update)
150 widget:connect_signal('mouse::enter', function () tpbat:show(0) end)
151 widget:connect_signal('mouse::leave', function () tpbat:hide() end)
156 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })