2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local pairs = pairs
|
|
|
|
local table = table
|
2008-11-19 02:22:05 +01:00
|
|
|
local unpack = unpack
|
2008-09-29 16:49:18 +02:00
|
|
|
local ipairs = ipairs
|
|
|
|
local type = type
|
|
|
|
local math = math
|
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
hooks = hooks
|
|
|
|
}
|
|
|
|
|
|
|
|
--- Hooks module for awful
|
|
|
|
module("awful.hooks")
|
|
|
|
|
|
|
|
-- User hook functions
|
|
|
|
user = {}
|
|
|
|
|
|
|
|
--- Create a new userhook (for external libs).
|
|
|
|
-- @param name Hook name.
|
|
|
|
function user.create(name)
|
|
|
|
_M[name] = {}
|
|
|
|
_M[name].callbacks = {}
|
|
|
|
_M[name].register = function (f)
|
|
|
|
table.insert(_M[name].callbacks, f)
|
|
|
|
end
|
|
|
|
_M[name].unregister = function (f)
|
|
|
|
for k, h in ipairs(_M[name].callbacks) do
|
|
|
|
if h == f then
|
|
|
|
table.remove(_M[name].callbacks, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Call a created userhook (for external libs).
|
|
|
|
-- @param name Hook name.
|
|
|
|
function user.call(name, ...)
|
|
|
|
for name, callback in pairs(_M[name].callbacks) do
|
|
|
|
callback(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Autodeclare awful.hooks.* functions
|
|
|
|
-- mapped to awesome hooks.* functions
|
|
|
|
for name, hook in pairs(capi.hooks) do
|
2008-11-19 02:22:05 +01:00
|
|
|
_M[name] = {}
|
|
|
|
if name == 'timer' then
|
2008-09-29 16:49:18 +02:00
|
|
|
_M[name].register = function (time, f, runnow)
|
2009-02-09 12:14:15 +01:00
|
|
|
if type(time) ~= 'number' or type(f) ~= 'function' or time <= 0 then
|
2008-09-29 16:49:18 +02:00
|
|
|
return
|
|
|
|
end
|
2009-02-20 22:34:19 +01:00
|
|
|
|
|
|
|
if not _M[name].callbacks then
|
|
|
|
_M[name].callbacks = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in pairs(_M[name].callbacks) do
|
|
|
|
if v.callback == f then
|
|
|
|
_M[name].unregister(f)
|
|
|
|
_M[name].register(time, f, runnow)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-29 16:49:18 +02:00
|
|
|
local new_timer
|
|
|
|
if _M[name].timer then
|
|
|
|
-- Take the smallest between current and new
|
|
|
|
new_timer = math.min(time, _M[name].timer)
|
|
|
|
else
|
|
|
|
new_timer = time
|
|
|
|
end
|
2009-02-20 22:34:19 +01:00
|
|
|
|
2008-09-29 16:49:18 +02:00
|
|
|
if _M[name].timer ~= new_timer then
|
|
|
|
_M[name].timer = new_timer
|
|
|
|
end
|
2009-02-20 22:34:19 +01:00
|
|
|
|
2009-02-06 17:14:44 +01:00
|
|
|
hook(_M[name].timer, function (...)
|
|
|
|
for i, callback in ipairs(_M[name].callbacks) do
|
|
|
|
callback['counter'] = callback['counter'] + _M[name].timer
|
|
|
|
if callback['counter'] >= callback['timer'] then
|
|
|
|
callback['callback'](...)
|
|
|
|
callback['counter'] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2008-09-29 16:49:18 +02:00
|
|
|
|
|
|
|
if runnow then
|
|
|
|
table.insert(_M[name].callbacks, { callback = f, timer = time, counter = time })
|
|
|
|
else
|
|
|
|
table.insert(_M[name].callbacks, { callback = f, timer = time, counter = 0 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
_M[name].unregister = function (f)
|
|
|
|
if _M[name].callbacks then
|
|
|
|
for k, h in ipairs(_M[name].callbacks) do
|
|
|
|
if h.callback == f then
|
|
|
|
table.remove(_M[name].callbacks, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2009-02-06 15:08:47 +01:00
|
|
|
local delays = { }
|
|
|
|
for k, h in ipairs(_M[name].callbacks) do
|
|
|
|
table.insert(delays, h.timer)
|
|
|
|
end
|
|
|
|
table.sort(delays)
|
2009-02-06 20:55:38 +01:00
|
|
|
_M[name].timer = delays[1]
|
|
|
|
hook(delays[1], function (...)
|
|
|
|
for i, callback in ipairs(_M[name].callbacks) do
|
|
|
|
callback['counter'] = callback['counter'] + _M[name].timer
|
|
|
|
if callback['counter'] >= callback['timer'] then
|
|
|
|
callback['callback'](...)
|
|
|
|
callback['counter'] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
end
|
2008-11-19 02:22:05 +01:00
|
|
|
elseif name == "dbus" then
|
|
|
|
_M[name].register = function (interface, f)
|
|
|
|
if not _M[name].callbacks then
|
|
|
|
_M[name].callbacks = {}
|
|
|
|
hook(function (msg, ...)
|
|
|
|
if _M[name].callbacks[msg.interface] then
|
|
|
|
return _M[name].callbacks[msg.interface](msg, unpack(arg))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
_M[name].callbacks[interface] = f
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_M[name].register = function (f)
|
|
|
|
if not _M[name].callbacks then
|
|
|
|
_M[name].callbacks = {}
|
|
|
|
hook(function (...)
|
|
|
|
for i, callback in ipairs(_M[name].callbacks) do
|
|
|
|
callback(...)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(_M[name].callbacks, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if name ~= "timer" then
|
|
|
|
_M[name].unregister = function (f)
|
|
|
|
if _M[name].callbacks then
|
|
|
|
for k, h in ipairs(_M[name].callbacks) do
|
|
|
|
if h == f then
|
|
|
|
table.remove(_M[name].callbacks, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|