timer: Move from C to lua as gears.timer

No API-change (besides the obvious no-longer-a-global-object) intended.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2014-12-06 16:44:04 +01:00
parent e5120e1bec
commit 94e61ec94b
12 changed files with 111 additions and 249 deletions

View File

@ -72,7 +72,6 @@ set(AWE_SRCS
${SOURCE_DIR}/objects/key.c ${SOURCE_DIR}/objects/key.c
${SOURCE_DIR}/objects/screen.c ${SOURCE_DIR}/objects/screen.c
${SOURCE_DIR}/objects/tag.c ${SOURCE_DIR}/objects/tag.c
${SOURCE_DIR}/objects/timer.c
${SOURCE_DIR}/objects/window.c) ${SOURCE_DIR}/objects/window.c)
set(AWE_MAN_SRCS set(AWE_MAN_SRCS

View File

@ -26,7 +26,6 @@ local table = table
local type = type local type = type
local math = math local math = math
local capi = { local capi = {
timer = timer,
screen = screen, screen = screen,
mouse = mouse, mouse = mouse,
client = client } client = client }

View File

@ -6,7 +6,7 @@
local mouse = mouse local mouse = mouse
local screen = screen local screen = screen
local timer = timer local timer = require("gears.timer")
local wibox = require("wibox") local wibox = require("wibox")
local a_placement = require("awful.placement") local a_placement = require("awful.placement")
local a_wibox = require("awful.wibox") local a_wibox = require("awful.wibox")

View File

@ -6,7 +6,7 @@
local mouse = mouse local mouse = mouse
local screen = screen local screen = screen
local timer = timer local timer = require("gears.timer")
local wibox = require("wibox") local wibox = require("wibox")
local a_placement = require("awful.placement") local a_placement = require("awful.placement")
local beautiful = require("beautiful") local beautiful = require("beautiful")

View File

@ -7,7 +7,7 @@
local setmetatable = setmetatable local setmetatable = setmetatable
local os = os local os = os
local textbox = require("wibox.widget.textbox") local textbox = require("wibox.widget.textbox")
local capi = { timer = timer } local timer = require("gears.timer")
--- Text clock widget. --- Text clock widget.
-- awful.widget.textclock -- awful.widget.textclock
@ -22,10 +22,10 @@ function textclock.new(format, timeout)
local timeout = timeout or 60 local timeout = timeout or 60
local w = textbox() local w = textbox()
local timer = capi.timer { timeout = timeout } local t = timer { timeout = timeout }
timer:connect_signal("timeout", function() w:set_markup(os.date(format)) end) t:connect_signal("timeout", function() w:set_markup(os.date(format)) end)
timer:start() t:start()
timer:emit_signal("timeout") t:emit_signal("timeout")
return w return w
end end

View File

@ -14,6 +14,7 @@ return
sort = require("gears.sort"); sort = require("gears.sort");
surface = require("gears.surface"); surface = require("gears.surface");
wallpaper = require("gears.wallpaper"); wallpaper = require("gears.wallpaper");
timer = require("gears.timer");
} }
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

100
lib/gears/timer.lua.in Normal file
View File

