2014-12-06 16:44:04 +01:00
|
|
|
---------------------------------------------------------------------------
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Timer objects and functions.
|
|
|
|
--
|
2014-12-06 16:44:04 +01:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2014 Uli Schlachter
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @classmod gears.timer
|
2014-12-06 16:44:04 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2015-01-11 10:58:49 +01:00
|
|
|
local capi = { awesome = awesome }
|
|
|
|
local ipairs = ipairs
|
2014-12-06 16:44:04 +01:00
|
|
|
local pairs = pairs
|
2015-01-11 10:58:49 +01:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local table = table
|
2014-12-06 16:44:04 +01:00
|
|
|
local tonumber = tonumber
|
|
|
|
local traceback = debug.traceback
|
2016-02-07 13:29:46 +01:00
|
|
|
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
|
2014-12-06 16:44:04 +01:00
|
|
|
local glib = require("lgi").GLib
|
|
|
|
local object = require("gears.object")
|
2016-02-28 12:28:55 +01:00
|
|
|
local protected_call = require("gears.protected_call")
|
2014-12-06 16:44:04 +01:00
|
|
|
|
|
|
|
--- Timer objects. This type of object is useful when triggering events repeatedly.
|
|
|
|
-- The timer will emit the "timeout" signal every N seconds, N being the timeout
|
2015-08-11 13:29:49 +02:00
|
|
|
-- value. Note that a started timer will not be garbage collected. Call `:stop`
|
|
|
|
-- to enable garbage collection.
|
2015-02-15 22:26:47 +01:00
|
|
|
-- @tfield number timeout Interval in seconds to emit the timeout signal.
|
2015-10-14 00:21:29 +02:00
|
|
|
-- Can be any value, including floating point ones (e.g. 1.5 seconds).
|
2015-02-15 22:26:47 +01:00
|
|
|
-- @tfield boolean started Read-only boolean field indicating if the timer has been
|
2015-10-14 00:21:29 +02:00
|
|
|
-- started.
|
2015-02-15 22:26:47 +01:00
|
|
|
-- @table timer
|
2014-12-06 16:44:04 +01:00
|
|
|
|
2015-11-01 17:08:20 +01:00
|
|
|
--- When the timer is started.
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @signal .start
|
2015-11-01 17:08:20 +01:00
|
|
|
|
|
|
|
--- When the timer is stopped.
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @signal .stop
|
|
|
|
|
|
|
|
--- When the timer had a timeout event.
|
|
|
|
-- @signal .timeout
|
2015-11-01 17:08:20 +01:00
|
|
|
|
2014-12-06 16:44:04 +01:00
|
|
|
local timer = { mt = {} }
|
|
|
|
|
|
|
|
--- Start the timer.
|
|
|
|
function timer:start()
|
|
|
|
if self.data.source_id ~= nil then
|
|
|
|
print(traceback("timer already started"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.data.source_id = glib.timeout_add(glib.PRIORITY_DEFAULT, self.data.timeout * 1000, function()
|
2016-02-28 12:28:55 +01:00
|
|
|
protected_call(self.emit_signal, self, "timeout")
|
2014-12-06 16:44:04 +01:00
|
|
|
return true
|
|
|
|
end)
|
2015-10-18 15:31:52 +02:00
|
|
|
self:emit_signal("start")
|
2014-12-06 16:44:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Stop the timer.
|
|
|
|
function timer:stop()
|
|
|
|
if self.data.source_id == nil then
|
|
|
|
print(traceback("timer not started"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
glib.source_remove(self.data.source_id)
|
|
|
|
self.data.source_id = nil
|
2015-10-18 15:31:52 +02:00
|
|
|
self:emit_signal("stop")
|
2014-12-06 16:44:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Restart the timer.
|
2015-11-01 17:08:20 +01:00
|
|
|
-- This is equivalent to stopping the timer if it is running and then starting
|
|
|
|
-- it.
|
2014-12-06 16:44:04 +01:00
|
|
|
function timer:again()
|
|
|
|
if self.data.source_id ~= nil then
|
|
|
|
self:stop()
|
|
|
|
end
|
|
|
|
self:start()
|
|
|
|
end
|
|
|
|
|
2016-08-13 09:13:25 +02:00
|
|
|
--- The timer is started.
|
|
|
|
-- @property started
|
|
|
|
-- @param boolean
|
|
|
|
|
|
|
|
--- The timer timeout value.
|
|
|
|
-- **Signal:** property::timeout
|
|
|
|
-- @property timeout
|
|
|
|
-- @param number
|
|
|
|
|
2014-12-06 16:44:04 +01:00
|
|
|
local timer_instance_mt = {
|
|
|
|
__index = function(self, property)
|
|
|
|
if property == "timeout" then
|
|
|
|
return self.data.timeout
|
|
|
|
elseif property == "started" then
|
|
|
|
return self.data.source_id ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return timer[property]
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function(self, property, value)
|
|
|
|
if property == "timeout" then
|
|
|
|
self.data.timeout = tonumber(value)
|
|
|
|
self:emit_signal("property::timeout")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2015-02-15 22:26:47 +01:00
|
|
|
--- Create a new timer object.
|
|
|
|
-- @tparam table args Arguments.
|
|
|
|
-- @tparam number args.timeout Timeout in seconds (e.g. 1.5).
|
|
|
|
-- @treturn timer
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @function gears.timer
|
2015-02-15 22:26:47 +01:00
|
|
|
timer.new = function(args)
|
2014-12-06 16:44:04 +01:00
|
|
|
local ret = object()
|
|
|
|
|
|
|
|
ret.data = { timeout = 0 }
|
|
|
|
setmetatable(ret, timer_instance_mt)
|
|
|
|
|
|
|
|
for k, v in pairs(args) do
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2015-08-11 13:21:32 +02:00
|
|
|
--- Create a timeout for calling some callback function.
|
|
|
|
-- When the callback function returns true, it will be called again after the
|
|
|
|
-- same timeout. If false is returned, no more calls will be done. If the
|
|
|
|
-- callback function causes an error, no more calls are done.
|
|
|
|
-- @tparam number timeout Timeout in seconds (e.g. 1.5).
|
|
|
|
-- @tparam function callback Function to run.
|
2015-11-01 17:08:20 +01:00
|
|
|
-- @treturn timer The timer object that was set up.
|
2015-08-11 13:21:32 +02:00
|
|
|
-- @see timer.weak_start_new
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @function gears.timer.start_new
|
2015-08-11 13:21:32 +02:00
|
|
|
function timer.start_new(timeout, callback)
|
|
|
|
local t = timer.new({ timeout = timeout })
|
|
|
|
t:connect_signal("timeout", function()
|
2016-02-28 12:28:55 +01:00
|
|
|
local cont = protected_call(callback)
|
|
|
|
if not cont then
|
2015-08-11 13:21:32 +02:00
|
|
|
t:stop()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
t:start()
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a timeout for calling some callback function.
|
|
|
|
-- This function is almost identical to `timer.start_new`. The only difference
|
|
|
|
-- is that this does not prevent the callback function from being garbage
|
|
|
|
-- collected. After the callback function was collected, the timer returned
|
|
|
|
-- will automatically be stopped.
|
|
|
|
-- @tparam number timeout Timeout in seconds (e.g. 1.5).
|
|
|
|
-- @tparam function callback Function to start.
|
2015-11-01 17:08:20 +01:00
|
|
|
-- @treturn timer The timer object that was set up.
|
2015-08-11 13:21:32 +02:00
|
|
|
-- @see timer.start_new
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @function gears.timer.weak_start_new
|
2015-08-11 13:21:32 +02:00
|
|
|
function timer.weak_start_new(timeout, callback)
|
|
|
|
local indirection = setmetatable({}, { __mode = "v" })
|
|
|
|
indirection.callback = callback
|
|
|
|
return timer.start_new(timeout, function()
|
|
|
|
local cb = indirection.callback
|
|
|
|
if cb then
|
|
|
|
return cb()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2015-01-11 10:58:49 +01:00
|
|
|
local delayed_calls = {}
|
|
|
|
capi.awesome.connect_signal("refresh", function()
|
|
|
|
for _, callback in ipairs(delayed_calls) do
|
2016-02-28 12:28:55 +01:00
|
|
|
protected_call(unpack(callback))
|
2015-01-11 10:58:49 +01:00
|
|
|
end
|
|
|
|
delayed_calls = {}
|
|
|
|
end)
|
|
|
|
|
|
|
|
--- Call the given function at the end of the current main loop iteration
|
2015-02-15 22:26:47 +01:00
|
|
|
-- @tparam function callback The function that should be called
|
2015-01-11 10:58:49 +01:00
|
|
|
-- @param ... Arguments to the callback function
|
2016-08-13 09:13:25 +02:00
|
|
|
-- @function gears.timer.delayed_call
|
2015-01-11 10:58:49 +01:00
|
|
|
function timer.delayed_call(callback, ...)
|
|
|
|
assert(type(callback) == "function", "callback must be a function, got: " .. type(callback))
|
|
|
|
table.insert(delayed_calls, { callback, ... })
|
|
|
|
end
|
|
|
|
|
2016-02-07 13:29:46 +01:00
|
|
|
function timer.mt.__call(_, ...)
|
2015-02-15 22:26:47 +01:00
|
|
|
return timer.new(...)
|
2014-12-06 16:44:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(timer, timer.mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|