Fix bug when the focused window deinitialized
[notion.git] / contrib / statusd / legacy / statusd_batt.lua
blob831ad407e4bee29a6a126a5012f3f76ff52e8ffd
1 -- Authors: Relu Patrascu
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2004-11-05
4 --
5 -- statusd_batt.lua
6 --
7 -- Copyright (c) Relu Patrascu 2004.
8 --
9 -- Ion is free software; you can redistribute it and/or modify it under
10 -- the terms of the GNU Lesser General Public License as published by
11 -- the Free Software Foundation; either version 2.1 of the License, or
12 -- (at your option) any later version.
14 -- Nov. 5, 2004
15 -- Disclaimer
16 -- Neither am I a lua expert nor do I have the time to invest in writing
17 -- better code here. I simply needed this utility and it works OK for me.
18 -- I give no guarantees of any kind for this code. Suggestions for
19 -- improvement are welcome.
20 -- ikoflexer at gmail dot com
22 if not statusd_batt then
23 statusd_batt={ interval=10*1000 }
24 end
26 local battInfoStr
27 local function get_batt_procapm()
28 local f=io.open('/proc/apm', 'r')
29 if not f then
30 return ""
31 end
32 local s=f:read('*l')
33 f:close()
34 local st, en, battInfoStr = string.find(s, '(%d+%% [-]*%d+ [%?]*[%a]*)$')
35 battInfoStr = string.gsub(battInfoStr, '-1.*',"-(= ")
36 return tostring(battInfoStr)
37 end
39 local function get_batt_fn()
40 return get_batt_procapm
41 end
43 local get_batt, batt_timer
45 local function update_batt()
46 statusd.inform("batt", get_batt())
47 batt_timer:set(statusd_batt.interval, update_batt)
48 end
50 -- Init
51 get_batt=get_batt_fn()
52 batt_timer=statusd.create_timer()
53 update_batt()