@ -0,0 +1,100 @@
---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @copyright 2014 Uli Schlachter
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
local setmetatable = setmetatable
local pairs = pairs
local tonumber = tonumber
local traceback = debug.traceback
local glib = require("lgi").GLib
local object = require("gears.object")
--- 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
-- value.
-- @field timeout Interval in seconds to emit the timeout signal. Can be any
-- value, including floating point ones (i.e. 1.5 seconds).
-- @field started Read-only boolean field indicating if the timer has been
-- started.
-- @class table
-- @name timer
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()
local success, message = pcall(function()
self:emit_signal("timeout")
end)
assert(success, message)
return true
end)
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
end
--- Restart the timer.
function timer:again()
if self.data.source_id ~= nil then
self:stop()
end
self:start()
end
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
}
local function new(args)
local ret = object()
ret:add_signal("property::timeout")
ret:add_signal("timeout")
ret.data = { timeout = 0 }
setmetatable(ret, timer_instance_mt)
for k, v in pairs(args) do
ret[k] = v
end
return ret
end
function timer.mt:__call(...)
return new(...)
end
return setmetatable(timer, timer.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -14,8 +14,8 @@ local pcall = pcall
local capi = { screen = screen, local capi = { screen = screen,
awesome = awesome, awesome = awesome,
dbus = dbus, dbus = dbus,
timer = timer,
awesome = awesome } awesome = awesome }
local timer = require("gears.timer")
local button = require("awful.button") local button = require("awful.button")
local util = require("awful.util") local util = require("awful.util")
local bt = require("beautiful") local bt = require("beautiful")
@ -342,7 +342,7 @@ function naughty.notify(args)
-- hook destroy -- hook destroy
local die = function () naughty.destroy(notification) end local die = function () naughty.destroy(notification) end
if timeout > 0 then if timeout > 0 then
local timer_die = capi.timer { timeout = timeout } local timer_die = timer { timeout = timeout }
timer_die:connect_signal("timeout", die) timer_die:connect_signal("timeout", die)
if not suspended then if not suspended then
timer_die:start() timer_die:start()
@ -364,7 +364,7 @@ function naughty.notify(args)
die() die()
else else
if notification.timer then notification.timer:stop() end if notification.timer then notification.timer:stop() end
notification.timer = capi.timer { timeout = hover_timeout } notification.timer = timer { timeout = hover_timeout }
notification.timer:connect_signal("timeout", die) notification.timer:connect_signal("timeout", die)
notification.timer:start() notification.timer:start()
end end

4
luaa.c
View File

@ -33,7 +33,6 @@
#include "objects/drawin.h" #include "objects/drawin.h"
#include "objects/screen.h" #include "objects/screen.h"
#include "objects/tag.h" #include "objects/tag.h"
#include "objects/timer.h"
#include "property.h" #include "property.h"
#include "selection.h" #include "selection.h"
#include "spawn.h" #include "spawn.h"
@ -425,9 +424,6 @@ luaA_init(xdgHandle* xdg)
/* Export keys */ /* Export keys */
key_class_setup(L); key_class_setup(L);
/* Export timer */
timer_class_setup(L);
/* add Lua search paths */ /* add Lua search paths */
lua_getglobal(L, "package"); lua_getglobal(L, "package");
if (LUA_TTABLE != lua_type(L, 1)) if (LUA_TTABLE != lua_type(L, 1))

View File

@ -1,47 +0,0 @@
--- awesome timer API
-- @author Julien Danjou &lt;julien@danjou.info&gt;
-- @copyright 2009 Julien Danjou
module("timer")
--- Timer object. This type of object is useful when triggering events in a repeatedly.
-- The timer will emit the "timeout" signal every N seconds, N being the timeout value.
-- @field timeout Interval in seconds to emit the timeout signal.
-- Can be any value, including floating ones (i.e. 1.5 second).
-- @field started Read-only boolean field indicating if the timer has been started.
-- @class table
-- @name timer
--- Start the timer.
-- @name start
-- @class function
--- Stop the timer.
-- @name stop
-- @class function
--- Restart the timer.
-- @name again
-- @class function
--- Add a signal.
-- @param name A signal name.
-- @param func A function to call when the signal is emitted.
-- @name connect_signal
-- @class function
--- Remove a signal.
-- @param name A signal name.
-- @param func A function to remove.
-- @name disconnect_signal
-- @class function
--- Emit a signal.
-- @param name A signal name.
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of timer objects alive.
-- @name instances
-- @class function

View File

@ -1,155 +0,0 @@
/*
* timer.c - Timer signals management
*
* Copyright © 2009 Julien Danjou <julien@danjou.info>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "timer.h"
#include "common/luaobject.h"
#include "globalconf.h"
#include "luaa.h"
typedef struct
{
LUA_OBJECT_HEADER
bool started;
guint source_id;
double timeout;
} atimer_t;
static lua_class_t timer_class;
LUA_OBJECT_FUNCS(timer_class, atimer_t, timer)
static gboolean
timer_emit_signal(gpointer data)
{
lua_State *L = globalconf_get_lua_State();
luaA_object_push(L, data);
luaA_object_emit_signal(L, -1, "timeout", 0);
lua_pop(L, 1);
return TRUE;
}
static int
luaA_timer_new(lua_State *L)
{
luaA_class_new(L, &timer_class);
return 1;
}
static int
luaA_timer_set_timeout(lua_State *L, atimer_t *timer)
{
double timeout = luaL_checknumber(L, -1);
timer->timeout = timeout;
luaA_object_emit_signal(L, -3, "property::timeout", 0);
return 0;
}
static int
luaA_timer_get_timeout(lua_State *L, atimer_t *timer)
{
lua_pushnumber(L, timer->timeout);
return 1;
}
static int
luaA_timer_start(lua_State *L)
{
atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
if(timer->started)
luaA_warn(L, "timer already started");
else
{
luaA_object_ref(L, 1);
timer->started = true;
timer->source_id = g_timeout_add(timer->timeout * 1000, timer_emit_signal, timer);
}
return 0;
}
static int
luaA_timer_stop(lua_State *L)
{
atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
if(timer->started)
{
g_source_remove(timer->source_id);
luaA_object_unref(L, timer);
timer->started = false;
}
else
luaA_warn(L, "timer not started");
return 0;
}
static int
luaA_timer_again(lua_State *L)
{
atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
if (timer->started)
g_source_remove(timer->source_id);
else
luaA_object_ref(L, 1);
timer->started = true;
timer->source_id = g_timeout_add(timer->timeout * 1000, timer_emit_signal, timer);
return 0;
}
LUA_OBJECT_EXPORT_PROPERTY(timer, atimer_t, started, lua_pushboolean)
void
timer_class_setup(lua_State *L)
{
static const struct luaL_Reg timer_methods[] =
{
LUA_CLASS_METHODS(timer)
{ "__call", luaA_timer_new },
{ NULL, NULL }
};
static const struct luaL_Reg timer_meta[] =
{
LUA_OBJECT_META(timer)
LUA_CLASS_META
{ "start", luaA_timer_start },
{ "stop", luaA_timer_stop },
{ "again", luaA_timer_again },
{ NULL, NULL },
};
luaA_class_setup(L, &timer_class, "timer", NULL,
(lua_class_allocator_t) timer_new, NULL, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
timer_methods, timer_meta);
luaA_class_add_property(&timer_class, "timeout",
(lua_class_propfunc_t) luaA_timer_set_timeout,
(lua_class_propfunc_t) luaA_timer_get_timeout,
(lua_class_propfunc_t) luaA_timer_set_timeout);
luaA_class_add_property(&timer_class, "started",
NULL,
(lua_class_propfunc_t) luaA_timer_get_started,
NULL);
signal_add(&timer_class.signals, "property::timeout");
signal_add(&timer_class.signals, "timeout");
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -1,31 +0,0 @@
/*
* timer.h - Timer signals management header
*
* Copyright © 2009 Julien Danjou <julien@danjou.info>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef AWESOME_OBJECTS_TIMER_H
#define AWESOME_OBJECTS_TIMER_H
#include <lua.h>
void timer_class_setup(lua_State *);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80