change codename
[awesome.git] / timer.c
bloba0419e4d7d69859ad11192632c527fd2588c5f98
1 /*
2 * timer.c - Timer signals management
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <ev.h>
24 #include "globalconf.h"
25 #include "luaa.h"
26 #include "timer.h"
27 #include "common/luaobject.h"
29 typedef struct
31 LUA_OBJECT_HEADER
32 bool started;
33 struct ev_timer timer;
34 } atimer_t;
36 static lua_class_t timer_class;
37 LUA_OBJECT_FUNCS(timer_class, atimer_t, timer)
39 static void
40 ev_timer_emit_signal(struct ev_loop *loop, struct ev_timer *w, int revents)
42 luaA_object_push(globalconf.L, w->data);
43 luaA_object_emit_signal(globalconf.L, -1, "timeout", 0);
44 lua_pop(globalconf.L, 1);
47 static int
48 luaA_timer_new(lua_State *L)
50 luaA_class_new(L, &timer_class);
51 atimer_t *timer = luaA_checkudata(L, -1, &timer_class);
52 timer->timer.data = timer;
53 ev_set_cb(&timer->timer, ev_timer_emit_signal);
54 return 1;
57 static int
58 luaA_timer_set_timeout(lua_State *L, atimer_t *timer)
60 double timeout = luaL_checknumber(L, -1);
61 ev_timer_set(&timer->timer, timeout, timeout);
62 luaA_object_emit_signal(L, -3, "property::timeout", 0);
63 return 0;
66 static int
67 luaA_timer_get_timeout(lua_State *L, atimer_t *timer)
69 lua_pushnumber(L, timer->timer.repeat);
70 return 1;
73 static int
74 luaA_timer_start(lua_State *L)
76 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
77 if(timer->started)
78 luaA_warn(L, "timer already started");
79 else
81 luaA_object_ref(L, 1);
82 ev_timer_start(globalconf.loop, &timer->timer);
83 timer->started = true;
85 return 0;
88 static int
89 luaA_timer_stop(lua_State *L)
91 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
92 if(timer->started)
94 ev_timer_stop(globalconf.loop, &timer->timer);
95 luaA_object_unref(L, timer);
96 timer->started = false;
98 else
99 luaA_warn(L, "timer not started");
100 return 0;
103 static int
104 luaA_timer_again(lua_State *L)
106 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
108 ev_timer_again(globalconf.loop, &timer->timer);
110 if(!timer->started)
112 luaA_object_ref(L, 1);
113 timer->started = true;
116 return 0;
119 LUA_OBJECT_EXPORT_PROPERTY(timer, atimer_t, started, lua_pushboolean)
121 void
122 timer_class_setup(lua_State *L)
124 static const struct luaL_reg timer_methods[] =
126 LUA_CLASS_METHODS(timer)
127 { "__call", luaA_timer_new },
128 { NULL, NULL }
131 static const struct luaL_reg timer_meta[] =
133 LUA_OBJECT_META(timer)
134 LUA_CLASS_META
135 { "start", luaA_timer_start },
136 { "stop", luaA_timer_stop },
137 { "again", luaA_timer_again },
138 { "__gc", luaA_object_gc },
139 { NULL, NULL },
142 luaA_class_setup(L, &timer_class, "timer", (lua_class_allocator_t) timer_new,
143 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
144 timer_methods, timer_meta);
145 luaA_class_add_property(&timer_class, A_TK_TIMEOUT,
146 (lua_class_propfunc_t) luaA_timer_set_timeout,
147 (lua_class_propfunc_t) luaA_timer_get_timeout,
148 (lua_class_propfunc_t) luaA_timer_set_timeout);
149 luaA_class_add_property(&timer_class, A_TK_STARTED,
150 NULL,
151 (lua_class_propfunc_t) luaA_timer_get_started,
152 NULL);
155 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80