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