1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
17 local util
= require("awful.util")
19 --- Hooks module for awful
22 -- User hook functions
25 --- Create a new userhook (for external libs).
26 -- @param name Hook name.
27 function user
.create(name
)
29 _M
[name
].callbacks
= {}
30 _M
[name
].register
= function (f
)
31 table.insert(_M
[name
].callbacks
, f
)
33 _M
[name
].unregister
= function (f
)
34 for k
, h
in ipairs(_M
[name
].callbacks
) do
36 table.remove(_M
[name
].callbacks
, k
)
43 --- Call a created userhook (for external libs).
44 -- @param name Hook name.
45 function user
.call(name
, ...)
46 for name
, callback
in pairs(_M
[name
].callbacks
) do
51 -- Autodeclare awful.hooks.* functions
52 -- mapped to awesome hooks.* functions
53 for name
, hook
in pairs(capi
.hooks
) do
55 if name
== 'timer' then
56 _M
[name
].register
= function (time
, f
, runnow
)
57 util
.deprecate("timer object")
58 if type(time
) ~= 'number' or type(f
) ~= 'function' or time
<= 0 then
62 if not _M
[name
].callbacks
then
63 _M
[name
].callbacks
= {}
66 for k
, v
in pairs(_M
[name
].callbacks
) do
67 if v
.callback
== f
then
68 _M
[name
].unregister(f
)
69 _M
[name
].register(time
, f
, runnow
)
75 if _M
[name
].timer
then
76 -- Take the smallest between current and new
77 new_timer
= math
.min(time
, _M
[name
].timer
)
82 if _M
[name
].timer
~= new_timer
then
83 _M
[name
].timer
= new_timer
86 hook(_M
[name
].timer
, function (...)
87 for i
, callback
in ipairs(_M
[name
].callbacks
) do
88 callback
['counter'] = callback
['counter'] + _M
[name
].timer
89 if callback
['counter'] >= callback
['timer'] then
90 callback
['callback'](...)
91 callback
['counter'] = 0
97 table.insert(_M
[name
].callbacks
, { callback
= f
, timer
= time
, counter
= time
})
99 table.insert(_M
[name
].callbacks
, { callback
= f
, timer
= time
, counter
= 0 })
102 _M
[name
].unregister
= function (f
)
103 if _M
[name
].callbacks
then
104 for k
, h
in ipairs(_M
[name
].callbacks
) do
105 if h
.callback
== f
then
106 table.remove(_M
[name
].callbacks
, k
)
111 for k
, h
in ipairs(_M
[name
].callbacks
) do
112 table.insert(delays
, h
.timer
)
115 _M
[name
].timer
= delays
[1]
116 if not delays
[1] then delays
[1] = 0 end
117 hook(delays
[1], function (...)
118 for i
, callback
in ipairs(_M
[name
].callbacks
) do
119 callback
['counter'] = callback
['counter'] + _M
[name
].timer
120 if callback
['counter'] >= callback
['timer'] then
121 callback
['callback'](...)
122 callback
['counter'] = 0
129 _M
[name
].register
= function (f
)
130 util
.deprecate("signals")
131 if not _M
[name
].callbacks
then
132 _M
[name
].callbacks
= {}
134 for i
, callback
in ipairs(_M
[name
].callbacks
) do
140 table.insert(_M
[name
].callbacks
, f
)
144 if name
~= "timer" then
145 _M
[name
].unregister
= function (f
)
146 if _M
[name
].callbacks
then
147 for k
, h
in ipairs(_M
[name
].callbacks
) do
149 table.remove(_M
[name
].callbacks
, k
)
158 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80