From 432f4229be5da97fe5b332e823ad7944acd63bb6 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 30 Jun 2009 12:10:30 +0200 Subject: [PATCH] timer: import Signed-off-by: Julien Danjou --- CMakeLists.txt | 1 + common/tokenize.gperf | 1 + luaa.c | 4 ++ timer.c | 130 ++++++++++++++++++++++++++++++++++++++++++ timer.h | 31 ++++++++++ 5 files changed, 167 insertions(+) create mode 100644 timer.c create mode 100644 timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a196a7ae..01902fc0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ set(AWE_SRCS ${SOURCE_DIR}/image.c ${SOURCE_DIR}/draw.c ${SOURCE_DIR}/color.c + ${SOURCE_DIR}/timer.c ${SOURCE_DIR}/common/buffer.c ${SOURCE_DIR}/common/atoms.c ${SOURCE_DIR}/common/util.c diff --git a/common/tokenize.gperf b/common/tokenize.gperf index d8dc02d28..aa4d4d3dc 100644 --- a/common/tokenize.gperf +++ b/common/tokenize.gperf @@ -103,6 +103,7 @@ text textbox ticks_count ticks_gap +timeout titlebar top transient_for diff --git a/luaa.c b/luaa.c index bd877c9c0..a61733fcb 100644 --- a/luaa.c +++ b/luaa.c @@ -30,6 +30,7 @@ #include #include "awesome.h" +#include "timer.h" #include "awesome-version-internal.h" #include "ewmh.h" #include "luaa.h" @@ -768,6 +769,9 @@ luaA_init(xdgHandle* xdg) luaA_class_setup(L, &key_class, "key", (lua_class_allocator_t) key_new, awesome_key_methods, awesome_key_meta); + /* Export timer */ + timer_class_setup(L); + /* init hooks */ globalconf.hooks.manage = LUA_REFNIL; globalconf.hooks.unmanage = LUA_REFNIL; diff --git a/timer.c b/timer.c new file mode 100644 index 000000000..b0857a25c --- /dev/null +++ b/timer.c @@ -0,0 +1,130 @@ +/* + * 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 + +#include "structs.h" +#include "luaa.h" +#include "timer.h" + +typedef struct +{ + LUA_OBJECT_HEADER + bool started; + struct ev_timer timer; +} atimer_t; + +static lua_class_t timer_class; +LUA_OBJECT_FUNCS(timer_class, atimer_t, timer, "timer") + +static void +ev_timer_emit_signal(struct ev_loop *loop, struct ev_timer *w, int revents) +{ + luaA_object_push(globalconf.L, w->data); + luaA_object_emit_signal(globalconf.L, -1, "timeout", 0); + lua_pop(globalconf.L, 1); +} + +static int +luaA_timer_new(lua_State *L) +{ + luaA_class_new(L, &timer_class); + atimer_t *timer = luaL_checkudata(L, -1, "timer"); + timer->timer.data = timer; + ev_set_cb(&timer->timer, ev_timer_emit_signal); + return 1; +} + +static int +luaA_timer_set_timeout(lua_State *L, atimer_t *timer) +{ + double timeout = luaL_checknumber(L, -1); + ev_timer_set(&timer->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->timer.repeat); + return 1; +} + +static int +luaA_timer_start(lua_State *L) +{ + atimer_t *timer = luaL_checkudata(L, 1, "timer"); + if(timer->started) + luaA_warn(L, "timer already started"); + else + { + luaA_object_ref(L, 1); + ev_timer_start(globalconf.loop, &timer->timer); + timer->started = true; + } + return 0; +} + +static int +luaA_timer_stop(lua_State *L) +{ + atimer_t *timer = luaL_checkudata(L, 1, "timer"); + if(timer->started) + { + ev_timer_stop(globalconf.loop, &timer->timer); + luaA_object_unref(L, timer); + timer->started = false; + } + else + luaA_warn(L, "timer not started"); + return 0; +} + +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 }, + { "__gc", luaA_object_gc }, + { NULL, NULL }, + }; + + luaA_class_setup(L, &timer_class, "timer", (lua_class_allocator_t) timer_new, + timer_methods, timer_meta); + luaA_class_add_property(&timer_class, A_TK_TIMEOUT, "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); +} + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/timer.h b/timer.h new file mode 100644 index 000000000..ded85f64b --- /dev/null +++ b/timer.h @@ -0,0 +1,31 @@ +/* + * 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_TIMER +#define AWESOME_TIMER + +#include + +void timer_class_setup(lua_State *); + +#endif + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80