/usr/share/awesome/lib/flaw/battery.lua is in awesome-extra 2012061101.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | -- flaw, a Lua OO management framework for Awesome WM widgets.
-- Copyright (C) 2009,2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- Grab environment.
local io = io
local lfs = require('lfs')
local math = math
local os = os
local beautiful = require('beautiful')
local naughty = require('naughty')
local flaw = {
helper = require('flaw.helper'),
gadget = require('flaw.gadget'),
provider = require('flaw.provider')
}
-- Provider sources.
local SRC_PROC_DIR = '/proc/acpi/battery/'
local SRC_SYSFS_DIR = '/sys/class/power_supply/'
-- Helper tools for environment settings.
local function has_proc_information()
return lfs.attributes(SRC_PROC_DIR, 'mode') == 'directory'
end
local function has_sysfs_information()
local path = SRC_SYSFS_DIR
local r = false
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local f = io.open(path .. '/' .. file .. '/type')
if f ~= nil then
if f:read() == 'Battery' then
r = true
end
f:close()
end
end
if r ~= false then break end
end
return r
end
--- Battery information gadgets and provider.
--
-- <p>This module contains a provider for battery information and two
-- gadgets: a text gadget and an icon gadget.</p>
--
-- <h2>Gadgets</h2>
--
-- <p>The ID of the gadget designates the battery slot to be
-- monitored. Battery gadgets provide no custom parameters. See the <a
-- href="flaw.gadget.html">gadget</a> module documentation to learn
-- about standard gadgets parameters.</p>
--
-- <h3>Icon Gadget</h3>
--
-- <p>The battery icon gadget can be instantiated by indexing the
-- gadget module with <code>icon.battery</code>. Here is an exemple
-- which assumes the battery icon path is stored in a <b>beautiful</b>
-- property.</p>
--
-- <div class='example'>
-- g = flaw.gadget.icon.battery(<br/>
-- 'BAT0', {}, { image = image(beautiful.battery_icon) })<br/>
-- </div>
--
-- <h3>Text Gadget</h3>
--
-- <p>The battery text gadget can be instantiated by indexing the
-- gadget module with <code>text.battery</code>. By default, the
-- gadget pattern is <code>'$load% $status'</code>. See the provider's
-- documentation below to learn about the available variables.</p>
--
-- <div class='example'>
-- g = flaw.gadget.text.battery('BAT0')
-- </div>
--
-- <h2>Provider</h2>
--
-- <p>The battery provider loads its information from
-- <code>/proc/acpi/battery/<ID>/</code> and
-- <code>/sys/class/power_supply/<ID>/</code> if they exist.</p>
--
-- <p>The battery provider data is composed of the following fields.</p>
--
-- <ul>
--
-- <li><code>load</code>
--
-- <p>The current battery load in percents.</p></li>
--
-- <li><code>st_symbol</code>
--
-- <p>The current power supply utilisation. It is set to the value of
-- <code>battery.STATUS_PLUGGED</code> if AC adaptor is in use and
-- there is no activity on the battery,
-- <code>battery.STATUS_CHARGING</code> if the battery is in charge,
-- or <code>battery.STATUS_DISCHARGING</code> if the battery is
-- currently the only power supply. At last, the symbol
-- <code>battery.STATUS_UNKNOWN</code> is used when the provider has
-- no information on the battery slot.</p></li>
--
-- <li><code>seconds</code>
--
-- <p>The number of seconds until the battery is empty or loaded,
-- whether its status is discharging or charging. This information is
-- currently only available if the
-- <code>/proc/acpi/battery/<ID>/</code> path is
-- available</p></li>
--
-- <li><code>time</code>
--
-- <p>The <code>seconds</code> information, formatted as a date.</p></li>
--
-- </ul>
--
--
-- @author David Soulayrol <david.soulayrol AT gmail DOT com>
-- @copyright 2009,2010,2011 David Soulayrol
module('flaw.battery')
-- Battery statuses.
STATUS_UNKNOWN = '='
STATUS_PLUGGED = '(A/C)'
STATUS_CHARGING = '^'
STATUS_DISCHARGING = 'v'
--- The battery provider prototype.
--
-- <p>The battery provider type is set to
-- <code>battery._NAME</code>.</p>
--
-- @class table
-- @name BatteryProvider
BatteryProvider = flaw.provider.CyclicProvider:new{ type = _NAME }
--- Load state information from
--- <code>/proc/acpi/battery/<ID>/</code> if it exists.
function BatteryProvider:load_from_procfs()
local p = self.data.proc
local r1 = flaw.helper.file.load_state_file(
SRC_PROC_DIR .. self.id:upper(), 'state', p)
local r2 = flaw.helper.file.load_state_file(
SRC_PROC_DIR .. self.id:upper(), 'info', p)
if r1 > 0 or r2 > 0 then
-- Adapt values.
local state = p.state_charging_state or ''
local r_capacity = p.state_remaining_capacity:match('(%d+).*') or 0
local rate = p.state_present_rate:match('(%d+).*') or 1
if rate ~= nil and rate ~= 0 then
if state == 'discharging' then
-- remaining seconds.
self.data.seconds = 3600 * r_capacity / rate
self.data.time = os.date('!remaining %X', self.data.seconds)
elseif state == 'charging' then
-- seconds until charged.
local l_capacity = p.info_last_full_capacity:match('(%d+).*') or 0
self.data.seconds = 3600 * (l_capacity - r_capacity) / rate
self.data.time = os.date('!full in %X', self.data.seconds)
end
end
end
end
--- Load state information from
--- <code>/sys/class/power_supply/<ID>/</code> if it exists.
function BatteryProvider:load_from_sysfs()
local f = nil
-- Load raw values.
local f = io.open(SRC_SYSFS_DIR .. self.id .. "/charge_now")
if f ~= nil then
self.data.sys.charge_now = f:read()
f:close()
end
f = io.open(SRC_SYSFS_DIR .. self.id .. "/charge_full")
if f ~= nil then
self.data.sys.charge_full = f:read()
f:close()
end
f = io.open(SRC_SYSFS_DIR .. self.id .. "/status")
if f ~= nil then
self.data.sys.status = f:read()
f:close()
end
-- Compute interesting values.
self.data.load =
math.floor(self.data.sys.charge_now * 100 / self.data.sys.charge_full)
if self.data.load > 100 then self.data.load = 100 end
self.data.st_symbol = STATUS_PLUGGED
if self.data.sys.status:match("Charging") then
self.data.st_symbol = STATUS_CHARGING
elseif self.data.sys.status:match("Discharging") then
self.data.st_symbol = STATUS_DISCHARGING
end
end
--- Callback for provider refresh.
function BatteryProvider:do_refresh()
if has_proc_information() then
self:load_from_procfs()
end
self:load_from_sysfs()
end
--- A factory for battery providers.
--
-- <p>Only one provider is built for a slot. Created providers are
-- stored in the global provider cache.</p>
--
-- @param slot the identifier of the battery for which the new
-- provider should gather information.
-- @return a brand new battery provider, or an existing one if the
-- given slot was already used to create one.
function provider_factory(slot)
local p = flaw.provider.get(_NAME, slot)
-- Create the provider if necessary.
if p == nil then
p = BatteryProvider:new{
id = slot, data = {
load = 0,
st_symbol = '',
seconds = 0,
time = '',
sys = {}, proc = {} } }
flaw.provider.add(p)
end
return p
end
--- Module initialization routine.
--
-- <p>This method tests the available information on the system. If
-- battery data can be found, it registers gadgets and returns the
-- module; otherwise it simply returns nil.</p>
--
-- @return return this module if it can be used on the system,
-- false otherwise.
function init()
if has_proc_information() or has_sysfs_information() then
-- A Text gadget prototype for battery status display.
flaw.gadget.register.text(_M, { pattern = '$load% $status' })
-- An icon gadget prototype for battery status display.
flaw.gadget.register.icon(_M, { status = STATUS_UNKNOWN })
return _M
end
end
|