-- flaw, a Lua OO management framework for Awesome WM widgets. -- Copyright (C) 2011 David Soulayrol -- 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 . -- Grab environment. local io = io local string = string local tonumber = tonumber local lfs = require('lfs') local flaw = { gadget = require('flaw.gadget'), helper = require('flaw.helper'), provider = require('flaw.provider') } --- Wifi information. -- --

This module contains a provider for wifi information and two -- gadgets: a text gadget and an icon gadget.

-- --

Gadgets

-- --

The ID of the gadgets designates the network device to be -- monitored. Wifi gadgets provide no custom parameters. See the gadget module documentation to learn -- about standard gadgets parameters.

-- --

Icon Gadget

-- --

The wifi icon gadget can be instantiated by indexing the -- gadget module with icon.wifi. Here is an exemple -- which assumes the icon path is stored in a beautiful -- property.

-- --
-- g = flaw.gadget.icon.wifi(
--    'wlan0', {}, { image = image(beautiful.wifi_icon) })
--
-- --

Text Gadget

-- --

The wifi text gadget can be instantiated by indexing the -- gadget module with text.wifi. By default, the -- gadget pattern is $essid: $rate Mb/s. See the -- provider's documentation below to learn about the available -- variables.

-- --
-- g = flaw.gadget.text.wifi('wlan1') --
-- --

Provider

-- --

The wifi provider loads its information from -- iwconfig output (generally from the wireless-tools -- package). The use of iwconfig was prefered to -- iwgetid (which output if far simpler to parse) because -- it returns all data at once and so limits the number of processes -- to launch.

-- --

The wifi provider stores the data for each configured wireless -- device at once. Data is composed of the following fields.

-- -- -- -- -- @author David Soulayrol <david.soulayrol AT gmail DOT com> -- @copyright 2011, David Soulayrol module('flaw.wifi') --- The wifi provider prototype. -- --

The wifi provider type is set to wifi._NAME.

-- -- @class table -- @name WifiProvider WifiProvider = flaw.provider.CyclicProvider:new{ type = _NAME } --- Parse one entry from iwconfig output. -- -- @param bloc the text representing information for one device. The -- first characters of this bloc are the name of the device -- which is analysed. function WifiProvider:parse_adapter_info(bloc) local adapter = string.match(bloc, '([%w%d]+)') self.data[adapter] = {} self.data[adapter].essid = string.match(bloc, 'ESSID[=:]"(.-)"') or 'N/A' self.data[adapter].ap = string.match(bloc, 'Access Point[=:] ([%w%d%:]+)') or 'No Access-Point' self.data[adapter].mode = string.match(bloc, "Mode[=:]([%w%-]*)") or '' self.data[adapter].freq = tonumber(string.match(bloc, "Frequency[=:]([%d%.]+)")) or 0 self.data[adapter].channel = tonumber(string.match(bloc, "Channel[=:]([%d]+)")) or 0 self.data[adapter].rate = tonumber(string.match(bloc, "Bit Rate[=:]([%s]*[%d%.]*)")) or 0 self.data[adapter].quality = string.match(bloc, "Link Quality[=:]([%d]+/[%d]+)") or 'N/A' self.data[adapter].level = tonumber(string.match(bloc, "Signal level[=:]([%-]?[%d]+)")) or 0 end --- Callback for provider refresh. function WifiProvider:do_refresh() local file = io.popen('/sbin/iwconfig 2> /dev/null', 'r') local content = '' local line = false if file then while line ~= nil do line = file:read() if line == '' then self:parse_adapter_info(content) elseif line ~= nil then content = content .. line end end file:close() end end --- A factory for wifi providers. -- --

Only one provider is built for all wireless adapters. Its data -- is a map indexed by the name of the wireless device.

-- -- @return a brand new wireless provider, or the existing one if found -- in the providers cache. function provider_factory() local p = flaw.provider.get(_NAME, '') -- Create the provider if necessary. if p == nil then p = WifiProvider:new{ id = '', data = {} } flaw.provider.add(p) end return p end --- Module initialization routine. -- --

This method tests the available information on the system. If -- wireless devices can can be found, it registers gadgets and returns -- the module; otherwise it simply returns nil.

-- -- @return return this module if it can be used on the system, -- false otherwise. function init() local file = '/proc/net/wireless' if lfs.attributes(file, 'mode') == 'file' then local t = {} if flaw.helper.file.load_state_file('', file, t) > 0 then -- An icon gadget prototype for wifi status display. flaw.gadget.register.icon(_M) -- A Text gadget prototype for wifi status display. flaw.gadget.register.text(_M, { pattern = '$essid: $rate Mb/s' }) return _M end end end