Fix bug when the focused window deinitialized
[notion.git] / contrib / statusd / legacy / statusd_apm2.lua
blob4552c5f61d25e3846b42d80b0effe32f079594a0
1 -- Authors: Greg Steuck, Gerald Young <ion3script@gwy.org>
2 -- License: Public domain
3 -- Last Changed: Unknown
4 --
5 -- Public domain, written by Greg Steuck
6 -- Edited and updated by Gerald Young -- ion3script@gwy.org
7 -- This works on FreeBSD's apm (5.x) program added some color to indicate
8 -- AC connection and status (Charging, low, critical)
9 -- Allows displaying apm information in the statusbar.
10 -- To install:
11 -- save this file into ~/.ion3/statusd_apm.lua,
12 -- copy the default cfg_statusbar.lua to ~/.ion3, edit it to include (~line 81):
13 -- -- Battery meter
14 -- --[[
15 -- apm={},
16 -- --]]
17 -- add some of the following fields into your template in cfg_statusbar.lua:
18 -- %apm_ac: A/C cable on-line (connected) off-line (disconnected)
19 -- %apm_pct: percent of remaining battery
20 -- %apm_estimate: time remaining based upon usage ... or whatever apm thinks.
21 -- %apm_state: Status: charging/high/low/critical
22 -- in cfg_statusbar.lua, about line 28, add the next line without the leading "--"
23 -- template="[ %date || load:% %>load_1min || battery: %apm_pct AC: %apm_ac Status: %apm_state ]",
24 -- If you've already customized your template= line, then simply add the field(s) where you want.
26 local unknown = "?", "?", "?", "?"
28 -- Runs apm utility and grabs relevant pieces from its output.
29 -- Most likely will only work on OpenBSD due to reliance on its output pattern.
30 function get_apm()
31 local f=io.popen('/usr/sbin/apm', 'r')
32 if not f then
33 return unknown
34 end
35 local s=f:read('*all')
36 f:close()
37 local _, _, ac, state, pct, estimate =
38 string.find(s,
39 "AC Line status: (.*)\n"..
40 "Battery Status: (.*)\n"..
41 "Remaining battery life: (.*)\n"..
42 "Remaining battery time: (.*)\n"
44 if not state then
45 return unknown
46 end
47 return state, pct, estimate, ac
48 end
50 local function inform(key, value)
51 statusd.inform("apm_"..key, value)
52 end
54 local apm_timer = statusd.create_timer()
56 local function update_apm()
57 local state, pct, estimate, ac = get_apm()
58 local stateinf
59 if state=="low" then
60 stateinf = "important"
61 end
62 if state == "critical" then
63 stateinf = "critical"
64 end
65 if state == "charging" then
66 stateinf = "important"
67 end
68 inform("state", state)
69 inform("state_hint", stateinf)
70 inform("pct", pct)
71 inform("estimate", estimate)
72 if ac == "off-line" then
73 stateinf="critical"
74 end
75 if ac == "on-line" then
76 stateinf="important"
77 end
78 inform("ac", ac)
79 inform("ac_hint", stateinf)
80 apm_timer:set(60*1000, update_apm)
81 end
83 update_apm()