diff --git a/CMakeLists.txt b/CMakeLists.txt index 37142e61..e97361f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,6 @@ set(AWE_SRCS ${SOURCE_DIR}/objects/key.c ${SOURCE_DIR}/objects/screen.c ${SOURCE_DIR}/objects/tag.c - ${SOURCE_DIR}/objects/timer.c ${SOURCE_DIR}/objects/window.c) set(AWE_MAN_SRCS diff --git a/lib/awful/menu.lua.in b/lib/awful/menu.lua.in index eccf3e88..9943c4fb 100644 --- a/lib/awful/menu.lua.in +++ b/lib/awful/menu.lua.in @@ -26,7 +26,6 @@ local table = table local type = type local math = math local capi = { - timer = timer, screen = screen, mouse = mouse, client = client } diff --git a/lib/awful/mouse/finder.lua.in b/lib/awful/mouse/finder.lua.in index 7420a118..342c4852 100644 --- a/lib/awful/mouse/finder.lua.in +++ b/lib/awful/mouse/finder.lua.in @@ -6,7 +6,7 @@ local mouse = mouse local screen = screen -local timer = timer +local timer = require("gears.timer") local wibox = require("wibox") local a_placement = require("awful.placement") local a_wibox = require("awful.wibox") diff --git a/lib/awful/tooltip.lua.in b/lib/awful/tooltip.lua.in index 66132c09..98188a05 100644 --- a/lib/awful/tooltip.lua.in +++ b/lib/awful/tooltip.lua.in @@ -6,7 +6,7 @@ local mouse = mouse local screen = screen -local timer = timer +local timer = require("gears.timer") local wibox = require("wibox") local a_placement = require("awful.placement") local beautiful = require("beautiful") diff --git a/lib/awful/widget/textclock.lua.in b/lib/awful/widget/textclock.lua.in index df04f4d2..bf8efffc 100644 --- a/lib/awful/widget/textclock.lua.in +++ b/lib/awful/widget/textclock.lua.in @@ -7,7 +7,7 @@ local setmetatable = setmetatable local os = os local textbox = require("wibox.widget.textbox") -local capi = { timer = timer } +local timer = require("gears.timer") --- Text clock widget. -- awful.widget.textclock @@ -22,10 +22,10 @@ function textclock.new(format, timeout) local timeout = timeout or 60 local w = textbox() - local timer = capi.timer { timeout = timeout } - timer:connect_signal("timeout", function() w:set_markup(os.date(format)) end) - timer:start() - timer:emit_signal("timeout") + local t = timer { timeout = timeout } + t:connect_signal("timeout", function() w:set_markup(os.date(format)) end) + t:start() + t:emit_signal("timeout") return w end diff --git a/lib/gears/init.lua.in b/lib/gears/init.lua.in index fa2b15e1..9f6b8e7f 100644 --- a/lib/gears/init.lua.in +++ b/lib/gears/init.lua.in @@ -14,6 +14,7 @@ return sort = require("gears.sort"); surface = require("gears.surface"); wallpaper = require("gears.wallpaper"); + timer = require("gears.timer"); } -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/lib/gears/timer.lua.in b/lib/gears/timer.lua.in new file mode 100644 index 00000000..f0e49d3c --- /dev/null +++ b/lib/gears/timer.lua.in @@ -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 diff --git a/lib/naughty.lua.in b/lib/naughty.lua.in index 6c3528e5..dd230ab9 100644 --- a/lib/naughty.lua.in +++ b/lib/naughty.lua.in @@ -14,8 +14,8 @@ local pcall = pcall local capi = { screen = screen, awesome = awesome, dbus = dbus, - timer = timer, awesome = awesome } +local timer = require("gears.timer") local button = require("awful.button") local util = require("awful.util") local bt = require("beautiful") @@ -342,7 +342,7 @@ function naughty.notify(args) -- hook destroy local die = function () naughty.destroy(notification) end if timeout > 0 then - local timer_die = capi.timer { timeout = timeout } + local timer_die = timer { timeout = timeout } timer_die:connect_signal("timeout", die) if not suspended then timer_die:start() @@ -364,7 +364,7 @@ function naughty.notify(args) die() else 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:start() end diff --git a/luaa.c b/luaa.c index c2f93283..1a2aef78 100644 --- a/luaa.c +++ b/luaa.c @@ -33,7 +33,6 @@ #include "objects/drawin.h" #include "objects/screen.h" #include "objects/tag.h" -#include "objects/timer.h" #include "property.h" #include "selection.h" #include "spawn.h" @@ -425,9 +424,6 @@ luaA_init(xdgHandle* xdg) /* Export keys */ key_class_setup(L); - /* Export timer */ - timer_class_setup(L); - /* add Lua search paths */ lua_getglobal(L, "package"); if (LUA_TTABLE != lua_type(L, 1)) diff --git a/luadoc/timer.lua b/luadoc/timer.lua deleted file mode 100644 index 51edcc29..00000000 --- a/luadoc/timer.lua +++ /dev/null @@ -1,47 +0,0 @@ ---- awesome timer API --- @author Julien Danjou <julien@danjou.info> --- @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 diff --git a/objects/timer.c b/objects/timer.c deleted file mode 100644 index c7ea5e31..00000000 --- a/objects/timer.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * timer.c - Timer signals management - * - * Copyright © 2009 Julien Danjou - * - * 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 diff --git a/objects/timer.h b/objects/timer.h deleted file mode 100644 index 89014295..00000000 --- a/objects/timer.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * timer.h - Timer signals management header - * - * Copyright © 2009 Julien Danjou - * - * 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 - -void timer_class_setup(lua_State *); - -#endif - -// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80