[UP] use ion3-plus haha.
[arrow.git] / archlinux_conf / etc / ion3 / old / statusd_linuxbatt.lua
blob074df95564c9f04855f7b821336252bd719d2b5e
1 -- statusd_linuxbatt.lua
2 --
3 -- Public domain
4 --
5 -- Uses the /proc/acpi interface to get battery percentage.
6 --
7 -- Use the key "linuxbatt" to get the battery percentage; use
8 -- "linuxbatt_state" to get a symbol indicating charging "+",
9 -- discharging "-", or charged " ".
11 -- Now uses lua functions instead of bash, awk, dc. MUCH faster!
13 -- The "bat" option to the statusd settings for linuxbatt modifies which
14 -- battery we look at.
16 local defaults={
17 update_interval=15*1000,
18 bat=0,
19 important_threshold=30,
20 critical_threshold=10,
22 local settings=table.join(statusd.get_config("linuxbatt"), defaults)
24 function linuxbatt_do_find_capacity()
25 local f=io.open('/proc/acpi/battery/BAT'.. settings.bat ..'/info')
26 local infofile=f:read('*a')
27 f:close()
28 local i, j, capacity = string.find(infofile, 'last full capacity:%s*(%d+) .*')
29 return capacity
30 end
32 local capacity = linuxbatt_do_find_capacity()
34 function get_linuxbatt()
36 local f=io.open('/proc/acpi/battery/BAT'.. settings.bat ..'/state')
37 local statefile=f:read('*a')
38 f:close()
39 local i, j, remaining = string.find(statefile, 'remaining capacity:%s*(%d+) .*')
40 local percent = math.floor( remaining * 100 / capacity )
42 local i, j, statename = string.find(statefile, 'charging state:%s*(%a+).*')
43 if statename == "charging" then
44 return percent, "+"
45 elseif statename == "discharging" then
46 return percent, "-"
47 else
48 return percent, " "
49 end
50 end
52 function update_linuxbatt()
53 local perc, state = get_linuxbatt()
54 statusd.inform("linuxbatt", tostring(perc))
55 statusd.inform("linuxbatt_state", state)
56 if perc < settings.critical_threshold
57 then statusd.inform("linuxbatt_hint", "critical")
58 elseif perc < settings.important_threshold
59 then statusd.inform("linuxbatt_hint", "important")
60 else statusd.inform("linuxbatt_hint", "normal")
61 end
62 linuxbatt_timer:set(settings.update_interval, update_linuxbatt)
63 end
65 linuxbatt_timer = statusd.create_timer()
66 update_linuxbatt()