]> git.madduck.net Git - etc/awesome.git/blob - .config/awesome/lain/widget/contrib/tp_smapi.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Flameshot shortcut
[etc/awesome.git] / .config / awesome / lain / widget / contrib / tp_smapi.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2018, Luca CPZ
5       * (c) 2013, Conor Heine
6
7 --]]
8
9 local helpers = require("lain.helpers")
10 local focused = require("awful.screen").focused
11 local gears   = require("gears")
12 local naughty = require("naughty")
13 local wibox   = require("wibox")
14 local string  = string
15 local type    = type
16
17 -- ThinkPad battery infos and widget creator
18 -- http://www.thinkwiki.org/wiki/Tp_smapi
19 -- lain.widget.contrib.tp_smapi
20
21 local function factory(apipath)
22     local tp_smapi = {
23         path = apipath or "/sys/devices/platform/smapi"
24     }
25
26     function tp_smapi.get(batid, feature)
27         return helpers.first_line(string.format("%s/%s/%s", tp_smapi.path, batid or "BAT0", feature or ""))
28     end
29
30     function tp_smapi.installed(batid)
31         return tp_smapi.get(batid, "installed") == "1"
32     end
33
34     function tp_smapi.status(batid)
35         return tp_smapi.get(batid, "state")
36     end
37
38     function tp_smapi.percentage(batid)
39         return tp_smapi.get(batid, "remaining_percent")
40     end
41
42     -- either running or charging time
43     function tp_smapi.time(batid)
44         local status = tp_smapi.status(batid)
45         local mins_left = tp_smapi.get(batid, string.match(string.lower(status), "discharging") and "remaining_running_time" or "remaining_charging_time")
46         if not string.find(mins_left, "^%d+") then return "N/A" end
47         return string.format("%02d:%02d", math.floor(mins_left / 60), mins_left % 60) -- HH:mm
48     end
49
50     function tp_smapi.hide()
51         if not tp_smapi.notification then return end
52         naughty.destroy(tp_smapi.notification)
53         tp_smapi.notification = nil
54     end
55
56     function tp_smapi.show(batid, seconds, scr)
57         if not tp_smapi.installed(batid) then return end
58
59         local mfgr   = tp_smapi.get(batid, "manufacturer") or "no_mfgr"
60         local model  = tp_smapi.get(batid, "model") or "no_model"
61         local chem   = tp_smapi.get(batid, "chemistry") or "no_chem"
62         local status = tp_smapi.get(batid, "state")
63         local time   = tp_smapi.time(batid)
64         local msg    = ""
65
66         if status and status ~= "idle" then
67             msg = string.format("[%s] %s %s", status, time ~= "N/A" and time or "unknown remaining time",
68                   string.lower(status):gsub(" ", ""):gsub("\n", "") == "charging" and " until charged" or " remaining")
69         else
70             msg = "On AC power"
71         end
72
73         tp_smapi.hide()
74         tp_smapi.notification = naughty.notify {
75             title   = string.format("%s: %s %s (%s)", batid, mfgr, model, chem),
76             text    = msg,
77             timeout = type(seconds) == "number" and seconds or 0,
78             screen  = scr or focused()
79         }
80     end
81
82     function tp_smapi.create_widget(args)
83         local args      = args or {}
84         local pspath    = args.pspath or "/sys/class/power_supply/"
85         local batteries = args.batteries or (args.battery and {args.battery}) or {}
86         local timeout   = args.timeout or 30
87         local settings  = args.settings or function() end
88
89         if #batteries == 0 then
90             helpers.line_callback("ls -1 " .. pspath, function(line)
91                 local bstr = string.match(line, "BAT%w+")
92                 if bstr then batteries[#batteries + 1] = bstr end
93             end)
94         end
95
96         local all_batteries_installed = true
97
98         for i, battery in ipairs(batteries) do
99             if not tp_smapi.installed(battery) then
100                 naughty.notify {
101                     preset = naughty.config.critical,
102                     title  = "tp_smapi: error while creating widget",
103                     text   = string.format("battery %s is not installed", battery)
104                 }
105                 all_batteries_installed = false
106                 break
107             end
108         end
109
110         if not all_batteries_installed then return end
111
112         tpbat = {
113             batteries = batteries,
114             widget    = args.widget or wibox.widget.textbox()
115         }
116
117         function tpbat.update()
118             tpbat_now = {
119                 n_status = {},
120                 n_perc   = {},
121                 n_time   = {},
122                 status   = "N/A"
123             }
124
125             for i = 1, #batteries do
126                 tpbat_now.n_status[i] = tp_smapi.status(batteries[i]) or "N/A"
127                 tpbat_now.n_perc[i] = tp_smapi.percentage(batteries[i])
128                 tpbat_now.n_time[i] = tp_smapi.time(batteries[i]) or "N/A"
129
130                 if not tpbat_now.n_status[i]:lower():match("full") then
131                     tpbat_now.status = tpbat_now.n_status[i]
132                 end
133             end
134
135             widget = tpbat.widget -- backwards compatibility
136             settings()
137         end
138
139         helpers.newtimer("thinkpad-batteries", timeout, tpbat.update)
140
141         return tpbat
142     end
143
144     return tp_smapi
145 end
146
147 return factory