Debian Codegraph @

flaw/init.lua

raw symbols SCIP
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 ipairs = ipairs
20
21 local naughty = require('naughty')
22 local table = require('table')
23
24 local service_modules = { loaded = {}, dropped = {}, invalid = {} }
25
26 local function load_flaw_component(name)
27 local m = require('flaw.' .. name)
28 if m then
29 if m.init ~= nil then
30 m = m.init()
31 end
32 if m ~= nil then
33 table.insert(service_modules.loaded, name)
34 else
35 table.insert(service_modules.dropped, name)
36 end
37 else
38 table.insert(service_modules.invalid, name)
39 end
40 return m
41 end
42
43 local function get_nb_modules(section)
44 return # service_modules[section]
45 end
46
47 local function get_modules_names(section)
48 local str = ''
49 for _, v in ipairs(service_modules[section]) do
50 str = str .. v .. ' '
51 end
52 return str
53 end
54
55 -- Essential modules.
56 require('flaw.gadget')
57 require('flaw.provider')
58 require('flaw.event')
59 require('flaw.helper')
60
61 -- Service modules.
62 load_flaw_component('alsa')
63 load_flaw_component('battery')
64 load_flaw_component('calendar')
65 load_flaw_component('cpu')
66 load_flaw_component('gmail')
67 load_flaw_component('keyboard')
68 load_flaw_component('memory')
69 load_flaw_component('network')
70 load_flaw_component('title')
71 load_flaw_component('wifi')
72
73
74 --- Introduction to the core concepts and mechanisms.
75 --
76 -- <p>The <b>flaw</b> package is composed of core modules which
77 -- provide the raw mechanisms and utilities, and service modules which
78 -- build upon this core to propose interesting gadgets with their
79 -- associated information sources.</p>
80 --
81 -- <p>Hereafter are described the core concepts developed by
82 -- <b>flaw</b>. Users also should have a look at the service modules
83 -- they are interested in. Developers may read the core modules
84 -- documentation before doing more complicated things.</p>
85 --
86 -- <h2>Setup</h2>
87 --
88 -- <p>The following statement must be inserted in the <b>awesome</b>
89 -- configuration before any <b>flaw</b> gadget or mechanism can be
90 -- used. Such a statement is usually written at the top of the
91 -- configuration file, where <b>naughty</b>, <b>beautiful</b> or
92 -- others modules are declared.</p>
93 --
94 -- <div class='example'>
95 -- require('flaw')<br/>
96 -- </div>
97 --
98 -- <p>When this statement is parsed at start up, each of the non-core
99 -- modules referenced in <b>flaw</b>'s <code>init.lua</code> are
100 -- loaded and then register the gadgets they provide, and a factory
101 -- for their associated provider. All this information is maintained
102 -- in a table in the <a href="flaw.gadget.html">gadget</a> core
103 -- module, and is ready to be used to instantiate gadgets.</p>
104 --
105 -- <h2>Gadgets</h2>
106 --
107 -- <p>Gadgets are a wrapper around <b>awful</b> widgets that provide
108 -- an automated way to react asynchronously on some hardware or
109 -- software events. The <a href="flaw.gadget.html">gadget</a> core
110 -- module provides the main gadgets factory and management.</p>
111 --
112 -- <p>Gadgets are divided in types which are named after the kinds of
113 -- widgets <b>awesome</b> provides: <code>Text</code>,
114 -- <code>Icon</code>, <code>Graph</code> and <code>ProgressBar</code>
115 -- (which is reduced to <code>Bar</code> in <b>flaw</b>). Thanks to
116 -- some nifty tricks, creating a gadget is straight forward. As an
117 -- example, the following statement instantiates a very simple gadget
118 -- which displays the title of the focused window.</p>
119 --
120 -- <div class='example'>
121 -- gtitle = flaw.gadget.text.title('')
122 -- </div>
123 --
124 -- <p>The <a href="flaw.gadget.html">gadget</a> module is indexed
125 -- first with the type of the desired gadget, and then with its
126 -- name. <code>title</code> is actually one of the service modules
127 -- which were loaded at start up, and which have registered a
128 -- <code>Text</code> gadget. Note that the parameter is required,
129 -- while not useful to this gadget (hence the empty string).</p>
130 --
131 -- <p>Here is a moderately complicated example which builds an
132 -- <b>awful</b> 60x18 graph widget which is updated every 5 seconds
133 -- with the current CPU activity.</p>
134 --
135 -- <div class='example'>
136 -- gcpu = flaw.gadget.graph.cpu('cpu',<br/>
137 -- &nbsp;&nbsp;&nbsp;{ delay = 5 }, { width = 60, height = 18 })<br/>
138 -- </div>
139 --
140 -- <p>The <a href="flaw.gadget.html">gadget</a> module is indexed
141 -- first with the <code>graph</code> type, and then with the
142 -- <code>cpu</code> gadget name. Indeed, the <code>cpu</code> module
143 -- registers a <code>Text</code>, a <code>Graph</code> and an
144 -- <code>Icon</code> gadget when loaded. Here the required first
145 -- parameter designates the cpu to be monitored. The second and third
146 -- parameters are respectively the gadget and the wrapped widget
147 -- options.</p>
148 --
149 -- <p>Here is now how these two gadgets can be embedded in a
150 -- <i>wibox</i> with a layout box and a tags list.</p>
151 --
152 -- <div class='example'>
153 -- for s = 1, screen.count() do<br/>
154 -- &nbsp;&nbsp;&nbsp;mylayoutbox[s] = awful.widget.layoutbox(s)<br/>
155 -- &nbsp;&nbsp;&nbsp;mytaglist[s] = awful.widget.taglist(<br/>
156 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s, awful.widget.taglist.label.all, mytaglist.buttons))<br/>
157 -- &nbsp;&nbsp;&nbsp;mywibox[s] = awful.wibox({ position = "top", screen = s })<br/>
158 -- &nbsp;&nbsp;&nbsp;<br/>
159 -- &nbsp;&nbsp;&nbsp;-- Add widgets to the wibox - order matters<br/>
160 -- &nbsp;&nbsp;&nbsp;mywibox[s].widgets = {<br/>
161 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
162 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mytaglist[s],<br/>
163 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gcpu.widget,<br/>
164 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gtitle.widget,<br/>
165 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layout = awful.widget.layout.horizontal.leftright<br/>
166 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br/>
167 -- &nbsp;&nbsp;&nbsp;mylayoutbox[s],<br/>
168 -- &nbsp;&nbsp;&nbsp;layout = awful.widget.layout.horizontal.rightleft<br/>
169 -- &nbsp;&nbsp;&nbsp;}<br/>
170 -- end<br/>
171 -- </div>
172 --
173 -- <h2>Providers</h2>
174 --
175 -- <p>Providers are the objects that gather data to feed a gadget. The
176 -- <a href="flaw.provider.html">provider</a> core module provides the
177 -- roots of the data mining objects.</p>
178 --
179 -- <p>Creating a gadget automatically registers itself to a provider
180 -- so that the displayed information is refreshed at the requested
181 -- pace. Providers can be shared among gadgets, and only refresh their
182 -- data according to the smallest gadget refresh rate.</p>
183 --
184 -- <p>No user action is required for this mechanism. However, the
185 -- refresh rate of a gadget can be tuned using the <code>delay</code>
186 -- gadget option. As seen in the gadgets description above, it
187 -- defaults to 10 seconds.</p>
188 --
189 -- <h2>Events</h2>
190 --
191 -- <p>The automated and efficient refresh loop of <b>flaw</b> is the
192 -- core of its design. It provides cyclic data updates to gadgets
193 -- which display it in accordance with their type. Events were
194 -- introduced to provide the capability to react to some data values,
195 -- and to do something else than just displaying them. This mechanism
196 -- is available to all of gadgets and is implemented in the <a
197 -- href="flaw.event.html">event</a> core module.</p>
198 --
199 -- <p>Events are made of a trigger and an action. The trigger's
200 -- condition takes the provider's data and is tested each time a
201 -- refresh is called. If it returns true, the event's action is then
202 -- fired, with the gadget as parameter. It can do absolutely anything,
203 -- and its return value has no meaning for <b>flaw</b>.</p>
204 --
205 -- <p>Here is an example assuming <code>g</code> is a battery icon
206 -- gadget and <code>battery_low_icon</code> a beautiful property
207 -- holding the path to an explicit low battery icon. This snippet
208 -- changes of the gadget when the battery load gets really low.</p>
209 --
210 -- <div class='example'>
211 -- g.add_event(<br/>
212 -- &nbsp;&nbsp;&nbsp;flaw.event.LatchTriger:new{
213 -- condition = function(d) return d.load &lt; 9 end },<br/>
214 -- &nbsp;&nbsp;&nbsp;function(g) g.widget.image =
215 -- image(beautiful.battery_low_icon) end)
216 -- </div>
217 --
218 -- <h2>Writing a new service module</h2>
219 --
220 -- <p><b>FIXME</b></p>
221 --
222 --
223 -- @author David Soulayrol &lt;david.soulayrol AT gmail DOT com&gt;
224 -- @copyright 2009,2010,2011, David Soulayrol
225 module("flaw")
226
227 --- Notify the dropped service modules to the user.
228 --
229 -- <p>This function pops up a notification displaying a list of
230 -- modules which were not loaded. These checked modules are those
231 -- which depend on hardware or sources that can be absent from some
232 -- system, like the battery.</p>
233 --
234 -- <p>This function can typically be called at the end of the
235 -- configuration file, or on demand to check the flaw library status.</p>
236 function check_modules()
237 if get_nb_modules('dropped') ~= 0 then
238 naughty.notify{
239 title = "Flaw",
240 text = "The following modules are absent from your system:\n"
241 .. get_modules_names('dropped'),
242 timeout = 12,
243 position = "top_right"}
244 end
245 end
246
247 --- Check the availability of a service module.
248 --
249 -- @param m the name of the service module to check.
250 -- @return true if the module was loaded, false otherwise. Note that
251 -- loaded means here that the module was correctly parsed and
252 -- has registered the provider or the gadgets it contains.
253 function check_module(m)
254 for _, v in ipairs(service_modules['loaded']) do
255 if m == v then return true end
256 end
257 return false
258 end