-- flaw, a Lua OO management framework for Awesome WM widgets. -- Copyright (C) 2009,2010,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 ipairs = ipairs local naughty = require('naughty') local table = require('table') local service_modules = { loaded = {}, dropped = {}, invalid = {} } local function load_flaw_component(name) local m = require('flaw.' .. name) if m then if m.init ~= nil then m = m.init() end if m ~= nil then table.insert(service_modules.loaded, name) else table.insert(service_modules.dropped, name) end else table.insert(service_modules.invalid, name) end return m end local function get_nb_modules(section) return # service_modules[section] end local function get_modules_names(section) local str = '' for _, v in ipairs(service_modules[section]) do str = str .. v .. ' ' end return str end -- Essential modules. require('flaw.gadget') require('flaw.provider') require('flaw.event') require('flaw.helper') -- Service modules. load_flaw_component('alsa') load_flaw_component('battery') load_flaw_component('calendar') load_flaw_component('cpu') load_flaw_component('gmail') load_flaw_component('keyboard') load_flaw_component('memory') load_flaw_component('network') load_flaw_component('title') load_flaw_component('wifi') --- Introduction to the core concepts and mechanisms. -- --

The flaw package is composed of core modules which -- provide the raw mechanisms and utilities, and service modules which -- build upon this core to propose interesting gadgets with their -- associated information sources.

-- --

Hereafter are described the core concepts developed by -- flaw. Users also should have a look at the service modules -- they are interested in. Developers may read the core modules -- documentation before doing more complicated things.

-- --

Setup

-- --

The following statement must be inserted in the awesome -- configuration before any flaw gadget or mechanism can be -- used. Such a statement is usually written at the top of the -- configuration file, where naughty, beautiful or -- others modules are declared.

-- --
-- require('flaw')
--
-- --

When this statement is parsed at start up, each of the non-core -- modules referenced in flaw's init.lua are -- loaded and then register the gadgets they provide, and a factory -- for their associated provider. All this information is maintained -- in a table in the gadget core -- module, and is ready to be used to instantiate gadgets.

-- --

Gadgets

-- --

Gadgets are a wrapper around awful widgets that provide -- an automated way to react asynchronously on some hardware or -- software events. The gadget core -- module provides the main gadgets factory and management.

-- --

Gadgets are divided in types which are named after the kinds of -- widgets awesome provides: Text, -- Icon, Graph and ProgressBar -- (which is reduced to Bar in flaw). Thanks to -- some nifty tricks, creating a gadget is straight forward. As an -- example, the following statement instantiates a very simple gadget -- which displays the title of the focused window.

-- --
-- gtitle = flaw.gadget.text.title('') --
-- --

The gadget module is indexed -- first with the type of the desired gadget, and then with its -- name. title is actually one of the service modules -- which were loaded at start up, and which have registered a -- Text gadget. Note that the parameter is required, -- while not useful to this gadget (hence the empty string).

-- --

Here is a moderately complicated example which builds an -- awful 60x18 graph widget which is updated every 5 seconds -- with the current CPU activity.

-- --
-- gcpu = flaw.gadget.graph.cpu('cpu',
--    { delay = 5 }, { width = 60, height = 18 })
--
-- --

The gadget module is indexed -- first with the graph type, and then with the -- cpu gadget name. Indeed, the cpu module -- registers a Text, a Graph and an -- Icon gadget when loaded. Here the required first -- parameter designates the cpu to be monitored. The second and third -- parameters are respectively the gadget and the wrapped widget -- options.

-- --

Here is now how these two gadgets can be embedded in a -- wibox with a layout box and a tags list.

-- --
-- for s = 1, screen.count() do
--    mylayoutbox[s] = awful.widget.layoutbox(s)
--    mytaglist[s] = awful.widget.taglist(
--       s, awful.widget.taglist.label.all, mytaglist.buttons))
--    mywibox[s] = awful.wibox({ position = "top", screen = s })
--    
--    -- Add widgets to the wibox - order matters
--    mywibox[s].widgets = {
--       {
--       mytaglist[s],
--       gcpu.widget,
--       gtitle.widget,
--       layout = awful.widget.layout.horizontal.leftright
--       },
--    mylayoutbox[s],
--    layout = awful.widget.layout.horizontal.rightleft
--    }
-- end
--
-- --

Providers

-- --

Providers are the objects that gather data to feed a gadget. The -- provider core module provides the -- roots of the data mining objects.

-- --

Creating a gadget automatically registers itself to a provider -- so that the displayed information is refreshed at the requested -- pace. Providers can be shared among gadgets, and only refresh their -- data according to the smallest gadget refresh rate.

-- --

No user action is required for this mechanism. However, the -- refresh rate of a gadget can be tuned using the delay -- gadget option. As seen in the gadgets description above, it -- defaults to 10 seconds.

-- --

Events

-- --

The automated and efficient refresh loop of flaw is the -- core of its design. It provides cyclic data updates to gadgets -- which display it in accordance with their type. Events were -- introduced to provide the capability to react to some data values, -- and to do something else than just displaying them. This mechanism -- is available to all of gadgets and is implemented in the event core module.

-- --

Events are made of a trigger and an action. The trigger's -- condition takes the provider's data and is tested each time a -- refresh is called. If it returns true, the event's action is then -- fired, with the gadget as parameter. It can do absolutely anything, -- and its return value has no meaning for flaw.

-- --

Here is an example assuming g is a battery icon -- gadget and battery_low_icon a beautiful property -- holding the path to an explicit low battery icon. This snippet -- changes of the gadget when the battery load gets really low.

-- --
-- g.add_event(
--    flaw.event.LatchTriger:new{ -- condition = function(d) return d.load < 9 end },
--    function(g) g.widget.image = -- image(beautiful.battery_low_icon) end) --
-- --

Writing a new service module

-- --

FIXME

-- -- -- @author David Soulayrol <david.soulayrol AT gmail DOT com> -- @copyright 2009,2010,2011, David Soulayrol module("flaw") --- Notify the dropped service modules to the user. -- --

This function pops up a notification displaying a list of -- modules which were not loaded. These checked modules are those -- which depend on hardware or sources that can be absent from some -- system, like the battery.

-- --

This function can typically be called at the end of the -- configuration file, or on demand to check the flaw library status.

function check_modules() if get_nb_modules('dropped') ~= 0 then naughty.notify{ title = "Flaw", text = "The following modules are absent from your system:\n" .. get_modules_names('dropped'), timeout = 12, position = "top_right"} end end --- Check the availability of a service module. -- -- @param m the name of the service module to check. -- @return true if the module was loaded, false otherwise. Note that -- loaded means here that the module was correctly parsed and -- has registered the provider or the gadgets it contains. function check_module(m) for _, v in ipairs(service_modules['loaded']) do if m == v then return true end end return false end