Released version 3-2014010502
[notion.git] / contrib / statusd / legacy / statusd_apm.lua
blob850c92b68f1510e78a86285654e275157314b84c
1 -- Authors: Greg Steuck, Darrin Chandler <dwchandler@stilyagin.com>
2 -- License: Public domain
3 -- Last Changed: 2006-10-28
4 --
5 -- Adds capability for OpenBSD APM info in ion3 statusbar.
6 --
7 -- Originally written by Greg Steuck and released into the Public Domain
8 -- 2006-10-28 modified by Darrin Chandler <dwchandler@stilyagin.com>
9 -- to work with OpenBSD 4.0 apm output.
11 -- To install:
12 -- Save this file as ~/.ion3/statusd_apm.lua,
13 -- Change ~/.ion3/cfg_statusbar.lua like so:
14 -- Add "apm={}," to mod_statusbar.launch_statusd,
15 -- Modify "template" to include %apm_ variables
16 -- e.g. template="[ %date || load:% %>load || bat: %apm_pct%%, A/C %apm_ac ],"
17 --
18 -- Available variables:
19 -- %apm_state high, low, critical
20 -- %apm_pct battery life (in percent)
21 -- %apm_estimate battery life (in minutes)
22 -- %apm_ac External A/C charger state
23 -- %apm_mode Adjustment mode (manual, auto, cool running)
24 -- %apm_speed CPU speed
26 local unknown = "?", "?", "?", "?", "?", "?"
28 function get_apm()
29 local f=io.popen('/usr/sbin/apm', 'r')
30 if not f then
31 return unknown
32 end
33 local s=f:read('*all')
34 f:close()
35 local _, _, state, pct, estimate, ac, mode, speed =
36 string.find(s, "Battery state: (%S+), "..
37 "(%d+)%% remaining, "..
38 "([0-9]*) minutes life estimate\n"..
39 "A/C adapter state: ([^\n]*)\n"..
40 "Performance adjustment mode:%s(.+)%s"..
41 "%((.+)%)\n"..
42 ".*"
44 if not state then
45 return unknown
46 end
47 return state, pct, estimate, ac, mode, speed
48 end
50 local function inform(key, value)
51 statusd.inform("apm_"..key, value)
52 end
54 local apm_timer
56 local function update_apm()
57 local state, pct, estimate, ac, mode, speed = get_apm()
58 local hint
59 if statusd ~= nil then
60 inform("state", state)
61 inform("pct", pct)
62 inform("estimate", estimate)
63 if state == "high" then
64 hint = "normal"
65 elseif state == "low" then
66 hint = "important"
67 else
68 hint = "critical"
69 end
70 if hint ~= nil then
71 inform("state_hint", hint)
72 inform("pct_hint", hint)
73 inform("estimate_hint", hint)
74 end
75 inform("ac", ac)
76 if ac == "connected" then
77 hint = "important"
78 else
79 hint = "critical"
80 end
81 inform("ac_hint", hint)
82 inform("mode", mode)
83 inform("speed", speed)
84 apm_timer:set(30*1000, update_apm)
85 else
86 io.stdout:write("Batt: "..pct.."% ("..state..
87 ", "..estimate.." mins)\n"..
88 "A/C: "..ac.."\n"..
89 "Mode: "..mode.."\n"..
90 "CPU Speed: "..speed.."\n"
92 end
93 end
95 if statusd ~= nil then
96 apm_timer = statusd.create_timer()
97 end
98 update_apm()