| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local assert = assert |
| 7 |
local iopopen = io.popen |
| 8 |
local setmetatable = setmetatable |
| 9 |
local tonumber = tonumber |
| 10 |
local tostring = tostring |
| 11 |
local floor = math.floor |
| 12 |
local sformat = string.format |
| 13 |
local smatch = string.match |
| 14 |
local unpack = unpack or table.unpack |
| 15 |
|
| 16 |
local backend = {} |
| 17 |
|
| 18 |
local function popen(cmd) |
| 19 |
return iopopen('LC_ALL=C LANG=C ' .. cmd .. ' 2>/dev/null') |
| 20 |
end |
| 21 |
|
| 22 |
function backend:clone(clone) |
| 23 |
return setmetatable(clone or {}, { __index = self }) |
| 24 |
end |
| 25 |
|
| 26 |
function backend:configure() |
| 27 |
end |
| 28 |
|
| 29 |
function backend:state() |
| 30 |
end |
| 31 |
|
| 32 |
function backend:details() |
| 33 |
local fd, err = self:details_pipe() |
| 34 |
|
| 35 |
if not fd then |
| 36 |
return nil, err |
| 37 |
end |
| 38 |
|
| 39 |
local d = fd:read('*all'):gsub('\n+$', '') |
| 40 |
fd:close() |
| 41 |
return d |
| 42 |
end |
| 43 |
|
| 44 |
local upower_backend = backend:clone { name = 'upower' } |
| 45 |
local acpiconf_backend = backend:clone { name = 'acpiconf' } |
| 46 |
local acpi_backend = backend:clone { name = 'acpi' } |
| 47 |
local acpitool_backend = acpi_backend:clone { name = 'acpitool' } |
| 48 |
local apm_backend = backend:clone { name = 'apm' } |
| 49 |
local apm_openbsd_backend = backend:clone { name = 'apm-openbsd' } |
| 50 |
local null_backend = backend:clone { name = 'null' } |
| 51 |
|
| 52 |
local backends = { |
| 53 |
upower_backend, |
| 54 |
acpiconf_backend, |
| 55 |
acpi_backend, |
| 56 |
acpitool_backend, |
| 57 |
apm_backend, |
| 58 |
apm_openbsd_backend, |
| 59 |
null_backend, |
| 60 |
} |
| 61 |
|
| 62 |
local function default_configure(orig_backend) |
| 63 |
local backend = orig_backend:clone() |
| 64 |
local rv = backend:state() |
| 65 |
if next(rv) == nil then |
| 66 |
return nil |
| 67 |
end |
| 68 |
return backend |
| 69 |
end |
| 70 |
|
| 71 |
local function exists(name) |
| 72 |
local pipe = popen('which ' .. name) |
| 73 |
|
| 74 |
if not pipe then |
| 75 |
return false |
| 76 |
end |
| 77 |
|
| 78 |
local line = pipe:read '*l' |
| 79 |
pipe:close() |
| 80 |
|
| 81 |
return line and line ~= '' |
| 82 |
end |
| 83 |
|
| 84 |
local function defaults_to_key(t) |
| 85 |
local function return_key__index(_, key) |
| 86 |
return key |
| 87 |
end |
| 88 |
|
| 89 |
return setmetatable(t, { __index = return_key__index }) |
| 90 |
end |
| 91 |
|
| 92 |
local function match_case(input, ...) |
| 93 |
local args = { ... } |
| 94 |
|
| 95 |
for i = 1, #args, 2 do |
| 96 |
local pattern = args[i] |
| 97 |
local action = args[i + 1] |
| 98 |
|
| 99 |
local matches = { smatch(input, pattern) } |
| 100 |
|
| 101 |
if matches[1] then |
| 102 |
action(unpack(matches)) |
| 103 |
break |
| 104 |
end |
| 105 |
end |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
local upower_status_mapping = defaults_to_key { |
| 114 |
['fully-charged'] = 'charged', |
| 115 |
} |
| 116 |
|
| 117 |
function upower_backend:configure() |
| 118 |
local fd, err = popen 'upower -e' |
| 119 |
if not fd then |
| 120 |
return nil, err |
| 121 |
end |
| 122 |
local battery_filenames = {} |
| 123 |
for line in fd:lines() do |
| 124 |
if line:match('battery_BAT') then |
| 125 |
battery_filenames[#battery_filenames + 1] = line |
| 126 |
end |
| 127 |
end |
| 128 |
fd:close() |
| 129 |
|
| 130 |
if #battery_filenames > 0 then |
| 131 |
return upower_backend:clone { filenames = battery_filenames } |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
function upower_backend:state() |
| 136 |
local results = {} |
| 137 |
|
| 138 |
for i = 1, #self.filenames do |
| 139 |
local filename = self.filenames[i] |
| 140 |
|
| 141 |
local rv = {} |
| 142 |
local fd, err = popen('upower -i ' .. filename) |
| 143 |
|
| 144 |
if not fd then |
| 145 |
return nil, err |
| 146 |
end |
| 147 |
|
| 148 |
local function handle_time(time, units) |
| 149 |
if time == 'unknown' then |
| 150 |
time = nil |
| 151 |
elseif units == 'hour' or units == 'hours' then |
| 152 |
time = floor(time * 60) |
| 153 |
elseif units == 'minute' or units == 'minutes' then |
| 154 |
time = tonumber(floor(time)) |
| 155 |
else |
| 156 |
time = 0 |
| 157 |
end |
| 158 |
|
| 159 |
rv.time = time |
| 160 |
end |
| 161 |
|
| 162 |
for line in fd:lines() do |
| 163 |
match_case(line, |
| 164 |
'^%s*percentage:%s*(%d+)', function(charge) |
| 165 |
rv.charge = tonumber(charge) |
| 166 |
end, |
| 167 |
'%s*time to empty:%s*(%S+)%s*(%w*)', handle_time, |
| 168 |
'%s*time to full:%s*(%S+)%s*(%w*)', handle_time, |
| 169 |
'state:%s*(%S+)', function(status) |
| 170 |
rv.status = upower_status_mapping[status] |
| 171 |
end) |
| 172 |
end |
| 173 |
fd:close() |
| 174 |
results[i] = rv |
| 175 |
end |
| 176 |
|
| 177 |
return unpack(results) |
| 178 |
end |
| 179 |
|
| 180 |
function upower_backend:details() |
| 181 |
local results = '' |
| 182 |
|
| 183 |
for i = 1, #self.filenames do |
| 184 |
local filename = self.filenames[i] |
| 185 |
|
| 186 |
local pipe, err = popen('upower -i ' .. filename) |
| 187 |
|
| 188 |
if not pipe then |
| 189 |
return nil, err |
| 190 |
end |
| 191 |
|
| 192 |
local output = pipe:read '*a' |
| 193 |
results = results .. '\n' .. output:gsub('^\n', '') |
| 194 |
end |
| 195 |
|
| 196 |
return results |
| 197 |
end |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
function acpiconf_backend:configure() |
| 202 |
return default_configure(acpiconf_backend) |
| 203 |
end |
| 204 |
|
| 205 |
local acpiconf_status_mapping = defaults_to_key { |
| 206 |
high = 'charged', |
| 207 |
} |
| 208 |
|
| 209 |
function acpiconf_backend:state() |
| 210 |
local rv = {} |
| 211 |
local fd, err = popen('acpiconf -i0') |
| 212 |
|
| 213 |
if not fd then |
| 214 |
return nil, err |
| 215 |
end |
| 216 |
|
| 217 |
for line in fd:lines() do |
| 218 |
match_case(line, |
| 219 |
'^Remaining capacity:%s*(%d+)', function(charge) |
| 220 |
rv.charge = tonumber(charge) |
| 221 |
end, |
| 222 |
'^Remaining time:%s*unknown', function() |
| 223 |
rv.time = nil |
| 224 |
end, |
| 225 |
'^Remaining time:%s*(%d+):(%d+)', function(hours, minutes) |
| 226 |
rv.time = tonumber(hours) * 60 + tonumber(minutes) |
| 227 |
end, |
| 228 |
'^State:%s*(%S+)', function(status) |
| 229 |
rv.status = acpiconf_status_mapping[status] |
| 230 |
end) |
| 231 |
end |
| 232 |
fd:close() |
| 233 |
return rv |
| 234 |
end |
| 235 |
|
| 236 |
function acpiconf_backend:details() |
| 237 |
local details = '' |
| 238 |
local fd, err = popen('acpiconf -i0') |
| 239 |
|
| 240 |
if not fd then |
| 241 |
return nil, err |
| 242 |
end |
| 243 |
|
| 244 |
for line in fd:lines() do |
| 245 |
details = details .. '\n' .. line |
| 246 |
end |
| 247 |
fd:close() |
| 248 |
return details:gsub('^\n', '') |
| 249 |
end |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
function acpitool_backend:configure() |
| 254 |
return default_configure(acpitool_backend) |
| 255 |
end |
| 256 |
|
| 257 |
acpitool_backend.backend = 'acpitool' |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
function acpi_backend:configure() |
| 262 |
return default_configure(acpi_backend) |
| 263 |
end |
| 264 |
|
| 265 |
function acpi_backend:state() |
| 266 |
local rv = {} |
| 267 |
local fd, err = popen(self.backend .. ' -b') |
| 268 |
|
| 269 |
if not fd then |
| 270 |
return nil, err |
| 271 |
end |
| 272 |
|
| 273 |
for line in fd:lines() do |
| 274 |
local data = line:match('Battery #?[0-9]%s*: ([^\n]*)') |
| 275 |
|
| 276 |
if not data then |
| 277 |
break |
| 278 |
end |
| 279 |
|
| 280 |
rv.status = data:match('([%a]*),.*'):lower() |
| 281 |
rv.charge = floor(tonumber(data:match('.*, (%d+%.?%d*)%%'))) |
| 282 |
|
| 283 |
local hours, minutes = data:match('.*, (%d*):?(%d+):%d+') |
| 284 |
if hours or minutes then |
| 285 |
rv.time = (hours or 0) * 60 + minutes |
| 286 |
end |
| 287 |
|
| 288 |
if not rv.status:match('unknown') then |
| 289 |
break |
| 290 |
end |
| 291 |
end |
| 292 |
|
| 293 |
fd:close() |
| 294 |
|
| 295 |
return rv |
| 296 |
end |
| 297 |
|
| 298 |
function acpi_backend:details_pipe() |
| 299 |
return popen(self.backend) |
| 300 |
end |
| 301 |
|
| 302 |
acpi_backend.backend = 'acpi' |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
function apm_backend:configure() |
| 307 |
if exists 'apm' then |
| 308 |
local fd, err = popen 'uname' |
| 309 |
if not fd then |
| 310 |
return nil, err |
| 311 |
end |
| 312 |
local os_name = fd:read '*l' |
| 313 |
fd:close() |
| 314 |
|
| 315 |
if os_name ~= 'OpenBSD' then |
| 316 |
return apm_backend:clone() |
| 317 |
end |
| 318 |
end |
| 319 |
end |
| 320 |
|
| 321 |
function apm_backend:state() |
| 322 |
local rv = {} |
| 323 |
local fd, err = popen 'apm' |
| 324 |
|
| 325 |
if not fd then |
| 326 |
return nil, err |
| 327 |
end |
| 328 |
|
| 329 |
local data = fd:read '*all' |
| 330 |
fd:close() |
| 331 |
|
| 332 |
if not data then return end |
| 333 |
|
| 334 |
rv.status = data:match('battery ([a-z]+):') |
| 335 |
rv.charge = tonumber(data:match('.*, .*: (%d+)%%')) |
| 336 |
rv.time = data:match('%((.*)%)$') |
| 337 |
|
| 338 |
return rv |
| 339 |
end |
| 340 |
|
| 341 |
function apm_backend:details_pipe() |
| 342 |
return popen 'apm' |
| 343 |
end |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
function apm_openbsd_backend:configure() |
| 348 |
if exists 'apm' then |
| 349 |
local fd, err = popen 'uname' |
| 350 |
if not fd then |
| 351 |
return nil, err |
| 352 |
end |
| 353 |
local os_name = fd:read '*l' |
| 354 |
fd:close() |
| 355 |
|
| 356 |
if os_name == 'OpenBSD' then |
| 357 |
return apm_openbsd_backend:clone() |
| 358 |
end |
| 359 |
end |
| 360 |
end |
| 361 |
|
| 362 |
function apm_openbsd_backend:state() |
| 363 |
local rv = {} |
| 364 |
local fd, err = popen('apm -l -a -m') |
| 365 |
|
| 366 |
if not fd then |
| 367 |
return nil, err |
| 368 |
end |
| 369 |
|
| 370 |
local charge = fd:read '*l' |
| 371 |
local time = fd:read '*l' |
| 372 |
local status = fd:read '*l' |
| 373 |
fd:close() |
| 374 |
|
| 375 |
if status == '0' then |
| 376 |
status = 'discharging' |
| 377 |
elseif status == '1' then |
| 378 |
status = 'charging' |
| 379 |
else |
| 380 |
status = 'unknown' |
| 381 |
end |
| 382 |
|
| 383 |
charge = tonumber(charge) |
| 384 |
|
| 385 |
if time == 'unknown' then |
| 386 |
time = nil |
| 387 |
else |
| 388 |
local hours = tostring(floor(rv.time / 60 + 0.5)) |
| 389 |
local minutes = tostring(rv.time % 60) |
| 390 |
|
| 391 |
time = sformat('%02d:%02d', hours, minutes) |
| 392 |
end |
| 393 |
|
| 394 |
|
| 395 |
if charge >= 98 and status == 'charging' then |
| 396 |
status = 'full' |
| 397 |
end |
| 398 |
|
| 399 |
return { |
| 400 |
status = status, |
| 401 |
charge = charge, |
| 402 |
time = time, |
| 403 |
} |
| 404 |
end |
| 405 |
|
| 406 |
function apm_openbsd_backend:details_pipe() |
| 407 |
return popen 'apm' |
| 408 |
end |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
function null_backend:configure() |
| 413 |
return self |
| 414 |
end |
| 415 |
|
| 416 |
function null_backend:state() |
| 417 |
return nil |
| 418 |
end |
| 419 |
|
| 420 |
function null_backend:details() |
| 421 |
return 'unknown backend' |
| 422 |
end |
| 423 |
|
| 424 |
|
| 425 |
local function get(preferred_backend) |
| 426 |
local backend |
| 427 |
|
| 428 |
if preferred_backend then |
| 429 |
for i = 1, #backends do |
| 430 |
if backends[i].name == preferred_backend then |
| 431 |
backend = backends[i]:configure() |
| 432 |
end |
| 433 |
end |
| 434 |
end |
| 435 |
|
| 436 |
if not backend then |
| 437 |
for i = 1, #backends do |
| 438 |
backend = backends[i]:configure() |
| 439 |
|
| 440 |
if backend then |
| 441 |
break |
| 442 |
end |
| 443 |
end |
| 444 |
end |
| 445 |
|
| 446 |
assert(backend, 'I should always fall back to the null backend!') |
| 447 |
|
| 448 |
return backend |
| 449 |
end |
| 450 |
|
| 451 |
local _M = { get = get } |
| 452 |
|
| 453 |
for i = 1, #backends do |
| 454 |
local backend = backends[i] |
| 455 |
_M[backend.name] = backend |
| 456 |
end |
| 457 |
|
| 458 |
return _M |
| 459 |
|
| 460 |
|